blob: 39b2058845f5786fc2273d0ee7d30fb277547538 [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>
NeilBrownbff61972009-03-31 14:33:13 +110024#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100025#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110026#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110027#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110028#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110029#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * RAID10 provides a combination of RAID0 and RAID1 functionality.
33 * The layout of data is defined by
34 * chunk_size
35 * raid_disks
36 * near_copies (stored in low byte of layout)
37 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070038 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 *
40 * The data to be stored is divided into chunks using chunksize.
41 * Each device is divided into far_copies sections.
42 * In each section, chunks are laid out in a style similar to raid0, but
43 * near_copies copies of each chunk is stored (each on a different drive).
44 * The starting device for each section is offset near_copies from the starting
45 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070046 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * drive.
48 * near_copies and far_copies must be at least one, and their product is at most
49 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070050 *
51 * If far_offset is true, then the far_copies are handled a bit differently.
52 * The copies are still in different stripes, but instead of be very far apart
53 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
56/*
57 * Number of guaranteed r10bios in case of extreme VM load:
58 */
59#define NR_RAID10_BIOS 256
60
NeilBrown0a27ec92006-01-06 00:20:13 -080061static void allow_barrier(conf_t *conf);
62static void lower_barrier(conf_t *conf);
63
Al Virodd0fc662005-10-07 07:46:04 +010064static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 conf_t *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int size = offsetof(struct r10bio_s, devs[conf->copies]);
68
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010070 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void r10bio_pool_free(void *r10_bio, void *data)
74{
75 kfree(r10_bio);
76}
77
NeilBrown0310fa22008-08-05 15:54:14 +100078/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100081/* amount of memory to reserve for resync requests */
82#define RESYNC_WINDOW (1024*1024)
83/* maximum number of concurrent requests, memory permitting */
84#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * When performing a resync, we need to read and compare, so
88 * we need as many pages are there are copies.
89 * When performing a recovery, we need 2 bios, one for read,
90 * one for write (we recover only one drive per r10buf)
91 *
92 */
Al Virodd0fc662005-10-07 07:46:04 +010093static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 conf_t *conf = data;
96 struct page *page;
97 r10bio_t *r10_bio;
98 struct bio *bio;
99 int i, j;
100 int nalloc;
101
102 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100103 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
107 nalloc = conf->copies; /* resync */
108 else
109 nalloc = 2; /* recovery */
110
111 /*
112 * Allocate bios.
113 */
114 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100115 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!bio)
117 goto out_free_bio;
118 r10_bio->devs[j].bio = bio;
119 }
120 /*
121 * Allocate RESYNC_PAGES data pages and attach them
122 * where needed.
123 */
124 for (j = 0 ; j < nalloc; j++) {
125 bio = r10_bio->devs[j].bio;
126 for (i = 0; i < RESYNC_PAGES; i++) {
Namhyung Kimc65060a2011-07-18 17:38:49 +1000127 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
128 &conf->mddev->recovery)) {
129 /* we can share bv_page's during recovery */
130 struct bio *rbio = r10_bio->devs[0].bio;
131 page = rbio->bi_io_vec[i].bv_page;
132 get_page(page);
133 } else
134 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (unlikely(!page))
136 goto out_free_pages;
137
138 bio->bi_io_vec[i].bv_page = page;
139 }
140 }
141
142 return r10_bio;
143
144out_free_pages:
145 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800146 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 while (j--)
148 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800149 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 j = -1;
151out_free_bio:
152 while ( ++j < nalloc )
153 bio_put(r10_bio->devs[j].bio);
154 r10bio_pool_free(r10_bio, conf);
155 return NULL;
156}
157
158static void r10buf_pool_free(void *__r10_bio, void *data)
159{
160 int i;
161 conf_t *conf = data;
162 r10bio_t *r10bio = __r10_bio;
163 int j;
164
165 for (j=0; j < conf->copies; j++) {
166 struct bio *bio = r10bio->devs[j].bio;
167 if (bio) {
168 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800169 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 bio->bi_io_vec[i].bv_page = NULL;
171 }
172 bio_put(bio);
173 }
174 }
175 r10bio_pool_free(r10bio, conf);
176}
177
178static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
179{
180 int i;
181
182 for (i = 0; i < conf->copies; i++) {
183 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000184 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 bio_put(*bio);
186 *bio = NULL;
187 }
188}
189
Arjan van de Ven858119e2006-01-14 13:20:43 -0800190static void free_r10bio(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
NeilBrown070ec552009-06-16 16:54:21 +1000192 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 put_all_bios(conf, r10_bio);
195 mempool_free(r10_bio, conf->r10bio_pool);
196}
197
Arjan van de Ven858119e2006-01-14 13:20:43 -0800198static void put_buf(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
NeilBrown070ec552009-06-16 16:54:21 +1000200 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 mempool_free(r10_bio, conf->r10buf_pool);
203
NeilBrown0a27ec92006-01-06 00:20:13 -0800204 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207static void reschedule_retry(r10bio_t *r10_bio)
208{
209 unsigned long flags;
210 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +1000211 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 spin_lock_irqsave(&conf->device_lock, flags);
214 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800215 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 spin_unlock_irqrestore(&conf->device_lock, flags);
217
Arthur Jones388667b2008-07-25 12:03:38 -0700218 /* wake up frozen array... */
219 wake_up(&conf->wait_barrier);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 md_wakeup_thread(mddev->thread);
222}
223
224/*
225 * raid_end_bio_io() is called when we have finished servicing a mirrored
226 * operation and are ready to return a success/failure code to the buffer
227 * cache layer.
228 */
229static void raid_end_bio_io(r10bio_t *r10_bio)
230{
231 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000232 int done;
233 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
NeilBrown856e08e2011-07-28 11:39:23 +1000235 if (bio->bi_phys_segments) {
236 unsigned long flags;
237 spin_lock_irqsave(&conf->device_lock, flags);
238 bio->bi_phys_segments--;
239 done = (bio->bi_phys_segments == 0);
240 spin_unlock_irqrestore(&conf->device_lock, flags);
241 } else
242 done = 1;
243 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
244 clear_bit(BIO_UPTODATE, &bio->bi_flags);
245 if (done) {
246 bio_endio(bio, 0);
247 /*
248 * Wake up any possible resync thread that waits for the device
249 * to go idle.
250 */
251 allow_barrier(conf);
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 free_r10bio(r10_bio);
254}
255
256/*
257 * Update disk head position estimator based on IRQ completion info.
258 */
259static inline void update_head_pos(int slot, r10bio_t *r10_bio)
260{
NeilBrown070ec552009-06-16 16:54:21 +1000261 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
264 r10_bio->devs[slot].addr + (r10_bio->sectors);
265}
266
Namhyung Kim778ca012011-07-18 17:38:47 +1000267/*
268 * Find the disk number which triggered given bio
269 */
NeilBrown749c55e2011-07-28 11:39:24 +1000270static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio,
271 struct bio *bio, int *slotp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000272{
273 int slot;
274
275 for (slot = 0; slot < conf->copies; slot++)
276 if (r10_bio->devs[slot].bio == bio)
277 break;
278
279 BUG_ON(slot == conf->copies);
280 update_head_pos(slot, r10_bio);
281
NeilBrown749c55e2011-07-28 11:39:24 +1000282 if (slotp)
283 *slotp = slot;
Namhyung Kim778ca012011-07-18 17:38:47 +1000284 return r10_bio->devs[slot].devnum;
285}
286
NeilBrown6712ecf2007-09-27 12:47:43 +0200287static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100290 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000292 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 slot = r10_bio->read_slot;
296 dev = r10_bio->devs[slot].devnum;
297 /*
298 * this branch is our 'one mirror IO has finished' event handler:
299 */
NeilBrown4443ae12006-01-06 00:20:28 -0800300 update_head_pos(slot, r10_bio);
301
302 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /*
304 * Set R10BIO_Uptodate in our master bio, so that
305 * we will return a good error code to the higher
306 * levels even if IO on some other mirrored buffer fails.
307 *
308 * The 'master' represents the composite IO operation to
309 * user-side. So if something waits for IO, then it will
310 * wait for the 'master' bio.
311 */
312 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 raid_end_bio_io(r10_bio);
NeilBrown7c4e06f2011-05-11 14:53:17 +1000314 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800315 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000317 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
319 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000320 printk_ratelimited(KERN_ERR
321 "md/raid10:%s: %s: rescheduling sector %llu\n",
322 mdname(conf->mddev),
323 bdevname(conf->mirrors[dev].rdev->bdev, b),
324 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000325 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 reschedule_retry(r10_bio);
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
NeilBrown6712ecf2007-09-27 12:47:43 +0200330static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100333 r10bio_t *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000334 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000335 int dec_rdev = 1;
NeilBrown070ec552009-06-16 16:54:21 +1000336 conf_t *conf = r10_bio->mddev->private;
NeilBrown749c55e2011-07-28 11:39:24 +1000337 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
NeilBrown749c55e2011-07-28 11:39:24 +1000339 dev = find_bio_disk(conf, r10_bio, bio, &slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * this branch is our 'one mirror IO has finished' event handler:
343 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800344 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800346 /* an I/O failed, we can't clear the bitmap */
347 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrown749c55e2011-07-28 11:39:24 +1000348 } else {
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 for 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 */
NeilBrown749c55e2011-07-28 11:39:24 +1000358 sector_t first_bad;
359 int bad_sectors;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 set_bit(R10BIO_Uptodate, &r10_bio->state);
362
NeilBrown749c55e2011-07-28 11:39:24 +1000363 /* Maybe we can clear some bad blocks. */
364 if (is_badblock(conf->mirrors[dev].rdev,
365 r10_bio->devs[slot].addr,
366 r10_bio->sectors,
367 &first_bad, &bad_sectors)) {
368 bio_put(bio);
369 r10_bio->devs[slot].bio = IO_MADE_GOOD;
370 dec_rdev = 0;
371 set_bit(R10BIO_MadeGood, &r10_bio->state);
372 }
373 }
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 *
377 * Let's see if all mirrored write operations have finished
378 * already.
379 */
380 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800381 /* clear the bitmap if all writes complete successfully */
382 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
383 r10_bio->sectors,
384 !test_bit(R10BIO_Degraded, &r10_bio->state),
385 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 md_write_end(r10_bio->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +1000387 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
388 reschedule_retry(r10_bio);
389 else
390 raid_end_bio_io(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
NeilBrown749c55e2011-07-28 11:39:24 +1000392 if (dec_rdev)
393 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396
397/*
398 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300399 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * parameters: near_copies and far_copies.
401 * near_copies * far_copies must be <= raid_disks.
402 * Normally one of these will be 1.
403 * If both are 1, we get raid0.
404 * If near_copies == raid_disks, we get raid1.
405 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300406 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * first chunk, followed by near_copies copies of the next chunk and
408 * so on.
409 * If far_copies > 1, then after 1/far_copies of the array has been assigned
410 * as described above, we start again with a device offset of near_copies.
411 * So we effectively have another copy of the whole array further down all
412 * the drives, but with blocks on different drives.
413 * With this layout, and block is never stored twice on the one device.
414 *
415 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700416 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
418 * raid10_find_virt does the reverse mapping, from a device and a
419 * sector offset to a virtual address
420 */
421
422static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
423{
424 int n,f;
425 sector_t sector;
426 sector_t chunk;
427 sector_t stripe;
428 int dev;
429
430 int slot = 0;
431
432 /* now calculate first sector/dev */
433 chunk = r10bio->sector >> conf->chunk_shift;
434 sector = r10bio->sector & conf->chunk_mask;
435
436 chunk *= conf->near_copies;
437 stripe = chunk;
438 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700439 if (conf->far_offset)
440 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 sector += stripe << conf->chunk_shift;
443
444 /* and calculate all the others */
445 for (n=0; n < conf->near_copies; n++) {
446 int d = dev;
447 sector_t s = sector;
448 r10bio->devs[slot].addr = sector;
449 r10bio->devs[slot].devnum = d;
450 slot++;
451
452 for (f = 1; f < conf->far_copies; f++) {
453 d += conf->near_copies;
454 if (d >= conf->raid_disks)
455 d -= conf->raid_disks;
456 s += conf->stride;
457 r10bio->devs[slot].devnum = d;
458 r10bio->devs[slot].addr = s;
459 slot++;
460 }
461 dev++;
462 if (dev >= conf->raid_disks) {
463 dev = 0;
464 sector += (conf->chunk_mask + 1);
465 }
466 }
467 BUG_ON(slot != conf->copies);
468}
469
470static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
471{
472 sector_t offset, chunk, vchunk;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700475 if (conf->far_offset) {
476 int fc;
477 chunk = sector >> conf->chunk_shift;
478 fc = sector_div(chunk, conf->far_copies);
479 dev -= fc * conf->near_copies;
480 if (dev < 0)
481 dev += conf->raid_disks;
482 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800483 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700484 sector -= conf->stride;
485 if (dev < conf->near_copies)
486 dev += conf->raid_disks - conf->near_copies;
487 else
488 dev -= conf->near_copies;
489 }
490 chunk = sector >> conf->chunk_shift;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 vchunk = chunk * conf->raid_disks + dev;
493 sector_div(vchunk, conf->near_copies);
494 return (vchunk << conf->chunk_shift) + offset;
495}
496
497/**
498 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
499 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200500 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * @biovec: the request that could be merged to it.
502 *
503 * Return amount of bytes we can accept at this offset
504 * If near_copies == raid_disk, there are no striping issues,
505 * but in that case, the function isn't called at all.
506 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200507static int raid10_mergeable_bvec(struct request_queue *q,
508 struct bvec_merge_data *bvm,
509 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200512 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000514 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200515 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
518 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200519 if (max <= biovec->bv_len && bio_sectors == 0)
520 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 else
522 return max;
523}
524
525/*
526 * This routine returns the disk from which the requested read should
527 * be done. There is a per-array 'next expected sequential IO' sector
528 * number - if this matches on the next IO then we use the last disk.
529 * There is also a per-disk 'last know head position' sector that is
530 * maintained from IRQ contexts, both the normal and the resync IO
531 * completion handlers update this position correctly. If there is no
532 * perfect sequential match then we pick the disk whose head is closest.
533 *
534 * If there are 2 mirrors in the same 2 devices, performance degrades
535 * because position is mirror, not device based.
536 *
537 * The rdev for the device selected will have nr_pending incremented.
538 */
539
540/*
541 * FIXME: possibly should rethink readbalancing and do it differently
542 * depending on near_copies / far_copies geometry.
543 */
NeilBrown856e08e2011-07-28 11:39:23 +1000544static int read_balance(conf_t *conf, r10bio_t *r10_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000546 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000547 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000548 int sectors = r10_bio->sectors;
549 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000550 sector_t new_distance, best_dist;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800551 mdk_rdev_t *rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000552 int do_balance;
553 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 raid10_find_phys(conf, r10_bio);
556 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000557retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000558 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000559 best_slot = -1;
560 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000561 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000562 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
564 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800565 * device if no resync is going on (recovery is ok), or below
566 * the resync window. We take the first readable disk when
567 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
569 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000570 && (this_sector + sectors >= conf->next_resync))
571 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
NeilBrown56d99122011-05-11 14:27:03 +1000573 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000574 sector_t first_bad;
575 int bad_sectors;
576 sector_t dev_sector;
577
NeilBrown56d99122011-05-11 14:27:03 +1000578 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000580 disk = r10_bio->devs[slot].devnum;
581 rdev = rcu_dereference(conf->mirrors[disk].rdev);
582 if (rdev == NULL)
583 continue;
584 if (!test_bit(In_sync, &rdev->flags))
585 continue;
586
NeilBrown856e08e2011-07-28 11:39:23 +1000587 dev_sector = r10_bio->devs[slot].addr;
588 if (is_badblock(rdev, dev_sector, sectors,
589 &first_bad, &bad_sectors)) {
590 if (best_dist < MaxSector)
591 /* Already have a better slot */
592 continue;
593 if (first_bad <= dev_sector) {
594 /* Cannot read here. If this is the
595 * 'primary' device, then we must not read
596 * beyond 'bad_sectors' from another device.
597 */
598 bad_sectors -= (dev_sector - first_bad);
599 if (!do_balance && sectors > bad_sectors)
600 sectors = bad_sectors;
601 if (best_good_sectors > sectors)
602 best_good_sectors = sectors;
603 } else {
604 sector_t good_sectors =
605 first_bad - dev_sector;
606 if (good_sectors > best_good_sectors) {
607 best_good_sectors = good_sectors;
608 best_slot = slot;
609 }
610 if (!do_balance)
611 /* Must read from here */
612 break;
613 }
614 continue;
615 } else
616 best_good_sectors = sectors;
617
NeilBrown56d99122011-05-11 14:27:03 +1000618 if (!do_balance)
619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
NeilBrown22dfdf52005-11-28 13:44:09 -0800621 /* This optimisation is debatable, and completely destroys
622 * sequential read speed for 'far copies' arrays. So only
623 * keep it for 'near' arrays, and review those later.
624 */
NeilBrown56d99122011-05-11 14:27:03 +1000625 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800627
628 /* for far > 1 always use the lowest address */
629 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000630 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800631 else
NeilBrown56d99122011-05-11 14:27:03 +1000632 new_distance = abs(r10_bio->devs[slot].addr -
633 conf->mirrors[disk].head_position);
634 if (new_distance < best_dist) {
635 best_dist = new_distance;
636 best_slot = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 }
NeilBrown56d99122011-05-11 14:27:03 +1000639 if (slot == conf->copies)
640 slot = best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
NeilBrown56d99122011-05-11 14:27:03 +1000642 if (slot >= 0) {
643 disk = r10_bio->devs[slot].devnum;
644 rdev = rcu_dereference(conf->mirrors[disk].rdev);
645 if (!rdev)
646 goto retry;
647 atomic_inc(&rdev->nr_pending);
648 if (test_bit(Faulty, &rdev->flags)) {
649 /* Cannot risk returning a device that failed
650 * before we inc'ed nr_pending
651 */
652 rdev_dec_pending(rdev, conf->mddev);
653 goto retry;
654 }
655 r10_bio->read_slot = slot;
656 } else
NeilBrown29fc7e32006-02-03 03:03:41 -0800657 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000659 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 return disk;
662}
663
NeilBrown0d129222006-10-03 01:15:54 -0700664static int raid10_congested(void *data, int bits)
665{
666 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000667 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700668 int i, ret = 0;
669
NeilBrown3fa841d2009-09-23 18:10:29 +1000670 if (mddev_congested(mddev, bits))
671 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700672 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100673 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700674 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
675 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200676 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700677
678 ret |= bdi_congested(&q->backing_dev_info, bits);
679 }
680 }
681 rcu_read_unlock();
682 return ret;
683}
684
Jens Axboe7eaceac2011-03-10 08:52:07 +0100685static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800686{
687 /* Any writes that have been queued but are awaiting
688 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800689 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800690 spin_lock_irq(&conf->device_lock);
691
692 if (conf->pending_bio_list.head) {
693 struct bio *bio;
694 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800695 spin_unlock_irq(&conf->device_lock);
696 /* flush any pending bitmap writes to disk
697 * before proceeding w/ I/O */
698 bitmap_unplug(conf->mddev->bitmap);
699
700 while (bio) { /* submit pending writes */
701 struct bio *next = bio->bi_next;
702 bio->bi_next = NULL;
703 generic_make_request(bio);
704 bio = next;
705 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800706 } else
707 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800708}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100709
NeilBrown0a27ec92006-01-06 00:20:13 -0800710/* Barriers....
711 * Sometimes we need to suspend IO while we do something else,
712 * either some resync/recovery, or reconfigure the array.
713 * To do this we raise a 'barrier'.
714 * The 'barrier' is a counter that can be raised multiple times
715 * to count how many activities are happening which preclude
716 * normal IO.
717 * We can only raise the barrier if there is no pending IO.
718 * i.e. if nr_pending == 0.
719 * We choose only to raise the barrier if no-one is waiting for the
720 * barrier to go down. This means that as soon as an IO request
721 * is ready, no other operations which require a barrier will start
722 * until the IO request has had a chance.
723 *
724 * So: regular IO calls 'wait_barrier'. When that returns there
725 * is no backgroup IO happening, It must arrange to call
726 * allow_barrier when it has finished its IO.
727 * backgroup IO calls must call raise_barrier. Once that returns
728 * there is no normal IO happeing. It must arrange to call
729 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
NeilBrown6cce3b22006-01-06 00:20:16 -0800732static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
NeilBrown6cce3b22006-01-06 00:20:16 -0800734 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
NeilBrown6cce3b22006-01-06 00:20:16 -0800737 /* Wait until no block IO is waiting (unless 'force') */
738 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000739 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800740
741 /* block any new IO from starting */
742 conf->barrier++;
743
NeilBrownc3b328a2011-04-18 18:25:43 +1000744 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800745 wait_event_lock_irq(conf->wait_barrier,
746 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000747 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_unlock_irq(&conf->resync_lock);
750}
751
NeilBrown0a27ec92006-01-06 00:20:13 -0800752static void lower_barrier(conf_t *conf)
753{
754 unsigned long flags;
755 spin_lock_irqsave(&conf->resync_lock, flags);
756 conf->barrier--;
757 spin_unlock_irqrestore(&conf->resync_lock, flags);
758 wake_up(&conf->wait_barrier);
759}
760
761static void wait_barrier(conf_t *conf)
762{
763 spin_lock_irq(&conf->resync_lock);
764 if (conf->barrier) {
765 conf->nr_waiting++;
766 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
767 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000768 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800769 conf->nr_waiting--;
770 }
771 conf->nr_pending++;
772 spin_unlock_irq(&conf->resync_lock);
773}
774
775static void allow_barrier(conf_t *conf)
776{
777 unsigned long flags;
778 spin_lock_irqsave(&conf->resync_lock, flags);
779 conf->nr_pending--;
780 spin_unlock_irqrestore(&conf->resync_lock, flags);
781 wake_up(&conf->wait_barrier);
782}
783
NeilBrown4443ae12006-01-06 00:20:28 -0800784static void freeze_array(conf_t *conf)
785{
786 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800787 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800788 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800789 * wait until nr_pending match nr_queued+1
790 * This is called in the context of one normal IO request
791 * that has failed. Thus any sync request that might be pending
792 * will be blocked by nr_pending, and we need to wait for
793 * pending IO requests to complete or be queued for re-try.
794 * Thus the number queued (nr_queued) plus this request (1)
795 * must match the number of pending IOs (nr_pending) before
796 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800797 */
798 spin_lock_irq(&conf->resync_lock);
799 conf->barrier++;
800 conf->nr_waiting++;
801 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800802 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800803 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000804 flush_pending_writes(conf));
805
NeilBrown4443ae12006-01-06 00:20:28 -0800806 spin_unlock_irq(&conf->resync_lock);
807}
808
809static void unfreeze_array(conf_t *conf)
810{
811 /* reverse the effect of the freeze */
812 spin_lock_irq(&conf->resync_lock);
813 conf->barrier--;
814 conf->nr_waiting--;
815 wake_up(&conf->wait_barrier);
816 spin_unlock_irq(&conf->resync_lock);
817}
818
NeilBrown21a52c62010-04-01 15:02:13 +1100819static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
NeilBrown070ec552009-06-16 16:54:21 +1000821 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 mirror_info_t *mirror;
823 r10bio_t *r10_bio;
824 struct bio *read_bio;
825 int i;
826 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100827 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000828 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200829 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800830 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700831 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000832 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000833 int sectors_handled;
834 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Tejun Heoe9c74692010-09-03 11:56:18 +0200836 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
837 md_flush_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700838 return 0;
839 }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* If this request crosses a chunk boundary, we need to
842 * split it. This will only happen for 1 PAGE (or less) requests.
843 */
844 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
845 > chunk_sects &&
846 conf->near_copies < conf->raid_disks)) {
847 struct bio_pair *bp;
848 /* Sanity check -- queue functions should prevent this happening */
849 if (bio->bi_vcnt != 1 ||
850 bio->bi_idx != 0)
851 goto bad_map;
852 /* This is a one page bio that upper layers
853 * refuse to split for us, so we need to split it.
854 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200855 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000857
858 /* Each of these 'make_request' calls will call 'wait_barrier'.
859 * If the first succeeds but the second blocks due to the resync
860 * thread raising the barrier, we will deadlock because the
861 * IO to the underlying device will be queued in generic_make_request
862 * and will never complete, so will never reduce nr_pending.
863 * So increment nr_waiting here so no new raise_barriers will
864 * succeed, and so the second wait_barrier cannot block.
865 */
866 spin_lock_irq(&conf->resync_lock);
867 conf->nr_waiting++;
868 spin_unlock_irq(&conf->resync_lock);
869
NeilBrown21a52c62010-04-01 15:02:13 +1100870 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100872 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 generic_make_request(&bp->bio2);
874
NeilBrown51e9ac72010-08-07 21:17:00 +1000875 spin_lock_irq(&conf->resync_lock);
876 conf->nr_waiting--;
877 wake_up(&conf->wait_barrier);
878 spin_unlock_irq(&conf->resync_lock);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 bio_pair_release(bp);
881 return 0;
882 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000883 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
884 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
886
NeilBrown6712ecf2007-09-27 12:47:43 +0200887 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889 }
890
NeilBrown3d310eb2005-06-21 17:17:26 -0700891 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /*
894 * Register the new request and wait if the reconstruction
895 * thread has put up a bar for new requests.
896 * Continue immediately if no resync is active currently.
897 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800898 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
901
902 r10_bio->master_bio = bio;
903 r10_bio->sectors = bio->bi_size >> 9;
904
905 r10_bio->mddev = mddev;
906 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800907 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
NeilBrown856e08e2011-07-28 11:39:23 +1000909 /* We might need to issue multiple reads to different
910 * devices if there are bad blocks around, so we keep
911 * track of the number of reads in bio->bi_phys_segments.
912 * If this is 0, there is only one r10_bio and no locking
913 * will be needed when the request completes. If it is
914 * non-zero, then it is the number of not-completed requests.
915 */
916 bio->bi_phys_segments = 0;
917 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
918
Jens Axboea3623572005-11-01 09:26:16 +0100919 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /*
921 * read balancing logic:
922 */
NeilBrown856e08e2011-07-28 11:39:23 +1000923 int disk;
924 int slot;
925
926read_again:
927 disk = read_balance(conf, r10_bio, &max_sectors);
928 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (disk < 0) {
930 raid_end_bio_io(r10_bio);
931 return 0;
932 }
933 mirror = conf->mirrors + disk;
934
NeilBrowna167f662010-10-26 18:31:13 +1100935 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +1000936 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
937 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 r10_bio->devs[slot].bio = read_bio;
940
941 read_bio->bi_sector = r10_bio->devs[slot].addr +
942 mirror->rdev->data_offset;
943 read_bio->bi_bdev = mirror->rdev->bdev;
944 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200945 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 read_bio->bi_private = r10_bio;
947
NeilBrown856e08e2011-07-28 11:39:23 +1000948 if (max_sectors < r10_bio->sectors) {
949 /* Could not read all from this device, so we will
950 * need another r10_bio.
951 */
NeilBrown856e08e2011-07-28 11:39:23 +1000952 sectors_handled = (r10_bio->sectors + max_sectors
953 - bio->bi_sector);
954 r10_bio->sectors = max_sectors;
955 spin_lock_irq(&conf->device_lock);
956 if (bio->bi_phys_segments == 0)
957 bio->bi_phys_segments = 2;
958 else
959 bio->bi_phys_segments++;
960 spin_unlock(&conf->device_lock);
961 /* Cannot call generic_make_request directly
962 * as that will be queued in __generic_make_request
963 * and subsequent mempool_alloc might block
964 * waiting for it. so hand bio over to raid10d.
965 */
966 reschedule_retry(r10_bio);
967
968 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
969
970 r10_bio->master_bio = bio;
971 r10_bio->sectors = ((bio->bi_size >> 9)
972 - sectors_handled);
973 r10_bio->state = 0;
974 r10_bio->mddev = mddev;
975 r10_bio->sector = bio->bi_sector + sectors_handled;
976 goto read_again;
977 } else
978 generic_make_request(read_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980 }
981
982 /*
983 * WRITE:
984 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700985 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * inc refcount on their rdev. Record them by setting
987 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +1000988 * If there are known/acknowledged bad blocks on any device
989 * on which we have seen a write error, we want to avoid
990 * writing to those blocks. This potentially requires several
991 * writes to write around the bad blocks. Each set of writes
992 * gets its own r10_bio with a set of bios attached. The number
993 * of r10_bios is recored in bio->bi_phys_segments just as with
994 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000996 plugged = mddev_check_plugged(mddev);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +1000999retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001000 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001002 max_sectors = r10_bio->sectors;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 for (i = 0; i < conf->copies; i++) {
1005 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001006 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001007 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1008 atomic_inc(&rdev->nr_pending);
1009 blocked_rdev = rdev;
1010 break;
1011 }
NeilBrownd4432c22011-07-28 11:39:24 +10001012 r10_bio->devs[i].bio = NULL;
1013 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001014 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001015 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -08001016 }
NeilBrownd4432c22011-07-28 11:39:24 +10001017 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1018 sector_t first_bad;
1019 sector_t dev_sector = r10_bio->devs[i].addr;
1020 int bad_sectors;
1021 int is_bad;
1022
1023 is_bad = is_badblock(rdev, dev_sector,
1024 max_sectors,
1025 &first_bad, &bad_sectors);
1026 if (is_bad < 0) {
1027 /* Mustn't write here until the bad block
1028 * is acknowledged
1029 */
1030 atomic_inc(&rdev->nr_pending);
1031 set_bit(BlockedBadBlocks, &rdev->flags);
1032 blocked_rdev = rdev;
1033 break;
1034 }
1035 if (is_bad && first_bad <= dev_sector) {
1036 /* Cannot write here at all */
1037 bad_sectors -= (dev_sector - first_bad);
1038 if (bad_sectors < max_sectors)
1039 /* Mustn't write more than bad_sectors
1040 * to other devices yet
1041 */
1042 max_sectors = bad_sectors;
1043 /* We don't set R10BIO_Degraded as that
1044 * only applies if the disk is missing,
1045 * so it might be re-added, and we want to
1046 * know to recover this chunk.
1047 * In this case the device is here, and the
1048 * fact that this chunk is not in-sync is
1049 * recorded in the bad block log.
1050 */
1051 continue;
1052 }
1053 if (is_bad) {
1054 int good_sectors = first_bad - dev_sector;
1055 if (good_sectors < max_sectors)
1056 max_sectors = good_sectors;
1057 }
1058 }
1059 r10_bio->devs[i].bio = bio;
1060 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 rcu_read_unlock();
1063
Dan Williams6bfe0b42008-04-30 00:52:32 -07001064 if (unlikely(blocked_rdev)) {
1065 /* Have to wait for this device to get unblocked, then retry */
1066 int j;
1067 int d;
1068
1069 for (j = 0; j < i; j++)
1070 if (r10_bio->devs[j].bio) {
1071 d = r10_bio->devs[j].devnum;
1072 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1073 }
1074 allow_barrier(conf);
1075 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1076 wait_barrier(conf);
1077 goto retry_write;
1078 }
1079
NeilBrownd4432c22011-07-28 11:39:24 +10001080 if (max_sectors < r10_bio->sectors) {
1081 /* We are splitting this into multiple parts, so
1082 * we need to prepare for allocating another r10_bio.
1083 */
1084 r10_bio->sectors = max_sectors;
1085 spin_lock_irq(&conf->device_lock);
1086 if (bio->bi_phys_segments == 0)
1087 bio->bi_phys_segments = 2;
1088 else
1089 bio->bi_phys_segments++;
1090 spin_unlock_irq(&conf->device_lock);
1091 }
1092 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1093
NeilBrown4e780642010-10-19 12:54:01 +11001094 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001095 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 for (i = 0; i < conf->copies; i++) {
1098 struct bio *mbio;
1099 int d = r10_bio->devs[i].devnum;
1100 if (!r10_bio->devs[i].bio)
1101 continue;
1102
NeilBrowna167f662010-10-26 18:31:13 +11001103 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001104 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1105 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 r10_bio->devs[i].bio = mbio;
1107
NeilBrownd4432c22011-07-28 11:39:24 +10001108 mbio->bi_sector = (r10_bio->devs[i].addr+
1109 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1111 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001112 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 mbio->bi_private = r10_bio;
1114
1115 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001116 spin_lock_irqsave(&conf->device_lock, flags);
1117 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +11001118 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
NeilBrown4e780642010-10-19 12:54:01 +11001121 if (atomic_dec_and_test(&r10_bio->remaining)) {
1122 /* This matches the end of raid10_end_write_request() */
1123 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
1124 r10_bio->sectors,
1125 !test_bit(R10BIO_Degraded, &r10_bio->state),
1126 0);
Arne Redlichf6f953a2007-07-31 00:37:57 -07001127 md_write_end(mddev);
1128 raid_end_bio_io(r10_bio);
Arne Redlichf6f953a2007-07-31 00:37:57 -07001129 }
1130
NeilBrowna35e63e2008-03-04 14:29:29 -08001131 /* In case raid10d snuck in to freeze_array */
1132 wake_up(&conf->wait_barrier);
1133
NeilBrownd4432c22011-07-28 11:39:24 +10001134 if (sectors_handled < (bio->bi_size >> 9)) {
1135 /* We need another r1_bio. It has already been counted
1136 * in bio->bi_phys_segments.
1137 */
1138 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1139
1140 r10_bio->master_bio = bio;
1141 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1142
1143 r10_bio->mddev = mddev;
1144 r10_bio->sector = bio->bi_sector + sectors_handled;
1145 r10_bio->state = 0;
1146 goto retry_write;
1147 }
1148
NeilBrownc3b328a2011-04-18 18:25:43 +10001149 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001150 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152}
1153
1154static void status(struct seq_file *seq, mddev_t *mddev)
1155{
NeilBrown070ec552009-06-16 16:54:21 +10001156 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int i;
1158
1159 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001160 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (conf->near_copies > 1)
1162 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001163 if (conf->far_copies > 1) {
1164 if (conf->far_offset)
1165 seq_printf(seq, " %d offset-copies", conf->far_copies);
1166 else
1167 seq_printf(seq, " %d far-copies", conf->far_copies);
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001170 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 for (i = 0; i < conf->raid_disks; i++)
1172 seq_printf(seq, "%s",
1173 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001174 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 seq_printf(seq, "]");
1176}
1177
NeilBrown700c7212011-07-27 11:00:36 +10001178/* check if there are enough drives for
1179 * every block to appear on atleast one.
1180 * Don't consider the device numbered 'ignore'
1181 * as we might be about to remove it.
1182 */
1183static int enough(conf_t *conf, int ignore)
1184{
1185 int first = 0;
1186
1187 do {
1188 int n = conf->copies;
1189 int cnt = 0;
1190 while (n--) {
1191 if (conf->mirrors[first].rdev &&
1192 first != ignore)
1193 cnt++;
1194 first = (first+1) % conf->raid_disks;
1195 }
1196 if (cnt == 0)
1197 return 0;
1198 } while (first != 0);
1199 return 1;
1200}
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1203{
1204 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001205 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /*
1208 * If it is not operational, then we have already marked it as dead
1209 * else if it is the last working disks, ignore the error, let the
1210 * next level up know.
1211 * else mark the drive as failed
1212 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001213 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001214 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /*
1216 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
1218 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001219 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1220 unsigned long flags;
1221 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001223 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /*
1225 * if recovery is running, make sure it aborts.
1226 */
NeilBrowndfc70642008-05-23 13:04:39 -07001227 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
NeilBrownde393cd2011-07-28 11:31:48 +10001229 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001230 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b422006-10-03 01:15:46 -07001231 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001232 printk(KERN_ALERT
1233 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1234 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001235 mdname(mddev), bdevname(rdev->bdev, b),
1236 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static void print_conf(conf_t *conf)
1240{
1241 int i;
1242 mirror_info_t *tmp;
1243
NeilBrown128595e2010-05-03 14:47:14 +10001244 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001246 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return;
1248 }
NeilBrown128595e2010-05-03 14:47:14 +10001249 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 conf->raid_disks);
1251
1252 for (i = 0; i < conf->raid_disks; i++) {
1253 char b[BDEVNAME_SIZE];
1254 tmp = conf->mirrors + i;
1255 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001256 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001257 i, !test_bit(In_sync, &tmp->rdev->flags),
1258 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 bdevname(tmp->rdev->bdev,b));
1260 }
1261}
1262
1263static void close_sync(conf_t *conf)
1264{
NeilBrown0a27ec92006-01-06 00:20:13 -08001265 wait_barrier(conf);
1266 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 mempool_destroy(conf->r10buf_pool);
1269 conf->r10buf_pool = NULL;
1270}
1271
1272static int raid10_spare_active(mddev_t *mddev)
1273{
1274 int i;
1275 conf_t *conf = mddev->private;
1276 mirror_info_t *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001277 int count = 0;
1278 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /*
1281 * Find all non-in_sync disks within the RAID10 configuration
1282 * and mark them in_sync
1283 */
1284 for (i = 0; i < conf->raid_disks; i++) {
1285 tmp = conf->mirrors + i;
1286 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001287 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001288 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001289 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001290 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292 }
NeilBrown6b965622010-08-18 11:56:59 +10001293 spin_lock_irqsave(&conf->device_lock, flags);
1294 mddev->degraded -= count;
1295 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001298 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301
1302static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1303{
1304 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001305 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001307 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001308 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (mddev->recovery_cp < MaxSector)
1311 /* only hot-add to in-sync arrays, as recovery is
1312 * very different from resync
1313 */
Neil Brown199050e2008-06-28 08:31:33 +10001314 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001315 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001316 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
NeilBrowna53a6c82008-11-06 17:28:20 +11001318 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001319 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001321 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001322 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1323 mirror = rdev->saved_raid_disk;
1324 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001325 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001326 for ( ; mirror <= last ; mirror++) {
1327 mirror_info_t *p = &conf->mirrors[mirror];
1328 if (p->recovery_disabled == mddev->recovery_disabled)
1329 continue;
1330 if (!p->rdev)
1331 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
NeilBrown2bb77732011-07-27 11:00:36 +10001333 disk_stack_limits(mddev->gendisk, rdev->bdev,
1334 rdev->data_offset << 9);
1335 /* as we don't honour merge_bvec_fn, we must
1336 * never risk violating it, so limit
1337 * ->max_segments to one lying with a single
1338 * page, as a one page request is never in
1339 * violation.
1340 */
1341 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1342 blk_queue_max_segments(mddev->queue, 1);
1343 blk_queue_segment_boundary(mddev->queue,
1344 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
NeilBrown2bb77732011-07-27 11:00:36 +10001347 p->head_position = 0;
1348 rdev->raid_disk = mirror;
1349 err = 0;
1350 if (rdev->saved_raid_disk != mirror)
1351 conf->fullsync = 1;
1352 rcu_assign_pointer(p->rdev, rdev);
1353 break;
1354 }
1355
Andre Nollac5e7112009-08-03 10:59:47 +10001356 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001358 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361static int raid10_remove_disk(mddev_t *mddev, int number)
1362{
1363 conf_t *conf = mddev->private;
1364 int err = 0;
1365 mdk_rdev_t *rdev;
1366 mirror_info_t *p = conf->mirrors+ number;
1367
1368 print_conf(conf);
1369 rdev = p->rdev;
1370 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001371 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 atomic_read(&rdev->nr_pending)) {
1373 err = -EBUSY;
1374 goto abort;
1375 }
NeilBrowndfc70642008-05-23 13:04:39 -07001376 /* Only remove faulty devices in recovery
1377 * is not possible.
1378 */
1379 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown2bb77732011-07-27 11:00:36 +10001380 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown700c7212011-07-27 11:00:36 +10001381 enough(conf, -1)) {
NeilBrowndfc70642008-05-23 13:04:39 -07001382 err = -EBUSY;
1383 goto abort;
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001386 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (atomic_read(&rdev->nr_pending)) {
1388 /* lost the race, try later */
1389 err = -EBUSY;
1390 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001391 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001393 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395abort:
1396
1397 print_conf(conf);
1398 return err;
1399}
1400
1401
NeilBrown6712ecf2007-09-27 12:47:43 +02001402static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001404 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001405 conf_t *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001406 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
NeilBrown749c55e2011-07-28 11:39:24 +10001408 d = find_bio_disk(conf, r10_bio, bio, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001409
1410 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1411 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001412 else {
1413 atomic_add(r10_bio->sectors,
1414 &conf->mirrors[d].rdev->corrected_errors);
1415 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1416 md_error(r10_bio->mddev,
1417 conf->mirrors[d].rdev);
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /* for reconstruct, we always reschedule after a read.
1421 * for resync, only after all reads
1422 */
NeilBrown73d5c382009-02-25 13:18:47 +11001423 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1425 atomic_dec_and_test(&r10_bio->remaining)) {
1426 /* we have read all the blocks,
1427 * do the comparison in process context in raid10d
1428 */
1429 reschedule_retry(r10_bio);
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
NeilBrown6712ecf2007-09-27 12:47:43 +02001433static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001436 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001438 conf_t *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001439 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001440 sector_t first_bad;
1441 int bad_sectors;
1442 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
NeilBrown749c55e2011-07-28 11:39:24 +10001444 d = find_bio_disk(conf, r10_bio, bio, &slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 if (!uptodate)
1447 md_error(mddev, conf->mirrors[d].rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10001448 else if (is_badblock(conf->mirrors[d].rdev,
1449 r10_bio->devs[slot].addr,
1450 r10_bio->sectors,
1451 &first_bad, &bad_sectors))
1452 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001453
NeilBrown73d5c382009-02-25 13:18:47 +11001454 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 while (atomic_dec_and_test(&r10_bio->remaining)) {
1456 if (r10_bio->master_bio == NULL) {
1457 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001458 sector_t s = r10_bio->sectors;
NeilBrown749c55e2011-07-28 11:39:24 +10001459 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
1460 reschedule_retry(r10_bio);
1461 else
1462 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001463 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 break;
1465 } else {
1466 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
NeilBrown749c55e2011-07-28 11:39:24 +10001467 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
1468 reschedule_retry(r10_bio);
1469 else
1470 put_buf(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 r10_bio = r10_bio2;
1472 }
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476/*
1477 * Note: sync and recover and handled very differently for raid10
1478 * This code is for resync.
1479 * For resync, we read through virtual addresses and read all blocks.
1480 * If there is any error, we schedule a write. The lowest numbered
1481 * drive is authoritative.
1482 * However requests come for physical address, so we need to map.
1483 * For every physical address there are raid_disks/copies virtual addresses,
1484 * which is always are least one, but is not necessarly an integer.
1485 * This means that a physical address can span multiple chunks, so we may
1486 * have to submit multiple io requests for a single sync request.
1487 */
1488/*
1489 * We check if all blocks are in-sync and only write to blocks that
1490 * aren't in sync
1491 */
1492static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1493{
NeilBrown070ec552009-06-16 16:54:21 +10001494 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int i, first;
1496 struct bio *tbio, *fbio;
1497
1498 atomic_set(&r10_bio->remaining, 1);
1499
1500 /* find the first device with a block */
1501 for (i=0; i<conf->copies; i++)
1502 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1503 break;
1504
1505 if (i == conf->copies)
1506 goto done;
1507
1508 first = i;
1509 fbio = r10_bio->devs[i].bio;
1510
1511 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001512 for (i=0 ; i < conf->copies ; i++) {
1513 int j, d;
1514 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001517
1518 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001520 if (i == first)
1521 continue;
1522 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1523 /* We know that the bi_io_vec layout is the same for
1524 * both 'first' and 'i', so we just compare them.
1525 * All vec entries are PAGE_SIZE;
1526 */
1527 for (j = 0; j < vcnt; j++)
1528 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1529 page_address(tbio->bi_io_vec[j].bv_page),
1530 PAGE_SIZE))
1531 break;
1532 if (j == vcnt)
1533 continue;
1534 mddev->resync_mismatches += r10_bio->sectors;
1535 }
NeilBrown18f08812006-01-06 00:20:25 -08001536 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1537 /* Don't fix anything. */
1538 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* Ok, we need to write this bio
1540 * First we need to fixup bv_offset, bv_len and
1541 * bi_vecs, as the read request might have corrupted these
1542 */
1543 tbio->bi_vcnt = vcnt;
1544 tbio->bi_size = r10_bio->sectors << 9;
1545 tbio->bi_idx = 0;
1546 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1548 tbio->bi_flags |= 1 << BIO_UPTODATE;
1549 tbio->bi_next = NULL;
1550 tbio->bi_rw = WRITE;
1551 tbio->bi_private = r10_bio;
1552 tbio->bi_sector = r10_bio->devs[i].addr;
1553
1554 for (j=0; j < vcnt ; j++) {
1555 tbio->bi_io_vec[j].bv_offset = 0;
1556 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1557
1558 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1559 page_address(fbio->bi_io_vec[j].bv_page),
1560 PAGE_SIZE);
1561 }
1562 tbio->bi_end_io = end_sync_write;
1563
1564 d = r10_bio->devs[i].devnum;
1565 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1566 atomic_inc(&r10_bio->remaining);
1567 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1568
1569 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1570 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1571 generic_make_request(tbio);
1572 }
1573
1574done:
1575 if (atomic_dec_and_test(&r10_bio->remaining)) {
1576 md_done_sync(mddev, r10_bio->sectors, 1);
1577 put_buf(r10_bio);
1578 }
1579}
1580
1581/*
1582 * Now for the recovery code.
1583 * Recovery happens across physical sectors.
1584 * We recover all non-is_sync drives by finding the virtual address of
1585 * each, and then choose a working drive that also has that virt address.
1586 * There is a separate r10_bio for each non-in_sync drive.
1587 * Only the first two slots are in use. The first for reading,
1588 * The second for writing.
1589 *
1590 */
1591
1592static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1593{
NeilBrown070ec552009-06-16 16:54:21 +10001594 conf_t *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001595 int d;
1596 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Namhyung Kimc65060a2011-07-18 17:38:49 +10001598 /*
1599 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 * and submit the write request
1601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 wbio = r10_bio->devs[1].bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 d = r10_bio->devs[1].devnum;
1604
1605 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1606 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001607 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1608 generic_make_request(wbio);
NeilBrown2bb77732011-07-27 11:00:36 +10001609 else {
1610 printk(KERN_NOTICE
1611 "md/raid10:%s: recovery aborted due to read error\n",
1612 mdname(mddev));
1613 conf->mirrors[d].recovery_disabled = mddev->recovery_disabled;
1614 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1615 bio_endio(wbio, 0);
1616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619
1620/*
Robert Becker1e509152009-12-14 12:49:58 +11001621 * Used by fix_read_error() to decay the per rdev read_errors.
1622 * We halve the read error count for every hour that has elapsed
1623 * since the last recorded read error.
1624 *
1625 */
1626static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1627{
1628 struct timespec cur_time_mon;
1629 unsigned long hours_since_last;
1630 unsigned int read_errors = atomic_read(&rdev->read_errors);
1631
1632 ktime_get_ts(&cur_time_mon);
1633
1634 if (rdev->last_read_error.tv_sec == 0 &&
1635 rdev->last_read_error.tv_nsec == 0) {
1636 /* first time we've seen a read error */
1637 rdev->last_read_error = cur_time_mon;
1638 return;
1639 }
1640
1641 hours_since_last = (cur_time_mon.tv_sec -
1642 rdev->last_read_error.tv_sec) / 3600;
1643
1644 rdev->last_read_error = cur_time_mon;
1645
1646 /*
1647 * if hours_since_last is > the number of bits in read_errors
1648 * just set read errors to 0. We do this to avoid
1649 * overflowing the shift of read_errors by hours_since_last.
1650 */
1651 if (hours_since_last >= 8 * sizeof(read_errors))
1652 atomic_set(&rdev->read_errors, 0);
1653 else
1654 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1655}
1656
1657/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * This is a kernel thread which:
1659 *
1660 * 1. Retries failed read operations on working mirrors.
1661 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001662 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 */
1664
NeilBrown6814d532006-10-03 01:15:45 -07001665static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1666{
1667 int sect = 0; /* Offset from r10_bio->sector */
1668 int sectors = r10_bio->sectors;
1669 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001670 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001671 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001672
NeilBrown7c4e06f2011-05-11 14:53:17 +10001673 /* still own a reference to this rdev, so it cannot
1674 * have been cleared recently.
1675 */
1676 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001677
NeilBrown7c4e06f2011-05-11 14:53:17 +10001678 if (test_bit(Faulty, &rdev->flags))
1679 /* drive has already been failed, just ignore any
1680 more fix_read_error() attempts */
1681 return;
1682
1683 check_decay_read_errors(mddev, rdev);
1684 atomic_inc(&rdev->read_errors);
1685 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1686 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001687 bdevname(rdev->bdev, b);
1688
NeilBrown7c4e06f2011-05-11 14:53:17 +10001689 printk(KERN_NOTICE
1690 "md/raid10:%s: %s: Raid device exceeded "
1691 "read_error threshold [cur %d:max %d]\n",
1692 mdname(mddev), b,
1693 atomic_read(&rdev->read_errors), max_read_errors);
1694 printk(KERN_NOTICE
1695 "md/raid10:%s: %s: Failing raid device\n",
1696 mdname(mddev), b);
1697 md_error(mddev, conf->mirrors[d].rdev);
1698 return;
Robert Becker1e509152009-12-14 12:49:58 +11001699 }
Robert Becker1e509152009-12-14 12:49:58 +11001700
NeilBrown6814d532006-10-03 01:15:45 -07001701 while(sectors) {
1702 int s = sectors;
1703 int sl = r10_bio->read_slot;
1704 int success = 0;
1705 int start;
1706
1707 if (s > (PAGE_SIZE>>9))
1708 s = PAGE_SIZE >> 9;
1709
1710 rcu_read_lock();
1711 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10001712 sector_t first_bad;
1713 int bad_sectors;
1714
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001715 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001716 rdev = rcu_dereference(conf->mirrors[d].rdev);
1717 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10001718 test_bit(In_sync, &rdev->flags) &&
1719 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1720 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001721 atomic_inc(&rdev->nr_pending);
1722 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001723 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001724 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001725 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001726 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001727 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001728 rdev_dec_pending(rdev, mddev);
1729 rcu_read_lock();
1730 if (success)
1731 break;
1732 }
1733 sl++;
1734 if (sl == conf->copies)
1735 sl = 0;
1736 } while (!success && sl != r10_bio->read_slot);
1737 rcu_read_unlock();
1738
1739 if (!success) {
1740 /* Cannot read from anywhere -- bye bye array */
1741 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1742 md_error(mddev, conf->mirrors[dn].rdev);
1743 break;
1744 }
1745
1746 start = sl;
1747 /* write it back and re-read */
1748 rcu_read_lock();
1749 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001750 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001751
NeilBrown6814d532006-10-03 01:15:45 -07001752 if (sl==0)
1753 sl = conf->copies;
1754 sl--;
1755 d = r10_bio->devs[sl].devnum;
1756 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001757 if (!rdev ||
1758 !test_bit(In_sync, &rdev->flags))
1759 continue;
1760
1761 atomic_inc(&rdev->nr_pending);
1762 rcu_read_unlock();
1763 if (sync_page_io(rdev,
1764 r10_bio->devs[sl].addr +
1765 sect,
1766 s<<9, conf->tmppage, WRITE, false)
1767 == 0) {
1768 /* Well, this device is dead */
1769 printk(KERN_NOTICE
1770 "md/raid10:%s: read correction "
1771 "write failed"
1772 " (%d sectors at %llu on %s)\n",
1773 mdname(mddev), s,
1774 (unsigned long long)(
1775 sect + rdev->data_offset),
1776 bdevname(rdev->bdev, b));
1777 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1778 "drive\n",
1779 mdname(mddev),
1780 bdevname(rdev->bdev, b));
1781 md_error(mddev, rdev);
NeilBrown6814d532006-10-03 01:15:45 -07001782 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001783 rdev_dec_pending(rdev, mddev);
1784 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001785 }
1786 sl = start;
1787 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10001788 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001789
NeilBrown6814d532006-10-03 01:15:45 -07001790 if (sl==0)
1791 sl = conf->copies;
1792 sl--;
1793 d = r10_bio->devs[sl].devnum;
1794 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001795 if (!rdev ||
1796 !test_bit(In_sync, &rdev->flags))
1797 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11001798
NeilBrown1294b9c2011-07-28 11:39:23 +10001799 atomic_inc(&rdev->nr_pending);
1800 rcu_read_unlock();
1801 if (sync_page_io(rdev,
1802 r10_bio->devs[sl].addr +
1803 sect,
1804 s<<9, conf->tmppage,
1805 READ, false) == 0) {
1806 /* Well, this device is dead */
1807 printk(KERN_NOTICE
1808 "md/raid10:%s: unable to read back "
1809 "corrected sectors"
1810 " (%d sectors at %llu on %s)\n",
1811 mdname(mddev), s,
1812 (unsigned long long)(
1813 sect + rdev->data_offset),
1814 bdevname(rdev->bdev, b));
1815 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1816 "drive\n",
1817 mdname(mddev),
1818 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001819
NeilBrown1294b9c2011-07-28 11:39:23 +10001820 md_error(mddev, rdev);
1821 } else {
1822 printk(KERN_INFO
1823 "md/raid10:%s: read error corrected"
1824 " (%d sectors at %llu on %s)\n",
1825 mdname(mddev), s,
1826 (unsigned long long)(
1827 sect + rdev->data_offset),
1828 bdevname(rdev->bdev, b));
1829 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07001830 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001831
1832 rdev_dec_pending(rdev, mddev);
1833 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001834 }
1835 rcu_read_unlock();
1836
1837 sectors -= s;
1838 sect += s;
1839 }
1840}
1841
NeilBrown560f8e52011-07-28 11:39:23 +10001842static void handle_read_error(mddev_t *mddev, r10bio_t *r10_bio)
1843{
1844 int slot = r10_bio->read_slot;
1845 int mirror = r10_bio->devs[slot].devnum;
1846 struct bio *bio;
1847 conf_t *conf = mddev->private;
1848 mdk_rdev_t *rdev;
1849 char b[BDEVNAME_SIZE];
1850 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10001851 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10001852
1853 /* we got a read error. Maybe the drive is bad. Maybe just
1854 * the block and we can fix it.
1855 * We freeze all other IO, and try reading the block from
1856 * other devices. When we find one, we re-write
1857 * and check it that fixes the read error.
1858 * This is all done synchronously while the array is
1859 * frozen.
1860 */
1861 if (mddev->ro == 0) {
1862 freeze_array(conf);
1863 fix_read_error(conf, mddev, r10_bio);
1864 unfreeze_array(conf);
1865 }
1866 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1867
1868 bio = r10_bio->devs[slot].bio;
NeilBrown7399c312011-07-28 11:39:23 +10001869 bdevname(bio->bi_bdev, b);
NeilBrown560f8e52011-07-28 11:39:23 +10001870 r10_bio->devs[slot].bio =
1871 mddev->ro ? IO_BLOCKED : NULL;
NeilBrown7399c312011-07-28 11:39:23 +10001872read_more:
NeilBrown856e08e2011-07-28 11:39:23 +10001873 mirror = read_balance(conf, r10_bio, &max_sectors);
NeilBrown7399c312011-07-28 11:39:23 +10001874 if (mirror == -1) {
NeilBrown560f8e52011-07-28 11:39:23 +10001875 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
1876 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10001877 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10001878 (unsigned long long)r10_bio->sector);
1879 raid_end_bio_io(r10_bio);
1880 bio_put(bio);
1881 return;
1882 }
1883
1884 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown7399c312011-07-28 11:39:23 +10001885 if (bio)
1886 bio_put(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10001887 slot = r10_bio->read_slot;
1888 rdev = conf->mirrors[mirror].rdev;
1889 printk_ratelimited(
1890 KERN_ERR
1891 "md/raid10:%s: %s: redirecting"
1892 "sector %llu to another mirror\n",
1893 mdname(mddev),
1894 bdevname(rdev->bdev, b),
1895 (unsigned long long)r10_bio->sector);
1896 bio = bio_clone_mddev(r10_bio->master_bio,
1897 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10001898 md_trim_bio(bio,
1899 r10_bio->sector - bio->bi_sector,
1900 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10001901 r10_bio->devs[slot].bio = bio;
1902 bio->bi_sector = r10_bio->devs[slot].addr
1903 + rdev->data_offset;
1904 bio->bi_bdev = rdev->bdev;
1905 bio->bi_rw = READ | do_sync;
1906 bio->bi_private = r10_bio;
1907 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10001908 if (max_sectors < r10_bio->sectors) {
1909 /* Drat - have to split this up more */
1910 struct bio *mbio = r10_bio->master_bio;
1911 int sectors_handled =
1912 r10_bio->sector + max_sectors
1913 - mbio->bi_sector;
1914 r10_bio->sectors = max_sectors;
1915 spin_lock_irq(&conf->device_lock);
1916 if (mbio->bi_phys_segments == 0)
1917 mbio->bi_phys_segments = 2;
1918 else
1919 mbio->bi_phys_segments++;
1920 spin_unlock_irq(&conf->device_lock);
1921 generic_make_request(bio);
1922 bio = NULL;
1923
1924 r10_bio = mempool_alloc(conf->r10bio_pool,
1925 GFP_NOIO);
1926 r10_bio->master_bio = mbio;
1927 r10_bio->sectors = (mbio->bi_size >> 9)
1928 - sectors_handled;
1929 r10_bio->state = 0;
1930 set_bit(R10BIO_ReadError,
1931 &r10_bio->state);
1932 r10_bio->mddev = mddev;
1933 r10_bio->sector = mbio->bi_sector
1934 + sectors_handled;
1935
1936 goto read_more;
1937 } else
1938 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10001939}
1940
NeilBrown749c55e2011-07-28 11:39:24 +10001941static void handle_write_completed(conf_t *conf, r10bio_t *r10_bio)
1942{
1943 /* Some sort of write request has finished and it
1944 * succeeded in writing where we thought there was a
1945 * bad block. So forget the bad block.
1946 */
1947 int m;
1948 mdk_rdev_t *rdev;
1949
1950 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
1951 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1952 for (m = 0; m < conf->copies; m++)
1953 if (r10_bio->devs[m].bio &&
1954 test_bit(BIO_UPTODATE,
1955 &r10_bio->devs[m].bio->bi_flags)) {
1956 int dev = r10_bio->devs[m].devnum;
1957 rdev = conf->mirrors[dev].rdev;
1958 rdev_clear_badblocks(
1959 rdev,
1960 r10_bio->devs[m].addr,
1961 r10_bio->sectors);
1962 }
1963 put_buf(r10_bio);
1964 } else {
1965 for (m = 0; m < conf->copies; m++)
1966 if (r10_bio->devs[m].bio == IO_MADE_GOOD) {
1967 int dev = r10_bio->devs[m].devnum;
1968 rdev = conf->mirrors[dev].rdev;
1969 rdev_clear_badblocks(
1970 rdev,
1971 r10_bio->devs[m].addr,
1972 r10_bio->sectors);
1973 rdev_dec_pending(rdev, conf->mddev);
1974 }
1975 raid_end_bio_io(r10_bio);
1976 }
1977}
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979static void raid10d(mddev_t *mddev)
1980{
1981 r10bio_t *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001983 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001985 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001989 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08001991
Jens Axboe7eaceac2011-03-10 08:52:07 +01001992 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001995 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001996 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
2000 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002001 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 spin_unlock_irqrestore(&conf->device_lock, flags);
2003
2004 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002005 conf = mddev->private;
NeilBrown749c55e2011-07-28 11:39:24 +10002006 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
2007 handle_write_completed(conf, r10_bio);
2008 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002010 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002012 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002013 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002014 else {
2015 /* just a partial read to be scheduled from a
2016 * separate context
2017 */
2018 int slot = r10_bio->read_slot;
2019 generic_make_request(r10_bio->devs[slot].bio);
2020 }
NeilBrown4443ae12006-01-06 00:20:28 -08002021
NeilBrown1d9d5242009-10-16 15:55:32 +11002022 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002023 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2024 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002026 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
2029
2030static int init_resync(conf_t *conf)
2031{
2032 int buffs;
2033
2034 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002035 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2037 if (!conf->r10buf_pool)
2038 return -ENOMEM;
2039 conf->next_resync = 0;
2040 return 0;
2041}
2042
2043/*
2044 * perform a "sync" on one "block"
2045 *
2046 * We need to make sure that no normal I/O request - particularly write
2047 * requests - conflict with active sync requests.
2048 *
2049 * This is achieved by tracking pending requests and a 'barrier' concept
2050 * that can be installed to exclude normal IO requests.
2051 *
2052 * Resync and recovery are handled very differently.
2053 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2054 *
2055 * For resync, we iterate over virtual addresses, read all copies,
2056 * and update if there are differences. If only one copy is live,
2057 * skip it.
2058 * For recovery, we iterate over physical addresses, read a good
2059 * value for each non-in_sync drive, and over-write.
2060 *
2061 * So, for recovery we may have several outstanding complex requests for a
2062 * given address, one for each out-of-sync device. We model this by allocating
2063 * a number of r10_bio structures, one for each out-of-sync device.
2064 * As we setup these structures, we collect all bio's together into a list
2065 * which we then process collectively to add pages, and then process again
2066 * to pass to generic_make_request.
2067 *
2068 * The r10_bio structures are linked using a borrowed master_bio pointer.
2069 * This link is counted in ->remaining. When the r10_bio that points to NULL
2070 * has its remaining count decremented to 0, the whole complex operation
2071 * is complete.
2072 *
2073 */
2074
NeilBrownab9d47e2011-05-11 14:54:41 +10002075static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
2076 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
NeilBrown070ec552009-06-16 16:54:21 +10002078 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 r10bio_t *r10_bio;
2080 struct bio *biolist = NULL, *bio;
2081 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002083 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002084 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 sector_t sectors_skipped = 0;
2086 int chunks_skipped = 0;
2087
2088 if (!conf->r10buf_pool)
2089 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002093 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2095 max_sector = mddev->resync_max_sectors;
2096 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002097 /* If we aborted, we need to abort the
2098 * sync on the 'current' bitmap chucks (there can
2099 * be several when recovering multiple devices).
2100 * as we may have started syncing it but not finished.
2101 * We can find the current address in
2102 * mddev->curr_resync, but for recovery,
2103 * we need to convert that to several
2104 * virtual addresses.
2105 */
2106 if (mddev->curr_resync < max_sector) { /* aborted */
2107 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2108 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2109 &sync_blocks, 1);
2110 else for (i=0; i<conf->raid_disks; i++) {
2111 sector_t sect =
2112 raid10_find_virt(conf, mddev->curr_resync, i);
2113 bitmap_end_sync(mddev->bitmap, sect,
2114 &sync_blocks, 1);
2115 }
2116 } else /* completed sync */
2117 conf->fullsync = 0;
2118
2119 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002121 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return sectors_skipped;
2123 }
2124 if (chunks_skipped >= conf->raid_disks) {
2125 /* if there has been nothing to do on any drive,
2126 * then there is nothing to do at all..
2127 */
NeilBrown57afd892005-06-21 17:17:13 -07002128 *skipped = 1;
2129 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131
NeilBrownc6207272008-02-06 01:39:52 -08002132 if (max_sector > mddev->resync_max)
2133 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* make sure whole request will fit in a chunk - if chunks
2136 * are meaningful
2137 */
2138 if (conf->near_copies < conf->raid_disks &&
2139 max_sector > (sector_nr | conf->chunk_mask))
2140 max_sector = (sector_nr | conf->chunk_mask) + 1;
2141 /*
2142 * If there is non-resync activity waiting for us then
2143 * put in a delay to throttle resync.
2144 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002145 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 /* Again, very different code for resync and recovery.
2149 * Both must result in an r10bio with a list of bios that
2150 * have bi_end_io, bi_sector, bi_bdev set,
2151 * and bi_private set to the r10bio.
2152 * For recovery, we may actually create several r10bios
2153 * with 2 bios in each, that correspond to the bios in the main one.
2154 * In this case, the subordinate r10bios link back through a
2155 * borrowed master_bio pointer, and the counter in the master
2156 * includes a ref from each subordinate.
2157 */
2158 /* First, we decide what to do and set ->bi_end_io
2159 * To end_sync_read if we want to read, and
2160 * end_sync_write if we will want to write.
2161 */
2162
NeilBrown6cce3b22006-01-06 00:20:16 -08002163 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2165 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002166 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 r10_bio = NULL;
2168
NeilBrownab9d47e2011-05-11 14:54:41 +10002169 for (i=0 ; i<conf->raid_disks; i++) {
2170 int still_degraded;
2171 r10bio_t *rb2;
2172 sector_t sect;
2173 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002174 int any_working;
NeilBrownab9d47e2011-05-11 14:54:41 +10002175
2176 if (conf->mirrors[i].rdev == NULL ||
2177 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2178 continue;
2179
2180 still_degraded = 0;
2181 /* want to reconstruct this device */
2182 rb2 = r10_bio;
2183 sect = raid10_find_virt(conf, sector_nr, i);
2184 /* Unless we are doing a full sync, we only need
2185 * to recover the block if it is set in the bitmap
2186 */
2187 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2188 &sync_blocks, 1);
2189 if (sync_blocks < max_sync)
2190 max_sync = sync_blocks;
2191 if (!must_sync &&
2192 !conf->fullsync) {
2193 /* yep, skip the sync_blocks here, but don't assume
2194 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002195 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002196 chunks_skipped = -1;
2197 continue;
2198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
NeilBrownab9d47e2011-05-11 14:54:41 +10002200 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2201 raise_barrier(conf, rb2 != NULL);
2202 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
NeilBrownab9d47e2011-05-11 14:54:41 +10002204 r10_bio->master_bio = (struct bio*)rb2;
2205 if (rb2)
2206 atomic_inc(&rb2->remaining);
2207 r10_bio->mddev = mddev;
2208 set_bit(R10BIO_IsRecover, &r10_bio->state);
2209 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002210
NeilBrownab9d47e2011-05-11 14:54:41 +10002211 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002212
NeilBrownab9d47e2011-05-11 14:54:41 +10002213 /* Need to check if the array will still be
2214 * degraded
2215 */
2216 for (j=0; j<conf->raid_disks; j++)
2217 if (conf->mirrors[j].rdev == NULL ||
2218 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2219 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002222
2223 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2224 &sync_blocks, still_degraded);
2225
NeilBrowne875ece2011-07-28 11:39:24 +10002226 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002227 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002228 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002229 int d = r10_bio->devs[j].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002230 mdk_rdev_t *rdev;
2231 sector_t sector, first_bad;
2232 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002233 if (!conf->mirrors[d].rdev ||
2234 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2235 continue;
2236 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002237 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002238 rdev = conf->mirrors[d].rdev;
2239 sector = r10_bio->devs[j].addr;
2240
2241 if (is_badblock(rdev, sector, max_sync,
2242 &first_bad, &bad_sectors)) {
2243 if (first_bad > sector)
2244 max_sync = first_bad - sector;
2245 else {
2246 bad_sectors -= (sector
2247 - first_bad);
2248 if (max_sync > bad_sectors)
2249 max_sync = bad_sectors;
2250 continue;
2251 }
2252 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002253 bio = r10_bio->devs[0].bio;
2254 bio->bi_next = biolist;
2255 biolist = bio;
2256 bio->bi_private = r10_bio;
2257 bio->bi_end_io = end_sync_read;
2258 bio->bi_rw = READ;
2259 bio->bi_sector = r10_bio->devs[j].addr +
2260 conf->mirrors[d].rdev->data_offset;
2261 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2262 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2263 atomic_inc(&r10_bio->remaining);
2264 /* and we write to 'i' */
2265
2266 for (k=0; k<conf->copies; k++)
2267 if (r10_bio->devs[k].devnum == i)
2268 break;
2269 BUG_ON(k == conf->copies);
2270 bio = r10_bio->devs[1].bio;
2271 bio->bi_next = biolist;
2272 biolist = bio;
2273 bio->bi_private = r10_bio;
2274 bio->bi_end_io = end_sync_write;
2275 bio->bi_rw = WRITE;
2276 bio->bi_sector = r10_bio->devs[k].addr +
2277 conf->mirrors[i].rdev->data_offset;
2278 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2279
2280 r10_bio->devs[0].devnum = d;
2281 r10_bio->devs[1].devnum = i;
2282
2283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002285 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002286 /* Cannot recover, so abort the recovery or
2287 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002288 put_buf(r10_bio);
2289 if (rb2)
2290 atomic_dec(&rb2->remaining);
2291 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002292 if (any_working) {
2293 /* problem is that there are bad blocks
2294 * on other device(s)
2295 */
2296 int k;
2297 for (k = 0; k < conf->copies; k++)
2298 if (r10_bio->devs[k].devnum == i)
2299 break;
2300 if (!rdev_set_badblocks(
2301 conf->mirrors[i].rdev,
2302 r10_bio->devs[k].addr,
2303 max_sync, 0))
2304 any_working = 0;
2305 }
2306 if (!any_working) {
2307 if (!test_and_set_bit(MD_RECOVERY_INTR,
2308 &mddev->recovery))
2309 printk(KERN_INFO "md/raid10:%s: insufficient "
2310 "working devices for recovery.\n",
2311 mdname(mddev));
2312 conf->mirrors[i].recovery_disabled
2313 = mddev->recovery_disabled;
2314 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002315 break;
2316 }
2317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (biolist == NULL) {
2319 while (r10_bio) {
2320 r10bio_t *rb2 = r10_bio;
2321 r10_bio = (r10bio_t*) rb2->master_bio;
2322 rb2->master_bio = NULL;
2323 put_buf(rb2);
2324 }
2325 goto giveup;
2326 }
2327 } else {
2328 /* resync. Schedule a read for every block at this virt offset */
2329 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002330
NeilBrown78200d42009-02-25 13:18:47 +11002331 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2332
NeilBrown6cce3b22006-01-06 00:20:16 -08002333 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2334 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002335 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2336 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002337 /* We can skip this block */
2338 *skipped = 1;
2339 return sync_blocks + sectors_skipped;
2340 }
2341 if (sync_blocks < max_sync)
2342 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 r10_bio->mddev = mddev;
2346 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002347 raise_barrier(conf, 0);
2348 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 r10_bio->master_bio = NULL;
2351 r10_bio->sector = sector_nr;
2352 set_bit(R10BIO_IsSync, &r10_bio->state);
2353 raid10_find_phys(conf, r10_bio);
2354 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2355
2356 for (i=0; i<conf->copies; i++) {
2357 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002358 sector_t first_bad, sector;
2359 int bad_sectors;
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 bio = r10_bio->devs[i].bio;
2362 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002363 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002365 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10002367 sector = r10_bio->devs[i].addr;
2368 if (is_badblock(conf->mirrors[d].rdev,
2369 sector, max_sync,
2370 &first_bad, &bad_sectors)) {
2371 if (first_bad > sector)
2372 max_sync = first_bad - sector;
2373 else {
2374 bad_sectors -= (sector - first_bad);
2375 if (max_sync > bad_sectors)
2376 max_sync = max_sync;
2377 continue;
2378 }
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2381 atomic_inc(&r10_bio->remaining);
2382 bio->bi_next = biolist;
2383 biolist = bio;
2384 bio->bi_private = r10_bio;
2385 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002386 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10002387 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 conf->mirrors[d].rdev->data_offset;
2389 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2390 count++;
2391 }
2392
2393 if (count < 2) {
2394 for (i=0; i<conf->copies; i++) {
2395 int d = r10_bio->devs[i].devnum;
2396 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002397 rdev_dec_pending(conf->mirrors[d].rdev,
2398 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400 put_buf(r10_bio);
2401 biolist = NULL;
2402 goto giveup;
2403 }
2404 }
2405
2406 for (bio = biolist; bio ; bio=bio->bi_next) {
2407
2408 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2409 if (bio->bi_end_io)
2410 bio->bi_flags |= 1 << BIO_UPTODATE;
2411 bio->bi_vcnt = 0;
2412 bio->bi_idx = 0;
2413 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 bio->bi_size = 0;
2415 }
2416
2417 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002418 if (sector_nr + max_sync < max_sector)
2419 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 do {
2421 struct page *page;
2422 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (sector_nr + (len>>9) > max_sector)
2424 len = (max_sector - sector_nr) << 9;
2425 if (len == 0)
2426 break;
2427 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002428 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002430 if (bio_add_page(bio, page, len, 0))
2431 continue;
2432
2433 /* stop here */
2434 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2435 for (bio2 = biolist;
2436 bio2 && bio2 != bio;
2437 bio2 = bio2->bi_next) {
2438 /* remove last page from this bio */
2439 bio2->bi_vcnt--;
2440 bio2->bi_size -= len;
2441 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002443 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 nr_sectors += len>>9;
2446 sector_nr += len>>9;
2447 } while (biolist->bi_vcnt < RESYNC_PAGES);
2448 bio_full:
2449 r10_bio->sectors = nr_sectors;
2450
2451 while (biolist) {
2452 bio = biolist;
2453 biolist = biolist->bi_next;
2454
2455 bio->bi_next = NULL;
2456 r10_bio = bio->bi_private;
2457 r10_bio->sectors = nr_sectors;
2458
2459 if (bio->bi_end_io == end_sync_read) {
2460 md_sync_acct(bio->bi_bdev, nr_sectors);
2461 generic_make_request(bio);
2462 }
2463 }
2464
NeilBrown57afd892005-06-21 17:17:13 -07002465 if (sectors_skipped)
2466 /* pretend they weren't skipped, it makes
2467 * no important difference in this case
2468 */
2469 md_done_sync(mddev, sectors_skipped, 1);
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return sectors_skipped + nr_sectors;
2472 giveup:
2473 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10002474 * drives must be failed or in resync, all drives
2475 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 */
NeilBrown09b40682009-02-25 13:18:47 +11002477 if (sector_nr + max_sync < max_sector)
2478 max_sector = sector_nr + max_sync;
2479
2480 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 chunks_skipped ++;
2482 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Dan Williams80c3a6c2009-03-17 18:10:40 -07002486static sector_t
2487raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2488{
2489 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002490 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002491
2492 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002493 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002494 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002495 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002496
2497 size = sectors >> conf->chunk_shift;
2498 sector_div(size, conf->far_copies);
2499 size = size * raid_disks;
2500 sector_div(size, conf->near_copies);
2501
2502 return size << conf->chunk_shift;
2503}
2504
Trela, Maciejdab8b292010-03-08 16:02:45 +11002505
2506static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002508 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002509 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002511 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Maciej Trelaf73ea872010-06-16 11:46:29 +01002513 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2514 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002515 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2516 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2517 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
NeilBrown2604b702006-01-06 00:20:36 -08002520
Maciej Trelaf73ea872010-06-16 11:46:29 +01002521 nc = mddev->new_layout & 255;
2522 fc = (mddev->new_layout >> 8) & 255;
2523 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002526 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002527 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002528 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 goto out;
2530 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002531
2532 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002533 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002534 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002536
NeilBrown4443ae12006-01-06 00:20:28 -08002537 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002538 GFP_KERNEL);
2539 if (!conf->mirrors)
2540 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002541
2542 conf->tmppage = alloc_page(GFP_KERNEL);
2543 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002544 goto out;
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
NeilBrown64a742b2007-02-28 20:11:18 -08002547 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 conf->near_copies = nc;
2549 conf->far_copies = fc;
2550 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002551 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002552 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2553 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2554
2555 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2556 r10bio_pool_free, conf);
2557 if (!conf->r10bio_pool)
2558 goto out;
2559
Andre Noll58c0fed2009-03-31 14:33:13 +11002560 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002561 sector_div(size, fc);
2562 size = size * conf->raid_disks;
2563 sector_div(size, nc);
2564 /* 'size' is now the number of chunks in the array */
2565 /* calculate "used chunks per device" in 'stride' */
2566 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002567
2568 /* We need to round up when dividing by raid_disks to
2569 * get the stride size.
2570 */
2571 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002572 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002573
2574 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002575
NeilBrownc93983b2006-06-26 00:27:41 -07002576 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002577 stride = 1;
2578 else
NeilBrownc93983b2006-06-26 00:27:41 -07002579 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002580 conf->stride = stride << conf->chunk_shift;
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Neil Browne7e72bf2008-05-14 16:05:54 -07002583 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002584 INIT_LIST_HEAD(&conf->retry_list);
2585
2586 spin_lock_init(&conf->resync_lock);
2587 init_waitqueue_head(&conf->wait_barrier);
2588
2589 conf->thread = md_register_thread(raid10d, mddev, NULL);
2590 if (!conf->thread)
2591 goto out;
2592
Trela, Maciejdab8b292010-03-08 16:02:45 +11002593 conf->mddev = mddev;
2594 return conf;
2595
2596 out:
NeilBrown128595e2010-05-03 14:47:14 +10002597 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002598 mdname(mddev));
2599 if (conf) {
2600 if (conf->r10bio_pool)
2601 mempool_destroy(conf->r10bio_pool);
2602 kfree(conf->mirrors);
2603 safe_put_page(conf->tmppage);
2604 kfree(conf);
2605 }
2606 return ERR_PTR(err);
2607}
2608
2609static int run(mddev_t *mddev)
2610{
2611 conf_t *conf;
2612 int i, disk_idx, chunk_size;
2613 mirror_info_t *disk;
2614 mdk_rdev_t *rdev;
2615 sector_t size;
2616
2617 /*
2618 * copy the already verified devices into our private RAID10
2619 * bookkeeping area. [whatever we allocate in run(),
2620 * should be freed in stop()]
2621 */
2622
2623 if (mddev->private == NULL) {
2624 conf = setup_conf(mddev);
2625 if (IS_ERR(conf))
2626 return PTR_ERR(conf);
2627 mddev->private = conf;
2628 }
2629 conf = mddev->private;
2630 if (!conf)
2631 goto out;
2632
Trela, Maciejdab8b292010-03-08 16:02:45 +11002633 mddev->thread = conf->thread;
2634 conf->thread = NULL;
2635
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002636 chunk_size = mddev->chunk_sectors << 9;
2637 blk_queue_io_min(mddev->queue, chunk_size);
2638 if (conf->raid_disks % conf->near_copies)
2639 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2640 else
2641 blk_queue_io_opt(mddev->queue, chunk_size *
2642 (conf->raid_disks / conf->near_copies));
2643
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002644 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown34b343c2011-07-28 11:31:47 +10002645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002647 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 || disk_idx < 0)
2649 continue;
2650 disk = conf->mirrors + disk_idx;
2651
2652 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002653 disk_stack_limits(mddev->gendisk, rdev->bdev,
2654 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002656 * violating it, so limit max_segments to 1 lying
2657 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 */
NeilBrown627a2d32010-03-08 16:44:38 +11002659 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2660 blk_queue_max_segments(mddev->queue, 1);
2661 blk_queue_segment_boundary(mddev->queue,
2662 PAGE_CACHE_SIZE - 1);
2663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
NeilBrown6d508242005-09-09 16:24:03 -07002667 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10002668 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10002669 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002670 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 goto out_free_conf;
2672 }
2673
2674 mddev->degraded = 0;
2675 for (i = 0; i < conf->raid_disks; i++) {
2676
2677 disk = conf->mirrors + i;
2678
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002679 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002680 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 disk->head_position = 0;
2682 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002683 if (disk->rdev)
2684 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
2686 }
2687
Andre Noll8c6ac8682009-06-18 08:48:06 +10002688 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002689 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10002690 " -- starting background reconstruction\n",
2691 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002693 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002694 mdname(mddev), conf->raid_disks - mddev->degraded,
2695 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 /*
2697 * Ok, everything is just fine now
2698 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002699 mddev->dev_sectors = conf->dev_sectors;
2700 size = raid10_size(mddev, 0, 0);
2701 md_set_array_sectors(mddev, size);
2702 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
NeilBrown0d129222006-10-03 01:15:54 -07002704 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2705 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /* Calculate max read-ahead size.
2708 * We need to readahead at least twice a whole stripe....
2709 * maybe...
2710 */
2711 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002712 int stripe = conf->raid_disks *
2713 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 stripe /= conf->near_copies;
2715 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2716 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2717 }
2718
NeilBrown84707f32010-03-16 17:23:35 +11002719 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002721
2722 if (md_integrity_register(mddev))
2723 goto out_free_conf;
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return 0;
2726
2727out_free_conf:
NeilBrown589a5942010-12-09 17:02:14 +11002728 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 if (conf->r10bio_pool)
2730 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002731 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002732 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 kfree(conf);
2734 mddev->private = NULL;
2735out:
2736 return -EIO;
2737}
2738
2739static int stop(mddev_t *mddev)
2740{
NeilBrown070ec552009-06-16 16:54:21 +10002741 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
NeilBrown409c57f2009-03-31 14:39:39 +11002743 raise_barrier(conf, 0);
2744 lower_barrier(conf);
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 md_unregister_thread(mddev->thread);
2747 mddev->thread = NULL;
2748 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2749 if (conf->r10bio_pool)
2750 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002751 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 kfree(conf);
2753 mddev->private = NULL;
2754 return 0;
2755}
2756
NeilBrown6cce3b22006-01-06 00:20:16 -08002757static void raid10_quiesce(mddev_t *mddev, int state)
2758{
NeilBrown070ec552009-06-16 16:54:21 +10002759 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002760
2761 switch(state) {
2762 case 1:
2763 raise_barrier(conf, 0);
2764 break;
2765 case 0:
2766 lower_barrier(conf);
2767 break;
2768 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Trela, Maciejdab8b292010-03-08 16:02:45 +11002771static void *raid10_takeover_raid0(mddev_t *mddev)
2772{
2773 mdk_rdev_t *rdev;
2774 conf_t *conf;
2775
2776 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10002777 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2778 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002779 return ERR_PTR(-EINVAL);
2780 }
2781
Trela, Maciejdab8b292010-03-08 16:02:45 +11002782 /* Set new parameters */
2783 mddev->new_level = 10;
2784 /* new layout: far_copies = 1, near_copies = 2 */
2785 mddev->new_layout = (1<<8) + 2;
2786 mddev->new_chunk_sectors = mddev->chunk_sectors;
2787 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002788 mddev->raid_disks *= 2;
2789 /* make sure it will be not marked as dirty */
2790 mddev->recovery_cp = MaxSector;
2791
2792 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002793 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01002794 list_for_each_entry(rdev, &mddev->disks, same_set)
2795 if (rdev->raid_disk >= 0)
2796 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002797 conf->barrier = 1;
2798 }
2799
Trela, Maciejdab8b292010-03-08 16:02:45 +11002800 return conf;
2801}
2802
2803static void *raid10_takeover(mddev_t *mddev)
2804{
2805 struct raid0_private_data *raid0_priv;
2806
2807 /* raid10 can take over:
2808 * raid0 - providing it has only two drives
2809 */
2810 if (mddev->level == 0) {
2811 /* for raid0 takeover only one zone is supported */
2812 raid0_priv = mddev->private;
2813 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10002814 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2815 " with more than one zone.\n",
2816 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002817 return ERR_PTR(-EINVAL);
2818 }
2819 return raid10_takeover_raid0(mddev);
2820 }
2821 return ERR_PTR(-EINVAL);
2822}
2823
NeilBrown2604b702006-01-06 00:20:36 -08002824static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002827 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 .owner = THIS_MODULE,
2829 .make_request = make_request,
2830 .run = run,
2831 .stop = stop,
2832 .status = status,
2833 .error_handler = error,
2834 .hot_add_disk = raid10_add_disk,
2835 .hot_remove_disk= raid10_remove_disk,
2836 .spare_active = raid10_spare_active,
2837 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002838 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002839 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002840 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841};
2842
2843static int __init raid_init(void)
2844{
NeilBrown2604b702006-01-06 00:20:36 -08002845 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848static void raid_exit(void)
2849{
NeilBrown2604b702006-01-06 00:20:36 -08002850 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
2853module_init(raid_init);
2854module_exit(raid_exit);
2855MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002856MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002858MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002859MODULE_ALIAS("md-level-10");