[PATCH] md: move bitmap_create to after md array has been initialised
[linux-2.6-block.git] / drivers / md / raid10.c
CommitLineData
1da177e4
LT
1/*
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
21#include <linux/raid/raid10.h>
22
23/*
24 * RAID10 provides a combination of RAID0 and RAID1 functionality.
25 * The layout of data is defined by
26 * chunk_size
27 * raid_disks
28 * near_copies (stored in low byte of layout)
29 * far_copies (stored in second byte of layout)
30 *
31 * The data to be stored is divided into chunks using chunksize.
32 * Each device is divided into far_copies sections.
33 * In each section, chunks are laid out in a style similar to raid0, but
34 * near_copies copies of each chunk is stored (each on a different drive).
35 * The starting device for each section is offset near_copies from the starting
36 * device of the previous section.
37 * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
38 * drive.
39 * near_copies and far_copies must be at least one, and their product is at most
40 * raid_disks.
41 */
42
43/*
44 * Number of guaranteed r10bios in case of extreme VM load:
45 */
46#define NR_RAID10_BIOS 256
47
48static void unplug_slaves(mddev_t *mddev);
49
0a27ec96
N
50static void allow_barrier(conf_t *conf);
51static void lower_barrier(conf_t *conf);
52
dd0fc66f 53static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
1da177e4
LT
54{
55 conf_t *conf = data;
56 r10bio_t *r10_bio;
57 int size = offsetof(struct r10bio_s, devs[conf->copies]);
58
59 /* allocate a r10bio with room for raid_disks entries in the bios array */
60 r10_bio = kmalloc(size, gfp_flags);
61 if (r10_bio)
62 memset(r10_bio, 0, size);
63 else
64 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 */
dd0fc66f 87static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
1da177e4
LT
88{
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--)
135 __free_page(bio->bi_io_vec[i-1].bv_page);
136 while (j--)
137 for (i = 0; i < RESYNC_PAGES ; i++)
138 __free_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
139 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++) {
158 __free_page(bio->bi_io_vec[i].bv_page);
159 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;
173 if (*bio)
174 bio_put(*bio);
175 *bio = NULL;
176 }
177}
178
179static inline void free_r10bio(r10bio_t *r10_bio)
180{
1da177e4
LT
181 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 */
0a27ec96 187 allow_barrier(conf);
1da177e4
LT
188
189 put_all_bios(conf, r10_bio);
190 mempool_free(r10_bio, conf->r10bio_pool);
191}
192
193static inline void put_buf(r10bio_t *r10_bio)
194{
195 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1da177e4
LT
196
197 mempool_free(r10_bio, conf->r10buf_pool);
198
0a27ec96 199 lower_barrier(conf);
1da177e4
LT
200}
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);
210 spin_unlock_irqrestore(&conf->device_lock, flags);
211
212 md_wakeup_thread(mddev->thread);
213}
214
215/*
216 * raid_end_bio_io() is called when we have finished servicing a mirrored
217 * operation and are ready to return a success/failure code to the buffer
218 * cache layer.
219 */
220static void raid_end_bio_io(r10bio_t *r10_bio)
221{
222 struct bio *bio = r10_bio->master_bio;
223
224 bio_endio(bio, bio->bi_size,
225 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
226 free_r10bio(r10_bio);
227}
228
229/*
230 * Update disk head position estimator based on IRQ completion info.
231 */
232static inline void update_head_pos(int slot, r10bio_t *r10_bio)
233{
234 conf_t *conf = mddev_to_conf(r10_bio->mddev);
235
236 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
237 r10_bio->devs[slot].addr + (r10_bio->sectors);
238}
239
240static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
241{
242 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
243 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
244 int slot, dev;
245 conf_t *conf = mddev_to_conf(r10_bio->mddev);
246
247 if (bio->bi_size)
248 return 1;
249
250 slot = r10_bio->read_slot;
251 dev = r10_bio->devs[slot].devnum;
252 /*
253 * this branch is our 'one mirror IO has finished' event handler:
254 */
255 if (!uptodate)
256 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
257 else
258 /*
259 * Set R10BIO_Uptodate in our master bio, so that
260 * we will return a good error code to the higher
261 * levels even if IO on some other mirrored buffer fails.
262 *
263 * The 'master' represents the composite IO operation to
264 * user-side. So if something waits for IO, then it will
265 * wait for the 'master' bio.
266 */
267 set_bit(R10BIO_Uptodate, &r10_bio->state);
268
269 update_head_pos(slot, r10_bio);
270
271 /*
272 * we have only one bio on the read side
273 */
274 if (uptodate)
275 raid_end_bio_io(r10_bio);
276 else {
277 /*
278 * oops, read error:
279 */
280 char b[BDEVNAME_SIZE];
281 if (printk_ratelimit())
282 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
283 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
284 reschedule_retry(r10_bio);
285 }
286
287 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
288 return 0;
289}
290
291static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
292{
293 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
294 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
295 int slot, dev;
296 conf_t *conf = mddev_to_conf(r10_bio->mddev);
297
298 if (bio->bi_size)
299 return 1;
300
301 for (slot = 0; slot < conf->copies; slot++)
302 if (r10_bio->devs[slot].bio == bio)
303 break;
304 dev = r10_bio->devs[slot].devnum;
305
306 /*
307 * this branch is our 'one mirror IO has finished' event handler:
308 */
309 if (!uptodate)
310 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
311 else
312 /*
313 * Set R10BIO_Uptodate in our master bio, so that
314 * we will return a good error code for to the higher
315 * levels even if IO on some other mirrored buffer fails.
316 *
317 * The 'master' represents the composite IO operation to
318 * user-side. So if something waits for IO, then it will
319 * wait for the 'master' bio.
320 */
321 set_bit(R10BIO_Uptodate, &r10_bio->state);
322
323 update_head_pos(slot, r10_bio);
324
325 /*
326 *
327 * Let's see if all mirrored write operations have finished
328 * already.
329 */
330 if (atomic_dec_and_test(&r10_bio->remaining)) {
331 md_write_end(r10_bio->mddev);
332 raid_end_bio_io(r10_bio);
333 }
334
335 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
336 return 0;
337}
338
339
340/*
341 * RAID10 layout manager
342 * Aswell as the chunksize and raid_disks count, there are two
343 * parameters: near_copies and far_copies.
344 * near_copies * far_copies must be <= raid_disks.
345 * Normally one of these will be 1.
346 * If both are 1, we get raid0.
347 * If near_copies == raid_disks, we get raid1.
348 *
349 * Chunks are layed out in raid0 style with near_copies copies of the
350 * first chunk, followed by near_copies copies of the next chunk and
351 * so on.
352 * If far_copies > 1, then after 1/far_copies of the array has been assigned
353 * as described above, we start again with a device offset of near_copies.
354 * So we effectively have another copy of the whole array further down all
355 * the drives, but with blocks on different drives.
356 * With this layout, and block is never stored twice on the one device.
357 *
358 * raid10_find_phys finds the sector offset of a given virtual sector
359 * on each device that it is on. If a block isn't on a device,
360 * that entry in the array is set to MaxSector.
361 *
362 * raid10_find_virt does the reverse mapping, from a device and a
363 * sector offset to a virtual address
364 */
365
366static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
367{
368 int n,f;
369 sector_t sector;
370 sector_t chunk;
371 sector_t stripe;
372 int dev;
373
374 int slot = 0;
375
376 /* now calculate first sector/dev */
377 chunk = r10bio->sector >> conf->chunk_shift;
378 sector = r10bio->sector & conf->chunk_mask;
379
380 chunk *= conf->near_copies;
381 stripe = chunk;
382 dev = sector_div(stripe, conf->raid_disks);
383
384 sector += stripe << conf->chunk_shift;
385
386 /* and calculate all the others */
387 for (n=0; n < conf->near_copies; n++) {
388 int d = dev;
389 sector_t s = sector;
390 r10bio->devs[slot].addr = sector;
391 r10bio->devs[slot].devnum = d;
392 slot++;
393
394 for (f = 1; f < conf->far_copies; f++) {
395 d += conf->near_copies;
396 if (d >= conf->raid_disks)
397 d -= conf->raid_disks;
398 s += conf->stride;
399 r10bio->devs[slot].devnum = d;
400 r10bio->devs[slot].addr = s;
401 slot++;
402 }
403 dev++;
404 if (dev >= conf->raid_disks) {
405 dev = 0;
406 sector += (conf->chunk_mask + 1);
407 }
408 }
409 BUG_ON(slot != conf->copies);
410}
411
412static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
413{
414 sector_t offset, chunk, vchunk;
415
416 while (sector > conf->stride) {
417 sector -= conf->stride;
418 if (dev < conf->near_copies)
419 dev += conf->raid_disks - conf->near_copies;
420 else
421 dev -= conf->near_copies;
422 }
423
424 offset = sector & conf->chunk_mask;
425 chunk = sector >> conf->chunk_shift;
426 vchunk = chunk * conf->raid_disks + dev;
427 sector_div(vchunk, conf->near_copies);
428 return (vchunk << conf->chunk_shift) + offset;
429}
430
431/**
432 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
433 * @q: request queue
434 * @bio: the buffer head that's been built up so far
435 * @biovec: the request that could be merged to it.
436 *
437 * Return amount of bytes we can accept at this offset
438 * If near_copies == raid_disk, there are no striping issues,
439 * but in that case, the function isn't called at all.
440 */
441static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
442 struct bio_vec *bio_vec)
443{
444 mddev_t *mddev = q->queuedata;
445 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
446 int max;
447 unsigned int chunk_sectors = mddev->chunk_size >> 9;
448 unsigned int bio_sectors = bio->bi_size >> 9;
449
450 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
451 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
452 if (max <= bio_vec->bv_len && bio_sectors == 0)
453 return bio_vec->bv_len;
454 else
455 return max;
456}
457
458/*
459 * This routine returns the disk from which the requested read should
460 * be done. There is a per-array 'next expected sequential IO' sector
461 * number - if this matches on the next IO then we use the last disk.
462 * There is also a per-disk 'last know head position' sector that is
463 * maintained from IRQ contexts, both the normal and the resync IO
464 * completion handlers update this position correctly. If there is no
465 * perfect sequential match then we pick the disk whose head is closest.
466 *
467 * If there are 2 mirrors in the same 2 devices, performance degrades
468 * because position is mirror, not device based.
469 *
470 * The rdev for the device selected will have nr_pending incremented.
471 */
472
473/*
474 * FIXME: possibly should rethink readbalancing and do it differently
475 * depending on near_copies / far_copies geometry.
476 */
477static int read_balance(conf_t *conf, r10bio_t *r10_bio)
478{
479 const unsigned long this_sector = r10_bio->sector;
480 int disk, slot, nslot;
481 const int sectors = r10_bio->sectors;
482 sector_t new_distance, current_distance;
d6065f7b 483 mdk_rdev_t *rdev;
1da177e4
LT
484
485 raid10_find_phys(conf, r10_bio);
486 rcu_read_lock();
487 /*
488 * Check if we can balance. We can balance on the whole
489 * device if no resync is going on, or below the resync window.
490 * We take the first readable disk when above the resync window.
491 */
492 if (conf->mddev->recovery_cp < MaxSector
493 && (this_sector + sectors >= conf->next_resync)) {
494 /* make sure that disk is operational */
495 slot = 0;
496 disk = r10_bio->devs[slot].devnum;
497
d6065f7b 498 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
b2d444d7 499 !test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
500 slot++;
501 if (slot == conf->copies) {
502 slot = 0;
503 disk = -1;
504 break;
505 }
506 disk = r10_bio->devs[slot].devnum;
507 }
508 goto rb_out;
509 }
510
511
512 /* make sure the disk is operational */
513 slot = 0;
514 disk = r10_bio->devs[slot].devnum;
d6065f7b 515 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
b2d444d7 516 !test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
517 slot ++;
518 if (slot == conf->copies) {
519 disk = -1;
520 goto rb_out;
521 }
522 disk = r10_bio->devs[slot].devnum;
523 }
524
525
3ec67ac1
N
526 current_distance = abs(r10_bio->devs[slot].addr -
527 conf->mirrors[disk].head_position);
1da177e4
LT
528
529 /* Find the disk whose head is closest */
530
531 for (nslot = slot; nslot < conf->copies; nslot++) {
532 int ndisk = r10_bio->devs[nslot].devnum;
533
534
d6065f7b 535 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
b2d444d7 536 !test_bit(In_sync, &rdev->flags))
1da177e4
LT
537 continue;
538
22dfdf52
N
539 /* This optimisation is debatable, and completely destroys
540 * sequential read speed for 'far copies' arrays. So only
541 * keep it for 'near' arrays, and review those later.
542 */
543 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
1da177e4
LT
544 disk = ndisk;
545 slot = nslot;
546 break;
547 }
548 new_distance = abs(r10_bio->devs[nslot].addr -
549 conf->mirrors[ndisk].head_position);
550 if (new_distance < current_distance) {
551 current_distance = new_distance;
552 disk = ndisk;
553 slot = nslot;
554 }
555 }
556
557rb_out:
558 r10_bio->read_slot = slot;
559/* conf->next_seq_sect = this_sector + sectors;*/
560
d6065f7b 561 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
1da177e4
LT
562 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
563 rcu_read_unlock();
564
565 return disk;
566}
567
568static void unplug_slaves(mddev_t *mddev)
569{
570 conf_t *conf = mddev_to_conf(mddev);
571 int i;
572
573 rcu_read_lock();
574 for (i=0; i<mddev->raid_disks; i++) {
d6065f7b 575 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
b2d444d7 576 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
1da177e4
LT
577 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
578
579 atomic_inc(&rdev->nr_pending);
580 rcu_read_unlock();
581
582 if (r_queue->unplug_fn)
583 r_queue->unplug_fn(r_queue);
584
585 rdev_dec_pending(rdev, mddev);
586 rcu_read_lock();
587 }
588 }
589 rcu_read_unlock();
590}
591
592static void raid10_unplug(request_queue_t *q)
593{
594 unplug_slaves(q->queuedata);
595}
596
597static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
598 sector_t *error_sector)
599{
600 mddev_t *mddev = q->queuedata;
601 conf_t *conf = mddev_to_conf(mddev);
602 int i, ret = 0;
603
604 rcu_read_lock();
605 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
d6065f7b 606 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
b2d444d7 607 if (rdev && !test_bit(Faulty, &rdev->flags)) {
1da177e4
LT
608 struct block_device *bdev = rdev->bdev;
609 request_queue_t *r_queue = bdev_get_queue(bdev);
610
611 if (!r_queue->issue_flush_fn)
612 ret = -EOPNOTSUPP;
613 else {
614 atomic_inc(&rdev->nr_pending);
615 rcu_read_unlock();
616 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
617 error_sector);
618 rdev_dec_pending(rdev, mddev);
619 rcu_read_lock();
620 }
621 }
622 }
623 rcu_read_unlock();
624 return ret;
625}
626
0a27ec96
N
627/* Barriers....
628 * Sometimes we need to suspend IO while we do something else,
629 * either some resync/recovery, or reconfigure the array.
630 * To do this we raise a 'barrier'.
631 * The 'barrier' is a counter that can be raised multiple times
632 * to count how many activities are happening which preclude
633 * normal IO.
634 * We can only raise the barrier if there is no pending IO.
635 * i.e. if nr_pending == 0.
636 * We choose only to raise the barrier if no-one is waiting for the
637 * barrier to go down. This means that as soon as an IO request
638 * is ready, no other operations which require a barrier will start
639 * until the IO request has had a chance.
640 *
641 * So: regular IO calls 'wait_barrier'. When that returns there
642 * is no backgroup IO happening, It must arrange to call
643 * allow_barrier when it has finished its IO.
644 * backgroup IO calls must call raise_barrier. Once that returns
645 * there is no normal IO happeing. It must arrange to call
646 * lower_barrier when the particular background IO completes.
1da177e4
LT
647 */
648#define RESYNC_DEPTH 32
649
0a27ec96 650static void raise_barrier(conf_t *conf)
1da177e4
LT
651{
652 spin_lock_irq(&conf->resync_lock);
0a27ec96
N
653
654 /* Wait until no block IO is waiting */
655 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
656 conf->resync_lock,
657 raid10_unplug(conf->mddev->queue));
658
659 /* block any new IO from starting */
660 conf->barrier++;
661
662 /* No wait for all pending IO to complete */
663 wait_event_lock_irq(conf->wait_barrier,
664 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
665 conf->resync_lock,
666 raid10_unplug(conf->mddev->queue));
667
668 spin_unlock_irq(&conf->resync_lock);
669}
670
671static void lower_barrier(conf_t *conf)
672{
673 unsigned long flags;
674 spin_lock_irqsave(&conf->resync_lock, flags);
675 conf->barrier--;
676 spin_unlock_irqrestore(&conf->resync_lock, flags);
677 wake_up(&conf->wait_barrier);
678}
679
680static void wait_barrier(conf_t *conf)
681{
682 spin_lock_irq(&conf->resync_lock);
683 if (conf->barrier) {
684 conf->nr_waiting++;
685 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
686 conf->resync_lock,
687 raid10_unplug(conf->mddev->queue));
688 conf->nr_waiting--;
1da177e4 689 }
0a27ec96 690 conf->nr_pending++;
1da177e4
LT
691 spin_unlock_irq(&conf->resync_lock);
692}
693
0a27ec96
N
694static void allow_barrier(conf_t *conf)
695{
696 unsigned long flags;
697 spin_lock_irqsave(&conf->resync_lock, flags);
698 conf->nr_pending--;
699 spin_unlock_irqrestore(&conf->resync_lock, flags);
700 wake_up(&conf->wait_barrier);
701}
702
1da177e4
LT
703static int make_request(request_queue_t *q, struct bio * bio)
704{
705 mddev_t *mddev = q->queuedata;
706 conf_t *conf = mddev_to_conf(mddev);
707 mirror_info_t *mirror;
708 r10bio_t *r10_bio;
709 struct bio *read_bio;
710 int i;
711 int chunk_sects = conf->chunk_mask + 1;
a362357b 712 const int rw = bio_data_dir(bio);
1da177e4 713
e5dcdd80
N
714 if (unlikely(bio_barrier(bio))) {
715 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
716 return 0;
717 }
718
1da177e4
LT
719 /* If this request crosses a chunk boundary, we need to
720 * split it. This will only happen for 1 PAGE (or less) requests.
721 */
722 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
723 > chunk_sects &&
724 conf->near_copies < conf->raid_disks)) {
725 struct bio_pair *bp;
726 /* Sanity check -- queue functions should prevent this happening */
727 if (bio->bi_vcnt != 1 ||
728 bio->bi_idx != 0)
729 goto bad_map;
730 /* This is a one page bio that upper layers
731 * refuse to split for us, so we need to split it.
732 */
733 bp = bio_split(bio, bio_split_pool,
734 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
735 if (make_request(q, &bp->bio1))
736 generic_make_request(&bp->bio1);
737 if (make_request(q, &bp->bio2))
738 generic_make_request(&bp->bio2);
739
740 bio_pair_release(bp);
741 return 0;
742 bad_map:
743 printk("raid10_make_request bug: can't convert block across chunks"
744 " or bigger than %dk %llu %d\n", chunk_sects/2,
745 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
746
747 bio_io_error(bio, bio->bi_size);
748 return 0;
749 }
750
3d310eb7 751 md_write_start(mddev, bio);
06d91a5f 752
1da177e4
LT
753 /*
754 * Register the new request and wait if the reconstruction
755 * thread has put up a bar for new requests.
756 * Continue immediately if no resync is active currently.
757 */
0a27ec96 758 wait_barrier(conf);
1da177e4 759
a362357b
JA
760 disk_stat_inc(mddev->gendisk, ios[rw]);
761 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
1da177e4
LT
762
763 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
764
765 r10_bio->master_bio = bio;
766 r10_bio->sectors = bio->bi_size >> 9;
767
768 r10_bio->mddev = mddev;
769 r10_bio->sector = bio->bi_sector;
770
a362357b 771 if (rw == READ) {
1da177e4
LT
772 /*
773 * read balancing logic:
774 */
775 int disk = read_balance(conf, r10_bio);
776 int slot = r10_bio->read_slot;
777 if (disk < 0) {
778 raid_end_bio_io(r10_bio);
779 return 0;
780 }
781 mirror = conf->mirrors + disk;
782
783 read_bio = bio_clone(bio, GFP_NOIO);
784
785 r10_bio->devs[slot].bio = read_bio;
786
787 read_bio->bi_sector = r10_bio->devs[slot].addr +
788 mirror->rdev->data_offset;
789 read_bio->bi_bdev = mirror->rdev->bdev;
790 read_bio->bi_end_io = raid10_end_read_request;
791 read_bio->bi_rw = READ;
792 read_bio->bi_private = r10_bio;
793
794 generic_make_request(read_bio);
795 return 0;
796 }
797
798 /*
799 * WRITE:
800 */
801 /* first select target devices under spinlock and
802 * inc refcount on their rdev. Record them by setting
803 * bios[x] to bio
804 */
805 raid10_find_phys(conf, r10_bio);
806 rcu_read_lock();
807 for (i = 0; i < conf->copies; i++) {
808 int d = r10_bio->devs[i].devnum;
d6065f7b
SW
809 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
810 if (rdev &&
b2d444d7 811 !test_bit(Faulty, &rdev->flags)) {
d6065f7b 812 atomic_inc(&rdev->nr_pending);
1da177e4
LT
813 r10_bio->devs[i].bio = bio;
814 } else
815 r10_bio->devs[i].bio = NULL;
816 }
817 rcu_read_unlock();
818
819 atomic_set(&r10_bio->remaining, 1);
06d91a5f 820
1da177e4
LT
821 for (i = 0; i < conf->copies; i++) {
822 struct bio *mbio;
823 int d = r10_bio->devs[i].devnum;
824 if (!r10_bio->devs[i].bio)
825 continue;
826
827 mbio = bio_clone(bio, GFP_NOIO);
828 r10_bio->devs[i].bio = mbio;
829
830 mbio->bi_sector = r10_bio->devs[i].addr+
831 conf->mirrors[d].rdev->data_offset;
832 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
833 mbio->bi_end_io = raid10_end_write_request;
834 mbio->bi_rw = WRITE;
835 mbio->bi_private = r10_bio;
836
837 atomic_inc(&r10_bio->remaining);
838 generic_make_request(mbio);
839 }
840
841 if (atomic_dec_and_test(&r10_bio->remaining)) {
842 md_write_end(mddev);
843 raid_end_bio_io(r10_bio);
844 }
845
846 return 0;
847}
848
849static void status(struct seq_file *seq, mddev_t *mddev)
850{
851 conf_t *conf = mddev_to_conf(mddev);
852 int i;
853
854 if (conf->near_copies < conf->raid_disks)
855 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
856 if (conf->near_copies > 1)
857 seq_printf(seq, " %d near-copies", conf->near_copies);
858 if (conf->far_copies > 1)
859 seq_printf(seq, " %d far-copies", conf->far_copies);
860
861 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
862 conf->working_disks);
863 for (i = 0; i < conf->raid_disks; i++)
864 seq_printf(seq, "%s",
865 conf->mirrors[i].rdev &&
b2d444d7 866 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1da177e4
LT
867 seq_printf(seq, "]");
868}
869
870static void error(mddev_t *mddev, mdk_rdev_t *rdev)
871{
872 char b[BDEVNAME_SIZE];
873 conf_t *conf = mddev_to_conf(mddev);
874
875 /*
876 * If it is not operational, then we have already marked it as dead
877 * else if it is the last working disks, ignore the error, let the
878 * next level up know.
879 * else mark the drive as failed
880 */
b2d444d7 881 if (test_bit(In_sync, &rdev->flags)
1da177e4
LT
882 && conf->working_disks == 1)
883 /*
884 * Don't fail the drive, just return an IO error.
885 * The test should really be more sophisticated than
886 * "working_disks == 1", but it isn't critical, and
887 * can wait until we do more sophisticated "is the drive
888 * really dead" tests...
889 */
890 return;
b2d444d7 891 if (test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
892 mddev->degraded++;
893 conf->working_disks--;
894 /*
895 * if recovery is running, make sure it aborts.
896 */
897 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
898 }
b2d444d7
N
899 clear_bit(In_sync, &rdev->flags);
900 set_bit(Faulty, &rdev->flags);
1da177e4
LT
901 mddev->sb_dirty = 1;
902 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
903 " Operation continuing on %d devices\n",
904 bdevname(rdev->bdev,b), conf->working_disks);
905}
906
907static void print_conf(conf_t *conf)
908{
909 int i;
910 mirror_info_t *tmp;
911
912 printk("RAID10 conf printout:\n");
913 if (!conf) {
914 printk("(!conf)\n");
915 return;
916 }
917 printk(" --- wd:%d rd:%d\n", conf->working_disks,
918 conf->raid_disks);
919
920 for (i = 0; i < conf->raid_disks; i++) {
921 char b[BDEVNAME_SIZE];
922 tmp = conf->mirrors + i;
923 if (tmp->rdev)
924 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
b2d444d7
N
925 i, !test_bit(In_sync, &tmp->rdev->flags),
926 !test_bit(Faulty, &tmp->rdev->flags),
1da177e4
LT
927 bdevname(tmp->rdev->bdev,b));
928 }
929}
930
931static void close_sync(conf_t *conf)
932{
0a27ec96
N
933 wait_barrier(conf);
934 allow_barrier(conf);
1da177e4
LT
935
936 mempool_destroy(conf->r10buf_pool);
937 conf->r10buf_pool = NULL;
938}
939
6d508242
N
940/* check if there are enough drives for
941 * every block to appear on atleast one
942 */
943static int enough(conf_t *conf)
944{
945 int first = 0;
946
947 do {
948 int n = conf->copies;
949 int cnt = 0;
950 while (n--) {
951 if (conf->mirrors[first].rdev)
952 cnt++;
953 first = (first+1) % conf->raid_disks;
954 }
955 if (cnt == 0)
956 return 0;
957 } while (first != 0);
958 return 1;
959}
960
1da177e4
LT
961static int raid10_spare_active(mddev_t *mddev)
962{
963 int i;
964 conf_t *conf = mddev->private;
965 mirror_info_t *tmp;
966
967 /*
968 * Find all non-in_sync disks within the RAID10 configuration
969 * and mark them in_sync
970 */
971 for (i = 0; i < conf->raid_disks; i++) {
972 tmp = conf->mirrors + i;
973 if (tmp->rdev
b2d444d7
N
974 && !test_bit(Faulty, &tmp->rdev->flags)
975 && !test_bit(In_sync, &tmp->rdev->flags)) {
1da177e4
LT
976 conf->working_disks++;
977 mddev->degraded--;
b2d444d7 978 set_bit(In_sync, &tmp->rdev->flags);
1da177e4
LT
979 }
980 }
981
982 print_conf(conf);
983 return 0;
984}
985
986
987static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
988{
989 conf_t *conf = mddev->private;
990 int found = 0;
991 int mirror;
992 mirror_info_t *p;
993
994 if (mddev->recovery_cp < MaxSector)
995 /* only hot-add to in-sync arrays, as recovery is
996 * very different from resync
997 */
998 return 0;
6d508242
N
999 if (!enough(conf))
1000 return 0;
1da177e4
LT
1001
1002 for (mirror=0; mirror < mddev->raid_disks; mirror++)
1003 if ( !(p=conf->mirrors+mirror)->rdev) {
1004
1005 blk_queue_stack_limits(mddev->queue,
1006 rdev->bdev->bd_disk->queue);
1007 /* as we don't honour merge_bvec_fn, we must never risk
1008 * violating it, so limit ->max_sector to one PAGE, as
1009 * a one page request is never in violation.
1010 */
1011 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1012 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1013 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1014
1015 p->head_position = 0;
1016 rdev->raid_disk = mirror;
1017 found = 1;
d6065f7b 1018 rcu_assign_pointer(p->rdev, rdev);
1da177e4
LT
1019 break;
1020 }
1021
1022 print_conf(conf);
1023 return found;
1024}
1025
1026static int raid10_remove_disk(mddev_t *mddev, int number)
1027{
1028 conf_t *conf = mddev->private;
1029 int err = 0;
1030 mdk_rdev_t *rdev;
1031 mirror_info_t *p = conf->mirrors+ number;
1032
1033 print_conf(conf);
1034 rdev = p->rdev;
1035 if (rdev) {
b2d444d7 1036 if (test_bit(In_sync, &rdev->flags) ||
1da177e4
LT
1037 atomic_read(&rdev->nr_pending)) {
1038 err = -EBUSY;
1039 goto abort;
1040 }
1041 p->rdev = NULL;
fbd568a3 1042 synchronize_rcu();
1da177e4
LT
1043 if (atomic_read(&rdev->nr_pending)) {
1044 /* lost the race, try later */
1045 err = -EBUSY;
1046 p->rdev = rdev;
1047 }
1048 }
1049abort:
1050
1051 print_conf(conf);
1052 return err;
1053}
1054
1055
1056static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1057{
1058 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1059 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1060 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1061 int i,d;
1062
1063 if (bio->bi_size)
1064 return 1;
1065
1066 for (i=0; i<conf->copies; i++)
1067 if (r10_bio->devs[i].bio == bio)
1068 break;
1069 if (i == conf->copies)
1070 BUG();
1071 update_head_pos(i, r10_bio);
1072 d = r10_bio->devs[i].devnum;
1073 if (!uptodate)
1074 md_error(r10_bio->mddev,
1075 conf->mirrors[d].rdev);
1076
1077 /* for reconstruct, we always reschedule after a read.
1078 * for resync, only after all reads
1079 */
1080 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1081 atomic_dec_and_test(&r10_bio->remaining)) {
1082 /* we have read all the blocks,
1083 * do the comparison in process context in raid10d
1084 */
1085 reschedule_retry(r10_bio);
1086 }
1087 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1088 return 0;
1089}
1090
1091static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1092{
1093 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1094 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1095 mddev_t *mddev = r10_bio->mddev;
1096 conf_t *conf = mddev_to_conf(mddev);
1097 int i,d;
1098
1099 if (bio->bi_size)
1100 return 1;
1101
1102 for (i = 0; i < conf->copies; i++)
1103 if (r10_bio->devs[i].bio == bio)
1104 break;
1105 d = r10_bio->devs[i].devnum;
1106
1107 if (!uptodate)
1108 md_error(mddev, conf->mirrors[d].rdev);
1109 update_head_pos(i, r10_bio);
1110
1111 while (atomic_dec_and_test(&r10_bio->remaining)) {
1112 if (r10_bio->master_bio == NULL) {
1113 /* the primary of several recovery bios */
1114 md_done_sync(mddev, r10_bio->sectors, 1);
1115 put_buf(r10_bio);
1116 break;
1117 } else {
1118 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1119 put_buf(r10_bio);
1120 r10_bio = r10_bio2;
1121 }
1122 }
1123 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1124 return 0;
1125}
1126
1127/*
1128 * Note: sync and recover and handled very differently for raid10
1129 * This code is for resync.
1130 * For resync, we read through virtual addresses and read all blocks.
1131 * If there is any error, we schedule a write. The lowest numbered
1132 * drive is authoritative.
1133 * However requests come for physical address, so we need to map.
1134 * For every physical address there are raid_disks/copies virtual addresses,
1135 * which is always are least one, but is not necessarly an integer.
1136 * This means that a physical address can span multiple chunks, so we may
1137 * have to submit multiple io requests for a single sync request.
1138 */
1139/*
1140 * We check if all blocks are in-sync and only write to blocks that
1141 * aren't in sync
1142 */
1143static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1144{
1145 conf_t *conf = mddev_to_conf(mddev);
1146 int i, first;
1147 struct bio *tbio, *fbio;
1148
1149 atomic_set(&r10_bio->remaining, 1);
1150
1151 /* find the first device with a block */
1152 for (i=0; i<conf->copies; i++)
1153 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1154 break;
1155
1156 if (i == conf->copies)
1157 goto done;
1158
1159 first = i;
1160 fbio = r10_bio->devs[i].bio;
1161
1162 /* now find blocks with errors */
1163 for (i=first+1 ; i < conf->copies ; i++) {
1164 int vcnt, j, d;
1165
1166 if (!test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1167 continue;
1168 /* We know that the bi_io_vec layout is the same for
1169 * both 'first' and 'i', so we just compare them.
1170 * All vec entries are PAGE_SIZE;
1171 */
1172 tbio = r10_bio->devs[i].bio;
1173 vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1174 for (j = 0; j < vcnt; j++)
1175 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1176 page_address(tbio->bi_io_vec[j].bv_page),
1177 PAGE_SIZE))
1178 break;
1179 if (j == vcnt)
1180 continue;
1181 /* Ok, we need to write this bio
1182 * First we need to fixup bv_offset, bv_len and
1183 * bi_vecs, as the read request might have corrupted these
1184 */
1185 tbio->bi_vcnt = vcnt;
1186 tbio->bi_size = r10_bio->sectors << 9;
1187 tbio->bi_idx = 0;
1188 tbio->bi_phys_segments = 0;
1189 tbio->bi_hw_segments = 0;
1190 tbio->bi_hw_front_size = 0;
1191 tbio->bi_hw_back_size = 0;
1192 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1193 tbio->bi_flags |= 1 << BIO_UPTODATE;
1194 tbio->bi_next = NULL;
1195 tbio->bi_rw = WRITE;
1196 tbio->bi_private = r10_bio;
1197 tbio->bi_sector = r10_bio->devs[i].addr;
1198
1199 for (j=0; j < vcnt ; j++) {
1200 tbio->bi_io_vec[j].bv_offset = 0;
1201 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1202
1203 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1204 page_address(fbio->bi_io_vec[j].bv_page),
1205 PAGE_SIZE);
1206 }
1207 tbio->bi_end_io = end_sync_write;
1208
1209 d = r10_bio->devs[i].devnum;
1210 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1211 atomic_inc(&r10_bio->remaining);
1212 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1213
1214 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1215 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1216 generic_make_request(tbio);
1217 }
1218
1219done:
1220 if (atomic_dec_and_test(&r10_bio->remaining)) {
1221 md_done_sync(mddev, r10_bio->sectors, 1);
1222 put_buf(r10_bio);
1223 }
1224}
1225
1226/*
1227 * Now for the recovery code.
1228 * Recovery happens across physical sectors.
1229 * We recover all non-is_sync drives by finding the virtual address of
1230 * each, and then choose a working drive that also has that virt address.
1231 * There is a separate r10_bio for each non-in_sync drive.
1232 * Only the first two slots are in use. The first for reading,
1233 * The second for writing.
1234 *
1235 */
1236
1237static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1238{
1239 conf_t *conf = mddev_to_conf(mddev);
1240 int i, d;
1241 struct bio *bio, *wbio;
1242
1243
1244 /* move the pages across to the second bio
1245 * and submit the write request
1246 */
1247 bio = r10_bio->devs[0].bio;
1248 wbio = r10_bio->devs[1].bio;
1249 for (i=0; i < wbio->bi_vcnt; i++) {
1250 struct page *p = bio->bi_io_vec[i].bv_page;
1251 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1252 wbio->bi_io_vec[i].bv_page = p;
1253 }
1254 d = r10_bio->devs[1].devnum;
1255
1256 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1257 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1258 generic_make_request(wbio);
1259}
1260
1261
1262/*
1263 * This is a kernel thread which:
1264 *
1265 * 1. Retries failed read operations on working mirrors.
1266 * 2. Updates the raid superblock when problems encounter.
1267 * 3. Performs writes following reads for array syncronising.
1268 */
1269
1270static void raid10d(mddev_t *mddev)
1271{
1272 r10bio_t *r10_bio;
1273 struct bio *bio;
1274 unsigned long flags;
1275 conf_t *conf = mddev_to_conf(mddev);
1276 struct list_head *head = &conf->retry_list;
1277 int unplug=0;
1278 mdk_rdev_t *rdev;
1279
1280 md_check_recovery(mddev);
1da177e4
LT
1281
1282 for (;;) {
1283 char b[BDEVNAME_SIZE];
1284 spin_lock_irqsave(&conf->device_lock, flags);
1285 if (list_empty(head))
1286 break;
1287 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1288 list_del(head->prev);
1289 spin_unlock_irqrestore(&conf->device_lock, flags);
1290
1291 mddev = r10_bio->mddev;
1292 conf = mddev_to_conf(mddev);
1293 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1294 sync_request_write(mddev, r10_bio);
1295 unplug = 1;
1296 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1297 recovery_request_write(mddev, r10_bio);
1298 unplug = 1;
1299 } else {
1300 int mirror;
1301 bio = r10_bio->devs[r10_bio->read_slot].bio;
1302 r10_bio->devs[r10_bio->read_slot].bio = NULL;
1303 bio_put(bio);
1304 mirror = read_balance(conf, r10_bio);
1305 if (mirror == -1) {
1306 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1307 " read error for block %llu\n",
1308 bdevname(bio->bi_bdev,b),
1309 (unsigned long long)r10_bio->sector);
1310 raid_end_bio_io(r10_bio);
1311 } else {
1312 rdev = conf->mirrors[mirror].rdev;
1313 if (printk_ratelimit())
1314 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1315 " another mirror\n",
1316 bdevname(rdev->bdev,b),
1317 (unsigned long long)r10_bio->sector);
1318 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1319 r10_bio->devs[r10_bio->read_slot].bio = bio;
1320 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1321 + rdev->data_offset;
1322 bio->bi_bdev = rdev->bdev;
1323 bio->bi_rw = READ;
1324 bio->bi_private = r10_bio;
1325 bio->bi_end_io = raid10_end_read_request;
1326 unplug = 1;
1327 generic_make_request(bio);
1328 }
1329 }
1330 }
1331 spin_unlock_irqrestore(&conf->device_lock, flags);
1332 if (unplug)
1333 unplug_slaves(mddev);
1334}
1335
1336
1337static int init_resync(conf_t *conf)
1338{
1339 int buffs;
1340
1341 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1342 if (conf->r10buf_pool)
1343 BUG();
1344 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1345 if (!conf->r10buf_pool)
1346 return -ENOMEM;
1347 conf->next_resync = 0;
1348 return 0;
1349}
1350
1351/*
1352 * perform a "sync" on one "block"
1353 *
1354 * We need to make sure that no normal I/O request - particularly write
1355 * requests - conflict with active sync requests.
1356 *
1357 * This is achieved by tracking pending requests and a 'barrier' concept
1358 * that can be installed to exclude normal IO requests.
1359 *
1360 * Resync and recovery are handled very differently.
1361 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1362 *
1363 * For resync, we iterate over virtual addresses, read all copies,
1364 * and update if there are differences. If only one copy is live,
1365 * skip it.
1366 * For recovery, we iterate over physical addresses, read a good
1367 * value for each non-in_sync drive, and over-write.
1368 *
1369 * So, for recovery we may have several outstanding complex requests for a
1370 * given address, one for each out-of-sync device. We model this by allocating
1371 * a number of r10_bio structures, one for each out-of-sync device.
1372 * As we setup these structures, we collect all bio's together into a list
1373 * which we then process collectively to add pages, and then process again
1374 * to pass to generic_make_request.
1375 *
1376 * The r10_bio structures are linked using a borrowed master_bio pointer.
1377 * This link is counted in ->remaining. When the r10_bio that points to NULL
1378 * has its remaining count decremented to 0, the whole complex operation
1379 * is complete.
1380 *
1381 */
1382
57afd89f 1383static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1da177e4
LT
1384{
1385 conf_t *conf = mddev_to_conf(mddev);
1386 r10bio_t *r10_bio;
1387 struct bio *biolist = NULL, *bio;
1388 sector_t max_sector, nr_sectors;
1389 int disk;
1390 int i;
1391
1392 sector_t sectors_skipped = 0;
1393 int chunks_skipped = 0;
1394
1395 if (!conf->r10buf_pool)
1396 if (init_resync(conf))
57afd89f 1397 return 0;
1da177e4
LT
1398
1399 skipped:
1400 max_sector = mddev->size << 1;
1401 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1402 max_sector = mddev->resync_max_sectors;
1403 if (sector_nr >= max_sector) {
1404 close_sync(conf);
57afd89f 1405 *skipped = 1;
1da177e4
LT
1406 return sectors_skipped;
1407 }
1408 if (chunks_skipped >= conf->raid_disks) {
1409 /* if there has been nothing to do on any drive,
1410 * then there is nothing to do at all..
1411 */
57afd89f
N
1412 *skipped = 1;
1413 return (max_sector - sector_nr) + sectors_skipped;
1da177e4
LT
1414 }
1415
1416 /* make sure whole request will fit in a chunk - if chunks
1417 * are meaningful
1418 */
1419 if (conf->near_copies < conf->raid_disks &&
1420 max_sector > (sector_nr | conf->chunk_mask))
1421 max_sector = (sector_nr | conf->chunk_mask) + 1;
1422 /*
1423 * If there is non-resync activity waiting for us then
1424 * put in a delay to throttle resync.
1425 */
0a27ec96 1426 if (!go_faster && conf->nr_waiting)
1da177e4 1427 msleep_interruptible(1000);
0a27ec96
N
1428 raise_barrier(conf);
1429 conf->next_resync = sector_nr;
1da177e4
LT
1430
1431 /* Again, very different code for resync and recovery.
1432 * Both must result in an r10bio with a list of bios that
1433 * have bi_end_io, bi_sector, bi_bdev set,
1434 * and bi_private set to the r10bio.
1435 * For recovery, we may actually create several r10bios
1436 * with 2 bios in each, that correspond to the bios in the main one.
1437 * In this case, the subordinate r10bios link back through a
1438 * borrowed master_bio pointer, and the counter in the master
1439 * includes a ref from each subordinate.
1440 */
1441 /* First, we decide what to do and set ->bi_end_io
1442 * To end_sync_read if we want to read, and
1443 * end_sync_write if we will want to write.
1444 */
1445
1446 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1447 /* recovery... the complicated one */
1448 int i, j, k;
1449 r10_bio = NULL;
1450
1451 for (i=0 ; i<conf->raid_disks; i++)
1452 if (conf->mirrors[i].rdev &&
b2d444d7 1453 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
1da177e4
LT
1454 /* want to reconstruct this device */
1455 r10bio_t *rb2 = r10_bio;
1456
1457 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1458 spin_lock_irq(&conf->resync_lock);
1da177e4
LT
1459 if (rb2) conf->barrier++;
1460 spin_unlock_irq(&conf->resync_lock);
1461 atomic_set(&r10_bio->remaining, 0);
1462
1463 r10_bio->master_bio = (struct bio*)rb2;
1464 if (rb2)
1465 atomic_inc(&rb2->remaining);
1466 r10_bio->mddev = mddev;
1467 set_bit(R10BIO_IsRecover, &r10_bio->state);
1468 r10_bio->sector = raid10_find_virt(conf, sector_nr, i);
1469 raid10_find_phys(conf, r10_bio);
1470 for (j=0; j<conf->copies;j++) {
1471 int d = r10_bio->devs[j].devnum;
1472 if (conf->mirrors[d].rdev &&
b2d444d7 1473 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1da177e4
LT
1474 /* This is where we read from */
1475 bio = r10_bio->devs[0].bio;
1476 bio->bi_next = biolist;
1477 biolist = bio;
1478 bio->bi_private = r10_bio;
1479 bio->bi_end_io = end_sync_read;
1480 bio->bi_rw = 0;
1481 bio->bi_sector = r10_bio->devs[j].addr +
1482 conf->mirrors[d].rdev->data_offset;
1483 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1484 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1485 atomic_inc(&r10_bio->remaining);
1486 /* and we write to 'i' */
1487
1488 for (k=0; k<conf->copies; k++)
1489 if (r10_bio->devs[k].devnum == i)
1490 break;
1491 bio = r10_bio->devs[1].bio;
1492 bio->bi_next = biolist;
1493 biolist = bio;
1494 bio->bi_private = r10_bio;
1495 bio->bi_end_io = end_sync_write;
1496 bio->bi_rw = 1;
1497 bio->bi_sector = r10_bio->devs[k].addr +
1498 conf->mirrors[i].rdev->data_offset;
1499 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1500
1501 r10_bio->devs[0].devnum = d;
1502 r10_bio->devs[1].devnum = i;
1503
1504 break;
1505 }
1506 }
1507 if (j == conf->copies) {
87fc767b
N
1508 /* Cannot recover, so abort the recovery */
1509 put_buf(r10_bio);
1510 r10_bio = rb2;
1511 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1512 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1513 mdname(mddev));
1514 break;
1da177e4
LT
1515 }
1516 }
1517 if (biolist == NULL) {
1518 while (r10_bio) {
1519 r10bio_t *rb2 = r10_bio;
1520 r10_bio = (r10bio_t*) rb2->master_bio;
1521 rb2->master_bio = NULL;
1522 put_buf(rb2);
1523 }
1524 goto giveup;
1525 }
1526 } else {
1527 /* resync. Schedule a read for every block at this virt offset */
1528 int count = 0;
1529 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1530
1da177e4
LT
1531 r10_bio->mddev = mddev;
1532 atomic_set(&r10_bio->remaining, 0);
1533
1534 r10_bio->master_bio = NULL;
1535 r10_bio->sector = sector_nr;
1536 set_bit(R10BIO_IsSync, &r10_bio->state);
1537 raid10_find_phys(conf, r10_bio);
1538 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1539
1540 for (i=0; i<conf->copies; i++) {
1541 int d = r10_bio->devs[i].devnum;
1542 bio = r10_bio->devs[i].bio;
1543 bio->bi_end_io = NULL;
1544 if (conf->mirrors[d].rdev == NULL ||
b2d444d7 1545 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1da177e4
LT
1546 continue;
1547 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1548 atomic_inc(&r10_bio->remaining);
1549 bio->bi_next = biolist;
1550 biolist = bio;
1551 bio->bi_private = r10_bio;
1552 bio->bi_end_io = end_sync_read;
1553 bio->bi_rw = 0;
1554 bio->bi_sector = r10_bio->devs[i].addr +
1555 conf->mirrors[d].rdev->data_offset;
1556 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1557 count++;
1558 }
1559
1560 if (count < 2) {
1561 for (i=0; i<conf->copies; i++) {
1562 int d = r10_bio->devs[i].devnum;
1563 if (r10_bio->devs[i].bio->bi_end_io)
1564 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1565 }
1566 put_buf(r10_bio);
1567 biolist = NULL;
1568 goto giveup;
1569 }
1570 }
1571
1572 for (bio = biolist; bio ; bio=bio->bi_next) {
1573
1574 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1575 if (bio->bi_end_io)
1576 bio->bi_flags |= 1 << BIO_UPTODATE;
1577 bio->bi_vcnt = 0;
1578 bio->bi_idx = 0;
1579 bio->bi_phys_segments = 0;
1580 bio->bi_hw_segments = 0;
1581 bio->bi_size = 0;
1582 }
1583
1584 nr_sectors = 0;
1585 do {
1586 struct page *page;
1587 int len = PAGE_SIZE;
1588 disk = 0;
1589 if (sector_nr + (len>>9) > max_sector)
1590 len = (max_sector - sector_nr) << 9;
1591 if (len == 0)
1592 break;
1593 for (bio= biolist ; bio ; bio=bio->bi_next) {
1594 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1595 if (bio_add_page(bio, page, len, 0) == 0) {
1596 /* stop here */
1597 struct bio *bio2;
1598 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1599 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1600 /* remove last page from this bio */
1601 bio2->bi_vcnt--;
1602 bio2->bi_size -= len;
1603 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1604 }
1605 goto bio_full;
1606 }
1607 disk = i;
1608 }
1609 nr_sectors += len>>9;
1610 sector_nr += len>>9;
1611 } while (biolist->bi_vcnt < RESYNC_PAGES);
1612 bio_full:
1613 r10_bio->sectors = nr_sectors;
1614
1615 while (biolist) {
1616 bio = biolist;
1617 biolist = biolist->bi_next;
1618
1619 bio->bi_next = NULL;
1620 r10_bio = bio->bi_private;
1621 r10_bio->sectors = nr_sectors;
1622
1623 if (bio->bi_end_io == end_sync_read) {
1624 md_sync_acct(bio->bi_bdev, nr_sectors);
1625 generic_make_request(bio);
1626 }
1627 }
1628
57afd89f
N
1629 if (sectors_skipped)
1630 /* pretend they weren't skipped, it makes
1631 * no important difference in this case
1632 */
1633 md_done_sync(mddev, sectors_skipped, 1);
1634
1da177e4
LT
1635 return sectors_skipped + nr_sectors;
1636 giveup:
1637 /* There is nowhere to write, so all non-sync
1638 * drives must be failed, so try the next chunk...
1639 */
1640 {
57afd89f 1641 sector_t sec = max_sector - sector_nr;
1da177e4
LT
1642 sectors_skipped += sec;
1643 chunks_skipped ++;
1644 sector_nr = max_sector;
1da177e4
LT
1645 goto skipped;
1646 }
1647}
1648
1649static int run(mddev_t *mddev)
1650{
1651 conf_t *conf;
1652 int i, disk_idx;
1653 mirror_info_t *disk;
1654 mdk_rdev_t *rdev;
1655 struct list_head *tmp;
1656 int nc, fc;
1657 sector_t stride, size;
1658
1659 if (mddev->level != 10) {
1660 printk(KERN_ERR "raid10: %s: raid level not set correctly... (%d)\n",
1661 mdname(mddev), mddev->level);
1662 goto out;
1663 }
1664 nc = mddev->layout & 255;
1665 fc = (mddev->layout >> 8) & 255;
1666 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
1667 (mddev->layout >> 16)) {
1668 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1669 mdname(mddev), mddev->layout);
1670 goto out;
1671 }
1672 /*
1673 * copy the already verified devices into our private RAID10
1674 * bookkeeping area. [whatever we allocate in run(),
1675 * should be freed in stop()]
1676 */
1677 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1678 mddev->private = conf;
1679 if (!conf) {
1680 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1681 mdname(mddev));
1682 goto out;
1683 }
1684 memset(conf, 0, sizeof(*conf));
1685 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1686 GFP_KERNEL);
1687 if (!conf->mirrors) {
1688 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1689 mdname(mddev));
1690 goto out_free_conf;
1691 }
1692 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1693
1694 conf->near_copies = nc;
1695 conf->far_copies = fc;
1696 conf->copies = nc*fc;
1697 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
1698 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
1699 stride = mddev->size >> (conf->chunk_shift-1);
1700 sector_div(stride, fc);
1701 conf->stride = stride << conf->chunk_shift;
1702
1703 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
1704 r10bio_pool_free, conf);
1705 if (!conf->r10bio_pool) {
1706 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1707 mdname(mddev));
1708 goto out_free_conf;
1709 }
1da177e4
LT
1710
1711 ITERATE_RDEV(mddev, rdev, tmp) {
1712 disk_idx = rdev->raid_disk;
1713 if (disk_idx >= mddev->raid_disks
1714 || disk_idx < 0)
1715 continue;
1716 disk = conf->mirrors + disk_idx;
1717
1718 disk->rdev = rdev;
1719
1720 blk_queue_stack_limits(mddev->queue,
1721 rdev->bdev->bd_disk->queue);
1722 /* as we don't honour merge_bvec_fn, we must never risk
1723 * violating it, so limit ->max_sector to one PAGE, as
1724 * a one page request is never in violation.
1725 */
1726 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1727 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1728 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1729
1730 disk->head_position = 0;
b2d444d7 1731 if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
1da177e4
LT
1732 conf->working_disks++;
1733 }
1734 conf->raid_disks = mddev->raid_disks;
1735 conf->mddev = mddev;
1736 spin_lock_init(&conf->device_lock);
1737 INIT_LIST_HEAD(&conf->retry_list);
1738
1739 spin_lock_init(&conf->resync_lock);
0a27ec96 1740 init_waitqueue_head(&conf->wait_barrier);
1da177e4 1741
6d508242
N
1742 /* need to check that every block has at least one working mirror */
1743 if (!enough(conf)) {
1744 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
1745 mdname(mddev));
1da177e4
LT
1746 goto out_free_conf;
1747 }
1748
1749 mddev->degraded = 0;
1750 for (i = 0; i < conf->raid_disks; i++) {
1751
1752 disk = conf->mirrors + i;
1753
1754 if (!disk->rdev) {
1755 disk->head_position = 0;
1756 mddev->degraded++;
1757 }
1758 }
1759
1760
1761 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
1762 if (!mddev->thread) {
1763 printk(KERN_ERR
1764 "raid10: couldn't allocate thread for %s\n",
1765 mdname(mddev));
1766 goto out_free_conf;
1767 }
1768
1769 printk(KERN_INFO
1770 "raid10: raid set %s active with %d out of %d devices\n",
1771 mdname(mddev), mddev->raid_disks - mddev->degraded,
1772 mddev->raid_disks);
1773 /*
1774 * Ok, everything is just fine now
1775 */
1776 size = conf->stride * conf->raid_disks;
1777 sector_div(size, conf->near_copies);
1778 mddev->array_size = size/2;
1779 mddev->resync_max_sectors = size;
1780
7a5febe9
N
1781 mddev->queue->unplug_fn = raid10_unplug;
1782 mddev->queue->issue_flush_fn = raid10_issue_flush;
1783
1da177e4
LT
1784 /* Calculate max read-ahead size.
1785 * We need to readahead at least twice a whole stripe....
1786 * maybe...
1787 */
1788 {
1789 int stripe = conf->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
1790 stripe /= conf->near_copies;
1791 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
1792 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
1793 }
1794
1795 if (conf->near_copies < mddev->raid_disks)
1796 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
1797 return 0;
1798
1799out_free_conf:
1800 if (conf->r10bio_pool)
1801 mempool_destroy(conf->r10bio_pool);
990a8baf 1802 kfree(conf->mirrors);
1da177e4
LT
1803 kfree(conf);
1804 mddev->private = NULL;
1805out:
1806 return -EIO;
1807}
1808
1809static int stop(mddev_t *mddev)
1810{
1811 conf_t *conf = mddev_to_conf(mddev);
1812
1813 md_unregister_thread(mddev->thread);
1814 mddev->thread = NULL;
1815 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1816 if (conf->r10bio_pool)
1817 mempool_destroy(conf->r10bio_pool);
990a8baf 1818 kfree(conf->mirrors);
1da177e4
LT
1819 kfree(conf);
1820 mddev->private = NULL;
1821 return 0;
1822}
1823
1824
1825static mdk_personality_t raid10_personality =
1826{
1827 .name = "raid10",
1828 .owner = THIS_MODULE,
1829 .make_request = make_request,
1830 .run = run,
1831 .stop = stop,
1832 .status = status,
1833 .error_handler = error,
1834 .hot_add_disk = raid10_add_disk,
1835 .hot_remove_disk= raid10_remove_disk,
1836 .spare_active = raid10_spare_active,
1837 .sync_request = sync_request,
1838};
1839
1840static int __init raid_init(void)
1841{
1842 return register_md_personality(RAID10, &raid10_personality);
1843}
1844
1845static void raid_exit(void)
1846{
1847 unregister_md_personality(RAID10);
1848}
1849
1850module_init(raid_init);
1851module_exit(raid_exit);
1852MODULE_LICENSE("GPL");
1853MODULE_ALIAS("md-personality-9"); /* RAID10 */