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