md/raid10: Make use of new recovery_disabled handling
[deliverable/linux.git] / drivers / md / raid10.c
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 further 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/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/seq_file.h>
25 #include <linux/ratelimit.h>
26 #include "md.h"
27 #include "raid10.h"
28 #include "raid0.h"
29 #include "bitmap.h"
30
31 /*
32 * RAID10 provides a combination of RAID0 and RAID1 functionality.
33 * The layout of data is defined by
34 * chunk_size
35 * raid_disks
36 * near_copies (stored in low byte of layout)
37 * far_copies (stored in second byte of layout)
38 * far_offset (stored in bit 16 of layout )
39 *
40 * The data to be stored is divided into chunks using chunksize.
41 * Each device is divided into far_copies sections.
42 * In each section, chunks are laid out in a style similar to raid0, but
43 * near_copies copies of each chunk is stored (each on a different drive).
44 * The starting device for each section is offset near_copies from the starting
45 * device of the previous section.
46 * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
47 * drive.
48 * near_copies and far_copies must be at least one, and their product is at most
49 * raid_disks.
50 *
51 * If far_offset is true, then the far_copies are handled a bit differently.
52 * The copies are still in different stripes, but instead of be very far apart
53 * on disk, there are adjacent stripes.
54 */
55
56 /*
57 * Number of guaranteed r10bios in case of extreme VM load:
58 */
59 #define NR_RAID10_BIOS 256
60
61 static void allow_barrier(conf_t *conf);
62 static void lower_barrier(conf_t *conf);
63
64 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
65 {
66 conf_t *conf = data;
67 int size = offsetof(struct r10bio_s, devs[conf->copies]);
68
69 /* allocate a r10bio with room for raid_disks entries in the bios array */
70 return kzalloc(size, gfp_flags);
71 }
72
73 static void r10bio_pool_free(void *r10_bio, void *data)
74 {
75 kfree(r10_bio);
76 }
77
78 /* Maximum size of each resync request */
79 #define RESYNC_BLOCK_SIZE (64*1024)
80 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
81 /* amount of memory to reserve for resync requests */
82 #define RESYNC_WINDOW (1024*1024)
83 /* maximum number of concurrent requests, memory permitting */
84 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
85
86 /*
87 * When performing a resync, we need to read and compare, so
88 * we need as many pages are there are copies.
89 * When performing a recovery, we need 2 bios, one for read,
90 * one for write (we recover only one drive per r10buf)
91 *
92 */
93 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
94 {
95 conf_t *conf = data;
96 struct page *page;
97 r10bio_t *r10_bio;
98 struct bio *bio;
99 int i, j;
100 int nalloc;
101
102 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
103 if (!r10_bio)
104 return NULL;
105
106 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
107 nalloc = conf->copies; /* resync */
108 else
109 nalloc = 2; /* recovery */
110
111 /*
112 * Allocate bios.
113 */
114 for (j = nalloc ; j-- ; ) {
115 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
116 if (!bio)
117 goto out_free_bio;
118 r10_bio->devs[j].bio = bio;
119 }
120 /*
121 * Allocate RESYNC_PAGES data pages and attach them
122 * where needed.
123 */
124 for (j = 0 ; j < nalloc; j++) {
125 bio = r10_bio->devs[j].bio;
126 for (i = 0; i < RESYNC_PAGES; i++) {
127 if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
128 &conf->mddev->recovery)) {
129 /* we can share bv_page's during recovery */
130 struct bio *rbio = r10_bio->devs[0].bio;
131 page = rbio->bi_io_vec[i].bv_page;
132 get_page(page);
133 } else
134 page = alloc_page(gfp_flags);
135 if (unlikely(!page))
136 goto out_free_pages;
137
138 bio->bi_io_vec[i].bv_page = page;
139 }
140 }
141
142 return r10_bio;
143
144 out_free_pages:
145 for ( ; i > 0 ; i--)
146 safe_put_page(bio->bi_io_vec[i-1].bv_page);
147 while (j--)
148 for (i = 0; i < RESYNC_PAGES ; i++)
149 safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
150 j = -1;
151 out_free_bio:
152 while ( ++j < nalloc )
153 bio_put(r10_bio->devs[j].bio);
154 r10bio_pool_free(r10_bio, conf);
155 return NULL;
156 }
157
158 static void r10buf_pool_free(void *__r10_bio, void *data)
159 {
160 int i;
161 conf_t *conf = data;
162 r10bio_t *r10bio = __r10_bio;
163 int j;
164
165 for (j=0; j < conf->copies; j++) {
166 struct bio *bio = r10bio->devs[j].bio;
167 if (bio) {
168 for (i = 0; i < RESYNC_PAGES; i++) {
169 safe_put_page(bio->bi_io_vec[i].bv_page);
170 bio->bi_io_vec[i].bv_page = NULL;
171 }
172 bio_put(bio);
173 }
174 }
175 r10bio_pool_free(r10bio, conf);
176 }
177
178 static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
179 {
180 int i;
181
182 for (i = 0; i < conf->copies; i++) {
183 struct bio **bio = & r10_bio->devs[i].bio;
184 if (*bio && *bio != IO_BLOCKED)
185 bio_put(*bio);
186 *bio = NULL;
187 }
188 }
189
190 static void free_r10bio(r10bio_t *r10_bio)
191 {
192 conf_t *conf = r10_bio->mddev->private;
193
194 /*
195 * Wake up any possible resync thread that waits for the device
196 * to go idle.
197 */
198 allow_barrier(conf);
199
200 put_all_bios(conf, r10_bio);
201 mempool_free(r10_bio, conf->r10bio_pool);
202 }
203
204 static void put_buf(r10bio_t *r10_bio)
205 {
206 conf_t *conf = r10_bio->mddev->private;
207
208 mempool_free(r10_bio, conf->r10buf_pool);
209
210 lower_barrier(conf);
211 }
212
213 static void reschedule_retry(r10bio_t *r10_bio)
214 {
215 unsigned long flags;
216 mddev_t *mddev = r10_bio->mddev;
217 conf_t *conf = mddev->private;
218
219 spin_lock_irqsave(&conf->device_lock, flags);
220 list_add(&r10_bio->retry_list, &conf->retry_list);
221 conf->nr_queued ++;
222 spin_unlock_irqrestore(&conf->device_lock, flags);
223
224 /* wake up frozen array... */
225 wake_up(&conf->wait_barrier);
226
227 md_wakeup_thread(mddev->thread);
228 }
229
230 /*
231 * raid_end_bio_io() is called when we have finished servicing a mirrored
232 * operation and are ready to return a success/failure code to the buffer
233 * cache layer.
234 */
235 static void raid_end_bio_io(r10bio_t *r10_bio)
236 {
237 struct bio *bio = r10_bio->master_bio;
238
239 bio_endio(bio,
240 test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
241 free_r10bio(r10_bio);
242 }
243
244 /*
245 * Update disk head position estimator based on IRQ completion info.
246 */
247 static inline void update_head_pos(int slot, r10bio_t *r10_bio)
248 {
249 conf_t *conf = r10_bio->mddev->private;
250
251 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
252 r10_bio->devs[slot].addr + (r10_bio->sectors);
253 }
254
255 /*
256 * Find the disk number which triggered given bio
257 */
258 static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio, struct bio *bio)
259 {
260 int slot;
261
262 for (slot = 0; slot < conf->copies; slot++)
263 if (r10_bio->devs[slot].bio == bio)
264 break;
265
266 BUG_ON(slot == conf->copies);
267 update_head_pos(slot, r10_bio);
268
269 return r10_bio->devs[slot].devnum;
270 }
271
272 static void raid10_end_read_request(struct bio *bio, int error)
273 {
274 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
275 r10bio_t *r10_bio = bio->bi_private;
276 int slot, dev;
277 conf_t *conf = r10_bio->mddev->private;
278
279
280 slot = r10_bio->read_slot;
281 dev = r10_bio->devs[slot].devnum;
282 /*
283 * this branch is our 'one mirror IO has finished' event handler:
284 */
285 update_head_pos(slot, r10_bio);
286
287 if (uptodate) {
288 /*
289 * Set R10BIO_Uptodate in our master bio, so that
290 * we will return a good error code to the higher
291 * levels even if IO on some other mirrored buffer fails.
292 *
293 * The 'master' represents the composite IO operation to
294 * user-side. So if something waits for IO, then it will
295 * wait for the 'master' bio.
296 */
297 set_bit(R10BIO_Uptodate, &r10_bio->state);
298 raid_end_bio_io(r10_bio);
299 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
300 } else {
301 /*
302 * oops, read error - keep the refcount on the rdev
303 */
304 char b[BDEVNAME_SIZE];
305 printk_ratelimited(KERN_ERR
306 "md/raid10:%s: %s: rescheduling sector %llu\n",
307 mdname(conf->mddev),
308 bdevname(conf->mirrors[dev].rdev->bdev, b),
309 (unsigned long long)r10_bio->sector);
310 reschedule_retry(r10_bio);
311 }
312 }
313
314 static void raid10_end_write_request(struct bio *bio, int error)
315 {
316 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
317 r10bio_t *r10_bio = bio->bi_private;
318 int dev;
319 conf_t *conf = r10_bio->mddev->private;
320
321 dev = find_bio_disk(conf, r10_bio, bio);
322
323 /*
324 * this branch is our 'one mirror IO has finished' event handler:
325 */
326 if (!uptodate) {
327 md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
328 /* an I/O failed, we can't clear the bitmap */
329 set_bit(R10BIO_Degraded, &r10_bio->state);
330 } else
331 /*
332 * Set R10BIO_Uptodate in our master bio, so that
333 * we will return a good error code for to the higher
334 * levels even if IO on some other mirrored buffer fails.
335 *
336 * The 'master' represents the composite IO operation to
337 * user-side. So if something waits for IO, then it will
338 * wait for the 'master' bio.
339 */
340 set_bit(R10BIO_Uptodate, &r10_bio->state);
341
342 /*
343 *
344 * Let's see if all mirrored write operations have finished
345 * already.
346 */
347 if (atomic_dec_and_test(&r10_bio->remaining)) {
348 /* clear the bitmap if all writes complete successfully */
349 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
350 r10_bio->sectors,
351 !test_bit(R10BIO_Degraded, &r10_bio->state),
352 0);
353 md_write_end(r10_bio->mddev);
354 raid_end_bio_io(r10_bio);
355 }
356
357 rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
358 }
359
360
361 /*
362 * RAID10 layout manager
363 * As well as the chunksize and raid_disks count, there are two
364 * parameters: near_copies and far_copies.
365 * near_copies * far_copies must be <= raid_disks.
366 * Normally one of these will be 1.
367 * If both are 1, we get raid0.
368 * If near_copies == raid_disks, we get raid1.
369 *
370 * Chunks are laid out in raid0 style with near_copies copies of the
371 * first chunk, followed by near_copies copies of the next chunk and
372 * so on.
373 * If far_copies > 1, then after 1/far_copies of the array has been assigned
374 * as described above, we start again with a device offset of near_copies.
375 * So we effectively have another copy of the whole array further down all
376 * the drives, but with blocks on different drives.
377 * With this layout, and block is never stored twice on the one device.
378 *
379 * raid10_find_phys finds the sector offset of a given virtual sector
380 * on each device that it is on.
381 *
382 * raid10_find_virt does the reverse mapping, from a device and a
383 * sector offset to a virtual address
384 */
385
386 static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
387 {
388 int n,f;
389 sector_t sector;
390 sector_t chunk;
391 sector_t stripe;
392 int dev;
393
394 int slot = 0;
395
396 /* now calculate first sector/dev */
397 chunk = r10bio->sector >> conf->chunk_shift;
398 sector = r10bio->sector & conf->chunk_mask;
399
400 chunk *= conf->near_copies;
401 stripe = chunk;
402 dev = sector_div(stripe, conf->raid_disks);
403 if (conf->far_offset)
404 stripe *= conf->far_copies;
405
406 sector += stripe << conf->chunk_shift;
407
408 /* and calculate all the others */
409 for (n=0; n < conf->near_copies; n++) {
410 int d = dev;
411 sector_t s = sector;
412 r10bio->devs[slot].addr = sector;
413 r10bio->devs[slot].devnum = d;
414 slot++;
415
416 for (f = 1; f < conf->far_copies; f++) {
417 d += conf->near_copies;
418 if (d >= conf->raid_disks)
419 d -= conf->raid_disks;
420 s += conf->stride;
421 r10bio->devs[slot].devnum = d;
422 r10bio->devs[slot].addr = s;
423 slot++;
424 }
425 dev++;
426 if (dev >= conf->raid_disks) {
427 dev = 0;
428 sector += (conf->chunk_mask + 1);
429 }
430 }
431 BUG_ON(slot != conf->copies);
432 }
433
434 static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
435 {
436 sector_t offset, chunk, vchunk;
437
438 offset = sector & conf->chunk_mask;
439 if (conf->far_offset) {
440 int fc;
441 chunk = sector >> conf->chunk_shift;
442 fc = sector_div(chunk, conf->far_copies);
443 dev -= fc * conf->near_copies;
444 if (dev < 0)
445 dev += conf->raid_disks;
446 } else {
447 while (sector >= conf->stride) {
448 sector -= conf->stride;
449 if (dev < conf->near_copies)
450 dev += conf->raid_disks - conf->near_copies;
451 else
452 dev -= conf->near_copies;
453 }
454 chunk = sector >> conf->chunk_shift;
455 }
456 vchunk = chunk * conf->raid_disks + dev;
457 sector_div(vchunk, conf->near_copies);
458 return (vchunk << conf->chunk_shift) + offset;
459 }
460
461 /**
462 * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
463 * @q: request queue
464 * @bvm: properties of new bio
465 * @biovec: the request that could be merged to it.
466 *
467 * Return amount of bytes we can accept at this offset
468 * If near_copies == raid_disk, there are no striping issues,
469 * but in that case, the function isn't called at all.
470 */
471 static int raid10_mergeable_bvec(struct request_queue *q,
472 struct bvec_merge_data *bvm,
473 struct bio_vec *biovec)
474 {
475 mddev_t *mddev = q->queuedata;
476 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
477 int max;
478 unsigned int chunk_sectors = mddev->chunk_sectors;
479 unsigned int bio_sectors = bvm->bi_size >> 9;
480
481 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
482 if (max < 0) max = 0; /* bio_add cannot handle a negative return */
483 if (max <= biovec->bv_len && bio_sectors == 0)
484 return biovec->bv_len;
485 else
486 return max;
487 }
488
489 /*
490 * This routine returns the disk from which the requested read should
491 * be done. There is a per-array 'next expected sequential IO' sector
492 * number - if this matches on the next IO then we use the last disk.
493 * There is also a per-disk 'last know head position' sector that is
494 * maintained from IRQ contexts, both the normal and the resync IO
495 * completion handlers update this position correctly. If there is no
496 * perfect sequential match then we pick the disk whose head is closest.
497 *
498 * If there are 2 mirrors in the same 2 devices, performance degrades
499 * because position is mirror, not device based.
500 *
501 * The rdev for the device selected will have nr_pending incremented.
502 */
503
504 /*
505 * FIXME: possibly should rethink readbalancing and do it differently
506 * depending on near_copies / far_copies geometry.
507 */
508 static int read_balance(conf_t *conf, r10bio_t *r10_bio)
509 {
510 const sector_t this_sector = r10_bio->sector;
511 int disk, slot;
512 const int sectors = r10_bio->sectors;
513 sector_t new_distance, best_dist;
514 mdk_rdev_t *rdev;
515 int do_balance;
516 int best_slot;
517
518 raid10_find_phys(conf, r10_bio);
519 rcu_read_lock();
520 retry:
521 best_slot = -1;
522 best_dist = MaxSector;
523 do_balance = 1;
524 /*
525 * Check if we can balance. We can balance on the whole
526 * device if no resync is going on (recovery is ok), or below
527 * the resync window. We take the first readable disk when
528 * above the resync window.
529 */
530 if (conf->mddev->recovery_cp < MaxSector
531 && (this_sector + sectors >= conf->next_resync))
532 do_balance = 0;
533
534 for (slot = 0; slot < conf->copies ; slot++) {
535 if (r10_bio->devs[slot].bio == IO_BLOCKED)
536 continue;
537 disk = r10_bio->devs[slot].devnum;
538 rdev = rcu_dereference(conf->mirrors[disk].rdev);
539 if (rdev == NULL)
540 continue;
541 if (!test_bit(In_sync, &rdev->flags))
542 continue;
543
544 if (!do_balance)
545 break;
546
547 /* This optimisation is debatable, and completely destroys
548 * sequential read speed for 'far copies' arrays. So only
549 * keep it for 'near' arrays, and review those later.
550 */
551 if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
552 break;
553
554 /* for far > 1 always use the lowest address */
555 if (conf->far_copies > 1)
556 new_distance = r10_bio->devs[slot].addr;
557 else
558 new_distance = abs(r10_bio->devs[slot].addr -
559 conf->mirrors[disk].head_position);
560 if (new_distance < best_dist) {
561 best_dist = new_distance;
562 best_slot = slot;
563 }
564 }
565 if (slot == conf->copies)
566 slot = best_slot;
567
568 if (slot >= 0) {
569 disk = r10_bio->devs[slot].devnum;
570 rdev = rcu_dereference(conf->mirrors[disk].rdev);
571 if (!rdev)
572 goto retry;
573 atomic_inc(&rdev->nr_pending);
574 if (test_bit(Faulty, &rdev->flags)) {
575 /* Cannot risk returning a device that failed
576 * before we inc'ed nr_pending
577 */
578 rdev_dec_pending(rdev, conf->mddev);
579 goto retry;
580 }
581 r10_bio->read_slot = slot;
582 } else
583 disk = -1;
584 rcu_read_unlock();
585
586 return disk;
587 }
588
589 static int raid10_congested(void *data, int bits)
590 {
591 mddev_t *mddev = data;
592 conf_t *conf = mddev->private;
593 int i, ret = 0;
594
595 if (mddev_congested(mddev, bits))
596 return 1;
597 rcu_read_lock();
598 for (i = 0; i < conf->raid_disks && ret == 0; i++) {
599 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
600 if (rdev && !test_bit(Faulty, &rdev->flags)) {
601 struct request_queue *q = bdev_get_queue(rdev->bdev);
602
603 ret |= bdi_congested(&q->backing_dev_info, bits);
604 }
605 }
606 rcu_read_unlock();
607 return ret;
608 }
609
610 static void flush_pending_writes(conf_t *conf)
611 {
612 /* Any writes that have been queued but are awaiting
613 * bitmap updates get flushed here.
614 */
615 spin_lock_irq(&conf->device_lock);
616
617 if (conf->pending_bio_list.head) {
618 struct bio *bio;
619 bio = bio_list_get(&conf->pending_bio_list);
620 spin_unlock_irq(&conf->device_lock);
621 /* flush any pending bitmap writes to disk
622 * before proceeding w/ I/O */
623 bitmap_unplug(conf->mddev->bitmap);
624
625 while (bio) { /* submit pending writes */
626 struct bio *next = bio->bi_next;
627 bio->bi_next = NULL;
628 generic_make_request(bio);
629 bio = next;
630 }
631 } else
632 spin_unlock_irq(&conf->device_lock);
633 }
634
635 /* Barriers....
636 * Sometimes we need to suspend IO while we do something else,
637 * either some resync/recovery, or reconfigure the array.
638 * To do this we raise a 'barrier'.
639 * The 'barrier' is a counter that can be raised multiple times
640 * to count how many activities are happening which preclude
641 * normal IO.
642 * We can only raise the barrier if there is no pending IO.
643 * i.e. if nr_pending == 0.
644 * We choose only to raise the barrier if no-one is waiting for the
645 * barrier to go down. This means that as soon as an IO request
646 * is ready, no other operations which require a barrier will start
647 * until the IO request has had a chance.
648 *
649 * So: regular IO calls 'wait_barrier'. When that returns there
650 * is no backgroup IO happening, It must arrange to call
651 * allow_barrier when it has finished its IO.
652 * backgroup IO calls must call raise_barrier. Once that returns
653 * there is no normal IO happeing. It must arrange to call
654 * lower_barrier when the particular background IO completes.
655 */
656
657 static void raise_barrier(conf_t *conf, int force)
658 {
659 BUG_ON(force && !conf->barrier);
660 spin_lock_irq(&conf->resync_lock);
661
662 /* Wait until no block IO is waiting (unless 'force') */
663 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
664 conf->resync_lock, );
665
666 /* block any new IO from starting */
667 conf->barrier++;
668
669 /* Now wait for all pending IO to complete */
670 wait_event_lock_irq(conf->wait_barrier,
671 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
672 conf->resync_lock, );
673
674 spin_unlock_irq(&conf->resync_lock);
675 }
676
677 static void lower_barrier(conf_t *conf)
678 {
679 unsigned long flags;
680 spin_lock_irqsave(&conf->resync_lock, flags);
681 conf->barrier--;
682 spin_unlock_irqrestore(&conf->resync_lock, flags);
683 wake_up(&conf->wait_barrier);
684 }
685
686 static void wait_barrier(conf_t *conf)
687 {
688 spin_lock_irq(&conf->resync_lock);
689 if (conf->barrier) {
690 conf->nr_waiting++;
691 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
692 conf->resync_lock,
693 );
694 conf->nr_waiting--;
695 }
696 conf->nr_pending++;
697 spin_unlock_irq(&conf->resync_lock);
698 }
699
700 static void allow_barrier(conf_t *conf)
701 {
702 unsigned long flags;
703 spin_lock_irqsave(&conf->resync_lock, flags);
704 conf->nr_pending--;
705 spin_unlock_irqrestore(&conf->resync_lock, flags);
706 wake_up(&conf->wait_barrier);
707 }
708
709 static void freeze_array(conf_t *conf)
710 {
711 /* stop syncio and normal IO and wait for everything to
712 * go quiet.
713 * We increment barrier and nr_waiting, and then
714 * wait until nr_pending match nr_queued+1
715 * This is called in the context of one normal IO request
716 * that has failed. Thus any sync request that might be pending
717 * will be blocked by nr_pending, and we need to wait for
718 * pending IO requests to complete or be queued for re-try.
719 * Thus the number queued (nr_queued) plus this request (1)
720 * must match the number of pending IOs (nr_pending) before
721 * we continue.
722 */
723 spin_lock_irq(&conf->resync_lock);
724 conf->barrier++;
725 conf->nr_waiting++;
726 wait_event_lock_irq(conf->wait_barrier,
727 conf->nr_pending == conf->nr_queued+1,
728 conf->resync_lock,
729 flush_pending_writes(conf));
730
731 spin_unlock_irq(&conf->resync_lock);
732 }
733
734 static void unfreeze_array(conf_t *conf)
735 {
736 /* reverse the effect of the freeze */
737 spin_lock_irq(&conf->resync_lock);
738 conf->barrier--;
739 conf->nr_waiting--;
740 wake_up(&conf->wait_barrier);
741 spin_unlock_irq(&conf->resync_lock);
742 }
743
744 static int make_request(mddev_t *mddev, struct bio * bio)
745 {
746 conf_t *conf = mddev->private;
747 mirror_info_t *mirror;
748 r10bio_t *r10_bio;
749 struct bio *read_bio;
750 int i;
751 int chunk_sects = conf->chunk_mask + 1;
752 const int rw = bio_data_dir(bio);
753 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
754 const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
755 unsigned long flags;
756 mdk_rdev_t *blocked_rdev;
757 int plugged;
758
759 if (unlikely(bio->bi_rw & REQ_FLUSH)) {
760 md_flush_request(mddev, bio);
761 return 0;
762 }
763
764 /* If this request crosses a chunk boundary, we need to
765 * split it. This will only happen for 1 PAGE (or less) requests.
766 */
767 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
768 > chunk_sects &&
769 conf->near_copies < conf->raid_disks)) {
770 struct bio_pair *bp;
771 /* Sanity check -- queue functions should prevent this happening */
772 if (bio->bi_vcnt != 1 ||
773 bio->bi_idx != 0)
774 goto bad_map;
775 /* This is a one page bio that upper layers
776 * refuse to split for us, so we need to split it.
777 */
778 bp = bio_split(bio,
779 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
780
781 /* Each of these 'make_request' calls will call 'wait_barrier'.
782 * If the first succeeds but the second blocks due to the resync
783 * thread raising the barrier, we will deadlock because the
784 * IO to the underlying device will be queued in generic_make_request
785 * and will never complete, so will never reduce nr_pending.
786 * So increment nr_waiting here so no new raise_barriers will
787 * succeed, and so the second wait_barrier cannot block.
788 */
789 spin_lock_irq(&conf->resync_lock);
790 conf->nr_waiting++;
791 spin_unlock_irq(&conf->resync_lock);
792
793 if (make_request(mddev, &bp->bio1))
794 generic_make_request(&bp->bio1);
795 if (make_request(mddev, &bp->bio2))
796 generic_make_request(&bp->bio2);
797
798 spin_lock_irq(&conf->resync_lock);
799 conf->nr_waiting--;
800 wake_up(&conf->wait_barrier);
801 spin_unlock_irq(&conf->resync_lock);
802
803 bio_pair_release(bp);
804 return 0;
805 bad_map:
806 printk("md/raid10:%s: make_request bug: can't convert block across chunks"
807 " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
808 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
809
810 bio_io_error(bio);
811 return 0;
812 }
813
814 md_write_start(mddev, bio);
815
816 /*
817 * Register the new request and wait if the reconstruction
818 * thread has put up a bar for new requests.
819 * Continue immediately if no resync is active currently.
820 */
821 wait_barrier(conf);
822
823 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
824
825 r10_bio->master_bio = bio;
826 r10_bio->sectors = bio->bi_size >> 9;
827
828 r10_bio->mddev = mddev;
829 r10_bio->sector = bio->bi_sector;
830 r10_bio->state = 0;
831
832 if (rw == READ) {
833 /*
834 * read balancing logic:
835 */
836 int disk = read_balance(conf, r10_bio);
837 int slot = r10_bio->read_slot;
838 if (disk < 0) {
839 raid_end_bio_io(r10_bio);
840 return 0;
841 }
842 mirror = conf->mirrors + disk;
843
844 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
845
846 r10_bio->devs[slot].bio = read_bio;
847
848 read_bio->bi_sector = r10_bio->devs[slot].addr +
849 mirror->rdev->data_offset;
850 read_bio->bi_bdev = mirror->rdev->bdev;
851 read_bio->bi_end_io = raid10_end_read_request;
852 read_bio->bi_rw = READ | do_sync;
853 read_bio->bi_private = r10_bio;
854
855 generic_make_request(read_bio);
856 return 0;
857 }
858
859 /*
860 * WRITE:
861 */
862 /* first select target devices under rcu_lock and
863 * inc refcount on their rdev. Record them by setting
864 * bios[x] to bio
865 */
866 plugged = mddev_check_plugged(mddev);
867
868 raid10_find_phys(conf, r10_bio);
869 retry_write:
870 blocked_rdev = NULL;
871 rcu_read_lock();
872 for (i = 0; i < conf->copies; i++) {
873 int d = r10_bio->devs[i].devnum;
874 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
875 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
876 atomic_inc(&rdev->nr_pending);
877 blocked_rdev = rdev;
878 break;
879 }
880 if (rdev && !test_bit(Faulty, &rdev->flags)) {
881 atomic_inc(&rdev->nr_pending);
882 r10_bio->devs[i].bio = bio;
883 } else {
884 r10_bio->devs[i].bio = NULL;
885 set_bit(R10BIO_Degraded, &r10_bio->state);
886 }
887 }
888 rcu_read_unlock();
889
890 if (unlikely(blocked_rdev)) {
891 /* Have to wait for this device to get unblocked, then retry */
892 int j;
893 int d;
894
895 for (j = 0; j < i; j++)
896 if (r10_bio->devs[j].bio) {
897 d = r10_bio->devs[j].devnum;
898 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
899 }
900 allow_barrier(conf);
901 md_wait_for_blocked_rdev(blocked_rdev, mddev);
902 wait_barrier(conf);
903 goto retry_write;
904 }
905
906 atomic_set(&r10_bio->remaining, 1);
907 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
908
909 for (i = 0; i < conf->copies; i++) {
910 struct bio *mbio;
911 int d = r10_bio->devs[i].devnum;
912 if (!r10_bio->devs[i].bio)
913 continue;
914
915 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
916 r10_bio->devs[i].bio = mbio;
917
918 mbio->bi_sector = r10_bio->devs[i].addr+
919 conf->mirrors[d].rdev->data_offset;
920 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
921 mbio->bi_end_io = raid10_end_write_request;
922 mbio->bi_rw = WRITE | do_sync | do_fua;
923 mbio->bi_private = r10_bio;
924
925 atomic_inc(&r10_bio->remaining);
926 spin_lock_irqsave(&conf->device_lock, flags);
927 bio_list_add(&conf->pending_bio_list, mbio);
928 spin_unlock_irqrestore(&conf->device_lock, flags);
929 }
930
931 if (atomic_dec_and_test(&r10_bio->remaining)) {
932 /* This matches the end of raid10_end_write_request() */
933 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
934 r10_bio->sectors,
935 !test_bit(R10BIO_Degraded, &r10_bio->state),
936 0);
937 md_write_end(mddev);
938 raid_end_bio_io(r10_bio);
939 }
940
941 /* In case raid10d snuck in to freeze_array */
942 wake_up(&conf->wait_barrier);
943
944 if (do_sync || !mddev->bitmap || !plugged)
945 md_wakeup_thread(mddev->thread);
946 return 0;
947 }
948
949 static void status(struct seq_file *seq, mddev_t *mddev)
950 {
951 conf_t *conf = mddev->private;
952 int i;
953
954 if (conf->near_copies < conf->raid_disks)
955 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
956 if (conf->near_copies > 1)
957 seq_printf(seq, " %d near-copies", conf->near_copies);
958 if (conf->far_copies > 1) {
959 if (conf->far_offset)
960 seq_printf(seq, " %d offset-copies", conf->far_copies);
961 else
962 seq_printf(seq, " %d far-copies", conf->far_copies);
963 }
964 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
965 conf->raid_disks - mddev->degraded);
966 for (i = 0; i < conf->raid_disks; i++)
967 seq_printf(seq, "%s",
968 conf->mirrors[i].rdev &&
969 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
970 seq_printf(seq, "]");
971 }
972
973 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
974 {
975 char b[BDEVNAME_SIZE];
976 conf_t *conf = mddev->private;
977
978 /*
979 * If it is not operational, then we have already marked it as dead
980 * else if it is the last working disks, ignore the error, let the
981 * next level up know.
982 * else mark the drive as failed
983 */
984 if (test_bit(In_sync, &rdev->flags)
985 && conf->raid_disks-mddev->degraded == 1)
986 /*
987 * Don't fail the drive, just return an IO error.
988 * The test should really be more sophisticated than
989 * "working_disks == 1", but it isn't critical, and
990 * can wait until we do more sophisticated "is the drive
991 * really dead" tests...
992 */
993 return;
994 if (test_and_clear_bit(In_sync, &rdev->flags)) {
995 unsigned long flags;
996 spin_lock_irqsave(&conf->device_lock, flags);
997 mddev->degraded++;
998 spin_unlock_irqrestore(&conf->device_lock, flags);
999 /*
1000 * if recovery is running, make sure it aborts.
1001 */
1002 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1003 }
1004 set_bit(Faulty, &rdev->flags);
1005 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1006 printk(KERN_ALERT
1007 "md/raid10:%s: Disk failure on %s, disabling device.\n"
1008 "md/raid10:%s: Operation continuing on %d devices.\n",
1009 mdname(mddev), bdevname(rdev->bdev, b),
1010 mdname(mddev), conf->raid_disks - mddev->degraded);
1011 }
1012
1013 static void print_conf(conf_t *conf)
1014 {
1015 int i;
1016 mirror_info_t *tmp;
1017
1018 printk(KERN_DEBUG "RAID10 conf printout:\n");
1019 if (!conf) {
1020 printk(KERN_DEBUG "(!conf)\n");
1021 return;
1022 }
1023 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1024 conf->raid_disks);
1025
1026 for (i = 0; i < conf->raid_disks; i++) {
1027 char b[BDEVNAME_SIZE];
1028 tmp = conf->mirrors + i;
1029 if (tmp->rdev)
1030 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1031 i, !test_bit(In_sync, &tmp->rdev->flags),
1032 !test_bit(Faulty, &tmp->rdev->flags),
1033 bdevname(tmp->rdev->bdev,b));
1034 }
1035 }
1036
1037 static void close_sync(conf_t *conf)
1038 {
1039 wait_barrier(conf);
1040 allow_barrier(conf);
1041
1042 mempool_destroy(conf->r10buf_pool);
1043 conf->r10buf_pool = NULL;
1044 }
1045
1046 /* check if there are enough drives for
1047 * every block to appear on atleast one
1048 */
1049 static int enough(conf_t *conf)
1050 {
1051 int first = 0;
1052
1053 do {
1054 int n = conf->copies;
1055 int cnt = 0;
1056 while (n--) {
1057 if (conf->mirrors[first].rdev)
1058 cnt++;
1059 first = (first+1) % conf->raid_disks;
1060 }
1061 if (cnt == 0)
1062 return 0;
1063 } while (first != 0);
1064 return 1;
1065 }
1066
1067 static int raid10_spare_active(mddev_t *mddev)
1068 {
1069 int i;
1070 conf_t *conf = mddev->private;
1071 mirror_info_t *tmp;
1072 int count = 0;
1073 unsigned long flags;
1074
1075 /*
1076 * Find all non-in_sync disks within the RAID10 configuration
1077 * and mark them in_sync
1078 */
1079 for (i = 0; i < conf->raid_disks; i++) {
1080 tmp = conf->mirrors + i;
1081 if (tmp->rdev
1082 && !test_bit(Faulty, &tmp->rdev->flags)
1083 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1084 count++;
1085 sysfs_notify_dirent(tmp->rdev->sysfs_state);
1086 }
1087 }
1088 spin_lock_irqsave(&conf->device_lock, flags);
1089 mddev->degraded -= count;
1090 spin_unlock_irqrestore(&conf->device_lock, flags);
1091
1092 print_conf(conf);
1093 return count;
1094 }
1095
1096
1097 static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1098 {
1099 conf_t *conf = mddev->private;
1100 int err = -EEXIST;
1101 int mirror;
1102 int first = 0;
1103 int last = conf->raid_disks - 1;
1104
1105 if (mddev->recovery_cp < MaxSector)
1106 /* only hot-add to in-sync arrays, as recovery is
1107 * very different from resync
1108 */
1109 return -EBUSY;
1110 if (!enough(conf))
1111 return -EINVAL;
1112
1113 if (rdev->raid_disk >= 0)
1114 first = last = rdev->raid_disk;
1115
1116 if (rdev->saved_raid_disk >= first &&
1117 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1118 mirror = rdev->saved_raid_disk;
1119 else
1120 mirror = first;
1121 for ( ; mirror <= last ; mirror++) {
1122 mirror_info_t *p = &conf->mirrors[mirror];
1123 if (p->recovery_disabled == mddev->recovery_disabled)
1124 continue;
1125 if (!p->rdev)
1126 continue;
1127
1128 disk_stack_limits(mddev->gendisk, rdev->bdev,
1129 rdev->data_offset << 9);
1130 /* as we don't honour merge_bvec_fn, we must
1131 * never risk violating it, so limit
1132 * ->max_segments to one lying with a single
1133 * page, as a one page request is never in
1134 * violation.
1135 */
1136 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
1137 blk_queue_max_segments(mddev->queue, 1);
1138 blk_queue_segment_boundary(mddev->queue,
1139 PAGE_CACHE_SIZE - 1);
1140 }
1141
1142 p->head_position = 0;
1143 rdev->raid_disk = mirror;
1144 err = 0;
1145 if (rdev->saved_raid_disk != mirror)
1146 conf->fullsync = 1;
1147 rcu_assign_pointer(p->rdev, rdev);
1148 break;
1149 }
1150
1151 md_integrity_add_rdev(rdev, mddev);
1152 print_conf(conf);
1153 return err;
1154 }
1155
1156 static int raid10_remove_disk(mddev_t *mddev, int number)
1157 {
1158 conf_t *conf = mddev->private;
1159 int err = 0;
1160 mdk_rdev_t *rdev;
1161 mirror_info_t *p = conf->mirrors+ number;
1162
1163 print_conf(conf);
1164 rdev = p->rdev;
1165 if (rdev) {
1166 if (test_bit(In_sync, &rdev->flags) ||
1167 atomic_read(&rdev->nr_pending)) {
1168 err = -EBUSY;
1169 goto abort;
1170 }
1171 /* Only remove faulty devices in recovery
1172 * is not possible.
1173 */
1174 if (!test_bit(Faulty, &rdev->flags) &&
1175 mddev->recovery_disabled != p->recovery_disabled &&
1176 enough(conf)) {
1177 err = -EBUSY;
1178 goto abort;
1179 }
1180 p->rdev = NULL;
1181 synchronize_rcu();
1182 if (atomic_read(&rdev->nr_pending)) {
1183 /* lost the race, try later */
1184 err = -EBUSY;
1185 p->rdev = rdev;
1186 goto abort;
1187 }
1188 err = md_integrity_register(mddev);
1189 }
1190 abort:
1191
1192 print_conf(conf);
1193 return err;
1194 }
1195
1196
1197 static void end_sync_read(struct bio *bio, int error)
1198 {
1199 r10bio_t *r10_bio = bio->bi_private;
1200 conf_t *conf = r10_bio->mddev->private;
1201 int d;
1202
1203 d = find_bio_disk(conf, r10_bio, bio);
1204
1205 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1206 set_bit(R10BIO_Uptodate, &r10_bio->state);
1207 else {
1208 atomic_add(r10_bio->sectors,
1209 &conf->mirrors[d].rdev->corrected_errors);
1210 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1211 md_error(r10_bio->mddev,
1212 conf->mirrors[d].rdev);
1213 }
1214
1215 /* for reconstruct, we always reschedule after a read.
1216 * for resync, only after all reads
1217 */
1218 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1219 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1220 atomic_dec_and_test(&r10_bio->remaining)) {
1221 /* we have read all the blocks,
1222 * do the comparison in process context in raid10d
1223 */
1224 reschedule_retry(r10_bio);
1225 }
1226 }
1227
1228 static void end_sync_write(struct bio *bio, int error)
1229 {
1230 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1231 r10bio_t *r10_bio = bio->bi_private;
1232 mddev_t *mddev = r10_bio->mddev;
1233 conf_t *conf = mddev->private;
1234 int d;
1235
1236 d = find_bio_disk(conf, r10_bio, bio);
1237
1238 if (!uptodate)
1239 md_error(mddev, conf->mirrors[d].rdev);
1240
1241 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1242 while (atomic_dec_and_test(&r10_bio->remaining)) {
1243 if (r10_bio->master_bio == NULL) {
1244 /* the primary of several recovery bios */
1245 sector_t s = r10_bio->sectors;
1246 put_buf(r10_bio);
1247 md_done_sync(mddev, s, 1);
1248 break;
1249 } else {
1250 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1251 put_buf(r10_bio);
1252 r10_bio = r10_bio2;
1253 }
1254 }
1255 }
1256
1257 /*
1258 * Note: sync and recover and handled very differently for raid10
1259 * This code is for resync.
1260 * For resync, we read through virtual addresses and read all blocks.
1261 * If there is any error, we schedule a write. The lowest numbered
1262 * drive is authoritative.
1263 * However requests come for physical address, so we need to map.
1264 * For every physical address there are raid_disks/copies virtual addresses,
1265 * which is always are least one, but is not necessarly an integer.
1266 * This means that a physical address can span multiple chunks, so we may
1267 * have to submit multiple io requests for a single sync request.
1268 */
1269 /*
1270 * We check if all blocks are in-sync and only write to blocks that
1271 * aren't in sync
1272 */
1273 static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1274 {
1275 conf_t *conf = mddev->private;
1276 int i, first;
1277 struct bio *tbio, *fbio;
1278
1279 atomic_set(&r10_bio->remaining, 1);
1280
1281 /* find the first device with a block */
1282 for (i=0; i<conf->copies; i++)
1283 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1284 break;
1285
1286 if (i == conf->copies)
1287 goto done;
1288
1289 first = i;
1290 fbio = r10_bio->devs[i].bio;
1291
1292 /* now find blocks with errors */
1293 for (i=0 ; i < conf->copies ; i++) {
1294 int j, d;
1295 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1296
1297 tbio = r10_bio->devs[i].bio;
1298
1299 if (tbio->bi_end_io != end_sync_read)
1300 continue;
1301 if (i == first)
1302 continue;
1303 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1304 /* We know that the bi_io_vec layout is the same for
1305 * both 'first' and 'i', so we just compare them.
1306 * All vec entries are PAGE_SIZE;
1307 */
1308 for (j = 0; j < vcnt; j++)
1309 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1310 page_address(tbio->bi_io_vec[j].bv_page),
1311 PAGE_SIZE))
1312 break;
1313 if (j == vcnt)
1314 continue;
1315 mddev->resync_mismatches += r10_bio->sectors;
1316 }
1317 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1318 /* Don't fix anything. */
1319 continue;
1320 /* Ok, we need to write this bio
1321 * First we need to fixup bv_offset, bv_len and
1322 * bi_vecs, as the read request might have corrupted these
1323 */
1324 tbio->bi_vcnt = vcnt;
1325 tbio->bi_size = r10_bio->sectors << 9;
1326 tbio->bi_idx = 0;
1327 tbio->bi_phys_segments = 0;
1328 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1329 tbio->bi_flags |= 1 << BIO_UPTODATE;
1330 tbio->bi_next = NULL;
1331 tbio->bi_rw = WRITE;
1332 tbio->bi_private = r10_bio;
1333 tbio->bi_sector = r10_bio->devs[i].addr;
1334
1335 for (j=0; j < vcnt ; j++) {
1336 tbio->bi_io_vec[j].bv_offset = 0;
1337 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1338
1339 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1340 page_address(fbio->bi_io_vec[j].bv_page),
1341 PAGE_SIZE);
1342 }
1343 tbio->bi_end_io = end_sync_write;
1344
1345 d = r10_bio->devs[i].devnum;
1346 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1347 atomic_inc(&r10_bio->remaining);
1348 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1349
1350 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1351 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1352 generic_make_request(tbio);
1353 }
1354
1355 done:
1356 if (atomic_dec_and_test(&r10_bio->remaining)) {
1357 md_done_sync(mddev, r10_bio->sectors, 1);
1358 put_buf(r10_bio);
1359 }
1360 }
1361
1362 /*
1363 * Now for the recovery code.
1364 * Recovery happens across physical sectors.
1365 * We recover all non-is_sync drives by finding the virtual address of
1366 * each, and then choose a working drive that also has that virt address.
1367 * There is a separate r10_bio for each non-in_sync drive.
1368 * Only the first two slots are in use. The first for reading,
1369 * The second for writing.
1370 *
1371 */
1372
1373 static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1374 {
1375 conf_t *conf = mddev->private;
1376 int d;
1377 struct bio *wbio;
1378
1379 /*
1380 * share the pages with the first bio
1381 * and submit the write request
1382 */
1383 wbio = r10_bio->devs[1].bio;
1384 d = r10_bio->devs[1].devnum;
1385
1386 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1387 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
1388 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1389 generic_make_request(wbio);
1390 else {
1391 printk(KERN_NOTICE
1392 "md/raid10:%s: recovery aborted due to read error\n",
1393 mdname(mddev));
1394 conf->mirrors[d].recovery_disabled = mddev->recovery_disabled;
1395 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1396 bio_endio(wbio, 0);
1397 }
1398 }
1399
1400
1401 /*
1402 * Used by fix_read_error() to decay the per rdev read_errors.
1403 * We halve the read error count for every hour that has elapsed
1404 * since the last recorded read error.
1405 *
1406 */
1407 static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev)
1408 {
1409 struct timespec cur_time_mon;
1410 unsigned long hours_since_last;
1411 unsigned int read_errors = atomic_read(&rdev->read_errors);
1412
1413 ktime_get_ts(&cur_time_mon);
1414
1415 if (rdev->last_read_error.tv_sec == 0 &&
1416 rdev->last_read_error.tv_nsec == 0) {
1417 /* first time we've seen a read error */
1418 rdev->last_read_error = cur_time_mon;
1419 return;
1420 }
1421
1422 hours_since_last = (cur_time_mon.tv_sec -
1423 rdev->last_read_error.tv_sec) / 3600;
1424
1425 rdev->last_read_error = cur_time_mon;
1426
1427 /*
1428 * if hours_since_last is > the number of bits in read_errors
1429 * just set read errors to 0. We do this to avoid
1430 * overflowing the shift of read_errors by hours_since_last.
1431 */
1432 if (hours_since_last >= 8 * sizeof(read_errors))
1433 atomic_set(&rdev->read_errors, 0);
1434 else
1435 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
1436 }
1437
1438 /*
1439 * This is a kernel thread which:
1440 *
1441 * 1. Retries failed read operations on working mirrors.
1442 * 2. Updates the raid superblock when problems encounter.
1443 * 3. Performs writes following reads for array synchronising.
1444 */
1445
1446 static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1447 {
1448 int sect = 0; /* Offset from r10_bio->sector */
1449 int sectors = r10_bio->sectors;
1450 mdk_rdev_t*rdev;
1451 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
1452 int d = r10_bio->devs[r10_bio->read_slot].devnum;
1453
1454 /* still own a reference to this rdev, so it cannot
1455 * have been cleared recently.
1456 */
1457 rdev = conf->mirrors[d].rdev;
1458
1459 if (test_bit(Faulty, &rdev->flags))
1460 /* drive has already been failed, just ignore any
1461 more fix_read_error() attempts */
1462 return;
1463
1464 check_decay_read_errors(mddev, rdev);
1465 atomic_inc(&rdev->read_errors);
1466 if (atomic_read(&rdev->read_errors) > max_read_errors) {
1467 char b[BDEVNAME_SIZE];
1468 bdevname(rdev->bdev, b);
1469
1470 printk(KERN_NOTICE
1471 "md/raid10:%s: %s: Raid device exceeded "
1472 "read_error threshold [cur %d:max %d]\n",
1473 mdname(mddev), b,
1474 atomic_read(&rdev->read_errors), max_read_errors);
1475 printk(KERN_NOTICE
1476 "md/raid10:%s: %s: Failing raid device\n",
1477 mdname(mddev), b);
1478 md_error(mddev, conf->mirrors[d].rdev);
1479 return;
1480 }
1481
1482 while(sectors) {
1483 int s = sectors;
1484 int sl = r10_bio->read_slot;
1485 int success = 0;
1486 int start;
1487
1488 if (s > (PAGE_SIZE>>9))
1489 s = PAGE_SIZE >> 9;
1490
1491 rcu_read_lock();
1492 do {
1493 d = r10_bio->devs[sl].devnum;
1494 rdev = rcu_dereference(conf->mirrors[d].rdev);
1495 if (rdev &&
1496 test_bit(In_sync, &rdev->flags)) {
1497 atomic_inc(&rdev->nr_pending);
1498 rcu_read_unlock();
1499 success = sync_page_io(rdev,
1500 r10_bio->devs[sl].addr +
1501 sect,
1502 s<<9,
1503 conf->tmppage, READ, false);
1504 rdev_dec_pending(rdev, mddev);
1505 rcu_read_lock();
1506 if (success)
1507 break;
1508 }
1509 sl++;
1510 if (sl == conf->copies)
1511 sl = 0;
1512 } while (!success && sl != r10_bio->read_slot);
1513 rcu_read_unlock();
1514
1515 if (!success) {
1516 /* Cannot read from anywhere -- bye bye array */
1517 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1518 md_error(mddev, conf->mirrors[dn].rdev);
1519 break;
1520 }
1521
1522 start = sl;
1523 /* write it back and re-read */
1524 rcu_read_lock();
1525 while (sl != r10_bio->read_slot) {
1526 char b[BDEVNAME_SIZE];
1527
1528 if (sl==0)
1529 sl = conf->copies;
1530 sl--;
1531 d = r10_bio->devs[sl].devnum;
1532 rdev = rcu_dereference(conf->mirrors[d].rdev);
1533 if (rdev &&
1534 test_bit(In_sync, &rdev->flags)) {
1535 atomic_inc(&rdev->nr_pending);
1536 rcu_read_unlock();
1537 atomic_add(s, &rdev->corrected_errors);
1538 if (sync_page_io(rdev,
1539 r10_bio->devs[sl].addr +
1540 sect,
1541 s<<9, conf->tmppage, WRITE, false)
1542 == 0) {
1543 /* Well, this device is dead */
1544 printk(KERN_NOTICE
1545 "md/raid10:%s: read correction "
1546 "write failed"
1547 " (%d sectors at %llu on %s)\n",
1548 mdname(mddev), s,
1549 (unsigned long long)(
1550 sect + rdev->data_offset),
1551 bdevname(rdev->bdev, b));
1552 printk(KERN_NOTICE "md/raid10:%s: %s: failing "
1553 "drive\n",
1554 mdname(mddev),
1555 bdevname(rdev->bdev, b));
1556 md_error(mddev, rdev);
1557 }
1558 rdev_dec_pending(rdev, mddev);
1559 rcu_read_lock();
1560 }
1561 }
1562 sl = start;
1563 while (sl != r10_bio->read_slot) {
1564
1565 if (sl==0)
1566 sl = conf->copies;
1567 sl--;
1568 d = r10_bio->devs[sl].devnum;
1569 rdev = rcu_dereference(conf->mirrors[d].rdev);
1570 if (rdev &&
1571 test_bit(In_sync, &rdev->flags)) {
1572 char b[BDEVNAME_SIZE];
1573 atomic_inc(&rdev->nr_pending);
1574 rcu_read_unlock();
1575 if (sync_page_io(rdev,
1576 r10_bio->devs[sl].addr +
1577 sect,
1578 s<<9, conf->tmppage,
1579 READ, false) == 0) {
1580 /* Well, this device is dead */
1581 printk(KERN_NOTICE
1582 "md/raid10:%s: unable to read back "
1583 "corrected sectors"
1584 " (%d sectors at %llu on %s)\n",
1585 mdname(mddev), s,
1586 (unsigned long long)(
1587 sect + rdev->data_offset),
1588 bdevname(rdev->bdev, b));
1589 printk(KERN_NOTICE "md/raid10:%s: %s: failing drive\n",
1590 mdname(mddev),
1591 bdevname(rdev->bdev, b));
1592
1593 md_error(mddev, rdev);
1594 } else {
1595 printk(KERN_INFO
1596 "md/raid10:%s: read error corrected"
1597 " (%d sectors at %llu on %s)\n",
1598 mdname(mddev), s,
1599 (unsigned long long)(
1600 sect + rdev->data_offset),
1601 bdevname(rdev->bdev, b));
1602 }
1603
1604 rdev_dec_pending(rdev, mddev);
1605 rcu_read_lock();
1606 }
1607 }
1608 rcu_read_unlock();
1609
1610 sectors -= s;
1611 sect += s;
1612 }
1613 }
1614
1615 static void raid10d(mddev_t *mddev)
1616 {
1617 r10bio_t *r10_bio;
1618 struct bio *bio;
1619 unsigned long flags;
1620 conf_t *conf = mddev->private;
1621 struct list_head *head = &conf->retry_list;
1622 mdk_rdev_t *rdev;
1623 struct blk_plug plug;
1624
1625 md_check_recovery(mddev);
1626
1627 blk_start_plug(&plug);
1628 for (;;) {
1629 char b[BDEVNAME_SIZE];
1630
1631 flush_pending_writes(conf);
1632
1633 spin_lock_irqsave(&conf->device_lock, flags);
1634 if (list_empty(head)) {
1635 spin_unlock_irqrestore(&conf->device_lock, flags);
1636 break;
1637 }
1638 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1639 list_del(head->prev);
1640 conf->nr_queued--;
1641 spin_unlock_irqrestore(&conf->device_lock, flags);
1642
1643 mddev = r10_bio->mddev;
1644 conf = mddev->private;
1645 if (test_bit(R10BIO_IsSync, &r10_bio->state))
1646 sync_request_write(mddev, r10_bio);
1647 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
1648 recovery_request_write(mddev, r10_bio);
1649 else {
1650 int slot = r10_bio->read_slot;
1651 int mirror = r10_bio->devs[slot].devnum;
1652 /* we got a read error. Maybe the drive is bad. Maybe just
1653 * the block and we can fix it.
1654 * We freeze all other IO, and try reading the block from
1655 * other devices. When we find one, we re-write
1656 * and check it that fixes the read error.
1657 * This is all done synchronously while the array is
1658 * frozen.
1659 */
1660 if (mddev->ro == 0) {
1661 freeze_array(conf);
1662 fix_read_error(conf, mddev, r10_bio);
1663 unfreeze_array(conf);
1664 }
1665 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
1666
1667 bio = r10_bio->devs[slot].bio;
1668 r10_bio->devs[slot].bio =
1669 mddev->ro ? IO_BLOCKED : NULL;
1670 mirror = read_balance(conf, r10_bio);
1671 if (mirror == -1) {
1672 printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
1673 " read error for block %llu\n",
1674 mdname(mddev),
1675 bdevname(bio->bi_bdev,b),
1676 (unsigned long long)r10_bio->sector);
1677 raid_end_bio_io(r10_bio);
1678 bio_put(bio);
1679 } else {
1680 const unsigned long do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
1681 bio_put(bio);
1682 slot = r10_bio->read_slot;
1683 rdev = conf->mirrors[mirror].rdev;
1684 printk_ratelimited(
1685 KERN_ERR
1686 "md/raid10:%s: %s: redirecting"
1687 "sector %llu to another mirror\n",
1688 mdname(mddev),
1689 bdevname(rdev->bdev, b),
1690 (unsigned long long)r10_bio->sector);
1691 bio = bio_clone_mddev(r10_bio->master_bio,
1692 GFP_NOIO, mddev);
1693 r10_bio->devs[slot].bio = bio;
1694 bio->bi_sector = r10_bio->devs[slot].addr
1695 + rdev->data_offset;
1696 bio->bi_bdev = rdev->bdev;
1697 bio->bi_rw = READ | do_sync;
1698 bio->bi_private = r10_bio;
1699 bio->bi_end_io = raid10_end_read_request;
1700 generic_make_request(bio);
1701 }
1702 }
1703 cond_resched();
1704 }
1705 blk_finish_plug(&plug);
1706 }
1707
1708
1709 static int init_resync(conf_t *conf)
1710 {
1711 int buffs;
1712
1713 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1714 BUG_ON(conf->r10buf_pool);
1715 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1716 if (!conf->r10buf_pool)
1717 return -ENOMEM;
1718 conf->next_resync = 0;
1719 return 0;
1720 }
1721
1722 /*
1723 * perform a "sync" on one "block"
1724 *
1725 * We need to make sure that no normal I/O request - particularly write
1726 * requests - conflict with active sync requests.
1727 *
1728 * This is achieved by tracking pending requests and a 'barrier' concept
1729 * that can be installed to exclude normal IO requests.
1730 *
1731 * Resync and recovery are handled very differently.
1732 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1733 *
1734 * For resync, we iterate over virtual addresses, read all copies,
1735 * and update if there are differences. If only one copy is live,
1736 * skip it.
1737 * For recovery, we iterate over physical addresses, read a good
1738 * value for each non-in_sync drive, and over-write.
1739 *
1740 * So, for recovery we may have several outstanding complex requests for a
1741 * given address, one for each out-of-sync device. We model this by allocating
1742 * a number of r10_bio structures, one for each out-of-sync device.
1743 * As we setup these structures, we collect all bio's together into a list
1744 * which we then process collectively to add pages, and then process again
1745 * to pass to generic_make_request.
1746 *
1747 * The r10_bio structures are linked using a borrowed master_bio pointer.
1748 * This link is counted in ->remaining. When the r10_bio that points to NULL
1749 * has its remaining count decremented to 0, the whole complex operation
1750 * is complete.
1751 *
1752 */
1753
1754 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr,
1755 int *skipped, int go_faster)
1756 {
1757 conf_t *conf = mddev->private;
1758 r10bio_t *r10_bio;
1759 struct bio *biolist = NULL, *bio;
1760 sector_t max_sector, nr_sectors;
1761 int i;
1762 int max_sync;
1763 sector_t sync_blocks;
1764
1765 sector_t sectors_skipped = 0;
1766 int chunks_skipped = 0;
1767
1768 if (!conf->r10buf_pool)
1769 if (init_resync(conf))
1770 return 0;
1771
1772 skipped:
1773 max_sector = mddev->dev_sectors;
1774 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1775 max_sector = mddev->resync_max_sectors;
1776 if (sector_nr >= max_sector) {
1777 /* If we aborted, we need to abort the
1778 * sync on the 'current' bitmap chucks (there can
1779 * be several when recovering multiple devices).
1780 * as we may have started syncing it but not finished.
1781 * We can find the current address in
1782 * mddev->curr_resync, but for recovery,
1783 * we need to convert that to several
1784 * virtual addresses.
1785 */
1786 if (mddev->curr_resync < max_sector) { /* aborted */
1787 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1788 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1789 &sync_blocks, 1);
1790 else for (i=0; i<conf->raid_disks; i++) {
1791 sector_t sect =
1792 raid10_find_virt(conf, mddev->curr_resync, i);
1793 bitmap_end_sync(mddev->bitmap, sect,
1794 &sync_blocks, 1);
1795 }
1796 } else /* completed sync */
1797 conf->fullsync = 0;
1798
1799 bitmap_close_sync(mddev->bitmap);
1800 close_sync(conf);
1801 *skipped = 1;
1802 return sectors_skipped;
1803 }
1804 if (chunks_skipped >= conf->raid_disks) {
1805 /* if there has been nothing to do on any drive,
1806 * then there is nothing to do at all..
1807 */
1808 *skipped = 1;
1809 return (max_sector - sector_nr) + sectors_skipped;
1810 }
1811
1812 if (max_sector > mddev->resync_max)
1813 max_sector = mddev->resync_max; /* Don't do IO beyond here */
1814
1815 /* make sure whole request will fit in a chunk - if chunks
1816 * are meaningful
1817 */
1818 if (conf->near_copies < conf->raid_disks &&
1819 max_sector > (sector_nr | conf->chunk_mask))
1820 max_sector = (sector_nr | conf->chunk_mask) + 1;
1821 /*
1822 * If there is non-resync activity waiting for us then
1823 * put in a delay to throttle resync.
1824 */
1825 if (!go_faster && conf->nr_waiting)
1826 msleep_interruptible(1000);
1827
1828 /* Again, very different code for resync and recovery.
1829 * Both must result in an r10bio with a list of bios that
1830 * have bi_end_io, bi_sector, bi_bdev set,
1831 * and bi_private set to the r10bio.
1832 * For recovery, we may actually create several r10bios
1833 * with 2 bios in each, that correspond to the bios in the main one.
1834 * In this case, the subordinate r10bios link back through a
1835 * borrowed master_bio pointer, and the counter in the master
1836 * includes a ref from each subordinate.
1837 */
1838 /* First, we decide what to do and set ->bi_end_io
1839 * To end_sync_read if we want to read, and
1840 * end_sync_write if we will want to write.
1841 */
1842
1843 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1844 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1845 /* recovery... the complicated one */
1846 int j, k;
1847 r10_bio = NULL;
1848
1849 for (i=0 ; i<conf->raid_disks; i++) {
1850 int still_degraded;
1851 r10bio_t *rb2;
1852 sector_t sect;
1853 int must_sync;
1854
1855 if (conf->mirrors[i].rdev == NULL ||
1856 test_bit(In_sync, &conf->mirrors[i].rdev->flags))
1857 continue;
1858
1859 still_degraded = 0;
1860 /* want to reconstruct this device */
1861 rb2 = r10_bio;
1862 sect = raid10_find_virt(conf, sector_nr, i);
1863 /* Unless we are doing a full sync, we only need
1864 * to recover the block if it is set in the bitmap
1865 */
1866 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1867 &sync_blocks, 1);
1868 if (sync_blocks < max_sync)
1869 max_sync = sync_blocks;
1870 if (!must_sync &&
1871 !conf->fullsync) {
1872 /* yep, skip the sync_blocks here, but don't assume
1873 * that there will never be anything to do here
1874 */
1875 chunks_skipped = -1;
1876 continue;
1877 }
1878
1879 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1880 raise_barrier(conf, rb2 != NULL);
1881 atomic_set(&r10_bio->remaining, 0);
1882
1883 r10_bio->master_bio = (struct bio*)rb2;
1884 if (rb2)
1885 atomic_inc(&rb2->remaining);
1886 r10_bio->mddev = mddev;
1887 set_bit(R10BIO_IsRecover, &r10_bio->state);
1888 r10_bio->sector = sect;
1889
1890 raid10_find_phys(conf, r10_bio);
1891
1892 /* Need to check if the array will still be
1893 * degraded
1894 */
1895 for (j=0; j<conf->raid_disks; j++)
1896 if (conf->mirrors[j].rdev == NULL ||
1897 test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
1898 still_degraded = 1;
1899 break;
1900 }
1901
1902 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1903 &sync_blocks, still_degraded);
1904
1905 for (j=0; j<conf->copies;j++) {
1906 int d = r10_bio->devs[j].devnum;
1907 if (!conf->mirrors[d].rdev ||
1908 !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
1909 continue;
1910 /* This is where we read from */
1911 bio = r10_bio->devs[0].bio;
1912 bio->bi_next = biolist;
1913 biolist = bio;
1914 bio->bi_private = r10_bio;
1915 bio->bi_end_io = end_sync_read;
1916 bio->bi_rw = READ;
1917 bio->bi_sector = r10_bio->devs[j].addr +
1918 conf->mirrors[d].rdev->data_offset;
1919 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1920 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1921 atomic_inc(&r10_bio->remaining);
1922 /* and we write to 'i' */
1923
1924 for (k=0; k<conf->copies; k++)
1925 if (r10_bio->devs[k].devnum == i)
1926 break;
1927 BUG_ON(k == conf->copies);
1928 bio = r10_bio->devs[1].bio;
1929 bio->bi_next = biolist;
1930 biolist = bio;
1931 bio->bi_private = r10_bio;
1932 bio->bi_end_io = end_sync_write;
1933 bio->bi_rw = WRITE;
1934 bio->bi_sector = r10_bio->devs[k].addr +
1935 conf->mirrors[i].rdev->data_offset;
1936 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1937
1938 r10_bio->devs[0].devnum = d;
1939 r10_bio->devs[1].devnum = i;
1940
1941 break;
1942 }
1943 if (j == conf->copies) {
1944 /* Cannot recover, so abort the recovery */
1945 put_buf(r10_bio);
1946 if (rb2)
1947 atomic_dec(&rb2->remaining);
1948 r10_bio = rb2;
1949 if (!test_and_set_bit(MD_RECOVERY_INTR,
1950 &mddev->recovery))
1951 printk(KERN_INFO "md/raid10:%s: insufficient "
1952 "working devices for recovery.\n",
1953 mdname(mddev));
1954 break;
1955 }
1956 }
1957 if (biolist == NULL) {
1958 while (r10_bio) {
1959 r10bio_t *rb2 = r10_bio;
1960 r10_bio = (r10bio_t*) rb2->master_bio;
1961 rb2->master_bio = NULL;
1962 put_buf(rb2);
1963 }
1964 goto giveup;
1965 }
1966 } else {
1967 /* resync. Schedule a read for every block at this virt offset */
1968 int count = 0;
1969
1970 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
1971
1972 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1973 &sync_blocks, mddev->degraded) &&
1974 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
1975 &mddev->recovery)) {
1976 /* We can skip this block */
1977 *skipped = 1;
1978 return sync_blocks + sectors_skipped;
1979 }
1980 if (sync_blocks < max_sync)
1981 max_sync = sync_blocks;
1982 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1983
1984 r10_bio->mddev = mddev;
1985 atomic_set(&r10_bio->remaining, 0);
1986 raise_barrier(conf, 0);
1987 conf->next_resync = sector_nr;
1988
1989 r10_bio->master_bio = NULL;
1990 r10_bio->sector = sector_nr;
1991 set_bit(R10BIO_IsSync, &r10_bio->state);
1992 raid10_find_phys(conf, r10_bio);
1993 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1994
1995 for (i=0; i<conf->copies; i++) {
1996 int d = r10_bio->devs[i].devnum;
1997 bio = r10_bio->devs[i].bio;
1998 bio->bi_end_io = NULL;
1999 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2000 if (conf->mirrors[d].rdev == NULL ||
2001 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
2002 continue;
2003 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2004 atomic_inc(&r10_bio->remaining);
2005 bio->bi_next = biolist;
2006 biolist = bio;
2007 bio->bi_private = r10_bio;
2008 bio->bi_end_io = end_sync_read;
2009 bio->bi_rw = READ;
2010 bio->bi_sector = r10_bio->devs[i].addr +
2011 conf->mirrors[d].rdev->data_offset;
2012 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
2013 count++;
2014 }
2015
2016 if (count < 2) {
2017 for (i=0; i<conf->copies; i++) {
2018 int d = r10_bio->devs[i].devnum;
2019 if (r10_bio->devs[i].bio->bi_end_io)
2020 rdev_dec_pending(conf->mirrors[d].rdev,
2021 mddev);
2022 }
2023 put_buf(r10_bio);
2024 biolist = NULL;
2025 goto giveup;
2026 }
2027 }
2028
2029 for (bio = biolist; bio ; bio=bio->bi_next) {
2030
2031 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
2032 if (bio->bi_end_io)
2033 bio->bi_flags |= 1 << BIO_UPTODATE;
2034 bio->bi_vcnt = 0;
2035 bio->bi_idx = 0;
2036 bio->bi_phys_segments = 0;
2037 bio->bi_size = 0;
2038 }
2039
2040 nr_sectors = 0;
2041 if (sector_nr + max_sync < max_sector)
2042 max_sector = sector_nr + max_sync;
2043 do {
2044 struct page *page;
2045 int len = PAGE_SIZE;
2046 if (sector_nr + (len>>9) > max_sector)
2047 len = (max_sector - sector_nr) << 9;
2048 if (len == 0)
2049 break;
2050 for (bio= biolist ; bio ; bio=bio->bi_next) {
2051 struct bio *bio2;
2052 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2053 if (bio_add_page(bio, page, len, 0))
2054 continue;
2055
2056 /* stop here */
2057 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2058 for (bio2 = biolist;
2059 bio2 && bio2 != bio;
2060 bio2 = bio2->bi_next) {
2061 /* remove last page from this bio */
2062 bio2->bi_vcnt--;
2063 bio2->bi_size -= len;
2064 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
2065 }
2066 goto bio_full;
2067 }
2068 nr_sectors += len>>9;
2069 sector_nr += len>>9;
2070 } while (biolist->bi_vcnt < RESYNC_PAGES);
2071 bio_full:
2072 r10_bio->sectors = nr_sectors;
2073
2074 while (biolist) {
2075 bio = biolist;
2076 biolist = biolist->bi_next;
2077
2078 bio->bi_next = NULL;
2079 r10_bio = bio->bi_private;
2080 r10_bio->sectors = nr_sectors;
2081
2082 if (bio->bi_end_io == end_sync_read) {
2083 md_sync_acct(bio->bi_bdev, nr_sectors);
2084 generic_make_request(bio);
2085 }
2086 }
2087
2088 if (sectors_skipped)
2089 /* pretend they weren't skipped, it makes
2090 * no important difference in this case
2091 */
2092 md_done_sync(mddev, sectors_skipped, 1);
2093
2094 return sectors_skipped + nr_sectors;
2095 giveup:
2096 /* There is nowhere to write, so all non-sync
2097 * drives must be failed, so try the next chunk...
2098 */
2099 if (sector_nr + max_sync < max_sector)
2100 max_sector = sector_nr + max_sync;
2101
2102 sectors_skipped += (max_sector - sector_nr);
2103 chunks_skipped ++;
2104 sector_nr = max_sector;
2105 goto skipped;
2106 }
2107
2108 static sector_t
2109 raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks)
2110 {
2111 sector_t size;
2112 conf_t *conf = mddev->private;
2113
2114 if (!raid_disks)
2115 raid_disks = conf->raid_disks;
2116 if (!sectors)
2117 sectors = conf->dev_sectors;
2118
2119 size = sectors >> conf->chunk_shift;
2120 sector_div(size, conf->far_copies);
2121 size = size * raid_disks;
2122 sector_div(size, conf->near_copies);
2123
2124 return size << conf->chunk_shift;
2125 }
2126
2127
2128 static conf_t *setup_conf(mddev_t *mddev)
2129 {
2130 conf_t *conf = NULL;
2131 int nc, fc, fo;
2132 sector_t stride, size;
2133 int err = -EINVAL;
2134
2135 if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
2136 !is_power_of_2(mddev->new_chunk_sectors)) {
2137 printk(KERN_ERR "md/raid10:%s: chunk size must be "
2138 "at least PAGE_SIZE(%ld) and be a power of 2.\n",
2139 mdname(mddev), PAGE_SIZE);
2140 goto out;
2141 }
2142
2143 nc = mddev->new_layout & 255;
2144 fc = (mddev->new_layout >> 8) & 255;
2145 fo = mddev->new_layout & (1<<16);
2146
2147 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
2148 (mddev->new_layout >> 17)) {
2149 printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
2150 mdname(mddev), mddev->new_layout);
2151 goto out;
2152 }
2153
2154 err = -ENOMEM;
2155 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
2156 if (!conf)
2157 goto out;
2158
2159 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
2160 GFP_KERNEL);
2161 if (!conf->mirrors)
2162 goto out;
2163
2164 conf->tmppage = alloc_page(GFP_KERNEL);
2165 if (!conf->tmppage)
2166 goto out;
2167
2168
2169 conf->raid_disks = mddev->raid_disks;
2170 conf->near_copies = nc;
2171 conf->far_copies = fc;
2172 conf->copies = nc*fc;
2173 conf->far_offset = fo;
2174 conf->chunk_mask = mddev->new_chunk_sectors - 1;
2175 conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
2176
2177 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2178 r10bio_pool_free, conf);
2179 if (!conf->r10bio_pool)
2180 goto out;
2181
2182 size = mddev->dev_sectors >> conf->chunk_shift;
2183 sector_div(size, fc);
2184 size = size * conf->raid_disks;
2185 sector_div(size, nc);
2186 /* 'size' is now the number of chunks in the array */
2187 /* calculate "used chunks per device" in 'stride' */
2188 stride = size * conf->copies;
2189
2190 /* We need to round up when dividing by raid_disks to
2191 * get the stride size.
2192 */
2193 stride += conf->raid_disks - 1;
2194 sector_div(stride, conf->raid_disks);
2195
2196 conf->dev_sectors = stride << conf->chunk_shift;
2197
2198 if (fo)
2199 stride = 1;
2200 else
2201 sector_div(stride, fc);
2202 conf->stride = stride << conf->chunk_shift;
2203
2204
2205 spin_lock_init(&conf->device_lock);
2206 INIT_LIST_HEAD(&conf->retry_list);
2207
2208 spin_lock_init(&conf->resync_lock);
2209 init_waitqueue_head(&conf->wait_barrier);
2210
2211 conf->thread = md_register_thread(raid10d, mddev, NULL);
2212 if (!conf->thread)
2213 goto out;
2214
2215 conf->mddev = mddev;
2216 return conf;
2217
2218 out:
2219 printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
2220 mdname(mddev));
2221 if (conf) {
2222 if (conf->r10bio_pool)
2223 mempool_destroy(conf->r10bio_pool);
2224 kfree(conf->mirrors);
2225 safe_put_page(conf->tmppage);
2226 kfree(conf);
2227 }
2228 return ERR_PTR(err);
2229 }
2230
2231 static int run(mddev_t *mddev)
2232 {
2233 conf_t *conf;
2234 int i, disk_idx, chunk_size;
2235 mirror_info_t *disk;
2236 mdk_rdev_t *rdev;
2237 sector_t size;
2238
2239 /*
2240 * copy the already verified devices into our private RAID10
2241 * bookkeeping area. [whatever we allocate in run(),
2242 * should be freed in stop()]
2243 */
2244
2245 if (mddev->private == NULL) {
2246 conf = setup_conf(mddev);
2247 if (IS_ERR(conf))
2248 return PTR_ERR(conf);
2249 mddev->private = conf;
2250 }
2251 conf = mddev->private;
2252 if (!conf)
2253 goto out;
2254
2255 mddev->thread = conf->thread;
2256 conf->thread = NULL;
2257
2258 chunk_size = mddev->chunk_sectors << 9;
2259 blk_queue_io_min(mddev->queue, chunk_size);
2260 if (conf->raid_disks % conf->near_copies)
2261 blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
2262 else
2263 blk_queue_io_opt(mddev->queue, chunk_size *
2264 (conf->raid_disks / conf->near_copies));
2265
2266 list_for_each_entry(rdev, &mddev->disks, same_set) {
2267 disk_idx = rdev->raid_disk;
2268 if (disk_idx >= conf->raid_disks
2269 || disk_idx < 0)
2270 continue;
2271 disk = conf->mirrors + disk_idx;
2272
2273 disk->rdev = rdev;
2274 disk_stack_limits(mddev->gendisk, rdev->bdev,
2275 rdev->data_offset << 9);
2276 /* as we don't honour merge_bvec_fn, we must never risk
2277 * violating it, so limit max_segments to 1 lying
2278 * within a single page.
2279 */
2280 if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
2281 blk_queue_max_segments(mddev->queue, 1);
2282 blk_queue_segment_boundary(mddev->queue,
2283 PAGE_CACHE_SIZE - 1);
2284 }
2285
2286 disk->head_position = 0;
2287 }
2288 /* need to check that every block has at least one working mirror */
2289 if (!enough(conf)) {
2290 printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
2291 mdname(mddev));
2292 goto out_free_conf;
2293 }
2294
2295 mddev->degraded = 0;
2296 for (i = 0; i < conf->raid_disks; i++) {
2297
2298 disk = conf->mirrors + i;
2299
2300 if (!disk->rdev ||
2301 !test_bit(In_sync, &disk->rdev->flags)) {
2302 disk->head_position = 0;
2303 mddev->degraded++;
2304 if (disk->rdev)
2305 conf->fullsync = 1;
2306 }
2307 }
2308
2309 if (mddev->recovery_cp != MaxSector)
2310 printk(KERN_NOTICE "md/raid10:%s: not clean"
2311 " -- starting background reconstruction\n",
2312 mdname(mddev));
2313 printk(KERN_INFO
2314 "md/raid10:%s: active with %d out of %d devices\n",
2315 mdname(mddev), conf->raid_disks - mddev->degraded,
2316 conf->raid_disks);
2317 /*
2318 * Ok, everything is just fine now
2319 */
2320 mddev->dev_sectors = conf->dev_sectors;
2321 size = raid10_size(mddev, 0, 0);
2322 md_set_array_sectors(mddev, size);
2323 mddev->resync_max_sectors = size;
2324
2325 mddev->queue->backing_dev_info.congested_fn = raid10_congested;
2326 mddev->queue->backing_dev_info.congested_data = mddev;
2327
2328 /* Calculate max read-ahead size.
2329 * We need to readahead at least twice a whole stripe....
2330 * maybe...
2331 */
2332 {
2333 int stripe = conf->raid_disks *
2334 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
2335 stripe /= conf->near_copies;
2336 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2337 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2338 }
2339
2340 if (conf->near_copies < conf->raid_disks)
2341 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2342
2343 if (md_integrity_register(mddev))
2344 goto out_free_conf;
2345
2346 return 0;
2347
2348 out_free_conf:
2349 md_unregister_thread(mddev->thread);
2350 if (conf->r10bio_pool)
2351 mempool_destroy(conf->r10bio_pool);
2352 safe_put_page(conf->tmppage);
2353 kfree(conf->mirrors);
2354 kfree(conf);
2355 mddev->private = NULL;
2356 out:
2357 return -EIO;
2358 }
2359
2360 static int stop(mddev_t *mddev)
2361 {
2362 conf_t *conf = mddev->private;
2363
2364 raise_barrier(conf, 0);
2365 lower_barrier(conf);
2366
2367 md_unregister_thread(mddev->thread);
2368 mddev->thread = NULL;
2369 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2370 if (conf->r10bio_pool)
2371 mempool_destroy(conf->r10bio_pool);
2372 kfree(conf->mirrors);
2373 kfree(conf);
2374 mddev->private = NULL;
2375 return 0;
2376 }
2377
2378 static void raid10_quiesce(mddev_t *mddev, int state)
2379 {
2380 conf_t *conf = mddev->private;
2381
2382 switch(state) {
2383 case 1:
2384 raise_barrier(conf, 0);
2385 break;
2386 case 0:
2387 lower_barrier(conf);
2388 break;
2389 }
2390 }
2391
2392 static void *raid10_takeover_raid0(mddev_t *mddev)
2393 {
2394 mdk_rdev_t *rdev;
2395 conf_t *conf;
2396
2397 if (mddev->degraded > 0) {
2398 printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
2399 mdname(mddev));
2400 return ERR_PTR(-EINVAL);
2401 }
2402
2403 /* Set new parameters */
2404 mddev->new_level = 10;
2405 /* new layout: far_copies = 1, near_copies = 2 */
2406 mddev->new_layout = (1<<8) + 2;
2407 mddev->new_chunk_sectors = mddev->chunk_sectors;
2408 mddev->delta_disks = mddev->raid_disks;
2409 mddev->raid_disks *= 2;
2410 /* make sure it will be not marked as dirty */
2411 mddev->recovery_cp = MaxSector;
2412
2413 conf = setup_conf(mddev);
2414 if (!IS_ERR(conf)) {
2415 list_for_each_entry(rdev, &mddev->disks, same_set)
2416 if (rdev->raid_disk >= 0)
2417 rdev->new_raid_disk = rdev->raid_disk * 2;
2418 conf->barrier = 1;
2419 }
2420
2421 return conf;
2422 }
2423
2424 static void *raid10_takeover(mddev_t *mddev)
2425 {
2426 struct raid0_private_data *raid0_priv;
2427
2428 /* raid10 can take over:
2429 * raid0 - providing it has only two drives
2430 */
2431 if (mddev->level == 0) {
2432 /* for raid0 takeover only one zone is supported */
2433 raid0_priv = mddev->private;
2434 if (raid0_priv->nr_strip_zones > 1) {
2435 printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
2436 " with more than one zone.\n",
2437 mdname(mddev));
2438 return ERR_PTR(-EINVAL);
2439 }
2440 return raid10_takeover_raid0(mddev);
2441 }
2442 return ERR_PTR(-EINVAL);
2443 }
2444
2445 static struct mdk_personality raid10_personality =
2446 {
2447 .name = "raid10",
2448 .level = 10,
2449 .owner = THIS_MODULE,
2450 .make_request = make_request,
2451 .run = run,
2452 .stop = stop,
2453 .status = status,
2454 .error_handler = error,
2455 .hot_add_disk = raid10_add_disk,
2456 .hot_remove_disk= raid10_remove_disk,
2457 .spare_active = raid10_spare_active,
2458 .sync_request = sync_request,
2459 .quiesce = raid10_quiesce,
2460 .size = raid10_size,
2461 .takeover = raid10_takeover,
2462 };
2463
2464 static int __init raid_init(void)
2465 {
2466 return register_md_personality(&raid10_personality);
2467 }
2468
2469 static void raid_exit(void)
2470 {
2471 unregister_md_personality(&raid10_personality);
2472 }
2473
2474 module_init(raid_init);
2475 module_exit(raid_exit);
2476 MODULE_LICENSE("GPL");
2477 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
2478 MODULE_ALIAS("md-personality-9"); /* RAID10 */
2479 MODULE_ALIAS("md-raid10");
2480 MODULE_ALIAS("md-level-10");
This page took 0.18285 seconds and 5 git commands to generate.