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 "dm-bio-list.h"
22 #include <linux/raid/raid10.h>
23 #include <linux/raid/bitmap.h>
24
25 /*
26 * RAID10 provides a combination of RAID0 and RAID1 functionality.
27 * The layout of data is defined by
28 * chunk_size
29 * raid_disks
30 * near_copies (stored in low byte of layout)
31 * far_copies (stored in second byte of layout)
32 *
33 * The data to be stored is divided into chunks using chunksize.
34 * Each device is divided into far_copies sections.
35 * In each section, chunks are laid out in a style similar to raid0, but
36 * near_copies copies of each chunk is stored (each on a different drive).
37 * The starting device for each section is offset near_copies from the starting
38 * device of the previous section.
39 * Thus there are (near_copies*far_copies) of each chunk, and each is on a different
40 * drive.
41 * near_copies and far_copies must be at least one, and their product is at most
42 * raid_disks.
43 */
44
45 /*
46 * Number of guaranteed r10bios in case of extreme VM load:
47 */
48 #define NR_RAID10_BIOS 256
49
50 static void unplug_slaves(mddev_t *mddev);
51
52 static void allow_barrier(conf_t *conf);
53 static void lower_barrier(conf_t *conf);
54
55 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
56 {
57 conf_t *conf = data;
58 r10bio_t *r10_bio;
59 int size = offsetof(struct r10bio_s, devs[conf->copies]);
60
61 /* allocate a r10bio with room for raid_disks entries in the bios array */
62 r10_bio = kzalloc(size, gfp_flags);
63 if (!r10_bio)
64 unplug_slaves(conf->mddev);
65
66 return r10_bio;
67 }
68
69 static 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 */
87 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
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
133 out_free_pages:
134 for ( ; i > 0 ; i--)
135 safe_put_page(bio->bi_io_vec[i-1].bv_page);
136 while (j--)
137 for (i = 0; i < RESYNC_PAGES ; i++)
138 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
139 j = -1;
140 out_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
147 static 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 safe_put_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
167 static 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 && *bio != IO_BLOCKED)
174 bio_put(*bio);
175 *bio = NULL;
176 }
177 }
178
179 static void free_r10bio(r10bio_t *r10_bio)
180 {
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 */
187 allow_barrier(conf);
188
189 put_all_bios(conf, r10_bio);
190 mempool_free(r10_bio, conf->r10bio_pool);
191 }
192
193 static void put_buf(r10bio_t *r10_bio)
194 {
195 conf_t *conf = mddev_to_conf(r10_bio->mddev);
196
197 mempool_free(r10_bio, conf->r10buf_pool);
198
199 lower_barrier(conf);
200 }
201
202 static 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 conf->nr_queued ++;
211 spin_unlock_irqrestore(&conf->device_lock, flags);
212
213 md_wakeup_thread(mddev->thread);
214 }
215
216 /*
217 * raid_end_bio_io() is called when we have finished servicing a mirrored
218 * operation and are ready to return a success/failure code to the buffer
219 * cache layer.
220 */
221 static void raid_end_bio_io(r10bio_t *r10_bio)
222 {
223 struct bio *bio = r10_bio->master_bio;
224
225 bio_endio(bio, bio->bi_size,
226 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
227 free_r10bio(r10_bio);
228 }
229
230 /*
231 * Update disk head position estimator based on IRQ completion info.
232 */
233 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
234 {
235 conf_t *conf = mddev_to_conf(r10_bio->mddev);
236
237 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
238 r10_bio->devs[slot].addr + (r10_bio->sectors);
239 }
240
241 static int raid10_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
242 {
243 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
244 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
245 int slot, dev;
246 conf_t *conf = mddev_to_conf(r10_bio->mddev);
247
248 if (bio->bi_size)
249 return 1;
250
251 slot = r10_bio->read_slot;
252 dev = r10_bio->devs[slot].devnum;
253 /*
254 * this branch is our 'one mirror IO has finished' event handler:
255 */
256 update_head_pos(slot, r10_bio);
257
258 if (uptodate) {
259 /*
260 * Set R10BIO_Uptodate in our master bio, so that
261 * we will return a good error code to the higher
262 * levels even if IO on some other mirrored buffer fails.
263 *
264 * The 'master' represents the composite IO operation to
265 * user-side. So if something waits for IO, then it will
266 * wait for the 'master' bio.
267 */
268 set_bit(R10BIO_Uptodate, &r10_bio->state);
269 raid_end_bio_io(r10_bio);
270 } else {
271 /*
272 * oops, read error:
273 */
274 char b[BDEVNAME_SIZE];
275 if (printk_ratelimit())
276 printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
277 bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
278 reschedule_retry(r10_bio);
279 }
280
281 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
282 return 0;
283 }
284
285 static int raid10_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
286 {
287 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
288 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
289 int slot, dev;
290 conf_t *conf = mddev_to_conf(r10_bio->mddev);
291
292 if (bio->bi_size)
293 return 1;
294
295 for (slot = 0; slot < conf->copies; slot++)
296 if (r10_bio->devs[slot].bio == bio)
297 break;
298 dev = r10_bio->devs[slot].devnum;
299
300 /*
301 * this branch is our 'one mirror IO has finished' event handler:
302 */
303 if (!uptodate) {
304 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
305 /* an I/O failed, we can't clear the bitmap */
306 set_bit(R10BIO_Degraded, &r10_bio->state);
307 } else
308 /*
309 * Set R10BIO_Uptodate in our master bio, so that
310 * we will return a good error code for to the higher
311 * levels even if IO on some other mirrored buffer fails.
312 *
313 * The 'master' represents the composite IO operation to
314 * user-side. So if something waits for IO, then it will
315 * wait for the 'master' bio.
316 */
317 set_bit(R10BIO_Uptodate, &r10_bio->state);
318
319 update_head_pos(slot, r10_bio);
320
321 /*
322 *
323 * Let's see if all mirrored write operations have finished
324 * already.
325 */
326 if (atomic_dec_and_test(&r10_bio->remaining)) {
327 /* clear the bitmap if all writes complete successfully */
328 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
329 r10_bio->sectors,
330 !test_bit(R10BIO_Degraded, &r10_bio->state),
331 0);
332 md_write_end(r10_bio->mddev);
333 raid_end_bio_io(r10_bio);
334 }
335
336 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
337 return 0;
338 }
339
340
341 /*
342 * RAID10 layout manager
343 * Aswell as the chunksize and raid_disks count, there are two
344 * parameters: near_copies and far_copies.
345 * near_copies * far_copies must be <= raid_disks.
346 * Normally one of these will be 1.
347 * If both are 1, we get raid0.
348 * If near_copies == raid_disks, we get raid1.
349 *
350 * Chunks are layed out in raid0 style with near_copies copies of the
351 * first chunk, followed by near_copies copies of the next chunk and
352 * so on.
353 * If far_copies > 1, then after 1/far_copies of the array has been assigned
354 * as described above, we start again with a device offset of near_copies.
355 * So we effectively have another copy of the whole array further down all
356 * the drives, but with blocks on different drives.
357 * With this layout, and block is never stored twice on the one device.
358 *
359 * raid10_find_phys finds the sector offset of a given virtual sector
360 * on each device that it is on. If a block isn't on a device,
361 * that entry in the array is set to MaxSector.
362 *
363 * raid10_find_virt does the reverse mapping, from a device and a
364 * sector offset to a virtual address
365 */
366
367 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
368 {
369 int n,f;
370 sector_t sector;
371 sector_t chunk;
372 sector_t stripe;
373 int dev;
374
375 int slot = 0;
376
377 /* now calculate first sector/dev */
378 chunk = r10bio->sector >> conf->chunk_shift;
379 sector = r10bio->sector & conf->chunk_mask;
380
381 chunk *= conf->near_copies;
382 stripe = chunk;
383 dev = sector_div(stripe, conf->raid_disks);
384
385 sector += stripe << conf->chunk_shift;
386
387 /* and calculate all the others */
388 for (n=0; n < conf->near_copies; n++) {
389 int d = dev;
390 sector_t s = sector;
391 r10bio->devs[slot].addr = sector;
392 r10bio->devs[slot].devnum = d;
393 slot++;
394
395 for (f = 1; f < conf->far_copies; f++) {
396 d += conf->near_copies;
397 if (d >= conf->raid_disks)
398 d -= conf->raid_disks;
399 s += conf->stride;
400 r10bio->devs[slot].devnum = d;
401 r10bio->devs[slot].addr = s;
402 slot++;
403 }
404 dev++;
405 if (dev >= conf->raid_disks) {
406 dev = 0;
407 sector += (conf->chunk_mask + 1);
408 }
409 }
410 BUG_ON(slot != conf->copies);
411 }
412
413 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
414 {
415 sector_t offset, chunk, vchunk;
416
417 while (sector > conf->stride) {
418 sector -= conf->stride;
419 if (dev < conf->near_copies)
420 dev += conf->raid_disks - conf->near_copies;
421 else
422 dev -= conf->near_copies;
423 }
424
425 offset = sector & conf->chunk_mask;
426 chunk = sector >> conf->chunk_shift;
427 vchunk = chunk * conf->raid_disks + dev;
428 sector_div(vchunk, conf->near_copies);
429 return (vchunk << conf->chunk_shift) + offset;
430 }
431
432 /**
433 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
434 * @q: request queue
435 * @bio: the buffer head that's been built up so far
436 * @biovec: the request that could be merged to it.
437 *
438 * Return amount of bytes we can accept at this offset
439 * If near_copies == raid_disk, there are no striping issues,
440 * but in that case, the function isn't called at all.
441 */
442 static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
443 struct bio_vec *bio_vec)
444 {
445 mddev_t *mddev = q->queuedata;
446 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
447 int max;
448 unsigned int chunk_sectors = mddev->chunk_size >> 9;
449 unsigned int bio_sectors = bio->bi_size >> 9;
450
451 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
452 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
453 if (max <= bio_vec->bv_len && bio_sectors == 0)
454 return bio_vec->bv_len;
455 else
456 return max;
457 }
458
459 /*
460 * This routine returns the disk from which the requested read should
461 * be done. There is a per-array 'next expected sequential IO' sector
462 * number - if this matches on the next IO then we use the last disk.
463 * There is also a per-disk 'last know head position' sector that is
464 * maintained from IRQ contexts, both the normal and the resync IO
465 * completion handlers update this position correctly. If there is no
466 * perfect sequential match then we pick the disk whose head is closest.
467 *
468 * If there are 2 mirrors in the same 2 devices, performance degrades
469 * because position is mirror, not device based.
470 *
471 * The rdev for the device selected will have nr_pending incremented.
472 */
473
474 /*
475 * FIXME: possibly should rethink readbalancing and do it differently
476 * depending on near_copies / far_copies geometry.
477 */
478 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
479 {
480 const unsigned long this_sector = r10_bio->sector;
481 int disk, slot, nslot;
482 const int sectors = r10_bio->sectors;
483 sector_t new_distance, current_distance;
484 mdk_rdev_t *rdev;
485
486 raid10_find_phys(conf, r10_bio);
487 rcu_read_lock();
488 /*
489 * Check if we can balance. We can balance on the whole
490 * device if no resync is going on (recovery is ok), or below
491 * the resync window. We take the first readable disk when
492 * above the resync window.
493 */
494 if (conf->mddev->recovery_cp < MaxSector
495 && (this_sector + sectors >= conf->next_resync)) {
496 /* make sure that disk is operational */
497 slot = 0;
498 disk = r10_bio->devs[slot].devnum;
499
500 while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
501 r10_bio->devs[slot].bio == IO_BLOCKED ||
502 !test_bit(In_sync, &rdev->flags)) {
503 slot++;
504 if (slot == conf->copies) {
505 slot = 0;
506 disk = -1;
507 break;
508 }
509 disk = r10_bio->devs[slot].devnum;
510 }
511 goto rb_out;
512 }
513
514
515 /* make sure the disk is operational */
516 slot = 0;
517 disk = r10_bio->devs[slot].devnum;
518 while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
519 r10_bio->devs[slot].bio == IO_BLOCKED ||
520 !test_bit(In_sync, &rdev->flags)) {
521 slot ++;
522 if (slot == conf->copies) {
523 disk = -1;
524 goto rb_out;
525 }
526 disk = r10_bio->devs[slot].devnum;
527 }
528
529
530 current_distance = abs(r10_bio->devs[slot].addr -
531 conf->mirrors[disk].head_position);
532
533 /* Find the disk whose head is closest */
534
535 for (nslot = slot; nslot < conf->copies; nslot++) {
536 int ndisk = r10_bio->devs[nslot].devnum;
537
538
539 if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
540 r10_bio->devs[nslot].bio == IO_BLOCKED ||
541 !test_bit(In_sync, &rdev->flags))
542 continue;
543
544 /* This optimisation is debatable, and completely destroys
545 * sequential read speed for 'far copies' arrays. So only
546 * keep it for 'near' arrays, and review those later.
547 */
548 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
549 disk = ndisk;
550 slot = nslot;
551 break;
552 }
553 new_distance = abs(r10_bio->devs[nslot].addr -
554 conf->mirrors[ndisk].head_position);
555 if (new_distance < current_distance) {
556 current_distance = new_distance;
557 disk = ndisk;
558 slot = nslot;
559 }
560 }
561
562 rb_out:
563 r10_bio->read_slot = slot;
564 /* conf->next_seq_sect = this_sector + sectors;*/
565
566 if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
567 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
568 else
569 disk = -1;
570 rcu_read_unlock();
571
572 return disk;
573 }
574
575 static void unplug_slaves(mddev_t *mddev)
576 {
577 conf_t *conf = mddev_to_conf(mddev);
578 int i;
579
580 rcu_read_lock();
581 for (i=0; i<mddev->raid_disks; i++) {
582 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
583 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
584 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
585
586 atomic_inc(&rdev->nr_pending);
587 rcu_read_unlock();
588
589 if (r_queue->unplug_fn)
590 r_queue->unplug_fn(r_queue);
591
592 rdev_dec_pending(rdev, mddev);
593 rcu_read_lock();
594 }
595 }
596 rcu_read_unlock();
597 }
598
599 static void raid10_unplug(request_queue_t *q)
600 {
601 mddev_t *mddev = q->queuedata;
602
603 unplug_slaves(q->queuedata);
604 md_wakeup_thread(mddev->thread);
605 }
606
607 static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
608 sector_t *error_sector)
609 {
610 mddev_t *mddev = q->queuedata;
611 conf_t *conf = mddev_to_conf(mddev);
612 int i, ret = 0;
613
614 rcu_read_lock();
615 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
616 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
617 if (rdev && !test_bit(Faulty, &rdev->flags)) {
618 struct block_device *bdev = rdev->bdev;
619 request_queue_t *r_queue = bdev_get_queue(bdev);
620
621 if (!r_queue->issue_flush_fn)
622 ret = -EOPNOTSUPP;
623 else {
624 atomic_inc(&rdev->nr_pending);
625 rcu_read_unlock();
626 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
627 error_sector);
628 rdev_dec_pending(rdev, mddev);
629 rcu_read_lock();
630 }
631 }
632 }
633 rcu_read_unlock();
634 return ret;
635 }
636
637 /* Barriers....
638 * Sometimes we need to suspend IO while we do something else,
639 * either some resync/recovery, or reconfigure the array.
640 * To do this we raise a 'barrier'.
641 * The 'barrier' is a counter that can be raised multiple times
642 * to count how many activities are happening which preclude
643 * normal IO.
644 * We can only raise the barrier if there is no pending IO.
645 * i.e. if nr_pending == 0.
646 * We choose only to raise the barrier if no-one is waiting for the
647 * barrier to go down. This means that as soon as an IO request
648 * is ready, no other operations which require a barrier will start
649 * until the IO request has had a chance.
650 *
651 * So: regular IO calls 'wait_barrier'. When that returns there
652 * is no backgroup IO happening, It must arrange to call
653 * allow_barrier when it has finished its IO.
654 * backgroup IO calls must call raise_barrier. Once that returns
655 * there is no normal IO happeing. It must arrange to call
656 * lower_barrier when the particular background IO completes.
657 */
658 #define RESYNC_DEPTH 32
659
660 static void raise_barrier(conf_t *conf, int force)
661 {
662 BUG_ON(force && !conf->barrier);
663 spin_lock_irq(&conf->resync_lock);
664
665 /* Wait until no block IO is waiting (unless 'force') */
666 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
667 conf->resync_lock,
668 raid10_unplug(conf->mddev->queue));
669
670 /* block any new IO from starting */
671 conf->barrier++;
672
673 /* No wait for all pending IO to complete */
674 wait_event_lock_irq(conf->wait_barrier,
675 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
676 conf->resync_lock,
677 raid10_unplug(conf->mddev->queue));
678
679 spin_unlock_irq(&conf->resync_lock);
680 }
681
682 static void lower_barrier(conf_t *conf)
683 {
684 unsigned long flags;
685 spin_lock_irqsave(&conf->resync_lock, flags);
686 conf->barrier--;
687 spin_unlock_irqrestore(&conf->resync_lock, flags);
688 wake_up(&conf->wait_barrier);
689 }
690
691 static void wait_barrier(conf_t *conf)
692 {
693 spin_lock_irq(&conf->resync_lock);
694 if (conf->barrier) {
695 conf->nr_waiting++;
696 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
697 conf->resync_lock,
698 raid10_unplug(conf->mddev->queue));
699 conf->nr_waiting--;
700 }
701 conf->nr_pending++;
702 spin_unlock_irq(&conf->resync_lock);
703 }
704
705 static void allow_barrier(conf_t *conf)
706 {
707 unsigned long flags;
708 spin_lock_irqsave(&conf->resync_lock, flags);
709 conf->nr_pending--;
710 spin_unlock_irqrestore(&conf->resync_lock, flags);
711 wake_up(&conf->wait_barrier);
712 }
713
714 static void freeze_array(conf_t *conf)
715 {
716 /* stop syncio and normal IO and wait for everything to
717 * go quiet.
718 * We increment barrier and nr_waiting, and then
719 * wait until barrier+nr_pending match nr_queued+2
720 */
721 spin_lock_irq(&conf->resync_lock);
722 conf->barrier++;
723 conf->nr_waiting++;
724 wait_event_lock_irq(conf->wait_barrier,
725 conf->barrier+conf->nr_pending == conf->nr_queued+2,
726 conf->resync_lock,
727 raid10_unplug(conf->mddev->queue));
728 spin_unlock_irq(&conf->resync_lock);
729 }
730
731 static void unfreeze_array(conf_t *conf)
732 {
733 /* reverse the effect of the freeze */
734 spin_lock_irq(&conf->resync_lock);
735 conf->barrier--;
736 conf->nr_waiting--;
737 wake_up(&conf->wait_barrier);
738 spin_unlock_irq(&conf->resync_lock);
739 }
740
741 static int make_request(request_queue_t *q, struct bio * bio)
742 {
743 mddev_t *mddev = q->queuedata;
744 conf_t *conf = mddev_to_conf(mddev);
745 mirror_info_t *mirror;
746 r10bio_t *r10_bio;
747 struct bio *read_bio;
748 int i;
749 int chunk_sects = conf->chunk_mask + 1;
750 const int rw = bio_data_dir(bio);
751 struct bio_list bl;
752 unsigned long flags;
753
754 if (unlikely(bio_barrier(bio))) {
755 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
756 return 0;
757 }
758
759 /* If this request crosses a chunk boundary, we need to
760 * split it. This will only happen for 1 PAGE (or less) requests.
761 */
762 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
763 > chunk_sects &&
764 conf->near_copies < conf->raid_disks)) {
765 struct bio_pair *bp;
766 /* Sanity check -- queue functions should prevent this happening */
767 if (bio->bi_vcnt != 1 ||
768 bio->bi_idx != 0)
769 goto bad_map;
770 /* This is a one page bio that upper layers
771 * refuse to split for us, so we need to split it.
772 */
773 bp = bio_split(bio, bio_split_pool,
774 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
775 if (make_request(q, &bp->bio1))
776 generic_make_request(&bp->bio1);
777 if (make_request(q, &bp->bio2))
778 generic_make_request(&bp->bio2);
779
780 bio_pair_release(bp);
781 return 0;
782 bad_map:
783 printk("raid10_make_request bug: can't convert block across chunks"
784 " or bigger than %dk %llu %d\n", chunk_sects/2,
785 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
786
787 bio_io_error(bio, bio->bi_size);
788 return 0;
789 }
790
791 md_write_start(mddev, bio);
792
793 /*
794 * Register the new request and wait if the reconstruction
795 * thread has put up a bar for new requests.
796 * Continue immediately if no resync is active currently.
797 */
798 wait_barrier(conf);
799
800 disk_stat_inc(mddev->gendisk, ios[rw]);
801 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
802
803 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
804
805 r10_bio->master_bio = bio;
806 r10_bio->sectors = bio->bi_size >> 9;
807
808 r10_bio->mddev = mddev;
809 r10_bio->sector = bio->bi_sector;
810 r10_bio->state = 0;
811
812 if (rw == READ) {
813 /*
814 * read balancing logic:
815 */
816 int disk = read_balance(conf, r10_bio);
817 int slot = r10_bio->read_slot;
818 if (disk < 0) {
819 raid_end_bio_io(r10_bio);
820 return 0;
821 }
822 mirror = conf->mirrors + disk;
823
824 read_bio = bio_clone(bio, GFP_NOIO);
825
826 r10_bio->devs[slot].bio = read_bio;
827
828 read_bio->bi_sector = r10_bio->devs[slot].addr +
829 mirror->rdev->data_offset;
830 read_bio->bi_bdev = mirror->rdev->bdev;
831 read_bio->bi_end_io = raid10_end_read_request;
832 read_bio->bi_rw = READ;
833 read_bio->bi_private = r10_bio;
834
835 generic_make_request(read_bio);
836 return 0;
837 }
838
839 /*
840 * WRITE:
841 */
842 /* first select target devices under spinlock and
843 * inc refcount on their rdev. Record them by setting
844 * bios[x] to bio
845 */
846 raid10_find_phys(conf, r10_bio);
847 rcu_read_lock();
848 for (i = 0; i < conf->copies; i++) {
849 int d = r10_bio->devs[i].devnum;
850 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
851 if (rdev &&
852 !test_bit(Faulty, &rdev->flags)) {
853 atomic_inc(&rdev->nr_pending);
854 r10_bio->devs[i].bio = bio;
855 } else {
856 r10_bio->devs[i].bio = NULL;
857 set_bit(R10BIO_Degraded, &r10_bio->state);
858 }
859 }
860 rcu_read_unlock();
861
862 atomic_set(&r10_bio->remaining, 0);
863
864 bio_list_init(&bl);
865 for (i = 0; i < conf->copies; i++) {
866 struct bio *mbio;
867 int d = r10_bio->devs[i].devnum;
868 if (!r10_bio->devs[i].bio)
869 continue;
870
871 mbio = bio_clone(bio, GFP_NOIO);
872 r10_bio->devs[i].bio = mbio;
873
874 mbio->bi_sector = r10_bio->devs[i].addr+
875 conf->mirrors[d].rdev->data_offset;
876 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
877 mbio->bi_end_io = raid10_end_write_request;
878 mbio->bi_rw = WRITE;
879 mbio->bi_private = r10_bio;
880
881 atomic_inc(&r10_bio->remaining);
882 bio_list_add(&bl, mbio);
883 }
884
885 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
886 spin_lock_irqsave(&conf->device_lock, flags);
887 bio_list_merge(&conf->pending_bio_list, &bl);
888 blk_plug_device(mddev->queue);
889 spin_unlock_irqrestore(&conf->device_lock, flags);
890
891 return 0;
892 }
893
894 static void status(struct seq_file *seq, mddev_t *mddev)
895 {
896 conf_t *conf = mddev_to_conf(mddev);
897 int i;
898
899 if (conf->near_copies < conf->raid_disks)
900 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
901 if (conf->near_copies > 1)
902 seq_printf(seq, " %d near-copies", conf->near_copies);
903 if (conf->far_copies > 1)
904 seq_printf(seq, " %d far-copies", conf->far_copies);
905
906 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
907 conf->working_disks);
908 for (i = 0; i < conf->raid_disks; i++)
909 seq_printf(seq, "%s",
910 conf->mirrors[i].rdev &&
911 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
912 seq_printf(seq, "]");
913 }
914
915 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
916 {
917 char b[BDEVNAME_SIZE];
918 conf_t *conf = mddev_to_conf(mddev);
919
920 /*
921 * If it is not operational, then we have already marked it as dead
922 * else if it is the last working disks, ignore the error, let the
923 * next level up know.
924 * else mark the drive as failed
925 */
926 if (test_bit(In_sync, &rdev->flags)
927 && conf->working_disks == 1)
928 /*
929 * Don't fail the drive, just return an IO error.
930 * The test should really be more sophisticated than
931 * "working_disks == 1", but it isn't critical, and
932 * can wait until we do more sophisticated "is the drive
933 * really dead" tests...
934 */
935 return;
936 if (test_bit(In_sync, &rdev->flags)) {
937 mddev->degraded++;
938 conf->working_disks--;
939 /*
940 * if recovery is running, make sure it aborts.
941 */
942 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
943 }
944 clear_bit(In_sync, &rdev->flags);
945 set_bit(Faulty, &rdev->flags);
946 mddev->sb_dirty = 1;
947 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
948 " Operation continuing on %d devices\n",
949 bdevname(rdev->bdev,b), conf->working_disks);
950 }
951
952 static void print_conf(conf_t *conf)
953 {
954 int i;
955 mirror_info_t *tmp;
956
957 printk("RAID10 conf printout:\n");
958 if (!conf) {
959 printk("(!conf)\n");
960 return;
961 }
962 printk(" --- wd:%d rd:%d\n", conf->working_disks,
963 conf->raid_disks);
964
965 for (i = 0; i < conf->raid_disks; i++) {
966 char b[BDEVNAME_SIZE];
967 tmp = conf->mirrors + i;
968 if (tmp->rdev)
969 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
970 i, !test_bit(In_sync, &tmp->rdev->flags),
971 !test_bit(Faulty, &tmp->rdev->flags),
972 bdevname(tmp->rdev->bdev,b));
973 }
974 }
975
976 static void close_sync(conf_t *conf)
977 {
978 wait_barrier(conf);
979 allow_barrier(conf);
980
981 mempool_destroy(conf->r10buf_pool);
982 conf->r10buf_pool = NULL;
983 }
984
985 /* check if there are enough drives for
986 * every block to appear on atleast one
987 */
988 static int enough(conf_t *conf)
989 {
990 int first = 0;
991
992 do {
993 int n = conf->copies;
994 int cnt = 0;
995 while (n--) {
996 if (conf->mirrors[