blob: a1f5fd2d69ce3bd33584ab255675e9a5dff0225e [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 *
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
9 *
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
Stephen Rothwell25570722008-10-15 09:09:21 +110021#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110022#include <linux/blkdev.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110024#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110025#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110026#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110027#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * RAID10 provides a combination of RAID0 and RAID1 functionality.
31 * The layout of data is defined by
32 * chunk_size
33 * raid_disks
34 * near_copies (stored in low byte of layout)
35 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070036 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
38 * The data to be stored is divided into chunks using chunksize.
39 * Each device is divided into far_copies sections.
40 * In each section, chunks are laid out in a style similar to raid0, but
41 * near_copies copies of each chunk is stored (each on a different drive).
42 * The starting device for each section is offset near_copies from the starting
43 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070044 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * drive.
46 * near_copies and far_copies must be at least one, and their product is at most
47 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070048 *
49 * If far_offset is true, then the far_copies are handled a bit differently.
50 * The copies are still in different stripes, but instead of be very far apart
51 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53
54/*
55 * Number of guaranteed r10bios in case of extreme VM load:
56 */
57#define NR_RAID10_BIOS 256
58
59static void unplug_slaves(mddev_t *mddev);
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;
67 r10bio_t *r10_bio;
68 int size = offsetof(struct r10bio_s, devs[conf->copies]);
69
70 /* allocate a r10bio with room for raid_disks entries in the bios array */
NeilBrown9ffae0c2006-01-06 00:20:32 -080071 r10_bio = kzalloc(size, gfp_flags);
NeilBrowned9bfdf2009-10-16 15:55:44 +110072 if (!r10_bio && conf->mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unplug_slaves(conf->mddev);
74
75 return r10_bio;
76}
77
78static void r10bio_pool_free(void *r10_bio, void *data)
79{
80 kfree(r10_bio);
81}
82
NeilBrown0310fa22008-08-05 15:54:14 +100083/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100086/* amount of memory to reserve for resync requests */
87#define RESYNC_WINDOW (1024*1024)
88/* maximum number of concurrent requests, memory permitting */
89#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * When performing a resync, we need to read and compare, so
93 * we need as many pages are there are copies.
94 * When performing a recovery, we need 2 bios, one for read,
95 * one for write (we recover only one drive per r10buf)
96 *
97 */
Al Virodd0fc662005-10-07 07:46:04 +010098static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 conf_t *conf = data;
101 struct page *page;
102 r10bio_t *r10_bio;
103 struct bio *bio;
104 int i, j;
105 int nalloc;
106
107 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
108 if (!r10_bio) {
109 unplug_slaves(conf->mddev);
110 return NULL;
111 }
112
113 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
114 nalloc = conf->copies; /* resync */
115 else
116 nalloc = 2; /* recovery */
117
118 /*
119 * Allocate bios.
120 */
121 for (j = nalloc ; j-- ; ) {
122 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
123 if (!bio)
124 goto out_free_bio;
125 r10_bio->devs[j].bio = bio;
126 }
127 /*
128 * Allocate RESYNC_PAGES data pages and attach them
129 * where needed.
130 */
131 for (j = 0 ; j < nalloc; j++) {
132 bio = r10_bio->devs[j].bio;
133 for (i = 0; i < RESYNC_PAGES; i++) {
134 page = alloc_page(gfp_flags);
135 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
NeilBrown6712ecf2007-09-27 12:47:43 +0200255static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100258 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000260 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 slot = r10_bio->read_slot;
264 dev = r10_bio->devs[slot].devnum;
265 /*
266 * this branch is our 'one mirror IO has finished' event handler:
267 */
NeilBrown4443ae12006-01-06 00:20:28 -0800268 update_head_pos(slot, r10_bio);
269
270 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /*
272 * Set R10BIO_Uptodate in our master bio, so that
273 * we will return a good error code to the higher
274 * levels even if IO on some other mirrored buffer fails.
275 *
276 * The 'master' represents the composite IO operation to
277 * user-side. So if something waits for IO, then it will
278 * wait for the 'master' bio.
279 */
280 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 raid_end_bio_io(r10_bio);
NeilBrown4443ae12006-01-06 00:20:28 -0800282 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /*
284 * oops, read error:
285 */
286 char b[BDEVNAME_SIZE];
287 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +1000288 printk(KERN_ERR "md/raid10:%s: %s: rescheduling sector %llu\n",
289 mdname(conf->mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
291 reschedule_retry(r10_bio);
292 }
293
294 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
NeilBrown6712ecf2007-09-27 12:47:43 +0200297static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100300 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000302 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 for (slot = 0; slot < conf->copies; slot++)
305 if (r10_bio->devs[slot].bio == bio)
306 break;
307 dev = r10_bio->devs[slot].devnum;
308
309 /*
310 * this branch is our 'one mirror IO has finished' event handler:
311 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800312 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800314 /* an I/O failed, we can't clear the bitmap */
315 set_bit(R10BIO_Degraded, &r10_bio->state);
316 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /*
318 * Set R10BIO_Uptodate in our master bio, so that
319 * we will return a good error code for to the higher
320 * levels even if IO on some other mirrored buffer fails.
321 *
322 * The 'master' represents the composite IO operation to
323 * user-side. So if something waits for IO, then it will
324 * wait for the 'master' bio.
325 */
326 set_bit(R10BIO_Uptodate, &r10_bio->state);
327
328 update_head_pos(slot, r10_bio);
329
330 /*
331 *
332 * Let's see if all mirrored write operations have finished
333 * already.
334 */
335 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800336 /* clear the bitmap if all writes complete successfully */
337 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
338 r10_bio->sectors,
339 !test_bit(R10BIO_Degraded, &r10_bio->state),
340 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 md_write_end(r10_bio->mddev);
342 raid_end_bio_io(r10_bio);
343 }
344
345 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348
349/*
350 * RAID10 layout manager
351 * Aswell as the chunksize and raid_disks count, there are two
352 * parameters: near_copies and far_copies.
353 * near_copies * far_copies must be <= raid_disks.
354 * Normally one of these will be 1.
355 * If both are 1, we get raid0.
356 * If near_copies == raid_disks, we get raid1.
357 *
358 * Chunks are layed out in raid0 style with near_copies copies of the
359 * first chunk, followed by near_copies copies of the next chunk and
360 * so on.
361 * If far_copies > 1, then after 1/far_copies of the array has been assigned
362 * as described above, we start again with a device offset of near_copies.
363 * So we effectively have another copy of the whole array further down all
364 * the drives, but with blocks on different drives.
365 * With this layout, and block is never stored twice on the one device.
366 *
367 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700368 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
370 * raid10_find_virt does the reverse mapping, from a device and a
371 * sector offset to a virtual address
372 */
373
374static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
375{
376 int n,f;
377 sector_t sector;
378 sector_t chunk;
379 sector_t stripe;
380 int dev;
381
382 int slot = 0;
383
384 /* now calculate first sector/dev */
385 chunk = r10bio->sector >> conf->chunk_shift;
386 sector = r10bio->sector & conf->chunk_mask;
387
388 chunk *= conf->near_copies;
389 stripe = chunk;
390 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700391 if (conf->far_offset)
392 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 sector += stripe << conf->chunk_shift;
395
396 /* and calculate all the others */
397 for (n=0; n < conf->near_copies; n++) {
398 int d = dev;
399 sector_t s = sector;
400 r10bio->devs[slot].addr = sector;
401 r10bio->devs[slot].devnum = d;
402 slot++;
403
404 for (f = 1; f < conf->far_copies; f++) {
405 d += conf->near_copies;
406 if (d >= conf->raid_disks)
407 d -= conf->raid_disks;
408 s += conf->stride;
409 r10bio->devs[slot].devnum = d;
410 r10bio->devs[slot].addr = s;
411 slot++;
412 }
413 dev++;
414 if (dev >= conf->raid_disks) {
415 dev = 0;
416 sector += (conf->chunk_mask + 1);
417 }
418 }
419 BUG_ON(slot != conf->copies);
420}
421
422static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
423{
424 sector_t offset, chunk, vchunk;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700427 if (conf->far_offset) {
428 int fc;
429 chunk = sector >> conf->chunk_shift;
430 fc = sector_div(chunk, conf->far_copies);
431 dev -= fc * conf->near_copies;
432 if (dev < 0)
433 dev += conf->raid_disks;
434 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800435 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700436 sector -= conf->stride;
437 if (dev < conf->near_copies)
438 dev += conf->raid_disks - conf->near_copies;
439 else
440 dev -= conf->near_copies;
441 }
442 chunk = sector >> conf->chunk_shift;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 vchunk = chunk * conf->raid_disks + dev;
445 sector_div(vchunk, conf->near_copies);
446 return (vchunk << conf->chunk_shift) + offset;
447}
448
449/**
450 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
451 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200452 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * @biovec: the request that could be merged to it.
454 *
455 * Return amount of bytes we can accept at this offset
456 * If near_copies == raid_disk, there are no striping issues,
457 * but in that case, the function isn't called at all.
458 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200459static int raid10_mergeable_bvec(struct request_queue *q,
460 struct bvec_merge_data *bvm,
461 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200464 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000466 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200467 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
470 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200471 if (max <= biovec->bv_len && bio_sectors == 0)
472 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 else
474 return max;
475}
476
477/*
478 * This routine returns the disk from which the requested read should
479 * be done. There is a per-array 'next expected sequential IO' sector
480 * number - if this matches on the next IO then we use the last disk.
481 * There is also a per-disk 'last know head position' sector that is
482 * maintained from IRQ contexts, both the normal and the resync IO
483 * completion handlers update this position correctly. If there is no
484 * perfect sequential match then we pick the disk whose head is closest.
485 *
486 * If there are 2 mirrors in the same 2 devices, performance degrades
487 * because position is mirror, not device based.
488 *
489 * The rdev for the device selected will have nr_pending incremented.
490 */
491
492/*
493 * FIXME: possibly should rethink readbalancing and do it differently
494 * depending on near_copies / far_copies geometry.
495 */
496static int read_balance(conf_t *conf, r10bio_t *r10_bio)
497{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000498 const sector_t this_sector = r10_bio->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int disk, slot, nslot;
500 const int sectors = r10_bio->sectors;
501 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800502 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 raid10_find_phys(conf, r10_bio);
505 rcu_read_lock();
506 /*
507 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800508 * device if no resync is going on (recovery is ok), or below
509 * the resync window. We take the first readable disk when
510 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
512 if (conf->mddev->recovery_cp < MaxSector
513 && (this_sector + sectors >= conf->next_resync)) {
514 /* make sure that disk is operational */
515 slot = 0;
516 disk = r10_bio->devs[slot].devnum;
517
Suzanne Woodd6065f72005-11-08 21:39:27 -0800518 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800519 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800520 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 slot++;
522 if (slot == conf->copies) {
523 slot = 0;
524 disk = -1;
525 break;
526 }
527 disk = r10_bio->devs[slot].devnum;
528 }
529 goto rb_out;
530 }
531
532
533 /* make sure the disk is operational */
534 slot = 0;
535 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800536 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800537 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800538 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 slot ++;
540 if (slot == conf->copies) {
541 disk = -1;
542 goto rb_out;
543 }
544 disk = r10_bio->devs[slot].devnum;
545 }
546
547
NeilBrown3ec67ac2005-09-09 16:23:40 -0700548 current_distance = abs(r10_bio->devs[slot].addr -
549 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800551 /* Find the disk whose head is closest,
552 * or - for far > 1 - find the closest to partition beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 for (nslot = slot; nslot < conf->copies; nslot++) {
555 int ndisk = r10_bio->devs[nslot].devnum;
556
557
Suzanne Woodd6065f72005-11-08 21:39:27 -0800558 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800559 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800560 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 continue;
562
NeilBrown22dfdf52005-11-28 13:44:09 -0800563 /* This optimisation is debatable, and completely destroys
564 * sequential read speed for 'far copies' arrays. So only
565 * keep it for 'near' arrays, and review those later.
566 */
567 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 disk = ndisk;
569 slot = nslot;
570 break;
571 }
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800572
573 /* for far > 1 always use the lowest address */
574 if (conf->far_copies > 1)
575 new_distance = r10_bio->devs[nslot].addr;
576 else
577 new_distance = abs(r10_bio->devs[nslot].addr -
578 conf->mirrors[ndisk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (new_distance < current_distance) {
580 current_distance = new_distance;
581 disk = ndisk;
582 slot = nslot;
583 }
584 }
585
586rb_out:
587 r10_bio->read_slot = slot;
588/* conf->next_seq_sect = this_sector + sectors;*/
589
Suzanne Woodd6065f72005-11-08 21:39:27 -0800590 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800592 else
593 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 rcu_read_unlock();
595
596 return disk;
597}
598
599static void unplug_slaves(mddev_t *mddev)
600{
NeilBrown070ec552009-06-16 16:54:21 +1000601 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int i;
603
604 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100605 for (i=0; i < conf->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800606 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800607 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200608 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 atomic_inc(&rdev->nr_pending);
611 rcu_read_unlock();
612
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500613 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 rdev_dec_pending(rdev, mddev);
616 rcu_read_lock();
617 }
618 }
619 rcu_read_unlock();
620}
621
Jens Axboe165125e2007-07-24 09:28:11 +0200622static void raid10_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
NeilBrown6cce3b22006-01-06 00:20:16 -0800624 mddev_t *mddev = q->queuedata;
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unplug_slaves(q->queuedata);
NeilBrown6cce3b22006-01-06 00:20:16 -0800627 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
NeilBrown0d129222006-10-03 01:15:54 -0700630static int raid10_congested(void *data, int bits)
631{
632 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000633 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700634 int i, ret = 0;
635
NeilBrown3fa841d2009-09-23 18:10:29 +1000636 if (mddev_congested(mddev, bits))
637 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700638 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100639 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700640 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
641 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200642 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700643
644 ret |= bdi_congested(&q->backing_dev_info, bits);
645 }
646 }
647 rcu_read_unlock();
648 return ret;
649}
650
NeilBrowna35e63e2008-03-04 14:29:29 -0800651static int flush_pending_writes(conf_t *conf)
652{
653 /* Any writes that have been queued but are awaiting
654 * bitmap updates get flushed here.
655 * We return 1 if any requests were actually submitted.
656 */
657 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700658
NeilBrowna35e63e2008-03-04 14:29:29 -0800659 spin_lock_irq(&conf->device_lock);
660
661 if (conf->pending_bio_list.head) {
662 struct bio *bio;
663 bio = bio_list_get(&conf->pending_bio_list);
664 blk_remove_plug(conf->mddev->queue);
665 spin_unlock_irq(&conf->device_lock);
666 /* flush any pending bitmap writes to disk
667 * before proceeding w/ I/O */
668 bitmap_unplug(conf->mddev->bitmap);
669
670 while (bio) { /* submit pending writes */
671 struct bio *next = bio->bi_next;
672 bio->bi_next = NULL;
673 generic_make_request(bio);
674 bio = next;
675 }
676 rv = 1;
677 } else
678 spin_unlock_irq(&conf->device_lock);
679 return rv;
680}
NeilBrown0a27ec92006-01-06 00:20:13 -0800681/* Barriers....
682 * Sometimes we need to suspend IO while we do something else,
683 * either some resync/recovery, or reconfigure the array.
684 * To do this we raise a 'barrier'.
685 * The 'barrier' is a counter that can be raised multiple times
686 * to count how many activities are happening which preclude
687 * normal IO.
688 * We can only raise the barrier if there is no pending IO.
689 * i.e. if nr_pending == 0.
690 * We choose only to raise the barrier if no-one is waiting for the
691 * barrier to go down. This means that as soon as an IO request
692 * is ready, no other operations which require a barrier will start
693 * until the IO request has had a chance.
694 *
695 * So: regular IO calls 'wait_barrier'. When that returns there
696 * is no backgroup IO happening, It must arrange to call
697 * allow_barrier when it has finished its IO.
698 * backgroup IO calls must call raise_barrier. Once that returns
699 * there is no normal IO happeing. It must arrange to call
700 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
NeilBrown6cce3b22006-01-06 00:20:16 -0800703static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
NeilBrown6cce3b22006-01-06 00:20:16 -0800705 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
NeilBrown6cce3b22006-01-06 00:20:16 -0800708 /* Wait until no block IO is waiting (unless 'force') */
709 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800710 conf->resync_lock,
711 raid10_unplug(conf->mddev->queue));
712
713 /* block any new IO from starting */
714 conf->barrier++;
715
716 /* No wait for all pending IO to complete */
717 wait_event_lock_irq(conf->wait_barrier,
718 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
719 conf->resync_lock,
720 raid10_unplug(conf->mddev->queue));
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 spin_unlock_irq(&conf->resync_lock);
723}
724
NeilBrown0a27ec92006-01-06 00:20:13 -0800725static void lower_barrier(conf_t *conf)
726{
727 unsigned long flags;
728 spin_lock_irqsave(&conf->resync_lock, flags);
729 conf->barrier--;
730 spin_unlock_irqrestore(&conf->resync_lock, flags);
731 wake_up(&conf->wait_barrier);
732}
733
734static void wait_barrier(conf_t *conf)
735{
736 spin_lock_irq(&conf->resync_lock);
737 if (conf->barrier) {
738 conf->nr_waiting++;
739 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
740 conf->resync_lock,
741 raid10_unplug(conf->mddev->queue));
742 conf->nr_waiting--;
743 }
744 conf->nr_pending++;
745 spin_unlock_irq(&conf->resync_lock);
746}
747
748static void allow_barrier(conf_t *conf)
749{
750 unsigned long flags;
751 spin_lock_irqsave(&conf->resync_lock, flags);
752 conf->nr_pending--;
753 spin_unlock_irqrestore(&conf->resync_lock, flags);
754 wake_up(&conf->wait_barrier);
755}
756
NeilBrown4443ae12006-01-06 00:20:28 -0800757static void freeze_array(conf_t *conf)
758{
759 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800760 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800761 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800762 * wait until nr_pending match nr_queued+1
763 * This is called in the context of one normal IO request
764 * that has failed. Thus any sync request that might be pending
765 * will be blocked by nr_pending, and we need to wait for
766 * pending IO requests to complete or be queued for re-try.
767 * Thus the number queued (nr_queued) plus this request (1)
768 * must match the number of pending IOs (nr_pending) before
769 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800770 */
771 spin_lock_irq(&conf->resync_lock);
772 conf->barrier++;
773 conf->nr_waiting++;
774 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800775 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800776 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800777 ({ flush_pending_writes(conf);
778 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800779 spin_unlock_irq(&conf->resync_lock);
780}
781
782static void unfreeze_array(conf_t *conf)
783{
784 /* reverse the effect of the freeze */
785 spin_lock_irq(&conf->resync_lock);
786 conf->barrier--;
787 conf->nr_waiting--;
788 wake_up(&conf->wait_barrier);
789 spin_unlock_irq(&conf->resync_lock);
790}
791
NeilBrown21a52c62010-04-01 15:02:13 +1100792static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
NeilBrown070ec552009-06-16 16:54:21 +1000794 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 mirror_info_t *mirror;
796 r10bio_t *r10_bio;
797 struct bio *read_bio;
798 int i;
799 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100800 const int rw = bio_data_dir(bio);
Jens Axboe1f98a132009-09-11 14:32:04 +0200801 const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
NeilBrown6cce3b22006-01-06 00:20:16 -0800802 struct bio_list bl;
803 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700804 mdk_rdev_t *blocked_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jens Axboe1f98a132009-09-11 14:32:04 +0200806 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
NeilBrowna2826aa2009-12-14 12:49:49 +1100807 md_barrier_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700808 return 0;
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* If this request crosses a chunk boundary, we need to
812 * split it. This will only happen for 1 PAGE (or less) requests.
813 */
814 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
815 > chunk_sects &&
816 conf->near_copies < conf->raid_disks)) {
817 struct bio_pair *bp;
818 /* Sanity check -- queue functions should prevent this happening */
819 if (bio->bi_vcnt != 1 ||
820 bio->bi_idx != 0)
821 goto bad_map;
822 /* This is a one page bio that upper layers
823 * refuse to split for us, so we need to split it.
824 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200825 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown21a52c62010-04-01 15:02:13 +1100827 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100829 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 generic_make_request(&bp->bio2);
831
832 bio_pair_release(bp);
833 return 0;
834 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000835 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
836 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
838
NeilBrown6712ecf2007-09-27 12:47:43 +0200839 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841 }
842
NeilBrown3d310eb2005-06-21 17:17:26 -0700843 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /*
846 * Register the new request and wait if the reconstruction
847 * thread has put up a bar for new requests.
848 * Continue immediately if no resync is active currently.
849 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800850 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
853
854 r10_bio->master_bio = bio;
855 r10_bio->sectors = bio->bi_size >> 9;
856
857 r10_bio->mddev = mddev;
858 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800859 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Jens Axboea3623572005-11-01 09:26:16 +0100861 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * read balancing logic:
864 */
865 int disk = read_balance(conf, r10_bio);
866 int slot = r10_bio->read_slot;
867 if (disk < 0) {
868 raid_end_bio_io(r10_bio);
869 return 0;
870 }
871 mirror = conf->mirrors + disk;
872
873 read_bio = bio_clone(bio, GFP_NOIO);
874
875 r10_bio->devs[slot].bio = read_bio;
876
877 read_bio->bi_sector = r10_bio->devs[slot].addr +
878 mirror->rdev->data_offset;
879 read_bio->bi_bdev = mirror->rdev->bdev;
880 read_bio->bi_end_io = raid10_end_read_request;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +0400881 read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 read_bio->bi_private = r10_bio;
883
884 generic_make_request(read_bio);
885 return 0;
886 }
887
888 /*
889 * WRITE:
890 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700891 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * inc refcount on their rdev. Record them by setting
893 * bios[x] to bio
894 */
895 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700896 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700897 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 rcu_read_lock();
899 for (i = 0; i < conf->copies; i++) {
900 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800901 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700902 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
903 atomic_inc(&rdev->nr_pending);
904 blocked_rdev = rdev;
905 break;
906 }
907 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800908 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800910 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800912 set_bit(R10BIO_Degraded, &r10_bio->state);
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 rcu_read_unlock();
916
Dan Williams6bfe0b42008-04-30 00:52:32 -0700917 if (unlikely(blocked_rdev)) {
918 /* Have to wait for this device to get unblocked, then retry */
919 int j;
920 int d;
921
922 for (j = 0; j < i; j++)
923 if (r10_bio->devs[j].bio) {
924 d = r10_bio->devs[j].devnum;
925 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
926 }
927 allow_barrier(conf);
928 md_wait_for_blocked_rdev(blocked_rdev, mddev);
929 wait_barrier(conf);
930 goto retry_write;
931 }
932
NeilBrown6cce3b22006-01-06 00:20:16 -0800933 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700934
NeilBrown6cce3b22006-01-06 00:20:16 -0800935 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 for (i = 0; i < conf->copies; i++) {
937 struct bio *mbio;
938 int d = r10_bio->devs[i].devnum;
939 if (!r10_bio->devs[i].bio)
940 continue;
941
942 mbio = bio_clone(bio, GFP_NOIO);
943 r10_bio->devs[i].bio = mbio;
944
945 mbio->bi_sector = r10_bio->devs[i].addr+
946 conf->mirrors[d].rdev->data_offset;
947 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
948 mbio->bi_end_io = raid10_end_write_request;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +0400949 mbio->bi_rw = WRITE | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 mbio->bi_private = r10_bio;
951
952 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b22006-01-06 00:20:16 -0800953 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Arne Redlichf6f953a2007-07-31 00:37:57 -0700956 if (unlikely(!atomic_read(&r10_bio->remaining))) {
957 /* the array is dead */
958 md_write_end(mddev);
959 raid_end_bio_io(r10_bio);
960 return 0;
961 }
962
NeilBrown6cce3b22006-01-06 00:20:16 -0800963 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
964 spin_lock_irqsave(&conf->device_lock, flags);
965 bio_list_merge(&conf->pending_bio_list, &bl);
966 blk_plug_device(mddev->queue);
967 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
NeilBrowna35e63e2008-03-04 14:29:29 -0800969 /* In case raid10d snuck in to freeze_array */
970 wake_up(&conf->wait_barrier);
971
Lars Ellenberge3881a62007-01-10 23:15:37 -0800972 if (do_sync)
973 md_wakeup_thread(mddev->thread);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return 0;
976}
977
978static void status(struct seq_file *seq, mddev_t *mddev)
979{
NeilBrown070ec552009-06-16 16:54:21 +1000980 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int i;
982
983 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +1000984 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (conf->near_copies > 1)
986 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700987 if (conf->far_copies > 1) {
988 if (conf->far_offset)
989 seq_printf(seq, " %d offset-copies", conf->far_copies);
990 else
991 seq_printf(seq, " %d far-copies", conf->far_copies);
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700994 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 for (i = 0; i < conf->raid_disks; i++)
996 seq_printf(seq, "%s",
997 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800998 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 seq_printf(seq, "]");
1000}
1001
1002static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1003{
1004 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001005 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
1008 * If it is not operational, then we have already marked it as dead
1009 * else if it is the last working disks, ignore the error, let the
1010 * next level up know.
1011 * else mark the drive as failed
1012 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001013 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -07001014 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * Don't fail the drive, just return an IO error.
1017 * The test should really be more sophisticated than
1018 * "working_disks == 1", but it isn't critical, and
1019 * can wait until we do more sophisticated "is the drive
1020 * really dead" tests...
1021 */
1022 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001023 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1024 unsigned long flags;
1025 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001027 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * if recovery is running, make sure it aborts.
1030 */
NeilBrowndfc70642008-05-23 13:04:39 -07001031 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001033 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001034 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown128595e2010-05-03 14:47:14 +10001035 printk(KERN_ALERT "md/raid10:%s: Disk failure on %s, disabling device.\n"
1036 KERN_ALERT "md/raid10:%s: Operation continuing on %d devices.\n",
1037 mdname(mddev), bdevname(rdev->bdev, b),
1038 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041static void print_conf(conf_t *conf)
1042{
1043 int i;
1044 mirror_info_t *tmp;
1045
NeilBrown128595e2010-05-03 14:47:14 +10001046 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001048 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return;
1050 }
NeilBrown128595e2010-05-03 14:47:14 +10001051 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 conf->raid_disks);
1053
1054 for (i = 0; i < conf->raid_disks; i++) {
1055 char b[BDEVNAME_SIZE];
1056 tmp = conf->mirrors + i;
1057 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001058 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001059 i, !test_bit(In_sync, &tmp->rdev->flags),
1060 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 bdevname(tmp->rdev->bdev,b));
1062 }
1063}
1064
1065static void close_sync(conf_t *conf)
1066{
NeilBrown0a27ec92006-01-06 00:20:13 -08001067 wait_barrier(conf);
1068 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 mempool_destroy(conf->r10buf_pool);
1071 conf->r10buf_pool = NULL;
1072}
1073
NeilBrown6d508242005-09-09 16:24:03 -07001074/* check if there are enough drives for
1075 * every block to appear on atleast one
1076 */
1077static int enough(conf_t *conf)
1078{
1079 int first = 0;
1080
1081 do {
1082 int n = conf->copies;
1083 int cnt = 0;
1084 while (n--) {
1085 if (conf->mirrors[first].rdev)
1086 cnt++;
1087 first = (first+1) % conf->raid_disks;
1088 }
1089 if (cnt == 0)
1090 return 0;
1091 } while (first != 0);
1092 return 1;
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static int raid10_spare_active(mddev_t *mddev)
1096{
1097 int i;
1098 conf_t *conf = mddev->private;
1099 mirror_info_t *tmp;
1100
1101 /*
1102 * Find all non-in_sync disks within the RAID10 configuration
1103 * and mark them in_sync
1104 */
1105 for (i = 0; i < conf->raid_disks; i++) {
1106 tmp = conf->mirrors + i;
1107 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001108 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001109 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1110 unsigned long flags;
1111 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001113 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115 }
1116
1117 print_conf(conf);
1118 return 0;
1119}
1120
1121
1122static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1123{
1124 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001125 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int mirror;
1127 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001128 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001129 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (mddev->recovery_cp < MaxSector)
1132 /* only hot-add to in-sync arrays, as recovery is
1133 * very different from resync
1134 */
Neil Brown199050e2008-06-28 08:31:33 +10001135 return -EBUSY;
NeilBrown6d508242005-09-09 16:24:03 -07001136 if (!enough(conf))
Neil Brown199050e2008-06-28 08:31:33 +10001137 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
NeilBrowna53a6c82008-11-06 17:28:20 +11001139 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001140 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
NeilBrown6cce3b22006-01-06 00:20:16 -08001142 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10001143 rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001144 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1145 mirror = rdev->saved_raid_disk;
1146 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001147 mirror = first;
1148 for ( ; mirror <= last ; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if ( !(p=conf->mirrors+mirror)->rdev) {
1150
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001151 disk_stack_limits(mddev->gendisk, rdev->bdev,
1152 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001153 /* as we don't honour merge_bvec_fn, we must
1154 * never risk violating it, so limit
1155 * ->max_segments to one lying with a single
1156 * page, as a one page request is never in
1157 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
NeilBrown627a2d32010-03-08 16:44:38 +11001159 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1160 blk_queue_max_segments(mddev->queue, 1);
1161 blk_queue_segment_boundary(mddev->queue,
1162 PAGE_CACHE_SIZE - 1);
1163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 p->head_position = 0;
1166 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001167 err = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001168 if (rdev->saved_raid_disk != mirror)
1169 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001170 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
1172 }
1173
Andre Nollac5e7112009-08-03 10:59:47 +10001174 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001176 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179static int raid10_remove_disk(mddev_t *mddev, int number)
1180{
1181 conf_t *conf = mddev->private;
1182 int err = 0;
1183 mdk_rdev_t *rdev;
1184 mirror_info_t *p = conf->mirrors+ number;
1185
1186 print_conf(conf);
1187 rdev = p->rdev;
1188 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001189 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 atomic_read(&rdev->nr_pending)) {
1191 err = -EBUSY;
1192 goto abort;
1193 }
NeilBrowndfc70642008-05-23 13:04:39 -07001194 /* Only remove faulty devices in recovery
1195 * is not possible.
1196 */
1197 if (!test_bit(Faulty, &rdev->flags) &&
1198 enough(conf)) {
1199 err = -EBUSY;
1200 goto abort;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001203 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (atomic_read(&rdev->nr_pending)) {
1205 /* lost the race, try later */
1206 err = -EBUSY;
1207 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001208 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Andre Nollac5e7112009-08-03 10:59:47 +10001210 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212abort:
1213
1214 print_conf(conf);
1215 return err;
1216}
1217
1218
NeilBrown6712ecf2007-09-27 12:47:43 +02001219static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001221 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001222 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int i,d;
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 for (i=0; i<conf->copies; i++)
1226 if (r10_bio->devs[i].bio == bio)
1227 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001228 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 update_head_pos(i, r10_bio);
1230 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001231
1232 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1233 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001234 else {
1235 atomic_add(r10_bio->sectors,
1236 &conf->mirrors[d].rdev->corrected_errors);
1237 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1238 md_error(r10_bio->mddev,
1239 conf->mirrors[d].rdev);
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* for reconstruct, we always reschedule after a read.
1243 * for resync, only after all reads
1244 */
NeilBrown73d5c382009-02-25 13:18:47 +11001245 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1247 atomic_dec_and_test(&r10_bio->remaining)) {
1248 /* we have read all the blocks,
1249 * do the comparison in process context in raid10d
1250 */
1251 reschedule_retry(r10_bio);
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
NeilBrown6712ecf2007-09-27 12:47:43 +02001255static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001258 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001260 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int i,d;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 for (i = 0; i < conf->copies; i++)
1264 if (r10_bio->devs[i].bio == bio)
1265 break;
1266 d = r10_bio->devs[i].devnum;
1267
1268 if (!uptodate)
1269 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 update_head_pos(i, r10_bio);
1272
NeilBrown73d5c382009-02-25 13:18:47 +11001273 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 while (atomic_dec_and_test(&r10_bio->remaining)) {
1275 if (r10_bio->master_bio == NULL) {
1276 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001277 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001279 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 break;
1281 } else {
1282 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1283 put_buf(r10_bio);
1284 r10_bio = r10_bio2;
1285 }
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/*
1290 * Note: sync and recover and handled very differently for raid10
1291 * This code is for resync.
1292 * For resync, we read through virtual addresses and read all blocks.
1293 * If there is any error, we schedule a write. The lowest numbered
1294 * drive is authoritative.
1295 * However requests come for physical address, so we need to map.
1296 * For every physical address there are raid_disks/copies virtual addresses,
1297 * which is always are least one, but is not necessarly an integer.
1298 * This means that a physical address can span multiple chunks, so we may
1299 * have to submit multiple io requests for a single sync request.
1300 */
1301/*
1302 * We check if all blocks are in-sync and only write to blocks that
1303 * aren't in sync
1304 */
1305static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1306{
NeilBrown070ec552009-06-16 16:54:21 +10001307 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int i, first;
1309 struct bio *tbio, *fbio;
1310
1311 atomic_set(&r10_bio->remaining, 1);
1312
1313 /* find the first device with a block */
1314 for (i=0; i<conf->copies; i++)
1315 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1316 break;
1317
1318 if (i == conf->copies)
1319 goto done;
1320
1321 first = i;
1322 fbio = r10_bio->devs[i].bio;
1323
1324 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001325 for (i=0 ; i < conf->copies ; i++) {
1326 int j, d;
1327 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001330
1331 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001333 if (i == first)
1334 continue;
1335 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1336 /* We know that the bi_io_vec layout is the same for
1337 * both 'first' and 'i', so we just compare them.
1338 * All vec entries are PAGE_SIZE;
1339 */
1340 for (j = 0; j < vcnt; j++)
1341 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1342 page_address(tbio->bi_io_vec[j].bv_page),
1343 PAGE_SIZE))
1344 break;
1345 if (j == vcnt)
1346 continue;
1347 mddev->resync_mismatches += r10_bio->sectors;
1348 }
NeilBrown18f08812006-01-06 00:20:25 -08001349 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1350 /* Don't fix anything. */
1351 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* Ok, we need to write this bio
1353 * First we need to fixup bv_offset, bv_len and
1354 * bi_vecs, as the read request might have corrupted these
1355 */
1356 tbio->bi_vcnt = vcnt;
1357 tbio->bi_size = r10_bio->sectors << 9;
1358 tbio->bi_idx = 0;
1359 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1361 tbio->bi_flags |= 1 << BIO_UPTODATE;
1362 tbio->bi_next = NULL;
1363 tbio->bi_rw = WRITE;
1364 tbio->bi_private = r10_bio;
1365 tbio->bi_sector = r10_bio->devs[i].addr;
1366
1367 for (j=0; j < vcnt ; j++) {
1368 tbio->bi_io_vec[j].bv_offset = 0;
1369 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1370
1371 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1372 page_address(fbio->bi_io_vec[j].bv_page),
1373 PAGE_SIZE);
1374 }
1375 tbio->bi_end_io = end_sync_write;
1376
1377 d = r10_bio->devs[i].devnum;
1378 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1379 atomic_inc(&r10_bio->remaining);
1380 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1381
1382 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1383 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1384 generic_make_request(tbio);
1385 }
1386
1387done:
1388 if (atomic_dec_and_test(&r10_bio->remaining)) {
1389 md_done_sync(mddev, r10_bio->sectors, 1);
1390 put_buf(r10_bio);
1391 }
1392}
1393
1394/*
1395 * Now for the recovery code.
1396 * Recovery happens across physical sectors.
1397 * We recover all non-is_sync drives by finding the virtual address of
1398 * each, and then choose a working drive that also has that virt address.
1399 * There is a separate r10_bio for each non-in_sync drive.
1400 * Only the first two slots are in use. The first for reading,
1401 * The second for writing.
1402 *
1403 */
1404
1405static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1406{
NeilBrown070ec552009-06-16 16:54:21 +10001407 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int i, d;
1409 struct bio *bio, *wbio;
1410
1411
1412 /* move the pages across to the second bio
1413 * and submit the write request
1414 */
1415 bio = r10_bio->devs[0].bio;
1416 wbio = r10_bio->devs[1].bio;
1417 for (i=0; i < wbio->bi_vcnt; i++) {
1418 struct page *p = bio->bi_io_vec[i].bv_page;
1419 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1420 wbio->bi_io_vec[i].bv_page = p;
1421 }
1422 d = r10_bio->devs[1].devnum;
1423
1424 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1425 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001426 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1427 generic_make_request(wbio);
1428 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001429 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432
1433/*
Robert Becker1e509152009-12-14 12:49:58 +11001434 * Used by fix_read_error() to decay the per rdev read_errors.
1435 * We halve the read error count for every hour that has elapsed
1436 * since the last recorded read error.
1437 *
1438 */
1439static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1440{
1441 struct timespec cur_time_mon;
1442 unsigned long hours_since_last;
1443 unsigned int read_errors = atomic_read(&rdev->read_errors);
1444
1445 ktime_get_ts(&cur_time_mon);
1446
1447 if (rdev->last_read_error.tv_sec == 0 &&
1448 rdev->last_read_error.tv_nsec == 0) {
1449 /* first time we've seen a read error */
1450 rdev->last_read_error = cur_time_mon;
1451 return;
1452 }
1453
1454 hours_since_last = (cur_time_mon.tv_sec -
1455 rdev->last_read_error.tv_sec) / 3600;
1456
1457 rdev->last_read_error = cur_time_mon;
1458
1459 /*
1460 * if hours_since_last is > the number of bits in read_errors
1461 * just set read errors to 0. We do this to avoid
1462 * overflowing the shift of read_errors by hours_since_last.
1463 */
1464 if (hours_since_last >= 8 * sizeof(read_errors))
1465 atomic_set(&rdev->read_errors, 0);
1466 else
1467 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1468}
1469
1470/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * This is a kernel thread which:
1472 *
1473 * 1. Retries failed read operations on working mirrors.
1474 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001475 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 */
1477
NeilBrown6814d532006-10-03 01:15:45 -07001478static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1479{
1480 int sect = 0; /* Offset from r10_bio->sector */
1481 int sectors = r10_bio->sectors;
1482 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001483 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1484
1485 rcu_read_lock();
1486 {
1487 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1488 char b[BDEVNAME_SIZE];
1489 int cur_read_error_count = 0;
1490
1491 rdev = rcu_dereference(conf->mirrors[d].rdev);
1492 bdevname(rdev->bdev, b);
1493
1494 if (test_bit(Faulty, &rdev->flags)) {
1495 rcu_read_unlock();
1496 /* drive has already been failed, just ignore any
1497 more fix_read_error() attempts */
1498 return;
1499 }
1500
1501 check_decay_read_errors(mddev, rdev);
1502 atomic_inc(&rdev->read_errors);
1503 cur_read_error_count = atomic_read(&rdev->read_errors);
1504 if (cur_read_error_count > max_read_errors) {
1505 rcu_read_unlock();
1506 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001507 "md/raid10:%s: %s: Raid device exceeded "
Robert Becker1e509152009-12-14 12:49:58 +11001508 "read_error threshold "
1509 "[cur %d:max %d]\n",
NeilBrown128595e2010-05-03 14:47:14 +10001510 mdname(mddev),
Robert Becker1e509152009-12-14 12:49:58 +11001511 b, cur_read_error_count, max_read_errors);
1512 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001513 "md/raid10:%s: %s: Failing raid "
1514 "device\n", mdname(mddev), b);
Robert Becker1e509152009-12-14 12:49:58 +11001515 md_error(mddev, conf->mirrors[d].rdev);
1516 return;
1517 }
1518 }
1519 rcu_read_unlock();
1520
NeilBrown6814d532006-10-03 01:15:45 -07001521 while(sectors) {
1522 int s = sectors;
1523 int sl = r10_bio->read_slot;
1524 int success = 0;
1525 int start;
1526
1527 if (s > (PAGE_SIZE>>9))
1528 s = PAGE_SIZE >> 9;
1529
1530 rcu_read_lock();
1531 do {
1532 int d = r10_bio->devs[sl].devnum;
1533 rdev = rcu_dereference(conf->mirrors[d].rdev);
1534 if (rdev &&
1535 test_bit(In_sync, &rdev->flags)) {
1536 atomic_inc(&rdev->nr_pending);
1537 rcu_read_unlock();
1538 success = sync_page_io(rdev->bdev,
1539 r10_bio->devs[sl].addr +
1540 sect + rdev->data_offset,
1541 s<<9,
1542 conf->tmppage, READ);
1543 rdev_dec_pending(rdev, mddev);
1544 rcu_read_lock();
1545 if (success)
1546 break;
1547 }
1548 sl++;
1549 if (sl == conf->copies)
1550 sl = 0;
1551 } while (!success && sl != r10_bio->read_slot);
1552 rcu_read_unlock();
1553
1554 if (!success) {
1555 /* Cannot read from anywhere -- bye bye array */
1556 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1557 md_error(mddev, conf->mirrors[dn].rdev);
1558 break;
1559 }
1560
1561 start = sl;
1562 /* write it back and re-read */
1563 rcu_read_lock();
1564 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001565 char b[BDEVNAME_SIZE];
NeilBrown6814d532006-10-03 01:15:45 -07001566 int d;
1567 if (sl==0)
1568 sl = conf->copies;
1569 sl--;
1570 d = r10_bio->devs[sl].devnum;
1571 rdev = rcu_dereference(conf->mirrors[d].rdev);
1572 if (rdev &&
1573 test_bit(In_sync, &rdev->flags)) {
1574 atomic_inc(&rdev->nr_pending);
1575 rcu_read_unlock();
1576 atomic_add(s, &rdev->corrected_errors);
1577 if (sync_page_io(rdev->bdev,
1578 r10_bio->devs[sl].addr +
1579 sect + rdev->data_offset,
1580 s<<9, conf->tmppage, WRITE)
Robert Becker67b8dc42009-12-14 12:49:57 +11001581 == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001582 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001583 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001584 "md/raid10:%s: read correction "
Robert Becker67b8dc42009-12-14 12:49:57 +11001585 "write failed"
1586 " (%d sectors at %llu on %s)\n",
1587 mdname(mddev), s,
1588 (unsigned long long)(sect+
1589 rdev->data_offset),
1590 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001591 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
Robert Becker67b8dc42009-12-14 12:49:57 +11001592 "drive\n",
NeilBrown128595e2010-05-03 14:47:14 +10001593 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001594 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001595 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001596 }
NeilBrown6814d532006-10-03 01:15:45 -07001597 rdev_dec_pending(rdev, mddev);
1598 rcu_read_lock();
1599 }
1600 }
1601 sl = start;
1602 while (sl != r10_bio->read_slot) {
1603 int d;
1604 if (sl==0)
1605 sl = conf->copies;
1606 sl--;
1607 d = r10_bio->devs[sl].devnum;
1608 rdev = rcu_dereference(conf->mirrors[d].rdev);
1609 if (rdev &&
1610 test_bit(In_sync, &rdev->flags)) {
1611 char b[BDEVNAME_SIZE];
1612 atomic_inc(&rdev->nr_pending);
1613 rcu_read_unlock();
1614 if (sync_page_io(rdev->bdev,
1615 r10_bio->devs[sl].addr +
1616 sect + rdev->data_offset,
Robert Becker67b8dc42009-12-14 12:49:57 +11001617 s<<9, conf->tmppage,
1618 READ) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001619 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001620 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001621 "md/raid10:%s: unable to read back "
Robert Becker67b8dc42009-12-14 12:49:57 +11001622 "corrected sectors"
1623 " (%d sectors at %llu on %s)\n",
1624 mdname(mddev), s,
1625 (unsigned long long)(sect+
1626 rdev->data_offset),
1627 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001628 printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
1629 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001630 bdevname(rdev->bdev, b));
1631
NeilBrown6814d532006-10-03 01:15:45 -07001632 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001633 } else {
NeilBrown6814d532006-10-03 01:15:45 -07001634 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10001635 "md/raid10:%s: read error corrected"
NeilBrown6814d532006-10-03 01:15:45 -07001636 " (%d sectors at %llu on %s)\n",
1637 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001638 (unsigned long long)(sect+
1639 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001640 bdevname(rdev->bdev, b));
Robert Becker67b8dc42009-12-14 12:49:57 +11001641 }
NeilBrown6814d532006-10-03 01:15:45 -07001642
1643 rdev_dec_pending(rdev, mddev);
1644 rcu_read_lock();
1645 }
1646 }
1647 rcu_read_unlock();
1648
1649 sectors -= s;
1650 sect += s;
1651 }
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654static void raid10d(mddev_t *mddev)
1655{
1656 r10bio_t *r10_bio;
1657 struct bio *bio;
1658 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001659 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 struct list_head *head = &conf->retry_list;
1661 int unplug=0;
1662 mdk_rdev_t *rdev;
1663
1664 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 for (;;) {
1667 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001668
1669 unplug += flush_pending_writes(conf);
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001672 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001673 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1677 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001678 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 spin_unlock_irqrestore(&conf->device_lock, flags);
1680
1681 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001682 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1684 sync_request_write(mddev, r10_bio);
1685 unplug = 1;
1686 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1687 recovery_request_write(mddev, r10_bio);
1688 unplug = 1;
1689 } else {
1690 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001691 /* we got a read error. Maybe the drive is bad. Maybe just
1692 * the block and we can fix it.
1693 * We freeze all other IO, and try reading the block from
1694 * other devices. When we find one, we re-write
1695 * and check it that fixes the read error.
1696 * This is all done synchronously while the array is
1697 * frozen.
1698 */
NeilBrown6814d532006-10-03 01:15:45 -07001699 if (mddev->ro == 0) {
1700 freeze_array(conf);
1701 fix_read_error(conf, mddev, r10_bio);
1702 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001703 }
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001706 r10_bio->devs[r10_bio->read_slot].bio =
1707 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 mirror = read_balance(conf, r10_bio);
1709 if (mirror == -1) {
NeilBrown128595e2010-05-03 14:47:14 +10001710 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 " read error for block %llu\n",
NeilBrown128595e2010-05-03 14:47:14 +10001712 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 bdevname(bio->bi_bdev,b),
1714 (unsigned long long)r10_bio->sector);
1715 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001716 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 } else {
Jens Axboe1f98a132009-09-11 14:32:04 +02001718 const bool do_sync = bio_rw_flagged(r10_bio->master_bio, BIO_RW_SYNCIO);
Maik Hampel14e71342007-07-31 00:37:57 -07001719 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rdev = conf->mirrors[mirror].rdev;
1721 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +10001722 printk(KERN_ERR "md/raid10:%s: %s: redirecting sector %llu to"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 " another mirror\n",
NeilBrown128595e2010-05-03 14:47:14 +10001724 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 bdevname(rdev->bdev,b),
1726 (unsigned long long)r10_bio->sector);
1727 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1728 r10_bio->devs[r10_bio->read_slot].bio = bio;
1729 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1730 + rdev->data_offset;
1731 bio->bi_bdev = rdev->bdev;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +04001732 bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 bio->bi_private = r10_bio;
1734 bio->bi_end_io = raid10_end_read_request;
1735 unplug = 1;
1736 generic_make_request(bio);
1737 }
1738 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001739 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (unplug)
1742 unplug_slaves(mddev);
1743}
1744
1745
1746static int init_resync(conf_t *conf)
1747{
1748 int buffs;
1749
1750 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001751 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1753 if (!conf->r10buf_pool)
1754 return -ENOMEM;
1755 conf->next_resync = 0;
1756 return 0;
1757}
1758
1759/*
1760 * perform a "sync" on one "block"
1761 *
1762 * We need to make sure that no normal I/O request - particularly write
1763 * requests - conflict with active sync requests.
1764 *
1765 * This is achieved by tracking pending requests and a 'barrier' concept
1766 * that can be installed to exclude normal IO requests.
1767 *
1768 * Resync and recovery are handled very differently.
1769 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1770 *
1771 * For resync, we iterate over virtual addresses, read all copies,
1772 * and update if there are differences. If only one copy is live,
1773 * skip it.
1774 * For recovery, we iterate over physical addresses, read a good
1775 * value for each non-in_sync drive, and over-write.
1776 *
1777 * So, for recovery we may have several outstanding complex requests for a
1778 * given address, one for each out-of-sync device. We model this by allocating
1779 * a number of r10_bio structures, one for each out-of-sync device.
1780 * As we setup these structures, we collect all bio's together into a list
1781 * which we then process collectively to add pages, and then process again
1782 * to pass to generic_make_request.
1783 *
1784 * The r10_bio structures are linked using a borrowed master_bio pointer.
1785 * This link is counted in ->remaining. When the r10_bio that points to NULL
1786 * has its remaining count decremented to 0, the whole complex operation
1787 * is complete.
1788 *
1789 */
1790
NeilBrown57afd892005-06-21 17:17:13 -07001791static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
NeilBrown070ec552009-06-16 16:54:21 +10001793 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 r10bio_t *r10_bio;
1795 struct bio *biolist = NULL, *bio;
1796 sector_t max_sector, nr_sectors;
1797 int disk;
1798 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001799 int max_sync;
1800 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 sector_t sectors_skipped = 0;
1803 int chunks_skipped = 0;
1804
1805 if (!conf->r10buf_pool)
1806 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001810 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1812 max_sector = mddev->resync_max_sectors;
1813 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001814 /* If we aborted, we need to abort the
1815 * sync on the 'current' bitmap chucks (there can
1816 * be several when recovering multiple devices).
1817 * as we may have started syncing it but not finished.
1818 * We can find the current address in
1819 * mddev->curr_resync, but for recovery,
1820 * we need to convert that to several
1821 * virtual addresses.
1822 */
1823 if (mddev->curr_resync < max_sector) { /* aborted */
1824 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1825 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1826 &sync_blocks, 1);
1827 else for (i=0; i<conf->raid_disks; i++) {
1828 sector_t sect =
1829 raid10_find_virt(conf, mddev->curr_resync, i);
1830 bitmap_end_sync(mddev->bitmap, sect,
1831 &sync_blocks, 1);
1832 }
1833 } else /* completed sync */
1834 conf->fullsync = 0;
1835
1836 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001838 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return sectors_skipped;
1840 }
1841 if (chunks_skipped >= conf->raid_disks) {
1842 /* if there has been nothing to do on any drive,
1843 * then there is nothing to do at all..
1844 */
NeilBrown57afd892005-06-21 17:17:13 -07001845 *skipped = 1;
1846 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848
NeilBrownc6207272008-02-06 01:39:52 -08001849 if (max_sector > mddev->resync_max)
1850 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /* make sure whole request will fit in a chunk - if chunks
1853 * are meaningful
1854 */
1855 if (conf->near_copies < conf->raid_disks &&
1856 max_sector > (sector_nr | conf->chunk_mask))
1857 max_sector = (sector_nr | conf->chunk_mask) + 1;
1858 /*
1859 * If there is non-resync activity waiting for us then
1860 * put in a delay to throttle resync.
1861 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001862 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /* Again, very different code for resync and recovery.
1866 * Both must result in an r10bio with a list of bios that
1867 * have bi_end_io, bi_sector, bi_bdev set,
1868 * and bi_private set to the r10bio.
1869 * For recovery, we may actually create several r10bios
1870 * with 2 bios in each, that correspond to the bios in the main one.
1871 * In this case, the subordinate r10bios link back through a
1872 * borrowed master_bio pointer, and the counter in the master
1873 * includes a ref from each subordinate.
1874 */
1875 /* First, we decide what to do and set ->bi_end_io
1876 * To end_sync_read if we want to read, and
1877 * end_sync_write if we will want to write.
1878 */
1879
NeilBrown6cce3b22006-01-06 00:20:16 -08001880 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1882 /* recovery... the complicated one */
NeilBrowna9f326e2009-09-23 18:06:41 +10001883 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 r10_bio = NULL;
1885
1886 for (i=0 ; i<conf->raid_disks; i++)
1887 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001888 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001889 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* want to reconstruct this device */
1891 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001892 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1893 int must_sync;
1894 /* Unless we are doing a full sync, we only need
1895 * to recover the block if it is set in the bitmap
1896 */
1897 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1898 &sync_blocks, 1);
1899 if (sync_blocks < max_sync)
1900 max_sync = sync_blocks;
1901 if (!must_sync &&
1902 !conf->fullsync) {
1903 /* yep, skip the sync_blocks here, but don't assume
1904 * that there will never be anything to do here
1905 */
1906 chunks_skipped = -1;
1907 continue;
1908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001911 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 atomic_set(&r10_bio->remaining, 0);
1913
1914 r10_bio->master_bio = (struct bio*)rb2;
1915 if (rb2)
1916 atomic_inc(&rb2->remaining);
1917 r10_bio->mddev = mddev;
1918 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001919 r10_bio->sector = sect;
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001922
1923 /* Need to check if the array will still be
NeilBrown6cce3b22006-01-06 00:20:16 -08001924 * degraded
1925 */
NeilBrown18055562009-05-07 12:48:10 +10001926 for (j=0; j<conf->raid_disks; j++)
1927 if (conf->mirrors[j].rdev == NULL ||
1928 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001929 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001930 break;
1931 }
NeilBrown18055562009-05-07 12:48:10 +10001932
NeilBrown6cce3b22006-01-06 00:20:16 -08001933 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1934 &sync_blocks, still_degraded);
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 for (j=0; j<conf->copies;j++) {
1937 int d = r10_bio->devs[j].devnum;
1938 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001939 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* This is where we read from */
1941 bio = r10_bio->devs[0].bio;
1942 bio->bi_next = biolist;
1943 biolist = bio;
1944 bio->bi_private = r10_bio;
1945 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001946 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 bio->bi_sector = r10_bio->devs[j].addr +
1948 conf->mirrors[d].rdev->data_offset;
1949 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1950 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1951 atomic_inc(&r10_bio->remaining);
1952 /* and we write to 'i' */
1953
1954 for (k=0; k<conf->copies; k++)
1955 if (r10_bio->devs[k].devnum == i)
1956 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001957 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 bio = r10_bio->devs[1].bio;
1959 bio->bi_next = biolist;
1960 biolist = bio;
1961 bio->bi_private = r10_bio;
1962 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001963 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 bio->bi_sector = r10_bio->devs[k].addr +
1965 conf->mirrors[i].rdev->data_offset;
1966 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1967
1968 r10_bio->devs[0].devnum = d;
1969 r10_bio->devs[1].devnum = i;
1970
1971 break;
1972 }
1973 }
1974 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001975 /* Cannot recover, so abort the recovery */
1976 put_buf(r10_bio);
K.Tanakaa07e6ab2008-03-04 14:29:37 -08001977 if (rb2)
1978 atomic_dec(&rb2->remaining);
NeilBrown87fc7672005-09-09 16:24:04 -07001979 r10_bio = rb2;
NeilBrowndfc70642008-05-23 13:04:39 -07001980 if (!test_and_set_bit(MD_RECOVERY_INTR,
1981 &mddev->recovery))
NeilBrown128595e2010-05-03 14:47:14 +10001982 printk(KERN_INFO "md/raid10:%s: insufficient "
1983 "working devices for recovery.\n",
NeilBrown87fc7672005-09-09 16:24:04 -07001984 mdname(mddev));
1985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 }
1988 if (biolist == NULL) {
1989 while (r10_bio) {
1990 r10bio_t *rb2 = r10_bio;
1991 r10_bio = (r10bio_t*) rb2->master_bio;
1992 rb2->master_bio = NULL;
1993 put_buf(rb2);
1994 }
1995 goto giveup;
1996 }
1997 } else {
1998 /* resync. Schedule a read for every block at this virt offset */
1999 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002000
NeilBrown78200d42009-02-25 13:18:47 +11002001 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2002
NeilBrown6cce3b22006-01-06 00:20:16 -08002003 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2004 &sync_blocks, mddev->degraded) &&
2005 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2006 /* We can skip this block */
2007 *skipped = 1;
2008 return sync_blocks + sectors_skipped;
2009 }
2010 if (sync_blocks < max_sync)
2011 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 r10_bio->mddev = mddev;
2015 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002016 raise_barrier(conf, 0);
2017 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 r10_bio->master_bio = NULL;
2020 r10_bio->sector = sector_nr;
2021 set_bit(R10BIO_IsSync, &r10_bio->state);
2022 raid10_find_phys(conf, r10_bio);
2023 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2024
2025 for (i=0; i<conf->copies; i++) {
2026 int d = r10_bio->devs[i].devnum;
2027 bio = r10_bio->devs[i].bio;
2028 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002029 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002031 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 continue;
2033 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2034 atomic_inc(&r10_bio->remaining);
2035 bio->bi_next = biolist;
2036 biolist = bio;
2037 bio->bi_private = r10_bio;
2038 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002039 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 bio->bi_sector = r10_bio->devs[i].addr +
2041 conf->mirrors[d].rdev->data_offset;
2042 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2043 count++;
2044 }
2045
2046 if (count < 2) {
2047 for (i=0; i<conf->copies; i++) {
2048 int d = r10_bio->devs[i].devnum;
2049 if (r10_bio->devs[i].bio->bi_end_io)
2050 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
2051 }
2052 put_buf(r10_bio);
2053 biolist = NULL;
2054 goto giveup;
2055 }
2056 }
2057
2058 for (bio = biolist; bio ; bio=bio->bi_next) {
2059
2060 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2061 if (bio->bi_end_io)
2062 bio->bi_flags |= 1 << BIO_UPTODATE;
2063 bio->bi_vcnt = 0;
2064 bio->bi_idx = 0;
2065 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 bio->bi_size = 0;
2067 }
2068
2069 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002070 if (sector_nr + max_sync < max_sector)
2071 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 do {
2073 struct page *page;
2074 int len = PAGE_SIZE;
2075 disk = 0;
2076 if (sector_nr + (len>>9) > max_sector)
2077 len = (max_sector - sector_nr) << 9;
2078 if (len == 0)
2079 break;
2080 for (bio= biolist ; bio ; bio=bio->bi_next) {
2081 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2082 if (bio_add_page(bio, page, len, 0) == 0) {
2083 /* stop here */
2084 struct bio *bio2;
2085 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2086 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
2087 /* remove last page from this bio */
2088 bio2->bi_vcnt--;
2089 bio2->bi_size -= len;
2090 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2091 }
2092 goto bio_full;
2093 }
2094 disk = i;
2095 }
2096 nr_sectors += len>>9;
2097 sector_nr += len>>9;
2098 } while (biolist->bi_vcnt < RESYNC_PAGES);
2099 bio_full:
2100 r10_bio->sectors = nr_sectors;
2101
2102 while (biolist) {
2103 bio = biolist;
2104 biolist = biolist->bi_next;
2105
2106 bio->bi_next = NULL;
2107 r10_bio = bio->bi_private;
2108 r10_bio->sectors = nr_sectors;
2109
2110 if (bio->bi_end_io == end_sync_read) {
2111 md_sync_acct(bio->bi_bdev, nr_sectors);
2112 generic_make_request(bio);
2113 }
2114 }
2115
NeilBrown57afd892005-06-21 17:17:13 -07002116 if (sectors_skipped)
2117 /* pretend they weren't skipped, it makes
2118 * no important difference in this case
2119 */
2120 md_done_sync(mddev, sectors_skipped, 1);
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return sectors_skipped + nr_sectors;
2123 giveup:
2124 /* There is nowhere to write, so all non-sync
2125 * drives must be failed, so try the next chunk...
2126 */
NeilBrown09b40682009-02-25 13:18:47 +11002127 if (sector_nr + max_sync < max_sector)
2128 max_sector = sector_nr + max_sync;
2129
2130 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 chunks_skipped ++;
2132 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Dan Williams80c3a6c2009-03-17 18:10:40 -07002136static sector_t
2137raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2138{
2139 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002140 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002141
2142 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002143 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002144 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002145 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002146
2147 size = sectors >> conf->chunk_shift;
2148 sector_div(size, conf->far_copies);
2149 size = size * raid_disks;
2150 sector_div(size, conf->near_copies);
2151
2152 return size << conf->chunk_shift;
2153}
2154
Trela, Maciejdab8b292010-03-08 16:02:45 +11002155
2156static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002158 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002159 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002161 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Andre Noll9d8f0362009-06-18 08:45:01 +10002163 if (mddev->chunk_sectors < (PAGE_SIZE >> 9) ||
2164 !is_power_of_2(mddev->chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002165 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2166 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2167 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
NeilBrown2604b702006-01-06 00:20:36 -08002170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 nc = mddev->layout & 255;
2172 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07002173 fo = mddev->layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07002176 (mddev->layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002177 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 mdname(mddev), mddev->layout);
2179 goto out;
2180 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002181
2182 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002183 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002184 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002186
NeilBrown4443ae12006-01-06 00:20:28 -08002187 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002188 GFP_KERNEL);
2189 if (!conf->mirrors)
2190 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002191
2192 conf->tmppage = alloc_page(GFP_KERNEL);
2193 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002194 goto out;
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
NeilBrown64a742b2007-02-28 20:11:18 -08002197 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 conf->near_copies = nc;
2199 conf->far_copies = fc;
2200 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002201 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002202 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2203 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2204
2205 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2206 r10bio_pool_free, conf);
2207 if (!conf->r10bio_pool)
2208 goto out;
2209
Andre Noll58c0fed2009-03-31 14:33:13 +11002210 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002211 sector_div(size, fc);
2212 size = size * conf->raid_disks;
2213 sector_div(size, nc);
2214 /* 'size' is now the number of chunks in the array */
2215 /* calculate "used chunks per device" in 'stride' */
2216 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002217
2218 /* We need to round up when dividing by raid_disks to
2219 * get the stride size.
2220 */
2221 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002222 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002223
2224 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002225
NeilBrownc93983b2006-06-26 00:27:41 -07002226 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002227 stride = 1;
2228 else
NeilBrownc93983b2006-06-26 00:27:41 -07002229 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002230 conf->stride = stride << conf->chunk_shift;
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Neil Browne7e72bf2008-05-14 16:05:54 -07002233 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002234 INIT_LIST_HEAD(&conf->retry_list);
2235
2236 spin_lock_init(&conf->resync_lock);
2237 init_waitqueue_head(&conf->wait_barrier);
2238
2239 conf->thread = md_register_thread(raid10d, mddev, NULL);
2240 if (!conf->thread)
2241 goto out;
2242
2243 conf->scale_disks = 0;
2244 conf->mddev = mddev;
2245 return conf;
2246
2247 out:
NeilBrown128595e2010-05-03 14:47:14 +10002248 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002249 mdname(mddev));
2250 if (conf) {
2251 if (conf->r10bio_pool)
2252 mempool_destroy(conf->r10bio_pool);
2253 kfree(conf->mirrors);
2254 safe_put_page(conf->tmppage);
2255 kfree(conf);
2256 }
2257 return ERR_PTR(err);
2258}
2259
2260static int run(mddev_t *mddev)
2261{
2262 conf_t *conf;
2263 int i, disk_idx, chunk_size;
2264 mirror_info_t *disk;
2265 mdk_rdev_t *rdev;
2266 sector_t size;
2267
2268 /*
2269 * copy the already verified devices into our private RAID10
2270 * bookkeeping area. [whatever we allocate in run(),
2271 * should be freed in stop()]
2272 */
2273
2274 if (mddev->private == NULL) {
2275 conf = setup_conf(mddev);
2276 if (IS_ERR(conf))
2277 return PTR_ERR(conf);
2278 mddev->private = conf;
2279 }
2280 conf = mddev->private;
2281 if (!conf)
2282 goto out;
2283
Neil Browne7e72bf2008-05-14 16:05:54 -07002284 mddev->queue->queue_lock = &conf->device_lock;
2285
Trela, Maciejdab8b292010-03-08 16:02:45 +11002286 mddev->thread = conf->thread;
2287 conf->thread = NULL;
2288
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002289 chunk_size = mddev->chunk_sectors << 9;
2290 blk_queue_io_min(mddev->queue, chunk_size);
2291 if (conf->raid_disks % conf->near_copies)
2292 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2293 else
2294 blk_queue_io_opt(mddev->queue, chunk_size *
2295 (conf->raid_disks / conf->near_copies));
2296
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002297 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002299 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 || disk_idx < 0)
2301 continue;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002302 if (conf->scale_disks) {
2303 disk_idx *= conf->scale_disks;
2304 rdev->raid_disk = disk_idx;
2305 /* MOVE 'rd%d' link !! */
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 disk = conf->mirrors + disk_idx;
2308
2309 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002310 disk_stack_limits(mddev->gendisk, rdev->bdev,
2311 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002313 * violating it, so limit max_segments to 1 lying
2314 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 */
NeilBrown627a2d32010-03-08 16:44:38 +11002316 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2317 blk_queue_max_segments(mddev->queue, 1);
2318 blk_queue_segment_boundary(mddev->queue,
2319 PAGE_CACHE_SIZE - 1);
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
NeilBrown6d508242005-09-09 16:24:03 -07002324 /* need to check that every block has at least one working mirror */
2325 if (!enough(conf)) {
NeilBrown128595e2010-05-03 14:47:14 +10002326 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002327 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto out_free_conf;
2329 }
2330
2331 mddev->degraded = 0;
2332 for (i = 0; i < conf->raid_disks; i++) {
2333
2334 disk = conf->mirrors + i;
2335
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002336 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002337 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 disk->head_position = 0;
2339 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002340 if (disk->rdev)
2341 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343 }
2344
Andre Noll8c6ac862009-06-18 08:48:06 +10002345 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002346 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002347 " -- starting background reconstruction\n",
2348 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002350 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002351 mdname(mddev), conf->raid_disks - mddev->degraded,
2352 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /*
2354 * Ok, everything is just fine now
2355 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002356 mddev->dev_sectors = conf->dev_sectors;
2357 size = raid10_size(mddev, 0, 0);
2358 md_set_array_sectors(mddev, size);
2359 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
NeilBrown7a5febe2005-05-16 21:53:16 -07002361 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002362 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2363 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /* Calculate max read-ahead size.
2366 * We need to readahead at least twice a whole stripe....
2367 * maybe...
2368 */
2369 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002370 int stripe = conf->raid_disks *
2371 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 stripe /= conf->near_copies;
2373 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2374 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2375 }
2376
NeilBrown84707f32010-03-16 17:23:35 +11002377 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Andre Nollac5e7112009-08-03 10:59:47 +10002379 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return 0;
2381
2382out_free_conf:
2383 if (conf->r10bio_pool)
2384 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002385 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002386 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 kfree(conf);
2388 mddev->private = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002389 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390out:
2391 return -EIO;
2392}
2393
2394static int stop(mddev_t *mddev)
2395{
NeilBrown070ec552009-06-16 16:54:21 +10002396 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
NeilBrown409c57f2009-03-31 14:39:39 +11002398 raise_barrier(conf, 0);
2399 lower_barrier(conf);
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 md_unregister_thread(mddev->thread);
2402 mddev->thread = NULL;
2403 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2404 if (conf->r10bio_pool)
2405 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002406 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 kfree(conf);
2408 mddev->private = NULL;
2409 return 0;
2410}
2411
NeilBrown6cce3b22006-01-06 00:20:16 -08002412static void raid10_quiesce(mddev_t *mddev, int state)
2413{
NeilBrown070ec552009-06-16 16:54:21 +10002414 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002415
2416 switch(state) {
2417 case 1:
2418 raise_barrier(conf, 0);
2419 break;
2420 case 0:
2421 lower_barrier(conf);
2422 break;
2423 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Trela, Maciejdab8b292010-03-08 16:02:45 +11002426static void *raid10_takeover_raid0(mddev_t *mddev)
2427{
2428 mdk_rdev_t *rdev;
2429 conf_t *conf;
2430
2431 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10002432 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2433 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002434 return ERR_PTR(-EINVAL);
2435 }
2436
2437 /* Update slot numbers to obtain
2438 * degraded raid10 with missing mirrors
2439 */
2440 list_for_each_entry(rdev, &mddev->disks, same_set) {
2441 rdev->raid_disk *= 2;
2442 }
2443
2444 /* Set new parameters */
2445 mddev->new_level = 10;
2446 /* new layout: far_copies = 1, near_copies = 2 */
2447 mddev->new_layout = (1<<8) + 2;
2448 mddev->new_chunk_sectors = mddev->chunk_sectors;
2449 mddev->delta_disks = mddev->raid_disks;
2450 mddev->degraded = mddev->raid_disks;
2451 mddev->raid_disks *= 2;
2452 /* make sure it will be not marked as dirty */
2453 mddev->recovery_cp = MaxSector;
2454
2455 conf = setup_conf(mddev);
2456 conf->scale_disks = 2;
2457 return conf;
2458}
2459
2460static void *raid10_takeover(mddev_t *mddev)
2461{
2462 struct raid0_private_data *raid0_priv;
2463
2464 /* raid10 can take over:
2465 * raid0 - providing it has only two drives
2466 */
2467 if (mddev->level == 0) {
2468 /* for raid0 takeover only one zone is supported */
2469 raid0_priv = mddev->private;
2470 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10002471 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2472 " with more than one zone.\n",
2473 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002474 return ERR_PTR(-EINVAL);
2475 }
2476 return raid10_takeover_raid0(mddev);
2477 }
2478 return ERR_PTR(-EINVAL);
2479}
2480
NeilBrown2604b702006-01-06 00:20:36 -08002481static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002484 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 .owner = THIS_MODULE,
2486 .make_request = make_request,
2487 .run = run,
2488 .stop = stop,
2489 .status = status,
2490 .error_handler = error,
2491 .hot_add_disk = raid10_add_disk,
2492 .hot_remove_disk= raid10_remove_disk,
2493 .spare_active = raid10_spare_active,
2494 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002495 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002496 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002497 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498};
2499
2500static int __init raid_init(void)
2501{
NeilBrown2604b702006-01-06 00:20:36 -08002502 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505static void raid_exit(void)
2506{
NeilBrown2604b702006-01-06 00:20:36 -08002507 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510module_init(raid_init);
2511module_exit(raid_exit);
2512MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002513MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002515MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002516MODULE_ALIAS("md-level-10");