blob: 8e5671d2f3d3cdd1225137f33616d065d1ea80e4 [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
NeilBrown6cce3b22006-01-06 00:20:16 -080021#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/raid/raid10.h>
NeilBrown6cce3b22006-01-06 00:20:16 -080023#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * RAID10 provides a combination of RAID0 and RAID1 functionality.
27 * The layout of data is defined by
28 * chunk_size
29 * raid_disks
30 * near_copies (stored in low byte of layout)
31 * far_copies (stored in second byte of layout)
NeilBrownc93983b2006-06-26 00:27:41 -070032 * far_offset (stored in bit 16 of layout )
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
34 * The data to be stored is divided into chunks using chunksize.
35 * Each device is divided into far_copies sections.
36 * In each section, chunks are laid out in a style similar to raid0, but
37 * near_copies copies of each chunk is stored (each on a different drive).
38 * The starting device for each section is offset near_copies from the starting
39 * device of the previous section.
NeilBrownc93983b2006-06-26 00:27:41 -070040 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * drive.
42 * near_copies and far_copies must be at least one, and their product is at most
43 * raid_disks.
NeilBrownc93983b2006-06-26 00:27:41 -070044 *
45 * If far_offset is true, then the far_copies are handled a bit differently.
46 * The copies are still in different stripes, but instead of be very far apart
47 * on disk, there are adjacent stripes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50/*
51 * Number of guaranteed r10bios in case of extreme VM load:
52 */
53#define NR_RAID10_BIOS 256
54
55static void unplug_slaves(mddev_t *mddev);
56
NeilBrown0a27ec92006-01-06 00:20:13 -080057static void allow_barrier(conf_t *conf);
58static void lower_barrier(conf_t *conf);
59
Al Virodd0fc662005-10-07 07:46:04 +010060static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 conf_t *conf = data;
63 r10bio_t *r10_bio;
64 int size = offsetof(struct r10bio_s, devs[conf->copies]);
65
66 /* allocate a r10bio with room for raid_disks entries in the bios array */
NeilBrown9ffae0c2006-01-06 00:20:32 -080067 r10_bio = kzalloc(size, gfp_flags);
68 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unplug_slaves(conf->mddev);
70
71 return r10_bio;
72}
73
74static void r10bio_pool_free(void *r10_bio, void *data)
75{
76 kfree(r10_bio);
77}
78
79#define RESYNC_BLOCK_SIZE (64*1024)
80//#define RESYNC_BLOCK_SIZE PAGE_SIZE
81#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
82#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
83#define RESYNC_WINDOW (2048*1024)
84
85/*
86 * When performing a resync, we need to read and compare, so
87 * we need as many pages are there are copies.
88 * When performing a recovery, we need 2 bios, one for read,
89 * one for write (we recover only one drive per r10buf)
90 *
91 */
Al Virodd0fc662005-10-07 07:46:04 +010092static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 conf_t *conf = data;
95 struct page *page;
96 r10bio_t *r10_bio;
97 struct bio *bio;
98 int i, j;
99 int nalloc;
100
101 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
102 if (!r10_bio) {
103 unplug_slaves(conf->mddev);
104 return NULL;
105 }
106
107 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
108 nalloc = conf->copies; /* resync */
109 else
110 nalloc = 2; /* recovery */
111
112 /*
113 * Allocate bios.
114 */
115 for (j = nalloc ; j-- ; ) {
116 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
117 if (!bio)
118 goto out_free_bio;
119 r10_bio->devs[j].bio = bio;
120 }
121 /*
122 * Allocate RESYNC_PAGES data pages and attach them
123 * where needed.
124 */
125 for (j = 0 ; j < nalloc; j++) {
126 bio = r10_bio->devs[j].bio;
127 for (i = 0; i < RESYNC_PAGES; i++) {
128 page = alloc_page(gfp_flags);
129 if (unlikely(!page))
130 goto out_free_pages;
131
132 bio->bi_io_vec[i].bv_page = page;
133 }
134 }
135
136 return r10_bio;
137
138out_free_pages:
139 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800140 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 while (j--)
142 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800143 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 j = -1;
145out_free_bio:
146 while ( ++j < nalloc )
147 bio_put(r10_bio->devs[j].bio);
148 r10bio_pool_free(r10_bio, conf);
149 return NULL;
150}
151
152static void r10buf_pool_free(void *__r10_bio, void *data)
153{
154 int i;
155 conf_t *conf = data;
156 r10bio_t *r10bio = __r10_bio;
157 int j;
158
159 for (j=0; j < conf->copies; j++) {
160 struct bio *bio = r10bio->devs[j].bio;
161 if (bio) {
162 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800163 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 bio->bi_io_vec[i].bv_page = NULL;
165 }
166 bio_put(bio);
167 }
168 }
169 r10bio_pool_free(r10bio, conf);
170}
171
172static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
173{
174 int i;
175
176 for (i = 0; i < conf->copies; i++) {
177 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -0800178 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 bio_put(*bio);
180 *bio = NULL;
181 }
182}
183
Arjan van de Ven858119e2006-01-14 13:20:43 -0800184static void free_r10bio(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 conf_t *conf = mddev_to_conf(r10_bio->mddev);
187
188 /*
189 * Wake up any possible resync thread that waits for the device
190 * to go idle.
191 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800192 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 put_all_bios(conf, r10_bio);
195 mempool_free(r10_bio, conf->r10bio_pool);
196}
197
Arjan van de Ven858119e2006-01-14 13:20:43 -0800198static void put_buf(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 conf_t *conf = mddev_to_conf(r10_bio->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 mempool_free(r10_bio, conf->r10buf_pool);
203
NeilBrown0a27ec92006-01-06 00:20:13 -0800204 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207static void reschedule_retry(r10bio_t *r10_bio)
208{
209 unsigned long flags;
210 mddev_t *mddev = r10_bio->mddev;
211 conf_t *conf = mddev_to_conf(mddev);
212
213 spin_lock_irqsave(&conf->device_lock, flags);
214 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800215 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 spin_unlock_irqrestore(&conf->device_lock, flags);
217
218 md_wakeup_thread(mddev->thread);
219}
220
221/*
222 * raid_end_bio_io() is called when we have finished servicing a mirrored
223 * operation and are ready to return a success/failure code to the buffer
224 * cache layer.
225 */
226static void raid_end_bio_io(r10bio_t *r10_bio)
227{
228 struct bio *bio = r10_bio->master_bio;
229
NeilBrown6712ecf2007-09-27 12:47:43 +0200230 bio_endio(bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
232 free_r10bio(r10_bio);
233}
234
235/*
236 * Update disk head position estimator based on IRQ completion info.
237 */
238static inline void update_head_pos(int slot, r10bio_t *r10_bio)
239{
240 conf_t *conf = mddev_to_conf(r10_bio->mddev);
241
242 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
243 r10_bio->devs[slot].addr + (r10_bio->sectors);
244}
245
NeilBrown6712ecf2007-09-27 12:47:43 +0200246static void raid10_end_read_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
249 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
250 int slot, dev;
251 conf_t *conf = mddev_to_conf(r10_bio->mddev);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 slot = r10_bio->read_slot;
255 dev = r10_bio->devs[slot].devnum;
256 /*
257 * this branch is our 'one mirror IO has finished' event handler:
258 */
NeilBrown4443ae12006-01-06 00:20:28 -0800259 update_head_pos(slot, r10_bio);
260
261 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * Set R10BIO_Uptodate in our master bio, so that
264 * we will return a good error code to the higher
265 * levels even if IO on some other mirrored buffer fails.
266 *
267 * The 'master' represents the composite IO operation to
268 * user-side. So if something waits for IO, then it will
269 * wait for the 'master' bio.
270 */
271 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 raid_end_bio_io(r10_bio);
NeilBrown4443ae12006-01-06 00:20:28 -0800273 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
275 * oops, read error:
276 */
277 char b[BDEVNAME_SIZE];
278 if (printk_ratelimit())
279 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
280 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
281 reschedule_retry(r10_bio);
282 }
283
284 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
NeilBrown6712ecf2007-09-27 12:47:43 +0200287static void raid10_end_write_request(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
290 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
291 int slot, dev;
292 conf_t *conf = mddev_to_conf(r10_bio->mddev);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 for (slot = 0; slot < conf->copies; slot++)
295 if (r10_bio->devs[slot].bio == bio)
296 break;
297 dev = r10_bio->devs[slot].devnum;
298
299 /*
300 * this branch is our 'one mirror IO has finished' event handler:
301 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800302 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800304 /* an I/O failed, we can't clear the bitmap */
305 set_bit(R10BIO_Degraded, &r10_bio->state);
306 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /*
308 * Set R10BIO_Uptodate in our master bio, so that
309 * we will return a good error code for to the higher
310 * levels even if IO on some other mirrored buffer fails.
311 *
312 * The 'master' represents the composite IO operation to
313 * user-side. So if something waits for IO, then it will
314 * wait for the 'master' bio.
315 */
316 set_bit(R10BIO_Uptodate, &r10_bio->state);
317
318 update_head_pos(slot, r10_bio);
319
320 /*
321 *
322 * Let's see if all mirrored write operations have finished
323 * already.
324 */
325 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800326 /* clear the bitmap if all writes complete successfully */
327 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
328 r10_bio->sectors,
329 !test_bit(R10BIO_Degraded, &r10_bio->state),
330 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 md_write_end(r10_bio->mddev);
332 raid_end_bio_io(r10_bio);
333 }
334
335 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338
339/*
340 * RAID10 layout manager
341 * Aswell as the chunksize and raid_disks count, there are two
342 * parameters: near_copies and far_copies.
343 * near_copies * far_copies must be <= raid_disks.
344 * Normally one of these will be 1.
345 * If both are 1, we get raid0.
346 * If near_copies == raid_disks, we get raid1.
347 *
348 * Chunks are layed out in raid0 style with near_copies copies of the
349 * first chunk, followed by near_copies copies of the next chunk and
350 * so on.
351 * If far_copies > 1, then after 1/far_copies of the array has been assigned
352 * as described above, we start again with a device offset of near_copies.
353 * So we effectively have another copy of the whole array further down all
354 * the drives, but with blocks on different drives.
355 * With this layout, and block is never stored twice on the one device.
356 *
357 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700358 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
360 * raid10_find_virt does the reverse mapping, from a device and a
361 * sector offset to a virtual address
362 */
363
364static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
365{
366 int n,f;
367 sector_t sector;
368 sector_t chunk;
369 sector_t stripe;
370 int dev;
371
372 int slot = 0;
373
374 /* now calculate first sector/dev */
375 chunk = r10bio->sector >> conf->chunk_shift;
376 sector = r10bio->sector & conf->chunk_mask;
377
378 chunk *= conf->near_copies;
379 stripe = chunk;
380 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700381 if (conf->far_offset)
382 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 sector += stripe << conf->chunk_shift;
385
386 /* and calculate all the others */
387 for (n=0; n < conf->near_copies; n++) {
388 int d = dev;
389 sector_t s = sector;
390 r10bio->devs[slot].addr = sector;
391 r10bio->devs[slot].devnum = d;
392 slot++;
393
394 for (f = 1; f < conf->far_copies; f++) {
395 d += conf->near_copies;
396 if (d >= conf->raid_disks)
397 d -= conf->raid_disks;
398 s += conf->stride;
399 r10bio->devs[slot].devnum = d;
400 r10bio->devs[slot].addr = s;
401 slot++;
402 }
403 dev++;
404 if (dev >= conf->raid_disks) {
405 dev = 0;
406 sector += (conf->chunk_mask + 1);
407 }
408 }
409 BUG_ON(slot != conf->copies);
410}
411
412static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
413{
414 sector_t offset, chunk, vchunk;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700417 if (conf->far_offset) {
418 int fc;
419 chunk = sector >> conf->chunk_shift;
420 fc = sector_div(chunk, conf->far_copies);
421 dev -= fc * conf->near_copies;
422 if (dev < 0)
423 dev += conf->raid_disks;
424 } else {
NeilBrown64a742b2007-02-28 20:11:18 -0800425 while (sector >= conf->stride) {
NeilBrownc93983b2006-06-26 00:27:41 -0700426 sector -= conf->stride;
427 if (dev < conf->near_copies)
428 dev += conf->raid_disks - conf->near_copies;
429 else
430 dev -= conf->near_copies;
431 }
432 chunk = sector >> conf->chunk_shift;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 vchunk = chunk * conf->raid_disks + dev;
435 sector_div(vchunk, conf->near_copies);
436 return (vchunk << conf->chunk_shift) + offset;
437}
438
439/**
440 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
441 * @q: request queue
442 * @bio: the buffer head that's been built up so far
443 * @biovec: the request that could be merged to it.
444 *
445 * Return amount of bytes we can accept at this offset
446 * If near_copies == raid_disk, there are no striping issues,
447 * but in that case, the function isn't called at all.
448 */
Jens Axboe165125e2007-07-24 09:28:11 +0200449static int raid10_mergeable_bvec(struct request_queue *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct bio_vec *bio_vec)
451{
452 mddev_t *mddev = q->queuedata;
453 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
454 int max;
455 unsigned int chunk_sectors = mddev->chunk_size >> 9;
456 unsigned int bio_sectors = bio->bi_size >> 9;
457
458 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
459 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
460 if (max <= bio_vec->bv_len && bio_sectors == 0)
461 return bio_vec->bv_len;
462 else
463 return max;
464}
465
466/*
467 * This routine returns the disk from which the requested read should
468 * be done. There is a per-array 'next expected sequential IO' sector
469 * number - if this matches on the next IO then we use the last disk.
470 * There is also a per-disk 'last know head position' sector that is
471 * maintained from IRQ contexts, both the normal and the resync IO
472 * completion handlers update this position correctly. If there is no
473 * perfect sequential match then we pick the disk whose head is closest.
474 *
475 * If there are 2 mirrors in the same 2 devices, performance degrades
476 * because position is mirror, not device based.
477 *
478 * The rdev for the device selected will have nr_pending incremented.
479 */
480
481/*
482 * FIXME: possibly should rethink readbalancing and do it differently
483 * depending on near_copies / far_copies geometry.
484 */
485static int read_balance(conf_t *conf, r10bio_t *r10_bio)
486{
487 const unsigned long this_sector = r10_bio->sector;
488 int disk, slot, nslot;
489 const int sectors = r10_bio->sectors;
490 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800491 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 raid10_find_phys(conf, r10_bio);
494 rcu_read_lock();
495 /*
496 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800497 * device if no resync is going on (recovery is ok), or below
498 * the resync window. We take the first readable disk when
499 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
501 if (conf->mddev->recovery_cp < MaxSector
502 && (this_sector + sectors >= conf->next_resync)) {
503 /* make sure that disk is operational */
504 slot = 0;
505 disk = r10_bio->devs[slot].devnum;
506
Suzanne Woodd6065f72005-11-08 21:39:27 -0800507 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800508 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800509 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 slot++;
511 if (slot == conf->copies) {
512 slot = 0;
513 disk = -1;
514 break;
515 }
516 disk = r10_bio->devs[slot].devnum;
517 }
518 goto rb_out;
519 }
520
521
522 /* make sure the disk is operational */
523 slot = 0;
524 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800525 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800526 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800527 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 slot ++;
529 if (slot == conf->copies) {
530 disk = -1;
531 goto rb_out;
532 }
533 disk = r10_bio->devs[slot].devnum;
534 }
535
536
NeilBrown3ec67ac2005-09-09 16:23:40 -0700537 current_distance = abs(r10_bio->devs[slot].addr -
538 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800540 /* Find the disk whose head is closest,
541 * or - for far > 1 - find the closest to partition beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 for (nslot = slot; nslot < conf->copies; nslot++) {
544 int ndisk = r10_bio->devs[nslot].devnum;
545
546
Suzanne Woodd6065f72005-11-08 21:39:27 -0800547 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800548 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800549 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 continue;
551
NeilBrown22dfdf52005-11-28 13:44:09 -0800552 /* This optimisation is debatable, and completely destroys
553 * sequential read speed for 'far copies' arrays. So only
554 * keep it for 'near' arrays, and review those later.
555 */
556 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 disk = ndisk;
558 slot = nslot;
559 break;
560 }
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800561
562 /* for far > 1 always use the lowest address */
563 if (conf->far_copies > 1)
564 new_distance = r10_bio->devs[nslot].addr;
565 else
566 new_distance = abs(r10_bio->devs[nslot].addr -
567 conf->mirrors[ndisk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (new_distance < current_distance) {
569 current_distance = new_distance;
570 disk = ndisk;
571 slot = nslot;
572 }
573 }
574
575rb_out:
576 r10_bio->read_slot = slot;
577/* conf->next_seq_sect = this_sector + sectors;*/
578
Suzanne Woodd6065f72005-11-08 21:39:27 -0800579 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800581 else
582 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 rcu_read_unlock();
584
585 return disk;
586}
587
588static void unplug_slaves(mddev_t *mddev)
589{
590 conf_t *conf = mddev_to_conf(mddev);
591 int i;
592
593 rcu_read_lock();
594 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800595 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800596 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200597 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 atomic_inc(&rdev->nr_pending);
600 rcu_read_unlock();
601
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500602 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 rdev_dec_pending(rdev, mddev);
605 rcu_read_lock();
606 }
607 }
608 rcu_read_unlock();
609}
610
Jens Axboe165125e2007-07-24 09:28:11 +0200611static void raid10_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
NeilBrown6cce3b22006-01-06 00:20:16 -0800613 mddev_t *mddev = q->queuedata;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unplug_slaves(q->queuedata);
NeilBrown6cce3b22006-01-06 00:20:16 -0800616 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
NeilBrown0d129222006-10-03 01:15:54 -0700619static int raid10_congested(void *data, int bits)
620{
621 mddev_t *mddev = data;
622 conf_t *conf = mddev_to_conf(mddev);
623 int i, ret = 0;
624
625 rcu_read_lock();
626 for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
627 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
628 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200629 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700630
631 ret |= bdi_congested(&q->backing_dev_info, bits);
632 }
633 }
634 rcu_read_unlock();
635 return ret;
636}
637
NeilBrowna35e63e2008-03-04 14:29:29 -0800638static int flush_pending_writes(conf_t *conf)
639{
640 /* Any writes that have been queued but are awaiting
641 * bitmap updates get flushed here.
642 * We return 1 if any requests were actually submitted.
643 */
644 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700645
NeilBrowna35e63e2008-03-04 14:29:29 -0800646 spin_lock_irq(&conf->device_lock);
647
648 if (conf->pending_bio_list.head) {
649 struct bio *bio;
650 bio = bio_list_get(&conf->pending_bio_list);
651 blk_remove_plug(conf->mddev->queue);
652 spin_unlock_irq(&conf->device_lock);
653 /* flush any pending bitmap writes to disk
654 * before proceeding w/ I/O */
655 bitmap_unplug(conf->mddev->bitmap);
656
657 while (bio) { /* submit pending writes */
658 struct bio *next = bio->bi_next;
659 bio->bi_next = NULL;
660 generic_make_request(bio);
661 bio = next;
662 }
663 rv = 1;
664 } else
665 spin_unlock_irq(&conf->device_lock);
666 return rv;
667}
NeilBrown0a27ec92006-01-06 00:20:13 -0800668/* Barriers....
669 * Sometimes we need to suspend IO while we do something else,
670 * either some resync/recovery, or reconfigure the array.
671 * To do this we raise a 'barrier'.
672 * The 'barrier' is a counter that can be raised multiple times
673 * to count how many activities are happening which preclude
674 * normal IO.
675 * We can only raise the barrier if there is no pending IO.
676 * i.e. if nr_pending == 0.
677 * We choose only to raise the barrier if no-one is waiting for the
678 * barrier to go down. This means that as soon as an IO request
679 * is ready, no other operations which require a barrier will start
680 * until the IO request has had a chance.
681 *
682 * So: regular IO calls 'wait_barrier'. When that returns there
683 * is no backgroup IO happening, It must arrange to call
684 * allow_barrier when it has finished its IO.
685 * backgroup IO calls must call raise_barrier. Once that returns
686 * there is no normal IO happeing. It must arrange to call
687 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
689#define RESYNC_DEPTH 32
690
NeilBrown6cce3b22006-01-06 00:20:16 -0800691static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
NeilBrown6cce3b22006-01-06 00:20:16 -0800693 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
NeilBrown6cce3b22006-01-06 00:20:16 -0800696 /* Wait until no block IO is waiting (unless 'force') */
697 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800698 conf->resync_lock,
699 raid10_unplug(conf->mddev->queue));
700
701 /* block any new IO from starting */
702 conf->barrier++;
703
704 /* No wait for all pending IO to complete */
705 wait_event_lock_irq(conf->wait_barrier,
706 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
707 conf->resync_lock,
708 raid10_unplug(conf->mddev->queue));
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 spin_unlock_irq(&conf->resync_lock);
711}
712
NeilBrown0a27ec92006-01-06 00:20:13 -0800713static void lower_barrier(conf_t *conf)
714{
715 unsigned long flags;
716 spin_lock_irqsave(&conf->resync_lock, flags);
717 conf->barrier--;
718 spin_unlock_irqrestore(&conf->resync_lock, flags);
719 wake_up(&conf->wait_barrier);
720}
721
722static void wait_barrier(conf_t *conf)
723{
724 spin_lock_irq(&conf->resync_lock);
725 if (conf->barrier) {
726 conf->nr_waiting++;
727 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
728 conf->resync_lock,
729 raid10_unplug(conf->mddev->queue));
730 conf->nr_waiting--;
731 }
732 conf->nr_pending++;
733 spin_unlock_irq(&conf->resync_lock);
734}
735
736static void allow_barrier(conf_t *conf)
737{
738 unsigned long flags;
739 spin_lock_irqsave(&conf->resync_lock, flags);
740 conf->nr_pending--;
741 spin_unlock_irqrestore(&conf->resync_lock, flags);
742 wake_up(&conf->wait_barrier);
743}
744
NeilBrown4443ae12006-01-06 00:20:28 -0800745static void freeze_array(conf_t *conf)
746{
747 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800748 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800749 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800750 * wait until nr_pending match nr_queued+1
751 * This is called in the context of one normal IO request
752 * that has failed. Thus any sync request that might be pending
753 * will be blocked by nr_pending, and we need to wait for
754 * pending IO requests to complete or be queued for re-try.
755 * Thus the number queued (nr_queued) plus this request (1)
756 * must match the number of pending IOs (nr_pending) before
757 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800758 */
759 spin_lock_irq(&conf->resync_lock);
760 conf->barrier++;
761 conf->nr_waiting++;
762 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800763 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800764 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800765 ({ flush_pending_writes(conf);
766 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800767 spin_unlock_irq(&conf->resync_lock);
768}
769
770static void unfreeze_array(conf_t *conf)
771{
772 /* reverse the effect of the freeze */
773 spin_lock_irq(&conf->resync_lock);
774 conf->barrier--;
775 conf->nr_waiting--;
776 wake_up(&conf->wait_barrier);
777 spin_unlock_irq(&conf->resync_lock);
778}
779
Jens Axboe165125e2007-07-24 09:28:11 +0200780static int make_request(struct request_queue *q, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 mddev_t *mddev = q->queuedata;
783 conf_t *conf = mddev_to_conf(mddev);
784 mirror_info_t *mirror;
785 r10bio_t *r10_bio;
786 struct bio *read_bio;
787 int i;
788 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100789 const int rw = bio_data_dir(bio);
Lars Ellenberge3881a62007-01-10 23:15:37 -0800790 const int do_sync = bio_sync(bio);
NeilBrown6cce3b22006-01-06 00:20:16 -0800791 struct bio_list bl;
792 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
NeilBrowne5dcdd82005-09-09 16:23:41 -0700794 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200795 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700796 return 0;
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* If this request crosses a chunk boundary, we need to
800 * split it. This will only happen for 1 PAGE (or less) requests.
801 */
802 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
803 > chunk_sects &&
804 conf->near_copies < conf->raid_disks)) {
805 struct bio_pair *bp;
806 /* Sanity check -- queue functions should prevent this happening */
807 if (bio->bi_vcnt != 1 ||
808 bio->bi_idx != 0)
809 goto bad_map;
810 /* This is a one page bio that upper layers
811 * refuse to split for us, so we need to split it.
812 */
813 bp = bio_split(bio, bio_split_pool,
814 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
815 if (make_request(q, &bp->bio1))
816 generic_make_request(&bp->bio1);
817 if (make_request(q, &bp->bio2))
818 generic_make_request(&bp->bio2);
819
820 bio_pair_release(bp);
821 return 0;
822 bad_map:
823 printk("raid10_make_request bug: can't convert block across chunks"
824 " or bigger than %dk %llu %d\n", chunk_sects/2,
825 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
826
NeilBrown6712ecf2007-09-27 12:47:43 +0200827 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829 }
830
NeilBrown3d310eb2005-06-21 17:17:26 -0700831 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /*
834 * Register the new request and wait if the reconstruction
835 * thread has put up a bar for new requests.
836 * Continue immediately if no resync is active currently.
837 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800838 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Jens Axboea3623572005-11-01 09:26:16 +0100840 disk_stat_inc(mddev->gendisk, ios[rw]);
841 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
844
845 r10_bio->master_bio = bio;
846 r10_bio->sectors = bio->bi_size >> 9;
847
848 r10_bio->mddev = mddev;
849 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800850 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jens Axboea3623572005-11-01 09:26:16 +0100852 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * read balancing logic:
855 */
856 int disk = read_balance(conf, r10_bio);
857 int slot = r10_bio->read_slot;
858 if (disk < 0) {
859 raid_end_bio_io(r10_bio);
860 return 0;
861 }
862 mirror = conf->mirrors + disk;
863
864 read_bio = bio_clone(bio, GFP_NOIO);
865
866 r10_bio->devs[slot].bio = read_bio;
867
868 read_bio->bi_sector = r10_bio->devs[slot].addr +
869 mirror->rdev->data_offset;
870 read_bio->bi_bdev = mirror->rdev->bdev;
871 read_bio->bi_end_io = raid10_end_read_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800872 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 read_bio->bi_private = r10_bio;
874
875 generic_make_request(read_bio);
876 return 0;
877 }
878
879 /*
880 * WRITE:
881 */
882 /* first select target devices under spinlock and
883 * inc refcount on their rdev. Record them by setting
884 * bios[x] to bio
885 */
886 raid10_find_phys(conf, r10_bio);
887 rcu_read_lock();
888 for (i = 0; i < conf->copies; i++) {
889 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800890 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
891 if (rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800892 !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800893 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800895 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800897 set_bit(R10BIO_Degraded, &r10_bio->state);
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 rcu_read_unlock();
901
NeilBrown6cce3b22006-01-06 00:20:16 -0800902 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700903
NeilBrown6cce3b22006-01-06 00:20:16 -0800904 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 for (i = 0; i < conf->copies; i++) {
906 struct bio *mbio;
907 int d = r10_bio->devs[i].devnum;
908 if (!r10_bio->devs[i].bio)
909 continue;
910
911 mbio = bio_clone(bio, GFP_NOIO);
912 r10_bio->devs[i].bio = mbio;
913
914 mbio->bi_sector = r10_bio->devs[i].addr+
915 conf->mirrors[d].rdev->data_offset;
916 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
917 mbio->bi_end_io = raid10_end_write_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800918 mbio->bi_rw = WRITE | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 mbio->bi_private = r10_bio;
920
921 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b22006-01-06 00:20:16 -0800922 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Arne Redlichf6f953a2007-07-31 00:37:57 -0700925 if (unlikely(!atomic_read(&r10_bio->remaining))) {
926 /* the array is dead */
927 md_write_end(mddev);
928 raid_end_bio_io(r10_bio);
929 return 0;
930 }
931
NeilBrown6cce3b22006-01-06 00:20:16 -0800932 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
933 spin_lock_irqsave(&conf->device_lock, flags);
934 bio_list_merge(&conf->pending_bio_list, &bl);
935 blk_plug_device(mddev->queue);
936 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
NeilBrowna35e63e2008-03-04 14:29:29 -0800938 /* In case raid10d snuck in to freeze_array */
939 wake_up(&conf->wait_barrier);
940
Lars Ellenberge3881a62007-01-10 23:15:37 -0800941 if (do_sync)
942 md_wakeup_thread(mddev->thread);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
945}
946
947static void status(struct seq_file *seq, mddev_t *mddev)
948{
949 conf_t *conf = mddev_to_conf(mddev);
950 int i;
951
952 if (conf->near_copies < conf->raid_disks)
953 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
954 if (conf->near_copies > 1)
955 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700956 if (conf->far_copies > 1) {
957 if (conf->far_offset)
958 seq_printf(seq, " %d offset-copies", conf->far_copies);
959 else
960 seq_printf(seq, " %d far-copies", conf->far_copies);
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700963 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 for (i = 0; i < conf->raid_disks; i++)
965 seq_printf(seq, "%s",
966 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800967 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 seq_printf(seq, "]");
969}
970
971static void error(mddev_t *mddev, mdk_rdev_t *rdev)
972{
973 char b[BDEVNAME_SIZE];
974 conf_t *conf = mddev_to_conf(mddev);
975
976 /*
977 * If it is not operational, then we have already marked it as dead
978 * else if it is the last working disks, ignore the error, let the
979 * next level up know.
980 * else mark the drive as failed
981 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800982 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -0700983 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * Don't fail the drive, just return an IO error.
986 * The test should really be more sophisticated than
987 * "working_disks == 1", but it isn't critical, and
988 * can wait until we do more sophisticated "is the drive
989 * really dead" tests...
990 */
991 return;
NeilBrownc04be0a2006-10-03 01:15:53 -0700992 if (test_and_clear_bit(In_sync, &rdev->flags)) {
993 unsigned long flags;
994 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700996 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * if recovery is running, make sure it aborts.
999 */
1000 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
1001 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001002 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001003 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
1005 " Operation continuing on %d devices\n",
NeilBrown76186dd2006-10-03 01:15:48 -07001006 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009static void print_conf(conf_t *conf)
1010{
1011 int i;
1012 mirror_info_t *tmp;
1013
1014 printk("RAID10 conf printout:\n");
1015 if (!conf) {
1016 printk("(!conf)\n");
1017 return;
1018 }
NeilBrown76186dd2006-10-03 01:15:48 -07001019 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 conf->raid_disks);
1021
1022 for (i = 0; i < conf->raid_disks; i++) {
1023 char b[BDEVNAME_SIZE];
1024 tmp = conf->mirrors + i;
1025 if (tmp->rdev)
1026 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001027 i, !test_bit(In_sync, &tmp->rdev->flags),
1028 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 bdevname(tmp->rdev->bdev,b));
1030 }
1031}
1032
1033static void close_sync(conf_t *conf)
1034{
NeilBrown0a27ec92006-01-06 00:20:13 -08001035 wait_barrier(conf);
1036 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 mempool_destroy(conf->r10buf_pool);
1039 conf->r10buf_pool = NULL;
1040}
1041
NeilBrown6d508242005-09-09 16:24:03 -07001042/* check if there are enough drives for
1043 * every block to appear on atleast one
1044 */
1045static int enough(conf_t *conf)
1046{
1047 int first = 0;
1048
1049 do {
1050 int n = conf->copies;
1051 int cnt = 0;
1052 while (n--) {
1053 if (conf->mirrors[first].rdev)
1054 cnt++;
1055 first = (first+1) % conf->raid_disks;
1056 }
1057 if (cnt == 0)
1058 return 0;
1059 } while (first != 0);
1060 return 1;
1061}
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063static int raid10_spare_active(mddev_t *mddev)
1064{
1065 int i;
1066 conf_t *conf = mddev->private;
1067 mirror_info_t *tmp;
1068
1069 /*
1070 * Find all non-in_sync disks within the RAID10 configuration
1071 * and mark them in_sync
1072 */
1073 for (i = 0; i < conf->raid_disks; i++) {
1074 tmp = conf->mirrors + i;
1075 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001076 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001077 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1078 unsigned long flags;
1079 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001081 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 }
1084
1085 print_conf(conf);
1086 return 0;
1087}
1088
1089
1090static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1091{
1092 conf_t *conf = mddev->private;
1093 int found = 0;
1094 int mirror;
1095 mirror_info_t *p;
1096
1097 if (mddev->recovery_cp < MaxSector)
1098 /* only hot-add to in-sync arrays, as recovery is
1099 * very different from resync
1100 */
1101 return 0;
NeilBrown6d508242005-09-09 16:24:03 -07001102 if (!enough(conf))
1103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrown6cce3b22006-01-06 00:20:16 -08001105 if (rdev->saved_raid_disk >= 0 &&
1106 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1107 mirror = rdev->saved_raid_disk;
1108 else
1109 mirror = 0;
1110 for ( ; mirror < mddev->raid_disks; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if ( !(p=conf->mirrors+mirror)->rdev) {
1112
1113 blk_queue_stack_limits(mddev->queue,
1114 rdev->bdev->bd_disk->queue);
1115 /* as we don't honour merge_bvec_fn, we must never risk
1116 * violating it, so limit ->max_sector to one PAGE, as
1117 * a one page request is never in violation.
1118 */
1119 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1120 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1121 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1122
1123 p->head_position = 0;
1124 rdev->raid_disk = mirror;
1125 found = 1;
NeilBrown6cce3b22006-01-06 00:20:16 -08001126 if (rdev->saved_raid_disk != mirror)
1127 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001128 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130 }
1131
1132 print_conf(conf);
1133 return found;
1134}
1135
1136static int raid10_remove_disk(mddev_t *mddev, int number)
1137{
1138 conf_t *conf = mddev->private;
1139 int err = 0;
1140 mdk_rdev_t *rdev;
1141 mirror_info_t *p = conf->mirrors+ number;
1142
1143 print_conf(conf);
1144 rdev = p->rdev;
1145 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001146 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 atomic_read(&rdev->nr_pending)) {
1148 err = -EBUSY;
1149 goto abort;
1150 }
1151 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001152 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (atomic_read(&rdev->nr_pending)) {
1154 /* lost the race, try later */
1155 err = -EBUSY;
1156 p->rdev = rdev;
1157 }
1158 }
1159abort:
1160
1161 print_conf(conf);
1162 return err;
1163}
1164
1165
NeilBrown6712ecf2007-09-27 12:47:43 +02001166static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1169 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1170 int i,d;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 for (i=0; i<conf->copies; i++)
1173 if (r10_bio->devs[i].bio == bio)
1174 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001175 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 update_head_pos(i, r10_bio);
1177 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001178
1179 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1180 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001181 else {
1182 atomic_add(r10_bio->sectors,
1183 &conf->mirrors[d].rdev->corrected_errors);
1184 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1185 md_error(r10_bio->mddev,
1186 conf->mirrors[d].rdev);
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /* for reconstruct, we always reschedule after a read.
1190 * for resync, only after all reads
1191 */
1192 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1193 atomic_dec_and_test(&r10_bio->remaining)) {
1194 /* we have read all the blocks,
1195 * do the comparison in process context in raid10d
1196 */
1197 reschedule_retry(r10_bio);
1198 }
1199 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
NeilBrown6712ecf2007-09-27 12:47:43 +02001202static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1205 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1206 mddev_t *mddev = r10_bio->mddev;
1207 conf_t *conf = mddev_to_conf(mddev);
1208 int i,d;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 for (i = 0; i < conf->copies; i++)
1211 if (r10_bio->devs[i].bio == bio)
1212 break;
1213 d = r10_bio->devs[i].devnum;
1214
1215 if (!uptodate)
1216 md_error(mddev, conf->mirrors[d].rdev);
1217 update_head_pos(i, r10_bio);
1218
1219 while (atomic_dec_and_test(&r10_bio->remaining)) {
1220 if (r10_bio->master_bio == NULL) {
1221 /* the primary of several recovery bios */
1222 md_done_sync(mddev, r10_bio->sectors, 1);
1223 put_buf(r10_bio);
1224 break;
1225 } else {
1226 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1227 put_buf(r10_bio);
1228 r10_bio = r10_bio2;
1229 }
1230 }
1231 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234/*
1235 * Note: sync and recover and handled very differently for raid10
1236 * This code is for resync.
1237 * For resync, we read through virtual addresses and read all blocks.
1238 * If there is any error, we schedule a write. The lowest numbered
1239 * drive is authoritative.
1240 * However requests come for physical address, so we need to map.
1241 * For every physical address there are raid_disks/copies virtual addresses,
1242 * which is always are least one, but is not necessarly an integer.
1243 * This means that a physical address can span multiple chunks, so we may
1244 * have to submit multiple io requests for a single sync request.
1245 */
1246/*
1247 * We check if all blocks are in-sync and only write to blocks that
1248 * aren't in sync
1249 */
1250static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1251{
1252 conf_t *conf = mddev_to_conf(mddev);
1253 int i, first;
1254 struct bio *tbio, *fbio;
1255
1256 atomic_set(&r10_bio->remaining, 1);
1257
1258 /* find the first device with a block */
1259 for (i=0; i<conf->copies; i++)
1260 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1261 break;
1262
1263 if (i == conf->copies)
1264 goto done;
1265
1266 first = i;
1267 fbio = r10_bio->devs[i].bio;
1268
1269 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001270 for (i=0 ; i < conf->copies ; i++) {
1271 int j, d;
1272 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001275
1276 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001278 if (i == first)
1279 continue;
1280 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1281 /* We know that the bi_io_vec layout is the same for
1282 * both 'first' and 'i', so we just compare them.
1283 * All vec entries are PAGE_SIZE;
1284 */
1285 for (j = 0; j < vcnt; j++)
1286 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1287 page_address(tbio->bi_io_vec[j].bv_page),
1288 PAGE_SIZE))
1289 break;
1290 if (j == vcnt)
1291 continue;
1292 mddev->resync_mismatches += r10_bio->sectors;
1293 }
NeilBrown18f08812006-01-06 00:20:25 -08001294 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1295 /* Don't fix anything. */
1296 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* Ok, we need to write this bio
1298 * First we need to fixup bv_offset, bv_len and
1299 * bi_vecs, as the read request might have corrupted these
1300 */
1301 tbio->bi_vcnt = vcnt;
1302 tbio->bi_size = r10_bio->sectors << 9;
1303 tbio->bi_idx = 0;
1304 tbio->bi_phys_segments = 0;
1305 tbio->bi_hw_segments = 0;
1306 tbio->bi_hw_front_size = 0;
1307 tbio->bi_hw_back_size = 0;
1308 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1309 tbio->bi_flags |= 1 << BIO_UPTODATE;
1310 tbio->bi_next = NULL;
1311 tbio->bi_rw = WRITE;
1312 tbio->bi_private = r10_bio;
1313 tbio->bi_sector = r10_bio->devs[i].addr;
1314
1315 for (j=0; j < vcnt ; j++) {
1316 tbio->bi_io_vec[j].bv_offset = 0;
1317 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1318
1319 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1320 page_address(fbio->bi_io_vec[j].bv_page),
1321 PAGE_SIZE);
1322 }
1323 tbio->bi_end_io = end_sync_write;
1324
1325 d = r10_bio->devs[i].devnum;
1326 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1327 atomic_inc(&r10_bio->remaining);
1328 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1329
1330 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1331 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1332 generic_make_request(tbio);
1333 }
1334
1335done:
1336 if (atomic_dec_and_test(&r10_bio->remaining)) {
1337 md_done_sync(mddev, r10_bio->sectors, 1);
1338 put_buf(r10_bio);
1339 }
1340}
1341
1342/*
1343 * Now for the recovery code.
1344 * Recovery happens across physical sectors.
1345 * We recover all non-is_sync drives by finding the virtual address of
1346 * each, and then choose a working drive that also has that virt address.
1347 * There is a separate r10_bio for each non-in_sync drive.
1348 * Only the first two slots are in use. The first for reading,
1349 * The second for writing.
1350 *
1351 */
1352
1353static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1354{
1355 conf_t *conf = mddev_to_conf(mddev);
1356 int i, d;
1357 struct bio *bio, *wbio;
1358
1359
1360 /* move the pages across to the second bio
1361 * and submit the write request
1362 */
1363 bio = r10_bio->devs[0].bio;
1364 wbio = r10_bio->devs[1].bio;
1365 for (i=0; i < wbio->bi_vcnt; i++) {
1366 struct page *p = bio->bi_io_vec[i].bv_page;
1367 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1368 wbio->bi_io_vec[i].bv_page = p;
1369 }
1370 d = r10_bio->devs[1].devnum;
1371
1372 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1373 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001374 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1375 generic_make_request(wbio);
1376 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001377 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
1380
1381/*
1382 * This is a kernel thread which:
1383 *
1384 * 1. Retries failed read operations on working mirrors.
1385 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001386 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 */
1388
NeilBrown6814d532006-10-03 01:15:45 -07001389static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1390{
1391 int sect = 0; /* Offset from r10_bio->sector */
1392 int sectors = r10_bio->sectors;
1393 mdk_rdev_t*rdev;
1394 while(sectors) {
1395 int s = sectors;
1396 int sl = r10_bio->read_slot;
1397 int success = 0;
1398 int start;
1399
1400 if (s > (PAGE_SIZE>>9))
1401 s = PAGE_SIZE >> 9;
1402
1403 rcu_read_lock();
1404 do {
1405 int d = r10_bio->devs[sl].devnum;
1406 rdev = rcu_dereference(conf->mirrors[d].rdev);
1407 if (rdev &&
1408 test_bit(In_sync, &rdev->flags)) {
1409 atomic_inc(&rdev->nr_pending);
1410 rcu_read_unlock();
1411 success = sync_page_io(rdev->bdev,
1412 r10_bio->devs[sl].addr +
1413 sect + rdev->data_offset,
1414 s<<9,
1415 conf->tmppage, READ);
1416 rdev_dec_pending(rdev, mddev);
1417 rcu_read_lock();
1418 if (success)
1419 break;
1420 }
1421 sl++;
1422 if (sl == conf->copies)
1423 sl = 0;
1424 } while (!success && sl != r10_bio->read_slot);
1425 rcu_read_unlock();
1426
1427 if (!success) {
1428 /* Cannot read from anywhere -- bye bye array */
1429 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1430 md_error(mddev, conf->mirrors[dn].rdev);
1431 break;
1432 }
1433
1434 start = sl;
1435 /* write it back and re-read */
1436 rcu_read_lock();
1437 while (sl != r10_bio->read_slot) {
1438 int d;
1439 if (sl==0)
1440 sl = conf->copies;
1441 sl--;
1442 d = r10_bio->devs[sl].devnum;
1443 rdev = rcu_dereference(conf->mirrors[d].rdev);
1444 if (rdev &&
1445 test_bit(In_sync, &rdev->flags)) {
1446 atomic_inc(&rdev->nr_pending);
1447 rcu_read_unlock();
1448 atomic_add(s, &rdev->corrected_errors);
1449 if (sync_page_io(rdev->bdev,
1450 r10_bio->devs[sl].addr +
1451 sect + rdev->data_offset,
1452 s<<9, conf->tmppage, WRITE)
1453 == 0)
1454 /* Well, this device is dead */
1455 md_error(mddev, rdev);
1456 rdev_dec_pending(rdev, mddev);
1457 rcu_read_lock();
1458 }
1459 }
1460 sl = start;
1461 while (sl != r10_bio->read_slot) {
1462 int d;
1463 if (sl==0)
1464 sl = conf->copies;
1465 sl--;
1466 d = r10_bio->devs[sl].devnum;
1467 rdev = rcu_dereference(conf->mirrors[d].rdev);
1468 if (rdev &&
1469 test_bit(In_sync, &rdev->flags)) {
1470 char b[BDEVNAME_SIZE];
1471 atomic_inc(&rdev->nr_pending);
1472 rcu_read_unlock();
1473 if (sync_page_io(rdev->bdev,
1474 r10_bio->devs[sl].addr +
1475 sect + rdev->data_offset,
1476 s<<9, conf->tmppage, READ) == 0)
1477 /* Well, this device is dead */
1478 md_error(mddev, rdev);
1479 else
1480 printk(KERN_INFO
1481 "raid10:%s: read error corrected"
1482 " (%d sectors at %llu on %s)\n",
1483 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001484 (unsigned long long)(sect+
1485 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001486 bdevname(rdev->bdev, b));
1487
1488 rdev_dec_pending(rdev, mddev);
1489 rcu_read_lock();
1490 }
1491 }
1492 rcu_read_unlock();
1493
1494 sectors -= s;
1495 sect += s;
1496 }
1497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499static void raid10d(mddev_t *mddev)
1500{
1501 r10bio_t *r10_bio;
1502 struct bio *bio;
1503 unsigned long flags;
1504 conf_t *conf = mddev_to_conf(mddev);
1505 struct list_head *head = &conf->retry_list;
1506 int unplug=0;
1507 mdk_rdev_t *rdev;
1508
1509 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 for (;;) {
1512 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001513
1514 unplug += flush_pending_writes(conf);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001517 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001518 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1522 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001523 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 spin_unlock_irqrestore(&conf->device_lock, flags);
1525
1526 mddev = r10_bio->mddev;
1527 conf = mddev_to_conf(mddev);
1528 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1529 sync_request_write(mddev, r10_bio);
1530 unplug = 1;
1531 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1532 recovery_request_write(mddev, r10_bio);
1533 unplug = 1;
1534 } else {
1535 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001536 /* we got a read error. Maybe the drive is bad. Maybe just
1537 * the block and we can fix it.
1538 * We freeze all other IO, and try reading the block from
1539 * other devices. When we find one, we re-write
1540 * and check it that fixes the read error.
1541 * This is all done synchronously while the array is
1542 * frozen.
1543 */
NeilBrown6814d532006-10-03 01:15:45 -07001544 if (mddev->ro == 0) {
1545 freeze_array(conf);
1546 fix_read_error(conf, mddev, r10_bio);
1547 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001548 }
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001551 r10_bio->devs[r10_bio->read_slot].bio =
1552 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 mirror = read_balance(conf, r10_bio);
1554 if (mirror == -1) {
1555 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1556 " read error for block %llu\n",
1557 bdevname(bio->bi_bdev,b),
1558 (unsigned long long)r10_bio->sector);
1559 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001560 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 } else {
Lars Ellenberge3881a62007-01-10 23:15:37 -08001562 const int do_sync = bio_sync(r10_bio->master_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001563 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 rdev = conf->mirrors[mirror].rdev;
1565 if (printk_ratelimit())
1566 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1567 " another mirror\n",
1568 bdevname(rdev->bdev,b),
1569 (unsigned long long)r10_bio->sector);
1570 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1571 r10_bio->devs[r10_bio->read_slot].bio = bio;
1572 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1573 + rdev->data_offset;
1574 bio->bi_bdev = rdev->bdev;
Lars Ellenberge3881a62007-01-10 23:15:37 -08001575 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 bio->bi_private = r10_bio;
1577 bio->bi_end_io = raid10_end_read_request;
1578 unplug = 1;
1579 generic_make_request(bio);
1580 }
1581 }
1582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (unplug)
1584 unplug_slaves(mddev);
1585}
1586
1587
1588static int init_resync(conf_t *conf)
1589{
1590 int buffs;
1591
1592 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001593 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1595 if (!conf->r10buf_pool)
1596 return -ENOMEM;
1597 conf->next_resync = 0;
1598 return 0;
1599}
1600
1601/*
1602 * perform a "sync" on one "block"
1603 *
1604 * We need to make sure that no normal I/O request - particularly write
1605 * requests - conflict with active sync requests.
1606 *
1607 * This is achieved by tracking pending requests and a 'barrier' concept
1608 * that can be installed to exclude normal IO requests.
1609 *
1610 * Resync and recovery are handled very differently.
1611 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1612 *
1613 * For resync, we iterate over virtual addresses, read all copies,
1614 * and update if there are differences. If only one copy is live,
1615 * skip it.
1616 * For recovery, we iterate over physical addresses, read a good
1617 * value for each non-in_sync drive, and over-write.
1618 *
1619 * So, for recovery we may have several outstanding complex requests for a
1620 * given address, one for each out-of-sync device. We model this by allocating
1621 * a number of r10_bio structures, one for each out-of-sync device.
1622 * As we setup these structures, we collect all bio's together into a list
1623 * which we then process collectively to add pages, and then process again
1624 * to pass to generic_make_request.
1625 *
1626 * The r10_bio structures are linked using a borrowed master_bio pointer.
1627 * This link is counted in ->remaining. When the r10_bio that points to NULL
1628 * has its remaining count decremented to 0, the whole complex operation
1629 * is complete.
1630 *
1631 */
1632
NeilBrown57afd892005-06-21 17:17:13 -07001633static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 conf_t *conf = mddev_to_conf(mddev);
1636 r10bio_t *r10_bio;
1637 struct bio *biolist = NULL, *bio;
1638 sector_t max_sector, nr_sectors;
1639 int disk;
1640 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001641 int max_sync;
1642 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 sector_t sectors_skipped = 0;
1645 int chunks_skipped = 0;
1646
1647 if (!conf->r10buf_pool)
1648 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 skipped:
1652 max_sector = mddev->size << 1;
1653 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1654 max_sector = mddev->resync_max_sectors;
1655 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001656 /* If we aborted, we need to abort the
1657 * sync on the 'current' bitmap chucks (there can
1658 * be several when recovering multiple devices).
1659 * as we may have started syncing it but not finished.
1660 * We can find the current address in
1661 * mddev->curr_resync, but for recovery,
1662 * we need to convert that to several
1663 * virtual addresses.
1664 */
1665 if (mddev->curr_resync < max_sector) { /* aborted */
1666 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1667 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1668 &sync_blocks, 1);
1669 else for (i=0; i<conf->raid_disks; i++) {
1670 sector_t sect =
1671 raid10_find_virt(conf, mddev->curr_resync, i);
1672 bitmap_end_sync(mddev->bitmap, sect,
1673 &sync_blocks, 1);
1674 }
1675 } else /* completed sync */
1676 conf->fullsync = 0;
1677
1678 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001680 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return sectors_skipped;
1682 }
1683 if (chunks_skipped >= conf->raid_disks) {
1684 /* if there has been nothing to do on any drive,
1685 * then there is nothing to do at all..
1686 */
NeilBrown57afd892005-06-21 17:17:13 -07001687 *skipped = 1;
1688 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
NeilBrownc6207272008-02-06 01:39:52 -08001691 if (max_sector > mddev->resync_max)
1692 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /* make sure whole request will fit in a chunk - if chunks
1695 * are meaningful
1696 */
1697 if (conf->near_copies < conf->raid_disks &&
1698 max_sector > (sector_nr | conf->chunk_mask))
1699 max_sector = (sector_nr | conf->chunk_mask) + 1;
1700 /*
1701 * If there is non-resync activity waiting for us then
1702 * put in a delay to throttle resync.
1703 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001704 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
NeilBrownb47490c2008-02-06 01:39:50 -08001707 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /* Again, very different code for resync and recovery.
1710 * Both must result in an r10bio with a list of bios that
1711 * have bi_end_io, bi_sector, bi_bdev set,
1712 * and bi_private set to the r10bio.
1713 * For recovery, we may actually create several r10bios
1714 * with 2 bios in each, that correspond to the bios in the main one.
1715 * In this case, the subordinate r10bios link back through a
1716 * borrowed master_bio pointer, and the counter in the master
1717 * includes a ref from each subordinate.
1718 */
1719 /* First, we decide what to do and set ->bi_end_io
1720 * To end_sync_read if we want to read, and
1721 * end_sync_write if we will want to write.
1722 */
1723
NeilBrown6cce3b22006-01-06 00:20:16 -08001724 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1726 /* recovery... the complicated one */
1727 int i, j, k;
1728 r10_bio = NULL;
1729
1730 for (i=0 ; i<conf->raid_disks; i++)
1731 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001732 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001733 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /* want to reconstruct this device */
1735 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001736 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1737 int must_sync;
1738 /* Unless we are doing a full sync, we only need
1739 * to recover the block if it is set in the bitmap
1740 */
1741 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1742 &sync_blocks, 1);
1743 if (sync_blocks < max_sync)
1744 max_sync = sync_blocks;
1745 if (!must_sync &&
1746 !conf->fullsync) {
1747 /* yep, skip the sync_blocks here, but don't assume
1748 * that there will never be anything to do here
1749 */
1750 chunks_skipped = -1;
1751 continue;
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001755 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 atomic_set(&r10_bio->remaining, 0);
1757
1758 r10_bio->master_bio = (struct bio*)rb2;
1759 if (rb2)
1760 atomic_inc(&rb2->remaining);
1761 r10_bio->mddev = mddev;
1762 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001763 r10_bio->sector = sect;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 raid10_find_phys(conf, r10_bio);
NeilBrown6cce3b22006-01-06 00:20:16 -08001766 /* Need to check if this section will still be
1767 * degraded
1768 */
1769 for (j=0; j<conf->copies;j++) {
1770 int d = r10_bio->devs[j].devnum;
1771 if (conf->mirrors[d].rdev == NULL ||
NeilBrowna24a8dd2006-01-06 00:20:35 -08001772 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001773 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001774 break;
1775 }
NeilBrown6cce3b22006-01-06 00:20:16 -08001776 }
1777 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1778 &sync_blocks, still_degraded);
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 for (j=0; j<conf->copies;j++) {
1781 int d = r10_bio->devs[j].devnum;
1782 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001783 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /* This is where we read from */
1785 bio = r10_bio->devs[0].bio;
1786 bio->bi_next = biolist;
1787 biolist = bio;
1788 bio->bi_private = r10_bio;
1789 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001790 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 bio->bi_sector = r10_bio->devs[j].addr +
1792 conf->mirrors[d].rdev->data_offset;
1793 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1794 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1795 atomic_inc(&r10_bio->remaining);
1796 /* and we write to 'i' */
1797
1798 for (k=0; k<conf->copies; k++)
1799 if (r10_bio->devs[k].devnum == i)
1800 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001801 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 bio = r10_bio->devs[1].bio;
1803 bio->bi_next = biolist;
1804 biolist = bio;
1805 bio->bi_private = r10_bio;
1806 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001807 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 bio->bi_sector = r10_bio->devs[k].addr +
1809 conf->mirrors[i].rdev->data_offset;
1810 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1811
1812 r10_bio->devs[0].devnum = d;
1813 r10_bio->devs[1].devnum = i;
1814
1815 break;
1816 }
1817 }
1818 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001819 /* Cannot recover, so abort the recovery */
1820 put_buf(r10_bio);
1821 r10_bio = rb2;
1822 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1823 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1824 mdname(mddev));
1825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 }
1828 if (biolist == NULL) {
1829 while (r10_bio) {
1830 r10bio_t *rb2 = r10_bio;
1831 r10_bio = (r10bio_t*) rb2->master_bio;
1832 rb2->master_bio = NULL;
1833 put_buf(rb2);
1834 }
1835 goto giveup;
1836 }
1837 } else {
1838 /* resync. Schedule a read for every block at this virt offset */
1839 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001840
1841 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1842 &sync_blocks, mddev->degraded) &&
1843 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1844 /* We can skip this block */
1845 *skipped = 1;
1846 return sync_blocks + sectors_skipped;
1847 }
1848 if (sync_blocks < max_sync)
1849 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 r10_bio->mddev = mddev;
1853 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001854 raise_barrier(conf, 0);
1855 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 r10_bio->master_bio = NULL;
1858 r10_bio->sector = sector_nr;
1859 set_bit(R10BIO_IsSync, &r10_bio->state);
1860 raid10_find_phys(conf, r10_bio);
1861 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1862
1863 for (i=0; i<conf->copies; i++) {
1864 int d = r10_bio->devs[i].devnum;
1865 bio = r10_bio->devs[i].bio;
1866 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001867 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001869 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 continue;
1871 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1872 atomic_inc(&r10_bio->remaining);
1873 bio->bi_next = biolist;
1874 biolist = bio;
1875 bio->bi_private = r10_bio;
1876 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001877 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 bio->bi_sector = r10_bio->devs[i].addr +
1879 conf->mirrors[d].rdev->data_offset;
1880 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1881 count++;
1882 }
1883
1884 if (count < 2) {
1885 for (i=0; i<conf->copies; i++) {
1886 int d = r10_bio->devs[i].devnum;
1887 if (r10_bio->devs[i].bio->bi_end_io)
1888 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1889 }
1890 put_buf(r10_bio);
1891 biolist = NULL;
1892 goto giveup;
1893 }
1894 }
1895
1896 for (bio = biolist; bio ; bio=bio->bi_next) {
1897
1898 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1899 if (bio->bi_end_io)
1900 bio->bi_flags |= 1 << BIO_UPTODATE;
1901 bio->bi_vcnt = 0;
1902 bio->bi_idx = 0;
1903 bio->bi_phys_segments = 0;
1904 bio->bi_hw_segments = 0;
1905 bio->bi_size = 0;
1906 }
1907
1908 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001909 if (sector_nr + max_sync < max_sector)
1910 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 do {
1912 struct page *page;
1913 int len = PAGE_SIZE;
1914 disk = 0;
1915 if (sector_nr + (len>>9) > max_sector)
1916 len = (max_sector - sector_nr) << 9;
1917 if (len == 0)
1918 break;
1919 for (bio= biolist ; bio ; bio=bio->bi_next) {
1920 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1921 if (bio_add_page(bio, page, len, 0) == 0) {
1922 /* stop here */
1923 struct bio *bio2;
1924 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1925 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1926 /* remove last page from this bio */
1927 bio2->bi_vcnt--;
1928 bio2->bi_size -= len;
1929 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1930 }
1931 goto bio_full;
1932 }
1933 disk = i;
1934 }
1935 nr_sectors += len>>9;
1936 sector_nr += len>>9;
1937 } while (biolist->bi_vcnt < RESYNC_PAGES);
1938 bio_full:
1939 r10_bio->sectors = nr_sectors;
1940
1941 while (biolist) {
1942 bio = biolist;
1943 biolist = biolist->bi_next;
1944
1945 bio->bi_next = NULL;
1946 r10_bio = bio->bi_private;
1947 r10_bio->sectors = nr_sectors;
1948
1949 if (bio->bi_end_io == end_sync_read) {
1950 md_sync_acct(bio->bi_bdev, nr_sectors);
1951 generic_make_request(bio);
1952 }
1953 }
1954
NeilBrown57afd892005-06-21 17:17:13 -07001955 if (sectors_skipped)
1956 /* pretend they weren't skipped, it makes
1957 * no important difference in this case
1958 */
1959 md_done_sync(mddev, sectors_skipped, 1);
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return sectors_skipped + nr_sectors;
1962 giveup:
1963 /* There is nowhere to write, so all non-sync
1964 * drives must be failed, so try the next chunk...
1965 */
1966 {
NeilBrown57afd892005-06-21 17:17:13 -07001967 sector_t sec = max_sector - sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 sectors_skipped += sec;
1969 chunks_skipped ++;
1970 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto skipped;
1972 }
1973}
1974
1975static int run(mddev_t *mddev)
1976{
1977 conf_t *conf;
1978 int i, disk_idx;
1979 mirror_info_t *disk;
1980 mdk_rdev_t *rdev;
1981 struct list_head *tmp;
NeilBrownc93983b2006-06-26 00:27:41 -07001982 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 sector_t stride, size;
1984
NeilBrown2604b702006-01-06 00:20:36 -08001985 if (mddev->chunk_size == 0) {
1986 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
1987 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
NeilBrown2604b702006-01-06 00:20:36 -08001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 nc = mddev->layout & 255;
1991 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07001992 fo = mddev->layout & (1<<16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07001994 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1996 mdname(mddev), mddev->layout);
1997 goto out;
1998 }
1999 /*
2000 * copy the already verified devices into our private RAID10
2001 * bookkeeping area. [whatever we allocate in run(),
2002 * should be freed in stop()]
2003 */
NeilBrown4443ae12006-01-06 00:20:28 -08002004 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 mddev->private = conf;
2006 if (!conf) {
2007 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2008 mdname(mddev));
2009 goto out;
2010 }
NeilBrown4443ae12006-01-06 00:20:28 -08002011 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 GFP_KERNEL);
2013 if (!conf->mirrors) {
2014 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2015 mdname(mddev));
2016 goto out_free_conf;
2017 }
NeilBrown4443ae12006-01-06 00:20:28 -08002018
2019 conf->tmppage = alloc_page(GFP_KERNEL);
2020 if (!conf->tmppage)
2021 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
NeilBrown64a742b2007-02-28 20:11:18 -08002023 conf->mddev = mddev;
2024 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 conf->near_copies = nc;
2026 conf->far_copies = fc;
2027 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002028 conf->far_offset = fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
2030 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
NeilBrown64a742b2007-02-28 20:11:18 -08002031 size = mddev->size >> (conf->chunk_shift-1);
2032 sector_div(size, fc);
2033 size = size * conf->raid_disks;
2034 sector_div(size, nc);
2035 /* 'size' is now the number of chunks in the array */
2036 /* calculate "used chunks per device" in 'stride' */
2037 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002038
2039 /* We need to round up when dividing by raid_disks to
2040 * get the stride size.
2041 */
2042 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002043 sector_div(stride, conf->raid_disks);
2044 mddev->size = stride << (conf->chunk_shift-1);
2045
NeilBrownc93983b2006-06-26 00:27:41 -07002046 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002047 stride = 1;
2048 else
NeilBrownc93983b2006-06-26 00:27:41 -07002049 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002050 conf->stride = stride << conf->chunk_shift;
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2053 r10bio_pool_free, conf);
2054 if (!conf->r10bio_pool) {
2055 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2056 mdname(mddev));
2057 goto out_free_conf;
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
NeilBrownd089c6a2008-02-06 01:39:59 -08002060 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 disk_idx = rdev->raid_disk;
2062 if (disk_idx >= mddev->raid_disks
2063 || disk_idx < 0)
2064 continue;
2065 disk = conf->mirrors + disk_idx;
2066
2067 disk->rdev = rdev;
2068
2069 blk_queue_stack_limits(mddev->queue,
2070 rdev->bdev->bd_disk->queue);
2071 /* as we don't honour merge_bvec_fn, we must never risk
2072 * violating it, so limit ->max_sector to one PAGE, as
2073 * a one page request is never in violation.
2074 */
2075 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2076 mddev->queue->max_sectors > (PAGE_SIZE>>9))
2077 mddev->queue->max_sectors = (PAGE_SIZE>>9);
2078
2079 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 spin_lock_init(&conf->device_lock);
2082 INIT_LIST_HEAD(&conf->retry_list);
2083
2084 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08002085 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
NeilBrown6d508242005-09-09 16:24:03 -07002087 /* need to check that every block has at least one working mirror */
2088 if (!enough(conf)) {
2089 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2090 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 goto out_free_conf;
2092 }
2093
2094 mddev->degraded = 0;
2095 for (i = 0; i < conf->raid_disks; i++) {
2096
2097 disk = conf->mirrors + i;
2098
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002099 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002100 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 disk->head_position = 0;
2102 mddev->degraded++;
2103 }
2104 }
2105
2106
2107 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2108 if (!mddev->thread) {
2109 printk(KERN_ERR
2110 "raid10: couldn't allocate thread for %s\n",
2111 mdname(mddev));
2112 goto out_free_conf;
2113 }
2114
2115 printk(KERN_INFO
2116 "raid10: raid set %s active with %d out of %d devices\n",
2117 mdname(mddev), mddev->raid_disks - mddev->degraded,
2118 mddev->raid_disks);
2119 /*
2120 * Ok, everything is just fine now
2121 */
NeilBrown64a742b2007-02-28 20:11:18 -08002122 mddev->array_size = size << (conf->chunk_shift-1);
2123 mddev->resync_max_sectors = size << conf->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
NeilBrown7a5febe2005-05-16 21:53:16 -07002125 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002126 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2127 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 /* Calculate max read-ahead size.
2130 * We need to readahead at least twice a whole stripe....
2131 * maybe...
2132 */
2133 {
NeilBrown8932c2e2006-06-26 00:27:36 -07002134 int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 stripe /= conf->near_copies;
2136 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2137 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2138 }
2139
2140 if (conf->near_copies < mddev->raid_disks)
2141 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2142 return 0;
2143
2144out_free_conf:
2145 if (conf->r10bio_pool)
2146 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002147 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002148 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 kfree(conf);
2150 mddev->private = NULL;
2151out:
2152 return -EIO;
2153}
2154
2155static int stop(mddev_t *mddev)
2156{
2157 conf_t *conf = mddev_to_conf(mddev);
2158
2159 md_unregister_thread(mddev->thread);
2160 mddev->thread = NULL;
2161 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2162 if (conf->r10bio_pool)
2163 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002164 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 kfree(conf);
2166 mddev->private = NULL;
2167 return 0;
2168}
2169
NeilBrown6cce3b22006-01-06 00:20:16 -08002170static void raid10_quiesce(mddev_t *mddev, int state)
2171{
2172 conf_t *conf = mddev_to_conf(mddev);
2173
2174 switch(state) {
2175 case 1:
2176 raise_barrier(conf, 0);
2177 break;
2178 case 0:
2179 lower_barrier(conf);
2180 break;
2181 }
2182 if (mddev->thread) {
2183 if (mddev->bitmap)
2184 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2185 else
2186 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2187 md_wakeup_thread(mddev->thread);
2188 }
2189}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
NeilBrown2604b702006-01-06 00:20:36 -08002191static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002194 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 .owner = THIS_MODULE,
2196 .make_request = make_request,
2197 .run = run,
2198 .stop = stop,
2199 .status = status,
2200 .error_handler = error,
2201 .hot_add_disk = raid10_add_disk,
2202 .hot_remove_disk= raid10_remove_disk,
2203 .spare_active = raid10_spare_active,
2204 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002205 .quiesce = raid10_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206};
2207
2208static int __init raid_init(void)
2209{
NeilBrown2604b702006-01-06 00:20:36 -08002210 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
2213static void raid_exit(void)
2214{
NeilBrown2604b702006-01-06 00:20:36 -08002215 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218module_init(raid_init);
2219module_exit(raid_exit);
2220MODULE_LICENSE("GPL");
2221MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002222MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002223MODULE_ALIAS("md-level-10");