[PATCH] md: Remove working_disks from raid1 state data
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / md / raid10.c
CommitLineData
1da177e4
LT
1/*
2 * raid10.c : Multiple Devices driver for Linux
3 *
4 * Copyright (C) 2000-2004 Neil Brown
5 *
6 * RAID-10 support for md.
7 *
8 * Base on code in raid1.c. See raid1.c for futher copyright information.
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
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 {
432 while (sector > conf->stride) {
433 sector -= conf->stride;
434 if (dev < conf->near_copies)
435 dev += conf->raid_disks - conf->near_copies;
436 else
437 dev -= conf->near_copies;
438 }
439 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 */
456static int raid10_mergeable_bvec(request_queue_t *q, struct bio *bio,
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)) {
1da177e4
LT
598 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
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
613static void raid10_unplug(request_queue_t *q)
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
621static int raid10_issue_flush(request_queue_t *q, struct gendisk *disk,
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
LT
632 struct block_device *bdev = rdev->bdev;
633 request_queue_t *r_queue = bdev_get_queue(bdev);
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
0a27ec96
N
651/* Barriers....
652 * Sometimes we need to suspend IO while we do something else,
653 * either some resync/recovery, or reconfigure the array.
654 * To do this we raise a 'barrier'.
655 * The 'barrier' is a counter that can be raised multiple times
656 * to count how many activities are happening which preclude
657 * normal IO.
658 * We can only raise the barrier if there is no pending IO.
659 * i.e. if nr_pending == 0.
660 * We choose only to raise the barrier if no-one is waiting for the
661 * barrier to go down. This means that as soon as an IO request
662 * is ready, no other operations which require a barrier will start
663 * until the IO request has had a chance.
664 *
665 * So: regular IO calls 'wait_barrier'. When that returns there
666 * is no backgroup IO happening, It must arrange to call
667 * allow_barrier when it has finished its IO.
668 * backgroup IO calls must call raise_barrier. Once that returns
669 * there is no normal IO happeing. It must arrange to call
670 * lower_barrier when the particular background IO completes.
1da177e4
LT
671 */
672#define RESYNC_DEPTH 32
673
6cce3b23 674static void raise_barrier(conf_t *conf, int force)
1da177e4 675{
6cce3b23 676 BUG_ON(force && !conf->barrier);
1da177e4 677 spin_lock_irq(&conf->resync_lock);
0a27ec96 678
6cce3b23
N
679 /* Wait until no block IO is waiting (unless 'force') */
680 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
0a27ec96
N
681 conf->resync_lock,
682 raid10_unplug(conf->mddev->queue));
683
684 /* block any new IO from starting */
685 conf->barrier++;
686
687 /* No wait for all pending IO to complete */
688 wait_event_lock_irq(conf->wait_barrier,
689 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
690 conf->resync_lock,
691 raid10_unplug(conf->mddev->queue));
692
693 spin_unlock_irq(&conf->resync_lock);
694}
695
696static void lower_barrier(conf_t *conf)
697{
698 unsigned long flags;
699 spin_lock_irqsave(&conf->resync_lock, flags);
700 conf->barrier--;
701 spin_unlock_irqrestore(&conf->resync_lock, flags);
702 wake_up(&conf->wait_barrier);
703}
704
705static void wait_barrier(conf_t *conf)
706{
707 spin_lock_irq(&conf->resync_lock);
708 if (conf->barrier) {
709 conf->nr_waiting++;
710 wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
711 conf->resync_lock,
712 raid10_unplug(conf->mddev->queue));
713 conf->nr_waiting--;
1da177e4 714 }
0a27ec96 715 conf->nr_pending++;
1da177e4
LT
716 spin_unlock_irq(&conf->resync_lock);
717}
718
0a27ec96
N
719static void allow_barrier(conf_t *conf)
720{
721 unsigned long flags;
722 spin_lock_irqsave(&conf->resync_lock, flags);
723 conf->nr_pending--;
724 spin_unlock_irqrestore(&conf->resync_lock, flags);
725 wake_up(&conf->wait_barrier);
726}
727
4443ae10
N
728static void freeze_array(conf_t *conf)
729{
730 /* stop syncio and normal IO and wait for everything to
f188593e 731 * go quiet.
4443ae10
N
732 * We increment barrier and nr_waiting, and then
733 * wait until barrier+nr_pending match nr_queued+2
734 */
735 spin_lock_irq(&conf->resync_lock);
736 conf->barrier++;
737 conf->nr_waiting++;
738 wait_event_lock_irq(conf->wait_barrier,
739 conf->barrier+conf->nr_pending == conf->nr_queued+2,
740 conf->resync_lock,
741 raid10_unplug(conf->mddev->queue));
742 spin_unlock_irq(&conf->resync_lock);
743}
744
745static void unfreeze_array(conf_t *conf)
746{
747 /* reverse the effect of the freeze */
748 spin_lock_irq(&conf->resync_lock);
749 conf->barrier--;
750 conf->nr_waiting--;
751 wake_up(&conf->wait_barrier);
752 spin_unlock_irq(&conf->resync_lock);
753}
754
1da177e4
LT
755static int make_request(request_queue_t *q, struct bio * bio)
756{
757 mddev_t *mddev = q->queuedata;
758 conf_t *conf = mddev_to_conf(mddev);
759 mirror_info_t *mirror;
760 r10bio_t *r10_bio;
761 struct bio *read_bio;
762 int i;
763 int chunk_sects = conf->chunk_mask + 1;
a362357b 764 const int rw = bio_data_dir(bio);
6cce3b23
N
765 struct bio_list bl;
766 unsigned long flags;
1da177e4 767
e5dcdd80
N
768 if (unlikely(bio_barrier(bio))) {
769 bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
770 return 0;
771 }
772
1da177e4
LT
773 /* If this request crosses a chunk boundary, we need to
774 * split it. This will only happen for 1 PAGE (or less) requests.
775 */
776 if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
777 > chunk_sects &&
778 conf->near_copies < conf->raid_disks)) {
779 struct bio_pair *bp;
780 /* Sanity check -- queue functions should prevent this happening */
781 if (bio->bi_vcnt != 1 ||
782 bio->bi_idx != 0)
783 goto bad_map;
784 /* This is a one page bio that upper layers
785 * refuse to split for us, so we need to split it.
786 */
787 bp = bio_split(bio, bio_split_pool,
788 chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
789 if (make_request(q, &bp->bio1))
790 generic_make_request(&bp->bio1);
791 if (make_request(q, &bp->bio2))
792 generic_make_request(&bp->bio2);
793
794 bio_pair_release(bp);
795 return 0;
796 bad_map:
797 printk("raid10_make_request bug: can't convert block across chunks"
798 " or bigger than %dk %llu %d\n", chunk_sects/2,
799 (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
800
801 bio_io_error(bio, bio->bi_size);
802 return 0;
803 }
804
3d310eb7 805 md_write_start(mddev, bio);
06d91a5f 806
1da177e4
LT
807 /*
808 * Register the new request and wait if the reconstruction
809 * thread has put up a bar for new requests.
810 * Continue immediately if no resync is active currently.
811 */
0a27ec96 812 wait_barrier(conf);
1da177e4 813
a362357b
JA
814 disk_stat_inc(mddev->gendisk, ios[rw]);
815 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
1da177e4
LT
816
817 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
818
819 r10_bio->master_bio = bio;
820 r10_bio->sectors = bio->bi_size >> 9;
821
822 r10_bio->mddev = mddev;
823 r10_bio->sector = bio->bi_sector;
6cce3b23 824 r10_bio->state = 0;
1da177e4 825
a362357b 826 if (rw == READ) {
1da177e4
LT
827 /*
828 * read balancing logic:
829 */
830 int disk = read_balance(conf, r10_bio);
831 int slot = r10_bio->read_slot;
832 if (disk < 0) {
833 raid_end_bio_io(r10_bio);
834 return 0;
835 }
836 mirror = conf->mirrors + disk;
837
838 read_bio = bio_clone(bio, GFP_NOIO);
839
840 r10_bio->devs[slot].bio = read_bio;
841
842 read_bio->bi_sector = r10_bio->devs[slot].addr +
843 mirror->rdev->data_offset;
844 read_bio->bi_bdev = mirror->rdev->bdev;
845 read_bio->bi_end_io = raid10_end_read_request;
846 read_bio->bi_rw = READ;
847 read_bio->bi_private = r10_bio;
848
849 generic_make_request(read_bio);
850 return 0;
851 }
852
853 /*
854 * WRITE:
855 */
856 /* first select target devices under spinlock and
857 * inc refcount on their rdev. Record them by setting
858 * bios[x] to bio
859 */
860 raid10_find_phys(conf, r10_bio);
861 rcu_read_lock();
862 for (i = 0; i < conf->copies; i++) {
863 int d = r10_bio->devs[i].devnum;
d6065f7b
SW
864 mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
865 if (rdev &&
b2d444d7 866 !test_bit(Faulty, &rdev->flags)) {
d6065f7b 867 atomic_inc(&rdev->nr_pending);
1da177e4 868 r10_bio->devs[i].bio = bio;
6cce3b23 869 } else {
1da177e4 870 r10_bio->devs[i].bio = NULL;
6cce3b23
N
871 set_bit(R10BIO_Degraded, &r10_bio->state);
872 }
1da177e4
LT
873 }
874 rcu_read_unlock();
875
6cce3b23 876 atomic_set(&r10_bio->remaining, 0);
06d91a5f 877
6cce3b23 878 bio_list_init(&bl);
1da177e4
LT
879 for (i = 0; i < conf->copies; i++) {
880 struct bio *mbio;
881 int d = r10_bio->devs[i].devnum;
882 if (!r10_bio->devs[i].bio)
883 continue;
884
885 mbio = bio_clone(bio, GFP_NOIO);
886 r10_bio->devs[i].bio = mbio;
887
888 mbio->bi_sector = r10_bio->devs[i].addr+
889 conf->mirrors[d].rdev->data_offset;
890 mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
891 mbio->bi_end_io = raid10_end_write_request;
892 mbio->bi_rw = WRITE;
893 mbio->bi_private = r10_bio;
894
895 atomic_inc(&r10_bio->remaining);
6cce3b23 896 bio_list_add(&bl, mbio);
1da177e4
LT
897 }
898
6cce3b23
N
899 bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
900 spin_lock_irqsave(&conf->device_lock, flags);
901 bio_list_merge(&conf->pending_bio_list, &bl);
902 blk_plug_device(mddev->queue);
903 spin_unlock_irqrestore(&conf->device_lock, flags);
1da177e4
LT
904
905 return 0;
906}
907
908static void status(struct seq_file *seq, mddev_t *mddev)
909{
910 conf_t *conf = mddev_to_conf(mddev);
911 int i;
912
913 if (conf->near_copies < conf->raid_disks)
914 seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
915 if (conf->near_copies > 1)
916 seq_printf(seq, " %d near-copies", conf->near_copies);
c93983bf
N
917 if (conf->far_copies > 1) {
918 if (conf->far_offset)
919 seq_printf(seq, " %d offset-copies", conf->far_copies);
920 else
921 seq_printf(seq, " %d far-copies", conf->far_copies);
922 }
1da177e4 923 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
76186dd8 924 conf->raid_disks - mddev->degraded);
1da177e4
LT
925 for (i = 0; i < conf->raid_disks; i++)
926 seq_printf(seq, "%s",
927 conf->mirrors[i].rdev &&
b2d444d7 928 test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
1da177e4
LT
929 seq_printf(seq, "]");
930}
931
932static void error(mddev_t *mddev, mdk_rdev_t *rdev)
933{
934 char b[BDEVNAME_SIZE];
935 conf_t *conf = mddev_to_conf(mddev);
936
937 /*
938 * If it is not operational, then we have already marked it as dead
939 * else if it is the last working disks, ignore the error, let the
940 * next level up know.
941 * else mark the drive as failed
942 */
b2d444d7 943 if (test_bit(In_sync, &rdev->flags)
76186dd8 944 && conf->raid_disks-mddev->degraded == 1)
1da177e4
LT
945 /*
946 * Don't fail the drive, just return an IO error.
947 * The test should really be more sophisticated than
948 * "working_disks == 1", but it isn't critical, and
949 * can wait until we do more sophisticated "is the drive
950 * really dead" tests...
951 */
952 return;
b2d444d7 953 if (test_bit(In_sync, &rdev->flags)) {
1da177e4 954 mddev->degraded++;
1da177e4
LT
955 /*
956 * if recovery is running, make sure it aborts.
957 */
958 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
959 }
b2d444d7
N
960 clear_bit(In_sync, &rdev->flags);
961 set_bit(Faulty, &rdev->flags);
850b2b42 962 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1da177e4
LT
963 printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
964 " Operation continuing on %d devices\n",
76186dd8 965 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
1da177e4
LT
966}
967
968static void print_conf(conf_t *conf)
969{
970 int i;
971 mirror_info_t *tmp;
972
973 printk("RAID10 conf printout:\n");
974 if (!conf) {
975 printk("(!conf)\n");
976 return;
977 }
76186dd8 978 printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1da177e4
LT
979 conf->raid_disks);
980
981 for (i = 0; i < conf->raid_disks; i++) {
982 char b[BDEVNAME_SIZE];
983 tmp = conf->mirrors + i;
984 if (tmp->rdev)
985 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
b2d444d7
N
986 i, !test_bit(In_sync, &tmp->rdev->flags),
987 !test_bit(Faulty, &tmp->rdev->flags),
1da177e4
LT
988 bdevname(tmp->rdev->bdev,b));
989 }
990}
991
992static void close_sync(conf_t *conf)
993{
0a27ec96
N
994 wait_barrier(conf);
995 allow_barrier(conf);
1da177e4
LT
996
997 mempool_destroy(conf->r10buf_pool);
998 conf->r10buf_pool = NULL;
999}
1000
6d508242
N
1001/* check if there are enough drives for
1002 * every block to appear on atleast one
1003 */
1004static int enough(conf_t *conf)
1005{
1006 int first = 0;
1007
1008 do {
1009 int n = conf->copies;
1010 int cnt = 0;
1011 while (n--) {
1012 if (conf->mirrors[first].rdev)
1013 cnt++;
1014 first = (first+1) % conf->raid_disks;
1015 }
1016 if (cnt == 0)
1017 return 0;
1018 } while (first != 0);
1019 return 1;
1020}
1021
1da177e4
LT
1022static int raid10_spare_active(mddev_t *mddev)
1023{
1024 int i;
1025 conf_t *conf = mddev->private;
1026 mirror_info_t *tmp;
1027
1028 /*
1029 * Find all non-in_sync disks within the RAID10 configuration
1030 * and mark them in_sync
1031 */
1032 for (i = 0; i < conf->raid_disks; i++) {
1033 tmp = conf->mirrors + i;
1034 if (tmp->rdev
b2d444d7
N
1035 && !test_bit(Faulty, &tmp->rdev->flags)
1036 && !test_bit(In_sync, &tmp->rdev->flags)) {
1da177e4 1037 mddev->degraded--;
b2d444d7 1038 set_bit(In_sync, &tmp->rdev->flags);
1da177e4
LT
1039 }
1040 }
1041
1042 print_conf(conf);
1043 return 0;
1044}
1045
1046
1047static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1048{
1049 conf_t *conf = mddev->private;
1050 int found = 0;
1051 int mirror;
1052 mirror_info_t *p;
1053
1054 if (mddev->recovery_cp < MaxSector)
1055 /* only hot-add to in-sync arrays, as recovery is
1056 * very different from resync
1057 */
1058 return 0;
6d508242
N
1059 if (!enough(conf))
1060 return 0;
1da177e4 1061
6cce3b23
N
1062 if (rdev->saved_raid_disk >= 0 &&
1063 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1064 mirror = rdev->saved_raid_disk;
1065 else
1066 mirror = 0;
1067 for ( ; mirror < mddev->raid_disks; mirror++)
1da177e4
LT
1068 if ( !(p=conf->mirrors+mirror)->rdev) {
1069
1070 blk_queue_stack_limits(mddev->queue,
1071 rdev->bdev->bd_disk->queue);
1072 /* as we don't honour merge_bvec_fn, we must never risk
1073 * violating it, so limit ->max_sector to one PAGE, as
1074 * a one page request is never in violation.
1075 */
1076 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1077 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1078 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1079
1080 p->head_position = 0;
1081 rdev->raid_disk = mirror;
1082 found = 1;
6cce3b23
N
1083 if (rdev->saved_raid_disk != mirror)
1084 conf->fullsync = 1;
d6065f7b 1085 rcu_assign_pointer(p->rdev, rdev);
1da177e4
LT
1086 break;
1087 }
1088
1089 print_conf(conf);
1090 return found;
1091}
1092
1093static int raid10_remove_disk(mddev_t *mddev, int number)
1094{
1095 conf_t *conf = mddev->private;
1096 int err = 0;
1097 mdk_rdev_t *rdev;
1098 mirror_info_t *p = conf->mirrors+ number;
1099
1100 print_conf(conf);
1101 rdev = p->rdev;
1102 if (rdev) {
b2d444d7 1103 if (test_bit(In_sync, &rdev->flags) ||
1da177e4
LT
1104 atomic_read(&rdev->nr_pending)) {
1105 err = -EBUSY;
1106 goto abort;
1107 }
1108 p->rdev = NULL;
fbd568a3 1109 synchronize_rcu();
1da177e4
LT
1110 if (atomic_read(&rdev->nr_pending)) {
1111 /* lost the race, try later */
1112 err = -EBUSY;
1113 p->rdev = rdev;
1114 }
1115 }
1116abort:
1117
1118 print_conf(conf);
1119 return err;
1120}
1121
1122
1123static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
1124{
1da177e4
LT
1125 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1126 conf_t *conf = mddev_to_conf(r10_bio->mddev);
1127 int i,d;
1128
1129 if (bio->bi_size)
1130 return 1;
1131
1132 for (i=0; i<conf->copies; i++)
1133 if (r10_bio->devs[i].bio == bio)
1134 break;
b6385483 1135 BUG_ON(i == conf->copies);
1da177e4
LT
1136 update_head_pos(i, r10_bio);
1137 d = r10_bio->devs[i].devnum;
0eb3ff12
N
1138
1139 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1140 set_bit(R10BIO_Uptodate, &r10_bio->state);
4dbcdc75
N
1141 else {
1142 atomic_add(r10_bio->sectors,
1143 &conf->mirrors[d].rdev->corrected_errors);
1144 if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
1145 md_error(r10_bio->mddev,
1146 conf->mirrors[d].rdev);
1147 }
1da177e4
LT
1148
1149 /* for reconstruct, we always reschedule after a read.
1150 * for resync, only after all reads
1151 */
1152 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1153 atomic_dec_and_test(&r10_bio->remaining)) {
1154 /* we have read all the blocks,
1155 * do the comparison in process context in raid10d
1156 */
1157 reschedule_retry(r10_bio);
1158 }
1159 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1160 return 0;
1161}
1162
1163static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
1164{
1165 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1166 r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
1167 mddev_t *mddev = r10_bio->mddev;
1168 conf_t *conf = mddev_to_conf(mddev);
1169 int i,d;
1170
1171 if (bio->bi_size)
1172 return 1;
1173
1174 for (i = 0; i < conf->copies; i++)
1175 if (r10_bio->devs[i].bio == bio)
1176 break;
1177 d = r10_bio->devs[i].devnum;
1178
1179 if (!uptodate)
1180 md_error(mddev, conf->mirrors[d].rdev);
1181 update_head_pos(i, r10_bio);
1182
1183 while (atomic_dec_and_test(&r10_bio->remaining)) {
1184 if (r10_bio->master_bio == NULL) {
1185 /* the primary of several recovery bios */
1186 md_done_sync(mddev, r10_bio->sectors, 1);
1187 put_buf(r10_bio);
1188 break;
1189 } else {
1190 r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
1191 put_buf(r10_bio);
1192 r10_bio = r10_bio2;
1193 }
1194 }
1195 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1196 return 0;
1197}
1198
1199/*
1200 * Note: sync and recover and handled very differently for raid10
1201 * This code is for resync.
1202 * For resync, we read through virtual addresses and read all blocks.
1203 * If there is any error, we schedule a write. The lowest numbered
1204 * drive is authoritative.
1205 * However requests come for physical address, so we need to map.
1206 * For every physical address there are raid_disks/copies virtual addresses,
1207 * which is always are least one, but is not necessarly an integer.
1208 * This means that a physical address can span multiple chunks, so we may
1209 * have to submit multiple io requests for a single sync request.
1210 */
1211/*
1212 * We check if all blocks are in-sync and only write to blocks that
1213 * aren't in sync
1214 */
1215static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1216{
1217 conf_t *conf = mddev_to_conf(mddev);
1218 int i, first;
1219 struct bio *tbio, *fbio;
1220
1221 atomic_set(&r10_bio->remaining, 1);
1222
1223 /* find the first device with a block */
1224 for (i=0; i<conf->copies; i++)
1225 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
1226 break;
1227
1228 if (i == conf->copies)
1229 goto done;
1230
1231 first = i;
1232 fbio = r10_bio->devs[i].bio;
1233
1234 /* now find blocks with errors */
0eb3ff12
N
1235 for (i=0 ; i < conf->copies ; i++) {
1236 int j, d;
1237 int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
1da177e4 1238
1da177e4 1239 tbio = r10_bio->devs[i].bio;
0eb3ff12
N
1240
1241 if (tbio->bi_end_io != end_sync_read)
1242 continue;
1243 if (i == first)
1da177e4 1244 continue;
0eb3ff12
N
1245 if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
1246 /* We know that the bi_io_vec layout is the same for
1247 * both 'first' and 'i', so we just compare them.
1248 * All vec entries are PAGE_SIZE;
1249 */
1250 for (j = 0; j < vcnt; j++)
1251 if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
1252 page_address(tbio->bi_io_vec[j].bv_page),
1253 PAGE_SIZE))
1254 break;
1255 if (j == vcnt)
1256 continue;
1257 mddev->resync_mismatches += r10_bio->sectors;
1258 }
18f08819
N
1259 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1260 /* Don't fix anything. */
1261 continue;
1da177e4
LT
1262 /* Ok, we need to write this bio
1263 * First we need to fixup bv_offset, bv_len and
1264 * bi_vecs, as the read request might have corrupted these
1265 */
1266 tbio->bi_vcnt = vcnt;
1267 tbio->bi_size = r10_bio->sectors << 9;
1268 tbio->bi_idx = 0;
1269 tbio->bi_phys_segments = 0;
1270 tbio->bi_hw_segments = 0;
1271 tbio->bi_hw_front_size = 0;
1272 tbio->bi_hw_back_size = 0;
1273 tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1274 tbio->bi_flags |= 1 << BIO_UPTODATE;
1275 tbio->bi_next = NULL;
1276 tbio->bi_rw = WRITE;
1277 tbio->bi_private = r10_bio;
1278 tbio->bi_sector = r10_bio->devs[i].addr;
1279
1280 for (j=0; j < vcnt ; j++) {
1281 tbio->bi_io_vec[j].bv_offset = 0;
1282 tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
1283
1284 memcpy(page_address(tbio->bi_io_vec[j].bv_page),
1285 page_address(fbio->bi_io_vec[j].bv_page),
1286 PAGE_SIZE);
1287 }
1288 tbio->bi_end_io = end_sync_write;
1289
1290 d = r10_bio->devs[i].devnum;
1291 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1292 atomic_inc(&r10_bio->remaining);
1293 md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
1294
1295 tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
1296 tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
1297 generic_make_request(tbio);
1298 }
1299
1300done:
1301 if (atomic_dec_and_test(&r10_bio->remaining)) {
1302 md_done_sync(mddev, r10_bio->sectors, 1);
1303 put_buf(r10_bio);
1304 }
1305}
1306
1307/*
1308 * Now for the recovery code.
1309 * Recovery happens across physical sectors.
1310 * We recover all non-is_sync drives by finding the virtual address of
1311 * each, and then choose a working drive that also has that virt address.
1312 * There is a separate r10_bio for each non-in_sync drive.
1313 * Only the first two slots are in use. The first for reading,
1314 * The second for writing.
1315 *
1316 */
1317
1318static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
1319{
1320 conf_t *conf = mddev_to_conf(mddev);
1321 int i, d;
1322 struct bio *bio, *wbio;
1323
1324
1325 /* move the pages across to the second bio
1326 * and submit the write request
1327 */
1328 bio = r10_bio->devs[0].bio;
1329 wbio = r10_bio->devs[1].bio;
1330 for (i=0; i < wbio->bi_vcnt; i++) {
1331 struct page *p = bio->bi_io_vec[i].bv_page;
1332 bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
1333 wbio->bi_io_vec[i].bv_page = p;
1334 }
1335 d = r10_bio->devs[1].devnum;
1336
1337 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1338 md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
0eb3ff12
N
1339 if (test_bit(R10BIO_Uptodate, &r10_bio->state))
1340 generic_make_request(wbio);
1341 else
1342 bio_endio(wbio, wbio->bi_size, -EIO);
1da177e4
LT
1343}
1344
1345
1346/*
1347 * This is a kernel thread which:
1348 *
1349 * 1. Retries failed read operations on working mirrors.
1350 * 2. Updates the raid superblock when problems encounter.
6814d536 1351 * 3. Performs writes following reads for array synchronising.
1da177e4
LT
1352 */
1353
6814d536
N
1354static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
1355{
1356 int sect = 0; /* Offset from r10_bio->sector */
1357 int sectors = r10_bio->sectors;
1358 mdk_rdev_t*rdev;
1359 while(sectors) {
1360 int s = sectors;
1361 int sl = r10_bio->read_slot;
1362 int success = 0;
1363 int start;
1364
1365 if (s > (PAGE_SIZE>>9))
1366 s = PAGE_SIZE >> 9;
1367
1368 rcu_read_lock();
1369 do {
1370 int d = r10_bio->devs[sl].devnum;
1371 rdev = rcu_dereference(conf->mirrors[d].rdev);
1372 if (rdev &&
1373 test_bit(In_sync, &rdev->flags)) {
1374 atomic_inc(&rdev->nr_pending);
1375 rcu_read_unlock();
1376 success = sync_page_io(rdev->bdev,
1377 r10_bio->devs[sl].addr +
1378 sect + rdev->data_offset,
1379 s<<9,
1380 conf->tmppage, READ);
1381 rdev_dec_pending(rdev, mddev);
1382 rcu_read_lock();
1383 if (success)
1384 break;
1385 }
1386 sl++;
1387 if (sl == conf->copies)
1388 sl = 0;
1389 } while (!success && sl != r10_bio->read_slot);
1390 rcu_read_unlock();
1391
1392 if (!success) {
1393 /* Cannot read from anywhere -- bye bye array */
1394 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
1395 md_error(mddev, conf->mirrors[dn].rdev);
1396 break;
1397 }
1398
1399 start = sl;
1400 /* write it back and re-read */
1401 rcu_read_lock();
1402 while (sl != r10_bio->read_slot) {
1403 int d;
1404 if (sl==0)
1405 sl = conf->copies;
1406 sl--;
1407 d = r10_bio->devs[sl].devnum;
1408 rdev = rcu_dereference(conf->mirrors[d].rdev);
1409 if (rdev &&
1410 test_bit(In_sync, &rdev->flags)) {
1411 atomic_inc(&rdev->nr_pending);
1412 rcu_read_unlock();
1413 atomic_add(s, &rdev->corrected_errors);
1414 if (sync_page_io(rdev->bdev,
1415 r10_bio->devs[sl].addr +
1416 sect + rdev->data_offset,
1417 s<<9, conf->tmppage, WRITE)
1418 == 0)
1419 /* Well, this device is dead */
1420 md_error(mddev, rdev);
1421 rdev_dec_pending(rdev, mddev);
1422 rcu_read_lock();
1423 }
1424 }
1425 sl = start;
1426 while (sl != r10_bio->read_slot) {
1427 int d;
1428 if (sl==0)
1429 sl = conf->copies;
1430 sl--;
1431 d = r10_bio->devs[sl].devnum;
1432 rdev = rcu_dereference(conf->mirrors[d].rdev);
1433 if (rdev &&
1434 test_bit(In_sync, &rdev->flags)) {
1435 char b[BDEVNAME_SIZE];
1436 atomic_inc(&rdev->nr_pending);
1437 rcu_read_unlock();
1438 if (sync_page_io(rdev->bdev,
1439 r10_bio->devs[sl].addr +
1440 sect + rdev->data_offset,
1441 s<<9, conf->tmppage, READ) == 0)
1442 /* Well, this device is dead */
1443 md_error(mddev, rdev);
1444 else
1445 printk(KERN_INFO
1446 "raid10:%s: read error corrected"
1447 " (%d sectors at %llu on %s)\n",
1448 mdname(mddev), s,
1449 (unsigned long long)sect+
1450 rdev->data_offset,
1451 bdevname(rdev->bdev, b));
1452
1453 rdev_dec_pending(rdev, mddev);
1454 rcu_read_lock();
1455 }
1456 }
1457 rcu_read_unlock();
1458
1459 sectors -= s;
1460 sect += s;
1461 }
1462}
1463
1da177e4
LT
1464static void raid10d(mddev_t *mddev)
1465{
1466 r10bio_t *r10_bio;
1467 struct bio *bio;
1468 unsigned long flags;
1469 conf_t *conf = mddev_to_conf(mddev);
1470 struct list_head *head = &conf->retry_list;
1471 int unplug=0;
1472 mdk_rdev_t *rdev;
1473
1474 md_check_recovery(mddev);
1da177e4
LT
1475
1476 for (;;) {
1477 char b[BDEVNAME_SIZE];
1478 spin_lock_irqsave(&conf->device_lock, flags);
6cce3b23
N
1479
1480 if (conf->pending_bio_list.head) {
1481 bio = bio_list_get(&conf->pending_bio_list);
1482 blk_remove_plug(mddev->queue);
1483 spin_unlock_irqrestore(&conf->device_lock, flags);
1484 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1485 if (bitmap_unplug(mddev->bitmap) != 0)
1486 printk("%s: bitmap file write failed!\n", mdname(mddev));
1487
1488 while (bio) { /* submit pending writes */
1489 struct bio *next = bio->bi_next;
1490 bio->bi_next = NULL;
1491 generic_make_request(bio);
1492 bio = next;
1493 }
1494 unplug = 1;
1495
1496 continue;
1497 }
1498
1da177e4
LT
1499 if (list_empty(head))
1500 break;
1501 r10_bio = list_entry(head->prev, r10bio_t, retry_list);
1502 list_del(head->prev);
4443ae10 1503 conf->nr_queued--;
1da177e4
LT
1504 spin_unlock_irqrestore(&conf->device_lock, flags);
1505
1506 mddev = r10_bio->mddev;
1507 conf = mddev_to_conf(mddev);
1508 if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
1509 sync_request_write(mddev, r10_bio);
1510 unplug = 1;
1511 } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
1512 recovery_request_write(mddev, r10_bio);
1513 unplug = 1;
1514 } else {
1515 int mirror;
4443ae10
N
1516 /* we got a read error. Maybe the drive is bad. Maybe just
1517 * the block and we can fix it.
1518 * We freeze all other IO, and try reading the block from
1519 * other devices. When we find one, we re-write
1520 * and check it that fixes the read error.
1521 * This is all done synchronously while the array is
1522 * frozen.
1523 */
6814d536
N
1524 if (mddev->ro == 0) {
1525 freeze_array(conf);
1526 fix_read_error(conf, mddev, r10_bio);
1527 unfreeze_array(conf);
4443ae10
N
1528 }
1529
1da177e4 1530 bio = r10_bio->devs[r10_bio->read_slot].bio;
0eb3ff12
N
1531 r10_bio->devs[r10_bio->read_slot].bio =
1532 mddev->ro ? IO_BLOCKED : NULL;
1da177e4
LT
1533 bio_put(bio);
1534 mirror = read_balance(conf, r10_bio);
1535 if (mirror == -1) {
1536 printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
1537 " read error for block %llu\n",
1538 bdevname(bio->bi_bdev,b),
1539 (unsigned long long)r10_bio->sector);
1540 raid_end_bio_io(r10_bio);
1541 } else {
1542 rdev = conf->mirrors[mirror].rdev;
1543 if (printk_ratelimit())
1544 printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
1545 " another mirror\n",
1546 bdevname(rdev->bdev,b),
1547 (unsigned long long)r10_bio->sector);
1548 bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
1549 r10_bio->devs[r10_bio->read_slot].bio = bio;
1550 bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
1551 + rdev->data_offset;
1552 bio->bi_bdev = rdev->bdev;
1553 bio->bi_rw = READ;
1554 bio->bi_private = r10_bio;
1555 bio->bi_end_io = raid10_end_read_request;
1556 unplug = 1;
1557 generic_make_request(bio);
1558 }
1559 }
1560 }
1561 spin_unlock_irqrestore(&conf->device_lock, flags);
1562 if (unplug)
1563 unplug_slaves(mddev);
1564}
1565
1566
1567static int init_resync(conf_t *conf)
1568{
1569 int buffs;
1570
1571 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
b6385483 1572 BUG_ON(conf->r10buf_pool);
1da177e4
LT
1573 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
1574 if (!conf->r10buf_pool)
1575 return -ENOMEM;
1576 conf->next_resync = 0;
1577 return 0;
1578}
1579
1580/*
1581 * perform a "sync" on one "block"
1582 *
1583 * We need to make sure that no normal I/O request - particularly write
1584 * requests - conflict with active sync requests.
1585 *
1586 * This is achieved by tracking pending requests and a 'barrier' concept
1587 * that can be installed to exclude normal IO requests.
1588 *
1589 * Resync and recovery are handled very differently.
1590 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
1591 *
1592 * For resync, we iterate over virtual addresses, read all copies,
1593 * and update if there are differences. If only one copy is live,
1594 * skip it.
1595 * For recovery, we iterate over physical addresses, read a good
1596 * value for each non-in_sync drive, and over-write.
1597 *
1598 * So, for recovery we may have several outstanding complex requests for a
1599 * given address, one for each out-of-sync device. We model this by allocating
1600 * a number of r10_bio structures, one for each out-of-sync device.
1601 * As we setup these structures, we collect all bio's together into a list
1602 * which we then process collectively to add pages, and then process again
1603 * to pass to generic_make_request.
1604 *
1605 * The r10_bio structures are linked using a borrowed master_bio pointer.
1606 * This link is counted in ->remaining. When the r10_bio that points to NULL
1607 * has its remaining count decremented to 0, the whole complex operation
1608 * is complete.
1609 *
1610 */
1611
57afd89f 1612static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1da177e4
LT
1613{
1614 conf_t *conf = mddev_to_conf(mddev);
1615 r10bio_t *r10_bio;
1616 struct bio *biolist = NULL, *bio;
1617 sector_t max_sector, nr_sectors;
1618 int disk;
1619 int i;
6cce3b23
N
1620 int max_sync;
1621 int sync_blocks;
1da177e4
LT
1622
1623 sector_t sectors_skipped = 0;
1624 int chunks_skipped = 0;
1625
1626 if (!conf->r10buf_pool)
1627 if (init_resync(conf))
57afd89f 1628 return 0;
1da177e4
LT
1629
1630 skipped:
1631 max_sector = mddev->size << 1;
1632 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1633 max_sector = mddev->resync_max_sectors;
1634 if (sector_nr >= max_sector) {
6cce3b23
N
1635 /* If we aborted, we need to abort the
1636 * sync on the 'current' bitmap chucks (there can
1637 * be several when recovering multiple devices).
1638 * as we may have started syncing it but not finished.
1639 * We can find the current address in
1640 * mddev->curr_resync, but for recovery,
1641 * we need to convert that to several
1642 * virtual addresses.
1643 */
1644 if (mddev->curr_resync < max_sector) { /* aborted */
1645 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
1646 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1647 &sync_blocks, 1);
1648 else for (i=0; i<conf->raid_disks; i++) {
1649 sector_t sect =
1650 raid10_find_virt(conf, mddev->curr_resync, i);
1651 bitmap_end_sync(mddev->bitmap, sect,
1652 &sync_blocks, 1);
1653 }
1654 } else /* completed sync */
1655 conf->fullsync = 0;
1656
1657 bitmap_close_sync(mddev->bitmap);
1da177e4 1658 close_sync(conf);
57afd89f 1659 *skipped = 1;
1da177e4
LT
1660 return sectors_skipped;
1661 }
1662 if (chunks_skipped >= conf->raid_disks) {
1663 /* if there has been nothing to do on any drive,
1664 * then there is nothing to do at all..
1665 */
57afd89f
N
1666 *skipped = 1;
1667 return (max_sector - sector_nr) + sectors_skipped;
1da177e4
LT
1668 }
1669
1670 /* make sure whole request will fit in a chunk - if chunks
1671 * are meaningful
1672 */
1673 if (conf->near_copies < conf->raid_disks &&
1674 max_sector > (sector_nr | conf->chunk_mask))
1675 max_sector = (sector_nr | conf->chunk_mask) + 1;
1676 /*
1677 * If there is non-resync activity waiting for us then
1678 * put in a delay to throttle resync.
1679 */
0a27ec96 1680 if (!go_faster && conf->nr_waiting)
1da177e4 1681 msleep_interruptible(1000);
1da177e4
LT
1682
1683 /* Again, very different code for resync and recovery.
1684 * Both must result in an r10bio with a list of bios that
1685 * have bi_end_io, bi_sector, bi_bdev set,
1686 * and bi_private set to the r10bio.
1687 * For recovery, we may actually create several r10bios
1688 * with 2 bios in each, that correspond to the bios in the main one.
1689 * In this case, the subordinate r10bios link back through a
1690 * borrowed master_bio pointer, and the counter in the master
1691 * includes a ref from each subordinate.
1692 */
1693 /* First, we decide what to do and set ->bi_end_io
1694 * To end_sync_read if we want to read, and
1695 * end_sync_write if we will want to write.
1696 */
1697
6cce3b23 1698 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
1da177e4
LT
1699 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1700 /* recovery... the complicated one */
1701 int i, j, k;
1702 r10_bio = NULL;
1703
1704 for (i=0 ; i<conf->raid_disks; i++)
1705 if (conf->mirrors[i].rdev &&
b2d444d7 1706 !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
6cce3b23 1707 int still_degraded = 0;
1da177e4
LT
1708 /* want to reconstruct this device */
1709 r10bio_t *rb2 = r10_bio;
6cce3b23
N
1710 sector_t sect = raid10_find_virt(conf, sector_nr, i);
1711 int must_sync;
1712 /* Unless we are doing a full sync, we only need
1713 * to recover the block if it is set in the bitmap
1714 */
1715 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1716 &sync_blocks, 1);
1717 if (sync_blocks < max_sync)
1718 max_sync = sync_blocks;
1719 if (!must_sync &&
1720 !conf->fullsync) {
1721 /* yep, skip the sync_blocks here, but don't assume
1722 * that there will never be anything to do here
1723 */
1724 chunks_skipped = -1;
1725 continue;
1726 }
1da177e4
LT
1727
1728 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
6cce3b23 1729 raise_barrier(conf, rb2 != NULL);
1da177e4
LT
1730 atomic_set(&r10_bio->remaining, 0);
1731
1732 r10_bio->master_bio = (struct bio*)rb2;
1733 if (rb2)
1734 atomic_inc(&rb2->remaining);
1735 r10_bio->mddev = mddev;
1736 set_bit(R10BIO_IsRecover, &r10_bio->state);
6cce3b23
N
1737 r10_bio->sector = sect;
1738
1da177e4 1739 raid10_find_phys(conf, r10_bio);
6cce3b23
N
1740 /* Need to check if this section will still be
1741 * degraded
1742 */
1743 for (j=0; j<conf->copies;j++) {
1744 int d = r10_bio->devs[j].devnum;
1745 if (conf->mirrors[d].rdev == NULL ||
a24a8dd8 1746 test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
6cce3b23 1747 still_degraded = 1;
a24a8dd8
N
1748 break;
1749 }
6cce3b23
N
1750 }
1751 must_sync = bitmap_start_sync(mddev->bitmap, sect,
1752 &sync_blocks, still_degraded);
1753
1da177e4
LT
1754 for (j=0; j<conf->copies;j++) {
1755 int d = r10_bio->devs[j].devnum;
1756 if (conf->mirrors[d].rdev &&
b2d444d7 1757 test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
1da177e4
LT
1758 /* This is where we read from */
1759 bio = r10_bio->devs[0].bio;
1760 bio->bi_next = biolist;
1761 biolist = bio;
1762 bio->bi_private = r10_bio;
1763 bio->bi_end_io = end_sync_read;
1764 bio->bi_rw = 0;
1765 bio->bi_sector = r10_bio->devs[j].addr +
1766 conf->mirrors[d].rdev->data_offset;
1767 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1768 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1769 atomic_inc(&r10_bio->remaining);
1770 /* and we write to 'i' */
1771
1772 for (k=0; k<conf->copies; k++)
1773 if (r10_bio->devs[k].devnum == i)
1774 break;
1775 bio = r10_bio->devs[1].bio;
1776 bio->bi_next = biolist;
1777 biolist = bio;
1778 bio->bi_private = r10_bio;
1779 bio->bi_end_io = end_sync_write;
1780 bio->bi_rw = 1;
1781 bio->bi_sector = r10_bio->devs[k].addr +
1782 conf->mirrors[i].rdev->data_offset;
1783 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1784
1785 r10_bio->devs[0].devnum = d;
1786 r10_bio->devs[1].devnum = i;
1787
1788 break;
1789 }
1790 }
1791 if (j == conf->copies) {
87fc767b
N
1792 /* Cannot recover, so abort the recovery */
1793 put_buf(r10_bio);
1794 r10_bio = rb2;
1795 if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
1796 printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
1797 mdname(mddev));
1798 break;
1da177e4
LT
1799 }
1800 }
1801 if (biolist == NULL) {
1802 while (r10_bio) {
1803 r10bio_t *rb2 = r10_bio;
1804 r10_bio = (r10bio_t*) rb2->master_bio;
1805 rb2->master_bio = NULL;
1806 put_buf(rb2);
1807 }
1808 goto giveup;
1809 }
1810 } else {
1811 /* resync. Schedule a read for every block at this virt offset */
1812 int count = 0;
6cce3b23
N
1813
1814 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
1815 &sync_blocks, mddev->degraded) &&
1816 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
1817 /* We can skip this block */
1818 *skipped = 1;
1819 return sync_blocks + sectors_skipped;
1820 }
1821 if (sync_blocks < max_sync)
1822 max_sync = sync_blocks;
1da177e4
LT
1823 r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
1824
1da177e4
LT
1825 r10_bio->mddev = mddev;
1826 atomic_set(&r10_bio->remaining, 0);
6cce3b23
N
1827 raise_barrier(conf, 0);
1828 conf->next_resync = sector_nr;
1da177e4
LT
1829
1830 r10_bio->master_bio = NULL;
1831 r10_bio->sector = sector_nr;
1832 set_bit(R10BIO_IsSync, &r10_bio->state);
1833 raid10_find_phys(conf, r10_bio);
1834 r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
1835
1836 for (i=0; i<conf->copies; i++) {
1837 int d = r10_bio->devs[i].devnum;
1838 bio = r10_bio->devs[i].bio;
1839 bio->bi_end_io = NULL;
1840 if (conf->mirrors[d].rdev == NULL ||
b2d444d7 1841 test_bit(Faulty, &conf->mirrors[d].rdev->flags))
1da177e4
LT
1842 continue;
1843 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
1844 atomic_inc(&r10_bio->remaining);
1845 bio->bi_next = biolist;
1846 biolist = bio;
1847 bio->bi_private = r10_bio;
1848 bio->bi_end_io = end_sync_read;
1849 bio->bi_rw = 0;
1850 bio->bi_sector = r10_bio->devs[i].addr +
1851 conf->mirrors[d].rdev->data_offset;
1852 bio->bi_bdev = conf->mirrors[d].rdev->bdev;
1853 count++;
1854 }
1855
1856 if (count < 2) {
1857 for (i=0; i<conf->copies; i++) {
1858 int d = r10_bio->devs[i].devnum;
1859 if (r10_bio->devs[i].bio->bi_end_io)
1860 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1861 }
1862 put_buf(r10_bio);
1863 biolist = NULL;
1864 goto giveup;
1865 }
1866 }
1867
1868 for (bio = biolist; bio ; bio=bio->bi_next) {
1869
1870 bio->bi_flags &= ~(BIO_POOL_MASK - 1);
1871 if (bio->bi_end_io)
1872 bio->bi_flags |= 1 << BIO_UPTODATE;
1873 bio->bi_vcnt = 0;
1874 bio->bi_idx = 0;
1875 bio->bi_phys_segments = 0;
1876 bio->bi_hw_segments = 0;
1877 bio->bi_size = 0;
1878 }
1879
1880 nr_sectors = 0;
6cce3b23
N
1881 if (sector_nr + max_sync < max_sector)
1882 max_sector = sector_nr + max_sync;
1da177e4
LT
1883 do {
1884 struct page *page;
1885 int len = PAGE_SIZE;
1886 disk = 0;
1887 if (sector_nr + (len>>9) > max_sector)
1888 len = (max_sector - sector_nr) << 9;
1889 if (len == 0)
1890 break;
1891 for (bio= biolist ; bio ; bio=bio->bi_next) {
1892 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
1893 if (bio_add_page(bio, page, len, 0) == 0) {
1894 /* stop here */
1895 struct bio *bio2;
1896 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
1897 for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
1898 /* remove last page from this bio */
1899 bio2->bi_vcnt--;
1900 bio2->bi_size -= len;
1901 bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
1902 }
1903 goto bio_full;
1904 }
1905 disk = i;
1906 }
1907 nr_sectors += len>>9;
1908 sector_nr += len>>9;
1909 } while (biolist->bi_vcnt < RESYNC_PAGES);
1910 bio_full:
1911 r10_bio->sectors = nr_sectors;
1912
1913 while (biolist) {
1914 bio = biolist;
1915 biolist = biolist->bi_next;
1916
1917 bio->bi_next = NULL;
1918 r10_bio = bio->bi_private;
1919 r10_bio->sectors = nr_sectors;
1920
1921 if (bio->bi_end_io == end_sync_read) {
1922 md_sync_acct(bio->bi_bdev, nr_sectors);
1923 generic_make_request(bio);
1924 }
1925 }
1926
57afd89f
N
1927 if (sectors_skipped)
1928 /* pretend they weren't skipped, it makes
1929 * no important difference in this case
1930 */
1931 md_done_sync(mddev, sectors_skipped, 1);
1932
1da177e4
LT
1933 return sectors_skipped + nr_sectors;
1934 giveup:
1935 /* There is nowhere to write, so all non-sync
1936 * drives must be failed, so try the next chunk...
1937 */
1938 {
57afd89f 1939 sector_t sec = max_sector - sector_nr;
1da177e4
LT
1940 sectors_skipped += sec;
1941 chunks_skipped ++;
1942 sector_nr = max_sector;
1da177e4
LT
1943 goto skipped;
1944 }
1945}
1946
1947static int run(mddev_t *mddev)
1948{
1949 conf_t *conf;
1950 int i, disk_idx;
1951 mirror_info_t *disk;
1952 mdk_rdev_t *rdev;
1953 struct list_head *tmp;
c93983bf 1954 int nc, fc, fo;
1da177e4
LT
1955 sector_t stride, size;
1956
2604b703
N
1957 if (mddev->chunk_size == 0) {
1958 printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
1959 return -EINVAL;
1da177e4 1960 }
2604b703 1961
1da177e4
LT
1962 nc = mddev->layout & 255;
1963 fc = (mddev->layout >> 8) & 255;
c93983bf 1964 fo = mddev->layout & (1<<16);
1da177e4 1965 if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
c93983bf 1966 (mddev->layout >> 17)) {
1da177e4
LT
1967 printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
1968 mdname(mddev), mddev->layout);
1969 goto out;
1970 }
1971 /*
1972 * copy the already verified devices into our private RAID10
1973 * bookkeeping area. [whatever we allocate in run(),
1974 * should be freed in stop()]
1975 */
4443ae10 1976 conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
1da177e4
LT
1977 mddev->private = conf;
1978 if (!conf) {
1979 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1980 mdname(mddev));
1981 goto out;
1982 }
4443ae10 1983 conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1da177e4
LT
1984 GFP_KERNEL);
1985 if (!conf->mirrors) {
1986 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
1987 mdname(mddev));
1988 goto out_free_conf;
1989 }
4443ae10
N
1990
1991 conf->tmppage = alloc_page(GFP_KERNEL);
1992 if (!conf->tmppage)
1993 goto out_free_conf;
1da177e4
LT
1994
1995 conf->near_copies = nc;
1996 conf->far_copies = fc;
1997 conf->copies = nc*fc;
c93983bf 1998 conf->far_offset = fo;
1da177e4
LT
1999 conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
2000 conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
c93983bf
N
2001 if (fo)
2002 conf->stride = 1 << conf->chunk_shift;
2003 else {
2004 stride = mddev->size >> (conf->chunk_shift-1);
2005 sector_div(stride, fc);
2006 conf->stride = stride << conf->chunk_shift;
2007 }
1da177e4
LT
2008 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
2009 r10bio_pool_free, conf);
2010 if (!conf->r10bio_pool) {
2011 printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
2012 mdname(mddev));
2013 goto out_free_conf;
2014 }
1da177e4
LT
2015
2016 ITERATE_RDEV(mddev, rdev, tmp) {
2017 disk_idx = rdev->raid_disk;
2018 if (disk_idx >= mddev->raid_disks
2019 || disk_idx < 0)
2020 continue;
2021 disk = conf->mirrors + disk_idx;
2022
2023 disk->rdev = rdev;
2024
2025 blk_queue_stack_limits(mddev->queue,
2026 rdev->bdev->bd_disk->queue);
2027 /* as we don't honour merge_bvec_fn, we must never risk
2028 * violating it, so limit ->max_sector to one PAGE, as
2029 * a one page request is never in violation.
2030 */
2031 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
2032 mddev->queue->max_sectors > (PAGE_SIZE>>9))
2033 mddev->queue->max_sectors = (PAGE_SIZE>>9);
2034
2035 disk->head_position = 0;
1da177e4
LT
2036 }
2037 conf->raid_disks = mddev->raid_disks;
2038 conf->mddev = mddev;
2039 spin_lock_init(&conf->device_lock);
2040 INIT_LIST_HEAD(&conf->retry_list);
2041
2042 spin_lock_init(&conf->resync_lock);
0a27ec96 2043 init_waitqueue_head(&conf->wait_barrier);
1da177e4 2044
6d508242
N
2045 /* need to check that every block has at least one working mirror */
2046 if (!enough(conf)) {
2047 printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
2048 mdname(mddev));
1da177e4
LT
2049 goto out_free_conf;
2050 }
2051
2052 mddev->degraded = 0;
2053 for (i = 0; i < conf->raid_disks; i++) {
2054
2055 disk = conf->mirrors + i;
2056
5fd6c1dc
N
2057 if (!disk->rdev ||
2058 !test_bit(In_sync, &rdev->flags)) {
1da177e4
LT
2059 disk->head_position = 0;
2060 mddev->degraded++;
2061 }
2062 }
2063
2064
2065 mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
2066 if (!mddev->thread) {
2067 printk(KERN_ERR
2068 "raid10: couldn't allocate thread for %s\n",
2069 mdname(mddev));
2070 goto out_free_conf;
2071 }
2072
2073 printk(KERN_INFO
2074 "raid10: raid set %s active with %d out of %d devices\n",
2075 mdname(mddev), mddev->raid_disks - mddev->degraded,
2076 mddev->raid_disks);
2077 /*
2078 * Ok, everything is just fine now
2079 */
88388328
N
2080 if (conf->far_offset) {
2081 size = mddev->size >> (conf->chunk_shift-1);
2082 size *= conf->raid_disks;
2083 size <<= conf->chunk_shift;
2084 sector_div(size, conf->far_copies);
2085 } else
2086 size = conf->stride * conf->raid_disks;
1da177e4
LT
2087 sector_div(size, conf->near_copies);
2088 mddev->array_size = size/2;
2089 mddev->resync_max_sectors = size;
2090
7a5febe9
N
2091 mddev->queue->unplug_fn = raid10_unplug;
2092 mddev->queue->issue_flush_fn = raid10_issue_flush;
2093
1da177e4
LT
2094 /* Calculate max read-ahead size.
2095 * We need to readahead at least twice a whole stripe....
2096 * maybe...
2097 */
2098 {
8932c2e0 2099 int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
1da177e4
LT
2100 stripe /= conf->near_copies;
2101 if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
2102 mddev->queue->backing_dev_info.ra_pages = 2* stripe;
2103 }
2104
2105 if (conf->near_copies < mddev->raid_disks)
2106 blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
2107 return 0;
2108
2109out_free_conf:
2110 if (conf->r10bio_pool)
2111 mempool_destroy(conf->r10bio_pool);
1345b1d8 2112 safe_put_page(conf->tmppage);
990a8baf 2113 kfree(conf->mirrors);
1da177e4
LT
2114 kfree(conf);
2115 mddev->private = NULL;
2116out:
2117 return -EIO;
2118}
2119
2120static int stop(mddev_t *mddev)
2121{
2122 conf_t *conf = mddev_to_conf(mddev);
2123
2124 md_unregister_thread(mddev->thread);
2125 mddev->thread = NULL;
2126 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2127 if (conf->r10bio_pool)
2128 mempool_destroy(conf->r10bio_pool);
990a8baf 2129 kfree(conf->mirrors);
1da177e4
LT
2130 kfree(conf);
2131 mddev->private = NULL;
2132 return 0;
2133}
2134
6cce3b23
N
2135static void raid10_quiesce(mddev_t *mddev, int state)
2136{
2137 conf_t *conf = mddev_to_conf(mddev);
2138
2139 switch(state) {
2140 case 1:
2141 raise_barrier(conf, 0);
2142 break;
2143 case 0:
2144 lower_barrier(conf);
2145 break;
2146 }
2147 if (mddev->thread) {
2148 if (mddev->bitmap)
2149 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2150 else
2151 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2152 md_wakeup_thread(mddev->thread);
2153 }
2154}
1da177e4 2155
2604b703 2156static struct mdk_personality raid10_personality =
1da177e4
LT
2157{
2158 .name = "raid10",
2604b703 2159 .level = 10,
1da177e4
LT
2160 .owner = THIS_MODULE,
2161 .make_request = make_request,
2162 .run = run,
2163 .stop = stop,
2164 .status = status,
2165 .error_handler = error,
2166 .hot_add_disk = raid10_add_disk,
2167 .hot_remove_disk= raid10_remove_disk,
2168 .spare_active = raid10_spare_active,
2169 .sync_request = sync_request,
6cce3b23 2170 .quiesce = raid10_quiesce,
1da177e4
LT
2171};
2172
2173static int __init raid_init(void)
2174{
2604b703 2175 return register_md_personality(&raid10_personality);
1da177e4
LT
2176}
2177
2178static void raid_exit(void)
2179{
2604b703 2180 unregister_md_personality(&raid10_personality);
1da177e4
LT
2181}
2182
2183module_init(raid_init);
2184module_exit(raid_exit);
2185MODULE_LICENSE("GPL");
2186MODULE_ALIAS("md-personality-9"); /* RAID10 */
d9d166c2 2187MODULE_ALIAS("md-raid10");
2604b703 2188MODULE_ALIAS("md-level-10");