blob: 3d9020cf6f6e4482c857644f721366fb8102f3d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
Stephen Rothwell25570722008-10-15 09:09:21 +110021#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110022#include <linux/blkdev.h>
NeilBrownbff61972009-03-31 14:33:13 +110023#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110024#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110025#include "raid10.h"
26#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * RAID10 provides a combination of RAID0 and RAID1 functionality.
30 * The layout of data is defined by
31 * chunk_size
32 * raid_disks
33 * near_copies (stored in low byte of layout)
34 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070035 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 *
37 * The data to be stored is divided into chunks using chunksize.
38 * Each device is divided into far_copies sections.
39 * In each section, chunks are laid out in a style similar to raid0, but
40 * near_copies copies of each chunk is stored (each on a different drive).
41 * The starting device for each section is offset near_copies from the starting
42 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070043 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * drive.
45 * near_copies and far_copies must be at least one, and their product is at most
46 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070047 *
48 * If far_offset is true, then the far_copies are handled a bit differently.
49 * The copies are still in different stripes, but instead of be very far apart
50 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
53/*
54 * Number of guaranteed r10bios in case of extreme VM load:
55 */
56#define NR_RAID10_BIOS 256
57
58static void unplug_slaves(mddev_t *mddev);
59
NeilBrown0a27ec92006-01-06 00:20:13 -080060static void allow_barrier(conf_t *conf);
61static void lower_barrier(conf_t *conf);
62
Al Virodd0fc662005-10-07 07:46:04 +010063static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 conf_t *conf = data;
66 r10bio_t *r10_bio;
67 int size = offsetof(struct r10bio_s, devs[conf->copies]);
68
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
NeilBrown9ffae0c2006-01-06 00:20:32 -080070 r10_bio = kzalloc(size, gfp_flags);
71 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unplug_slaves(conf->mddev);
73
74 return r10_bio;
75}
76
77static void r10bio_pool_free(void *r10_bio, void *data)
78{
79 kfree(r10_bio);
80}
81
NeilBrown0310fa22008-08-05 15:54:14 +100082/* Maximum size of each resync request */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define RESYNC_BLOCK_SIZE (64*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
NeilBrown0310fa22008-08-05 15:54:14 +100085/* amount of memory to reserve for resync requests */
86#define RESYNC_WINDOW (1024*1024)
87/* maximum number of concurrent requests, memory permitting */
88#define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * When performing a resync, we need to read and compare, so
92 * we need as many pages are there are copies.
93 * When performing a recovery, we need 2 bios, one for read,
94 * one for write (we recover only one drive per r10buf)
95 *
96 */
Al Virodd0fc662005-10-07 07:46:04 +010097static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 conf_t *conf = data;
100 struct page *page;
101 r10bio_t *r10_bio;
102 struct bio *bio;
103 int i, j;
104 int nalloc;
105
106 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
107 if (!r10_bio) {
108 unplug_slaves(conf->mddev);
109 return NULL;
110 }
111
112 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
113 nalloc = conf->copies; /* resync */
114 else
115 nalloc = 2; /* recovery */
116
117 /*
118 * Allocate bios.
119 */
120 for (j = nalloc ; j-- ; ) {
121 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
122 if (!bio)
123 goto out_free_bio;
124 r10_bio->devs[j].bio = bio;
125 }
126 /*
127 * Allocate RESYNC_PAGES data pages and attach them
128 * where needed.
129 */
130 for (j = 0 ; j < nalloc; j++) {
131 bio = r10_bio->devs[j].bio;
132 for (i = 0; i < RESYNC_PAGES; i++) {
133 page = alloc_page(gfp_flags);
134 if (unlikely(!page))
135 goto out_free_pages;
136
137 bio->bi_io_vec[i].bv_page = page;
138 }
139 }
140
141 return r10_bio;
142
143out_free_pages:
144 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800145 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 while (j--)
147 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800148 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 j = -1;
150out_free_bio:
151 while ( ++j < nalloc )
152 bio_put(r10_bio->devs[j].bio);
153 r10bio_pool_free(r10_bio, conf);
154 return NULL;
155}
156
157static void r10buf_pool_free(void *__r10_bio, void *data)
158{
159 int i;
160 conf_t *conf = data;
161 r10bio_t *r10bio = __r10_bio;
162 int j;
163
164 for (j=0; j < conf->copies; j++) {
165 struct bio *bio = r10bio->devs[j].bio;
166 if (bio) {
167 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800168 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 bio->bi_io_vec[i].bv_page = NULL;
170 }
171 bio_put(bio);
172 }
173 }
174 r10bio_pool_free(r10bio, conf);
175}
176
177static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
178{
179 int i;
180
181 for (i = 0; i < conf->copies; i++) {
182 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -0800183 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 bio_put(*bio);
185 *bio = NULL;
186 }
187}
188
Arjan van de Ven858119e2006-01-14 13:20:43 -0800189static void free_r10bio(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
NeilBrown070ec552009-06-16 16:54:21 +1000191 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /*
194 * Wake up any possible resync thread that waits for the device
195 * to go idle.
196 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800197 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 put_all_bios(conf, r10_bio);
200 mempool_free(r10_bio, conf->r10bio_pool);
201}
202
Arjan van de Ven858119e2006-01-14 13:20:43 -0800203static void put_buf(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
NeilBrown070ec552009-06-16 16:54:21 +1000205 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 mempool_free(r10_bio, conf->r10buf_pool);
208
NeilBrown0a27ec92006-01-06 00:20:13 -0800209 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static void reschedule_retry(r10bio_t *r10_bio)
213{
214 unsigned long flags;
215 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +1000216 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 spin_lock_irqsave(&conf->device_lock, flags);
219 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800220 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 spin_unlock_irqrestore(&conf->device_lock, flags);
222
Arthur Jones388667b2008-07-25 12:03:38 -0700223 /* wake up frozen array... */
224 wake_up(&conf->wait_barrier);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 md_wakeup_thread(mddev->thread);
227}
228
229/*
230 * raid_end_bio_io() is called when we have finished servicing a mirrored
231 * operation and are ready to return a success/failure code to the buffer
232 * cache layer.
233 */
234static void raid_end_bio_io(r10bio_t *r10_bio)
235{
236 struct bio *bio = r10_bio->master_bio;
237
NeilBrown6712ecf2007-09-27 12:47:43 +0200238 bio_endio(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
240 free_r10bio(r10_bio);
241}
242
243/*
244 * Update disk head position estimator based on IRQ completion info.
245 */
246static inline void update_head_pos(int slot, r10bio_t *r10_bio)
247{
NeilBrown070ec552009-06-16 16:54:21 +1000248 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
251 r10_bio->devs[slot].addr + (r10_bio->sectors);
252}
253
NeilBrown6712ecf2007-09-27 12:47:43 +0200254static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
257 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
258 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000259 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 slot = r10_bio->read_slot;
263 dev = r10_bio->devs[slot].devnum;
264 /*
265 * this branch is our 'one mirror IO has finished' event handler:
266 */
NeilBrown4443ae12006-01-06 00:20:28 -0800267 update_head_pos(slot, r10_bio);
268
269 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /*
271 * Set R10BIO_Uptodate in our master bio, so that
272 * we will return a good error code to the higher
273 * levels even if IO on some other mirrored buffer fails.
274 *
275 * The 'master' represents the composite IO operation to
276 * user-side. So if something waits for IO, then it will
277 * wait for the 'master' bio.
278 */
279 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 raid_end_bio_io(r10_bio);
NeilBrown4443ae12006-01-06 00:20:28 -0800281 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /*
283 * oops, read error:
284 */
285 char b[BDEVNAME_SIZE];
286 if (printk_ratelimit())
287 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
288 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
289 reschedule_retry(r10_bio);
290 }
291
292 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
NeilBrown6712ecf2007-09-27 12:47:43 +0200295static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
298 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
299 int slot, dev;
NeilBrown070ec552009-06-16 16:54:21 +1000300 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 for (slot = 0; slot < conf->copies; slot++)
303 if (r10_bio->devs[slot].bio == bio)
304 break;
305 dev = r10_bio->devs[slot].devnum;
306
307 /*
308 * this branch is our 'one mirror IO has finished' event handler:
309 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800310 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800312 /* an I/O failed, we can't clear the bitmap */
313 set_bit(R10BIO_Degraded, &r10_bio->state);
314 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * Set R10BIO_Uptodate in our master bio, so that
317 * we will return a good error code for to the higher
318 * levels even if IO on some other mirrored buffer fails.
319 *
320 * The 'master' represents the composite IO operation to
321 * user-side. So if something waits for IO, then it will
322 * wait for the 'master' bio.
323 */
324 set_bit(R10BIO_Uptodate, &r10_bio->state);
325
326 update_head_pos(slot, r10_bio);
327
328 /*
329 *
330 * Let's see if all mirrored write operations have finished
331 * already.
332 */
333 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800334 /* clear the bitmap if all writes complete successfully */
335 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
336 r10_bio->sectors,
337 !test_bit(R10BIO_Degraded, &r10_bio->state),
338 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 md_write_end(r10_bio->mddev);
340 raid_end_bio_io(r10_bio);
341 }
342
343 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346
347/*
348 * RAID10 layout manager
349 * Aswell as the chunksize and raid_disks count, there are two
350 * parameters: near_copies and far_copies.
351 * near_copies * far_copies must be <= raid_disks.
352 * Normally one of these will be 1.
353 * If both are 1, we get raid0.
354 * If near_copies == raid_disks, we get raid1.
355 *
356 * Chunks are layed out in raid0 style with near_copies copies of the
357 * first chunk, followed by near_copies copies of the next chunk and
358 * so on.
359 * If far_copies > 1, then after 1/far_copies of the array has been assigned
360 * as described above, we start again with a device offset of near_copies.
361 * So we effectively have another copy of the whole array further down all
362 * the drives, but with blocks on different drives.
363 * With this layout, and block is never stored twice on the one device.
364 *
365 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700366 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 *
368 * raid10_find_virt does the reverse mapping, from a device and a
369 * sector offset to a virtual address
370 */
371
372static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
373{
374 int n,f;
375 sector_t sector;
376 sector_t chunk;
377 sector_t stripe;
378 int dev;
379
380 int slot = 0;
381
382 /* now calculate first sector/dev */
383 chunk = r10bio->sector >> conf->chunk_shift;
384 sector = r10bio->sector & conf->chunk_mask;
385
386 chunk *= conf->near_copies;
387 stripe = chunk;
388 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700389 if (conf->far_offset)
390 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 sector += stripe << conf->chunk_shift;
393
394 /* and calculate all the others */
395 for (n=0; n < conf->near_copies; n++) {
396 int d = dev;
397 sector_t s = sector;
398 r10bio->devs[slot].addr = sector;
399 r10bio->devs[slot].devnum = d;
400 slot++;
401
402 for (f = 1; f < conf->far_copies; f++) {
403 d += conf->near_copies;
404 if (d >= conf->raid_disks)
405 d -= conf->raid_disks;
406 s += conf->stride;
407 r10bio->devs[slot].devnum = d;
408 r10bio->devs[slot].addr = s;
409 slot++;
410 }
411 dev++;
412 if (dev >= conf->raid_disks) {
413 dev = 0;
414 sector += (conf->chunk_mask + 1);
415 }
416 }
417 BUG_ON(slot != conf->copies);
418}
419
420static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
421{
422 sector_t offset, chunk, vchunk;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700425 if (conf->far_offset) {
426 int fc;
427 chunk = sector >> conf->chunk_shift;
428 fc = sector_div(chunk, conf->far_copies);
429 dev -= fc * conf->near_copies;
430 if (dev < 0)
431 dev += conf->raid_disks;
432 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800433 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700434 sector -= conf->stride;
435 if (dev < conf->near_copies)
436 dev += conf->raid_disks - conf->near_copies;
437 else
438 dev -= conf->near_copies;
439 }
440 chunk = sector >> conf->chunk_shift;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 vchunk = chunk * conf->raid_disks + dev;
443 sector_div(vchunk, conf->near_copies);
444 return (vchunk << conf->chunk_shift) + offset;
445}
446
447/**
448 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
449 * @q: request queue
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200450 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * @biovec: the request that could be merged to it.
452 *
453 * Return amount of bytes we can accept at this offset
454 * If near_copies == raid_disk, there are no striping issues,
455 * but in that case, the function isn't called at all.
456 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200457static int raid10_mergeable_bvec(struct request_queue *q,
458 struct bvec_merge_data *bvm,
459 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200462 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +1000464 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200465 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
468 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200469 if (max <= biovec->bv_len && bio_sectors == 0)
470 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 else
472 return max;
473}
474
475/*
476 * This routine returns the disk from which the requested read should
477 * be done. There is a per-array 'next expected sequential IO' sector
478 * number - if this matches on the next IO then we use the last disk.
479 * There is also a per-disk 'last know head position' sector that is
480 * maintained from IRQ contexts, both the normal and the resync IO
481 * completion handlers update this position correctly. If there is no
482 * perfect sequential match then we pick the disk whose head is closest.
483 *
484 * If there are 2 mirrors in the same 2 devices, performance degrades
485 * because position is mirror, not device based.
486 *
487 * The rdev for the device selected will have nr_pending incremented.
488 */
489
490/*
491 * FIXME: possibly should rethink readbalancing and do it differently
492 * depending on near_copies / far_copies geometry.
493 */
494static int read_balance(conf_t *conf, r10bio_t *r10_bio)
495{
496 const unsigned long this_sector = r10_bio->sector;
497 int disk, slot, nslot;
498 const int sectors = r10_bio->sectors;
499 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800500 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 raid10_find_phys(conf, r10_bio);
503 rcu_read_lock();
504 /*
505 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800506 * device if no resync is going on (recovery is ok), or below
507 * the resync window. We take the first readable disk when
508 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
510 if (conf->mddev->recovery_cp < MaxSector
511 && (this_sector + sectors >= conf->next_resync)) {
512 /* make sure that disk is operational */
513 slot = 0;
514 disk = r10_bio->devs[slot].devnum;
515
Suzanne Woodd6065f72005-11-08 21:39:27 -0800516 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800517 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800518 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 slot++;
520 if (slot == conf->copies) {
521 slot = 0;
522 disk = -1;
523 break;
524 }
525 disk = r10_bio->devs[slot].devnum;
526 }
527 goto rb_out;
528 }
529
530
531 /* make sure the disk is operational */
532 slot = 0;
533 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800534 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800535 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800536 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 slot ++;
538 if (slot == conf->copies) {
539 disk = -1;
540 goto rb_out;
541 }
542 disk = r10_bio->devs[slot].devnum;
543 }
544
545
NeilBrown3ec67ac2005-09-09 16:23:40 -0700546 current_distance = abs(r10_bio->devs[slot].addr -
547 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800549 /* Find the disk whose head is closest,
550 * or - for far > 1 - find the closest to partition beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 for (nslot = slot; nslot < conf->copies; nslot++) {
553 int ndisk = r10_bio->devs[nslot].devnum;
554
555
Suzanne Woodd6065f72005-11-08 21:39:27 -0800556 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800557 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800558 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 continue;
560
NeilBrown22dfdf52005-11-28 13:44:09 -0800561 /* This optimisation is debatable, and completely destroys
562 * sequential read speed for 'far copies' arrays. So only
563 * keep it for 'near' arrays, and review those later.
564 */
565 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 disk = ndisk;
567 slot = nslot;
568 break;
569 }
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800570
571 /* for far > 1 always use the lowest address */
572 if (conf->far_copies > 1)
573 new_distance = r10_bio->devs[nslot].addr;
574 else
575 new_distance = abs(r10_bio->devs[nslot].addr -
576 conf->mirrors[ndisk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (new_distance < current_distance) {
578 current_distance = new_distance;
579 disk = ndisk;
580 slot = nslot;
581 }
582 }
583
584rb_out:
585 r10_bio->read_slot = slot;
586/* conf->next_seq_sect = this_sector + sectors;*/
587
Suzanne Woodd6065f72005-11-08 21:39:27 -0800588 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800590 else
591 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 rcu_read_unlock();
593
594 return disk;
595}
596
597static void unplug_slaves(mddev_t *mddev)
598{
NeilBrown070ec552009-06-16 16:54:21 +1000599 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int i;
601
602 rcu_read_lock();
603 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800604 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800605 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200606 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 atomic_inc(&rdev->nr_pending);
609 rcu_read_unlock();
610
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500611 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 rdev_dec_pending(rdev, mddev);
614 rcu_read_lock();
615 }
616 }
617 rcu_read_unlock();
618}
619
Jens Axboe165125e2007-07-24 09:28:11 +0200620static void raid10_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
NeilBrown6cce3b22006-01-06 00:20:16 -0800622 mddev_t *mddev = q->queuedata;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 unplug_slaves(q->queuedata);
NeilBrown6cce3b22006-01-06 00:20:16 -0800625 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
NeilBrown0d129222006-10-03 01:15:54 -0700628static int raid10_congested(void *data, int bits)
629{
630 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +1000631 conf_t *conf = mddev->private;
NeilBrown0d129222006-10-03 01:15:54 -0700632 int i, ret = 0;
633
634 rcu_read_lock();
635 for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
636 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
637 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200638 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700639
640 ret |= bdi_congested(&q->backing_dev_info, bits);
641 }
642 }
643 rcu_read_unlock();
644 return ret;
645}
646
NeilBrowna35e63e2008-03-04 14:29:29 -0800647static int flush_pending_writes(conf_t *conf)
648{
649 /* Any writes that have been queued but are awaiting
650 * bitmap updates get flushed here.
651 * We return 1 if any requests were actually submitted.
652 */
653 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700654
NeilBrowna35e63e2008-03-04 14:29:29 -0800655 spin_lock_irq(&conf->device_lock);
656
657 if (conf->pending_bio_list.head) {
658 struct bio *bio;
659 bio = bio_list_get(&conf->pending_bio_list);
660 blk_remove_plug(conf->mddev->queue);
661 spin_unlock_irq(&conf->device_lock);
662 /* flush any pending bitmap writes to disk
663 * before proceeding w/ I/O */
664 bitmap_unplug(conf->mddev->bitmap);
665
666 while (bio) { /* submit pending writes */
667 struct bio *next = bio->bi_next;
668 bio->bi_next = NULL;
669 generic_make_request(bio);
670 bio = next;
671 }
672 rv = 1;
673 } else
674 spin_unlock_irq(&conf->device_lock);
675 return rv;
676}
NeilBrown0a27ec92006-01-06 00:20:13 -0800677/* Barriers....
678 * Sometimes we need to suspend IO while we do something else,
679 * either some resync/recovery, or reconfigure the array.
680 * To do this we raise a 'barrier'.
681 * The 'barrier' is a counter that can be raised multiple times
682 * to count how many activities are happening which preclude
683 * normal IO.
684 * We can only raise the barrier if there is no pending IO.
685 * i.e. if nr_pending == 0.
686 * We choose only to raise the barrier if no-one is waiting for the
687 * barrier to go down. This means that as soon as an IO request
688 * is ready, no other operations which require a barrier will start
689 * until the IO request has had a chance.
690 *
691 * So: regular IO calls 'wait_barrier'. When that returns there
692 * is no backgroup IO happening, It must arrange to call
693 * allow_barrier when it has finished its IO.
694 * backgroup IO calls must call raise_barrier. Once that returns
695 * there is no normal IO happeing. It must arrange to call
696 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
NeilBrown6cce3b22006-01-06 00:20:16 -0800699static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
NeilBrown6cce3b22006-01-06 00:20:16 -0800701 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
NeilBrown6cce3b22006-01-06 00:20:16 -0800704 /* Wait until no block IO is waiting (unless 'force') */
705 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800706 conf->resync_lock,
707 raid10_unplug(conf->mddev->queue));
708
709 /* block any new IO from starting */
710 conf->barrier++;
711
712 /* No wait for all pending IO to complete */
713 wait_event_lock_irq(conf->wait_barrier,
714 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
715 conf->resync_lock,
716 raid10_unplug(conf->mddev->queue));
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 spin_unlock_irq(&conf->resync_lock);
719}
720
NeilBrown0a27ec92006-01-06 00:20:13 -0800721static void lower_barrier(conf_t *conf)
722{
723 unsigned long flags;
724 spin_lock_irqsave(&conf->resync_lock, flags);
725 conf->barrier--;
726 spin_unlock_irqrestore(&conf->resync_lock, flags);
727 wake_up(&conf->wait_barrier);
728}
729
730static void wait_barrier(conf_t *conf)
731{
732 spin_lock_irq(&conf->resync_lock);
733 if (conf->barrier) {
734 conf->nr_waiting++;
735 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
736 conf->resync_lock,
737 raid10_unplug(conf->mddev->queue));
738 conf->nr_waiting--;
739 }
740 conf->nr_pending++;
741 spin_unlock_irq(&conf->resync_lock);
742}
743
744static void allow_barrier(conf_t *conf)
745{
746 unsigned long flags;
747 spin_lock_irqsave(&conf->resync_lock, flags);
748 conf->nr_pending--;
749 spin_unlock_irqrestore(&conf->resync_lock, flags);
750 wake_up(&conf->wait_barrier);
751}
752
NeilBrown4443ae12006-01-06 00:20:28 -0800753static void freeze_array(conf_t *conf)
754{
755 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800756 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800757 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800758 * wait until nr_pending match nr_queued+1
759 * This is called in the context of one normal IO request
760 * that has failed. Thus any sync request that might be pending
761 * will be blocked by nr_pending, and we need to wait for
762 * pending IO requests to complete or be queued for re-try.
763 * Thus the number queued (nr_queued) plus this request (1)
764 * must match the number of pending IOs (nr_pending) before
765 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800766 */
767 spin_lock_irq(&conf->resync_lock);
768 conf->barrier++;
769 conf->nr_waiting++;
770 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800771 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800772 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800773 ({ flush_pending_writes(conf);
774 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800775 spin_unlock_irq(&conf->resync_lock);
776}
777
778static void unfreeze_array(conf_t *conf)
779{
780 /* reverse the effect of the freeze */
781 spin_lock_irq(&conf->resync_lock);
782 conf->barrier--;
783 conf->nr_waiting--;
784 wake_up(&conf->wait_barrier);
785 spin_unlock_irq(&conf->resync_lock);
786}
787
Jens Axboe165125e2007-07-24 09:28:11 +0200788static int make_request(struct request_queue *q, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +1000791 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 mirror_info_t *mirror;
793 r10bio_t *r10_bio;
794 struct bio *read_bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900795 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int i;
797 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100798 const int rw = bio_data_dir(bio);
Lars Ellenberge3881a62007-01-10 23:15:37 -0800799 const int do_sync = bio_sync(bio);
NeilBrown6cce3b22006-01-06 00:20:16 -0800800 struct bio_list bl;
801 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700802 mdk_rdev_t *blocked_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
NeilBrowne5dcdd82005-09-09 16:23:41 -0700804 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200805 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700806 return 0;
807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /* If this request crosses a chunk boundary, we need to
810 * split it. This will only happen for 1 PAGE (or less) requests.
811 */
812 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
813 > chunk_sects &&
814 conf->near_copies < conf->raid_disks)) {
815 struct bio_pair *bp;
816 /* Sanity check -- queue functions should prevent this happening */
817 if (bio->bi_vcnt != 1 ||
818 bio->bi_idx != 0)
819 goto bad_map;
820 /* This is a one page bio that upper layers
821 * refuse to split for us, so we need to split it.
822 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200823 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
825 if (make_request(q, &bp->bio1))
826 generic_make_request(&bp->bio1);
827 if (make_request(q, &bp->bio2))
828 generic_make_request(&bp->bio2);
829
830 bio_pair_release(bp);
831 return 0;
832 bad_map:
833 printk("raid10_make_request bug: can't convert block across chunks"
834 " or bigger than %dk %llu %d\n", chunk_sects/2,
835 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
836
NeilBrown6712ecf2007-09-27 12:47:43 +0200837 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
839 }
840
NeilBrown3d310eb2005-06-21 17:17:26 -0700841 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /*
844 * Register the new request and wait if the reconstruction
845 * thread has put up a bar for new requests.
846 * Continue immediately if no resync is active currently.
847 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800848 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Tejun Heo074a7ac2008-08-25 19:56:14 +0900850 cpu = part_stat_lock();
851 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
852 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
853 bio_sectors(bio));
854 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
857
858 r10_bio->master_bio = bio;
859 r10_bio->sectors = bio->bi_size >> 9;
860
861 r10_bio->mddev = mddev;
862 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800863 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jens Axboea3623572005-11-01 09:26:16 +0100865 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /*
867 * read balancing logic:
868 */
869 int disk = read_balance(conf, r10_bio);
870 int slot = r10_bio->read_slot;
871 if (disk < 0) {
872 raid_end_bio_io(r10_bio);
873 return 0;
874 }
875 mirror = conf->mirrors + disk;
876
877 read_bio = bio_clone(bio, GFP_NOIO);
878
879 r10_bio->devs[slot].bio = read_bio;
880
881 read_bio->bi_sector = r10_bio->devs[slot].addr +
882 mirror->rdev->data_offset;
883 read_bio->bi_bdev = mirror->rdev->bdev;
884 read_bio->bi_end_io = raid10_end_read_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800885 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 read_bio->bi_private = r10_bio;
887
888 generic_make_request(read_bio);
889 return 0;
890 }
891
892 /*
893 * WRITE:
894 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700895 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * inc refcount on their rdev. Record them by setting
897 * bios[x] to bio
898 */
899 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700900 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700901 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 rcu_read_lock();
903 for (i = 0; i < conf->copies; i++) {
904 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800905 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700906 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
907 atomic_inc(&rdev->nr_pending);
908 blocked_rdev = rdev;
909 break;
910 }
911 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800912 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800914 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800916 set_bit(R10BIO_Degraded, &r10_bio->state);
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 rcu_read_unlock();
920
Dan Williams6bfe0b42008-04-30 00:52:32 -0700921 if (unlikely(blocked_rdev)) {
922 /* Have to wait for this device to get unblocked, then retry */
923 int j;
924 int d;
925
926 for (j = 0; j < i; j++)
927 if (r10_bio->devs[j].bio) {
928 d = r10_bio->devs[j].devnum;
929 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
930 }
931 allow_barrier(conf);
932 md_wait_for_blocked_rdev(blocked_rdev, mddev);
933 wait_barrier(conf);
934 goto retry_write;
935 }
936
NeilBrown6cce3b22006-01-06 00:20:16 -0800937 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700938
NeilBrown6cce3b22006-01-06 00:20:16 -0800939 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 for (i = 0; i < conf->copies; i++) {
941 struct bio *mbio;
942 int d = r10_bio->devs[i].devnum;
943 if (!r10_bio->devs[i].bio)
944 continue;
945
946 mbio = bio_clone(bio, GFP_NOIO);
947 r10_bio->devs[i].bio = mbio;
948
949 mbio->bi_sector = r10_bio->devs[i].addr+
950 conf->mirrors[d].rdev->data_offset;
951 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
952 mbio->bi_end_io = raid10_end_write_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800953 mbio->bi_rw = WRITE | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 mbio->bi_private = r10_bio;
955
956 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b22006-01-06 00:20:16 -0800957 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Arne Redlichf6f953a2007-07-31 00:37:57 -0700960 if (unlikely(!atomic_read(&r10_bio->remaining))) {
961 /* the array is dead */
962 md_write_end(mddev);
963 raid_end_bio_io(r10_bio);
964 return 0;
965 }
966
NeilBrown6cce3b22006-01-06 00:20:16 -0800967 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
968 spin_lock_irqsave(&conf->device_lock, flags);
969 bio_list_merge(&conf->pending_bio_list, &bl);
970 blk_plug_device(mddev->queue);
971 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
NeilBrowna35e63e2008-03-04 14:29:29 -0800973 /* In case raid10d snuck in to freeze_array */
974 wake_up(&conf->wait_barrier);
975
Lars Ellenberge3881a62007-01-10 23:15:37 -0800976 if (do_sync)
977 md_wakeup_thread(mddev->thread);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
982static void status(struct seq_file *seq, mddev_t *mddev)
983{
NeilBrown070ec552009-06-16 16:54:21 +1000984 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int i;
986
987 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +1000988 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (conf->near_copies > 1)
990 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700991 if (conf->far_copies > 1) {
992 if (conf->far_offset)
993 seq_printf(seq, " %d offset-copies", conf->far_copies);
994 else
995 seq_printf(seq, " %d far-copies", conf->far_copies);
996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700998 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 for (i = 0; i < conf->raid_disks; i++)
1000 seq_printf(seq, "%s",
1001 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001002 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 seq_printf(seq, "]");
1004}
1005
1006static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1007{
1008 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001009 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /*
1012 * If it is not operational, then we have already marked it as dead
1013 * else if it is the last working disks, ignore the error, let the
1014 * next level up know.
1015 * else mark the drive as failed
1016 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001017 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -07001018 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
1020 * Don't fail the drive, just return an IO error.
1021 * The test should really be more sophisticated than
1022 * "working_disks == 1", but it isn't critical, and
1023 * can wait until we do more sophisticated "is the drive
1024 * really dead" tests...
1025 */
1026 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001027 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1028 unsigned long flags;
1029 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001031 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
1033 * if recovery is running, make sure it aborts.
1034 */
NeilBrowndfc70642008-05-23 13:04:39 -07001035 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001037 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001038 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Nick Andrewd7a420c2008-04-28 02:15:55 -07001039 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n"
1040 "raid10: Operation continuing on %d devices.\n",
NeilBrown76186dd2006-10-03 01:15:48 -07001041 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static void print_conf(conf_t *conf)
1045{
1046 int i;
1047 mirror_info_t *tmp;
1048
1049 printk("RAID10 conf printout:\n");
1050 if (!conf) {
1051 printk("(!conf)\n");
1052 return;
1053 }
NeilBrown76186dd2006-10-03 01:15:48 -07001054 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 conf->raid_disks);
1056
1057 for (i = 0; i < conf->raid_disks; i++) {
1058 char b[BDEVNAME_SIZE];
1059 tmp = conf->mirrors + i;
1060 if (tmp->rdev)
1061 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001062 i, !test_bit(In_sync, &tmp->rdev->flags),
1063 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 bdevname(tmp->rdev->bdev,b));
1065 }
1066}
1067
1068static void close_sync(conf_t *conf)
1069{
NeilBrown0a27ec92006-01-06 00:20:13 -08001070 wait_barrier(conf);
1071 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 mempool_destroy(conf->r10buf_pool);
1074 conf->r10buf_pool = NULL;
1075}
1076
NeilBrown6d508242005-09-09 16:24:03 -07001077/* check if there are enough drives for
1078 * every block to appear on atleast one
1079 */
1080static int enough(conf_t *conf)
1081{
1082 int first = 0;
1083
1084 do {
1085 int n = conf->copies;
1086 int cnt = 0;
1087 while (n--) {
1088 if (conf->mirrors[first].rdev)
1089 cnt++;
1090 first = (first+1) % conf->raid_disks;
1091 }
1092 if (cnt == 0)
1093 return 0;
1094 } while (first != 0);
1095 return 1;
1096}
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098static int raid10_spare_active(mddev_t *mddev)
1099{
1100 int i;
1101 conf_t *conf = mddev->private;
1102 mirror_info_t *tmp;
1103
1104 /*
1105 * Find all non-in_sync disks within the RAID10 configuration
1106 * and mark them in_sync
1107 */
1108 for (i = 0; i < conf->raid_disks; i++) {
1109 tmp = conf->mirrors + i;
1110 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001111 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001112 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1113 unsigned long flags;
1114 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001116 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 }
1119
1120 print_conf(conf);
1121 return 0;
1122}
1123
1124
1125static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1126{
1127 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001128 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int mirror;
1130 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001131 int first = 0;
1132 int last = mddev->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (mddev->recovery_cp < MaxSector)
1135 /* only hot-add to in-sync arrays, as recovery is
1136 * very different from resync
1137 */
Neil Brown199050e2008-06-28 08:31:33 +10001138 return -EBUSY;
NeilBrown6d508242005-09-09 16:24:03 -07001139 if (!enough(conf))
Neil Brown199050e2008-06-28 08:31:33 +10001140 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
NeilBrowna53a6c82008-11-06 17:28:20 +11001142 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001143 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
NeilBrown6cce3b22006-01-06 00:20:16 -08001145 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10001146 rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001147 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1148 mirror = rdev->saved_raid_disk;
1149 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001150 mirror = first;
1151 for ( ; mirror <= last ; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if ( !(p=conf->mirrors+mirror)->rdev) {
1153
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001154 disk_stack_limits(mddev->gendisk, rdev->bdev,
1155 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* as we don't honour merge_bvec_fn, we must never risk
1157 * violating it, so limit ->max_sector to one PAGE, as
1158 * a one page request is never in violation.
1159 */
1160 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001161 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
1162 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 p->head_position = 0;
1165 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001166 err = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001167 if (rdev->saved_raid_disk != mirror)
1168 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001169 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 }
1172
Andre Nollac5e7112009-08-03 10:59:47 +10001173 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178static int raid10_remove_disk(mddev_t *mddev, int number)
1179{
1180 conf_t *conf = mddev->private;
1181 int err = 0;
1182 mdk_rdev_t *rdev;
1183 mirror_info_t *p = conf->mirrors+ number;
1184
1185 print_conf(conf);
1186 rdev = p->rdev;
1187 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001188 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 atomic_read(&rdev->nr_pending)) {
1190 err = -EBUSY;
1191 goto abort;
1192 }
NeilBrowndfc70642008-05-23 13:04:39 -07001193 /* Only remove faulty devices in recovery
1194 * is not possible.
1195 */
1196 if (!test_bit(Faulty, &rdev->flags) &&
1197 enough(conf)) {
1198 err = -EBUSY;
1199 goto abort;
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001202 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (atomic_read(&rdev->nr_pending)) {
1204 /* lost the race, try later */
1205 err = -EBUSY;
1206 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001207 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Andre Nollac5e7112009-08-03 10:59:47 +10001209 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211abort:
1212
1213 print_conf(conf);
1214 return err;
1215}
1216
1217
NeilBrown6712ecf2007-09-27 12:47:43 +02001218static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
NeilBrown070ec552009-06-16 16:54:21 +10001221 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int i,d;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 for (i=0; i<conf->copies; i++)
1225 if (r10_bio->devs[i].bio == bio)
1226 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001227 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 update_head_pos(i, r10_bio);
1229 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001230
1231 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1232 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001233 else {
1234 atomic_add(r10_bio->sectors,
1235 &conf->mirrors[d].rdev->corrected_errors);
1236 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1237 md_error(r10_bio->mddev,
1238 conf->mirrors[d].rdev);
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* for reconstruct, we always reschedule after a read.
1242 * for resync, only after all reads
1243 */
NeilBrown73d5c382009-02-25 13:18:47 +11001244 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1246 atomic_dec_and_test(&r10_bio->remaining)) {
1247 /* we have read all the blocks,
1248 * do the comparison in process context in raid10d
1249 */
1250 reschedule_retry(r10_bio);
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
NeilBrown6712ecf2007-09-27 12:47:43 +02001254static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1257 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1258 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001259 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 int i,d;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 for (i = 0; i < conf->copies; i++)
1263 if (r10_bio->devs[i].bio == bio)
1264 break;
1265 d = r10_bio->devs[i].devnum;
1266
1267 if (!uptodate)
1268 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 update_head_pos(i, r10_bio);
1271
NeilBrown73d5c382009-02-25 13:18:47 +11001272 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 while (atomic_dec_and_test(&r10_bio->remaining)) {
1274 if (r10_bio->master_bio == NULL) {
1275 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001276 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001278 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 } else {
1281 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1282 put_buf(r10_bio);
1283 r10_bio = r10_bio2;
1284 }
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288/*
1289 * Note: sync and recover and handled very differently for raid10
1290 * This code is for resync.
1291 * For resync, we read through virtual addresses and read all blocks.
1292 * If there is any error, we schedule a write. The lowest numbered
1293 * drive is authoritative.
1294 * However requests come for physical address, so we need to map.
1295 * For every physical address there are raid_disks/copies virtual addresses,
1296 * which is always are least one, but is not necessarly an integer.
1297 * This means that a physical address can span multiple chunks, so we may
1298 * have to submit multiple io requests for a single sync request.
1299 */
1300/*
1301 * We check if all blocks are in-sync and only write to blocks that
1302 * aren't in sync
1303 */
1304static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1305{
NeilBrown070ec552009-06-16 16:54:21 +10001306 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int i, first;
1308 struct bio *tbio, *fbio;
1309
1310 atomic_set(&r10_bio->remaining, 1);
1311
1312 /* find the first device with a block */
1313 for (i=0; i<conf->copies; i++)
1314 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1315 break;
1316
1317 if (i == conf->copies)
1318 goto done;
1319
1320 first = i;
1321 fbio = r10_bio->devs[i].bio;
1322
1323 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001324 for (i=0 ; i < conf->copies ; i++) {
1325 int j, d;
1326 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001329
1330 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001332 if (i == first)
1333 continue;
1334 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1335 /* We know that the bi_io_vec layout is the same for
1336 * both 'first' and 'i', so we just compare them.
1337 * All vec entries are PAGE_SIZE;
1338 */
1339 for (j = 0; j < vcnt; j++)
1340 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1341 page_address(tbio->bi_io_vec[j].bv_page),
1342 PAGE_SIZE))
1343 break;
1344 if (j == vcnt)
1345 continue;
1346 mddev->resync_mismatches += r10_bio->sectors;
1347 }
NeilBrown18f08812006-01-06 00:20:25 -08001348 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1349 /* Don't fix anything. */
1350 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* Ok, we need to write this bio
1352 * First we need to fixup bv_offset, bv_len and
1353 * bi_vecs, as the read request might have corrupted these
1354 */
1355 tbio->bi_vcnt = vcnt;
1356 tbio->bi_size = r10_bio->sectors << 9;
1357 tbio->bi_idx = 0;
1358 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1360 tbio->bi_flags |= 1 << BIO_UPTODATE;
1361 tbio->bi_next = NULL;
1362 tbio->bi_rw = WRITE;
1363 tbio->bi_private = r10_bio;
1364 tbio->bi_sector = r10_bio->devs[i].addr;
1365
1366 for (j=0; j < vcnt ; j++) {
1367 tbio->bi_io_vec[j].bv_offset = 0;
1368 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1369
1370 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1371 page_address(fbio->bi_io_vec[j].bv_page),
1372 PAGE_SIZE);
1373 }
1374 tbio->bi_end_io = end_sync_write;
1375
1376 d = r10_bio->devs[i].devnum;
1377 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1378 atomic_inc(&r10_bio->remaining);
1379 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1380
1381 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1382 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1383 generic_make_request(tbio);
1384 }
1385
1386done:
1387 if (atomic_dec_and_test(&r10_bio->remaining)) {
1388 md_done_sync(mddev, r10_bio->sectors, 1);
1389 put_buf(r10_bio);
1390 }
1391}
1392
1393/*
1394 * Now for the recovery code.
1395 * Recovery happens across physical sectors.
1396 * We recover all non-is_sync drives by finding the virtual address of
1397 * each, and then choose a working drive that also has that virt address.
1398 * There is a separate r10_bio for each non-in_sync drive.
1399 * Only the first two slots are in use. The first for reading,
1400 * The second for writing.
1401 *
1402 */
1403
1404static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1405{
NeilBrown070ec552009-06-16 16:54:21 +10001406 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int i, d;
1408 struct bio *bio, *wbio;
1409
1410
1411 /* move the pages across to the second bio
1412 * and submit the write request
1413 */
1414 bio = r10_bio->devs[0].bio;
1415 wbio = r10_bio->devs[1].bio;
1416 for (i=0; i < wbio->bi_vcnt; i++) {
1417 struct page *p = bio->bi_io_vec[i].bv_page;
1418 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1419 wbio->bi_io_vec[i].bv_page = p;
1420 }
1421 d = r10_bio->devs[1].devnum;
1422
1423 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1424 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001425 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1426 generic_make_request(wbio);
1427 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001428 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431
1432/*
1433 * This is a kernel thread which:
1434 *
1435 * 1. Retries failed read operations on working mirrors.
1436 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001437 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
1439
NeilBrown6814d532006-10-03 01:15:45 -07001440static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1441{
1442 int sect = 0; /* Offset from r10_bio->sector */
1443 int sectors = r10_bio->sectors;
1444 mdk_rdev_t*rdev;
1445 while(sectors) {
1446 int s = sectors;
1447 int sl = r10_bio->read_slot;
1448 int success = 0;
1449 int start;
1450
1451 if (s > (PAGE_SIZE>>9))
1452 s = PAGE_SIZE >> 9;
1453
1454 rcu_read_lock();
1455 do {
1456 int d = r10_bio->devs[sl].devnum;
1457 rdev = rcu_dereference(conf->mirrors[d].rdev);
1458 if (rdev &&
1459 test_bit(In_sync, &rdev->flags)) {
1460 atomic_inc(&rdev->nr_pending);
1461 rcu_read_unlock();
1462 success = sync_page_io(rdev->bdev,
1463 r10_bio->devs[sl].addr +
1464 sect + rdev->data_offset,
1465 s<<9,
1466 conf->tmppage, READ);
1467 rdev_dec_pending(rdev, mddev);
1468 rcu_read_lock();
1469 if (success)
1470 break;
1471 }
1472 sl++;
1473 if (sl == conf->copies)
1474 sl = 0;
1475 } while (!success && sl != r10_bio->read_slot);
1476 rcu_read_unlock();
1477
1478 if (!success) {
1479 /* Cannot read from anywhere -- bye bye array */
1480 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1481 md_error(mddev, conf->mirrors[dn].rdev);
1482 break;
1483 }
1484
1485 start = sl;
1486 /* write it back and re-read */
1487 rcu_read_lock();
1488 while (sl != r10_bio->read_slot) {
1489 int d;
1490 if (sl==0)
1491 sl = conf->copies;
1492 sl--;
1493 d = r10_bio->devs[sl].devnum;
1494 rdev = rcu_dereference(conf->mirrors[d].rdev);
1495 if (rdev &&
1496 test_bit(In_sync, &rdev->flags)) {
1497 atomic_inc(&rdev->nr_pending);
1498 rcu_read_unlock();
1499 atomic_add(s, &rdev->corrected_errors);
1500 if (sync_page_io(rdev->bdev,
1501 r10_bio->devs[sl].addr +
1502 sect + rdev->data_offset,
1503 s<<9, conf->tmppage, WRITE)
1504 == 0)
1505 /* Well, this device is dead */
1506 md_error(mddev, rdev);
1507 rdev_dec_pending(rdev, mddev);
1508 rcu_read_lock();
1509 }
1510 }
1511 sl = start;
1512 while (sl != r10_bio->read_slot) {
1513 int d;
1514 if (sl==0)
1515 sl = conf->copies;
1516 sl--;
1517 d = r10_bio->devs[sl].devnum;
1518 rdev = rcu_dereference(conf->mirrors[d].rdev);
1519 if (rdev &&
1520 test_bit(In_sync, &rdev->flags)) {
1521 char b[BDEVNAME_SIZE];
1522 atomic_inc(&rdev->nr_pending);
1523 rcu_read_unlock();
1524 if (sync_page_io(rdev->bdev,
1525 r10_bio->devs[sl].addr +
1526 sect + rdev->data_offset,
1527 s<<9, conf->tmppage, READ) == 0)
1528 /* Well, this device is dead */
1529 md_error(mddev, rdev);
1530 else
1531 printk(KERN_INFO
1532 "raid10:%s: read error corrected"
1533 " (%d sectors at %llu on %s)\n",
1534 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001535 (unsigned long long)(sect+
1536 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001537 bdevname(rdev->bdev, b));
1538
1539 rdev_dec_pending(rdev, mddev);
1540 rcu_read_lock();
1541 }
1542 }
1543 rcu_read_unlock();
1544
1545 sectors -= s;
1546 sect += s;
1547 }
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static void raid10d(mddev_t *mddev)
1551{
1552 r10bio_t *r10_bio;
1553 struct bio *bio;
1554 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001555 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct list_head *head = &conf->retry_list;
1557 int unplug=0;
1558 mdk_rdev_t *rdev;
1559
1560 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 for (;;) {
1563 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001564
1565 unplug += flush_pending_writes(conf);
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001568 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001569 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1573 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001574 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 spin_unlock_irqrestore(&conf->device_lock, flags);
1576
1577 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001578 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1580 sync_request_write(mddev, r10_bio);
1581 unplug = 1;
1582 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1583 recovery_request_write(mddev, r10_bio);
1584 unplug = 1;
1585 } else {
1586 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001587 /* we got a read error. Maybe the drive is bad. Maybe just
1588 * the block and we can fix it.
1589 * We freeze all other IO, and try reading the block from
1590 * other devices. When we find one, we re-write
1591 * and check it that fixes the read error.
1592 * This is all done synchronously while the array is
1593 * frozen.
1594 */
NeilBrown6814d532006-10-03 01:15:45 -07001595 if (mddev->ro == 0) {
1596 freeze_array(conf);
1597 fix_read_error(conf, mddev, r10_bio);
1598 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001599 }
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001602 r10_bio->devs[r10_bio->read_slot].bio =
1603 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 mirror = read_balance(conf, r10_bio);
1605 if (mirror == -1) {
1606 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1607 " read error for block %llu\n",
1608 bdevname(bio->bi_bdev,b),
1609 (unsigned long long)r10_bio->sector);
1610 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001611 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 } else {
Lars Ellenberge3881a62007-01-10 23:15:37 -08001613 const int do_sync = bio_sync(r10_bio->master_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001614 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 rdev = conf->mirrors[mirror].rdev;
1616 if (printk_ratelimit())
1617 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1618 " another mirror\n",
1619 bdevname(rdev->bdev,b),
1620 (unsigned long long)r10_bio->sector);
1621 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1622 r10_bio->devs[r10_bio->read_slot].bio = bio;
1623 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1624 + rdev->data_offset;
1625 bio->bi_bdev = rdev->bdev;
Lars Ellenberge3881a62007-01-10 23:15:37 -08001626 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 bio->bi_private = r10_bio;
1628 bio->bi_end_io = raid10_end_read_request;
1629 unplug = 1;
1630 generic_make_request(bio);
1631 }
1632 }
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (unplug)
1635 unplug_slaves(mddev);
1636}
1637
1638
1639static int init_resync(conf_t *conf)
1640{
1641 int buffs;
1642
1643 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001644 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1646 if (!conf->r10buf_pool)
1647 return -ENOMEM;
1648 conf->next_resync = 0;
1649 return 0;
1650}
1651
1652/*
1653 * perform a "sync" on one "block"
1654 *
1655 * We need to make sure that no normal I/O request - particularly write
1656 * requests - conflict with active sync requests.
1657 *
1658 * This is achieved by tracking pending requests and a 'barrier' concept
1659 * that can be installed to exclude normal IO requests.
1660 *
1661 * Resync and recovery are handled very differently.
1662 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1663 *
1664 * For resync, we iterate over virtual addresses, read all copies,
1665 * and update if there are differences. If only one copy is live,
1666 * skip it.
1667 * For recovery, we iterate over physical addresses, read a good
1668 * value for each non-in_sync drive, and over-write.
1669 *
1670 * So, for recovery we may have several outstanding complex requests for a
1671 * given address, one for each out-of-sync device. We model this by allocating
1672 * a number of r10_bio structures, one for each out-of-sync device.
1673 * As we setup these structures, we collect all bio's together into a list
1674 * which we then process collectively to add pages, and then process again
1675 * to pass to generic_make_request.
1676 *
1677 * The r10_bio structures are linked using a borrowed master_bio pointer.
1678 * This link is counted in ->remaining. When the r10_bio that points to NULL
1679 * has its remaining count decremented to 0, the whole complex operation
1680 * is complete.
1681 *
1682 */
1683
NeilBrown57afd892005-06-21 17:17:13 -07001684static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
NeilBrown070ec552009-06-16 16:54:21 +10001686 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 r10bio_t *r10_bio;
1688 struct bio *biolist = NULL, *bio;
1689 sector_t max_sector, nr_sectors;
1690 int disk;
1691 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001692 int max_sync;
1693 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 sector_t sectors_skipped = 0;
1696 int chunks_skipped = 0;
1697
1698 if (!conf->r10buf_pool)
1699 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001700 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001703 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1705 max_sector = mddev->resync_max_sectors;
1706 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001707 /* If we aborted, we need to abort the
1708 * sync on the 'current' bitmap chucks (there can
1709 * be several when recovering multiple devices).
1710 * as we may have started syncing it but not finished.
1711 * We can find the current address in
1712 * mddev->curr_resync, but for recovery,
1713 * we need to convert that to several
1714 * virtual addresses.
1715 */
1716 if (mddev->curr_resync < max_sector) { /* aborted */
1717 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1718 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1719 &sync_blocks, 1);
1720 else for (i=0; i<conf->raid_disks; i++) {
1721 sector_t sect =
1722 raid10_find_virt(conf, mddev->curr_resync, i);
1723 bitmap_end_sync(mddev->bitmap, sect,
1724 &sync_blocks, 1);
1725 }
1726 } else /* completed sync */
1727 conf->fullsync = 0;
1728
1729 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001731 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return sectors_skipped;
1733 }
1734 if (chunks_skipped >= conf->raid_disks) {
1735 /* if there has been nothing to do on any drive,
1736 * then there is nothing to do at all..
1737 */
NeilBrown57afd892005-06-21 17:17:13 -07001738 *skipped = 1;
1739 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
NeilBrownc6207272008-02-06 01:39:52 -08001742 if (max_sector > mddev->resync_max)
1743 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /* make sure whole request will fit in a chunk - if chunks
1746 * are meaningful
1747 */
1748 if (conf->near_copies < conf->raid_disks &&
1749 max_sector > (sector_nr | conf->chunk_mask))
1750 max_sector = (sector_nr | conf->chunk_mask) + 1;
1751 /*
1752 * If there is non-resync activity waiting for us then
1753 * put in a delay to throttle resync.
1754 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001755 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 /* Again, very different code for resync and recovery.
1759 * Both must result in an r10bio with a list of bios that
1760 * have bi_end_io, bi_sector, bi_bdev set,
1761 * and bi_private set to the r10bio.
1762 * For recovery, we may actually create several r10bios
1763 * with 2 bios in each, that correspond to the bios in the main one.
1764 * In this case, the subordinate r10bios link back through a
1765 * borrowed master_bio pointer, and the counter in the master
1766 * includes a ref from each subordinate.
1767 */
1768 /* First, we decide what to do and set ->bi_end_io
1769 * To end_sync_read if we want to read, and
1770 * end_sync_write if we will want to write.
1771 */
1772
NeilBrown6cce3b22006-01-06 00:20:16 -08001773 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1775 /* recovery... the complicated one */
1776 int i, j, k;
1777 r10_bio = NULL;
1778
1779 for (i=0 ; i<conf->raid_disks; i++)
1780 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001781 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001782 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* want to reconstruct this device */
1784 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001785 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1786 int must_sync;
1787 /* Unless we are doing a full sync, we only need
1788 * to recover the block if it is set in the bitmap
1789 */
1790 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1791 &sync_blocks, 1);
1792 if (sync_blocks < max_sync)
1793 max_sync = sync_blocks;
1794 if (!must_sync &&
1795 !conf->fullsync) {
1796 /* yep, skip the sync_blocks here, but don't assume
1797 * that there will never be anything to do here
1798 */
1799 chunks_skipped = -1;
1800 continue;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001804 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 atomic_set(&r10_bio->remaining, 0);
1806
1807 r10_bio->master_bio = (struct bio*)rb2;
1808 if (rb2)
1809 atomic_inc(&rb2->remaining);
1810 r10_bio->mddev = mddev;
1811 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001812 r10_bio->sector = sect;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001815
1816 /* Need to check if the array will still be
NeilBrown6cce3b22006-01-06 00:20:16 -08001817 * degraded
1818 */
NeilBrown18055562009-05-07 12:48:10 +10001819 for (j=0; j<conf->raid_disks; j++)
1820 if (conf->mirrors[j].rdev == NULL ||
1821 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001822 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001823 break;
1824 }
NeilBrown18055562009-05-07 12:48:10 +10001825
NeilBrown6cce3b22006-01-06 00:20:16 -08001826 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1827 &sync_blocks, still_degraded);
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 for (j=0; j<conf->copies;j++) {
1830 int d = r10_bio->devs[j].devnum;
1831 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001832 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* This is where we read from */
1834 bio = r10_bio->devs[0].bio;
1835 bio->bi_next = biolist;
1836 biolist = bio;
1837 bio->bi_private = r10_bio;
1838 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001839 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 bio->bi_sector = r10_bio->devs[j].addr +
1841 conf->mirrors[d].rdev->data_offset;
1842 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1843 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1844 atomic_inc(&r10_bio->remaining);
1845 /* and we write to 'i' */
1846
1847 for (k=0; k<conf->copies; k++)
1848 if (r10_bio->devs[k].devnum == i)
1849 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001850 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 bio = r10_bio->devs[1].bio;
1852 bio->bi_next = biolist;
1853 biolist = bio;
1854 bio->bi_private = r10_bio;
1855 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001856 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 bio->bi_sector = r10_bio->devs[k].addr +
1858 conf->mirrors[i].rdev->data_offset;
1859 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1860
1861 r10_bio->devs[0].devnum = d;
1862 r10_bio->devs[1].devnum = i;
1863
1864 break;
1865 }
1866 }
1867 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001868 /* Cannot recover, so abort the recovery */
1869 put_buf(r10_bio);
K.Tanakaa07e6ab2008-03-04 14:29:37 -08001870 if (rb2)
1871 atomic_dec(&rb2->remaining);
NeilBrown87fc7672005-09-09 16:24:04 -07001872 r10_bio = rb2;
NeilBrowndfc70642008-05-23 13:04:39 -07001873 if (!test_and_set_bit(MD_RECOVERY_INTR,
1874 &mddev->recovery))
NeilBrown87fc7672005-09-09 16:24:04 -07001875 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1876 mdname(mddev));
1877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879 }
1880 if (biolist == NULL) {
1881 while (r10_bio) {
1882 r10bio_t *rb2 = r10_bio;
1883 r10_bio = (r10bio_t*) rb2->master_bio;
1884 rb2->master_bio = NULL;
1885 put_buf(rb2);
1886 }
1887 goto giveup;
1888 }
1889 } else {
1890 /* resync. Schedule a read for every block at this virt offset */
1891 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001892
NeilBrown78200d42009-02-25 13:18:47 +11001893 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1894
NeilBrown6cce3b22006-01-06 00:20:16 -08001895 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1896 &sync_blocks, mddev->degraded) &&
1897 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1898 /* We can skip this block */
1899 *skipped = 1;
1900 return sync_blocks + sectors_skipped;
1901 }
1902 if (sync_blocks < max_sync)
1903 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 r10_bio->mddev = mddev;
1907 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001908 raise_barrier(conf, 0);
1909 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 r10_bio->master_bio = NULL;
1912 r10_bio->sector = sector_nr;
1913 set_bit(R10BIO_IsSync, &r10_bio->state);
1914 raid10_find_phys(conf, r10_bio);
1915 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1916
1917 for (i=0; i<conf->copies; i++) {
1918 int d = r10_bio->devs[i].devnum;
1919 bio = r10_bio->devs[i].bio;
1920 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001921 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001923 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 continue;
1925 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1926 atomic_inc(&r10_bio->remaining);
1927 bio->bi_next = biolist;
1928 biolist = bio;
1929 bio->bi_private = r10_bio;
1930 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001931 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 bio->bi_sector = r10_bio->devs[i].addr +
1933 conf->mirrors[d].rdev->data_offset;
1934 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1935 count++;
1936 }
1937
1938 if (count < 2) {
1939 for (i=0; i<conf->copies; i++) {
1940 int d = r10_bio->devs[i].devnum;
1941 if (r10_bio->devs[i].bio->bi_end_io)
1942 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1943 }
1944 put_buf(r10_bio);
1945 biolist = NULL;
1946 goto giveup;
1947 }
1948 }
1949
1950 for (bio = biolist; bio ; bio=bio->bi_next) {
1951
1952 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1953 if (bio->bi_end_io)
1954 bio->bi_flags |= 1 << BIO_UPTODATE;
1955 bio->bi_vcnt = 0;
1956 bio->bi_idx = 0;
1957 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 bio->bi_size = 0;
1959 }
1960
1961 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001962 if (sector_nr + max_sync < max_sector)
1963 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 do {
1965 struct page *page;
1966 int len = PAGE_SIZE;
1967 disk = 0;
1968 if (sector_nr + (len>>9) > max_sector)
1969 len = (max_sector - sector_nr) << 9;
1970 if (len == 0)
1971 break;
1972 for (bio= biolist ; bio ; bio=bio->bi_next) {
1973 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1974 if (bio_add_page(bio, page, len, 0) == 0) {
1975 /* stop here */
1976 struct bio *bio2;
1977 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1978 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1979 /* remove last page from this bio */
1980 bio2->bi_vcnt--;
1981 bio2->bi_size -= len;
1982 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1983 }
1984 goto bio_full;
1985 }
1986 disk = i;
1987 }
1988 nr_sectors += len>>9;
1989 sector_nr += len>>9;
1990 } while (biolist->bi_vcnt < RESYNC_PAGES);
1991 bio_full:
1992 r10_bio->sectors = nr_sectors;
1993
1994 while (biolist) {
1995 bio = biolist;
1996 biolist = biolist->bi_next;
1997
1998 bio->bi_next = NULL;
1999 r10_bio = bio->bi_private;
2000 r10_bio->sectors = nr_sectors;
2001
2002 if (bio->bi_end_io == end_sync_read) {
2003 md_sync_acct(bio->bi_bdev, nr_sectors);
2004 generic_make_request(bio);
2005 }
2006 }
2007
NeilBrown57afd892005-06-21 17:17:13 -07002008 if (sectors_skipped)
2009 /* pretend they weren't skipped, it makes
2010 * no important difference in this case
2011 */
2012 md_done_sync(mddev, sectors_skipped, 1);
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return sectors_skipped + nr_sectors;
2015 giveup:
2016 /* There is nowhere to write, so all non-sync
2017 * drives must be failed, so try the next chunk...
2018 */
NeilBrown09b40682009-02-25 13:18:47 +11002019 if (sector_nr + max_sync < max_sector)
2020 max_sector = sector_nr + max_sync;
2021
2022 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 chunks_skipped ++;
2024 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Dan Williams80c3a6c2009-03-17 18:10:40 -07002028static sector_t
2029raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2030{
2031 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002032 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002033
2034 if (!raid_disks)
2035 raid_disks = mddev->raid_disks;
2036 if (!sectors)
2037 sectors = mddev->dev_sectors;
2038
2039 size = sectors >> conf->chunk_shift;
2040 sector_div(size, conf->far_copies);
2041 size = size * raid_disks;
2042 sector_div(size, conf->near_copies);
2043
2044 return size << conf->chunk_shift;
2045}
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047static int run(mddev_t *mddev)
2048{
2049 conf_t *conf;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002050 int i, disk_idx, chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 mirror_info_t *disk;
2052 mdk_rdev_t *rdev;
NeilBrownc93983b2006-06-26 00:27:41 -07002053 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 sector_t stride, size;
2055
Andre Noll9d8f0362009-06-18 08:45:01 +10002056 if (mddev->chunk_sectors < (PAGE_SIZE >> 9) ||
2057 !is_power_of_2(mddev->chunk_sectors)) {
NeilBrown4bbf3772008-10-13 11:55:12 +11002058 printk(KERN_ERR "md/raid10: chunk size must be "
raz ben yehuda964e7912009-06-16 17:01:22 +10002059 "at least PAGE_SIZE(%ld) and be a power of 2.\n", PAGE_SIZE);
NeilBrown2604b702006-01-06 00:20:36 -08002060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
NeilBrown2604b702006-01-06 00:20:36 -08002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 nc = mddev->layout & 255;
2064 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07002065 fo = mddev->layout & (1<<16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07002067 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2069 mdname(mddev), mddev->layout);
2070 goto out;
2071 }
2072 /*
2073 * copy the already verified devices into our private RAID10
2074 * bookkeeping area. [whatever we allocate in run(),
2075 * should be freed in stop()]
2076 */
NeilBrown4443ae12006-01-06 00:20:28 -08002077 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 mddev->private = conf;
2079 if (!conf) {
2080 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2081 mdname(mddev));
2082 goto out;
2083 }
NeilBrown4443ae12006-01-06 00:20:28 -08002084 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 GFP_KERNEL);
2086 if (!conf->mirrors) {
2087 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2088 mdname(mddev));
2089 goto out_free_conf;
2090 }
NeilBrown4443ae12006-01-06 00:20:28 -08002091
2092 conf->tmppage = alloc_page(GFP_KERNEL);
2093 if (!conf->tmppage)
2094 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
NeilBrown64a742b2007-02-28 20:11:18 -08002096 conf->mddev = mddev;
2097 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 conf->near_copies = nc;
2099 conf->far_copies = fc;
2100 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002101 conf->far_offset = fo;
Andre Noll9d8f0362009-06-18 08:45:01 +10002102 conf->chunk_mask = mddev->chunk_sectors - 1;
2103 conf->chunk_shift = ffz(~mddev->chunk_sectors);
Andre Noll58c0fed2009-03-31 14:33:13 +11002104 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002105 sector_div(size, fc);
2106 size = size * conf->raid_disks;
2107 sector_div(size, nc);
2108 /* 'size' is now the number of chunks in the array */
2109 /* calculate "used chunks per device" in 'stride' */
2110 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002111
2112 /* We need to round up when dividing by raid_disks to
2113 * get the stride size.
2114 */
2115 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002116 sector_div(stride, conf->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002117 mddev->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002118
NeilBrownc93983b2006-06-26 00:27:41 -07002119 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002120 stride = 1;
2121 else
NeilBrownc93983b2006-06-26 00:27:41 -07002122 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002123 conf->stride = stride << conf->chunk_shift;
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2126 r10bio_pool_free, conf);
2127 if (!conf->r10bio_pool) {
2128 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2129 mdname(mddev));
2130 goto out_free_conf;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Neil Browne7e72bf2008-05-14 16:05:54 -07002133 spin_lock_init(&conf->device_lock);
2134 mddev->queue->queue_lock = &conf->device_lock;
2135
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002136 chunk_size = mddev->chunk_sectors << 9;
2137 blk_queue_io_min(mddev->queue, chunk_size);
2138 if (conf->raid_disks % conf->near_copies)
2139 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2140 else
2141 blk_queue_io_opt(mddev->queue, chunk_size *
2142 (conf->raid_disks / conf->near_copies));
2143
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002144 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 disk_idx = rdev->raid_disk;
2146 if (disk_idx >= mddev->raid_disks
2147 || disk_idx < 0)
2148 continue;
2149 disk = conf->mirrors + disk_idx;
2150
2151 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002152 disk_stack_limits(mddev->gendisk, rdev->bdev,
2153 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* as we don't honour merge_bvec_fn, we must never risk
2155 * violating it, so limit ->max_sector to one PAGE, as
2156 * a one page request is never in violation.
2157 */
2158 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
Martin K. Petersenae03bf62009-05-22 17:17:50 -04002159 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
2160 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 INIT_LIST_HEAD(&conf->retry_list);
2165
2166 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08002167 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
NeilBrown6d508242005-09-09 16:24:03 -07002169 /* need to check that every block has at least one working mirror */
2170 if (!enough(conf)) {
2171 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2172 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto out_free_conf;
2174 }
2175
2176 mddev->degraded = 0;
2177 for (i = 0; i < conf->raid_disks; i++) {
2178
2179 disk = conf->mirrors + i;
2180
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002181 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002182 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 disk->head_position = 0;
2184 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002185 if (disk->rdev)
2186 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188 }
2189
2190
2191 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2192 if (!mddev->thread) {
2193 printk(KERN_ERR
2194 "raid10: couldn't allocate thread for %s\n",
2195 mdname(mddev));
2196 goto out_free_conf;
2197 }
2198
Andre Noll8c6ac862009-06-18 08:48:06 +10002199 if (mddev->recovery_cp != MaxSector)
2200 printk(KERN_NOTICE "raid10: %s is not clean"
2201 " -- starting background reconstruction\n",
2202 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 printk(KERN_INFO
2204 "raid10: raid set %s active with %d out of %d devices\n",
2205 mdname(mddev), mddev->raid_disks - mddev->degraded,
2206 mddev->raid_disks);
2207 /*
2208 * Ok, everything is just fine now
2209 */
Dan Williams1f403622009-03-31 14:59:03 +11002210 md_set_array_sectors(mddev, raid10_size(mddev, 0, 0));
Dan Williamsb522adc2009-03-31 15:00:31 +11002211 mddev->resync_max_sectors = raid10_size(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
NeilBrown7a5febe2005-05-16 21:53:16 -07002213 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002214 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2215 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /* Calculate max read-ahead size.
2218 * We need to readahead at least twice a whole stripe....
2219 * maybe...
2220 */
2221 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002222 int stripe = conf->raid_disks *
2223 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 stripe /= conf->near_copies;
2225 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2226 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2227 }
2228
2229 if (conf->near_copies < mddev->raid_disks)
2230 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Andre Nollac5e7112009-08-03 10:59:47 +10002231 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return 0;
2233
2234out_free_conf:
2235 if (conf->r10bio_pool)
2236 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002237 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002238 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 kfree(conf);
2240 mddev->private = NULL;
2241out:
2242 return -EIO;
2243}
2244
2245static int stop(mddev_t *mddev)
2246{
NeilBrown070ec552009-06-16 16:54:21 +10002247 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
NeilBrown409c57f2009-03-31 14:39:39 +11002249 raise_barrier(conf, 0);
2250 lower_barrier(conf);
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 md_unregister_thread(mddev->thread);
2253 mddev->thread = NULL;
2254 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2255 if (conf->r10bio_pool)
2256 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002257 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 kfree(conf);
2259 mddev->private = NULL;
2260 return 0;
2261}
2262
NeilBrown6cce3b22006-01-06 00:20:16 -08002263static void raid10_quiesce(mddev_t *mddev, int state)
2264{
NeilBrown070ec552009-06-16 16:54:21 +10002265 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002266
2267 switch(state) {
2268 case 1:
2269 raise_barrier(conf, 0);
2270 break;
2271 case 0:
2272 lower_barrier(conf);
2273 break;
2274 }
2275 if (mddev->thread) {
2276 if (mddev->bitmap)
2277 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2278 else
2279 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2280 md_wakeup_thread(mddev->thread);
2281 }
2282}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
NeilBrown2604b702006-01-06 00:20:36 -08002284static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002287 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 .owner = THIS_MODULE,
2289 .make_request = make_request,
2290 .run = run,
2291 .stop = stop,
2292 .status = status,
2293 .error_handler = error,
2294 .hot_add_disk = raid10_add_disk,
2295 .hot_remove_disk= raid10_remove_disk,
2296 .spare_active = raid10_spare_active,
2297 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002298 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002299 .size = raid10_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300};
2301
2302static int __init raid_init(void)
2303{
NeilBrown2604b702006-01-06 00:20:36 -08002304 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
2307static void raid_exit(void)
2308{
NeilBrown2604b702006-01-06 00:20:36 -08002309 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
2312module_init(raid_init);
2313module_exit(raid_exit);
2314MODULE_LICENSE("GPL");
2315MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002316MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002317MODULE_ALIAS("md-level-10");