blob: faf3d89129799bc8157fa891c0176d985528c189 [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;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700793 mdk_rdev_t *blocked_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
NeilBrowne5dcdd82005-09-09 16:23:41 -0700795 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200796 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700797 return 0;
798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* If this request crosses a chunk boundary, we need to
801 * split it. This will only happen for 1 PAGE (or less) requests.
802 */
803 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
804 > chunk_sects &&
805 conf->near_copies < conf->raid_disks)) {
806 struct bio_pair *bp;
807 /* Sanity check -- queue functions should prevent this happening */
808 if (bio->bi_vcnt != 1 ||
809 bio->bi_idx != 0)
810 goto bad_map;
811 /* This is a one page bio that upper layers
812 * refuse to split for us, so we need to split it.
813 */
814 bp = bio_split(bio, bio_split_pool,
815 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
816 if (make_request(q, &bp->bio1))
817 generic_make_request(&bp->bio1);
818 if (make_request(q, &bp->bio2))
819 generic_make_request(&bp->bio2);
820
821 bio_pair_release(bp);
822 return 0;
823 bad_map:
824 printk("raid10_make_request bug: can't convert block across chunks"
825 " or bigger than %dk %llu %d\n", chunk_sects/2,
826 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
827
NeilBrown6712ecf2007-09-27 12:47:43 +0200828 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830 }
831
NeilBrown3d310eb2005-06-21 17:17:26 -0700832 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /*
835 * Register the new request and wait if the reconstruction
836 * thread has put up a bar for new requests.
837 * Continue immediately if no resync is active currently.
838 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800839 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jens Axboea3623572005-11-01 09:26:16 +0100841 disk_stat_inc(mddev->gendisk, ios[rw]);
842 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
845
846 r10_bio->master_bio = bio;
847 r10_bio->sectors = bio->bi_size >> 9;
848
849 r10_bio->mddev = mddev;
850 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800851 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Jens Axboea3623572005-11-01 09:26:16 +0100853 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /*
855 * read balancing logic:
856 */
857 int disk = read_balance(conf, r10_bio);
858 int slot = r10_bio->read_slot;
859 if (disk < 0) {
860 raid_end_bio_io(r10_bio);
861 return 0;
862 }
863 mirror = conf->mirrors + disk;
864
865 read_bio = bio_clone(bio, GFP_NOIO);
866
867 r10_bio->devs[slot].bio = read_bio;
868
869 read_bio->bi_sector = r10_bio->devs[slot].addr +
870 mirror->rdev->data_offset;
871 read_bio->bi_bdev = mirror->rdev->bdev;
872 read_bio->bi_end_io = raid10_end_read_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800873 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 read_bio->bi_private = r10_bio;
875
876 generic_make_request(read_bio);
877 return 0;
878 }
879
880 /*
881 * WRITE:
882 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700883 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * inc refcount on their rdev. Record them by setting
885 * bios[x] to bio
886 */
887 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700888 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700889 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 rcu_read_lock();
891 for (i = 0; i < conf->copies; i++) {
892 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800893 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700894 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
895 atomic_inc(&rdev->nr_pending);
896 blocked_rdev = rdev;
897 break;
898 }
899 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800900 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800902 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800904 set_bit(R10BIO_Degraded, &r10_bio->state);
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 rcu_read_unlock();
908
Dan Williams6bfe0b42008-04-30 00:52:32 -0700909 if (unlikely(blocked_rdev)) {
910 /* Have to wait for this device to get unblocked, then retry */
911 int j;
912 int d;
913
914 for (j = 0; j < i; j++)
915 if (r10_bio->devs[j].bio) {
916 d = r10_bio->devs[j].devnum;
917 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
918 }
919 allow_barrier(conf);
920 md_wait_for_blocked_rdev(blocked_rdev, mddev);
921 wait_barrier(conf);
922 goto retry_write;
923 }
924
NeilBrown6cce3b22006-01-06 00:20:16 -0800925 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700926
NeilBrown6cce3b22006-01-06 00:20:16 -0800927 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 for (i = 0; i < conf->copies; i++) {
929 struct bio *mbio;
930 int d = r10_bio->devs[i].devnum;
931 if (!r10_bio->devs[i].bio)
932 continue;
933
934 mbio = bio_clone(bio, GFP_NOIO);
935 r10_bio->devs[i].bio = mbio;
936
937 mbio->bi_sector = r10_bio->devs[i].addr+
938 conf->mirrors[d].rdev->data_offset;
939 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
940 mbio->bi_end_io = raid10_end_write_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800941 mbio->bi_rw = WRITE | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 mbio->bi_private = r10_bio;
943
944 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b22006-01-06 00:20:16 -0800945 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947
Arne Redlichf6f953a2007-07-31 00:37:57 -0700948 if (unlikely(!atomic_read(&r10_bio->remaining))) {
949 /* the array is dead */
950 md_write_end(mddev);
951 raid_end_bio_io(r10_bio);
952 return 0;
953 }
954
NeilBrown6cce3b22006-01-06 00:20:16 -0800955 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
956 spin_lock_irqsave(&conf->device_lock, flags);
957 bio_list_merge(&conf->pending_bio_list, &bl);
958 blk_plug_device(mddev->queue);
959 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
NeilBrowna35e63e2008-03-04 14:29:29 -0800961 /* In case raid10d snuck in to freeze_array */
962 wake_up(&conf->wait_barrier);
963
Lars Ellenberge3881a62007-01-10 23:15:37 -0800964 if (do_sync)
965 md_wakeup_thread(mddev->thread);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
970static void status(struct seq_file *seq, mddev_t *mddev)
971{
972 conf_t *conf = mddev_to_conf(mddev);
973 int i;
974
975 if (conf->near_copies < conf->raid_disks)
976 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
977 if (conf->near_copies > 1)
978 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700979 if (conf->far_copies > 1) {
980 if (conf->far_offset)
981 seq_printf(seq, " %d offset-copies", conf->far_copies);
982 else
983 seq_printf(seq, " %d far-copies", conf->far_copies);
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700986 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 for (i = 0; i < conf->raid_disks; i++)
988 seq_printf(seq, "%s",
989 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800990 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 seq_printf(seq, "]");
992}
993
994static void error(mddev_t *mddev, mdk_rdev_t *rdev)
995{
996 char b[BDEVNAME_SIZE];
997 conf_t *conf = mddev_to_conf(mddev);
998
999 /*
1000 * If it is not operational, then we have already marked it as dead
1001 * else if it is the last working disks, ignore the error, let the
1002 * next level up know.
1003 * else mark the drive as failed
1004 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001005 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -07001006 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /*
1008 * Don't fail the drive, just return an IO error.
1009 * The test should really be more sophisticated than
1010 * "working_disks == 1", but it isn't critical, and
1011 * can wait until we do more sophisticated "is the drive
1012 * really dead" tests...
1013 */
1014 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001015 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1016 unsigned long flags;
1017 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001019 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /*
1021 * if recovery is running, make sure it aborts.
1022 */
1023 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
1024 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001025 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001026 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Nick Andrewd7a420c2008-04-28 02:15:55 -07001027 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n"
1028 "raid10: Operation continuing on %d devices.\n",
NeilBrown76186dd2006-10-03 01:15:48 -07001029 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032static void print_conf(conf_t *conf)
1033{
1034 int i;
1035 mirror_info_t *tmp;
1036
1037 printk("RAID10 conf printout:\n");
1038 if (!conf) {
1039 printk("(!conf)\n");
1040 return;
1041 }
NeilBrown76186dd2006-10-03 01:15:48 -07001042 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 conf->raid_disks);
1044
1045 for (i = 0; i < conf->raid_disks; i++) {
1046 char b[BDEVNAME_SIZE];
1047 tmp = conf->mirrors + i;
1048 if (tmp->rdev)
1049 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001050 i, !test_bit(In_sync, &tmp->rdev->flags),
1051 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 bdevname(tmp->rdev->bdev,b));
1053 }
1054}
1055
1056static void close_sync(conf_t *conf)
1057{
NeilBrown0a27ec92006-01-06 00:20:13 -08001058 wait_barrier(conf);
1059 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 mempool_destroy(conf->r10buf_pool);
1062 conf->r10buf_pool = NULL;
1063}
1064
NeilBrown6d508242005-09-09 16:24:03 -07001065/* check if there are enough drives for
1066 * every block to appear on atleast one
1067 */
1068static int enough(conf_t *conf)
1069{
1070 int first = 0;
1071
1072 do {
1073 int n = conf->copies;
1074 int cnt = 0;
1075 while (n--) {
1076 if (conf->mirrors[first].rdev)
1077 cnt++;
1078 first = (first+1) % conf->raid_disks;
1079 }
1080 if (cnt == 0)
1081 return 0;
1082 } while (first != 0);
1083 return 1;
1084}
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static int raid10_spare_active(mddev_t *mddev)
1087{
1088 int i;
1089 conf_t *conf = mddev->private;
1090 mirror_info_t *tmp;
1091
1092 /*
1093 * Find all non-in_sync disks within the RAID10 configuration
1094 * and mark them in_sync
1095 */
1096 for (i = 0; i < conf->raid_disks; i++) {
1097 tmp = conf->mirrors + i;
1098 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001099 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001100 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1101 unsigned long flags;
1102 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001104 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 }
1107
1108 print_conf(conf);
1109 return 0;
1110}
1111
1112
1113static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1114{
1115 conf_t *conf = mddev->private;
1116 int found = 0;
1117 int mirror;
1118 mirror_info_t *p;
1119
1120 if (mddev->recovery_cp < MaxSector)
1121 /* only hot-add to in-sync arrays, as recovery is
1122 * very different from resync
1123 */
1124 return 0;
NeilBrown6d508242005-09-09 16:24:03 -07001125 if (!enough(conf))
1126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
NeilBrown6cce3b22006-01-06 00:20:16 -08001128 if (rdev->saved_raid_disk >= 0 &&
1129 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1130 mirror = rdev->saved_raid_disk;
1131 else
1132 mirror = 0;
1133 for ( ; mirror < mddev->raid_disks; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if ( !(p=conf->mirrors+mirror)->rdev) {
1135
1136 blk_queue_stack_limits(mddev->queue,
1137 rdev->bdev->bd_disk->queue);
1138 /* as we don't honour merge_bvec_fn, we must never risk
1139 * violating it, so limit ->max_sector to one PAGE, as
1140 * a one page request is never in violation.
1141 */
1142 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1143 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1144 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1145
1146 p->head_position = 0;
1147 rdev->raid_disk = mirror;
1148 found = 1;
NeilBrown6cce3b22006-01-06 00:20:16 -08001149 if (rdev->saved_raid_disk != mirror)
1150 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001151 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 }
1154
1155 print_conf(conf);
1156 return found;
1157}
1158
1159static int raid10_remove_disk(mddev_t *mddev, int number)
1160{
1161 conf_t *conf = mddev->private;
1162 int err = 0;
1163 mdk_rdev_t *rdev;
1164 mirror_info_t *p = conf->mirrors+ number;
1165
1166 print_conf(conf);
1167 rdev = p->rdev;
1168 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 atomic_read(&rdev->nr_pending)) {
1171 err = -EBUSY;
1172 goto abort;
1173 }
1174 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001175 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (atomic_read(&rdev->nr_pending)) {
1177 /* lost the race, try later */
1178 err = -EBUSY;
1179 p->rdev = rdev;
1180 }
1181 }
1182abort:
1183
1184 print_conf(conf);
1185 return err;
1186}
1187
1188
NeilBrown6712ecf2007-09-27 12:47:43 +02001189static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1192 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1193 int i,d;
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 for (i=0; i<conf->copies; i++)
1196 if (r10_bio->devs[i].bio == bio)
1197 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001198 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 update_head_pos(i, r10_bio);
1200 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001201
1202 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1203 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001204 else {
1205 atomic_add(r10_bio->sectors,
1206 &conf->mirrors[d].rdev->corrected_errors);
1207 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1208 md_error(r10_bio->mddev,
1209 conf->mirrors[d].rdev);
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /* for reconstruct, we always reschedule after a read.
1213 * for resync, only after all reads
1214 */
1215 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1216 atomic_dec_and_test(&r10_bio->remaining)) {
1217 /* we have read all the blocks,
1218 * do the comparison in process context in raid10d
1219 */
1220 reschedule_retry(r10_bio);
1221 }
1222 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
NeilBrown6712ecf2007-09-27 12:47:43 +02001225static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1228 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1229 mddev_t *mddev = r10_bio->mddev;
1230 conf_t *conf = mddev_to_conf(mddev);
1231 int i,d;
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 for (i = 0; i < conf->copies; i++)
1234 if (r10_bio->devs[i].bio == bio)
1235 break;
1236 d = r10_bio->devs[i].devnum;
1237
1238 if (!uptodate)
1239 md_error(mddev, conf->mirrors[d].rdev);
1240 update_head_pos(i, r10_bio);
1241
1242 while (atomic_dec_and_test(&r10_bio->remaining)) {
1243 if (r10_bio->master_bio == NULL) {
1244 /* the primary of several recovery bios */
1245 md_done_sync(mddev, r10_bio->sectors, 1);
1246 put_buf(r10_bio);
1247 break;
1248 } else {
1249 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1250 put_buf(r10_bio);
1251 r10_bio = r10_bio2;
1252 }
1253 }
1254 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257/*
1258 * Note: sync and recover and handled very differently for raid10
1259 * This code is for resync.
1260 * For resync, we read through virtual addresses and read all blocks.
1261 * If there is any error, we schedule a write. The lowest numbered
1262 * drive is authoritative.
1263 * However requests come for physical address, so we need to map.
1264 * For every physical address there are raid_disks/copies virtual addresses,
1265 * which is always are least one, but is not necessarly an integer.
1266 * This means that a physical address can span multiple chunks, so we may
1267 * have to submit multiple io requests for a single sync request.
1268 */
1269/*
1270 * We check if all blocks are in-sync and only write to blocks that
1271 * aren't in sync
1272 */
1273static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1274{
1275 conf_t *conf = mddev_to_conf(mddev);
1276 int i, first;
1277 struct bio *tbio, *fbio;
1278
1279 atomic_set(&r10_bio->remaining, 1);
1280
1281 /* find the first device with a block */
1282 for (i=0; i<conf->copies; i++)
1283 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1284 break;
1285
1286 if (i == conf->copies)
1287 goto done;
1288
1289 first = i;
1290 fbio = r10_bio->devs[i].bio;
1291
1292 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001293 for (i=0 ; i < conf->copies ; i++) {
1294 int j, d;
1295 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001298
1299 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001301 if (i == first)
1302 continue;
1303 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1304 /* We know that the bi_io_vec layout is the same for
1305 * both 'first' and 'i', so we just compare them.
1306 * All vec entries are PAGE_SIZE;
1307 */
1308 for (j = 0; j < vcnt; j++)
1309 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1310 page_address(tbio->bi_io_vec[j].bv_page),
1311 PAGE_SIZE))
1312 break;
1313 if (j == vcnt)
1314 continue;
1315 mddev->resync_mismatches += r10_bio->sectors;
1316 }
NeilBrown18f08812006-01-06 00:20:25 -08001317 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1318 /* Don't fix anything. */
1319 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* Ok, we need to write this bio
1321 * First we need to fixup bv_offset, bv_len and
1322 * bi_vecs, as the read request might have corrupted these
1323 */
1324 tbio->bi_vcnt = vcnt;
1325 tbio->bi_size = r10_bio->sectors << 9;
1326 tbio->bi_idx = 0;
1327 tbio->bi_phys_segments = 0;
1328 tbio->bi_hw_segments = 0;
1329 tbio->bi_hw_front_size = 0;
1330 tbio->bi_hw_back_size = 0;
1331 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1332 tbio->bi_flags |= 1 << BIO_UPTODATE;
1333 tbio->bi_next = NULL;
1334 tbio->bi_rw = WRITE;
1335 tbio->bi_private = r10_bio;
1336 tbio->bi_sector = r10_bio->devs[i].addr;
1337
1338 for (j=0; j < vcnt ; j++) {
1339 tbio->bi_io_vec[j].bv_offset = 0;
1340 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1341
1342 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1343 page_address(fbio->bi_io_vec[j].bv_page),
1344 PAGE_SIZE);
1345 }
1346 tbio->bi_end_io = end_sync_write;
1347
1348 d = r10_bio->devs[i].devnum;
1349 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1350 atomic_inc(&r10_bio->remaining);
1351 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1352
1353 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1354 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1355 generic_make_request(tbio);
1356 }
1357
1358done:
1359 if (atomic_dec_and_test(&r10_bio->remaining)) {
1360 md_done_sync(mddev, r10_bio->sectors, 1);
1361 put_buf(r10_bio);
1362 }
1363}
1364
1365/*
1366 * Now for the recovery code.
1367 * Recovery happens across physical sectors.
1368 * We recover all non-is_sync drives by finding the virtual address of
1369 * each, and then choose a working drive that also has that virt address.
1370 * There is a separate r10_bio for each non-in_sync drive.
1371 * Only the first two slots are in use. The first for reading,
1372 * The second for writing.
1373 *
1374 */
1375
1376static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1377{
1378 conf_t *conf = mddev_to_conf(mddev);
1379 int i, d;
1380 struct bio *bio, *wbio;
1381
1382
1383 /* move the pages across to the second bio
1384 * and submit the write request
1385 */
1386 bio = r10_bio->devs[0].bio;
1387 wbio = r10_bio->devs[1].bio;
1388 for (i=0; i < wbio->bi_vcnt; i++) {
1389 struct page *p = bio->bi_io_vec[i].bv_page;
1390 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1391 wbio->bi_io_vec[i].bv_page = p;
1392 }
1393 d = r10_bio->devs[1].devnum;
1394
1395 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1396 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001397 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1398 generic_make_request(wbio);
1399 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001400 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403
1404/*
1405 * This is a kernel thread which:
1406 *
1407 * 1. Retries failed read operations on working mirrors.
1408 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001409 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
1411
NeilBrown6814d532006-10-03 01:15:45 -07001412static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1413{
1414 int sect = 0; /* Offset from r10_bio->sector */
1415 int sectors = r10_bio->sectors;
1416 mdk_rdev_t*rdev;
1417 while(sectors) {
1418 int s = sectors;
1419 int sl = r10_bio->read_slot;
1420 int success = 0;
1421 int start;
1422
1423 if (s > (PAGE_SIZE>>9))
1424 s = PAGE_SIZE >> 9;
1425
1426 rcu_read_lock();
1427 do {
1428 int d = r10_bio->devs[sl].devnum;
1429 rdev = rcu_dereference(conf->mirrors[d].rdev);
1430 if (rdev &&
1431 test_bit(In_sync, &rdev->flags)) {
1432 atomic_inc(&rdev->nr_pending);
1433 rcu_read_unlock();
1434 success = sync_page_io(rdev->bdev,
1435 r10_bio->devs[sl].addr +
1436 sect + rdev->data_offset,
1437 s<<9,
1438 conf->tmppage, READ);
1439 rdev_dec_pending(rdev, mddev);
1440 rcu_read_lock();
1441 if (success)
1442 break;
1443 }
1444 sl++;
1445 if (sl == conf->copies)
1446 sl = 0;
1447 } while (!success && sl != r10_bio->read_slot);
1448 rcu_read_unlock();
1449
1450 if (!success) {
1451 /* Cannot read from anywhere -- bye bye array */
1452 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1453 md_error(mddev, conf->mirrors[dn].rdev);
1454 break;
1455 }
1456
1457 start = sl;
1458 /* write it back and re-read */
1459 rcu_read_lock();
1460 while (sl != r10_bio->read_slot) {
1461 int d;
1462 if (sl==0)
1463 sl = conf->copies;
1464 sl--;
1465 d = r10_bio->devs[sl].devnum;
1466 rdev = rcu_dereference(conf->mirrors[d].rdev);
1467 if (rdev &&
1468 test_bit(In_sync, &rdev->flags)) {
1469 atomic_inc(&rdev->nr_pending);
1470 rcu_read_unlock();
1471 atomic_add(s, &rdev->corrected_errors);
1472 if (sync_page_io(rdev->bdev,
1473 r10_bio->devs[sl].addr +
1474 sect + rdev->data_offset,
1475 s<<9, conf->tmppage, WRITE)
1476 == 0)
1477 /* Well, this device is dead */
1478 md_error(mddev, rdev);
1479 rdev_dec_pending(rdev, mddev);
1480 rcu_read_lock();
1481 }
1482 }
1483 sl = start;
1484 while (sl != r10_bio->read_slot) {
1485 int d;
1486 if (sl==0)
1487 sl = conf->copies;
1488 sl--;
1489 d = r10_bio->devs[sl].devnum;
1490 rdev = rcu_dereference(conf->mirrors[d].rdev);
1491 if (rdev &&
1492 test_bit(In_sync, &rdev->flags)) {
1493 char b[BDEVNAME_SIZE];
1494 atomic_inc(&rdev->nr_pending);
1495 rcu_read_unlock();
1496 if (sync_page_io(rdev->bdev,
1497 r10_bio->devs[sl].addr +
1498 sect + rdev->data_offset,
1499 s<<9, conf->tmppage, READ) == 0)
1500 /* Well, this device is dead */
1501 md_error(mddev, rdev);
1502 else
1503 printk(KERN_INFO
1504 "raid10:%s: read error corrected"
1505 " (%d sectors at %llu on %s)\n",
1506 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001507 (unsigned long long)(sect+
1508 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001509 bdevname(rdev->bdev, b));
1510
1511 rdev_dec_pending(rdev, mddev);
1512 rcu_read_lock();
1513 }
1514 }
1515 rcu_read_unlock();
1516
1517 sectors -= s;
1518 sect += s;
1519 }
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522static void raid10d(mddev_t *mddev)
1523{
1524 r10bio_t *r10_bio;
1525 struct bio *bio;
1526 unsigned long flags;
1527 conf_t *conf = mddev_to_conf(mddev);
1528 struct list_head *head = &conf->retry_list;
1529 int unplug=0;
1530 mdk_rdev_t *rdev;
1531
1532 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 for (;;) {
1535 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001536
1537 unplug += flush_pending_writes(conf);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001540 if (list_empty(head)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001541 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1545 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001546 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_unlock_irqrestore(&conf->device_lock, flags);
1548
1549 mddev = r10_bio->mddev;
1550 conf = mddev_to_conf(mddev);
1551 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1552 sync_request_write(mddev, r10_bio);
1553 unplug = 1;
1554 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1555 recovery_request_write(mddev, r10_bio);
1556 unplug = 1;
1557 } else {
1558 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001559 /* we got a read error. Maybe the drive is bad. Maybe just
1560 * the block and we can fix it.
1561 * We freeze all other IO, and try reading the block from
1562 * other devices. When we find one, we re-write
1563 * and check it that fixes the read error.
1564 * This is all done synchronously while the array is
1565 * frozen.
1566 */
NeilBrown6814d532006-10-03 01:15:45 -07001567 if (mddev->ro == 0) {
1568 freeze_array(conf);
1569 fix_read_error(conf, mddev, r10_bio);
1570 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001571 }
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001574 r10_bio->devs[r10_bio->read_slot].bio =
1575 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mirror = read_balance(conf, r10_bio);
1577 if (mirror == -1) {
1578 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1579 " read error for block %llu\n",
1580 bdevname(bio->bi_bdev,b),
1581 (unsigned long long)r10_bio->sector);
1582 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001583 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 } else {
Lars Ellenberge3881a62007-01-10 23:15:37 -08001585 const int do_sync = bio_sync(r10_bio->master_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001586 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 rdev = conf->mirrors[mirror].rdev;
1588 if (printk_ratelimit())
1589 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1590 " another mirror\n",
1591 bdevname(rdev->bdev,b),
1592 (unsigned long long)r10_bio->sector);
1593 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1594 r10_bio->devs[r10_bio->read_slot].bio = bio;
1595 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1596 + rdev->data_offset;
1597 bio->bi_bdev = rdev->bdev;
Lars Ellenberge3881a62007-01-10 23:15:37 -08001598 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 bio->bi_private = r10_bio;
1600 bio->bi_end_io = raid10_end_read_request;
1601 unplug = 1;
1602 generic_make_request(bio);
1603 }
1604 }
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (unplug)
1607 unplug_slaves(mddev);
1608}
1609
1610
1611static int init_resync(conf_t *conf)
1612{
1613 int buffs;
1614
1615 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001616 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1618 if (!conf->r10buf_pool)
1619 return -ENOMEM;
1620 conf->next_resync = 0;
1621 return 0;
1622}
1623
1624/*
1625 * perform a "sync" on one "block"
1626 *
1627 * We need to make sure that no normal I/O request - particularly write
1628 * requests - conflict with active sync requests.
1629 *
1630 * This is achieved by tracking pending requests and a 'barrier' concept
1631 * that can be installed to exclude normal IO requests.
1632 *
1633 * Resync and recovery are handled very differently.
1634 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1635 *
1636 * For resync, we iterate over virtual addresses, read all copies,
1637 * and update if there are differences. If only one copy is live,
1638 * skip it.
1639 * For recovery, we iterate over physical addresses, read a good
1640 * value for each non-in_sync drive, and over-write.
1641 *
1642 * So, for recovery we may have several outstanding complex requests for a
1643 * given address, one for each out-of-sync device. We model this by allocating
1644 * a number of r10_bio structures, one for each out-of-sync device.
1645 * As we setup these structures, we collect all bio's together into a list
1646 * which we then process collectively to add pages, and then process again
1647 * to pass to generic_make_request.
1648 *
1649 * The r10_bio structures are linked using a borrowed master_bio pointer.
1650 * This link is counted in ->remaining. When the r10_bio that points to NULL
1651 * has its remaining count decremented to 0, the whole complex operation
1652 * is complete.
1653 *
1654 */
1655
NeilBrown57afd892005-06-21 17:17:13 -07001656static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 conf_t *conf = mddev_to_conf(mddev);
1659 r10bio_t *r10_bio;
1660 struct bio *biolist = NULL, *bio;
1661 sector_t max_sector, nr_sectors;
1662 int disk;
1663 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001664 int max_sync;
1665 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 sector_t sectors_skipped = 0;
1668 int chunks_skipped = 0;
1669
1670 if (!conf->r10buf_pool)
1671 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 skipped:
1675 max_sector = mddev->size << 1;
1676 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1677 max_sector = mddev->resync_max_sectors;
1678 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001679 /* If we aborted, we need to abort the
1680 * sync on the 'current' bitmap chucks (there can
1681 * be several when recovering multiple devices).
1682 * as we may have started syncing it but not finished.
1683 * We can find the current address in
1684 * mddev->curr_resync, but for recovery,
1685 * we need to convert that to several
1686 * virtual addresses.
1687 */
1688 if (mddev->curr_resync < max_sector) { /* aborted */
1689 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1690 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1691 &sync_blocks, 1);
1692 else for (i=0; i<conf->raid_disks; i++) {
1693 sector_t sect =
1694 raid10_find_virt(conf, mddev->curr_resync, i);
1695 bitmap_end_sync(mddev->bitmap, sect,
1696 &sync_blocks, 1);
1697 }
1698 } else /* completed sync */
1699 conf->fullsync = 0;
1700
1701 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001703 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return sectors_skipped;
1705 }
1706 if (chunks_skipped >= conf->raid_disks) {
1707 /* if there has been nothing to do on any drive,
1708 * then there is nothing to do at all..
1709 */
NeilBrown57afd892005-06-21 17:17:13 -07001710 *skipped = 1;
1711 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
NeilBrownc6207272008-02-06 01:39:52 -08001714 if (max_sector > mddev->resync_max)
1715 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /* make sure whole request will fit in a chunk - if chunks
1718 * are meaningful
1719 */
1720 if (conf->near_copies < conf->raid_disks &&
1721 max_sector > (sector_nr | conf->chunk_mask))
1722 max_sector = (sector_nr | conf->chunk_mask) + 1;
1723 /*
1724 * If there is non-resync activity waiting for us then
1725 * put in a delay to throttle resync.
1726 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001727 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
NeilBrownb47490c2008-02-06 01:39:50 -08001730 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Again, very different code for resync and recovery.
1733 * Both must result in an r10bio with a list of bios that
1734 * have bi_end_io, bi_sector, bi_bdev set,
1735 * and bi_private set to the r10bio.
1736 * For recovery, we may actually create several r10bios
1737 * with 2 bios in each, that correspond to the bios in the main one.
1738 * In this case, the subordinate r10bios link back through a
1739 * borrowed master_bio pointer, and the counter in the master
1740 * includes a ref from each subordinate.
1741 */
1742 /* First, we decide what to do and set ->bi_end_io
1743 * To end_sync_read if we want to read, and
1744 * end_sync_write if we will want to write.
1745 */
1746
NeilBrown6cce3b22006-01-06 00:20:16 -08001747 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1749 /* recovery... the complicated one */
1750 int i, j, k;
1751 r10_bio = NULL;
1752
1753 for (i=0 ; i<conf->raid_disks; i++)
1754 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001755 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001756 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /* want to reconstruct this device */
1758 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001759 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1760 int must_sync;
1761 /* Unless we are doing a full sync, we only need
1762 * to recover the block if it is set in the bitmap
1763 */
1764 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1765 &sync_blocks, 1);
1766 if (sync_blocks < max_sync)
1767 max_sync = sync_blocks;
1768 if (!must_sync &&
1769 !conf->fullsync) {
1770 /* yep, skip the sync_blocks here, but don't assume
1771 * that there will never be anything to do here
1772 */
1773 chunks_skipped = -1;
1774 continue;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001778 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 atomic_set(&r10_bio->remaining, 0);
1780
1781 r10_bio->master_bio = (struct bio*)rb2;
1782 if (rb2)
1783 atomic_inc(&rb2->remaining);
1784 r10_bio->mddev = mddev;
1785 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001786 r10_bio->sector = sect;
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 raid10_find_phys(conf, r10_bio);
NeilBrown6cce3b22006-01-06 00:20:16 -08001789 /* Need to check if this section will still be
1790 * degraded
1791 */
1792 for (j=0; j<conf->copies;j++) {
1793 int d = r10_bio->devs[j].devnum;
1794 if (conf->mirrors[d].rdev == NULL ||
NeilBrowna24a8dd2006-01-06 00:20:35 -08001795 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001796 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001797 break;
1798 }
NeilBrown6cce3b22006-01-06 00:20:16 -08001799 }
1800 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1801 &sync_blocks, still_degraded);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 for (j=0; j<conf->copies;j++) {
1804 int d = r10_bio->devs[j].devnum;
1805 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001806 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /* This is where we read from */
1808 bio = r10_bio->devs[0].bio;
1809 bio->bi_next = biolist;
1810 biolist = bio;
1811 bio->bi_private = r10_bio;
1812 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001813 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 bio->bi_sector = r10_bio->devs[j].addr +
1815 conf->mirrors[d].rdev->data_offset;
1816 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1817 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1818 atomic_inc(&r10_bio->remaining);
1819 /* and we write to 'i' */
1820
1821 for (k=0; k<conf->copies; k++)
1822 if (r10_bio->devs[k].devnum == i)
1823 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001824 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 bio = r10_bio->devs[1].bio;
1826 bio->bi_next = biolist;
1827 biolist = bio;
1828 bio->bi_private = r10_bio;
1829 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001830 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 bio->bi_sector = r10_bio->devs[k].addr +
1832 conf->mirrors[i].rdev->data_offset;
1833 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1834
1835 r10_bio->devs[0].devnum = d;
1836 r10_bio->devs[1].devnum = i;
1837
1838 break;
1839 }
1840 }
1841 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001842 /* Cannot recover, so abort the recovery */
1843 put_buf(r10_bio);
K.Tanakaa07e6ab2008-03-04 14:29:37 -08001844 if (rb2)
1845 atomic_dec(&rb2->remaining);
NeilBrown87fc7672005-09-09 16:24:04 -07001846 r10_bio = rb2;
1847 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1848 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1849 mdname(mddev));
1850 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852 }
1853 if (biolist == NULL) {
1854 while (r10_bio) {
1855 r10bio_t *rb2 = r10_bio;
1856 r10_bio = (r10bio_t*) rb2->master_bio;
1857 rb2->master_bio = NULL;
1858 put_buf(rb2);
1859 }
1860 goto giveup;
1861 }
1862 } else {
1863 /* resync. Schedule a read for every block at this virt offset */
1864 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001865
1866 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1867 &sync_blocks, mddev->degraded) &&
1868 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1869 /* We can skip this block */
1870 *skipped = 1;
1871 return sync_blocks + sectors_skipped;
1872 }
1873 if (sync_blocks < max_sync)
1874 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 r10_bio->mddev = mddev;
1878 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001879 raise_barrier(conf, 0);
1880 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 r10_bio->master_bio = NULL;
1883 r10_bio->sector = sector_nr;
1884 set_bit(R10BIO_IsSync, &r10_bio->state);
1885 raid10_find_phys(conf, r10_bio);
1886 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1887
1888 for (i=0; i<conf->copies; i++) {
1889 int d = r10_bio->devs[i].devnum;
1890 bio = r10_bio->devs[i].bio;
1891 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001892 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001894 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 continue;
1896 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1897 atomic_inc(&r10_bio->remaining);
1898 bio->bi_next = biolist;
1899 biolist = bio;
1900 bio->bi_private = r10_bio;
1901 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001902 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 bio->bi_sector = r10_bio->devs[i].addr +
1904 conf->mirrors[d].rdev->data_offset;
1905 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1906 count++;
1907 }
1908
1909 if (count < 2) {
1910 for (i=0; i<conf->copies; i++) {
1911 int d = r10_bio->devs[i].devnum;
1912 if (r10_bio->devs[i].bio->bi_end_io)
1913 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1914 }
1915 put_buf(r10_bio);
1916 biolist = NULL;
1917 goto giveup;
1918 }
1919 }
1920
1921 for (bio = biolist; bio ; bio=bio->bi_next) {
1922
1923 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1924 if (bio->bi_end_io)
1925 bio->bi_flags |= 1 << BIO_UPTODATE;
1926 bio->bi_vcnt = 0;
1927 bio->bi_idx = 0;
1928 bio->bi_phys_segments = 0;
1929 bio->bi_hw_segments = 0;
1930 bio->bi_size = 0;
1931 }
1932
1933 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001934 if (sector_nr + max_sync < max_sector)
1935 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 do {
1937 struct page *page;
1938 int len = PAGE_SIZE;
1939 disk = 0;
1940 if (sector_nr + (len>>9) > max_sector)
1941 len = (max_sector - sector_nr) << 9;
1942 if (len == 0)
1943 break;
1944 for (bio= biolist ; bio ; bio=bio->bi_next) {
1945 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1946 if (bio_add_page(bio, page, len, 0) == 0) {
1947 /* stop here */
1948 struct bio *bio2;
1949 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1950 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1951 /* remove last page from this bio */
1952 bio2->bi_vcnt--;
1953 bio2->bi_size -= len;
1954 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1955 }
1956 goto bio_full;
1957 }
1958 disk = i;
1959 }
1960 nr_sectors += len>>9;
1961 sector_nr += len>>9;
1962 } while (biolist->bi_vcnt < RESYNC_PAGES);
1963 bio_full:
1964 r10_bio->sectors = nr_sectors;
1965
1966 while (biolist) {
1967 bio = biolist;
1968 biolist = biolist->bi_next;
1969
1970 bio->bi_next = NULL;
1971 r10_bio = bio->bi_private;
1972 r10_bio->sectors = nr_sectors;
1973
1974 if (bio->bi_end_io == end_sync_read) {
1975 md_sync_acct(bio->bi_bdev, nr_sectors);
1976 generic_make_request(bio);
1977 }
1978 }
1979
NeilBrown57afd892005-06-21 17:17:13 -07001980 if (sectors_skipped)
1981 /* pretend they weren't skipped, it makes
1982 * no important difference in this case
1983 */
1984 md_done_sync(mddev, sectors_skipped, 1);
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return sectors_skipped + nr_sectors;
1987 giveup:
1988 /* There is nowhere to write, so all non-sync
1989 * drives must be failed, so try the next chunk...
1990 */
1991 {
NeilBrown57afd892005-06-21 17:17:13 -07001992 sector_t sec = max_sector - sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 sectors_skipped += sec;
1994 chunks_skipped ++;
1995 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto skipped;
1997 }
1998}
1999
2000static int run(mddev_t *mddev)
2001{
2002 conf_t *conf;
2003 int i, disk_idx;
2004 mirror_info_t *disk;
2005 mdk_rdev_t *rdev;
2006 struct list_head *tmp;
NeilBrownc93983b2006-06-26 00:27:41 -07002007 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 sector_t stride, size;
2009
NeilBrown2604b702006-01-06 00:20:36 -08002010 if (mddev->chunk_size == 0) {
2011 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
2012 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
NeilBrown2604b702006-01-06 00:20:36 -08002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 nc = mddev->layout & 255;
2016 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07002017 fo = mddev->layout & (1<<16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07002019 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2021 mdname(mddev), mddev->layout);
2022 goto out;
2023 }
2024 /*
2025 * copy the already verified devices into our private RAID10
2026 * bookkeeping area. [whatever we allocate in run(),
2027 * should be freed in stop()]
2028 */
NeilBrown4443ae12006-01-06 00:20:28 -08002029 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 mddev->private = conf;
2031 if (!conf) {
2032 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2033 mdname(mddev));
2034 goto out;
2035 }
NeilBrown4443ae12006-01-06 00:20:28 -08002036 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 GFP_KERNEL);
2038 if (!conf->mirrors) {
2039 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2040 mdname(mddev));
2041 goto out_free_conf;
2042 }
NeilBrown4443ae12006-01-06 00:20:28 -08002043
2044 conf->tmppage = alloc_page(GFP_KERNEL);
2045 if (!conf->tmppage)
2046 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
NeilBrown64a742b2007-02-28 20:11:18 -08002048 conf->mddev = mddev;
2049 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 conf->near_copies = nc;
2051 conf->far_copies = fc;
2052 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002053 conf->far_offset = fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
2055 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
NeilBrown64a742b2007-02-28 20:11:18 -08002056 size = mddev->size >> (conf->chunk_shift-1);
2057 sector_div(size, fc);
2058 size = size * conf->raid_disks;
2059 sector_div(size, nc);
2060 /* 'size' is now the number of chunks in the array */
2061 /* calculate "used chunks per device" in 'stride' */
2062 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002063
2064 /* We need to round up when dividing by raid_disks to
2065 * get the stride size.
2066 */
2067 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002068 sector_div(stride, conf->raid_disks);
2069 mddev->size = stride << (conf->chunk_shift-1);
2070
NeilBrownc93983b2006-06-26 00:27:41 -07002071 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002072 stride = 1;
2073 else
NeilBrownc93983b2006-06-26 00:27:41 -07002074 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002075 conf->stride = stride << conf->chunk_shift;
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2078 r10bio_pool_free, conf);
2079 if (!conf->r10bio_pool) {
2080 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2081 mdname(mddev));
2082 goto out_free_conf;
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
NeilBrownd089c6a2008-02-06 01:39:59 -08002085 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 disk_idx = rdev->raid_disk;
2087 if (disk_idx >= mddev->raid_disks
2088 || disk_idx < 0)
2089 continue;
2090 disk = conf->mirrors + disk_idx;
2091
2092 disk->rdev = rdev;
2093
2094 blk_queue_stack_limits(mddev->queue,
2095 rdev->bdev->bd_disk->queue);
2096 /* as we don't honour merge_bvec_fn, we must never risk
2097 * violating it, so limit ->max_sector to one PAGE, as
2098 * a one page request is never in violation.
2099 */
2100 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2101 mddev->queue->max_sectors > (PAGE_SIZE>>9))
2102 mddev->queue->max_sectors = (PAGE_SIZE>>9);
2103
2104 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 spin_lock_init(&conf->device_lock);
2107 INIT_LIST_HEAD(&conf->retry_list);
2108
2109 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08002110 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
NeilBrown6d508242005-09-09 16:24:03 -07002112 /* need to check that every block has at least one working mirror */
2113 if (!enough(conf)) {
2114 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2115 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 goto out_free_conf;
2117 }
2118
2119 mddev->degraded = 0;
2120 for (i = 0; i < conf->raid_disks; i++) {
2121
2122 disk = conf->mirrors + i;
2123
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002124 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002125 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 disk->head_position = 0;
2127 mddev->degraded++;
2128 }
2129 }
2130
2131
2132 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2133 if (!mddev->thread) {
2134 printk(KERN_ERR
2135 "raid10: couldn't allocate thread for %s\n",
2136 mdname(mddev));
2137 goto out_free_conf;
2138 }
2139
2140 printk(KERN_INFO
2141 "raid10: raid set %s active with %d out of %d devices\n",
2142 mdname(mddev), mddev->raid_disks - mddev->degraded,
2143 mddev->raid_disks);
2144 /*
2145 * Ok, everything is just fine now
2146 */
NeilBrown64a742b2007-02-28 20:11:18 -08002147 mddev->array_size = size << (conf->chunk_shift-1);
2148 mddev->resync_max_sectors = size << conf->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
NeilBrown7a5febe2005-05-16 21:53:16 -07002150 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002151 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2152 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* Calculate max read-ahead size.
2155 * We need to readahead at least twice a whole stripe....
2156 * maybe...
2157 */
2158 {
NeilBrown8932c2e2006-06-26 00:27:36 -07002159 int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 stripe /= conf->near_copies;
2161 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2162 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2163 }
2164
2165 if (conf->near_copies < mddev->raid_disks)
2166 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2167 return 0;
2168
2169out_free_conf:
2170 if (conf->r10bio_pool)
2171 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002172 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002173 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 kfree(conf);
2175 mddev->private = NULL;
2176out:
2177 return -EIO;
2178}
2179
2180static int stop(mddev_t *mddev)
2181{
2182 conf_t *conf = mddev_to_conf(mddev);
2183
2184 md_unregister_thread(mddev->thread);
2185 mddev->thread = NULL;
2186 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2187 if (conf->r10bio_pool)
2188 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002189 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 kfree(conf);
2191 mddev->private = NULL;
2192 return 0;
2193}
2194
NeilBrown6cce3b22006-01-06 00:20:16 -08002195static void raid10_quiesce(mddev_t *mddev, int state)
2196{
2197 conf_t *conf = mddev_to_conf(mddev);
2198
2199 switch(state) {
2200 case 1:
2201 raise_barrier(conf, 0);
2202 break;
2203 case 0:
2204 lower_barrier(conf);
2205 break;
2206 }
2207 if (mddev->thread) {
2208 if (mddev->bitmap)
2209 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2210 else
2211 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2212 md_wakeup_thread(mddev->thread);
2213 }
2214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
NeilBrown2604b702006-01-06 00:20:36 -08002216static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002219 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 .owner = THIS_MODULE,
2221 .make_request = make_request,
2222 .run = run,
2223 .stop = stop,
2224 .status = status,
2225 .error_handler = error,
2226 .hot_add_disk = raid10_add_disk,
2227 .hot_remove_disk= raid10_remove_disk,
2228 .spare_active = raid10_spare_active,
2229 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002230 .quiesce = raid10_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231};
2232
2233static int __init raid_init(void)
2234{
NeilBrown2604b702006-01-06 00:20:36 -08002235 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238static void raid_exit(void)
2239{
NeilBrown2604b702006-01-06 00:20:36 -08002240 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
2243module_init(raid_init);
2244module_exit(raid_exit);
2245MODULE_LICENSE("GPL");
2246MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002247MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002248MODULE_ALIAS("md-level-10");