blob: 5de42d87bf4ed23d17a8762609140f01c536779b [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
540 /* Find the disk whose head is closest */
541
542 for (nslot = slot; nslot < conf->copies; nslot++) {
543 int ndisk = r10_bio->devs[nslot].devnum;
544
545
Suzanne Woodd6065f72005-11-08 21:39:27 -0800546 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800547 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800548 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 continue;
550
NeilBrown22dfdf52005-11-28 13:44:09 -0800551 /* This optimisation is debatable, and completely destroys
552 * sequential read speed for 'far copies' arrays. So only
553 * keep it for 'near' arrays, and review those later.
554 */
555 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 disk = ndisk;
557 slot = nslot;
558 break;
559 }
560 new_distance = abs(r10_bio->devs[nslot].addr -
561 conf->mirrors[ndisk].head_position);
562 if (new_distance < current_distance) {
563 current_distance = new_distance;
564 disk = ndisk;
565 slot = nslot;
566 }
567 }
568
569rb_out:
570 r10_bio->read_slot = slot;
571/* conf->next_seq_sect = this_sector + sectors;*/
572
Suzanne Woodd6065f72005-11-08 21:39:27 -0800573 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800575 else
576 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 rcu_read_unlock();
578
579 return disk;
580}
581
582static void unplug_slaves(mddev_t *mddev)
583{
584 conf_t *conf = mddev_to_conf(mddev);
585 int i;
586
587 rcu_read_lock();
588 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800589 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800590 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200591 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 atomic_inc(&rdev->nr_pending);
594 rcu_read_unlock();
595
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500596 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 rdev_dec_pending(rdev, mddev);
599 rcu_read_lock();
600 }
601 }
602 rcu_read_unlock();
603}
604
Jens Axboe165125e2007-07-24 09:28:11 +0200605static void raid10_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
NeilBrown6cce3b22006-01-06 00:20:16 -0800607 mddev_t *mddev = q->queuedata;
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 unplug_slaves(q->queuedata);
NeilBrown6cce3b22006-01-06 00:20:16 -0800610 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
NeilBrown0d129222006-10-03 01:15:54 -0700613static int raid10_congested(void *data, int bits)
614{
615 mddev_t *mddev = data;
616 conf_t *conf = mddev_to_conf(mddev);
617 int i, ret = 0;
618
619 rcu_read_lock();
620 for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
621 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
622 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200623 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700624
625 ret |= bdi_congested(&q->backing_dev_info, bits);
626 }
627 }
628 rcu_read_unlock();
629 return ret;
630}
631
NeilBrowna35e63e2008-03-04 14:29:29 -0800632static int flush_pending_writes(conf_t *conf)
633{
634 /* Any writes that have been queued but are awaiting
635 * bitmap updates get flushed here.
636 * We return 1 if any requests were actually submitted.
637 */
638 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700639
NeilBrowna35e63e2008-03-04 14:29:29 -0800640 spin_lock_irq(&conf->device_lock);
641
642 if (conf->pending_bio_list.head) {
643 struct bio *bio;
644 bio = bio_list_get(&conf->pending_bio_list);
645 blk_remove_plug(conf->mddev->queue);
646 spin_unlock_irq(&conf->device_lock);
647 /* flush any pending bitmap writes to disk
648 * before proceeding w/ I/O */
649 bitmap_unplug(conf->mddev->bitmap);
650
651 while (bio) { /* submit pending writes */
652 struct bio *next = bio->bi_next;
653 bio->bi_next = NULL;
654 generic_make_request(bio);
655 bio = next;
656 }
657 rv = 1;
658 } else
659 spin_unlock_irq(&conf->device_lock);
660 return rv;
661}
NeilBrown0a27ec92006-01-06 00:20:13 -0800662/* Barriers....
663 * Sometimes we need to suspend IO while we do something else,
664 * either some resync/recovery, or reconfigure the array.
665 * To do this we raise a 'barrier'.
666 * The 'barrier' is a counter that can be raised multiple times
667 * to count how many activities are happening which preclude
668 * normal IO.
669 * We can only raise the barrier if there is no pending IO.
670 * i.e. if nr_pending == 0.
671 * We choose only to raise the barrier if no-one is waiting for the
672 * barrier to go down. This means that as soon as an IO request
673 * is ready, no other operations which require a barrier will start
674 * until the IO request has had a chance.
675 *
676 * So: regular IO calls 'wait_barrier'. When that returns there
677 * is no backgroup IO happening, It must arrange to call
678 * allow_barrier when it has finished its IO.
679 * backgroup IO calls must call raise_barrier. Once that returns
680 * there is no normal IO happeing. It must arrange to call
681 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
683#define RESYNC_DEPTH 32
684
NeilBrown6cce3b22006-01-06 00:20:16 -0800685static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
NeilBrown6cce3b22006-01-06 00:20:16 -0800687 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
NeilBrown6cce3b22006-01-06 00:20:16 -0800690 /* Wait until no block IO is waiting (unless 'force') */
691 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800692 conf->resync_lock,
693 raid10_unplug(conf->mddev->queue));
694
695 /* block any new IO from starting */
696 conf->barrier++;
697
698 /* No wait for all pending IO to complete */
699 wait_event_lock_irq(conf->wait_barrier,
700 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
701 conf->resync_lock,
702 raid10_unplug(conf->mddev->queue));
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 spin_unlock_irq(&conf->resync_lock);
705}
706
NeilBrown0a27ec92006-01-06 00:20:13 -0800707static void lower_barrier(conf_t *conf)
708{
709 unsigned long flags;
710 spin_lock_irqsave(&conf->resync_lock, flags);
711 conf->barrier--;
712 spin_unlock_irqrestore(&conf->resync_lock, flags);
713 wake_up(&conf->wait_barrier);
714}
715
716static void wait_barrier(conf_t *conf)
717{
718 spin_lock_irq(&conf->resync_lock);
719 if (conf->barrier) {
720 conf->nr_waiting++;
721 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
722 conf->resync_lock,
723 raid10_unplug(conf->mddev->queue));
724 conf->nr_waiting--;
725 }
726 conf->nr_pending++;
727 spin_unlock_irq(&conf->resync_lock);
728}
729
730static void allow_barrier(conf_t *conf)
731{
732 unsigned long flags;
733 spin_lock_irqsave(&conf->resync_lock, flags);
734 conf->nr_pending--;
735 spin_unlock_irqrestore(&conf->resync_lock, flags);
736 wake_up(&conf->wait_barrier);
737}
738
NeilBrown4443ae12006-01-06 00:20:28 -0800739static void freeze_array(conf_t *conf)
740{
741 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800742 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800743 * We increment barrier and nr_waiting, and then
744 * wait until barrier+nr_pending match nr_queued+2
745 */
746 spin_lock_irq(&conf->resync_lock);
747 conf->barrier++;
748 conf->nr_waiting++;
749 wait_event_lock_irq(conf->wait_barrier,
750 conf->barrier+conf->nr_pending == conf->nr_queued+2,
751 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800752 ({ flush_pending_writes(conf);
753 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800754 spin_unlock_irq(&conf->resync_lock);
755}
756
757static void unfreeze_array(conf_t *conf)
758{
759 /* reverse the effect of the freeze */
760 spin_lock_irq(&conf->resync_lock);
761 conf->barrier--;
762 conf->nr_waiting--;
763 wake_up(&conf->wait_barrier);
764 spin_unlock_irq(&conf->resync_lock);
765}
766
Jens Axboe165125e2007-07-24 09:28:11 +0200767static int make_request(struct request_queue *q, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 mddev_t *mddev = q->queuedata;
770 conf_t *conf = mddev_to_conf(mddev);
771 mirror_info_t *mirror;
772 r10bio_t *r10_bio;
773 struct bio *read_bio;
774 int i;
775 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100776 const int rw = bio_data_dir(bio);
Lars Ellenberge3881a62007-01-10 23:15:37 -0800777 const int do_sync = bio_sync(bio);
NeilBrown6cce3b22006-01-06 00:20:16 -0800778 struct bio_list bl;
779 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
NeilBrowne5dcdd82005-09-09 16:23:41 -0700781 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200782 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700783 return 0;
784 }
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /* If this request crosses a chunk boundary, we need to
787 * split it. This will only happen for 1 PAGE (or less) requests.
788 */
789 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
790 > chunk_sects &&
791 conf->near_copies < conf->raid_disks)) {
792 struct bio_pair *bp;
793 /* Sanity check -- queue functions should prevent this happening */
794 if (bio->bi_vcnt != 1 ||
795 bio->bi_idx != 0)
796 goto bad_map;
797 /* This is a one page bio that upper layers
798 * refuse to split for us, so we need to split it.
799 */
800 bp = bio_split(bio, bio_split_pool,
801 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
802 if (make_request(q, &bp->bio1))
803 generic_make_request(&bp->bio1);
804 if (make_request(q, &bp->bio2))
805 generic_make_request(&bp->bio2);
806
807 bio_pair_release(bp);
808 return 0;
809 bad_map:
810 printk("raid10_make_request bug: can't convert block across chunks"
811 " or bigger than %dk %llu %d\n", chunk_sects/2,
812 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
813
NeilBrown6712ecf2007-09-27 12:47:43 +0200814 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return 0;
816 }
817
NeilBrown3d310eb2005-06-21 17:17:26 -0700818 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * Register the new request and wait if the reconstruction
822 * thread has put up a bar for new requests.
823 * Continue immediately if no resync is active currently.
824 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800825 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jens Axboea3623572005-11-01 09:26:16 +0100827 disk_stat_inc(mddev->gendisk, ios[rw]);
828 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
831
832 r10_bio->master_bio = bio;
833 r10_bio->sectors = bio->bi_size >> 9;
834
835 r10_bio->mddev = mddev;
836 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800837 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Jens Axboea3623572005-11-01 09:26:16 +0100839 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
841 * read balancing logic:
842 */
843 int disk = read_balance(conf, r10_bio);
844 int slot = r10_bio->read_slot;
845 if (disk < 0) {
846 raid_end_bio_io(r10_bio);
847 return 0;
848 }
849 mirror = conf->mirrors + disk;
850
851 read_bio = bio_clone(bio, GFP_NOIO);
852
853 r10_bio->devs[slot].bio = read_bio;
854
855 read_bio->bi_sector = r10_bio->devs[slot].addr +
856 mirror->rdev->data_offset;
857 read_bio->bi_bdev = mirror->rdev->bdev;
858 read_bio->bi_end_io = raid10_end_read_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800859 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 read_bio->bi_private = r10_bio;
861
862 generic_make_request(read_bio);
863 return 0;
864 }
865
866 /*
867 * WRITE:
868 */
869 /* first select target devices under spinlock and
870 * inc refcount on their rdev. Record them by setting
871 * bios[x] to bio
872 */
873 raid10_find_phys(conf, r10_bio);
874 rcu_read_lock();
875 for (i = 0; i < conf->copies; i++) {
876 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800877 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
878 if (rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800879 !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800880 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800882 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800884 set_bit(R10BIO_Degraded, &r10_bio->state);
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 rcu_read_unlock();
888
NeilBrown6cce3b22006-01-06 00:20:16 -0800889 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700890
NeilBrown6cce3b22006-01-06 00:20:16 -0800891 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 for (i = 0; i < conf->copies; i++) {
893 struct bio *mbio;
894 int d = r10_bio->devs[i].devnum;
895 if (!r10_bio->devs[i].bio)
896 continue;
897
898 mbio = bio_clone(bio, GFP_NOIO);
899 r10_bio->devs[i].bio = mbio;
900
901 mbio->bi_sector = r10_bio->devs[i].addr+
902 conf->mirrors[d].rdev->data_offset;
903 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
904 mbio->bi_end_io = raid10_end_write_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800905 mbio->bi_rw = WRITE | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 mbio->bi_private = r10_bio;
907
908 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b22006-01-06 00:20:16 -0800909 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
Arne Redlichf6f953a2007-07-31 00:37:57 -0700912 if (unlikely(!atomic_read(&r10_bio->remaining))) {
913 /* the array is dead */
914 md_write_end(mddev);
915 raid_end_bio_io(r10_bio);
916 return 0;
917 }
918
NeilBrown6cce3b22006-01-06 00:20:16 -0800919 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
920 spin_lock_irqsave(&conf->device_lock, flags);
921 bio_list_merge(&conf->pending_bio_list, &bl);
922 blk_plug_device(mddev->queue);
923 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
NeilBrowna35e63e2008-03-04 14:29:29 -0800925 /* In case raid10d snuck in to freeze_array */
926 wake_up(&conf->wait_barrier);
927
Lars Ellenberge3881a62007-01-10 23:15:37 -0800928 if (do_sync)
929 md_wakeup_thread(mddev->thread);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932}
933
934static void status(struct seq_file *seq, mddev_t *mddev)
935{
936 conf_t *conf = mddev_to_conf(mddev);
937 int i;
938
939 if (conf->near_copies < conf->raid_disks)
940 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
941 if (conf->near_copies > 1)
942 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700943 if (conf->far_copies > 1) {
944 if (conf->far_offset)
945 seq_printf(seq, " %d offset-copies", conf->far_copies);
946 else
947 seq_printf(seq, " %d far-copies", conf->far_copies);
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700950 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 for (i = 0; i < conf->raid_disks; i++)
952 seq_printf(seq, "%s",
953 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800954 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 seq_printf(seq, "]");
956}
957
958static void error(mddev_t *mddev, mdk_rdev_t *rdev)
959{
960 char b[BDEVNAME_SIZE];
961 conf_t *conf = mddev_to_conf(mddev);
962
963 /*
964 * If it is not operational, then we have already marked it as dead
965 * else if it is the last working disks, ignore the error, let the
966 * next level up know.
967 * else mark the drive as failed
968 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800969 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -0700970 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * Don't fail the drive, just return an IO error.
973 * The test should really be more sophisticated than
974 * "working_disks == 1", but it isn't critical, and
975 * can wait until we do more sophisticated "is the drive
976 * really dead" tests...
977 */
978 return;
NeilBrownc04be0a2006-10-03 01:15:53 -0700979 if (test_and_clear_bit(In_sync, &rdev->flags)) {
980 unsigned long flags;
981 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700983 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * if recovery is running, make sure it aborts.
986 */
987 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
988 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800989 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -0700990 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
992 " Operation continuing on %d devices\n",
NeilBrown76186dd2006-10-03 01:15:48 -0700993 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996static void print_conf(conf_t *conf)
997{
998 int i;
999 mirror_info_t *tmp;
1000
1001 printk("RAID10 conf printout:\n");
1002 if (!conf) {
1003 printk("(!conf)\n");
1004 return;
1005 }
NeilBrown76186dd2006-10-03 01:15:48 -07001006 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 conf->raid_disks);
1008
1009 for (i = 0; i < conf->raid_disks; i++) {
1010 char b[BDEVNAME_SIZE];
1011 tmp = conf->mirrors + i;
1012 if (tmp->rdev)
1013 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001014 i, !test_bit(In_sync, &tmp->rdev->flags),
1015 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 bdevname(tmp->rdev->bdev,b));
1017 }
1018}
1019
1020static void close_sync(conf_t *conf)
1021{
NeilBrown0a27ec92006-01-06 00:20:13 -08001022 wait_barrier(conf);
1023 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 mempool_destroy(conf->r10buf_pool);
1026 conf->r10buf_pool = NULL;
1027}
1028
NeilBrown6d508242005-09-09 16:24:03 -07001029/* check if there are enough drives for
1030 * every block to appear on atleast one
1031 */
1032static int enough(conf_t *conf)
1033{
1034 int first = 0;
1035
1036 do {
1037 int n = conf->copies;
1038 int cnt = 0;
1039 while (n--) {
1040 if (conf->mirrors[first].rdev)
1041 cnt++;
1042 first = (first+1) % conf->raid_disks;
1043 }
1044 if (cnt == 0)
1045 return 0;
1046 } while (first != 0);
1047 return 1;
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050static int raid10_spare_active(mddev_t *mddev)
1051{
1052 int i;
1053 conf_t *conf = mddev->private;
1054 mirror_info_t *tmp;
1055
1056 /*
1057 * Find all non-in_sync disks within the RAID10 configuration
1058 * and mark them in_sync
1059 */
1060 for (i = 0; i < conf->raid_disks; i++) {
1061 tmp = conf->mirrors + i;
1062 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001063 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001064 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1065 unsigned long flags;
1066 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001068 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070 }
1071
1072 print_conf(conf);
1073 return 0;
1074}
1075
1076
1077static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1078{
1079 conf_t *conf = mddev->private;
1080 int found = 0;
1081 int mirror;
1082 mirror_info_t *p;
1083
1084 if (mddev->recovery_cp < MaxSector)
1085 /* only hot-add to in-sync arrays, as recovery is
1086 * very different from resync
1087 */
1088 return 0;
NeilBrown6d508242005-09-09 16:24:03 -07001089 if (!enough(conf))
1090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
NeilBrown6cce3b22006-01-06 00:20:16 -08001092 if (rdev->saved_raid_disk >= 0 &&
1093 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1094 mirror = rdev->saved_raid_disk;
1095 else
1096 mirror = 0;
1097 for ( ; mirror < mddev->raid_disks; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if ( !(p=conf->mirrors+mirror)->rdev) {
1099
1100 blk_queue_stack_limits(mddev->queue,
1101 rdev->bdev->bd_disk->queue);
1102 /* as we don't honour merge_bvec_fn, we must never risk
1103 * violating it, so limit ->max_sector to one PAGE, as
1104 * a one page request is never in violation.
1105 */
1106 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1107 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1108 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1109
1110 p->head_position = 0;
1111 rdev->raid_disk = mirror;
1112 found = 1;
NeilBrown6cce3b22006-01-06 00:20:16 -08001113 if (rdev->saved_raid_disk != mirror)
1114 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001115 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
1117 }
1118
1119 print_conf(conf);
1120 return found;
1121}
1122
1123static int raid10_remove_disk(mddev_t *mddev, int number)
1124{
1125 conf_t *conf = mddev->private;
1126 int err = 0;
1127 mdk_rdev_t *rdev;
1128 mirror_info_t *p = conf->mirrors+ number;
1129
1130 print_conf(conf);
1131 rdev = p->rdev;
1132 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001133 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 atomic_read(&rdev->nr_pending)) {
1135 err = -EBUSY;
1136 goto abort;
1137 }
1138 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001139 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (atomic_read(&rdev->nr_pending)) {
1141 /* lost the race, try later */
1142 err = -EBUSY;
1143 p->rdev = rdev;
1144 }
1145 }
1146abort:
1147
1148 print_conf(conf);
1149 return err;
1150}
1151
1152
NeilBrown6712ecf2007-09-27 12:47:43 +02001153static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1156 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1157 int i,d;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 for (i=0; i<conf->copies; i++)
1160 if (r10_bio->devs[i].bio == bio)
1161 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001162 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 update_head_pos(i, r10_bio);
1164 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001165
1166 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1167 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001168 else {
1169 atomic_add(r10_bio->sectors,
1170 &conf->mirrors[d].rdev->corrected_errors);
1171 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1172 md_error(r10_bio->mddev,
1173 conf->mirrors[d].rdev);
1174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* for reconstruct, we always reschedule after a read.
1177 * for resync, only after all reads
1178 */
1179 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1180 atomic_dec_and_test(&r10_bio->remaining)) {
1181 /* we have read all the blocks,
1182 * do the comparison in process context in raid10d
1183 */
1184 reschedule_retry(r10_bio);
1185 }
1186 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
NeilBrown6712ecf2007-09-27 12:47:43 +02001189static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1192 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1193 mddev_t *mddev = r10_bio->mddev;
1194 conf_t *conf = mddev_to_conf(mddev);
1195 int i,d;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 for (i = 0; i < conf->copies; i++)
1198 if (r10_bio->devs[i].bio == bio)
1199 break;
1200 d = r10_bio->devs[i].devnum;
1201
1202 if (!uptodate)
1203 md_error(mddev, conf->mirrors[d].rdev);
1204 update_head_pos(i, r10_bio);
1205
1206 while (atomic_dec_and_test(&r10_bio->remaining)) {
1207 if (r10_bio->master_bio == NULL) {
1208 /* the primary of several recovery bios */
1209 md_done_sync(mddev, r10_bio->sectors, 1);
1210 put_buf(r10_bio);
1211 break;
1212 } else {
1213 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1214 put_buf(r10_bio);
1215 r10_bio = r10_bio2;
1216 }
1217 }
1218 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
1221/*
1222 * Note: sync and recover and handled very differently for raid10
1223 * This code is for resync.
1224 * For resync, we read through virtual addresses and read all blocks.
1225 * If there is any error, we schedule a write. The lowest numbered
1226 * drive is authoritative.
1227 * However requests come for physical address, so we need to map.
1228 * For every physical address there are raid_disks/copies virtual addresses,
1229 * which is always are least one, but is not necessarly an integer.
1230 * This means that a physical address can span multiple chunks, so we may
1231 * have to submit multiple io requests for a single sync request.
1232 */
1233/*
1234 * We check if all blocks are in-sync and only write to blocks that
1235 * aren't in sync
1236 */
1237static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1238{
1239 conf_t *conf = mddev_to_conf(mddev);
1240 int i, first;
1241 struct bio *tbio, *fbio;
1242
1243 atomic_set(&r10_bio->remaining, 1);
1244
1245 /* find the first device with a block */
1246 for (i=0; i<conf->copies; i++)
1247 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1248 break;
1249
1250 if (i == conf->copies)
1251 goto done;
1252
1253 first = i;
1254 fbio = r10_bio->devs[i].bio;
1255
1256 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001257 for (i=0 ; i < conf->copies ; i++) {
1258 int j, d;
1259 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001262
1263 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001265 if (i == first)
1266 continue;
1267 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1268 /* We know that the bi_io_vec layout is the same for
1269 * both 'first' and 'i', so we just compare them.
1270 * All vec entries are PAGE_SIZE;
1271 */
1272 for (j = 0; j < vcnt; j++)
1273 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1274 page_address(tbio->bi_io_vec[j].bv_page),
1275 PAGE_SIZE))
1276 break;
1277 if (j == vcnt)
1278 continue;
1279 mddev->resync_mismatches += r10_bio->sectors;
1280 }
NeilBrown18f08812006-01-06 00:20:25 -08001281 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1282 /* Don't fix anything. */
1283 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* Ok, we need to write this bio
1285 * First we need to fixup bv_offset, bv_len and
1286 * bi_vecs, as the read request might have corrupted these
1287 */
1288 tbio->bi_vcnt = vcnt;
1289 tbio->bi_size = r10_bio->sectors << 9;
1290 tbio->bi_idx = 0;
1291 tbio->bi_phys_segments = 0;
1292 tbio->bi_hw_segments = 0;
1293 tbio->bi_hw_front_size = 0;
1294 tbio->bi_hw_back_size = 0;
1295 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1296 tbio->bi_flags |= 1 << BIO_UPTODATE;
1297 tbio->bi_next = NULL;
1298 tbio->bi_rw = WRITE;
1299 tbio->bi_private = r10_bio;
1300 tbio->bi_sector = r10_bio->devs[i].addr;
1301
1302 for (j=0; j < vcnt ; j++) {
1303 tbio->bi_io_vec[j].bv_offset = 0;
1304 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1305
1306 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1307 page_address(fbio->bi_io_vec[j].bv_page),
1308 PAGE_SIZE);
1309 }
1310 tbio->bi_end_io = end_sync_write;
1311
1312 d = r10_bio->devs[i].devnum;
1313 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1314 atomic_inc(&r10_bio->remaining);
1315 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1316
1317 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1318 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1319 generic_make_request(tbio);
1320 }
1321
1322done:
1323 if (atomic_dec_and_test(&r10_bio->remaining)) {
1324 md_done_sync(mddev, r10_bio->sectors, 1);
1325 put_buf(r10_bio);
1326 }
1327}
1328
1329/*
1330 * Now for the recovery code.
1331 * Recovery happens across physical sectors.
1332 * We recover all non-is_sync drives by finding the virtual address of
1333 * each, and then choose a working drive that also has that virt address.
1334 * There is a separate r10_bio for each non-in_sync drive.
1335 * Only the first two slots are in use. The first for reading,
1336 * The second for writing.
1337 *
1338 */
1339
1340static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1341{
1342 conf_t *conf = mddev_to_conf(mddev);
1343 int i, d;
1344 struct bio *bio, *wbio;
1345
1346
1347 /* move the pages across to the second bio
1348 * and submit the write request
1349 */
1350 bio = r10_bio->devs[0].bio;
1351 wbio = r10_bio->devs[1].bio;
1352 for (i=0; i < wbio->bi_vcnt; i++) {
1353 struct page *p = bio->bi_io_vec[i].bv_page;
1354 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1355 wbio->bi_io_vec[i].bv_page = p;
1356 }
1357 d = r10_bio->devs[1].devnum;
1358
1359 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1360 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001361 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1362 generic_make_request(wbio);
1363 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001364 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367
1368/*
1369 * This is a kernel thread which:
1370 *
1371 * 1. Retries failed read operations on working mirrors.
1372 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001373 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
1375
NeilBrown6814d532006-10-03 01:15:45 -07001376static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1377{
1378 int sect = 0; /* Offset from r10_bio->sector */
1379 int sectors = r10_bio->sectors;
1380 mdk_rdev_t*rdev;
1381 while(sectors) {
1382 int s = sectors;
1383 int sl = r10_bio->read_slot;
1384 int success = 0;
1385 int start;
1386
1387 if (s > (PAGE_SIZE>>9))
1388 s = PAGE_SIZE >> 9;
1389
1390 rcu_read_lock();
1391 do {
1392 int d = r10_bio->devs[sl].devnum;
1393 rdev = rcu_dereference(conf->mirrors[d].rdev);
1394 if (rdev &&
1395 test_bit(In_sync, &rdev->flags)) {
1396 atomic_inc(&rdev->nr_pending);
1397 rcu_read_unlock();
1398 success = sync_page_io(rdev->bdev,
1399 r10_bio->devs[sl].addr +
1400 sect + rdev->data_offset,
1401 s<<9,
1402 conf->tmppage, READ);
1403 rdev_dec_pending(rdev, mddev);
1404 rcu_read_lock();
1405 if (success)
1406 break;
1407 }
1408 sl++;
1409 if (sl == conf->copies)
1410 sl = 0;
1411 } while (!success && sl != r10_bio->read_slot);
1412 rcu_read_unlock();
1413
1414 if (!success) {
1415 /* Cannot read from anywhere -- bye bye array */
1416 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1417 md_error(mddev, conf->mirrors[dn].rdev);
1418 break;
1419 }
1420
1421 start = sl;
1422 /* write it back and re-read */
1423 rcu_read_lock();
1424 while (sl != r10_bio->read_slot) {
1425 int d;
1426 if (sl==0)
1427 sl = conf->copies;
1428 sl--;
1429 d = r10_bio->devs[sl].devnum;
1430 rdev = rcu_dereference(conf->mirrors[d].rdev);
1431 if (rdev &&
1432 test_bit(In_sync, &rdev->flags)) {
1433 atomic_inc(&rdev->nr_pending);
1434 rcu_read_unlock();
1435 atomic_add(s, &rdev->corrected_errors);
1436 if (sync_page_io(rdev->bdev,
1437 r10_bio->devs[sl].addr +
1438 sect + rdev->data_offset,
1439 s<<9, conf->tmppage, WRITE)
1440 == 0)
1441 /* Well, this device is dead */
1442 md_error(mddev, rdev);
1443 rdev_dec_pending(rdev, mddev);
1444 rcu_read_lock();
1445 }
1446 }
1447 sl = start;
1448 while (sl != r10_bio->read_slot) {
1449 int d;
1450 if (sl==0)
1451 sl = conf->copies;
1452 sl--;
1453 d = r10_bio->devs[sl].devnum;
1454 rdev = rcu_dereference(conf->mirrors[d].rdev);
1455 if (rdev &&
1456 test_bit(In_sync, &rdev->flags)) {
1457 char b[BDEVNAME_SIZE];
1458 atomic_inc(&rdev->nr_pending);
1459 rcu_read_unlock();
1460 if (sync_page_io(rdev->bdev,
1461 r10_bio->devs[sl].addr +
1462 sect + rdev->data_offset,
1463 s<<9, conf->tmppage, READ) == 0)
1464 /* Well, this device is dead */
1465 md_error(mddev, rdev);
1466 else
1467 printk(KERN_INFO
1468 "raid10:%s: read error corrected"
1469 " (%d sectors at %llu on %s)\n",
1470 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001471 (unsigned long long)(sect+
1472 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001473 bdevname(rdev->bdev, b));
1474
1475 rdev_dec_pending(rdev, mddev);
1476 rcu_read_lock();
1477 }
1478 }
1479 rcu_read_unlock();
1480
1481 sectors -= s;
1482 sect += s;
1483 }
1484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486static void raid10d(mddev_t *mddev)
1487{
1488 r10bio_t *r10_bio;
1489 struct bio *bio;
1490 unsigned long flags;
1491 conf_t *conf = mddev_to_conf(mddev);
1492 struct list_head *head = &conf->retry_list;
1493 int unplug=0;
1494 mdk_rdev_t *rdev;
1495
1496 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 for (;;) {
1499 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001500
1501 unplug += flush_pending_writes(conf);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001504 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001505 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1509 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001510 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 spin_unlock_irqrestore(&conf->device_lock, flags);
1512
1513 mddev = r10_bio->mddev;
1514 conf = mddev_to_conf(mddev);
1515 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1516 sync_request_write(mddev, r10_bio);
1517 unplug = 1;
1518 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1519 recovery_request_write(mddev, r10_bio);
1520 unplug = 1;
1521 } else {
1522 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001523 /* we got a read error. Maybe the drive is bad. Maybe just
1524 * the block and we can fix it.
1525 * We freeze all other IO, and try reading the block from
1526 * other devices. When we find one, we re-write
1527 * and check it that fixes the read error.
1528 * This is all done synchronously while the array is
1529 * frozen.
1530 */
NeilBrown6814d532006-10-03 01:15:45 -07001531 if (mddev->ro == 0) {
1532 freeze_array(conf);
1533 fix_read_error(conf, mddev, r10_bio);
1534 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001535 }
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001538 r10_bio->devs[r10_bio->read_slot].bio =
1539 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 mirror = read_balance(conf, r10_bio);
1541 if (mirror == -1) {
1542 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1543 " read error for block %llu\n",
1544 bdevname(bio->bi_bdev,b),
1545 (unsigned long long)r10_bio->sector);
1546 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001547 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 } else {
Lars Ellenberge3881a62007-01-10 23:15:37 -08001549 const int do_sync = bio_sync(r10_bio->master_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001550 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 rdev = conf->mirrors[mirror].rdev;
1552 if (printk_ratelimit())
1553 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1554 " another mirror\n",
1555 bdevname(rdev->bdev,b),
1556 (unsigned long long)r10_bio->sector);
1557 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1558 r10_bio->devs[r10_bio->read_slot].bio = bio;
1559 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1560 + rdev->data_offset;
1561 bio->bi_bdev = rdev->bdev;
Lars Ellenberge3881a62007-01-10 23:15:37 -08001562 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 bio->bi_private = r10_bio;
1564 bio->bi_end_io = raid10_end_read_request;
1565 unplug = 1;
1566 generic_make_request(bio);
1567 }
1568 }
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (unplug)
1571 unplug_slaves(mddev);
1572}
1573
1574
1575static int init_resync(conf_t *conf)
1576{
1577 int buffs;
1578
1579 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001580 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1582 if (!conf->r10buf_pool)
1583 return -ENOMEM;
1584 conf->next_resync = 0;
1585 return 0;
1586}
1587
1588/*
1589 * perform a "sync" on one "block"
1590 *
1591 * We need to make sure that no normal I/O request - particularly write
1592 * requests - conflict with active sync requests.
1593 *
1594 * This is achieved by tracking pending requests and a 'barrier' concept
1595 * that can be installed to exclude normal IO requests.
1596 *
1597 * Resync and recovery are handled very differently.
1598 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1599 *
1600 * For resync, we iterate over virtual addresses, read all copies,
1601 * and update if there are differences. If only one copy is live,
1602 * skip it.
1603 * For recovery, we iterate over physical addresses, read a good
1604 * value for each non-in_sync drive, and over-write.
1605 *
1606 * So, for recovery we may have several outstanding complex requests for a
1607 * given address, one for each out-of-sync device. We model this by allocating
1608 * a number of r10_bio structures, one for each out-of-sync device.
1609 * As we setup these structures, we collect all bio's together into a list
1610 * which we then process collectively to add pages, and then process again
1611 * to pass to generic_make_request.
1612 *
1613 * The r10_bio structures are linked using a borrowed master_bio pointer.
1614 * This link is counted in ->remaining. When the r10_bio that points to NULL
1615 * has its remaining count decremented to 0, the whole complex operation
1616 * is complete.
1617 *
1618 */
1619
NeilBrown57afd892005-06-21 17:17:13 -07001620static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 conf_t *conf = mddev_to_conf(mddev);
1623 r10bio_t *r10_bio;
1624 struct bio *biolist = NULL, *bio;
1625 sector_t max_sector, nr_sectors;
1626 int disk;
1627 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001628 int max_sync;
1629 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 sector_t sectors_skipped = 0;
1632 int chunks_skipped = 0;
1633
1634 if (!conf->r10buf_pool)
1635 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 skipped:
1639 max_sector = mddev->size << 1;
1640 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1641 max_sector = mddev->resync_max_sectors;
1642 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001643 /* If we aborted, we need to abort the
1644 * sync on the 'current' bitmap chucks (there can
1645 * be several when recovering multiple devices).
1646 * as we may have started syncing it but not finished.
1647 * We can find the current address in
1648 * mddev->curr_resync, but for recovery,
1649 * we need to convert that to several
1650 * virtual addresses.
1651 */
1652 if (mddev->curr_resync < max_sector) { /* aborted */
1653 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1654 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1655 &sync_blocks, 1);
1656 else for (i=0; i<conf->raid_disks; i++) {
1657 sector_t sect =
1658 raid10_find_virt(conf, mddev->curr_resync, i);
1659 bitmap_end_sync(mddev->bitmap, sect,
1660 &sync_blocks, 1);
1661 }
1662 } else /* completed sync */
1663 conf->fullsync = 0;
1664
1665 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001667 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return sectors_skipped;
1669 }
1670 if (chunks_skipped >= conf->raid_disks) {
1671 /* if there has been nothing to do on any drive,
1672 * then there is nothing to do at all..
1673 */
NeilBrown57afd892005-06-21 17:17:13 -07001674 *skipped = 1;
1675 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
NeilBrownc6207272008-02-06 01:39:52 -08001678 if (max_sector > mddev->resync_max)
1679 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /* make sure whole request will fit in a chunk - if chunks
1682 * are meaningful
1683 */
1684 if (conf->near_copies < conf->raid_disks &&
1685 max_sector > (sector_nr | conf->chunk_mask))
1686 max_sector = (sector_nr | conf->chunk_mask) + 1;
1687 /*
1688 * If there is non-resync activity waiting for us then
1689 * put in a delay to throttle resync.
1690 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001691 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
NeilBrownb47490c2008-02-06 01:39:50 -08001694 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /* Again, very different code for resync and recovery.
1697 * Both must result in an r10bio with a list of bios that
1698 * have bi_end_io, bi_sector, bi_bdev set,
1699 * and bi_private set to the r10bio.
1700 * For recovery, we may actually create several r10bios
1701 * with 2 bios in each, that correspond to the bios in the main one.
1702 * In this case, the subordinate r10bios link back through a
1703 * borrowed master_bio pointer, and the counter in the master
1704 * includes a ref from each subordinate.
1705 */
1706 /* First, we decide what to do and set ->bi_end_io
1707 * To end_sync_read if we want to read, and
1708 * end_sync_write if we will want to write.
1709 */
1710
NeilBrown6cce3b22006-01-06 00:20:16 -08001711 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1713 /* recovery... the complicated one */
1714 int i, j, k;
1715 r10_bio = NULL;
1716
1717 for (i=0 ; i<conf->raid_disks; i++)
1718 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001719 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001720 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* want to reconstruct this device */
1722 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001723 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1724 int must_sync;
1725 /* Unless we are doing a full sync, we only need
1726 * to recover the block if it is set in the bitmap
1727 */
1728 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1729 &sync_blocks, 1);
1730 if (sync_blocks < max_sync)
1731 max_sync = sync_blocks;
1732 if (!must_sync &&
1733 !conf->fullsync) {
1734 /* yep, skip the sync_blocks here, but don't assume
1735 * that there will never be anything to do here
1736 */
1737 chunks_skipped = -1;
1738 continue;
1739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001742 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 atomic_set(&r10_bio->remaining, 0);
1744
1745 r10_bio->master_bio = (struct bio*)rb2;
1746 if (rb2)
1747 atomic_inc(&rb2->remaining);
1748 r10_bio->mddev = mddev;
1749 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001750 r10_bio->sector = sect;
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 raid10_find_phys(conf, r10_bio);
NeilBrown6cce3b22006-01-06 00:20:16 -08001753 /* Need to check if this section will still be
1754 * degraded
1755 */
1756 for (j=0; j<conf->copies;j++) {
1757 int d = r10_bio->devs[j].devnum;
1758 if (conf->mirrors[d].rdev == NULL ||
NeilBrowna24a8dd2006-01-06 00:20:35 -08001759 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001760 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001761 break;
1762 }
NeilBrown6cce3b22006-01-06 00:20:16 -08001763 }
1764 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1765 &sync_blocks, still_degraded);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 for (j=0; j<conf->copies;j++) {
1768 int d = r10_bio->devs[j].devnum;
1769 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001770 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /* This is where we read from */
1772 bio = r10_bio->devs[0].bio;
1773 bio->bi_next = biolist;
1774 biolist = bio;
1775 bio->bi_private = r10_bio;
1776 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001777 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 bio->bi_sector = r10_bio->devs[j].addr +
1779 conf->mirrors[d].rdev->data_offset;
1780 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1781 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1782 atomic_inc(&r10_bio->remaining);
1783 /* and we write to 'i' */
1784
1785 for (k=0; k<conf->copies; k++)
1786 if (r10_bio->devs[k].devnum == i)
1787 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001788 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 bio = r10_bio->devs[1].bio;
1790 bio->bi_next = biolist;
1791 biolist = bio;
1792 bio->bi_private = r10_bio;
1793 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001794 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 bio->bi_sector = r10_bio->devs[k].addr +
1796 conf->mirrors[i].rdev->data_offset;
1797 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1798
1799 r10_bio->devs[0].devnum = d;
1800 r10_bio->devs[1].devnum = i;
1801
1802 break;
1803 }
1804 }
1805 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001806 /* Cannot recover, so abort the recovery */
1807 put_buf(r10_bio);
1808 r10_bio = rb2;
1809 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1810 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1811 mdname(mddev));
1812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814 }
1815 if (biolist == NULL) {
1816 while (r10_bio) {
1817 r10bio_t *rb2 = r10_bio;
1818 r10_bio = (r10bio_t*) rb2->master_bio;
1819 rb2->master_bio = NULL;
1820 put_buf(rb2);
1821 }
1822 goto giveup;
1823 }
1824 } else {
1825 /* resync. Schedule a read for every block at this virt offset */
1826 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001827
1828 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1829 &sync_blocks, mddev->degraded) &&
1830 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1831 /* We can skip this block */
1832 *skipped = 1;
1833 return sync_blocks + sectors_skipped;
1834 }
1835 if (sync_blocks < max_sync)
1836 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 r10_bio->mddev = mddev;
1840 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001841 raise_barrier(conf, 0);
1842 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 r10_bio->master_bio = NULL;
1845 r10_bio->sector = sector_nr;
1846 set_bit(R10BIO_IsSync, &r10_bio->state);
1847 raid10_find_phys(conf, r10_bio);
1848 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1849
1850 for (i=0; i<conf->copies; i++) {
1851 int d = r10_bio->devs[i].devnum;
1852 bio = r10_bio->devs[i].bio;
1853 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001854 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001856 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 continue;
1858 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1859 atomic_inc(&r10_bio->remaining);
1860 bio->bi_next = biolist;
1861 biolist = bio;
1862 bio->bi_private = r10_bio;
1863 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001864 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 bio->bi_sector = r10_bio->devs[i].addr +
1866 conf->mirrors[d].rdev->data_offset;
1867 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1868 count++;
1869 }
1870
1871 if (count < 2) {
1872 for (i=0; i<conf->copies; i++) {
1873 int d = r10_bio->devs[i].devnum;
1874 if (r10_bio->devs[i].bio->bi_end_io)
1875 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1876 }
1877 put_buf(r10_bio);
1878 biolist = NULL;
1879 goto giveup;
1880 }
1881 }
1882
1883 for (bio = biolist; bio ; bio=bio->bi_next) {
1884
1885 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1886 if (bio->bi_end_io)
1887 bio->bi_flags |= 1 << BIO_UPTODATE;
1888 bio->bi_vcnt = 0;
1889 bio->bi_idx = 0;
1890 bio->bi_phys_segments = 0;
1891 bio->bi_hw_segments = 0;
1892 bio->bi_size = 0;
1893 }
1894
1895 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001896 if (sector_nr + max_sync < max_sector)
1897 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 do {
1899 struct page *page;
1900 int len = PAGE_SIZE;
1901 disk = 0;
1902 if (sector_nr + (len>>9) > max_sector)
1903 len = (max_sector - sector_nr) << 9;
1904 if (len == 0)
1905 break;
1906 for (bio= biolist ; bio ; bio=bio->bi_next) {
1907 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1908 if (bio_add_page(bio, page, len, 0) == 0) {
1909 /* stop here */
1910 struct bio *bio2;
1911 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1912 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1913 /* remove last page from this bio */
1914 bio2->bi_vcnt--;
1915 bio2->bi_size -= len;
1916 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1917 }
1918 goto bio_full;
1919 }
1920 disk = i;
1921 }
1922 nr_sectors += len>>9;
1923 sector_nr += len>>9;
1924 } while (biolist->bi_vcnt < RESYNC_PAGES);
1925 bio_full:
1926 r10_bio->sectors = nr_sectors;
1927
1928 while (biolist) {
1929 bio = biolist;
1930 biolist = biolist->bi_next;
1931
1932 bio->bi_next = NULL;
1933 r10_bio = bio->bi_private;
1934 r10_bio->sectors = nr_sectors;
1935
1936 if (bio->bi_end_io == end_sync_read) {
1937 md_sync_acct(bio->bi_bdev, nr_sectors);
1938 generic_make_request(bio);
1939 }
1940 }
1941
NeilBrown57afd892005-06-21 17:17:13 -07001942 if (sectors_skipped)
1943 /* pretend they weren't skipped, it makes
1944 * no important difference in this case
1945 */
1946 md_done_sync(mddev, sectors_skipped, 1);
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return sectors_skipped + nr_sectors;
1949 giveup:
1950 /* There is nowhere to write, so all non-sync
1951 * drives must be failed, so try the next chunk...
1952 */
1953 {
NeilBrown57afd892005-06-21 17:17:13 -07001954 sector_t sec = max_sector - sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 sectors_skipped += sec;
1956 chunks_skipped ++;
1957 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 goto skipped;
1959 }
1960}
1961
1962static int run(mddev_t *mddev)
1963{
1964 conf_t *conf;
1965 int i, disk_idx;
1966 mirror_info_t *disk;
1967 mdk_rdev_t *rdev;
1968 struct list_head *tmp;
NeilBrownc93983b2006-06-26 00:27:41 -07001969 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 sector_t stride, size;
1971
NeilBrown2604b702006-01-06 00:20:36 -08001972 if (mddev->chunk_size == 0) {
1973 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
1974 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
NeilBrown2604b702006-01-06 00:20:36 -08001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 nc = mddev->layout & 255;
1978 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07001979 fo = mddev->layout & (1<<16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07001981 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1983 mdname(mddev), mddev->layout);
1984 goto out;
1985 }
1986 /*
1987 * copy the already verified devices into our private RAID10
1988 * bookkeeping area. [whatever we allocate in run(),
1989 * should be freed in stop()]
1990 */
NeilBrown4443ae12006-01-06 00:20:28 -08001991 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 mddev->private = conf;
1993 if (!conf) {
1994 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1995 mdname(mddev));
1996 goto out;
1997 }
NeilBrown4443ae12006-01-06 00:20:28 -08001998 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 GFP_KERNEL);
2000 if (!conf->mirrors) {
2001 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2002 mdname(mddev));
2003 goto out_free_conf;
2004 }
NeilBrown4443ae12006-01-06 00:20:28 -08002005
2006 conf->tmppage = alloc_page(GFP_KERNEL);
2007 if (!conf->tmppage)
2008 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
NeilBrown64a742b2007-02-28 20:11:18 -08002010 conf->mddev = mddev;
2011 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 conf->near_copies = nc;
2013 conf->far_copies = fc;
2014 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002015 conf->far_offset = fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
2017 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
NeilBrown64a742b2007-02-28 20:11:18 -08002018 size = mddev->size >> (conf->chunk_shift-1);
2019 sector_div(size, fc);
2020 size = size * conf->raid_disks;
2021 sector_div(size, nc);
2022 /* 'size' is now the number of chunks in the array */
2023 /* calculate "used chunks per device" in 'stride' */
2024 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002025
2026 /* We need to round up when dividing by raid_disks to
2027 * get the stride size.
2028 */
2029 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002030 sector_div(stride, conf->raid_disks);
2031 mddev->size = stride << (conf->chunk_shift-1);
2032
NeilBrownc93983b2006-06-26 00:27:41 -07002033 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002034 stride = 1;
2035 else
NeilBrownc93983b2006-06-26 00:27:41 -07002036 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002037 conf->stride = stride << conf->chunk_shift;
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2040 r10bio_pool_free, conf);
2041 if (!conf->r10bio_pool) {
2042 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2043 mdname(mddev));
2044 goto out_free_conf;
2045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
NeilBrownd089c6a2008-02-06 01:39:59 -08002047 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 disk_idx = rdev->raid_disk;
2049 if (disk_idx >= mddev->raid_disks
2050 || disk_idx < 0)
2051 continue;
2052 disk = conf->mirrors + disk_idx;
2053
2054 disk->rdev = rdev;
2055
2056 blk_queue_stack_limits(mddev->queue,
2057 rdev->bdev->bd_disk->queue);
2058 /* as we don't honour merge_bvec_fn, we must never risk
2059 * violating it, so limit ->max_sector to one PAGE, as
2060 * a one page request is never in violation.
2061 */
2062 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2063 mddev->queue->max_sectors > (PAGE_SIZE>>9))
2064 mddev->queue->max_sectors = (PAGE_SIZE>>9);
2065
2066 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 spin_lock_init(&conf->device_lock);
2069 INIT_LIST_HEAD(&conf->retry_list);
2070
2071 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08002072 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
NeilBrown6d508242005-09-09 16:24:03 -07002074 /* need to check that every block has at least one working mirror */
2075 if (!enough(conf)) {
2076 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2077 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 goto out_free_conf;
2079 }
2080
2081 mddev->degraded = 0;
2082 for (i = 0; i < conf->raid_disks; i++) {
2083
2084 disk = conf->mirrors + i;
2085
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002086 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002087 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 disk->head_position = 0;
2089 mddev->degraded++;
2090 }
2091 }
2092
2093
2094 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2095 if (!mddev->thread) {
2096 printk(KERN_ERR
2097 "raid10: couldn't allocate thread for %s\n",
2098 mdname(mddev));
2099 goto out_free_conf;
2100 }
2101
2102 printk(KERN_INFO
2103 "raid10: raid set %s active with %d out of %d devices\n",
2104 mdname(mddev), mddev->raid_disks - mddev->degraded,
2105 mddev->raid_disks);
2106 /*
2107 * Ok, everything is just fine now
2108 */
NeilBrown64a742b2007-02-28 20:11:18 -08002109 mddev->array_size = size << (conf->chunk_shift-1);
2110 mddev->resync_max_sectors = size << conf->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
NeilBrown7a5febe2005-05-16 21:53:16 -07002112 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002113 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2114 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* Calculate max read-ahead size.
2117 * We need to readahead at least twice a whole stripe....
2118 * maybe...
2119 */
2120 {
NeilBrown8932c2e2006-06-26 00:27:36 -07002121 int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 stripe /= conf->near_copies;
2123 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2124 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2125 }
2126
2127 if (conf->near_copies < mddev->raid_disks)
2128 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2129 return 0;
2130
2131out_free_conf:
2132 if (conf->r10bio_pool)
2133 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002134 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002135 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 kfree(conf);
2137 mddev->private = NULL;
2138out:
2139 return -EIO;
2140}
2141
2142static int stop(mddev_t *mddev)
2143{
2144 conf_t *conf = mddev_to_conf(mddev);
2145
2146 md_unregister_thread(mddev->thread);
2147 mddev->thread = NULL;
2148 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2149 if (conf->r10bio_pool)
2150 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002151 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 kfree(conf);
2153 mddev->private = NULL;
2154 return 0;
2155}
2156
NeilBrown6cce3b22006-01-06 00:20:16 -08002157static void raid10_quiesce(mddev_t *mddev, int state)
2158{
2159 conf_t *conf = mddev_to_conf(mddev);
2160
2161 switch(state) {
2162 case 1:
2163 raise_barrier(conf, 0);
2164 break;
2165 case 0:
2166 lower_barrier(conf);
2167 break;
2168 }
2169 if (mddev->thread) {
2170 if (mddev->bitmap)
2171 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2172 else
2173 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2174 md_wakeup_thread(mddev->thread);
2175 }
2176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
NeilBrown2604b702006-01-06 00:20:36 -08002178static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002181 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 .owner = THIS_MODULE,
2183 .make_request = make_request,
2184 .run = run,
2185 .stop = stop,
2186 .status = status,
2187 .error_handler = error,
2188 .hot_add_disk = raid10_add_disk,
2189 .hot_remove_disk= raid10_remove_disk,
2190 .spare_active = raid10_spare_active,
2191 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002192 .quiesce = raid10_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193};
2194
2195static int __init raid_init(void)
2196{
NeilBrown2604b702006-01-06 00:20:36 -08002197 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
2200static void raid_exit(void)
2201{
NeilBrown2604b702006-01-06 00:20:36 -08002202 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
2205module_init(raid_init);
2206module_exit(raid_exit);
2207MODULE_LICENSE("GPL");
2208MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002209MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002210MODULE_ALIAS("md-level-10");