blob: 6721cb080354c18d2ae6978e59ec791f374dec64 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * Base on code in raid1.c. See raid1.c for further copyright information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
NeilBrownbff61972009-03-31 14:33:13 +110024#include <linux/seq_file.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100025#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110026#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110027#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110028#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110029#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * RAID10 provides a combination of RAID0 and RAID1 functionality.
33 * The layout of data is defined by
34 * chunk_size
35 * raid_disks
36 * near_copies (stored in low byte of layout)
37 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070038 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 *
40 * The data to be stored is divided into chunks using chunksize.
41 * Each device is divided into far_copies sections.
42 * In each section, chunks are laid out in a style similar to raid0, but
43 * near_copies copies of each chunk is stored (each on a different drive).
44 * The starting device for each section is offset near_copies from the starting
45 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070046 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * drive.
48 * near_copies and far_copies must be at least one, and their product is at most
49 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070050 *
51 * If far_offset is true, then the far_copies are handled a bit differently.
52 * The copies are still in different stripes, but instead of be very far apart
53 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
56/*
57 * Number of guaranteed r10bios in case of extreme VM load:
58 */
59#define NR_RAID10_BIOS 256
60
NeilBrown0a27ec92006-01-06 00:20:13 -080061static void allow_barrier(conf_t *conf);
62static void lower_barrier(conf_t *conf);
63
Al Virodd0fc662005-10-07 07:46:04 +010064static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 conf_t *conf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int size = offsetof(struct r10bio_s, devs[conf->copies]);
68
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
Jens Axboe7eaceac2011-03-10 08:52:07 +010070 return kzalloc(size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73static void r10bio_pool_free(void *r10_bio, void *data)
74{
75 kfree(r10_bio);
76}
77
NeilBrown0310fa22008-08-05 15:54:14 +100078/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100081/* amount of memory to reserve for resync requests */
82#define RESYNC_WINDOW (1024*1024)
83/* maximum number of concurrent requests, memory permitting */
84#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * When performing a resync, we need to read and compare, so
88 * we need as many pages are there are copies.
89 * When performing a recovery, we need 2 bios, one for read,
90 * one for write (we recover only one drive per r10buf)
91 *
92 */
Al Virodd0fc662005-10-07 07:46:04 +010093static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 conf_t *conf = data;
96 struct page *page;
97 r10bio_t *r10_bio;
98 struct bio *bio;
99 int i, j;
100 int nalloc;
101
102 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100103 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
107 nalloc = conf->copies; /* resync */
108 else
109 nalloc = 2; /* recovery */
110
111 /*
112 * Allocate bios.
113 */
114 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100115 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!bio)
117 goto out_free_bio;
118 r10_bio->devs[j].bio = bio;
119 }
120 /*
121 * Allocate RESYNC_PAGES data pages and attach them
122 * where needed.
123 */
124 for (j = 0 ; j < nalloc; j++) {
125 bio = r10_bio->devs[j].bio;
126 for (i = 0; i < RESYNC_PAGES; i++) {
Namhyung Kimc65060a2011-07-18 17:38:49 +1000127 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
128 &conf->mddev->recovery)) {
129 /* we can share bv_page's during recovery */
130 struct bio *rbio = r10_bio->devs[0].bio;
131 page = rbio->bi_io_vec[i].bv_page;
132 get_page(page);
133 } else
134 page = alloc_page(gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (unlikely(!page))
136 goto out_free_pages;
137
138 bio->bi_io_vec[i].bv_page = page;
139 }
140 }
141
142 return r10_bio;
143
144out_free_pages:
145 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800146 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 while (j--)
148 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800149 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 j = -1;
151out_free_bio:
152 while ( ++j < nalloc )
153 bio_put(r10_bio->devs[j].bio);
154 r10bio_pool_free(r10_bio, conf);
155 return NULL;
156}
157
158static void r10buf_pool_free(void *__r10_bio, void *data)
159{
160 int i;
161 conf_t *conf = data;
162 r10bio_t *r10bio = __r10_bio;
163 int j;
164
165 for (j=0; j < conf->copies; j++) {
166 struct bio *bio = r10bio->devs[j].bio;
167 if (bio) {
168 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800169 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 bio->bi_io_vec[i].bv_page = NULL;
171 }
172 bio_put(bio);
173 }
174 }
175 r10bio_pool_free(r10bio, conf);
176}
177
178static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
179{
180 int i;
181
182 for (i = 0; i < conf->copies; i++) {
183 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -0800184 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 bio_put(*bio);
186 *bio = NULL;
187 }
188}
189
Arjan van de Ven858119e2006-01-14 13:20:43 -0800190static void free_r10bio(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
NeilBrown070ec552009-06-16 16:54:21 +1000192 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /*
195 * Wake up any possible resync thread that waits for the device
196 * to go idle.
197 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800198 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 put_all_bios(conf, r10_bio);
201 mempool_free(r10_bio, conf->r10bio_pool);
202}
203
Arjan van de Ven858119e2006-01-14 13:20:43 -0800204static void put_buf(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
NeilBrown070ec552009-06-16 16:54:21 +1000206 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 mempool_free(r10_bio, conf->r10buf_pool);
209
NeilBrown0a27ec92006-01-06 00:20:13 -0800210 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213static void reschedule_retry(r10bio_t *r10_bio)
214{
215 unsigned long flags;
216 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +1000217 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 spin_lock_irqsave(&conf->device_lock, flags);
220 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800221 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 spin_unlock_irqrestore(&conf->device_lock, flags);
223
Arthur Jones388667b2008-07-25 12:03:38 -0700224 /* wake up frozen array... */
225 wake_up(&conf->wait_barrier);
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 md_wakeup_thread(mddev->thread);
228}
229
230/*
231 * raid_end_bio_io() is called when we have finished servicing a mirrored
232 * operation and are ready to return a success/failure code to the buffer
233 * cache layer.
234 */
235static void raid_end_bio_io(r10bio_t *r10_bio)
236{
237 struct bio *bio = r10_bio->master_bio;
238
NeilBrown6712ecf2007-09-27 12:47:43 +0200239 bio_endio(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
241 free_r10bio(r10_bio);
242}
243
244/*
245 * Update disk head position estimator based on IRQ completion info.
246 */
247static inline void update_head_pos(int slot, r10bio_t *r10_bio)
248{
NeilBrown070ec552009-06-16 16:54:21 +1000249 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
252 r10_bio->devs[slot].addr + (r10_bio->sectors);
253}
254
Namhyung Kim778ca012011-07-18 17:38:47 +1000255/*
256 * Find the disk number which triggered given bio
257 */
258static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio, struct bio *bio)
259{
260 int slot;
261
262 for (slot = 0; slot < conf->copies; slot++)
263 if (r10_bio->devs[slot].bio == bio)
264 break;
265
266 BUG_ON(slot == conf->copies);
267 update_head_pos(slot, r10_bio);
268
269 return r10_bio->devs[slot].devnum;
270}
271
NeilBrown6712ecf2007-09-27 12:47:43 +0200272static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100275 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000277 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 slot = r10_bio->read_slot;
281 dev = r10_bio->devs[slot].devnum;
282 /*
283 * this branch is our 'one mirror IO has finished' event handler:
284 */
NeilBrown4443ae12006-01-06 00:20:28 -0800285 update_head_pos(slot, r10_bio);
286
287 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /*
289 * Set R10BIO_Uptodate in our master bio, so that
290 * we will return a good error code to the higher
291 * levels even if IO on some other mirrored buffer fails.
292 *
293 * The 'master' represents the composite IO operation to
294 * user-side. So if something waits for IO, then it will
295 * wait for the 'master' bio.
296 */
297 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 raid_end_bio_io(r10_bio);
NeilBrown7c4e06f2011-05-11 14:53:17 +1000299 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
NeilBrown4443ae12006-01-06 00:20:28 -0800300 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /*
NeilBrown7c4e06f2011-05-11 14:53:17 +1000302 * oops, read error - keep the refcount on the rdev
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304 char b[BDEVNAME_SIZE];
Christian Dietrich8bda4702011-07-27 11:00:36 +1000305 printk_ratelimited(KERN_ERR
306 "md/raid10:%s: %s: rescheduling sector %llu\n",
307 mdname(conf->mddev),
308 bdevname(conf->mirrors[dev].rdev->bdev, b),
309 (unsigned long long)r10_bio->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 reschedule_retry(r10_bio);
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
NeilBrown6712ecf2007-09-27 12:47:43 +0200314static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100317 r10bio_t *r10_bio = bio->bi_private;
Namhyung Kim778ca012011-07-18 17:38:47 +1000318 int dev;
NeilBrown070ec552009-06-16 16:54:21 +1000319 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Namhyung Kim778ca012011-07-18 17:38:47 +1000321 dev = find_bio_disk(conf, r10_bio, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /*
324 * this branch is our 'one mirror IO has finished' event handler:
325 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800326 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800328 /* an I/O failed, we can't clear the bitmap */
329 set_bit(R10BIO_Degraded, &r10_bio->state);
330 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /*
332 * Set R10BIO_Uptodate in our master bio, so that
333 * we will return a good error code for to the higher
334 * levels even if IO on some other mirrored buffer fails.
335 *
336 * The 'master' represents the composite IO operation to
337 * user-side. So if something waits for IO, then it will
338 * wait for the 'master' bio.
339 */
340 set_bit(R10BIO_Uptodate, &r10_bio->state);
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /*
343 *
344 * Let's see if all mirrored write operations have finished
345 * already.
346 */
347 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800348 /* clear the bitmap if all writes complete successfully */
349 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
350 r10_bio->sectors,
351 !test_bit(R10BIO_Degraded, &r10_bio->state),
352 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 md_write_end(r10_bio->mddev);
354 raid_end_bio_io(r10_bio);
355 }
356
357 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360
361/*
362 * RAID10 layout manager
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300363 * As well as the chunksize and raid_disks count, there are two
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * parameters: near_copies and far_copies.
365 * near_copies * far_copies must be <= raid_disks.
366 * Normally one of these will be 1.
367 * If both are 1, we get raid0.
368 * If near_copies == raid_disks, we get raid1.
369 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300370 * Chunks are laid out in raid0 style with near_copies copies of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * first chunk, followed by near_copies copies of the next chunk and
372 * so on.
373 * If far_copies > 1, then after 1/far_copies of the array has been assigned
374 * as described above, we start again with a device offset of near_copies.
375 * So we effectively have another copy of the whole array further down all
376 * the drives, but with blocks on different drives.
377 * With this layout, and block is never stored twice on the one device.
378 *
379 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700380 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
382 * raid10_find_virt does the reverse mapping, from a device and a
383 * sector offset to a virtual address
384 */
385
386static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
387{
388 int n,f;
389 sector_t sector;
390 sector_t chunk;
391 sector_t stripe;
392 int dev;
393
394 int slot = 0;
395
396 /* now calculate first sector/dev */
397 chunk = r10bio->sector >> conf->chunk_shift;
398 sector = r10bio->sector & conf->chunk_mask;
399
400 chunk *= conf->near_copies;
401 stripe = chunk;
402 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700403 if (conf->far_offset)
404 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 sector += stripe << conf->chunk_shift;
407
408 /* and calculate all the others */
409 for (n=0; n < conf->near_copies; n++) {
410 int d = dev;
411 sector_t s = sector;
412 r10bio->devs[slot].addr = sector;
413 r10bio->devs[slot].devnum = d;
414 slot++;
415
416 for (f = 1; f < conf->far_copies; f++) {
417 d += conf->near_copies;
418 if (d >= conf->raid_disks)
419 d -= conf->raid_disks;
420 s += conf->stride;
421 r10bio->devs[slot].devnum = d;
422 r10bio->devs[slot].addr = s;
423 slot++;
424 }
425 dev++;
426 if (dev >= conf->raid_disks) {
427 dev = 0;
428 sector += (conf->chunk_mask + 1);
429 }
430 }
431 BUG_ON(slot != conf->copies);
432}
433
434static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
435{
436 sector_t offset, chunk, vchunk;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700439 if (conf->far_offset) {
440 int fc;
441 chunk = sector >> conf->chunk_shift;
442 fc = sector_div(chunk, conf->far_copies);
443 dev -= fc * conf->near_copies;
444 if (dev < 0)
445 dev += conf->raid_disks;
446 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800447 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700448 sector -= conf->stride;
449 if (dev < conf->near_copies)
450 dev += conf->raid_disks - conf->near_copies;
451 else
452 dev -= conf->near_copies;
453 }
454 chunk = sector >> conf->chunk_shift;
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 vchunk = chunk * conf->raid_disks + dev;
457 sector_div(vchunk, conf->near_copies);
458 return (vchunk << conf->chunk_shift) + offset;
459}
460
461/**
462 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
463 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200464 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * @biovec: the request that could be merged to it.
466 *
467 * Return amount of bytes we can accept at this offset
468 * If near_copies == raid_disk, there are no striping issues,
469 * but in that case, the function isn't called at all.
470 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200471static int raid10_mergeable_bvec(struct request_queue *q,
472 struct bvec_merge_data *bvm,
473 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200476 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000478 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200479 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
482 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200483 if (max <= biovec->bv_len && bio_sectors == 0)
484 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 else
486 return max;
487}
488
489/*
490 * This routine returns the disk from which the requested read should
491 * be done. There is a per-array 'next expected sequential IO' sector
492 * number - if this matches on the next IO then we use the last disk.
493 * There is also a per-disk 'last know head position' sector that is
494 * maintained from IRQ contexts, both the normal and the resync IO
495 * completion handlers update this position correctly. If there is no
496 * perfect sequential match then we pick the disk whose head is closest.
497 *
498 * If there are 2 mirrors in the same 2 devices, performance degrades
499 * because position is mirror, not device based.
500 *
501 * The rdev for the device selected will have nr_pending incremented.
502 */
503
504/*
505 * FIXME: possibly should rethink readbalancing and do it differently
506 * depending on near_copies / far_copies geometry.
507 */
508static int read_balance(conf_t *conf, r10bio_t *r10_bio)
509{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000510 const sector_t this_sector = r10_bio->sector;
NeilBrown56d99122011-05-11 14:27:03 +1000511 int disk, slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 const int sectors = r10_bio->sectors;
NeilBrown56d99122011-05-11 14:27:03 +1000513 sector_t new_distance, best_dist;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800514 mdk_rdev_t *rdev;
NeilBrown56d99122011-05-11 14:27:03 +1000515 int do_balance;
516 int best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 raid10_find_phys(conf, r10_bio);
519 rcu_read_lock();
NeilBrown56d99122011-05-11 14:27:03 +1000520retry:
521 best_slot = -1;
522 best_dist = MaxSector;
523 do_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /*
525 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800526 * device if no resync is going on (recovery is ok), or below
527 * the resync window. We take the first readable disk when
528 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
530 if (conf->mddev->recovery_cp < MaxSector
NeilBrown56d99122011-05-11 14:27:03 +1000531 && (this_sector + sectors >= conf->next_resync))
532 do_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
NeilBrown56d99122011-05-11 14:27:03 +1000534 for (slot = 0; slot < conf->copies ; slot++) {
535 if (r10_bio->devs[slot].bio == IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 continue;
NeilBrown56d99122011-05-11 14:27:03 +1000537 disk = r10_bio->devs[slot].devnum;
538 rdev = rcu_dereference(conf->mirrors[disk].rdev);
539 if (rdev == NULL)
540 continue;
541 if (!test_bit(In_sync, &rdev->flags))
542 continue;
543
544 if (!do_balance)
545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
NeilBrown22dfdf52005-11-28 13:44:09 -0800547 /* This optimisation is debatable, and completely destroys
548 * sequential read speed for 'far copies' arrays. So only
549 * keep it for 'near' arrays, and review those later.
550 */
NeilBrown56d99122011-05-11 14:27:03 +1000551 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 break;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800553
554 /* for far > 1 always use the lowest address */
555 if (conf->far_copies > 1)
NeilBrown56d99122011-05-11 14:27:03 +1000556 new_distance = r10_bio->devs[slot].addr;
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800557 else
NeilBrown56d99122011-05-11 14:27:03 +1000558 new_distance = abs(r10_bio->devs[slot].addr -
559 conf->mirrors[disk].head_position);
560 if (new_distance < best_dist) {
561 best_dist = new_distance;
562 best_slot = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 }
NeilBrown56d99122011-05-11 14:27:03 +1000565 if (slot == conf->copies)
566 slot = best_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
NeilBrown56d99122011-05-11 14:27:03 +1000568 if (slot >= 0) {
569 disk = r10_bio->devs[slot].devnum;
570 rdev = rcu_dereference(conf->mirrors[disk].rdev);
571 if (!rdev)
572 goto retry;
573 atomic_inc(&rdev->nr_pending);
574 if (test_bit(Faulty, &rdev->flags)) {
575 /* Cannot risk returning a device that failed
576 * before we inc'ed nr_pending
577 */
578 rdev_dec_pending(rdev, conf->mddev);
579 goto retry;
580 }
581 r10_bio->read_slot = slot;
582 } else
NeilBrown29fc7e32006-02-03 03:03:41 -0800583 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 rcu_read_unlock();
585
586 return disk;
587}
588
NeilBrown0d129222006-10-03 01:15:54 -0700589static int raid10_congested(void *data, int bits)
590{
591 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000592 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700593 int i, ret = 0;
594
NeilBrown3fa841d2009-09-23 18:10:29 +1000595 if (mddev_congested(mddev, bits))
596 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700597 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100598 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700599 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
600 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200601 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700602
603 ret |= bdi_congested(&q->backing_dev_info, bits);
604 }
605 }
606 rcu_read_unlock();
607 return ret;
608}
609
Jens Axboe7eaceac2011-03-10 08:52:07 +0100610static void flush_pending_writes(conf_t *conf)
NeilBrowna35e63e2008-03-04 14:29:29 -0800611{
612 /* Any writes that have been queued but are awaiting
613 * bitmap updates get flushed here.
NeilBrowna35e63e2008-03-04 14:29:29 -0800614 */
NeilBrowna35e63e2008-03-04 14:29:29 -0800615 spin_lock_irq(&conf->device_lock);
616
617 if (conf->pending_bio_list.head) {
618 struct bio *bio;
619 bio = bio_list_get(&conf->pending_bio_list);
NeilBrowna35e63e2008-03-04 14:29:29 -0800620 spin_unlock_irq(&conf->device_lock);
621 /* flush any pending bitmap writes to disk
622 * before proceeding w/ I/O */
623 bitmap_unplug(conf->mddev->bitmap);
624
625 while (bio) { /* submit pending writes */
626 struct bio *next = bio->bi_next;
627 bio->bi_next = NULL;
628 generic_make_request(bio);
629 bio = next;
630 }
NeilBrowna35e63e2008-03-04 14:29:29 -0800631 } else
632 spin_unlock_irq(&conf->device_lock);
NeilBrowna35e63e2008-03-04 14:29:29 -0800633}
Jens Axboe7eaceac2011-03-10 08:52:07 +0100634
NeilBrown0a27ec92006-01-06 00:20:13 -0800635/* Barriers....
636 * Sometimes we need to suspend IO while we do something else,
637 * either some resync/recovery, or reconfigure the array.
638 * To do this we raise a 'barrier'.
639 * The 'barrier' is a counter that can be raised multiple times
640 * to count how many activities are happening which preclude
641 * normal IO.
642 * We can only raise the barrier if there is no pending IO.
643 * i.e. if nr_pending == 0.
644 * We choose only to raise the barrier if no-one is waiting for the
645 * barrier to go down. This means that as soon as an IO request
646 * is ready, no other operations which require a barrier will start
647 * until the IO request has had a chance.
648 *
649 * So: regular IO calls 'wait_barrier'. When that returns there
650 * is no backgroup IO happening, It must arrange to call
651 * allow_barrier when it has finished its IO.
652 * backgroup IO calls must call raise_barrier. Once that returns
653 * there is no normal IO happeing. It must arrange to call
654 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
NeilBrown6cce3b22006-01-06 00:20:16 -0800657static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
NeilBrown6cce3b22006-01-06 00:20:16 -0800659 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
NeilBrown6cce3b22006-01-06 00:20:16 -0800662 /* Wait until no block IO is waiting (unless 'force') */
663 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrownc3b328a2011-04-18 18:25:43 +1000664 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800665
666 /* block any new IO from starting */
667 conf->barrier++;
668
NeilBrownc3b328a2011-04-18 18:25:43 +1000669 /* Now wait for all pending IO to complete */
NeilBrown0a27ec92006-01-06 00:20:13 -0800670 wait_event_lock_irq(conf->wait_barrier,
671 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
NeilBrownc3b328a2011-04-18 18:25:43 +1000672 conf->resync_lock, );
NeilBrown0a27ec92006-01-06 00:20:13 -0800673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 spin_unlock_irq(&conf->resync_lock);
675}
676
NeilBrown0a27ec92006-01-06 00:20:13 -0800677static void lower_barrier(conf_t *conf)
678{
679 unsigned long flags;
680 spin_lock_irqsave(&conf->resync_lock, flags);
681 conf->barrier--;
682 spin_unlock_irqrestore(&conf->resync_lock, flags);
683 wake_up(&conf->wait_barrier);
684}
685
686static void wait_barrier(conf_t *conf)
687{
688 spin_lock_irq(&conf->resync_lock);
689 if (conf->barrier) {
690 conf->nr_waiting++;
691 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
692 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000693 );
NeilBrown0a27ec92006-01-06 00:20:13 -0800694 conf->nr_waiting--;
695 }
696 conf->nr_pending++;
697 spin_unlock_irq(&conf->resync_lock);
698}
699
700static void allow_barrier(conf_t *conf)
701{
702 unsigned long flags;
703 spin_lock_irqsave(&conf->resync_lock, flags);
704 conf->nr_pending--;
705 spin_unlock_irqrestore(&conf->resync_lock, flags);
706 wake_up(&conf->wait_barrier);
707}
708
NeilBrown4443ae12006-01-06 00:20:28 -0800709static void freeze_array(conf_t *conf)
710{
711 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800712 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800713 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800714 * wait until nr_pending match nr_queued+1
715 * This is called in the context of one normal IO request
716 * that has failed. Thus any sync request that might be pending
717 * will be blocked by nr_pending, and we need to wait for
718 * pending IO requests to complete or be queued for re-try.
719 * Thus the number queued (nr_queued) plus this request (1)
720 * must match the number of pending IOs (nr_pending) before
721 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800722 */
723 spin_lock_irq(&conf->resync_lock);
724 conf->barrier++;
725 conf->nr_waiting++;
726 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800727 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800728 conf->resync_lock,
NeilBrownc3b328a2011-04-18 18:25:43 +1000729 flush_pending_writes(conf));
730
NeilBrown4443ae12006-01-06 00:20:28 -0800731 spin_unlock_irq(&conf->resync_lock);
732}
733
734static void unfreeze_array(conf_t *conf)
735{
736 /* reverse the effect of the freeze */
737 spin_lock_irq(&conf->resync_lock);
738 conf->barrier--;
739 conf->nr_waiting--;
740 wake_up(&conf->wait_barrier);
741 spin_unlock_irq(&conf->resync_lock);
742}
743
NeilBrown21a52c62010-04-01 15:02:13 +1100744static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
NeilBrown070ec552009-06-16 16:54:21 +1000746 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 mirror_info_t *mirror;
748 r10bio_t *r10_bio;
749 struct bio *read_bio;
750 int i;
751 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100752 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000753 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200754 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800755 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700756 mdk_rdev_t *blocked_rdev;
NeilBrownc3b328a2011-04-18 18:25:43 +1000757 int plugged;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Tejun Heoe9c74692010-09-03 11:56:18 +0200759 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
760 md_flush_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700761 return 0;
762 }
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* If this request crosses a chunk boundary, we need to
765 * split it. This will only happen for 1 PAGE (or less) requests.
766 */
767 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
768 > chunk_sects &&
769 conf->near_copies < conf->raid_disks)) {
770 struct bio_pair *bp;
771 /* Sanity check -- queue functions should prevent this happening */
772 if (bio->bi_vcnt != 1 ||
773 bio->bi_idx != 0)
774 goto bad_map;
775 /* This is a one page bio that upper layers
776 * refuse to split for us, so we need to split it.
777 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200778 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000780
781 /* Each of these 'make_request' calls will call 'wait_barrier'.
782 * If the first succeeds but the second blocks due to the resync
783 * thread raising the barrier, we will deadlock because the
784 * IO to the underlying device will be queued in generic_make_request
785 * and will never complete, so will never reduce nr_pending.
786 * So increment nr_waiting here so no new raise_barriers will
787 * succeed, and so the second wait_barrier cannot block.
788 */
789 spin_lock_irq(&conf->resync_lock);
790 conf->nr_waiting++;
791 spin_unlock_irq(&conf->resync_lock);
792
NeilBrown21a52c62010-04-01 15:02:13 +1100793 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100795 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 generic_make_request(&bp->bio2);
797
NeilBrown51e9ac72010-08-07 21:17:00 +1000798 spin_lock_irq(&conf->resync_lock);
799 conf->nr_waiting--;
800 wake_up(&conf->wait_barrier);
801 spin_unlock_irq(&conf->resync_lock);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 bio_pair_release(bp);
804 return 0;
805 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000806 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
807 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
809
NeilBrown6712ecf2007-09-27 12:47:43 +0200810 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return 0;
812 }
813
NeilBrown3d310eb2005-06-21 17:17:26 -0700814 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /*
817 * Register the new request and wait if the reconstruction
818 * thread has put up a bar for new requests.
819 * Continue immediately if no resync is active currently.
820 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800821 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
824
825 r10_bio->master_bio = bio;
826 r10_bio->sectors = bio->bi_size >> 9;
827
828 r10_bio->mddev = mddev;
829 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800830 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jens Axboea3623572005-11-01 09:26:16 +0100832 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * read balancing logic:
835 */
836 int disk = read_balance(conf, r10_bio);
837 int slot = r10_bio->read_slot;
838 if (disk < 0) {
839 raid_end_bio_io(r10_bio);
840 return 0;
841 }
842 mirror = conf->mirrors + disk;
843
NeilBrowna167f662010-10-26 18:31:13 +1100844 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 r10_bio->devs[slot].bio = read_bio;
847
848 read_bio->bi_sector = r10_bio->devs[slot].addr +
849 mirror->rdev->data_offset;
850 read_bio->bi_bdev = mirror->rdev->bdev;
851 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200852 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 read_bio->bi_private = r10_bio;
854
855 generic_make_request(read_bio);
856 return 0;
857 }
858
859 /*
860 * WRITE:
861 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700862 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * inc refcount on their rdev. Record them by setting
864 * bios[x] to bio
865 */
NeilBrownc3b328a2011-04-18 18:25:43 +1000866 plugged = mddev_check_plugged(mddev);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700869 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700870 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 rcu_read_lock();
872 for (i = 0; i < conf->copies; i++) {
873 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800874 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700875 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
876 atomic_inc(&rdev->nr_pending);
877 blocked_rdev = rdev;
878 break;
879 }
880 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800881 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800883 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800885 set_bit(R10BIO_Degraded, &r10_bio->state);
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 rcu_read_unlock();
889
Dan Williams6bfe0b42008-04-30 00:52:32 -0700890 if (unlikely(blocked_rdev)) {
891 /* Have to wait for this device to get unblocked, then retry */
892 int j;
893 int d;
894
895 for (j = 0; j < i; j++)
896 if (r10_bio->devs[j].bio) {
897 d = r10_bio->devs[j].devnum;
898 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
899 }
900 allow_barrier(conf);
901 md_wait_for_blocked_rdev(blocked_rdev, mddev);
902 wait_barrier(conf);
903 goto retry_write;
904 }
905
NeilBrown4e780642010-10-19 12:54:01 +1100906 atomic_set(&r10_bio->remaining, 1);
907 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 for (i = 0; i < conf->copies; i++) {
910 struct bio *mbio;
911 int d = r10_bio->devs[i].devnum;
912 if (!r10_bio->devs[i].bio)
913 continue;
914
NeilBrowna167f662010-10-26 18:31:13 +1100915 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 r10_bio->devs[i].bio = mbio;
917
918 mbio->bi_sector = r10_bio->devs[i].addr+
919 conf->mirrors[d].rdev->data_offset;
920 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
921 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +0200922 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 mbio->bi_private = r10_bio;
924
925 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +1100926 spin_lock_irqsave(&conf->device_lock, flags);
927 bio_list_add(&conf->pending_bio_list, mbio);
NeilBrown4e780642010-10-19 12:54:01 +1100928 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
NeilBrown4e780642010-10-19 12:54:01 +1100931 if (atomic_dec_and_test(&r10_bio->remaining)) {
932 /* This matches the end of raid10_end_write_request() */
933 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
934 r10_bio->sectors,
935 !test_bit(R10BIO_Degraded, &r10_bio->state),
936 0);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700937 md_write_end(mddev);
938 raid_end_bio_io(r10_bio);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700939 }
940
NeilBrowna35e63e2008-03-04 14:29:29 -0800941 /* In case raid10d snuck in to freeze_array */
942 wake_up(&conf->wait_barrier);
943
NeilBrownc3b328a2011-04-18 18:25:43 +1000944 if (do_sync || !mddev->bitmap || !plugged)
Lars Ellenberge3881a62007-01-10 23:15:37 -0800945 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947}
948
949static void status(struct seq_file *seq, mddev_t *mddev)
950{
NeilBrown070ec552009-06-16 16:54:21 +1000951 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int i;
953
954 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +1000955 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (conf->near_copies > 1)
957 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700958 if (conf->far_copies > 1) {
959 if (conf->far_offset)
960 seq_printf(seq, " %d offset-copies", conf->far_copies);
961 else
962 seq_printf(seq, " %d far-copies", conf->far_copies);
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700965 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 for (i = 0; i < conf->raid_disks; i++)
967 seq_printf(seq, "%s",
968 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800969 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 seq_printf(seq, "]");
971}
972
NeilBrown700c7212011-07-27 11:00:36 +1000973/* check if there are enough drives for
974 * every block to appear on atleast one.
975 * Don't consider the device numbered 'ignore'
976 * as we might be about to remove it.
977 */
978static int enough(conf_t *conf, int ignore)
979{
980 int first = 0;
981
982 do {
983 int n = conf->copies;
984 int cnt = 0;
985 while (n--) {
986 if (conf->mirrors[first].rdev &&
987 first != ignore)
988 cnt++;
989 first = (first+1) % conf->raid_disks;
990 }
991 if (cnt == 0)
992 return 0;
993 } while (first != 0);
994 return 1;
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997static void error(mddev_t *mddev, mdk_rdev_t *rdev)
998{
999 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001000 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
1003 * If it is not operational, then we have already marked it as dead
1004 * else if it is the last working disks, ignore the error, let the
1005 * next level up know.
1006 * else mark the drive as failed
1007 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001008 if (test_bit(In_sync, &rdev->flags)
NeilBrown700c7212011-07-27 11:00:36 +10001009 && !enough(conf, rdev->raid_disk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /*
1011 * Don't fail the drive, just return an IO error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
1013 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001014 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1015 unsigned long flags;
1016 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001018 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
1020 * if recovery is running, make sure it aborts.
1021 */
NeilBrowndfc70642008-05-23 13:04:39 -07001022 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001024 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001025 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001026 printk(KERN_ALERT
1027 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1028 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001029 mdname(mddev), bdevname(rdev->bdev, b),
1030 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static void print_conf(conf_t *conf)
1034{
1035 int i;
1036 mirror_info_t *tmp;
1037
NeilBrown128595e2010-05-03 14:47:14 +10001038 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001040 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return;
1042 }
NeilBrown128595e2010-05-03 14:47:14 +10001043 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 conf->raid_disks);
1045
1046 for (i = 0; i < conf->raid_disks; i++) {
1047 char b[BDEVNAME_SIZE];
1048 tmp = conf->mirrors + i;
1049 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001050 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001051 i, !test_bit(In_sync, &tmp->rdev->flags),
1052 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 bdevname(tmp->rdev->bdev,b));
1054 }
1055}
1056
1057static void close_sync(conf_t *conf)
1058{
NeilBrown0a27ec92006-01-06 00:20:13 -08001059 wait_barrier(conf);
1060 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 mempool_destroy(conf->r10buf_pool);
1063 conf->r10buf_pool = NULL;
1064}
1065
1066static int raid10_spare_active(mddev_t *mddev)
1067{
1068 int i;
1069 conf_t *conf = mddev->private;
1070 mirror_info_t *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001071 int count = 0;
1072 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /*
1075 * Find all non-in_sync disks within the RAID10 configuration
1076 * and mark them in_sync
1077 */
1078 for (i = 0; i < conf->raid_disks; i++) {
1079 tmp = conf->mirrors + i;
1080 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001081 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001082 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001083 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001084 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 }
NeilBrown6b965622010-08-18 11:56:59 +10001087 spin_lock_irqsave(&conf->device_lock, flags);
1088 mddev->degraded -= count;
1089 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001092 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095
1096static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1097{
1098 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001099 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int mirror;
Neil Brown6c2fce22008-06-28 08:31:31 +10001101 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001102 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (mddev->recovery_cp < MaxSector)
1105 /* only hot-add to in-sync arrays, as recovery is
1106 * very different from resync
1107 */
Neil Brown199050e2008-06-28 08:31:33 +10001108 return -EBUSY;
NeilBrown700c7212011-07-27 11:00:36 +10001109 if (!enough(conf, -1))
Neil Brown199050e2008-06-28 08:31:33 +10001110 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
NeilBrowna53a6c82008-11-06 17:28:20 +11001112 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001113 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Namhyung Kim2c4193d2011-07-18 17:38:43 +10001115 if (rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001116 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1117 mirror = rdev->saved_raid_disk;
1118 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001119 mirror = first;
NeilBrown2bb77732011-07-27 11:00:36 +10001120 for ( ; mirror <= last ; mirror++) {
1121 mirror_info_t *p = &conf->mirrors[mirror];
1122 if (p->recovery_disabled == mddev->recovery_disabled)
1123 continue;
1124 if (!p->rdev)
1125 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
NeilBrown2bb77732011-07-27 11:00:36 +10001127 disk_stack_limits(mddev->gendisk, rdev->bdev,
1128 rdev->data_offset << 9);
1129 /* as we don't honour merge_bvec_fn, we must
1130 * never risk violating it, so limit
1131 * ->max_segments to one lying with a single
1132 * page, as a one page request is never in
1133 * violation.
1134 */
1135 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1136 blk_queue_max_segments(mddev->queue, 1);
1137 blk_queue_segment_boundary(mddev->queue,
1138 PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
NeilBrown2bb77732011-07-27 11:00:36 +10001141 p->head_position = 0;
1142 rdev->raid_disk = mirror;
1143 err = 0;
1144 if (rdev->saved_raid_disk != mirror)
1145 conf->fullsync = 1;
1146 rcu_assign_pointer(p->rdev, rdev);
1147 break;
1148 }
1149
Andre Nollac5e7112009-08-03 10:59:47 +10001150 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155static int raid10_remove_disk(mddev_t *mddev, int number)
1156{
1157 conf_t *conf = mddev->private;
1158 int err = 0;
1159 mdk_rdev_t *rdev;
1160 mirror_info_t *p = conf->mirrors+ number;
1161
1162 print_conf(conf);
1163 rdev = p->rdev;
1164 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001165 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 atomic_read(&rdev->nr_pending)) {
1167 err = -EBUSY;
1168 goto abort;
1169 }
NeilBrowndfc70642008-05-23 13:04:39 -07001170 /* Only remove faulty devices in recovery
1171 * is not possible.
1172 */
1173 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown2bb77732011-07-27 11:00:36 +10001174 mddev->recovery_disabled != p->recovery_disabled &&
NeilBrown700c7212011-07-27 11:00:36 +10001175 enough(conf, -1)) {
NeilBrowndfc70642008-05-23 13:04:39 -07001176 err = -EBUSY;
1177 goto abort;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001180 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (atomic_read(&rdev->nr_pending)) {
1182 /* lost the race, try later */
1183 err = -EBUSY;
1184 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001185 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001187 err = md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189abort:
1190
1191 print_conf(conf);
1192 return err;
1193}
1194
1195
NeilBrown6712ecf2007-09-27 12:47:43 +02001196static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001198 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001199 conf_t *conf = r10_bio->mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001200 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Namhyung Kim778ca012011-07-18 17:38:47 +10001202 d = find_bio_disk(conf, r10_bio, bio);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001203
1204 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1205 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001206 else {
1207 atomic_add(r10_bio->sectors,
1208 &conf->mirrors[d].rdev->corrected_errors);
1209 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1210 md_error(r10_bio->mddev,
1211 conf->mirrors[d].rdev);
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /* for reconstruct, we always reschedule after a read.
1215 * for resync, only after all reads
1216 */
NeilBrown73d5c382009-02-25 13:18:47 +11001217 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1219 atomic_dec_and_test(&r10_bio->remaining)) {
1220 /* we have read all the blocks,
1221 * do the comparison in process context in raid10d
1222 */
1223 reschedule_retry(r10_bio);
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
NeilBrown6712ecf2007-09-27 12:47:43 +02001227static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001230 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001232 conf_t *conf = mddev->private;
Namhyung Kim778ca012011-07-18 17:38:47 +10001233 int d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Namhyung Kim778ca012011-07-18 17:38:47 +10001235 d = find_bio_disk(conf, r10_bio, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 if (!uptodate)
1238 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001239
NeilBrown73d5c382009-02-25 13:18:47 +11001240 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 while (atomic_dec_and_test(&r10_bio->remaining)) {
1242 if (r10_bio->master_bio == NULL) {
1243 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001244 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001246 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248 } else {
1249 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1250 put_buf(r10_bio);
1251 r10_bio = r10_bio2;
1252 }
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256/*
1257 * Note: sync and recover and handled very differently for raid10
1258 * This code is for resync.
1259 * For resync, we read through virtual addresses and read all blocks.
1260 * If there is any error, we schedule a write. The lowest numbered
1261 * drive is authoritative.
1262 * However requests come for physical address, so we need to map.
1263 * For every physical address there are raid_disks/copies virtual addresses,
1264 * which is always are least one, but is not necessarly an integer.
1265 * This means that a physical address can span multiple chunks, so we may
1266 * have to submit multiple io requests for a single sync request.
1267 */
1268/*
1269 * We check if all blocks are in-sync and only write to blocks that
1270 * aren't in sync
1271 */
1272static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1273{
NeilBrown070ec552009-06-16 16:54:21 +10001274 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int i, first;
1276 struct bio *tbio, *fbio;
1277
1278 atomic_set(&r10_bio->remaining, 1);
1279
1280 /* find the first device with a block */
1281 for (i=0; i<conf->copies; i++)
1282 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1283 break;
1284
1285 if (i == conf->copies)
1286 goto done;
1287
1288 first = i;
1289 fbio = r10_bio->devs[i].bio;
1290
1291 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001292 for (i=0 ; i < conf->copies ; i++) {
1293 int j, d;
1294 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001297
1298 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001300 if (i == first)
1301 continue;
1302 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1303 /* We know that the bi_io_vec layout is the same for
1304 * both 'first' and 'i', so we just compare them.
1305 * All vec entries are PAGE_SIZE;
1306 */
1307 for (j = 0; j < vcnt; j++)
1308 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1309 page_address(tbio->bi_io_vec[j].bv_page),
1310 PAGE_SIZE))
1311 break;
1312 if (j == vcnt)
1313 continue;
1314 mddev->resync_mismatches += r10_bio->sectors;
1315 }
NeilBrown18f08812006-01-06 00:20:25 -08001316 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1317 /* Don't fix anything. */
1318 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* Ok, we need to write this bio
1320 * First we need to fixup bv_offset, bv_len and
1321 * bi_vecs, as the read request might have corrupted these
1322 */
1323 tbio->bi_vcnt = vcnt;
1324 tbio->bi_size = r10_bio->sectors << 9;
1325 tbio->bi_idx = 0;
1326 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1328 tbio->bi_flags |= 1 << BIO_UPTODATE;
1329 tbio->bi_next = NULL;
1330 tbio->bi_rw = WRITE;
1331 tbio->bi_private = r10_bio;
1332 tbio->bi_sector = r10_bio->devs[i].addr;
1333
1334 for (j=0; j < vcnt ; j++) {
1335 tbio->bi_io_vec[j].bv_offset = 0;
1336 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1337
1338 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1339 page_address(fbio->bi_io_vec[j].bv_page),
1340 PAGE_SIZE);
1341 }
1342 tbio->bi_end_io = end_sync_write;
1343
1344 d = r10_bio->devs[i].devnum;
1345 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1346 atomic_inc(&r10_bio->remaining);
1347 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1348
1349 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1350 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1351 generic_make_request(tbio);
1352 }
1353
1354done:
1355 if (atomic_dec_and_test(&r10_bio->remaining)) {
1356 md_done_sync(mddev, r10_bio->sectors, 1);
1357 put_buf(r10_bio);
1358 }
1359}
1360
1361/*
1362 * Now for the recovery code.
1363 * Recovery happens across physical sectors.
1364 * We recover all non-is_sync drives by finding the virtual address of
1365 * each, and then choose a working drive that also has that virt address.
1366 * There is a separate r10_bio for each non-in_sync drive.
1367 * Only the first two slots are in use. The first for reading,
1368 * The second for writing.
1369 *
1370 */
1371
1372static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1373{
NeilBrown070ec552009-06-16 16:54:21 +10001374 conf_t *conf = mddev->private;
Namhyung Kimc65060a2011-07-18 17:38:49 +10001375 int d;
1376 struct bio *wbio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Namhyung Kimc65060a2011-07-18 17:38:49 +10001378 /*
1379 * share the pages with the first bio
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * and submit the write request
1381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 wbio = r10_bio->devs[1].bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 d = r10_bio->devs[1].devnum;
1384
1385 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1386 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001387 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1388 generic_make_request(wbio);
NeilBrown2bb77732011-07-27 11:00:36 +10001389 else {
1390 printk(KERN_NOTICE
1391 "md/raid10:%s: recovery aborted due to read error\n",
1392 mdname(mddev));
1393 conf->mirrors[d].recovery_disabled = mddev->recovery_disabled;
1394 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1395 bio_endio(wbio, 0);
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399
1400/*
Robert Becker1e509152009-12-14 12:49:58 +11001401 * Used by fix_read_error() to decay the per rdev read_errors.
1402 * We halve the read error count for every hour that has elapsed
1403 * since the last recorded read error.
1404 *
1405 */
1406static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1407{
1408 struct timespec cur_time_mon;
1409 unsigned long hours_since_last;
1410 unsigned int read_errors = atomic_read(&rdev->read_errors);
1411
1412 ktime_get_ts(&cur_time_mon);
1413
1414 if (rdev->last_read_error.tv_sec == 0 &&
1415 rdev->last_read_error.tv_nsec == 0) {
1416 /* first time we've seen a read error */
1417 rdev->last_read_error = cur_time_mon;
1418 return;
1419 }
1420
1421 hours_since_last = (cur_time_mon.tv_sec -
1422 rdev->last_read_error.tv_sec) / 3600;
1423
1424 rdev->last_read_error = cur_time_mon;
1425
1426 /*
1427 * if hours_since_last is > the number of bits in read_errors
1428 * just set read errors to 0. We do this to avoid
1429 * overflowing the shift of read_errors by hours_since_last.
1430 */
1431 if (hours_since_last >= 8 * sizeof(read_errors))
1432 atomic_set(&rdev->read_errors, 0);
1433 else
1434 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1435}
1436
1437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * This is a kernel thread which:
1439 *
1440 * 1. Retries failed read operations on working mirrors.
1441 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001442 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444
NeilBrown6814d532006-10-03 01:15:45 -07001445static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1446{
1447 int sect = 0; /* Offset from r10_bio->sector */
1448 int sectors = r10_bio->sectors;
1449 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001450 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001451 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001452
NeilBrown7c4e06f2011-05-11 14:53:17 +10001453 /* still own a reference to this rdev, so it cannot
1454 * have been cleared recently.
1455 */
1456 rdev = conf->mirrors[d].rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001457
NeilBrown7c4e06f2011-05-11 14:53:17 +10001458 if (test_bit(Faulty, &rdev->flags))
1459 /* drive has already been failed, just ignore any
1460 more fix_read_error() attempts */
1461 return;
1462
1463 check_decay_read_errors(mddev, rdev);
1464 atomic_inc(&rdev->read_errors);
1465 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1466 char b[BDEVNAME_SIZE];
Robert Becker1e509152009-12-14 12:49:58 +11001467 bdevname(rdev->bdev, b);
1468
NeilBrown7c4e06f2011-05-11 14:53:17 +10001469 printk(KERN_NOTICE
1470 "md/raid10:%s: %s: Raid device exceeded "
1471 "read_error threshold [cur %d:max %d]\n",
1472 mdname(mddev), b,
1473 atomic_read(&rdev->read_errors), max_read_errors);
1474 printk(KERN_NOTICE
1475 "md/raid10:%s: %s: Failing raid device\n",
1476 mdname(mddev), b);
1477 md_error(mddev, conf->mirrors[d].rdev);
1478 return;
Robert Becker1e509152009-12-14 12:49:58 +11001479 }
Robert Becker1e509152009-12-14 12:49:58 +11001480
NeilBrown6814d532006-10-03 01:15:45 -07001481 while(sectors) {
1482 int s = sectors;
1483 int sl = r10_bio->read_slot;
1484 int success = 0;
1485 int start;
1486
1487 if (s > (PAGE_SIZE>>9))
1488 s = PAGE_SIZE >> 9;
1489
1490 rcu_read_lock();
1491 do {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001492 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001493 rdev = rcu_dereference(conf->mirrors[d].rdev);
1494 if (rdev &&
1495 test_bit(In_sync, &rdev->flags)) {
1496 atomic_inc(&rdev->nr_pending);
1497 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001498 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001499 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001500 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001501 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001502 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001503 rdev_dec_pending(rdev, mddev);
1504 rcu_read_lock();
1505 if (success)
1506 break;
1507 }
1508 sl++;
1509 if (sl == conf->copies)
1510 sl = 0;
1511 } while (!success && sl != r10_bio->read_slot);
1512 rcu_read_unlock();
1513
1514 if (!success) {
1515 /* Cannot read from anywhere -- bye bye array */
1516 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1517 md_error(mddev, conf->mirrors[dn].rdev);
1518 break;
1519 }
1520
1521 start = sl;
1522 /* write it back and re-read */
1523 rcu_read_lock();
1524 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001525 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001526
NeilBrown6814d532006-10-03 01:15:45 -07001527 if (sl==0)
1528 sl = conf->copies;
1529 sl--;
1530 d = r10_bio->devs[sl].devnum;
1531 rdev = rcu_dereference(conf->mirrors[d].rdev);
1532 if (rdev &&
1533 test_bit(In_sync, &rdev->flags)) {
1534 atomic_inc(&rdev->nr_pending);
1535 rcu_read_unlock();
1536 atomic_add(s, &rdev->corrected_errors);
NeilBrown2b193362010-10-27 15:16:40 +11001537 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001538 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001539 sect,
1540 s<<9, conf->tmppage, WRITE, false)
Robert Becker67b8dc42009-12-14 12:49:57 +11001541 == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001542 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001543 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001544 "md/raid10:%s: read correction "
Robert Becker67b8dc42009-12-14 12:49:57 +11001545 "write failed"
1546 " (%d sectors at %llu on %s)\n",
1547 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001548 (unsigned long long)(
1549 sect + rdev->data_offset),
Robert Becker67b8dc42009-12-14 12:49:57 +11001550 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001551 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
Robert Becker67b8dc42009-12-14 12:49:57 +11001552 "drive\n",
NeilBrown128595e2010-05-03 14:47:14 +10001553 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001554 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001555 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001556 }
NeilBrown6814d532006-10-03 01:15:45 -07001557 rdev_dec_pending(rdev, mddev);
1558 rcu_read_lock();
1559 }
1560 }
1561 sl = start;
1562 while (sl != r10_bio->read_slot) {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001563
NeilBrown6814d532006-10-03 01:15:45 -07001564 if (sl==0)
1565 sl = conf->copies;
1566 sl--;
1567 d = r10_bio->devs[sl].devnum;
1568 rdev = rcu_dereference(conf->mirrors[d].rdev);
1569 if (rdev &&
1570 test_bit(In_sync, &rdev->flags)) {
1571 char b[BDEVNAME_SIZE];
1572 atomic_inc(&rdev->nr_pending);
1573 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001574 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001575 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001576 sect,
Robert Becker67b8dc42009-12-14 12:49:57 +11001577 s<<9, conf->tmppage,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001578 READ, false) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001579 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001580 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001581 "md/raid10:%s: unable to read back "
Robert Becker67b8dc42009-12-14 12:49:57 +11001582 "corrected sectors"
1583 " (%d sectors at %llu on %s)\n",
1584 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001585 (unsigned long long)(
1586 sect + rdev->data_offset),
Robert Becker67b8dc42009-12-14 12:49:57 +11001587 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001588 printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
1589 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001590 bdevname(rdev->bdev, b));
1591
NeilBrown6814d532006-10-03 01:15:45 -07001592 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001593 } else {
NeilBrown6814d532006-10-03 01:15:45 -07001594 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10001595 "md/raid10:%s: read error corrected"
NeilBrown6814d532006-10-03 01:15:45 -07001596 " (%d sectors at %llu on %s)\n",
1597 mdname(mddev), s,
NeilBrown7c4e06f2011-05-11 14:53:17 +10001598 (unsigned long long)(
1599 sect + rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001600 bdevname(rdev->bdev, b));
Robert Becker67b8dc42009-12-14 12:49:57 +11001601 }
NeilBrown6814d532006-10-03 01:15:45 -07001602
1603 rdev_dec_pending(rdev, mddev);
1604 rcu_read_lock();
1605 }
1606 }
1607 rcu_read_unlock();
1608
1609 sectors -= s;
1610 sect += s;
1611 }
1612}
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614static void raid10d(mddev_t *mddev)
1615{
1616 r10bio_t *r10_bio;
1617 struct bio *bio;
1618 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001619 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct list_head *head = &conf->retry_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 mdk_rdev_t *rdev;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001622 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001626 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 for (;;) {
1628 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001629
Jens Axboe7eaceac2011-03-10 08:52:07 +01001630 flush_pending_writes(conf);
NeilBrowna35e63e2008-03-04 14:29:29 -08001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001633 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001634 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1638 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001639 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 spin_unlock_irqrestore(&conf->device_lock, flags);
1641
1642 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001643 conf = mddev->private;
Jens Axboe7eaceac2011-03-10 08:52:07 +01001644 if (test_bit(R10BIO_IsSync, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 sync_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001646 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 recovery_request_write(mddev, r10_bio);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001648 else {
NeilBrown7c4e06f2011-05-11 14:53:17 +10001649 int slot = r10_bio->read_slot;
1650 int mirror = r10_bio->devs[slot].devnum;
NeilBrown4443ae12006-01-06 00:20:28 -08001651 /* we got a read error. Maybe the drive is bad. Maybe just
1652 * the block and we can fix it.
1653 * We freeze all other IO, and try reading the block from
1654 * other devices. When we find one, we re-write
1655 * and check it that fixes the read error.
1656 * This is all done synchronously while the array is
1657 * frozen.
1658 */
NeilBrown6814d532006-10-03 01:15:45 -07001659 if (mddev->ro == 0) {
1660 freeze_array(conf);
1661 fix_read_error(conf, mddev, r10_bio);
1662 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001663 }
NeilBrown7c4e06f2011-05-11 14:53:17 +10001664 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
NeilBrown4443ae12006-01-06 00:20:28 -08001665
NeilBrowna8830bca2011-05-11 14:54:19 +10001666 bio = r10_bio->devs[slot].bio;
1667 r10_bio->devs[slot].bio =
NeilBrown0eb3ff12006-01-06 00:20:29 -08001668 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 mirror = read_balance(conf, r10_bio);
1670 if (mirror == -1) {
NeilBrown128595e2010-05-03 14:47:14 +10001671 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 " read error for block %llu\n",
NeilBrown128595e2010-05-03 14:47:14 +10001673 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 bdevname(bio->bi_bdev,b),
1675 (unsigned long long)r10_bio->sector);
1676 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001677 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 } else {
NeilBrown2c7d46e2010-08-18 16:16:05 +10001679 const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
Maik Hampel14e71342007-07-31 00:37:57 -07001680 bio_put(bio);
NeilBrowna8830bca2011-05-11 14:54:19 +10001681 slot = r10_bio->read_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 rdev = conf->mirrors[mirror].rdev;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001683 printk_ratelimited(
1684 KERN_ERR
1685 "md/raid10:%s: %s: redirecting"
1686 "sector %llu to another mirror\n",
1687 mdname(mddev),
1688 bdevname(rdev->bdev, b),
1689 (unsigned long long)r10_bio->sector);
NeilBrowna167f662010-10-26 18:31:13 +11001690 bio = bio_clone_mddev(r10_bio->master_bio,
1691 GFP_NOIO, mddev);
NeilBrowna8830bca2011-05-11 14:54:19 +10001692 r10_bio->devs[slot].bio = bio;
1693 bio->bi_sector = r10_bio->devs[slot].addr
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 + rdev->data_offset;
1695 bio->bi_bdev = rdev->bdev;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001696 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 bio->bi_private = r10_bio;
1698 bio->bi_end_io = raid10_end_read_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 generic_make_request(bio);
1700 }
1701 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001702 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
NeilBrowne1dfa0a2011-04-18 18:25:41 +10001704 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707
1708static int init_resync(conf_t *conf)
1709{
1710 int buffs;
1711
1712 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001713 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1715 if (!conf->r10buf_pool)
1716 return -ENOMEM;
1717 conf->next_resync = 0;
1718 return 0;
1719}
1720
1721/*
1722 * perform a "sync" on one "block"
1723 *
1724 * We need to make sure that no normal I/O request - particularly write
1725 * requests - conflict with active sync requests.
1726 *
1727 * This is achieved by tracking pending requests and a 'barrier' concept
1728 * that can be installed to exclude normal IO requests.
1729 *
1730 * Resync and recovery are handled very differently.
1731 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1732 *
1733 * For resync, we iterate over virtual addresses, read all copies,
1734 * and update if there are differences. If only one copy is live,
1735 * skip it.
1736 * For recovery, we iterate over physical addresses, read a good
1737 * value for each non-in_sync drive, and over-write.
1738 *
1739 * So, for recovery we may have several outstanding complex requests for a
1740 * given address, one for each out-of-sync device. We model this by allocating
1741 * a number of r10_bio structures, one for each out-of-sync device.
1742 * As we setup these structures, we collect all bio's together into a list
1743 * which we then process collectively to add pages, and then process again
1744 * to pass to generic_make_request.
1745 *
1746 * The r10_bio structures are linked using a borrowed master_bio pointer.
1747 * This link is counted in ->remaining. When the r10_bio that points to NULL
1748 * has its remaining count decremented to 0, the whole complex operation
1749 * is complete.
1750 *
1751 */
1752
NeilBrownab9d47e2011-05-11 14:54:41 +10001753static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
1754 int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
NeilBrown070ec552009-06-16 16:54:21 +10001756 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 r10bio_t *r10_bio;
1758 struct bio *biolist = NULL, *bio;
1759 sector_t max_sector, nr_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001761 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11001762 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 sector_t sectors_skipped = 0;
1765 int chunks_skipped = 0;
1766
1767 if (!conf->r10buf_pool)
1768 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001772 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1774 max_sector = mddev->resync_max_sectors;
1775 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001776 /* If we aborted, we need to abort the
1777 * sync on the 'current' bitmap chucks (there can
1778 * be several when recovering multiple devices).
1779 * as we may have started syncing it but not finished.
1780 * We can find the current address in
1781 * mddev->curr_resync, but for recovery,
1782 * we need to convert that to several
1783 * virtual addresses.
1784 */
1785 if (mddev->curr_resync < max_sector) { /* aborted */
1786 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1787 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1788 &sync_blocks, 1);
1789 else for (i=0; i<conf->raid_disks; i++) {
1790 sector_t sect =
1791 raid10_find_virt(conf, mddev->curr_resync, i);
1792 bitmap_end_sync(mddev->bitmap, sect,
1793 &sync_blocks, 1);
1794 }
1795 } else /* completed sync */
1796 conf->fullsync = 0;
1797
1798 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001800 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return sectors_skipped;
1802 }
1803 if (chunks_skipped >= conf->raid_disks) {
1804 /* if there has been nothing to do on any drive,
1805 * then there is nothing to do at all..
1806 */
NeilBrown57afd892005-06-21 17:17:13 -07001807 *skipped = 1;
1808 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810
NeilBrownc6207272008-02-06 01:39:52 -08001811 if (max_sector > mddev->resync_max)
1812 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* make sure whole request will fit in a chunk - if chunks
1815 * are meaningful
1816 */
1817 if (conf->near_copies < conf->raid_disks &&
1818 max_sector > (sector_nr | conf->chunk_mask))
1819 max_sector = (sector_nr | conf->chunk_mask) + 1;
1820 /*
1821 * If there is non-resync activity waiting for us then
1822 * put in a delay to throttle resync.
1823 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001824 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /* Again, very different code for resync and recovery.
1828 * Both must result in an r10bio with a list of bios that
1829 * have bi_end_io, bi_sector, bi_bdev set,
1830 * and bi_private set to the r10bio.
1831 * For recovery, we may actually create several r10bios
1832 * with 2 bios in each, that correspond to the bios in the main one.
1833 * In this case, the subordinate r10bios link back through a
1834 * borrowed master_bio pointer, and the counter in the master
1835 * includes a ref from each subordinate.
1836 */
1837 /* First, we decide what to do and set ->bi_end_io
1838 * To end_sync_read if we want to read, and
1839 * end_sync_write if we will want to write.
1840 */
1841
NeilBrown6cce3b22006-01-06 00:20:16 -08001842 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1844 /* recovery... the complicated one */
NeilBrowna9f326e2009-09-23 18:06:41 +10001845 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 r10_bio = NULL;
1847
NeilBrownab9d47e2011-05-11 14:54:41 +10001848 for (i=0 ; i<conf->raid_disks; i++) {
1849 int still_degraded;
1850 r10bio_t *rb2;
1851 sector_t sect;
1852 int must_sync;
1853
1854 if (conf->mirrors[i].rdev == NULL ||
1855 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
1856 continue;
1857
1858 still_degraded = 0;
1859 /* want to reconstruct this device */
1860 rb2 = r10_bio;
1861 sect = raid10_find_virt(conf, sector_nr, i);
1862 /* Unless we are doing a full sync, we only need
1863 * to recover the block if it is set in the bitmap
1864 */
1865 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1866 &sync_blocks, 1);
1867 if (sync_blocks < max_sync)
1868 max_sync = sync_blocks;
1869 if (!must_sync &&
1870 !conf->fullsync) {
1871 /* yep, skip the sync_blocks here, but don't assume
1872 * that there will never be anything to do here
NeilBrown6cce3b22006-01-06 00:20:16 -08001873 */
NeilBrownab9d47e2011-05-11 14:54:41 +10001874 chunks_skipped = -1;
1875 continue;
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
NeilBrownab9d47e2011-05-11 14:54:41 +10001878 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1879 raise_barrier(conf, rb2 != NULL);
1880 atomic_set(&r10_bio->remaining, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
NeilBrownab9d47e2011-05-11 14:54:41 +10001882 r10_bio->master_bio = (struct bio*)rb2;
1883 if (rb2)
1884 atomic_inc(&rb2->remaining);
1885 r10_bio->mddev = mddev;
1886 set_bit(R10BIO_IsRecover, &r10_bio->state);
1887 r10_bio->sector = sect;
NeilBrown6cce3b22006-01-06 00:20:16 -08001888
NeilBrownab9d47e2011-05-11 14:54:41 +10001889 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001890
NeilBrownab9d47e2011-05-11 14:54:41 +10001891 /* Need to check if the array will still be
1892 * degraded
1893 */
1894 for (j=0; j<conf->raid_disks; j++)
1895 if (conf->mirrors[j].rdev == NULL ||
1896 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
1897 still_degraded = 1;
NeilBrown87fc7672005-09-09 16:24:04 -07001898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
NeilBrownab9d47e2011-05-11 14:54:41 +10001900
1901 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1902 &sync_blocks, still_degraded);
1903
1904 for (j=0; j<conf->copies;j++) {
1905 int d = r10_bio->devs[j].devnum;
1906 if (!conf->mirrors[d].rdev ||
1907 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
1908 continue;
1909 /* This is where we read from */
1910 bio = r10_bio->devs[0].bio;
1911 bio->bi_next = biolist;
1912 biolist = bio;
1913 bio->bi_private = r10_bio;
1914 bio->bi_end_io = end_sync_read;
1915 bio->bi_rw = READ;
1916 bio->bi_sector = r10_bio->devs[j].addr +
1917 conf->mirrors[d].rdev->data_offset;
1918 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1919 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1920 atomic_inc(&r10_bio->remaining);
1921 /* and we write to 'i' */
1922
1923 for (k=0; k<conf->copies; k++)
1924 if (r10_bio->devs[k].devnum == i)
1925 break;
1926 BUG_ON(k == conf->copies);
1927 bio = r10_bio->devs[1].bio;
1928 bio->bi_next = biolist;
1929 biolist = bio;
1930 bio->bi_private = r10_bio;
1931 bio->bi_end_io = end_sync_write;
1932 bio->bi_rw = WRITE;
1933 bio->bi_sector = r10_bio->devs[k].addr +
1934 conf->mirrors[i].rdev->data_offset;
1935 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1936
1937 r10_bio->devs[0].devnum = d;
1938 r10_bio->devs[1].devnum = i;
1939
1940 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
NeilBrownab9d47e2011-05-11 14:54:41 +10001942 if (j == conf->copies) {
1943 /* Cannot recover, so abort the recovery */
1944 put_buf(r10_bio);
1945 if (rb2)
1946 atomic_dec(&rb2->remaining);
1947 r10_bio = rb2;
1948 if (!test_and_set_bit(MD_RECOVERY_INTR,
1949 &mddev->recovery))
1950 printk(KERN_INFO "md/raid10:%s: insufficient "
1951 "working devices for recovery.\n",
1952 mdname(mddev));
1953 break;
1954 }
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (biolist == NULL) {
1957 while (r10_bio) {
1958 r10bio_t *rb2 = r10_bio;
1959 r10_bio = (r10bio_t*) rb2->master_bio;
1960 rb2->master_bio = NULL;
1961 put_buf(rb2);
1962 }
1963 goto giveup;
1964 }
1965 } else {
1966 /* resync. Schedule a read for every block at this virt offset */
1967 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001968
NeilBrown78200d42009-02-25 13:18:47 +11001969 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1970
NeilBrown6cce3b22006-01-06 00:20:16 -08001971 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1972 &sync_blocks, mddev->degraded) &&
NeilBrownab9d47e2011-05-11 14:54:41 +10001973 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
1974 &mddev->recovery)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001975 /* We can skip this block */
1976 *skipped = 1;
1977 return sync_blocks + sectors_skipped;
1978 }
1979 if (sync_blocks < max_sync)
1980 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 r10_bio->mddev = mddev;
1984 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001985 raise_barrier(conf, 0);
1986 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 r10_bio->master_bio = NULL;
1989 r10_bio->sector = sector_nr;
1990 set_bit(R10BIO_IsSync, &r10_bio->state);
1991 raid10_find_phys(conf, r10_bio);
1992 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1993
1994 for (i=0; i<conf->copies; i++) {
1995 int d = r10_bio->devs[i].devnum;
1996 bio = r10_bio->devs[i].bio;
1997 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001998 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002000 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 continue;
2002 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2003 atomic_inc(&r10_bio->remaining);
2004 bio->bi_next = biolist;
2005 biolist = bio;
2006 bio->bi_private = r10_bio;
2007 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002008 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 bio->bi_sector = r10_bio->devs[i].addr +
2010 conf->mirrors[d].rdev->data_offset;
2011 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2012 count++;
2013 }
2014
2015 if (count < 2) {
2016 for (i=0; i<conf->copies; i++) {
2017 int d = r10_bio->devs[i].devnum;
2018 if (r10_bio->devs[i].bio->bi_end_io)
NeilBrownab9d47e2011-05-11 14:54:41 +10002019 rdev_dec_pending(conf->mirrors[d].rdev,
2020 mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 put_buf(r10_bio);
2023 biolist = NULL;
2024 goto giveup;
2025 }
2026 }
2027
2028 for (bio = biolist; bio ; bio=bio->bi_next) {
2029
2030 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2031 if (bio->bi_end_io)
2032 bio->bi_flags |= 1 << BIO_UPTODATE;
2033 bio->bi_vcnt = 0;
2034 bio->bi_idx = 0;
2035 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 bio->bi_size = 0;
2037 }
2038
2039 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002040 if (sector_nr + max_sync < max_sector)
2041 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 do {
2043 struct page *page;
2044 int len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (sector_nr + (len>>9) > max_sector)
2046 len = (max_sector - sector_nr) << 9;
2047 if (len == 0)
2048 break;
2049 for (bio= biolist ; bio ; bio=bio->bi_next) {
NeilBrownab9d47e2011-05-11 14:54:41 +10002050 struct bio *bio2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
NeilBrownab9d47e2011-05-11 14:54:41 +10002052 if (bio_add_page(bio, page, len, 0))
2053 continue;
2054
2055 /* stop here */
2056 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2057 for (bio2 = biolist;
2058 bio2 && bio2 != bio;
2059 bio2 = bio2->bi_next) {
2060 /* remove last page from this bio */
2061 bio2->bi_vcnt--;
2062 bio2->bi_size -= len;
2063 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
NeilBrownab9d47e2011-05-11 14:54:41 +10002065 goto bio_full;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067 nr_sectors += len>>9;
2068 sector_nr += len>>9;
2069 } while (biolist->bi_vcnt < RESYNC_PAGES);
2070 bio_full:
2071 r10_bio->sectors = nr_sectors;
2072
2073 while (biolist) {
2074 bio = biolist;
2075 biolist = biolist->bi_next;
2076
2077 bio->bi_next = NULL;
2078 r10_bio = bio->bi_private;
2079 r10_bio->sectors = nr_sectors;
2080
2081 if (bio->bi_end_io == end_sync_read) {
2082 md_sync_acct(bio->bi_bdev, nr_sectors);
2083 generic_make_request(bio);
2084 }
2085 }
2086
NeilBrown57afd892005-06-21 17:17:13 -07002087 if (sectors_skipped)
2088 /* pretend they weren't skipped, it makes
2089 * no important difference in this case
2090 */
2091 md_done_sync(mddev, sectors_skipped, 1);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return sectors_skipped + nr_sectors;
2094 giveup:
2095 /* There is nowhere to write, so all non-sync
2096 * drives must be failed, so try the next chunk...
2097 */
NeilBrown09b40682009-02-25 13:18:47 +11002098 if (sector_nr + max_sync < max_sector)
2099 max_sector = sector_nr + max_sync;
2100
2101 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 chunks_skipped ++;
2103 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Dan Williams80c3a6c2009-03-17 18:10:40 -07002107static sector_t
2108raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2109{
2110 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002111 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002112
2113 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002114 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002115 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002116 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002117
2118 size = sectors >> conf->chunk_shift;
2119 sector_div(size, conf->far_copies);
2120 size = size * raid_disks;
2121 sector_div(size, conf->near_copies);
2122
2123 return size << conf->chunk_shift;
2124}
2125
Trela, Maciejdab8b292010-03-08 16:02:45 +11002126
2127static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002129 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002130 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002132 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Maciej Trelaf73ea872010-06-16 11:46:29 +01002134 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2135 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002136 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2137 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2138 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002139 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
NeilBrown2604b702006-01-06 00:20:36 -08002141
Maciej Trelaf73ea872010-06-16 11:46:29 +01002142 nc = mddev->new_layout & 255;
2143 fc = (mddev->new_layout >> 8) & 255;
2144 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002147 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002148 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002149 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 goto out;
2151 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002152
2153 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002154 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002155 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002157
NeilBrown4443ae12006-01-06 00:20:28 -08002158 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002159 GFP_KERNEL);
2160 if (!conf->mirrors)
2161 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002162
2163 conf->tmppage = alloc_page(GFP_KERNEL);
2164 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002165 goto out;
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
NeilBrown64a742b2007-02-28 20:11:18 -08002168 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 conf->near_copies = nc;
2170 conf->far_copies = fc;
2171 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002172 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002173 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2174 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2175
2176 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2177 r10bio_pool_free, conf);
2178 if (!conf->r10bio_pool)
2179 goto out;
2180
Andre Noll58c0fed2009-03-31 14:33:13 +11002181 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002182 sector_div(size, fc);
2183 size = size * conf->raid_disks;
2184 sector_div(size, nc);
2185 /* 'size' is now the number of chunks in the array */
2186 /* calculate "used chunks per device" in 'stride' */
2187 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002188
2189 /* We need to round up when dividing by raid_disks to
2190 * get the stride size.
2191 */
2192 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002193 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002194
2195 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002196
NeilBrownc93983b2006-06-26 00:27:41 -07002197 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002198 stride = 1;
2199 else
NeilBrownc93983b2006-06-26 00:27:41 -07002200 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002201 conf->stride = stride << conf->chunk_shift;
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Neil Browne7e72bf2008-05-14 16:05:54 -07002204 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002205 INIT_LIST_HEAD(&conf->retry_list);
2206
2207 spin_lock_init(&conf->resync_lock);
2208 init_waitqueue_head(&conf->wait_barrier);
2209
2210 conf->thread = md_register_thread(raid10d, mddev, NULL);
2211 if (!conf->thread)
2212 goto out;
2213
Trela, Maciejdab8b292010-03-08 16:02:45 +11002214 conf->mddev = mddev;
2215 return conf;
2216
2217 out:
NeilBrown128595e2010-05-03 14:47:14 +10002218 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002219 mdname(mddev));
2220 if (conf) {
2221 if (conf->r10bio_pool)
2222 mempool_destroy(conf->r10bio_pool);
2223 kfree(conf->mirrors);
2224 safe_put_page(conf->tmppage);
2225 kfree(conf);
2226 }
2227 return ERR_PTR(err);
2228}
2229
2230static int run(mddev_t *mddev)
2231{
2232 conf_t *conf;
2233 int i, disk_idx, chunk_size;
2234 mirror_info_t *disk;
2235 mdk_rdev_t *rdev;
2236 sector_t size;
2237
2238 /*
2239 * copy the already verified devices into our private RAID10
2240 * bookkeeping area. [whatever we allocate in run(),
2241 * should be freed in stop()]
2242 */
2243
2244 if (mddev->private == NULL) {
2245 conf = setup_conf(mddev);
2246 if (IS_ERR(conf))
2247 return PTR_ERR(conf);
2248 mddev->private = conf;
2249 }
2250 conf = mddev->private;
2251 if (!conf)
2252 goto out;
2253
Trela, Maciejdab8b292010-03-08 16:02:45 +11002254 mddev->thread = conf->thread;
2255 conf->thread = NULL;
2256
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002257 chunk_size = mddev->chunk_sectors << 9;
2258 blk_queue_io_min(mddev->queue, chunk_size);
2259 if (conf->raid_disks % conf->near_copies)
2260 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2261 else
2262 blk_queue_io_opt(mddev->queue, chunk_size *
2263 (conf->raid_disks / conf->near_copies));
2264
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002265 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002267 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 || disk_idx < 0)
2269 continue;
2270 disk = conf->mirrors + disk_idx;
2271
2272 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002273 disk_stack_limits(mddev->gendisk, rdev->bdev,
2274 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002276 * violating it, so limit max_segments to 1 lying
2277 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
NeilBrown627a2d32010-03-08 16:44:38 +11002279 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2280 blk_queue_max_segments(mddev->queue, 1);
2281 blk_queue_segment_boundary(mddev->queue,
2282 PAGE_CACHE_SIZE - 1);
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
NeilBrown6d508242005-09-09 16:24:03 -07002287 /* need to check that every block has at least one working mirror */
NeilBrown700c7212011-07-27 11:00:36 +10002288 if (!enough(conf, -1)) {
NeilBrown128595e2010-05-03 14:47:14 +10002289 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002290 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto out_free_conf;
2292 }
2293
2294 mddev->degraded = 0;
2295 for (i = 0; i < conf->raid_disks; i++) {
2296
2297 disk = conf->mirrors + i;
2298
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002299 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002300 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 disk->head_position = 0;
2302 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002303 if (disk->rdev)
2304 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 }
2307
Andre Noll8c6ac862009-06-18 08:48:06 +10002308 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002309 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002310 " -- starting background reconstruction\n",
2311 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002313 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002314 mdname(mddev), conf->raid_disks - mddev->degraded,
2315 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /*
2317 * Ok, everything is just fine now
2318 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002319 mddev->dev_sectors = conf->dev_sectors;
2320 size = raid10_size(mddev, 0, 0);
2321 md_set_array_sectors(mddev, size);
2322 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
NeilBrown0d129222006-10-03 01:15:54 -07002324 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2325 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /* Calculate max read-ahead size.
2328 * We need to readahead at least twice a whole stripe....
2329 * maybe...
2330 */
2331 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002332 int stripe = conf->raid_disks *
2333 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 stripe /= conf->near_copies;
2335 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2336 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2337 }
2338
NeilBrown84707f32010-03-16 17:23:35 +11002339 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Martin K. Petersena91a2782011-03-17 11:11:05 +01002341
2342 if (md_integrity_register(mddev))
2343 goto out_free_conf;
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return 0;
2346
2347out_free_conf:
NeilBrown589a5942010-12-09 17:02:14 +11002348 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (conf->r10bio_pool)
2350 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002351 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002352 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 kfree(conf);
2354 mddev->private = NULL;
2355out:
2356 return -EIO;
2357}
2358
2359static int stop(mddev_t *mddev)
2360{
NeilBrown070ec552009-06-16 16:54:21 +10002361 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
NeilBrown409c57f2009-03-31 14:39:39 +11002363 raise_barrier(conf, 0);
2364 lower_barrier(conf);
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 md_unregister_thread(mddev->thread);
2367 mddev->thread = NULL;
2368 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2369 if (conf->r10bio_pool)
2370 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002371 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 kfree(conf);
2373 mddev->private = NULL;
2374 return 0;
2375}
2376
NeilBrown6cce3b22006-01-06 00:20:16 -08002377static void raid10_quiesce(mddev_t *mddev, int state)
2378{
NeilBrown070ec552009-06-16 16:54:21 +10002379 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002380
2381 switch(state) {
2382 case 1:
2383 raise_barrier(conf, 0);
2384 break;
2385 case 0:
2386 lower_barrier(conf);
2387 break;
2388 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002389}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Trela, Maciejdab8b292010-03-08 16:02:45 +11002391static void *raid10_takeover_raid0(mddev_t *mddev)
2392{
2393 mdk_rdev_t *rdev;
2394 conf_t *conf;
2395
2396 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10002397 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2398 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002399 return ERR_PTR(-EINVAL);
2400 }
2401
Trela, Maciejdab8b292010-03-08 16:02:45 +11002402 /* Set new parameters */
2403 mddev->new_level = 10;
2404 /* new layout: far_copies = 1, near_copies = 2 */
2405 mddev->new_layout = (1<<8) + 2;
2406 mddev->new_chunk_sectors = mddev->chunk_sectors;
2407 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002408 mddev->raid_disks *= 2;
2409 /* make sure it will be not marked as dirty */
2410 mddev->recovery_cp = MaxSector;
2411
2412 conf = setup_conf(mddev);
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002413 if (!IS_ERR(conf)) {
NeilBrowne93f68a2010-06-15 09:36:03 +01002414 list_for_each_entry(rdev, &mddev->disks, same_set)
2415 if (rdev->raid_disk >= 0)
2416 rdev->new_raid_disk = rdev->raid_disk * 2;
Krzysztof Wojcik02214dc2011-02-04 14:18:26 +01002417 conf->barrier = 1;
2418 }
2419
Trela, Maciejdab8b292010-03-08 16:02:45 +11002420 return conf;
2421}
2422
2423static void *raid10_takeover(mddev_t *mddev)
2424{
2425 struct raid0_private_data *raid0_priv;
2426
2427 /* raid10 can take over:
2428 * raid0 - providing it has only two drives
2429 */
2430 if (mddev->level == 0) {
2431 /* for raid0 takeover only one zone is supported */
2432 raid0_priv = mddev->private;
2433 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10002434 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2435 " with more than one zone.\n",
2436 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002437 return ERR_PTR(-EINVAL);
2438 }
2439 return raid10_takeover_raid0(mddev);
2440 }
2441 return ERR_PTR(-EINVAL);
2442}
2443
NeilBrown2604b702006-01-06 00:20:36 -08002444static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
2446 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002447 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 .owner = THIS_MODULE,
2449 .make_request = make_request,
2450 .run = run,
2451 .stop = stop,
2452 .status = status,
2453 .error_handler = error,
2454 .hot_add_disk = raid10_add_disk,
2455 .hot_remove_disk= raid10_remove_disk,
2456 .spare_active = raid10_spare_active,
2457 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002458 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002459 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002460 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461};
2462
2463static int __init raid_init(void)
2464{
NeilBrown2604b702006-01-06 00:20:36 -08002465 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
2468static void raid_exit(void)
2469{
NeilBrown2604b702006-01-06 00:20:36 -08002470 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
2473module_init(raid_init);
2474module_exit(raid_exit);
2475MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002476MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002478MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002479MODULE_ALIAS("md-level-10");