blob: 69b6595443901ade9d72871d77e40fad73e98e49 [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110022#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/blkdev.h>
NeilBrownbff61972009-03-31 14:33:13 +110024#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110025#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110026#include "raid10.h"
Trela, Maciejdab8b292010-03-08 16:02:45 +110027#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110028#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * RAID10 provides a combination of RAID0 and RAID1 functionality.
32 * The layout of data is defined by
33 * chunk_size
34 * raid_disks
35 * near_copies (stored in low byte of layout)
36 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070037 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 *
39 * The data to be stored is divided into chunks using chunksize.
40 * Each device is divided into far_copies sections.
41 * In each section, chunks are laid out in a style similar to raid0, but
42 * near_copies copies of each chunk is stored (each on a different drive).
43 * The starting device for each section is offset near_copies from the starting
44 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070045 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * drive.
47 * near_copies and far_copies must be at least one, and their product is at most
48 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070049 *
50 * If far_offset is true, then the far_copies are handled a bit differently.
51 * The copies are still in different stripes, but instead of be very far apart
52 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
55/*
56 * Number of guaranteed r10bios in case of extreme VM load:
57 */
58#define NR_RAID10_BIOS 256
59
60static void unplug_slaves(mddev_t *mddev);
61
NeilBrown0a27ec92006-01-06 00:20:13 -080062static void allow_barrier(conf_t *conf);
63static void lower_barrier(conf_t *conf);
64
Al Virodd0fc662005-10-07 07:46:04 +010065static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 conf_t *conf = data;
68 r10bio_t *r10_bio;
69 int size = offsetof(struct r10bio_s, devs[conf->copies]);
70
71 /* allocate a r10bio with room for raid_disks entries in the bios array */
NeilBrown9ffae0c2006-01-06 00:20:32 -080072 r10_bio = kzalloc(size, gfp_flags);
NeilBrowned9bfdf2009-10-16 15:55:44 +110073 if (!r10_bio && conf->mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 unplug_slaves(conf->mddev);
75
76 return r10_bio;
77}
78
79static void r10bio_pool_free(void *r10_bio, void *data)
80{
81 kfree(r10_bio);
82}
83
NeilBrown0310fa22008-08-05 15:54:14 +100084/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100087/* amount of memory to reserve for resync requests */
88#define RESYNC_WINDOW (1024*1024)
89/* maximum number of concurrent requests, memory permitting */
90#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * When performing a resync, we need to read and compare, so
94 * we need as many pages are there are copies.
95 * When performing a recovery, we need 2 bios, one for read,
96 * one for write (we recover only one drive per r10buf)
97 *
98 */
Al Virodd0fc662005-10-07 07:46:04 +010099static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 conf_t *conf = data;
102 struct page *page;
103 r10bio_t *r10_bio;
104 struct bio *bio;
105 int i, j;
106 int nalloc;
107
108 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
109 if (!r10_bio) {
110 unplug_slaves(conf->mddev);
111 return NULL;
112 }
113
114 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
115 nalloc = conf->copies; /* resync */
116 else
117 nalloc = 2; /* recovery */
118
119 /*
120 * Allocate bios.
121 */
122 for (j = nalloc ; j-- ; ) {
NeilBrown67465572010-10-26 17:33:54 +1100123 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!bio)
125 goto out_free_bio;
126 r10_bio->devs[j].bio = bio;
127 }
128 /*
129 * Allocate RESYNC_PAGES data pages and attach them
130 * where needed.
131 */
132 for (j = 0 ; j < nalloc; j++) {
133 bio = r10_bio->devs[j].bio;
134 for (i = 0; i < RESYNC_PAGES; i++) {
135 page = alloc_page(gfp_flags);
136 if (unlikely(!page))
137 goto out_free_pages;
138
139 bio->bi_io_vec[i].bv_page = page;
140 }
141 }
142
143 return r10_bio;
144
145out_free_pages:
146 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800147 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 while (j--)
149 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800150 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 j = -1;
152out_free_bio:
153 while ( ++j < nalloc )
154 bio_put(r10_bio->devs[j].bio);
155 r10bio_pool_free(r10_bio, conf);
156 return NULL;
157}
158
159static void r10buf_pool_free(void *__r10_bio, void *data)
160{
161 int i;
162 conf_t *conf = data;
163 r10bio_t *r10bio = __r10_bio;
164 int j;
165
166 for (j=0; j < conf->copies; j++) {
167 struct bio *bio = r10bio->devs[j].bio;
168 if (bio) {
169 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800170 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 bio->bi_io_vec[i].bv_page = NULL;
172 }
173 bio_put(bio);
174 }
175 }
176 r10bio_pool_free(r10bio, conf);
177}
178
179static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
180{
181 int i;
182
183 for (i = 0; i < conf->copies; i++) {
184 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -0800185 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 bio_put(*bio);
187 *bio = NULL;
188 }
189}
190
Arjan van de Ven858119e2006-01-14 13:20:43 -0800191static void free_r10bio(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
NeilBrown070ec552009-06-16 16:54:21 +1000193 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /*
196 * Wake up any possible resync thread that waits for the device
197 * to go idle.
198 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800199 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 put_all_bios(conf, r10_bio);
202 mempool_free(r10_bio, conf->r10bio_pool);
203}
204
Arjan van de Ven858119e2006-01-14 13:20:43 -0800205static void put_buf(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
NeilBrown070ec552009-06-16 16:54:21 +1000207 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 mempool_free(r10_bio, conf->r10buf_pool);
210
NeilBrown0a27ec92006-01-06 00:20:13 -0800211 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static void reschedule_retry(r10bio_t *r10_bio)
215{
216 unsigned long flags;
217 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +1000218 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 spin_lock_irqsave(&conf->device_lock, flags);
221 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800222 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_unlock_irqrestore(&conf->device_lock, flags);
224
Arthur Jones388667b2008-07-25 12:03:38 -0700225 /* wake up frozen array... */
226 wake_up(&conf->wait_barrier);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 md_wakeup_thread(mddev->thread);
229}
230
231/*
232 * raid_end_bio_io() is called when we have finished servicing a mirrored
233 * operation and are ready to return a success/failure code to the buffer
234 * cache layer.
235 */
236static void raid_end_bio_io(r10bio_t *r10_bio)
237{
238 struct bio *bio = r10_bio->master_bio;
239
NeilBrown6712ecf2007-09-27 12:47:43 +0200240 bio_endio(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
242 free_r10bio(r10_bio);
243}
244
245/*
246 * Update disk head position estimator based on IRQ completion info.
247 */
248static inline void update_head_pos(int slot, r10bio_t *r10_bio)
249{
NeilBrown070ec552009-06-16 16:54:21 +1000250 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
253 r10_bio->devs[slot].addr + (r10_bio->sectors);
254}
255
NeilBrown6712ecf2007-09-27 12:47:43 +0200256static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100259 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000261 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 slot = r10_bio->read_slot;
265 dev = r10_bio->devs[slot].devnum;
266 /*
267 * this branch is our 'one mirror IO has finished' event handler:
268 */
NeilBrown4443ae12006-01-06 00:20:28 -0800269 update_head_pos(slot, r10_bio);
270
271 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /*
273 * Set R10BIO_Uptodate in our master bio, so that
274 * we will return a good error code to the higher
275 * levels even if IO on some other mirrored buffer fails.
276 *
277 * The 'master' represents the composite IO operation to
278 * user-side. So if something waits for IO, then it will
279 * wait for the 'master' bio.
280 */
281 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 raid_end_bio_io(r10_bio);
NeilBrown4443ae12006-01-06 00:20:28 -0800283 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /*
285 * oops, read error:
286 */
287 char b[BDEVNAME_SIZE];
288 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +1000289 printk(KERN_ERR "md/raid10:%s: %s: rescheduling sector %llu\n",
290 mdname(conf->mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
292 reschedule_retry(r10_bio);
293 }
294
295 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
NeilBrown6712ecf2007-09-27 12:47:43 +0200298static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +1100301 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000303 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 for (slot = 0; slot < conf->copies; slot++)
306 if (r10_bio->devs[slot].bio == bio)
307 break;
308 dev = r10_bio->devs[slot].devnum;
309
310 /*
311 * this branch is our 'one mirror IO has finished' event handler:
312 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800313 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800315 /* an I/O failed, we can't clear the bitmap */
316 set_bit(R10BIO_Degraded, &r10_bio->state);
317 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * Set R10BIO_Uptodate in our master bio, so that
320 * we will return a good error code for to the higher
321 * levels even if IO on some other mirrored buffer fails.
322 *
323 * The 'master' represents the composite IO operation to
324 * user-side. So if something waits for IO, then it will
325 * wait for the 'master' bio.
326 */
327 set_bit(R10BIO_Uptodate, &r10_bio->state);
328
329 update_head_pos(slot, r10_bio);
330
331 /*
332 *
333 * Let's see if all mirrored write operations have finished
334 * already.
335 */
336 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800337 /* clear the bitmap if all writes complete successfully */
338 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
339 r10_bio->sectors,
340 !test_bit(R10BIO_Degraded, &r10_bio->state),
341 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 md_write_end(r10_bio->mddev);
343 raid_end_bio_io(r10_bio);
344 }
345
346 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349
350/*
351 * RAID10 layout manager
352 * Aswell as the chunksize and raid_disks count, there are two
353 * parameters: near_copies and far_copies.
354 * near_copies * far_copies must be <= raid_disks.
355 * Normally one of these will be 1.
356 * If both are 1, we get raid0.
357 * If near_copies == raid_disks, we get raid1.
358 *
359 * Chunks are layed out in raid0 style with near_copies copies of the
360 * first chunk, followed by near_copies copies of the next chunk and
361 * so on.
362 * If far_copies > 1, then after 1/far_copies of the array has been assigned
363 * as described above, we start again with a device offset of near_copies.
364 * So we effectively have another copy of the whole array further down all
365 * the drives, but with blocks on different drives.
366 * With this layout, and block is never stored twice on the one device.
367 *
368 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700369 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *
371 * raid10_find_virt does the reverse mapping, from a device and a
372 * sector offset to a virtual address
373 */
374
375static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
376{
377 int n,f;
378 sector_t sector;
379 sector_t chunk;
380 sector_t stripe;
381 int dev;
382
383 int slot = 0;
384
385 /* now calculate first sector/dev */
386 chunk = r10bio->sector >> conf->chunk_shift;
387 sector = r10bio->sector & conf->chunk_mask;
388
389 chunk *= conf->near_copies;
390 stripe = chunk;
391 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700392 if (conf->far_offset)
393 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 sector += stripe << conf->chunk_shift;
396
397 /* and calculate all the others */
398 for (n=0; n < conf->near_copies; n++) {
399 int d = dev;
400 sector_t s = sector;
401 r10bio->devs[slot].addr = sector;
402 r10bio->devs[slot].devnum = d;
403 slot++;
404
405 for (f = 1; f < conf->far_copies; f++) {
406 d += conf->near_copies;
407 if (d >= conf->raid_disks)
408 d -= conf->raid_disks;
409 s += conf->stride;
410 r10bio->devs[slot].devnum = d;
411 r10bio->devs[slot].addr = s;
412 slot++;
413 }
414 dev++;
415 if (dev >= conf->raid_disks) {
416 dev = 0;
417 sector += (conf->chunk_mask + 1);
418 }
419 }
420 BUG_ON(slot != conf->copies);
421}
422
423static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
424{
425 sector_t offset, chunk, vchunk;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700428 if (conf->far_offset) {
429 int fc;
430 chunk = sector >> conf->chunk_shift;
431 fc = sector_div(chunk, conf->far_copies);
432 dev -= fc * conf->near_copies;
433 if (dev < 0)
434 dev += conf->raid_disks;
435 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800436 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700437 sector -= conf->stride;
438 if (dev < conf->near_copies)
439 dev += conf->raid_disks - conf->near_copies;
440 else
441 dev -= conf->near_copies;
442 }
443 chunk = sector >> conf->chunk_shift;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 vchunk = chunk * conf->raid_disks + dev;
446 sector_div(vchunk, conf->near_copies);
447 return (vchunk << conf->chunk_shift) + offset;
448}
449
450/**
451 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
452 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200453 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * @biovec: the request that could be merged to it.
455 *
456 * Return amount of bytes we can accept at this offset
457 * If near_copies == raid_disk, there are no striping issues,
458 * but in that case, the function isn't called at all.
459 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200460static int raid10_mergeable_bvec(struct request_queue *q,
461 struct bvec_merge_data *bvm,
462 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200465 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000467 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200468 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
471 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200472 if (max <= biovec->bv_len && bio_sectors == 0)
473 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 else
475 return max;
476}
477
478/*
479 * This routine returns the disk from which the requested read should
480 * be done. There is a per-array 'next expected sequential IO' sector
481 * number - if this matches on the next IO then we use the last disk.
482 * There is also a per-disk 'last know head position' sector that is
483 * maintained from IRQ contexts, both the normal and the resync IO
484 * completion handlers update this position correctly. If there is no
485 * perfect sequential match then we pick the disk whose head is closest.
486 *
487 * If there are 2 mirrors in the same 2 devices, performance degrades
488 * because position is mirror, not device based.
489 *
490 * The rdev for the device selected will have nr_pending incremented.
491 */
492
493/*
494 * FIXME: possibly should rethink readbalancing and do it differently
495 * depending on near_copies / far_copies geometry.
496 */
497static int read_balance(conf_t *conf, r10bio_t *r10_bio)
498{
NeilBrownaf3a2cd2010-05-08 08:20:17 +1000499 const sector_t this_sector = r10_bio->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int disk, slot, nslot;
501 const int sectors = r10_bio->sectors;
502 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800503 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 raid10_find_phys(conf, r10_bio);
506 rcu_read_lock();
507 /*
508 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800509 * device if no resync is going on (recovery is ok), or below
510 * the resync window. We take the first readable disk when
511 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513 if (conf->mddev->recovery_cp < MaxSector
514 && (this_sector + sectors >= conf->next_resync)) {
515 /* make sure that disk is operational */
516 slot = 0;
517 disk = r10_bio->devs[slot].devnum;
518
Suzanne Woodd6065f72005-11-08 21:39:27 -0800519 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800520 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800521 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 slot++;
523 if (slot == conf->copies) {
524 slot = 0;
525 disk = -1;
526 break;
527 }
528 disk = r10_bio->devs[slot].devnum;
529 }
530 goto rb_out;
531 }
532
533
534 /* make sure the disk is operational */
535 slot = 0;
536 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800537 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800538 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800539 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 slot ++;
541 if (slot == conf->copies) {
542 disk = -1;
543 goto rb_out;
544 }
545 disk = r10_bio->devs[slot].devnum;
546 }
547
548
NeilBrown3ec67ac2005-09-09 16:23:40 -0700549 current_distance = abs(r10_bio->devs[slot].addr -
550 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800552 /* Find the disk whose head is closest,
553 * or - for far > 1 - find the closest to partition beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 for (nslot = slot; nslot < conf->copies; nslot++) {
556 int ndisk = r10_bio->devs[nslot].devnum;
557
558
Suzanne Woodd6065f72005-11-08 21:39:27 -0800559 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800560 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800561 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 continue;
563
NeilBrown22dfdf52005-11-28 13:44:09 -0800564 /* This optimisation is debatable, and completely destroys
565 * sequential read speed for 'far copies' arrays. So only
566 * keep it for 'near' arrays, and review those later.
567 */
568 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 disk = ndisk;
570 slot = nslot;
571 break;
572 }
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800573
574 /* for far > 1 always use the lowest address */
575 if (conf->far_copies > 1)
576 new_distance = r10_bio->devs[nslot].addr;
577 else
578 new_distance = abs(r10_bio->devs[nslot].addr -
579 conf->mirrors[ndisk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (new_distance < current_distance) {
581 current_distance = new_distance;
582 disk = ndisk;
583 slot = nslot;
584 }
585 }
586
587rb_out:
588 r10_bio->read_slot = slot;
589/* conf->next_seq_sect = this_sector + sectors;*/
590
Suzanne Woodd6065f72005-11-08 21:39:27 -0800591 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800593 else
594 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 rcu_read_unlock();
596
597 return disk;
598}
599
600static void unplug_slaves(mddev_t *mddev)
601{
NeilBrown070ec552009-06-16 16:54:21 +1000602 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int i;
604
605 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100606 for (i=0; i < conf->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800607 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800608 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200609 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 atomic_inc(&rdev->nr_pending);
612 rcu_read_unlock();
613
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500614 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 rdev_dec_pending(rdev, mddev);
617 rcu_read_lock();
618 }
619 }
620 rcu_read_unlock();
621}
622
Jens Axboe165125e2007-07-24 09:28:11 +0200623static void raid10_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
NeilBrown6cce3b22006-01-06 00:20:16 -0800625 mddev_t *mddev = q->queuedata;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unplug_slaves(q->queuedata);
NeilBrown6cce3b22006-01-06 00:20:16 -0800628 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
NeilBrown0d129222006-10-03 01:15:54 -0700631static int raid10_congested(void *data, int bits)
632{
633 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000634 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700635 int i, ret = 0;
636
NeilBrown3fa841d2009-09-23 18:10:29 +1000637 if (mddev_congested(mddev, bits))
638 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700639 rcu_read_lock();
NeilBrown84707f32010-03-16 17:23:35 +1100640 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
NeilBrown0d129222006-10-03 01:15:54 -0700641 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
642 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200643 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700644
645 ret |= bdi_congested(&q->backing_dev_info, bits);
646 }
647 }
648 rcu_read_unlock();
649 return ret;
650}
651
NeilBrowna35e63e2008-03-04 14:29:29 -0800652static int flush_pending_writes(conf_t *conf)
653{
654 /* Any writes that have been queued but are awaiting
655 * bitmap updates get flushed here.
656 * We return 1 if any requests were actually submitted.
657 */
658 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700659
NeilBrowna35e63e2008-03-04 14:29:29 -0800660 spin_lock_irq(&conf->device_lock);
661
662 if (conf->pending_bio_list.head) {
663 struct bio *bio;
664 bio = bio_list_get(&conf->pending_bio_list);
665 blk_remove_plug(conf->mddev->queue);
666 spin_unlock_irq(&conf->device_lock);
667 /* flush any pending bitmap writes to disk
668 * before proceeding w/ I/O */
669 bitmap_unplug(conf->mddev->bitmap);
670
671 while (bio) { /* submit pending writes */
672 struct bio *next = bio->bi_next;
673 bio->bi_next = NULL;
674 generic_make_request(bio);
675 bio = next;
676 }
677 rv = 1;
678 } else
679 spin_unlock_irq(&conf->device_lock);
680 return rv;
681}
NeilBrown0a27ec92006-01-06 00:20:13 -0800682/* Barriers....
683 * Sometimes we need to suspend IO while we do something else,
684 * either some resync/recovery, or reconfigure the array.
685 * To do this we raise a 'barrier'.
686 * The 'barrier' is a counter that can be raised multiple times
687 * to count how many activities are happening which preclude
688 * normal IO.
689 * We can only raise the barrier if there is no pending IO.
690 * i.e. if nr_pending == 0.
691 * We choose only to raise the barrier if no-one is waiting for the
692 * barrier to go down. This means that as soon as an IO request
693 * is ready, no other operations which require a barrier will start
694 * until the IO request has had a chance.
695 *
696 * So: regular IO calls 'wait_barrier'. When that returns there
697 * is no backgroup IO happening, It must arrange to call
698 * allow_barrier when it has finished its IO.
699 * backgroup IO calls must call raise_barrier. Once that returns
700 * there is no normal IO happeing. It must arrange to call
701 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
NeilBrown6cce3b22006-01-06 00:20:16 -0800704static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
NeilBrown6cce3b22006-01-06 00:20:16 -0800706 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
NeilBrown6cce3b22006-01-06 00:20:16 -0800709 /* Wait until no block IO is waiting (unless 'force') */
710 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800711 conf->resync_lock,
712 raid10_unplug(conf->mddev->queue));
713
714 /* block any new IO from starting */
715 conf->barrier++;
716
717 /* No wait for all pending IO to complete */
718 wait_event_lock_irq(conf->wait_barrier,
719 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
720 conf->resync_lock,
721 raid10_unplug(conf->mddev->queue));
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 spin_unlock_irq(&conf->resync_lock);
724}
725
NeilBrown0a27ec92006-01-06 00:20:13 -0800726static void lower_barrier(conf_t *conf)
727{
728 unsigned long flags;
729 spin_lock_irqsave(&conf->resync_lock, flags);
730 conf->barrier--;
731 spin_unlock_irqrestore(&conf->resync_lock, flags);
732 wake_up(&conf->wait_barrier);
733}
734
735static void wait_barrier(conf_t *conf)
736{
737 spin_lock_irq(&conf->resync_lock);
738 if (conf->barrier) {
739 conf->nr_waiting++;
740 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
741 conf->resync_lock,
742 raid10_unplug(conf->mddev->queue));
743 conf->nr_waiting--;
744 }
745 conf->nr_pending++;
746 spin_unlock_irq(&conf->resync_lock);
747}
748
749static void allow_barrier(conf_t *conf)
750{
751 unsigned long flags;
752 spin_lock_irqsave(&conf->resync_lock, flags);
753 conf->nr_pending--;
754 spin_unlock_irqrestore(&conf->resync_lock, flags);
755 wake_up(&conf->wait_barrier);
756}
757
NeilBrown4443ae12006-01-06 00:20:28 -0800758static void freeze_array(conf_t *conf)
759{
760 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800761 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800762 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800763 * wait until nr_pending match nr_queued+1
764 * This is called in the context of one normal IO request
765 * that has failed. Thus any sync request that might be pending
766 * will be blocked by nr_pending, and we need to wait for
767 * pending IO requests to complete or be queued for re-try.
768 * Thus the number queued (nr_queued) plus this request (1)
769 * must match the number of pending IOs (nr_pending) before
770 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800771 */
772 spin_lock_irq(&conf->resync_lock);
773 conf->barrier++;
774 conf->nr_waiting++;
775 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800776 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800777 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800778 ({ flush_pending_writes(conf);
779 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800780 spin_unlock_irq(&conf->resync_lock);
781}
782
783static void unfreeze_array(conf_t *conf)
784{
785 /* reverse the effect of the freeze */
786 spin_lock_irq(&conf->resync_lock);
787 conf->barrier--;
788 conf->nr_waiting--;
789 wake_up(&conf->wait_barrier);
790 spin_unlock_irq(&conf->resync_lock);
791}
792
NeilBrown21a52c62010-04-01 15:02:13 +1100793static int make_request(mddev_t *mddev, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
NeilBrown070ec552009-06-16 16:54:21 +1000795 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 mirror_info_t *mirror;
797 r10bio_t *r10_bio;
798 struct bio *read_bio;
799 int i;
800 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100801 const int rw = bio_data_dir(bio);
NeilBrown2c7d46e2010-08-18 16:16:05 +1000802 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
Tejun Heoe9c74692010-09-03 11:56:18 +0200803 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
NeilBrown6cce3b22006-01-06 00:20:16 -0800804 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700805 mdk_rdev_t *blocked_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Tejun Heoe9c74692010-09-03 11:56:18 +0200807 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
808 md_flush_request(mddev, bio);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700809 return 0;
810 }
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* If this request crosses a chunk boundary, we need to
813 * split it. This will only happen for 1 PAGE (or less) requests.
814 */
815 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
816 > chunk_sects &&
817 conf->near_copies < conf->raid_disks)) {
818 struct bio_pair *bp;
819 /* Sanity check -- queue functions should prevent this happening */
820 if (bio->bi_vcnt != 1 ||
821 bio->bi_idx != 0)
822 goto bad_map;
823 /* This is a one page bio that upper layers
824 * refuse to split for us, so we need to split it.
825 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200826 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
NeilBrown51e9ac72010-08-07 21:17:00 +1000828
829 /* Each of these 'make_request' calls will call 'wait_barrier'.
830 * If the first succeeds but the second blocks due to the resync
831 * thread raising the barrier, we will deadlock because the
832 * IO to the underlying device will be queued in generic_make_request
833 * and will never complete, so will never reduce nr_pending.
834 * So increment nr_waiting here so no new raise_barriers will
835 * succeed, and so the second wait_barrier cannot block.
836 */
837 spin_lock_irq(&conf->resync_lock);
838 conf->nr_waiting++;
839 spin_unlock_irq(&conf->resync_lock);
840
NeilBrown21a52c62010-04-01 15:02:13 +1100841 if (make_request(mddev, &bp->bio1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 generic_make_request(&bp->bio1);
NeilBrown21a52c62010-04-01 15:02:13 +1100843 if (make_request(mddev, &bp->bio2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 generic_make_request(&bp->bio2);
845
NeilBrown51e9ac72010-08-07 21:17:00 +1000846 spin_lock_irq(&conf->resync_lock);
847 conf->nr_waiting--;
848 wake_up(&conf->wait_barrier);
849 spin_unlock_irq(&conf->resync_lock);
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 bio_pair_release(bp);
852 return 0;
853 bad_map:
NeilBrown128595e2010-05-03 14:47:14 +1000854 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
855 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
857
NeilBrown6712ecf2007-09-27 12:47:43 +0200858 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 0;
860 }
861
NeilBrown3d310eb2005-06-21 17:17:26 -0700862 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /*
865 * Register the new request and wait if the reconstruction
866 * thread has put up a bar for new requests.
867 * Continue immediately if no resync is active currently.
868 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800869 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
872
873 r10_bio->master_bio = bio;
874 r10_bio->sectors = bio->bi_size >> 9;
875
876 r10_bio->mddev = mddev;
877 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800878 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jens Axboea3623572005-11-01 09:26:16 +0100880 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
882 * read balancing logic:
883 */
884 int disk = read_balance(conf, r10_bio);
885 int slot = r10_bio->read_slot;
886 if (disk < 0) {
887 raid_end_bio_io(r10_bio);
888 return 0;
889 }
890 mirror = conf->mirrors + disk;
891
NeilBrowna167f662010-10-26 18:31:13 +1100892 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 r10_bio->devs[slot].bio = read_bio;
895
896 read_bio->bi_sector = r10_bio->devs[slot].addr +
897 mirror->rdev->data_offset;
898 read_bio->bi_bdev = mirror->rdev->bdev;
899 read_bio->bi_end_io = raid10_end_read_request;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200900 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 read_bio->bi_private = r10_bio;
902
903 generic_make_request(read_bio);
904 return 0;
905 }
906
907 /*
908 * WRITE:
909 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700910 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * inc refcount on their rdev. Record them by setting
912 * bios[x] to bio
913 */
914 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700915 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700916 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 rcu_read_lock();
918 for (i = 0; i < conf->copies; i++) {
919 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800920 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700921 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
922 atomic_inc(&rdev->nr_pending);
923 blocked_rdev = rdev;
924 break;
925 }
926 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800927 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800929 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800931 set_bit(R10BIO_Degraded, &r10_bio->state);
932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 rcu_read_unlock();
935
Dan Williams6bfe0b42008-04-30 00:52:32 -0700936 if (unlikely(blocked_rdev)) {
937 /* Have to wait for this device to get unblocked, then retry */
938 int j;
939 int d;
940
941 for (j = 0; j < i; j++)
942 if (r10_bio->devs[j].bio) {
943 d = r10_bio->devs[j].devnum;
944 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
945 }
946 allow_barrier(conf);
947 md_wait_for_blocked_rdev(blocked_rdev, mddev);
948 wait_barrier(conf);
949 goto retry_write;
950 }
951
NeilBrown4e780642010-10-19 12:54:01 +1100952 atomic_set(&r10_bio->remaining, 1);
953 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 for (i = 0; i < conf->copies; i++) {
956 struct bio *mbio;
957 int d = r10_bio->devs[i].devnum;
958 if (!r10_bio->devs[i].bio)
959 continue;
960
NeilBrowna167f662010-10-26 18:31:13 +1100961 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 r10_bio->devs[i].bio = mbio;
963
964 mbio->bi_sector = r10_bio->devs[i].addr+
965 conf->mirrors[d].rdev->data_offset;
966 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
967 mbio->bi_end_io = raid10_end_write_request;
Tejun Heoe9c74692010-09-03 11:56:18 +0200968 mbio->bi_rw = WRITE | do_sync | do_fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 mbio->bi_private = r10_bio;
970
971 atomic_inc(&r10_bio->remaining);
NeilBrown4e780642010-10-19 12:54:01 +1100972 spin_lock_irqsave(&conf->device_lock, flags);
973 bio_list_add(&conf->pending_bio_list, mbio);
974 blk_plug_device(mddev->queue);
975 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
NeilBrown4e780642010-10-19 12:54:01 +1100978 if (atomic_dec_and_test(&r10_bio->remaining)) {
979 /* This matches the end of raid10_end_write_request() */
980 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
981 r10_bio->sectors,
982 !test_bit(R10BIO_Degraded, &r10_bio->state),
983 0);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700984 md_write_end(mddev);
985 raid_end_bio_io(r10_bio);
Arne Redlichf6f953a2007-07-31 00:37:57 -0700986 }
987
NeilBrowna35e63e2008-03-04 14:29:29 -0800988 /* In case raid10d snuck in to freeze_array */
989 wake_up(&conf->wait_barrier);
990
Lars Ellenberge3881a62007-01-10 23:15:37 -0800991 if (do_sync)
992 md_wakeup_thread(mddev->thread);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return 0;
995}
996
997static void status(struct seq_file *seq, mddev_t *mddev)
998{
NeilBrown070ec552009-06-16 16:54:21 +1000999 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int i;
1001
1002 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +10001003 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (conf->near_copies > 1)
1005 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -07001006 if (conf->far_copies > 1) {
1007 if (conf->far_offset)
1008 seq_printf(seq, " %d offset-copies", conf->far_copies);
1009 else
1010 seq_printf(seq, " %d far-copies", conf->far_copies);
1011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001013 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 for (i = 0; i < conf->raid_disks; i++)
1015 seq_printf(seq, "%s",
1016 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001017 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 seq_printf(seq, "]");
1019}
1020
1021static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1022{
1023 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001024 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /*
1027 * If it is not operational, then we have already marked it as dead
1028 * else if it is the last working disks, ignore the error, let the
1029 * next level up know.
1030 * else mark the drive as failed
1031 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001032 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -07001033 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * Don't fail the drive, just return an IO error.
1036 * The test should really be more sophisticated than
1037 * "working_disks == 1", but it isn't critical, and
1038 * can wait until we do more sophisticated "is the drive
1039 * really dead" tests...
1040 */
1041 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001042 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1043 unsigned long flags;
1044 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001046 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
1048 * if recovery is running, make sure it aborts.
1049 */
NeilBrowndfc70642008-05-23 13:04:39 -07001050 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001052 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001053 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Joe Perches067032b2011-01-14 09:14:33 +11001054 printk(KERN_ALERT
1055 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1056 "md/raid10:%s: Operation continuing on %d devices.\n",
NeilBrown128595e2010-05-03 14:47:14 +10001057 mdname(mddev), bdevname(rdev->bdev, b),
1058 mdname(mddev), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061static void print_conf(conf_t *conf)
1062{
1063 int i;
1064 mirror_info_t *tmp;
1065
NeilBrown128595e2010-05-03 14:47:14 +10001066 printk(KERN_DEBUG "RAID10 conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (!conf) {
NeilBrown128595e2010-05-03 14:47:14 +10001068 printk(KERN_DEBUG "(!conf)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return;
1070 }
NeilBrown128595e2010-05-03 14:47:14 +10001071 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 conf->raid_disks);
1073
1074 for (i = 0; i < conf->raid_disks; i++) {
1075 char b[BDEVNAME_SIZE];
1076 tmp = conf->mirrors + i;
1077 if (tmp->rdev)
NeilBrown128595e2010-05-03 14:47:14 +10001078 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001079 i, !test_bit(In_sync, &tmp->rdev->flags),
1080 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 bdevname(tmp->rdev->bdev,b));
1082 }
1083}
1084
1085static void close_sync(conf_t *conf)
1086{
NeilBrown0a27ec92006-01-06 00:20:13 -08001087 wait_barrier(conf);
1088 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 mempool_destroy(conf->r10buf_pool);
1091 conf->r10buf_pool = NULL;
1092}
1093
NeilBrown6d508242005-09-09 16:24:03 -07001094/* check if there are enough drives for
1095 * every block to appear on atleast one
1096 */
1097static int enough(conf_t *conf)
1098{
1099 int first = 0;
1100
1101 do {
1102 int n = conf->copies;
1103 int cnt = 0;
1104 while (n--) {
1105 if (conf->mirrors[first].rdev)
1106 cnt++;
1107 first = (first+1) % conf->raid_disks;
1108 }
1109 if (cnt == 0)
1110 return 0;
1111 } while (first != 0);
1112 return 1;
1113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115static int raid10_spare_active(mddev_t *mddev)
1116{
1117 int i;
1118 conf_t *conf = mddev->private;
1119 mirror_info_t *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10001120 int count = 0;
1121 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
1124 * Find all non-in_sync disks within the RAID10 configuration
1125 * and mark them in_sync
1126 */
1127 for (i = 0; i < conf->raid_disks; i++) {
1128 tmp = conf->mirrors + i;
1129 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001130 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001131 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10001132 count++;
Adrian Drzewieckie6ffbcb2010-08-18 11:49:02 +10001133 sysfs_notify_dirent(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 }
NeilBrown6b965622010-08-18 11:56:59 +10001136 spin_lock_irqsave(&conf->device_lock, flags);
1137 mddev->degraded -= count;
1138 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 print_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10001141 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144
1145static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1146{
1147 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001148 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int mirror;
1150 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001151 int first = 0;
NeilBrown84707f32010-03-16 17:23:35 +11001152 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 if (mddev->recovery_cp < MaxSector)
1155 /* only hot-add to in-sync arrays, as recovery is
1156 * very different from resync
1157 */
Neil Brown199050e2008-06-28 08:31:33 +10001158 return -EBUSY;
NeilBrown6d508242005-09-09 16:24:03 -07001159 if (!enough(conf))
Neil Brown199050e2008-06-28 08:31:33 +10001160 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
NeilBrowna53a6c82008-11-06 17:28:20 +11001162 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001163 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
NeilBrown6cce3b22006-01-06 00:20:16 -08001165 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10001166 rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001167 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1168 mirror = rdev->saved_raid_disk;
1169 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001170 mirror = first;
1171 for ( ; mirror <= last ; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if ( !(p=conf->mirrors+mirror)->rdev) {
1173
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001174 disk_stack_limits(mddev->gendisk, rdev->bdev,
1175 rdev->data_offset << 9);
NeilBrown627a2d32010-03-08 16:44:38 +11001176 /* as we don't honour merge_bvec_fn, we must
1177 * never risk violating it, so limit
1178 * ->max_segments to one lying with a single
1179 * page, as a one page request is never in
1180 * violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 */
NeilBrown627a2d32010-03-08 16:44:38 +11001182 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1183 blk_queue_max_segments(mddev->queue, 1);
1184 blk_queue_segment_boundary(mddev->queue,
1185 PAGE_CACHE_SIZE - 1);
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 p->head_position = 0;
1189 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001190 err = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001191 if (rdev->saved_raid_disk != mirror)
1192 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001193 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 break;
1195 }
1196
Andre Nollac5e7112009-08-03 10:59:47 +10001197 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
1202static int raid10_remove_disk(mddev_t *mddev, int number)
1203{
1204 conf_t *conf = mddev->private;
1205 int err = 0;
1206 mdk_rdev_t *rdev;
1207 mirror_info_t *p = conf->mirrors+ number;
1208
1209 print_conf(conf);
1210 rdev = p->rdev;
1211 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001212 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 atomic_read(&rdev->nr_pending)) {
1214 err = -EBUSY;
1215 goto abort;
1216 }
NeilBrowndfc70642008-05-23 13:04:39 -07001217 /* Only remove faulty devices in recovery
1218 * is not possible.
1219 */
1220 if (!test_bit(Faulty, &rdev->flags) &&
1221 enough(conf)) {
1222 err = -EBUSY;
1223 goto abort;
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001226 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (atomic_read(&rdev->nr_pending)) {
1228 /* lost the race, try later */
1229 err = -EBUSY;
1230 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001231 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Andre Nollac5e7112009-08-03 10:59:47 +10001233 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235abort:
1236
1237 print_conf(conf);
1238 return err;
1239}
1240
1241
NeilBrown6712ecf2007-09-27 12:47:43 +02001242static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001244 r10bio_t *r10_bio = bio->bi_private;
NeilBrown070ec552009-06-16 16:54:21 +10001245 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int i,d;
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 for (i=0; i<conf->copies; i++)
1249 if (r10_bio->devs[i].bio == bio)
1250 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001251 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 update_head_pos(i, r10_bio);
1253 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001254
1255 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1256 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001257 else {
1258 atomic_add(r10_bio->sectors,
1259 &conf->mirrors[d].rdev->corrected_errors);
1260 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1261 md_error(r10_bio->mddev,
1262 conf->mirrors[d].rdev);
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* for reconstruct, we always reschedule after a read.
1266 * for resync, only after all reads
1267 */
NeilBrown73d5c382009-02-25 13:18:47 +11001268 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1270 atomic_dec_and_test(&r10_bio->remaining)) {
1271 /* we have read all the blocks,
1272 * do the comparison in process context in raid10d
1273 */
1274 reschedule_retry(r10_bio);
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
NeilBrown6712ecf2007-09-27 12:47:43 +02001278static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
H Hartley Sweeten7b928132010-03-08 16:02:40 +11001281 r10bio_t *r10_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001283 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int i,d;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 for (i = 0; i < conf->copies; i++)
1287 if (r10_bio->devs[i].bio == bio)
1288 break;
1289 d = r10_bio->devs[i].devnum;
1290
1291 if (!uptodate)
1292 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 update_head_pos(i, r10_bio);
1295
NeilBrown73d5c382009-02-25 13:18:47 +11001296 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 while (atomic_dec_and_test(&r10_bio->remaining)) {
1298 if (r10_bio->master_bio == NULL) {
1299 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001300 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001302 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304 } else {
1305 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1306 put_buf(r10_bio);
1307 r10_bio = r10_bio2;
1308 }
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312/*
1313 * Note: sync and recover and handled very differently for raid10
1314 * This code is for resync.
1315 * For resync, we read through virtual addresses and read all blocks.
1316 * If there is any error, we schedule a write. The lowest numbered
1317 * drive is authoritative.
1318 * However requests come for physical address, so we need to map.
1319 * For every physical address there are raid_disks/copies virtual addresses,
1320 * which is always are least one, but is not necessarly an integer.
1321 * This means that a physical address can span multiple chunks, so we may
1322 * have to submit multiple io requests for a single sync request.
1323 */
1324/*
1325 * We check if all blocks are in-sync and only write to blocks that
1326 * aren't in sync
1327 */
1328static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1329{
NeilBrown070ec552009-06-16 16:54:21 +10001330 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int i, first;
1332 struct bio *tbio, *fbio;
1333
1334 atomic_set(&r10_bio->remaining, 1);
1335
1336 /* find the first device with a block */
1337 for (i=0; i<conf->copies; i++)
1338 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1339 break;
1340
1341 if (i == conf->copies)
1342 goto done;
1343
1344 first = i;
1345 fbio = r10_bio->devs[i].bio;
1346
1347 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001348 for (i=0 ; i < conf->copies ; i++) {
1349 int j, d;
1350 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001353
1354 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001356 if (i == first)
1357 continue;
1358 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1359 /* We know that the bi_io_vec layout is the same for
1360 * both 'first' and 'i', so we just compare them.
1361 * All vec entries are PAGE_SIZE;
1362 */
1363 for (j = 0; j < vcnt; j++)
1364 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1365 page_address(tbio->bi_io_vec[j].bv_page),
1366 PAGE_SIZE))
1367 break;
1368 if (j == vcnt)
1369 continue;
1370 mddev->resync_mismatches += r10_bio->sectors;
1371 }
NeilBrown18f08812006-01-06 00:20:25 -08001372 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1373 /* Don't fix anything. */
1374 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /* Ok, we need to write this bio
1376 * First we need to fixup bv_offset, bv_len and
1377 * bi_vecs, as the read request might have corrupted these
1378 */
1379 tbio->bi_vcnt = vcnt;
1380 tbio->bi_size = r10_bio->sectors << 9;
1381 tbio->bi_idx = 0;
1382 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1384 tbio->bi_flags |= 1 << BIO_UPTODATE;
1385 tbio->bi_next = NULL;
1386 tbio->bi_rw = WRITE;
1387 tbio->bi_private = r10_bio;
1388 tbio->bi_sector = r10_bio->devs[i].addr;
1389
1390 for (j=0; j < vcnt ; j++) {
1391 tbio->bi_io_vec[j].bv_offset = 0;
1392 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1393
1394 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1395 page_address(fbio->bi_io_vec[j].bv_page),
1396 PAGE_SIZE);
1397 }
1398 tbio->bi_end_io = end_sync_write;
1399
1400 d = r10_bio->devs[i].devnum;
1401 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1402 atomic_inc(&r10_bio->remaining);
1403 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1404
1405 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1406 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1407 generic_make_request(tbio);
1408 }
1409
1410done:
1411 if (atomic_dec_and_test(&r10_bio->remaining)) {
1412 md_done_sync(mddev, r10_bio->sectors, 1);
1413 put_buf(r10_bio);
1414 }
1415}
1416
1417/*
1418 * Now for the recovery code.
1419 * Recovery happens across physical sectors.
1420 * We recover all non-is_sync drives by finding the virtual address of
1421 * each, and then choose a working drive that also has that virt address.
1422 * There is a separate r10_bio for each non-in_sync drive.
1423 * Only the first two slots are in use. The first for reading,
1424 * The second for writing.
1425 *
1426 */
1427
1428static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1429{
NeilBrown070ec552009-06-16 16:54:21 +10001430 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int i, d;
1432 struct bio *bio, *wbio;
1433
1434
1435 /* move the pages across to the second bio
1436 * and submit the write request
1437 */
1438 bio = r10_bio->devs[0].bio;
1439 wbio = r10_bio->devs[1].bio;
1440 for (i=0; i < wbio->bi_vcnt; i++) {
1441 struct page *p = bio->bi_io_vec[i].bv_page;
1442 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1443 wbio->bi_io_vec[i].bv_page = p;
1444 }
1445 d = r10_bio->devs[1].devnum;
1446
1447 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1448 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001449 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1450 generic_make_request(wbio);
1451 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001452 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455
1456/*
Robert Becker1e509152009-12-14 12:49:58 +11001457 * Used by fix_read_error() to decay the per rdev read_errors.
1458 * We halve the read error count for every hour that has elapsed
1459 * since the last recorded read error.
1460 *
1461 */
1462static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1463{
1464 struct timespec cur_time_mon;
1465 unsigned long hours_since_last;
1466 unsigned int read_errors = atomic_read(&rdev->read_errors);
1467
1468 ktime_get_ts(&cur_time_mon);
1469
1470 if (rdev->last_read_error.tv_sec == 0 &&
1471 rdev->last_read_error.tv_nsec == 0) {
1472 /* first time we've seen a read error */
1473 rdev->last_read_error = cur_time_mon;
1474 return;
1475 }
1476
1477 hours_since_last = (cur_time_mon.tv_sec -
1478 rdev->last_read_error.tv_sec) / 3600;
1479
1480 rdev->last_read_error = cur_time_mon;
1481
1482 /*
1483 * if hours_since_last is > the number of bits in read_errors
1484 * just set read errors to 0. We do this to avoid
1485 * overflowing the shift of read_errors by hours_since_last.
1486 */
1487 if (hours_since_last >= 8 * sizeof(read_errors))
1488 atomic_set(&rdev->read_errors, 0);
1489 else
1490 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1491}
1492
1493/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * This is a kernel thread which:
1495 *
1496 * 1. Retries failed read operations on working mirrors.
1497 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001498 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
1500
NeilBrown6814d532006-10-03 01:15:45 -07001501static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1502{
1503 int sect = 0; /* Offset from r10_bio->sector */
1504 int sectors = r10_bio->sectors;
1505 mdk_rdev_t*rdev;
Robert Becker1e509152009-12-14 12:49:58 +11001506 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001507 int d = r10_bio->devs[r10_bio->read_slot].devnum;
Robert Becker1e509152009-12-14 12:49:58 +11001508
1509 rcu_read_lock();
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001510 rdev = rcu_dereference(conf->mirrors[d].rdev);
1511 if (rdev) { /* If rdev is not NULL */
Robert Becker1e509152009-12-14 12:49:58 +11001512 char b[BDEVNAME_SIZE];
1513 int cur_read_error_count = 0;
1514
Robert Becker1e509152009-12-14 12:49:58 +11001515 bdevname(rdev->bdev, b);
1516
1517 if (test_bit(Faulty, &rdev->flags)) {
1518 rcu_read_unlock();
1519 /* drive has already been failed, just ignore any
1520 more fix_read_error() attempts */
1521 return;
1522 }
1523
1524 check_decay_read_errors(mddev, rdev);
1525 atomic_inc(&rdev->read_errors);
1526 cur_read_error_count = atomic_read(&rdev->read_errors);
1527 if (cur_read_error_count > max_read_errors) {
1528 rcu_read_unlock();
1529 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001530 "md/raid10:%s: %s: Raid device exceeded "
Robert Becker1e509152009-12-14 12:49:58 +11001531 "read_error threshold "
1532 "[cur %d:max %d]\n",
NeilBrown128595e2010-05-03 14:47:14 +10001533 mdname(mddev),
Robert Becker1e509152009-12-14 12:49:58 +11001534 b, cur_read_error_count, max_read_errors);
1535 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001536 "md/raid10:%s: %s: Failing raid "
1537 "device\n", mdname(mddev), b);
Robert Becker1e509152009-12-14 12:49:58 +11001538 md_error(mddev, conf->mirrors[d].rdev);
1539 return;
1540 }
1541 }
1542 rcu_read_unlock();
1543
NeilBrown6814d532006-10-03 01:15:45 -07001544 while(sectors) {
1545 int s = sectors;
1546 int sl = r10_bio->read_slot;
1547 int success = 0;
1548 int start;
1549
1550 if (s > (PAGE_SIZE>>9))
1551 s = PAGE_SIZE >> 9;
1552
1553 rcu_read_lock();
1554 do {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001555 d = r10_bio->devs[sl].devnum;
NeilBrown6814d532006-10-03 01:15:45 -07001556 rdev = rcu_dereference(conf->mirrors[d].rdev);
1557 if (rdev &&
1558 test_bit(In_sync, &rdev->flags)) {
1559 atomic_inc(&rdev->nr_pending);
1560 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001561 success = sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001562 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001563 sect,
NeilBrown6814d532006-10-03 01:15:45 -07001564 s<<9,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001565 conf->tmppage, READ, false);
NeilBrown6814d532006-10-03 01:15:45 -07001566 rdev_dec_pending(rdev, mddev);
1567 rcu_read_lock();
1568 if (success)
1569 break;
1570 }
1571 sl++;
1572 if (sl == conf->copies)
1573 sl = 0;
1574 } while (!success && sl != r10_bio->read_slot);
1575 rcu_read_unlock();
1576
1577 if (!success) {
1578 /* Cannot read from anywhere -- bye bye array */
1579 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1580 md_error(mddev, conf->mirrors[dn].rdev);
1581 break;
1582 }
1583
1584 start = sl;
1585 /* write it back and re-read */
1586 rcu_read_lock();
1587 while (sl != r10_bio->read_slot) {
Robert Becker67b8dc42009-12-14 12:49:57 +11001588 char b[BDEVNAME_SIZE];
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001589
NeilBrown6814d532006-10-03 01:15:45 -07001590 if (sl==0)
1591 sl = conf->copies;
1592 sl--;
1593 d = r10_bio->devs[sl].devnum;
1594 rdev = rcu_dereference(conf->mirrors[d].rdev);
1595 if (rdev &&
1596 test_bit(In_sync, &rdev->flags)) {
1597 atomic_inc(&rdev->nr_pending);
1598 rcu_read_unlock();
1599 atomic_add(s, &rdev->corrected_errors);
NeilBrown2b193362010-10-27 15:16:40 +11001600 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001601 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001602 sect,
1603 s<<9, conf->tmppage, WRITE, false)
Robert Becker67b8dc42009-12-14 12:49:57 +11001604 == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001605 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001606 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001607 "md/raid10:%s: read correction "
Robert Becker67b8dc42009-12-14 12:49:57 +11001608 "write failed"
1609 " (%d sectors at %llu on %s)\n",
1610 mdname(mddev), s,
1611 (unsigned long long)(sect+
1612 rdev->data_offset),
1613 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001614 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
Robert Becker67b8dc42009-12-14 12:49:57 +11001615 "drive\n",
NeilBrown128595e2010-05-03 14:47:14 +10001616 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001617 bdevname(rdev->bdev, b));
NeilBrown6814d532006-10-03 01:15:45 -07001618 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001619 }
NeilBrown6814d532006-10-03 01:15:45 -07001620 rdev_dec_pending(rdev, mddev);
1621 rcu_read_lock();
1622 }
1623 }
1624 sl = start;
1625 while (sl != r10_bio->read_slot) {
Prasanna S. Panchamukhi0544a212010-06-24 13:31:03 +10001626
NeilBrown6814d532006-10-03 01:15:45 -07001627 if (sl==0)
1628 sl = conf->copies;
1629 sl--;
1630 d = r10_bio->devs[sl].devnum;
1631 rdev = rcu_dereference(conf->mirrors[d].rdev);
1632 if (rdev &&
1633 test_bit(In_sync, &rdev->flags)) {
1634 char b[BDEVNAME_SIZE];
1635 atomic_inc(&rdev->nr_pending);
1636 rcu_read_unlock();
NeilBrown2b193362010-10-27 15:16:40 +11001637 if (sync_page_io(rdev,
NeilBrown6814d532006-10-03 01:15:45 -07001638 r10_bio->devs[sl].addr +
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001639 sect,
Robert Becker67b8dc42009-12-14 12:49:57 +11001640 s<<9, conf->tmppage,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +11001641 READ, false) == 0) {
NeilBrown6814d532006-10-03 01:15:45 -07001642 /* Well, this device is dead */
Robert Becker67b8dc42009-12-14 12:49:57 +11001643 printk(KERN_NOTICE
NeilBrown128595e2010-05-03 14:47:14 +10001644 "md/raid10:%s: unable to read back "
Robert Becker67b8dc42009-12-14 12:49:57 +11001645 "corrected sectors"
1646 " (%d sectors at %llu on %s)\n",
1647 mdname(mddev), s,
1648 (unsigned long long)(sect+
1649 rdev->data_offset),
1650 bdevname(rdev->bdev, b));
NeilBrown128595e2010-05-03 14:47:14 +10001651 printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
1652 mdname(mddev),
Robert Becker67b8dc42009-12-14 12:49:57 +11001653 bdevname(rdev->bdev, b));
1654
NeilBrown6814d532006-10-03 01:15:45 -07001655 md_error(mddev, rdev);
Robert Becker67b8dc42009-12-14 12:49:57 +11001656 } else {
NeilBrown6814d532006-10-03 01:15:45 -07001657 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10001658 "md/raid10:%s: read error corrected"
NeilBrown6814d532006-10-03 01:15:45 -07001659 " (%d sectors at %llu on %s)\n",
1660 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001661 (unsigned long long)(sect+
1662 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001663 bdevname(rdev->bdev, b));
Robert Becker67b8dc42009-12-14 12:49:57 +11001664 }
NeilBrown6814d532006-10-03 01:15:45 -07001665
1666 rdev_dec_pending(rdev, mddev);
1667 rcu_read_lock();
1668 }
1669 }
1670 rcu_read_unlock();
1671
1672 sectors -= s;
1673 sect += s;
1674 }
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static void raid10d(mddev_t *mddev)
1678{
1679 r10bio_t *r10_bio;
1680 struct bio *bio;
1681 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001682 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 struct list_head *head = &conf->retry_list;
1684 int unplug=0;
1685 mdk_rdev_t *rdev;
1686
1687 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 for (;;) {
1690 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001691
1692 unplug += flush_pending_writes(conf);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001695 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001696 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1700 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001701 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 spin_unlock_irqrestore(&conf->device_lock, flags);
1703
1704 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001705 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1707 sync_request_write(mddev, r10_bio);
1708 unplug = 1;
1709 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1710 recovery_request_write(mddev, r10_bio);
1711 unplug = 1;
1712 } else {
1713 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001714 /* we got a read error. Maybe the drive is bad. Maybe just
1715 * the block and we can fix it.
1716 * We freeze all other IO, and try reading the block from
1717 * other devices. When we find one, we re-write
1718 * and check it that fixes the read error.
1719 * This is all done synchronously while the array is
1720 * frozen.
1721 */
NeilBrown6814d532006-10-03 01:15:45 -07001722 if (mddev->ro == 0) {
1723 freeze_array(conf);
1724 fix_read_error(conf, mddev, r10_bio);
1725 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001726 }
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001729 r10_bio->devs[r10_bio->read_slot].bio =
1730 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 mirror = read_balance(conf, r10_bio);
1732 if (mirror == -1) {
NeilBrown128595e2010-05-03 14:47:14 +10001733 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 " read error for block %llu\n",
NeilBrown128595e2010-05-03 14:47:14 +10001735 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 bdevname(bio->bi_bdev,b),
1737 (unsigned long long)r10_bio->sector);
1738 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001739 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 } else {
NeilBrown2c7d46e2010-08-18 16:16:05 +10001741 const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
Maik Hampel14e71342007-07-31 00:37:57 -07001742 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 rdev = conf->mirrors[mirror].rdev;
1744 if (printk_ratelimit())
NeilBrown128595e2010-05-03 14:47:14 +10001745 printk(KERN_ERR "md/raid10:%s: %s: redirecting sector %llu to"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 " another mirror\n",
NeilBrown128595e2010-05-03 14:47:14 +10001747 mdname(mddev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 bdevname(rdev->bdev,b),
1749 (unsigned long long)r10_bio->sector);
NeilBrowna167f662010-10-26 18:31:13 +11001750 bio = bio_clone_mddev(r10_bio->master_bio,
1751 GFP_NOIO, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 r10_bio->devs[r10_bio->read_slot].bio = bio;
1753 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1754 + rdev->data_offset;
1755 bio->bi_bdev = rdev->bdev;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001756 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 bio->bi_private = r10_bio;
1758 bio->bi_end_io = raid10_end_read_request;
1759 unplug = 1;
1760 generic_make_request(bio);
1761 }
1762 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001763 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (unplug)
1766 unplug_slaves(mddev);
1767}
1768
1769
1770static int init_resync(conf_t *conf)
1771{
1772 int buffs;
1773
1774 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001775 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1777 if (!conf->r10buf_pool)
1778 return -ENOMEM;
1779 conf->next_resync = 0;
1780 return 0;
1781}
1782
1783/*
1784 * perform a "sync" on one "block"
1785 *
1786 * We need to make sure that no normal I/O request - particularly write
1787 * requests - conflict with active sync requests.
1788 *
1789 * This is achieved by tracking pending requests and a 'barrier' concept
1790 * that can be installed to exclude normal IO requests.
1791 *
1792 * Resync and recovery are handled very differently.
1793 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1794 *
1795 * For resync, we iterate over virtual addresses, read all copies,
1796 * and update if there are differences. If only one copy is live,
1797 * skip it.
1798 * For recovery, we iterate over physical addresses, read a good
1799 * value for each non-in_sync drive, and over-write.
1800 *
1801 * So, for recovery we may have several outstanding complex requests for a
1802 * given address, one for each out-of-sync device. We model this by allocating
1803 * a number of r10_bio structures, one for each out-of-sync device.
1804 * As we setup these structures, we collect all bio's together into a list
1805 * which we then process collectively to add pages, and then process again
1806 * to pass to generic_make_request.
1807 *
1808 * The r10_bio structures are linked using a borrowed master_bio pointer.
1809 * This link is counted in ->remaining. When the r10_bio that points to NULL
1810 * has its remaining count decremented to 0, the whole complex operation
1811 * is complete.
1812 *
1813 */
1814
NeilBrown57afd892005-06-21 17:17:13 -07001815static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
NeilBrown070ec552009-06-16 16:54:21 +10001817 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 r10bio_t *r10_bio;
1819 struct bio *biolist = NULL, *bio;
1820 sector_t max_sector, nr_sectors;
1821 int disk;
1822 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001823 int max_sync;
NeilBrown57dab0b2010-10-19 10:03:39 +11001824 sector_t sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 sector_t sectors_skipped = 0;
1827 int chunks_skipped = 0;
1828
1829 if (!conf->r10buf_pool)
1830 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001834 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1836 max_sector = mddev->resync_max_sectors;
1837 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001838 /* If we aborted, we need to abort the
1839 * sync on the 'current' bitmap chucks (there can
1840 * be several when recovering multiple devices).
1841 * as we may have started syncing it but not finished.
1842 * We can find the current address in
1843 * mddev->curr_resync, but for recovery,
1844 * we need to convert that to several
1845 * virtual addresses.
1846 */
1847 if (mddev->curr_resync < max_sector) { /* aborted */
1848 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1849 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1850 &sync_blocks, 1);
1851 else for (i=0; i<conf->raid_disks; i++) {
1852 sector_t sect =
1853 raid10_find_virt(conf, mddev->curr_resync, i);
1854 bitmap_end_sync(mddev->bitmap, sect,
1855 &sync_blocks, 1);
1856 }
1857 } else /* completed sync */
1858 conf->fullsync = 0;
1859
1860 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001862 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return sectors_skipped;
1864 }
1865 if (chunks_skipped >= conf->raid_disks) {
1866 /* if there has been nothing to do on any drive,
1867 * then there is nothing to do at all..
1868 */
NeilBrown57afd892005-06-21 17:17:13 -07001869 *skipped = 1;
1870 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
NeilBrownc6207272008-02-06 01:39:52 -08001873 if (max_sector > mddev->resync_max)
1874 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* make sure whole request will fit in a chunk - if chunks
1877 * are meaningful
1878 */
1879 if (conf->near_copies < conf->raid_disks &&
1880 max_sector > (sector_nr | conf->chunk_mask))
1881 max_sector = (sector_nr | conf->chunk_mask) + 1;
1882 /*
1883 * If there is non-resync activity waiting for us then
1884 * put in a delay to throttle resync.
1885 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001886 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 /* Again, very different code for resync and recovery.
1890 * Both must result in an r10bio with a list of bios that
1891 * have bi_end_io, bi_sector, bi_bdev set,
1892 * and bi_private set to the r10bio.
1893 * For recovery, we may actually create several r10bios
1894 * with 2 bios in each, that correspond to the bios in the main one.
1895 * In this case, the subordinate r10bios link back through a
1896 * borrowed master_bio pointer, and the counter in the master
1897 * includes a ref from each subordinate.
1898 */
1899 /* First, we decide what to do and set ->bi_end_io
1900 * To end_sync_read if we want to read, and
1901 * end_sync_write if we will want to write.
1902 */
1903
NeilBrown6cce3b22006-01-06 00:20:16 -08001904 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1906 /* recovery... the complicated one */
NeilBrowna9f326e2009-09-23 18:06:41 +10001907 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 r10_bio = NULL;
1909
1910 for (i=0 ; i<conf->raid_disks; i++)
1911 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001912 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001913 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /* want to reconstruct this device */
1915 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001916 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1917 int must_sync;
1918 /* Unless we are doing a full sync, we only need
1919 * to recover the block if it is set in the bitmap
1920 */
1921 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1922 &sync_blocks, 1);
1923 if (sync_blocks < max_sync)
1924 max_sync = sync_blocks;
1925 if (!must_sync &&
1926 !conf->fullsync) {
1927 /* yep, skip the sync_blocks here, but don't assume
1928 * that there will never be anything to do here
1929 */
1930 chunks_skipped = -1;
1931 continue;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001935 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 atomic_set(&r10_bio->remaining, 0);
1937
1938 r10_bio->master_bio = (struct bio*)rb2;
1939 if (rb2)
1940 atomic_inc(&rb2->remaining);
1941 r10_bio->mddev = mddev;
1942 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001943 r10_bio->sector = sect;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001946
1947 /* Need to check if the array will still be
NeilBrown6cce3b22006-01-06 00:20:16 -08001948 * degraded
1949 */
NeilBrown18055562009-05-07 12:48:10 +10001950 for (j=0; j<conf->raid_disks; j++)
1951 if (conf->mirrors[j].rdev == NULL ||
1952 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001953 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001954 break;
1955 }
NeilBrown18055562009-05-07 12:48:10 +10001956
NeilBrown6cce3b22006-01-06 00:20:16 -08001957 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1958 &sync_blocks, still_degraded);
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 for (j=0; j<conf->copies;j++) {
1961 int d = r10_bio->devs[j].devnum;
1962 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001963 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /* This is where we read from */
1965 bio = r10_bio->devs[0].bio;
1966 bio->bi_next = biolist;
1967 biolist = bio;
1968 bio->bi_private = r10_bio;
1969 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001970 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 bio->bi_sector = r10_bio->devs[j].addr +
1972 conf->mirrors[d].rdev->data_offset;
1973 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1974 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1975 atomic_inc(&r10_bio->remaining);
1976 /* and we write to 'i' */
1977
1978 for (k=0; k<conf->copies; k++)
1979 if (r10_bio->devs[k].devnum == i)
1980 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001981 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 bio = r10_bio->devs[1].bio;
1983 bio->bi_next = biolist;
1984 biolist = bio;
1985 bio->bi_private = r10_bio;
1986 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001987 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 bio->bi_sector = r10_bio->devs[k].addr +
1989 conf->mirrors[i].rdev->data_offset;
1990 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1991
1992 r10_bio->devs[0].devnum = d;
1993 r10_bio->devs[1].devnum = i;
1994
1995 break;
1996 }
1997 }
1998 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001999 /* Cannot recover, so abort the recovery */
2000 put_buf(r10_bio);
K.Tanakaa07e6ab2008-03-04 14:29:37 -08002001 if (rb2)
2002 atomic_dec(&rb2->remaining);
NeilBrown87fc7672005-09-09 16:24:04 -07002003 r10_bio = rb2;
NeilBrowndfc70642008-05-23 13:04:39 -07002004 if (!test_and_set_bit(MD_RECOVERY_INTR,
2005 &mddev->recovery))
NeilBrown128595e2010-05-03 14:47:14 +10002006 printk(KERN_INFO "md/raid10:%s: insufficient "
2007 "working devices for recovery.\n",
NeilBrown87fc7672005-09-09 16:24:04 -07002008 mdname(mddev));
2009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011 }
2012 if (biolist == NULL) {
2013 while (r10_bio) {
2014 r10bio_t *rb2 = r10_bio;
2015 r10_bio = (r10bio_t*) rb2->master_bio;
2016 rb2->master_bio = NULL;
2017 put_buf(rb2);
2018 }
2019 goto giveup;
2020 }
2021 } else {
2022 /* resync. Schedule a read for every block at this virt offset */
2023 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002024
NeilBrown78200d42009-02-25 13:18:47 +11002025 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2026
NeilBrown6cce3b22006-01-06 00:20:16 -08002027 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2028 &sync_blocks, mddev->degraded) &&
2029 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2030 /* We can skip this block */
2031 *skipped = 1;
2032 return sync_blocks + sectors_skipped;
2033 }
2034 if (sync_blocks < max_sync)
2035 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 r10_bio->mddev = mddev;
2039 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08002040 raise_barrier(conf, 0);
2041 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 r10_bio->master_bio = NULL;
2044 r10_bio->sector = sector_nr;
2045 set_bit(R10BIO_IsSync, &r10_bio->state);
2046 raid10_find_phys(conf, r10_bio);
2047 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
2048
2049 for (i=0; i<conf->copies; i++) {
2050 int d = r10_bio->devs[i].devnum;
2051 bio = r10_bio->devs[i].bio;
2052 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002053 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08002055 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 continue;
2057 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2058 atomic_inc(&r10_bio->remaining);
2059 bio->bi_next = biolist;
2060 biolist = bio;
2061 bio->bi_private = r10_bio;
2062 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08002063 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 bio->bi_sector = r10_bio->devs[i].addr +
2065 conf->mirrors[d].rdev->data_offset;
2066 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2067 count++;
2068 }
2069
2070 if (count < 2) {
2071 for (i=0; i<conf->copies; i++) {
2072 int d = r10_bio->devs[i].devnum;
2073 if (r10_bio->devs[i].bio->bi_end_io)
2074 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
2075 }
2076 put_buf(r10_bio);
2077 biolist = NULL;
2078 goto giveup;
2079 }
2080 }
2081
2082 for (bio = biolist; bio ; bio=bio->bi_next) {
2083
2084 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2085 if (bio->bi_end_io)
2086 bio->bi_flags |= 1 << BIO_UPTODATE;
2087 bio->bi_vcnt = 0;
2088 bio->bi_idx = 0;
2089 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 bio->bi_size = 0;
2091 }
2092
2093 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08002094 if (sector_nr + max_sync < max_sector)
2095 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 do {
2097 struct page *page;
2098 int len = PAGE_SIZE;
2099 disk = 0;
2100 if (sector_nr + (len>>9) > max_sector)
2101 len = (max_sector - sector_nr) << 9;
2102 if (len == 0)
2103 break;
2104 for (bio= biolist ; bio ; bio=bio->bi_next) {
2105 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2106 if (bio_add_page(bio, page, len, 0) == 0) {
2107 /* stop here */
2108 struct bio *bio2;
2109 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2110 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
2111 /* remove last page from this bio */
2112 bio2->bi_vcnt--;
2113 bio2->bi_size -= len;
2114 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2115 }
2116 goto bio_full;
2117 }
2118 disk = i;
2119 }
2120 nr_sectors += len>>9;
2121 sector_nr += len>>9;
2122 } while (biolist->bi_vcnt < RESYNC_PAGES);
2123 bio_full:
2124 r10_bio->sectors = nr_sectors;
2125
2126 while (biolist) {
2127 bio = biolist;
2128 biolist = biolist->bi_next;
2129
2130 bio->bi_next = NULL;
2131 r10_bio = bio->bi_private;
2132 r10_bio->sectors = nr_sectors;
2133
2134 if (bio->bi_end_io == end_sync_read) {
2135 md_sync_acct(bio->bi_bdev, nr_sectors);
2136 generic_make_request(bio);
2137 }
2138 }
2139
NeilBrown57afd892005-06-21 17:17:13 -07002140 if (sectors_skipped)
2141 /* pretend they weren't skipped, it makes
2142 * no important difference in this case
2143 */
2144 md_done_sync(mddev, sectors_skipped, 1);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return sectors_skipped + nr_sectors;
2147 giveup:
2148 /* There is nowhere to write, so all non-sync
2149 * drives must be failed, so try the next chunk...
2150 */
NeilBrown09b40682009-02-25 13:18:47 +11002151 if (sector_nr + max_sync < max_sector)
2152 max_sector = sector_nr + max_sync;
2153
2154 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 chunks_skipped ++;
2156 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Dan Williams80c3a6c2009-03-17 18:10:40 -07002160static sector_t
2161raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2162{
2163 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002164 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002165
2166 if (!raid_disks)
NeilBrown84707f32010-03-16 17:23:35 +11002167 raid_disks = conf->raid_disks;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002168 if (!sectors)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002169 sectors = conf->dev_sectors;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002170
2171 size = sectors >> conf->chunk_shift;
2172 sector_div(size, conf->far_copies);
2173 size = size * raid_disks;
2174 sector_div(size, conf->near_copies);
2175
2176 return size << conf->chunk_shift;
2177}
2178
Trela, Maciejdab8b292010-03-08 16:02:45 +11002179
2180static conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Trela, Maciejdab8b292010-03-08 16:02:45 +11002182 conf_t *conf = NULL;
NeilBrownc93983b2006-06-26 00:27:41 -07002183 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 sector_t stride, size;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002185 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Maciej Trelaf73ea872010-06-16 11:46:29 +01002187 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2188 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown128595e2010-05-03 14:47:14 +10002189 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2190 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2191 mdname(mddev), PAGE_SIZE);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
NeilBrown2604b702006-01-06 00:20:36 -08002194
Maciej Trelaf73ea872010-06-16 11:46:29 +01002195 nc = mddev->new_layout & 255;
2196 fc = (mddev->new_layout >> 8) & 255;
2197 fo = mddev->new_layout & (1<<16);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
Maciej Trelaf73ea872010-06-16 11:46:29 +01002200 (mddev->new_layout >> 17)) {
NeilBrown128595e2010-05-03 14:47:14 +10002201 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
Maciej Trelaf73ea872010-06-16 11:46:29 +01002202 mdname(mddev), mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto out;
2204 }
Trela, Maciejdab8b292010-03-08 16:02:45 +11002205
2206 err = -ENOMEM;
NeilBrown4443ae12006-01-06 00:20:28 -08002207 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002208 if (!conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto out;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002210
NeilBrown4443ae12006-01-06 00:20:28 -08002211 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002212 GFP_KERNEL);
2213 if (!conf->mirrors)
2214 goto out;
NeilBrown4443ae12006-01-06 00:20:28 -08002215
2216 conf->tmppage = alloc_page(GFP_KERNEL);
2217 if (!conf->tmppage)
Trela, Maciejdab8b292010-03-08 16:02:45 +11002218 goto out;
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
NeilBrown64a742b2007-02-28 20:11:18 -08002221 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 conf->near_copies = nc;
2223 conf->far_copies = fc;
2224 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002225 conf->far_offset = fo;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002226 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2227 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2228
2229 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2230 r10bio_pool_free, conf);
2231 if (!conf->r10bio_pool)
2232 goto out;
2233
Andre Noll58c0fed2009-03-31 14:33:13 +11002234 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002235 sector_div(size, fc);
2236 size = size * conf->raid_disks;
2237 sector_div(size, nc);
2238 /* 'size' is now the number of chunks in the array */
2239 /* calculate "used chunks per device" in 'stride' */
2240 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002241
2242 /* We need to round up when dividing by raid_disks to
2243 * get the stride size.
2244 */
2245 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002246 sector_div(stride, conf->raid_disks);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002247
2248 conf->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002249
NeilBrownc93983b2006-06-26 00:27:41 -07002250 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002251 stride = 1;
2252 else
NeilBrownc93983b2006-06-26 00:27:41 -07002253 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002254 conf->stride = stride << conf->chunk_shift;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Neil Browne7e72bf2008-05-14 16:05:54 -07002257 spin_lock_init(&conf->device_lock);
Trela, Maciejdab8b292010-03-08 16:02:45 +11002258 INIT_LIST_HEAD(&conf->retry_list);
2259
2260 spin_lock_init(&conf->resync_lock);
2261 init_waitqueue_head(&conf->wait_barrier);
2262
2263 conf->thread = md_register_thread(raid10d, mddev, NULL);
2264 if (!conf->thread)
2265 goto out;
2266
Trela, Maciejdab8b292010-03-08 16:02:45 +11002267 conf->mddev = mddev;
2268 return conf;
2269
2270 out:
NeilBrown128595e2010-05-03 14:47:14 +10002271 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
Trela, Maciejdab8b292010-03-08 16:02:45 +11002272 mdname(mddev));
2273 if (conf) {
2274 if (conf->r10bio_pool)
2275 mempool_destroy(conf->r10bio_pool);
2276 kfree(conf->mirrors);
2277 safe_put_page(conf->tmppage);
2278 kfree(conf);
2279 }
2280 return ERR_PTR(err);
2281}
2282
2283static int run(mddev_t *mddev)
2284{
2285 conf_t *conf;
2286 int i, disk_idx, chunk_size;
2287 mirror_info_t *disk;
2288 mdk_rdev_t *rdev;
2289 sector_t size;
2290
2291 /*
2292 * copy the already verified devices into our private RAID10
2293 * bookkeeping area. [whatever we allocate in run(),
2294 * should be freed in stop()]
2295 */
2296
2297 if (mddev->private == NULL) {
2298 conf = setup_conf(mddev);
2299 if (IS_ERR(conf))
2300 return PTR_ERR(conf);
2301 mddev->private = conf;
2302 }
2303 conf = mddev->private;
2304 if (!conf)
2305 goto out;
2306
Neil Browne7e72bf2008-05-14 16:05:54 -07002307 mddev->queue->queue_lock = &conf->device_lock;
2308
Trela, Maciejdab8b292010-03-08 16:02:45 +11002309 mddev->thread = conf->thread;
2310 conf->thread = NULL;
2311
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002312 chunk_size = mddev->chunk_sectors << 9;
2313 blk_queue_io_min(mddev->queue, chunk_size);
2314 if (conf->raid_disks % conf->near_copies)
2315 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2316 else
2317 blk_queue_io_opt(mddev->queue, chunk_size *
2318 (conf->raid_disks / conf->near_copies));
2319
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002320 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 disk_idx = rdev->raid_disk;
NeilBrown84707f32010-03-16 17:23:35 +11002322 if (disk_idx >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 || disk_idx < 0)
2324 continue;
2325 disk = conf->mirrors + disk_idx;
2326
2327 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002328 disk_stack_limits(mddev->gendisk, rdev->bdev,
2329 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /* as we don't honour merge_bvec_fn, we must never risk
NeilBrown627a2d32010-03-08 16:44:38 +11002331 * violating it, so limit max_segments to 1 lying
2332 * within a single page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 */
NeilBrown627a2d32010-03-08 16:44:38 +11002334 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2335 blk_queue_max_segments(mddev->queue, 1);
2336 blk_queue_segment_boundary(mddev->queue,
2337 PAGE_CACHE_SIZE - 1);
2338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
NeilBrown6d508242005-09-09 16:24:03 -07002342 /* need to check that every block has at least one working mirror */
2343 if (!enough(conf)) {
NeilBrown128595e2010-05-03 14:47:14 +10002344 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
NeilBrown6d508242005-09-09 16:24:03 -07002345 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 goto out_free_conf;
2347 }
2348
2349 mddev->degraded = 0;
2350 for (i = 0; i < conf->raid_disks; i++) {
2351
2352 disk = conf->mirrors + i;
2353
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002354 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002355 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 disk->head_position = 0;
2357 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002358 if (disk->rdev)
2359 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 }
2362
Andre Noll8c6ac862009-06-18 08:48:06 +10002363 if (mddev->recovery_cp != MaxSector)
NeilBrown128595e2010-05-03 14:47:14 +10002364 printk(KERN_NOTICE "md/raid10:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10002365 " -- starting background reconstruction\n",
2366 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 printk(KERN_INFO
NeilBrown128595e2010-05-03 14:47:14 +10002368 "md/raid10:%s: active with %d out of %d devices\n",
NeilBrown84707f32010-03-16 17:23:35 +11002369 mdname(mddev), conf->raid_disks - mddev->degraded,
2370 conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /*
2372 * Ok, everything is just fine now
2373 */
Trela, Maciejdab8b292010-03-08 16:02:45 +11002374 mddev->dev_sectors = conf->dev_sectors;
2375 size = raid10_size(mddev, 0, 0);
2376 md_set_array_sectors(mddev, size);
2377 mddev->resync_max_sectors = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
NeilBrown7a5febe2005-05-16 21:53:16 -07002379 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002380 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2381 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /* Calculate max read-ahead size.
2384 * We need to readahead at least twice a whole stripe....
2385 * maybe...
2386 */
2387 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002388 int stripe = conf->raid_disks *
2389 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 stripe /= conf->near_copies;
2391 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2392 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2393 }
2394
NeilBrown84707f32010-03-16 17:23:35 +11002395 if (conf->near_copies < conf->raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Andre Nollac5e7112009-08-03 10:59:47 +10002397 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return 0;
2399
2400out_free_conf:
NeilBrown589a5942010-12-09 17:02:14 +11002401 md_unregister_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (conf->r10bio_pool)
2403 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002404 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002405 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 kfree(conf);
2407 mddev->private = NULL;
2408out:
2409 return -EIO;
2410}
2411
2412static int stop(mddev_t *mddev)
2413{
NeilBrown070ec552009-06-16 16:54:21 +10002414 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
NeilBrown409c57f2009-03-31 14:39:39 +11002416 raise_barrier(conf, 0);
2417 lower_barrier(conf);
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 md_unregister_thread(mddev->thread);
2420 mddev->thread = NULL;
2421 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2422 if (conf->r10bio_pool)
2423 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002424 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 kfree(conf);
2426 mddev->private = NULL;
2427 return 0;
2428}
2429
NeilBrown6cce3b22006-01-06 00:20:16 -08002430static void raid10_quiesce(mddev_t *mddev, int state)
2431{
NeilBrown070ec552009-06-16 16:54:21 +10002432 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002433
2434 switch(state) {
2435 case 1:
2436 raise_barrier(conf, 0);
2437 break;
2438 case 0:
2439 lower_barrier(conf);
2440 break;
2441 }
NeilBrown6cce3b22006-01-06 00:20:16 -08002442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Trela, Maciejdab8b292010-03-08 16:02:45 +11002444static void *raid10_takeover_raid0(mddev_t *mddev)
2445{
2446 mdk_rdev_t *rdev;
2447 conf_t *conf;
2448
2449 if (mddev->degraded > 0) {
NeilBrown128595e2010-05-03 14:47:14 +10002450 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2451 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002452 return ERR_PTR(-EINVAL);
2453 }
2454
Trela, Maciejdab8b292010-03-08 16:02:45 +11002455 /* Set new parameters */
2456 mddev->new_level = 10;
2457 /* new layout: far_copies = 1, near_copies = 2 */
2458 mddev->new_layout = (1<<8) + 2;
2459 mddev->new_chunk_sectors = mddev->chunk_sectors;
2460 mddev->delta_disks = mddev->raid_disks;
Trela, Maciejdab8b292010-03-08 16:02:45 +11002461 mddev->raid_disks *= 2;
2462 /* make sure it will be not marked as dirty */
2463 mddev->recovery_cp = MaxSector;
2464
2465 conf = setup_conf(mddev);
NeilBrowne93f68a2010-06-15 09:36:03 +01002466 if (!IS_ERR(conf))
2467 list_for_each_entry(rdev, &mddev->disks, same_set)
2468 if (rdev->raid_disk >= 0)
2469 rdev->new_raid_disk = rdev->raid_disk * 2;
2470
Trela, Maciejdab8b292010-03-08 16:02:45 +11002471 return conf;
2472}
2473
2474static void *raid10_takeover(mddev_t *mddev)
2475{
2476 struct raid0_private_data *raid0_priv;
2477
2478 /* raid10 can take over:
2479 * raid0 - providing it has only two drives
2480 */
2481 if (mddev->level == 0) {
2482 /* for raid0 takeover only one zone is supported */
2483 raid0_priv = mddev->private;
2484 if (raid0_priv->nr_strip_zones > 1) {
NeilBrown128595e2010-05-03 14:47:14 +10002485 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2486 " with more than one zone.\n",
2487 mdname(mddev));
Trela, Maciejdab8b292010-03-08 16:02:45 +11002488 return ERR_PTR(-EINVAL);
2489 }
2490 return raid10_takeover_raid0(mddev);
2491 }
2492 return ERR_PTR(-EINVAL);
2493}
2494
NeilBrown2604b702006-01-06 00:20:36 -08002495static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002498 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 .owner = THIS_MODULE,
2500 .make_request = make_request,
2501 .run = run,
2502 .stop = stop,
2503 .status = status,
2504 .error_handler = error,
2505 .hot_add_disk = raid10_add_disk,
2506 .hot_remove_disk= raid10_remove_disk,
2507 .spare_active = raid10_spare_active,
2508 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002509 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002510 .size = raid10_size,
Trela, Maciejdab8b292010-03-08 16:02:45 +11002511 .takeover = raid10_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512};
2513
2514static int __init raid_init(void)
2515{
NeilBrown2604b702006-01-06 00:20:36 -08002516 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519static void raid_exit(void)
2520{
NeilBrown2604b702006-01-06 00:20:36 -08002521 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
2524module_init(raid_init);
2525module_exit(raid_exit);
2526MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11002527MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002529MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002530MODULE_ALIAS("md-level-10");