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