blob: 685ddf325ee43f4492466c34a50a4a836c766e65 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040024#include <linux/module.h>
NeilBrownbff61972009-03-31 14:33:13 +110025#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100026#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110027#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110028#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110029#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110030#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * RAID10 provides a combination of RAID0 and RAID1 functionality.
34 * The layout of data is defined by
35 * chunk_size
36 * raid_disks
37 * near_copies (stored in low byte of layout)
38 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070039 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
41 * The data to be stored is divided into chunks using chunksize.
42 * Each device is divided into far_copies sections.
43 * In each section, chunks are laid out in a style similar to raid0, but
44 * near_copies copies of each chunk is stored (each on a different drive).
45 * The starting device for each section is offset near_copies from the starting
46 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070047 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * drive.
49 * near_copies and far_copies must be at least one, and their product is at most
50 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070051 *
52 * If far_offset is true, then the far_copies are handled a bit differently.
53 * The copies are still in different stripes, but instead of be very far apart
54 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
57/*
58 * Number of guaranteed r10bios in case of extreme VM load:
59 */
60#define NR_RAID10_BIOS 256
61
NeilBrown34db0cd2011-10-11 16:50:01 +110062/* When there are this many requests queue to be written by
63 * the raid10 thread, we become 'congested' to provide back-pressure
64 * for writeback.
65 */
66static int max_queued_requests = 1024;
67
NeilBrowne879a872011-10-11 16:49:02 +110068static void allow_barrier(struct r10conf *conf);
69static void lower_barrier(struct r10conf *conf);
NeilBrown0a27ec92006-01-06 00:20:13 -080070
Al Virodd0fc662005-10-07 07:46:04 +010071static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
NeilBrowne879a872011-10-11 16:49:02 +110073 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +110074 int size = offsetof(struct r10bio, devs[conf->copies]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* allocate a r10bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010077 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void r10bio_pool_free(void *r10_bio, void *data)
81{
82 kfree(r10_bio);
83}
84
NeilBrown0310fa22008-08-05 15:54:14 +100085/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100088/* amount of memory to reserve for resync requests */
89#define RESYNC_WINDOW (1024*1024)
90/* maximum number of concurrent requests, memory permitting */
91#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/*
94 * When performing a resync, we need to read and compare, so
95 * we need as many pages are there are copies.
96 * When performing a recovery, we need 2 bios, one for read,
97 * one for write (we recover only one drive per r10buf)
98 *
99 */
Al Virodd0fc662005-10-07 07:46:04 +0100100static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
NeilBrowne879a872011-10-11 16:49:02 +1100102 struct r10conf *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct page *page;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100104 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct bio *bio;
106 int i, j;
107 int nalloc;
108
109 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100110 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
114 nalloc = conf->copies; /* resync */
115 else
116 nalloc = 2; /* recovery */
117
118 /*
119 * Allocate bios.
120 */
121 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100122 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!bio)
124 goto out_free_bio;
125 r10_bio->devs[j].bio = bio;
126 }
127 /*
128 * Allocate RESYNC_PAGES data pages and attach them
129 * where needed.
130 */
131 for (j = 0 ; j < nalloc; j++) {
132 bio = r10_bio->devs[j].bio;
133 for (i = 0; i < RESYNC_PAGES; i++) {
Namhyung Kimc65060a2011-07-18 17:38:49 +1000134 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
135 &conf->mddev->recovery)) {
136 /* we can share bv_page's during recovery */
137 struct bio *rbio = r10_bio->devs[0].bio;
138 page = rbio->bi_io_vec[i].bv_page;
139 get_page(page);
140 } else
141 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (unlikely(!page))
143 goto out_free_pages;
144
145 bio->bi_io_vec[i].bv_page = page;
146 }
147 }
148
149 return r10_bio;
150
151out_free_pages:
152 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800153 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 while (j--)
155 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800156 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 j = -1;
158out_free_bio:
159 while ( ++j < nalloc )
160 bio_put(r10_bio->devs[j].bio);
161 r10bio_pool_free(r10_bio, conf);
162 return NULL;
163}
164
165static void r10buf_pool_free(void *__r10_bio, void *data)
166{
167 int i;
NeilBrowne879a872011-10-11 16:49:02 +1100168 struct r10conf *conf = data;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100169 struct r10bio *r10bio = __r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 int j;
171
172 for (j=0; j < conf->copies; j++) {
173 struct bio *bio = r10bio->devs[j].bio;
174 if (bio) {
175 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800176 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 bio->bi_io_vec[i].bv_page = NULL;
178 }
179 bio_put(bio);
180 }
181 }
182 r10bio_pool_free(r10bio, conf);
183}
184
NeilBrowne879a872011-10-11 16:49:02 +1100185static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 int i;
188
189 for (i = 0; i < conf->copies; i++) {
190 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown749c55e2011-07-28 11:39:24 +1000191 if (!BIO_SPECIAL(*bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 bio_put(*bio);
193 *bio = NULL;
194 }
195}
196
NeilBrown9f2c9d12011-10-11 16:48:43 +1100197static void free_r10bio(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
NeilBrowne879a872011-10-11 16:49:02 +1100199 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 put_all_bios(conf, r10_bio);
202 mempool_free(r10_bio, conf->r10bio_pool);
203}
204
NeilBrown9f2c9d12011-10-11 16:48:43 +1100205static void put_buf(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
NeilBrowne879a872011-10-11 16:49:02 +1100207 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 mempool_free(r10_bio, conf->r10buf_pool);
210
NeilBrown0a27ec92006-01-06 00:20:13 -0800211 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
NeilBrown9f2c9d12011-10-11 16:48:43 +1100214static void reschedule_retry(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 unsigned long flags;
NeilBrownfd01b882011-10-11 16:47:53 +1100217 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +1100218 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 spin_lock_irqsave(&conf->device_lock, flags);
221 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800222 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_unlock_irqrestore(&conf->device_lock, flags);
224
Arthur Jones388667b2008-07-25 12:03:38 -0700225 /* wake up frozen array... */
226 wake_up(&conf->wait_barrier);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 md_wakeup_thread(mddev->thread);
229}
230
231/*
232 * raid_end_bio_io() is called when we have finished servicing a mirrored
233 * operation and are ready to return a success/failure code to the buffer
234 * cache layer.
235 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100236static void raid_end_bio_io(struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct bio *bio = r10_bio->master_bio;
NeilBrown856e08e2011-07-28 11:39:23 +1000239 int done;
NeilBrowne879a872011-10-11 16:49:02 +1100240 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrown856e08e2011-07-28 11:39:23 +1000242 if (bio->bi_phys_segments) {
243 unsigned long flags;
244 spin_lock_irqsave(&conf->device_lock, flags);
245 bio->bi_phys_segments--;
246 done = (bio->bi_phys_segments == 0);
247 spin_unlock_irqrestore(&conf->device_lock, flags);
248 } else
249 done = 1;
250 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
251 clear_bit(BIO_UPTODATE, &bio->bi_flags);
252 if (done) {
253 bio_endio(bio, 0);
254 /*
255 * Wake up any possible resync thread that waits for the device
256 * to go idle.
257 */
258 allow_barrier(conf);
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 free_r10bio(r10_bio);
261}
262
263/*
264 * Update disk head position estimator based on IRQ completion info.
265 */
NeilBrown9f2c9d12011-10-11 16:48:43 +1100266static inline void update_head_pos(int slot, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
NeilBrowne879a872011-10-11 16:49:02 +1100268 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
271 r10_bio->devs[slot].addr + (r10_bio->sectors);
272}
273
Namhyung Kim778ca012011-07-18 17:38:47 +1000274/*
275 * Find the disk number which triggered given bio
276 */
NeilBrowne879a872011-10-11 16:49:02 +1100277static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
NeilBrown749c55e2011-07-28 11:39:24 +1000278 struct bio *bio, int *slotp)
Namhyung Kim778ca012011-07-18 17:38:47 +1000279{
280 int slot;
281
282 for (slot = 0; slot < conf->copies; slot++)
283 if (r10_bio->devs[slot].bio == bio)
284 break;
285
286 BUG_ON(slot == conf->copies);
287 update_head_pos(slot, r10_bio);
288
NeilBrown749c55e2011-07-28 11:39:24 +1000289 if (slotp)
290 *slotp = slot;
Namhyung Kim778ca012011-07-18 17:38:47 +1000291 return r10_bio->devs[slot].devnum;
292}
293
NeilBrown6712ecf2007-09-27 12:47:43 +0200294static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100297 struct r10bio *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int slot, dev;
NeilBrowne879a872011-10-11 16:49:02 +1100299 struct r10conf *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 slot = r10_bio->read_slot;
303 dev = r10_bio->devs[slot].devnum;
304 /*
305 * this branch is our 'one mirror IO has finished' event handler:
306 */
NeilBrown4443ae12006-01-06 00:20:28 -0800307 update_head_pos(slot, r10_bio);
308
309 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
311 * Set R10BIO_Uptodate in our master bio, so that
312 * we will return a good error code to the higher
313 * levels even if IO on some other mirrored buffer fails.
314 *
315 * The 'master' represents the composite IO operation to
316 * user-side. So if something waits for IO, then it will
317 * wait for the 'master' bio.
318 */
319 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 raid_end_bio_io(r10_bio);
NeilBrown7c4e06f2011-05-11 14:53:17 +1000321 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800322 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000324 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000327 printk_ratelimited(KERN_ERR
328 "md/raid10:%s: %s: rescheduling sector %llu\n",
329 mdname(conf->mddev),
330 bdevname(conf->mirrors[dev].rdev->bdev, b),
331 (unsigned long long)r10_bio->sector);
NeilBrown856e08e2011-07-28 11:39:23 +1000332 set_bit(R10BIO_ReadError, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 reschedule_retry(r10_bio);
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
NeilBrown9f2c9d12011-10-11 16:48:43 +1100337static void close_write(struct r10bio *r10_bio)
NeilBrownbd870a12011-07-28 11:39:24 +1000338{
339 /* clear the bitmap if all writes complete successfully */
340 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
341 r10_bio->sectors,
342 !test_bit(R10BIO_Degraded, &r10_bio->state),
343 0);
344 md_write_end(r10_bio->mddev);
345}
346
NeilBrown9f2c9d12011-10-11 16:48:43 +1100347static void one_write_done(struct r10bio *r10_bio)
NeilBrown19d5f832011-09-10 17:21:17 +1000348{
349 if (atomic_dec_and_test(&r10_bio->remaining)) {
350 if (test_bit(R10BIO_WriteError, &r10_bio->state))
351 reschedule_retry(r10_bio);
352 else {
353 close_write(r10_bio);
354 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
355 reschedule_retry(r10_bio);
356 else
357 raid_end_bio_io(r10_bio);
358 }
359 }
360}
361
NeilBrown6712ecf2007-09-27 12:47:43 +0200362static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +1100365 struct r10bio *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000366 int dev;
NeilBrown749c55e2011-07-28 11:39:24 +1000367 int dec_rdev = 1;
NeilBrowne879a872011-10-11 16:49:02 +1100368 struct r10conf *conf = r10_bio->mddev->private;
NeilBrown749c55e2011-07-28 11:39:24 +1000369 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
NeilBrown749c55e2011-07-28 11:39:24 +1000371 dev = find_bio_disk(conf, r10_bio, bio, &slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /*
374 * this branch is our 'one mirror IO has finished' event handler:
375 */
NeilBrown6cce3b232006-01-06 00:20:16 -0800376 if (!uptodate) {
NeilBrownbd870a12011-07-28 11:39:24 +1000377 set_bit(WriteErrorSeen, &conf->mirrors[dev].rdev->flags);
378 set_bit(R10BIO_WriteError, &r10_bio->state);
379 dec_rdev = 0;
NeilBrown749c55e2011-07-28 11:39:24 +1000380 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Set R10BIO_Uptodate in our master bio, so that
383 * we will return a good error code for to the higher
384 * levels even if IO on some other mirrored buffer fails.
385 *
386 * The 'master' represents the composite IO operation to
387 * user-side. So if something waits for IO, then it will
388 * wait for the 'master' bio.
389 */
NeilBrown749c55e2011-07-28 11:39:24 +1000390 sector_t first_bad;
391 int bad_sectors;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 set_bit(R10BIO_Uptodate, &r10_bio->state);
394
NeilBrown749c55e2011-07-28 11:39:24 +1000395 /* Maybe we can clear some bad blocks. */
396 if (is_badblock(conf->mirrors[dev].rdev,
397 r10_bio->devs[slot].addr,
398 r10_bio->sectors,
399 &first_bad, &bad_sectors)) {
400 bio_put(bio);
401 r10_bio->devs[slot].bio = IO_MADE_GOOD;
402 dec_rdev = 0;
403 set_bit(R10BIO_MadeGood, &r10_bio->state);
404 }
405 }
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
408 *
409 * Let's see if all mirrored write operations have finished
410 * already.
411 */
NeilBrown19d5f832011-09-10 17:21:17 +1000412 one_write_done(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +1000413 if (dec_rdev)
414 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417
418/*
419 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300420 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * parameters: near_copies and far_copies.
422 * near_copies * far_copies must be <= raid_disks.
423 * Normally one of these will be 1.
424 * If both are 1, we get raid0.
425 * If near_copies == raid_disks, we get raid1.
426 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300427 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * first chunk, followed by near_copies copies of the next chunk and
429 * so on.
430 * If far_copies > 1, then after 1/far_copies of the array has been assigned
431 * as described above, we start again with a device offset of near_copies.
432 * So we effectively have another copy of the whole array further down all
433 * the drives, but with blocks on different drives.
434 * With this layout, and block is never stored twice on the one device.
435 *
436 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700437 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *
439 * raid10_find_virt does the reverse mapping, from a device and a
440 * sector offset to a virtual address
441 */
442
NeilBrowne879a872011-10-11 16:49:02 +1100443static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 int n,f;
446 sector_t sector;
447 sector_t chunk;
448 sector_t stripe;
449 int dev;
450
451 int slot = 0;
452
453 /* now calculate first sector/dev */
454 chunk = r10bio->sector >> conf->chunk_shift;
455 sector = r10bio->sector & conf->chunk_mask;
456
457 chunk *= conf->near_copies;
458 stripe = chunk;
459 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700460 if (conf->far_offset)
461 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 sector += stripe << conf->chunk_shift;
464
465 /* and calculate all the others */
466 for (n=0; n < conf->near_copies; n++) {
467 int d = dev;
468 sector_t s = sector;
469 r10bio->devs[slot].addr = sector;
470 r10bio->devs[slot].devnum = d;
471 slot++;
472
473 for (f = 1; f < conf->far_copies; f++) {
474 d += conf->near_copies;
475 if (d >= conf->raid_disks)
476 d -= conf->raid_disks;
477 s += conf->stride;
478 r10bio->devs[slot].devnum = d;
479 r10bio->devs[slot].addr = s;
480 slot++;
481 }
482 dev++;
483 if (dev >= conf->raid_disks) {
484 dev = 0;
485 sector += (conf->chunk_mask + 1);
486 }
487 }
488 BUG_ON(slot != conf->copies);
489}
490
NeilBrowne879a872011-10-11 16:49:02 +1100491static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 sector_t offset, chunk, vchunk;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700496 if (conf->far_offset) {
497 int fc;
498 chunk = sector >> conf->chunk_shift;
499 fc = sector_div(chunk, conf->far_copies);
500 dev -= fc * conf->near_copies;
501 if (dev < 0)
502 dev += conf->raid_disks;
503 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800504 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700505 sector -= conf->stride;
506 if (dev < conf->near_copies)
507 dev += conf->raid_disks - conf->near_copies;
508 else
509 dev -= conf->near_copies;
510 }
511 chunk = sector >> conf->chunk_shift;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 vchunk = chunk * conf->raid_disks + dev;
514 sector_div(vchunk, conf->near_copies);
515 return (vchunk << conf->chunk_shift) + offset;
516}
517
518/**
519 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
520 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200521 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * @biovec: the request that could be merged to it.
523 *
524 * Return amount of bytes we can accept at this offset
525 * If near_copies == raid_disk, there are no striping issues,
526 * but in that case, the function isn't called at all.
527 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200528static int raid10_mergeable_bvec(struct request_queue *q,
529 struct bvec_merge_data *bvm,
530 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
NeilBrownfd01b882011-10-11 16:47:53 +1100532 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200533 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000535 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200536 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
539 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200540 if (max <= biovec->bv_len && bio_sectors == 0)
541 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 else
543 return max;
544}
545
546/*
547 * This routine returns the disk from which the requested read should
548 * be done. There is a per-array 'next expected sequential IO' sector
549 * number - if this matches on the next IO then we use the last disk.
550 * There is also a per-disk 'last know head position' sector that is
551 * maintained from IRQ contexts, both the normal and the resync IO
552 * completion handlers update this position correctly. If there is no
553 * perfect sequential match then we pick the disk whose head is closest.
554 *
555 * If there are 2 mirrors in the same 2 devices, performance degrades
556 * because position is mirror, not device based.
557 *
558 * The rdev for the device selected will have nr_pending incremented.
559 */
560
561/*
562 * FIXME: possibly should rethink readbalancing and do it differently
563 * depending on near_copies / far_copies geometry.
564 */
NeilBrowne879a872011-10-11 16:49:02 +1100565static int read_balance(struct r10conf *conf, struct r10bio *r10_bio, int *max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000567 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000568 int disk, slot;
NeilBrown856e08e2011-07-28 11:39:23 +1000569 int sectors = r10_bio->sectors;
570 int best_good_sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000571 sector_t new_distance, best_dist;
NeilBrown3cb03002011-10-11 16:45:26 +1100572 struct md_rdev *rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000573 int do_balance;
574 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 raid10_find_phys(conf, r10_bio);
577 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000578retry:
NeilBrown856e08e2011-07-28 11:39:23 +1000579 sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000580 best_slot = -1;
581 best_dist = MaxSector;
NeilBrown856e08e2011-07-28 11:39:23 +1000582 best_good_sectors = 0;
NeilBrown56d99122011-05-11 14:27:03 +1000583 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b232006-01-06 00:20:16 -0800586 * device if no resync is going on (recovery is ok), or below
587 * the resync window. We take the first readable disk when
588 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
590 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000591 && (this_sector + sectors >= conf->next_resync))
592 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
NeilBrown56d99122011-05-11 14:27:03 +1000594 for (slot = 0; slot < conf->copies ; slot++) {
NeilBrown856e08e2011-07-28 11:39:23 +1000595 sector_t first_bad;
596 int bad_sectors;
597 sector_t dev_sector;
598
NeilBrown56d99122011-05-11 14:27:03 +1000599 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000601 disk = r10_bio->devs[slot].devnum;
602 rdev = rcu_dereference(conf->mirrors[disk].rdev);
603 if (rdev == NULL)
604 continue;
605 if (!test_bit(In_sync, &rdev->flags))
606 continue;
607
NeilBrown856e08e2011-07-28 11:39:23 +1000608 dev_sector = r10_bio->devs[slot].addr;
609 if (is_badblock(rdev, dev_sector, sectors,
610 &first_bad, &bad_sectors)) {
611 if (best_dist < MaxSector)
612 /* Already have a better slot */
613 continue;
614 if (first_bad <= dev_sector) {
615 /* Cannot read here. If this is the
616 * 'primary' device, then we must not read
617 * beyond 'bad_sectors' from another device.
618 */
619 bad_sectors -= (dev_sector - first_bad);
620 if (!do_balance && sectors > bad_sectors)
621 sectors = bad_sectors;
622 if (best_good_sectors > sectors)
623 best_good_sectors = sectors;
624 } else {
625 sector_t good_sectors =
626 first_bad - dev_sector;
627 if (good_sectors > best_good_sectors) {
628 best_good_sectors = good_sectors;
629 best_slot = slot;
630 }
631 if (!do_balance)
632 /* Must read from here */
633 break;
634 }
635 continue;
636 } else
637 best_good_sectors = sectors;
638
NeilBrown56d99122011-05-11 14:27:03 +1000639 if (!do_balance)
640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
NeilBrown22dfdf52005-11-28 13:44:09 -0800642 /* This optimisation is debatable, and completely destroys
643 * sequential read speed for 'far copies' arrays. So only
644 * keep it for 'near' arrays, and review those later.
645 */
NeilBrown56d99122011-05-11 14:27:03 +1000646 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800648
649 /* for far > 1 always use the lowest address */
650 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000651 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800652 else
NeilBrown56d99122011-05-11 14:27:03 +1000653 new_distance = abs(r10_bio->devs[slot].addr -
654 conf->mirrors[disk].head_position);
655 if (new_distance < best_dist) {
656 best_dist = new_distance;
657 best_slot = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 }
NeilBrown56d99122011-05-11 14:27:03 +1000660 if (slot == conf->copies)
661 slot = best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
NeilBrown56d99122011-05-11 14:27:03 +1000663 if (slot >= 0) {
664 disk = r10_bio->devs[slot].devnum;
665 rdev = rcu_dereference(conf->mirrors[disk].rdev);
666 if (!rdev)
667 goto retry;
668 atomic_inc(&rdev->nr_pending);
669 if (test_bit(Faulty, &rdev->flags)) {
670 /* Cannot risk returning a device that failed
671 * before we inc'ed nr_pending
672 */
673 rdev_dec_pending(rdev, conf->mddev);
674 goto retry;
675 }
676 r10_bio->read_slot = slot;
677 } else
NeilBrown29fc7e32006-02-03 03:03:41 -0800678 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 rcu_read_unlock();
NeilBrown856e08e2011-07-28 11:39:23 +1000680 *max_sectors = best_good_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return disk;
683}
684
NeilBrown0d129222006-10-03 01:15:54 -0700685static int raid10_congested(void *data, int bits)
686{
NeilBrownfd01b882011-10-11 16:47:53 +1100687 struct mddev *mddev = data;
NeilBrowne879a872011-10-11 16:49:02 +1100688 struct r10conf *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700689 int i, ret = 0;
690
NeilBrown34db0cd2011-10-11 16:50:01 +1100691 if ((bits & (1 << BDI_async_congested)) &&
692 conf->pending_count >= max_queued_requests)
693 return 1;
694
NeilBrown3fa841d2009-09-23 18:10:29 +1000695 if (mddev_congested(mddev, bits))
696 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700697 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100698 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100699 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrown0d129222006-10-03 01:15:54 -0700700 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200701 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700702
703 ret |= bdi_congested(&q->backing_dev_info, bits);
704 }
705 }
706 rcu_read_unlock();
707 return ret;
708}
709
NeilBrowne879a872011-10-11 16:49:02 +1100710static void flush_pending_writes(struct r10conf *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800711{
712 /* Any writes that have been queued but are awaiting
713 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800714 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800715 spin_lock_irq(&conf->device_lock);
716
717 if (conf->pending_bio_list.head) {
718 struct bio *bio;
719 bio = bio_list_get(&conf->pending_bio_list);
NeilBrown34db0cd2011-10-11 16:50:01 +1100720 conf->pending_count = 0;
NeilBrowna35e63e2008-03-04 14:29:29 -0800721 spin_unlock_irq(&conf->device_lock);
722 /* flush any pending bitmap writes to disk
723 * before proceeding w/ I/O */
724 bitmap_unplug(conf->mddev->bitmap);
NeilBrown34db0cd2011-10-11 16:50:01 +1100725 wake_up(&conf->wait_barrier);
NeilBrowna35e63e2008-03-04 14:29:29 -0800726
727 while (bio) { /* submit pending writes */
728 struct bio *next = bio->bi_next;
729 bio->bi_next = NULL;
730 generic_make_request(bio);
731 bio = next;
732 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800733 } else
734 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800735}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100736
NeilBrown0a27ec92006-01-06 00:20:13 -0800737/* Barriers....
738 * Sometimes we need to suspend IO while we do something else,
739 * either some resync/recovery, or reconfigure the array.
740 * To do this we raise a 'barrier'.
741 * The 'barrier' is a counter that can be raised multiple times
742 * to count how many activities are happening which preclude
743 * normal IO.
744 * We can only raise the barrier if there is no pending IO.
745 * i.e. if nr_pending == 0.
746 * We choose only to raise the barrier if no-one is waiting for the
747 * barrier to go down. This means that as soon as an IO request
748 * is ready, no other operations which require a barrier will start
749 * until the IO request has had a chance.
750 *
751 * So: regular IO calls 'wait_barrier'. When that returns there
752 * is no backgroup IO happening, It must arrange to call
753 * allow_barrier when it has finished its IO.
754 * backgroup IO calls must call raise_barrier. Once that returns
755 * there is no normal IO happeing. It must arrange to call
756 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
NeilBrowne879a872011-10-11 16:49:02 +1100759static void raise_barrier(struct r10conf *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
NeilBrown6cce3b232006-01-06 00:20:16 -0800761 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
NeilBrown6cce3b232006-01-06 00:20:16 -0800764 /* Wait until no block IO is waiting (unless 'force') */
765 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000766 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800767
768 /* block any new IO from starting */
769 conf->barrier++;
770
NeilBrownc3b328a2011-04-18 18:25:43 +1000771 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800772 wait_event_lock_irq(conf->wait_barrier,
773 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000774 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 spin_unlock_irq(&conf->resync_lock);
777}
778
NeilBrowne879a872011-10-11 16:49:02 +1100779static void lower_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800780{
781 unsigned long flags;
782 spin_lock_irqsave(&conf->resync_lock, flags);
783 conf->barrier--;
784 spin_unlock_irqrestore(&conf->resync_lock, flags);
785 wake_up(&conf->wait_barrier);
786}
787
NeilBrowne879a872011-10-11 16:49:02 +1100788static void wait_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800789{
790 spin_lock_irq(&conf->resync_lock);
791 if (conf->barrier) {
792 conf->nr_waiting++;
793 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
794 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000795 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800796 conf->nr_waiting--;
797 }
798 conf->nr_pending++;
799 spin_unlock_irq(&conf->resync_lock);
800}
801
NeilBrowne879a872011-10-11 16:49:02 +1100802static void allow_barrier(struct r10conf *conf)
NeilBrown0a27ec92006-01-06 00:20:13 -0800803{
804 unsigned long flags;
805 spin_lock_irqsave(&conf->resync_lock, flags);
806 conf->nr_pending--;
807 spin_unlock_irqrestore(&conf->resync_lock, flags);
808 wake_up(&conf->wait_barrier);
809}
810
NeilBrowne879a872011-10-11 16:49:02 +1100811static void freeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800812{
813 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800814 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800815 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800816 * wait until nr_pending match nr_queued+1
817 * This is called in the context of one normal IO request
818 * that has failed. Thus any sync request that might be pending
819 * will be blocked by nr_pending, and we need to wait for
820 * pending IO requests to complete or be queued for re-try.
821 * Thus the number queued (nr_queued) plus this request (1)
822 * must match the number of pending IOs (nr_pending) before
823 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800824 */
825 spin_lock_irq(&conf->resync_lock);
826 conf->barrier++;
827 conf->nr_waiting++;
828 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800829 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800830 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000831 flush_pending_writes(conf));
832
NeilBrown4443ae12006-01-06 00:20:28 -0800833 spin_unlock_irq(&conf->resync_lock);
834}
835
NeilBrowne879a872011-10-11 16:49:02 +1100836static void unfreeze_array(struct r10conf *conf)
NeilBrown4443ae12006-01-06 00:20:28 -0800837{
838 /* reverse the effect of the freeze */
839 spin_lock_irq(&conf->resync_lock);
840 conf->barrier--;
841 conf->nr_waiting--;
842 wake_up(&conf->wait_barrier);
843 spin_unlock_irq(&conf->resync_lock);
844}
845
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -0700846static void make_request(struct mddev *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
NeilBrowne879a872011-10-11 16:49:02 +1100848 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +1100849 struct mirror_info *mirror;
NeilBrown9f2c9d12011-10-11 16:48:43 +1100850 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct bio *read_bio;
852 int i;
853 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100854 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000855 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200856 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b232006-01-06 00:20:16 -0800857 unsigned long flags;
NeilBrown3cb03002011-10-11 16:45:26 +1100858 struct md_rdev *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000859 int plugged;
NeilBrownd4432c22011-07-28 11:39:24 +1000860 int sectors_handled;
861 int max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Tejun Heoe9c74692010-09-03 11:56:18 +0200863 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
864 md_flush_request(mddev, bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200865 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -0700866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* If this request crosses a chunk boundary, we need to
869 * split it. This will only happen for 1 PAGE (or less) requests.
870 */
871 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
872 > chunk_sects &&
873 conf->near_copies < conf->raid_disks)) {
874 struct bio_pair *bp;
875 /* Sanity check -- queue functions should prevent this happening */
876 if (bio->bi_vcnt != 1 ||
877 bio->bi_idx != 0)
878 goto bad_map;
879 /* This is a one page bio that upper layers
880 * refuse to split for us, so we need to split it.
881 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200882 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000884
885 /* Each of these 'make_request' calls will call 'wait_barrier'.
886 * If the first succeeds but the second blocks due to the resync
887 * thread raising the barrier, we will deadlock because the
888 * IO to the underlying device will be queued in generic_make_request
889 * and will never complete, so will never reduce nr_pending.
890 * So increment nr_waiting here so no new raise_barriers will
891 * succeed, and so the second wait_barrier cannot block.
892 */
893 spin_lock_irq(&conf->resync_lock);
894 conf->nr_waiting++;
895 spin_unlock_irq(&conf->resync_lock);
896
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200897 make_request(mddev, &bp->bio1);
898 make_request(mddev, &bp->bio2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
NeilBrown51e9ac72010-08-07 21:17:00 +1000900 spin_lock_irq(&conf->resync_lock);
901 conf->nr_waiting--;
902 wake_up(&conf->wait_barrier);
903 spin_unlock_irq(&conf->resync_lock);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 bio_pair_release(bp);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200906 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000908 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
909 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
911
NeilBrown6712ecf2007-09-27 12:47:43 +0200912 bio_io_error(bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200913 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
NeilBrown3d310eb2005-06-21 17:17:26 -0700916 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /*
919 * Register the new request and wait if the reconstruction
920 * thread has put up a bar for new requests.
921 * Continue immediately if no resync is active currently.
922 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800923 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
926
927 r10_bio->master_bio = bio;
928 r10_bio->sectors = bio->bi_size >> 9;
929
930 r10_bio->mddev = mddev;
931 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b232006-01-06 00:20:16 -0800932 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
NeilBrown856e08e2011-07-28 11:39:23 +1000934 /* We might need to issue multiple reads to different
935 * devices if there are bad blocks around, so we keep
936 * track of the number of reads in bio->bi_phys_segments.
937 * If this is 0, there is only one r10_bio and no locking
938 * will be needed when the request completes. If it is
939 * non-zero, then it is the number of not-completed requests.
940 */
941 bio->bi_phys_segments = 0;
942 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
943
Jens Axboea3623572005-11-01 09:26:16 +0100944 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /*
946 * read balancing logic:
947 */
NeilBrown856e08e2011-07-28 11:39:23 +1000948 int disk;
949 int slot;
950
951read_again:
952 disk = read_balance(conf, r10_bio, &max_sectors);
953 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (disk < 0) {
955 raid_end_bio_io(r10_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200956 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 mirror = conf->mirrors + disk;
959
NeilBrowna167f662010-10-26 18:31:13 +1100960 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrown856e08e2011-07-28 11:39:23 +1000961 md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
962 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 r10_bio->devs[slot].bio = read_bio;
965
966 read_bio->bi_sector = r10_bio->devs[slot].addr +
967 mirror->rdev->data_offset;
968 read_bio->bi_bdev = mirror->rdev->bdev;
969 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200970 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 read_bio->bi_private = r10_bio;
972
NeilBrown856e08e2011-07-28 11:39:23 +1000973 if (max_sectors < r10_bio->sectors) {
974 /* Could not read all from this device, so we will
975 * need another r10_bio.
976 */
NeilBrown856e08e2011-07-28 11:39:23 +1000977 sectors_handled = (r10_bio->sectors + max_sectors
978 - bio->bi_sector);
979 r10_bio->sectors = max_sectors;
980 spin_lock_irq(&conf->device_lock);
981 if (bio->bi_phys_segments == 0)
982 bio->bi_phys_segments = 2;
983 else
984 bio->bi_phys_segments++;
985 spin_unlock(&conf->device_lock);
986 /* Cannot call generic_make_request directly
987 * as that will be queued in __generic_make_request
988 * and subsequent mempool_alloc might block
989 * waiting for it. so hand bio over to raid10d.
990 */
991 reschedule_retry(r10_bio);
992
993 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
994
995 r10_bio->master_bio = bio;
996 r10_bio->sectors = ((bio->bi_size >> 9)
997 - sectors_handled);
998 r10_bio->state = 0;
999 r10_bio->mddev = mddev;
1000 r10_bio->sector = bio->bi_sector + sectors_handled;
1001 goto read_again;
1002 } else
1003 generic_make_request(read_bio);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001004 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
1007 /*
1008 * WRITE:
1009 */
NeilBrown34db0cd2011-10-11 16:50:01 +11001010 if (conf->pending_count >= max_queued_requests) {
1011 md_wakeup_thread(mddev->thread);
1012 wait_event(conf->wait_barrier,
1013 conf->pending_count < max_queued_requests);
1014 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001015 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * inc refcount on their rdev. Record them by setting
1017 * bios[x] to bio
NeilBrownd4432c22011-07-28 11:39:24 +10001018 * If there are known/acknowledged bad blocks on any device
1019 * on which we have seen a write error, we want to avoid
1020 * writing to those blocks. This potentially requires several
1021 * writes to write around the bad blocks. Each set of writes
1022 * gets its own r10_bio with a set of bios attached. The number
1023 * of r10_bios is recored in bio->bi_phys_segments just as with
1024 * the read case.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
NeilBrownc3b328a2011-04-18 18:25:43 +10001026 plugged = mddev_check_plugged(mddev);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 raid10_find_phys(conf, r10_bio);
NeilBrownd4432c22011-07-28 11:39:24 +10001029retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -07001030 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 rcu_read_lock();
NeilBrownd4432c22011-07-28 11:39:24 +10001032 max_sectors = r10_bio->sectors;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 for (i = 0; i < conf->copies; i++) {
1035 int d = r10_bio->devs[i].devnum;
NeilBrown3cb03002011-10-11 16:45:26 +11001036 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -07001037 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1038 atomic_inc(&rdev->nr_pending);
1039 blocked_rdev = rdev;
1040 break;
1041 }
NeilBrownd4432c22011-07-28 11:39:24 +10001042 r10_bio->devs[i].bio = NULL;
1043 if (!rdev || test_bit(Faulty, &rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001044 set_bit(R10BIO_Degraded, &r10_bio->state);
NeilBrownd4432c22011-07-28 11:39:24 +10001045 continue;
NeilBrown6cce3b232006-01-06 00:20:16 -08001046 }
NeilBrownd4432c22011-07-28 11:39:24 +10001047 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1048 sector_t first_bad;
1049 sector_t dev_sector = r10_bio->devs[i].addr;
1050 int bad_sectors;
1051 int is_bad;
1052
1053 is_bad = is_badblock(rdev, dev_sector,
1054 max_sectors,
1055 &first_bad, &bad_sectors);
1056 if (is_bad < 0) {
1057 /* Mustn't write here until the bad block
1058 * is acknowledged
1059 */
1060 atomic_inc(&rdev->nr_pending);
1061 set_bit(BlockedBadBlocks, &rdev->flags);
1062 blocked_rdev = rdev;
1063 break;
1064 }
1065 if (is_bad && first_bad <= dev_sector) {
1066 /* Cannot write here at all */
1067 bad_sectors -= (dev_sector - first_bad);
1068 if (bad_sectors < max_sectors)
1069 /* Mustn't write more than bad_sectors
1070 * to other devices yet
1071 */
1072 max_sectors = bad_sectors;
1073 /* We don't set R10BIO_Degraded as that
1074 * only applies if the disk is missing,
1075 * so it might be re-added, and we want to
1076 * know to recover this chunk.
1077 * In this case the device is here, and the
1078 * fact that this chunk is not in-sync is
1079 * recorded in the bad block log.
1080 */
1081 continue;
1082 }
1083 if (is_bad) {
1084 int good_sectors = first_bad - dev_sector;
1085 if (good_sectors < max_sectors)
1086 max_sectors = good_sectors;
1087 }
1088 }
1089 r10_bio->devs[i].bio = bio;
1090 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092 rcu_read_unlock();
1093
Dan Williams6bfe0b42008-04-30 00:52:32 -07001094 if (unlikely(blocked_rdev)) {
1095 /* Have to wait for this device to get unblocked, then retry */
1096 int j;
1097 int d;
1098
1099 for (j = 0; j < i; j++)
1100 if (r10_bio->devs[j].bio) {
1101 d = r10_bio->devs[j].devnum;
1102 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1103 }
1104 allow_barrier(conf);
1105 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1106 wait_barrier(conf);
1107 goto retry_write;
1108 }
1109
NeilBrownd4432c22011-07-28 11:39:24 +10001110 if (max_sectors < r10_bio->sectors) {
1111 /* We are splitting this into multiple parts, so
1112 * we need to prepare for allocating another r10_bio.
1113 */
1114 r10_bio->sectors = max_sectors;
1115 spin_lock_irq(&conf->device_lock);
1116 if (bio->bi_phys_segments == 0)
1117 bio->bi_phys_segments = 2;
1118 else
1119 bio->bi_phys_segments++;
1120 spin_unlock_irq(&conf->device_lock);
1121 }
1122 sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
1123
NeilBrown4e780642010-10-19 12:54:01 +11001124 atomic_set(&r10_bio->remaining, 1);
NeilBrownd4432c22011-07-28 11:39:24 +10001125 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 for (i = 0; i < conf->copies; i++) {
1128 struct bio *mbio;
1129 int d = r10_bio->devs[i].devnum;
1130 if (!r10_bio->devs[i].bio)
1131 continue;
1132
NeilBrowna167f662010-10-26 18:31:13 +11001133 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
NeilBrownd4432c22011-07-28 11:39:24 +10001134 md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
1135 max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 r10_bio->devs[i].bio = mbio;
1137
NeilBrownd4432c22011-07-28 11:39:24 +10001138 mbio->bi_sector = (r10_bio->devs[i].addr+
1139 conf->mirrors[d].rdev->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1141 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +02001142 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 mbio->bi_private = r10_bio;
1144
1145 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +11001146 spin_lock_irqsave(&conf->device_lock, flags);
1147 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown34db0cd2011-10-11 16:50:01 +11001148 conf->pending_count++;
NeilBrown4e780642010-10-19 12:54:01 +11001149 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
NeilBrown079fa162011-09-10 17:21:23 +10001152 /* Don't remove the bias on 'remaining' (one_write_done) until
1153 * after checking if we need to go around again.
1154 */
NeilBrowna35e63e2008-03-04 14:29:29 -08001155
NeilBrownd4432c22011-07-28 11:39:24 +10001156 if (sectors_handled < (bio->bi_size >> 9)) {
NeilBrown079fa162011-09-10 17:21:23 +10001157 one_write_done(r10_bio);
NeilBrown5e570282011-07-28 11:39:25 +10001158 /* We need another r10_bio. It has already been counted
NeilBrownd4432c22011-07-28 11:39:24 +10001159 * in bio->bi_phys_segments.
1160 */
1161 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1162
1163 r10_bio->master_bio = bio;
1164 r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1165
1166 r10_bio->mddev = mddev;
1167 r10_bio->sector = bio->bi_sector + sectors_handled;
1168 r10_bio->state = 0;
1169 goto retry_write;
1170 }
NeilBrown079fa162011-09-10 17:21:23 +10001171 one_write_done(r10_bio);
1172
1173 /* In case raid10d snuck in to freeze_array */
1174 wake_up(&conf->wait_barrier);
NeilBrownd4432c22011-07-28 11:39:24 +10001175
NeilBrownc3b328a2011-04-18 18:25:43 +10001176 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -08001177 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
NeilBrownfd01b882011-10-11 16:47:53 +11001180static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
NeilBrowne879a872011-10-11 16:49:02 +11001182 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int i;
1184
1185 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001186 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (conf->near_copies > 1)
1188 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001189 if (conf->far_copies > 1) {
1190 if (conf->far_offset)
1191 seq_printf(seq, " %d offset-copies", conf->far_copies);
1192 else
1193 seq_printf(seq, " %d far-copies", conf->far_copies);
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001196 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 for (i = 0; i < conf->raid_disks; i++)
1198 seq_printf(seq, "%s",
1199 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001200 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 seq_printf(seq, "]");
1202}
1203
NeilBrown700c7212011-07-27 11:00:36 +10001204/* check if there are enough drives for
1205 * every block to appear on atleast one.
1206 * Don't consider the device numbered 'ignore'
1207 * as we might be about to remove it.
1208 */
NeilBrowne879a872011-10-11 16:49:02 +11001209static int enough(struct r10conf *conf, int ignore)
NeilBrown700c7212011-07-27 11:00:36 +10001210{
1211 int first = 0;
1212
1213 do {
1214 int n = conf->copies;
1215 int cnt = 0;
1216 while (n--) {
1217 if (conf->mirrors[first].rdev &&
1218 first != ignore)
1219 cnt++;
1220 first = (first+1) % conf->raid_disks;
1221 }
1222 if (cnt == 0)
1223 return 0;
1224 } while (first != 0);
1225 return 1;
1226}
1227
NeilBrownfd01b882011-10-11 16:47:53 +11001228static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 char b[BDEVNAME_SIZE];
NeilBrowne879a872011-10-11 16:49:02 +11001231 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /*
1234 * If it is not operational, then we have already marked it as dead
1235 * else if it is the last working disks, ignore the error, let the
1236 * next level up know.
1237 * else mark the drive as failed
1238 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001239 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001240 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
1244 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001245 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1246 unsigned long flags;
1247 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001249 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * if recovery is running, make sure it aborts.
1252 */
NeilBrowndfc70642008-05-23 13:04:39 -07001253 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
NeilBrownde393cd2011-07-28 11:31:48 +10001255 set_bit(Blocked, &rdev->flags);
NeilBrownb2d444d2005-11-08 21:39:31 -08001256 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001257 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001258 printk(KERN_ALERT
1259 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1260 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001261 mdname(mddev), bdevname(rdev->bdev, b),
1262 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
NeilBrowne879a872011-10-11 16:49:02 +11001265static void print_conf(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 int i;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001268 struct mirror_info *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
NeilBrown128595e2010-05-03 14:47:14 +10001270 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001272 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return;
1274 }
NeilBrown128595e2010-05-03 14:47:14 +10001275 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 conf->raid_disks);
1277
1278 for (i = 0; i < conf->raid_disks; i++) {
1279 char b[BDEVNAME_SIZE];
1280 tmp = conf->mirrors + i;
1281 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001282 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001283 i, !test_bit(In_sync, &tmp->rdev->flags),
1284 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 bdevname(tmp->rdev->bdev,b));
1286 }
1287}
1288
NeilBrowne879a872011-10-11 16:49:02 +11001289static void close_sync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
NeilBrown0a27ec92006-01-06 00:20:13 -08001291 wait_barrier(conf);
1292 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 mempool_destroy(conf->r10buf_pool);
1295 conf->r10buf_pool = NULL;
1296}
1297
NeilBrownfd01b882011-10-11 16:47:53 +11001298static int raid10_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 int i;
NeilBrowne879a872011-10-11 16:49:02 +11001301 struct r10conf *conf = mddev->private;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001302 struct mirror_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001303 int count = 0;
1304 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /*
1307 * Find all non-in_sync disks within the RAID10 configuration
1308 * and mark them in_sync
1309 */
1310 for (i = 0; i < conf->raid_disks; i++) {
1311 tmp = conf->mirrors + i;
1312 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001313 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001314 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001315 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001316 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 }
NeilBrown6b965622010-08-18 11:56:59 +10001319 spin_lock_irqsave(&conf->device_lock, flags);
1320 mddev->degraded -= count;
1321 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001324 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327
NeilBrownfd01b882011-10-11 16:47:53 +11001328static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
NeilBrowne879a872011-10-11 16:49:02 +11001330 struct r10conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001331 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001333 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001334 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 if (mddev->recovery_cp < MaxSector)
1337 /* only hot-add to in-sync arrays, as recovery is
1338 * very different from resync
1339 */
Neil Brown199050e2008-06-28 08:31:33 +10001340 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001341 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
NeilBrowna53a6c82008-11-06 17:28:20 +11001344 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001345 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001347 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b232006-01-06 00:20:16 -08001348 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1349 mirror = rdev->saved_raid_disk;
1350 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001351 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001352 for ( ; mirror <= last ; mirror++) {
NeilBrown0f6d02d2011-10-11 16:48:46 +11001353 struct mirror_info *p = &conf->mirrors[mirror];
NeilBrown2bb77732011-07-27 11:00:36 +10001354 if (p->recovery_disabled == mddev->recovery_disabled)
1355 continue;
NeilBrown7fcc7c82011-10-31 12:59:44 +11001356 if (p->rdev)
NeilBrown2bb77732011-07-27 11:00:36 +10001357 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
NeilBrown2bb77732011-07-27 11:00:36 +10001359 disk_stack_limits(mddev->gendisk, rdev->bdev,
1360 rdev->data_offset << 9);
1361 /* as we don't honour merge_bvec_fn, we must
1362 * never risk violating it, so limit
1363 * ->max_segments to one lying with a single
1364 * page, as a one page request is never in
1365 * violation.
1366 */
1367 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1368 blk_queue_max_segments(mddev->queue, 1);
1369 blk_queue_segment_boundary(mddev->queue,
1370 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
NeilBrown2bb77732011-07-27 11:00:36 +10001373 p->head_position = 0;
NeilBrownd890fa22011-10-26 11:54:39 +11001374 p->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown2bb77732011-07-27 11:00:36 +10001375 rdev->raid_disk = mirror;
1376 err = 0;
1377 if (rdev->saved_raid_disk != mirror)
1378 conf->fullsync = 1;
1379 rcu_assign_pointer(p->rdev, rdev);
1380 break;
1381 }
1382
Andre Nollac5e7112009-08-03 10:59:47 +10001383 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
NeilBrownfd01b882011-10-11 16:47:53 +11001388static int raid10_remove_disk(struct mddev *mddev, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
NeilBrowne879a872011-10-11 16:49:02 +11001390 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int err = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11001392 struct md_rdev *rdev;
NeilBrown0f6d02d2011-10-11 16:48:46 +11001393 struct mirror_info *p = conf->mirrors+ number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 print_conf(conf);
1396 rdev = p->rdev;
1397 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001398 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 atomic_read(&rdev->nr_pending)) {
1400 err = -EBUSY;
1401 goto abort;
1402 }
NeilBrowndfc70642008-05-23 13:04:39 -07001403 /* Only remove faulty devices in recovery
1404 * is not possible.
1405 */
1406 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown2bb77732011-07-27 11:00:36 +10001407 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown700c7212011-07-27 11:00:36 +10001408 enough(conf, -1)) {
NeilBrowndfc70642008-05-23 13:04:39 -07001409 err = -EBUSY;
1410 goto abort;
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001413 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (atomic_read(&rdev->nr_pending)) {
1415 /* lost the race, try later */
1416 err = -EBUSY;
1417 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001418 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001420 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422abort:
1423
1424 print_conf(conf);
1425 return err;
1426}
1427
1428
NeilBrown6712ecf2007-09-27 12:47:43 +02001429static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
NeilBrown9f2c9d12011-10-11 16:48:43 +11001431 struct r10bio *r10_bio = bio->bi_private;
NeilBrowne879a872011-10-11 16:49:02 +11001432 struct r10conf *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001433 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
NeilBrown749c55e2011-07-28 11:39:24 +10001435 d = find_bio_disk(conf, r10_bio, bio, NULL);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001436
1437 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1438 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrowne684e412011-07-28 11:39:25 +10001439 else
1440 /* The write handler will notice the lack of
1441 * R10BIO_Uptodate and record any errors etc
1442 */
NeilBrown4dbcdc72006-01-06 00:20:52 -08001443 atomic_add(r10_bio->sectors,
1444 &conf->mirrors[d].rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /* for reconstruct, we always reschedule after a read.
1447 * for resync, only after all reads
1448 */
NeilBrown73d5c382009-02-25 13:18:47 +11001449 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1451 atomic_dec_and_test(&r10_bio->remaining)) {
1452 /* we have read all the blocks,
1453 * do the comparison in process context in raid10d
1454 */
1455 reschedule_retry(r10_bio);
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
NeilBrown9f2c9d12011-10-11 16:48:43 +11001459static void end_sync_request(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001460{
NeilBrownfd01b882011-10-11 16:47:53 +11001461 struct mddev *mddev = r10_bio->mddev;
NeilBrown5e570282011-07-28 11:39:25 +10001462
1463 while (atomic_dec_and_test(&r10_bio->remaining)) {
1464 if (r10_bio->master_bio == NULL) {
1465 /* the primary of several recovery bios */
1466 sector_t s = r10_bio->sectors;
1467 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1468 test_bit(R10BIO_WriteError, &r10_bio->state))
1469 reschedule_retry(r10_bio);
1470 else
1471 put_buf(r10_bio);
1472 md_done_sync(mddev, s, 1);
1473 break;
1474 } else {
NeilBrown9f2c9d12011-10-11 16:48:43 +11001475 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
NeilBrown5e570282011-07-28 11:39:25 +10001476 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1477 test_bit(R10BIO_WriteError, &r10_bio->state))
1478 reschedule_retry(r10_bio);
1479 else
1480 put_buf(r10_bio);
1481 r10_bio = r10_bio2;
1482 }
1483 }
1484}
1485
NeilBrown6712ecf2007-09-27 12:47:43 +02001486static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
NeilBrown9f2c9d12011-10-11 16:48:43 +11001489 struct r10bio *r10_bio = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11001490 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001491 struct r10conf *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001492 int d;
NeilBrown749c55e2011-07-28 11:39:24 +10001493 sector_t first_bad;
1494 int bad_sectors;
1495 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
NeilBrown749c55e2011-07-28 11:39:24 +10001497 d = find_bio_disk(conf, r10_bio, bio, &slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
NeilBrown1a0b7cd2011-07-28 11:39:25 +10001499 if (!uptodate) {
1500 set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags);
1501 set_bit(R10BIO_WriteError, &r10_bio->state);
1502 } else if (is_badblock(conf->mirrors[d].rdev,
NeilBrown749c55e2011-07-28 11:39:24 +10001503 r10_bio->devs[slot].addr,
1504 r10_bio->sectors,
1505 &first_bad, &bad_sectors))
1506 set_bit(R10BIO_MadeGood, &r10_bio->state);
NeilBrowndfc70642008-05-23 13:04:39 -07001507
NeilBrown73d5c382009-02-25 13:18:47 +11001508 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
NeilBrown5e570282011-07-28 11:39:25 +10001509
1510 end_sync_request(r10_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513/*
1514 * Note: sync and recover and handled very differently for raid10
1515 * This code is for resync.
1516 * For resync, we read through virtual addresses and read all blocks.
1517 * If there is any error, we schedule a write. The lowest numbered
1518 * drive is authoritative.
1519 * However requests come for physical address, so we need to map.
1520 * For every physical address there are raid_disks/copies virtual addresses,
1521 * which is always are least one, but is not necessarly an integer.
1522 * This means that a physical address can span multiple chunks, so we may
1523 * have to submit multiple io requests for a single sync request.
1524 */
1525/*
1526 * We check if all blocks are in-sync and only write to blocks that
1527 * aren't in sync
1528 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001529static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
NeilBrowne879a872011-10-11 16:49:02 +11001531 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int i, first;
1533 struct bio *tbio, *fbio;
1534
1535 atomic_set(&r10_bio->remaining, 1);
1536
1537 /* find the first device with a block */
1538 for (i=0; i<conf->copies; i++)
1539 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1540 break;
1541
1542 if (i == conf->copies)
1543 goto done;
1544
1545 first = i;
1546 fbio = r10_bio->devs[i].bio;
1547
1548 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001549 for (i=0 ; i < conf->copies ; i++) {
1550 int j, d;
1551 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001554
1555 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001557 if (i == first)
1558 continue;
1559 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1560 /* We know that the bi_io_vec layout is the same for
1561 * both 'first' and 'i', so we just compare them.
1562 * All vec entries are PAGE_SIZE;
1563 */
1564 for (j = 0; j < vcnt; j++)
1565 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1566 page_address(tbio->bi_io_vec[j].bv_page),
1567 PAGE_SIZE))
1568 break;
1569 if (j == vcnt)
1570 continue;
1571 mddev->resync_mismatches += r10_bio->sectors;
NeilBrownf84ee362011-07-28 11:39:25 +10001572 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1573 /* Don't fix anything. */
1574 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001575 }
NeilBrownf84ee362011-07-28 11:39:25 +10001576 /* Ok, we need to write this bio, either to correct an
1577 * inconsistency or to correct an unreadable block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * First we need to fixup bv_offset, bv_len and
1579 * bi_vecs, as the read request might have corrupted these
1580 */
1581 tbio->bi_vcnt = vcnt;
1582 tbio->bi_size = r10_bio->sectors << 9;
1583 tbio->bi_idx = 0;
1584 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1586 tbio->bi_flags |= 1 << BIO_UPTODATE;
1587 tbio->bi_next = NULL;
1588 tbio->bi_rw = WRITE;
1589 tbio->bi_private = r10_bio;
1590 tbio->bi_sector = r10_bio->devs[i].addr;
1591
1592 for (j=0; j < vcnt ; j++) {
1593 tbio->bi_io_vec[j].bv_offset = 0;
1594 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1595
1596 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1597 page_address(fbio->bi_io_vec[j].bv_page),
1598 PAGE_SIZE);
1599 }
1600 tbio->bi_end_io = end_sync_write;
1601
1602 d = r10_bio->devs[i].devnum;
1603 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1604 atomic_inc(&r10_bio->remaining);
1605 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1606
1607 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1608 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1609 generic_make_request(tbio);
1610 }
1611
1612done:
1613 if (atomic_dec_and_test(&r10_bio->remaining)) {
1614 md_done_sync(mddev, r10_bio->sectors, 1);
1615 put_buf(r10_bio);
1616 }
1617}
1618
1619/*
1620 * Now for the recovery code.
1621 * Recovery happens across physical sectors.
1622 * We recover all non-is_sync drives by finding the virtual address of
1623 * each, and then choose a working drive that also has that virt address.
1624 * There is a separate r10_bio for each non-in_sync drive.
1625 * Only the first two slots are in use. The first for reading,
1626 * The second for writing.
1627 *
1628 */
NeilBrown9f2c9d12011-10-11 16:48:43 +11001629static void fix_recovery_read_error(struct r10bio *r10_bio)
NeilBrown5e570282011-07-28 11:39:25 +10001630{
1631 /* We got a read error during recovery.
1632 * We repeat the read in smaller page-sized sections.
1633 * If a read succeeds, write it to the new device or record
1634 * a bad block if we cannot.
1635 * If a read fails, record a bad block on both old and
1636 * new devices.
1637 */
NeilBrownfd01b882011-10-11 16:47:53 +11001638 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11001639 struct r10conf *conf = mddev->private;
NeilBrown5e570282011-07-28 11:39:25 +10001640 struct bio *bio = r10_bio->devs[0].bio;
1641 sector_t sect = 0;
1642 int sectors = r10_bio->sectors;
1643 int idx = 0;
1644 int dr = r10_bio->devs[0].devnum;
1645 int dw = r10_bio->devs[1].devnum;
1646
1647 while (sectors) {
1648 int s = sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001649 struct md_rdev *rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001650 sector_t addr;
1651 int ok;
1652
1653 if (s > (PAGE_SIZE>>9))
1654 s = PAGE_SIZE >> 9;
1655
1656 rdev = conf->mirrors[dr].rdev;
1657 addr = r10_bio->devs[0].addr + sect,
1658 ok = sync_page_io(rdev,
1659 addr,
1660 s << 9,
1661 bio->bi_io_vec[idx].bv_page,
1662 READ, false);
1663 if (ok) {
1664 rdev = conf->mirrors[dw].rdev;
1665 addr = r10_bio->devs[1].addr + sect;
1666 ok = sync_page_io(rdev,
1667 addr,
1668 s << 9,
1669 bio->bi_io_vec[idx].bv_page,
1670 WRITE, false);
1671 if (!ok)
1672 set_bit(WriteErrorSeen, &rdev->flags);
1673 }
1674 if (!ok) {
1675 /* We don't worry if we cannot set a bad block -
1676 * it really is bad so there is no loss in not
1677 * recording it yet
1678 */
1679 rdev_set_badblocks(rdev, addr, s, 0);
1680
1681 if (rdev != conf->mirrors[dw].rdev) {
1682 /* need bad block on destination too */
NeilBrown3cb03002011-10-11 16:45:26 +11001683 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
NeilBrown5e570282011-07-28 11:39:25 +10001684 addr = r10_bio->devs[1].addr + sect;
1685 ok = rdev_set_badblocks(rdev2, addr, s, 0);
1686 if (!ok) {
1687 /* just abort the recovery */
1688 printk(KERN_NOTICE
1689 "md/raid10:%s: recovery aborted"
1690 " due to read error\n",
1691 mdname(mddev));
1692
1693 conf->mirrors[dw].recovery_disabled
1694 = mddev->recovery_disabled;
1695 set_bit(MD_RECOVERY_INTR,
1696 &mddev->recovery);
1697 break;
1698 }
1699 }
1700 }
1701
1702 sectors -= s;
1703 sect += s;
1704 idx++;
1705 }
1706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
NeilBrown9f2c9d12011-10-11 16:48:43 +11001708static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
NeilBrowne879a872011-10-11 16:49:02 +11001710 struct r10conf *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001711 int d;
1712 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
NeilBrown5e570282011-07-28 11:39:25 +10001714 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
1715 fix_recovery_read_error(r10_bio);
1716 end_sync_request(r10_bio);
1717 return;
1718 }
1719
Namhyung Kimc65060a2011-07-18 17:38:49 +10001720 /*
1721 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * and submit the write request
1723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 wbio = r10_bio->devs[1].bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 d = r10_bio->devs[1].devnum;
1726
1727 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1728 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown5e570282011-07-28 11:39:25 +10001729 generic_make_request(wbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732
1733/*
Robert Becker1e509152009-12-14 12:49:58 +11001734 * Used by fix_read_error() to decay the per rdev read_errors.
1735 * We halve the read error count for every hour that has elapsed
1736 * since the last recorded read error.
1737 *
1738 */
NeilBrownfd01b882011-10-11 16:47:53 +11001739static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
Robert Becker1e509152009-12-14 12:49:58 +11001740{
1741 struct timespec cur_time_mon;
1742 unsigned long hours_since_last;
1743 unsigned int read_errors = atomic_read(&rdev->read_errors);
1744
1745 ktime_get_ts(&cur_time_mon);
1746
1747 if (rdev->last_read_error.tv_sec == 0 &&
1748 rdev->last_read_error.tv_nsec == 0) {
1749 /* first time we've seen a read error */
1750 rdev->last_read_error = cur_time_mon;
1751 return;
1752 }
1753
1754 hours_since_last = (cur_time_mon.tv_sec -
1755 rdev->last_read_error.tv_sec) / 3600;
1756
1757 rdev->last_read_error = cur_time_mon;
1758
1759 /*
1760 * if hours_since_last is > the number of bits in read_errors
1761 * just set read errors to 0. We do this to avoid
1762 * overflowing the shift of read_errors by hours_since_last.
1763 */
1764 if (hours_since_last >= 8 * sizeof(read_errors))
1765 atomic_set(&rdev->read_errors, 0);
1766 else
1767 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1768}
1769
NeilBrown3cb03002011-10-11 16:45:26 +11001770static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
NeilBrown58c54fc2011-07-28 11:39:25 +10001771 int sectors, struct page *page, int rw)
1772{
1773 sector_t first_bad;
1774 int bad_sectors;
1775
1776 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
1777 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
1778 return -1;
1779 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1780 /* success */
1781 return 1;
1782 if (rw == WRITE)
1783 set_bit(WriteErrorSeen, &rdev->flags);
1784 /* need to record an error - either for the block or the device */
1785 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1786 md_error(rdev->mddev, rdev);
1787 return 0;
1788}
1789
Robert Becker1e509152009-12-14 12:49:58 +11001790/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * This is a kernel thread which:
1792 *
1793 * 1. Retries failed read operations on working mirrors.
1794 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001795 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
1797
NeilBrowne879a872011-10-11 16:49:02 +11001798static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown6814d532006-10-03 01:15:45 -07001799{
1800 int sect = 0; /* Offset from r10_bio->sector */
1801 int sectors = r10_bio->sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11001802 struct md_rdev*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001803 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001804 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001805
NeilBrown7c4e06f2011-05-11 14:53:17 +10001806 /* still own a reference to this rdev, so it cannot
1807 * have been cleared recently.
1808 */
1809 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001810
NeilBrown7c4e06f2011-05-11 14:53:17 +10001811 if (test_bit(Faulty, &rdev->flags))
1812 /* drive has already been failed, just ignore any
1813 more fix_read_error() attempts */
1814 return;
1815
1816 check_decay_read_errors(mddev, rdev);
1817 atomic_inc(&rdev->read_errors);
1818 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1819 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001820 bdevname(rdev->bdev, b);
1821
NeilBrown7c4e06f2011-05-11 14:53:17 +10001822 printk(KERN_NOTICE
1823 "md/raid10:%s: %s: Raid device exceeded "
1824 "read_error threshold [cur %d:max %d]\n",
1825 mdname(mddev), b,
1826 atomic_read(&rdev->read_errors), max_read_errors);
1827 printk(KERN_NOTICE
1828 "md/raid10:%s: %s: Failing raid device\n",
1829 mdname(mddev), b);
1830 md_error(mddev, conf->mirrors[d].rdev);
1831 return;
Robert Becker1e509152009-12-14 12:49:58 +11001832 }
Robert Becker1e509152009-12-14 12:49:58 +11001833
NeilBrown6814d532006-10-03 01:15:45 -07001834 while(sectors) {
1835 int s = sectors;
1836 int sl = r10_bio->read_slot;
1837 int success = 0;
1838 int start;
1839
1840 if (s > (PAGE_SIZE>>9))
1841 s = PAGE_SIZE >> 9;
1842
1843 rcu_read_lock();
1844 do {
NeilBrown8dbed5c2011-07-28 11:39:24 +10001845 sector_t first_bad;
1846 int bad_sectors;
1847
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001848 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001849 rdev = rcu_dereference(conf->mirrors[d].rdev);
1850 if (rdev &&
NeilBrown8dbed5c2011-07-28 11:39:24 +10001851 test_bit(In_sync, &rdev->flags) &&
1852 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
1853 &first_bad, &bad_sectors) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001854 atomic_inc(&rdev->nr_pending);
1855 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001856 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001857 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001858 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001859 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001860 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001861 rdev_dec_pending(rdev, mddev);
1862 rcu_read_lock();
1863 if (success)
1864 break;
1865 }
1866 sl++;
1867 if (sl == conf->copies)
1868 sl = 0;
1869 } while (!success && sl != r10_bio->read_slot);
1870 rcu_read_unlock();
1871
1872 if (!success) {
NeilBrown58c54fc2011-07-28 11:39:25 +10001873 /* Cannot read from anywhere, just mark the block
1874 * as bad on the first device to discourage future
1875 * reads.
1876 */
NeilBrown6814d532006-10-03 01:15:45 -07001877 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
NeilBrown58c54fc2011-07-28 11:39:25 +10001878 rdev = conf->mirrors[dn].rdev;
1879
1880 if (!rdev_set_badblocks(
1881 rdev,
1882 r10_bio->devs[r10_bio->read_slot].addr
1883 + sect,
1884 s, 0))
1885 md_error(mddev, rdev);
NeilBrown6814d532006-10-03 01:15:45 -07001886 break;
1887 }
1888
1889 start = sl;
1890 /* write it back and re-read */
1891 rcu_read_lock();
1892 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001893 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001894
NeilBrown6814d532006-10-03 01:15:45 -07001895 if (sl==0)
1896 sl = conf->copies;
1897 sl--;
1898 d = r10_bio->devs[sl].devnum;
1899 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001900 if (!rdev ||
1901 !test_bit(In_sync, &rdev->flags))
1902 continue;
1903
1904 atomic_inc(&rdev->nr_pending);
1905 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10001906 if (r10_sync_page_io(rdev,
1907 r10_bio->devs[sl].addr +
1908 sect,
1909 s<<9, conf->tmppage, WRITE)
NeilBrown1294b9c2011-07-28 11:39:23 +10001910 == 0) {
1911 /* Well, this device is dead */
1912 printk(KERN_NOTICE
1913 "md/raid10:%s: read correction "
1914 "write failed"
1915 " (%d sectors at %llu on %s)\n",
1916 mdname(mddev), s,
1917 (unsigned long long)(
1918 sect + rdev->data_offset),
1919 bdevname(rdev->bdev, b));
1920 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1921 "drive\n",
1922 mdname(mddev),
1923 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001924 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001925 rdev_dec_pending(rdev, mddev);
1926 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001927 }
1928 sl = start;
1929 while (sl != r10_bio->read_slot) {
NeilBrown1294b9c2011-07-28 11:39:23 +10001930 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001931
NeilBrown6814d532006-10-03 01:15:45 -07001932 if (sl==0)
1933 sl = conf->copies;
1934 sl--;
1935 d = r10_bio->devs[sl].devnum;
1936 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown1294b9c2011-07-28 11:39:23 +10001937 if (!rdev ||
1938 !test_bit(In_sync, &rdev->flags))
1939 continue;
Robert Becker67b8dc42009-12-14 12:49:57 +11001940
NeilBrown1294b9c2011-07-28 11:39:23 +10001941 atomic_inc(&rdev->nr_pending);
1942 rcu_read_unlock();
NeilBrown58c54fc2011-07-28 11:39:25 +10001943 switch (r10_sync_page_io(rdev,
1944 r10_bio->devs[sl].addr +
1945 sect,
1946 s<<9, conf->tmppage,
1947 READ)) {
1948 case 0:
NeilBrown1294b9c2011-07-28 11:39:23 +10001949 /* Well, this device is dead */
1950 printk(KERN_NOTICE
1951 "md/raid10:%s: unable to read back "
1952 "corrected sectors"
1953 " (%d sectors at %llu on %s)\n",
1954 mdname(mddev), s,
1955 (unsigned long long)(
1956 sect + rdev->data_offset),
1957 bdevname(rdev->bdev, b));
1958 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1959 "drive\n",
1960 mdname(mddev),
1961 bdevname(rdev->bdev, b));
NeilBrown58c54fc2011-07-28 11:39:25 +10001962 break;
1963 case 1:
NeilBrown1294b9c2011-07-28 11:39:23 +10001964 printk(KERN_INFO
1965 "md/raid10:%s: read error corrected"
1966 " (%d sectors at %llu on %s)\n",
1967 mdname(mddev), s,
1968 (unsigned long long)(
1969 sect + rdev->data_offset),
1970 bdevname(rdev->bdev, b));
1971 atomic_add(s, &rdev->corrected_errors);
NeilBrown6814d532006-10-03 01:15:45 -07001972 }
NeilBrown1294b9c2011-07-28 11:39:23 +10001973
1974 rdev_dec_pending(rdev, mddev);
1975 rcu_read_lock();
NeilBrown6814d532006-10-03 01:15:45 -07001976 }
1977 rcu_read_unlock();
1978
1979 sectors -= s;
1980 sect += s;
1981 }
1982}
1983
NeilBrownbd870a12011-07-28 11:39:24 +10001984static void bi_complete(struct bio *bio, int error)
1985{
1986 complete((struct completion *)bio->bi_private);
1987}
1988
1989static int submit_bio_wait(int rw, struct bio *bio)
1990{
1991 struct completion event;
1992 rw |= REQ_SYNC;
1993
1994 init_completion(&event);
1995 bio->bi_private = &event;
1996 bio->bi_end_io = bi_complete;
1997 submit_bio(rw, bio);
1998 wait_for_completion(&event);
1999
2000 return test_bit(BIO_UPTODATE, &bio->bi_flags);
2001}
2002
NeilBrown9f2c9d12011-10-11 16:48:43 +11002003static int narrow_write_error(struct r10bio *r10_bio, int i)
NeilBrownbd870a12011-07-28 11:39:24 +10002004{
2005 struct bio *bio = r10_bio->master_bio;
NeilBrownfd01b882011-10-11 16:47:53 +11002006 struct mddev *mddev = r10_bio->mddev;
NeilBrowne879a872011-10-11 16:49:02 +11002007 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002008 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
NeilBrownbd870a12011-07-28 11:39:24 +10002009 /* bio has the data to be written to slot 'i' where
2010 * we just recently had a write error.
2011 * We repeatedly clone the bio and trim down to one block,
2012 * then try the write. Where the write fails we record
2013 * a bad block.
2014 * It is conceivable that the bio doesn't exactly align with
2015 * blocks. We must handle this.
2016 *
2017 * We currently own a reference to the rdev.
2018 */
2019
2020 int block_sectors;
2021 sector_t sector;
2022 int sectors;
2023 int sect_to_write = r10_bio->sectors;
2024 int ok = 1;
2025
2026 if (rdev->badblocks.shift < 0)
2027 return 0;
2028
2029 block_sectors = 1 << rdev->badblocks.shift;
2030 sector = r10_bio->sector;
2031 sectors = ((r10_bio->sector + block_sectors)
2032 & ~(sector_t)(block_sectors - 1))
2033 - sector;
2034
2035 while (sect_to_write) {
2036 struct bio *wbio;
2037 if (sectors > sect_to_write)
2038 sectors = sect_to_write;
2039 /* Write at 'sector' for 'sectors' */
2040 wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
2041 md_trim_bio(wbio, sector - bio->bi_sector, sectors);
2042 wbio->bi_sector = (r10_bio->devs[i].addr+
2043 rdev->data_offset+
2044 (sector - r10_bio->sector));
2045 wbio->bi_bdev = rdev->bdev;
2046 if (submit_bio_wait(WRITE, wbio) == 0)
2047 /* Failure! */
2048 ok = rdev_set_badblocks(rdev, sector,
2049 sectors, 0)
2050 && ok;
2051
2052 bio_put(wbio);
2053 sect_to_write -= sectors;
2054 sector += sectors;
2055 sectors = block_sectors;
2056 }
2057 return ok;
2058}
2059
NeilBrown9f2c9d12011-10-11 16:48:43 +11002060static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
NeilBrown560f8e52011-07-28 11:39:23 +10002061{
2062 int slot = r10_bio->read_slot;
2063 int mirror = r10_bio->devs[slot].devnum;
2064 struct bio *bio;
NeilBrowne879a872011-10-11 16:49:02 +11002065 struct r10conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11002066 struct md_rdev *rdev;
NeilBrown560f8e52011-07-28 11:39:23 +10002067 char b[BDEVNAME_SIZE];
2068 unsigned long do_sync;
NeilBrown856e08e2011-07-28 11:39:23 +10002069 int max_sectors;
NeilBrown560f8e52011-07-28 11:39:23 +10002070
2071 /* we got a read error. Maybe the drive is bad. Maybe just
2072 * the block and we can fix it.
2073 * We freeze all other IO, and try reading the block from
2074 * other devices. When we find one, we re-write
2075 * and check it that fixes the read error.
2076 * This is all done synchronously while the array is
2077 * frozen.
2078 */
2079 if (mddev->ro == 0) {
2080 freeze_array(conf);
2081 fix_read_error(conf, mddev, r10_bio);
2082 unfreeze_array(conf);
2083 }
2084 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
2085
2086 bio = r10_bio->devs[slot].bio;
NeilBrown7399c312011-07-28 11:39:23 +10002087 bdevname(bio->bi_bdev, b);
NeilBrown560f8e52011-07-28 11:39:23 +10002088 r10_bio->devs[slot].bio =
2089 mddev->ro ? IO_BLOCKED : NULL;
NeilBrown7399c312011-07-28 11:39:23 +10002090read_more:
NeilBrown856e08e2011-07-28 11:39:23 +10002091 mirror = read_balance(conf, r10_bio, &max_sectors);
NeilBrown7399c312011-07-28 11:39:23 +10002092 if (mirror == -1) {
NeilBrown560f8e52011-07-28 11:39:23 +10002093 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
2094 " read error for block %llu\n",
NeilBrown7399c312011-07-28 11:39:23 +10002095 mdname(mddev), b,
NeilBrown560f8e52011-07-28 11:39:23 +10002096 (unsigned long long)r10_bio->sector);
2097 raid_end_bio_io(r10_bio);
2098 bio_put(bio);
2099 return;
2100 }
2101
2102 do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
NeilBrown7399c312011-07-28 11:39:23 +10002103 if (bio)
2104 bio_put(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002105 slot = r10_bio->read_slot;
2106 rdev = conf->mirrors[mirror].rdev;
2107 printk_ratelimited(
2108 KERN_ERR
2109 "md/raid10:%s: %s: redirecting"
2110 "sector %llu to another mirror\n",
2111 mdname(mddev),
2112 bdevname(rdev->bdev, b),
2113 (unsigned long long)r10_bio->sector);
2114 bio = bio_clone_mddev(r10_bio->master_bio,
2115 GFP_NOIO, mddev);
NeilBrown7399c312011-07-28 11:39:23 +10002116 md_trim_bio(bio,
2117 r10_bio->sector - bio->bi_sector,
2118 max_sectors);
NeilBrown560f8e52011-07-28 11:39:23 +10002119 r10_bio->devs[slot].bio = bio;
2120 bio->bi_sector = r10_bio->devs[slot].addr
2121 + rdev->data_offset;
2122 bio->bi_bdev = rdev->bdev;
2123 bio->bi_rw = READ | do_sync;
2124 bio->bi_private = r10_bio;
2125 bio->bi_end_io = raid10_end_read_request;
NeilBrown7399c312011-07-28 11:39:23 +10002126 if (max_sectors < r10_bio->sectors) {
2127 /* Drat - have to split this up more */
2128 struct bio *mbio = r10_bio->master_bio;
2129 int sectors_handled =
2130 r10_bio->sector + max_sectors
2131 - mbio->bi_sector;
2132 r10_bio->sectors = max_sectors;
2133 spin_lock_irq(&conf->device_lock);
2134 if (mbio->bi_phys_segments == 0)
2135 mbio->bi_phys_segments = 2;
2136 else
2137 mbio->bi_phys_segments++;
2138 spin_unlock_irq(&conf->device_lock);
2139 generic_make_request(bio);
2140 bio = NULL;
2141
2142 r10_bio = mempool_alloc(conf->r10bio_pool,
2143 GFP_NOIO);
2144 r10_bio->master_bio = mbio;
2145 r10_bio->sectors = (mbio->bi_size >> 9)
2146 - sectors_handled;
2147 r10_bio->state = 0;
2148 set_bit(R10BIO_ReadError,
2149 &r10_bio->state);
2150 r10_bio->mddev = mddev;
2151 r10_bio->sector = mbio->bi_sector
2152 + sectors_handled;
2153
2154 goto read_more;
2155 } else
2156 generic_make_request(bio);
NeilBrown560f8e52011-07-28 11:39:23 +10002157}
2158
NeilBrowne879a872011-10-11 16:49:02 +11002159static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
NeilBrown749c55e2011-07-28 11:39:24 +10002160{
2161 /* Some sort of write request has finished and it
2162 * succeeded in writing where we thought there was a
2163 * bad block. So forget the bad block.
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002164 * Or possibly if failed and we need to record
2165 * a bad block.
NeilBrown749c55e2011-07-28 11:39:24 +10002166 */
2167 int m;
NeilBrown3cb03002011-10-11 16:45:26 +11002168 struct md_rdev *rdev;
NeilBrown749c55e2011-07-28 11:39:24 +10002169
2170 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2171 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002172 for (m = 0; m < conf->copies; m++) {
2173 int dev = r10_bio->devs[m].devnum;
2174 rdev = conf->mirrors[dev].rdev;
2175 if (r10_bio->devs[m].bio == NULL)
2176 continue;
2177 if (test_bit(BIO_UPTODATE,
NeilBrown749c55e2011-07-28 11:39:24 +10002178 &r10_bio->devs[m].bio->bi_flags)) {
NeilBrown749c55e2011-07-28 11:39:24 +10002179 rdev_clear_badblocks(
2180 rdev,
2181 r10_bio->devs[m].addr,
2182 r10_bio->sectors);
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002183 } else {
2184 if (!rdev_set_badblocks(
2185 rdev,
2186 r10_bio->devs[m].addr,
2187 r10_bio->sectors, 0))
2188 md_error(conf->mddev, rdev);
NeilBrown749c55e2011-07-28 11:39:24 +10002189 }
NeilBrown1a0b7cd2011-07-28 11:39:25 +10002190 }
NeilBrown749c55e2011-07-28 11:39:24 +10002191 put_buf(r10_bio);
2192 } else {
NeilBrownbd870a12011-07-28 11:39:24 +10002193 for (m = 0; m < conf->copies; m++) {
2194 int dev = r10_bio->devs[m].devnum;
2195 struct bio *bio = r10_bio->devs[m].bio;
2196 rdev = conf->mirrors[dev].rdev;
2197 if (bio == IO_MADE_GOOD) {
NeilBrown749c55e2011-07-28 11:39:24 +10002198 rdev_clear_badblocks(
2199 rdev,
2200 r10_bio->devs[m].addr,
2201 r10_bio->sectors);
2202 rdev_dec_pending(rdev, conf->mddev);
NeilBrownbd870a12011-07-28 11:39:24 +10002203 } else if (bio != NULL &&
2204 !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2205 if (!narrow_write_error(r10_bio, m)) {
2206 md_error(conf->mddev, rdev);
2207 set_bit(R10BIO_Degraded,
2208 &r10_bio->state);
2209 }
2210 rdev_dec_pending(rdev, conf->mddev);
NeilBrown749c55e2011-07-28 11:39:24 +10002211 }
NeilBrownbd870a12011-07-28 11:39:24 +10002212 }
2213 if (test_bit(R10BIO_WriteError,
2214 &r10_bio->state))
2215 close_write(r10_bio);
NeilBrown749c55e2011-07-28 11:39:24 +10002216 raid_end_bio_io(r10_bio);
2217 }
2218}
2219
NeilBrownfd01b882011-10-11 16:47:53 +11002220static void raid10d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
NeilBrown9f2c9d12011-10-11 16:48:43 +11002222 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 unsigned long flags;
NeilBrowne879a872011-10-11 16:49:02 +11002224 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 struct list_head *head = &conf->retry_list;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002226 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002230 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 for (;;) {
NeilBrowna35e63e2008-03-04 14:29:29 -08002232
Jens Axboe7eaceac2011-03-10 08:52:07 +01002233 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08002236 if (list_empty(head)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002237 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08002239 }
NeilBrown9f2c9d12011-10-11 16:48:43 +11002240 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08002242 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 spin_unlock_irqrestore(&conf->device_lock, flags);
2244
2245 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10002246 conf = mddev->private;
NeilBrownbd870a12011-07-28 11:39:24 +10002247 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2248 test_bit(R10BIO_WriteError, &r10_bio->state))
NeilBrown749c55e2011-07-28 11:39:24 +10002249 handle_write_completed(conf, r10_bio);
2250 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01002252 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 recovery_request_write(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002254 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
NeilBrown560f8e52011-07-28 11:39:23 +10002255 handle_read_error(mddev, r10_bio);
NeilBrown856e08e2011-07-28 11:39:23 +10002256 else {
2257 /* just a partial read to be scheduled from a
2258 * separate context
2259 */
2260 int slot = r10_bio->read_slot;
2261 generic_make_request(r10_bio->devs[slot].bio);
2262 }
NeilBrown4443ae12006-01-06 00:20:28 -08002263
NeilBrown1d9d5242009-10-16 15:55:32 +11002264 cond_resched();
NeilBrownde393cd2011-07-28 11:31:48 +10002265 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2266 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10002268 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
2271
NeilBrowne879a872011-10-11 16:49:02 +11002272static int init_resync(struct r10conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 int buffs;
2275
2276 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02002277 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2279 if (!conf->r10buf_pool)
2280 return -ENOMEM;
2281 conf->next_resync = 0;
2282 return 0;
2283}
2284
2285/*
2286 * perform a "sync" on one "block"
2287 *
2288 * We need to make sure that no normal I/O request - particularly write
2289 * requests - conflict with active sync requests.
2290 *
2291 * This is achieved by tracking pending requests and a 'barrier' concept
2292 * that can be installed to exclude normal IO requests.
2293 *
2294 * Resync and recovery are handled very differently.
2295 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2296 *
2297 * For resync, we iterate over virtual addresses, read all copies,
2298 * and update if there are differences. If only one copy is live,
2299 * skip it.
2300 * For recovery, we iterate over physical addresses, read a good
2301 * value for each non-in_sync drive, and over-write.
2302 *
2303 * So, for recovery we may have several outstanding complex requests for a
2304 * given address, one for each out-of-sync device. We model this by allocating
2305 * a number of r10_bio structures, one for each out-of-sync device.
2306 * As we setup these structures, we collect all bio's together into a list
2307 * which we then process collectively to add pages, and then process again
2308 * to pass to generic_make_request.
2309 *
2310 * The r10_bio structures are linked using a borrowed master_bio pointer.
2311 * This link is counted in ->remaining. When the r10_bio that points to NULL
2312 * has its remaining count decremented to 0, the whole complex operation
2313 * is complete.
2314 *
2315 */
2316
NeilBrownfd01b882011-10-11 16:47:53 +11002317static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
NeilBrownab9d47e2011-05-11 14:54:41 +10002318 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
NeilBrowne879a872011-10-11 16:49:02 +11002320 struct r10conf *conf = mddev->private;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002321 struct r10bio *r10_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct bio *biolist = NULL, *bio;
2323 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int i;
NeilBrown6cce3b232006-01-06 00:20:16 -08002325 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11002326 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 sector_t sectors_skipped = 0;
2328 int chunks_skipped = 0;
2329
2330 if (!conf->r10buf_pool)
2331 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07002332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11002335 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2337 max_sector = mddev->resync_max_sectors;
2338 if (sector_nr >= max_sector) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002339 /* If we aborted, we need to abort the
2340 * sync on the 'current' bitmap chucks (there can
2341 * be several when recovering multiple devices).
2342 * as we may have started syncing it but not finished.
2343 * We can find the current address in
2344 * mddev->curr_resync, but for recovery,
2345 * we need to convert that to several
2346 * virtual addresses.
2347 */
2348 if (mddev->curr_resync < max_sector) { /* aborted */
2349 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2350 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2351 &sync_blocks, 1);
2352 else for (i=0; i<conf->raid_disks; i++) {
2353 sector_t sect =
2354 raid10_find_virt(conf, mddev->curr_resync, i);
2355 bitmap_end_sync(mddev->bitmap, sect,
2356 &sync_blocks, 1);
2357 }
2358 } else /* completed sync */
2359 conf->fullsync = 0;
2360
2361 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07002363 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return sectors_skipped;
2365 }
2366 if (chunks_skipped >= conf->raid_disks) {
2367 /* if there has been nothing to do on any drive,
2368 * then there is nothing to do at all..
2369 */
NeilBrown57afd892005-06-21 17:17:13 -07002370 *skipped = 1;
2371 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373
NeilBrownc6207272008-02-06 01:39:52 -08002374 if (max_sector > mddev->resync_max)
2375 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /* make sure whole request will fit in a chunk - if chunks
2378 * are meaningful
2379 */
2380 if (conf->near_copies < conf->raid_disks &&
2381 max_sector > (sector_nr | conf->chunk_mask))
2382 max_sector = (sector_nr | conf->chunk_mask) + 1;
2383 /*
2384 * If there is non-resync activity waiting for us then
2385 * put in a delay to throttle resync.
2386 */
NeilBrown0a27ec92006-01-06 00:20:13 -08002387 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /* Again, very different code for resync and recovery.
2391 * Both must result in an r10bio with a list of bios that
2392 * have bi_end_io, bi_sector, bi_bdev set,
2393 * and bi_private set to the r10bio.
2394 * For recovery, we may actually create several r10bios
2395 * with 2 bios in each, that correspond to the bios in the main one.
2396 * In this case, the subordinate r10bios link back through a
2397 * borrowed master_bio pointer, and the counter in the master
2398 * includes a ref from each subordinate.
2399 */
2400 /* First, we decide what to do and set ->bi_end_io
2401 * To end_sync_read if we want to read, and
2402 * end_sync_write if we will want to write.
2403 */
2404
NeilBrown6cce3b232006-01-06 00:20:16 -08002405 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2407 /* recovery... the complicated one */
NeilBrowne875ece2011-07-28 11:39:24 +10002408 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 r10_bio = NULL;
2410
NeilBrownab9d47e2011-05-11 14:54:41 +10002411 for (i=0 ; i<conf->raid_disks; i++) {
2412 int still_degraded;
NeilBrown9f2c9d12011-10-11 16:48:43 +11002413 struct r10bio *rb2;
NeilBrownab9d47e2011-05-11 14:54:41 +10002414 sector_t sect;
2415 int must_sync;
NeilBrowne875ece2011-07-28 11:39:24 +10002416 int any_working;
NeilBrownab9d47e2011-05-11 14:54:41 +10002417
2418 if (conf->mirrors[i].rdev == NULL ||
2419 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
2420 continue;
2421
2422 still_degraded = 0;
2423 /* want to reconstruct this device */
2424 rb2 = r10_bio;
2425 sect = raid10_find_virt(conf, sector_nr, i);
2426 /* Unless we are doing a full sync, we only need
2427 * to recover the block if it is set in the bitmap
2428 */
2429 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2430 &sync_blocks, 1);
2431 if (sync_blocks < max_sync)
2432 max_sync = sync_blocks;
2433 if (!must_sync &&
2434 !conf->fullsync) {
2435 /* yep, skip the sync_blocks here, but don't assume
2436 * that there will never be anything to do here
NeilBrown6cce3b232006-01-06 00:20:16 -08002437 */
NeilBrownab9d47e2011-05-11 14:54:41 +10002438 chunks_skipped = -1;
2439 continue;
2440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
NeilBrownab9d47e2011-05-11 14:54:41 +10002442 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2443 raise_barrier(conf, rb2 != NULL);
2444 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
NeilBrownab9d47e2011-05-11 14:54:41 +10002446 r10_bio->master_bio = (struct bio*)rb2;
2447 if (rb2)
2448 atomic_inc(&rb2->remaining);
2449 r10_bio->mddev = mddev;
2450 set_bit(R10BIO_IsRecover, &r10_bio->state);
2451 r10_bio->sector = sect;
NeilBrown6cce3b232006-01-06 00:20:16 -08002452
NeilBrownab9d47e2011-05-11 14:54:41 +10002453 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10002454
NeilBrownab9d47e2011-05-11 14:54:41 +10002455 /* Need to check if the array will still be
2456 * degraded
2457 */
2458 for (j=0; j<conf->raid_disks; j++)
2459 if (conf->mirrors[j].rdev == NULL ||
2460 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
2461 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07002462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002464
2465 must_sync = bitmap_start_sync(mddev->bitmap, sect,
2466 &sync_blocks, still_degraded);
2467
NeilBrowne875ece2011-07-28 11:39:24 +10002468 any_working = 0;
NeilBrownab9d47e2011-05-11 14:54:41 +10002469 for (j=0; j<conf->copies;j++) {
NeilBrowne875ece2011-07-28 11:39:24 +10002470 int k;
NeilBrownab9d47e2011-05-11 14:54:41 +10002471 int d = r10_bio->devs[j].devnum;
NeilBrown5e570282011-07-28 11:39:25 +10002472 sector_t from_addr, to_addr;
NeilBrown3cb03002011-10-11 16:45:26 +11002473 struct md_rdev *rdev;
NeilBrown40c356c2011-07-28 11:39:24 +10002474 sector_t sector, first_bad;
2475 int bad_sectors;
NeilBrownab9d47e2011-05-11 14:54:41 +10002476 if (!conf->mirrors[d].rdev ||
2477 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
2478 continue;
2479 /* This is where we read from */
NeilBrowne875ece2011-07-28 11:39:24 +10002480 any_working = 1;
NeilBrown40c356c2011-07-28 11:39:24 +10002481 rdev = conf->mirrors[d].rdev;
2482 sector = r10_bio->devs[j].addr;
2483
2484 if (is_badblock(rdev, sector, max_sync,
2485 &first_bad, &bad_sectors)) {
2486 if (first_bad > sector)
2487 max_sync = first_bad - sector;
2488 else {
2489 bad_sectors -= (sector
2490 - first_bad);
2491 if (max_sync > bad_sectors)
2492 max_sync = bad_sectors;
2493 continue;
2494 }
2495 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002496 bio = r10_bio->devs[0].bio;
2497 bio->bi_next = biolist;
2498 biolist = bio;
2499 bio->bi_private = r10_bio;
2500 bio->bi_end_io = end_sync_read;
2501 bio->bi_rw = READ;
NeilBrown5e570282011-07-28 11:39:25 +10002502 from_addr = r10_bio->devs[j].addr;
2503 bio->bi_sector = from_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002504 conf->mirrors[d].rdev->data_offset;
2505 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2506 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2507 atomic_inc(&r10_bio->remaining);
2508 /* and we write to 'i' */
2509
2510 for (k=0; k<conf->copies; k++)
2511 if (r10_bio->devs[k].devnum == i)
2512 break;
2513 BUG_ON(k == conf->copies);
2514 bio = r10_bio->devs[1].bio;
2515 bio->bi_next = biolist;
2516 biolist = bio;
2517 bio->bi_private = r10_bio;
2518 bio->bi_end_io = end_sync_write;
2519 bio->bi_rw = WRITE;
NeilBrown5e570282011-07-28 11:39:25 +10002520 to_addr = r10_bio->devs[k].addr;
2521 bio->bi_sector = to_addr +
NeilBrownab9d47e2011-05-11 14:54:41 +10002522 conf->mirrors[i].rdev->data_offset;
2523 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
2524
2525 r10_bio->devs[0].devnum = d;
NeilBrown5e570282011-07-28 11:39:25 +10002526 r10_bio->devs[0].addr = from_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002527 r10_bio->devs[1].devnum = i;
NeilBrown5e570282011-07-28 11:39:25 +10002528 r10_bio->devs[1].addr = to_addr;
NeilBrownab9d47e2011-05-11 14:54:41 +10002529
2530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002532 if (j == conf->copies) {
NeilBrowne875ece2011-07-28 11:39:24 +10002533 /* Cannot recover, so abort the recovery or
2534 * record a bad block */
NeilBrownab9d47e2011-05-11 14:54:41 +10002535 put_buf(r10_bio);
2536 if (rb2)
2537 atomic_dec(&rb2->remaining);
2538 r10_bio = rb2;
NeilBrowne875ece2011-07-28 11:39:24 +10002539 if (any_working) {
2540 /* problem is that there are bad blocks
2541 * on other device(s)
2542 */
2543 int k;
2544 for (k = 0; k < conf->copies; k++)
2545 if (r10_bio->devs[k].devnum == i)
2546 break;
2547 if (!rdev_set_badblocks(
2548 conf->mirrors[i].rdev,
2549 r10_bio->devs[k].addr,
2550 max_sync, 0))
2551 any_working = 0;
2552 }
2553 if (!any_working) {
2554 if (!test_and_set_bit(MD_RECOVERY_INTR,
2555 &mddev->recovery))
2556 printk(KERN_INFO "md/raid10:%s: insufficient "
2557 "working devices for recovery.\n",
2558 mdname(mddev));
2559 conf->mirrors[i].recovery_disabled
2560 = mddev->recovery_disabled;
2561 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002562 break;
2563 }
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (biolist == NULL) {
2566 while (r10_bio) {
NeilBrown9f2c9d12011-10-11 16:48:43 +11002567 struct r10bio *rb2 = r10_bio;
2568 r10_bio = (struct r10bio*) rb2->master_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 rb2->master_bio = NULL;
2570 put_buf(rb2);
2571 }
2572 goto giveup;
2573 }
2574 } else {
2575 /* resync. Schedule a read for every block at this virt offset */
2576 int count = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08002577
NeilBrown78200d42009-02-25 13:18:47 +11002578 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2579
NeilBrown6cce3b232006-01-06 00:20:16 -08002580 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2581 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10002582 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
2583 &mddev->recovery)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08002584 /* We can skip this block */
2585 *skipped = 1;
2586 return sync_blocks + sectors_skipped;
2587 }
2588 if (sync_blocks < max_sync)
2589 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 r10_bio->mddev = mddev;
2593 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b232006-01-06 00:20:16 -08002594 raise_barrier(conf, 0);
2595 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 r10_bio->master_bio = NULL;
2598 r10_bio->sector = sector_nr;
2599 set_bit(R10BIO_IsSync, &r10_bio->state);
2600 raid10_find_phys(conf, r10_bio);
2601 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2602
2603 for (i=0; i<conf->copies; i++) {
2604 int d = r10_bio->devs[i].devnum;
NeilBrown40c356c2011-07-28 11:39:24 +10002605 sector_t first_bad, sector;
2606 int bad_sectors;
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 bio = r10_bio->devs[i].bio;
2609 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002610 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002612 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 continue;
NeilBrown40c356c2011-07-28 11:39:24 +10002614 sector = r10_bio->devs[i].addr;
2615 if (is_badblock(conf->mirrors[d].rdev,
2616 sector, max_sync,
2617 &first_bad, &bad_sectors)) {
2618 if (first_bad > sector)
2619 max_sync = first_bad - sector;
2620 else {
2621 bad_sectors -= (sector - first_bad);
2622 if (max_sync > bad_sectors)
2623 max_sync = max_sync;
2624 continue;
2625 }
2626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2628 atomic_inc(&r10_bio->remaining);
2629 bio->bi_next = biolist;
2630 biolist = bio;
2631 bio->bi_private = r10_bio;
2632 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002633 bio->bi_rw = READ;
NeilBrown40c356c2011-07-28 11:39:24 +10002634 bio->bi_sector = sector +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 conf->mirrors[d].rdev->data_offset;
2636 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2637 count++;
2638 }
2639
2640 if (count < 2) {
2641 for (i=0; i<conf->copies; i++) {
2642 int d = r10_bio->devs[i].devnum;
2643 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002644 rdev_dec_pending(conf->mirrors[d].rdev,
2645 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
2647 put_buf(r10_bio);
2648 biolist = NULL;
2649 goto giveup;
2650 }
2651 }
2652
2653 for (bio = biolist; bio ; bio=bio->bi_next) {
2654
2655 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2656 if (bio->bi_end_io)
2657 bio->bi_flags |= 1 << BIO_UPTODATE;
2658 bio->bi_vcnt = 0;
2659 bio->bi_idx = 0;
2660 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 bio->bi_size = 0;
2662 }
2663
2664 nr_sectors = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08002665 if (sector_nr + max_sync < max_sector)
2666 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 do {
2668 struct page *page;
2669 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (sector_nr + (len>>9) > max_sector)
2671 len = (max_sector - sector_nr) << 9;
2672 if (len == 0)
2673 break;
2674 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002675 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002677 if (bio_add_page(bio, page, len, 0))
2678 continue;
2679
2680 /* stop here */
2681 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2682 for (bio2 = biolist;
2683 bio2 && bio2 != bio;
2684 bio2 = bio2->bi_next) {
2685 /* remove last page from this bio */
2686 bio2->bi_vcnt--;
2687 bio2->bi_size -= len;
2688 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002690 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
2692 nr_sectors += len>>9;
2693 sector_nr += len>>9;
2694 } while (biolist->bi_vcnt < RESYNC_PAGES);
2695 bio_full:
2696 r10_bio->sectors = nr_sectors;
2697
2698 while (biolist) {
2699 bio = biolist;
2700 biolist = biolist->bi_next;
2701
2702 bio->bi_next = NULL;
2703 r10_bio = bio->bi_private;
2704 r10_bio->sectors = nr_sectors;
2705
2706 if (bio->bi_end_io == end_sync_read) {
2707 md_sync_acct(bio->bi_bdev, nr_sectors);
2708 generic_make_request(bio);
2709 }
2710 }
2711
NeilBrown57afd892005-06-21 17:17:13 -07002712 if (sectors_skipped)
2713 /* pretend they weren't skipped, it makes
2714 * no important difference in this case
2715 */
2716 md_done_sync(mddev, sectors_skipped, 1);
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return sectors_skipped + nr_sectors;
2719 giveup:
2720 /* There is nowhere to write, so all non-sync
NeilBrowne875ece2011-07-28 11:39:24 +10002721 * drives must be failed or in resync, all drives
2722 * have a bad block, so try the next chunk...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 */
NeilBrown09b40682009-02-25 13:18:47 +11002724 if (sector_nr + max_sync < max_sector)
2725 max_sector = sector_nr + max_sync;
2726
2727 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 chunks_skipped ++;
2729 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Dan Williams80c3a6c2009-03-17 18:10:40 -07002733static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11002734raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07002735{
2736 sector_t size;
NeilBrowne879a872011-10-11 16:49:02 +11002737 struct r10conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002738
2739 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002740 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002741 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002742 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002743
2744 size = sectors >> conf->chunk_shift;
2745 sector_div(size, conf->far_copies);
2746 size = size * raid_disks;
2747 sector_div(size, conf->near_copies);
2748
2749 return size << conf->chunk_shift;
2750}
2751
Trela, Maciejdab8b292010-03-08 16:02:45 +11002752
NeilBrowne879a872011-10-11 16:49:02 +11002753static struct r10conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
NeilBrowne879a872011-10-11 16:49:02 +11002755 struct r10conf *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002756 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002758 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Maciej Trelaf73ea872010-06-16 11:46:29 +01002760 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2761 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002762 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2763 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2764 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002765 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
NeilBrown2604b702006-01-06 00:20:36 -08002767
Maciej Trelaf73ea872010-06-16 11:46:29 +01002768 nc = mddev->new_layout & 255;
2769 fc = (mddev->new_layout >> 8) & 255;
2770 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002773 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002774 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002775 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 goto out;
2777 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002778
2779 err = -ENOMEM;
NeilBrowne879a872011-10-11 16:49:02 +11002780 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002781 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002783
NeilBrown4443ae12006-01-06 00:20:28 -08002784 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002785 GFP_KERNEL);
2786 if (!conf->mirrors)
2787 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002788
2789 conf->tmppage = alloc_page(GFP_KERNEL);
2790 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002791 goto out;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
NeilBrown64a742b2007-02-28 20:11:18 -08002794 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 conf->near_copies = nc;
2796 conf->far_copies = fc;
2797 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002798 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002799 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2800 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2801
2802 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2803 r10bio_pool_free, conf);
2804 if (!conf->r10bio_pool)
2805 goto out;
2806
Andre Noll58c0fed2009-03-31 14:33:13 +11002807 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002808 sector_div(size, fc);
2809 size = size * conf->raid_disks;
2810 sector_div(size, nc);
2811 /* 'size' is now the number of chunks in the array */
2812 /* calculate "used chunks per device" in 'stride' */
2813 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002814
2815 /* We need to round up when dividing by raid_disks to
2816 * get the stride size.
2817 */
2818 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002819 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002820
2821 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002822
NeilBrownc93983b2006-06-26 00:27:41 -07002823 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002824 stride = 1;
2825 else
NeilBrownc93983b2006-06-26 00:27:41 -07002826 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002827 conf->stride = stride << conf->chunk_shift;
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Neil Browne7e72bf2008-05-14 16:05:54 -07002830 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002831 INIT_LIST_HEAD(&conf->retry_list);
2832
2833 spin_lock_init(&conf->resync_lock);
2834 init_waitqueue_head(&conf->wait_barrier);
2835
2836 conf->thread = md_register_thread(raid10d, mddev, NULL);
2837 if (!conf->thread)
2838 goto out;
2839
Trela, Maciejdab8b292010-03-08 16:02:45 +11002840 conf->mddev = mddev;
2841 return conf;
2842
2843 out:
NeilBrown128595e2010-05-03 14:47:14 +10002844 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002845 mdname(mddev));
2846 if (conf) {
2847 if (conf->r10bio_pool)
2848 mempool_destroy(conf->r10bio_pool);
2849 kfree(conf->mirrors);
2850 safe_put_page(conf->tmppage);
2851 kfree(conf);
2852 }
2853 return ERR_PTR(err);
2854}
2855
NeilBrownfd01b882011-10-11 16:47:53 +11002856static int run(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002857{
NeilBrowne879a872011-10-11 16:49:02 +11002858 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002859 int i, disk_idx, chunk_size;
NeilBrown0f6d02d2011-10-11 16:48:46 +11002860 struct mirror_info *disk;
NeilBrown3cb03002011-10-11 16:45:26 +11002861 struct md_rdev *rdev;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002862 sector_t size;
2863
2864 /*
2865 * copy the already verified devices into our private RAID10
2866 * bookkeeping area. [whatever we allocate in run(),
2867 * should be freed in stop()]
2868 */
2869
2870 if (mddev->private == NULL) {
2871 conf = setup_conf(mddev);
2872 if (IS_ERR(conf))
2873 return PTR_ERR(conf);
2874 mddev->private = conf;
2875 }
2876 conf = mddev->private;
2877 if (!conf)
2878 goto out;
2879
Trela, Maciejdab8b292010-03-08 16:02:45 +11002880 mddev->thread = conf->thread;
2881 conf->thread = NULL;
2882
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002883 chunk_size = mddev->chunk_sectors << 9;
2884 blk_queue_io_min(mddev->queue, chunk_size);
2885 if (conf->raid_disks % conf->near_copies)
2886 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2887 else
2888 blk_queue_io_opt(mddev->queue, chunk_size *
2889 (conf->raid_disks / conf->near_copies));
2890
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002891 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown34b343c2011-07-28 11:31:47 +10002892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002894 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 || disk_idx < 0)
2896 continue;
2897 disk = conf->mirrors + disk_idx;
2898
2899 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002900 disk_stack_limits(mddev->gendisk, rdev->bdev,
2901 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002903 * violating it, so limit max_segments to 1 lying
2904 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 */
NeilBrown627a2d32010-03-08 16:44:38 +11002906 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2907 blk_queue_max_segments(mddev->queue, 1);
2908 blk_queue_segment_boundary(mddev->queue,
2909 PAGE_CACHE_SIZE - 1);
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
NeilBrown6d508242005-09-09 16:24:03 -07002914 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10002915 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10002916 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002917 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 goto out_free_conf;
2919 }
2920
2921 mddev->degraded = 0;
2922 for (i = 0; i < conf->raid_disks; i++) {
2923
2924 disk = conf->mirrors + i;
2925
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002926 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002927 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 disk->head_position = 0;
2929 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002930 if (disk->rdev)
2931 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
NeilBrownd890fa22011-10-26 11:54:39 +11002933 disk->recovery_disabled = mddev->recovery_disabled - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935
Andre Noll8c6ac862009-06-18 08:48:06 +10002936 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002937 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002938 " -- starting background reconstruction\n",
2939 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002941 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002942 mdname(mddev), conf->raid_disks - mddev->degraded,
2943 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /*
2945 * Ok, everything is just fine now
2946 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002947 mddev->dev_sectors = conf->dev_sectors;
2948 size = raid10_size(mddev, 0, 0);
2949 md_set_array_sectors(mddev, size);
2950 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
NeilBrown0d129222006-10-03 01:15:54 -07002952 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2953 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /* Calculate max read-ahead size.
2956 * We need to readahead at least twice a whole stripe....
2957 * maybe...
2958 */
2959 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002960 int stripe = conf->raid_disks *
2961 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 stripe /= conf->near_copies;
2963 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2964 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2965 }
2966
NeilBrown84707f32010-03-16 17:23:35 +11002967 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002969
2970 if (md_integrity_register(mddev))
2971 goto out_free_conf;
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return 0;
2974
2975out_free_conf:
NeilBrown01f96c02011-09-21 15:30:20 +10002976 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (conf->r10bio_pool)
2978 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002979 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002980 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 kfree(conf);
2982 mddev->private = NULL;
2983out:
2984 return -EIO;
2985}
2986
NeilBrownfd01b882011-10-11 16:47:53 +11002987static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
NeilBrowne879a872011-10-11 16:49:02 +11002989 struct r10conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
NeilBrown409c57f2009-03-31 14:39:39 +11002991 raise_barrier(conf, 0);
2992 lower_barrier(conf);
2993
NeilBrown01f96c02011-09-21 15:30:20 +10002994 md_unregister_thread(&mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2996 if (conf->r10bio_pool)
2997 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002998 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 kfree(conf);
3000 mddev->private = NULL;
3001 return 0;
3002}
3003
NeilBrownfd01b882011-10-11 16:47:53 +11003004static void raid10_quiesce(struct mddev *mddev, int state)
NeilBrown6cce3b232006-01-06 00:20:16 -08003005{
NeilBrowne879a872011-10-11 16:49:02 +11003006 struct r10conf *conf = mddev->private;
NeilBrown6cce3b232006-01-06 00:20:16 -08003007
3008 switch(state) {
3009 case 1:
3010 raise_barrier(conf, 0);
3011 break;
3012 case 0:
3013 lower_barrier(conf);
3014 break;
3015 }
NeilBrown6cce3b232006-01-06 00:20:16 -08003016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
NeilBrownfd01b882011-10-11 16:47:53 +11003018static void *raid10_takeover_raid0(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003019{
NeilBrown3cb03002011-10-11 16:45:26 +11003020 struct md_rdev *rdev;
NeilBrowne879a872011-10-11 16:49:02 +11003021 struct r10conf *conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003022
3023 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10003024 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
3025 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003026 return ERR_PTR(-EINVAL);
3027 }
3028
Trela, Maciejdab8b292010-03-08 16:02:45 +11003029 /* Set new parameters */
3030 mddev->new_level = 10;
3031 /* new layout: far_copies = 1, near_copies = 2 */
3032 mddev->new_layout = (1<<8) + 2;
3033 mddev->new_chunk_sectors = mddev->chunk_sectors;
3034 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003035 mddev->raid_disks *= 2;
3036 /* make sure it will be not marked as dirty */
3037 mddev->recovery_cp = MaxSector;
3038
3039 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003040 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003041 list_for_each_entry(rdev, &mddev->disks, same_set)
3042 if (rdev->raid_disk >= 0)
3043 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01003044 conf->barrier = 1;
3045 }
3046
Trela, Maciejdab8b292010-03-08 16:02:45 +11003047 return conf;
3048}
3049
NeilBrownfd01b882011-10-11 16:47:53 +11003050static void *raid10_takeover(struct mddev *mddev)
Trela, Maciejdab8b292010-03-08 16:02:45 +11003051{
NeilBrowne373ab12011-10-11 16:48:59 +11003052 struct r0conf *raid0_conf;
Trela, Maciejdab8b292010-03-08 16:02:45 +11003053
3054 /* raid10 can take over:
3055 * raid0 - providing it has only two drives
3056 */
3057 if (mddev->level == 0) {
3058 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11003059 raid0_conf = mddev->private;
3060 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10003061 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
3062 " with more than one zone.\n",
3063 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11003064 return ERR_PTR(-EINVAL);
3065 }
3066 return raid10_takeover_raid0(mddev);
3067 }
3068 return ERR_PTR(-EINVAL);
3069}
3070
NeilBrown84fc4b52011-10-11 16:49:58 +11003071static struct md_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08003074 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 .owner = THIS_MODULE,
3076 .make_request = make_request,
3077 .run = run,
3078 .stop = stop,
3079 .status = status,
3080 .error_handler = error,
3081 .hot_add_disk = raid10_add_disk,
3082 .hot_remove_disk= raid10_remove_disk,
3083 .spare_active = raid10_spare_active,
3084 .sync_request = sync_request,
NeilBrown6cce3b232006-01-06 00:20:16 -08003085 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07003086 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11003087 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088};
3089
3090static int __init raid_init(void)
3091{
NeilBrown2604b702006-01-06 00:20:36 -08003092 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
3095static void raid_exit(void)
3096{
NeilBrown2604b702006-01-06 00:20:36 -08003097 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
3100module_init(raid_init);
3101module_exit(raid_exit);
3102MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11003103MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003105MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08003106MODULE_ALIAS("md-level-10");
NeilBrown34db0cd2011-10-11 16:50:01 +11003107
3108module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);