blob: 22bb2b1b886d96887948f25fd8efbc199b3b3079 [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
NeilBrown6cce3b232006-01-06 00:20:16 -080021#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/raid/raid10.h>
NeilBrown6cce3b232006-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 */
NeilBrown6cce3b232006-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);
NeilBrown6cce3b232006-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)) {
NeilBrown6cce3b232006-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
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200442 * @bvm: properties of new bio
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * @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 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200449static int raid10_mergeable_bvec(struct request_queue *q,
450 struct bvec_merge_data *bvm,
451 struct bio_vec *biovec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200454 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int max;
456 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200457 unsigned int bio_sectors = bvm->bi_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
460 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +0200461 if (max <= biovec->bv_len && bio_sectors == 0)
462 return biovec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 else
464 return max;
465}
466
467/*
468 * This routine returns the disk from which the requested read should
469 * be done. There is a per-array 'next expected sequential IO' sector
470 * number - if this matches on the next IO then we use the last disk.
471 * There is also a per-disk 'last know head position' sector that is
472 * maintained from IRQ contexts, both the normal and the resync IO
473 * completion handlers update this position correctly. If there is no
474 * perfect sequential match then we pick the disk whose head is closest.
475 *
476 * If there are 2 mirrors in the same 2 devices, performance degrades
477 * because position is mirror, not device based.
478 *
479 * The rdev for the device selected will have nr_pending incremented.
480 */
481
482/*
483 * FIXME: possibly should rethink readbalancing and do it differently
484 * depending on near_copies / far_copies geometry.
485 */
486static int read_balance(conf_t *conf, r10bio_t *r10_bio)
487{
488 const unsigned long this_sector = r10_bio->sector;
489 int disk, slot, nslot;
490 const int sectors = r10_bio->sectors;
491 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800492 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 raid10_find_phys(conf, r10_bio);
495 rcu_read_lock();
496 /*
497 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b232006-01-06 00:20:16 -0800498 * device if no resync is going on (recovery is ok), or below
499 * the resync window. We take the first readable disk when
500 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
502 if (conf->mddev->recovery_cp < MaxSector
503 && (this_sector + sectors >= conf->next_resync)) {
504 /* make sure that disk is operational */
505 slot = 0;
506 disk = r10_bio->devs[slot].devnum;
507
Suzanne Woodd6065f72005-11-08 21:39:27 -0800508 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800509 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800510 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 slot++;
512 if (slot == conf->copies) {
513 slot = 0;
514 disk = -1;
515 break;
516 }
517 disk = r10_bio->devs[slot].devnum;
518 }
519 goto rb_out;
520 }
521
522
523 /* make sure the disk is operational */
524 slot = 0;
525 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800526 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800527 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800528 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 slot ++;
530 if (slot == conf->copies) {
531 disk = -1;
532 goto rb_out;
533 }
534 disk = r10_bio->devs[slot].devnum;
535 }
536
537
NeilBrown3ec67ac2005-09-09 16:23:40 -0700538 current_distance = abs(r10_bio->devs[slot].addr -
539 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800541 /* Find the disk whose head is closest,
542 * or - for far > 1 - find the closest to partition beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 for (nslot = slot; nslot < conf->copies; nslot++) {
545 int ndisk = r10_bio->devs[nslot].devnum;
546
547
Suzanne Woodd6065f72005-11-08 21:39:27 -0800548 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800549 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800550 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 continue;
552
NeilBrown22dfdf52005-11-28 13:44:09 -0800553 /* This optimisation is debatable, and completely destroys
554 * sequential read speed for 'far copies' arrays. So only
555 * keep it for 'near' arrays, and review those later.
556 */
557 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 disk = ndisk;
559 slot = nslot;
560 break;
561 }
Keld Simonsen8ed3a192008-03-04 14:29:34 -0800562
563 /* for far > 1 always use the lowest address */
564 if (conf->far_copies > 1)
565 new_distance = r10_bio->devs[nslot].addr;
566 else
567 new_distance = abs(r10_bio->devs[nslot].addr -
568 conf->mirrors[ndisk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (new_distance < current_distance) {
570 current_distance = new_distance;
571 disk = ndisk;
572 slot = nslot;
573 }
574 }
575
576rb_out:
577 r10_bio->read_slot = slot;
578/* conf->next_seq_sect = this_sector + sectors;*/
579
Suzanne Woodd6065f72005-11-08 21:39:27 -0800580 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800582 else
583 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 rcu_read_unlock();
585
586 return disk;
587}
588
589static void unplug_slaves(mddev_t *mddev)
590{
591 conf_t *conf = mddev_to_conf(mddev);
592 int i;
593
594 rcu_read_lock();
595 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800596 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800597 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200598 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 atomic_inc(&rdev->nr_pending);
601 rcu_read_unlock();
602
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500603 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 rdev_dec_pending(rdev, mddev);
606 rcu_read_lock();
607 }
608 }
609 rcu_read_unlock();
610}
611
Jens Axboe165125e2007-07-24 09:28:11 +0200612static void raid10_unplug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
NeilBrown6cce3b232006-01-06 00:20:16 -0800614 mddev_t *mddev = q->queuedata;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 unplug_slaves(q->queuedata);
NeilBrown6cce3b232006-01-06 00:20:16 -0800617 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
NeilBrown0d129222006-10-03 01:15:54 -0700620static int raid10_congested(void *data, int bits)
621{
622 mddev_t *mddev = data;
623 conf_t *conf = mddev_to_conf(mddev);
624 int i, ret = 0;
625
626 rcu_read_lock();
627 for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
628 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
629 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Jens Axboe165125e2007-07-24 09:28:11 +0200630 struct request_queue *q = bdev_get_queue(rdev->bdev);
NeilBrown0d129222006-10-03 01:15:54 -0700631
632 ret |= bdi_congested(&q->backing_dev_info, bits);
633 }
634 }
635 rcu_read_unlock();
636 return ret;
637}
638
NeilBrowna35e63e2008-03-04 14:29:29 -0800639static int flush_pending_writes(conf_t *conf)
640{
641 /* Any writes that have been queued but are awaiting
642 * bitmap updates get flushed here.
643 * We return 1 if any requests were actually submitted.
644 */
645 int rv = 0;
NeilBrown0d129222006-10-03 01:15:54 -0700646
NeilBrowna35e63e2008-03-04 14:29:29 -0800647 spin_lock_irq(&conf->device_lock);
648
649 if (conf->pending_bio_list.head) {
650 struct bio *bio;
651 bio = bio_list_get(&conf->pending_bio_list);
652 blk_remove_plug(conf->mddev->queue);
653 spin_unlock_irq(&conf->device_lock);
654 /* flush any pending bitmap writes to disk
655 * before proceeding w/ I/O */
656 bitmap_unplug(conf->mddev->bitmap);
657
658 while (bio) { /* submit pending writes */
659 struct bio *next = bio->bi_next;
660 bio->bi_next = NULL;
661 generic_make_request(bio);
662 bio = next;
663 }
664 rv = 1;
665 } else
666 spin_unlock_irq(&conf->device_lock);
667 return rv;
668}
NeilBrown0a27ec92006-01-06 00:20:13 -0800669/* Barriers....
670 * Sometimes we need to suspend IO while we do something else,
671 * either some resync/recovery, or reconfigure the array.
672 * To do this we raise a 'barrier'.
673 * The 'barrier' is a counter that can be raised multiple times
674 * to count how many activities are happening which preclude
675 * normal IO.
676 * We can only raise the barrier if there is no pending IO.
677 * i.e. if nr_pending == 0.
678 * We choose only to raise the barrier if no-one is waiting for the
679 * barrier to go down. This means that as soon as an IO request
680 * is ready, no other operations which require a barrier will start
681 * until the IO request has had a chance.
682 *
683 * So: regular IO calls 'wait_barrier'. When that returns there
684 * is no backgroup IO happening, It must arrange to call
685 * allow_barrier when it has finished its IO.
686 * backgroup IO calls must call raise_barrier. Once that returns
687 * there is no normal IO happeing. It must arrange to call
688 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
690#define RESYNC_DEPTH 32
691
NeilBrown6cce3b232006-01-06 00:20:16 -0800692static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
NeilBrown6cce3b232006-01-06 00:20:16 -0800694 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
NeilBrown6cce3b232006-01-06 00:20:16 -0800697 /* Wait until no block IO is waiting (unless 'force') */
698 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800699 conf->resync_lock,
700 raid10_unplug(conf->mddev->queue));
701
702 /* block any new IO from starting */
703 conf->barrier++;
704
705 /* No wait for all pending IO to complete */
706 wait_event_lock_irq(conf->wait_barrier,
707 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
708 conf->resync_lock,
709 raid10_unplug(conf->mddev->queue));
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 spin_unlock_irq(&conf->resync_lock);
712}
713
NeilBrown0a27ec92006-01-06 00:20:13 -0800714static void lower_barrier(conf_t *conf)
715{
716 unsigned long flags;
717 spin_lock_irqsave(&conf->resync_lock, flags);
718 conf->barrier--;
719 spin_unlock_irqrestore(&conf->resync_lock, flags);
720 wake_up(&conf->wait_barrier);
721}
722
723static void wait_barrier(conf_t *conf)
724{
725 spin_lock_irq(&conf->resync_lock);
726 if (conf->barrier) {
727 conf->nr_waiting++;
728 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
729 conf->resync_lock,
730 raid10_unplug(conf->mddev->queue));
731 conf->nr_waiting--;
732 }
733 conf->nr_pending++;
734 spin_unlock_irq(&conf->resync_lock);
735}
736
737static void allow_barrier(conf_t *conf)
738{
739 unsigned long flags;
740 spin_lock_irqsave(&conf->resync_lock, flags);
741 conf->nr_pending--;
742 spin_unlock_irqrestore(&conf->resync_lock, flags);
743 wake_up(&conf->wait_barrier);
744}
745
NeilBrown4443ae12006-01-06 00:20:28 -0800746static void freeze_array(conf_t *conf)
747{
748 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800749 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800750 * We increment barrier and nr_waiting, and then
NeilBrown1c830532008-03-04 14:29:35 -0800751 * wait until nr_pending match nr_queued+1
752 * This is called in the context of one normal IO request
753 * that has failed. Thus any sync request that might be pending
754 * will be blocked by nr_pending, and we need to wait for
755 * pending IO requests to complete or be queued for re-try.
756 * Thus the number queued (nr_queued) plus this request (1)
757 * must match the number of pending IOs (nr_pending) before
758 * we continue.
NeilBrown4443ae12006-01-06 00:20:28 -0800759 */
760 spin_lock_irq(&conf->resync_lock);
761 conf->barrier++;
762 conf->nr_waiting++;
763 wait_event_lock_irq(conf->wait_barrier,
NeilBrown1c830532008-03-04 14:29:35 -0800764 conf->nr_pending == conf->nr_queued+1,
NeilBrown4443ae12006-01-06 00:20:28 -0800765 conf->resync_lock,
NeilBrowna35e63e2008-03-04 14:29:29 -0800766 ({ flush_pending_writes(conf);
767 raid10_unplug(conf->mddev->queue); }));
NeilBrown4443ae12006-01-06 00:20:28 -0800768 spin_unlock_irq(&conf->resync_lock);
769}
770
771static void unfreeze_array(conf_t *conf)
772{
773 /* reverse the effect of the freeze */
774 spin_lock_irq(&conf->resync_lock);
775 conf->barrier--;
776 conf->nr_waiting--;
777 wake_up(&conf->wait_barrier);
778 spin_unlock_irq(&conf->resync_lock);
779}
780
Jens Axboe165125e2007-07-24 09:28:11 +0200781static int make_request(struct request_queue *q, struct bio * bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 mddev_t *mddev = q->queuedata;
784 conf_t *conf = mddev_to_conf(mddev);
785 mirror_info_t *mirror;
786 r10bio_t *r10_bio;
787 struct bio *read_bio;
788 int i;
789 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100790 const int rw = bio_data_dir(bio);
Lars Ellenberge3881a62007-01-10 23:15:37 -0800791 const int do_sync = bio_sync(bio);
NeilBrown6cce3b232006-01-06 00:20:16 -0800792 struct bio_list bl;
793 unsigned long flags;
Dan Williams6bfe0b42008-04-30 00:52:32 -0700794 mdk_rdev_t *blocked_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
NeilBrowne5dcdd82005-09-09 16:23:41 -0700796 if (unlikely(bio_barrier(bio))) {
NeilBrown6712ecf2007-09-27 12:47:43 +0200797 bio_endio(bio, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -0700798 return 0;
799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* If this request crosses a chunk boundary, we need to
802 * split it. This will only happen for 1 PAGE (or less) requests.
803 */
804 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
805 > chunk_sects &&
806 conf->near_copies < conf->raid_disks)) {
807 struct bio_pair *bp;
808 /* Sanity check -- queue functions should prevent this happening */
809 if (bio->bi_vcnt != 1 ||
810 bio->bi_idx != 0)
811 goto bad_map;
812 /* This is a one page bio that upper layers
813 * refuse to split for us, so we need to split it.
814 */
815 bp = bio_split(bio, bio_split_pool,
816 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
817 if (make_request(q, &bp->bio1))
818 generic_make_request(&bp->bio1);
819 if (make_request(q, &bp->bio2))
820 generic_make_request(&bp->bio2);
821
822 bio_pair_release(bp);
823 return 0;
824 bad_map:
825 printk("raid10_make_request bug: can't convert block across chunks"
826 " or bigger than %dk %llu %d\n", chunk_sects/2,
827 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
828
NeilBrown6712ecf2007-09-27 12:47:43 +0200829 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831 }
832
NeilBrown3d310eb2005-06-21 17:17:26 -0700833 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * Register the new request and wait if the reconstruction
837 * thread has put up a bar for new requests.
838 * Continue immediately if no resync is active currently.
839 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800840 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Jens Axboea3623572005-11-01 09:26:16 +0100842 disk_stat_inc(mddev->gendisk, ios[rw]);
843 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
846
847 r10_bio->master_bio = bio;
848 r10_bio->sectors = bio->bi_size >> 9;
849
850 r10_bio->mddev = mddev;
851 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b232006-01-06 00:20:16 -0800852 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Jens Axboea3623572005-11-01 09:26:16 +0100854 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * read balancing logic:
857 */
858 int disk = read_balance(conf, r10_bio);
859 int slot = r10_bio->read_slot;
860 if (disk < 0) {
861 raid_end_bio_io(r10_bio);
862 return 0;
863 }
864 mirror = conf->mirrors + disk;
865
866 read_bio = bio_clone(bio, GFP_NOIO);
867
868 r10_bio->devs[slot].bio = read_bio;
869
870 read_bio->bi_sector = r10_bio->devs[slot].addr +
871 mirror->rdev->data_offset;
872 read_bio->bi_bdev = mirror->rdev->bdev;
873 read_bio->bi_end_io = raid10_end_read_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800874 read_bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 read_bio->bi_private = r10_bio;
876
877 generic_make_request(read_bio);
878 return 0;
879 }
880
881 /*
882 * WRITE:
883 */
Dan Williams6bfe0b42008-04-30 00:52:32 -0700884 /* first select target devices under rcu_lock and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * inc refcount on their rdev. Record them by setting
886 * bios[x] to bio
887 */
888 raid10_find_phys(conf, r10_bio);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700889 retry_write:
Harvey Harrisoncb6969e2008-05-06 20:42:32 -0700890 blocked_rdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 rcu_read_lock();
892 for (i = 0; i < conf->copies; i++) {
893 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800894 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
Dan Williams6bfe0b42008-04-30 00:52:32 -0700895 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
896 atomic_inc(&rdev->nr_pending);
897 blocked_rdev = rdev;
898 break;
899 }
900 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800901 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b232006-01-06 00:20:16 -0800903 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b232006-01-06 00:20:16 -0800905 set_bit(R10BIO_Degraded, &r10_bio->state);
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 rcu_read_unlock();
909
Dan Williams6bfe0b42008-04-30 00:52:32 -0700910 if (unlikely(blocked_rdev)) {
911 /* Have to wait for this device to get unblocked, then retry */
912 int j;
913 int d;
914
915 for (j = 0; j < i; j++)
916 if (r10_bio->devs[j].bio) {
917 d = r10_bio->devs[j].devnum;
918 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
919 }
920 allow_barrier(conf);
921 md_wait_for_blocked_rdev(blocked_rdev, mddev);
922 wait_barrier(conf);
923 goto retry_write;
924 }
925
NeilBrown6cce3b232006-01-06 00:20:16 -0800926 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700927
NeilBrown6cce3b232006-01-06 00:20:16 -0800928 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 for (i = 0; i < conf->copies; i++) {
930 struct bio *mbio;
931 int d = r10_bio->devs[i].devnum;
932 if (!r10_bio->devs[i].bio)
933 continue;
934
935 mbio = bio_clone(bio, GFP_NOIO);
936 r10_bio->devs[i].bio = mbio;
937
938 mbio->bi_sector = r10_bio->devs[i].addr+
939 conf->mirrors[d].rdev->data_offset;
940 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
941 mbio->bi_end_io = raid10_end_write_request;
Lars Ellenberge3881a62007-01-10 23:15:37 -0800942 mbio->bi_rw = WRITE | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 mbio->bi_private = r10_bio;
944
945 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b232006-01-06 00:20:16 -0800946 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
Arne Redlichf6f953a2007-07-31 00:37:57 -0700949 if (unlikely(!atomic_read(&r10_bio->remaining))) {
950 /* the array is dead */
951 md_write_end(mddev);
952 raid_end_bio_io(r10_bio);
953 return 0;
954 }
955
NeilBrown6cce3b232006-01-06 00:20:16 -0800956 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
957 spin_lock_irqsave(&conf->device_lock, flags);
958 bio_list_merge(&conf->pending_bio_list, &bl);
959 blk_plug_device(mddev->queue);
960 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
NeilBrowna35e63e2008-03-04 14:29:29 -0800962 /* In case raid10d snuck in to freeze_array */
963 wake_up(&conf->wait_barrier);
964
Lars Ellenberge3881a62007-01-10 23:15:37 -0800965 if (do_sync)
966 md_wakeup_thread(mddev->thread);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969}
970
971static void status(struct seq_file *seq, mddev_t *mddev)
972{
973 conf_t *conf = mddev_to_conf(mddev);
974 int i;
975
976 if (conf->near_copies < conf->raid_disks)
977 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
978 if (conf->near_copies > 1)
979 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700980 if (conf->far_copies > 1) {
981 if (conf->far_offset)
982 seq_printf(seq, " %d offset-copies", conf->far_copies);
983 else
984 seq_printf(seq, " %d far-copies", conf->far_copies);
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
NeilBrown76186dd2006-10-03 01:15:48 -0700987 conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 for (i = 0; i < conf->raid_disks; i++)
989 seq_printf(seq, "%s",
990 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800991 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 seq_printf(seq, "]");
993}
994
995static void error(mddev_t *mddev, mdk_rdev_t *rdev)
996{
997 char b[BDEVNAME_SIZE];
998 conf_t *conf = mddev_to_conf(mddev);
999
1000 /*
1001 * If it is not operational, then we have already marked it as dead
1002 * else if it is the last working disks, ignore the error, let the
1003 * next level up know.
1004 * else mark the drive as failed
1005 */
NeilBrownb2d444d2005-11-08 21:39:31 -08001006 if (test_bit(In_sync, &rdev->flags)
NeilBrown76186dd2006-10-03 01:15:48 -07001007 && conf->raid_disks-mddev->degraded == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * Don't fail the drive, just return an IO error.
1010 * The test should really be more sophisticated than
1011 * "working_disks == 1", but it isn't critical, and
1012 * can wait until we do more sophisticated "is the drive
1013 * really dead" tests...
1014 */
1015 return;
NeilBrownc04be0a2006-10-03 01:15:53 -07001016 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1017 unsigned long flags;
1018 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001020 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /*
1022 * if recovery is running, make sure it aborts.
1023 */
NeilBrowndfc70642008-05-23 13:04:39 -07001024 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001026 set_bit(Faulty, &rdev->flags);
NeilBrown850b2b42006-10-03 01:15:46 -07001027 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Nick Andrewd7a420c2008-04-28 02:15:55 -07001028 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device.\n"
1029 "raid10: Operation continuing on %d devices.\n",
NeilBrown76186dd2006-10-03 01:15:48 -07001030 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static void print_conf(conf_t *conf)
1034{
1035 int i;
1036 mirror_info_t *tmp;
1037
1038 printk("RAID10 conf printout:\n");
1039 if (!conf) {
1040 printk("(!conf)\n");
1041 return;
1042 }
NeilBrown76186dd2006-10-03 01:15:48 -07001043 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 conf->raid_disks);
1045
1046 for (i = 0; i < conf->raid_disks; i++) {
1047 char b[BDEVNAME_SIZE];
1048 tmp = conf->mirrors + i;
1049 if (tmp->rdev)
1050 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08001051 i, !test_bit(In_sync, &tmp->rdev->flags),
1052 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 bdevname(tmp->rdev->bdev,b));
1054 }
1055}
1056
1057static void close_sync(conf_t *conf)
1058{
NeilBrown0a27ec92006-01-06 00:20:13 -08001059 wait_barrier(conf);
1060 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 mempool_destroy(conf->r10buf_pool);
1063 conf->r10buf_pool = NULL;
1064}
1065
NeilBrown6d508242005-09-09 16:24:03 -07001066/* check if there are enough drives for
1067 * every block to appear on atleast one
1068 */
1069static int enough(conf_t *conf)
1070{
1071 int first = 0;
1072
1073 do {
1074 int n = conf->copies;
1075 int cnt = 0;
1076 while (n--) {
1077 if (conf->mirrors[first].rdev)
1078 cnt++;
1079 first = (first+1) % conf->raid_disks;
1080 }
1081 if (cnt == 0)
1082 return 0;
1083 } while (first != 0);
1084 return 1;
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087static int raid10_spare_active(mddev_t *mddev)
1088{
1089 int i;
1090 conf_t *conf = mddev->private;
1091 mirror_info_t *tmp;
1092
1093 /*
1094 * Find all non-in_sync disks within the RAID10 configuration
1095 * and mark them in_sync
1096 */
1097 for (i = 0; i < conf->raid_disks; i++) {
1098 tmp = conf->mirrors + i;
1099 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001100 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07001101 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1102 unsigned long flags;
1103 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07001105 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107 }
1108
1109 print_conf(conf);
1110 return 0;
1111}
1112
1113
1114static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1115{
1116 conf_t *conf = mddev->private;
1117 int found = 0;
1118 int mirror;
1119 mirror_info_t *p;
1120
1121 if (mddev->recovery_cp < MaxSector)
1122 /* only hot-add to in-sync arrays, as recovery is
1123 * very different from resync
1124 */
1125 return 0;
NeilBrown6d508242005-09-09 16:24:03 -07001126 if (!enough(conf))
1127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
NeilBrown6cce3b232006-01-06 00:20:16 -08001129 if (rdev->saved_raid_disk >= 0 &&
1130 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1131 mirror = rdev->saved_raid_disk;
1132 else
1133 mirror = 0;
1134 for ( ; mirror < mddev->raid_disks; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if ( !(p=conf->mirrors+mirror)->rdev) {
1136
1137 blk_queue_stack_limits(mddev->queue,
1138 rdev->bdev->bd_disk->queue);
1139 /* as we don't honour merge_bvec_fn, we must never risk
1140 * violating it, so limit ->max_sector to one PAGE, as
1141 * a one page request is never in violation.
1142 */
1143 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1144 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1145 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1146
1147 p->head_position = 0;
1148 rdev->raid_disk = mirror;
1149 found = 1;
NeilBrown6cce3b232006-01-06 00:20:16 -08001150 if (rdev->saved_raid_disk != mirror)
1151 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001152 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 }
1155
1156 print_conf(conf);
1157 return found;
1158}
1159
1160static int raid10_remove_disk(mddev_t *mddev, int number)
1161{
1162 conf_t *conf = mddev->private;
1163 int err = 0;
1164 mdk_rdev_t *rdev;
1165 mirror_info_t *p = conf->mirrors+ number;
1166
1167 print_conf(conf);
1168 rdev = p->rdev;
1169 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001170 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 atomic_read(&rdev->nr_pending)) {
1172 err = -EBUSY;
1173 goto abort;
1174 }
NeilBrowndfc70642008-05-23 13:04:39 -07001175 /* Only remove faulty devices in recovery
1176 * is not possible.
1177 */
1178 if (!test_bit(Faulty, &rdev->flags) &&
1179 enough(conf)) {
1180 err = -EBUSY;
1181 goto abort;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001184 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (atomic_read(&rdev->nr_pending)) {
1186 /* lost the race, try later */
1187 err = -EBUSY;
1188 p->rdev = rdev;
1189 }
1190 }
1191abort:
1192
1193 print_conf(conf);
1194 return err;
1195}
1196
1197
NeilBrown6712ecf2007-09-27 12:47:43 +02001198static void end_sync_read(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1201 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1202 int i,d;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 for (i=0; i<conf->copies; i++)
1205 if (r10_bio->devs[i].bio == bio)
1206 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001207 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 update_head_pos(i, r10_bio);
1209 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001210
1211 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1212 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001213 else {
1214 atomic_add(r10_bio->sectors,
1215 &conf->mirrors[d].rdev->corrected_errors);
1216 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1217 md_error(r10_bio->mddev,
1218 conf->mirrors[d].rdev);
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* for reconstruct, we always reschedule after a read.
1222 * for resync, only after all reads
1223 */
1224 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1225 atomic_dec_and_test(&r10_bio->remaining)) {
1226 /* we have read all the blocks,
1227 * do the comparison in process context in raid10d
1228 */
1229 reschedule_retry(r10_bio);
1230 }
1231 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
NeilBrown6712ecf2007-09-27 12:47:43 +02001234static void end_sync_write(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1237 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1238 mddev_t *mddev = r10_bio->mddev;
1239 conf_t *conf = mddev_to_conf(mddev);
1240 int i,d;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 for (i = 0; i < conf->copies; i++)
1243 if (r10_bio->devs[i].bio == bio)
1244 break;
1245 d = r10_bio->devs[i].devnum;
1246
1247 if (!uptodate)
1248 md_error(mddev, conf->mirrors[d].rdev);
NeilBrowndfc70642008-05-23 13:04:39 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 update_head_pos(i, r10_bio);
1251
1252 while (atomic_dec_and_test(&r10_bio->remaining)) {
1253 if (r10_bio->master_bio == NULL) {
1254 /* the primary of several recovery bios */
1255 md_done_sync(mddev, r10_bio->sectors, 1);
1256 put_buf(r10_bio);
1257 break;
1258 } else {
1259 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1260 put_buf(r10_bio);
1261 r10_bio = r10_bio2;
1262 }
1263 }
1264 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267/*
1268 * Note: sync and recover and handled very differently for raid10
1269 * This code is for resync.
1270 * For resync, we read through virtual addresses and read all blocks.
1271 * If there is any error, we schedule a write. The lowest numbered
1272 * drive is authoritative.
1273 * However requests come for physical address, so we need to map.
1274 * For every physical address there are raid_disks/copies virtual addresses,
1275 * which is always are least one, but is not necessarly an integer.
1276 * This means that a physical address can span multiple chunks, so we may
1277 * have to submit multiple io requests for a single sync request.
1278 */
1279/*
1280 * We check if all blocks are in-sync and only write to blocks that
1281 * aren't in sync
1282 */
1283static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1284{
1285 conf_t *conf = mddev_to_conf(mddev);
1286 int i, first;
1287 struct bio *tbio, *fbio;
1288
1289 atomic_set(&r10_bio->remaining, 1);
1290
1291 /* find the first device with a block */
1292 for (i=0; i<conf->copies; i++)
1293 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1294 break;
1295
1296 if (i == conf->copies)
1297 goto done;
1298
1299 first = i;
1300 fbio = r10_bio->devs[i].bio;
1301
1302 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001303 for (i=0 ; i < conf->copies ; i++) {
1304 int j, d;
1305 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001308
1309 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001311 if (i == first)
1312 continue;
1313 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1314 /* We know that the bi_io_vec layout is the same for
1315 * both 'first' and 'i', so we just compare them.
1316 * All vec entries are PAGE_SIZE;
1317 */
1318 for (j = 0; j < vcnt; j++)
1319 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1320 page_address(tbio->bi_io_vec[j].bv_page),
1321 PAGE_SIZE))
1322 break;
1323 if (j == vcnt)
1324 continue;
1325 mddev->resync_mismatches += r10_bio->sectors;
1326 }
NeilBrown18f08812006-01-06 00:20:25 -08001327 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1328 /* Don't fix anything. */
1329 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* Ok, we need to write this bio
1331 * First we need to fixup bv_offset, bv_len and
1332 * bi_vecs, as the read request might have corrupted these
1333 */
1334 tbio->bi_vcnt = vcnt;
1335 tbio->bi_size = r10_bio->sectors << 9;
1336 tbio->bi_idx = 0;
1337 tbio->bi_phys_segments = 0;
1338 tbio->bi_hw_segments = 0;
1339 tbio->bi_hw_front_size = 0;
1340 tbio->bi_hw_back_size = 0;
1341 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1342 tbio->bi_flags |= 1 << BIO_UPTODATE;
1343 tbio->bi_next = NULL;
1344 tbio->bi_rw = WRITE;
1345 tbio->bi_private = r10_bio;
1346 tbio->bi_sector = r10_bio->devs[i].addr;
1347
1348 for (j=0; j < vcnt ; j++) {
1349 tbio->bi_io_vec[j].bv_offset = 0;
1350 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1351
1352 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1353 page_address(fbio->bi_io_vec[j].bv_page),
1354 PAGE_SIZE);
1355 }
1356 tbio->bi_end_io = end_sync_write;
1357
1358 d = r10_bio->devs[i].devnum;
1359 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1360 atomic_inc(&r10_bio->remaining);
1361 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1362
1363 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1364 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1365 generic_make_request(tbio);
1366 }
1367
1368done:
1369 if (atomic_dec_and_test(&r10_bio->remaining)) {
1370 md_done_sync(mddev, r10_bio->sectors, 1);
1371 put_buf(r10_bio);
1372 }
1373}
1374
1375/*
1376 * Now for the recovery code.
1377 * Recovery happens across physical sectors.
1378 * We recover all non-is_sync drives by finding the virtual address of
1379 * each, and then choose a working drive that also has that virt address.
1380 * There is a separate r10_bio for each non-in_sync drive.
1381 * Only the first two slots are in use. The first for reading,
1382 * The second for writing.
1383 *
1384 */
1385
1386static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1387{
1388 conf_t *conf = mddev_to_conf(mddev);
1389 int i, d;
1390 struct bio *bio, *wbio;
1391
1392
1393 /* move the pages across to the second bio
1394 * and submit the write request
1395 */
1396 bio = r10_bio->devs[0].bio;
1397 wbio = r10_bio->devs[1].bio;
1398 for (i=0; i < wbio->bi_vcnt; i++) {
1399 struct page *p = bio->bi_io_vec[i].bv_page;
1400 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1401 wbio->bi_io_vec[i].bv_page = p;
1402 }
1403 d = r10_bio->devs[1].devnum;
1404
1405 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1406 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001407 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1408 generic_make_request(wbio);
1409 else
NeilBrown6712ecf2007-09-27 12:47:43 +02001410 bio_endio(wbio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413
1414/*
1415 * This is a kernel thread which:
1416 *
1417 * 1. Retries failed read operations on working mirrors.
1418 * 2. Updates the raid superblock when problems encounter.
NeilBrown6814d532006-10-03 01:15:45 -07001419 * 3. Performs writes following reads for array synchronising.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
1421
NeilBrown6814d532006-10-03 01:15:45 -07001422static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1423{
1424 int sect = 0; /* Offset from r10_bio->sector */
1425 int sectors = r10_bio->sectors;
1426 mdk_rdev_t*rdev;
1427 while(sectors) {
1428 int s = sectors;
1429 int sl = r10_bio->read_slot;
1430 int success = 0;
1431 int start;
1432
1433 if (s > (PAGE_SIZE>>9))
1434 s = PAGE_SIZE >> 9;
1435
1436 rcu_read_lock();
1437 do {
1438 int d = r10_bio->devs[sl].devnum;
1439 rdev = rcu_dereference(conf->mirrors[d].rdev);
1440 if (rdev &&
1441 test_bit(In_sync, &rdev->flags)) {
1442 atomic_inc(&rdev->nr_pending);
1443 rcu_read_unlock();
1444 success = sync_page_io(rdev->bdev,
1445 r10_bio->devs[sl].addr +
1446 sect + rdev->data_offset,
1447 s<<9,
1448 conf->tmppage, READ);
1449 rdev_dec_pending(rdev, mddev);
1450 rcu_read_lock();
1451 if (success)
1452 break;
1453 }
1454 sl++;
1455 if (sl == conf->copies)
1456 sl = 0;
1457 } while (!success && sl != r10_bio->read_slot);
1458 rcu_read_unlock();
1459
1460 if (!success) {
1461 /* Cannot read from anywhere -- bye bye array */
1462 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1463 md_error(mddev, conf->mirrors[dn].rdev);
1464 break;
1465 }
1466
1467 start = sl;
1468 /* write it back and re-read */
1469 rcu_read_lock();
1470 while (sl != r10_bio->read_slot) {
1471 int d;
1472 if (sl==0)
1473 sl = conf->copies;
1474 sl--;
1475 d = r10_bio->devs[sl].devnum;
1476 rdev = rcu_dereference(conf->mirrors[d].rdev);
1477 if (rdev &&
1478 test_bit(In_sync, &rdev->flags)) {
1479 atomic_inc(&rdev->nr_pending);
1480 rcu_read_unlock();
1481 atomic_add(s, &rdev->corrected_errors);
1482 if (sync_page_io(rdev->bdev,
1483 r10_bio->devs[sl].addr +
1484 sect + rdev->data_offset,
1485 s<<9, conf->tmppage, WRITE)
1486 == 0)
1487 /* Well, this device is dead */
1488 md_error(mddev, rdev);
1489 rdev_dec_pending(rdev, mddev);
1490 rcu_read_lock();
1491 }
1492 }
1493 sl = start;
1494 while (sl != r10_bio->read_slot) {
1495 int d;
1496 if (sl==0)
1497 sl = conf->copies;
1498 sl--;
1499 d = r10_bio->devs[sl].devnum;
1500 rdev = rcu_dereference(conf->mirrors[d].rdev);
1501 if (rdev &&
1502 test_bit(In_sync, &rdev->flags)) {
1503 char b[BDEVNAME_SIZE];
1504 atomic_inc(&rdev->nr_pending);
1505 rcu_read_unlock();
1506 if (sync_page_io(rdev->bdev,
1507 r10_bio->devs[sl].addr +
1508 sect + rdev->data_offset,
1509 s<<9, conf->tmppage, READ) == 0)
1510 /* Well, this device is dead */
1511 md_error(mddev, rdev);
1512 else
1513 printk(KERN_INFO
1514 "raid10:%s: read error corrected"
1515 " (%d sectors at %llu on %s)\n",
1516 mdname(mddev), s,
Randy Dunlap969b7552006-10-28 10:38:32 -07001517 (unsigned long long)(sect+
1518 rdev->data_offset),
NeilBrown6814d532006-10-03 01:15:45 -07001519 bdevname(rdev->bdev, b));
1520
1521 rdev_dec_pending(rdev, mddev);
1522 rcu_read_lock();
1523 }
1524 }
1525 rcu_read_unlock();
1526
1527 sectors -= s;
1528 sect += s;
1529 }
1530}
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532static void raid10d(mddev_t *mddev)
1533{
1534 r10bio_t *r10_bio;
1535 struct bio *bio;
1536 unsigned long flags;
1537 conf_t *conf = mddev_to_conf(mddev);
1538 struct list_head *head = &conf->retry_list;
1539 int unplug=0;
1540 mdk_rdev_t *rdev;
1541
1542 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 for (;;) {
1545 char b[BDEVNAME_SIZE];
NeilBrowna35e63e2008-03-04 14:29:29 -08001546
1547 unplug += flush_pending_writes(conf);
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrowna35e63e2008-03-04 14:29:29 -08001550 if (list_empty(head)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001551 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 break;
NeilBrowna35e63e2008-03-04 14:29:29 -08001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1555 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001556 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 spin_unlock_irqrestore(&conf->device_lock, flags);
1558
1559 mddev = r10_bio->mddev;
1560 conf = mddev_to_conf(mddev);
1561 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1562 sync_request_write(mddev, r10_bio);
1563 unplug = 1;
1564 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1565 recovery_request_write(mddev, r10_bio);
1566 unplug = 1;
1567 } else {
1568 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001569 /* we got a read error. Maybe the drive is bad. Maybe just
1570 * the block and we can fix it.
1571 * We freeze all other IO, and try reading the block from
1572 * other devices. When we find one, we re-write
1573 * and check it that fixes the read error.
1574 * This is all done synchronously while the array is
1575 * frozen.
1576 */
NeilBrown6814d532006-10-03 01:15:45 -07001577 if (mddev->ro == 0) {
1578 freeze_array(conf);
1579 fix_read_error(conf, mddev, r10_bio);
1580 unfreeze_array(conf);
NeilBrown4443ae12006-01-06 00:20:28 -08001581 }
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001584 r10_bio->devs[r10_bio->read_slot].bio =
1585 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 mirror = read_balance(conf, r10_bio);
1587 if (mirror == -1) {
1588 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1589 " read error for block %llu\n",
1590 bdevname(bio->bi_bdev,b),
1591 (unsigned long long)r10_bio->sector);
1592 raid_end_bio_io(r10_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001593 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 } else {
Lars Ellenberge3881a62007-01-10 23:15:37 -08001595 const int do_sync = bio_sync(r10_bio->master_bio);
Maik Hampel14e71342007-07-31 00:37:57 -07001596 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 rdev = conf->mirrors[mirror].rdev;
1598 if (printk_ratelimit())
1599 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1600 " another mirror\n",
1601 bdevname(rdev->bdev,b),
1602 (unsigned long long)r10_bio->sector);
1603 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1604 r10_bio->devs[r10_bio->read_slot].bio = bio;
1605 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1606 + rdev->data_offset;
1607 bio->bi_bdev = rdev->bdev;
Lars Ellenberge3881a62007-01-10 23:15:37 -08001608 bio->bi_rw = READ | do_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 bio->bi_private = r10_bio;
1610 bio->bi_end_io = raid10_end_read_request;
1611 unplug = 1;
1612 generic_make_request(bio);
1613 }
1614 }
1615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (unplug)
1617 unplug_slaves(mddev);
1618}
1619
1620
1621static int init_resync(conf_t *conf)
1622{
1623 int buffs;
1624
1625 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001626 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1628 if (!conf->r10buf_pool)
1629 return -ENOMEM;
1630 conf->next_resync = 0;
1631 return 0;
1632}
1633
1634/*
1635 * perform a "sync" on one "block"
1636 *
1637 * We need to make sure that no normal I/O request - particularly write
1638 * requests - conflict with active sync requests.
1639 *
1640 * This is achieved by tracking pending requests and a 'barrier' concept
1641 * that can be installed to exclude normal IO requests.
1642 *
1643 * Resync and recovery are handled very differently.
1644 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1645 *
1646 * For resync, we iterate over virtual addresses, read all copies,
1647 * and update if there are differences. If only one copy is live,
1648 * skip it.
1649 * For recovery, we iterate over physical addresses, read a good
1650 * value for each non-in_sync drive, and over-write.
1651 *
1652 * So, for recovery we may have several outstanding complex requests for a
1653 * given address, one for each out-of-sync device. We model this by allocating
1654 * a number of r10_bio structures, one for each out-of-sync device.
1655 * As we setup these structures, we collect all bio's together into a list
1656 * which we then process collectively to add pages, and then process again
1657 * to pass to generic_make_request.
1658 *
1659 * The r10_bio structures are linked using a borrowed master_bio pointer.
1660 * This link is counted in ->remaining. When the r10_bio that points to NULL
1661 * has its remaining count decremented to 0, the whole complex operation
1662 * is complete.
1663 *
1664 */
1665
NeilBrown57afd892005-06-21 17:17:13 -07001666static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 conf_t *conf = mddev_to_conf(mddev);
1669 r10bio_t *r10_bio;
1670 struct bio *biolist = NULL, *bio;
1671 sector_t max_sector, nr_sectors;
1672 int disk;
1673 int i;
NeilBrown6cce3b232006-01-06 00:20:16 -08001674 int max_sync;
1675 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 sector_t sectors_skipped = 0;
1678 int chunks_skipped = 0;
1679
1680 if (!conf->r10buf_pool)
1681 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 skipped:
1685 max_sector = mddev->size << 1;
1686 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1687 max_sector = mddev->resync_max_sectors;
1688 if (sector_nr >= max_sector) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001689 /* If we aborted, we need to abort the
1690 * sync on the 'current' bitmap chucks (there can
1691 * be several when recovering multiple devices).
1692 * as we may have started syncing it but not finished.
1693 * We can find the current address in
1694 * mddev->curr_resync, but for recovery,
1695 * we need to convert that to several
1696 * virtual addresses.
1697 */
1698 if (mddev->curr_resync < max_sector) { /* aborted */
1699 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1700 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1701 &sync_blocks, 1);
1702 else for (i=0; i<conf->raid_disks; i++) {
1703 sector_t sect =
1704 raid10_find_virt(conf, mddev->curr_resync, i);
1705 bitmap_end_sync(mddev->bitmap, sect,
1706 &sync_blocks, 1);
1707 }
1708 } else /* completed sync */
1709 conf->fullsync = 0;
1710
1711 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001713 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return sectors_skipped;
1715 }
1716 if (chunks_skipped >= conf->raid_disks) {
1717 /* if there has been nothing to do on any drive,
1718 * then there is nothing to do at all..
1719 */
NeilBrown57afd892005-06-21 17:17:13 -07001720 *skipped = 1;
1721 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723
NeilBrownc6207272008-02-06 01:39:52 -08001724 if (max_sector > mddev->resync_max)
1725 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* make sure whole request will fit in a chunk - if chunks
1728 * are meaningful
1729 */
1730 if (conf->near_copies < conf->raid_disks &&
1731 max_sector > (sector_nr | conf->chunk_mask))
1732 max_sector = (sector_nr | conf->chunk_mask) + 1;
1733 /*
1734 * If there is non-resync activity waiting for us then
1735 * put in a delay to throttle resync.
1736 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001737 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
NeilBrownb47490c2008-02-06 01:39:50 -08001740 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /* Again, very different code for resync and recovery.
1743 * Both must result in an r10bio with a list of bios that
1744 * have bi_end_io, bi_sector, bi_bdev set,
1745 * and bi_private set to the r10bio.
1746 * For recovery, we may actually create several r10bios
1747 * with 2 bios in each, that correspond to the bios in the main one.
1748 * In this case, the subordinate r10bios link back through a
1749 * borrowed master_bio pointer, and the counter in the master
1750 * includes a ref from each subordinate.
1751 */
1752 /* First, we decide what to do and set ->bi_end_io
1753 * To end_sync_read if we want to read, and
1754 * end_sync_write if we will want to write.
1755 */
1756
NeilBrown6cce3b232006-01-06 00:20:16 -08001757 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1759 /* recovery... the complicated one */
1760 int i, j, k;
1761 r10_bio = NULL;
1762
1763 for (i=0 ; i<conf->raid_disks; i++)
1764 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001765 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001766 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /* want to reconstruct this device */
1768 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b232006-01-06 00:20:16 -08001769 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1770 int must_sync;
1771 /* Unless we are doing a full sync, we only need
1772 * to recover the block if it is set in the bitmap
1773 */
1774 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1775 &sync_blocks, 1);
1776 if (sync_blocks < max_sync)
1777 max_sync = sync_blocks;
1778 if (!must_sync &&
1779 !conf->fullsync) {
1780 /* yep, skip the sync_blocks here, but don't assume
1781 * that there will never be anything to do here
1782 */
1783 chunks_skipped = -1;
1784 continue;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b232006-01-06 00:20:16 -08001788 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 atomic_set(&r10_bio->remaining, 0);
1790
1791 r10_bio->master_bio = (struct bio*)rb2;
1792 if (rb2)
1793 atomic_inc(&rb2->remaining);
1794 r10_bio->mddev = mddev;
1795 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b232006-01-06 00:20:16 -08001796 r10_bio->sector = sect;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 raid10_find_phys(conf, r10_bio);
NeilBrown6cce3b232006-01-06 00:20:16 -08001799 /* Need to check if this section will still be
1800 * degraded
1801 */
1802 for (j=0; j<conf->copies;j++) {
1803 int d = r10_bio->devs[j].devnum;
1804 if (conf->mirrors[d].rdev == NULL ||
NeilBrowna24a8dd2006-01-06 00:20:35 -08001805 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001806 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001807 break;
1808 }
NeilBrown6cce3b232006-01-06 00:20:16 -08001809 }
1810 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1811 &sync_blocks, still_degraded);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 for (j=0; j<conf->copies;j++) {
1814 int d = r10_bio->devs[j].devnum;
1815 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001816 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /* This is where we read from */
1818 bio = r10_bio->devs[0].bio;
1819 bio->bi_next = biolist;
1820 biolist = bio;
1821 bio->bi_private = r10_bio;
1822 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001823 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 bio->bi_sector = r10_bio->devs[j].addr +
1825 conf->mirrors[d].rdev->data_offset;
1826 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1827 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1828 atomic_inc(&r10_bio->remaining);
1829 /* and we write to 'i' */
1830
1831 for (k=0; k<conf->copies; k++)
1832 if (r10_bio->devs[k].devnum == i)
1833 break;
NeilBrown64a742b2007-02-28 20:11:18 -08001834 BUG_ON(k == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 bio = r10_bio->devs[1].bio;
1836 bio->bi_next = biolist;
1837 biolist = bio;
1838 bio->bi_private = r10_bio;
1839 bio->bi_end_io = end_sync_write;
NeilBrown802ba062006-12-13 00:34:13 -08001840 bio->bi_rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 bio->bi_sector = r10_bio->devs[k].addr +
1842 conf->mirrors[i].rdev->data_offset;
1843 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1844
1845 r10_bio->devs[0].devnum = d;
1846 r10_bio->devs[1].devnum = i;
1847
1848 break;
1849 }
1850 }
1851 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001852 /* Cannot recover, so abort the recovery */
1853 put_buf(r10_bio);
K.Tanakaa07e6ab2008-03-04 14:29:37 -08001854 if (rb2)
1855 atomic_dec(&rb2->remaining);
NeilBrown87fc7672005-09-09 16:24:04 -07001856 r10_bio = rb2;
NeilBrowndfc70642008-05-23 13:04:39 -07001857 if (!test_and_set_bit(MD_RECOVERY_INTR,
1858 &mddev->recovery))
NeilBrown87fc7672005-09-09 16:24:04 -07001859 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1860 mdname(mddev));
1861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 }
1864 if (biolist == NULL) {
1865 while (r10_bio) {
1866 r10bio_t *rb2 = r10_bio;
1867 r10_bio = (r10bio_t*) rb2->master_bio;
1868 rb2->master_bio = NULL;
1869 put_buf(rb2);
1870 }
1871 goto giveup;
1872 }
1873 } else {
1874 /* resync. Schedule a read for every block at this virt offset */
1875 int count = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08001876
1877 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1878 &sync_blocks, mddev->degraded) &&
1879 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1880 /* We can skip this block */
1881 *skipped = 1;
1882 return sync_blocks + sectors_skipped;
1883 }
1884 if (sync_blocks < max_sync)
1885 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 r10_bio->mddev = mddev;
1889 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b232006-01-06 00:20:16 -08001890 raise_barrier(conf, 0);
1891 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 r10_bio->master_bio = NULL;
1894 r10_bio->sector = sector_nr;
1895 set_bit(R10BIO_IsSync, &r10_bio->state);
1896 raid10_find_phys(conf, r10_bio);
1897 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1898
1899 for (i=0; i<conf->copies; i++) {
1900 int d = r10_bio->devs[i].devnum;
1901 bio = r10_bio->devs[i].bio;
1902 bio->bi_end_io = NULL;
NeilBrownaf03b8e2007-06-16 10:16:06 -07001903 clear_bit(BIO_UPTODATE, &bio->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001905 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 continue;
1907 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1908 atomic_inc(&r10_bio->remaining);
1909 bio->bi_next = biolist;
1910 biolist = bio;
1911 bio->bi_private = r10_bio;
1912 bio->bi_end_io = end_sync_read;
NeilBrown802ba062006-12-13 00:34:13 -08001913 bio->bi_rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 bio->bi_sector = r10_bio->devs[i].addr +
1915 conf->mirrors[d].rdev->data_offset;
1916 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1917 count++;
1918 }
1919
1920 if (count < 2) {
1921 for (i=0; i<conf->copies; i++) {
1922 int d = r10_bio->devs[i].devnum;
1923 if (r10_bio->devs[i].bio->bi_end_io)
1924 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1925 }
1926 put_buf(r10_bio);
1927 biolist = NULL;
1928 goto giveup;
1929 }
1930 }
1931
1932 for (bio = biolist; bio ; bio=bio->bi_next) {
1933
1934 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1935 if (bio->bi_end_io)
1936 bio->bi_flags |= 1 << BIO_UPTODATE;
1937 bio->bi_vcnt = 0;
1938 bio->bi_idx = 0;
1939 bio->bi_phys_segments = 0;
1940 bio->bi_hw_segments = 0;
1941 bio->bi_size = 0;
1942 }
1943
1944 nr_sectors = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08001945 if (sector_nr + max_sync < max_sector)
1946 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 do {
1948 struct page *page;
1949 int len = PAGE_SIZE;
1950 disk = 0;
1951 if (sector_nr + (len>>9) > max_sector)
1952 len = (max_sector - sector_nr) << 9;
1953 if (len == 0)
1954 break;
1955 for (bio= biolist ; bio ; bio=bio->bi_next) {
1956 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1957 if (bio_add_page(bio, page, len, 0) == 0) {
1958 /* stop here */
1959 struct bio *bio2;
1960 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1961 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1962 /* remove last page from this bio */
1963 bio2->bi_vcnt--;
1964 bio2->bi_size -= len;
1965 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1966 }
1967 goto bio_full;
1968 }
1969 disk = i;
1970 }
1971 nr_sectors += len>>9;
1972 sector_nr += len>>9;
1973 } while (biolist->bi_vcnt < RESYNC_PAGES);
1974 bio_full:
1975 r10_bio->sectors = nr_sectors;
1976
1977 while (biolist) {
1978 bio = biolist;
1979 biolist = biolist->bi_next;
1980
1981 bio->bi_next = NULL;
1982 r10_bio = bio->bi_private;
1983 r10_bio->sectors = nr_sectors;
1984
1985 if (bio->bi_end_io == end_sync_read) {
1986 md_sync_acct(bio->bi_bdev, nr_sectors);
1987 generic_make_request(bio);
1988 }
1989 }
1990
NeilBrown57afd892005-06-21 17:17:13 -07001991 if (sectors_skipped)
1992 /* pretend they weren't skipped, it makes
1993 * no important difference in this case
1994 */
1995 md_done_sync(mddev, sectors_skipped, 1);
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return sectors_skipped + nr_sectors;
1998 giveup:
1999 /* There is nowhere to write, so all non-sync
2000 * drives must be failed, so try the next chunk...
2001 */
2002 {
NeilBrown57afd892005-06-21 17:17:13 -07002003 sector_t sec = max_sector - sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 sectors_skipped += sec;
2005 chunks_skipped ++;
2006 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 goto skipped;
2008 }
2009}
2010
2011static int run(mddev_t *mddev)
2012{
2013 conf_t *conf;
2014 int i, disk_idx;
2015 mirror_info_t *disk;
2016 mdk_rdev_t *rdev;
2017 struct list_head *tmp;
NeilBrownc93983b2006-06-26 00:27:41 -07002018 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 sector_t stride, size;
2020
NeilBrown2604b702006-01-06 00:20:36 -08002021 if (mddev->chunk_size == 0) {
2022 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
2023 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
NeilBrown2604b702006-01-06 00:20:36 -08002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 nc = mddev->layout & 255;
2027 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07002028 fo = mddev->layout & (1<<16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07002030 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
2032 mdname(mddev), mddev->layout);
2033 goto out;
2034 }
2035 /*
2036 * copy the already verified devices into our private RAID10
2037 * bookkeeping area. [whatever we allocate in run(),
2038 * should be freed in stop()]
2039 */
NeilBrown4443ae12006-01-06 00:20:28 -08002040 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 mddev->private = conf;
2042 if (!conf) {
2043 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2044 mdname(mddev));
2045 goto out;
2046 }
NeilBrown4443ae12006-01-06 00:20:28 -08002047 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 GFP_KERNEL);
2049 if (!conf->mirrors) {
2050 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2051 mdname(mddev));
2052 goto out_free_conf;
2053 }
NeilBrown4443ae12006-01-06 00:20:28 -08002054
2055 conf->tmppage = alloc_page(GFP_KERNEL);
2056 if (!conf->tmppage)
2057 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
NeilBrown64a742b2007-02-28 20:11:18 -08002059 conf->mddev = mddev;
2060 conf->raid_disks = mddev->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 conf->near_copies = nc;
2062 conf->far_copies = fc;
2063 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07002064 conf->far_offset = fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
2066 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
NeilBrown64a742b2007-02-28 20:11:18 -08002067 size = mddev->size >> (conf->chunk_shift-1);
2068 sector_div(size, fc);
2069 size = size * conf->raid_disks;
2070 sector_div(size, nc);
2071 /* 'size' is now the number of chunks in the array */
2072 /* calculate "used chunks per device" in 'stride' */
2073 stride = size * conf->copies;
NeilBrownaf03b8e2007-06-16 10:16:06 -07002074
2075 /* We need to round up when dividing by raid_disks to
2076 * get the stride size.
2077 */
2078 stride += conf->raid_disks - 1;
NeilBrown64a742b2007-02-28 20:11:18 -08002079 sector_div(stride, conf->raid_disks);
2080 mddev->size = stride << (conf->chunk_shift-1);
2081
NeilBrownc93983b2006-06-26 00:27:41 -07002082 if (fo)
NeilBrown64a742b2007-02-28 20:11:18 -08002083 stride = 1;
2084 else
NeilBrownc93983b2006-06-26 00:27:41 -07002085 sector_div(stride, fc);
NeilBrown64a742b2007-02-28 20:11:18 -08002086 conf->stride = stride << conf->chunk_shift;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2089 r10bio_pool_free, conf);
2090 if (!conf->r10bio_pool) {
2091 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2092 mdname(mddev));
2093 goto out_free_conf;
2094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Neil Browne7e72bf2008-05-14 16:05:54 -07002096 spin_lock_init(&conf->device_lock);
2097 mddev->queue->queue_lock = &conf->device_lock;
2098
NeilBrownd089c6a2008-02-06 01:39:59 -08002099 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 disk_idx = rdev->raid_disk;
2101 if (disk_idx >= mddev->raid_disks
2102 || disk_idx < 0)
2103 continue;
2104 disk = conf->mirrors + disk_idx;
2105
2106 disk->rdev = rdev;
2107
2108 blk_queue_stack_limits(mddev->queue,
2109 rdev->bdev->bd_disk->queue);
2110 /* as we don't honour merge_bvec_fn, we must never risk
2111 * violating it, so limit ->max_sector to one PAGE, as
2112 * a one page request is never in violation.
2113 */
2114 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2115 mddev->queue->max_sectors > (PAGE_SIZE>>9))
2116 mddev->queue->max_sectors = (PAGE_SIZE>>9);
2117
2118 disk->head_position = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 INIT_LIST_HEAD(&conf->retry_list);
2121
2122 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08002123 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
NeilBrown6d508242005-09-09 16:24:03 -07002125 /* need to check that every block has at least one working mirror */
2126 if (!enough(conf)) {
2127 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2128 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto out_free_conf;
2130 }
2131
2132 mddev->degraded = 0;
2133 for (i = 0; i < conf->raid_disks; i++) {
2134
2135 disk = conf->mirrors + i;
2136
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002137 if (!disk->rdev ||
NeilBrown2e333e82006-10-21 10:24:07 -07002138 !test_bit(In_sync, &disk->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 disk->head_position = 0;
2140 mddev->degraded++;
Neil Brown8c2e8702008-06-28 08:30:52 +10002141 if (disk->rdev)
2142 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144 }
2145
2146
2147 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2148 if (!mddev->thread) {
2149 printk(KERN_ERR
2150 "raid10: couldn't allocate thread for %s\n",
2151 mdname(mddev));
2152 goto out_free_conf;
2153 }
2154
2155 printk(KERN_INFO
2156 "raid10: raid set %s active with %d out of %d devices\n",
2157 mdname(mddev), mddev->raid_disks - mddev->degraded,
2158 mddev->raid_disks);
2159 /*
2160 * Ok, everything is just fine now
2161 */
NeilBrown64a742b2007-02-28 20:11:18 -08002162 mddev->array_size = size << (conf->chunk_shift-1);
2163 mddev->resync_max_sectors = size << conf->chunk_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
NeilBrown7a5febe2005-05-16 21:53:16 -07002165 mddev->queue->unplug_fn = raid10_unplug;
NeilBrown0d129222006-10-03 01:15:54 -07002166 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2167 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown7a5febe2005-05-16 21:53:16 -07002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /* Calculate max read-ahead size.
2170 * We need to readahead at least twice a whole stripe....
2171 * maybe...
2172 */
2173 {
NeilBrown8932c2e2006-06-26 00:27:36 -07002174 int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 stripe /= conf->near_copies;
2176 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2177 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2178 }
2179
2180 if (conf->near_copies < mddev->raid_disks)
2181 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2182 return 0;
2183
2184out_free_conf:
2185 if (conf->r10bio_pool)
2186 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002187 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002188 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 kfree(conf);
2190 mddev->private = NULL;
2191out:
2192 return -EIO;
2193}
2194
2195static int stop(mddev_t *mddev)
2196{
2197 conf_t *conf = mddev_to_conf(mddev);
2198
2199 md_unregister_thread(mddev->thread);
2200 mddev->thread = NULL;
2201 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2202 if (conf->r10bio_pool)
2203 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002204 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 kfree(conf);
2206 mddev->private = NULL;
2207 return 0;
2208}
2209
NeilBrown6cce3b232006-01-06 00:20:16 -08002210static void raid10_quiesce(mddev_t *mddev, int state)
2211{
2212 conf_t *conf = mddev_to_conf(mddev);
2213
2214 switch(state) {
2215 case 1:
2216 raise_barrier(conf, 0);
2217 break;
2218 case 0:
2219 lower_barrier(conf);
2220 break;
2221 }
2222 if (mddev->thread) {
2223 if (mddev->bitmap)
2224 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2225 else
2226 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2227 md_wakeup_thread(mddev->thread);
2228 }
2229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
NeilBrown2604b702006-01-06 00:20:36 -08002231static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002234 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 .owner = THIS_MODULE,
2236 .make_request = make_request,
2237 .run = run,
2238 .stop = stop,
2239 .status = status,
2240 .error_handler = error,
2241 .hot_add_disk = raid10_add_disk,
2242 .hot_remove_disk= raid10_remove_disk,
2243 .spare_active = raid10_spare_active,
2244 .sync_request = sync_request,
NeilBrown6cce3b232006-01-06 00:20:16 -08002245 .quiesce = raid10_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246};
2247
2248static int __init raid_init(void)
2249{
NeilBrown2604b702006-01-06 00:20:36 -08002250 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
2253static void raid_exit(void)
2254{
NeilBrown2604b702006-01-06 00:20:36 -08002255 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
2258module_init(raid_init);
2259module_exit(raid_exit);
2260MODULE_LICENSE("GPL");
2261MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002262MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002263MODULE_ALIAS("md-level-10");