blob: e434f1e8d22331c2f4a4e8ccaa1a9c2fb386e21e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
NeilBrownbff61972009-03-31 14:33:13 +110024#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110025#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110026#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110027#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110028#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * RAID10 provides a combination of RAID0 and RAID1 functionality.
32 * The layout of data is defined by
33 * chunk_size
34 * raid_disks
35 * near_copies (stored in low byte of layout)
36 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070037 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 *
39 * The data to be stored is divided into chunks using chunksize.
40 * Each device is divided into far_copies sections.
41 * In each section, chunks are laid out in a style similar to raid0, but
42 * near_copies copies of each chunk is stored (each on a different drive).
43 * The starting device for each section is offset near_copies from the starting
44 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070045 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * drive.
47 * near_copies and far_copies must be at least one, and their product is at most
48 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070049 *
50 * If far_offset is true, then the far_copies are handled a bit differently.
51 * The copies are still in different stripes, but instead of be very far apart
52 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
55/*
56 * Number of guaranteed r10bios in case of extreme VM load:
57 */
58#define NR_RAID10_BIOS 256
59
NeilBrown0a27ec92006-01-06 00:20:13 -080060static void allow_barrier(conf_t *conf);
61static void lower_barrier(conf_t *conf);
62
Al Virodd0fc662005-10-07 07:46:04 +010063static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 conf_t *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int size = offsetof(struct r10bio_s, devs[conf->copies]);
67
68 /* allocate a r10bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010069 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72static void r10bio_pool_free(void *r10_bio, void *data)
73{
74 kfree(r10_bio);
75}
76
NeilBrown0310fa22008-08-05 15:54:14 +100077/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100080/* amount of memory to reserve for resync requests */
81#define RESYNC_WINDOW (1024*1024)
82/* maximum number of concurrent requests, memory permitting */
83#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * When performing a resync, we need to read and compare, so
87 * we need as many pages are there are copies.
88 * When performing a recovery, we need 2 bios, one for read,
89 * one for write (we recover only one drive per r10buf)
90 *
91 */
Al Virodd0fc662005-10-07 07:46:04 +010092static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 conf_t *conf = data;
95 struct page *page;
96 r10bio_t *r10_bio;
97 struct bio *bio;
98 int i, j;
99 int nalloc;
100
101 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100102 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
106 nalloc = conf->copies; /* resync */
107 else
108 nalloc = 2; /* recovery */
109
110 /*
111 * Allocate bios.
112 */
113 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100114 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (!bio)
116 goto out_free_bio;
117 r10_bio->devs[j].bio = bio;
118 }
119 /*
120 * Allocate RESYNC_PAGES data pages and attach them
121 * where needed.
122 */
123 for (j = 0 ; j < nalloc; j++) {
124 bio = r10_bio->devs[j].bio;
125 for (i = 0; i < RESYNC_PAGES; i++) {
126 page = alloc_page(gfp_flags);
127 if (unlikely(!page))
128 goto out_free_pages;
129
130 bio->bi_io_vec[i].bv_page = page;
131 }
132 }
133
134 return r10_bio;
135
136out_free_pages:
137 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800138 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 while (j--)
140 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800141 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 j = -1;
143out_free_bio:
144 while ( ++j < nalloc )
145 bio_put(r10_bio->devs[j].bio);
146 r10bio_pool_free(r10_bio, conf);
147 return NULL;
148}
149
150static void r10buf_pool_free(void *__r10_bio, void *data)
151{
152 int i;
153 conf_t *conf = data;
154 r10bio_t *r10bio = __r10_bio;
155 int j;
156
157 for (j=0; j < conf->copies; j++) {
158 struct bio *bio = r10bio->devs[j].bio;
159 if (bio) {
160 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800161 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 bio->bi_io_vec[i].bv_page = NULL;
163 }
164 bio_put(bio);
165 }
166 }
167 r10bio_pool_free(r10bio, conf);
168}
169
170static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
171{
172 int i;
173
174 for (i = 0; i < conf->copies; i++) {
175 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -0800176 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 bio_put(*bio);
178 *bio = NULL;
179 }
180}
181
Arjan van de Ven858119e2006-01-14 13:20:43 -0800182static void free_r10bio(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
NeilBrown070ec552009-06-16 16:54:21 +1000184 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /*
187 * Wake up any possible resync thread that waits for the device
188 * to go idle.
189 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800190 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 put_all_bios(conf, r10_bio);
193 mempool_free(r10_bio, conf->r10bio_pool);
194}
195
Arjan van de Ven858119e2006-01-14 13:20:43 -0800196static void put_buf(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
NeilBrown070ec552009-06-16 16:54:21 +1000198 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 mempool_free(r10_bio, conf->r10buf_pool);
201
NeilBrown0a27ec92006-01-06 00:20:13 -0800202 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205static void reschedule_retry(r10bio_t *r10_bio)
206{
207 unsigned long flags;
208 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +1000209 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 spin_lock_irqsave(&conf->device_lock, flags);
212 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800213 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_unlock_irqrestore(&conf->device_lock, flags);
215
Arthur Jones388667b2008-07-25 12:03:38 -0700216 /* wake up frozen array... */
217 wake_up(&conf->wait_barrier);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 md_wakeup_thread(mddev->thread);
220}
221
222/*
223 * raid_end_bio_io() is called when we have finished servicing a mirrored
224 * operation and are ready to return a success/failure code to the buffer
225 * cache layer.
226 */
227static void raid_end_bio_io(r10bio_t *r10_bio)
228{
229 struct bio *bio = r10_bio->master_bio;
230
NeilBrown6712ecf2007-09-27 12:47:43 +0200231 bio_endio(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
233 free_r10bio(r10_bio);
234}
235
236/*
237 * Update disk head position estimator based on IRQ completion info.
238 */
239static inline void update_head_pos(int slot, r10bio_t *r10_bio)
240{
NeilBrown070ec552009-06-16 16:54:21 +1000241 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
244 r10_bio->devs[slot].addr + (r10_bio->sectors);
245}
246
Namhyung Kim778ca012011-07-18 17:38:47 +1000247/*
248 * Find the disk number which triggered given bio
249 */
250static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio, struct bio *bio)
251{
252 int slot;
253
254 for (slot = 0; slot < conf->copies; slot++)
255 if (r10_bio->devs[slot].bio == bio)
256 break;
257
258 BUG_ON(slot == conf->copies);
259 update_head_pos(slot, r10_bio);
260
261 return r10_bio->devs[slot].devnum;
262}
263
NeilBrown6712ecf2007-09-27 12:47:43 +0200264static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100267 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000269 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 slot = r10_bio->read_slot;
273 dev = r10_bio->devs[slot].devnum;
274 /*
275 * this branch is our 'one mirror IO has finished' event handler:
276 */
NeilBrown4443ae12006-01-06 00:20:28 -0800277 update_head_pos(slot, r10_bio);
278
279 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /*
281 * Set R10BIO_Uptodate in our master bio, so that
282 * we will return a good error code to the higher
283 * levels even if IO on some other mirrored buffer fails.
284 *
285 * The 'master' represents the composite IO operation to
286 * user-side. So if something waits for IO, then it will
287 * wait for the 'master' bio.
288 */
289 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 raid_end_bio_io(r10_bio);
NeilBrown7c4e06f2011-05-11 14:53:17 +1000291 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800292 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000294 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
296 char b[BDEVNAME_SIZE];
297 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +1000298 printk(KERN_ERR "md/raid10:%s: %s: rescheduling sector %llu\n",
299 mdname(conf->mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
301 reschedule_retry(r10_bio);
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
NeilBrown6712ecf2007-09-27 12:47:43 +0200305static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100308 r10bio_t *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000309 int dev;
NeilBrown070ec552009-06-16 16:54:21 +1000310 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Namhyung Kim778ca012011-07-18 17:38:47 +1000312 dev = find_bio_disk(conf, r10_bio, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /*
315 * this branch is our 'one mirror IO has finished' event handler:
316 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800317 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800319 /* an I/O failed, we can't clear the bitmap */
320 set_bit(R10BIO_Degraded, &r10_bio->state);
321 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /*
323 * Set R10BIO_Uptodate in our master bio, so that
324 * we will return a good error code for to the higher
325 * levels even if IO on some other mirrored buffer fails.
326 *
327 * The 'master' represents the composite IO operation to
328 * user-side. So if something waits for IO, then it will
329 * wait for the 'master' bio.
330 */
331 set_bit(R10BIO_Uptodate, &r10_bio->state);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /*
334 *
335 * Let's see if all mirrored write operations have finished
336 * already.
337 */
338 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800339 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 md_write_end(r10_bio->mddev);
345 raid_end_bio_io(r10_bio);
346 }
347
348 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351
352/*
353 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300354 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * parameters: near_copies and far_copies.
356 * near_copies * far_copies must be <= raid_disks.
357 * Normally one of these will be 1.
358 * If both are 1, we get raid0.
359 * If near_copies == raid_disks, we get raid1.
360 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300361 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * first chunk, followed by near_copies copies of the next chunk and
363 * so on.
364 * If far_copies > 1, then after 1/far_copies of the array has been assigned
365 * as described above, we start again with a device offset of near_copies.
366 * So we effectively have another copy of the whole array further down all
367 * the drives, but with blocks on different drives.
368 * With this layout, and block is never stored twice on the one device.
369 *
370 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700371 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *
373 * raid10_find_virt does the reverse mapping, from a device and a
374 * sector offset to a virtual address
375 */
376
377static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
378{
379 int n,f;
380 sector_t sector;
381 sector_t chunk;
382 sector_t stripe;
383 int dev;
384
385 int slot = 0;
386
387 /* now calculate first sector/dev */
388 chunk = r10bio->sector >> conf->chunk_shift;
389 sector = r10bio->sector & conf->chunk_mask;
390
391 chunk *= conf->near_copies;
392 stripe = chunk;
393 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700394 if (conf->far_offset)
395 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 sector += stripe << conf->chunk_shift;
398
399 /* and calculate all the others */
400 for (n=0; n < conf->near_copies; n++) {
401 int d = dev;
402 sector_t s = sector;
403 r10bio->devs[slot].addr = sector;
404 r10bio->devs[slot].devnum = d;
405 slot++;
406
407 for (f = 1; f < conf->far_copies; f++) {
408 d += conf->near_copies;
409 if (d >= conf->raid_disks)
410 d -= conf->raid_disks;
411 s += conf->stride;
412 r10bio->devs[slot].devnum = d;
413 r10bio->devs[slot].addr = s;
414 slot++;
415 }
416 dev++;
417 if (dev >= conf->raid_disks) {
418 dev = 0;
419 sector += (conf->chunk_mask + 1);
420 }
421 }
422 BUG_ON(slot != conf->copies);
423}
424
425static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
426{
427 sector_t offset, chunk, vchunk;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700430 if (conf->far_offset) {
431 int fc;
432 chunk = sector >> conf->chunk_shift;
433 fc = sector_div(chunk, conf->far_copies);
434 dev -= fc * conf->near_copies;
435 if (dev < 0)
436 dev += conf->raid_disks;
437 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800438 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700439 sector -= conf->stride;
440 if (dev < conf->near_copies)
441 dev += conf->raid_disks - conf->near_copies;
442 else
443 dev -= conf->near_copies;
444 }
445 chunk = sector >> conf->chunk_shift;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 vchunk = chunk * conf->raid_disks + dev;
448 sector_div(vchunk, conf->near_copies);
449 return (vchunk << conf->chunk_shift) + offset;
450}
451
452/**
453 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
454 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200455 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * @biovec: the request that could be merged to it.
457 *
458 * Return amount of bytes we can accept at this offset
459 * If near_copies == raid_disk, there are no striping issues,
460 * but in that case, the function isn't called at all.
461 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200462static int raid10_mergeable_bvec(struct request_queue *q,
463 struct bvec_merge_data *bvm,
464 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200467 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000469 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200470 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
473 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200474 if (max <= biovec->bv_len && bio_sectors == 0)
475 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 else
477 return max;
478}
479
480/*
481 * This routine returns the disk from which the requested read should
482 * be done. There is a per-array 'next expected sequential IO' sector
483 * number - if this matches on the next IO then we use the last disk.
484 * There is also a per-disk 'last know head position' sector that is
485 * maintained from IRQ contexts, both the normal and the resync IO
486 * completion handlers update this position correctly. If there is no
487 * perfect sequential match then we pick the disk whose head is closest.
488 *
489 * If there are 2 mirrors in the same 2 devices, performance degrades
490 * because position is mirror, not device based.
491 *
492 * The rdev for the device selected will have nr_pending incremented.
493 */
494
495/*
496 * FIXME: possibly should rethink readbalancing and do it differently
497 * depending on near_copies / far_copies geometry.
498 */
499static int read_balance(conf_t *conf, r10bio_t *r10_bio)
500{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000501 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000502 int disk, slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 const int sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000504 sector_t new_distance, best_dist;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800505 mdk_rdev_t *rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000506 int do_balance;
507 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 raid10_find_phys(conf, r10_bio);
510 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000511retry:
512 best_slot = -1;
513 best_dist = MaxSector;
514 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
516 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800517 * device if no resync is going on (recovery is ok), or below
518 * the resync window. We take the first readable disk when
519 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
521 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000522 && (this_sector + sectors >= conf->next_resync))
523 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
NeilBrown56d99122011-05-11 14:27:03 +1000525 for (slot = 0; slot < conf->copies ; slot++) {
526 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000528 disk = r10_bio->devs[slot].devnum;
529 rdev = rcu_dereference(conf->mirrors[disk].rdev);
530 if (rdev == NULL)
531 continue;
532 if (!test_bit(In_sync, &rdev->flags))
533 continue;
534
535 if (!do_balance)
536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
NeilBrown22dfdf52005-11-28 13:44:09 -0800538 /* This optimisation is debatable, and completely destroys
539 * sequential read speed for 'far copies' arrays. So only
540 * keep it for 'near' arrays, and review those later.
541 */
NeilBrown56d99122011-05-11 14:27:03 +1000542 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800544
545 /* for far > 1 always use the lowest address */
546 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000547 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800548 else
NeilBrown56d99122011-05-11 14:27:03 +1000549 new_distance = abs(r10_bio->devs[slot].addr -
550 conf->mirrors[disk].head_position);
551 if (new_distance < best_dist) {
552 best_dist = new_distance;
553 best_slot = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 }
NeilBrown56d99122011-05-11 14:27:03 +1000556 if (slot == conf->copies)
557 slot = best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
NeilBrown56d99122011-05-11 14:27:03 +1000559 if (slot >= 0) {
560 disk = r10_bio->devs[slot].devnum;
561 rdev = rcu_dereference(conf->mirrors[disk].rdev);
562 if (!rdev)
563 goto retry;
564 atomic_inc(&rdev->nr_pending);
565 if (test_bit(Faulty, &rdev->flags)) {
566 /* Cannot risk returning a device that failed
567 * before we inc'ed nr_pending
568 */
569 rdev_dec_pending(rdev, conf->mddev);
570 goto retry;
571 }
572 r10_bio->read_slot = slot;
573 } else
NeilBrown29fc7e32006-02-03 03:03:41 -0800574 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 rcu_read_unlock();
576
577 return disk;
578}
579
NeilBrown0d129222006-10-03 01:15:54 -0700580static int raid10_congested(void *data, int bits)
581{
582 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000583 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700584 int i, ret = 0;
585
NeilBrown3fa841d2009-09-23 18:10:29 +1000586 if (mddev_congested(mddev, bits))
587 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700588 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100589 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700590 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
591 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200592 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700593
594 ret |= bdi_congested(&q->backing_dev_info, bits);
595 }
596 }
597 rcu_read_unlock();
598 return ret;
599}
600
Jens Axboe7eaceac2011-03-10 08:52:07 +0100601static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800602{
603 /* Any writes that have been queued but are awaiting
604 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800605 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800606 spin_lock_irq(&conf->device_lock);
607
608 if (conf->pending_bio_list.head) {
609 struct bio *bio;
610 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800611 spin_unlock_irq(&conf->device_lock);
612 /* flush any pending bitmap writes to disk
613 * before proceeding w/ I/O */
614 bitmap_unplug(conf->mddev->bitmap);
615
616 while (bio) { /* submit pending writes */
617 struct bio *next = bio->bi_next;
618 bio->bi_next = NULL;
619 generic_make_request(bio);
620 bio = next;
621 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800622 } else
623 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800624}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100625
NeilBrown0a27ec92006-01-06 00:20:13 -0800626/* Barriers....
627 * Sometimes we need to suspend IO while we do something else,
628 * either some resync/recovery, or reconfigure the array.
629 * To do this we raise a 'barrier'.
630 * The 'barrier' is a counter that can be raised multiple times
631 * to count how many activities are happening which preclude
632 * normal IO.
633 * We can only raise the barrier if there is no pending IO.
634 * i.e. if nr_pending == 0.
635 * We choose only to raise the barrier if no-one is waiting for the
636 * barrier to go down. This means that as soon as an IO request
637 * is ready, no other operations which require a barrier will start
638 * until the IO request has had a chance.
639 *
640 * So: regular IO calls 'wait_barrier'. When that returns there
641 * is no backgroup IO happening, It must arrange to call
642 * allow_barrier when it has finished its IO.
643 * backgroup IO calls must call raise_barrier. Once that returns
644 * there is no normal IO happeing. It must arrange to call
645 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
NeilBrown6cce3b22006-01-06 00:20:16 -0800648static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
NeilBrown6cce3b22006-01-06 00:20:16 -0800650 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
NeilBrown6cce3b22006-01-06 00:20:16 -0800653 /* Wait until no block IO is waiting (unless 'force') */
654 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000655 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800656
657 /* block any new IO from starting */
658 conf->barrier++;
659
NeilBrownc3b328a2011-04-18 18:25:43 +1000660 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800661 wait_event_lock_irq(conf->wait_barrier,
662 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000663 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_unlock_irq(&conf->resync_lock);
666}
667
NeilBrown0a27ec92006-01-06 00:20:13 -0800668static void lower_barrier(conf_t *conf)
669{
670 unsigned long flags;
671 spin_lock_irqsave(&conf->resync_lock, flags);
672 conf->barrier--;
673 spin_unlock_irqrestore(&conf->resync_lock, flags);
674 wake_up(&conf->wait_barrier);
675}
676
677static void wait_barrier(conf_t *conf)
678{
679 spin_lock_irq(&conf->resync_lock);
680 if (conf->barrier) {
681 conf->nr_waiting++;
682 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
683 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000684 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800685 conf->nr_waiting--;
686 }
687 conf->nr_pending++;
688 spin_unlock_irq(&conf->resync_lock);
689}
690
691static void allow_barrier(conf_t *conf)
692{
693 unsigned long flags;
694 spin_lock_irqsave(&conf->resync_lock, flags);
695 conf->nr_pending--;
696 spin_unlock_irqrestore(&conf->resync_lock, flags);
697 wake_up(&conf->wait_barrier);
698}
699
NeilBrown4443ae12006-01-06 00:20:28 -0800700static void freeze_array(conf_t *conf)
701{
702 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800703 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800704 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800705 * wait until nr_pending match nr_queued+1
706 * This is called in the context of one normal IO request
707 * that has failed. Thus any sync request that might be pending
708 * will be blocked by nr_pending, and we need to wait for
709 * pending IO requests to complete or be queued for re-try.
710 * Thus the number queued (nr_queued) plus this request (1)
711 * must match the number of pending IOs (nr_pending) before
712 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800713 */
714 spin_lock_irq(&conf->resync_lock);
715 conf->barrier++;
716 conf->nr_waiting++;
717 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800718 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800719 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000720 flush_pending_writes(conf));
721
NeilBrown4443ae12006-01-06 00:20:28 -0800722 spin_unlock_irq(&conf->resync_lock);
723}
724
725static void unfreeze_array(conf_t *conf)
726{
727 /* reverse the effect of the freeze */
728 spin_lock_irq(&conf->resync_lock);
729 conf->barrier--;
730 conf->nr_waiting--;
731 wake_up(&conf->wait_barrier);
732 spin_unlock_irq(&conf->resync_lock);
733}
734
NeilBrown21a52c62010-04-01 15:02:13 +1100735static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
NeilBrown070ec552009-06-16 16:54:21 +1000737 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mirror_info_t *mirror;
739 r10bio_t *r10_bio;
740 struct bio *read_bio;
741 int i;
742 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100743 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000744 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200745 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800746 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700747 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000748 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Tejun Heoe9c74692010-09-03 11:56:18 +0200750 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
751 md_flush_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700752 return 0;
753 }
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* If this request crosses a chunk boundary, we need to
756 * split it. This will only happen for 1 PAGE (or less) requests.
757 */
758 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
759 > chunk_sects &&
760 conf->near_copies < conf->raid_disks)) {
761 struct bio_pair *bp;
762 /* Sanity check -- queue functions should prevent this happening */
763 if (bio->bi_vcnt != 1 ||
764 bio->bi_idx != 0)
765 goto bad_map;
766 /* This is a one page bio that upper layers
767 * refuse to split for us, so we need to split it.
768 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200769 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000771
772 /* Each of these 'make_request' calls will call 'wait_barrier'.
773 * If the first succeeds but the second blocks due to the resync
774 * thread raising the barrier, we will deadlock because the
775 * IO to the underlying device will be queued in generic_make_request
776 * and will never complete, so will never reduce nr_pending.
777 * So increment nr_waiting here so no new raise_barriers will
778 * succeed, and so the second wait_barrier cannot block.
779 */
780 spin_lock_irq(&conf->resync_lock);
781 conf->nr_waiting++;
782 spin_unlock_irq(&conf->resync_lock);
783
NeilBrown21a52c62010-04-01 15:02:13 +1100784 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100786 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 generic_make_request(&bp->bio2);
788
NeilBrown51e9ac72010-08-07 21:17:00 +1000789 spin_lock_irq(&conf->resync_lock);
790 conf->nr_waiting--;
791 wake_up(&conf->wait_barrier);
792 spin_unlock_irq(&conf->resync_lock);
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 bio_pair_release(bp);
795 return 0;
796 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000797 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
798 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
800
NeilBrown6712ecf2007-09-27 12:47:43 +0200801 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803 }
804
NeilBrown3d310eb2005-06-21 17:17:26 -0700805 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /*
808 * Register the new request and wait if the reconstruction
809 * thread has put up a bar for new requests.
810 * Continue immediately if no resync is active currently.
811 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800812 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
815
816 r10_bio->master_bio = bio;
817 r10_bio->sectors = bio->bi_size >> 9;
818
819 r10_bio->mddev = mddev;
820 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800821 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Jens Axboea3623572005-11-01 09:26:16 +0100823 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
825 * read balancing logic:
826 */
827 int disk = read_balance(conf, r10_bio);
828 int slot = r10_bio->read_slot;
829 if (disk < 0) {
830 raid_end_bio_io(r10_bio);
831 return 0;
832 }
833 mirror = conf->mirrors + disk;
834
NeilBrowna167f662010-10-26 18:31:13 +1100835 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 r10_bio->devs[slot].bio = read_bio;
838
839 read_bio->bi_sector = r10_bio->devs[slot].addr +
840 mirror->rdev->data_offset;
841 read_bio->bi_bdev = mirror->rdev->bdev;
842 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200843 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 read_bio->bi_private = r10_bio;
845
846 generic_make_request(read_bio);
847 return 0;
848 }
849
850 /*
851 * WRITE:
852 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700853 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * inc refcount on their rdev. Record them by setting
855 * bios[x] to bio
856 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000857 plugged = mddev_check_plugged(mddev);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700860 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700861 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 rcu_read_lock();
863 for (i = 0; i < conf->copies; i++) {
864 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800865 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700866 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
867 atomic_inc(&rdev->nr_pending);
868 blocked_rdev = rdev;
869 break;
870 }
871 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800872 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800874 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800876 set_bit(R10BIO_Degraded, &r10_bio->state);
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 rcu_read_unlock();
880
Dan Williams6bfe0b42008-04-30 00:52:32 -0700881 if (unlikely(blocked_rdev)) {
882 /* Have to wait for this device to get unblocked, then retry */
883 int j;
884 int d;
885
886 for (j = 0; j < i; j++)
887 if (r10_bio->devs[j].bio) {
888 d = r10_bio->devs[j].devnum;
889 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
890 }
891 allow_barrier(conf);
892 md_wait_for_blocked_rdev(blocked_rdev, mddev);
893 wait_barrier(conf);
894 goto retry_write;
895 }
896
NeilBrown4e780642010-10-19 12:54:01 +1100897 atomic_set(&r10_bio->remaining, 1);
898 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 for (i = 0; i < conf->copies; i++) {
901 struct bio *mbio;
902 int d = r10_bio->devs[i].devnum;
903 if (!r10_bio->devs[i].bio)
904 continue;
905
NeilBrowna167f662010-10-26 18:31:13 +1100906 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 r10_bio->devs[i].bio = mbio;
908
909 mbio->bi_sector = r10_bio->devs[i].addr+
910 conf->mirrors[d].rdev->data_offset;
911 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
912 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +0200913 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 mbio->bi_private = r10_bio;
915
916 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +1100917 spin_lock_irqsave(&conf->device_lock, flags);
918 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +1100919 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
NeilBrown4e780642010-10-19 12:54:01 +1100922 if (atomic_dec_and_test(&r10_bio->remaining)) {
923 /* This matches the end of raid10_end_write_request() */
924 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
925 r10_bio->sectors,
926 !test_bit(R10BIO_Degraded, &r10_bio->state),
927 0);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700928 md_write_end(mddev);
929 raid_end_bio_io(r10_bio);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700930 }
931
NeilBrowna35e63e2008-03-04 14:29:29 -0800932 /* In case raid10d snuck in to freeze_array */
933 wake_up(&conf->wait_barrier);
934
NeilBrownc3b328a2011-04-18 18:25:43 +1000935 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -0800936 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
939
940static void status(struct seq_file *seq, mddev_t *mddev)
941{
NeilBrown070ec552009-06-16 16:54:21 +1000942 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 int i;
944
945 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +1000946 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (conf->near_copies > 1)
948 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700949 if (conf->far_copies > 1) {
950 if (conf->far_offset)
951 seq_printf(seq, " %d offset-copies", conf->far_copies);
952 else
953 seq_printf(seq, " %d far-copies", conf->far_copies);
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700956 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 for (i = 0; i < conf->raid_disks; i++)
958 seq_printf(seq, "%s",
959 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800960 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 seq_printf(seq, "]");
962}
963
964static void error(mddev_t *mddev, mdk_rdev_t *rdev)
965{
966 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +1000967 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /*
970 * If it is not operational, then we have already marked it as dead
971 * else if it is the last working disks, ignore the error, let the
972 * next level up know.
973 * else mark the drive as failed
974 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800975 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -0700976 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /*
978 * Don't fail the drive, just return an IO error.
979 * The test should really be more sophisticated than
980 * "working_disks == 1", but it isn't critical, and
981 * can wait until we do more sophisticated "is the drive
982 * really dead" tests...
983 */
984 return;
NeilBrownc04be0a2006-10-03 01:15:53 -0700985 if (test_and_clear_bit(In_sync, &rdev->flags)) {
986 unsigned long flags;
987 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700989 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
991 * if recovery is running, make sure it aborts.
992 */
NeilBrowndfc70642008-05-23 13:04:39 -0700993 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800995 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -0700996 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +1100997 printk(KERN_ALERT
998 "md/raid10:%s: Disk failure on %s, disabling device.\n"
999 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001000 mdname(mddev), bdevname(rdev->bdev, b),
1001 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004static void print_conf(conf_t *conf)
1005{
1006 int i;
1007 mirror_info_t *tmp;
1008
NeilBrown128595e2010-05-03 14:47:14 +10001009 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001011 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return;
1013 }
NeilBrown128595e2010-05-03 14:47:14 +10001014 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 conf->raid_disks);
1016
1017 for (i = 0; i < conf->raid_disks; i++) {
1018 char b[BDEVNAME_SIZE];
1019 tmp = conf->mirrors + i;
1020 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001021 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001022 i, !test_bit(In_sync, &tmp->rdev->flags),
1023 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 bdevname(tmp->rdev->bdev,b));
1025 }
1026}
1027
1028static void close_sync(conf_t *conf)
1029{
NeilBrown0a27ec92006-01-06 00:20:13 -08001030 wait_barrier(conf);
1031 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 mempool_destroy(conf->r10buf_pool);
1034 conf->r10buf_pool = NULL;
1035}
1036
NeilBrown6d508242005-09-09 16:24:03 -07001037/* check if there are enough drives for
1038 * every block to appear on atleast one
1039 */
1040static int enough(conf_t *conf)
1041{
1042 int first = 0;
1043
1044 do {
1045 int n = conf->copies;
1046 int cnt = 0;
1047 while (n--) {
1048 if (conf->mirrors[first].rdev)
1049 cnt++;
1050 first = (first+1) % conf->raid_disks;
1051 }
1052 if (cnt == 0)
1053 return 0;
1054 } while (first != 0);
1055 return 1;
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058static int raid10_spare_active(mddev_t *mddev)
1059{
1060 int i;
1061 conf_t *conf = mddev->private;
1062 mirror_info_t *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001063 int count = 0;
1064 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /*
1067 * Find all non-in_sync disks within the RAID10 configuration
1068 * and mark them in_sync
1069 */
1070 for (i = 0; i < conf->raid_disks; i++) {
1071 tmp = conf->mirrors + i;
1072 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001073 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001074 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001075 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001076 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 }
NeilBrown6b965622010-08-18 11:56:59 +10001079 spin_lock_irqsave(&conf->device_lock, flags);
1080 mddev->degraded -= count;
1081 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001084 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087
1088static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1089{
1090 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001091 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int mirror;
1093 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001094 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001095 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (mddev->recovery_cp < MaxSector)
1098 /* only hot-add to in-sync arrays, as recovery is
1099 * very different from resync
1100 */
Neil Brown199050e2008-06-28 08:31:33 +10001101 return -EBUSY;
NeilBrown6d508242005-09-09 16:24:03 -07001102 if (!enough(conf))
Neil Brown199050e2008-06-28 08:31:33 +10001103 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrowna53a6c82008-11-06 17:28:20 +11001105 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001106 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001108 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001109 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1110 mirror = rdev->saved_raid_disk;
1111 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001112 mirror = first;
1113 for ( ; mirror <= last ; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if ( !(p=conf->mirrors+mirror)->rdev) {
1115
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001116 disk_stack_limits(mddev->gendisk, rdev->bdev,
1117 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001118 /* as we don't honour merge_bvec_fn, we must
1119 * never risk violating it, so limit
1120 * ->max_segments to one lying with a single
1121 * page, as a one page request is never in
1122 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
NeilBrown627a2d32010-03-08 16:44:38 +11001124 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1125 blk_queue_max_segments(mddev->queue, 1);
1126 blk_queue_segment_boundary(mddev->queue,
1127 PAGE_CACHE_SIZE - 1);
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 p->head_position = 0;
1131 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001132 err = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001133 if (rdev->saved_raid_disk != mirror)
1134 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001135 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
1137 }
1138
Andre Nollac5e7112009-08-03 10:59:47 +10001139 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144static int raid10_remove_disk(mddev_t *mddev, int number)
1145{
1146 conf_t *conf = mddev->private;
1147 int err = 0;
1148 mdk_rdev_t *rdev;
1149 mirror_info_t *p = conf->mirrors+ number;
1150
1151 print_conf(conf);
1152 rdev = p->rdev;
1153 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001154 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 atomic_read(&rdev->nr_pending)) {
1156 err = -EBUSY;
1157 goto abort;
1158 }
NeilBrowndfc70642008-05-23 13:04:39 -07001159 /* Only remove faulty devices in recovery
1160 * is not possible.
1161 */
1162 if (!test_bit(Faulty, &rdev->flags) &&
1163 enough(conf)) {
1164 err = -EBUSY;
1165 goto abort;
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001168 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (atomic_read(&rdev->nr_pending)) {
1170 /* lost the race, try later */
1171 err = -EBUSY;
1172 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001173 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001175 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177abort:
1178
1179 print_conf(conf);
1180 return err;
1181}
1182
1183
NeilBrown6712ecf2007-09-27 12:47:43 +02001184static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001186 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001187 conf_t *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001188 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Namhyung Kim778ca012011-07-18 17:38:47 +10001190 d = find_bio_disk(conf, r10_bio, bio);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001191
1192 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1193 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001194 else {
1195 atomic_add(r10_bio->sectors,
1196 &conf->mirrors[d].rdev->corrected_errors);
1197 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1198 md_error(r10_bio->mddev,
1199 conf->mirrors[d].rdev);
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 /* for reconstruct, we always reschedule after a read.
1203 * for resync, only after all reads
1204 */
NeilBrown73d5c382009-02-25 13:18:47 +11001205 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1207 atomic_dec_and_test(&r10_bio->remaining)) {
1208 /* we have read all the blocks,
1209 * do the comparison in process context in raid10d
1210 */
1211 reschedule_retry(r10_bio);
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
NeilBrown6712ecf2007-09-27 12:47:43 +02001215static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001218 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001220 conf_t *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001221 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Namhyung Kim778ca012011-07-18 17:38:47 +10001223 d = find_bio_disk(conf, r10_bio, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 if (!uptodate)
1226 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001227
NeilBrown73d5c382009-02-25 13:18:47 +11001228 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 while (atomic_dec_and_test(&r10_bio->remaining)) {
1230 if (r10_bio->master_bio == NULL) {
1231 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001232 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001234 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 } else {
1237 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1238 put_buf(r10_bio);
1239 r10_bio = r10_bio2;
1240 }
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244/*
1245 * Note: sync and recover and handled very differently for raid10
1246 * This code is for resync.
1247 * For resync, we read through virtual addresses and read all blocks.
1248 * If there is any error, we schedule a write. The lowest numbered
1249 * drive is authoritative.
1250 * However requests come for physical address, so we need to map.
1251 * For every physical address there are raid_disks/copies virtual addresses,
1252 * which is always are least one, but is not necessarly an integer.
1253 * This means that a physical address can span multiple chunks, so we may
1254 * have to submit multiple io requests for a single sync request.
1255 */
1256/*
1257 * We check if all blocks are in-sync and only write to blocks that
1258 * aren't in sync
1259 */
1260static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1261{
NeilBrown070ec552009-06-16 16:54:21 +10001262 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int i, first;
1264 struct bio *tbio, *fbio;
1265
1266 atomic_set(&r10_bio->remaining, 1);
1267
1268 /* find the first device with a block */
1269 for (i=0; i<conf->copies; i++)
1270 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1271 break;
1272
1273 if (i == conf->copies)
1274 goto done;
1275
1276 first = i;
1277 fbio = r10_bio->devs[i].bio;
1278
1279 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001280 for (i=0 ; i < conf->copies ; i++) {
1281 int j, d;
1282 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001285
1286 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001288 if (i == first)
1289 continue;
1290 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1291 /* We know that the bi_io_vec layout is the same for
1292 * both 'first' and 'i', so we just compare them.
1293 * All vec entries are PAGE_SIZE;
1294 */
1295 for (j = 0; j < vcnt; j++)
1296 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1297 page_address(tbio->bi_io_vec[j].bv_page),
1298 PAGE_SIZE))
1299 break;
1300 if (j == vcnt)
1301 continue;
1302 mddev->resync_mismatches += r10_bio->sectors;
1303 }
NeilBrown18f08812006-01-06 00:20:25 -08001304 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1305 /* Don't fix anything. */
1306 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /* Ok, we need to write this bio
1308 * First we need to fixup bv_offset, bv_len and
1309 * bi_vecs, as the read request might have corrupted these
1310 */
1311 tbio->bi_vcnt = vcnt;
1312 tbio->bi_size = r10_bio->sectors << 9;
1313 tbio->bi_idx = 0;
1314 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1316 tbio->bi_flags |= 1 << BIO_UPTODATE;
1317 tbio->bi_next = NULL;
1318 tbio->bi_rw = WRITE;
1319 tbio->bi_private = r10_bio;
1320 tbio->bi_sector = r10_bio->devs[i].addr;
1321
1322 for (j=0; j < vcnt ; j++) {
1323 tbio->bi_io_vec[j].bv_offset = 0;
1324 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1325
1326 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1327 page_address(fbio->bi_io_vec[j].bv_page),
1328 PAGE_SIZE);
1329 }
1330 tbio->bi_end_io = end_sync_write;
1331
1332 d = r10_bio->devs[i].devnum;
1333 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1334 atomic_inc(&r10_bio->remaining);
1335 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1336
1337 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1338 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1339 generic_make_request(tbio);
1340 }
1341
1342done:
1343 if (atomic_dec_and_test(&r10_bio->remaining)) {
1344 md_done_sync(mddev, r10_bio->sectors, 1);
1345 put_buf(r10_bio);
1346 }
1347}
1348
1349/*
1350 * Now for the recovery code.
1351 * Recovery happens across physical sectors.
1352 * We recover all non-is_sync drives by finding the virtual address of
1353 * each, and then choose a working drive that also has that virt address.
1354 * There is a separate r10_bio for each non-in_sync drive.
1355 * Only the first two slots are in use. The first for reading,
1356 * The second for writing.
1357 *
1358 */
1359
1360static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1361{
NeilBrown070ec552009-06-16 16:54:21 +10001362 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 int i, d;
1364 struct bio *bio, *wbio;
1365
1366
1367 /* move the pages across to the second bio
1368 * and submit the write request
1369 */
1370 bio = r10_bio->devs[0].bio;
1371 wbio = r10_bio->devs[1].bio;
1372 for (i=0; i < wbio->bi_vcnt; i++) {
1373 struct page *p = bio->bi_io_vec[i].bv_page;
1374 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1375 wbio->bi_io_vec[i].bv_page = p;
1376 }
1377 d = r10_bio->devs[1].devnum;
1378
1379 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1380 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001381 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1382 generic_make_request(wbio);
1383 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001384 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
1387
1388/*
Robert Becker1e509152009-12-14 12:49:58 +11001389 * Used by fix_read_error() to decay the per rdev read_errors.
1390 * We halve the read error count for every hour that has elapsed
1391 * since the last recorded read error.
1392 *
1393 */
1394static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1395{
1396 struct timespec cur_time_mon;
1397 unsigned long hours_since_last;
1398 unsigned int read_errors = atomic_read(&rdev->read_errors);
1399
1400 ktime_get_ts(&cur_time_mon);
1401
1402 if (rdev->last_read_error.tv_sec == 0 &&
1403 rdev->last_read_error.tv_nsec == 0) {
1404 /* first time we've seen a read error */
1405 rdev->last_read_error = cur_time_mon;
1406 return;
1407 }
1408
1409 hours_since_last = (cur_time_mon.tv_sec -
1410 rdev->last_read_error.tv_sec) / 3600;
1411
1412 rdev->last_read_error = cur_time_mon;
1413
1414 /*
1415 * if hours_since_last is > the number of bits in read_errors
1416 * just set read errors to 0. We do this to avoid
1417 * overflowing the shift of read_errors by hours_since_last.
1418 */
1419 if (hours_since_last >= 8 * sizeof(read_errors))
1420 atomic_set(&rdev->read_errors, 0);
1421 else
1422 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1423}
1424
1425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * This is a kernel thread which:
1427 *
1428 * 1. Retries failed read operations on working mirrors.
1429 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001430 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
1432
NeilBrown6814d532006-10-03 01:15:45 -07001433static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1434{
1435 int sect = 0; /* Offset from r10_bio->sector */
1436 int sectors = r10_bio->sectors;
1437 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001438 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001439 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001440
NeilBrown7c4e06f2011-05-11 14:53:17 +10001441 /* still own a reference to this rdev, so it cannot
1442 * have been cleared recently.
1443 */
1444 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001445
NeilBrown7c4e06f2011-05-11 14:53:17 +10001446 if (test_bit(Faulty, &rdev->flags))
1447 /* drive has already been failed, just ignore any
1448 more fix_read_error() attempts */
1449 return;
1450
1451 check_decay_read_errors(mddev, rdev);
1452 atomic_inc(&rdev->read_errors);
1453 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1454 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001455 bdevname(rdev->bdev, b);
1456
NeilBrown7c4e06f2011-05-11 14:53:17 +10001457 printk(KERN_NOTICE
1458 "md/raid10:%s: %s: Raid device exceeded "
1459 "read_error threshold [cur %d:max %d]\n",
1460 mdname(mddev), b,
1461 atomic_read(&rdev->read_errors), max_read_errors);
1462 printk(KERN_NOTICE
1463 "md/raid10:%s: %s: Failing raid device\n",
1464 mdname(mddev), b);
1465 md_error(mddev, conf->mirrors[d].rdev);
1466 return;
Robert Becker1e509152009-12-14 12:49:58 +11001467 }
Robert Becker1e509152009-12-14 12:49:58 +11001468
NeilBrown6814d532006-10-03 01:15:45 -07001469 while(sectors) {
1470 int s = sectors;
1471 int sl = r10_bio->read_slot;
1472 int success = 0;
1473 int start;
1474
1475 if (s > (PAGE_SIZE>>9))
1476 s = PAGE_SIZE >> 9;
1477
1478 rcu_read_lock();
1479 do {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001480 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001481 rdev = rcu_dereference(conf->mirrors[d].rdev);
1482 if (rdev &&
1483 test_bit(In_sync, &rdev->flags)) {
1484 atomic_inc(&rdev->nr_pending);
1485 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001486 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001487 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001488 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001489 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001490 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001491 rdev_dec_pending(rdev, mddev);
1492 rcu_read_lock();
1493 if (success)
1494 break;
1495 }
1496 sl++;
1497 if (sl == conf->copies)
1498 sl = 0;
1499 } while (!success && sl != r10_bio->read_slot);
1500 rcu_read_unlock();
1501
1502 if (!success) {
1503 /* Cannot read from anywhere -- bye bye array */
1504 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1505 md_error(mddev, conf->mirrors[dn].rdev);
1506 break;
1507 }
1508
1509 start = sl;
1510 /* write it back and re-read */
1511 rcu_read_lock();
1512 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001513 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001514
NeilBrown6814d532006-10-03 01:15:45 -07001515 if (sl==0)
1516 sl = conf->copies;
1517 sl--;
1518 d = r10_bio->devs[sl].devnum;
1519 rdev = rcu_dereference(conf->mirrors[d].rdev);
1520 if (rdev &&
1521 test_bit(In_sync, &rdev->flags)) {
1522 atomic_inc(&rdev->nr_pending);
1523 rcu_read_unlock();
1524 atomic_add(s, &rdev->corrected_errors);
NeilBrown2b193362010-10-27 15:16:40 +11001525 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001526 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001527 sect,
1528 s<<9, conf->tmppage, WRITE, false)
Robert Becker67b8dc42009-12-14 12:49:57 +11001529 == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001530 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001531 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001532 "md/raid10:%s: read correction "
Robert Becker67b8dc42009-12-14 12:49:57 +11001533 "write failed"
1534 " (%d sectors at %llu on %s)\n",
1535 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001536 (unsigned long long)(
1537 sect + rdev->data_offset),
Robert Becker67b8dc42009-12-14 12:49:57 +11001538 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001539 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
Robert Becker67b8dc42009-12-14 12:49:57 +11001540 "drive\n",
NeilBrown128595e2010-05-03 14:47:14 +10001541 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001542 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001543 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001544 }
NeilBrown6814d532006-10-03 01:15:45 -07001545 rdev_dec_pending(rdev, mddev);
1546 rcu_read_lock();
1547 }
1548 }
1549 sl = start;
1550 while (sl != r10_bio->read_slot) {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001551
NeilBrown6814d532006-10-03 01:15:45 -07001552 if (sl==0)
1553 sl = conf->copies;
1554 sl--;
1555 d = r10_bio->devs[sl].devnum;
1556 rdev = rcu_dereference(conf->mirrors[d].rdev);
1557 if (rdev &&
1558 test_bit(In_sync, &rdev->flags)) {
1559 char b[BDEVNAME_SIZE];
1560 atomic_inc(&rdev->nr_pending);
1561 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001562 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001563 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001564 sect,
Robert Becker67b8dc42009-12-14 12:49:57 +11001565 s<<9, conf->tmppage,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001566 READ, false) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001567 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001568 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001569 "md/raid10:%s: unable to read back "
Robert Becker67b8dc42009-12-14 12:49:57 +11001570 "corrected sectors"
1571 " (%d sectors at %llu on %s)\n",
1572 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001573 (unsigned long long)(
1574 sect + rdev->data_offset),
Robert Becker67b8dc42009-12-14 12:49:57 +11001575 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001576 printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
1577 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001578 bdevname(rdev->bdev, b));
1579
NeilBrown6814d532006-10-03 01:15:45 -07001580 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001581 } else {
NeilBrown6814d532006-10-03 01:15:45 -07001582 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10001583 "md/raid10:%s: read error corrected"
NeilBrown6814d532006-10-03 01:15:45 -07001584 " (%d sectors at %llu on %s)\n",
1585 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001586 (unsigned long long)(
1587 sect + rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001588 bdevname(rdev->bdev, b));
Robert Becker67b8dc42009-12-14 12:49:57 +11001589 }
NeilBrown6814d532006-10-03 01:15:45 -07001590
1591 rdev_dec_pending(rdev, mddev);
1592 rcu_read_lock();
1593 }
1594 }
1595 rcu_read_unlock();
1596
1597 sectors -= s;
1598 sect += s;
1599 }
1600}
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static void raid10d(mddev_t *mddev)
1603{
1604 r10bio_t *r10_bio;
1605 struct bio *bio;
1606 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001607 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 struct list_head *head = &conf->retry_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 mdk_rdev_t *rdev;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001610 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001614 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 for (;;) {
1616 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001617
Jens Axboe7eaceac2011-03-10 08:52:07 +01001618 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001621 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001622 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1626 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001627 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 spin_unlock_irqrestore(&conf->device_lock, flags);
1629
1630 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001631 conf = mddev->private;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001632 if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001634 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 recovery_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001636 else {
NeilBrown7c4e06f2011-05-11 14:53:17 +10001637 int slot = r10_bio->read_slot;
1638 int mirror = r10_bio->devs[slot].devnum;
NeilBrown4443ae12006-01-06 00:20:28 -08001639 /* we got a read error. Maybe the drive is bad. Maybe just
1640 * the block and we can fix it.
1641 * We freeze all other IO, and try reading the block from
1642 * other devices. When we find one, we re-write
1643 * and check it that fixes the read error.
1644 * This is all done synchronously while the array is
1645 * frozen.
1646 */
NeilBrown6814d532006-10-03 01:15:45 -07001647 if (mddev->ro == 0) {
1648 freeze_array(conf);
1649 fix_read_error(conf, mddev, r10_bio);
1650 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001651 }
NeilBrown7c4e06f2011-05-11 14:53:17 +10001652 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
NeilBrown4443ae12006-01-06 00:20:28 -08001653
NeilBrowna8830bca2011-05-11 14:54:19 +10001654 bio = r10_bio->devs[slot].bio;
1655 r10_bio->devs[slot].bio =
NeilBrown0eb3ff12006-01-06 00:20:29 -08001656 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 mirror = read_balance(conf, r10_bio);
1658 if (mirror == -1) {
NeilBrown128595e2010-05-03 14:47:14 +10001659 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 " read error for block %llu\n",
NeilBrown128595e2010-05-03 14:47:14 +10001661 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 bdevname(bio->bi_bdev,b),
1663 (unsigned long long)r10_bio->sector);
1664 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001665 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 } else {
NeilBrown2c7d46e2010-08-18 16:16:05 +10001667 const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
Maik Hampel14e71342007-07-31 00:37:57 -07001668 bio_put(bio);
NeilBrowna8830bca2011-05-11 14:54:19 +10001669 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 rdev = conf->mirrors[mirror].rdev;
1671 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +10001672 printk(KERN_ERR "md/raid10:%s: %s: redirecting sector %llu to"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 " another mirror\n",
NeilBrown128595e2010-05-03 14:47:14 +10001674 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 bdevname(rdev->bdev,b),
1676 (unsigned long long)r10_bio->sector);
NeilBrowna167f662010-10-26 18:31:13 +11001677 bio = bio_clone_mddev(r10_bio->master_bio,
1678 GFP_NOIO, mddev);
NeilBrowna8830bca2011-05-11 14:54:19 +10001679 r10_bio->devs[slot].bio = bio;
1680 bio->bi_sector = r10_bio->devs[slot].addr
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 + rdev->data_offset;
1682 bio->bi_bdev = rdev->bdev;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001683 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 bio->bi_private = r10_bio;
1685 bio->bi_end_io = raid10_end_read_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 generic_make_request(bio);
1687 }
1688 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001689 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001691 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694
1695static int init_resync(conf_t *conf)
1696{
1697 int buffs;
1698
1699 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001700 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1702 if (!conf->r10buf_pool)
1703 return -ENOMEM;
1704 conf->next_resync = 0;
1705 return 0;
1706}
1707
1708/*
1709 * perform a "sync" on one "block"
1710 *
1711 * We need to make sure that no normal I/O request - particularly write
1712 * requests - conflict with active sync requests.
1713 *
1714 * This is achieved by tracking pending requests and a 'barrier' concept
1715 * that can be installed to exclude normal IO requests.
1716 *
1717 * Resync and recovery are handled very differently.
1718 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1719 *
1720 * For resync, we iterate over virtual addresses, read all copies,
1721 * and update if there are differences. If only one copy is live,
1722 * skip it.
1723 * For recovery, we iterate over physical addresses, read a good
1724 * value for each non-in_sync drive, and over-write.
1725 *
1726 * So, for recovery we may have several outstanding complex requests for a
1727 * given address, one for each out-of-sync device. We model this by allocating
1728 * a number of r10_bio structures, one for each out-of-sync device.
1729 * As we setup these structures, we collect all bio's together into a list
1730 * which we then process collectively to add pages, and then process again
1731 * to pass to generic_make_request.
1732 *
1733 * The r10_bio structures are linked using a borrowed master_bio pointer.
1734 * This link is counted in ->remaining. When the r10_bio that points to NULL
1735 * has its remaining count decremented to 0, the whole complex operation
1736 * is complete.
1737 *
1738 */
1739
NeilBrownab9d47e2011-05-11 14:54:41 +10001740static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
1741 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
NeilBrown070ec552009-06-16 16:54:21 +10001743 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 r10bio_t *r10_bio;
1745 struct bio *biolist = NULL, *bio;
1746 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001748 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11001749 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 sector_t sectors_skipped = 0;
1752 int chunks_skipped = 0;
1753
1754 if (!conf->r10buf_pool)
1755 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001759 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1761 max_sector = mddev->resync_max_sectors;
1762 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001763 /* If we aborted, we need to abort the
1764 * sync on the 'current' bitmap chucks (there can
1765 * be several when recovering multiple devices).
1766 * as we may have started syncing it but not finished.
1767 * We can find the current address in
1768 * mddev->curr_resync, but for recovery,
1769 * we need to convert that to several
1770 * virtual addresses.
1771 */
1772 if (mddev->curr_resync < max_sector) { /* aborted */
1773 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1774 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1775 &sync_blocks, 1);
1776 else for (i=0; i<conf->raid_disks; i++) {
1777 sector_t sect =
1778 raid10_find_virt(conf, mddev->curr_resync, i);
1779 bitmap_end_sync(mddev->bitmap, sect,
1780 &sync_blocks, 1);
1781 }
1782 } else /* completed sync */
1783 conf->fullsync = 0;
1784
1785 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001787 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return sectors_skipped;
1789 }
1790 if (chunks_skipped >= conf->raid_disks) {
1791 /* if there has been nothing to do on any drive,
1792 * then there is nothing to do at all..
1793 */
NeilBrown57afd892005-06-21 17:17:13 -07001794 *skipped = 1;
1795 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
NeilBrownc6207272008-02-06 01:39:52 -08001798 if (max_sector > mddev->resync_max)
1799 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* make sure whole request will fit in a chunk - if chunks
1802 * are meaningful
1803 */
1804 if (conf->near_copies < conf->raid_disks &&
1805 max_sector > (sector_nr | conf->chunk_mask))
1806 max_sector = (sector_nr | conf->chunk_mask) + 1;
1807 /*
1808 * If there is non-resync activity waiting for us then
1809 * put in a delay to throttle resync.
1810 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001811 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 /* Again, very different code for resync and recovery.
1815 * Both must result in an r10bio with a list of bios that
1816 * have bi_end_io, bi_sector, bi_bdev set,
1817 * and bi_private set to the r10bio.
1818 * For recovery, we may actually create several r10bios
1819 * with 2 bios in each, that correspond to the bios in the main one.
1820 * In this case, the subordinate r10bios link back through a
1821 * borrowed master_bio pointer, and the counter in the master
1822 * includes a ref from each subordinate.
1823 */
1824 /* First, we decide what to do and set ->bi_end_io
1825 * To end_sync_read if we want to read, and
1826 * end_sync_write if we will want to write.
1827 */
1828
NeilBrown6cce3b22006-01-06 00:20:16 -08001829 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1831 /* recovery... the complicated one */
NeilBrowna9f326e2009-09-23 18:06:41 +10001832 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 r10_bio = NULL;
1834
NeilBrownab9d47e2011-05-11 14:54:41 +10001835 for (i=0 ; i<conf->raid_disks; i++) {
1836 int still_degraded;
1837 r10bio_t *rb2;
1838 sector_t sect;
1839 int must_sync;
1840
1841 if (conf->mirrors[i].rdev == NULL ||
1842 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
1843 continue;
1844
1845 still_degraded = 0;
1846 /* want to reconstruct this device */
1847 rb2 = r10_bio;
1848 sect = raid10_find_virt(conf, sector_nr, i);
1849 /* Unless we are doing a full sync, we only need
1850 * to recover the block if it is set in the bitmap
1851 */
1852 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1853 &sync_blocks, 1);
1854 if (sync_blocks < max_sync)
1855 max_sync = sync_blocks;
1856 if (!must_sync &&
1857 !conf->fullsync) {
1858 /* yep, skip the sync_blocks here, but don't assume
1859 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08001860 */
NeilBrownab9d47e2011-05-11 14:54:41 +10001861 chunks_skipped = -1;
1862 continue;
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
NeilBrownab9d47e2011-05-11 14:54:41 +10001865 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1866 raise_barrier(conf, rb2 != NULL);
1867 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
NeilBrownab9d47e2011-05-11 14:54:41 +10001869 r10_bio->master_bio = (struct bio*)rb2;
1870 if (rb2)
1871 atomic_inc(&rb2->remaining);
1872 r10_bio->mddev = mddev;
1873 set_bit(R10BIO_IsRecover, &r10_bio->state);
1874 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08001875
NeilBrownab9d47e2011-05-11 14:54:41 +10001876 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001877
NeilBrownab9d47e2011-05-11 14:54:41 +10001878 /* Need to check if the array will still be
1879 * degraded
1880 */
1881 for (j=0; j<conf->raid_disks; j++)
1882 if (conf->mirrors[j].rdev == NULL ||
1883 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
1884 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07001885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
NeilBrownab9d47e2011-05-11 14:54:41 +10001887
1888 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1889 &sync_blocks, still_degraded);
1890
1891 for (j=0; j<conf->copies;j++) {
1892 int d = r10_bio->devs[j].devnum;
1893 if (!conf->mirrors[d].rdev ||
1894 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
1895 continue;
1896 /* This is where we read from */
1897 bio = r10_bio->devs[0].bio;
1898 bio->bi_next = biolist;
1899 biolist = bio;
1900 bio->bi_private = r10_bio;
1901 bio->bi_end_io = end_sync_read;
1902 bio->bi_rw = READ;
1903 bio->bi_sector = r10_bio->devs[j].addr +
1904 conf->mirrors[d].rdev->data_offset;
1905 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1906 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1907 atomic_inc(&r10_bio->remaining);
1908 /* and we write to 'i' */
1909
1910 for (k=0; k<conf->copies; k++)
1911 if (r10_bio->devs[k].devnum == i)
1912 break;
1913 BUG_ON(k == conf->copies);
1914 bio = r10_bio->devs[1].bio;
1915 bio->bi_next = biolist;
1916 biolist = bio;
1917 bio->bi_private = r10_bio;
1918 bio->bi_end_io = end_sync_write;
1919 bio->bi_rw = WRITE;
1920 bio->bi_sector = r10_bio->devs[k].addr +
1921 conf->mirrors[i].rdev->data_offset;
1922 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1923
1924 r10_bio->devs[0].devnum = d;
1925 r10_bio->devs[1].devnum = i;
1926
1927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
NeilBrownab9d47e2011-05-11 14:54:41 +10001929 if (j == conf->copies) {
1930 /* Cannot recover, so abort the recovery */
1931 put_buf(r10_bio);
1932 if (rb2)
1933 atomic_dec(&rb2->remaining);
1934 r10_bio = rb2;
1935 if (!test_and_set_bit(MD_RECOVERY_INTR,
1936 &mddev->recovery))
1937 printk(KERN_INFO "md/raid10:%s: insufficient "
1938 "working devices for recovery.\n",
1939 mdname(mddev));
1940 break;
1941 }
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (biolist == NULL) {
1944 while (r10_bio) {
1945 r10bio_t *rb2 = r10_bio;
1946 r10_bio = (r10bio_t*) rb2->master_bio;
1947 rb2->master_bio = NULL;
1948 put_buf(rb2);
1949 }
1950 goto giveup;
1951 }
1952 } else {
1953 /* resync. Schedule a read for every block at this virt offset */
1954 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001955
NeilBrown78200d42009-02-25 13:18:47 +11001956 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1957
NeilBrown6cce3b22006-01-06 00:20:16 -08001958 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1959 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10001960 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
1961 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001962 /* We can skip this block */
1963 *skipped = 1;
1964 return sync_blocks + sectors_skipped;
1965 }
1966 if (sync_blocks < max_sync)
1967 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 r10_bio->mddev = mddev;
1971 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001972 raise_barrier(conf, 0);
1973 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 r10_bio->master_bio = NULL;
1976 r10_bio->sector = sector_nr;
1977 set_bit(R10BIO_IsSync, &r10_bio->state);
1978 raid10_find_phys(conf, r10_bio);
1979 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1980
1981 for (i=0; i<conf->copies; i++) {
1982 int d = r10_bio->devs[i].devnum;
1983 bio = r10_bio->devs[i].bio;
1984 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001985 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001987 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 continue;
1989 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1990 atomic_inc(&r10_bio->remaining);
1991 bio->bi_next = biolist;
1992 biolist = bio;
1993 bio->bi_private = r10_bio;
1994 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001995 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 bio->bi_sector = r10_bio->devs[i].addr +
1997 conf->mirrors[d].rdev->data_offset;
1998 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1999 count++;
2000 }
2001
2002 if (count < 2) {
2003 for (i=0; i<conf->copies; i++) {
2004 int d = r10_bio->devs[i].devnum;
2005 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002006 rdev_dec_pending(conf->mirrors[d].rdev,
2007 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 put_buf(r10_bio);
2010 biolist = NULL;
2011 goto giveup;
2012 }
2013 }
2014
2015 for (bio = biolist; bio ; bio=bio->bi_next) {
2016
2017 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2018 if (bio->bi_end_io)
2019 bio->bi_flags |= 1 << BIO_UPTODATE;
2020 bio->bi_vcnt = 0;
2021 bio->bi_idx = 0;
2022 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 bio->bi_size = 0;
2024 }
2025
2026 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002027 if (sector_nr + max_sync < max_sector)
2028 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 do {
2030 struct page *page;
2031 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (sector_nr + (len>>9) > max_sector)
2033 len = (max_sector - sector_nr) << 9;
2034 if (len == 0)
2035 break;
2036 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002037 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002039 if (bio_add_page(bio, page, len, 0))
2040 continue;
2041
2042 /* stop here */
2043 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2044 for (bio2 = biolist;
2045 bio2 && bio2 != bio;
2046 bio2 = bio2->bi_next) {
2047 /* remove last page from this bio */
2048 bio2->bi_vcnt--;
2049 bio2->bi_size -= len;
2050 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002052 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054 nr_sectors += len>>9;
2055 sector_nr += len>>9;
2056 } while (biolist->bi_vcnt < RESYNC_PAGES);
2057 bio_full:
2058 r10_bio->sectors = nr_sectors;
2059
2060 while (biolist) {
2061 bio = biolist;
2062 biolist = biolist->bi_next;
2063
2064 bio->bi_next = NULL;
2065 r10_bio = bio->bi_private;
2066 r10_bio->sectors = nr_sectors;
2067
2068 if (bio->bi_end_io == end_sync_read) {
2069 md_sync_acct(bio->bi_bdev, nr_sectors);
2070 generic_make_request(bio);
2071 }
2072 }
2073
NeilBrown57afd892005-06-21 17:17:13 -07002074 if (sectors_skipped)
2075 /* pretend they weren't skipped, it makes
2076 * no important difference in this case
2077 */
2078 md_done_sync(mddev, sectors_skipped, 1);
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return sectors_skipped + nr_sectors;
2081 giveup:
2082 /* There is nowhere to write, so all non-sync
2083 * drives must be failed, so try the next chunk...
2084 */
NeilBrown09b40682009-02-25 13:18:47 +11002085 if (sector_nr + max_sync < max_sector)
2086 max_sector = sector_nr + max_sync;
2087
2088 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 chunks_skipped ++;
2090 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Dan Williams80c3a6c2009-03-17 18:10:40 -07002094static sector_t
2095raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2096{
2097 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002098 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002099
2100 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002101 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002102 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002103 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002104
2105 size = sectors >> conf->chunk_shift;
2106 sector_div(size, conf->far_copies);
2107 size = size * raid_disks;
2108 sector_div(size, conf->near_copies);
2109
2110 return size << conf->chunk_shift;
2111}
2112
Trela, Maciejdab8b292010-03-08 16:02:45 +11002113
2114static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002116 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002117 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002119 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Maciej Trelaf73ea872010-06-16 11:46:29 +01002121 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2122 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002123 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2124 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2125 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
NeilBrown2604b702006-01-06 00:20:36 -08002128
Maciej Trelaf73ea872010-06-16 11:46:29 +01002129 nc = mddev->new_layout & 255;
2130 fc = (mddev->new_layout >> 8) & 255;
2131 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002134 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002135 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002136 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 goto out;
2138 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002139
2140 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002141 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002142 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002144
NeilBrown4443ae12006-01-06 00:20:28 -08002145 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002146 GFP_KERNEL);
2147 if (!conf->mirrors)
2148 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002149
2150 conf->tmppage = alloc_page(GFP_KERNEL);
2151 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002152 goto out;
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
NeilBrown64a742b2007-02-28 20:11:18 -08002155 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 conf->near_copies = nc;
2157 conf->far_copies = fc;
2158 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002159 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002160 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2161 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2162
2163 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2164 r10bio_pool_free, conf);
2165 if (!conf->r10bio_pool)
2166 goto out;
2167
Andre Noll58c0fed2009-03-31 14:33:13 +11002168 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002169 sector_div(size, fc);
2170 size = size * conf->raid_disks;
2171 sector_div(size, nc);
2172 /* 'size' is now the number of chunks in the array */
2173 /* calculate "used chunks per device" in 'stride' */
2174 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002175
2176 /* We need to round up when dividing by raid_disks to
2177 * get the stride size.
2178 */
2179 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002180 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002181
2182 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002183
NeilBrownc93983b2006-06-26 00:27:41 -07002184 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002185 stride = 1;
2186 else
NeilBrownc93983b2006-06-26 00:27:41 -07002187 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002188 conf->stride = stride << conf->chunk_shift;
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Neil Browne7e72bf2008-05-14 16:05:54 -07002191 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002192 INIT_LIST_HEAD(&conf->retry_list);
2193
2194 spin_lock_init(&conf->resync_lock);
2195 init_waitqueue_head(&conf->wait_barrier);
2196
2197 conf->thread = md_register_thread(raid10d, mddev, NULL);
2198 if (!conf->thread)
2199 goto out;
2200
Trela, Maciejdab8b292010-03-08 16:02:45 +11002201 conf->mddev = mddev;
2202 return conf;
2203
2204 out:
NeilBrown128595e2010-05-03 14:47:14 +10002205 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002206 mdname(mddev));
2207 if (conf) {
2208 if (conf->r10bio_pool)
2209 mempool_destroy(conf->r10bio_pool);
2210 kfree(conf->mirrors);
2211 safe_put_page(conf->tmppage);
2212 kfree(conf);
2213 }
2214 return ERR_PTR(err);
2215}
2216
2217static int run(mddev_t *mddev)
2218{
2219 conf_t *conf;
2220 int i, disk_idx, chunk_size;
2221 mirror_info_t *disk;
2222 mdk_rdev_t *rdev;
2223 sector_t size;
2224
2225 /*
2226 * copy the already verified devices into our private RAID10
2227 * bookkeeping area. [whatever we allocate in run(),
2228 * should be freed in stop()]
2229 */
2230
2231 if (mddev->private == NULL) {
2232 conf = setup_conf(mddev);
2233 if (IS_ERR(conf))
2234 return PTR_ERR(conf);
2235 mddev->private = conf;
2236 }
2237 conf = mddev->private;
2238 if (!conf)
2239 goto out;
2240
Trela, Maciejdab8b292010-03-08 16:02:45 +11002241 mddev->thread = conf->thread;
2242 conf->thread = NULL;
2243
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002244 chunk_size = mddev->chunk_sectors << 9;
2245 blk_queue_io_min(mddev->queue, chunk_size);
2246 if (conf->raid_disks % conf->near_copies)
2247 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2248 else
2249 blk_queue_io_opt(mddev->queue, chunk_size *
2250 (conf->raid_disks / conf->near_copies));
2251
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002252 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002254 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 || disk_idx < 0)
2256 continue;
2257 disk = conf->mirrors + disk_idx;
2258
2259 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002260 disk_stack_limits(mddev->gendisk, rdev->bdev,
2261 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002263 * violating it, so limit max_segments to 1 lying
2264 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 */
NeilBrown627a2d32010-03-08 16:44:38 +11002266 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2267 blk_queue_max_segments(mddev->queue, 1);
2268 blk_queue_segment_boundary(mddev->queue,
2269 PAGE_CACHE_SIZE - 1);
2270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
NeilBrown6d508242005-09-09 16:24:03 -07002274 /* need to check that every block has at least one working mirror */
2275 if (!enough(conf)) {
NeilBrown128595e2010-05-03 14:47:14 +10002276 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002277 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto out_free_conf;
2279 }
2280
2281 mddev->degraded = 0;
2282 for (i = 0; i < conf->raid_disks; i++) {
2283
2284 disk = conf->mirrors + i;
2285
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002286 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002287 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 disk->head_position = 0;
2289 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002290 if (disk->rdev)
2291 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293 }
2294
Andre Noll8c6ac862009-06-18 08:48:06 +10002295 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002296 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002297 " -- starting background reconstruction\n",
2298 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002300 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002301 mdname(mddev), conf->raid_disks - mddev->degraded,
2302 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /*
2304 * Ok, everything is just fine now
2305 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002306 mddev->dev_sectors = conf->dev_sectors;
2307 size = raid10_size(mddev, 0, 0);
2308 md_set_array_sectors(mddev, size);
2309 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
NeilBrown0d129222006-10-03 01:15:54 -07002311 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2312 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* Calculate max read-ahead size.
2315 * We need to readahead at least twice a whole stripe....
2316 * maybe...
2317 */
2318 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002319 int stripe = conf->raid_disks *
2320 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 stripe /= conf->near_copies;
2322 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2323 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2324 }
2325
NeilBrown84707f32010-03-16 17:23:35 +11002326 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002328
2329 if (md_integrity_register(mddev))
2330 goto out_free_conf;
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return 0;
2333
2334out_free_conf:
NeilBrown589a5942010-12-09 17:02:14 +11002335 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (conf->r10bio_pool)
2337 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002338 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002339 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 kfree(conf);
2341 mddev->private = NULL;
2342out:
2343 return -EIO;
2344}
2345
2346static int stop(mddev_t *mddev)
2347{
NeilBrown070ec552009-06-16 16:54:21 +10002348 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
NeilBrown409c57f2009-03-31 14:39:39 +11002350 raise_barrier(conf, 0);
2351 lower_barrier(conf);
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 md_unregister_thread(mddev->thread);
2354 mddev->thread = NULL;
2355 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2356 if (conf->r10bio_pool)
2357 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002358 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 kfree(conf);
2360 mddev->private = NULL;
2361 return 0;
2362}
2363
NeilBrown6cce3b22006-01-06 00:20:16 -08002364static void raid10_quiesce(mddev_t *mddev, int state)
2365{
NeilBrown070ec552009-06-16 16:54:21 +10002366 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002367
2368 switch(state) {
2369 case 1:
2370 raise_barrier(conf, 0);
2371 break;
2372 case 0:
2373 lower_barrier(conf);
2374 break;
2375 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Trela, Maciejdab8b292010-03-08 16:02:45 +11002378static void *raid10_takeover_raid0(mddev_t *mddev)
2379{
2380 mdk_rdev_t *rdev;
2381 conf_t *conf;
2382
2383 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10002384 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2385 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002386 return ERR_PTR(-EINVAL);
2387 }
2388
Trela, Maciejdab8b292010-03-08 16:02:45 +11002389 /* Set new parameters */
2390 mddev->new_level = 10;
2391 /* new layout: far_copies = 1, near_copies = 2 */
2392 mddev->new_layout = (1<<8) + 2;
2393 mddev->new_chunk_sectors = mddev->chunk_sectors;
2394 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002395 mddev->raid_disks *= 2;
2396 /* make sure it will be not marked as dirty */
2397 mddev->recovery_cp = MaxSector;
2398
2399 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002400 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01002401 list_for_each_entry(rdev, &mddev->disks, same_set)
2402 if (rdev->raid_disk >= 0)
2403 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002404 conf->barrier = 1;
2405 }
2406
Trela, Maciejdab8b292010-03-08 16:02:45 +11002407 return conf;
2408}
2409
2410static void *raid10_takeover(mddev_t *mddev)
2411{
2412 struct raid0_private_data *raid0_priv;
2413
2414 /* raid10 can take over:
2415 * raid0 - providing it has only two drives
2416 */
2417 if (mddev->level == 0) {
2418 /* for raid0 takeover only one zone is supported */
2419 raid0_priv = mddev->private;
2420 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10002421 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2422 " with more than one zone.\n",
2423 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002424 return ERR_PTR(-EINVAL);
2425 }
2426 return raid10_takeover_raid0(mddev);
2427 }
2428 return ERR_PTR(-EINVAL);
2429}
2430
NeilBrown2604b702006-01-06 00:20:36 -08002431static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002434 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 .owner = THIS_MODULE,
2436 .make_request = make_request,
2437 .run = run,
2438 .stop = stop,
2439 .status = status,
2440 .error_handler = error,
2441 .hot_add_disk = raid10_add_disk,
2442 .hot_remove_disk= raid10_remove_disk,
2443 .spare_active = raid10_spare_active,
2444 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002445 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002446 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002447 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448};
2449
2450static int __init raid_init(void)
2451{
NeilBrown2604b702006-01-06 00:20:36 -08002452 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
2455static void raid_exit(void)
2456{
NeilBrown2604b702006-01-06 00:20:36 -08002457 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
2460module_init(raid_init);
2461module_exit(raid_exit);
2462MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002463MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002465MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002466MODULE_ALIAS("md-level-10");