blob: 69fc76caa469a9424a218db4e1217310919b4b17 [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
NeilBrown3fa841d2009-09-23 18:10:29 +1000634 if (mddev_congested(mddev, bits))
635 return 1;
NeilBrown0d129222006-10-03 01:15:54 -0700636 rcu_read_lock();
637 for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
638 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
639 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200640 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700641
642 ret |= bdi_congested(&q->backing_dev_info, bits);
643 }
644 }
645 rcu_read_unlock();
646 return ret;
647}
648
NeilBrowna35e63e2008-03-04 14:29:29 -0800649static int flush_pending_writes(conf_t *conf)
650{
651 /* Any writes that have been queued but are awaiting
652 * bitmap updates get flushed here.
653 * We return 1 if any requests were actually submitted.
654 */
655 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700656
NeilBrowna35e63e2008-03-04 14:29:29 -0800657 spin_lock_irq(&conf->device_lock);
658
659 if (conf->pending_bio_list.head) {
660 struct bio *bio;
661 bio = bio_list_get(&conf->pending_bio_list);
662 blk_remove_plug(conf->mddev->queue);
663 spin_unlock_irq(&conf->device_lock);
664 /* flush any pending bitmap writes to disk
665 * before proceeding w/ I/O */
666 bitmap_unplug(conf->mddev->bitmap);
667
668 while (bio) { /* submit pending writes */
669 struct bio *next = bio->bi_next;
670 bio->bi_next = NULL;
671 generic_make_request(bio);
672 bio = next;
673 }
674 rv = 1;
675 } else
676 spin_unlock_irq(&conf->device_lock);
677 return rv;
678}
NeilBrown0a27ec92006-01-06 00:20:13 -0800679/* Barriers....
680 * Sometimes we need to suspend IO while we do something else,
681 * either some resync/recovery, or reconfigure the array.
682 * To do this we raise a 'barrier'.
683 * The 'barrier' is a counter that can be raised multiple times
684 * to count how many activities are happening which preclude
685 * normal IO.
686 * We can only raise the barrier if there is no pending IO.
687 * i.e. if nr_pending == 0.
688 * We choose only to raise the barrier if no-one is waiting for the
689 * barrier to go down. This means that as soon as an IO request
690 * is ready, no other operations which require a barrier will start
691 * until the IO request has had a chance.
692 *
693 * So: regular IO calls 'wait_barrier'. When that returns there
694 * is no backgroup IO happening, It must arrange to call
695 * allow_barrier when it has finished its IO.
696 * backgroup IO calls must call raise_barrier. Once that returns
697 * there is no normal IO happeing. It must arrange to call
698 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
NeilBrown6cce3b22006-01-06 00:20:16 -0800701static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
NeilBrown6cce3b22006-01-06 00:20:16 -0800703 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
NeilBrown6cce3b22006-01-06 00:20:16 -0800706 /* Wait until no block IO is waiting (unless 'force') */
707 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800708 conf->resync_lock,
709 raid10_unplug(conf->mddev->queue));
710
711 /* block any new IO from starting */
712 conf->barrier++;
713
714 /* No wait for all pending IO to complete */
715 wait_event_lock_irq(conf->wait_barrier,
716 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
717 conf->resync_lock,
718 raid10_unplug(conf->mddev->queue));
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 spin_unlock_irq(&conf->resync_lock);
721}
722
NeilBrown0a27ec92006-01-06 00:20:13 -0800723static void lower_barrier(conf_t *conf)
724{
725 unsigned long flags;
726 spin_lock_irqsave(&conf->resync_lock, flags);
727 conf->barrier--;
728 spin_unlock_irqrestore(&conf->resync_lock, flags);
729 wake_up(&conf->wait_barrier);
730}
731
732static void wait_barrier(conf_t *conf)
733{
734 spin_lock_irq(&conf->resync_lock);
735 if (conf->barrier) {
736 conf->nr_waiting++;
737 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
738 conf->resync_lock,
739 raid10_unplug(conf->mddev->queue));
740 conf->nr_waiting--;
741 }
742 conf->nr_pending++;
743 spin_unlock_irq(&conf->resync_lock);
744}
745
746static void allow_barrier(conf_t *conf)
747{
748 unsigned long flags;
749 spin_lock_irqsave(&conf->resync_lock, flags);
750 conf->nr_pending--;
751 spin_unlock_irqrestore(&conf->resync_lock, flags);
752 wake_up(&conf->wait_barrier);
753}
754
NeilBrown4443ae12006-01-06 00:20:28 -0800755static void freeze_array(conf_t *conf)
756{
757 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800758 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800759 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800760 * wait until nr_pending match nr_queued+1
761 * This is called in the context of one normal IO request
762 * that has failed. Thus any sync request that might be pending
763 * will be blocked by nr_pending, and we need to wait for
764 * pending IO requests to complete or be queued for re-try.
765 * Thus the number queued (nr_queued) plus this request (1)
766 * must match the number of pending IOs (nr_pending) before
767 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800768 */
769 spin_lock_irq(&conf->resync_lock);
770 conf->barrier++;
771 conf->nr_waiting++;
772 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800773 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800774 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800775 ({ flush_pending_writes(conf);
776 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800777 spin_unlock_irq(&conf->resync_lock);
778}
779
780static void unfreeze_array(conf_t *conf)
781{
782 /* reverse the effect of the freeze */
783 spin_lock_irq(&conf->resync_lock);
784 conf->barrier--;
785 conf->nr_waiting--;
786 wake_up(&conf->wait_barrier);
787 spin_unlock_irq(&conf->resync_lock);
788}
789
Jens Axboe165125e2007-07-24 09:28:11 +0200790static int make_request(struct request_queue *q, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +1000793 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mirror_info_t *mirror;
795 r10bio_t *r10_bio;
796 struct bio *read_bio;
Tejun Heoc9959052008-08-25 19:47:21 +0900797 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int i;
799 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100800 const int rw = bio_data_dir(bio);
Jens Axboe1f98a132009-09-11 14:32:04 +0200801 const bool do_sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
NeilBrown6cce3b22006-01-06 00:20:16 -0800802 struct bio_list bl;
803 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700804 mdk_rdev_t *blocked_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jens Axboe1f98a132009-09-11 14:32:04 +0200806 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200807 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700808 return 0;
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* If this request crosses a chunk boundary, we need to
812 * split it. This will only happen for 1 PAGE (or less) requests.
813 */
814 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
815 > chunk_sects &&
816 conf->near_copies < conf->raid_disks)) {
817 struct bio_pair *bp;
818 /* Sanity check -- queue functions should prevent this happening */
819 if (bio->bi_vcnt != 1 ||
820 bio->bi_idx != 0)
821 goto bad_map;
822 /* This is a one page bio that upper layers
823 * refuse to split for us, so we need to split it.
824 */
Denis ChengRq6feef532008-10-09 08:57:05 +0200825 bp = bio_split(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
827 if (make_request(q, &bp->bio1))
828 generic_make_request(&bp->bio1);
829 if (make_request(q, &bp->bio2))
830 generic_make_request(&bp->bio2);
831
832 bio_pair_release(bp);
833 return 0;
834 bad_map:
835 printk("raid10_make_request bug: can't convert block across chunks"
836 " or bigger than %dk %llu %d\n", chunk_sects/2,
837 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
838
NeilBrown6712ecf2007-09-27 12:47:43 +0200839 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841 }
842
NeilBrown3d310eb2005-06-21 17:17:26 -0700843 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /*
846 * Register the new request and wait if the reconstruction
847 * thread has put up a bar for new requests.
848 * Continue immediately if no resync is active currently.
849 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800850 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Tejun Heo074a7ac2008-08-25 19:56:14 +0900852 cpu = part_stat_lock();
853 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
854 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
855 bio_sectors(bio));
856 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
859
860 r10_bio->master_bio = bio;
861 r10_bio->sectors = bio->bi_size >> 9;
862
863 r10_bio->mddev = mddev;
864 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800865 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Jens Axboea3623572005-11-01 09:26:16 +0100867 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * read balancing logic:
870 */
871 int disk = read_balance(conf, r10_bio);
872 int slot = r10_bio->read_slot;
873 if (disk < 0) {
874 raid_end_bio_io(r10_bio);
875 return 0;
876 }
877 mirror = conf->mirrors + disk;
878
879 read_bio = bio_clone(bio, GFP_NOIO);
880
881 r10_bio->devs[slot].bio = read_bio;
882
883 read_bio->bi_sector = r10_bio->devs[slot].addr +
884 mirror->rdev->data_offset;
885 read_bio->bi_bdev = mirror->rdev->bdev;
886 read_bio->bi_end_io = raid10_end_read_request;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +0400887 read_bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 read_bio->bi_private = r10_bio;
889
890 generic_make_request(read_bio);
891 return 0;
892 }
893
894 /*
895 * WRITE:
896 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700897 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * inc refcount on their rdev. Record them by setting
899 * bios[x] to bio
900 */
901 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700902 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700903 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 rcu_read_lock();
905 for (i = 0; i < conf->copies; i++) {
906 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800907 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700908 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
909 atomic_inc(&rdev->nr_pending);
910 blocked_rdev = rdev;
911 break;
912 }
913 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800914 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800916 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800918 set_bit(R10BIO_Degraded, &r10_bio->state);
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 rcu_read_unlock();
922
Dan Williams6bfe0b42008-04-30 00:52:32 -0700923 if (unlikely(blocked_rdev)) {
924 /* Have to wait for this device to get unblocked, then retry */
925 int j;
926 int d;
927
928 for (j = 0; j < i; j++)
929 if (r10_bio->devs[j].bio) {
930 d = r10_bio->devs[j].devnum;
931 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
932 }
933 allow_barrier(conf);
934 md_wait_for_blocked_rdev(blocked_rdev, mddev);
935 wait_barrier(conf);
936 goto retry_write;
937 }
938
NeilBrown6cce3b22006-01-06 00:20:16 -0800939 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700940
NeilBrown6cce3b22006-01-06 00:20:16 -0800941 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 for (i = 0; i < conf->copies; i++) {
943 struct bio *mbio;
944 int d = r10_bio->devs[i].devnum;
945 if (!r10_bio->devs[i].bio)
946 continue;
947
948 mbio = bio_clone(bio, GFP_NOIO);
949 r10_bio->devs[i].bio = mbio;
950
951 mbio->bi_sector = r10_bio->devs[i].addr+
952 conf->mirrors[d].rdev->data_offset;
953 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
954 mbio->bi_end_io = raid10_end_write_request;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +0400955 mbio->bi_rw = WRITE | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 mbio->bi_private = r10_bio;
957
958 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b22006-01-06 00:20:16 -0800959 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
Arne Redlichf6f953a2007-07-31 00:37:57 -0700962 if (unlikely(!atomic_read(&r10_bio->remaining))) {
963 /* the array is dead */
964 md_write_end(mddev);
965 raid_end_bio_io(r10_bio);
966 return 0;
967 }
968
NeilBrown6cce3b22006-01-06 00:20:16 -0800969 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
970 spin_lock_irqsave(&conf->device_lock, flags);
971 bio_list_merge(&conf->pending_bio_list, &bl);
972 blk_plug_device(mddev->queue);
973 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
NeilBrowna35e63e2008-03-04 14:29:29 -0800975 /* In case raid10d snuck in to freeze_array */
976 wake_up(&conf->wait_barrier);
977
Lars Ellenberge3881a62007-01-10 23:15:37 -0800978 if (do_sync)
979 md_wakeup_thread(mddev->thread);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return 0;
982}
983
984static void status(struct seq_file *seq, mddev_t *mddev)
985{
NeilBrown070ec552009-06-16 16:54:21 +1000986 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int i;
988
989 if (conf->near_copies < conf->raid_disks)
Andre Noll9d8f0362009-06-18 08:45:01 +1000990 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (conf->near_copies > 1)
992 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700993 if (conf->far_copies > 1) {
994 if (conf->far_offset)
995 seq_printf(seq, " %d offset-copies", conf->far_copies);
996 else
997 seq_printf(seq, " %d far-copies", conf->far_copies);
998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -07001000 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 for (i = 0; i < conf->raid_disks; i++)
1002 seq_printf(seq, "%s",
1003 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001004 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 seq_printf(seq, "]");
1006}
1007
1008static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1009{
1010 char b[BDEVNAME_SIZE];
NeilBrown070ec552009-06-16 16:54:21 +10001011 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 /*
1014 * If it is not operational, then we have already marked it as dead
1015 * else if it is the last working disks, ignore the error, let the
1016 * next level up know.
1017 * else mark the drive as failed
1018 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001019 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -07001020 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /*
1022 * Don't fail the drive, just return an IO error.
1023 * The test should really be more sophisticated than
1024 * "working_disks == 1", but it isn't critical, and
1025 * can wait until we do more sophisticated "is the drive
1026 * really dead" tests...
1027 */
1028 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001029 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1030 unsigned long flags;
1031 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001033 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * if recovery is running, make sure it aborts.
1036 */
NeilBrowndfc70642008-05-23 13:04:39 -07001037 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001039 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001040 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Nick Andrewd7a420c2008-04-28 02:15:55 -07001041 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n"
1042 "raid10: Operation continuing on %d devices.\n",
NeilBrown76186dd2006-10-03 01:15:48 -07001043 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046static void print_conf(conf_t *conf)
1047{
1048 int i;
1049 mirror_info_t *tmp;
1050
1051 printk("RAID10 conf printout:\n");
1052 if (!conf) {
1053 printk("(!conf)\n");
1054 return;
1055 }
NeilBrown76186dd2006-10-03 01:15:48 -07001056 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 conf->raid_disks);
1058
1059 for (i = 0; i < conf->raid_disks; i++) {
1060 char b[BDEVNAME_SIZE];
1061 tmp = conf->mirrors + i;
1062 if (tmp->rdev)
1063 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001064 i, !test_bit(In_sync, &tmp->rdev->flags),
1065 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 bdevname(tmp->rdev->bdev,b));
1067 }
1068}
1069
1070static void close_sync(conf_t *conf)
1071{
NeilBrown0a27ec92006-01-06 00:20:13 -08001072 wait_barrier(conf);
1073 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 mempool_destroy(conf->r10buf_pool);
1076 conf->r10buf_pool = NULL;
1077}
1078
NeilBrown6d508242005-09-09 16:24:03 -07001079/* check if there are enough drives for
1080 * every block to appear on atleast one
1081 */
1082static int enough(conf_t *conf)
1083{
1084 int first = 0;
1085
1086 do {
1087 int n = conf->copies;
1088 int cnt = 0;
1089 while (n--) {
1090 if (conf->mirrors[first].rdev)
1091 cnt++;
1092 first = (first+1) % conf->raid_disks;
1093 }
1094 if (cnt == 0)
1095 return 0;
1096 } while (first != 0);
1097 return 1;
1098}
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100static int raid10_spare_active(mddev_t *mddev)
1101{
1102 int i;
1103 conf_t *conf = mddev->private;
1104 mirror_info_t *tmp;
1105
1106 /*
1107 * Find all non-in_sync disks within the RAID10 configuration
1108 * and mark them in_sync
1109 */
1110 for (i = 0; i < conf->raid_disks; i++) {
1111 tmp = conf->mirrors + i;
1112 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001113 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001114 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1115 unsigned long flags;
1116 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001118 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 }
1121
1122 print_conf(conf);
1123 return 0;
1124}
1125
1126
1127static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1128{
1129 conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10001130 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int mirror;
1132 mirror_info_t *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10001133 int first = 0;
1134 int last = mddev->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (mddev->recovery_cp < MaxSector)
1137 /* only hot-add to in-sync arrays, as recovery is
1138 * very different from resync
1139 */
Neil Brown199050e2008-06-28 08:31:33 +10001140 return -EBUSY;
NeilBrown6d508242005-09-09 16:24:03 -07001141 if (!enough(conf))
Neil Brown199050e2008-06-28 08:31:33 +10001142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
NeilBrowna53a6c82008-11-06 17:28:20 +11001144 if (rdev->raid_disk >= 0)
Neil Brown6c2fce22008-06-28 08:31:31 +10001145 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
NeilBrown6cce3b22006-01-06 00:20:16 -08001147 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10001148 rdev->saved_raid_disk >= first &&
NeilBrown6cce3b22006-01-06 00:20:16 -08001149 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1150 mirror = rdev->saved_raid_disk;
1151 else
Neil Brown6c2fce22008-06-28 08:31:31 +10001152 mirror = first;
1153 for ( ; mirror <= last ; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if ( !(p=conf->mirrors+mirror)->rdev) {
1155
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10001156 disk_stack_limits(mddev->gendisk, rdev->bdev,
1157 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /* as we don't honour merge_bvec_fn, we must never risk
1159 * violating it, so limit ->max_sector to one PAGE, as
1160 * a one page request is never in violation.
1161 */
1162 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
Martin K. Petersenae03bf62009-05-22 17:17:50 -04001163 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
1164 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 p->head_position = 0;
1167 rdev->raid_disk = mirror;
Neil Brown199050e2008-06-28 08:31:33 +10001168 err = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001169 if (rdev->saved_raid_disk != mirror)
1170 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001171 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 }
1174
Andre Nollac5e7112009-08-03 10:59:47 +10001175 md_integrity_add_rdev(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 print_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10001177 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180static int raid10_remove_disk(mddev_t *mddev, int number)
1181{
1182 conf_t *conf = mddev->private;
1183 int err = 0;
1184 mdk_rdev_t *rdev;
1185 mirror_info_t *p = conf->mirrors+ number;
1186
1187 print_conf(conf);
1188 rdev = p->rdev;
1189 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001190 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 atomic_read(&rdev->nr_pending)) {
1192 err = -EBUSY;
1193 goto abort;
1194 }
NeilBrowndfc70642008-05-23 13:04:39 -07001195 /* Only remove faulty devices in recovery
1196 * is not possible.
1197 */
1198 if (!test_bit(Faulty, &rdev->flags) &&
1199 enough(conf)) {
1200 err = -EBUSY;
1201 goto abort;
1202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001204 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (atomic_read(&rdev->nr_pending)) {
1206 /* lost the race, try later */
1207 err = -EBUSY;
1208 p->rdev = rdev;
Andre Nollac5e7112009-08-03 10:59:47 +10001209 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Andre Nollac5e7112009-08-03 10:59:47 +10001211 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213abort:
1214
1215 print_conf(conf);
1216 return err;
1217}
1218
1219
NeilBrown6712ecf2007-09-27 12:47:43 +02001220static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
NeilBrown070ec552009-06-16 16:54:21 +10001223 conf_t *conf = r10_bio->mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int i,d;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 for (i=0; i<conf->copies; i++)
1227 if (r10_bio->devs[i].bio == bio)
1228 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001229 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 update_head_pos(i, r10_bio);
1231 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001232
1233 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1234 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001235 else {
1236 atomic_add(r10_bio->sectors,
1237 &conf->mirrors[d].rdev->corrected_errors);
1238 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1239 md_error(r10_bio->mddev,
1240 conf->mirrors[d].rdev);
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* for reconstruct, we always reschedule after a read.
1244 * for resync, only after all reads
1245 */
NeilBrown73d5c382009-02-25 13:18:47 +11001246 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1248 atomic_dec_and_test(&r10_bio->remaining)) {
1249 /* we have read all the blocks,
1250 * do the comparison in process context in raid10d
1251 */
1252 reschedule_retry(r10_bio);
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
NeilBrown6712ecf2007-09-27 12:47:43 +02001256static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1259 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1260 mddev_t *mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001261 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int i,d;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 for (i = 0; i < conf->copies; i++)
1265 if (r10_bio->devs[i].bio == bio)
1266 break;
1267 d = r10_bio->devs[i].devnum;
1268
1269 if (!uptodate)
1270 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 update_head_pos(i, r10_bio);
1273
NeilBrown73d5c382009-02-25 13:18:47 +11001274 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 while (atomic_dec_and_test(&r10_bio->remaining)) {
1276 if (r10_bio->master_bio == NULL) {
1277 /* the primary of several recovery bios */
NeilBrown73d5c382009-02-25 13:18:47 +11001278 sector_t s = r10_bio->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 put_buf(r10_bio);
NeilBrown73d5c382009-02-25 13:18:47 +11001280 md_done_sync(mddev, s, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 } else {
1283 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1284 put_buf(r10_bio);
1285 r10_bio = r10_bio2;
1286 }
1287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290/*
1291 * Note: sync and recover and handled very differently for raid10
1292 * This code is for resync.
1293 * For resync, we read through virtual addresses and read all blocks.
1294 * If there is any error, we schedule a write. The lowest numbered
1295 * drive is authoritative.
1296 * However requests come for physical address, so we need to map.
1297 * For every physical address there are raid_disks/copies virtual addresses,
1298 * which is always are least one, but is not necessarly an integer.
1299 * This means that a physical address can span multiple chunks, so we may
1300 * have to submit multiple io requests for a single sync request.
1301 */
1302/*
1303 * We check if all blocks are in-sync and only write to blocks that
1304 * aren't in sync
1305 */
1306static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1307{
NeilBrown070ec552009-06-16 16:54:21 +10001308 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int i, first;
1310 struct bio *tbio, *fbio;
1311
1312 atomic_set(&r10_bio->remaining, 1);
1313
1314 /* find the first device with a block */
1315 for (i=0; i<conf->copies; i++)
1316 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1317 break;
1318
1319 if (i == conf->copies)
1320 goto done;
1321
1322 first = i;
1323 fbio = r10_bio->devs[i].bio;
1324
1325 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001326 for (i=0 ; i < conf->copies ; i++) {
1327 int j, d;
1328 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001331
1332 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001334 if (i == first)
1335 continue;
1336 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1337 /* We know that the bi_io_vec layout is the same for
1338 * both 'first' and 'i', so we just compare them.
1339 * All vec entries are PAGE_SIZE;
1340 */
1341 for (j = 0; j < vcnt; j++)
1342 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1343 page_address(tbio->bi_io_vec[j].bv_page),
1344 PAGE_SIZE))
1345 break;
1346 if (j == vcnt)
1347 continue;
1348 mddev->resync_mismatches += r10_bio->sectors;
1349 }
NeilBrown18f08812006-01-06 00:20:25 -08001350 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1351 /* Don't fix anything. */
1352 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* Ok, we need to write this bio
1354 * First we need to fixup bv_offset, bv_len and
1355 * bi_vecs, as the read request might have corrupted these
1356 */
1357 tbio->bi_vcnt = vcnt;
1358 tbio->bi_size = r10_bio->sectors << 9;
1359 tbio->bi_idx = 0;
1360 tbio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1362 tbio->bi_flags |= 1 << BIO_UPTODATE;
1363 tbio->bi_next = NULL;
1364 tbio->bi_rw = WRITE;
1365 tbio->bi_private = r10_bio;
1366 tbio->bi_sector = r10_bio->devs[i].addr;
1367
1368 for (j=0; j < vcnt ; j++) {
1369 tbio->bi_io_vec[j].bv_offset = 0;
1370 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1371
1372 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1373 page_address(fbio->bi_io_vec[j].bv_page),
1374 PAGE_SIZE);
1375 }
1376 tbio->bi_end_io = end_sync_write;
1377
1378 d = r10_bio->devs[i].devnum;
1379 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1380 atomic_inc(&r10_bio->remaining);
1381 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1382
1383 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1384 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1385 generic_make_request(tbio);
1386 }
1387
1388done:
1389 if (atomic_dec_and_test(&r10_bio->remaining)) {
1390 md_done_sync(mddev, r10_bio->sectors, 1);
1391 put_buf(r10_bio);
1392 }
1393}
1394
1395/*
1396 * Now for the recovery code.
1397 * Recovery happens across physical sectors.
1398 * We recover all non-is_sync drives by finding the virtual address of
1399 * each, and then choose a working drive that also has that virt address.
1400 * There is a separate r10_bio for each non-in_sync drive.
1401 * Only the first two slots are in use. The first for reading,
1402 * The second for writing.
1403 *
1404 */
1405
1406static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1407{
NeilBrown070ec552009-06-16 16:54:21 +10001408 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int i, d;
1410 struct bio *bio, *wbio;
1411
1412
1413 /* move the pages across to the second bio
1414 * and submit the write request
1415 */
1416 bio = r10_bio->devs[0].bio;
1417 wbio = r10_bio->devs[1].bio;
1418 for (i=0; i < wbio->bi_vcnt; i++) {
1419 struct page *p = bio->bi_io_vec[i].bv_page;
1420 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1421 wbio->bi_io_vec[i].bv_page = p;
1422 }
1423 d = r10_bio->devs[1].devnum;
1424
1425 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1426 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001427 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1428 generic_make_request(wbio);
1429 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001430 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433
1434/*
1435 * This is a kernel thread which:
1436 *
1437 * 1. Retries failed read operations on working mirrors.
1438 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001439 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 */
1441
NeilBrown6814d532006-10-03 01:15:45 -07001442static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1443{
1444 int sect = 0; /* Offset from r10_bio->sector */
1445 int sectors = r10_bio->sectors;
1446 mdk_rdev_t*rdev;
1447 while(sectors) {
1448 int s = sectors;
1449 int sl = r10_bio->read_slot;
1450 int success = 0;
1451 int start;
1452
1453 if (s > (PAGE_SIZE>>9))
1454 s = PAGE_SIZE >> 9;
1455
1456 rcu_read_lock();
1457 do {
1458 int d = r10_bio->devs[sl].devnum;
1459 rdev = rcu_dereference(conf->mirrors[d].rdev);
1460 if (rdev &&
1461 test_bit(In_sync, &rdev->flags)) {
1462 atomic_inc(&rdev->nr_pending);
1463 rcu_read_unlock();
1464 success = sync_page_io(rdev->bdev,
1465 r10_bio->devs[sl].addr +
1466 sect + rdev->data_offset,
1467 s<<9,
1468 conf->tmppage, READ);
1469 rdev_dec_pending(rdev, mddev);
1470 rcu_read_lock();
1471 if (success)
1472 break;
1473 }
1474 sl++;
1475 if (sl == conf->copies)
1476 sl = 0;
1477 } while (!success && sl != r10_bio->read_slot);
1478 rcu_read_unlock();
1479
1480 if (!success) {
1481 /* Cannot read from anywhere -- bye bye array */
1482 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1483 md_error(mddev, conf->mirrors[dn].rdev);
1484 break;
1485 }
1486
1487 start = sl;
1488 /* write it back and re-read */
1489 rcu_read_lock();
1490 while (sl != r10_bio->read_slot) {
1491 int d;
1492 if (sl==0)
1493 sl = conf->copies;
1494 sl--;
1495 d = r10_bio->devs[sl].devnum;
1496 rdev = rcu_dereference(conf->mirrors[d].rdev);
1497 if (rdev &&
1498 test_bit(In_sync, &rdev->flags)) {
1499 atomic_inc(&rdev->nr_pending);
1500 rcu_read_unlock();
1501 atomic_add(s, &rdev->corrected_errors);
1502 if (sync_page_io(rdev->bdev,
1503 r10_bio->devs[sl].addr +
1504 sect + rdev->data_offset,
1505 s<<9, conf->tmppage, WRITE)
1506 == 0)
1507 /* Well, this device is dead */
1508 md_error(mddev, rdev);
1509 rdev_dec_pending(rdev, mddev);
1510 rcu_read_lock();
1511 }
1512 }
1513 sl = start;
1514 while (sl != r10_bio->read_slot) {
1515 int d;
1516 if (sl==0)
1517 sl = conf->copies;
1518 sl--;
1519 d = r10_bio->devs[sl].devnum;
1520 rdev = rcu_dereference(conf->mirrors[d].rdev);
1521 if (rdev &&
1522 test_bit(In_sync, &rdev->flags)) {
1523 char b[BDEVNAME_SIZE];
1524 atomic_inc(&rdev->nr_pending);
1525 rcu_read_unlock();
1526 if (sync_page_io(rdev->bdev,
1527 r10_bio->devs[sl].addr +
1528 sect + rdev->data_offset,
1529 s<<9, conf->tmppage, READ) == 0)
1530 /* Well, this device is dead */
1531 md_error(mddev, rdev);
1532 else
1533 printk(KERN_INFO
1534 "raid10:%s: read error corrected"
1535 " (%d sectors at %llu on %s)\n",
1536 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001537 (unsigned long long)(sect+
1538 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001539 bdevname(rdev->bdev, b));
1540
1541 rdev_dec_pending(rdev, mddev);
1542 rcu_read_lock();
1543 }
1544 }
1545 rcu_read_unlock();
1546
1547 sectors -= s;
1548 sect += s;
1549 }
1550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552static void raid10d(mddev_t *mddev)
1553{
1554 r10bio_t *r10_bio;
1555 struct bio *bio;
1556 unsigned long flags;
NeilBrown070ec552009-06-16 16:54:21 +10001557 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 struct list_head *head = &conf->retry_list;
1559 int unplug=0;
1560 mdk_rdev_t *rdev;
1561
1562 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 for (;;) {
1565 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001566
1567 unplug += flush_pending_writes(conf);
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001570 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001571 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1575 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001576 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 spin_unlock_irqrestore(&conf->device_lock, flags);
1578
1579 mddev = r10_bio->mddev;
NeilBrown070ec552009-06-16 16:54:21 +10001580 conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1582 sync_request_write(mddev, r10_bio);
1583 unplug = 1;
1584 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1585 recovery_request_write(mddev, r10_bio);
1586 unplug = 1;
1587 } else {
1588 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001589 /* we got a read error. Maybe the drive is bad. Maybe just
1590 * the block and we can fix it.
1591 * We freeze all other IO, and try reading the block from
1592 * other devices. When we find one, we re-write
1593 * and check it that fixes the read error.
1594 * This is all done synchronously while the array is
1595 * frozen.
1596 */
NeilBrown6814d532006-10-03 01:15:45 -07001597 if (mddev->ro == 0) {
1598 freeze_array(conf);
1599 fix_read_error(conf, mddev, r10_bio);
1600 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001601 }
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001604 r10_bio->devs[r10_bio->read_slot].bio =
1605 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 mirror = read_balance(conf, r10_bio);
1607 if (mirror == -1) {
1608 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1609 " read error for block %llu\n",
1610 bdevname(bio->bi_bdev,b),
1611 (unsigned long long)r10_bio->sector);
1612 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001613 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 } else {
Jens Axboe1f98a132009-09-11 14:32:04 +02001615 const bool do_sync = bio_rw_flagged(r10_bio->master_bio, BIO_RW_SYNCIO);
Maik Hampel14e71342007-07-31 00:37:57 -07001616 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 rdev = conf->mirrors[mirror].rdev;
1618 if (printk_ratelimit())
1619 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1620 " another mirror\n",
1621 bdevname(rdev->bdev,b),
1622 (unsigned long long)r10_bio->sector);
1623 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1624 r10_bio->devs[r10_bio->read_slot].bio = bio;
1625 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1626 + rdev->data_offset;
1627 bio->bi_bdev = rdev->bdev;
Dmitry Monakhov1ef04fe2009-09-20 05:52:25 +04001628 bio->bi_rw = READ | (do_sync << BIO_RW_SYNCIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 bio->bi_private = r10_bio;
1630 bio->bi_end_io = raid10_end_read_request;
1631 unplug = 1;
1632 generic_make_request(bio);
1633 }
1634 }
NeilBrown1d9d5242009-10-16 15:55:32 +11001635 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (unplug)
1638 unplug_slaves(mddev);
1639}
1640
1641
1642static int init_resync(conf_t *conf)
1643{
1644 int buffs;
1645
1646 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001647 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1649 if (!conf->r10buf_pool)
1650 return -ENOMEM;
1651 conf->next_resync = 0;
1652 return 0;
1653}
1654
1655/*
1656 * perform a "sync" on one "block"
1657 *
1658 * We need to make sure that no normal I/O request - particularly write
1659 * requests - conflict with active sync requests.
1660 *
1661 * This is achieved by tracking pending requests and a 'barrier' concept
1662 * that can be installed to exclude normal IO requests.
1663 *
1664 * Resync and recovery are handled very differently.
1665 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1666 *
1667 * For resync, we iterate over virtual addresses, read all copies,
1668 * and update if there are differences. If only one copy is live,
1669 * skip it.
1670 * For recovery, we iterate over physical addresses, read a good
1671 * value for each non-in_sync drive, and over-write.
1672 *
1673 * So, for recovery we may have several outstanding complex requests for a
1674 * given address, one for each out-of-sync device. We model this by allocating
1675 * a number of r10_bio structures, one for each out-of-sync device.
1676 * As we setup these structures, we collect all bio's together into a list
1677 * which we then process collectively to add pages, and then process again
1678 * to pass to generic_make_request.
1679 *
1680 * The r10_bio structures are linked using a borrowed master_bio pointer.
1681 * This link is counted in ->remaining. When the r10_bio that points to NULL
1682 * has its remaining count decremented to 0, the whole complex operation
1683 * is complete.
1684 *
1685 */
1686
NeilBrown57afd892005-06-21 17:17:13 -07001687static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
NeilBrown070ec552009-06-16 16:54:21 +10001689 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 r10bio_t *r10_bio;
1691 struct bio *biolist = NULL, *bio;
1692 sector_t max_sector, nr_sectors;
1693 int disk;
1694 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001695 int max_sync;
1696 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 sector_t sectors_skipped = 0;
1699 int chunks_skipped = 0;
1700
1701 if (!conf->r10buf_pool)
1702 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 skipped:
Andre Noll58c0fed2009-03-31 14:33:13 +11001706 max_sector = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1708 max_sector = mddev->resync_max_sectors;
1709 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001710 /* If we aborted, we need to abort the
1711 * sync on the 'current' bitmap chucks (there can
1712 * be several when recovering multiple devices).
1713 * as we may have started syncing it but not finished.
1714 * We can find the current address in
1715 * mddev->curr_resync, but for recovery,
1716 * we need to convert that to several
1717 * virtual addresses.
1718 */
1719 if (mddev->curr_resync < max_sector) { /* aborted */
1720 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1721 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1722 &sync_blocks, 1);
1723 else for (i=0; i<conf->raid_disks; i++) {
1724 sector_t sect =
1725 raid10_find_virt(conf, mddev->curr_resync, i);
1726 bitmap_end_sync(mddev->bitmap, sect,
1727 &sync_blocks, 1);
1728 }
1729 } else /* completed sync */
1730 conf->fullsync = 0;
1731
1732 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001734 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return sectors_skipped;
1736 }
1737 if (chunks_skipped >= conf->raid_disks) {
1738 /* if there has been nothing to do on any drive,
1739 * then there is nothing to do at all..
1740 */
NeilBrown57afd892005-06-21 17:17:13 -07001741 *skipped = 1;
1742 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
NeilBrownc6207272008-02-06 01:39:52 -08001745 if (max_sector > mddev->resync_max)
1746 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* make sure whole request will fit in a chunk - if chunks
1749 * are meaningful
1750 */
1751 if (conf->near_copies < conf->raid_disks &&
1752 max_sector > (sector_nr | conf->chunk_mask))
1753 max_sector = (sector_nr | conf->chunk_mask) + 1;
1754 /*
1755 * If there is non-resync activity waiting for us then
1756 * put in a delay to throttle resync.
1757 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001758 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /* Again, very different code for resync and recovery.
1762 * Both must result in an r10bio with a list of bios that
1763 * have bi_end_io, bi_sector, bi_bdev set,
1764 * and bi_private set to the r10bio.
1765 * For recovery, we may actually create several r10bios
1766 * with 2 bios in each, that correspond to the bios in the main one.
1767 * In this case, the subordinate r10bios link back through a
1768 * borrowed master_bio pointer, and the counter in the master
1769 * includes a ref from each subordinate.
1770 */
1771 /* First, we decide what to do and set ->bi_end_io
1772 * To end_sync_read if we want to read, and
1773 * end_sync_write if we will want to write.
1774 */
1775
NeilBrown6cce3b22006-01-06 00:20:16 -08001776 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1778 /* recovery... the complicated one */
NeilBrowna9f326e2009-09-23 18:06:41 +10001779 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 r10_bio = NULL;
1781
1782 for (i=0 ; i<conf->raid_disks; i++)
1783 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001784 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001785 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /* want to reconstruct this device */
1787 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001788 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1789 int must_sync;
1790 /* Unless we are doing a full sync, we only need
1791 * to recover the block if it is set in the bitmap
1792 */
1793 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1794 &sync_blocks, 1);
1795 if (sync_blocks < max_sync)
1796 max_sync = sync_blocks;
1797 if (!must_sync &&
1798 !conf->fullsync) {
1799 /* yep, skip the sync_blocks here, but don't assume
1800 * that there will never be anything to do here
1801 */
1802 chunks_skipped = -1;
1803 continue;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001807 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 atomic_set(&r10_bio->remaining, 0);
1809
1810 r10_bio->master_bio = (struct bio*)rb2;
1811 if (rb2)
1812 atomic_inc(&rb2->remaining);
1813 r10_bio->mddev = mddev;
1814 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001815 r10_bio->sector = sect;
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 raid10_find_phys(conf, r10_bio);
NeilBrown18055562009-05-07 12:48:10 +10001818
1819 /* Need to check if the array will still be
NeilBrown6cce3b22006-01-06 00:20:16 -08001820 * degraded
1821 */
NeilBrown18055562009-05-07 12:48:10 +10001822 for (j=0; j<conf->raid_disks; j++)
1823 if (conf->mirrors[j].rdev == NULL ||
1824 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001825 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001826 break;
1827 }
NeilBrown18055562009-05-07 12:48:10 +10001828
NeilBrown6cce3b22006-01-06 00:20:16 -08001829 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1830 &sync_blocks, still_degraded);
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 for (j=0; j<conf->copies;j++) {
1833 int d = r10_bio->devs[j].devnum;
1834 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001835 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /* This is where we read from */
1837 bio = r10_bio->devs[0].bio;
1838 bio->bi_next = biolist;
1839 biolist = bio;
1840 bio->bi_private = r10_bio;
1841 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001842 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 bio->bi_sector = r10_bio->devs[j].addr +
1844 conf->mirrors[d].rdev->data_offset;
1845 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1846 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1847 atomic_inc(&r10_bio->remaining);
1848 /* and we write to 'i' */
1849
1850 for (k=0; k<conf->copies; k++)
1851 if (r10_bio->devs[k].devnum == i)
1852 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001853 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 bio = r10_bio->devs[1].bio;
1855 bio->bi_next = biolist;
1856 biolist = bio;
1857 bio->bi_private = r10_bio;
1858 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001859 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 bio->bi_sector = r10_bio->devs[k].addr +
1861 conf->mirrors[i].rdev->data_offset;
1862 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1863
1864 r10_bio->devs[0].devnum = d;
1865 r10_bio->devs[1].devnum = i;
1866
1867 break;
1868 }
1869 }
1870 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001871 /* Cannot recover, so abort the recovery */
1872 put_buf(r10_bio);
K.Tanakaa07e6ab2008-03-04 14:29:37 -08001873 if (rb2)
1874 atomic_dec(&rb2->remaining);
NeilBrown87fc7672005-09-09 16:24:04 -07001875 r10_bio = rb2;
NeilBrowndfc70642008-05-23 13:04:39 -07001876 if (!test_and_set_bit(MD_RECOVERY_INTR,
1877 &mddev->recovery))
NeilBrown87fc7672005-09-09 16:24:04 -07001878 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1879 mdname(mddev));
1880 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882 }
1883 if (biolist == NULL) {
1884 while (r10_bio) {
1885 r10bio_t *rb2 = r10_bio;
1886 r10_bio = (r10bio_t*) rb2->master_bio;
1887 rb2->master_bio = NULL;
1888 put_buf(rb2);
1889 }
1890 goto giveup;
1891 }
1892 } else {
1893 /* resync. Schedule a read for every block at this virt offset */
1894 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001895
NeilBrown78200d42009-02-25 13:18:47 +11001896 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1897
NeilBrown6cce3b22006-01-06 00:20:16 -08001898 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1899 &sync_blocks, mddev->degraded) &&
1900 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1901 /* We can skip this block */
1902 *skipped = 1;
1903 return sync_blocks + sectors_skipped;
1904 }
1905 if (sync_blocks < max_sync)
1906 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 r10_bio->mddev = mddev;
1910 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001911 raise_barrier(conf, 0);
1912 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 r10_bio->master_bio = NULL;
1915 r10_bio->sector = sector_nr;
1916 set_bit(R10BIO_IsSync, &r10_bio->state);
1917 raid10_find_phys(conf, r10_bio);
1918 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1919
1920 for (i=0; i<conf->copies; i++) {
1921 int d = r10_bio->devs[i].devnum;
1922 bio = r10_bio->devs[i].bio;
1923 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001924 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001926 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 continue;
1928 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1929 atomic_inc(&r10_bio->remaining);
1930 bio->bi_next = biolist;
1931 biolist = bio;
1932 bio->bi_private = r10_bio;
1933 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001934 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 bio->bi_sector = r10_bio->devs[i].addr +
1936 conf->mirrors[d].rdev->data_offset;
1937 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1938 count++;
1939 }
1940
1941 if (count < 2) {
1942 for (i=0; i<conf->copies; i++) {
1943 int d = r10_bio->devs[i].devnum;
1944 if (r10_bio->devs[i].bio->bi_end_io)
1945 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1946 }
1947 put_buf(r10_bio);
1948 biolist = NULL;
1949 goto giveup;
1950 }
1951 }
1952
1953 for (bio = biolist; bio ; bio=bio->bi_next) {
1954
1955 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1956 if (bio->bi_end_io)
1957 bio->bi_flags |= 1 << BIO_UPTODATE;
1958 bio->bi_vcnt = 0;
1959 bio->bi_idx = 0;
1960 bio->bi_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 bio->bi_size = 0;
1962 }
1963
1964 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001965 if (sector_nr + max_sync < max_sector)
1966 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 do {
1968 struct page *page;
1969 int len = PAGE_SIZE;
1970 disk = 0;
1971 if (sector_nr + (len>>9) > max_sector)
1972 len = (max_sector - sector_nr) << 9;
1973 if (len == 0)
1974 break;
1975 for (bio= biolist ; bio ; bio=bio->bi_next) {
1976 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1977 if (bio_add_page(bio, page, len, 0) == 0) {
1978 /* stop here */
1979 struct bio *bio2;
1980 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1981 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1982 /* remove last page from this bio */
1983 bio2->bi_vcnt--;
1984 bio2->bi_size -= len;
1985 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1986 }
1987 goto bio_full;
1988 }
1989 disk = i;
1990 }
1991 nr_sectors += len>>9;
1992 sector_nr += len>>9;
1993 } while (biolist->bi_vcnt < RESYNC_PAGES);
1994 bio_full:
1995 r10_bio->sectors = nr_sectors;
1996
1997 while (biolist) {
1998 bio = biolist;
1999 biolist = biolist->bi_next;
2000
2001 bio->bi_next = NULL;
2002 r10_bio = bio->bi_private;
2003 r10_bio->sectors = nr_sectors;
2004
2005 if (bio->bi_end_io == end_sync_read) {
2006 md_sync_acct(bio->bi_bdev, nr_sectors);
2007 generic_make_request(bio);
2008 }
2009 }
2010
NeilBrown57afd892005-06-21 17:17:13 -07002011 if (sectors_skipped)
2012 /* pretend they weren't skipped, it makes
2013 * no important difference in this case
2014 */
2015 md_done_sync(mddev, sectors_skipped, 1);
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return sectors_skipped + nr_sectors;
2018 giveup:
2019 /* There is nowhere to write, so all non-sync
2020 * drives must be failed, so try the next chunk...
2021 */
NeilBrown09b40682009-02-25 13:18:47 +11002022 if (sector_nr + max_sync < max_sector)
2023 max_sector = sector_nr + max_sync;
2024
2025 sectors_skipped += (max_sector - sector_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 chunks_skipped ++;
2027 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 goto skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Dan Williams80c3a6c2009-03-17 18:10:40 -07002031static sector_t
2032raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2033{
2034 sector_t size;
NeilBrown070ec552009-06-16 16:54:21 +10002035 conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07002036
2037 if (!raid_disks)
2038 raid_disks = mddev->raid_disks;
2039 if (!sectors)
2040 sectors = mddev->dev_sectors;
2041
2042 size = sectors >> conf->chunk_shift;
2043 sector_div(size, conf->far_copies);
2044 size = size * raid_disks;
2045 sector_div(size, conf->near_copies);
2046
2047 return size << conf->chunk_shift;
2048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050static int run(mddev_t *mddev)
2051{
2052 conf_t *conf;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002053 int i, disk_idx, chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 mirror_info_t *disk;
2055 mdk_rdev_t *rdev;
NeilBrownc93983b2006-06-26 00:27:41 -07002056 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 sector_t stride, size;
2058
Andre Noll9d8f0362009-06-18 08:45:01 +10002059 if (mddev->chunk_sectors < (PAGE_SIZE >> 9) ||
2060 !is_power_of_2(mddev->chunk_sectors)) {
NeilBrown4bbf3772008-10-13 11:55:12 +11002061 printk(KERN_ERR "md/raid10: chunk size must be "
raz ben yehuda964e7912009-06-16 17:01:22 +10002062 "at least PAGE_SIZE(%ld) and be a power of 2.\n", PAGE_SIZE);
NeilBrown2604b702006-01-06 00:20:36 -08002063 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
NeilBrown2604b702006-01-06 00:20:36 -08002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 nc = mddev->layout & 255;
2067 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07002068 fo = mddev->layout & (1<<16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07002070 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2072 mdname(mddev), mddev->layout);
2073 goto out;
2074 }
2075 /*
2076 * copy the already verified devices into our private RAID10
2077 * bookkeeping area. [whatever we allocate in run(),
2078 * should be freed in stop()]
2079 */
NeilBrown4443ae12006-01-06 00:20:28 -08002080 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 mddev->private = conf;
2082 if (!conf) {
2083 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2084 mdname(mddev));
2085 goto out;
2086 }
NeilBrown4443ae12006-01-06 00:20:28 -08002087 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 GFP_KERNEL);
2089 if (!conf->mirrors) {
2090 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2091 mdname(mddev));
2092 goto out_free_conf;
2093 }
NeilBrown4443ae12006-01-06 00:20:28 -08002094
2095 conf->tmppage = alloc_page(GFP_KERNEL);
2096 if (!conf->tmppage)
2097 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
NeilBrown64a742b2007-02-28 20:11:18 -08002099 conf->mddev = mddev;
2100 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 conf->near_copies = nc;
2102 conf->far_copies = fc;
2103 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002104 conf->far_offset = fo;
Andre Noll9d8f0362009-06-18 08:45:01 +10002105 conf->chunk_mask = mddev->chunk_sectors - 1;
2106 conf->chunk_shift = ffz(~mddev->chunk_sectors);
Andre Noll58c0fed2009-03-31 14:33:13 +11002107 size = mddev->dev_sectors >> conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002108 sector_div(size, fc);
2109 size = size * conf->raid_disks;
2110 sector_div(size, nc);
2111 /* 'size' is now the number of chunks in the array */
2112 /* calculate "used chunks per device" in 'stride' */
2113 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002114
2115 /* We need to round up when dividing by raid_disks to
2116 * get the stride size.
2117 */
2118 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002119 sector_div(stride, conf->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11002120 mddev->dev_sectors = stride << conf->chunk_shift;
NeilBrown64a742b2007-02-28 20:11:18 -08002121
NeilBrownc93983b2006-06-26 00:27:41 -07002122 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002123 stride = 1;
2124 else
NeilBrownc93983b2006-06-26 00:27:41 -07002125 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002126 conf->stride = stride << conf->chunk_shift;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2129 r10bio_pool_free, conf);
2130 if (!conf->r10bio_pool) {
2131 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2132 mdname(mddev));
2133 goto out_free_conf;
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Neil Browne7e72bf2008-05-14 16:05:54 -07002136 spin_lock_init(&conf->device_lock);
2137 mddev->queue->queue_lock = &conf->device_lock;
2138
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002139 chunk_size = mddev->chunk_sectors << 9;
2140 blk_queue_io_min(mddev->queue, chunk_size);
2141 if (conf->raid_disks % conf->near_copies)
2142 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2143 else
2144 blk_queue_io_opt(mddev->queue, chunk_size *
2145 (conf->raid_disks / conf->near_copies));
2146
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002147 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 disk_idx = rdev->raid_disk;
2149 if (disk_idx >= mddev->raid_disks
2150 || disk_idx < 0)
2151 continue;
2152 disk = conf->mirrors + disk_idx;
2153
2154 disk->rdev = rdev;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10002155 disk_stack_limits(mddev->gendisk, rdev->bdev,
2156 rdev->data_offset << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /* as we don't honour merge_bvec_fn, we must never risk
2158 * violating it, so limit ->max_sector to one PAGE, as
2159 * a one page request is never in violation.
2160 */
2161 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
Martin K. Petersenae03bf62009-05-22 17:17:50 -04002162 queue_max_sectors(mddev->queue) > (PAGE_SIZE>>9))
2163 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 INIT_LIST_HEAD(&conf->retry_list);
2168
2169 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08002170 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
NeilBrown6d508242005-09-09 16:24:03 -07002172 /* need to check that every block has at least one working mirror */
2173 if (!enough(conf)) {
2174 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2175 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto out_free_conf;
2177 }
2178
2179 mddev->degraded = 0;
2180 for (i = 0; i < conf->raid_disks; i++) {
2181
2182 disk = conf->mirrors + i;
2183
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002184 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002185 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 disk->head_position = 0;
2187 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002188 if (disk->rdev)
2189 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191 }
2192
2193
NeilBrown0da3c612009-09-23 18:09:45 +10002194 mddev->thread = md_register_thread(raid10d, mddev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (!mddev->thread) {
2196 printk(KERN_ERR
2197 "raid10: couldn't allocate thread for %s\n",
2198 mdname(mddev));
2199 goto out_free_conf;
2200 }
2201
Andre Noll8c6ac862009-06-18 08:48:06 +10002202 if (mddev->recovery_cp != MaxSector)
2203 printk(KERN_NOTICE "raid10: %s is not clean"
2204 " -- starting background reconstruction\n",
2205 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 printk(KERN_INFO
2207 "raid10: raid set %s active with %d out of %d devices\n",
2208 mdname(mddev), mddev->raid_disks - mddev->degraded,
2209 mddev->raid_disks);
2210 /*
2211 * Ok, everything is just fine now
2212 */
Dan Williams1f403622009-03-31 14:59:03 +11002213 md_set_array_sectors(mddev, raid10_size(mddev, 0, 0));
Dan Williamsb522adc2009-03-31 15:00:31 +11002214 mddev->resync_max_sectors = raid10_size(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
NeilBrown7a5febe2005-05-16 21:53:16 -07002216 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002217 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2218 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* Calculate max read-ahead size.
2221 * We need to readahead at least twice a whole stripe....
2222 * maybe...
2223 */
2224 {
Andre Noll9d8f0362009-06-18 08:45:01 +10002225 int stripe = conf->raid_disks *
2226 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 stripe /= conf->near_copies;
2228 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2229 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2230 }
2231
2232 if (conf->near_copies < mddev->raid_disks)
2233 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
Andre Nollac5e7112009-08-03 10:59:47 +10002234 md_integrity_register(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 0;
2236
2237out_free_conf:
2238 if (conf->r10bio_pool)
2239 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002240 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002241 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 kfree(conf);
2243 mddev->private = NULL;
2244out:
2245 return -EIO;
2246}
2247
2248static int stop(mddev_t *mddev)
2249{
NeilBrown070ec552009-06-16 16:54:21 +10002250 conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
NeilBrown409c57f2009-03-31 14:39:39 +11002252 raise_barrier(conf, 0);
2253 lower_barrier(conf);
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 md_unregister_thread(mddev->thread);
2256 mddev->thread = NULL;
2257 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2258 if (conf->r10bio_pool)
2259 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002260 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 kfree(conf);
2262 mddev->private = NULL;
2263 return 0;
2264}
2265
NeilBrown6cce3b22006-01-06 00:20:16 -08002266static void raid10_quiesce(mddev_t *mddev, int state)
2267{
NeilBrown070ec552009-06-16 16:54:21 +10002268 conf_t *conf = mddev->private;
NeilBrown6cce3b22006-01-06 00:20:16 -08002269
2270 switch(state) {
2271 case 1:
2272 raise_barrier(conf, 0);
2273 break;
2274 case 0:
2275 lower_barrier(conf);
2276 break;
2277 }
2278 if (mddev->thread) {
2279 if (mddev->bitmap)
2280 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2281 else
2282 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2283 md_wakeup_thread(mddev->thread);
2284 }
2285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
NeilBrown2604b702006-01-06 00:20:36 -08002287static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002290 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 .owner = THIS_MODULE,
2292 .make_request = make_request,
2293 .run = run,
2294 .stop = stop,
2295 .status = status,
2296 .error_handler = error,
2297 .hot_add_disk = raid10_add_disk,
2298 .hot_remove_disk= raid10_remove_disk,
2299 .spare_active = raid10_spare_active,
2300 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002301 .quiesce = raid10_quiesce,
Dan Williams80c3a6c2009-03-17 18:10:40 -07002302 .size = raid10_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303};
2304
2305static int __init raid_init(void)
2306{
NeilBrown2604b702006-01-06 00:20:36 -08002307 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
2310static void raid_exit(void)
2311{
NeilBrown2604b702006-01-06 00:20:36 -08002312 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315module_init(raid_init);
2316module_exit(raid_exit);
2317MODULE_LICENSE("GPL");
2318MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002319MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002320MODULE_ALIAS("md-level-10");