blob: 7f636283a1ba3dfcb78aa52f925fbc65fc9525a7 [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
230 bio_endio(bio, bio->bi_size,
231 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
246static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
247{
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
253 if (bio->bi_size)
254 return 1;
255
256 slot = r10_bio->read_slot;
257 dev = r10_bio->devs[slot].devnum;
258 /*
259 * this branch is our 'one mirror IO has finished' event handler:
260 */
NeilBrown4443ae12006-01-06 00:20:28 -0800261 update_head_pos(slot, r10_bio);
262
263 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /*
265 * Set R10BIO_Uptodate in our master bio, so that
266 * we will return a good error code to the higher
267 * levels even if IO on some other mirrored buffer fails.
268 *
269 * The 'master' represents the composite IO operation to
270 * user-side. So if something waits for IO, then it will
271 * wait for the 'master' bio.
272 */
273 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 raid_end_bio_io(r10_bio);
NeilBrown4443ae12006-01-06 00:20:28 -0800275 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /*
277 * oops, read error:
278 */
279 char b[BDEVNAME_SIZE];
280 if (printk_ratelimit())
281 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
282 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
283 reschedule_retry(r10_bio);
284 }
285
286 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
287 return 0;
288}
289
290static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
291{
292 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
293 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
294 int slot, dev;
295 conf_t *conf = mddev_to_conf(r10_bio->mddev);
296
297 if (bio->bi_size)
298 return 1;
299
300 for (slot = 0; slot < conf->copies; slot++)
301 if (r10_bio->devs[slot].bio == bio)
302 break;
303 dev = r10_bio->devs[slot].devnum;
304
305 /*
306 * this branch is our 'one mirror IO has finished' event handler:
307 */
NeilBrown6cce3b232006-01-06 00:20:16 -0800308 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b232006-01-06 00:20:16 -0800310 /* an I/O failed, we can't clear the bitmap */
311 set_bit(R10BIO_Degraded, &r10_bio->state);
312 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /*
314 * Set R10BIO_Uptodate in our master bio, so that
315 * we will return a good error code for to the higher
316 * levels even if IO on some other mirrored buffer fails.
317 *
318 * The 'master' represents the composite IO operation to
319 * user-side. So if something waits for IO, then it will
320 * wait for the 'master' bio.
321 */
322 set_bit(R10BIO_Uptodate, &r10_bio->state);
323
324 update_head_pos(slot, r10_bio);
325
326 /*
327 *
328 * Let's see if all mirrored write operations have finished
329 * already.
330 */
331 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b232006-01-06 00:20:16 -0800332 /* clear the bitmap if all writes complete successfully */
333 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
334 r10_bio->sectors,
335 !test_bit(R10BIO_Degraded, &r10_bio->state),
336 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 md_write_end(r10_bio->mddev);
338 raid_end_bio_io(r10_bio);
339 }
340
341 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
342 return 0;
343}
344
345
346/*
347 * RAID10 layout manager
348 * Aswell as the chunksize and raid_disks count, there are two
349 * parameters: near_copies and far_copies.
350 * near_copies * far_copies must be <= raid_disks.
351 * Normally one of these will be 1.
352 * If both are 1, we get raid0.
353 * If near_copies == raid_disks, we get raid1.
354 *
355 * Chunks are layed out in raid0 style with near_copies copies of the
356 * first chunk, followed by near_copies copies of the next chunk and
357 * so on.
358 * If far_copies > 1, then after 1/far_copies of the array has been assigned
359 * as described above, we start again with a device offset of near_copies.
360 * So we effectively have another copy of the whole array further down all
361 * the drives, but with blocks on different drives.
362 * With this layout, and block is never stored twice on the one device.
363 *
364 * raid10_find_phys finds the sector offset of a given virtual sector
NeilBrownc93983b2006-06-26 00:27:41 -0700365 * on each device that it is on.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 *
367 * raid10_find_virt does the reverse mapping, from a device and a
368 * sector offset to a virtual address
369 */
370
371static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
372{
373 int n,f;
374 sector_t sector;
375 sector_t chunk;
376 sector_t stripe;
377 int dev;
378
379 int slot = 0;
380
381 /* now calculate first sector/dev */
382 chunk = r10bio->sector >> conf->chunk_shift;
383 sector = r10bio->sector & conf->chunk_mask;
384
385 chunk *= conf->near_copies;
386 stripe = chunk;
387 dev = sector_div(stripe, conf->raid_disks);
NeilBrownc93983b2006-06-26 00:27:41 -0700388 if (conf->far_offset)
389 stripe *= conf->far_copies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 sector += stripe << conf->chunk_shift;
392
393 /* and calculate all the others */
394 for (n=0; n < conf->near_copies; n++) {
395 int d = dev;
396 sector_t s = sector;
397 r10bio->devs[slot].addr = sector;
398 r10bio->devs[slot].devnum = d;
399 slot++;
400
401 for (f = 1; f < conf->far_copies; f++) {
402 d += conf->near_copies;
403 if (d >= conf->raid_disks)
404 d -= conf->raid_disks;
405 s += conf->stride;
406 r10bio->devs[slot].devnum = d;
407 r10bio->devs[slot].addr = s;
408 slot++;
409 }
410 dev++;
411 if (dev >= conf->raid_disks) {
412 dev = 0;
413 sector += (conf->chunk_mask + 1);
414 }
415 }
416 BUG_ON(slot != conf->copies);
417}
418
419static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
420{
421 sector_t offset, chunk, vchunk;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 offset = sector & conf->chunk_mask;
NeilBrownc93983b2006-06-26 00:27:41 -0700424 if (conf->far_offset) {
425 int fc;
426 chunk = sector >> conf->chunk_shift;
427 fc = sector_div(chunk, conf->far_copies);
428 dev -= fc * conf->near_copies;
429 if (dev < 0)
430 dev += conf->raid_disks;
431 } else {
432 while (sector > conf->stride) {
433 sector -= conf->stride;
434 if (dev < conf->near_copies)
435 dev += conf->raid_disks - conf->near_copies;
436 else
437 dev -= conf->near_copies;
438 }
439 chunk = sector >> conf->chunk_shift;
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 vchunk = chunk * conf->raid_disks + dev;
442 sector_div(vchunk, conf->near_copies);
443 return (vchunk << conf->chunk_shift) + offset;
444}
445
446/**
447 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
448 * @q: request queue
449 * @bio: the buffer head that's been built up so far
450 * @biovec: the request that could be merged to it.
451 *
452 * Return amount of bytes we can accept at this offset
453 * If near_copies == raid_disk, there are no striping issues,
454 * but in that case, the function isn't called at all.
455 */
456static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
457 struct bio_vec *bio_vec)
458{
459 mddev_t *mddev = q->queuedata;
460 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
461 int max;
462 unsigned int chunk_sectors = mddev->chunk_size >> 9;
463 unsigned int bio_sectors = bio->bi_size >> 9;
464
465 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
466 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
467 if (max <= bio_vec->bv_len && bio_sectors == 0)
468 return bio_vec->bv_len;
469 else
470 return max;
471}
472
473/*
474 * This routine returns the disk from which the requested read should
475 * be done. There is a per-array 'next expected sequential IO' sector
476 * number - if this matches on the next IO then we use the last disk.
477 * There is also a per-disk 'last know head position' sector that is
478 * maintained from IRQ contexts, both the normal and the resync IO
479 * completion handlers update this position correctly. If there is no
480 * perfect sequential match then we pick the disk whose head is closest.
481 *
482 * If there are 2 mirrors in the same 2 devices, performance degrades
483 * because position is mirror, not device based.
484 *
485 * The rdev for the device selected will have nr_pending incremented.
486 */
487
488/*
489 * FIXME: possibly should rethink readbalancing and do it differently
490 * depending on near_copies / far_copies geometry.
491 */
492static int read_balance(conf_t *conf, r10bio_t *r10_bio)
493{
494 const unsigned long this_sector = r10_bio->sector;
495 int disk, slot, nslot;
496 const int sectors = r10_bio->sectors;
497 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800498 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 raid10_find_phys(conf, r10_bio);
501 rcu_read_lock();
502 /*
503 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b232006-01-06 00:20:16 -0800504 * device if no resync is going on (recovery is ok), or below
505 * the resync window. We take the first readable disk when
506 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
508 if (conf->mddev->recovery_cp < MaxSector
509 && (this_sector + sectors >= conf->next_resync)) {
510 /* make sure that disk is operational */
511 slot = 0;
512 disk = r10_bio->devs[slot].devnum;
513
Suzanne Woodd6065f72005-11-08 21:39:27 -0800514 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800515 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800516 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 slot++;
518 if (slot == conf->copies) {
519 slot = 0;
520 disk = -1;
521 break;
522 }
523 disk = r10_bio->devs[slot].devnum;
524 }
525 goto rb_out;
526 }
527
528
529 /* make sure the disk is operational */
530 slot = 0;
531 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800532 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800533 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800534 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 slot ++;
536 if (slot == conf->copies) {
537 disk = -1;
538 goto rb_out;
539 }
540 disk = r10_bio->devs[slot].devnum;
541 }
542
543
NeilBrown3ec67ac2005-09-09 16:23:40 -0700544 current_distance = abs(r10_bio->devs[slot].addr -
545 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* Find the disk whose head is closest */
548
549 for (nslot = slot; nslot < conf->copies; nslot++) {
550 int ndisk = r10_bio->devs[nslot].devnum;
551
552
Suzanne Woodd6065f72005-11-08 21:39:27 -0800553 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800554 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800555 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 continue;
557
NeilBrown22dfdf52005-11-28 13:44:09 -0800558 /* This optimisation is debatable, and completely destroys
559 * sequential read speed for 'far copies' arrays. So only
560 * keep it for 'near' arrays, and review those later.
561 */
562 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 disk = ndisk;
564 slot = nslot;
565 break;
566 }
567 new_distance = abs(r10_bio->devs[nslot].addr -
568 conf->mirrors[ndisk].head_position);
569 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
599
600 atomic_inc(&rdev->nr_pending);
601 rcu_read_unlock();
602
603 if (r_queue->unplug_fn)
604 r_queue->unplug_fn(r_queue);
605
606 rdev_dec_pending(rdev, mddev);
607 rcu_read_lock();
608 }
609 }
610 rcu_read_unlock();
611}
612
613static void raid10_unplug(request_queue_t *q)
614{
NeilBrown6cce3b232006-01-06 00:20:16 -0800615 mddev_t *mddev = q->queuedata;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 unplug_slaves(q->queuedata);
NeilBrown6cce3b232006-01-06 00:20:16 -0800618 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
622 sector_t *error_sector)
623{
624 mddev_t *mddev = q->queuedata;
625 conf_t *conf = mddev_to_conf(mddev);
626 int i, ret = 0;
627
628 rcu_read_lock();
629 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800630 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800631 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct block_device *bdev = rdev->bdev;
633 request_queue_t *r_queue = bdev_get_queue(bdev);
634
635 if (!r_queue->issue_flush_fn)
636 ret = -EOPNOTSUPP;
637 else {
638 atomic_inc(&rdev->nr_pending);
639 rcu_read_unlock();
640 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
641 error_sector);
642 rdev_dec_pending(rdev, mddev);
643 rcu_read_lock();
644 }
645 }
646 }
647 rcu_read_unlock();
648 return ret;
649}
650
NeilBrown0a27ec92006-01-06 00:20:13 -0800651/* Barriers....
652 * Sometimes we need to suspend IO while we do something else,
653 * either some resync/recovery, or reconfigure the array.
654 * To do this we raise a 'barrier'.
655 * The 'barrier' is a counter that can be raised multiple times
656 * to count how many activities are happening which preclude
657 * normal IO.
658 * We can only raise the barrier if there is no pending IO.
659 * i.e. if nr_pending == 0.
660 * We choose only to raise the barrier if no-one is waiting for the
661 * barrier to go down. This means that as soon as an IO request
662 * is ready, no other operations which require a barrier will start
663 * until the IO request has had a chance.
664 *
665 * So: regular IO calls 'wait_barrier'. When that returns there
666 * is no backgroup IO happening, It must arrange to call
667 * allow_barrier when it has finished its IO.
668 * backgroup IO calls must call raise_barrier. Once that returns
669 * there is no normal IO happeing. It must arrange to call
670 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
672#define RESYNC_DEPTH 32
673
NeilBrown6cce3b232006-01-06 00:20:16 -0800674static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
NeilBrown6cce3b232006-01-06 00:20:16 -0800676 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
NeilBrown6cce3b232006-01-06 00:20:16 -0800679 /* Wait until no block IO is waiting (unless 'force') */
680 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800681 conf->resync_lock,
682 raid10_unplug(conf->mddev->queue));
683
684 /* block any new IO from starting */
685 conf->barrier++;
686
687 /* No wait for all pending IO to complete */
688 wait_event_lock_irq(conf->wait_barrier,
689 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
690 conf->resync_lock,
691 raid10_unplug(conf->mddev->queue));
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spin_unlock_irq(&conf->resync_lock);
694}
695
NeilBrown0a27ec92006-01-06 00:20:13 -0800696static void lower_barrier(conf_t *conf)
697{
698 unsigned long flags;
699 spin_lock_irqsave(&conf->resync_lock, flags);
700 conf->barrier--;
701 spin_unlock_irqrestore(&conf->resync_lock, flags);
702 wake_up(&conf->wait_barrier);
703}
704
705static void wait_barrier(conf_t *conf)
706{
707 spin_lock_irq(&conf->resync_lock);
708 if (conf->barrier) {
709 conf->nr_waiting++;
710 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
711 conf->resync_lock,
712 raid10_unplug(conf->mddev->queue));
713 conf->nr_waiting--;
714 }
715 conf->nr_pending++;
716 spin_unlock_irq(&conf->resync_lock);
717}
718
719static void allow_barrier(conf_t *conf)
720{
721 unsigned long flags;
722 spin_lock_irqsave(&conf->resync_lock, flags);
723 conf->nr_pending--;
724 spin_unlock_irqrestore(&conf->resync_lock, flags);
725 wake_up(&conf->wait_barrier);
726}
727
NeilBrown4443ae12006-01-06 00:20:28 -0800728static void freeze_array(conf_t *conf)
729{
730 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800731 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800732 * We increment barrier and nr_waiting, and then
733 * wait until barrier+nr_pending match nr_queued+2
734 */
735 spin_lock_irq(&conf->resync_lock);
736 conf->barrier++;
737 conf->nr_waiting++;
738 wait_event_lock_irq(conf->wait_barrier,
739 conf->barrier+conf->nr_pending == conf->nr_queued+2,
740 conf->resync_lock,
741 raid10_unplug(conf->mddev->queue));
742 spin_unlock_irq(&conf->resync_lock);
743}
744
745static void unfreeze_array(conf_t *conf)
746{
747 /* reverse the effect of the freeze */
748 spin_lock_irq(&conf->resync_lock);
749 conf->barrier--;
750 conf->nr_waiting--;
751 wake_up(&conf->wait_barrier);
752 spin_unlock_irq(&conf->resync_lock);
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755static int make_request(request_queue_t *q, struct bio * bio)
756{
757 mddev_t *mddev = q->queuedata;
758 conf_t *conf = mddev_to_conf(mddev);
759 mirror_info_t *mirror;
760 r10bio_t *r10_bio;
761 struct bio *read_bio;
762 int i;
763 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100764 const int rw = bio_data_dir(bio);
NeilBrown6cce3b232006-01-06 00:20:16 -0800765 struct bio_list bl;
766 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
NeilBrowne5dcdd82005-09-09 16:23:41 -0700768 if (unlikely(bio_barrier(bio))) {
769 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
770 return 0;
771 }
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* If this request crosses a chunk boundary, we need to
774 * split it. This will only happen for 1 PAGE (or less) requests.
775 */
776 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
777 > chunk_sects &&
778 conf->near_copies < conf->raid_disks)) {
779 struct bio_pair *bp;
780 /* Sanity check -- queue functions should prevent this happening */
781 if (bio->bi_vcnt != 1 ||
782 bio->bi_idx != 0)
783 goto bad_map;
784 /* This is a one page bio that upper layers
785 * refuse to split for us, so we need to split it.
786 */
787 bp = bio_split(bio, bio_split_pool,
788 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
789 if (make_request(q, &bp->bio1))
790 generic_make_request(&bp->bio1);
791 if (make_request(q, &bp->bio2))
792 generic_make_request(&bp->bio2);
793
794 bio_pair_release(bp);
795 return 0;
796 bad_map:
797 printk("raid10_make_request bug: can't convert block across chunks"
798 " or bigger than %dk %llu %d\n", chunk_sects/2,
799 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
800
801 bio_io_error(bio, bio->bi_size);
802 return 0;
803 }
804
NeilBrown3d310eb2005-06-21 17:17:26 -0700805 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /*
808 * Register the new request and wait if the reconstruction
809 * thread has put up a bar for new requests.
810 * Continue immediately if no resync is active currently.
811 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800812 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jens Axboea3623572005-11-01 09:26:16 +0100814 disk_stat_inc(mddev->gendisk, ios[rw]);
815 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
818
819 r10_bio->master_bio = bio;
820 r10_bio->sectors = bio->bi_size >> 9;
821
822 r10_bio->mddev = mddev;
823 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b232006-01-06 00:20:16 -0800824 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Jens Axboea3623572005-11-01 09:26:16 +0100826 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /*
828 * read balancing logic:
829 */
830 int disk = read_balance(conf, r10_bio);
831 int slot = r10_bio->read_slot;
832 if (disk < 0) {
833 raid_end_bio_io(r10_bio);
834 return 0;
835 }
836 mirror = conf->mirrors + disk;
837
838 read_bio = bio_clone(bio, GFP_NOIO);
839
840 r10_bio->devs[slot].bio = read_bio;
841
842 read_bio->bi_sector = r10_bio->devs[slot].addr +
843 mirror->rdev->data_offset;
844 read_bio->bi_bdev = mirror->rdev->bdev;
845 read_bio->bi_end_io = raid10_end_read_request;
846 read_bio->bi_rw = READ;
847 read_bio->bi_private = r10_bio;
848
849 generic_make_request(read_bio);
850 return 0;
851 }
852
853 /*
854 * WRITE:
855 */
856 /* first select target devices under spinlock and
857 * inc refcount on their rdev. Record them by setting
858 * bios[x] to bio
859 */
860 raid10_find_phys(conf, r10_bio);
861 rcu_read_lock();
862 for (i = 0; i < conf->copies; i++) {
863 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800864 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
865 if (rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800866 !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800867 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b232006-01-06 00:20:16 -0800869 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b232006-01-06 00:20:16 -0800871 set_bit(R10BIO_Degraded, &r10_bio->state);
872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 rcu_read_unlock();
875
NeilBrown6cce3b232006-01-06 00:20:16 -0800876 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700877
NeilBrown6cce3b232006-01-06 00:20:16 -0800878 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 for (i = 0; i < conf->copies; i++) {
880 struct bio *mbio;
881 int d = r10_bio->devs[i].devnum;
882 if (!r10_bio->devs[i].bio)
883 continue;
884
885 mbio = bio_clone(bio, GFP_NOIO);
886 r10_bio->devs[i].bio = mbio;
887
888 mbio->bi_sector = r10_bio->devs[i].addr+
889 conf->mirrors[d].rdev->data_offset;
890 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
891 mbio->bi_end_io = raid10_end_write_request;
892 mbio->bi_rw = WRITE;
893 mbio->bi_private = r10_bio;
894
895 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b232006-01-06 00:20:16 -0800896 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
NeilBrown6cce3b232006-01-06 00:20:16 -0800899 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
900 spin_lock_irqsave(&conf->device_lock, flags);
901 bio_list_merge(&conf->pending_bio_list, &bl);
902 blk_plug_device(mddev->queue);
903 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 return 0;
906}
907
908static void status(struct seq_file *seq, mddev_t *mddev)
909{
910 conf_t *conf = mddev_to_conf(mddev);
911 int i;
912
913 if (conf->near_copies < conf->raid_disks)
914 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
915 if (conf->near_copies > 1)
916 seq_printf(seq, " %d near-copies", conf->near_copies);
NeilBrownc93983b2006-06-26 00:27:41 -0700917 if (conf->far_copies > 1) {
918 if (conf->far_offset)
919 seq_printf(seq, " %d offset-copies", conf->far_copies);
920 else
921 seq_printf(seq, " %d far-copies", conf->far_copies);
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
924 conf->working_disks);
925 for (i = 0; i < conf->raid_disks; i++)
926 seq_printf(seq, "%s",
927 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800928 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 seq_printf(seq, "]");
930}
931
932static void error(mddev_t *mddev, mdk_rdev_t *rdev)
933{
934 char b[BDEVNAME_SIZE];
935 conf_t *conf = mddev_to_conf(mddev);
936
937 /*
938 * If it is not operational, then we have already marked it as dead
939 * else if it is the last working disks, ignore the error, let the
940 * next level up know.
941 * else mark the drive as failed
942 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800943 if (test_bit(In_sync, &rdev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 && conf->working_disks == 1)
945 /*
946 * Don't fail the drive, just return an IO error.
947 * The test should really be more sophisticated than
948 * "working_disks == 1", but it isn't critical, and
949 * can wait until we do more sophisticated "is the drive
950 * really dead" tests...
951 */
952 return;
NeilBrownb2d444d2005-11-08 21:39:31 -0800953 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 mddev->degraded++;
955 conf->working_disks--;
956 /*
957 * if recovery is running, make sure it aborts.
958 */
959 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
960 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800961 clear_bit(In_sync, &rdev->flags);
962 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 mddev->sb_dirty = 1;
964 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
965 " Operation continuing on %d devices\n",
966 bdevname(rdev->bdev,b), conf->working_disks);
967}
968
969static void print_conf(conf_t *conf)
970{
971 int i;
972 mirror_info_t *tmp;
973
974 printk("RAID10 conf printout:\n");
975 if (!conf) {
976 printk("(!conf)\n");
977 return;
978 }
979 printk(" --- wd:%d rd:%d\n", conf->working_disks,
980 conf->raid_disks);
981
982 for (i = 0; i < conf->raid_disks; i++) {
983 char b[BDEVNAME_SIZE];
984 tmp = conf->mirrors + i;
985 if (tmp->rdev)
986 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -0800987 i, !test_bit(In_sync, &tmp->rdev->flags),
988 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 bdevname(tmp->rdev->bdev,b));
990 }
991}
992
993static void close_sync(conf_t *conf)
994{
NeilBrown0a27ec92006-01-06 00:20:13 -0800995 wait_barrier(conf);
996 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 mempool_destroy(conf->r10buf_pool);
999 conf->r10buf_pool = NULL;
1000}
1001
NeilBrown6d508242005-09-09 16:24:03 -07001002/* check if there are enough drives for
1003 * every block to appear on atleast one
1004 */
1005static int enough(conf_t *conf)
1006{
1007 int first = 0;
1008
1009 do {
1010 int n = conf->copies;
1011 int cnt = 0;
1012 while (n--) {
1013 if (conf->mirrors[first].rdev)
1014 cnt++;
1015 first = (first+1) % conf->raid_disks;
1016 }
1017 if (cnt == 0)
1018 return 0;
1019 } while (first != 0);
1020 return 1;
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023static int raid10_spare_active(mddev_t *mddev)
1024{
1025 int i;
1026 conf_t *conf = mddev->private;
1027 mirror_info_t *tmp;
1028
1029 /*
1030 * Find all non-in_sync disks within the RAID10 configuration
1031 * and mark them in_sync
1032 */
1033 for (i = 0; i < conf->raid_disks; i++) {
1034 tmp = conf->mirrors + i;
1035 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001036 && !test_bit(Faulty, &tmp->rdev->flags)
1037 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 conf->working_disks++;
1039 mddev->degraded--;
NeilBrownb2d444d2005-11-08 21:39:31 -08001040 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 }
1043
1044 print_conf(conf);
1045 return 0;
1046}
1047
1048
1049static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1050{
1051 conf_t *conf = mddev->private;
1052 int found = 0;
1053 int mirror;
1054 mirror_info_t *p;
1055
1056 if (mddev->recovery_cp < MaxSector)
1057 /* only hot-add to in-sync arrays, as recovery is
1058 * very different from resync
1059 */
1060 return 0;
NeilBrown6d508242005-09-09 16:24:03 -07001061 if (!enough(conf))
1062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
NeilBrown6cce3b232006-01-06 00:20:16 -08001064 if (rdev->saved_raid_disk >= 0 &&
1065 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1066 mirror = rdev->saved_raid_disk;
1067 else
1068 mirror = 0;
1069 for ( ; mirror < mddev->raid_disks; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if ( !(p=conf->mirrors+mirror)->rdev) {
1071
1072 blk_queue_stack_limits(mddev->queue,
1073 rdev->bdev->bd_disk->queue);
1074 /* as we don't honour merge_bvec_fn, we must never risk
1075 * violating it, so limit ->max_sector to one PAGE, as
1076 * a one page request is never in violation.
1077 */
1078 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1079 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1080 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1081
1082 p->head_position = 0;
1083 rdev->raid_disk = mirror;
1084 found = 1;
NeilBrown6cce3b232006-01-06 00:20:16 -08001085 if (rdev->saved_raid_disk != mirror)
1086 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001087 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
1089 }
1090
1091 print_conf(conf);
1092 return found;
1093}
1094
1095static int raid10_remove_disk(mddev_t *mddev, int number)
1096{
1097 conf_t *conf = mddev->private;
1098 int err = 0;
1099 mdk_rdev_t *rdev;
1100 mirror_info_t *p = conf->mirrors+ number;
1101
1102 print_conf(conf);
1103 rdev = p->rdev;
1104 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001105 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 atomic_read(&rdev->nr_pending)) {
1107 err = -EBUSY;
1108 goto abort;
1109 }
1110 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001111 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (atomic_read(&rdev->nr_pending)) {
1113 /* lost the race, try later */
1114 err = -EBUSY;
1115 p->rdev = rdev;
1116 }
1117 }
1118abort:
1119
1120 print_conf(conf);
1121 return err;
1122}
1123
1124
1125static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1128 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1129 int i,d;
1130
1131 if (bio->bi_size)
1132 return 1;
1133
1134 for (i=0; i<conf->copies; i++)
1135 if (r10_bio->devs[i].bio == bio)
1136 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001137 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 update_head_pos(i, r10_bio);
1139 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001140
1141 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1142 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001143 else {
1144 atomic_add(r10_bio->sectors,
1145 &conf->mirrors[d].rdev->corrected_errors);
1146 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1147 md_error(r10_bio->mddev,
1148 conf->mirrors[d].rdev);
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* for reconstruct, we always reschedule after a read.
1152 * for resync, only after all reads
1153 */
1154 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1155 atomic_dec_and_test(&r10_bio->remaining)) {
1156 /* we have read all the blocks,
1157 * do the comparison in process context in raid10d
1158 */
1159 reschedule_retry(r10_bio);
1160 }
1161 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1162 return 0;
1163}
1164
1165static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1166{
1167 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1168 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1169 mddev_t *mddev = r10_bio->mddev;
1170 conf_t *conf = mddev_to_conf(mddev);
1171 int i,d;
1172
1173 if (bio->bi_size)
1174 return 1;
1175
1176 for (i = 0; i < conf->copies; i++)
1177 if (r10_bio->devs[i].bio == bio)
1178 break;
1179 d = r10_bio->devs[i].devnum;
1180
1181 if (!uptodate)
1182 md_error(mddev, conf->mirrors[d].rdev);
1183 update_head_pos(i, r10_bio);
1184
1185 while (atomic_dec_and_test(&r10_bio->remaining)) {
1186 if (r10_bio->master_bio == NULL) {
1187 /* the primary of several recovery bios */
1188 md_done_sync(mddev, r10_bio->sectors, 1);
1189 put_buf(r10_bio);
1190 break;
1191 } else {
1192 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1193 put_buf(r10_bio);
1194 r10_bio = r10_bio2;
1195 }
1196 }
1197 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1198 return 0;
1199}
1200
1201/*
1202 * Note: sync and recover and handled very differently for raid10
1203 * This code is for resync.
1204 * For resync, we read through virtual addresses and read all blocks.
1205 * If there is any error, we schedule a write. The lowest numbered
1206 * drive is authoritative.
1207 * However requests come for physical address, so we need to map.
1208 * For every physical address there are raid_disks/copies virtual addresses,
1209 * which is always are least one, but is not necessarly an integer.
1210 * This means that a physical address can span multiple chunks, so we may
1211 * have to submit multiple io requests for a single sync request.
1212 */
1213/*
1214 * We check if all blocks are in-sync and only write to blocks that
1215 * aren't in sync
1216 */
1217static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1218{
1219 conf_t *conf = mddev_to_conf(mddev);
1220 int i, first;
1221 struct bio *tbio, *fbio;
1222
1223 atomic_set(&r10_bio->remaining, 1);
1224
1225 /* find the first device with a block */
1226 for (i=0; i<conf->copies; i++)
1227 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1228 break;
1229
1230 if (i == conf->copies)
1231 goto done;
1232
1233 first = i;
1234 fbio = r10_bio->devs[i].bio;
1235
1236 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001237 for (i=0 ; i < conf->copies ; i++) {
1238 int j, d;
1239 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001242
1243 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001245 if (i == first)
1246 continue;
1247 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1248 /* We know that the bi_io_vec layout is the same for
1249 * both 'first' and 'i', so we just compare them.
1250 * All vec entries are PAGE_SIZE;
1251 */
1252 for (j = 0; j < vcnt; j++)
1253 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1254 page_address(tbio->bi_io_vec[j].bv_page),
1255 PAGE_SIZE))
1256 break;
1257 if (j == vcnt)
1258 continue;
1259 mddev->resync_mismatches += r10_bio->sectors;
1260 }
NeilBrown18f08812006-01-06 00:20:25 -08001261 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1262 /* Don't fix anything. */
1263 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* Ok, we need to write this bio
1265 * First we need to fixup bv_offset, bv_len and
1266 * bi_vecs, as the read request might have corrupted these
1267 */
1268 tbio->bi_vcnt = vcnt;
1269 tbio->bi_size = r10_bio->sectors << 9;
1270 tbio->bi_idx = 0;
1271 tbio->bi_phys_segments = 0;
1272 tbio->bi_hw_segments = 0;
1273 tbio->bi_hw_front_size = 0;
1274 tbio->bi_hw_back_size = 0;
1275 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1276 tbio->bi_flags |= 1 << BIO_UPTODATE;
1277 tbio->bi_next = NULL;
1278 tbio->bi_rw = WRITE;
1279 tbio->bi_private = r10_bio;
1280 tbio->bi_sector = r10_bio->devs[i].addr;
1281
1282 for (j=0; j < vcnt ; j++) {
1283 tbio->bi_io_vec[j].bv_offset = 0;
1284 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1285
1286 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1287 page_address(fbio->bi_io_vec[j].bv_page),
1288 PAGE_SIZE);
1289 }
1290 tbio->bi_end_io = end_sync_write;
1291
1292 d = r10_bio->devs[i].devnum;
1293 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1294 atomic_inc(&r10_bio->remaining);
1295 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1296
1297 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1298 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1299 generic_make_request(tbio);
1300 }
1301
1302done:
1303 if (atomic_dec_and_test(&r10_bio->remaining)) {
1304 md_done_sync(mddev, r10_bio->sectors, 1);
1305 put_buf(r10_bio);
1306 }
1307}
1308
1309/*
1310 * Now for the recovery code.
1311 * Recovery happens across physical sectors.
1312 * We recover all non-is_sync drives by finding the virtual address of
1313 * each, and then choose a working drive that also has that virt address.
1314 * There is a separate r10_bio for each non-in_sync drive.
1315 * Only the first two slots are in use. The first for reading,
1316 * The second for writing.
1317 *
1318 */
1319
1320static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1321{
1322 conf_t *conf = mddev_to_conf(mddev);
1323 int i, d;
1324 struct bio *bio, *wbio;
1325
1326
1327 /* move the pages across to the second bio
1328 * and submit the write request
1329 */
1330 bio = r10_bio->devs[0].bio;
1331 wbio = r10_bio->devs[1].bio;
1332 for (i=0; i < wbio->bi_vcnt; i++) {
1333 struct page *p = bio->bi_io_vec[i].bv_page;
1334 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1335 wbio->bi_io_vec[i].bv_page = p;
1336 }
1337 d = r10_bio->devs[1].devnum;
1338
1339 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1340 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001341 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1342 generic_make_request(wbio);
1343 else
1344 bio_endio(wbio, wbio->bi_size, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
1347
1348/*
1349 * This is a kernel thread which:
1350 *
1351 * 1. Retries failed read operations on working mirrors.
1352 * 2. Updates the raid superblock when problems encounter.
1353 * 3. Performs writes following reads for array syncronising.
1354 */
1355
1356static void raid10d(mddev_t *mddev)
1357{
1358 r10bio_t *r10_bio;
1359 struct bio *bio;
1360 unsigned long flags;
1361 conf_t *conf = mddev_to_conf(mddev);
1362 struct list_head *head = &conf->retry_list;
1363 int unplug=0;
1364 mdk_rdev_t *rdev;
1365
1366 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 for (;;) {
1369 char b[BDEVNAME_SIZE];
1370 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown6cce3b232006-01-06 00:20:16 -08001371
1372 if (conf->pending_bio_list.head) {
1373 bio = bio_list_get(&conf->pending_bio_list);
1374 blk_remove_plug(mddev->queue);
1375 spin_unlock_irqrestore(&conf->device_lock, flags);
1376 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1377 if (bitmap_unplug(mddev->bitmap) != 0)
1378 printk("%s: bitmap file write failed!\n", mdname(mddev));
1379
1380 while (bio) { /* submit pending writes */
1381 struct bio *next = bio->bi_next;
1382 bio->bi_next = NULL;
1383 generic_make_request(bio);
1384 bio = next;
1385 }
1386 unplug = 1;
1387
1388 continue;
1389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (list_empty(head))
1392 break;
1393 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1394 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001395 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 spin_unlock_irqrestore(&conf->device_lock, flags);
1397
1398 mddev = r10_bio->mddev;
1399 conf = mddev_to_conf(mddev);
1400 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1401 sync_request_write(mddev, r10_bio);
1402 unplug = 1;
1403 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1404 recovery_request_write(mddev, r10_bio);
1405 unplug = 1;
1406 } else {
1407 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001408 /* we got a read error. Maybe the drive is bad. Maybe just
1409 * the block and we can fix it.
1410 * We freeze all other IO, and try reading the block from
1411 * other devices. When we find one, we re-write
1412 * and check it that fixes the read error.
1413 * This is all done synchronously while the array is
1414 * frozen.
1415 */
1416 int sect = 0; /* Offset from r10_bio->sector */
1417 int sectors = r10_bio->sectors;
1418 freeze_array(conf);
1419 if (mddev->ro == 0) while(sectors) {
1420 int s = sectors;
1421 int sl = r10_bio->read_slot;
1422 int success = 0;
1423
1424 if (s > (PAGE_SIZE>>9))
1425 s = PAGE_SIZE >> 9;
1426
NeilBrowne0a33272006-05-01 12:15:45 -07001427 rcu_read_lock();
NeilBrown4443ae12006-01-06 00:20:28 -08001428 do {
1429 int d = r10_bio->devs[sl].devnum;
NeilBrowne0a33272006-05-01 12:15:45 -07001430 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown4443ae12006-01-06 00:20:28 -08001431 if (rdev &&
NeilBrowne0a33272006-05-01 12:15:45 -07001432 test_bit(In_sync, &rdev->flags)) {
1433 atomic_inc(&rdev->nr_pending);
1434 rcu_read_unlock();
1435 success = sync_page_io(rdev->bdev,
1436 r10_bio->devs[sl].addr +
1437 sect + rdev->data_offset,
1438 s<<9,
1439 conf->tmppage, READ);
1440 rdev_dec_pending(rdev, mddev);
1441 rcu_read_lock();
1442 if (success)
1443 break;
NeilBrown4443ae12006-01-06 00:20:28 -08001444 }
NeilBrowne0a33272006-05-01 12:15:45 -07001445 sl++;
1446 if (sl == conf->copies)
1447 sl = 0;
NeilBrown4443ae12006-01-06 00:20:28 -08001448 } while (!success && sl != r10_bio->read_slot);
NeilBrowne0a33272006-05-01 12:15:45 -07001449 rcu_read_unlock();
NeilBrown4443ae12006-01-06 00:20:28 -08001450
1451 if (success) {
NeilBrown097426f2006-01-06 00:20:37 -08001452 int start = sl;
NeilBrown4443ae12006-01-06 00:20:28 -08001453 /* write it back and re-read */
NeilBrowne0a33272006-05-01 12:15:45 -07001454 rcu_read_lock();
NeilBrown4443ae12006-01-06 00:20:28 -08001455 while (sl != r10_bio->read_slot) {
1456 int d;
1457 if (sl==0)
1458 sl = conf->copies;
1459 sl--;
1460 d = r10_bio->devs[sl].devnum;
NeilBrowne0a33272006-05-01 12:15:45 -07001461 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown4443ae12006-01-06 00:20:28 -08001462 if (rdev &&
1463 test_bit(In_sync, &rdev->flags)) {
NeilBrowne0a33272006-05-01 12:15:45 -07001464 atomic_inc(&rdev->nr_pending);
1465 rcu_read_unlock();
NeilBrowndf30d0f2006-05-01 12:15:44 -07001466 atomic_add(s, &rdev->corrected_errors);
NeilBrown4443ae12006-01-06 00:20:28 -08001467 if (sync_page_io(rdev->bdev,
1468 r10_bio->devs[sl].addr +
1469 sect + rdev->data_offset,
NeilBrown097426f2006-01-06 00:20:37 -08001470 s<<9, conf->tmppage, WRITE) == 0)
NeilBrown4443ae12006-01-06 00:20:28 -08001471 /* Well, this device is dead */
1472 md_error(mddev, rdev);
NeilBrowne0a33272006-05-01 12:15:45 -07001473 rdev_dec_pending(rdev, mddev);
1474 rcu_read_lock();
NeilBrown097426f2006-01-06 00:20:37 -08001475 }
1476 }
1477 sl = start;
1478 while (sl != r10_bio->read_slot) {
1479 int d;
1480 if (sl==0)
1481 sl = conf->copies;
1482 sl--;
1483 d = r10_bio->devs[sl].devnum;
NeilBrowne0a33272006-05-01 12:15:45 -07001484 rdev = rcu_dereference(conf->mirrors[d].rdev);
NeilBrown097426f2006-01-06 00:20:37 -08001485 if (rdev &&
1486 test_bit(In_sync, &rdev->flags)) {
NeilBrowne0a33272006-05-01 12:15:45 -07001487 atomic_inc(&rdev->nr_pending);
1488 rcu_read_unlock();
NeilBrown097426f2006-01-06 00:20:37 -08001489 if (sync_page_io(rdev->bdev,
1490 r10_bio->devs[sl].addr +
1491 sect + rdev->data_offset,
1492 s<<9, conf->tmppage, READ) == 0)
1493 /* Well, this device is dead */
1494 md_error(mddev, rdev);
NeilBrowne0a33272006-05-01 12:15:45 -07001495 rdev_dec_pending(rdev, mddev);
1496 rcu_read_lock();
NeilBrown4443ae12006-01-06 00:20:28 -08001497 }
1498 }
NeilBrowne0a33272006-05-01 12:15:45 -07001499 rcu_read_unlock();
NeilBrown4443ae12006-01-06 00:20:28 -08001500 } else {
1501 /* Cannot read from anywhere -- bye bye array */
1502 md_error(mddev, conf->mirrors[r10_bio->devs[r10_bio->read_slot].devnum].rdev);
1503 break;
1504 }
1505 sectors -= s;
1506 sect += s;
1507 }
1508
1509 unfreeze_array(conf);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001512 r10_bio->devs[r10_bio->read_slot].bio =
1513 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 bio_put(bio);
1515 mirror = read_balance(conf, r10_bio);
1516 if (mirror == -1) {
1517 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1518 " read error for block %llu\n",
1519 bdevname(bio->bi_bdev,b),
1520 (unsigned long long)r10_bio->sector);
1521 raid_end_bio_io(r10_bio);
1522 } else {
1523 rdev = conf->mirrors[mirror].rdev;
1524 if (printk_ratelimit())
1525 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1526 " another mirror\n",
1527 bdevname(rdev->bdev,b),
1528 (unsigned long long)r10_bio->sector);
1529 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1530 r10_bio->devs[r10_bio->read_slot].bio = bio;
1531 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1532 + rdev->data_offset;
1533 bio->bi_bdev = rdev->bdev;
1534 bio->bi_rw = READ;
1535 bio->bi_private = r10_bio;
1536 bio->bi_end_io = raid10_end_read_request;
1537 unplug = 1;
1538 generic_make_request(bio);
1539 }
1540 }
1541 }
1542 spin_unlock_irqrestore(&conf->device_lock, flags);
1543 if (unplug)
1544 unplug_slaves(mddev);
1545}
1546
1547
1548static int init_resync(conf_t *conf)
1549{
1550 int buffs;
1551
1552 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001553 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1555 if (!conf->r10buf_pool)
1556 return -ENOMEM;
1557 conf->next_resync = 0;
1558 return 0;
1559}
1560
1561/*
1562 * perform a "sync" on one "block"
1563 *
1564 * We need to make sure that no normal I/O request - particularly write
1565 * requests - conflict with active sync requests.
1566 *
1567 * This is achieved by tracking pending requests and a 'barrier' concept
1568 * that can be installed to exclude normal IO requests.
1569 *
1570 * Resync and recovery are handled very differently.
1571 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1572 *
1573 * For resync, we iterate over virtual addresses, read all copies,
1574 * and update if there are differences. If only one copy is live,
1575 * skip it.
1576 * For recovery, we iterate over physical addresses, read a good
1577 * value for each non-in_sync drive, and over-write.
1578 *
1579 * So, for recovery we may have several outstanding complex requests for a
1580 * given address, one for each out-of-sync device. We model this by allocating
1581 * a number of r10_bio structures, one for each out-of-sync device.
1582 * As we setup these structures, we collect all bio's together into a list
1583 * which we then process collectively to add pages, and then process again
1584 * to pass to generic_make_request.
1585 *
1586 * The r10_bio structures are linked using a borrowed master_bio pointer.
1587 * This link is counted in ->remaining. When the r10_bio that points to NULL
1588 * has its remaining count decremented to 0, the whole complex operation
1589 * is complete.
1590 *
1591 */
1592
NeilBrown57afd892005-06-21 17:17:13 -07001593static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 conf_t *conf = mddev_to_conf(mddev);
1596 r10bio_t *r10_bio;
1597 struct bio *biolist = NULL, *bio;
1598 sector_t max_sector, nr_sectors;
1599 int disk;
1600 int i;
NeilBrown6cce3b232006-01-06 00:20:16 -08001601 int max_sync;
1602 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 sector_t sectors_skipped = 0;
1605 int chunks_skipped = 0;
1606
1607 if (!conf->r10buf_pool)
1608 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 skipped:
1612 max_sector = mddev->size << 1;
1613 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1614 max_sector = mddev->resync_max_sectors;
1615 if (sector_nr >= max_sector) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001616 /* If we aborted, we need to abort the
1617 * sync on the 'current' bitmap chucks (there can
1618 * be several when recovering multiple devices).
1619 * as we may have started syncing it but not finished.
1620 * We can find the current address in
1621 * mddev->curr_resync, but for recovery,
1622 * we need to convert that to several
1623 * virtual addresses.
1624 */
1625 if (mddev->curr_resync < max_sector) { /* aborted */
1626 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1627 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1628 &sync_blocks, 1);
1629 else for (i=0; i<conf->raid_disks; i++) {
1630 sector_t sect =
1631 raid10_find_virt(conf, mddev->curr_resync, i);
1632 bitmap_end_sync(mddev->bitmap, sect,
1633 &sync_blocks, 1);
1634 }
1635 } else /* completed sync */
1636 conf->fullsync = 0;
1637
1638 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001640 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return sectors_skipped;
1642 }
1643 if (chunks_skipped >= conf->raid_disks) {
1644 /* if there has been nothing to do on any drive,
1645 * then there is nothing to do at all..
1646 */
NeilBrown57afd892005-06-21 17:17:13 -07001647 *skipped = 1;
1648 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650
1651 /* make sure whole request will fit in a chunk - if chunks
1652 * are meaningful
1653 */
1654 if (conf->near_copies < conf->raid_disks &&
1655 max_sector > (sector_nr | conf->chunk_mask))
1656 max_sector = (sector_nr | conf->chunk_mask) + 1;
1657 /*
1658 * If there is non-resync activity waiting for us then
1659 * put in a delay to throttle resync.
1660 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001661 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /* Again, very different code for resync and recovery.
1665 * Both must result in an r10bio with a list of bios that
1666 * have bi_end_io, bi_sector, bi_bdev set,
1667 * and bi_private set to the r10bio.
1668 * For recovery, we may actually create several r10bios
1669 * with 2 bios in each, that correspond to the bios in the main one.
1670 * In this case, the subordinate r10bios link back through a
1671 * borrowed master_bio pointer, and the counter in the master
1672 * includes a ref from each subordinate.
1673 */
1674 /* First, we decide what to do and set ->bi_end_io
1675 * To end_sync_read if we want to read, and
1676 * end_sync_write if we will want to write.
1677 */
1678
NeilBrown6cce3b232006-01-06 00:20:16 -08001679 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1681 /* recovery... the complicated one */
1682 int i, j, k;
1683 r10_bio = NULL;
1684
1685 for (i=0 ; i<conf->raid_disks; i++)
1686 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001687 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001688 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* want to reconstruct this device */
1690 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b232006-01-06 00:20:16 -08001691 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1692 int must_sync;
1693 /* Unless we are doing a full sync, we only need
1694 * to recover the block if it is set in the bitmap
1695 */
1696 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1697 &sync_blocks, 1);
1698 if (sync_blocks < max_sync)
1699 max_sync = sync_blocks;
1700 if (!must_sync &&
1701 !conf->fullsync) {
1702 /* yep, skip the sync_blocks here, but don't assume
1703 * that there will never be anything to do here
1704 */
1705 chunks_skipped = -1;
1706 continue;
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b232006-01-06 00:20:16 -08001710 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 atomic_set(&r10_bio->remaining, 0);
1712
1713 r10_bio->master_bio = (struct bio*)rb2;
1714 if (rb2)
1715 atomic_inc(&rb2->remaining);
1716 r10_bio->mddev = mddev;
1717 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b232006-01-06 00:20:16 -08001718 r10_bio->sector = sect;
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 raid10_find_phys(conf, r10_bio);
NeilBrown6cce3b232006-01-06 00:20:16 -08001721 /* Need to check if this section will still be
1722 * degraded
1723 */
1724 for (j=0; j<conf->copies;j++) {
1725 int d = r10_bio->devs[j].devnum;
1726 if (conf->mirrors[d].rdev == NULL ||
NeilBrowna24a8dd2006-01-06 00:20:35 -08001727 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
NeilBrown6cce3b232006-01-06 00:20:16 -08001728 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001729 break;
1730 }
NeilBrown6cce3b232006-01-06 00:20:16 -08001731 }
1732 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1733 &sync_blocks, still_degraded);
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 for (j=0; j<conf->copies;j++) {
1736 int d = r10_bio->devs[j].devnum;
1737 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001738 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* This is where we read from */
1740 bio = r10_bio->devs[0].bio;
1741 bio->bi_next = biolist;
1742 biolist = bio;
1743 bio->bi_private = r10_bio;
1744 bio->bi_end_io = end_sync_read;
1745 bio->bi_rw = 0;
1746 bio->bi_sector = r10_bio->devs[j].addr +
1747 conf->mirrors[d].rdev->data_offset;
1748 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1749 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1750 atomic_inc(&r10_bio->remaining);
1751 /* and we write to 'i' */
1752
1753 for (k=0; k<conf->copies; k++)
1754 if (r10_bio->devs[k].devnum == i)
1755 break;
1756 bio = r10_bio->devs[1].bio;
1757 bio->bi_next = biolist;
1758 biolist = bio;
1759 bio->bi_private = r10_bio;
1760 bio->bi_end_io = end_sync_write;
1761 bio->bi_rw = 1;
1762 bio->bi_sector = r10_bio->devs[k].addr +
1763 conf->mirrors[i].rdev->data_offset;
1764 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1765
1766 r10_bio->devs[0].devnum = d;
1767 r10_bio->devs[1].devnum = i;
1768
1769 break;
1770 }
1771 }
1772 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001773 /* Cannot recover, so abort the recovery */
1774 put_buf(r10_bio);
1775 r10_bio = rb2;
1776 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1777 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1778 mdname(mddev));
1779 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 }
1782 if (biolist == NULL) {
1783 while (r10_bio) {
1784 r10bio_t *rb2 = r10_bio;
1785 r10_bio = (r10bio_t*) rb2->master_bio;
1786 rb2->master_bio = NULL;
1787 put_buf(rb2);
1788 }
1789 goto giveup;
1790 }
1791 } else {
1792 /* resync. Schedule a read for every block at this virt offset */
1793 int count = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08001794
1795 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1796 &sync_blocks, mddev->degraded) &&
1797 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1798 /* We can skip this block */
1799 *skipped = 1;
1800 return sync_blocks + sectors_skipped;
1801 }
1802 if (sync_blocks < max_sync)
1803 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 r10_bio->mddev = mddev;
1807 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b232006-01-06 00:20:16 -08001808 raise_barrier(conf, 0);
1809 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 r10_bio->master_bio = NULL;
1812 r10_bio->sector = sector_nr;
1813 set_bit(R10BIO_IsSync, &r10_bio->state);
1814 raid10_find_phys(conf, r10_bio);
1815 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1816
1817 for (i=0; i<conf->copies; i++) {
1818 int d = r10_bio->devs[i].devnum;
1819 bio = r10_bio->devs[i].bio;
1820 bio->bi_end_io = NULL;
1821 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001822 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 continue;
1824 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1825 atomic_inc(&r10_bio->remaining);
1826 bio->bi_next = biolist;
1827 biolist = bio;
1828 bio->bi_private = r10_bio;
1829 bio->bi_end_io = end_sync_read;
1830 bio->bi_rw = 0;
1831 bio->bi_sector = r10_bio->devs[i].addr +
1832 conf->mirrors[d].rdev->data_offset;
1833 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1834 count++;
1835 }
1836
1837 if (count < 2) {
1838 for (i=0; i<conf->copies; i++) {
1839 int d = r10_bio->devs[i].devnum;
1840 if (r10_bio->devs[i].bio->bi_end_io)
1841 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1842 }
1843 put_buf(r10_bio);
1844 biolist = NULL;
1845 goto giveup;
1846 }
1847 }
1848
1849 for (bio = biolist; bio ; bio=bio->bi_next) {
1850
1851 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1852 if (bio->bi_end_io)
1853 bio->bi_flags |= 1 << BIO_UPTODATE;
1854 bio->bi_vcnt = 0;
1855 bio->bi_idx = 0;
1856 bio->bi_phys_segments = 0;
1857 bio->bi_hw_segments = 0;
1858 bio->bi_size = 0;
1859 }
1860
1861 nr_sectors = 0;
NeilBrown6cce3b232006-01-06 00:20:16 -08001862 if (sector_nr + max_sync < max_sector)
1863 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 do {
1865 struct page *page;
1866 int len = PAGE_SIZE;
1867 disk = 0;
1868 if (sector_nr + (len>>9) > max_sector)
1869 len = (max_sector - sector_nr) << 9;
1870 if (len == 0)
1871 break;
1872 for (bio= biolist ; bio ; bio=bio->bi_next) {
1873 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1874 if (bio_add_page(bio, page, len, 0) == 0) {
1875 /* stop here */
1876 struct bio *bio2;
1877 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1878 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1879 /* remove last page from this bio */
1880 bio2->bi_vcnt--;
1881 bio2->bi_size -= len;
1882 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1883 }
1884 goto bio_full;
1885 }
1886 disk = i;
1887 }
1888 nr_sectors += len>>9;
1889 sector_nr += len>>9;
1890 } while (biolist->bi_vcnt < RESYNC_PAGES);
1891 bio_full:
1892 r10_bio->sectors = nr_sectors;
1893
1894 while (biolist) {
1895 bio = biolist;
1896 biolist = biolist->bi_next;
1897
1898 bio->bi_next = NULL;
1899 r10_bio = bio->bi_private;
1900 r10_bio->sectors = nr_sectors;
1901
1902 if (bio->bi_end_io == end_sync_read) {
1903 md_sync_acct(bio->bi_bdev, nr_sectors);
1904 generic_make_request(bio);
1905 }
1906 }
1907
NeilBrown57afd892005-06-21 17:17:13 -07001908 if (sectors_skipped)
1909 /* pretend they weren't skipped, it makes
1910 * no important difference in this case
1911 */
1912 md_done_sync(mddev, sectors_skipped, 1);
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return sectors_skipped + nr_sectors;
1915 giveup:
1916 /* There is nowhere to write, so all non-sync
1917 * drives must be failed, so try the next chunk...
1918 */
1919 {
NeilBrown57afd892005-06-21 17:17:13 -07001920 sector_t sec = max_sector - sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 sectors_skipped += sec;
1922 chunks_skipped ++;
1923 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto skipped;
1925 }
1926}
1927
1928static int run(mddev_t *mddev)
1929{
1930 conf_t *conf;
1931 int i, disk_idx;
1932 mirror_info_t *disk;
1933 mdk_rdev_t *rdev;
1934 struct list_head *tmp;
NeilBrownc93983b2006-06-26 00:27:41 -07001935 int nc, fc, fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 sector_t stride, size;
1937
NeilBrown2604b702006-01-06 00:20:36 -08001938 if (mddev->chunk_size == 0) {
1939 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
1940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
NeilBrown2604b702006-01-06 00:20:36 -08001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 nc = mddev->layout & 255;
1944 fc = (mddev->layout >> 8) & 255;
NeilBrownc93983b2006-06-26 00:27:41 -07001945 fo = mddev->layout & (1<<16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
NeilBrownc93983b2006-06-26 00:27:41 -07001947 (mddev->layout >> 17)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1949 mdname(mddev), mddev->layout);
1950 goto out;
1951 }
1952 /*
1953 * copy the already verified devices into our private RAID10
1954 * bookkeeping area. [whatever we allocate in run(),
1955 * should be freed in stop()]
1956 */
NeilBrown4443ae12006-01-06 00:20:28 -08001957 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 mddev->private = conf;
1959 if (!conf) {
1960 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1961 mdname(mddev));
1962 goto out;
1963 }
NeilBrown4443ae12006-01-06 00:20:28 -08001964 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 GFP_KERNEL);
1966 if (!conf->mirrors) {
1967 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1968 mdname(mddev));
1969 goto out_free_conf;
1970 }
NeilBrown4443ae12006-01-06 00:20:28 -08001971
1972 conf->tmppage = alloc_page(GFP_KERNEL);
1973 if (!conf->tmppage)
1974 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 conf->near_copies = nc;
1977 conf->far_copies = fc;
1978 conf->copies = nc*fc;
NeilBrownc93983b2006-06-26 00:27:41 -07001979 conf->far_offset = fo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1981 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
NeilBrownc93983b2006-06-26 00:27:41 -07001982 if (fo)
1983 conf->stride = 1 << conf->chunk_shift;
1984 else {
1985 stride = mddev->size >> (conf->chunk_shift-1);
1986 sector_div(stride, fc);
1987 conf->stride = stride << conf->chunk_shift;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1990 r10bio_pool_free, conf);
1991 if (!conf->r10bio_pool) {
1992 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1993 mdname(mddev));
1994 goto out_free_conf;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 ITERATE_RDEV(mddev, rdev, tmp) {
1998 disk_idx = rdev->raid_disk;
1999 if (disk_idx >= mddev->raid_disks
2000 || disk_idx < 0)
2001 continue;
2002 disk = conf->mirrors + disk_idx;
2003
2004 disk->rdev = rdev;
2005
2006 blk_queue_stack_limits(mddev->queue,
2007 rdev->bdev->bd_disk->queue);
2008 /* as we don't honour merge_bvec_fn, we must never risk
2009 * violating it, so limit ->max_sector to one PAGE, as
2010 * a one page request is never in violation.
2011 */
2012 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2013 mddev->queue->max_sectors > (PAGE_SIZE>>9))
2014 mddev->queue->max_sectors = (PAGE_SIZE>>9);
2015
2016 disk->head_position = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08002017 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 conf->working_disks++;
2019 }
2020 conf->raid_disks = mddev->raid_disks;
2021 conf->mddev = mddev;
2022 spin_lock_init(&conf->device_lock);
2023 INIT_LIST_HEAD(&conf->retry_list);
2024
2025 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08002026 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
NeilBrown6d508242005-09-09 16:24:03 -07002028 /* need to check that every block has at least one working mirror */
2029 if (!enough(conf)) {
2030 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2031 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto out_free_conf;
2033 }
2034
2035 mddev->degraded = 0;
2036 for (i = 0; i < conf->raid_disks; i++) {
2037
2038 disk = conf->mirrors + i;
2039
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002040 if (!disk->rdev ||
2041 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 disk->head_position = 0;
2043 mddev->degraded++;
2044 }
2045 }
2046
2047
2048 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2049 if (!mddev->thread) {
2050 printk(KERN_ERR
2051 "raid10: couldn't allocate thread for %s\n",
2052 mdname(mddev));
2053 goto out_free_conf;
2054 }
2055
2056 printk(KERN_INFO
2057 "raid10: raid set %s active with %d out of %d devices\n",
2058 mdname(mddev), mddev->raid_disks - mddev->degraded,
2059 mddev->raid_disks);
2060 /*
2061 * Ok, everything is just fine now
2062 */
NeilBrown88388322006-06-26 00:27:50 -07002063 if (conf->far_offset) {
2064 size = mddev->size >> (conf->chunk_shift-1);
2065 size *= conf->raid_disks;
2066 size <<= conf->chunk_shift;
2067 sector_div(size, conf->far_copies);
2068 } else
2069 size = conf->stride * conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 sector_div(size, conf->near_copies);
2071 mddev->array_size = size/2;
2072 mddev->resync_max_sectors = size;
2073
NeilBrown7a5febe2005-05-16 21:53:16 -07002074 mddev->queue->unplug_fn = raid10_unplug;
2075 mddev->queue->issue_flush_fn = raid10_issue_flush;
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /* Calculate max read-ahead size.
2078 * We need to readahead at least twice a whole stripe....
2079 * maybe...
2080 */
2081 {
NeilBrown8932c2e2006-06-26 00:27:36 -07002082 int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 stripe /= conf->near_copies;
2084 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2085 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2086 }
2087
2088 if (conf->near_copies < mddev->raid_disks)
2089 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2090 return 0;
2091
2092out_free_conf:
2093 if (conf->r10bio_pool)
2094 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002095 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002096 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 kfree(conf);
2098 mddev->private = NULL;
2099out:
2100 return -EIO;
2101}
2102
2103static int stop(mddev_t *mddev)
2104{
2105 conf_t *conf = mddev_to_conf(mddev);
2106
2107 md_unregister_thread(mddev->thread);
2108 mddev->thread = NULL;
2109 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2110 if (conf->r10bio_pool)
2111 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002112 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 kfree(conf);
2114 mddev->private = NULL;
2115 return 0;
2116}
2117
NeilBrown6cce3b232006-01-06 00:20:16 -08002118static void raid10_quiesce(mddev_t *mddev, int state)
2119{
2120 conf_t *conf = mddev_to_conf(mddev);
2121
2122 switch(state) {
2123 case 1:
2124 raise_barrier(conf, 0);
2125 break;
2126 case 0:
2127 lower_barrier(conf);
2128 break;
2129 }
2130 if (mddev->thread) {
2131 if (mddev->bitmap)
2132 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2133 else
2134 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2135 md_wakeup_thread(mddev->thread);
2136 }
2137}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
NeilBrown2604b702006-01-06 00:20:36 -08002139static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002142 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 .owner = THIS_MODULE,
2144 .make_request = make_request,
2145 .run = run,
2146 .stop = stop,
2147 .status = status,
2148 .error_handler = error,
2149 .hot_add_disk = raid10_add_disk,
2150 .hot_remove_disk= raid10_remove_disk,
2151 .spare_active = raid10_spare_active,
2152 .sync_request = sync_request,
NeilBrown6cce3b232006-01-06 00:20:16 -08002153 .quiesce = raid10_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154};
2155
2156static int __init raid_init(void)
2157{
NeilBrown2604b702006-01-06 00:20:36 -08002158 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
2161static void raid_exit(void)
2162{
NeilBrown2604b702006-01-06 00:20:36 -08002163 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166module_init(raid_init);
2167module_exit(raid_exit);
2168MODULE_LICENSE("GPL");
2169MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002170MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002171MODULE_ALIAS("md-level-10");