blob: d55ae12b2d8c70e38e692bf4fe7cbe9bf861084b [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
NeilBrown6712ecf2007-09-27 12:47:43 +0200247static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100250 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000252 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 slot = r10_bio->read_slot;
256 dev = r10_bio->devs[slot].devnum;
257 /*
258 * this branch is our 'one mirror IO has finished' event handler:
259 */
NeilBrown4443ae12006-01-06 00:20:28 -0800260 update_head_pos(slot, r10_bio);
261
262 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /*
264 * Set R10BIO_Uptodate in our master bio, so that
265 * we will return a good error code to the higher
266 * levels even if IO on some other mirrored buffer fails.
267 *
268 * The 'master' represents the composite IO operation to
269 * user-side. So if something waits for IO, then it will
270 * wait for the 'master' bio.
271 */
272 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 raid_end_bio_io(r10_bio);
NeilBrown7c4e06f2011-05-11 14:53:17 +1000274 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800275 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000277 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279 char b[BDEVNAME_SIZE];
280 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +1000281 printk(KERN_ERR "md/raid10:%s: %s: rescheduling sector %llu\n",
282 mdname(conf->mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
284 reschedule_retry(r10_bio);
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
NeilBrown6712ecf2007-09-27 12:47:43 +0200288static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100291 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000293 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 for (slot = 0; slot < conf->copies; slot++)
296 if (r10_bio->devs[slot].bio == bio)
297 break;
298 dev = r10_bio->devs[slot].devnum;
299
300 /*
301 * this branch is our 'one mirror IO has finished' event handler:
302 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800303 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800305 /* an I/O failed, we can't clear the bitmap */
306 set_bit(R10BIO_Degraded, &r10_bio->state);
307 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /*
309 * Set R10BIO_Uptodate in our master bio, so that
310 * we will return a good error code for to the higher
311 * levels even if IO on some other mirrored buffer fails.
312 *
313 * The 'master' represents the composite IO operation to
314 * user-side. So if something waits for IO, then it will
315 * wait for the 'master' bio.
316 */
317 set_bit(R10BIO_Uptodate, &r10_bio->state);
318
319 update_head_pos(slot, r10_bio);
320
321 /*
322 *
323 * Let's see if all mirrored write operations have finished
324 * already.
325 */
326 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800327 /* clear the bitmap if all writes complete successfully */
328 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
329 r10_bio->sectors,
330 !test_bit(R10BIO_Degraded, &r10_bio->state),
331 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 md_write_end(r10_bio->mddev);
333 raid_end_bio_io(r10_bio);
334 }
335
336 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339
340/*
341 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300342 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * parameters: near_copies and far_copies.
344 * near_copies * far_copies must be <= raid_disks.
345 * Normally one of these will be 1.
346 * If both are 1, we get raid0.
347 * If near_copies == raid_disks, we get raid1.
348 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300349 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * first chunk, followed by near_copies copies of the next chunk and
351 * so on.
352 * If far_copies > 1, then after 1/far_copies of the array has been assigned
353 * as described above, we start again with a device offset of near_copies.
354 * So we effectively have another copy of the whole array further down all
355 * the drives, but with blocks on different drives.
356 * With this layout, and block is never stored twice on the one device.
357 *
358 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700359 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
361 * raid10_find_virt does the reverse mapping, from a device and a
362 * sector offset to a virtual address
363 */
364
365static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
366{
367 int n,f;
368 sector_t sector;
369 sector_t chunk;
370 sector_t stripe;
371 int dev;
372
373 int slot = 0;
374
375 /* now calculate first sector/dev */
376 chunk = r10bio->sector >> conf->chunk_shift;
377 sector = r10bio->sector & conf->chunk_mask;
378
379 chunk *= conf->near_copies;
380 stripe = chunk;
381 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700382 if (conf->far_offset)
383 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 sector += stripe << conf->chunk_shift;
386
387 /* and calculate all the others */
388 for (n=0; n < conf->near_copies; n++) {
389 int d = dev;
390 sector_t s = sector;
391 r10bio->devs[slot].addr = sector;
392 r10bio->devs[slot].devnum = d;
393 slot++;
394
395 for (f = 1; f < conf->far_copies; f++) {
396 d += conf->near_copies;
397 if (d >= conf->raid_disks)
398 d -= conf->raid_disks;
399 s += conf->stride;
400 r10bio->devs[slot].devnum = d;
401 r10bio->devs[slot].addr = s;
402 slot++;
403 }
404 dev++;
405 if (dev >= conf->raid_disks) {
406 dev = 0;
407 sector += (conf->chunk_mask + 1);
408 }
409 }
410 BUG_ON(slot != conf->copies);
411}
412
413static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
414{
415 sector_t offset, chunk, vchunk;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700418 if (conf->far_offset) {
419 int fc;
420 chunk = sector >> conf->chunk_shift;
421 fc = sector_div(chunk, conf->far_copies);
422 dev -= fc * conf->near_copies;
423 if (dev < 0)
424 dev += conf->raid_disks;
425 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800426 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700427 sector -= conf->stride;
428 if (dev < conf->near_copies)
429 dev += conf->raid_disks - conf->near_copies;
430 else
431 dev -= conf->near_copies;
432 }
433 chunk = sector >> conf->chunk_shift;
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 vchunk = chunk * conf->raid_disks + dev;
436 sector_div(vchunk, conf->near_copies);
437 return (vchunk << conf->chunk_shift) + offset;
438}
439
440/**
441 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
442 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200443 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * @biovec: the request that could be merged to it.
445 *
446 * Return amount of bytes we can accept at this offset
447 * If near_copies == raid_disk, there are no striping issues,
448 * but in that case, the function isn't called at all.
449 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200450static int raid10_mergeable_bvec(struct request_queue *q,
451 struct bvec_merge_data *bvm,
452 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200455 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000457 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200458 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
461 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200462 if (max <= biovec->bv_len && bio_sectors == 0)
463 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 else
465 return max;
466}
467
468/*
469 * This routine returns the disk from which the requested read should
470 * be done. There is a per-array 'next expected sequential IO' sector
471 * number - if this matches on the next IO then we use the last disk.
472 * There is also a per-disk 'last know head position' sector that is
473 * maintained from IRQ contexts, both the normal and the resync IO
474 * completion handlers update this position correctly. If there is no
475 * perfect sequential match then we pick the disk whose head is closest.
476 *
477 * If there are 2 mirrors in the same 2 devices, performance degrades
478 * because position is mirror, not device based.
479 *
480 * The rdev for the device selected will have nr_pending incremented.
481 */
482
483/*
484 * FIXME: possibly should rethink readbalancing and do it differently
485 * depending on near_copies / far_copies geometry.
486 */
487static int read_balance(conf_t *conf, r10bio_t *r10_bio)
488{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000489 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000490 int disk, slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 const int sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000492 sector_t new_distance, best_dist;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800493 mdk_rdev_t *rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000494 int do_balance;
495 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 raid10_find_phys(conf, r10_bio);
498 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000499retry:
500 best_slot = -1;
501 best_dist = MaxSector;
502 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
504 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800505 * device if no resync is going on (recovery is ok), or below
506 * the resync window. We take the first readable disk when
507 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
509 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000510 && (this_sector + sectors >= conf->next_resync))
511 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
NeilBrown56d99122011-05-11 14:27:03 +1000513 for (slot = 0; slot < conf->copies ; slot++) {
514 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000516 disk = r10_bio->devs[slot].devnum;
517 rdev = rcu_dereference(conf->mirrors[disk].rdev);
518 if (rdev == NULL)
519 continue;
520 if (!test_bit(In_sync, &rdev->flags))
521 continue;
522
523 if (!do_balance)
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
NeilBrown22dfdf52005-11-28 13:44:09 -0800526 /* This optimisation is debatable, and completely destroys
527 * sequential read speed for 'far copies' arrays. So only
528 * keep it for 'near' arrays, and review those later.
529 */
NeilBrown56d99122011-05-11 14:27:03 +1000530 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800532
533 /* for far > 1 always use the lowest address */
534 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000535 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800536 else
NeilBrown56d99122011-05-11 14:27:03 +1000537 new_distance = abs(r10_bio->devs[slot].addr -
538 conf->mirrors[disk].head_position);
539 if (new_distance < best_dist) {
540 best_dist = new_distance;
541 best_slot = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543 }
NeilBrown56d99122011-05-11 14:27:03 +1000544 if (slot == conf->copies)
545 slot = best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
NeilBrown56d99122011-05-11 14:27:03 +1000547 if (slot >= 0) {
548 disk = r10_bio->devs[slot].devnum;
549 rdev = rcu_dereference(conf->mirrors[disk].rdev);
550 if (!rdev)
551 goto retry;
552 atomic_inc(&rdev->nr_pending);
553 if (test_bit(Faulty, &rdev->flags)) {
554 /* Cannot risk returning a device that failed
555 * before we inc'ed nr_pending
556 */
557 rdev_dec_pending(rdev, conf->mddev);
558 goto retry;
559 }
560 r10_bio->read_slot = slot;
561 } else
NeilBrown29fc7e32006-02-03 03:03:41 -0800562 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 rcu_read_unlock();
564
565 return disk;
566}
567
NeilBrown0d129222006-10-03 01:15:54 -0700568static int raid10_congested(void *data, int bits)
569{
570 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000571 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700572 int i, ret = 0;
573
NeilBrown3fa841d2009-09-23 18:10:29 +1000574 if (mddev_congested(mddev, bits))
575 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700576 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100577 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700578 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
579 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200580 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700581
582 ret |= bdi_congested(&q->backing_dev_info, bits);
583 }
584 }
585 rcu_read_unlock();
586 return ret;
587}
588
Jens Axboe7eaceac2011-03-10 08:52:07 +0100589static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800590{
591 /* Any writes that have been queued but are awaiting
592 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800593 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800594 spin_lock_irq(&conf->device_lock);
595
596 if (conf->pending_bio_list.head) {
597 struct bio *bio;
598 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800599 spin_unlock_irq(&conf->device_lock);
600 /* flush any pending bitmap writes to disk
601 * before proceeding w/ I/O */
602 bitmap_unplug(conf->mddev->bitmap);
603
604 while (bio) { /* submit pending writes */
605 struct bio *next = bio->bi_next;
606 bio->bi_next = NULL;
607 generic_make_request(bio);
608 bio = next;
609 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800610 } else
611 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800612}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100613
NeilBrown0a27ec92006-01-06 00:20:13 -0800614/* Barriers....
615 * Sometimes we need to suspend IO while we do something else,
616 * either some resync/recovery, or reconfigure the array.
617 * To do this we raise a 'barrier'.
618 * The 'barrier' is a counter that can be raised multiple times
619 * to count how many activities are happening which preclude
620 * normal IO.
621 * We can only raise the barrier if there is no pending IO.
622 * i.e. if nr_pending == 0.
623 * We choose only to raise the barrier if no-one is waiting for the
624 * barrier to go down. This means that as soon as an IO request
625 * is ready, no other operations which require a barrier will start
626 * until the IO request has had a chance.
627 *
628 * So: regular IO calls 'wait_barrier'. When that returns there
629 * is no backgroup IO happening, It must arrange to call
630 * allow_barrier when it has finished its IO.
631 * backgroup IO calls must call raise_barrier. Once that returns
632 * there is no normal IO happeing. It must arrange to call
633 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
NeilBrown6cce3b22006-01-06 00:20:16 -0800636static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
NeilBrown6cce3b22006-01-06 00:20:16 -0800638 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
NeilBrown6cce3b22006-01-06 00:20:16 -0800641 /* Wait until no block IO is waiting (unless 'force') */
642 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000643 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800644
645 /* block any new IO from starting */
646 conf->barrier++;
647
NeilBrownc3b328a2011-04-18 18:25:43 +1000648 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800649 wait_event_lock_irq(conf->wait_barrier,
650 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000651 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 spin_unlock_irq(&conf->resync_lock);
654}
655
NeilBrown0a27ec92006-01-06 00:20:13 -0800656static void lower_barrier(conf_t *conf)
657{
658 unsigned long flags;
659 spin_lock_irqsave(&conf->resync_lock, flags);
660 conf->barrier--;
661 spin_unlock_irqrestore(&conf->resync_lock, flags);
662 wake_up(&conf->wait_barrier);
663}
664
665static void wait_barrier(conf_t *conf)
666{
667 spin_lock_irq(&conf->resync_lock);
668 if (conf->barrier) {
669 conf->nr_waiting++;
670 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
671 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000672 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800673 conf->nr_waiting--;
674 }
675 conf->nr_pending++;
676 spin_unlock_irq(&conf->resync_lock);
677}
678
679static void allow_barrier(conf_t *conf)
680{
681 unsigned long flags;
682 spin_lock_irqsave(&conf->resync_lock, flags);
683 conf->nr_pending--;
684 spin_unlock_irqrestore(&conf->resync_lock, flags);
685 wake_up(&conf->wait_barrier);
686}
687
NeilBrown4443ae12006-01-06 00:20:28 -0800688static void freeze_array(conf_t *conf)
689{
690 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800691 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800692 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800693 * wait until nr_pending match nr_queued+1
694 * This is called in the context of one normal IO request
695 * that has failed. Thus any sync request that might be pending
696 * will be blocked by nr_pending, and we need to wait for
697 * pending IO requests to complete or be queued for re-try.
698 * Thus the number queued (nr_queued) plus this request (1)
699 * must match the number of pending IOs (nr_pending) before
700 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800701 */
702 spin_lock_irq(&conf->resync_lock);
703 conf->barrier++;
704 conf->nr_waiting++;
705 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800706 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800707 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000708 flush_pending_writes(conf));
709
NeilBrown4443ae12006-01-06 00:20:28 -0800710 spin_unlock_irq(&conf->resync_lock);
711}
712
713static void unfreeze_array(conf_t *conf)
714{
715 /* reverse the effect of the freeze */
716 spin_lock_irq(&conf->resync_lock);
717 conf->barrier--;
718 conf->nr_waiting--;
719 wake_up(&conf->wait_barrier);
720 spin_unlock_irq(&conf->resync_lock);
721}
722
NeilBrown21a52c62010-04-01 15:02:13 +1100723static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
NeilBrown070ec552009-06-16 16:54:21 +1000725 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 mirror_info_t *mirror;
727 r10bio_t *r10_bio;
728 struct bio *read_bio;
729 int i;
730 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100731 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000732 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200733 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800734 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700735 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000736 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Tejun Heoe9c74692010-09-03 11:56:18 +0200738 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
739 md_flush_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700740 return 0;
741 }
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* If this request crosses a chunk boundary, we need to
744 * split it. This will only happen for 1 PAGE (or less) requests.
745 */
746 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
747 > chunk_sects &&
748 conf->near_copies < conf->raid_disks)) {
749 struct bio_pair *bp;
750 /* Sanity check -- queue functions should prevent this happening */
751 if (bio->bi_vcnt != 1 ||
752 bio->bi_idx != 0)
753 goto bad_map;
754 /* This is a one page bio that upper layers
755 * refuse to split for us, so we need to split it.
756 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200757 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000759
760 /* Each of these 'make_request' calls will call 'wait_barrier'.
761 * If the first succeeds but the second blocks due to the resync
762 * thread raising the barrier, we will deadlock because the
763 * IO to the underlying device will be queued in generic_make_request
764 * and will never complete, so will never reduce nr_pending.
765 * So increment nr_waiting here so no new raise_barriers will
766 * succeed, and so the second wait_barrier cannot block.
767 */
768 spin_lock_irq(&conf->resync_lock);
769 conf->nr_waiting++;
770 spin_unlock_irq(&conf->resync_lock);
771
NeilBrown21a52c62010-04-01 15:02:13 +1100772 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100774 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 generic_make_request(&bp->bio2);
776
NeilBrown51e9ac72010-08-07 21:17:00 +1000777 spin_lock_irq(&conf->resync_lock);
778 conf->nr_waiting--;
779 wake_up(&conf->wait_barrier);
780 spin_unlock_irq(&conf->resync_lock);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 bio_pair_release(bp);
783 return 0;
784 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000785 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
786 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
788
NeilBrown6712ecf2007-09-27 12:47:43 +0200789 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 0;
791 }
792
NeilBrown3d310eb2005-06-21 17:17:26 -0700793 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * Register the new request and wait if the reconstruction
797 * thread has put up a bar for new requests.
798 * Continue immediately if no resync is active currently.
799 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800800 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
803
804 r10_bio->master_bio = bio;
805 r10_bio->sectors = bio->bi_size >> 9;
806
807 r10_bio->mddev = mddev;
808 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800809 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jens Axboea3623572005-11-01 09:26:16 +0100811 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /*
813 * read balancing logic:
814 */
815 int disk = read_balance(conf, r10_bio);
816 int slot = r10_bio->read_slot;
817 if (disk < 0) {
818 raid_end_bio_io(r10_bio);
819 return 0;
820 }
821 mirror = conf->mirrors + disk;
822
NeilBrowna167f662010-10-26 18:31:13 +1100823 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 r10_bio->devs[slot].bio = read_bio;
826
827 read_bio->bi_sector = r10_bio->devs[slot].addr +
828 mirror->rdev->data_offset;
829 read_bio->bi_bdev = mirror->rdev->bdev;
830 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200831 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 read_bio->bi_private = r10_bio;
833
834 generic_make_request(read_bio);
835 return 0;
836 }
837
838 /*
839 * WRITE:
840 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700841 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * inc refcount on their rdev. Record them by setting
843 * bios[x] to bio
844 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000845 plugged = mddev_check_plugged(mddev);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700848 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700849 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 rcu_read_lock();
851 for (i = 0; i < conf->copies; i++) {
852 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800853 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700854 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
855 atomic_inc(&rdev->nr_pending);
856 blocked_rdev = rdev;
857 break;
858 }
859 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800860 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800862 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800864 set_bit(R10BIO_Degraded, &r10_bio->state);
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 rcu_read_unlock();
868
Dan Williams6bfe0b42008-04-30 00:52:32 -0700869 if (unlikely(blocked_rdev)) {
870 /* Have to wait for this device to get unblocked, then retry */
871 int j;
872 int d;
873
874 for (j = 0; j < i; j++)
875 if (r10_bio->devs[j].bio) {
876 d = r10_bio->devs[j].devnum;
877 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
878 }
879 allow_barrier(conf);
880 md_wait_for_blocked_rdev(blocked_rdev, mddev);
881 wait_barrier(conf);
882 goto retry_write;
883 }
884
NeilBrown4e780642010-10-19 12:54:01 +1100885 atomic_set(&r10_bio->remaining, 1);
886 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 for (i = 0; i < conf->copies; i++) {
889 struct bio *mbio;
890 int d = r10_bio->devs[i].devnum;
891 if (!r10_bio->devs[i].bio)
892 continue;
893
NeilBrowna167f662010-10-26 18:31:13 +1100894 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 r10_bio->devs[i].bio = mbio;
896
897 mbio->bi_sector = r10_bio->devs[i].addr+
898 conf->mirrors[d].rdev->data_offset;
899 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
900 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +0200901 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 mbio->bi_private = r10_bio;
903
904 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +1100905 spin_lock_irqsave(&conf->device_lock, flags);
906 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +1100907 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
NeilBrown4e780642010-10-19 12:54:01 +1100910 if (atomic_dec_and_test(&r10_bio->remaining)) {
911 /* This matches the end of raid10_end_write_request() */
912 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
913 r10_bio->sectors,
914 !test_bit(R10BIO_Degraded, &r10_bio->state),
915 0);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700916 md_write_end(mddev);
917 raid_end_bio_io(r10_bio);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700918 }
919
NeilBrowna35e63e2008-03-04 14:29:29 -0800920 /* In case raid10d snuck in to freeze_array */
921 wake_up(&conf->wait_barrier);
922
NeilBrownc3b328a2011-04-18 18:25:43 +1000923 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -0800924 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 0;
926}
927
928static void status(struct seq_file *seq, mddev_t *mddev)
929{
NeilBrown070ec552009-06-16 16:54:21 +1000930 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int i;
932
933 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +1000934 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (conf->near_copies > 1)
936 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700937 if (conf->far_copies > 1) {
938 if (conf->far_offset)
939 seq_printf(seq, " %d offset-copies", conf->far_copies);
940 else
941 seq_printf(seq, " %d far-copies", conf->far_copies);
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700944 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 for (i = 0; i < conf->raid_disks; i++)
946 seq_printf(seq, "%s",
947 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800948 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 seq_printf(seq, "]");
950}
951
952static void error(mddev_t *mddev, mdk_rdev_t *rdev)
953{
954 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +1000955 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /*
958 * If it is not operational, then we have already marked it as dead
959 * else if it is the last working disks, ignore the error, let the
960 * next level up know.
961 * else mark the drive as failed
962 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800963 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -0700964 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * Don't fail the drive, just return an IO error.
967 * The test should really be more sophisticated than
968 * "working_disks == 1", but it isn't critical, and
969 * can wait until we do more sophisticated "is the drive
970 * really dead" tests...
971 */
972 return;
NeilBrownc04be0a2006-10-03 01:15:53 -0700973 if (test_and_clear_bit(In_sync, &rdev->flags)) {
974 unsigned long flags;
975 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700977 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * if recovery is running, make sure it aborts.
980 */
NeilBrowndfc70642008-05-23 13:04:39 -0700981 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800983 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -0700984 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +1100985 printk(KERN_ALERT
986 "md/raid10:%s: Disk failure on %s, disabling device.\n"
987 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +1000988 mdname(mddev), bdevname(rdev->bdev, b),
989 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static void print_conf(conf_t *conf)
993{
994 int i;
995 mirror_info_t *tmp;
996
NeilBrown128595e2010-05-03 14:47:14 +1000997 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +1000999 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return;
1001 }
NeilBrown128595e2010-05-03 14:47:14 +10001002 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 conf->raid_disks);
1004
1005 for (i = 0; i < conf->raid_disks; i++) {
1006 char b[BDEVNAME_SIZE];
1007 tmp = conf->mirrors + i;
1008 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001009 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001010 i, !test_bit(In_sync, &tmp->rdev->flags),
1011 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 bdevname(tmp->rdev->bdev,b));
1013 }
1014}
1015
1016static void close_sync(conf_t *conf)
1017{
NeilBrown0a27ec92006-01-06 00:20:13 -08001018 wait_barrier(conf);
1019 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 mempool_destroy(conf->r10buf_pool);
1022 conf->r10buf_pool = NULL;
1023}
1024
NeilBrown6d508242005-09-09 16:24:03 -07001025/* check if there are enough drives for
1026 * every block to appear on atleast one
1027 */
1028static int enough(conf_t *conf)
1029{
1030 int first = 0;
1031
1032 do {
1033 int n = conf->copies;
1034 int cnt = 0;
1035 while (n--) {
1036 if (conf->mirrors[first].rdev)
1037 cnt++;
1038 first = (first+1) % conf->raid_disks;
1039 }
1040 if (cnt == 0)
1041 return 0;
1042 } while (first != 0);
1043 return 1;
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static int raid10_spare_active(mddev_t *mddev)
1047{
1048 int i;
1049 conf_t *conf = mddev->private;
1050 mirror_info_t *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001051 int count = 0;
1052 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 /*
1055 * Find all non-in_sync disks within the RAID10 configuration
1056 * and mark them in_sync
1057 */
1058 for (i = 0; i < conf->raid_disks; i++) {
1059 tmp = conf->mirrors + i;
1060 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001061 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001062 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001063 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001064 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066 }
NeilBrown6b965622010-08-18 11:56:59 +10001067 spin_lock_irqsave(&conf->device_lock, flags);
1068 mddev->degraded -= count;
1069 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001072 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075
1076static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1077{
1078 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001079 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 int mirror;
1081 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001082 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001083 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (mddev->recovery_cp < MaxSector)
1086 /* only hot-add to in-sync arrays, as recovery is
1087 * very different from resync
1088 */
Neil Brown199050e2008-06-28 08:31:33 +10001089 return -EBUSY;
NeilBrown6d508242005-09-09 16:24:03 -07001090 if (!enough(conf))
Neil Brown199050e2008-06-28 08:31:33 +10001091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
NeilBrowna53a6c82008-11-06 17:28:20 +11001093 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001094 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001096 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001097 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1098 mirror = rdev->saved_raid_disk;
1099 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001100 mirror = first;
1101 for ( ; mirror <= last ; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if ( !(p=conf->mirrors+mirror)->rdev) {
1103
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001104 disk_stack_limits(mddev->gendisk, rdev->bdev,
1105 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001106 /* as we don't honour merge_bvec_fn, we must
1107 * never risk violating it, so limit
1108 * ->max_segments to one lying with a single
1109 * page, as a one page request is never in
1110 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
NeilBrown627a2d32010-03-08 16:44:38 +11001112 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1113 blk_queue_max_segments(mddev->queue, 1);
1114 blk_queue_segment_boundary(mddev->queue,
1115 PAGE_CACHE_SIZE - 1);
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 p->head_position = 0;
1119 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001120 err = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001121 if (rdev->saved_raid_disk != mirror)
1122 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001123 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 }
1126
Andre Nollac5e7112009-08-03 10:59:47 +10001127 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132static int raid10_remove_disk(mddev_t *mddev, int number)
1133{
1134 conf_t *conf = mddev->private;
1135 int err = 0;
1136 mdk_rdev_t *rdev;
1137 mirror_info_t *p = conf->mirrors+ number;
1138
1139 print_conf(conf);
1140 rdev = p->rdev;
1141 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001142 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 atomic_read(&rdev->nr_pending)) {
1144 err = -EBUSY;
1145 goto abort;
1146 }
NeilBrowndfc70642008-05-23 13:04:39 -07001147 /* Only remove faulty devices in recovery
1148 * is not possible.
1149 */
1150 if (!test_bit(Faulty, &rdev->flags) &&
1151 enough(conf)) {
1152 err = -EBUSY;
1153 goto abort;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001156 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (atomic_read(&rdev->nr_pending)) {
1158 /* lost the race, try later */
1159 err = -EBUSY;
1160 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001161 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001163 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165abort:
1166
1167 print_conf(conf);
1168 return err;
1169}
1170
1171
NeilBrown6712ecf2007-09-27 12:47:43 +02001172static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001174 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001175 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int i,d;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 for (i=0; i<conf->copies; i++)
1179 if (r10_bio->devs[i].bio == bio)
1180 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001181 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 update_head_pos(i, r10_bio);
1183 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001184
1185 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1186 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001187 else {
1188 atomic_add(r10_bio->sectors,
1189 &conf->mirrors[d].rdev->corrected_errors);
1190 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1191 md_error(r10_bio->mddev,
1192 conf->mirrors[d].rdev);
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 /* for reconstruct, we always reschedule after a read.
1196 * for resync, only after all reads
1197 */
NeilBrown73d5c382009-02-25 13:18:47 +11001198 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1200 atomic_dec_and_test(&r10_bio->remaining)) {
1201 /* we have read all the blocks,
1202 * do the comparison in process context in raid10d
1203 */
1204 reschedule_retry(r10_bio);
1205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
NeilBrown6712ecf2007-09-27 12:47:43 +02001208static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001211 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001213 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int i,d;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 for (i = 0; i < conf->copies; i++)
1217 if (r10_bio->devs[i].bio == bio)
1218 break;
1219 d = r10_bio->devs[i].devnum;
1220
1221 if (!uptodate)
1222 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 update_head_pos(i, r10_bio);
1225
NeilBrown73d5c382009-02-25 13:18:47 +11001226 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 while (atomic_dec_and_test(&r10_bio->remaining)) {
1228 if (r10_bio->master_bio == NULL) {
1229 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001230 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001232 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 } else {
1235 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1236 put_buf(r10_bio);
1237 r10_bio = r10_bio2;
1238 }
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/*
1243 * Note: sync and recover and handled very differently for raid10
1244 * This code is for resync.
1245 * For resync, we read through virtual addresses and read all blocks.
1246 * If there is any error, we schedule a write. The lowest numbered
1247 * drive is authoritative.
1248 * However requests come for physical address, so we need to map.
1249 * For every physical address there are raid_disks/copies virtual addresses,
1250 * which is always are least one, but is not necessarly an integer.
1251 * This means that a physical address can span multiple chunks, so we may
1252 * have to submit multiple io requests for a single sync request.
1253 */
1254/*
1255 * We check if all blocks are in-sync and only write to blocks that
1256 * aren't in sync
1257 */
1258static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1259{
NeilBrown070ec552009-06-16 16:54:21 +10001260 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int i, first;
1262 struct bio *tbio, *fbio;
1263
1264 atomic_set(&r10_bio->remaining, 1);
1265
1266 /* find the first device with a block */
1267 for (i=0; i<conf->copies; i++)
1268 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1269 break;
1270
1271 if (i == conf->copies)
1272 goto done;
1273
1274 first = i;
1275 fbio = r10_bio->devs[i].bio;
1276
1277 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001278 for (i=0 ; i < conf->copies ; i++) {
1279 int j, d;
1280 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001283
1284 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001286 if (i == first)
1287 continue;
1288 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1289 /* We know that the bi_io_vec layout is the same for
1290 * both 'first' and 'i', so we just compare them.
1291 * All vec entries are PAGE_SIZE;
1292 */
1293 for (j = 0; j < vcnt; j++)
1294 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1295 page_address(tbio->bi_io_vec[j].bv_page),
1296 PAGE_SIZE))
1297 break;
1298 if (j == vcnt)
1299 continue;
1300 mddev->resync_mismatches += r10_bio->sectors;
1301 }
NeilBrown18f08812006-01-06 00:20:25 -08001302 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1303 /* Don't fix anything. */
1304 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /* Ok, we need to write this bio
1306 * First we need to fixup bv_offset, bv_len and
1307 * bi_vecs, as the read request might have corrupted these
1308 */
1309 tbio->bi_vcnt = vcnt;
1310 tbio->bi_size = r10_bio->sectors << 9;
1311 tbio->bi_idx = 0;
1312 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1314 tbio->bi_flags |= 1 << BIO_UPTODATE;
1315 tbio->bi_next = NULL;
1316 tbio->bi_rw = WRITE;
1317 tbio->bi_private = r10_bio;
1318 tbio->bi_sector = r10_bio->devs[i].addr;
1319
1320 for (j=0; j < vcnt ; j++) {
1321 tbio->bi_io_vec[j].bv_offset = 0;
1322 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1323
1324 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1325 page_address(fbio->bi_io_vec[j].bv_page),
1326 PAGE_SIZE);
1327 }
1328 tbio->bi_end_io = end_sync_write;
1329
1330 d = r10_bio->devs[i].devnum;
1331 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1332 atomic_inc(&r10_bio->remaining);
1333 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1334
1335 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1336 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1337 generic_make_request(tbio);
1338 }
1339
1340done:
1341 if (atomic_dec_and_test(&r10_bio->remaining)) {
1342 md_done_sync(mddev, r10_bio->sectors, 1);
1343 put_buf(r10_bio);
1344 }
1345}
1346
1347/*
1348 * Now for the recovery code.
1349 * Recovery happens across physical sectors.
1350 * We recover all non-is_sync drives by finding the virtual address of
1351 * each, and then choose a working drive that also has that virt address.
1352 * There is a separate r10_bio for each non-in_sync drive.
1353 * Only the first two slots are in use. The first for reading,
1354 * The second for writing.
1355 *
1356 */
1357
1358static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1359{
NeilBrown070ec552009-06-16 16:54:21 +10001360 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int i, d;
1362 struct bio *bio, *wbio;
1363
1364
1365 /* move the pages across to the second bio
1366 * and submit the write request
1367 */
1368 bio = r10_bio->devs[0].bio;
1369 wbio = r10_bio->devs[1].bio;
1370 for (i=0; i < wbio->bi_vcnt; i++) {
1371 struct page *p = bio->bi_io_vec[i].bv_page;
1372 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1373 wbio->bi_io_vec[i].bv_page = p;
1374 }
1375 d = r10_bio->devs[1].devnum;
1376
1377 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1378 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001379 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1380 generic_make_request(wbio);
1381 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001382 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385
1386/*
Robert Becker1e509152009-12-14 12:49:58 +11001387 * Used by fix_read_error() to decay the per rdev read_errors.
1388 * We halve the read error count for every hour that has elapsed
1389 * since the last recorded read error.
1390 *
1391 */
1392static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1393{
1394 struct timespec cur_time_mon;
1395 unsigned long hours_since_last;
1396 unsigned int read_errors = atomic_read(&rdev->read_errors);
1397
1398 ktime_get_ts(&cur_time_mon);
1399
1400 if (rdev->last_read_error.tv_sec == 0 &&
1401 rdev->last_read_error.tv_nsec == 0) {
1402 /* first time we've seen a read error */
1403 rdev->last_read_error = cur_time_mon;
1404 return;
1405 }
1406
1407 hours_since_last = (cur_time_mon.tv_sec -
1408 rdev->last_read_error.tv_sec) / 3600;
1409
1410 rdev->last_read_error = cur_time_mon;
1411
1412 /*
1413 * if hours_since_last is > the number of bits in read_errors
1414 * just set read errors to 0. We do this to avoid
1415 * overflowing the shift of read_errors by hours_since_last.
1416 */
1417 if (hours_since_last >= 8 * sizeof(read_errors))
1418 atomic_set(&rdev->read_errors, 0);
1419 else
1420 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1421}
1422
1423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * This is a kernel thread which:
1425 *
1426 * 1. Retries failed read operations on working mirrors.
1427 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001428 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 */
1430
NeilBrown6814d532006-10-03 01:15:45 -07001431static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1432{
1433 int sect = 0; /* Offset from r10_bio->sector */
1434 int sectors = r10_bio->sectors;
1435 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001436 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001437 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001438
NeilBrown7c4e06f2011-05-11 14:53:17 +10001439 /* still own a reference to this rdev, so it cannot
1440 * have been cleared recently.
1441 */
1442 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001443
NeilBrown7c4e06f2011-05-11 14:53:17 +10001444 if (test_bit(Faulty, &rdev->flags))
1445 /* drive has already been failed, just ignore any
1446 more fix_read_error() attempts */
1447 return;
1448
1449 check_decay_read_errors(mddev, rdev);
1450 atomic_inc(&rdev->read_errors);
1451 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1452 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001453 bdevname(rdev->bdev, b);
1454
NeilBrown7c4e06f2011-05-11 14:53:17 +10001455 printk(KERN_NOTICE
1456 "md/raid10:%s: %s: Raid device exceeded "
1457 "read_error threshold [cur %d:max %d]\n",
1458 mdname(mddev), b,
1459 atomic_read(&rdev->read_errors), max_read_errors);
1460 printk(KERN_NOTICE
1461 "md/raid10:%s: %s: Failing raid device\n",
1462 mdname(mddev), b);
1463 md_error(mddev, conf->mirrors[d].rdev);
1464 return;
Robert Becker1e509152009-12-14 12:49:58 +11001465 }
Robert Becker1e509152009-12-14 12:49:58 +11001466
NeilBrown6814d532006-10-03 01:15:45 -07001467 while(sectors) {
1468 int s = sectors;
1469 int sl = r10_bio->read_slot;
1470 int success = 0;
1471 int start;
1472
1473 if (s > (PAGE_SIZE>>9))
1474 s = PAGE_SIZE >> 9;
1475
1476 rcu_read_lock();
1477 do {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001478 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001479 rdev = rcu_dereference(conf->mirrors[d].rdev);
1480 if (rdev &&
1481 test_bit(In_sync, &rdev->flags)) {
1482 atomic_inc(&rdev->nr_pending);
1483 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001484 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001485 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001486 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001487 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001488 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001489 rdev_dec_pending(rdev, mddev);
1490 rcu_read_lock();
1491 if (success)
1492 break;
1493 }
1494 sl++;
1495 if (sl == conf->copies)
1496 sl = 0;
1497 } while (!success && sl != r10_bio->read_slot);
1498 rcu_read_unlock();
1499
1500 if (!success) {
1501 /* Cannot read from anywhere -- bye bye array */
1502 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1503 md_error(mddev, conf->mirrors[dn].rdev);
1504 break;
1505 }
1506
1507 start = sl;
1508 /* write it back and re-read */
1509 rcu_read_lock();
1510 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001511 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001512
NeilBrown6814d532006-10-03 01:15:45 -07001513 if (sl==0)
1514 sl = conf->copies;
1515 sl--;
1516 d = r10_bio->devs[sl].devnum;
1517 rdev = rcu_dereference(conf->mirrors[d].rdev);
1518 if (rdev &&
1519 test_bit(In_sync, &rdev->flags)) {
1520 atomic_inc(&rdev->nr_pending);
1521 rcu_read_unlock();
1522 atomic_add(s, &rdev->corrected_errors);
NeilBrown2b193362010-10-27 15:16:40 +11001523 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001524 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001525 sect,
1526 s<<9, conf->tmppage, WRITE, false)
Robert Becker67b8dc42009-12-14 12:49:57 +11001527 == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001528 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001529 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001530 "md/raid10:%s: read correction "
Robert Becker67b8dc42009-12-14 12:49:57 +11001531 "write failed"
1532 " (%d sectors at %llu on %s)\n",
1533 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001534 (unsigned long long)(
1535 sect + rdev->data_offset),
Robert Becker67b8dc42009-12-14 12:49:57 +11001536 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001537 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
Robert Becker67b8dc42009-12-14 12:49:57 +11001538 "drive\n",
NeilBrown128595e2010-05-03 14:47:14 +10001539 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001540 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001541 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001542 }
NeilBrown6814d532006-10-03 01:15:45 -07001543 rdev_dec_pending(rdev, mddev);
1544 rcu_read_lock();
1545 }
1546 }
1547 sl = start;
1548 while (sl != r10_bio->read_slot) {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001549
NeilBrown6814d532006-10-03 01:15:45 -07001550 if (sl==0)
1551 sl = conf->copies;
1552 sl--;
1553 d = r10_bio->devs[sl].devnum;
1554 rdev = rcu_dereference(conf->mirrors[d].rdev);
1555 if (rdev &&
1556 test_bit(In_sync, &rdev->flags)) {
1557 char b[BDEVNAME_SIZE];
1558 atomic_inc(&rdev->nr_pending);
1559 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001560 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001561 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001562 sect,
Robert Becker67b8dc42009-12-14 12:49:57 +11001563 s<<9, conf->tmppage,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001564 READ, false) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001565 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001566 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001567 "md/raid10:%s: unable to read back "
Robert Becker67b8dc42009-12-14 12:49:57 +11001568 "corrected sectors"
1569 " (%d sectors at %llu on %s)\n",
1570 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001571 (unsigned long long)(
1572 sect + rdev->data_offset),
Robert Becker67b8dc42009-12-14 12:49:57 +11001573 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001574 printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
1575 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001576 bdevname(rdev->bdev, b));
1577
NeilBrown6814d532006-10-03 01:15:45 -07001578 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001579 } else {
NeilBrown6814d532006-10-03 01:15:45 -07001580 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10001581 "md/raid10:%s: read error corrected"
NeilBrown6814d532006-10-03 01:15:45 -07001582 " (%d sectors at %llu on %s)\n",
1583 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001584 (unsigned long long)(
1585 sect + rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001586 bdevname(rdev->bdev, b));
Robert Becker67b8dc42009-12-14 12:49:57 +11001587 }
NeilBrown6814d532006-10-03 01:15:45 -07001588
1589 rdev_dec_pending(rdev, mddev);
1590 rcu_read_lock();
1591 }
1592 }
1593 rcu_read_unlock();
1594
1595 sectors -= s;
1596 sect += s;
1597 }
1598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600static void raid10d(mddev_t *mddev)
1601{
1602 r10bio_t *r10_bio;
1603 struct bio *bio;
1604 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001605 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct list_head *head = &conf->retry_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 mdk_rdev_t *rdev;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001608 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001612 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 for (;;) {
1614 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001615
Jens Axboe7eaceac2011-03-10 08:52:07 +01001616 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001619 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001620 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1624 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001625 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 spin_unlock_irqrestore(&conf->device_lock, flags);
1627
1628 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001629 conf = mddev->private;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001630 if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001632 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 recovery_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001634 else {
NeilBrown7c4e06f2011-05-11 14:53:17 +10001635 int slot = r10_bio->read_slot;
1636 int mirror = r10_bio->devs[slot].devnum;
NeilBrown4443ae12006-01-06 00:20:28 -08001637 /* we got a read error. Maybe the drive is bad. Maybe just
1638 * the block and we can fix it.
1639 * We freeze all other IO, and try reading the block from
1640 * other devices. When we find one, we re-write
1641 * and check it that fixes the read error.
1642 * This is all done synchronously while the array is
1643 * frozen.
1644 */
NeilBrown6814d532006-10-03 01:15:45 -07001645 if (mddev->ro == 0) {
1646 freeze_array(conf);
1647 fix_read_error(conf, mddev, r10_bio);
1648 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001649 }
NeilBrown7c4e06f2011-05-11 14:53:17 +10001650 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
NeilBrown4443ae12006-01-06 00:20:28 -08001651
NeilBrowna8830bca2011-05-11 14:54:19 +10001652 bio = r10_bio->devs[slot].bio;
1653 r10_bio->devs[slot].bio =
NeilBrown0eb3ff12006-01-06 00:20:29 -08001654 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 mirror = read_balance(conf, r10_bio);
1656 if (mirror == -1) {
NeilBrown128595e2010-05-03 14:47:14 +10001657 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 " read error for block %llu\n",
NeilBrown128595e2010-05-03 14:47:14 +10001659 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 bdevname(bio->bi_bdev,b),
1661 (unsigned long long)r10_bio->sector);
1662 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001663 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 } else {
NeilBrown2c7d46e2010-08-18 16:16:05 +10001665 const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
Maik Hampel14e71342007-07-31 00:37:57 -07001666 bio_put(bio);
NeilBrowna8830bca2011-05-11 14:54:19 +10001667 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 rdev = conf->mirrors[mirror].rdev;
1669 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +10001670 printk(KERN_ERR "md/raid10:%s: %s: redirecting sector %llu to"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 " another mirror\n",
NeilBrown128595e2010-05-03 14:47:14 +10001672 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 bdevname(rdev->bdev,b),
1674 (unsigned long long)r10_bio->sector);
NeilBrowna167f662010-10-26 18:31:13 +11001675 bio = bio_clone_mddev(r10_bio->master_bio,
1676 GFP_NOIO, mddev);
NeilBrowna8830bca2011-05-11 14:54:19 +10001677 r10_bio->devs[slot].bio = bio;
1678 bio->bi_sector = r10_bio->devs[slot].addr
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 + rdev->data_offset;
1680 bio->bi_bdev = rdev->bdev;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001681 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 bio->bi_private = r10_bio;
1683 bio->bi_end_io = raid10_end_read_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 generic_make_request(bio);
1685 }
1686 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001687 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001689 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692
1693static int init_resync(conf_t *conf)
1694{
1695 int buffs;
1696
1697 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001698 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1700 if (!conf->r10buf_pool)
1701 return -ENOMEM;
1702 conf->next_resync = 0;
1703 return 0;
1704}
1705
1706/*
1707 * perform a "sync" on one "block"
1708 *
1709 * We need to make sure that no normal I/O request - particularly write
1710 * requests - conflict with active sync requests.
1711 *
1712 * This is achieved by tracking pending requests and a 'barrier' concept
1713 * that can be installed to exclude normal IO requests.
1714 *
1715 * Resync and recovery are handled very differently.
1716 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1717 *
1718 * For resync, we iterate over virtual addresses, read all copies,
1719 * and update if there are differences. If only one copy is live,
1720 * skip it.
1721 * For recovery, we iterate over physical addresses, read a good
1722 * value for each non-in_sync drive, and over-write.
1723 *
1724 * So, for recovery we may have several outstanding complex requests for a
1725 * given address, one for each out-of-sync device. We model this by allocating
1726 * a number of r10_bio structures, one for each out-of-sync device.
1727 * As we setup these structures, we collect all bio's together into a list
1728 * which we then process collectively to add pages, and then process again
1729 * to pass to generic_make_request.
1730 *
1731 * The r10_bio structures are linked using a borrowed master_bio pointer.
1732 * This link is counted in ->remaining. When the r10_bio that points to NULL
1733 * has its remaining count decremented to 0, the whole complex operation
1734 * is complete.
1735 *
1736 */
1737
NeilBrownab9d47e2011-05-11 14:54:41 +10001738static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
1739 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
NeilBrown070ec552009-06-16 16:54:21 +10001741 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 r10bio_t *r10_bio;
1743 struct bio *biolist = NULL, *bio;
1744 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001746 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11001747 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 sector_t sectors_skipped = 0;
1750 int chunks_skipped = 0;
1751
1752 if (!conf->r10buf_pool)
1753 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001757 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1759 max_sector = mddev->resync_max_sectors;
1760 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001761 /* If we aborted, we need to abort the
1762 * sync on the 'current' bitmap chucks (there can
1763 * be several when recovering multiple devices).
1764 * as we may have started syncing it but not finished.
1765 * We can find the current address in
1766 * mddev->curr_resync, but for recovery,
1767 * we need to convert that to several
1768 * virtual addresses.
1769 */
1770 if (mddev->curr_resync < max_sector) { /* aborted */
1771 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1772 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1773 &sync_blocks, 1);
1774 else for (i=0; i<conf->raid_disks; i++) {
1775 sector_t sect =
1776 raid10_find_virt(conf, mddev->curr_resync, i);
1777 bitmap_end_sync(mddev->bitmap, sect,
1778 &sync_blocks, 1);
1779 }
1780 } else /* completed sync */
1781 conf->fullsync = 0;
1782
1783 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001785 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return sectors_skipped;
1787 }
1788 if (chunks_skipped >= conf->raid_disks) {
1789 /* if there has been nothing to do on any drive,
1790 * then there is nothing to do at all..
1791 */
NeilBrown57afd892005-06-21 17:17:13 -07001792 *skipped = 1;
1793 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795
NeilBrownc6207272008-02-06 01:39:52 -08001796 if (max_sector > mddev->resync_max)
1797 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /* make sure whole request will fit in a chunk - if chunks
1800 * are meaningful
1801 */
1802 if (conf->near_copies < conf->raid_disks &&
1803 max_sector > (sector_nr | conf->chunk_mask))
1804 max_sector = (sector_nr | conf->chunk_mask) + 1;
1805 /*
1806 * If there is non-resync activity waiting for us then
1807 * put in a delay to throttle resync.
1808 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001809 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 /* Again, very different code for resync and recovery.
1813 * Both must result in an r10bio with a list of bios that
1814 * have bi_end_io, bi_sector, bi_bdev set,
1815 * and bi_private set to the r10bio.
1816 * For recovery, we may actually create several r10bios
1817 * with 2 bios in each, that correspond to the bios in the main one.
1818 * In this case, the subordinate r10bios link back through a
1819 * borrowed master_bio pointer, and the counter in the master
1820 * includes a ref from each subordinate.
1821 */
1822 /* First, we decide what to do and set ->bi_end_io
1823 * To end_sync_read if we want to read, and
1824 * end_sync_write if we will want to write.
1825 */
1826
NeilBrown6cce3b22006-01-06 00:20:16 -08001827 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1829 /* recovery... the complicated one */
NeilBrowna9f326e2009-09-23 18:06:41 +10001830 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 r10_bio = NULL;
1832
NeilBrownab9d47e2011-05-11 14:54:41 +10001833 for (i=0 ; i<conf->raid_disks; i++) {
1834 int still_degraded;
1835 r10bio_t *rb2;
1836 sector_t sect;
1837 int must_sync;
1838
1839 if (conf->mirrors[i].rdev == NULL ||
1840 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
1841 continue;
1842
1843 still_degraded = 0;
1844 /* want to reconstruct this device */
1845 rb2 = r10_bio;
1846 sect = raid10_find_virt(conf, sector_nr, i);
1847 /* Unless we are doing a full sync, we only need
1848 * to recover the block if it is set in the bitmap
1849 */
1850 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1851 &sync_blocks, 1);
1852 if (sync_blocks < max_sync)
1853 max_sync = sync_blocks;
1854 if (!must_sync &&
1855 !conf->fullsync) {
1856 /* yep, skip the sync_blocks here, but don't assume
1857 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08001858 */
NeilBrownab9d47e2011-05-11 14:54:41 +10001859 chunks_skipped = -1;
1860 continue;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
NeilBrownab9d47e2011-05-11 14:54:41 +10001863 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1864 raise_barrier(conf, rb2 != NULL);
1865 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
NeilBrownab9d47e2011-05-11 14:54:41 +10001867 r10_bio->master_bio = (struct bio*)rb2;
1868 if (rb2)
1869 atomic_inc(&rb2->remaining);
1870 r10_bio->mddev = mddev;
1871 set_bit(R10BIO_IsRecover, &r10_bio->state);
1872 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08001873
NeilBrownab9d47e2011-05-11 14:54:41 +10001874 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001875
NeilBrownab9d47e2011-05-11 14:54:41 +10001876 /* Need to check if the array will still be
1877 * degraded
1878 */
1879 for (j=0; j<conf->raid_disks; j++)
1880 if (conf->mirrors[j].rdev == NULL ||
1881 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
1882 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07001883 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
NeilBrownab9d47e2011-05-11 14:54:41 +10001885
1886 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1887 &sync_blocks, still_degraded);
1888
1889 for (j=0; j<conf->copies;j++) {
1890 int d = r10_bio->devs[j].devnum;
1891 if (!conf->mirrors[d].rdev ||
1892 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
1893 continue;
1894 /* This is where we read from */
1895 bio = r10_bio->devs[0].bio;
1896 bio->bi_next = biolist;
1897 biolist = bio;
1898 bio->bi_private = r10_bio;
1899 bio->bi_end_io = end_sync_read;
1900 bio->bi_rw = READ;
1901 bio->bi_sector = r10_bio->devs[j].addr +
1902 conf->mirrors[d].rdev->data_offset;
1903 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1904 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1905 atomic_inc(&r10_bio->remaining);
1906 /* and we write to 'i' */
1907
1908 for (k=0; k<conf->copies; k++)
1909 if (r10_bio->devs[k].devnum == i)
1910 break;
1911 BUG_ON(k == conf->copies);
1912 bio = r10_bio->devs[1].bio;
1913 bio->bi_next = biolist;
1914 biolist = bio;
1915 bio->bi_private = r10_bio;
1916 bio->bi_end_io = end_sync_write;
1917 bio->bi_rw = WRITE;
1918 bio->bi_sector = r10_bio->devs[k].addr +
1919 conf->mirrors[i].rdev->data_offset;
1920 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1921
1922 r10_bio->devs[0].devnum = d;
1923 r10_bio->devs[1].devnum = i;
1924
1925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
NeilBrownab9d47e2011-05-11 14:54:41 +10001927 if (j == conf->copies) {
1928 /* Cannot recover, so abort the recovery */
1929 put_buf(r10_bio);
1930 if (rb2)
1931 atomic_dec(&rb2->remaining);
1932 r10_bio = rb2;
1933 if (!test_and_set_bit(MD_RECOVERY_INTR,
1934 &mddev->recovery))
1935 printk(KERN_INFO "md/raid10:%s: insufficient "
1936 "working devices for recovery.\n",
1937 mdname(mddev));
1938 break;
1939 }
1940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (biolist == NULL) {
1942 while (r10_bio) {
1943 r10bio_t *rb2 = r10_bio;
1944 r10_bio = (r10bio_t*) rb2->master_bio;
1945 rb2->master_bio = NULL;
1946 put_buf(rb2);
1947 }
1948 goto giveup;
1949 }
1950 } else {
1951 /* resync. Schedule a read for every block at this virt offset */
1952 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001953
NeilBrown78200d42009-02-25 13:18:47 +11001954 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1955
NeilBrown6cce3b22006-01-06 00:20:16 -08001956 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1957 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10001958 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
1959 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001960 /* We can skip this block */
1961 *skipped = 1;
1962 return sync_blocks + sectors_skipped;
1963 }
1964 if (sync_blocks < max_sync)
1965 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 r10_bio->mddev = mddev;
1969 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001970 raise_barrier(conf, 0);
1971 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 r10_bio->master_bio = NULL;
1974 r10_bio->sector = sector_nr;
1975 set_bit(R10BIO_IsSync, &r10_bio->state);
1976 raid10_find_phys(conf, r10_bio);
1977 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1978
1979 for (i=0; i<conf->copies; i++) {
1980 int d = r10_bio->devs[i].devnum;
1981 bio = r10_bio->devs[i].bio;
1982 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001983 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001985 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 continue;
1987 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1988 atomic_inc(&r10_bio->remaining);
1989 bio->bi_next = biolist;
1990 biolist = bio;
1991 bio->bi_private = r10_bio;
1992 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001993 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 bio->bi_sector = r10_bio->devs[i].addr +
1995 conf->mirrors[d].rdev->data_offset;
1996 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1997 count++;
1998 }
1999
2000 if (count < 2) {
2001 for (i=0; i<conf->copies; i++) {
2002 int d = r10_bio->devs[i].devnum;
2003 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002004 rdev_dec_pending(conf->mirrors[d].rdev,
2005 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007 put_buf(r10_bio);
2008 biolist = NULL;
2009 goto giveup;
2010 }
2011 }
2012
2013 for (bio = biolist; bio ; bio=bio->bi_next) {
2014
2015 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2016 if (bio->bi_end_io)
2017 bio->bi_flags |= 1 << BIO_UPTODATE;
2018 bio->bi_vcnt = 0;
2019 bio->bi_idx = 0;
2020 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 bio->bi_size = 0;
2022 }
2023
2024 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002025 if (sector_nr + max_sync < max_sector)
2026 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 do {
2028 struct page *page;
2029 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (sector_nr + (len>>9) > max_sector)
2031 len = (max_sector - sector_nr) << 9;
2032 if (len == 0)
2033 break;
2034 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002035 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002037 if (bio_add_page(bio, page, len, 0))
2038 continue;
2039
2040 /* stop here */
2041 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2042 for (bio2 = biolist;
2043 bio2 && bio2 != bio;
2044 bio2 = bio2->bi_next) {
2045 /* remove last page from this bio */
2046 bio2->bi_vcnt--;
2047 bio2->bi_size -= len;
2048 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002050 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 nr_sectors += len>>9;
2053 sector_nr += len>>9;
2054 } while (biolist->bi_vcnt < RESYNC_PAGES);
2055 bio_full:
2056 r10_bio->sectors = nr_sectors;
2057
2058 while (biolist) {
2059 bio = biolist;
2060 biolist = biolist->bi_next;
2061
2062 bio->bi_next = NULL;
2063 r10_bio = bio->bi_private;
2064 r10_bio->sectors = nr_sectors;
2065
2066 if (bio->bi_end_io == end_sync_read) {
2067 md_sync_acct(bio->bi_bdev, nr_sectors);
2068 generic_make_request(bio);
2069 }
2070 }
2071
NeilBrown57afd892005-06-21 17:17:13 -07002072 if (sectors_skipped)
2073 /* pretend they weren't skipped, it makes
2074 * no important difference in this case
2075 */
2076 md_done_sync(mddev, sectors_skipped, 1);
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return sectors_skipped + nr_sectors;
2079 giveup:
2080 /* There is nowhere to write, so all non-sync
2081 * drives must be failed, so try the next chunk...
2082 */
NeilBrown09b40682009-02-25 13:18:47 +11002083 if (sector_nr + max_sync < max_sector)
2084 max_sector = sector_nr + max_sync;
2085
2086 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 chunks_skipped ++;
2088 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Dan Williams80c3a6c2009-03-17 18:10:40 -07002092static sector_t
2093raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2094{
2095 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002096 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002097
2098 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002099 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002100 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002101 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002102
2103 size = sectors >> conf->chunk_shift;
2104 sector_div(size, conf->far_copies);
2105 size = size * raid_disks;
2106 sector_div(size, conf->near_copies);
2107
2108 return size << conf->chunk_shift;
2109}
2110
Trela, Maciejdab8b292010-03-08 16:02:45 +11002111
2112static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002114 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002115 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002117 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Maciej Trelaf73ea872010-06-16 11:46:29 +01002119 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2120 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002121 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2122 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2123 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
NeilBrown2604b702006-01-06 00:20:36 -08002126
Maciej Trelaf73ea872010-06-16 11:46:29 +01002127 nc = mddev->new_layout & 255;
2128 fc = (mddev->new_layout >> 8) & 255;
2129 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002132 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002133 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002134 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 goto out;
2136 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002137
2138 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002139 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002140 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002142
NeilBrown4443ae12006-01-06 00:20:28 -08002143 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002144 GFP_KERNEL);
2145 if (!conf->mirrors)
2146 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002147
2148 conf->tmppage = alloc_page(GFP_KERNEL);
2149 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002150 goto out;
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
NeilBrown64a742b2007-02-28 20:11:18 -08002153 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 conf->near_copies = nc;
2155 conf->far_copies = fc;
2156 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002157 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002158 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2159 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2160
2161 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2162 r10bio_pool_free, conf);
2163 if (!conf->r10bio_pool)
2164 goto out;
2165
Andre Noll58c0fed2009-03-31 14:33:13 +11002166 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002167 sector_div(size, fc);
2168 size = size * conf->raid_disks;
2169 sector_div(size, nc);
2170 /* 'size' is now the number of chunks in the array */
2171 /* calculate "used chunks per device" in 'stride' */
2172 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002173
2174 /* We need to round up when dividing by raid_disks to
2175 * get the stride size.
2176 */
2177 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002178 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002179
2180 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002181
NeilBrownc93983b2006-06-26 00:27:41 -07002182 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002183 stride = 1;
2184 else
NeilBrownc93983b2006-06-26 00:27:41 -07002185 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002186 conf->stride = stride << conf->chunk_shift;
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Neil Browne7e72bf2008-05-14 16:05:54 -07002189 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002190 INIT_LIST_HEAD(&conf->retry_list);
2191
2192 spin_lock_init(&conf->resync_lock);
2193 init_waitqueue_head(&conf->wait_barrier);
2194
2195 conf->thread = md_register_thread(raid10d, mddev, NULL);
2196 if (!conf->thread)
2197 goto out;
2198
Trela, Maciejdab8b292010-03-08 16:02:45 +11002199 conf->mddev = mddev;
2200 return conf;
2201
2202 out:
NeilBrown128595e2010-05-03 14:47:14 +10002203 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002204 mdname(mddev));
2205 if (conf) {
2206 if (conf->r10bio_pool)
2207 mempool_destroy(conf->r10bio_pool);
2208 kfree(conf->mirrors);
2209 safe_put_page(conf->tmppage);
2210 kfree(conf);
2211 }
2212 return ERR_PTR(err);
2213}
2214
2215static int run(mddev_t *mddev)
2216{
2217 conf_t *conf;
2218 int i, disk_idx, chunk_size;
2219 mirror_info_t *disk;
2220 mdk_rdev_t *rdev;
2221 sector_t size;
2222
2223 /*
2224 * copy the already verified devices into our private RAID10
2225 * bookkeeping area. [whatever we allocate in run(),
2226 * should be freed in stop()]
2227 */
2228
2229 if (mddev->private == NULL) {
2230 conf = setup_conf(mddev);
2231 if (IS_ERR(conf))
2232 return PTR_ERR(conf);
2233 mddev->private = conf;
2234 }
2235 conf = mddev->private;
2236 if (!conf)
2237 goto out;
2238
Trela, Maciejdab8b292010-03-08 16:02:45 +11002239 mddev->thread = conf->thread;
2240 conf->thread = NULL;
2241
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002242 chunk_size = mddev->chunk_sectors << 9;
2243 blk_queue_io_min(mddev->queue, chunk_size);
2244 if (conf->raid_disks % conf->near_copies)
2245 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2246 else
2247 blk_queue_io_opt(mddev->queue, chunk_size *
2248 (conf->raid_disks / conf->near_copies));
2249
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002250 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002252 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 || disk_idx < 0)
2254 continue;
2255 disk = conf->mirrors + disk_idx;
2256
2257 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002258 disk_stack_limits(mddev->gendisk, rdev->bdev,
2259 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002261 * violating it, so limit max_segments to 1 lying
2262 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 */
NeilBrown627a2d32010-03-08 16:44:38 +11002264 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2265 blk_queue_max_segments(mddev->queue, 1);
2266 blk_queue_segment_boundary(mddev->queue,
2267 PAGE_CACHE_SIZE - 1);
2268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
NeilBrown6d508242005-09-09 16:24:03 -07002272 /* need to check that every block has at least one working mirror */
2273 if (!enough(conf)) {
NeilBrown128595e2010-05-03 14:47:14 +10002274 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002275 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto out_free_conf;
2277 }
2278
2279 mddev->degraded = 0;
2280 for (i = 0; i < conf->raid_disks; i++) {
2281
2282 disk = conf->mirrors + i;
2283
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002284 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002285 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 disk->head_position = 0;
2287 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002288 if (disk->rdev)
2289 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291 }
2292
Andre Noll8c6ac862009-06-18 08:48:06 +10002293 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002294 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002295 " -- starting background reconstruction\n",
2296 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002298 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002299 mdname(mddev), conf->raid_disks - mddev->degraded,
2300 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 /*
2302 * Ok, everything is just fine now
2303 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002304 mddev->dev_sectors = conf->dev_sectors;
2305 size = raid10_size(mddev, 0, 0);
2306 md_set_array_sectors(mddev, size);
2307 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
NeilBrown0d129222006-10-03 01:15:54 -07002309 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2310 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* Calculate max read-ahead size.
2313 * We need to readahead at least twice a whole stripe....
2314 * maybe...
2315 */
2316 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002317 int stripe = conf->raid_disks *
2318 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 stripe /= conf->near_copies;
2320 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2321 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2322 }
2323
NeilBrown84707f32010-03-16 17:23:35 +11002324 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002326
2327 if (md_integrity_register(mddev))
2328 goto out_free_conf;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
2331
2332out_free_conf:
NeilBrown589a5942010-12-09 17:02:14 +11002333 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (conf->r10bio_pool)
2335 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002336 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002337 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 kfree(conf);
2339 mddev->private = NULL;
2340out:
2341 return -EIO;
2342}
2343
2344static int stop(mddev_t *mddev)
2345{
NeilBrown070ec552009-06-16 16:54:21 +10002346 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
NeilBrown409c57f2009-03-31 14:39:39 +11002348 raise_barrier(conf, 0);
2349 lower_barrier(conf);
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 md_unregister_thread(mddev->thread);
2352 mddev->thread = NULL;
2353 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2354 if (conf->r10bio_pool)
2355 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002356 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 kfree(conf);
2358 mddev->private = NULL;
2359 return 0;
2360}
2361
NeilBrown6cce3b22006-01-06 00:20:16 -08002362static void raid10_quiesce(mddev_t *mddev, int state)
2363{
NeilBrown070ec552009-06-16 16:54:21 +10002364 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002365
2366 switch(state) {
2367 case 1:
2368 raise_barrier(conf, 0);
2369 break;
2370 case 0:
2371 lower_barrier(conf);
2372 break;
2373 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Trela, Maciejdab8b292010-03-08 16:02:45 +11002376static void *raid10_takeover_raid0(mddev_t *mddev)
2377{
2378 mdk_rdev_t *rdev;
2379 conf_t *conf;
2380
2381 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10002382 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2383 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002384 return ERR_PTR(-EINVAL);
2385 }
2386
Trela, Maciejdab8b292010-03-08 16:02:45 +11002387 /* Set new parameters */
2388 mddev->new_level = 10;
2389 /* new layout: far_copies = 1, near_copies = 2 */
2390 mddev->new_layout = (1<<8) + 2;
2391 mddev->new_chunk_sectors = mddev->chunk_sectors;
2392 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002393 mddev->raid_disks *= 2;
2394 /* make sure it will be not marked as dirty */
2395 mddev->recovery_cp = MaxSector;
2396
2397 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002398 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01002399 list_for_each_entry(rdev, &mddev->disks, same_set)
2400 if (rdev->raid_disk >= 0)
2401 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002402 conf->barrier = 1;
2403 }
2404
Trela, Maciejdab8b292010-03-08 16:02:45 +11002405 return conf;
2406}
2407
2408static void *raid10_takeover(mddev_t *mddev)
2409{
2410 struct raid0_private_data *raid0_priv;
2411
2412 /* raid10 can take over:
2413 * raid0 - providing it has only two drives
2414 */
2415 if (mddev->level == 0) {
2416 /* for raid0 takeover only one zone is supported */
2417 raid0_priv = mddev->private;
2418 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10002419 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2420 " with more than one zone.\n",
2421 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002422 return ERR_PTR(-EINVAL);
2423 }
2424 return raid10_takeover_raid0(mddev);
2425 }
2426 return ERR_PTR(-EINVAL);
2427}
2428
NeilBrown2604b702006-01-06 00:20:36 -08002429static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002432 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 .owner = THIS_MODULE,
2434 .make_request = make_request,
2435 .run = run,
2436 .stop = stop,
2437 .status = status,
2438 .error_handler = error,
2439 .hot_add_disk = raid10_add_disk,
2440 .hot_remove_disk= raid10_remove_disk,
2441 .spare_active = raid10_spare_active,
2442 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002443 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002444 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002445 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446};
2447
2448static int __init raid_init(void)
2449{
NeilBrown2604b702006-01-06 00:20:36 -08002450 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
2453static void raid_exit(void)
2454{
NeilBrown2604b702006-01-06 00:20:36 -08002455 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
2458module_init(raid_init);
2459module_exit(raid_exit);
2460MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002461MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002463MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002464MODULE_ALIAS("md-level-10");