blob: 13077a3fd7d2fa610c657e8cd02ed347faedd915 [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;
NeilBrown0eb3ff12006-01-06 00:20:29 -0800184 if (*bio && *bio != IO_BLOCKED)
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 */
270static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio, struct bio *bio)
271{
272 int slot;
273
274 for (slot = 0; slot < conf->copies; slot++)
275 if (r10_bio->devs[slot].bio == bio)
276 break;
277
278 BUG_ON(slot == conf->copies);
279 update_head_pos(slot, r10_bio);
280
281 return r10_bio->devs[slot].devnum;
282}
283
NeilBrown6712ecf2007-09-27 12:47:43 +0200284static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100287 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000289 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 slot = r10_bio->read_slot;
293 dev = r10_bio->devs[slot].devnum;
294 /*
295 * this branch is our 'one mirror IO has finished' event handler:
296 */
NeilBrown4443ae12006-01-06 00:20:28 -0800297 update_head_pos(slot, r10_bio);
298
299 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
301 * Set R10BIO_Uptodate in our master bio, so that
302 * we will return a good error code to the higher
303 * levels even if IO on some other mirrored buffer fails.
304 *
305 * The 'master' represents the composite IO operation to
306 * user-side. So if something waits for IO, then it will
307 * wait for the 'master' bio.
308 */
309 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 raid_end_bio_io(r10_bio);
NeilBrown7c4e06f2011-05-11 14:53:17 +1000311 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800312 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000314 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000317 printk_ratelimited(KERN_ERR
318 "md/raid10:%s: %s: rescheduling sector %llu\n",
319 mdname(conf->mddev),
320 bdevname(conf->mirrors[dev].rdev->bdev, b),
321 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000322 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 reschedule_retry(r10_bio);
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
NeilBrown6712ecf2007-09-27 12:47:43 +0200327static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100330 r10bio_t *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000331 int dev;
NeilBrown070ec552009-06-16 16:54:21 +1000332 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Namhyung Kim778ca012011-07-18 17:38:47 +1000334 dev = find_bio_disk(conf, r10_bio, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /*
337 * this branch is our 'one mirror IO has finished' event handler:
338 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800339 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800341 /* an I/O failed, we can't clear the bitmap */
342 set_bit(R10BIO_Degraded, &r10_bio->state);
343 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
345 * Set R10BIO_Uptodate in our master bio, so that
346 * we will return a good error code for to the higher
347 * levels even if IO on some other mirrored buffer fails.
348 *
349 * The 'master' represents the composite IO operation to
350 * user-side. So if something waits for IO, then it will
351 * wait for the 'master' bio.
352 */
353 set_bit(R10BIO_Uptodate, &r10_bio->state);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 *
357 * Let's see if all mirrored write operations have finished
358 * already.
359 */
360 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800361 /* clear the bitmap if all writes complete successfully */
362 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
363 r10_bio->sectors,
364 !test_bit(R10BIO_Degraded, &r10_bio->state),
365 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 md_write_end(r10_bio->mddev);
367 raid_end_bio_io(r10_bio);
368 }
369
370 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373
374/*
375 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300376 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * parameters: near_copies and far_copies.
378 * near_copies * far_copies must be <= raid_disks.
379 * Normally one of these will be 1.
380 * If both are 1, we get raid0.
381 * If near_copies == raid_disks, we get raid1.
382 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300383 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * first chunk, followed by near_copies copies of the next chunk and
385 * so on.
386 * If far_copies > 1, then after 1/far_copies of the array has been assigned
387 * as described above, we start again with a device offset of near_copies.
388 * So we effectively have another copy of the whole array further down all
389 * the drives, but with blocks on different drives.
390 * With this layout, and block is never stored twice on the one device.
391 *
392 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700393 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *
395 * raid10_find_virt does the reverse mapping, from a device and a
396 * sector offset to a virtual address
397 */
398
399static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
400{
401 int n,f;
402 sector_t sector;
403 sector_t chunk;
404 sector_t stripe;
405 int dev;
406
407 int slot = 0;
408
409 /* now calculate first sector/dev */
410 chunk = r10bio->sector >> conf->chunk_shift;
411 sector = r10bio->sector & conf->chunk_mask;
412
413 chunk *= conf->near_copies;
414 stripe = chunk;
415 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700416 if (conf->far_offset)
417 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 sector += stripe << conf->chunk_shift;
420
421 /* and calculate all the others */
422 for (n=0; n < conf->near_copies; n++) {
423 int d = dev;
424 sector_t s = sector;
425 r10bio->devs[slot].addr = sector;
426 r10bio->devs[slot].devnum = d;
427 slot++;
428
429 for (f = 1; f < conf->far_copies; f++) {
430 d += conf->near_copies;
431 if (d >= conf->raid_disks)
432 d -= conf->raid_disks;
433 s += conf->stride;
434 r10bio->devs[slot].devnum = d;
435 r10bio->devs[slot].addr = s;
436 slot++;
437 }
438 dev++;
439 if (dev >= conf->raid_disks) {
440 dev = 0;
441 sector += (conf->chunk_mask + 1);
442 }
443 }
444 BUG_ON(slot != conf->copies);
445}
446
447static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
448{
449 sector_t offset, chunk, vchunk;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700452 if (conf->far_offset) {
453 int fc;
454 chunk = sector >> conf->chunk_shift;
455 fc = sector_div(chunk, conf->far_copies);
456 dev -= fc * conf->near_copies;
457 if (dev < 0)
458 dev += conf->raid_disks;
459 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800460 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700461 sector -= conf->stride;
462 if (dev < conf->near_copies)
463 dev += conf->raid_disks - conf->near_copies;
464 else
465 dev -= conf->near_copies;
466 }
467 chunk = sector >> conf->chunk_shift;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 vchunk = chunk * conf->raid_disks + dev;
470 sector_div(vchunk, conf->near_copies);
471 return (vchunk << conf->chunk_shift) + offset;
472}
473
474/**
475 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
476 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200477 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * @biovec: the request that could be merged to it.
479 *
480 * Return amount of bytes we can accept at this offset
481 * If near_copies == raid_disk, there are no striping issues,
482 * but in that case, the function isn't called at all.
483 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200484static int raid10_mergeable_bvec(struct request_queue *q,
485 struct bvec_merge_data *bvm,
486 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200489 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000491 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200492 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
495 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200496 if (max <= biovec->bv_len && bio_sectors == 0)
497 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 else
499 return max;
500}
501
502/*
503 * This routine returns the disk from which the requested read should
504 * be done. There is a per-array 'next expected sequential IO' sector
505 * number - if this matches on the next IO then we use the last disk.
506 * There is also a per-disk 'last know head position' sector that is
507 * maintained from IRQ contexts, both the normal and the resync IO
508 * completion handlers update this position correctly. If there is no
509 * perfect sequential match then we pick the disk whose head is closest.
510 *
511 * If there are 2 mirrors in the same 2 devices, performance degrades
512 * because position is mirror, not device based.
513 *
514 * The rdev for the device selected will have nr_pending incremented.
515 */
516
517/*
518 * FIXME: possibly should rethink readbalancing and do it differently
519 * depending on near_copies / far_copies geometry.
520 */
NeilBrown856e08e2011-07-28 11:39:23 +1000521static int read_balance(conf_t *conf, r10bio_t *r10_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000523 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000524 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000525 int sectors = r10_bio->sectors;
526 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000527 sector_t new_distance, best_dist;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800528 mdk_rdev_t *rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000529 int do_balance;
530 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 raid10_find_phys(conf, r10_bio);
533 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000534retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000535 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000536 best_slot = -1;
537 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000538 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000539 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800542 * device if no resync is going on (recovery is ok), or below
543 * the resync window. We take the first readable disk when
544 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
546 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000547 && (this_sector + sectors >= conf->next_resync))
548 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
NeilBrown56d99122011-05-11 14:27:03 +1000550 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000551 sector_t first_bad;
552 int bad_sectors;
553 sector_t dev_sector;
554
NeilBrown56d99122011-05-11 14:27:03 +1000555 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000557 disk = r10_bio->devs[slot].devnum;
558 rdev = rcu_dereference(conf->mirrors[disk].rdev);
559 if (rdev == NULL)
560 continue;
561 if (!test_bit(In_sync, &rdev->flags))
562 continue;
563
NeilBrown856e08e2011-07-28 11:39:23 +1000564 dev_sector = r10_bio->devs[slot].addr;
565 if (is_badblock(rdev, dev_sector, sectors,
566 &first_bad, &bad_sectors)) {
567 if (best_dist < MaxSector)
568 /* Already have a better slot */
569 continue;
570 if (first_bad <= dev_sector) {
571 /* Cannot read here. If this is the
572 * 'primary' device, then we must not read
573 * beyond 'bad_sectors' from another device.
574 */
575 bad_sectors -= (dev_sector - first_bad);
576 if (!do_balance && sectors > bad_sectors)
577 sectors = bad_sectors;
578 if (best_good_sectors > sectors)
579 best_good_sectors = sectors;
580 } else {
581 sector_t good_sectors =
582 first_bad - dev_sector;
583 if (good_sectors > best_good_sectors) {
584 best_good_sectors = good_sectors;
585 best_slot = slot;
586 }
587 if (!do_balance)
588 /* Must read from here */
589 break;
590 }
591 continue;
592 } else
593 best_good_sectors = sectors;
594
NeilBrown56d99122011-05-11 14:27:03 +1000595 if (!do_balance)
596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
NeilBrown22dfdf52005-11-28 13:44:09 -0800598 /* This optimisation is debatable, and completely destroys
599 * sequential read speed for 'far copies' arrays. So only
600 * keep it for 'near' arrays, and review those later.
601 */
NeilBrown56d99122011-05-11 14:27:03 +1000602 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800604
605 /* for far > 1 always use the lowest address */
606 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000607 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800608 else
NeilBrown56d99122011-05-11 14:27:03 +1000609 new_distance = abs(r10_bio->devs[slot].addr -
610 conf->mirrors[disk].head_position);
611 if (new_distance < best_dist) {
612 best_dist = new_distance;
613 best_slot = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 }
NeilBrown56d99122011-05-11 14:27:03 +1000616 if (slot == conf->copies)
617 slot = best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
NeilBrown56d99122011-05-11 14:27:03 +1000619 if (slot >= 0) {
620 disk = r10_bio->devs[slot].devnum;
621 rdev = rcu_dereference(conf->mirrors[disk].rdev);
622 if (!rdev)
623 goto retry;
624 atomic_inc(&rdev->nr_pending);
625 if (test_bit(Faulty, &rdev->flags)) {
626 /* Cannot risk returning a device that failed
627 * before we inc'ed nr_pending
628 */
629 rdev_dec_pending(rdev, conf->mddev);
630 goto retry;
631 }
632 r10_bio->read_slot = slot;
633 } else
NeilBrown29fc7e32006-02-03 03:03:41 -0800634 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000636 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 return disk;
639}
640
NeilBrown0d129222006-10-03 01:15:54 -0700641static int raid10_congested(void *data, int bits)
642{
643 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000644 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700645 int i, ret = 0;
646
NeilBrown3fa841d2009-09-23 18:10:29 +1000647 if (mddev_congested(mddev, bits))
648 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700649 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100650 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700651 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
652 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200653 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700654
655 ret |= bdi_congested(&q->backing_dev_info, bits);
656 }
657 }
658 rcu_read_unlock();
659 return ret;
660}
661
Jens Axboe7eaceac2011-03-10 08:52:07 +0100662static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800663{
664 /* Any writes that have been queued but are awaiting
665 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800666 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800667 spin_lock_irq(&conf->device_lock);
668
669 if (conf->pending_bio_list.head) {
670 struct bio *bio;
671 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800672 spin_unlock_irq(&conf->device_lock);
673 /* flush any pending bitmap writes to disk
674 * before proceeding w/ I/O */
675 bitmap_unplug(conf->mddev->bitmap);
676
677 while (bio) { /* submit pending writes */
678 struct bio *next = bio->bi_next;
679 bio->bi_next = NULL;
680 generic_make_request(bio);
681 bio = next;
682 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800683 } else
684 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800685}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100686
NeilBrown0a27ec92006-01-06 00:20:13 -0800687/* Barriers....
688 * Sometimes we need to suspend IO while we do something else,
689 * either some resync/recovery, or reconfigure the array.
690 * To do this we raise a 'barrier'.
691 * The 'barrier' is a counter that can be raised multiple times
692 * to count how many activities are happening which preclude
693 * normal IO.
694 * We can only raise the barrier if there is no pending IO.
695 * i.e. if nr_pending == 0.
696 * We choose only to raise the barrier if no-one is waiting for the
697 * barrier to go down. This means that as soon as an IO request
698 * is ready, no other operations which require a barrier will start
699 * until the IO request has had a chance.
700 *
701 * So: regular IO calls 'wait_barrier'. When that returns there
702 * is no backgroup IO happening, It must arrange to call
703 * allow_barrier when it has finished its IO.
704 * backgroup IO calls must call raise_barrier. Once that returns
705 * there is no normal IO happeing. It must arrange to call
706 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
NeilBrown6cce3b22006-01-06 00:20:16 -0800709static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
NeilBrown6cce3b22006-01-06 00:20:16 -0800711 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
NeilBrown6cce3b22006-01-06 00:20:16 -0800714 /* Wait until no block IO is waiting (unless 'force') */
715 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000716 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800717
718 /* block any new IO from starting */
719 conf->barrier++;
720
NeilBrownc3b328a2011-04-18 18:25:43 +1000721 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800722 wait_event_lock_irq(conf->wait_barrier,
723 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000724 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 spin_unlock_irq(&conf->resync_lock);
727}
728
NeilBrown0a27ec92006-01-06 00:20:13 -0800729static void lower_barrier(conf_t *conf)
730{
731 unsigned long flags;
732 spin_lock_irqsave(&conf->resync_lock, flags);
733 conf->barrier--;
734 spin_unlock_irqrestore(&conf->resync_lock, flags);
735 wake_up(&conf->wait_barrier);
736}
737
738static void wait_barrier(conf_t *conf)
739{
740 spin_lock_irq(&conf->resync_lock);
741 if (conf->barrier) {
742 conf->nr_waiting++;
743 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
744 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000745 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800746 conf->nr_waiting--;
747 }
748 conf->nr_pending++;
749 spin_unlock_irq(&conf->resync_lock);
750}
751
752static void allow_barrier(conf_t *conf)
753{
754 unsigned long flags;
755 spin_lock_irqsave(&conf->resync_lock, flags);
756 conf->nr_pending--;
757 spin_unlock_irqrestore(&conf->resync_lock, flags);
758 wake_up(&conf->wait_barrier);
759}
760
NeilBrown4443ae12006-01-06 00:20:28 -0800761static void freeze_array(conf_t *conf)
762{
763 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800764 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800765 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800766 * wait until nr_pending match nr_queued+1
767 * This is called in the context of one normal IO request
768 * that has failed. Thus any sync request that might be pending
769 * will be blocked by nr_pending, and we need to wait for
770 * pending IO requests to complete or be queued for re-try.
771 * Thus the number queued (nr_queued) plus this request (1)
772 * must match the number of pending IOs (nr_pending) before
773 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800774 */
775 spin_lock_irq(&conf->resync_lock);
776 conf->barrier++;
777 conf->nr_waiting++;
778 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800779 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800780 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000781 flush_pending_writes(conf));
782
NeilBrown4443ae12006-01-06 00:20:28 -0800783 spin_unlock_irq(&conf->resync_lock);
784}
785
786static void unfreeze_array(conf_t *conf)
787{
788 /* reverse the effect of the freeze */
789 spin_lock_irq(&conf->resync_lock);
790 conf->barrier--;
791 conf->nr_waiting--;
792 wake_up(&conf->wait_barrier);
793 spin_unlock_irq(&conf->resync_lock);
794}
795
NeilBrown21a52c62010-04-01 15:02:13 +1100796static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
NeilBrown070ec552009-06-16 16:54:21 +1000798 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 mirror_info_t *mirror;
800 r10bio_t *r10_bio;
801 struct bio *read_bio;
802 int i;
803 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100804 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000805 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200806 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800807 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700808 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000809 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000810 int sectors_handled;
811 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Tejun Heoe9c74692010-09-03 11:56:18 +0200813 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
814 md_flush_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700815 return 0;
816 }
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* If this request crosses a chunk boundary, we need to
819 * split it. This will only happen for 1 PAGE (or less) requests.
820 */
821 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
822 > chunk_sects &&
823 conf->near_copies < conf->raid_disks)) {
824 struct bio_pair *bp;
825 /* Sanity check -- queue functions should prevent this happening */
826 if (bio->bi_vcnt != 1 ||
827 bio->bi_idx != 0)
828 goto bad_map;
829 /* This is a one page bio that upper layers
830 * refuse to split for us, so we need to split it.
831 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200832 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000834
835 /* Each of these 'make_request' calls will call 'wait_barrier'.
836 * If the first succeeds but the second blocks due to the resync
837 * thread raising the barrier, we will deadlock because the
838 * IO to the underlying device will be queued in generic_make_request
839 * and will never complete, so will never reduce nr_pending.
840 * So increment nr_waiting here so no new raise_barriers will
841 * succeed, and so the second wait_barrier cannot block.
842 */
843 spin_lock_irq(&conf->resync_lock);
844 conf->nr_waiting++;
845 spin_unlock_irq(&conf->resync_lock);
846
NeilBrown21a52c62010-04-01 15:02:13 +1100847 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100849 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 generic_make_request(&bp->bio2);
851
NeilBrown51e9ac72010-08-07 21:17:00 +1000852 spin_lock_irq(&conf->resync_lock);
853 conf->nr_waiting--;
854 wake_up(&conf->wait_barrier);
855 spin_unlock_irq(&conf->resync_lock);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 bio_pair_release(bp);
858 return 0;
859 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000860 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
861 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
863
NeilBrown6712ecf2007-09-27 12:47:43 +0200864 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866 }
867
NeilBrown3d310eb2005-06-21 17:17:26 -0700868 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /*
871 * Register the new request and wait if the reconstruction
872 * thread has put up a bar for new requests.
873 * Continue immediately if no resync is active currently.
874 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800875 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
878
879 r10_bio->master_bio = bio;
880 r10_bio->sectors = bio->bi_size >> 9;
881
882 r10_bio->mddev = mddev;
883 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800884 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
NeilBrown856e08e2011-07-28 11:39:23 +1000886 /* We might need to issue multiple reads to different
887 * devices if there are bad blocks around, so we keep
888 * track of the number of reads in bio->bi_phys_segments.
889 * If this is 0, there is only one r10_bio and no locking
890 * will be needed when the request completes. If it is
891 * non-zero, then it is the number of not-completed requests.
892 */
893 bio->bi_phys_segments = 0;
894 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
895
Jens Axboea3623572005-11-01 09:26:16 +0100896 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
898 * read balancing logic:
899 */
NeilBrown856e08e2011-07-28 11:39:23 +1000900 int disk;
901 int slot;
902
903read_again:
904 disk = read_balance(conf, r10_bio, &max_sectors);
905 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (disk < 0) {
907 raid_end_bio_io(r10_bio);
908 return 0;
909 }
910 mirror = conf->mirrors + disk;
911
NeilBrowna167f662010-10-26 18:31:13 +1100912 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +1000913 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
914 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 r10_bio->devs[slot].bio = read_bio;
917
918 read_bio->bi_sector = r10_bio->devs[slot].addr +
919 mirror->rdev->data_offset;
920 read_bio->bi_bdev = mirror->rdev->bdev;
921 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200922 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 read_bio->bi_private = r10_bio;
924
NeilBrown856e08e2011-07-28 11:39:23 +1000925 if (max_sectors < r10_bio->sectors) {
926 /* Could not read all from this device, so we will
927 * need another r10_bio.
928 */
NeilBrown856e08e2011-07-28 11:39:23 +1000929 sectors_handled = (r10_bio->sectors + max_sectors
930 - bio->bi_sector);
931 r10_bio->sectors = max_sectors;
932 spin_lock_irq(&conf->device_lock);
933 if (bio->bi_phys_segments == 0)
934 bio->bi_phys_segments = 2;
935 else
936 bio->bi_phys_segments++;
937 spin_unlock(&conf->device_lock);
938 /* Cannot call generic_make_request directly
939 * as that will be queued in __generic_make_request
940 * and subsequent mempool_alloc might block
941 * waiting for it. so hand bio over to raid10d.
942 */
943 reschedule_retry(r10_bio);
944
945 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
946
947 r10_bio->master_bio = bio;
948 r10_bio->sectors = ((bio->bi_size >> 9)
949 - sectors_handled);
950 r10_bio->state = 0;
951 r10_bio->mddev = mddev;
952 r10_bio->sector = bio->bi_sector + sectors_handled;
953 goto read_again;
954 } else
955 generic_make_request(read_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 0;
957 }
958
959 /*
960 * WRITE:
961 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700962 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * inc refcount on their rdev. Record them by setting
964 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +1000965 * If there are known/acknowledged bad blocks on any device
966 * on which we have seen a write error, we want to avoid
967 * writing to those blocks. This potentially requires several
968 * writes to write around the bad blocks. Each set of writes
969 * gets its own r10_bio with a set of bios attached. The number
970 * of r10_bios is recored in bio->bi_phys_segments just as with
971 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000973 plugged = mddev_check_plugged(mddev);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +1000976retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700977 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +1000979 max_sectors = r10_bio->sectors;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 for (i = 0; i < conf->copies; i++) {
982 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800983 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700984 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
985 atomic_inc(&rdev->nr_pending);
986 blocked_rdev = rdev;
987 break;
988 }
NeilBrownd4432c22011-07-28 11:39:24 +1000989 r10_bio->devs[i].bio = NULL;
990 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800991 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +1000992 continue;
NeilBrown6cce3b22006-01-06 00:20:16 -0800993 }
NeilBrownd4432c22011-07-28 11:39:24 +1000994 if (test_bit(WriteErrorSeen, &rdev->flags)) {
995 sector_t first_bad;
996 sector_t dev_sector = r10_bio->devs[i].addr;
997 int bad_sectors;
998 int is_bad;
999
1000 is_bad = is_badblock(rdev, dev_sector,
1001 max_sectors,
1002 &first_bad, &bad_sectors);
1003 if (is_bad < 0) {
1004 /* Mustn't write here until the bad block
1005 * is acknowledged
1006 */
1007 atomic_inc(&rdev->nr_pending);
1008 set_bit(BlockedBadBlocks, &rdev->flags);
1009 blocked_rdev = rdev;
1010 break;
1011 }
1012 if (is_bad && first_bad <= dev_sector) {
1013 /* Cannot write here at all */
1014 bad_sectors -= (dev_sector - first_bad);
1015 if (bad_sectors < max_sectors)
1016 /* Mustn't write more than bad_sectors
1017 * to other devices yet
1018 */
1019 max_sectors = bad_sectors;
1020 /* We don't set R10BIO_Degraded as that
1021 * only applies if the disk is missing,
1022 * so it might be re-added, and we want to
1023 * know to recover this chunk.
1024 * In this case the device is here, and the
1025 * fact that this chunk is not in-sync is
1026 * recorded in the bad block log.
1027 */
1028 continue;
1029 }
1030 if (is_bad) {
1031 int good_sectors = first_bad - dev_sector;
1032 if (good_sectors < max_sectors)
1033 max_sectors = good_sectors;
1034 }
1035 }
1036 r10_bio->devs[i].bio = bio;
1037 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039 rcu_read_unlock();
1040
Dan Williams6bfe0b42008-04-30 00:52:32 -07001041 if (unlikely(blocked_rdev)) {
1042 /* Have to wait for this device to get unblocked, then retry */
1043 int j;
1044 int d;
1045
1046 for (j = 0; j < i; j++)
1047 if (r10_bio->devs[j].bio) {
1048 d = r10_bio->devs[j].devnum;
1049 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1050 }
1051 allow_barrier(conf);
1052 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1053 wait_barrier(conf);
1054 goto retry_write;
1055 }
1056
NeilBrownd4432c22011-07-28 11:39:24 +10001057 if (max_sectors < r10_bio->sectors) {
1058 /* We are splitting this into multiple parts, so
1059 * we need to prepare for allocating another r10_bio.
1060 */
1061 r10_bio->sectors = max_sectors;
1062 spin_lock_irq(&conf->device_lock);
1063 if (bio->bi_phys_segments == 0)
1064 bio->bi_phys_segments = 2;
1065 else
1066 bio->bi_phys_segments++;
1067 spin_unlock_irq(&conf->device_lock);
1068 }
1069 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1070
NeilBrown4e780642010-10-19 12:54:01 +11001071 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001072 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 for (i = 0; i < conf->copies; i++) {
1075 struct bio *mbio;
1076 int d = r10_bio->devs[i].devnum;
1077 if (!r10_bio->devs[i].bio)
1078 continue;
1079
NeilBrowna167f662010-10-26 18:31:13 +11001080 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001081 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1082 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 r10_bio->devs[i].bio = mbio;
1084
NeilBrownd4432c22011-07-28 11:39:24 +10001085 mbio->bi_sector = (r10_bio->devs[i].addr+
1086 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1088 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001089 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 mbio->bi_private = r10_bio;
1091
1092 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001093 spin_lock_irqsave(&conf->device_lock, flags);
1094 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +11001095 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
NeilBrown4e780642010-10-19 12:54:01 +11001098 if (atomic_dec_and_test(&r10_bio->remaining)) {
1099 /* This matches the end of raid10_end_write_request() */
1100 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
1101 r10_bio->sectors,
1102 !test_bit(R10BIO_Degraded, &r10_bio->state),
1103 0);
Arne Redlichf6f953a2007-07-31 00:37:57 -07001104 md_write_end(mddev);
1105 raid_end_bio_io(r10_bio);
Arne Redlichf6f953a2007-07-31 00:37:57 -07001106 }
1107
NeilBrowna35e63e2008-03-04 14:29:29 -08001108 /* In case raid10d snuck in to freeze_array */
1109 wake_up(&conf->wait_barrier);
1110
NeilBrownd4432c22011-07-28 11:39:24 +10001111 if (sectors_handled < (bio->bi_size >> 9)) {
1112 /* We need another r1_bio. It has already been counted
1113 * in bio->bi_phys_segments.
1114 */
1115 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1116
1117 r10_bio->master_bio = bio;
1118 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1119
1120 r10_bio->mddev = mddev;
1121 r10_bio->sector = bio->bi_sector + sectors_handled;
1122 r10_bio->state = 0;
1123 goto retry_write;
1124 }
1125
NeilBrownc3b328a2011-04-18 18:25:43 +10001126 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001127 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return 0;
1129}
1130
1131static void status(struct seq_file *seq, mddev_t *mddev)
1132{
NeilBrown070ec552009-06-16 16:54:21 +10001133 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 int i;
1135
1136 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001137 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (conf->near_copies > 1)
1139 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001140 if (conf->far_copies > 1) {
1141 if (conf->far_offset)
1142 seq_printf(seq, " %d offset-copies", conf->far_copies);
1143 else
1144 seq_printf(seq, " %d far-copies", conf->far_copies);
1145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001147 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 for (i = 0; i < conf->raid_disks; i++)
1149 seq_printf(seq, "%s",
1150 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001151 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 seq_printf(seq, "]");
1153}
1154
NeilBrown700c7212011-07-27 11:00:36 +10001155/* check if there are enough drives for
1156 * every block to appear on atleast one.
1157 * Don't consider the device numbered 'ignore'
1158 * as we might be about to remove it.
1159 */
1160static int enough(conf_t *conf, int ignore)
1161{
1162 int first = 0;
1163
1164 do {
1165 int n = conf->copies;
1166 int cnt = 0;
1167 while (n--) {
1168 if (conf->mirrors[first].rdev &&
1169 first != ignore)
1170 cnt++;
1171 first = (first+1) % conf->raid_disks;
1172 }
1173 if (cnt == 0)
1174 return 0;
1175 } while (first != 0);
1176 return 1;
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1180{
1181 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001182 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 /*
1185 * If it is not operational, then we have already marked it as dead
1186 * else if it is the last working disks, ignore the error, let the
1187 * next level up know.
1188 * else mark the drive as failed
1189 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001190 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001191 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /*
1193 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
1195 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001196 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1197 unsigned long flags;
1198 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001200 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /*
1202 * if recovery is running, make sure it aborts.
1203 */
NeilBrowndfc70642008-05-23 13:04:39 -07001204 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
NeilBrownde393cd2011-07-28 11:31:48 +10001206 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001207 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001208 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001209 printk(KERN_ALERT
1210 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1211 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001212 mdname(mddev), bdevname(rdev->bdev, b),
1213 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static void print_conf(conf_t *conf)
1217{
1218 int i;
1219 mirror_info_t *tmp;
1220
NeilBrown128595e2010-05-03 14:47:14 +10001221 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001223 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return;
1225 }
NeilBrown128595e2010-05-03 14:47:14 +10001226 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 conf->raid_disks);
1228
1229 for (i = 0; i < conf->raid_disks; i++) {
1230 char b[BDEVNAME_SIZE];
1231 tmp = conf->mirrors + i;
1232 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001233 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001234 i, !test_bit(In_sync, &tmp->rdev->flags),
1235 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 bdevname(tmp->rdev->bdev,b));
1237 }
1238}
1239
1240static void close_sync(conf_t *conf)
1241{
NeilBrown0a27ec92006-01-06 00:20:13 -08001242 wait_barrier(conf);
1243 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 mempool_destroy(conf->r10buf_pool);
1246 conf->r10buf_pool = NULL;
1247}
1248
1249static int raid10_spare_active(mddev_t *mddev)
1250{
1251 int i;
1252 conf_t *conf = mddev->private;
1253 mirror_info_t *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001254 int count = 0;
1255 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /*
1258 * Find all non-in_sync disks within the RAID10 configuration
1259 * and mark them in_sync
1260 */
1261 for (i = 0; i < conf->raid_disks; i++) {
1262 tmp = conf->mirrors + i;
1263 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001264 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001265 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001266 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001267 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 }
NeilBrown6b965622010-08-18 11:56:59 +10001270 spin_lock_irqsave(&conf->device_lock, flags);
1271 mddev->degraded -= count;
1272 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001275 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278
1279static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1280{
1281 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001282 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001284 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001285 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 if (mddev->recovery_cp < MaxSector)
1288 /* only hot-add to in-sync arrays, as recovery is
1289 * very different from resync
1290 */
Neil Brown199050e2008-06-28 08:31:33 +10001291 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001292 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
NeilBrowna53a6c82008-11-06 17:28:20 +11001295 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001296 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001298 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001299 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1300 mirror = rdev->saved_raid_disk;
1301 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001302 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001303 for ( ; mirror <= last ; mirror++) {
1304 mirror_info_t *p = &conf->mirrors[mirror];
1305 if (p->recovery_disabled == mddev->recovery_disabled)
1306 continue;
1307 if (!p->rdev)
1308 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
NeilBrown2bb77732011-07-27 11:00:36 +10001310 disk_stack_limits(mddev->gendisk, rdev->bdev,
1311 rdev->data_offset << 9);
1312 /* as we don't honour merge_bvec_fn, we must
1313 * never risk violating it, so limit
1314 * ->max_segments to one lying with a single
1315 * page, as a one page request is never in
1316 * violation.
1317 */
1318 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1319 blk_queue_max_segments(mddev->queue, 1);
1320 blk_queue_segment_boundary(mddev->queue,
1321 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
NeilBrown2bb77732011-07-27 11:00:36 +10001324 p->head_position = 0;
1325 rdev->raid_disk = mirror;
1326 err = 0;
1327 if (rdev->saved_raid_disk != mirror)
1328 conf->fullsync = 1;
1329 rcu_assign_pointer(p->rdev, rdev);
1330 break;
1331 }
1332
Andre Nollac5e7112009-08-03 10:59:47 +10001333 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338static int raid10_remove_disk(mddev_t *mddev, int number)
1339{
1340 conf_t *conf = mddev->private;
1341 int err = 0;
1342 mdk_rdev_t *rdev;
1343 mirror_info_t *p = conf->mirrors+ number;
1344
1345 print_conf(conf);
1346 rdev = p->rdev;
1347 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001348 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 atomic_read(&rdev->nr_pending)) {
1350 err = -EBUSY;
1351 goto abort;
1352 }
NeilBrowndfc70642008-05-23 13:04:39 -07001353 /* Only remove faulty devices in recovery
1354 * is not possible.
1355 */
1356 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown2bb77732011-07-27 11:00:36 +10001357 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown700c7212011-07-27 11:00:36 +10001358 enough(conf, -1)) {
NeilBrowndfc70642008-05-23 13:04:39 -07001359 err = -EBUSY;
1360 goto abort;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001363 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (atomic_read(&rdev->nr_pending)) {
1365 /* lost the race, try later */
1366 err = -EBUSY;
1367 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001368 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001370 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372abort:
1373
1374 print_conf(conf);
1375 return err;
1376}
1377
1378
NeilBrown6712ecf2007-09-27 12:47:43 +02001379static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001381 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001382 conf_t *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001383 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Namhyung Kim778ca012011-07-18 17:38:47 +10001385 d = find_bio_disk(conf, r10_bio, bio);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001386
1387 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1388 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001389 else {
1390 atomic_add(r10_bio->sectors,
1391 &conf->mirrors[d].rdev->corrected_errors);
1392 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1393 md_error(r10_bio->mddev,
1394 conf->mirrors[d].rdev);
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 /* for reconstruct, we always reschedule after a read.
1398 * for resync, only after all reads
1399 */
NeilBrown73d5c382009-02-25 13:18:47 +11001400 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1402 atomic_dec_and_test(&r10_bio->remaining)) {
1403 /* we have read all the blocks,
1404 * do the comparison in process context in raid10d
1405 */
1406 reschedule_retry(r10_bio);
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
NeilBrown6712ecf2007-09-27 12:47:43 +02001410static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001413 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001415 conf_t *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001416 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Namhyung Kim778ca012011-07-18 17:38:47 +10001418 d = find_bio_disk(conf, r10_bio, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 if (!uptodate)
1421 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001422
NeilBrown73d5c382009-02-25 13:18:47 +11001423 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 while (atomic_dec_and_test(&r10_bio->remaining)) {
1425 if (r10_bio->master_bio == NULL) {
1426 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001427 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001429 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 break;
1431 } else {
1432 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1433 put_buf(r10_bio);
1434 r10_bio = r10_bio2;
1435 }
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439/*
1440 * Note: sync and recover and handled very differently for raid10
1441 * This code is for resync.
1442 * For resync, we read through virtual addresses and read all blocks.
1443 * If there is any error, we schedule a write. The lowest numbered
1444 * drive is authoritative.
1445 * However requests come for physical address, so we need to map.
1446 * For every physical address there are raid_disks/copies virtual addresses,
1447 * which is always are least one, but is not necessarly an integer.
1448 * This means that a physical address can span multiple chunks, so we may
1449 * have to submit multiple io requests for a single sync request.
1450 */
1451/*
1452 * We check if all blocks are in-sync and only write to blocks that
1453 * aren't in sync
1454 */
1455static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1456{
NeilBrown070ec552009-06-16 16:54:21 +10001457 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int i, first;
1459 struct bio *tbio, *fbio;
1460
1461 atomic_set(&r10_bio->remaining, 1);
1462
1463 /* find the first device with a block */
1464 for (i=0; i<conf->copies; i++)
1465 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1466 break;
1467
1468 if (i == conf->copies)
1469 goto done;
1470
1471 first = i;
1472 fbio = r10_bio->devs[i].bio;
1473
1474 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001475 for (i=0 ; i < conf->copies ; i++) {
1476 int j, d;
1477 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001480
1481 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001483 if (i == first)
1484 continue;
1485 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1486 /* We know that the bi_io_vec layout is the same for
1487 * both 'first' and 'i', so we just compare them.
1488 * All vec entries are PAGE_SIZE;
1489 */
1490 for (j = 0; j < vcnt; j++)
1491 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1492 page_address(tbio->bi_io_vec[j].bv_page),
1493 PAGE_SIZE))
1494 break;
1495 if (j == vcnt)
1496 continue;
1497 mddev->resync_mismatches += r10_bio->sectors;
1498 }
NeilBrown18f08812006-01-06 00:20:25 -08001499 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1500 /* Don't fix anything. */
1501 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* Ok, we need to write this bio
1503 * First we need to fixup bv_offset, bv_len and
1504 * bi_vecs, as the read request might have corrupted these
1505 */
1506 tbio->bi_vcnt = vcnt;
1507 tbio->bi_size = r10_bio->sectors << 9;
1508 tbio->bi_idx = 0;
1509 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1511 tbio->bi_flags |= 1 << BIO_UPTODATE;
1512 tbio->bi_next = NULL;
1513 tbio->bi_rw = WRITE;
1514 tbio->bi_private = r10_bio;
1515 tbio->bi_sector = r10_bio->devs[i].addr;
1516
1517 for (j=0; j < vcnt ; j++) {
1518 tbio->bi_io_vec[j].bv_offset = 0;
1519 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1520
1521 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1522 page_address(fbio->bi_io_vec[j].bv_page),
1523 PAGE_SIZE);
1524 }
1525 tbio->bi_end_io = end_sync_write;
1526
1527 d = r10_bio->devs[i].devnum;
1528 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1529 atomic_inc(&r10_bio->remaining);
1530 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1531
1532 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1533 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1534 generic_make_request(tbio);
1535 }
1536
1537done:
1538 if (atomic_dec_and_test(&r10_bio->remaining)) {
1539 md_done_sync(mddev, r10_bio->sectors, 1);
1540 put_buf(r10_bio);
1541 }
1542}
1543
1544/*
1545 * Now for the recovery code.
1546 * Recovery happens across physical sectors.
1547 * We recover all non-is_sync drives by finding the virtual address of
1548 * each, and then choose a working drive that also has that virt address.
1549 * There is a separate r10_bio for each non-in_sync drive.
1550 * Only the first two slots are in use. The first for reading,
1551 * The second for writing.
1552 *
1553 */
1554
1555static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1556{
NeilBrown070ec552009-06-16 16:54:21 +10001557 conf_t *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001558 int d;
1559 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Namhyung Kimc65060a2011-07-18 17:38:49 +10001561 /*
1562 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 * and submit the write request
1564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 wbio = r10_bio->devs[1].bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 d = r10_bio->devs[1].devnum;
1567
1568 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1569 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001570 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1571 generic_make_request(wbio);
NeilBrown2bb77732011-07-27 11:00:36 +10001572 else {
1573 printk(KERN_NOTICE
1574 "md/raid10:%s: recovery aborted due to read error\n",
1575 mdname(mddev));
1576 conf->mirrors[d].recovery_disabled = mddev->recovery_disabled;
1577 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1578 bio_endio(wbio, 0);
1579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582
1583/*
Robert Becker1e509152009-12-14 12:49:58 +11001584 * Used by fix_read_error() to decay the per rdev read_errors.
1585 * We halve the read error count for every hour that has elapsed
1586 * since the last recorded read error.
1587 *
1588 */
1589static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1590{
1591 struct timespec cur_time_mon;
1592 unsigned long hours_since_last;
1593 unsigned int read_errors = atomic_read(&rdev->read_errors);
1594
1595 ktime_get_ts(&cur_time_mon);
1596
1597 if (rdev->last_read_error.tv_sec == 0 &&
1598 rdev->last_read_error.tv_nsec == 0) {
1599 /* first time we've seen a read error */
1600 rdev->last_read_error = cur_time_mon;
1601 return;
1602 }
1603
1604 hours_since_last = (cur_time_mon.tv_sec -
1605 rdev->last_read_error.tv_sec) / 3600;
1606
1607 rdev->last_read_error = cur_time_mon;
1608
1609 /*
1610 * if hours_since_last is > the number of bits in read_errors
1611 * just set read errors to 0. We do this to avoid
1612 * overflowing the shift of read_errors by hours_since_last.
1613 */
1614 if (hours_since_last >= 8 * sizeof(read_errors))
1615 atomic_set(&rdev->read_errors, 0);
1616 else
1617 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1618}
1619
1620/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 * This is a kernel thread which:
1622 *
1623 * 1. Retries failed read operations on working mirrors.
1624 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001625 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 */
1627
NeilBrown6814d532006-10-03 01:15:45 -07001628static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1629{
1630 int sect = 0; /* Offset from r10_bio->sector */
1631 int sectors = r10_bio->sectors;
1632 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001633 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001634 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001635
NeilBrown7c4e06f2011-05-11 14:53:17 +10001636 /* still own a reference to this rdev, so it cannot
1637 * have been cleared recently.
1638 */
1639 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001640
NeilBrown7c4e06f2011-05-11 14:53:17 +10001641 if (test_bit(Faulty, &rdev->flags))
1642 /* drive has already been failed, just ignore any
1643 more fix_read_error() attempts */
1644 return;
1645
1646 check_decay_read_errors(mddev, rdev);
1647 atomic_inc(&rdev->read_errors);
1648 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1649 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001650 bdevname(rdev->bdev, b);
1651
NeilBrown7c4e06f2011-05-11 14:53:17 +10001652 printk(KERN_NOTICE
1653 "md/raid10:%s: %s: Raid device exceeded "
1654 "read_error threshold [cur %d:max %d]\n",
1655 mdname(mddev), b,
1656 atomic_read(&rdev->read_errors), max_read_errors);
1657 printk(KERN_NOTICE
1658 "md/raid10:%s: %s: Failing raid device\n",
1659 mdname(mddev), b);
1660 md_error(mddev, conf->mirrors[d].rdev);
1661 return;
Robert Becker1e509152009-12-14 12:49:58 +11001662 }
Robert Becker1e509152009-12-14 12:49:58 +11001663
NeilBrown6814d532006-10-03 01:15:45 -07001664 while(sectors) {
1665 int s = sectors;
1666 int sl = r10_bio->read_slot;
1667 int success = 0;
1668 int start;
1669
1670 if (s > (PAGE_SIZE>>9))
1671 s = PAGE_SIZE >> 9;
1672
1673 rcu_read_lock();
1674 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10001675 sector_t first_bad;
1676 int bad_sectors;
1677
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001678 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001679 rdev = rcu_dereference(conf->mirrors[d].rdev);
1680 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10001681 test_bit(In_sync, &rdev->flags) &&
1682 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1683 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001684 atomic_inc(&rdev->nr_pending);
1685 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001686 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001687 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001688 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001689 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001690 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001691 rdev_dec_pending(rdev, mddev);
1692 rcu_read_lock();
1693 if (success)
1694 break;
1695 }
1696 sl++;
1697 if (sl == conf->copies)
1698 sl = 0;
1699 } while (!success && sl != r10_bio->read_slot);
1700 rcu_read_unlock();
1701
1702 if (!success) {
1703 /* Cannot read from anywhere -- bye bye array */
1704 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1705 md_error(mddev, conf->mirrors[dn].rdev);
1706 break;
1707 }
1708
1709 start = sl;
1710 /* write it back and re-read */
1711 rcu_read_lock();
1712 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001713 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001714
NeilBrown6814d532006-10-03 01:15:45 -07001715 if (sl==0)
1716 sl = conf->copies;
1717 sl--;
1718 d = r10_bio->devs[sl].devnum;
1719 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001720 if (!rdev ||
1721 !test_bit(In_sync, &rdev->flags))
1722 continue;
1723
1724 atomic_inc(&rdev->nr_pending);
1725 rcu_read_unlock();
1726 if (sync_page_io(rdev,
1727 r10_bio->devs[sl].addr +
1728 sect,
1729 s<<9, conf->tmppage, WRITE, false)
1730 == 0) {
1731 /* Well, this device is dead */
1732 printk(KERN_NOTICE
1733 "md/raid10:%s: read correction "
1734 "write failed"
1735 " (%d sectors at %llu on %s)\n",
1736 mdname(mddev), s,
1737 (unsigned long long)(
1738 sect + rdev->data_offset),
1739 bdevname(rdev->bdev, b));
1740 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1741 "drive\n",
1742 mdname(mddev),
1743 bdevname(rdev->bdev, b));
1744 md_error(mddev, rdev);
NeilBrown6814d532006-10-03 01:15:45 -07001745 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001746 rdev_dec_pending(rdev, mddev);
1747 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001748 }
1749 sl = start;
1750 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10001751 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001752
NeilBrown6814d532006-10-03 01:15:45 -07001753 if (sl==0)
1754 sl = conf->copies;
1755 sl--;
1756 d = r10_bio->devs[sl].devnum;
1757 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001758 if (!rdev ||
1759 !test_bit(In_sync, &rdev->flags))
1760 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11001761
NeilBrown1294b9c2011-07-28 11:39:23 +10001762 atomic_inc(&rdev->nr_pending);
1763 rcu_read_unlock();
1764 if (sync_page_io(rdev,
1765 r10_bio->devs[sl].addr +
1766 sect,
1767 s<<9, conf->tmppage,
1768 READ, false) == 0) {
1769 /* Well, this device is dead */
1770 printk(KERN_NOTICE
1771 "md/raid10:%s: unable to read back "
1772 "corrected sectors"
1773 " (%d sectors at %llu on %s)\n",
1774 mdname(mddev), s,
1775 (unsigned long long)(
1776 sect + rdev->data_offset),
1777 bdevname(rdev->bdev, b));
1778 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1779 "drive\n",
1780 mdname(mddev),
1781 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001782
NeilBrown1294b9c2011-07-28 11:39:23 +10001783 md_error(mddev, rdev);
1784 } else {
1785 printk(KERN_INFO
1786 "md/raid10:%s: read error corrected"
1787 " (%d sectors at %llu on %s)\n",
1788 mdname(mddev), s,
1789 (unsigned long long)(
1790 sect + rdev->data_offset),
1791 bdevname(rdev->bdev, b));
1792 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07001793 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001794
1795 rdev_dec_pending(rdev, mddev);
1796 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001797 }
1798 rcu_read_unlock();
1799
1800 sectors -= s;
1801 sect += s;
1802 }
1803}
1804
NeilBrown560f8e52011-07-28 11:39:23 +10001805static void handle_read_error(mddev_t *mddev, r10bio_t *r10_bio)
1806{
1807 int slot = r10_bio->read_slot;
1808 int mirror = r10_bio->devs[slot].devnum;
1809 struct bio *bio;
1810 conf_t *conf = mddev->private;
1811 mdk_rdev_t *rdev;
1812 char b[BDEVNAME_SIZE];
1813 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10001814 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10001815
1816 /* we got a read error. Maybe the drive is bad. Maybe just
1817 * the block and we can fix it.
1818 * We freeze all other IO, and try reading the block from
1819 * other devices. When we find one, we re-write
1820 * and check it that fixes the read error.
1821 * This is all done synchronously while the array is
1822 * frozen.
1823 */
1824 if (mddev->ro == 0) {
1825 freeze_array(conf);
1826 fix_read_error(conf, mddev, r10_bio);
1827 unfreeze_array(conf);
1828 }
1829 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1830
1831 bio = r10_bio->devs[slot].bio;
NeilBrown7399c312011-07-28 11:39:23 +10001832 bdevname(bio->bi_bdev, b);
NeilBrown560f8e52011-07-28 11:39:23 +10001833 r10_bio->devs[slot].bio =
1834 mddev->ro ? IO_BLOCKED : NULL;
NeilBrown7399c312011-07-28 11:39:23 +10001835read_more:
NeilBrown856e08e2011-07-28 11:39:23 +10001836 mirror = read_balance(conf, r10_bio, &max_sectors);
NeilBrown7399c312011-07-28 11:39:23 +10001837 if (mirror == -1) {
NeilBrown560f8e52011-07-28 11:39:23 +10001838 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
1839 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10001840 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10001841 (unsigned long long)r10_bio->sector);
1842 raid_end_bio_io(r10_bio);
1843 bio_put(bio);
1844 return;
1845 }
1846
1847 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown7399c312011-07-28 11:39:23 +10001848 if (bio)
1849 bio_put(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10001850 slot = r10_bio->read_slot;
1851 rdev = conf->mirrors[mirror].rdev;
1852 printk_ratelimited(
1853 KERN_ERR
1854 "md/raid10:%s: %s: redirecting"
1855 "sector %llu to another mirror\n",
1856 mdname(mddev),
1857 bdevname(rdev->bdev, b),
1858 (unsigned long long)r10_bio->sector);
1859 bio = bio_clone_mddev(r10_bio->master_bio,
1860 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10001861 md_trim_bio(bio,
1862 r10_bio->sector - bio->bi_sector,
1863 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10001864 r10_bio->devs[slot].bio = bio;
1865 bio->bi_sector = r10_bio->devs[slot].addr
1866 + rdev->data_offset;
1867 bio->bi_bdev = rdev->bdev;
1868 bio->bi_rw = READ | do_sync;
1869 bio->bi_private = r10_bio;
1870 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10001871 if (max_sectors < r10_bio->sectors) {
1872 /* Drat - have to split this up more */
1873 struct bio *mbio = r10_bio->master_bio;
1874 int sectors_handled =
1875 r10_bio->sector + max_sectors
1876 - mbio->bi_sector;
1877 r10_bio->sectors = max_sectors;
1878 spin_lock_irq(&conf->device_lock);
1879 if (mbio->bi_phys_segments == 0)
1880 mbio->bi_phys_segments = 2;
1881 else
1882 mbio->bi_phys_segments++;
1883 spin_unlock_irq(&conf->device_lock);
1884 generic_make_request(bio);
1885 bio = NULL;
1886
1887 r10_bio = mempool_alloc(conf->r10bio_pool,
1888 GFP_NOIO);
1889 r10_bio->master_bio = mbio;
1890 r10_bio->sectors = (mbio->bi_size >> 9)
1891 - sectors_handled;
1892 r10_bio->state = 0;
1893 set_bit(R10BIO_ReadError,
1894 &r10_bio->state);
1895 r10_bio->mddev = mddev;
1896 r10_bio->sector = mbio->bi_sector
1897 + sectors_handled;
1898
1899 goto read_more;
1900 } else
1901 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10001902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904static void raid10d(mddev_t *mddev)
1905{
1906 r10bio_t *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001908 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001910 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001914 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08001916
Jens Axboe7eaceac2011-03-10 08:52:07 +01001917 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001920 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001921 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1925 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001926 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 spin_unlock_irqrestore(&conf->device_lock, flags);
1928
1929 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001930 conf = mddev->private;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001931 if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001933 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10001935 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10001936 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10001937 else {
1938 /* just a partial read to be scheduled from a
1939 * separate context
1940 */
1941 int slot = r10_bio->read_slot;
1942 generic_make_request(r10_bio->devs[slot].bio);
1943 }
NeilBrown4443ae12006-01-06 00:20:28 -08001944
NeilBrown1d9d5242009-10-16 15:55:32 +11001945 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10001946 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
1947 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001949 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952
1953static int init_resync(conf_t *conf)
1954{
1955 int buffs;
1956
1957 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001958 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1960 if (!conf->r10buf_pool)
1961 return -ENOMEM;
1962 conf->next_resync = 0;
1963 return 0;
1964}
1965
1966/*
1967 * perform a "sync" on one "block"
1968 *
1969 * We need to make sure that no normal I/O request - particularly write
1970 * requests - conflict with active sync requests.
1971 *
1972 * This is achieved by tracking pending requests and a 'barrier' concept
1973 * that can be installed to exclude normal IO requests.
1974 *
1975 * Resync and recovery are handled very differently.
1976 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1977 *
1978 * For resync, we iterate over virtual addresses, read all copies,
1979 * and update if there are differences. If only one copy is live,
1980 * skip it.
1981 * For recovery, we iterate over physical addresses, read a good
1982 * value for each non-in_sync drive, and over-write.
1983 *
1984 * So, for recovery we may have several outstanding complex requests for a
1985 * given address, one for each out-of-sync device. We model this by allocating
1986 * a number of r10_bio structures, one for each out-of-sync device.
1987 * As we setup these structures, we collect all bio's together into a list
1988 * which we then process collectively to add pages, and then process again
1989 * to pass to generic_make_request.
1990 *
1991 * The r10_bio structures are linked using a borrowed master_bio pointer.
1992 * This link is counted in ->remaining. When the r10_bio that points to NULL
1993 * has its remaining count decremented to 0, the whole complex operation
1994 * is complete.
1995 *
1996 */
1997
NeilBrownab9d47e2011-05-11 14:54:41 +10001998static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
1999 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
NeilBrown070ec552009-06-16 16:54:21 +10002001 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 r10bio_t *r10_bio;
2003 struct bio *biolist = NULL, *bio;
2004 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08002006 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002007 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 sector_t sectors_skipped = 0;
2009 int chunks_skipped = 0;
2010
2011 if (!conf->r10buf_pool)
2012 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002016 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2018 max_sector = mddev->resync_max_sectors;
2019 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002020 /* If we aborted, we need to abort the
2021 * sync on the 'current' bitmap chucks (there can
2022 * be several when recovering multiple devices).
2023 * as we may have started syncing it but not finished.
2024 * We can find the current address in
2025 * mddev->curr_resync, but for recovery,
2026 * we need to convert that to several
2027 * virtual addresses.
2028 */
2029 if (mddev->curr_resync < max_sector) { /* aborted */
2030 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2031 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2032 &sync_blocks, 1);
2033 else for (i=0; i<conf->raid_disks; i++) {
2034 sector_t sect =
2035 raid10_find_virt(conf, mddev->curr_resync, i);
2036 bitmap_end_sync(mddev->bitmap, sect,
2037 &sync_blocks, 1);
2038 }
2039 } else /* completed sync */
2040 conf->fullsync = 0;
2041
2042 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002044 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return sectors_skipped;
2046 }
2047 if (chunks_skipped >= conf->raid_disks) {
2048 /* if there has been nothing to do on any drive,
2049 * then there is nothing to do at all..
2050 */
NeilBrown57afd892005-06-21 17:17:13 -07002051 *skipped = 1;
2052 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
NeilBrownc6207272008-02-06 01:39:52 -08002055 if (max_sector > mddev->resync_max)
2056 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* make sure whole request will fit in a chunk - if chunks
2059 * are meaningful
2060 */
2061 if (conf->near_copies < conf->raid_disks &&
2062 max_sector > (sector_nr | conf->chunk_mask))
2063 max_sector = (sector_nr | conf->chunk_mask) + 1;
2064 /*
2065 * If there is non-resync activity waiting for us then
2066 * put in a delay to throttle resync.
2067 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002068 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /* Again, very different code for resync and recovery.
2072 * Both must result in an r10bio with a list of bios that
2073 * have bi_end_io, bi_sector, bi_bdev set,
2074 * and bi_private set to the r10bio.
2075 * For recovery, we may actually create several r10bios
2076 * with 2 bios in each, that correspond to the bios in the main one.
2077 * In this case, the subordinate r10bios link back through a
2078 * borrowed master_bio pointer, and the counter in the master
2079 * includes a ref from each subordinate.
2080 */
2081 /* First, we decide what to do and set ->bi_end_io
2082 * To end_sync_read if we want to read, and
2083 * end_sync_write if we will want to write.
2084 */
2085
NeilBrown6cce3b22006-01-06 00:20:16 -08002086 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2088 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002089 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 r10_bio = NULL;
2091
NeilBrownab9d47e2011-05-11 14:54:41 +10002092 for (i=0 ; i<conf->raid_disks; i++) {
2093 int still_degraded;
2094 r10bio_t *rb2;
2095 sector_t sect;
2096 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002097 int any_working;
NeilBrownab9d47e2011-05-11 14:54:41 +10002098
2099 if (conf->mirrors[i].rdev == NULL ||
2100 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2101 continue;
2102
2103 still_degraded = 0;
2104 /* want to reconstruct this device */
2105 rb2 = r10_bio;
2106 sect = raid10_find_virt(conf, sector_nr, i);
2107 /* Unless we are doing a full sync, we only need
2108 * to recover the block if it is set in the bitmap
2109 */
2110 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2111 &sync_blocks, 1);
2112 if (sync_blocks < max_sync)
2113 max_sync = sync_blocks;
2114 if (!must_sync &&
2115 !conf->fullsync) {
2116 /* yep, skip the sync_blocks here, but don't assume
2117 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08002118 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002119 chunks_skipped = -1;
2120 continue;
2121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
NeilBrownab9d47e2011-05-11 14:54:41 +10002123 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2124 raise_barrier(conf, rb2 != NULL);
2125 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
NeilBrownab9d47e2011-05-11 14:54:41 +10002127 r10_bio->master_bio = (struct bio*)rb2;
2128 if (rb2)
2129 atomic_inc(&rb2->remaining);
2130 r10_bio->mddev = mddev;
2131 set_bit(R10BIO_IsRecover, &r10_bio->state);
2132 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08002133
NeilBrownab9d47e2011-05-11 14:54:41 +10002134 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002135
NeilBrownab9d47e2011-05-11 14:54:41 +10002136 /* Need to check if the array will still be
2137 * degraded
2138 */
2139 for (j=0; j<conf->raid_disks; j++)
2140 if (conf->mirrors[j].rdev == NULL ||
2141 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2142 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002145
2146 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2147 &sync_blocks, still_degraded);
2148
NeilBrowne875ece2011-07-28 11:39:24 +10002149 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002150 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002151 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002152 int d = r10_bio->devs[j].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002153 mdk_rdev_t *rdev;
2154 sector_t sector, first_bad;
2155 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002156 if (!conf->mirrors[d].rdev ||
2157 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2158 continue;
2159 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002160 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002161 rdev = conf->mirrors[d].rdev;
2162 sector = r10_bio->devs[j].addr;
2163
2164 if (is_badblock(rdev, sector, max_sync,
2165 &first_bad, &bad_sectors)) {
2166 if (first_bad > sector)
2167 max_sync = first_bad - sector;
2168 else {
2169 bad_sectors -= (sector
2170 - first_bad);
2171 if (max_sync > bad_sectors)
2172 max_sync = bad_sectors;
2173 continue;
2174 }
2175 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002176 bio = r10_bio->devs[0].bio;
2177 bio->bi_next = biolist;
2178 biolist = bio;
2179 bio->bi_private = r10_bio;
2180 bio->bi_end_io = end_sync_read;
2181 bio->bi_rw = READ;
2182 bio->bi_sector = r10_bio->devs[j].addr +
2183 conf->mirrors[d].rdev->data_offset;
2184 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2185 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2186 atomic_inc(&r10_bio->remaining);
2187 /* and we write to 'i' */
2188
2189 for (k=0; k<conf->copies; k++)
2190 if (r10_bio->devs[k].devnum == i)
2191 break;
2192 BUG_ON(k == conf->copies);
2193 bio = r10_bio->devs[1].bio;
2194 bio->bi_next = biolist;
2195 biolist = bio;
2196 bio->bi_private = r10_bio;
2197 bio->bi_end_io = end_sync_write;
2198 bio->bi_rw = WRITE;
2199 bio->bi_sector = r10_bio->devs[k].addr +
2200 conf->mirrors[i].rdev->data_offset;
2201 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2202
2203 r10_bio->devs[0].devnum = d;
2204 r10_bio->devs[1].devnum = i;
2205
2206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002208 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002209 /* Cannot recover, so abort the recovery or
2210 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002211 put_buf(r10_bio);
2212 if (rb2)
2213 atomic_dec(&rb2->remaining);
2214 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002215 if (any_working) {
2216 /* problem is that there are bad blocks
2217 * on other device(s)
2218 */
2219 int k;
2220 for (k = 0; k < conf->copies; k++)
2221 if (r10_bio->devs[k].devnum == i)
2222 break;
2223 if (!rdev_set_badblocks(
2224 conf->mirrors[i].rdev,
2225 r10_bio->devs[k].addr,
2226 max_sync, 0))
2227 any_working = 0;
2228 }
2229 if (!any_working) {
2230 if (!test_and_set_bit(MD_RECOVERY_INTR,
2231 &mddev->recovery))
2232 printk(KERN_INFO "md/raid10:%s: insufficient "
2233 "working devices for recovery.\n",
2234 mdname(mddev));
2235 conf->mirrors[i].recovery_disabled
2236 = mddev->recovery_disabled;
2237 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002238 break;
2239 }
2240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (biolist == NULL) {
2242 while (r10_bio) {
2243 r10bio_t *rb2 = r10_bio;
2244 r10_bio = (r10bio_t*) rb2->master_bio;
2245 rb2->master_bio = NULL;
2246 put_buf(rb2);
2247 }
2248 goto giveup;
2249 }
2250 } else {
2251 /* resync. Schedule a read for every block at this virt offset */
2252 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002253
NeilBrown78200d42009-02-25 13:18:47 +11002254 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2255
NeilBrown6cce3b22006-01-06 00:20:16 -08002256 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2257 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002258 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2259 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08002260 /* We can skip this block */
2261 *skipped = 1;
2262 return sync_blocks + sectors_skipped;
2263 }
2264 if (sync_blocks < max_sync)
2265 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 r10_bio->mddev = mddev;
2269 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002270 raise_barrier(conf, 0);
2271 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 r10_bio->master_bio = NULL;
2274 r10_bio->sector = sector_nr;
2275 set_bit(R10BIO_IsSync, &r10_bio->state);
2276 raid10_find_phys(conf, r10_bio);
2277 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2278
2279 for (i=0; i<conf->copies; i++) {
2280 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002281 sector_t first_bad, sector;
2282 int bad_sectors;
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 bio = r10_bio->devs[i].bio;
2285 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002286 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002288 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10002290 sector = r10_bio->devs[i].addr;
2291 if (is_badblock(conf->mirrors[d].rdev,
2292 sector, max_sync,
2293 &first_bad, &bad_sectors)) {
2294 if (first_bad > sector)
2295 max_sync = first_bad - sector;
2296 else {
2297 bad_sectors -= (sector - first_bad);
2298 if (max_sync > bad_sectors)
2299 max_sync = max_sync;
2300 continue;
2301 }
2302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2304 atomic_inc(&r10_bio->remaining);
2305 bio->bi_next = biolist;
2306 biolist = bio;
2307 bio->bi_private = r10_bio;
2308 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002309 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10002310 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 conf->mirrors[d].rdev->data_offset;
2312 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2313 count++;
2314 }
2315
2316 if (count < 2) {
2317 for (i=0; i<conf->copies; i++) {
2318 int d = r10_bio->devs[i].devnum;
2319 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002320 rdev_dec_pending(conf->mirrors[d].rdev,
2321 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 put_buf(r10_bio);
2324 biolist = NULL;
2325 goto giveup;
2326 }
2327 }
2328
2329 for (bio = biolist; bio ; bio=bio->bi_next) {
2330
2331 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2332 if (bio->bi_end_io)
2333 bio->bi_flags |= 1 << BIO_UPTODATE;
2334 bio->bi_vcnt = 0;
2335 bio->bi_idx = 0;
2336 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 bio->bi_size = 0;
2338 }
2339
2340 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002341 if (sector_nr + max_sync < max_sector)
2342 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 do {
2344 struct page *page;
2345 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (sector_nr + (len>>9) > max_sector)
2347 len = (max_sector - sector_nr) << 9;
2348 if (len == 0)
2349 break;
2350 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002351 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002353 if (bio_add_page(bio, page, len, 0))
2354 continue;
2355
2356 /* stop here */
2357 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2358 for (bio2 = biolist;
2359 bio2 && bio2 != bio;
2360 bio2 = bio2->bi_next) {
2361 /* remove last page from this bio */
2362 bio2->bi_vcnt--;
2363 bio2->bi_size -= len;
2364 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002366 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 nr_sectors += len>>9;
2369 sector_nr += len>>9;
2370 } while (biolist->bi_vcnt < RESYNC_PAGES);
2371 bio_full:
2372 r10_bio->sectors = nr_sectors;
2373
2374 while (biolist) {
2375 bio = biolist;
2376 biolist = biolist->bi_next;
2377
2378 bio->bi_next = NULL;
2379 r10_bio = bio->bi_private;
2380 r10_bio->sectors = nr_sectors;
2381
2382 if (bio->bi_end_io == end_sync_read) {
2383 md_sync_acct(bio->bi_bdev, nr_sectors);
2384 generic_make_request(bio);
2385 }
2386 }
2387
NeilBrown57afd892005-06-21 17:17:13 -07002388 if (sectors_skipped)
2389 /* pretend they weren't skipped, it makes
2390 * no important difference in this case
2391 */
2392 md_done_sync(mddev, sectors_skipped, 1);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return sectors_skipped + nr_sectors;
2395 giveup:
2396 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10002397 * drives must be failed or in resync, all drives
2398 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 */
NeilBrown09b40682009-02-25 13:18:47 +11002400 if (sector_nr + max_sync < max_sector)
2401 max_sector = sector_nr + max_sync;
2402
2403 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 chunks_skipped ++;
2405 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Dan Williams80c3a6c2009-03-17 18:10:40 -07002409static sector_t
2410raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2411{
2412 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002413 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002414
2415 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002416 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002417 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002418 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002419
2420 size = sectors >> conf->chunk_shift;
2421 sector_div(size, conf->far_copies);
2422 size = size * raid_disks;
2423 sector_div(size, conf->near_copies);
2424
2425 return size << conf->chunk_shift;
2426}
2427
Trela, Maciejdab8b292010-03-08 16:02:45 +11002428
2429static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002431 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002432 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002434 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Maciej Trelaf73ea872010-06-16 11:46:29 +01002436 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2437 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002438 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2439 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2440 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
NeilBrown2604b702006-01-06 00:20:36 -08002443
Maciej Trelaf73ea872010-06-16 11:46:29 +01002444 nc = mddev->new_layout & 255;
2445 fc = (mddev->new_layout >> 8) & 255;
2446 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002449 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002450 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002451 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 goto out;
2453 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002454
2455 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002456 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002457 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002459
NeilBrown4443ae12006-01-06 00:20:28 -08002460 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002461 GFP_KERNEL);
2462 if (!conf->mirrors)
2463 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002464
2465 conf->tmppage = alloc_page(GFP_KERNEL);
2466 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002467 goto out;
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
NeilBrown64a742b2007-02-28 20:11:18 -08002470 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 conf->near_copies = nc;
2472 conf->far_copies = fc;
2473 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002474 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002475 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2476 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2477
2478 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2479 r10bio_pool_free, conf);
2480 if (!conf->r10bio_pool)
2481 goto out;
2482
Andre Noll58c0fed2009-03-31 14:33:13 +11002483 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002484 sector_div(size, fc);
2485 size = size * conf->raid_disks;
2486 sector_div(size, nc);
2487 /* 'size' is now the number of chunks in the array */
2488 /* calculate "used chunks per device" in 'stride' */
2489 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002490
2491 /* We need to round up when dividing by raid_disks to
2492 * get the stride size.
2493 */
2494 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002495 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002496
2497 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002498
NeilBrownc93983b2006-06-26 00:27:41 -07002499 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002500 stride = 1;
2501 else
NeilBrownc93983b2006-06-26 00:27:41 -07002502 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002503 conf->stride = stride << conf->chunk_shift;
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Neil Browne7e72bf2008-05-14 16:05:54 -07002506 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002507 INIT_LIST_HEAD(&conf->retry_list);
2508
2509 spin_lock_init(&conf->resync_lock);
2510 init_waitqueue_head(&conf->wait_barrier);
2511
2512 conf->thread = md_register_thread(raid10d, mddev, NULL);
2513 if (!conf->thread)
2514 goto out;
2515
Trela, Maciejdab8b292010-03-08 16:02:45 +11002516 conf->mddev = mddev;
2517 return conf;
2518
2519 out:
NeilBrown128595e2010-05-03 14:47:14 +10002520 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002521 mdname(mddev));
2522 if (conf) {
2523 if (conf->r10bio_pool)
2524 mempool_destroy(conf->r10bio_pool);
2525 kfree(conf->mirrors);
2526 safe_put_page(conf->tmppage);
2527 kfree(conf);
2528 }
2529 return ERR_PTR(err);
2530}
2531
2532static int run(mddev_t *mddev)
2533{
2534 conf_t *conf;
2535 int i, disk_idx, chunk_size;
2536 mirror_info_t *disk;
2537 mdk_rdev_t *rdev;
2538 sector_t size;
2539
2540 /*
2541 * copy the already verified devices into our private RAID10
2542 * bookkeeping area. [whatever we allocate in run(),
2543 * should be freed in stop()]
2544 */
2545
2546 if (mddev->private == NULL) {
2547 conf = setup_conf(mddev);
2548 if (IS_ERR(conf))
2549 return PTR_ERR(conf);
2550 mddev->private = conf;
2551 }
2552 conf = mddev->private;
2553 if (!conf)
2554 goto out;
2555
Trela, Maciejdab8b292010-03-08 16:02:45 +11002556 mddev->thread = conf->thread;
2557 conf->thread = NULL;
2558
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002559 chunk_size = mddev->chunk_sectors << 9;
2560 blk_queue_io_min(mddev->queue, chunk_size);
2561 if (conf->raid_disks % conf->near_copies)
2562 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2563 else
2564 blk_queue_io_opt(mddev->queue, chunk_size *
2565 (conf->raid_disks / conf->near_copies));
2566
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002567 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown34b343c2011-07-28 11:31:47 +10002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002570 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 || disk_idx < 0)
2572 continue;
2573 disk = conf->mirrors + disk_idx;
2574
2575 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002576 disk_stack_limits(mddev->gendisk, rdev->bdev,
2577 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002579 * violating it, so limit max_segments to 1 lying
2580 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 */
NeilBrown627a2d32010-03-08 16:44:38 +11002582 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2583 blk_queue_max_segments(mddev->queue, 1);
2584 blk_queue_segment_boundary(mddev->queue,
2585 PAGE_CACHE_SIZE - 1);
2586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
NeilBrown6d508242005-09-09 16:24:03 -07002590 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10002591 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10002592 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002593 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 goto out_free_conf;
2595 }
2596
2597 mddev->degraded = 0;
2598 for (i = 0; i < conf->raid_disks; i++) {
2599
2600 disk = conf->mirrors + i;
2601
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002602 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002603 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 disk->head_position = 0;
2605 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002606 if (disk->rdev)
2607 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609 }
2610
Andre Noll8c6ac862009-06-18 08:48:06 +10002611 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002612 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002613 " -- starting background reconstruction\n",
2614 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002616 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002617 mdname(mddev), conf->raid_disks - mddev->degraded,
2618 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /*
2620 * Ok, everything is just fine now
2621 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002622 mddev->dev_sectors = conf->dev_sectors;
2623 size = raid10_size(mddev, 0, 0);
2624 md_set_array_sectors(mddev, size);
2625 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
NeilBrown0d129222006-10-03 01:15:54 -07002627 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2628 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 /* Calculate max read-ahead size.
2631 * We need to readahead at least twice a whole stripe....
2632 * maybe...
2633 */
2634 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002635 int stripe = conf->raid_disks *
2636 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 stripe /= conf->near_copies;
2638 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2639 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2640 }
2641
NeilBrown84707f32010-03-16 17:23:35 +11002642 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002644
2645 if (md_integrity_register(mddev))
2646 goto out_free_conf;
2647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return 0;
2649
2650out_free_conf:
NeilBrown589a5942010-12-09 17:02:14 +11002651 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (conf->r10bio_pool)
2653 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002654 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002655 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 kfree(conf);
2657 mddev->private = NULL;
2658out:
2659 return -EIO;
2660}
2661
2662static int stop(mddev_t *mddev)
2663{
NeilBrown070ec552009-06-16 16:54:21 +10002664 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
NeilBrown409c57f2009-03-31 14:39:39 +11002666 raise_barrier(conf, 0);
2667 lower_barrier(conf);
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 md_unregister_thread(mddev->thread);
2670 mddev->thread = NULL;
2671 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2672 if (conf->r10bio_pool)
2673 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002674 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 kfree(conf);
2676 mddev->private = NULL;
2677 return 0;
2678}
2679
NeilBrown6cce3b22006-01-06 00:20:16 -08002680static void raid10_quiesce(mddev_t *mddev, int state)
2681{
NeilBrown070ec552009-06-16 16:54:21 +10002682 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002683
2684 switch(state) {
2685 case 1:
2686 raise_barrier(conf, 0);
2687 break;
2688 case 0:
2689 lower_barrier(conf);
2690 break;
2691 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002692}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Trela, Maciejdab8b292010-03-08 16:02:45 +11002694static void *raid10_takeover_raid0(mddev_t *mddev)
2695{
2696 mdk_rdev_t *rdev;
2697 conf_t *conf;
2698
2699 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10002700 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2701 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002702 return ERR_PTR(-EINVAL);
2703 }
2704
Trela, Maciejdab8b292010-03-08 16:02:45 +11002705 /* Set new parameters */
2706 mddev->new_level = 10;
2707 /* new layout: far_copies = 1, near_copies = 2 */
2708 mddev->new_layout = (1<<8) + 2;
2709 mddev->new_chunk_sectors = mddev->chunk_sectors;
2710 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002711 mddev->raid_disks *= 2;
2712 /* make sure it will be not marked as dirty */
2713 mddev->recovery_cp = MaxSector;
2714
2715 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002716 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01002717 list_for_each_entry(rdev, &mddev->disks, same_set)
2718 if (rdev->raid_disk >= 0)
2719 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002720 conf->barrier = 1;
2721 }
2722
Trela, Maciejdab8b292010-03-08 16:02:45 +11002723 return conf;
2724}
2725
2726static void *raid10_takeover(mddev_t *mddev)
2727{
2728 struct raid0_private_data *raid0_priv;
2729
2730 /* raid10 can take over:
2731 * raid0 - providing it has only two drives
2732 */
2733 if (mddev->level == 0) {
2734 /* for raid0 takeover only one zone is supported */
2735 raid0_priv = mddev->private;
2736 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10002737 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2738 " with more than one zone.\n",
2739 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002740 return ERR_PTR(-EINVAL);
2741 }
2742 return raid10_takeover_raid0(mddev);
2743 }
2744 return ERR_PTR(-EINVAL);
2745}
2746
NeilBrown2604b702006-01-06 00:20:36 -08002747static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002750 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 .owner = THIS_MODULE,
2752 .make_request = make_request,
2753 .run = run,
2754 .stop = stop,
2755 .status = status,
2756 .error_handler = error,
2757 .hot_add_disk = raid10_add_disk,
2758 .hot_remove_disk= raid10_remove_disk,
2759 .spare_active = raid10_spare_active,
2760 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002761 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002762 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002763 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764};
2765
2766static int __init raid_init(void)
2767{
NeilBrown2604b702006-01-06 00:20:36 -08002768 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771static void raid_exit(void)
2772{
NeilBrown2604b702006-01-06 00:20:36 -08002773 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
2776module_init(raid_init);
2777module_exit(raid_exit);
2778MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002779MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002781MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002782MODULE_ALIAS("md-level-10");