blob: a1d727610a491b8ae023fa2c05389adc62673261 [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())
288 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
289 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
290 reschedule_retry(r10_bio);
291 }
292
293 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
NeilBrown6712ecf2007-09-27 12:47:43 +0200296static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100299 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000301 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 for (slot = 0; slot < conf->copies; slot++)
304 if (r10_bio->devs[slot].bio == bio)
305 break;
306 dev = r10_bio->devs[slot].devnum;
307
308 /*
309 * this branch is our 'one mirror IO has finished' event handler:
310 */
NeilBrown6cce3b232006-01-06 00:20:16 -0800311 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b232006-01-06 00:20:16 -0800313 /* an I/O failed, we can't clear the bitmap */
314 set_bit(R10BIO_Degraded, &r10_bio->state);
315 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /*
317 * Set R10BIO_Uptodate in our master bio, so that
318 * we will return a good error code for to the higher
319 * levels even if IO on some other mirrored buffer fails.
320 *
321 * The 'master' represents the composite IO operation to
322 * user-side. So if something waits for IO, then it will
323 * wait for the 'master' bio.
324 */
325 set_bit(R10BIO_Uptodate, &r10_bio->state);
326
327 update_head_pos(slot, r10_bio);
328
329 /*
330 *
331 * Let's see if all mirrored write operations have finished
332 * already.
333 */
334 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b232006-01-06 00:20:16 -0800335 /* clear the bitmap if all writes complete successfully */
336 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
337 r10_bio->sectors,
338 !test_bit(R10BIO_Degraded, &r10_bio->state),
339 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 md_write_end(r10_bio->mddev);
341 raid_end_bio_io(r10_bio);
342 }
343
344 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347
348/*
349 * RAID10 layout manager
350 * Aswell as the chunksize and raid_disks count, there are two
351 * parameters: near_copies and far_copies.
352 * near_copies * far_copies must be <= raid_disks.
353 * Normally one of these will be 1.
354 * If both are 1, we get raid0.
355 * If near_copies == raid_disks, we get raid1.
356 *
357 * Chunks are layed out in raid0 style with near_copies copies of the
358 * first chunk, followed by near_copies copies of the next chunk and
359 * so on.
360 * If far_copies > 1, then after 1/far_copies of the array has been assigned
361 * as described above, we start again with a device offset of near_copies.
362 * So we effectively have another copy of the whole array further down all
363 * the drives, but with blocks on different drives.
364 * With this layout, and block is never stored twice on the one device.
365 *
366 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700367 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
369 * raid10_find_virt does the reverse mapping, from a device and a
370 * sector offset to a virtual address
371 */
372
373static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
374{
375 int n,f;
376 sector_t sector;
377 sector_t chunk;
378 sector_t stripe;
379 int dev;
380
381 int slot = 0;
382
383 /* now calculate first sector/dev */
384 chunk = r10bio->sector >> conf->chunk_shift;
385 sector = r10bio->sector & conf->chunk_mask;
386
387 chunk *= conf->near_copies;
388 stripe = chunk;
389 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700390 if (conf->far_offset)
391 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 sector += stripe << conf->chunk_shift;
394
395 /* and calculate all the others */
396 for (n=0; n < conf->near_copies; n++) {
397 int d = dev;
398 sector_t s = sector;
399 r10bio->devs[slot].addr = sector;
400 r10bio->devs[slot].devnum = d;
401 slot++;
402
403 for (f = 1; f < conf->far_copies; f++) {
404 d += conf->near_copies;
405 if (d >= conf->raid_disks)
406 d -= conf->raid_disks;
407 s += conf->stride;
408 r10bio->devs[slot].devnum = d;
409 r10bio->devs[slot].addr = s;
410 slot++;
411 }
412 dev++;
413 if (dev >= conf->raid_disks) {
414 dev = 0;
415 sector += (conf->chunk_mask + 1);
416 }
417 }
418 BUG_ON(slot != conf->copies);
419}
420
421static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
422{
423 sector_t offset, chunk, vchunk;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700426 if (conf->far_offset) {
427 int fc;
428 chunk = sector >> conf->chunk_shift;
429 fc = sector_div(chunk, conf->far_copies);
430 dev -= fc * conf->near_copies;
431 if (dev < 0)
432 dev += conf->raid_disks;
433 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800434 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700435 sector -= conf->stride;
436 if (dev < conf->near_copies)
437 dev += conf->raid_disks - conf->near_copies;
438 else
439 dev -= conf->near_copies;
440 }
441 chunk = sector >> conf->chunk_shift;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 vchunk = chunk * conf->raid_disks + dev;
444 sector_div(vchunk, conf->near_copies);
445 return (vchunk << conf->chunk_shift) + offset;
446}
447
448/**
449 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
450 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200451 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * @biovec: the request that could be merged to it.
453 *
454 * Return amount of bytes we can accept at this offset
455 * If near_copies == raid_disk, there are no striping issues,
456 * but in that case, the function isn't called at all.
457 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200458static int raid10_mergeable_bvec(struct request_queue *q,
459 struct bvec_merge_data *bvm,
460 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200463 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000465 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200466 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
469 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200470 if (max <= biovec->bv_len && bio_sectors == 0)
471 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 else
473 return max;
474}
475
476/*
477 * This routine returns the disk from which the requested read should
478 * be done. There is a per-array 'next expected sequential IO' sector
479 * number - if this matches on the next IO then we use the last disk.
480 * There is also a per-disk 'last know head position' sector that is
481 * maintained from IRQ contexts, both the normal and the resync IO
482 * completion handlers update this position correctly. If there is no
483 * perfect sequential match then we pick the disk whose head is closest.
484 *
485 * If there are 2 mirrors in the same 2 devices, performance degrades
486 * because position is mirror, not device based.
487 *
488 * The rdev for the device selected will have nr_pending incremented.
489 */
490
491/*
492 * FIXME: possibly should rethink readbalancing and do it differently
493 * depending on near_copies / far_copies geometry.
494 */
495static int read_balance(conf_t *conf, r10bio_t *r10_bio)
496{
497 const unsigned long this_sector = r10_bio->sector;
498 int disk, slot, nslot;
499 const int sectors = r10_bio->sectors;
500 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800501 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 raid10_find_phys(conf, r10_bio);
504 rcu_read_lock();
505 /*
506 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b232006-01-06 00:20:16 -0800507 * device if no resync is going on (recovery is ok), or below
508 * the resync window. We take the first readable disk when
509 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
511 if (conf->mddev->recovery_cp < MaxSector
512 && (this_sector + sectors >= conf->next_resync)) {
513 /* make sure that disk is operational */
514 slot = 0;
515 disk = r10_bio->devs[slot].devnum;
516
Suzanne Woodd6065f72005-11-08 21:39:27 -0800517 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800518 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800519 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 slot++;
521 if (slot == conf->copies) {
522 slot = 0;
523 disk = -1;
524 break;
525 }
526 disk = r10_bio->devs[slot].devnum;
527 }
528 goto rb_out;
529 }
530
531
532 /* make sure the disk is operational */
533 slot = 0;
534 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800535 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800536 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800537 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 slot ++;
539 if (slot == conf->copies) {
540 disk = -1;
541 goto rb_out;
542 }
543 disk = r10_bio->devs[slot].devnum;
544 }
545
546
NeilBrown3ec67ac2005-09-09 16:23:40 -0700547 current_distance = abs(r10_bio->devs[slot].addr -
548 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800550 /* Find the disk whose head is closest,
551 * or - for far > 1 - find the closest to partition beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 for (nslot = slot; nslot < conf->copies; nslot++) {
554 int ndisk = r10_bio->devs[nslot].devnum;
555
556
Suzanne Woodd6065f72005-11-08 21:39:27 -0800557 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800558 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800559 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 continue;
561
NeilBrown22dfdf52005-11-28 13:44:09 -0800562 /* This optimisation is debatable, and completely destroys
563 * sequential read speed for 'far copies' arrays. So only
564 * keep it for 'near' arrays, and review those later.
565 */
566 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 disk = ndisk;
568 slot = nslot;
569 break;
570 }
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800571
572 /* for far > 1 always use the lowest address */
573 if (conf->far_copies > 1)
574 new_distance = r10_bio->devs[nslot].addr;
575 else
576 new_distance = abs(r10_bio->devs[nslot].addr -
577 conf->mirrors[ndisk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (new_distance < current_distance) {
579 current_distance = new_distance;
580 disk = ndisk;
581 slot = nslot;
582 }
583 }
584
585rb_out:
586 r10_bio->read_slot = slot;
587/* conf->next_seq_sect = this_sector + sectors;*/
588
Suzanne Woodd6065f72005-11-08 21:39:27 -0800589 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800591 else
592 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 rcu_read_unlock();
594
595 return disk;
596}
597
598static void unplug_slaves(mddev_t *mddev)
599{
NeilBrown070ec552009-06-16 16:54:21 +1000600 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int i;
602
603 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100604 for (i=0; i < conf->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800605 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800606 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200607 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 atomic_inc(&rdev->nr_pending);
610 rcu_read_unlock();
611
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500612 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 rdev_dec_pending(rdev, mddev);
615 rcu_read_lock();
616 }
617 }
618 rcu_read_unlock();
619}
620
Jens Axboe165125e2007-07-24 09:28:11 +0200621static void raid10_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
NeilBrown6cce3b232006-01-06 00:20:16 -0800623 mddev_t *mddev = q->queuedata;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unplug_slaves(q->queuedata);
NeilBrown6cce3b232006-01-06 00:20:16 -0800626 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
NeilBrown0d129222006-10-03 01:15:54 -0700629static int raid10_congested(void *data, int bits)
630{
631 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000632 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700633 int i, ret = 0;
634
NeilBrown3fa841d2009-09-23 18:10:29 +1000635 if (mddev_congested(mddev, bits))
636 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700637 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100638 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700639 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
640 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200641 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700642
643 ret |= bdi_congested(&q->backing_dev_info, bits);
644 }
645 }
646 rcu_read_unlock();
647 return ret;
648}
649
NeilBrowna35e63e2008-03-04 14:29:29 -0800650static int flush_pending_writes(conf_t *conf)
651{
652 /* Any writes that have been queued but are awaiting
653 * bitmap updates get flushed here.
654 * We return 1 if any requests were actually submitted.
655 */
656 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700657
NeilBrowna35e63e2008-03-04 14:29:29 -0800658 spin_lock_irq(&conf->device_lock);
659
660 if (conf->pending_bio_list.head) {
661 struct bio *bio;
662 bio = bio_list_get(&conf->pending_bio_list);
663 blk_remove_plug(conf->mddev->queue);
664 spin_unlock_irq(&conf->device_lock);
665 /* flush any pending bitmap writes to disk
666 * before proceeding w/ I/O */
667 bitmap_unplug(conf->mddev->bitmap);
668
669 while (bio) { /* submit pending writes */
670 struct bio *next = bio->bi_next;
671 bio->bi_next = NULL;
672 generic_make_request(bio);
673 bio = next;
674 }
675 rv = 1;
676 } else
677 spin_unlock_irq(&conf->device_lock);
678 return rv;
679}
NeilBrown0a27ec92006-01-06 00:20:13 -0800680/* Barriers....
681 * Sometimes we need to suspend IO while we do something else,
682 * either some resync/recovery, or reconfigure the array.
683 * To do this we raise a 'barrier'.
684 * The 'barrier' is a counter that can be raised multiple times
685 * to count how many activities are happening which preclude
686 * normal IO.
687 * We can only raise the barrier if there is no pending IO.
688 * i.e. if nr_pending == 0.
689 * We choose only to raise the barrier if no-one is waiting for the
690 * barrier to go down. This means that as soon as an IO request
691 * is ready, no other operations which require a barrier will start
692 * until the IO request has had a chance.
693 *
694 * So: regular IO calls 'wait_barrier'. When that returns there
695 * is no backgroup IO happening, It must arrange to call
696 * allow_barrier when it has finished its IO.
697 * backgroup IO calls must call raise_barrier. Once that returns
698 * there is no normal IO happeing. It must arrange to call
699 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
NeilBrown6cce3b232006-01-06 00:20:16 -0800702static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
NeilBrown6cce3b232006-01-06 00:20:16 -0800704 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
NeilBrown6cce3b232006-01-06 00:20:16 -0800707 /* Wait until no block IO is waiting (unless 'force') */
708 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800709 conf->resync_lock,
710 raid10_unplug(conf->mddev->queue));
711
712 /* block any new IO from starting */
713 conf->barrier++;
714
715 /* No wait for all pending IO to complete */
716 wait_event_lock_irq(conf->wait_barrier,
717 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
718 conf->resync_lock,
719 raid10_unplug(conf->mddev->queue));
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 spin_unlock_irq(&conf->resync_lock);
722}
723
NeilBrown0a27ec92006-01-06 00:20:13 -0800724static void lower_barrier(conf_t *conf)
725{
726 unsigned long flags;
727 spin_lock_irqsave(&conf->resync_lock, flags);
728 conf->barrier--;
729 spin_unlock_irqrestore(&conf->resync_lock, flags);
730 wake_up(&conf->wait_barrier);
731}
732
733static void wait_barrier(conf_t *conf)
734{
735 spin_lock_irq(&conf->resync_lock);
736 if (conf->barrier) {
737 conf->nr_waiting++;
738 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
739 conf->resync_lock,
740 raid10_unplug(conf->mddev->queue));
741 conf->nr_waiting--;
742 }
743 conf->nr_pending++;
744 spin_unlock_irq(&conf->resync_lock);
745}
746
747static void allow_barrier(conf_t *conf)
748{
749 unsigned long flags;
750 spin_lock_irqsave(&conf->resync_lock, flags);
751 conf->nr_pending--;
752 spin_unlock_irqrestore(&conf->resync_lock, flags);
753 wake_up(&conf->wait_barrier);
754}
755
NeilBrown4443ae12006-01-06 00:20:28 -0800756static void freeze_array(conf_t *conf)
757{
758 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800759 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800760 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800761 * wait until nr_pending match nr_queued+1
762 * This is called in the context of one normal IO request
763 * that has failed. Thus any sync request that might be pending
764 * will be blocked by nr_pending, and we need to wait for
765 * pending IO requests to complete or be queued for re-try.
766 * Thus the number queued (nr_queued) plus this request (1)
767 * must match the number of pending IOs (nr_pending) before
768 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800769 */
770 spin_lock_irq(&conf->resync_lock);
771 conf->barrier++;
772 conf->nr_waiting++;
773 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800774 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800775 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800776 ({ flush_pending_writes(conf);
777 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800778 spin_unlock_irq(&conf->resync_lock);
779}
780
781static void unfreeze_array(conf_t *conf)
782{
783 /* reverse the effect of the freeze */
784 spin_lock_irq(&conf->resync_lock);
785 conf->barrier--;
786 conf->nr_waiting--;
787 wake_up(&conf->wait_barrier);
788 spin_unlock_irq(&conf->resync_lock);
789}
790
NeilBrown21a52c62010-04-01 15:02:13 +1100791static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
NeilBrown070ec552009-06-16 16:54:21 +1000793 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mirror_info_t *mirror;
795 r10bio_t *r10_bio;
796 struct bio *read_bio;
797 int i;
798 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100799 const int rw = bio_data_dir(bio);
Jens Axboe1f98a132009-09-11 14:32:04 +0200800 const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
NeilBrown6cce3b232006-01-06 00:20:16 -0800801 struct bio_list bl;
802 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700803 mdk_rdev_t *blocked_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Jens Axboe1f98a132009-09-11 14:32:04 +0200805 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
NeilBrowna2826aa2009-12-14 12:49:49 +1100806 md_barrier_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700807 return 0;
808 }
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /* If this request crosses a chunk boundary, we need to
811 * split it. This will only happen for 1 PAGE (or less) requests.
812 */
813 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
814 > chunk_sects &&
815 conf->near_copies < conf->raid_disks)) {
816 struct bio_pair *bp;
817 /* Sanity check -- queue functions should prevent this happening */
818 if (bio->bi_vcnt != 1 ||
819 bio->bi_idx != 0)
820 goto bad_map;
821 /* This is a one page bio that upper layers
822 * refuse to split for us, so we need to split it.
823 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200824 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown21a52c62010-04-01 15:02:13 +1100826 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100828 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 generic_make_request(&bp->bio2);
830
831 bio_pair_release(bp);
832 return 0;
833 bad_map:
834 printk("raid10_make_request bug: can't convert block across chunks"
835 " or bigger than %dk %llu %d\n", chunk_sects/2,
836 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
837
NeilBrown6712ecf2007-09-27 12:47:43 +0200838 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840 }
841
NeilBrown3d310eb2005-06-21 17:17:26 -0700842 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * Register the new request and wait if the reconstruction
846 * thread has put up a bar for new requests.
847 * Continue immediately if no resync is active currently.
848 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800849 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
852
853 r10_bio->master_bio = bio;
854 r10_bio->sectors = bio->bi_size >> 9;
855
856 r10_bio->mddev = mddev;
857 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b232006-01-06 00:20:16 -0800858 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Jens Axboea3623572005-11-01 09:26:16 +0100860 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
862 * read balancing logic:
863 */
864 int disk = read_balance(conf, r10_bio);
865 int slot = r10_bio->read_slot;
866 if (disk < 0) {
867 raid_end_bio_io(r10_bio);
868 return 0;
869 }
870 mirror = conf->mirrors + disk;
871
872 read_bio = bio_clone(bio, GFP_NOIO);
873
874 r10_bio->devs[slot].bio = read_bio;
875
876 read_bio->bi_sector = r10_bio->devs[slot].addr +
877 mirror->rdev->data_offset;
878 read_bio->bi_bdev = mirror->rdev->bdev;
879 read_bio->bi_end_io = raid10_end_read_request;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +0400880 read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 read_bio->bi_private = r10_bio;
882
883 generic_make_request(read_bio);
884 return 0;
885 }
886
887 /*
888 * WRITE:
889 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700890 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * inc refcount on their rdev. Record them by setting
892 * bios[x] to bio
893 */
894 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700895 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700896 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 rcu_read_lock();
898 for (i = 0; i < conf->copies; i++) {
899 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800900 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700901 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
902 atomic_inc(&rdev->nr_pending);
903 blocked_rdev = rdev;
904 break;
905 }
906 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800907 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b232006-01-06 00:20:16 -0800909 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b232006-01-06 00:20:16 -0800911 set_bit(R10BIO_Degraded, &r10_bio->state);
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 rcu_read_unlock();
915
Dan Williams6bfe0b42008-04-30 00:52:32 -0700916 if (unlikely(blocked_rdev)) {
917 /* Have to wait for this device to get unblocked, then retry */
918 int j;
919 int d;
920
921 for (j = 0; j < i; j++)
922 if (r10_bio->devs[j].bio) {
923 d = r10_bio->devs[j].devnum;
924 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
925 }
926 allow_barrier(conf);
927 md_wait_for_blocked_rdev(blocked_rdev, mddev);
928 wait_barrier(conf);
929 goto retry_write;
930 }
931
NeilBrown6cce3b232006-01-06 00:20:16 -0800932 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700933
NeilBrown6cce3b232006-01-06 00:20:16 -0800934 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 for (i = 0; i < conf->copies; i++) {
936 struct bio *mbio;
937 int d = r10_bio->devs[i].devnum;
938 if (!r10_bio->devs[i].bio)
939 continue;
940
941 mbio = bio_clone(bio, GFP_NOIO);
942 r10_bio->devs[i].bio = mbio;
943
944 mbio->bi_sector = r10_bio->devs[i].addr+
945 conf->mirrors[d].rdev->data_offset;
946 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
947 mbio->bi_end_io = raid10_end_write_request;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +0400948 mbio->bi_rw = WRITE | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 mbio->bi_private = r10_bio;
950
951 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b232006-01-06 00:20:16 -0800952 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
Arne Redlichf6f953a2007-07-31 00:37:57 -0700955 if (unlikely(!atomic_read(&r10_bio->remaining))) {
956 /* the array is dead */
957 md_write_end(mddev);
958 raid_end_bio_io(r10_bio);
959 return 0;
960 }
961
NeilBrown6cce3b232006-01-06 00:20:16 -0800962 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
963 spin_lock_irqsave(&conf->device_lock, flags);
964 bio_list_merge(&conf->pending_bio_list, &bl);
965 blk_plug_device(mddev->queue);
966 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
NeilBrowna35e63e2008-03-04 14:29:29 -0800968 /* In case raid10d snuck in to freeze_array */
969 wake_up(&conf->wait_barrier);
970
Lars Ellenberge3881a62007-01-10 23:15:37 -0800971 if (do_sync)
972 md_wakeup_thread(mddev->thread);
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
976
977static void status(struct seq_file *seq, mddev_t *mddev)
978{
NeilBrown070ec552009-06-16 16:54:21 +1000979 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int i;
981
982 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +1000983 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (conf->near_copies > 1)
985 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700986 if (conf->far_copies > 1) {
987 if (conf->far_offset)
988 seq_printf(seq, " %d offset-copies", conf->far_copies);
989 else
990 seq_printf(seq, " %d far-copies", conf->far_copies);
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700993 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 for (i = 0; i < conf->raid_disks; i++)
995 seq_printf(seq, "%s",
996 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800997 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 seq_printf(seq, "]");
999}
1000
1001static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1002{
1003 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001004 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /*
1007 * If it is not operational, then we have already marked it as dead
1008 * else if it is the last working disks, ignore the error, let the
1009 * next level up know.
1010 * else mark the drive as failed
1011 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001012 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -07001013 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /*
1015 * Don't fail the drive, just return an IO error.
1016 * The test should really be more sophisticated than
1017 * "working_disks == 1", but it isn't critical, and
1018 * can wait until we do more sophisticated "is the drive
1019 * really dead" tests...
1020 */
1021 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001022 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1023 unsigned long flags;
1024 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001026 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
1028 * if recovery is running, make sure it aborts.
1029 */
NeilBrowndfc70642008-05-23 13:04:39 -07001030 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001032 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001033 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Nick Andrewd7a420c2008-04-28 02:15:55 -07001034 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n"
1035 "raid10: Operation continuing on %d devices.\n",
NeilBrown76186dd2006-10-03 01:15:48 -07001036 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039static void print_conf(conf_t *conf)
1040{
1041 int i;
1042 mirror_info_t *tmp;
1043
1044 printk("RAID10 conf printout:\n");
1045 if (!conf) {
1046 printk("(!conf)\n");
1047 return;
1048 }
NeilBrown76186dd2006-10-03 01:15:48 -07001049 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 conf->raid_disks);
1051
1052 for (i = 0; i < conf->raid_disks; i++) {
1053 char b[BDEVNAME_SIZE];
1054 tmp = conf->mirrors + i;
1055 if (tmp->rdev)
1056 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001057 i, !test_bit(In_sync, &tmp->rdev->flags),
1058 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 bdevname(tmp->rdev->bdev,b));
1060 }
1061}
1062
1063static void close_sync(conf_t *conf)
1064{
NeilBrown0a27ec92006-01-06 00:20:13 -08001065 wait_barrier(conf);
1066 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 mempool_destroy(conf->r10buf_pool);
1069 conf->r10buf_pool = NULL;
1070}
1071
NeilBrown6d508242005-09-09 16:24:03 -07001072/* check if there are enough drives for
1073 * every block to appear on atleast one
1074 */
1075static int enough(conf_t *conf)
1076{
1077 int first = 0;
1078
1079 do {
1080 int n = conf->copies;
1081 int cnt = 0;
1082 while (n--) {
1083 if (conf->mirrors[first].rdev)
1084 cnt++;
1085 first = (first+1) % conf->raid_disks;
1086 }
1087 if (cnt == 0)
1088 return 0;
1089 } while (first != 0);
1090 return 1;
1091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093static int raid10_spare_active(mddev_t *mddev)
1094{
1095 int i;
1096 conf_t *conf = mddev->private;
1097 mirror_info_t *tmp;
1098
1099 /*
1100 * Find all non-in_sync disks within the RAID10 configuration
1101 * and mark them in_sync
1102 */
1103 for (i = 0; i < conf->raid_disks; i++) {
1104 tmp = conf->mirrors + i;
1105 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001106 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001107 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1108 unsigned long flags;
1109 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001111 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113 }
1114
1115 print_conf(conf);
1116 return 0;
1117}
1118
1119
1120static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1121{
1122 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001123 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int mirror;
1125 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001126 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001127 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (mddev->recovery_cp < MaxSector)
1130 /* only hot-add to in-sync arrays, as recovery is
1131 * very different from resync
1132 */
Neil Brown199050e2008-06-28 08:31:33 +10001133 return -EBUSY;
NeilBrown6d508242005-09-09 16:24:03 -07001134 if (!enough(conf))
Neil Brown199050e2008-06-28 08:31:33 +10001135 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
NeilBrowna53a6c82008-11-06 17:28:20 +11001137 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001138 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
NeilBrown6cce3b232006-01-06 00:20:16 -08001140 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10001141 rdev->saved_raid_disk >= first &&
NeilBrown6cce3b232006-01-06 00:20:16 -08001142 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1143 mirror = rdev->saved_raid_disk;
1144 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001145 mirror = first;
1146 for ( ; mirror <= last ; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if ( !(p=conf->mirrors+mirror)->rdev) {
1148
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001149 disk_stack_limits(mddev->gendisk, rdev->bdev,
1150 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001151 /* as we don't honour merge_bvec_fn, we must
1152 * never risk violating it, so limit
1153 * ->max_segments to one lying with a single
1154 * page, as a one page request is never in
1155 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
NeilBrown627a2d32010-03-08 16:44:38 +11001157 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1158 blk_queue_max_segments(mddev->queue, 1);
1159 blk_queue_segment_boundary(mddev->queue,
1160 PAGE_CACHE_SIZE - 1);
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 p->head_position = 0;
1164 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001165 err = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08001166 if (rdev->saved_raid_disk != mirror)
1167 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001168 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 break;
1170 }
1171
Andre Nollac5e7112009-08-03 10:59:47 +10001172 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177static int raid10_remove_disk(mddev_t *mddev, int number)
1178{
1179 conf_t *conf = mddev->private;
1180 int err = 0;
1181 mdk_rdev_t *rdev;
1182 mirror_info_t *p = conf->mirrors+ number;
1183
1184 print_conf(conf);
1185 rdev = p->rdev;
1186 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001187 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 atomic_read(&rdev->nr_pending)) {
1189 err = -EBUSY;
1190 goto abort;
1191 }
NeilBrowndfc70642008-05-23 13:04:39 -07001192 /* Only remove faulty devices in recovery
1193 * is not possible.
1194 */
1195 if (!test_bit(Faulty, &rdev->flags) &&
1196 enough(conf)) {
1197 err = -EBUSY;
1198 goto abort;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001201 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (atomic_read(&rdev->nr_pending)) {
1203 /* lost the race, try later */
1204 err = -EBUSY;
1205 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001206 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Andre Nollac5e7112009-08-03 10:59:47 +10001208 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210abort:
1211
1212 print_conf(conf);
1213 return err;
1214}
1215
1216
NeilBrown6712ecf2007-09-27 12:47:43 +02001217static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001219 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001220 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int i,d;
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 for (i=0; i<conf->copies; i++)
1224 if (r10_bio->devs[i].bio == bio)
1225 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001226 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 update_head_pos(i, r10_bio);
1228 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001229
1230 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1231 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001232 else {
1233 atomic_add(r10_bio->sectors,
1234 &conf->mirrors[d].rdev->corrected_errors);
1235 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1236 md_error(r10_bio->mddev,
1237 conf->mirrors[d].rdev);
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* for reconstruct, we always reschedule after a read.
1241 * for resync, only after all reads
1242 */
NeilBrown73d5c382009-02-25 13:18:47 +11001243 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1245 atomic_dec_and_test(&r10_bio->remaining)) {
1246 /* we have read all the blocks,
1247 * do the comparison in process context in raid10d
1248 */
1249 reschedule_retry(r10_bio);
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
NeilBrown6712ecf2007-09-27 12:47:43 +02001253static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001256 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001258 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int i,d;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 for (i = 0; i < conf->copies; i++)
1262 if (r10_bio->devs[i].bio == bio)
1263 break;
1264 d = r10_bio->devs[i].devnum;
1265
1266 if (!uptodate)
1267 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 update_head_pos(i, r10_bio);
1270
NeilBrown73d5c382009-02-25 13:18:47 +11001271 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 while (atomic_dec_and_test(&r10_bio->remaining)) {
1273 if (r10_bio->master_bio == NULL) {
1274 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001275 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001277 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279 } else {
1280 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1281 put_buf(r10_bio);
1282 r10_bio = r10_bio2;
1283 }
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/*
1288 * Note: sync and recover and handled very differently for raid10
1289 * This code is for resync.
1290 * For resync, we read through virtual addresses and read all blocks.
1291 * If there is any error, we schedule a write. The lowest numbered
1292 * drive is authoritative.
1293 * However requests come for physical address, so we need to map.
1294 * For every physical address there are raid_disks/copies virtual addresses,
1295 * which is always are least one, but is not necessarly an integer.
1296 * This means that a physical address can span multiple chunks, so we may
1297 * have to submit multiple io requests for a single sync request.
1298 */
1299/*
1300 * We check if all blocks are in-sync and only write to blocks that
1301 * aren't in sync
1302 */
1303static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1304{
NeilBrown070ec552009-06-16 16:54:21 +10001305 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int i, first;
1307 struct bio *tbio, *fbio;
1308
1309 atomic_set(&r10_bio->remaining, 1);
1310
1311 /* find the first device with a block */
1312 for (i=0; i<conf->copies; i++)
1313 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1314 break;
1315
1316 if (i == conf->copies)
1317 goto done;
1318
1319 first = i;
1320 fbio = r10_bio->devs[i].bio;
1321
1322 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001323 for (i=0 ; i < conf->copies ; i++) {
1324 int j, d;
1325 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001328
1329 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001331 if (i == first)
1332 continue;
1333 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1334 /* We know that the bi_io_vec layout is the same for
1335 * both 'first' and 'i', so we just compare them.
1336 * All vec entries are PAGE_SIZE;
1337 */
1338 for (j = 0; j < vcnt; j++)
1339 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1340 page_address(tbio->bi_io_vec[j].bv_page),
1341 PAGE_SIZE))
1342 break;
1343 if (j == vcnt)
1344 continue;
1345 mddev->resync_mismatches += r10_bio->sectors;
1346 }
NeilBrown18f08812006-01-06 00:20:25 -08001347 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1348 /* Don't fix anything. */
1349 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* Ok, we need to write this bio
1351 * First we need to fixup bv_offset, bv_len and
1352 * bi_vecs, as the read request might have corrupted these
1353 */
1354 tbio->bi_vcnt = vcnt;
1355 tbio->bi_size = r10_bio->sectors << 9;
1356 tbio->bi_idx = 0;
1357 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1359 tbio->bi_flags |= 1 << BIO_UPTODATE;
1360 tbio->bi_next = NULL;
1361 tbio->bi_rw = WRITE;
1362 tbio->bi_private = r10_bio;
1363 tbio->bi_sector = r10_bio->devs[i].addr;
1364
1365 for (j=0; j < vcnt ; j++) {
1366 tbio->bi_io_vec[j].bv_offset = 0;
1367 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1368
1369 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1370 page_address(fbio->bi_io_vec[j].bv_page),
1371 PAGE_SIZE);
1372 }
1373 tbio->bi_end_io = end_sync_write;
1374
1375 d = r10_bio->devs[i].devnum;
1376 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1377 atomic_inc(&r10_bio->remaining);
1378 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1379
1380 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1381 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1382 generic_make_request(tbio);
1383 }
1384
1385done:
1386 if (atomic_dec_and_test(&r10_bio->remaining)) {
1387 md_done_sync(mddev, r10_bio->sectors, 1);
1388 put_buf(r10_bio);
1389 }
1390}
1391
1392/*
1393 * Now for the recovery code.
1394 * Recovery happens across physical sectors.
1395 * We recover all non-is_sync drives by finding the virtual address of
1396 * each, and then choose a working drive that also has that virt address.
1397 * There is a separate r10_bio for each non-in_sync drive.
1398 * Only the first two slots are in use. The first for reading,
1399 * The second for writing.
1400 *
1401 */
1402
1403static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1404{
NeilBrown070ec552009-06-16 16:54:21 +10001405 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int i, d;
1407 struct bio *bio, *wbio;
1408
1409
1410 /* move the pages across to the second bio
1411 * and submit the write request
1412 */
1413 bio = r10_bio->devs[0].bio;
1414 wbio = r10_bio->devs[1].bio;
1415 for (i=0; i < wbio->bi_vcnt; i++) {
1416 struct page *p = bio->bi_io_vec[i].bv_page;
1417 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1418 wbio->bi_io_vec[i].bv_page = p;
1419 }
1420 d = r10_bio->devs[1].devnum;
1421
1422 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1423 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001424 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1425 generic_make_request(wbio);
1426 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001427 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
1430
1431/*
Robert Becker1e509152009-12-14 12:49:58 +11001432 * Used by fix_read_error() to decay the per rdev read_errors.
1433 * We halve the read error count for every hour that has elapsed
1434 * since the last recorded read error.
1435 *
1436 */
1437static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1438{
1439 struct timespec cur_time_mon;
1440 unsigned long hours_since_last;
1441 unsigned int read_errors = atomic_read(&rdev->read_errors);
1442
1443 ktime_get_ts(&cur_time_mon);
1444
1445 if (rdev->last_read_error.tv_sec == 0 &&
1446 rdev->last_read_error.tv_nsec == 0) {
1447 /* first time we've seen a read error */
1448 rdev->last_read_error = cur_time_mon;
1449 return;
1450 }
1451
1452 hours_since_last = (cur_time_mon.tv_sec -
1453 rdev->last_read_error.tv_sec) / 3600;
1454
1455 rdev->last_read_error = cur_time_mon;
1456
1457 /*
1458 * if hours_since_last is > the number of bits in read_errors
1459 * just set read errors to 0. We do this to avoid
1460 * overflowing the shift of read_errors by hours_since_last.
1461 */
1462 if (hours_since_last >= 8 * sizeof(read_errors))
1463 atomic_set(&rdev->read_errors, 0);
1464 else
1465 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1466}
1467
1468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 * This is a kernel thread which:
1470 *
1471 * 1. Retries failed read operations on working mirrors.
1472 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001473 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
1475
NeilBrown6814d532006-10-03 01:15:45 -07001476static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1477{
1478 int sect = 0; /* Offset from r10_bio->sector */
1479 int sectors = r10_bio->sectors;
1480 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001481 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1482
1483 rcu_read_lock();
1484 {
1485 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1486 char b[BDEVNAME_SIZE];
1487 int cur_read_error_count = 0;
1488
1489 rdev = rcu_dereference(conf->mirrors[d].rdev);
1490 bdevname(rdev->bdev, b);
1491
1492 if (test_bit(Faulty, &rdev->flags)) {
1493 rcu_read_unlock();
1494 /* drive has already been failed, just ignore any
1495 more fix_read_error() attempts */
1496 return;
1497 }
1498
1499 check_decay_read_errors(mddev, rdev);
1500 atomic_inc(&rdev->read_errors);
1501 cur_read_error_count = atomic_read(&rdev->read_errors);
1502 if (cur_read_error_count > max_read_errors) {
1503 rcu_read_unlock();
1504 printk(KERN_NOTICE
1505 "raid10: %s: Raid device exceeded "
1506 "read_error threshold "
1507 "[cur %d:max %d]\n",
1508 b, cur_read_error_count, max_read_errors);
1509 printk(KERN_NOTICE
1510 "raid10: %s: Failing raid "
1511 "device\n", b);
1512 md_error(mddev, conf->mirrors[d].rdev);
1513 return;
1514 }
1515 }
1516 rcu_read_unlock();
1517
NeilBrown6814d532006-10-03 01:15:45 -07001518 while(sectors) {
1519 int s = sectors;
1520 int sl = r10_bio->read_slot;
1521 int success = 0;
1522 int start;
1523
1524 if (s > (PAGE_SIZE>>9))
1525 s = PAGE_SIZE >> 9;
1526
1527 rcu_read_lock();
1528 do {
1529 int d = r10_bio->devs[sl].devnum;
1530 rdev = rcu_dereference(conf->mirrors[d].rdev);
1531 if (rdev &&
1532 test_bit(In_sync, &rdev->flags)) {
1533 atomic_inc(&rdev->nr_pending);
1534 rcu_read_unlock();
1535 success = sync_page_io(rdev->bdev,
1536 r10_bio->devs[sl].addr +
1537 sect + rdev->data_offset,
1538 s<<9,
1539 conf->tmppage, READ);
1540 rdev_dec_pending(rdev, mddev);
1541 rcu_read_lock();
1542 if (success)
1543 break;
1544 }
1545 sl++;
1546 if (sl == conf->copies)
1547 sl = 0;
1548 } while (!success && sl != r10_bio->read_slot);
1549 rcu_read_unlock();
1550
1551 if (!success) {
1552 /* Cannot read from anywhere -- bye bye array */
1553 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1554 md_error(mddev, conf->mirrors[dn].rdev);
1555 break;
1556 }
1557
1558 start = sl;
1559 /* write it back and re-read */
1560 rcu_read_lock();
1561 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001562 char b[BDEVNAME_SIZE];
NeilBrown6814d532006-10-03 01:15:45 -07001563 int d;
1564 if (sl==0)
1565 sl = conf->copies;
1566 sl--;
1567 d = r10_bio->devs[sl].devnum;
1568 rdev = rcu_dereference(conf->mirrors[d].rdev);
1569 if (rdev &&
1570 test_bit(In_sync, &rdev->flags)) {
1571 atomic_inc(&rdev->nr_pending);
1572 rcu_read_unlock();
1573 atomic_add(s, &rdev->corrected_errors);
1574 if (sync_page_io(rdev->bdev,
1575 r10_bio->devs[sl].addr +
1576 sect + rdev->data_offset,
1577 s<<9, conf->tmppage, WRITE)
Robert Becker67b8dc42009-12-14 12:49:57 +11001578 == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001579 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001580 printk(KERN_NOTICE
1581 "raid10:%s: read correction "
1582 "write failed"
1583 " (%d sectors at %llu on %s)\n",
1584 mdname(mddev), s,
1585 (unsigned long long)(sect+
1586 rdev->data_offset),
1587 bdevname(rdev->bdev, b));
1588 printk(KERN_NOTICE "raid10:%s: failing "
1589 "drive\n",
1590 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001591 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001592 }
NeilBrown6814d532006-10-03 01:15:45 -07001593 rdev_dec_pending(rdev, mddev);
1594 rcu_read_lock();
1595 }
1596 }
1597 sl = start;
1598 while (sl != r10_bio->read_slot) {
1599 int d;
1600 if (sl==0)
1601 sl = conf->copies;
1602 sl--;
1603 d = r10_bio->devs[sl].devnum;
1604 rdev = rcu_dereference(conf->mirrors[d].rdev);
1605 if (rdev &&
1606 test_bit(In_sync, &rdev->flags)) {
1607 char b[BDEVNAME_SIZE];
1608 atomic_inc(&rdev->nr_pending);
1609 rcu_read_unlock();
1610 if (sync_page_io(rdev->bdev,
1611 r10_bio->devs[sl].addr +
1612 sect + rdev->data_offset,
Robert Becker67b8dc42009-12-14 12:49:57 +11001613 s<<9, conf->tmppage,
1614 READ) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001615 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001616 printk(KERN_NOTICE
1617 "raid10:%s: unable to read back "
1618 "corrected sectors"
1619 " (%d sectors at %llu on %s)\n",
1620 mdname(mddev), s,
1621 (unsigned long long)(sect+
1622 rdev->data_offset),
1623 bdevname(rdev->bdev, b));
1624 printk(KERN_NOTICE "raid10:%s: failing drive\n",
1625 bdevname(rdev->bdev, b));
1626
NeilBrown6814d532006-10-03 01:15:45 -07001627 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001628 } else {
NeilBrown6814d532006-10-03 01:15:45 -07001629 printk(KERN_INFO
1630 "raid10:%s: read error corrected"
1631 " (%d sectors at %llu on %s)\n",
1632 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001633 (unsigned long long)(sect+
1634 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001635 bdevname(rdev->bdev, b));
Robert Becker67b8dc42009-12-14 12:49:57 +11001636 }
NeilBrown6814d532006-10-03 01:15:45 -07001637
1638 rdev_dec_pending(rdev, mddev);
1639 rcu_read_lock();
1640 }
1641 }
1642 rcu_read_unlock();
1643
1644 sectors -= s;
1645 sect += s;
1646 }
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649static void raid10d(mddev_t *mddev)
1650{
1651 r10bio_t *r10_bio;
1652 struct bio *bio;
1653 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001654 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 struct list_head *head = &conf->retry_list;
1656 int unplug=0;
1657 mdk_rdev_t *rdev;
1658
1659 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 for (;;) {
1662 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001663
1664 unplug += flush_pending_writes(conf);
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001667 if (list_empty(head)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001668 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1672 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001673 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 spin_unlock_irqrestore(&conf->device_lock, flags);
1675
1676 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001677 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1679 sync_request_write(mddev, r10_bio);
1680 unplug = 1;
1681 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1682 recovery_request_write(mddev, r10_bio);
1683 unplug = 1;
1684 } else {
1685 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001686 /* we got a read error. Maybe the drive is bad. Maybe just
1687 * the block and we can fix it.
1688 * We freeze all other IO, and try reading the block from
1689 * other devices. When we find one, we re-write
1690 * and check it that fixes the read error.
1691 * This is all done synchronously while the array is
1692 * frozen.
1693 */
NeilBrown6814d532006-10-03 01:15:45 -07001694 if (mddev->ro == 0) {
1695 freeze_array(conf);
1696 fix_read_error(conf, mddev, r10_bio);
1697 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001698 }
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001701 r10_bio->devs[r10_bio->read_slot].bio =
1702 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 mirror = read_balance(conf, r10_bio);
1704 if (mirror == -1) {
1705 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1706 " read error for block %llu\n",
1707 bdevname(bio->bi_bdev,b),
1708 (unsigned long long)r10_bio->sector);
1709 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001710 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 } else {
Jens Axboe1f98a132009-09-11 14:32:04 +02001712 const bool do_sync = bio_rw_flagged(r10_bio->master_bio, BIO_RW_SYNCIO);
Maik Hampel14e71342007-07-31 00:37:57 -07001713 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 rdev = conf->mirrors[mirror].rdev;
1715 if (printk_ratelimit())
1716 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1717 " another mirror\n",
1718 bdevname(rdev->bdev,b),
1719 (unsigned long long)r10_bio->sector);
1720 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1721 r10_bio->devs[r10_bio->read_slot].bio = bio;
1722 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1723 + rdev->data_offset;
1724 bio->bi_bdev = rdev->bdev;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +04001725 bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 bio->bi_private = r10_bio;
1727 bio->bi_end_io = raid10_end_read_request;
1728 unplug = 1;
1729 generic_make_request(bio);
1730 }
1731 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001732 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (unplug)
1735 unplug_slaves(mddev);
1736}
1737
1738
1739static int init_resync(conf_t *conf)
1740{
1741 int buffs;
1742
1743 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001744 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1746 if (!conf->r10buf_pool)
1747 return -ENOMEM;
1748 conf->next_resync = 0;
1749 return 0;
1750}
1751
1752/*
1753 * perform a "sync" on one "block"
1754 *
1755 * We need to make sure that no normal I/O request - particularly write
1756 * requests - conflict with active sync requests.
1757 *
1758 * This is achieved by tracking pending requests and a 'barrier' concept
1759 * that can be installed to exclude normal IO requests.
1760 *
1761 * Resync and recovery are handled very differently.
1762 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1763 *
1764 * For resync, we iterate over virtual addresses, read all copies,
1765 * and update if there are differences. If only one copy is live,
1766 * skip it.
1767 * For recovery, we iterate over physical addresses, read a good
1768 * value for each non-in_sync drive, and over-write.
1769 *
1770 * So, for recovery we may have several outstanding complex requests for a
1771 * given address, one for each out-of-sync device. We model this by allocating
1772 * a number of r10_bio structures, one for each out-of-sync device.
1773 * As we setup these structures, we collect all bio's together into a list
1774 * which we then process collectively to add pages, and then process again
1775 * to pass to generic_make_request.
1776 *
1777 * The r10_bio structures are linked using a borrowed master_bio pointer.
1778 * This link is counted in ->remaining. When the r10_bio that points to NULL
1779 * has its remaining count decremented to 0, the whole complex operation
1780 * is complete.
1781 *
1782 */
1783
NeilBrown57afd892005-06-21 17:17:13 -07001784static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
NeilBrown070ec552009-06-16 16:54:21 +10001786 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 r10bio_t *r10_bio;
1788 struct bio *biolist = NULL, *bio;
1789 sector_t max_sector, nr_sectors;
1790 int disk;
1791 int i;
NeilBrown6cce3b232006-01-06 00:20:16 -08001792 int max_sync;
1793 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 sector_t sectors_skipped = 0;
1796 int chunks_skipped = 0;
1797
1798 if (!conf->r10buf_pool)
1799 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001803 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1805 max_sector = mddev->resync_max_sectors;
1806 if (sector_nr >= max_sector) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001807 /* If we aborted, we need to abort the
1808 * sync on the 'current' bitmap chucks (there can
1809 * be several when recovering multiple devices).
1810 * as we may have started syncing it but not finished.
1811 * We can find the current address in
1812 * mddev->curr_resync, but for recovery,
1813 * we need to convert that to several
1814 * virtual addresses.
1815 */
1816 if (mddev->curr_resync < max_sector) { /* aborted */
1817 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1818 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1819 &sync_blocks, 1);
1820 else for (i=0; i<conf->raid_disks; i++) {
1821 sector_t sect =
1822 raid10_find_virt(conf, mddev->curr_resync, i);
1823 bitmap_end_sync(mddev->bitmap, sect,
1824 &sync_blocks, 1);
1825 }
1826 } else /* completed sync */
1827 conf->fullsync = 0;
1828
1829 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001831 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return sectors_skipped;
1833 }
1834 if (chunks_skipped >= conf->raid_disks) {
1835 /* if there has been nothing to do on any drive,
1836 * then there is nothing to do at all..
1837 */
NeilBrown57afd892005-06-21 17:17:13 -07001838 *skipped = 1;
1839 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841
NeilBrownc6207272008-02-06 01:39:52 -08001842 if (max_sector > mddev->resync_max)
1843 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /* make sure whole request will fit in a chunk - if chunks
1846 * are meaningful
1847 */
1848 if (conf->near_copies < conf->raid_disks &&
1849 max_sector > (sector_nr | conf->chunk_mask))
1850 max_sector = (sector_nr | conf->chunk_mask) + 1;
1851 /*
1852 * If there is non-resync activity waiting for us then
1853 * put in a delay to throttle resync.
1854 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001855 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* Again, very different code for resync and recovery.
1859 * Both must result in an r10bio with a list of bios that
1860 * have bi_end_io, bi_sector, bi_bdev set,
1861 * and bi_private set to the r10bio.
1862 * For recovery, we may actually create several r10bios
1863 * with 2 bios in each, that correspond to the bios in the main one.
1864 * In this case, the subordinate r10bios link back through a
1865 * borrowed master_bio pointer, and the counter in the master
1866 * includes a ref from each subordinate.
1867 */
1868 /* First, we decide what to do and set ->bi_end_io
1869 * To end_sync_read if we want to read, and
1870 * end_sync_write if we will want to write.
1871 */
1872
NeilBrown6cce3b232006-01-06 00:20:16 -08001873 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1875 /* recovery... the complicated one */
NeilBrowna9f326e2009-09-23 18:06:41 +10001876 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 r10_bio = NULL;
1878
1879 for (i=0 ; i<conf->raid_disks; i++)
1880 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001881 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001882 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /* want to reconstruct this device */
1884 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b232006-01-06 00:20:16 -08001885 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1886 int must_sync;
1887 /* Unless we are doing a full sync, we only need
1888 * to recover the block if it is set in the bitmap
1889 */
1890 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1891 &sync_blocks, 1);
1892 if (sync_blocks < max_sync)
1893 max_sync = sync_blocks;
1894 if (!must_sync &&
1895 !conf->fullsync) {
1896 /* yep, skip the sync_blocks here, but don't assume
1897 * that there will never be anything to do here
1898 */
1899 chunks_skipped = -1;
1900 continue;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b232006-01-06 00:20:16 -08001904 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 atomic_set(&r10_bio->remaining, 0);
1906
1907 r10_bio->master_bio = (struct bio*)rb2;
1908 if (rb2)
1909 atomic_inc(&rb2->remaining);
1910 r10_bio->mddev = mddev;
1911 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b232006-01-06 00:20:16 -08001912 r10_bio->sector = sect;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001915
1916 /* Need to check if the array will still be
NeilBrown6cce3b232006-01-06 00:20:16 -08001917 * degraded
1918 */
NeilBrown18055562009-05-07 12:48:10 +10001919 for (j=0; j<conf->raid_disks; j++)
1920 if (conf->mirrors[j].rdev == NULL ||
1921 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001922 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001923 break;
1924 }
NeilBrown18055562009-05-07 12:48:10 +10001925
NeilBrown6cce3b232006-01-06 00:20:16 -08001926 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1927 &sync_blocks, still_degraded);
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 for (j=0; j<conf->copies;j++) {
1930 int d = r10_bio->devs[j].devnum;
1931 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001932 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /* This is where we read from */
1934 bio = r10_bio->devs[0].bio;
1935 bio->bi_next = biolist;
1936 biolist = bio;
1937 bio->bi_private = r10_bio;
1938 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001939 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 bio->bi_sector = r10_bio->devs[j].addr +
1941 conf->mirrors[d].rdev->data_offset;
1942 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1943 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1944 atomic_inc(&r10_bio->remaining);
1945 /* and we write to 'i' */
1946
1947 for (k=0; k<conf->copies; k++)
1948 if (r10_bio->devs[k].devnum == i)
1949 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001950 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 bio = r10_bio->devs[1].bio;
1952 bio->bi_next = biolist;
1953 biolist = bio;
1954 bio->bi_private = r10_bio;
1955 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001956 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 bio->bi_sector = r10_bio->devs[k].addr +
1958 conf->mirrors[i].rdev->data_offset;
1959 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1960
1961 r10_bio->devs[0].devnum = d;
1962 r10_bio->devs[1].devnum = i;
1963
1964 break;
1965 }
1966 }
1967 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001968 /* Cannot recover, so abort the recovery */
1969 put_buf(r10_bio);
K.Tanakaa07e6ab2008-03-04 14:29:37 -08001970 if (rb2)
1971 atomic_dec(&rb2->remaining);
NeilBrown87fc7672005-09-09 16:24:04 -07001972 r10_bio = rb2;
NeilBrowndfc70642008-05-23 13:04:39 -07001973 if (!test_and_set_bit(MD_RECOVERY_INTR,
1974 &mddev->recovery))
NeilBrown87fc7672005-09-09 16:24:04 -07001975 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1976 mdname(mddev));
1977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
1979 }
1980 if (biolist == NULL) {
1981 while (r10_bio) {
1982 r10bio_t *rb2 = r10_bio;
1983 r10_bio = (r10bio_t*) rb2->master_bio;
1984 rb2->master_bio = NULL;
1985 put_buf(rb2);
1986 }
1987 goto giveup;
1988 }
1989 } else {
1990 /* resync. Schedule a read for every block at this virt offset */
1991 int count = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08001992
NeilBrown78200d42009-02-25 13:18:47 +11001993 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1994
NeilBrown6cce3b232006-01-06 00:20:16 -08001995 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1996 &sync_blocks, mddev->degraded) &&
1997 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1998 /* We can skip this block */
1999 *skipped = 1;
2000 return sync_blocks + sectors_skipped;
2001 }
2002 if (sync_blocks < max_sync)
2003 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 r10_bio->mddev = mddev;
2007 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b232006-01-06 00:20:16 -08002008 raise_barrier(conf, 0);
2009 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 r10_bio->master_bio = NULL;
2012 r10_bio->sector = sector_nr;
2013 set_bit(R10BIO_IsSync, &r10_bio->state);
2014 raid10_find_phys(conf, r10_bio);
2015 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2016
2017 for (i=0; i<conf->copies; i++) {
2018 int d = r10_bio->devs[i].devnum;
2019 bio = r10_bio->devs[i].bio;
2020 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002021 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002023 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 continue;
2025 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2026 atomic_inc(&r10_bio->remaining);
2027 bio->bi_next = biolist;
2028 biolist = bio;
2029 bio->bi_private = r10_bio;
2030 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002031 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 bio->bi_sector = r10_bio->devs[i].addr +
2033 conf->mirrors[d].rdev->data_offset;
2034 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2035 count++;
2036 }
2037
2038 if (count < 2) {
2039 for (i=0; i<conf->copies; i++) {
2040 int d = r10_bio->devs[i].devnum;
2041 if (r10_bio->devs[i].bio->bi_end_io)
2042 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
2043 }
2044 put_buf(r10_bio);
2045 biolist = NULL;
2046 goto giveup;
2047 }
2048 }
2049
2050 for (bio = biolist; bio ; bio=bio->bi_next) {
2051
2052 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2053 if (bio->bi_end_io)
2054 bio->bi_flags |= 1 << BIO_UPTODATE;
2055 bio->bi_vcnt = 0;
2056 bio->bi_idx = 0;
2057 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 bio->bi_size = 0;
2059 }
2060
2061 nr_sectors = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08002062 if (sector_nr + max_sync < max_sector)
2063 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 do {
2065 struct page *page;
2066 int len = PAGE_SIZE;
2067 disk = 0;
2068 if (sector_nr + (len>>9) > max_sector)
2069 len = (max_sector - sector_nr) << 9;
2070 if (len == 0)
2071 break;
2072 for (bio= biolist ; bio ; bio=bio->bi_next) {
2073 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2074 if (bio_add_page(bio, page, len, 0) == 0) {
2075 /* stop here */
2076 struct bio *bio2;
2077 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2078 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
2079 /* remove last page from this bio */
2080 bio2->bi_vcnt--;
2081 bio2->bi_size -= len;
2082 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2083 }
2084 goto bio_full;
2085 }
2086 disk = i;
2087 }
2088 nr_sectors += len>>9;
2089 sector_nr += len>>9;
2090 } while (biolist->bi_vcnt < RESYNC_PAGES);
2091 bio_full:
2092 r10_bio->sectors = nr_sectors;
2093
2094 while (biolist) {
2095 bio = biolist;
2096 biolist = biolist->bi_next;
2097
2098 bio->bi_next = NULL;
2099 r10_bio = bio->bi_private;
2100 r10_bio->sectors = nr_sectors;
2101
2102 if (bio->bi_end_io == end_sync_read) {
2103 md_sync_acct(bio->bi_bdev, nr_sectors);
2104 generic_make_request(bio);
2105 }
2106 }
2107
NeilBrown57afd892005-06-21 17:17:13 -07002108 if (sectors_skipped)
2109 /* pretend they weren't skipped, it makes
2110 * no important difference in this case
2111 */
2112 md_done_sync(mddev, sectors_skipped, 1);
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return sectors_skipped + nr_sectors;
2115 giveup:
2116 /* There is nowhere to write, so all non-sync
2117 * drives must be failed, so try the next chunk...
2118 */
NeilBrown09b40682009-02-25 13:18:47 +11002119 if (sector_nr + max_sync < max_sector)
2120 max_sector = sector_nr + max_sync;
2121
2122 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 chunks_skipped ++;
2124 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Dan Williams80c3a6c2009-03-17 18:10:40 -07002128static sector_t
2129raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2130{
2131 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002132 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002133
2134 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002135 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002136 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002137 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002138
2139 size = sectors >> conf->chunk_shift;
2140 sector_div(size, conf->far_copies);
2141 size = size * raid_disks;
2142 sector_div(size, conf->near_copies);
2143
2144 return size << conf->chunk_shift;
2145}
2146
Trela, Maciejdab8b292010-03-08 16:02:45 +11002147
2148static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002150 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002151 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002153 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Andre Noll9d8f0362009-06-18 08:45:01 +10002155 if (mddev->chunk_sectors < (PAGE_SIZE >> 9) ||
2156 !is_power_of_2(mddev->chunk_sectors)) {
NeilBrown4bbf3772008-10-13 11:55:12 +11002157 printk(KERN_ERR "md/raid10: chunk size must be "
raz ben yehuda964e7912009-06-16 17:01:22 +10002158 "at least PAGE_SIZE(%ld) and be a power of 2.\n", PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
NeilBrown2604b702006-01-06 00:20:36 -08002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 nc = mddev->layout & 255;
2163 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07002164 fo = mddev->layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07002167 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2169 mdname(mddev), mddev->layout);
2170 goto out;
2171 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002172
2173 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002174 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002175 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002177
NeilBrown4443ae12006-01-06 00:20:28 -08002178 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002179 GFP_KERNEL);
2180 if (!conf->mirrors)
2181 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002182
2183 conf->tmppage = alloc_page(GFP_KERNEL);
2184 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002185 goto out;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
NeilBrown64a742b2007-02-28 20:11:18 -08002188 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 conf->near_copies = nc;
2190 conf->far_copies = fc;
2191 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002192 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002193 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2194 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2195
2196 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2197 r10bio_pool_free, conf);
2198 if (!conf->r10bio_pool)
2199 goto out;
2200
Andre Noll58c0fed2009-03-31 14:33:13 +11002201 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002202 sector_div(size, fc);
2203 size = size * conf->raid_disks;
2204 sector_div(size, nc);
2205 /* 'size' is now the number of chunks in the array */
2206 /* calculate "used chunks per device" in 'stride' */
2207 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002208
2209 /* We need to round up when dividing by raid_disks to
2210 * get the stride size.
2211 */
2212 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002213 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002214
2215 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002216
NeilBrownc93983b2006-06-26 00:27:41 -07002217 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002218 stride = 1;
2219 else
NeilBrownc93983b2006-06-26 00:27:41 -07002220 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002221 conf->stride = stride << conf->chunk_shift;
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Neil Browne7e72bf2008-05-14 16:05:54 -07002224 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002225 INIT_LIST_HEAD(&conf->retry_list);
2226
2227 spin_lock_init(&conf->resync_lock);
2228 init_waitqueue_head(&conf->wait_barrier);
2229
2230 conf->thread = md_register_thread(raid10d, mddev, NULL);
2231 if (!conf->thread)
2232 goto out;
2233
2234 conf->scale_disks = 0;
2235 conf->mddev = mddev;
2236 return conf;
2237
2238 out:
2239 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2240 mdname(mddev));
2241 if (conf) {
2242 if (conf->r10bio_pool)
2243 mempool_destroy(conf->r10bio_pool);
2244 kfree(conf->mirrors);
2245 safe_put_page(conf->tmppage);
2246 kfree(conf);
2247 }
2248 return ERR_PTR(err);
2249}
2250
2251static int run(mddev_t *mddev)
2252{
2253 conf_t *conf;
2254 int i, disk_idx, chunk_size;
2255 mirror_info_t *disk;
2256 mdk_rdev_t *rdev;
2257 sector_t size;
2258
2259 /*
2260 * copy the already verified devices into our private RAID10
2261 * bookkeeping area. [whatever we allocate in run(),
2262 * should be freed in stop()]
2263 */
2264
2265 if (mddev->private == NULL) {
2266 conf = setup_conf(mddev);
2267 if (IS_ERR(conf))
2268 return PTR_ERR(conf);
2269 mddev->private = conf;
2270 }
2271 conf = mddev->private;
2272 if (!conf)
2273 goto out;
2274
Neil Browne7e72bf2008-05-14 16:05:54 -07002275 mddev->queue->queue_lock = &conf->device_lock;
2276
Trela, Maciejdab8b292010-03-08 16:02:45 +11002277 mddev->thread = conf->thread;
2278 conf->thread = NULL;
2279
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002280 chunk_size = mddev->chunk_sectors << 9;
2281 blk_queue_io_min(mddev->queue, chunk_size);
2282 if (conf->raid_disks % conf->near_copies)
2283 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2284 else
2285 blk_queue_io_opt(mddev->queue, chunk_size *
2286 (conf->raid_disks / conf->near_copies));
2287
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002288 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002290 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 || disk_idx < 0)
2292 continue;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002293 if (conf->scale_disks) {
2294 disk_idx *= conf->scale_disks;
2295 rdev->raid_disk = disk_idx;
2296 /* MOVE 'rd%d' link !! */
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 disk = conf->mirrors + disk_idx;
2299
2300 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002301 disk_stack_limits(mddev->gendisk, rdev->bdev,
2302 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002304 * violating it, so limit max_segments to 1 lying
2305 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 */
NeilBrown627a2d32010-03-08 16:44:38 +11002307 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2308 blk_queue_max_segments(mddev->queue, 1);
2309 blk_queue_segment_boundary(mddev->queue,
2310 PAGE_CACHE_SIZE - 1);
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
NeilBrown6d508242005-09-09 16:24:03 -07002315 /* need to check that every block has at least one working mirror */
2316 if (!enough(conf)) {
2317 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2318 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 goto out_free_conf;
2320 }
2321
2322 mddev->degraded = 0;
2323 for (i = 0; i < conf->raid_disks; i++) {
2324
2325 disk = conf->mirrors + i;
2326
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002327 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002328 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 disk->head_position = 0;
2330 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002331 if (disk->rdev)
2332 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334 }
2335
Andre Noll8c6ac862009-06-18 08:48:06 +10002336 if (mddev->recovery_cp != MaxSector)
2337 printk(KERN_NOTICE "raid10: %s is not clean"
2338 " -- starting background reconstruction\n",
2339 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 printk(KERN_INFO
2341 "raid10: raid set %s active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002342 mdname(mddev), conf->raid_disks - mddev->degraded,
2343 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /*
2345 * Ok, everything is just fine now
2346 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002347 mddev->dev_sectors = conf->dev_sectors;
2348 size = raid10_size(mddev, 0, 0);
2349 md_set_array_sectors(mddev, size);
2350 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
NeilBrown7a5febe2005-05-16 21:53:16 -07002352 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002353 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2354 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /* Calculate max read-ahead size.
2357 * We need to readahead at least twice a whole stripe....
2358 * maybe...
2359 */
2360 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002361 int stripe = conf->raid_disks *
2362 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 stripe /= conf->near_copies;
2364 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2365 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2366 }
2367
NeilBrown84707f32010-03-16 17:23:35 +11002368 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Andre Nollac5e7112009-08-03 10:59:47 +10002370 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372
2373out_free_conf:
2374 if (conf->r10bio_pool)
2375 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002376 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002377 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 kfree(conf);
2379 mddev->private = NULL;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002380 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381out:
2382 return -EIO;
2383}
2384
2385static int stop(mddev_t *mddev)
2386{
NeilBrown070ec552009-06-16 16:54:21 +10002387 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
NeilBrown409c57f2009-03-31 14:39:39 +11002389 raise_barrier(conf, 0);
2390 lower_barrier(conf);
2391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 md_unregister_thread(mddev->thread);
2393 mddev->thread = NULL;
2394 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2395 if (conf->r10bio_pool)
2396 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002397 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 kfree(conf);
2399 mddev->private = NULL;
2400 return 0;
2401}
2402
NeilBrown6cce3b232006-01-06 00:20:16 -08002403static void raid10_quiesce(mddev_t *mddev, int state)
2404{
NeilBrown070ec552009-06-16 16:54:21 +10002405 conf_t *conf = mddev->private;
NeilBrown6cce3b232006-01-06 00:20:16 -08002406
2407 switch(state) {
2408 case 1:
2409 raise_barrier(conf, 0);
2410 break;
2411 case 0:
2412 lower_barrier(conf);
2413 break;
2414 }
NeilBrown6cce3b232006-01-06 00:20:16 -08002415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Trela, Maciejdab8b292010-03-08 16:02:45 +11002417static void *raid10_takeover_raid0(mddev_t *mddev)
2418{
2419 mdk_rdev_t *rdev;
2420 conf_t *conf;
2421
2422 if (mddev->degraded > 0) {
2423 printk(KERN_ERR "error: degraded raid0!\n");
2424 return ERR_PTR(-EINVAL);
2425 }
2426
2427 /* Update slot numbers to obtain
2428 * degraded raid10 with missing mirrors
2429 */
2430 list_for_each_entry(rdev, &mddev->disks, same_set) {
2431 rdev->raid_disk *= 2;
2432 }
2433
2434 /* Set new parameters */
2435 mddev->new_level = 10;
2436 /* new layout: far_copies = 1, near_copies = 2 */
2437 mddev->new_layout = (1<<8) + 2;
2438 mddev->new_chunk_sectors = mddev->chunk_sectors;
2439 mddev->delta_disks = mddev->raid_disks;
2440 mddev->degraded = mddev->raid_disks;
2441 mddev->raid_disks *= 2;
2442 /* make sure it will be not marked as dirty */
2443 mddev->recovery_cp = MaxSector;
2444
2445 conf = setup_conf(mddev);
2446 conf->scale_disks = 2;
2447 return conf;
2448}
2449
2450static void *raid10_takeover(mddev_t *mddev)
2451{
2452 struct raid0_private_data *raid0_priv;
2453
2454 /* raid10 can take over:
2455 * raid0 - providing it has only two drives
2456 */
2457 if (mddev->level == 0) {
2458 /* for raid0 takeover only one zone is supported */
2459 raid0_priv = mddev->private;
2460 if (raid0_priv->nr_strip_zones > 1) {
2461 printk(KERN_ERR "md: cannot takeover raid 0 with more than one zone.\n");
2462 return ERR_PTR(-EINVAL);
2463 }
2464 return raid10_takeover_raid0(mddev);
2465 }
2466 return ERR_PTR(-EINVAL);
2467}
2468
NeilBrown2604b702006-01-06 00:20:36 -08002469static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002472 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 .owner = THIS_MODULE,
2474 .make_request = make_request,
2475 .run = run,
2476 .stop = stop,
2477 .status = status,
2478 .error_handler = error,
2479 .hot_add_disk = raid10_add_disk,
2480 .hot_remove_disk= raid10_remove_disk,
2481 .spare_active = raid10_spare_active,
2482 .sync_request = sync_request,
NeilBrown6cce3b232006-01-06 00:20:16 -08002483 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002484 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002485 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486};
2487
2488static int __init raid_init(void)
2489{
NeilBrown2604b702006-01-06 00:20:36 -08002490 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
2493static void raid_exit(void)
2494{
NeilBrown2604b702006-01-06 00:20:36 -08002495 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
2498module_init(raid_init);
2499module_exit(raid_exit);
2500MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002501MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002503MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002504MODULE_ALIAS("md-level-10");