blob: 617012bc107a0205c597890d5167f03d86c45475 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrown6cce3b22006-01-06 00:20:16 -080021#include "dm-bio-list.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/raid/raid10.h>
NeilBrown6cce3b22006-01-06 00:20:16 -080023#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * RAID10 provides a combination of RAID0 and RAID1 functionality.
27 * The layout of data is defined by
28 * chunk_size
29 * raid_disks
30 * near_copies (stored in low byte of layout)
31 * far_copies (stored in second byte of layout)
32 *
33 * The data to be stored is divided into chunks using chunksize.
34 * Each device is divided into far_copies sections.
35 * In each section, chunks are laid out in a style similar to raid0, but
36 * near_copies copies of each chunk is stored (each on a different drive).
37 * The starting device for each section is offset near_copies from the starting
38 * device of the previous section.
39 * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
40 * drive.
41 * near_copies and far_copies must be at least one, and their product is at most
42 * raid_disks.
43 */
44
45/*
46 * Number of guaranteed r10bios in case of extreme VM load:
47 */
48#define NR_RAID10_BIOS 256
49
50static void unplug_slaves(mddev_t *mddev);
51
NeilBrown0a27ec92006-01-06 00:20:13 -080052static void allow_barrier(conf_t *conf);
53static void lower_barrier(conf_t *conf);
54
Al Virodd0fc662005-10-07 07:46:04 +010055static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 conf_t *conf = data;
58 r10bio_t *r10_bio;
59 int size = offsetof(struct r10bio_s, devs[conf->copies]);
60
61 /* allocate a r10bio with room for raid_disks entries in the bios array */
NeilBrown9ffae0c2006-01-06 00:20:32 -080062 r10_bio = kzalloc(size, gfp_flags);
63 if (!r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 unplug_slaves(conf->mddev);
65
66 return r10_bio;
67}
68
69static void r10bio_pool_free(void *r10_bio, void *data)
70{
71 kfree(r10_bio);
72}
73
74#define RESYNC_BLOCK_SIZE (64*1024)
75//#define RESYNC_BLOCK_SIZE PAGE_SIZE
76#define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
77#define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
78#define RESYNC_WINDOW (2048*1024)
79
80/*
81 * When performing a resync, we need to read and compare, so
82 * we need as many pages are there are copies.
83 * When performing a recovery, we need 2 bios, one for read,
84 * one for write (we recover only one drive per r10buf)
85 *
86 */
Al Virodd0fc662005-10-07 07:46:04 +010087static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 conf_t *conf = data;
90 struct page *page;
91 r10bio_t *r10_bio;
92 struct bio *bio;
93 int i, j;
94 int nalloc;
95
96 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
97 if (!r10_bio) {
98 unplug_slaves(conf->mddev);
99 return NULL;
100 }
101
102 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
103 nalloc = conf->copies; /* resync */
104 else
105 nalloc = 2; /* recovery */
106
107 /*
108 * Allocate bios.
109 */
110 for (j = nalloc ; j-- ; ) {
111 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
112 if (!bio)
113 goto out_free_bio;
114 r10_bio->devs[j].bio = bio;
115 }
116 /*
117 * Allocate RESYNC_PAGES data pages and attach them
118 * where needed.
119 */
120 for (j = 0 ; j < nalloc; j++) {
121 bio = r10_bio->devs[j].bio;
122 for (i = 0; i < RESYNC_PAGES; i++) {
123 page = alloc_page(gfp_flags);
124 if (unlikely(!page))
125 goto out_free_pages;
126
127 bio->bi_io_vec[i].bv_page = page;
128 }
129 }
130
131 return r10_bio;
132
133out_free_pages:
134 for ( ; i > 0 ; i--)
NeilBrown1345b1d2006-01-06 00:20:40 -0800135 safe_put_page(bio->bi_io_vec[i-1].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 while (j--)
137 for (i = 0; i < RESYNC_PAGES ; i++)
NeilBrown1345b1d2006-01-06 00:20:40 -0800138 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 j = -1;
140out_free_bio:
141 while ( ++j < nalloc )
142 bio_put(r10_bio->devs[j].bio);
143 r10bio_pool_free(r10_bio, conf);
144 return NULL;
145}
146
147static void r10buf_pool_free(void *__r10_bio, void *data)
148{
149 int i;
150 conf_t *conf = data;
151 r10bio_t *r10bio = __r10_bio;
152 int j;
153
154 for (j=0; j < conf->copies; j++) {
155 struct bio *bio = r10bio->devs[j].bio;
156 if (bio) {
157 for (i = 0; i < RESYNC_PAGES; i++) {
NeilBrown1345b1d2006-01-06 00:20:40 -0800158 safe_put_page(bio->bi_io_vec[i].bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 bio->bi_io_vec[i].bv_page = NULL;
160 }
161 bio_put(bio);
162 }
163 }
164 r10bio_pool_free(r10bio, conf);
165}
166
167static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
168{
169 int i;
170
171 for (i = 0; i < conf->copies; i++) {
172 struct bio **bio = & r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -0800173 if (*bio && *bio != IO_BLOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 bio_put(*bio);
175 *bio = NULL;
176 }
177}
178
Arjan van de Ven858119e2006-01-14 13:20:43 -0800179static void free_r10bio(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 conf_t *conf = mddev_to_conf(r10_bio->mddev);
182
183 /*
184 * Wake up any possible resync thread that waits for the device
185 * to go idle.
186 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800187 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 put_all_bios(conf, r10_bio);
190 mempool_free(r10_bio, conf->r10bio_pool);
191}
192
Arjan van de Ven858119e2006-01-14 13:20:43 -0800193static void put_buf(r10bio_t *r10_bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 conf_t *conf = mddev_to_conf(r10_bio->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 mempool_free(r10_bio, conf->r10buf_pool);
198
NeilBrown0a27ec92006-01-06 00:20:13 -0800199 lower_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static void reschedule_retry(r10bio_t *r10_bio)
203{
204 unsigned long flags;
205 mddev_t *mddev = r10_bio->mddev;
206 conf_t *conf = mddev_to_conf(mddev);
207
208 spin_lock_irqsave(&conf->device_lock, flags);
209 list_add(&r10_bio->retry_list, &conf->retry_list);
NeilBrown4443ae12006-01-06 00:20:28 -0800210 conf->nr_queued ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 spin_unlock_irqrestore(&conf->device_lock, flags);
212
213 md_wakeup_thread(mddev->thread);
214}
215
216/*
217 * raid_end_bio_io() is called when we have finished servicing a mirrored
218 * operation and are ready to return a success/failure code to the buffer
219 * cache layer.
220 */
221static void raid_end_bio_io(r10bio_t *r10_bio)
222{
223 struct bio *bio = r10_bio->master_bio;
224
225 bio_endio(bio, bio->bi_size,
226 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
227 free_r10bio(r10_bio);
228}
229
230/*
231 * Update disk head position estimator based on IRQ completion info.
232 */
233static inline void update_head_pos(int slot, r10bio_t *r10_bio)
234{
235 conf_t *conf = mddev_to_conf(r10_bio->mddev);
236
237 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
238 r10_bio->devs[slot].addr + (r10_bio->sectors);
239}
240
241static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
242{
243 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
244 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
245 int slot, dev;
246 conf_t *conf = mddev_to_conf(r10_bio->mddev);
247
248 if (bio->bi_size)
249 return 1;
250
251 slot = r10_bio->read_slot;
252 dev = r10_bio->devs[slot].devnum;
253 /*
254 * this branch is our 'one mirror IO has finished' event handler:
255 */
NeilBrown4443ae12006-01-06 00:20:28 -0800256 update_head_pos(slot, r10_bio);
257
258 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /*
260 * Set R10BIO_Uptodate in our master bio, so that
261 * we will return a good error code to the higher
262 * levels even if IO on some other mirrored buffer fails.
263 *
264 * The 'master' represents the composite IO operation to
265 * user-side. So if something waits for IO, then it will
266 * wait for the 'master' bio.
267 */
268 set_bit(R10BIO_Uptodate, &r10_bio->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 raid_end_bio_io(r10_bio);
NeilBrown4443ae12006-01-06 00:20:28 -0800270 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /*
272 * oops, read error:
273 */
274 char b[BDEVNAME_SIZE];
275 if (printk_ratelimit())
276 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
277 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
278 reschedule_retry(r10_bio);
279 }
280
281 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
282 return 0;
283}
284
285static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
286{
287 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
288 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
289 int slot, dev;
290 conf_t *conf = mddev_to_conf(r10_bio->mddev);
291
292 if (bio->bi_size)
293 return 1;
294
295 for (slot = 0; slot < conf->copies; slot++)
296 if (r10_bio->devs[slot].bio == bio)
297 break;
298 dev = r10_bio->devs[slot].devnum;
299
300 /*
301 * this branch is our 'one mirror IO has finished' event handler:
302 */
NeilBrown6cce3b22006-01-06 00:20:16 -0800303 if (!uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
NeilBrown6cce3b22006-01-06 00:20:16 -0800305 /* an I/O failed, we can't clear the bitmap */
306 set_bit(R10BIO_Degraded, &r10_bio->state);
307 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /*
309 * Set R10BIO_Uptodate in our master bio, so that
310 * we will return a good error code for to the higher
311 * levels even if IO on some other mirrored buffer fails.
312 *
313 * The 'master' represents the composite IO operation to
314 * user-side. So if something waits for IO, then it will
315 * wait for the 'master' bio.
316 */
317 set_bit(R10BIO_Uptodate, &r10_bio->state);
318
319 update_head_pos(slot, r10_bio);
320
321 /*
322 *
323 * Let's see if all mirrored write operations have finished
324 * already.
325 */
326 if (atomic_dec_and_test(&r10_bio->remaining)) {
NeilBrown6cce3b22006-01-06 00:20:16 -0800327 /* clear the bitmap if all writes complete successfully */
328 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
329 r10_bio->sectors,
330 !test_bit(R10BIO_Degraded, &r10_bio->state),
331 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 md_write_end(r10_bio->mddev);
333 raid_end_bio_io(r10_bio);
334 }
335
336 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
337 return 0;
338}
339
340
341/*
342 * RAID10 layout manager
343 * Aswell as the chunksize and raid_disks count, there are two
344 * parameters: near_copies and far_copies.
345 * near_copies * far_copies must be <= raid_disks.
346 * Normally one of these will be 1.
347 * If both are 1, we get raid0.
348 * If near_copies == raid_disks, we get raid1.
349 *
350 * Chunks are layed out in raid0 style with near_copies copies of the
351 * first chunk, followed by near_copies copies of the next chunk and
352 * so on.
353 * If far_copies > 1, then after 1/far_copies of the array has been assigned
354 * as described above, we start again with a device offset of near_copies.
355 * So we effectively have another copy of the whole array further down all
356 * the drives, but with blocks on different drives.
357 * With this layout, and block is never stored twice on the one device.
358 *
359 * raid10_find_phys finds the sector offset of a given virtual sector
360 * on each device that it is on. If a block isn't on a device,
361 * that entry in the array is set to MaxSector.
362 *
363 * raid10_find_virt does the reverse mapping, from a device and a
364 * sector offset to a virtual address
365 */
366
367static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
368{
369 int n,f;
370 sector_t sector;
371 sector_t chunk;
372 sector_t stripe;
373 int dev;
374
375 int slot = 0;
376
377 /* now calculate first sector/dev */
378 chunk = r10bio->sector >> conf->chunk_shift;
379 sector = r10bio->sector & conf->chunk_mask;
380
381 chunk *= conf->near_copies;
382 stripe = chunk;
383 dev = sector_div(stripe, conf->raid_disks);
384
385 sector += stripe << conf->chunk_shift;
386
387 /* and calculate all the others */
388 for (n=0; n < conf->near_copies; n++) {
389 int d = dev;
390 sector_t s = sector;
391 r10bio->devs[slot].addr = sector;
392 r10bio->devs[slot].devnum = d;
393 slot++;
394
395 for (f = 1; f < conf->far_copies; f++) {
396 d += conf->near_copies;
397 if (d >= conf->raid_disks)
398 d -= conf->raid_disks;
399 s += conf->stride;
400 r10bio->devs[slot].devnum = d;
401 r10bio->devs[slot].addr = s;
402 slot++;
403 }
404 dev++;
405 if (dev >= conf->raid_disks) {
406 dev = 0;
407 sector += (conf->chunk_mask + 1);
408 }
409 }
410 BUG_ON(slot != conf->copies);
411}
412
413static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
414{
415 sector_t offset, chunk, vchunk;
416
417 while (sector > conf->stride) {
418 sector -= conf->stride;
419 if (dev < conf->near_copies)
420 dev += conf->raid_disks - conf->near_copies;
421 else
422 dev -= conf->near_copies;
423 }
424
425 offset = sector & conf->chunk_mask;
426 chunk = sector >> conf->chunk_shift;
427 vchunk = chunk * conf->raid_disks + dev;
428 sector_div(vchunk, conf->near_copies);
429 return (vchunk << conf->chunk_shift) + offset;
430}
431
432/**
433 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
434 * @q: request queue
435 * @bio: the buffer head that's been built up so far
436 * @biovec: the request that could be merged to it.
437 *
438 * Return amount of bytes we can accept at this offset
439 * If near_copies == raid_disk, there are no striping issues,
440 * but in that case, the function isn't called at all.
441 */
442static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
443 struct bio_vec *bio_vec)
444{
445 mddev_t *mddev = q->queuedata;
446 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
447 int max;
448 unsigned int chunk_sectors = mddev->chunk_size >> 9;
449 unsigned int bio_sectors = bio->bi_size >> 9;
450
451 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
452 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
453 if (max <= bio_vec->bv_len && bio_sectors == 0)
454 return bio_vec->bv_len;
455 else
456 return max;
457}
458
459/*
460 * This routine returns the disk from which the requested read should
461 * be done. There is a per-array 'next expected sequential IO' sector
462 * number - if this matches on the next IO then we use the last disk.
463 * There is also a per-disk 'last know head position' sector that is
464 * maintained from IRQ contexts, both the normal and the resync IO
465 * completion handlers update this position correctly. If there is no
466 * perfect sequential match then we pick the disk whose head is closest.
467 *
468 * If there are 2 mirrors in the same 2 devices, performance degrades
469 * because position is mirror, not device based.
470 *
471 * The rdev for the device selected will have nr_pending incremented.
472 */
473
474/*
475 * FIXME: possibly should rethink readbalancing and do it differently
476 * depending on near_copies / far_copies geometry.
477 */
478static int read_balance(conf_t *conf, r10bio_t *r10_bio)
479{
480 const unsigned long this_sector = r10_bio->sector;
481 int disk, slot, nslot;
482 const int sectors = r10_bio->sectors;
483 sector_t new_distance, current_distance;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800484 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 raid10_find_phys(conf, r10_bio);
487 rcu_read_lock();
488 /*
489 * Check if we can balance. We can balance on the whole
NeilBrown6cce3b22006-01-06 00:20:16 -0800490 * device if no resync is going on (recovery is ok), or below
491 * the resync window. We take the first readable disk when
492 * above the resync window.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 */
494 if (conf->mddev->recovery_cp < MaxSector
495 && (this_sector + sectors >= conf->next_resync)) {
496 /* make sure that disk is operational */
497 slot = 0;
498 disk = r10_bio->devs[slot].devnum;
499
Suzanne Woodd6065f72005-11-08 21:39:27 -0800500 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800501 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800502 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 slot++;
504 if (slot == conf->copies) {
505 slot = 0;
506 disk = -1;
507 break;
508 }
509 disk = r10_bio->devs[slot].devnum;
510 }
511 goto rb_out;
512 }
513
514
515 /* make sure the disk is operational */
516 slot = 0;
517 disk = r10_bio->devs[slot].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800518 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800519 r10_bio->devs[slot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800520 !test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 slot ++;
522 if (slot == conf->copies) {
523 disk = -1;
524 goto rb_out;
525 }
526 disk = r10_bio->devs[slot].devnum;
527 }
528
529
NeilBrown3ec67ac2005-09-09 16:23:40 -0700530 current_distance = abs(r10_bio->devs[slot].addr -
531 conf->mirrors[disk].head_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Find the disk whose head is closest */
534
535 for (nslot = slot; nslot < conf->copies; nslot++) {
536 int ndisk = r10_bio->devs[nslot].devnum;
537
538
Suzanne Woodd6065f72005-11-08 21:39:27 -0800539 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
NeilBrown0eb3ff12006-01-06 00:20:29 -0800540 r10_bio->devs[nslot].bio == IO_BLOCKED ||
NeilBrownb2d444d2005-11-08 21:39:31 -0800541 !test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 continue;
543
NeilBrown22dfdf52005-11-28 13:44:09 -0800544 /* This optimisation is debatable, and completely destroys
545 * sequential read speed for 'far copies' arrays. So only
546 * keep it for 'near' arrays, and review those later.
547 */
548 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 disk = ndisk;
550 slot = nslot;
551 break;
552 }
553 new_distance = abs(r10_bio->devs[nslot].addr -
554 conf->mirrors[ndisk].head_position);
555 if (new_distance < current_distance) {
556 current_distance = new_distance;
557 disk = ndisk;
558 slot = nslot;
559 }
560 }
561
562rb_out:
563 r10_bio->read_slot = slot;
564/* conf->next_seq_sect = this_sector + sectors;*/
565
Suzanne Woodd6065f72005-11-08 21:39:27 -0800566 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
NeilBrown29fc7e32006-02-03 03:03:41 -0800568 else
569 disk = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 rcu_read_unlock();
571
572 return disk;
573}
574
575static void unplug_slaves(mddev_t *mddev)
576{
577 conf_t *conf = mddev_to_conf(mddev);
578 int i;
579
580 rcu_read_lock();
581 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800582 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800583 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
585
586 atomic_inc(&rdev->nr_pending);
587 rcu_read_unlock();
588
589 if (r_queue->unplug_fn)
590 r_queue->unplug_fn(r_queue);
591
592 rdev_dec_pending(rdev, mddev);
593 rcu_read_lock();
594 }
595 }
596 rcu_read_unlock();
597}
598
599static void raid10_unplug(request_queue_t *q)
600{
NeilBrown6cce3b22006-01-06 00:20:16 -0800601 mddev_t *mddev = q->queuedata;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unplug_slaves(q->queuedata);
NeilBrown6cce3b22006-01-06 00:20:16 -0800604 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
608 sector_t *error_sector)
609{
610 mddev_t *mddev = q->queuedata;
611 conf_t *conf = mddev_to_conf(mddev);
612 int i, ret = 0;
613
614 rcu_read_lock();
615 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800616 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800617 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct block_device *bdev = rdev->bdev;
619 request_queue_t *r_queue = bdev_get_queue(bdev);
620
621 if (!r_queue->issue_flush_fn)
622 ret = -EOPNOTSUPP;
623 else {
624 atomic_inc(&rdev->nr_pending);
625 rcu_read_unlock();
626 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
627 error_sector);
628 rdev_dec_pending(rdev, mddev);
629 rcu_read_lock();
630 }
631 }
632 }
633 rcu_read_unlock();
634 return ret;
635}
636
NeilBrown0a27ec92006-01-06 00:20:13 -0800637/* Barriers....
638 * Sometimes we need to suspend IO while we do something else,
639 * either some resync/recovery, or reconfigure the array.
640 * To do this we raise a 'barrier'.
641 * The 'barrier' is a counter that can be raised multiple times
642 * to count how many activities are happening which preclude
643 * normal IO.
644 * We can only raise the barrier if there is no pending IO.
645 * i.e. if nr_pending == 0.
646 * We choose only to raise the barrier if no-one is waiting for the
647 * barrier to go down. This means that as soon as an IO request
648 * is ready, no other operations which require a barrier will start
649 * until the IO request has had a chance.
650 *
651 * So: regular IO calls 'wait_barrier'. When that returns there
652 * is no backgroup IO happening, It must arrange to call
653 * allow_barrier when it has finished its IO.
654 * backgroup IO calls must call raise_barrier. Once that returns
655 * there is no normal IO happeing. It must arrange to call
656 * lower_barrier when the particular background IO completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658#define RESYNC_DEPTH 32
659
NeilBrown6cce3b22006-01-06 00:20:16 -0800660static void raise_barrier(conf_t *conf, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
NeilBrown6cce3b22006-01-06 00:20:16 -0800662 BUG_ON(force && !conf->barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_lock_irq(&conf->resync_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
NeilBrown6cce3b22006-01-06 00:20:16 -0800665 /* Wait until no block IO is waiting (unless 'force') */
666 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
NeilBrown0a27ec92006-01-06 00:20:13 -0800667 conf->resync_lock,
668 raid10_unplug(conf->mddev->queue));
669
670 /* block any new IO from starting */
671 conf->barrier++;
672
673 /* No wait for all pending IO to complete */
674 wait_event_lock_irq(conf->wait_barrier,
675 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
676 conf->resync_lock,
677 raid10_unplug(conf->mddev->queue));
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_unlock_irq(&conf->resync_lock);
680}
681
NeilBrown0a27ec92006-01-06 00:20:13 -0800682static void lower_barrier(conf_t *conf)
683{
684 unsigned long flags;
685 spin_lock_irqsave(&conf->resync_lock, flags);
686 conf->barrier--;
687 spin_unlock_irqrestore(&conf->resync_lock, flags);
688 wake_up(&conf->wait_barrier);
689}
690
691static void wait_barrier(conf_t *conf)
692{
693 spin_lock_irq(&conf->resync_lock);
694 if (conf->barrier) {
695 conf->nr_waiting++;
696 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
697 conf->resync_lock,
698 raid10_unplug(conf->mddev->queue));
699 conf->nr_waiting--;
700 }
701 conf->nr_pending++;
702 spin_unlock_irq(&conf->resync_lock);
703}
704
705static void allow_barrier(conf_t *conf)
706{
707 unsigned long flags;
708 spin_lock_irqsave(&conf->resync_lock, flags);
709 conf->nr_pending--;
710 spin_unlock_irqrestore(&conf->resync_lock, flags);
711 wake_up(&conf->wait_barrier);
712}
713
NeilBrown4443ae12006-01-06 00:20:28 -0800714static void freeze_array(conf_t *conf)
715{
716 /* stop syncio and normal IO and wait for everything to
NeilBrownf1885932006-01-06 00:20:42 -0800717 * go quiet.
NeilBrown4443ae12006-01-06 00:20:28 -0800718 * We increment barrier and nr_waiting, and then
719 * wait until barrier+nr_pending match nr_queued+2
720 */
721 spin_lock_irq(&conf->resync_lock);
722 conf->barrier++;
723 conf->nr_waiting++;
724 wait_event_lock_irq(conf->wait_barrier,
725 conf->barrier+conf->nr_pending == conf->nr_queued+2,
726 conf->resync_lock,
727 raid10_unplug(conf->mddev->queue));
728 spin_unlock_irq(&conf->resync_lock);
729}
730
731static void unfreeze_array(conf_t *conf)
732{
733 /* reverse the effect of the freeze */
734 spin_lock_irq(&conf->resync_lock);
735 conf->barrier--;
736 conf->nr_waiting--;
737 wake_up(&conf->wait_barrier);
738 spin_unlock_irq(&conf->resync_lock);
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static int make_request(request_queue_t *q, struct bio * bio)
742{
743 mddev_t *mddev = q->queuedata;
744 conf_t *conf = mddev_to_conf(mddev);
745 mirror_info_t *mirror;
746 r10bio_t *r10_bio;
747 struct bio *read_bio;
748 int i;
749 int chunk_sects = conf->chunk_mask + 1;
Jens Axboea3623572005-11-01 09:26:16 +0100750 const int rw = bio_data_dir(bio);
NeilBrown6cce3b22006-01-06 00:20:16 -0800751 struct bio_list bl;
752 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
NeilBrowne5dcdd82005-09-09 16:23:41 -0700754 if (unlikely(bio_barrier(bio))) {
755 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
756 return 0;
757 }
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* If this request crosses a chunk boundary, we need to
760 * split it. This will only happen for 1 PAGE (or less) requests.
761 */
762 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
763 > chunk_sects &&
764 conf->near_copies < conf->raid_disks)) {
765 struct bio_pair *bp;
766 /* Sanity check -- queue functions should prevent this happening */
767 if (bio->bi_vcnt != 1 ||
768 bio->bi_idx != 0)
769 goto bad_map;
770 /* This is a one page bio that upper layers
771 * refuse to split for us, so we need to split it.
772 */
773 bp = bio_split(bio, bio_split_pool,
774 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
775 if (make_request(q, &bp->bio1))
776 generic_make_request(&bp->bio1);
777 if (make_request(q, &bp->bio2))
778 generic_make_request(&bp->bio2);
779
780 bio_pair_release(bp);
781 return 0;
782 bad_map:
783 printk("raid10_make_request bug: can't convert block across chunks"
784 " or bigger than %dk %llu %d\n", chunk_sects/2,
785 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
786
787 bio_io_error(bio, bio->bi_size);
788 return 0;
789 }
790
NeilBrown3d310eb2005-06-21 17:17:26 -0700791 md_write_start(mddev, bio);
NeilBrown06d91a52005-06-21 17:17:12 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * Register the new request and wait if the reconstruction
795 * thread has put up a bar for new requests.
796 * Continue immediately if no resync is active currently.
797 */
NeilBrown0a27ec92006-01-06 00:20:13 -0800798 wait_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jens Axboea3623572005-11-01 09:26:16 +0100800 disk_stat_inc(mddev->gendisk, ios[rw]);
801 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
804
805 r10_bio->master_bio = bio;
806 r10_bio->sectors = bio->bi_size >> 9;
807
808 r10_bio->mddev = mddev;
809 r10_bio->sector = bio->bi_sector;
NeilBrown6cce3b22006-01-06 00:20:16 -0800810 r10_bio->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Jens Axboea3623572005-11-01 09:26:16 +0100812 if (rw == READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * read balancing logic:
815 */
816 int disk = read_balance(conf, r10_bio);
817 int slot = r10_bio->read_slot;
818 if (disk < 0) {
819 raid_end_bio_io(r10_bio);
820 return 0;
821 }
822 mirror = conf->mirrors + disk;
823
824 read_bio = bio_clone(bio, GFP_NOIO);
825
826 r10_bio->devs[slot].bio = read_bio;
827
828 read_bio->bi_sector = r10_bio->devs[slot].addr +
829 mirror->rdev->data_offset;
830 read_bio->bi_bdev = mirror->rdev->bdev;
831 read_bio->bi_end_io = raid10_end_read_request;
832 read_bio->bi_rw = READ;
833 read_bio->bi_private = r10_bio;
834
835 generic_make_request(read_bio);
836 return 0;
837 }
838
839 /*
840 * WRITE:
841 */
842 /* first select target devices under spinlock and
843 * inc refcount on their rdev. Record them by setting
844 * bios[x] to bio
845 */
846 raid10_find_phys(conf, r10_bio);
847 rcu_read_lock();
848 for (i = 0; i < conf->copies; i++) {
849 int d = r10_bio->devs[i].devnum;
Suzanne Woodd6065f72005-11-08 21:39:27 -0800850 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
851 if (rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800852 !test_bit(Faulty, &rdev->flags)) {
Suzanne Woodd6065f72005-11-08 21:39:27 -0800853 atomic_inc(&rdev->nr_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 r10_bio->devs[i].bio = bio;
NeilBrown6cce3b22006-01-06 00:20:16 -0800855 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 r10_bio->devs[i].bio = NULL;
NeilBrown6cce3b22006-01-06 00:20:16 -0800857 set_bit(R10BIO_Degraded, &r10_bio->state);
858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 rcu_read_unlock();
861
NeilBrown6cce3b22006-01-06 00:20:16 -0800862 atomic_set(&r10_bio->remaining, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700863
NeilBrown6cce3b22006-01-06 00:20:16 -0800864 bio_list_init(&bl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 for (i = 0; i < conf->copies; i++) {
866 struct bio *mbio;
867 int d = r10_bio->devs[i].devnum;
868 if (!r10_bio->devs[i].bio)
869 continue;
870
871 mbio = bio_clone(bio, GFP_NOIO);
872 r10_bio->devs[i].bio = mbio;
873
874 mbio->bi_sector = r10_bio->devs[i].addr+
875 conf->mirrors[d].rdev->data_offset;
876 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
877 mbio->bi_end_io = raid10_end_write_request;
878 mbio->bi_rw = WRITE;
879 mbio->bi_private = r10_bio;
880
881 atomic_inc(&r10_bio->remaining);
NeilBrown6cce3b22006-01-06 00:20:16 -0800882 bio_list_add(&bl, mbio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
NeilBrown6cce3b22006-01-06 00:20:16 -0800885 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
886 spin_lock_irqsave(&conf->device_lock, flags);
887 bio_list_merge(&conf->pending_bio_list, &bl);
888 blk_plug_device(mddev->queue);
889 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 return 0;
892}
893
894static void status(struct seq_file *seq, mddev_t *mddev)
895{
896 conf_t *conf = mddev_to_conf(mddev);
897 int i;
898
899 if (conf->near_copies < conf->raid_disks)
900 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
901 if (conf->near_copies > 1)
902 seq_printf(seq, " %d near-copies", conf->near_copies);
903 if (conf->far_copies > 1)
904 seq_printf(seq, " %d far-copies", conf->far_copies);
905
906 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
907 conf->working_disks);
908 for (i = 0; i < conf->raid_disks; i++)
909 seq_printf(seq, "%s",
910 conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -0800911 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 seq_printf(seq, "]");
913}
914
915static void error(mddev_t *mddev, mdk_rdev_t *rdev)
916{
917 char b[BDEVNAME_SIZE];
918 conf_t *conf = mddev_to_conf(mddev);
919
920 /*
921 * If it is not operational, then we have already marked it as dead
922 * else if it is the last working disks, ignore the error, let the
923 * next level up know.
924 * else mark the drive as failed
925 */
NeilBrownb2d444d2005-11-08 21:39:31 -0800926 if (test_bit(In_sync, &rdev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 && conf->working_disks == 1)
928 /*
929 * Don't fail the drive, just return an IO error.
930 * The test should really be more sophisticated than
931 * "working_disks == 1", but it isn't critical, and
932 * can wait until we do more sophisticated "is the drive
933 * really dead" tests...
934 */
935 return;
NeilBrownb2d444d2005-11-08 21:39:31 -0800936 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 mddev->degraded++;
938 conf->working_disks--;
939 /*
940 * if recovery is running, make sure it aborts.
941 */
942 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
943 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800944 clear_bit(In_sync, &rdev->flags);
945 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 mddev->sb_dirty = 1;
947 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
948 " Operation continuing on %d devices\n",
949 bdevname(rdev->bdev,b), conf->working_disks);
950}
951
952static void print_conf(conf_t *conf)
953{
954 int i;
955 mirror_info_t *tmp;
956
957 printk("RAID10 conf printout:\n");
958 if (!conf) {
959 printk("(!conf)\n");
960 return;
961 }
962 printk(" --- wd:%d rd:%d\n", conf->working_disks,
963 conf->raid_disks);
964
965 for (i = 0; i < conf->raid_disks; i++) {
966 char b[BDEVNAME_SIZE];
967 tmp = conf->mirrors + i;
968 if (tmp->rdev)
969 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -0800970 i, !test_bit(In_sync, &tmp->rdev->flags),
971 !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 bdevname(tmp->rdev->bdev,b));
973 }
974}
975
976static void close_sync(conf_t *conf)
977{
NeilBrown0a27ec92006-01-06 00:20:13 -0800978 wait_barrier(conf);
979 allow_barrier(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 mempool_destroy(conf->r10buf_pool);
982 conf->r10buf_pool = NULL;
983}
984
NeilBrown6d508242005-09-09 16:24:03 -0700985/* check if there are enough drives for
986 * every block to appear on atleast one
987 */
988static int enough(conf_t *conf)
989{
990 int first = 0;
991
992 do {
993 int n = conf->copies;
994 int cnt = 0;
995 while (n--) {
996 if (conf->mirrors[first].rdev)
997 cnt++;
998 first = (first+1) % conf->raid_disks;
999 }
1000 if (cnt == 0)
1001 return 0;
1002 } while (first != 0);
1003 return 1;
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006static int raid10_spare_active(mddev_t *mddev)
1007{
1008 int i;
1009 conf_t *conf = mddev->private;
1010 mirror_info_t *tmp;
1011
1012 /*
1013 * Find all non-in_sync disks within the RAID10 configuration
1014 * and mark them in_sync
1015 */
1016 for (i = 0; i < conf->raid_disks; i++) {
1017 tmp = conf->mirrors + i;
1018 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08001019 && !test_bit(Faulty, &tmp->rdev->flags)
1020 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 conf->working_disks++;
1022 mddev->degraded--;
NeilBrownb2d444d2005-11-08 21:39:31 -08001023 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 }
1026
1027 print_conf(conf);
1028 return 0;
1029}
1030
1031
1032static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1033{
1034 conf_t *conf = mddev->private;
1035 int found = 0;
1036 int mirror;
1037 mirror_info_t *p;
1038
1039 if (mddev->recovery_cp < MaxSector)
1040 /* only hot-add to in-sync arrays, as recovery is
1041 * very different from resync
1042 */
1043 return 0;
NeilBrown6d508242005-09-09 16:24:03 -07001044 if (!enough(conf))
1045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
NeilBrown6cce3b22006-01-06 00:20:16 -08001047 if (rdev->saved_raid_disk >= 0 &&
1048 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1049 mirror = rdev->saved_raid_disk;
1050 else
1051 mirror = 0;
1052 for ( ; mirror < mddev->raid_disks; mirror++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if ( !(p=conf->mirrors+mirror)->rdev) {
1054
1055 blk_queue_stack_limits(mddev->queue,
1056 rdev->bdev->bd_disk->queue);
1057 /* as we don't honour merge_bvec_fn, we must never risk
1058 * violating it, so limit ->max_sector to one PAGE, as
1059 * a one page request is never in violation.
1060 */
1061 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1062 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1063 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1064
1065 p->head_position = 0;
1066 rdev->raid_disk = mirror;
1067 found = 1;
NeilBrown6cce3b22006-01-06 00:20:16 -08001068 if (rdev->saved_raid_disk != mirror)
1069 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08001070 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
1072 }
1073
1074 print_conf(conf);
1075 return found;
1076}
1077
1078static int raid10_remove_disk(mddev_t *mddev, int number)
1079{
1080 conf_t *conf = mddev->private;
1081 int err = 0;
1082 mdk_rdev_t *rdev;
1083 mirror_info_t *p = conf->mirrors+ number;
1084
1085 print_conf(conf);
1086 rdev = p->rdev;
1087 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001088 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 atomic_read(&rdev->nr_pending)) {
1090 err = -EBUSY;
1091 goto abort;
1092 }
1093 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07001094 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (atomic_read(&rdev->nr_pending)) {
1096 /* lost the race, try later */
1097 err = -EBUSY;
1098 p->rdev = rdev;
1099 }
1100 }
1101abort:
1102
1103 print_conf(conf);
1104 return err;
1105}
1106
1107
1108static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1111 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1112 int i,d;
1113
1114 if (bio->bi_size)
1115 return 1;
1116
1117 for (i=0; i<conf->copies; i++)
1118 if (r10_bio->devs[i].bio == bio)
1119 break;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001120 BUG_ON(i == conf->copies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 update_head_pos(i, r10_bio);
1122 d = r10_bio->devs[i].devnum;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001123
1124 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1125 set_bit(R10BIO_Uptodate, &r10_bio->state);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001126 else {
1127 atomic_add(r10_bio->sectors,
1128 &conf->mirrors[d].rdev->corrected_errors);
1129 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1130 md_error(r10_bio->mddev,
1131 conf->mirrors[d].rdev);
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* for reconstruct, we always reschedule after a read.
1135 * for resync, only after all reads
1136 */
1137 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1138 atomic_dec_and_test(&r10_bio->remaining)) {
1139 /* we have read all the blocks,
1140 * do the comparison in process context in raid10d
1141 */
1142 reschedule_retry(r10_bio);
1143 }
1144 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1145 return 0;
1146}
1147
1148static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1149{
1150 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1151 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1152 mddev_t *mddev = r10_bio->mddev;
1153 conf_t *conf = mddev_to_conf(mddev);
1154 int i,d;
1155
1156 if (bio->bi_size)
1157 return 1;
1158
1159 for (i = 0; i < conf->copies; i++)
1160 if (r10_bio->devs[i].bio == bio)
1161 break;
1162 d = r10_bio->devs[i].devnum;
1163
1164 if (!uptodate)
1165 md_error(mddev, conf->mirrors[d].rdev);
1166 update_head_pos(i, r10_bio);
1167
1168 while (atomic_dec_and_test(&r10_bio->remaining)) {
1169 if (r10_bio->master_bio == NULL) {
1170 /* the primary of several recovery bios */
1171 md_done_sync(mddev, r10_bio->sectors, 1);
1172 put_buf(r10_bio);
1173 break;
1174 } else {
1175 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1176 put_buf(r10_bio);
1177 r10_bio = r10_bio2;
1178 }
1179 }
1180 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1181 return 0;
1182}
1183
1184/*
1185 * Note: sync and recover and handled very differently for raid10
1186 * This code is for resync.
1187 * For resync, we read through virtual addresses and read all blocks.
1188 * If there is any error, we schedule a write. The lowest numbered
1189 * drive is authoritative.
1190 * However requests come for physical address, so we need to map.
1191 * For every physical address there are raid_disks/copies virtual addresses,
1192 * which is always are least one, but is not necessarly an integer.
1193 * This means that a physical address can span multiple chunks, so we may
1194 * have to submit multiple io requests for a single sync request.
1195 */
1196/*
1197 * We check if all blocks are in-sync and only write to blocks that
1198 * aren't in sync
1199 */
1200static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1201{
1202 conf_t *conf = mddev_to_conf(mddev);
1203 int i, first;
1204 struct bio *tbio, *fbio;
1205
1206 atomic_set(&r10_bio->remaining, 1);
1207
1208 /* find the first device with a block */
1209 for (i=0; i<conf->copies; i++)
1210 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1211 break;
1212
1213 if (i == conf->copies)
1214 goto done;
1215
1216 first = i;
1217 fbio = r10_bio->devs[i].bio;
1218
1219 /* now find blocks with errors */
NeilBrown0eb3ff12006-01-06 00:20:29 -08001220 for (i=0 ; i < conf->copies ; i++) {
1221 int j, d;
1222 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 tbio = r10_bio->devs[i].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001225
1226 if (tbio->bi_end_io != end_sync_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 continue;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001228 if (i == first)
1229 continue;
1230 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1231 /* We know that the bi_io_vec layout is the same for
1232 * both 'first' and 'i', so we just compare them.
1233 * All vec entries are PAGE_SIZE;
1234 */
1235 for (j = 0; j < vcnt; j++)
1236 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1237 page_address(tbio->bi_io_vec[j].bv_page),
1238 PAGE_SIZE))
1239 break;
1240 if (j == vcnt)
1241 continue;
1242 mddev->resync_mismatches += r10_bio->sectors;
1243 }
NeilBrown18f08812006-01-06 00:20:25 -08001244 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1245 /* Don't fix anything. */
1246 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /* Ok, we need to write this bio
1248 * First we need to fixup bv_offset, bv_len and
1249 * bi_vecs, as the read request might have corrupted these
1250 */
1251 tbio->bi_vcnt = vcnt;
1252 tbio->bi_size = r10_bio->sectors << 9;
1253 tbio->bi_idx = 0;
1254 tbio->bi_phys_segments = 0;
1255 tbio->bi_hw_segments = 0;
1256 tbio->bi_hw_front_size = 0;
1257 tbio->bi_hw_back_size = 0;
1258 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1259 tbio->bi_flags |= 1 << BIO_UPTODATE;
1260 tbio->bi_next = NULL;
1261 tbio->bi_rw = WRITE;
1262 tbio->bi_private = r10_bio;
1263 tbio->bi_sector = r10_bio->devs[i].addr;
1264
1265 for (j=0; j < vcnt ; j++) {
1266 tbio->bi_io_vec[j].bv_offset = 0;
1267 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1268
1269 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1270 page_address(fbio->bi_io_vec[j].bv_page),
1271 PAGE_SIZE);
1272 }
1273 tbio->bi_end_io = end_sync_write;
1274
1275 d = r10_bio->devs[i].devnum;
1276 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1277 atomic_inc(&r10_bio->remaining);
1278 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1279
1280 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1281 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1282 generic_make_request(tbio);
1283 }
1284
1285done:
1286 if (atomic_dec_and_test(&r10_bio->remaining)) {
1287 md_done_sync(mddev, r10_bio->sectors, 1);
1288 put_buf(r10_bio);
1289 }
1290}
1291
1292/*
1293 * Now for the recovery code.
1294 * Recovery happens across physical sectors.
1295 * We recover all non-is_sync drives by finding the virtual address of
1296 * each, and then choose a working drive that also has that virt address.
1297 * There is a separate r10_bio for each non-in_sync drive.
1298 * Only the first two slots are in use. The first for reading,
1299 * The second for writing.
1300 *
1301 */
1302
1303static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1304{
1305 conf_t *conf = mddev_to_conf(mddev);
1306 int i, d;
1307 struct bio *bio, *wbio;
1308
1309
1310 /* move the pages across to the second bio
1311 * and submit the write request
1312 */
1313 bio = r10_bio->devs[0].bio;
1314 wbio = r10_bio->devs[1].bio;
1315 for (i=0; i < wbio->bi_vcnt; i++) {
1316 struct page *p = bio->bi_io_vec[i].bv_page;
1317 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1318 wbio->bi_io_vec[i].bv_page = p;
1319 }
1320 d = r10_bio->devs[1].devnum;
1321
1322 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1323 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
NeilBrown0eb3ff12006-01-06 00:20:29 -08001324 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1325 generic_make_request(wbio);
1326 else
1327 bio_endio(wbio, wbio->bi_size, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330
1331/*
1332 * This is a kernel thread which:
1333 *
1334 * 1. Retries failed read operations on working mirrors.
1335 * 2. Updates the raid superblock when problems encounter.
1336 * 3. Performs writes following reads for array syncronising.
1337 */
1338
1339static void raid10d(mddev_t *mddev)
1340{
1341 r10bio_t *r10_bio;
1342 struct bio *bio;
1343 unsigned long flags;
1344 conf_t *conf = mddev_to_conf(mddev);
1345 struct list_head *head = &conf->retry_list;
1346 int unplug=0;
1347 mdk_rdev_t *rdev;
1348
1349 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 for (;;) {
1352 char b[BDEVNAME_SIZE];
1353 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown6cce3b22006-01-06 00:20:16 -08001354
1355 if (conf->pending_bio_list.head) {
1356 bio = bio_list_get(&conf->pending_bio_list);
1357 blk_remove_plug(mddev->queue);
1358 spin_unlock_irqrestore(&conf->device_lock, flags);
1359 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1360 if (bitmap_unplug(mddev->bitmap) != 0)
1361 printk("%s: bitmap file write failed!\n", mdname(mddev));
1362
1363 while (bio) { /* submit pending writes */
1364 struct bio *next = bio->bi_next;
1365 bio->bi_next = NULL;
1366 generic_make_request(bio);
1367 bio = next;
1368 }
1369 unplug = 1;
1370
1371 continue;
1372 }
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (list_empty(head))
1375 break;
1376 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1377 list_del(head->prev);
NeilBrown4443ae12006-01-06 00:20:28 -08001378 conf->nr_queued--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 spin_unlock_irqrestore(&conf->device_lock, flags);
1380
1381 mddev = r10_bio->mddev;
1382 conf = mddev_to_conf(mddev);
1383 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1384 sync_request_write(mddev, r10_bio);
1385 unplug = 1;
1386 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1387 recovery_request_write(mddev, r10_bio);
1388 unplug = 1;
1389 } else {
1390 int mirror;
NeilBrown4443ae12006-01-06 00:20:28 -08001391 /* we got a read error. Maybe the drive is bad. Maybe just
1392 * the block and we can fix it.
1393 * We freeze all other IO, and try reading the block from
1394 * other devices. When we find one, we re-write
1395 * and check it that fixes the read error.
1396 * This is all done synchronously while the array is
1397 * frozen.
1398 */
1399 int sect = 0; /* Offset from r10_bio->sector */
1400 int sectors = r10_bio->sectors;
1401 freeze_array(conf);
1402 if (mddev->ro == 0) while(sectors) {
1403 int s = sectors;
1404 int sl = r10_bio->read_slot;
1405 int success = 0;
1406
1407 if (s > (PAGE_SIZE>>9))
1408 s = PAGE_SIZE >> 9;
1409
1410 do {
1411 int d = r10_bio->devs[sl].devnum;
1412 rdev = conf->mirrors[d].rdev;
1413 if (rdev &&
1414 test_bit(In_sync, &rdev->flags) &&
1415 sync_page_io(rdev->bdev,
1416 r10_bio->devs[sl].addr +
1417 sect + rdev->data_offset,
1418 s<<9,
1419 conf->tmppage, READ))
1420 success = 1;
1421 else {
1422 sl++;
1423 if (sl == conf->copies)
1424 sl = 0;
1425 }
1426 } while (!success && sl != r10_bio->read_slot);
1427
1428 if (success) {
NeilBrown097426f2006-01-06 00:20:37 -08001429 int start = sl;
NeilBrown4443ae12006-01-06 00:20:28 -08001430 /* write it back and re-read */
1431 while (sl != r10_bio->read_slot) {
1432 int d;
1433 if (sl==0)
1434 sl = conf->copies;
1435 sl--;
1436 d = r10_bio->devs[sl].devnum;
1437 rdev = conf->mirrors[d].rdev;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001438 atomic_add(s, &rdev->corrected_errors);
NeilBrown4443ae12006-01-06 00:20:28 -08001439 if (rdev &&
1440 test_bit(In_sync, &rdev->flags)) {
1441 if (sync_page_io(rdev->bdev,
1442 r10_bio->devs[sl].addr +
1443 sect + rdev->data_offset,
NeilBrown097426f2006-01-06 00:20:37 -08001444 s<<9, conf->tmppage, WRITE) == 0)
NeilBrown4443ae12006-01-06 00:20:28 -08001445 /* Well, this device is dead */
1446 md_error(mddev, rdev);
NeilBrown097426f2006-01-06 00:20:37 -08001447 }
1448 }
1449 sl = start;
1450 while (sl != r10_bio->read_slot) {
1451 int d;
1452 if (sl==0)
1453 sl = conf->copies;
1454 sl--;
1455 d = r10_bio->devs[sl].devnum;
1456 rdev = conf->mirrors[d].rdev;
1457 if (rdev &&
1458 test_bit(In_sync, &rdev->flags)) {
1459 if (sync_page_io(rdev->bdev,
1460 r10_bio->devs[sl].addr +
1461 sect + rdev->data_offset,
1462 s<<9, conf->tmppage, READ) == 0)
1463 /* Well, this device is dead */
1464 md_error(mddev, rdev);
NeilBrown4443ae12006-01-06 00:20:28 -08001465 }
1466 }
1467 } else {
1468 /* Cannot read from anywhere -- bye bye array */
1469 md_error(mddev, conf->mirrors[r10_bio->devs[r10_bio->read_slot].devnum].rdev);
1470 break;
1471 }
1472 sectors -= s;
1473 sect += s;
1474 }
1475
1476 unfreeze_array(conf);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 bio = r10_bio->devs[r10_bio->read_slot].bio;
NeilBrown0eb3ff12006-01-06 00:20:29 -08001479 r10_bio->devs[r10_bio->read_slot].bio =
1480 mddev->ro ? IO_BLOCKED : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 bio_put(bio);
1482 mirror = read_balance(conf, r10_bio);
1483 if (mirror == -1) {
1484 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1485 " read error for block %llu\n",
1486 bdevname(bio->bi_bdev,b),
1487 (unsigned long long)r10_bio->sector);
1488 raid_end_bio_io(r10_bio);
1489 } else {
1490 rdev = conf->mirrors[mirror].rdev;
1491 if (printk_ratelimit())
1492 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1493 " another mirror\n",
1494 bdevname(rdev->bdev,b),
1495 (unsigned long long)r10_bio->sector);
1496 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1497 r10_bio->devs[r10_bio->read_slot].bio = bio;
1498 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1499 + rdev->data_offset;
1500 bio->bi_bdev = rdev->bdev;
1501 bio->bi_rw = READ;
1502 bio->bi_private = r10_bio;
1503 bio->bi_end_io = raid10_end_read_request;
1504 unplug = 1;
1505 generic_make_request(bio);
1506 }
1507 }
1508 }
1509 spin_unlock_irqrestore(&conf->device_lock, flags);
1510 if (unplug)
1511 unplug_slaves(mddev);
1512}
1513
1514
1515static int init_resync(conf_t *conf)
1516{
1517 int buffs;
1518
1519 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
Eric Sesterhennb6385482006-04-02 13:34:29 +02001520 BUG_ON(conf->r10buf_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1522 if (!conf->r10buf_pool)
1523 return -ENOMEM;
1524 conf->next_resync = 0;
1525 return 0;
1526}
1527
1528/*
1529 * perform a "sync" on one "block"
1530 *
1531 * We need to make sure that no normal I/O request - particularly write
1532 * requests - conflict with active sync requests.
1533 *
1534 * This is achieved by tracking pending requests and a 'barrier' concept
1535 * that can be installed to exclude normal IO requests.
1536 *
1537 * Resync and recovery are handled very differently.
1538 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1539 *
1540 * For resync, we iterate over virtual addresses, read all copies,
1541 * and update if there are differences. If only one copy is live,
1542 * skip it.
1543 * For recovery, we iterate over physical addresses, read a good
1544 * value for each non-in_sync drive, and over-write.
1545 *
1546 * So, for recovery we may have several outstanding complex requests for a
1547 * given address, one for each out-of-sync device. We model this by allocating
1548 * a number of r10_bio structures, one for each out-of-sync device.
1549 * As we setup these structures, we collect all bio's together into a list
1550 * which we then process collectively to add pages, and then process again
1551 * to pass to generic_make_request.
1552 *
1553 * The r10_bio structures are linked using a borrowed master_bio pointer.
1554 * This link is counted in ->remaining. When the r10_bio that points to NULL
1555 * has its remaining count decremented to 0, the whole complex operation
1556 * is complete.
1557 *
1558 */
1559
NeilBrown57afd892005-06-21 17:17:13 -07001560static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 conf_t *conf = mddev_to_conf(mddev);
1563 r10bio_t *r10_bio;
1564 struct bio *biolist = NULL, *bio;
1565 sector_t max_sector, nr_sectors;
1566 int disk;
1567 int i;
NeilBrown6cce3b22006-01-06 00:20:16 -08001568 int max_sync;
1569 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 sector_t sectors_skipped = 0;
1572 int chunks_skipped = 0;
1573
1574 if (!conf->r10buf_pool)
1575 if (init_resync(conf))
NeilBrown57afd892005-06-21 17:17:13 -07001576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 skipped:
1579 max_sector = mddev->size << 1;
1580 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1581 max_sector = mddev->resync_max_sectors;
1582 if (sector_nr >= max_sector) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001583 /* If we aborted, we need to abort the
1584 * sync on the 'current' bitmap chucks (there can
1585 * be several when recovering multiple devices).
1586 * as we may have started syncing it but not finished.
1587 * We can find the current address in
1588 * mddev->curr_resync, but for recovery,
1589 * we need to convert that to several
1590 * virtual addresses.
1591 */
1592 if (mddev->curr_resync < max_sector) { /* aborted */
1593 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1594 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1595 &sync_blocks, 1);
1596 else for (i=0; i<conf->raid_disks; i++) {
1597 sector_t sect =
1598 raid10_find_virt(conf, mddev->curr_resync, i);
1599 bitmap_end_sync(mddev->bitmap, sect,
1600 &sync_blocks, 1);
1601 }
1602 } else /* completed sync */
1603 conf->fullsync = 0;
1604
1605 bitmap_close_sync(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 close_sync(conf);
NeilBrown57afd892005-06-21 17:17:13 -07001607 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return sectors_skipped;
1609 }
1610 if (chunks_skipped >= conf->raid_disks) {
1611 /* if there has been nothing to do on any drive,
1612 * then there is nothing to do at all..
1613 */
NeilBrown57afd892005-06-21 17:17:13 -07001614 *skipped = 1;
1615 return (max_sector - sector_nr) + sectors_skipped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 /* make sure whole request will fit in a chunk - if chunks
1619 * are meaningful
1620 */
1621 if (conf->near_copies < conf->raid_disks &&
1622 max_sector > (sector_nr | conf->chunk_mask))
1623 max_sector = (sector_nr | conf->chunk_mask) + 1;
1624 /*
1625 * If there is non-resync activity waiting for us then
1626 * put in a delay to throttle resync.
1627 */
NeilBrown0a27ec92006-01-06 00:20:13 -08001628 if (!go_faster && conf->nr_waiting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /* Again, very different code for resync and recovery.
1632 * Both must result in an r10bio with a list of bios that
1633 * have bi_end_io, bi_sector, bi_bdev set,
1634 * and bi_private set to the r10bio.
1635 * For recovery, we may actually create several r10bios
1636 * with 2 bios in each, that correspond to the bios in the main one.
1637 * In this case, the subordinate r10bios link back through a
1638 * borrowed master_bio pointer, and the counter in the master
1639 * includes a ref from each subordinate.
1640 */
1641 /* First, we decide what to do and set ->bi_end_io
1642 * To end_sync_read if we want to read, and
1643 * end_sync_write if we will want to write.
1644 */
1645
NeilBrown6cce3b22006-01-06 00:20:16 -08001646 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1648 /* recovery... the complicated one */
1649 int i, j, k;
1650 r10_bio = NULL;
1651
1652 for (i=0 ; i<conf->raid_disks; i++)
1653 if (conf->mirrors[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001654 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001655 int still_degraded = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* want to reconstruct this device */
1657 r10bio_t *rb2 = r10_bio;
NeilBrown6cce3b22006-01-06 00:20:16 -08001658 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1659 int must_sync;
1660 /* Unless we are doing a full sync, we only need
1661 * to recover the block if it is set in the bitmap
1662 */
1663 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1664 &sync_blocks, 1);
1665 if (sync_blocks < max_sync)
1666 max_sync = sync_blocks;
1667 if (!must_sync &&
1668 !conf->fullsync) {
1669 /* yep, skip the sync_blocks here, but don't assume
1670 * that there will never be anything to do here
1671 */
1672 chunks_skipped = -1;
1673 continue;
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
NeilBrown6cce3b22006-01-06 00:20:16 -08001677 raise_barrier(conf, rb2 != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 atomic_set(&r10_bio->remaining, 0);
1679
1680 r10_bio->master_bio = (struct bio*)rb2;
1681 if (rb2)
1682 atomic_inc(&rb2->remaining);
1683 r10_bio->mddev = mddev;
1684 set_bit(R10BIO_IsRecover, &r10_bio->state);
NeilBrown6cce3b22006-01-06 00:20:16 -08001685 r10_bio->sector = sect;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 raid10_find_phys(conf, r10_bio);
NeilBrown6cce3b22006-01-06 00:20:16 -08001688 /* Need to check if this section will still be
1689 * degraded
1690 */
1691 for (j=0; j<conf->copies;j++) {
1692 int d = r10_bio->devs[j].devnum;
1693 if (conf->mirrors[d].rdev == NULL ||
NeilBrowna24a8dd2006-01-06 00:20:35 -08001694 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001695 still_degraded = 1;
NeilBrowna24a8dd2006-01-06 00:20:35 -08001696 break;
1697 }
NeilBrown6cce3b22006-01-06 00:20:16 -08001698 }
1699 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1700 &sync_blocks, still_degraded);
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 for (j=0; j<conf->copies;j++) {
1703 int d = r10_bio->devs[j].devnum;
1704 if (conf->mirrors[d].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08001705 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* This is where we read from */
1707 bio = r10_bio->devs[0].bio;
1708 bio->bi_next = biolist;
1709 biolist = bio;
1710 bio->bi_private = r10_bio;
1711 bio->bi_end_io = end_sync_read;
1712 bio->bi_rw = 0;
1713 bio->bi_sector = r10_bio->devs[j].addr +
1714 conf->mirrors[d].rdev->data_offset;
1715 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1716 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1717 atomic_inc(&r10_bio->remaining);
1718 /* and we write to 'i' */
1719
1720 for (k=0; k<conf->copies; k++)
1721 if (r10_bio->devs[k].devnum == i)
1722 break;
1723 bio = r10_bio->devs[1].bio;
1724 bio->bi_next = biolist;
1725 biolist = bio;
1726 bio->bi_private = r10_bio;
1727 bio->bi_end_io = end_sync_write;
1728 bio->bi_rw = 1;
1729 bio->bi_sector = r10_bio->devs[k].addr +
1730 conf->mirrors[i].rdev->data_offset;
1731 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1732
1733 r10_bio->devs[0].devnum = d;
1734 r10_bio->devs[1].devnum = i;
1735
1736 break;
1737 }
1738 }
1739 if (j == conf->copies) {
NeilBrown87fc7672005-09-09 16:24:04 -07001740 /* Cannot recover, so abort the recovery */
1741 put_buf(r10_bio);
1742 r10_bio = rb2;
1743 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1744 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1745 mdname(mddev));
1746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748 }
1749 if (biolist == NULL) {
1750 while (r10_bio) {
1751 r10bio_t *rb2 = r10_bio;
1752 r10_bio = (r10bio_t*) rb2->master_bio;
1753 rb2->master_bio = NULL;
1754 put_buf(rb2);
1755 }
1756 goto giveup;
1757 }
1758 } else {
1759 /* resync. Schedule a read for every block at this virt offset */
1760 int count = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001761
1762 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1763 &sync_blocks, mddev->degraded) &&
1764 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1765 /* We can skip this block */
1766 *skipped = 1;
1767 return sync_blocks + sectors_skipped;
1768 }
1769 if (sync_blocks < max_sync)
1770 max_sync = sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 r10_bio->mddev = mddev;
1774 atomic_set(&r10_bio->remaining, 0);
NeilBrown6cce3b22006-01-06 00:20:16 -08001775 raise_barrier(conf, 0);
1776 conf->next_resync = sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 r10_bio->master_bio = NULL;
1779 r10_bio->sector = sector_nr;
1780 set_bit(R10BIO_IsSync, &r10_bio->state);
1781 raid10_find_phys(conf, r10_bio);
1782 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1783
1784 for (i=0; i<conf->copies; i++) {
1785 int d = r10_bio->devs[i].devnum;
1786 bio = r10_bio->devs[i].bio;
1787 bio->bi_end_io = NULL;
1788 if (conf->mirrors[d].rdev == NULL ||
NeilBrownb2d444d2005-11-08 21:39:31 -08001789 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 continue;
1791 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1792 atomic_inc(&r10_bio->remaining);
1793 bio->bi_next = biolist;
1794 biolist = bio;
1795 bio->bi_private = r10_bio;
1796 bio->bi_end_io = end_sync_read;
1797 bio->bi_rw = 0;
1798 bio->bi_sector = r10_bio->devs[i].addr +
1799 conf->mirrors[d].rdev->data_offset;
1800 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1801 count++;
1802 }
1803
1804 if (count < 2) {
1805 for (i=0; i<conf->copies; i++) {
1806 int d = r10_bio->devs[i].devnum;
1807 if (r10_bio->devs[i].bio->bi_end_io)
1808 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1809 }
1810 put_buf(r10_bio);
1811 biolist = NULL;
1812 goto giveup;
1813 }
1814 }
1815
1816 for (bio = biolist; bio ; bio=bio->bi_next) {
1817
1818 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1819 if (bio->bi_end_io)
1820 bio->bi_flags |= 1 << BIO_UPTODATE;
1821 bio->bi_vcnt = 0;
1822 bio->bi_idx = 0;
1823 bio->bi_phys_segments = 0;
1824 bio->bi_hw_segments = 0;
1825 bio->bi_size = 0;
1826 }
1827
1828 nr_sectors = 0;
NeilBrown6cce3b22006-01-06 00:20:16 -08001829 if (sector_nr + max_sync < max_sector)
1830 max_sector = sector_nr + max_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 do {
1832 struct page *page;
1833 int len = PAGE_SIZE;
1834 disk = 0;
1835 if (sector_nr + (len>>9) > max_sector)
1836 len = (max_sector - sector_nr) << 9;
1837 if (len == 0)
1838 break;
1839 for (bio= biolist ; bio ; bio=bio->bi_next) {
1840 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1841 if (bio_add_page(bio, page, len, 0) == 0) {
1842 /* stop here */
1843 struct bio *bio2;
1844 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1845 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1846 /* remove last page from this bio */
1847 bio2->bi_vcnt--;
1848 bio2->bi_size -= len;
1849 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1850 }
1851 goto bio_full;
1852 }
1853 disk = i;
1854 }
1855 nr_sectors += len>>9;
1856 sector_nr += len>>9;
1857 } while (biolist->bi_vcnt < RESYNC_PAGES);
1858 bio_full:
1859 r10_bio->sectors = nr_sectors;
1860
1861 while (biolist) {
1862 bio = biolist;
1863 biolist = biolist->bi_next;
1864
1865 bio->bi_next = NULL;
1866 r10_bio = bio->bi_private;
1867 r10_bio->sectors = nr_sectors;
1868
1869 if (bio->bi_end_io == end_sync_read) {
1870 md_sync_acct(bio->bi_bdev, nr_sectors);
1871 generic_make_request(bio);
1872 }
1873 }
1874
NeilBrown57afd892005-06-21 17:17:13 -07001875 if (sectors_skipped)
1876 /* pretend they weren't skipped, it makes
1877 * no important difference in this case
1878 */
1879 md_done_sync(mddev, sectors_skipped, 1);
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return sectors_skipped + nr_sectors;
1882 giveup:
1883 /* There is nowhere to write, so all non-sync
1884 * drives must be failed, so try the next chunk...
1885 */
1886 {
NeilBrown57afd892005-06-21 17:17:13 -07001887 sector_t sec = max_sector - sector_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 sectors_skipped += sec;
1889 chunks_skipped ++;
1890 sector_nr = max_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 goto skipped;
1892 }
1893}
1894
1895static int run(mddev_t *mddev)
1896{
1897 conf_t *conf;
1898 int i, disk_idx;
1899 mirror_info_t *disk;
1900 mdk_rdev_t *rdev;
1901 struct list_head *tmp;
1902 int nc, fc;
1903 sector_t stride, size;
1904
NeilBrown2604b702006-01-06 00:20:36 -08001905 if (mddev->chunk_size == 0) {
1906 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
1907 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
NeilBrown2604b702006-01-06 00:20:36 -08001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 nc = mddev->layout & 255;
1911 fc = (mddev->layout >> 8) & 255;
1912 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
1913 (mddev->layout >> 16)) {
1914 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1915 mdname(mddev), mddev->layout);
1916 goto out;
1917 }
1918 /*
1919 * copy the already verified devices into our private RAID10
1920 * bookkeeping area. [whatever we allocate in run(),
1921 * should be freed in stop()]
1922 */
NeilBrown4443ae12006-01-06 00:20:28 -08001923 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 mddev->private = conf;
1925 if (!conf) {
1926 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1927 mdname(mddev));
1928 goto out;
1929 }
NeilBrown4443ae12006-01-06 00:20:28 -08001930 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 GFP_KERNEL);
1932 if (!conf->mirrors) {
1933 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1934 mdname(mddev));
1935 goto out_free_conf;
1936 }
NeilBrown4443ae12006-01-06 00:20:28 -08001937
1938 conf->tmppage = alloc_page(GFP_KERNEL);
1939 if (!conf->tmppage)
1940 goto out_free_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 conf->near_copies = nc;
1943 conf->far_copies = fc;
1944 conf->copies = nc*fc;
1945 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1946 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
1947 stride = mddev->size >> (conf->chunk_shift-1);
1948 sector_div(stride, fc);
1949 conf->stride = stride << conf->chunk_shift;
1950
1951 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1952 r10bio_pool_free, conf);
1953 if (!conf->r10bio_pool) {
1954 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1955 mdname(mddev));
1956 goto out_free_conf;
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 ITERATE_RDEV(mddev, rdev, tmp) {
1960 disk_idx = rdev->raid_disk;
1961 if (disk_idx >= mddev->raid_disks
1962 || disk_idx < 0)
1963 continue;
1964 disk = conf->mirrors + disk_idx;
1965
1966 disk->rdev = rdev;
1967
1968 blk_queue_stack_limits(mddev->queue,
1969 rdev->bdev->bd_disk->queue);
1970 /* as we don't honour merge_bvec_fn, we must never risk
1971 * violating it, so limit ->max_sector to one PAGE, as
1972 * a one page request is never in violation.
1973 */
1974 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1975 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1976 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1977
1978 disk->head_position = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08001979 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 conf->working_disks++;
1981 }
1982 conf->raid_disks = mddev->raid_disks;
1983 conf->mddev = mddev;
1984 spin_lock_init(&conf->device_lock);
1985 INIT_LIST_HEAD(&conf->retry_list);
1986
1987 spin_lock_init(&conf->resync_lock);
NeilBrown0a27ec92006-01-06 00:20:13 -08001988 init_waitqueue_head(&conf->wait_barrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
NeilBrown6d508242005-09-09 16:24:03 -07001990 /* need to check that every block has at least one working mirror */
1991 if (!enough(conf)) {
1992 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
1993 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto out_free_conf;
1995 }
1996
1997 mddev->degraded = 0;
1998 for (i = 0; i < conf->raid_disks; i++) {
1999
2000 disk = conf->mirrors + i;
2001
2002 if (!disk->rdev) {
2003 disk->head_position = 0;
2004 mddev->degraded++;
2005 }
2006 }
2007
2008
2009 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2010 if (!mddev->thread) {
2011 printk(KERN_ERR
2012 "raid10: couldn't allocate thread for %s\n",
2013 mdname(mddev));
2014 goto out_free_conf;
2015 }
2016
2017 printk(KERN_INFO
2018 "raid10: raid set %s active with %d out of %d devices\n",
2019 mdname(mddev), mddev->raid_disks - mddev->degraded,
2020 mddev->raid_disks);
2021 /*
2022 * Ok, everything is just fine now
2023 */
2024 size = conf->stride * conf->raid_disks;
2025 sector_div(size, conf->near_copies);
2026 mddev->array_size = size/2;
2027 mddev->resync_max_sectors = size;
2028
NeilBrown7a5febe2005-05-16 21:53:16 -07002029 mddev->queue->unplug_fn = raid10_unplug;
2030 mddev->queue->issue_flush_fn = raid10_issue_flush;
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /* Calculate max read-ahead size.
2033 * We need to readahead at least twice a whole stripe....
2034 * maybe...
2035 */
2036 {
NeilBrown2d1f3b52006-01-06 00:20:31 -08002037 int stripe = conf->raid_disks * mddev->chunk_size / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 stripe /= conf->near_copies;
2039 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2040 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2041 }
2042
2043 if (conf->near_copies < mddev->raid_disks)
2044 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2045 return 0;
2046
2047out_free_conf:
2048 if (conf->r10bio_pool)
2049 mempool_destroy(conf->r10bio_pool);
NeilBrown1345b1d2006-01-06 00:20:40 -08002050 safe_put_page(conf->tmppage);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002051 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 kfree(conf);
2053 mddev->private = NULL;
2054out:
2055 return -EIO;
2056}
2057
2058static int stop(mddev_t *mddev)
2059{
2060 conf_t *conf = mddev_to_conf(mddev);
2061
2062 md_unregister_thread(mddev->thread);
2063 mddev->thread = NULL;
2064 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2065 if (conf->r10bio_pool)
2066 mempool_destroy(conf->r10bio_pool);
Jesper Juhl990a8ba2005-06-21 17:17:30 -07002067 kfree(conf->mirrors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 kfree(conf);
2069 mddev->private = NULL;
2070 return 0;
2071}
2072
NeilBrown6cce3b22006-01-06 00:20:16 -08002073static void raid10_quiesce(mddev_t *mddev, int state)
2074{
2075 conf_t *conf = mddev_to_conf(mddev);
2076
2077 switch(state) {
2078 case 1:
2079 raise_barrier(conf, 0);
2080 break;
2081 case 0:
2082 lower_barrier(conf);
2083 break;
2084 }
2085 if (mddev->thread) {
2086 if (mddev->bitmap)
2087 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2088 else
2089 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2090 md_wakeup_thread(mddev->thread);
2091 }
2092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
NeilBrown2604b702006-01-06 00:20:36 -08002094static struct mdk_personality raid10_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 .name = "raid10",
NeilBrown2604b702006-01-06 00:20:36 -08002097 .level = 10,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 .owner = THIS_MODULE,
2099 .make_request = make_request,
2100 .run = run,
2101 .stop = stop,
2102 .status = status,
2103 .error_handler = error,
2104 .hot_add_disk = raid10_add_disk,
2105 .hot_remove_disk= raid10_remove_disk,
2106 .spare_active = raid10_spare_active,
2107 .sync_request = sync_request,
NeilBrown6cce3b22006-01-06 00:20:16 -08002108 .quiesce = raid10_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109};
2110
2111static int __init raid_init(void)
2112{
NeilBrown2604b702006-01-06 00:20:36 -08002113 return register_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116static void raid_exit(void)
2117{
NeilBrown2604b702006-01-06 00:20:36 -08002118 unregister_md_personality(&raid10_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121module_init(raid_init);
2122module_exit(raid_exit);
2123MODULE_LICENSE("GPL");
2124MODULE_ALIAS("md-personality-9"); /* RAID10 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002125MODULE_ALIAS("md-raid10");
NeilBrown2604b702006-01-06 00:20:36 -08002126MODULE_ALIAS("md-level-10");