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