Commit | Line | Data |
---|---|---|
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 | * | |
25985edc | 8 | * Base on code in raid1.c. See raid1.c for further copyright information. |
1da177e4 LT |
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 | ||
5a0e3ad6 | 21 | #include <linux/slab.h> |
25570727 | 22 | #include <linux/delay.h> |
bff61975 | 23 | #include <linux/blkdev.h> |
056075c7 | 24 | #include <linux/module.h> |
bff61975 | 25 | #include <linux/seq_file.h> |
8bda470e | 26 | #include <linux/ratelimit.h> |
43b2e5d8 | 27 | #include "md.h" |
ef740c37 | 28 | #include "raid10.h" |
dab8b292 | 29 | #include "raid0.h" |
ef740c37 | 30 | #include "bitmap.h" |
1da177e4 LT |
31 | |
32 | /* | |
33 | * RAID10 provides a combination of RAID0 and RAID1 functionality. | |
34 | * The layout of data is defined by | |
35 | * chunk_size | |
36 | * raid_disks | |
37 | * near_copies (stored in low byte of layout) | |
38 | * far_copies (stored in second byte of layout) | |
c93983bf | 39 | * far_offset (stored in bit 16 of layout ) |
1da177e4 LT |
40 | * |
41 | * The data to be stored is divided into chunks using chunksize. | |
42 | * Each device is divided into far_copies sections. | |
43 | * In each section, chunks are laid out in a style similar to raid0, but | |
44 | * near_copies copies of each chunk is stored (each on a different drive). | |
45 | * The starting device for each section is offset near_copies from the starting | |
46 | * device of the previous section. | |
c93983bf | 47 | * Thus they are (near_copies*far_copies) of each chunk, and each is on a different |
1da177e4 LT |
48 | * drive. |
49 | * near_copies and far_copies must be at least one, and their product is at most | |
50 | * raid_disks. | |
c93983bf N |
51 | * |
52 | * If far_offset is true, then the far_copies are handled a bit differently. | |
53 | * The copies are still in different stripes, but instead of be very far apart | |
54 | * on disk, there are adjacent stripes. | |
1da177e4 LT |
55 | */ |
56 | ||
57 | /* | |
58 | * Number of guaranteed r10bios in case of extreme VM load: | |
59 | */ | |
60 | #define NR_RAID10_BIOS 256 | |
61 | ||
34db0cd6 N |
62 | /* When there are this many requests queue to be written by |
63 | * the raid10 thread, we become 'congested' to provide back-pressure | |
64 | * for writeback. | |
65 | */ | |
66 | static int max_queued_requests = 1024; | |
67 | ||
e879a879 N |
68 | static void allow_barrier(struct r10conf *conf); |
69 | static void lower_barrier(struct r10conf *conf); | |
0a27ec96 | 70 | |
dd0fc66f | 71 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 | 72 | { |
e879a879 | 73 | struct r10conf *conf = data; |
9f2c9d12 | 74 | int size = offsetof(struct r10bio, devs[conf->copies]); |
1da177e4 | 75 | |
69335ef3 N |
76 | /* allocate a r10bio with room for raid_disks entries in the |
77 | * bios array */ | |
7eaceacc | 78 | return kzalloc(size, gfp_flags); |
1da177e4 LT |
79 | } |
80 | ||
81 | static void r10bio_pool_free(void *r10_bio, void *data) | |
82 | { | |
83 | kfree(r10_bio); | |
84 | } | |
85 | ||
0310fa21 | 86 | /* Maximum size of each resync request */ |
1da177e4 | 87 | #define RESYNC_BLOCK_SIZE (64*1024) |
1da177e4 | 88 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) |
0310fa21 N |
89 | /* amount of memory to reserve for resync requests */ |
90 | #define RESYNC_WINDOW (1024*1024) | |
91 | /* maximum number of concurrent requests, memory permitting */ | |
92 | #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) | |
1da177e4 LT |
93 | |
94 | /* | |
95 | * When performing a resync, we need to read and compare, so | |
96 | * we need as many pages are there are copies. | |
97 | * When performing a recovery, we need 2 bios, one for read, | |
98 | * one for write (we recover only one drive per r10buf) | |
99 | * | |
100 | */ | |
dd0fc66f | 101 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 | 102 | { |
e879a879 | 103 | struct r10conf *conf = data; |
1da177e4 | 104 | struct page *page; |
9f2c9d12 | 105 | struct r10bio *r10_bio; |
1da177e4 LT |
106 | struct bio *bio; |
107 | int i, j; | |
108 | int nalloc; | |
109 | ||
110 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); | |
7eaceacc | 111 | if (!r10_bio) |
1da177e4 | 112 | return NULL; |
1da177e4 LT |
113 | |
114 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) | |
115 | nalloc = conf->copies; /* resync */ | |
116 | else | |
117 | nalloc = 2; /* recovery */ | |
118 | ||
119 | /* | |
120 | * Allocate bios. | |
121 | */ | |
122 | for (j = nalloc ; j-- ; ) { | |
6746557f | 123 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
1da177e4 LT |
124 | if (!bio) |
125 | goto out_free_bio; | |
126 | r10_bio->devs[j].bio = bio; | |
69335ef3 N |
127 | if (!conf->have_replacement) |
128 | continue; | |
129 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); | |
130 | if (!bio) | |
131 | goto out_free_bio; | |
132 | r10_bio->devs[j].repl_bio = bio; | |
1da177e4 LT |
133 | } |
134 | /* | |
135 | * Allocate RESYNC_PAGES data pages and attach them | |
136 | * where needed. | |
137 | */ | |
138 | for (j = 0 ; j < nalloc; j++) { | |
69335ef3 | 139 | struct bio *rbio = r10_bio->devs[j].repl_bio; |
1da177e4 LT |
140 | bio = r10_bio->devs[j].bio; |
141 | for (i = 0; i < RESYNC_PAGES; i++) { | |
c65060ad NK |
142 | if (j == 1 && !test_bit(MD_RECOVERY_SYNC, |
143 | &conf->mddev->recovery)) { | |
144 | /* we can share bv_page's during recovery */ | |
145 | struct bio *rbio = r10_bio->devs[0].bio; | |
146 | page = rbio->bi_io_vec[i].bv_page; | |
147 | get_page(page); | |
148 | } else | |
149 | page = alloc_page(gfp_flags); | |
1da177e4 LT |
150 | if (unlikely(!page)) |
151 | goto out_free_pages; | |
152 | ||
153 | bio->bi_io_vec[i].bv_page = page; | |
69335ef3 N |
154 | if (rbio) |
155 | rbio->bi_io_vec[i].bv_page = page; | |
1da177e4 LT |
156 | } |
157 | } | |
158 | ||
159 | return r10_bio; | |
160 | ||
161 | out_free_pages: | |
162 | for ( ; i > 0 ; i--) | |
1345b1d8 | 163 | safe_put_page(bio->bi_io_vec[i-1].bv_page); |
1da177e4 LT |
164 | while (j--) |
165 | for (i = 0; i < RESYNC_PAGES ; i++) | |
1345b1d8 | 166 | safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page); |
1da177e4 LT |
167 | j = -1; |
168 | out_free_bio: | |
69335ef3 | 169 | while (++j < nalloc) { |
1da177e4 | 170 | bio_put(r10_bio->devs[j].bio); |
69335ef3 N |
171 | if (r10_bio->devs[j].repl_bio) |
172 | bio_put(r10_bio->devs[j].repl_bio); | |
173 | } | |
1da177e4 LT |
174 | r10bio_pool_free(r10_bio, conf); |
175 | return NULL; | |
176 | } | |
177 | ||
178 | static void r10buf_pool_free(void *__r10_bio, void *data) | |
179 | { | |
180 | int i; | |
e879a879 | 181 | struct r10conf *conf = data; |
9f2c9d12 | 182 | struct r10bio *r10bio = __r10_bio; |
1da177e4 LT |
183 | int j; |
184 | ||
185 | for (j=0; j < conf->copies; j++) { | |
186 | struct bio *bio = r10bio->devs[j].bio; | |
187 | if (bio) { | |
188 | for (i = 0; i < RESYNC_PAGES; i++) { | |
1345b1d8 | 189 | safe_put_page(bio->bi_io_vec[i].bv_page); |
1da177e4 LT |
190 | bio->bi_io_vec[i].bv_page = NULL; |
191 | } | |
192 | bio_put(bio); | |
193 | } | |
69335ef3 N |
194 | bio = r10bio->devs[j].repl_bio; |
195 | if (bio) | |
196 | bio_put(bio); | |
1da177e4 LT |
197 | } |
198 | r10bio_pool_free(r10bio, conf); | |
199 | } | |
200 | ||
e879a879 | 201 | static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio) |
1da177e4 LT |
202 | { |
203 | int i; | |
204 | ||
205 | for (i = 0; i < conf->copies; i++) { | |
206 | struct bio **bio = & r10_bio->devs[i].bio; | |
749c55e9 | 207 | if (!BIO_SPECIAL(*bio)) |
1da177e4 LT |
208 | bio_put(*bio); |
209 | *bio = NULL; | |
69335ef3 N |
210 | bio = &r10_bio->devs[i].repl_bio; |
211 | if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio)) | |
212 | bio_put(*bio); | |
213 | *bio = NULL; | |
1da177e4 LT |
214 | } |
215 | } | |
216 | ||
9f2c9d12 | 217 | static void free_r10bio(struct r10bio *r10_bio) |
1da177e4 | 218 | { |
e879a879 | 219 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 | 220 | |
1da177e4 LT |
221 | put_all_bios(conf, r10_bio); |
222 | mempool_free(r10_bio, conf->r10bio_pool); | |
223 | } | |
224 | ||
9f2c9d12 | 225 | static void put_buf(struct r10bio *r10_bio) |
1da177e4 | 226 | { |
e879a879 | 227 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 LT |
228 | |
229 | mempool_free(r10_bio, conf->r10buf_pool); | |
230 | ||
0a27ec96 | 231 | lower_barrier(conf); |
1da177e4 LT |
232 | } |
233 | ||
9f2c9d12 | 234 | static void reschedule_retry(struct r10bio *r10_bio) |
1da177e4 LT |
235 | { |
236 | unsigned long flags; | |
fd01b88c | 237 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 238 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
239 | |
240 | spin_lock_irqsave(&conf->device_lock, flags); | |
241 | list_add(&r10_bio->retry_list, &conf->retry_list); | |
4443ae10 | 242 | conf->nr_queued ++; |
1da177e4 LT |
243 | spin_unlock_irqrestore(&conf->device_lock, flags); |
244 | ||
388667be AJ |
245 | /* wake up frozen array... */ |
246 | wake_up(&conf->wait_barrier); | |
247 | ||
1da177e4 LT |
248 | md_wakeup_thread(mddev->thread); |
249 | } | |
250 | ||
251 | /* | |
252 | * raid_end_bio_io() is called when we have finished servicing a mirrored | |
253 | * operation and are ready to return a success/failure code to the buffer | |
254 | * cache layer. | |
255 | */ | |
9f2c9d12 | 256 | static void raid_end_bio_io(struct r10bio *r10_bio) |
1da177e4 LT |
257 | { |
258 | struct bio *bio = r10_bio->master_bio; | |
856e08e2 | 259 | int done; |
e879a879 | 260 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 | 261 | |
856e08e2 N |
262 | if (bio->bi_phys_segments) { |
263 | unsigned long flags; | |
264 | spin_lock_irqsave(&conf->device_lock, flags); | |
265 | bio->bi_phys_segments--; | |
266 | done = (bio->bi_phys_segments == 0); | |
267 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
268 | } else | |
269 | done = 1; | |
270 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) | |
271 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | |
272 | if (done) { | |
273 | bio_endio(bio, 0); | |
274 | /* | |
275 | * Wake up any possible resync thread that waits for the device | |
276 | * to go idle. | |
277 | */ | |
278 | allow_barrier(conf); | |
279 | } | |
1da177e4 LT |
280 | free_r10bio(r10_bio); |
281 | } | |
282 | ||
283 | /* | |
284 | * Update disk head position estimator based on IRQ completion info. | |
285 | */ | |
9f2c9d12 | 286 | static inline void update_head_pos(int slot, struct r10bio *r10_bio) |
1da177e4 | 287 | { |
e879a879 | 288 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 LT |
289 | |
290 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = | |
291 | r10_bio->devs[slot].addr + (r10_bio->sectors); | |
292 | } | |
293 | ||
778ca018 NK |
294 | /* |
295 | * Find the disk number which triggered given bio | |
296 | */ | |
e879a879 | 297 | static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio, |
69335ef3 | 298 | struct bio *bio, int *slotp, int *replp) |
778ca018 NK |
299 | { |
300 | int slot; | |
69335ef3 | 301 | int repl = 0; |
778ca018 | 302 | |
69335ef3 | 303 | for (slot = 0; slot < conf->copies; slot++) { |
778ca018 NK |
304 | if (r10_bio->devs[slot].bio == bio) |
305 | break; | |
69335ef3 N |
306 | if (r10_bio->devs[slot].repl_bio == bio) { |
307 | repl = 1; | |
308 | break; | |
309 | } | |
310 | } | |
778ca018 NK |
311 | |
312 | BUG_ON(slot == conf->copies); | |
313 | update_head_pos(slot, r10_bio); | |
314 | ||
749c55e9 N |
315 | if (slotp) |
316 | *slotp = slot; | |
69335ef3 N |
317 | if (replp) |
318 | *replp = repl; | |
778ca018 NK |
319 | return r10_bio->devs[slot].devnum; |
320 | } | |
321 | ||
6712ecf8 | 322 | static void raid10_end_read_request(struct bio *bio, int error) |
1da177e4 LT |
323 | { |
324 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
9f2c9d12 | 325 | struct r10bio *r10_bio = bio->bi_private; |
1da177e4 | 326 | int slot, dev; |
abbf098e | 327 | struct md_rdev *rdev; |
e879a879 | 328 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 | 329 | |
1da177e4 LT |
330 | |
331 | slot = r10_bio->read_slot; | |
332 | dev = r10_bio->devs[slot].devnum; | |
abbf098e | 333 | rdev = r10_bio->devs[slot].rdev; |
1da177e4 LT |
334 | /* |
335 | * this branch is our 'one mirror IO has finished' event handler: | |
336 | */ | |
4443ae10 N |
337 | update_head_pos(slot, r10_bio); |
338 | ||
339 | if (uptodate) { | |
1da177e4 LT |
340 | /* |
341 | * Set R10BIO_Uptodate in our master bio, so that | |
342 | * we will return a good error code to the higher | |
343 | * levels even if IO on some other mirrored buffer fails. | |
344 | * | |
345 | * The 'master' represents the composite IO operation to | |
346 | * user-side. So if something waits for IO, then it will | |
347 | * wait for the 'master' bio. | |
348 | */ | |
349 | set_bit(R10BIO_Uptodate, &r10_bio->state); | |
1da177e4 | 350 | raid_end_bio_io(r10_bio); |
abbf098e | 351 | rdev_dec_pending(rdev, conf->mddev); |
4443ae10 | 352 | } else { |
1da177e4 | 353 | /* |
7c4e06ff | 354 | * oops, read error - keep the refcount on the rdev |
1da177e4 LT |
355 | */ |
356 | char b[BDEVNAME_SIZE]; | |
8bda470e CD |
357 | printk_ratelimited(KERN_ERR |
358 | "md/raid10:%s: %s: rescheduling sector %llu\n", | |
359 | mdname(conf->mddev), | |
abbf098e | 360 | bdevname(rdev->bdev, b), |
8bda470e | 361 | (unsigned long long)r10_bio->sector); |
856e08e2 | 362 | set_bit(R10BIO_ReadError, &r10_bio->state); |
1da177e4 LT |
363 | reschedule_retry(r10_bio); |
364 | } | |
1da177e4 LT |
365 | } |
366 | ||
9f2c9d12 | 367 | static void close_write(struct r10bio *r10_bio) |
bd870a16 N |
368 | { |
369 | /* clear the bitmap if all writes complete successfully */ | |
370 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, | |
371 | r10_bio->sectors, | |
372 | !test_bit(R10BIO_Degraded, &r10_bio->state), | |
373 | 0); | |
374 | md_write_end(r10_bio->mddev); | |
375 | } | |
376 | ||
9f2c9d12 | 377 | static void one_write_done(struct r10bio *r10_bio) |
19d5f834 N |
378 | { |
379 | if (atomic_dec_and_test(&r10_bio->remaining)) { | |
380 | if (test_bit(R10BIO_WriteError, &r10_bio->state)) | |
381 | reschedule_retry(r10_bio); | |
382 | else { | |
383 | close_write(r10_bio); | |
384 | if (test_bit(R10BIO_MadeGood, &r10_bio->state)) | |
385 | reschedule_retry(r10_bio); | |
386 | else | |
387 | raid_end_bio_io(r10_bio); | |
388 | } | |
389 | } | |
390 | } | |
391 | ||
6712ecf8 | 392 | static void raid10_end_write_request(struct bio *bio, int error) |
1da177e4 LT |
393 | { |
394 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
9f2c9d12 | 395 | struct r10bio *r10_bio = bio->bi_private; |
778ca018 | 396 | int dev; |
749c55e9 | 397 | int dec_rdev = 1; |
e879a879 | 398 | struct r10conf *conf = r10_bio->mddev->private; |
475b0321 | 399 | int slot, repl; |
4ca40c2c | 400 | struct md_rdev *rdev = NULL; |
1da177e4 | 401 | |
475b0321 | 402 | dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
1da177e4 | 403 | |
475b0321 N |
404 | if (repl) |
405 | rdev = conf->mirrors[dev].replacement; | |
4ca40c2c N |
406 | if (!rdev) { |
407 | smp_rmb(); | |
408 | repl = 0; | |
475b0321 | 409 | rdev = conf->mirrors[dev].rdev; |
4ca40c2c | 410 | } |
1da177e4 LT |
411 | /* |
412 | * this branch is our 'one mirror IO has finished' event handler: | |
413 | */ | |
6cce3b23 | 414 | if (!uptodate) { |
475b0321 N |
415 | if (repl) |
416 | /* Never record new bad blocks to replacement, | |
417 | * just fail it. | |
418 | */ | |
419 | md_error(rdev->mddev, rdev); | |
420 | else { | |
421 | set_bit(WriteErrorSeen, &rdev->flags); | |
b7044d41 N |
422 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
423 | set_bit(MD_RECOVERY_NEEDED, | |
424 | &rdev->mddev->recovery); | |
475b0321 N |
425 | set_bit(R10BIO_WriteError, &r10_bio->state); |
426 | dec_rdev = 0; | |
427 | } | |
749c55e9 | 428 | } else { |
1da177e4 LT |
429 | /* |
430 | * Set R10BIO_Uptodate in our master bio, so that | |
431 | * we will return a good error code for to the higher | |
432 | * levels even if IO on some other mirrored buffer fails. | |
433 | * | |
434 | * The 'master' represents the composite IO operation to | |
435 | * user-side. So if something waits for IO, then it will | |
436 | * wait for the 'master' bio. | |
437 | */ | |
749c55e9 N |
438 | sector_t first_bad; |
439 | int bad_sectors; | |
440 | ||
1da177e4 LT |
441 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
442 | ||
749c55e9 | 443 | /* Maybe we can clear some bad blocks. */ |
475b0321 | 444 | if (is_badblock(rdev, |
749c55e9 N |
445 | r10_bio->devs[slot].addr, |
446 | r10_bio->sectors, | |
447 | &first_bad, &bad_sectors)) { | |
448 | bio_put(bio); | |
475b0321 N |
449 | if (repl) |
450 | r10_bio->devs[slot].repl_bio = IO_MADE_GOOD; | |
451 | else | |
452 | r10_bio->devs[slot].bio = IO_MADE_GOOD; | |
749c55e9 N |
453 | dec_rdev = 0; |
454 | set_bit(R10BIO_MadeGood, &r10_bio->state); | |
455 | } | |
456 | } | |
457 | ||
1da177e4 LT |
458 | /* |
459 | * | |
460 | * Let's see if all mirrored write operations have finished | |
461 | * already. | |
462 | */ | |
19d5f834 | 463 | one_write_done(r10_bio); |
749c55e9 N |
464 | if (dec_rdev) |
465 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); | |
1da177e4 LT |
466 | } |
467 | ||
1da177e4 LT |
468 | /* |
469 | * RAID10 layout manager | |
25985edc | 470 | * As well as the chunksize and raid_disks count, there are two |
1da177e4 LT |
471 | * parameters: near_copies and far_copies. |
472 | * near_copies * far_copies must be <= raid_disks. | |
473 | * Normally one of these will be 1. | |
474 | * If both are 1, we get raid0. | |
475 | * If near_copies == raid_disks, we get raid1. | |
476 | * | |
25985edc | 477 | * Chunks are laid out in raid0 style with near_copies copies of the |
1da177e4 LT |
478 | * first chunk, followed by near_copies copies of the next chunk and |
479 | * so on. | |
480 | * If far_copies > 1, then after 1/far_copies of the array has been assigned | |
481 | * as described above, we start again with a device offset of near_copies. | |
482 | * So we effectively have another copy of the whole array further down all | |
483 | * the drives, but with blocks on different drives. | |
484 | * With this layout, and block is never stored twice on the one device. | |
485 | * | |
486 | * raid10_find_phys finds the sector offset of a given virtual sector | |
c93983bf | 487 | * on each device that it is on. |
1da177e4 LT |
488 | * |
489 | * raid10_find_virt does the reverse mapping, from a device and a | |
490 | * sector offset to a virtual address | |
491 | */ | |
492 | ||
e879a879 | 493 | static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio) |
1da177e4 LT |
494 | { |
495 | int n,f; | |
496 | sector_t sector; | |
497 | sector_t chunk; | |
498 | sector_t stripe; | |
499 | int dev; | |
500 | ||
501 | int slot = 0; | |
502 | ||
503 | /* now calculate first sector/dev */ | |
504 | chunk = r10bio->sector >> conf->chunk_shift; | |
505 | sector = r10bio->sector & conf->chunk_mask; | |
506 | ||
507 | chunk *= conf->near_copies; | |
508 | stripe = chunk; | |
509 | dev = sector_div(stripe, conf->raid_disks); | |
c93983bf N |
510 | if (conf->far_offset) |
511 | stripe *= conf->far_copies; | |
1da177e4 LT |
512 | |
513 | sector += stripe << conf->chunk_shift; | |
514 | ||
515 | /* and calculate all the others */ | |
516 | for (n=0; n < conf->near_copies; n++) { | |
517 | int d = dev; | |
518 | sector_t s = sector; | |
519 | r10bio->devs[slot].addr = sector; | |
520 | r10bio->devs[slot].devnum = d; | |
521 | slot++; | |
522 | ||
523 | for (f = 1; f < conf->far_copies; f++) { | |
524 | d += conf->near_copies; | |
525 | if (d >= conf->raid_disks) | |
526 | d -= conf->raid_disks; | |
527 | s += conf->stride; | |
528 | r10bio->devs[slot].devnum = d; | |
529 | r10bio->devs[slot].addr = s; | |
530 | slot++; | |
531 | } | |
532 | dev++; | |
533 | if (dev >= conf->raid_disks) { | |
534 | dev = 0; | |
535 | sector += (conf->chunk_mask + 1); | |
536 | } | |
537 | } | |
538 | BUG_ON(slot != conf->copies); | |
539 | } | |
540 | ||
e879a879 | 541 | static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev) |
1da177e4 LT |
542 | { |
543 | sector_t offset, chunk, vchunk; | |
544 | ||
1da177e4 | 545 | offset = sector & conf->chunk_mask; |
c93983bf N |
546 | if (conf->far_offset) { |
547 | int fc; | |
548 | chunk = sector >> conf->chunk_shift; | |
549 | fc = sector_div(chunk, conf->far_copies); | |
550 | dev -= fc * conf->near_copies; | |
551 | if (dev < 0) | |
552 | dev += conf->raid_disks; | |
553 | } else { | |
64a742bc | 554 | while (sector >= conf->stride) { |
c93983bf N |
555 | sector -= conf->stride; |
556 | if (dev < conf->near_copies) | |
557 | dev += conf->raid_disks - conf->near_copies; | |
558 | else | |
559 | dev -= conf->near_copies; | |
560 | } | |
561 | chunk = sector >> conf->chunk_shift; | |
562 | } | |
1da177e4 LT |
563 | vchunk = chunk * conf->raid_disks + dev; |
564 | sector_div(vchunk, conf->near_copies); | |
565 | return (vchunk << conf->chunk_shift) + offset; | |
566 | } | |
567 | ||
568 | /** | |
569 | * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged | |
570 | * @q: request queue | |
cc371e66 | 571 | * @bvm: properties of new bio |
1da177e4 LT |
572 | * @biovec: the request that could be merged to it. |
573 | * | |
574 | * Return amount of bytes we can accept at this offset | |
575 | * If near_copies == raid_disk, there are no striping issues, | |
576 | * but in that case, the function isn't called at all. | |
577 | */ | |
cc371e66 AK |
578 | static int raid10_mergeable_bvec(struct request_queue *q, |
579 | struct bvec_merge_data *bvm, | |
580 | struct bio_vec *biovec) | |
1da177e4 | 581 | { |
fd01b88c | 582 | struct mddev *mddev = q->queuedata; |
cc371e66 | 583 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); |
1da177e4 | 584 | int max; |
9d8f0363 | 585 | unsigned int chunk_sectors = mddev->chunk_sectors; |
cc371e66 | 586 | unsigned int bio_sectors = bvm->bi_size >> 9; |
1da177e4 LT |
587 | |
588 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; | |
589 | if (max < 0) max = 0; /* bio_add cannot handle a negative return */ | |
cc371e66 AK |
590 | if (max <= biovec->bv_len && bio_sectors == 0) |
591 | return biovec->bv_len; | |
1da177e4 LT |
592 | else |
593 | return max; | |
594 | } | |
595 | ||
596 | /* | |
597 | * This routine returns the disk from which the requested read should | |
598 | * be done. There is a per-array 'next expected sequential IO' sector | |
599 | * number - if this matches on the next IO then we use the last disk. | |
600 | * There is also a per-disk 'last know head position' sector that is | |
601 | * maintained from IRQ contexts, both the normal and the resync IO | |
602 | * completion handlers update this position correctly. If there is no | |
603 | * perfect sequential match then we pick the disk whose head is closest. | |
604 | * | |
605 | * If there are 2 mirrors in the same 2 devices, performance degrades | |
606 | * because position is mirror, not device based. | |
607 | * | |
608 | * The rdev for the device selected will have nr_pending incremented. | |
609 | */ | |
610 | ||
611 | /* | |
612 | * FIXME: possibly should rethink readbalancing and do it differently | |
613 | * depending on near_copies / far_copies geometry. | |
614 | */ | |
96c3fd1f N |
615 | static struct md_rdev *read_balance(struct r10conf *conf, |
616 | struct r10bio *r10_bio, | |
617 | int *max_sectors) | |
1da177e4 | 618 | { |
af3a2cd6 | 619 | const sector_t this_sector = r10_bio->sector; |
56d99121 | 620 | int disk, slot; |
856e08e2 N |
621 | int sectors = r10_bio->sectors; |
622 | int best_good_sectors; | |
56d99121 | 623 | sector_t new_distance, best_dist; |
abbf098e | 624 | struct md_rdev *rdev, *best_rdev; |
56d99121 N |
625 | int do_balance; |
626 | int best_slot; | |
1da177e4 LT |
627 | |
628 | raid10_find_phys(conf, r10_bio); | |
629 | rcu_read_lock(); | |
56d99121 | 630 | retry: |
856e08e2 | 631 | sectors = r10_bio->sectors; |
56d99121 | 632 | best_slot = -1; |
abbf098e | 633 | best_rdev = NULL; |
56d99121 | 634 | best_dist = MaxSector; |
856e08e2 | 635 | best_good_sectors = 0; |
56d99121 | 636 | do_balance = 1; |
1da177e4 LT |
637 | /* |
638 | * Check if we can balance. We can balance on the whole | |
6cce3b23 N |
639 | * device if no resync is going on (recovery is ok), or below |
640 | * the resync window. We take the first readable disk when | |
641 | * above the resync window. | |
1da177e4 LT |
642 | */ |
643 | if (conf->mddev->recovery_cp < MaxSector | |
56d99121 N |
644 | && (this_sector + sectors >= conf->next_resync)) |
645 | do_balance = 0; | |
1da177e4 | 646 | |
56d99121 | 647 | for (slot = 0; slot < conf->copies ; slot++) { |
856e08e2 N |
648 | sector_t first_bad; |
649 | int bad_sectors; | |
650 | sector_t dev_sector; | |
651 | ||
56d99121 N |
652 | if (r10_bio->devs[slot].bio == IO_BLOCKED) |
653 | continue; | |
1da177e4 | 654 | disk = r10_bio->devs[slot].devnum; |
abbf098e N |
655 | rdev = rcu_dereference(conf->mirrors[disk].replacement); |
656 | if (rdev == NULL || test_bit(Faulty, &rdev->flags) || | |
657 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) | |
658 | rdev = rcu_dereference(conf->mirrors[disk].rdev); | |
56d99121 | 659 | if (rdev == NULL) |
1da177e4 | 660 | continue; |
abbf098e N |
661 | if (test_bit(Faulty, &rdev->flags)) |
662 | continue; | |
663 | if (!test_bit(In_sync, &rdev->flags) && | |
664 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) | |
56d99121 N |
665 | continue; |
666 | ||
856e08e2 N |
667 | dev_sector = r10_bio->devs[slot].addr; |
668 | if (is_badblock(rdev, dev_sector, sectors, | |
669 | &first_bad, &bad_sectors)) { | |
670 | if (best_dist < MaxSector) | |
671 | /* Already have a better slot */ | |
672 | continue; | |
673 | if (first_bad <= dev_sector) { | |
674 | /* Cannot read here. If this is the | |
675 | * 'primary' device, then we must not read | |
676 | * beyond 'bad_sectors' from another device. | |
677 | */ | |
678 | bad_sectors -= (dev_sector - first_bad); | |
679 | if (!do_balance && sectors > bad_sectors) | |
680 | sectors = bad_sectors; | |
681 | if (best_good_sectors > sectors) | |
682 | best_good_sectors = sectors; | |
683 | } else { | |
684 | sector_t good_sectors = | |
685 | first_bad - dev_sector; | |
686 | if (good_sectors > best_good_sectors) { | |
687 | best_good_sectors = good_sectors; | |
688 | best_slot = slot; | |
abbf098e | 689 | best_rdev = rdev; |
856e08e2 N |
690 | } |
691 | if (!do_balance) | |
692 | /* Must read from here */ | |
693 | break; | |
694 | } | |
695 | continue; | |
696 | } else | |
697 | best_good_sectors = sectors; | |
698 | ||
56d99121 N |
699 | if (!do_balance) |
700 | break; | |
1da177e4 | 701 | |
22dfdf52 N |
702 | /* This optimisation is debatable, and completely destroys |
703 | * sequential read speed for 'far copies' arrays. So only | |
704 | * keep it for 'near' arrays, and review those later. | |
705 | */ | |
56d99121 | 706 | if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) |
1da177e4 | 707 | break; |
8ed3a195 KS |
708 | |
709 | /* for far > 1 always use the lowest address */ | |
710 | if (conf->far_copies > 1) | |
56d99121 | 711 | new_distance = r10_bio->devs[slot].addr; |
8ed3a195 | 712 | else |
56d99121 N |
713 | new_distance = abs(r10_bio->devs[slot].addr - |
714 | conf->mirrors[disk].head_position); | |
715 | if (new_distance < best_dist) { | |
716 | best_dist = new_distance; | |
717 | best_slot = slot; | |
abbf098e | 718 | best_rdev = rdev; |
1da177e4 LT |
719 | } |
720 | } | |
abbf098e | 721 | if (slot >= conf->copies) { |
56d99121 | 722 | slot = best_slot; |
abbf098e N |
723 | rdev = best_rdev; |
724 | } | |
1da177e4 | 725 | |
56d99121 | 726 | if (slot >= 0) { |
56d99121 N |
727 | atomic_inc(&rdev->nr_pending); |
728 | if (test_bit(Faulty, &rdev->flags)) { | |
729 | /* Cannot risk returning a device that failed | |
730 | * before we inc'ed nr_pending | |
731 | */ | |
732 | rdev_dec_pending(rdev, conf->mddev); | |
733 | goto retry; | |
734 | } | |
735 | r10_bio->read_slot = slot; | |
736 | } else | |
96c3fd1f | 737 | rdev = NULL; |
1da177e4 | 738 | rcu_read_unlock(); |
856e08e2 | 739 | *max_sectors = best_good_sectors; |
1da177e4 | 740 | |
96c3fd1f | 741 | return rdev; |
1da177e4 LT |
742 | } |
743 | ||
0d129228 N |
744 | static int raid10_congested(void *data, int bits) |
745 | { | |
fd01b88c | 746 | struct mddev *mddev = data; |
e879a879 | 747 | struct r10conf *conf = mddev->private; |
0d129228 N |
748 | int i, ret = 0; |
749 | ||
34db0cd6 N |
750 | if ((bits & (1 << BDI_async_congested)) && |
751 | conf->pending_count >= max_queued_requests) | |
752 | return 1; | |
753 | ||
3fa841d7 N |
754 | if (mddev_congested(mddev, bits)) |
755 | return 1; | |
0d129228 | 756 | rcu_read_lock(); |
84707f38 | 757 | for (i = 0; i < conf->raid_disks && ret == 0; i++) { |
3cb03002 | 758 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
0d129228 | 759 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
165125e1 | 760 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
0d129228 N |
761 | |
762 | ret |= bdi_congested(&q->backing_dev_info, bits); | |
763 | } | |
764 | } | |
765 | rcu_read_unlock(); | |
766 | return ret; | |
767 | } | |
768 | ||
e879a879 | 769 | static void flush_pending_writes(struct r10conf *conf) |
a35e63ef N |
770 | { |
771 | /* Any writes that have been queued but are awaiting | |
772 | * bitmap updates get flushed here. | |
a35e63ef | 773 | */ |
a35e63ef N |
774 | spin_lock_irq(&conf->device_lock); |
775 | ||
776 | if (conf->pending_bio_list.head) { | |
777 | struct bio *bio; | |
778 | bio = bio_list_get(&conf->pending_bio_list); | |
34db0cd6 | 779 | conf->pending_count = 0; |
a35e63ef N |
780 | spin_unlock_irq(&conf->device_lock); |
781 | /* flush any pending bitmap writes to disk | |
782 | * before proceeding w/ I/O */ | |
783 | bitmap_unplug(conf->mddev->bitmap); | |
34db0cd6 | 784 | wake_up(&conf->wait_barrier); |
a35e63ef N |
785 | |
786 | while (bio) { /* submit pending writes */ | |
787 | struct bio *next = bio->bi_next; | |
788 | bio->bi_next = NULL; | |
789 | generic_make_request(bio); | |
790 | bio = next; | |
791 | } | |
a35e63ef N |
792 | } else |
793 | spin_unlock_irq(&conf->device_lock); | |
a35e63ef | 794 | } |
7eaceacc | 795 | |
0a27ec96 N |
796 | /* Barriers.... |
797 | * Sometimes we need to suspend IO while we do something else, | |
798 | * either some resync/recovery, or reconfigure the array. | |
799 | * To do this we raise a 'barrier'. | |
800 | * The 'barrier' is a counter that can be raised multiple times | |
801 | * to count how many activities are happening which preclude | |
802 | * normal IO. | |
803 | * We can only raise the barrier if there is no pending IO. | |
804 | * i.e. if nr_pending == 0. | |
805 | * We choose only to raise the barrier if no-one is waiting for the | |
806 | * barrier to go down. This means that as soon as an IO request | |
807 | * is ready, no other operations which require a barrier will start | |
808 | * until the IO request has had a chance. | |
809 | * | |
810 | * So: regular IO calls 'wait_barrier'. When that returns there | |
811 | * is no backgroup IO happening, It must arrange to call | |
812 | * allow_barrier when it has finished its IO. | |
813 | * backgroup IO calls must call raise_barrier. Once that returns | |
814 | * there is no normal IO happeing. It must arrange to call | |
815 | * lower_barrier when the particular background IO completes. | |
1da177e4 | 816 | */ |
1da177e4 | 817 | |
e879a879 | 818 | static void raise_barrier(struct r10conf *conf, int force) |
1da177e4 | 819 | { |
6cce3b23 | 820 | BUG_ON(force && !conf->barrier); |
1da177e4 | 821 | spin_lock_irq(&conf->resync_lock); |
0a27ec96 | 822 | |
6cce3b23 N |
823 | /* Wait until no block IO is waiting (unless 'force') */ |
824 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, | |
c3b328ac | 825 | conf->resync_lock, ); |
0a27ec96 N |
826 | |
827 | /* block any new IO from starting */ | |
828 | conf->barrier++; | |
829 | ||
c3b328ac | 830 | /* Now wait for all pending IO to complete */ |
0a27ec96 N |
831 | wait_event_lock_irq(conf->wait_barrier, |
832 | !conf->nr_pending && conf->barrier < RESYNC_DEPTH, | |
c3b328ac | 833 | conf->resync_lock, ); |
0a27ec96 N |
834 | |
835 | spin_unlock_irq(&conf->resync_lock); | |
836 | } | |
837 | ||
e879a879 | 838 | static void lower_barrier(struct r10conf *conf) |
0a27ec96 N |
839 | { |
840 | unsigned long flags; | |
841 | spin_lock_irqsave(&conf->resync_lock, flags); | |
842 | conf->barrier--; | |
843 | spin_unlock_irqrestore(&conf->resync_lock, flags); | |
844 | wake_up(&conf->wait_barrier); | |
845 | } | |
846 | ||
e879a879 | 847 | static void wait_barrier(struct r10conf *conf) |
0a27ec96 N |
848 | { |
849 | spin_lock_irq(&conf->resync_lock); | |
850 | if (conf->barrier) { | |
851 | conf->nr_waiting++; | |
852 | wait_event_lock_irq(conf->wait_barrier, !conf->barrier, | |
853 | conf->resync_lock, | |
c3b328ac | 854 | ); |
0a27ec96 | 855 | conf->nr_waiting--; |
1da177e4 | 856 | } |
0a27ec96 | 857 | conf->nr_pending++; |
1da177e4 LT |
858 | spin_unlock_irq(&conf->resync_lock); |
859 | } | |
860 | ||
e879a879 | 861 | static void allow_barrier(struct r10conf *conf) |
0a27ec96 N |
862 | { |
863 | unsigned long flags; | |
864 | spin_lock_irqsave(&conf->resync_lock, flags); | |
865 | conf->nr_pending--; | |
866 | spin_unlock_irqrestore(&conf->resync_lock, flags); | |
867 | wake_up(&conf->wait_barrier); | |
868 | } | |
869 | ||
e879a879 | 870 | static void freeze_array(struct r10conf *conf) |
4443ae10 N |
871 | { |
872 | /* stop syncio and normal IO and wait for everything to | |
f188593e | 873 | * go quiet. |
4443ae10 | 874 | * We increment barrier and nr_waiting, and then |
1c830532 N |
875 | * wait until nr_pending match nr_queued+1 |
876 | * This is called in the context of one normal IO request | |
877 | * that has failed. Thus any sync request that might be pending | |
878 | * will be blocked by nr_pending, and we need to wait for | |
879 | * pending IO requests to complete or be queued for re-try. | |
880 | * Thus the number queued (nr_queued) plus this request (1) | |
881 | * must match the number of pending IOs (nr_pending) before | |
882 | * we continue. | |
4443ae10 N |
883 | */ |
884 | spin_lock_irq(&conf->resync_lock); | |
885 | conf->barrier++; | |
886 | conf->nr_waiting++; | |
887 | wait_event_lock_irq(conf->wait_barrier, | |
1c830532 | 888 | conf->nr_pending == conf->nr_queued+1, |
4443ae10 | 889 | conf->resync_lock, |
c3b328ac N |
890 | flush_pending_writes(conf)); |
891 | ||
4443ae10 N |
892 | spin_unlock_irq(&conf->resync_lock); |
893 | } | |
894 | ||
e879a879 | 895 | static void unfreeze_array(struct r10conf *conf) |
4443ae10 N |
896 | { |
897 | /* reverse the effect of the freeze */ | |
898 | spin_lock_irq(&conf->resync_lock); | |
899 | conf->barrier--; | |
900 | conf->nr_waiting--; | |
901 | wake_up(&conf->wait_barrier); | |
902 | spin_unlock_irq(&conf->resync_lock); | |
903 | } | |
904 | ||
b4fdcb02 | 905 | static void make_request(struct mddev *mddev, struct bio * bio) |
1da177e4 | 906 | { |
e879a879 | 907 | struct r10conf *conf = mddev->private; |
9f2c9d12 | 908 | struct r10bio *r10_bio; |
1da177e4 LT |
909 | struct bio *read_bio; |
910 | int i; | |
911 | int chunk_sects = conf->chunk_mask + 1; | |
a362357b | 912 | const int rw = bio_data_dir(bio); |
2c7d46ec | 913 | const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); |
e9c7469b | 914 | const unsigned long do_fua = (bio->bi_rw & REQ_FUA); |
6cce3b23 | 915 | unsigned long flags; |
3cb03002 | 916 | struct md_rdev *blocked_rdev; |
c3b328ac | 917 | int plugged; |
d4432c23 N |
918 | int sectors_handled; |
919 | int max_sectors; | |
1da177e4 | 920 | |
e9c7469b TH |
921 | if (unlikely(bio->bi_rw & REQ_FLUSH)) { |
922 | md_flush_request(mddev, bio); | |
5a7bbad2 | 923 | return; |
e5dcdd80 N |
924 | } |
925 | ||
1da177e4 LT |
926 | /* If this request crosses a chunk boundary, we need to |
927 | * split it. This will only happen for 1 PAGE (or less) requests. | |
928 | */ | |
929 | if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9) | |
930 | > chunk_sects && | |
931 | conf->near_copies < conf->raid_disks)) { | |
932 | struct bio_pair *bp; | |
933 | /* Sanity check -- queue functions should prevent this happening */ | |
934 | if (bio->bi_vcnt != 1 || | |
935 | bio->bi_idx != 0) | |
936 | goto bad_map; | |
937 | /* This is a one page bio that upper layers | |
938 | * refuse to split for us, so we need to split it. | |
939 | */ | |
6feef531 | 940 | bp = bio_split(bio, |
1da177e4 | 941 | chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); |
51e9ac77 N |
942 | |
943 | /* Each of these 'make_request' calls will call 'wait_barrier'. | |
944 | * If the first succeeds but the second blocks due to the resync | |
945 | * thread raising the barrier, we will deadlock because the | |
946 | * IO to the underlying device will be queued in generic_make_request | |
947 | * and will never complete, so will never reduce nr_pending. | |
948 | * So increment nr_waiting here so no new raise_barriers will | |
949 | * succeed, and so the second wait_barrier cannot block. | |
950 | */ | |
951 | spin_lock_irq(&conf->resync_lock); | |
952 | conf->nr_waiting++; | |
953 | spin_unlock_irq(&conf->resync_lock); | |
954 | ||
5a7bbad2 CH |
955 | make_request(mddev, &bp->bio1); |
956 | make_request(mddev, &bp->bio2); | |
1da177e4 | 957 | |
51e9ac77 N |
958 | spin_lock_irq(&conf->resync_lock); |
959 | conf->nr_waiting--; | |
960 | wake_up(&conf->wait_barrier); | |
961 | spin_unlock_irq(&conf->resync_lock); | |
962 | ||
1da177e4 | 963 | bio_pair_release(bp); |
5a7bbad2 | 964 | return; |
1da177e4 | 965 | bad_map: |
128595ed N |
966 | printk("md/raid10:%s: make_request bug: can't convert block across chunks" |
967 | " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2, | |
1da177e4 LT |
968 | (unsigned long long)bio->bi_sector, bio->bi_size >> 10); |
969 | ||
6712ecf8 | 970 | bio_io_error(bio); |
5a7bbad2 | 971 | return; |
1da177e4 LT |
972 | } |
973 | ||
3d310eb7 | 974 | md_write_start(mddev, bio); |
06d91a5f | 975 | |
1da177e4 LT |
976 | /* |
977 | * Register the new request and wait if the reconstruction | |
978 | * thread has put up a bar for new requests. | |
979 | * Continue immediately if no resync is active currently. | |
980 | */ | |
0a27ec96 | 981 | wait_barrier(conf); |
1da177e4 | 982 | |
1da177e4 LT |
983 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
984 | ||
985 | r10_bio->master_bio = bio; | |
986 | r10_bio->sectors = bio->bi_size >> 9; | |
987 | ||
988 | r10_bio->mddev = mddev; | |
989 | r10_bio->sector = bio->bi_sector; | |
6cce3b23 | 990 | r10_bio->state = 0; |
1da177e4 | 991 | |
856e08e2 N |
992 | /* We might need to issue multiple reads to different |
993 | * devices if there are bad blocks around, so we keep | |
994 | * track of the number of reads in bio->bi_phys_segments. | |
995 | * If this is 0, there is only one r10_bio and no locking | |
996 | * will be needed when the request completes. If it is | |
997 | * non-zero, then it is the number of not-completed requests. | |
998 | */ | |
999 | bio->bi_phys_segments = 0; | |
1000 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); | |
1001 | ||
a362357b | 1002 | if (rw == READ) { |
1da177e4 LT |
1003 | /* |
1004 | * read balancing logic: | |
1005 | */ | |
96c3fd1f | 1006 | struct md_rdev *rdev; |
856e08e2 N |
1007 | int slot; |
1008 | ||
1009 | read_again: | |
96c3fd1f N |
1010 | rdev = read_balance(conf, r10_bio, &max_sectors); |
1011 | if (!rdev) { | |
1da177e4 | 1012 | raid_end_bio_io(r10_bio); |
5a7bbad2 | 1013 | return; |
1da177e4 | 1014 | } |
96c3fd1f | 1015 | slot = r10_bio->read_slot; |
1da177e4 | 1016 | |
a167f663 | 1017 | read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
856e08e2 N |
1018 | md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector, |
1019 | max_sectors); | |
1da177e4 LT |
1020 | |
1021 | r10_bio->devs[slot].bio = read_bio; | |
abbf098e | 1022 | r10_bio->devs[slot].rdev = rdev; |
1da177e4 LT |
1023 | |
1024 | read_bio->bi_sector = r10_bio->devs[slot].addr + | |
96c3fd1f N |
1025 | rdev->data_offset; |
1026 | read_bio->bi_bdev = rdev->bdev; | |
1da177e4 | 1027 | read_bio->bi_end_io = raid10_end_read_request; |
7b6d91da | 1028 | read_bio->bi_rw = READ | do_sync; |
1da177e4 LT |
1029 | read_bio->bi_private = r10_bio; |
1030 | ||
856e08e2 N |
1031 | if (max_sectors < r10_bio->sectors) { |
1032 | /* Could not read all from this device, so we will | |
1033 | * need another r10_bio. | |
1034 | */ | |
856e08e2 N |
1035 | sectors_handled = (r10_bio->sectors + max_sectors |
1036 | - bio->bi_sector); | |
1037 | r10_bio->sectors = max_sectors; | |
1038 | spin_lock_irq(&conf->device_lock); | |
1039 | if (bio->bi_phys_segments == 0) | |
1040 | bio->bi_phys_segments = 2; | |
1041 | else | |
1042 | bio->bi_phys_segments++; | |
1043 | spin_unlock(&conf->device_lock); | |
1044 | /* Cannot call generic_make_request directly | |
1045 | * as that will be queued in __generic_make_request | |
1046 | * and subsequent mempool_alloc might block | |
1047 | * waiting for it. so hand bio over to raid10d. | |
1048 | */ | |
1049 | reschedule_retry(r10_bio); | |
1050 | ||
1051 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); | |
1052 | ||
1053 | r10_bio->master_bio = bio; | |
1054 | r10_bio->sectors = ((bio->bi_size >> 9) | |
1055 | - sectors_handled); | |
1056 | r10_bio->state = 0; | |
1057 | r10_bio->mddev = mddev; | |
1058 | r10_bio->sector = bio->bi_sector + sectors_handled; | |
1059 | goto read_again; | |
1060 | } else | |
1061 | generic_make_request(read_bio); | |
5a7bbad2 | 1062 | return; |
1da177e4 LT |
1063 | } |
1064 | ||
1065 | /* | |
1066 | * WRITE: | |
1067 | */ | |
34db0cd6 N |
1068 | if (conf->pending_count >= max_queued_requests) { |
1069 | md_wakeup_thread(mddev->thread); | |
1070 | wait_event(conf->wait_barrier, | |
1071 | conf->pending_count < max_queued_requests); | |
1072 | } | |
6bfe0b49 | 1073 | /* first select target devices under rcu_lock and |
1da177e4 LT |
1074 | * inc refcount on their rdev. Record them by setting |
1075 | * bios[x] to bio | |
d4432c23 N |
1076 | * If there are known/acknowledged bad blocks on any device |
1077 | * on which we have seen a write error, we want to avoid | |
1078 | * writing to those blocks. This potentially requires several | |
1079 | * writes to write around the bad blocks. Each set of writes | |
1080 | * gets its own r10_bio with a set of bios attached. The number | |
1081 | * of r10_bios is recored in bio->bi_phys_segments just as with | |
1082 | * the read case. | |
1da177e4 | 1083 | */ |
c3b328ac N |
1084 | plugged = mddev_check_plugged(mddev); |
1085 | ||
69335ef3 | 1086 | r10_bio->read_slot = -1; /* make sure repl_bio gets freed */ |
1da177e4 | 1087 | raid10_find_phys(conf, r10_bio); |
d4432c23 | 1088 | retry_write: |
cb6969e8 | 1089 | blocked_rdev = NULL; |
1da177e4 | 1090 | rcu_read_lock(); |
d4432c23 N |
1091 | max_sectors = r10_bio->sectors; |
1092 | ||
1da177e4 LT |
1093 | for (i = 0; i < conf->copies; i++) { |
1094 | int d = r10_bio->devs[i].devnum; | |
3cb03002 | 1095 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
475b0321 N |
1096 | struct md_rdev *rrdev = rcu_dereference( |
1097 | conf->mirrors[d].replacement); | |
4ca40c2c N |
1098 | if (rdev == rrdev) |
1099 | rrdev = NULL; | |
6bfe0b49 DW |
1100 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
1101 | atomic_inc(&rdev->nr_pending); | |
1102 | blocked_rdev = rdev; | |
1103 | break; | |
1104 | } | |
475b0321 N |
1105 | if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) { |
1106 | atomic_inc(&rrdev->nr_pending); | |
1107 | blocked_rdev = rrdev; | |
1108 | break; | |
1109 | } | |
1110 | if (rrdev && test_bit(Faulty, &rrdev->flags)) | |
1111 | rrdev = NULL; | |
1112 | ||
d4432c23 | 1113 | r10_bio->devs[i].bio = NULL; |
475b0321 | 1114 | r10_bio->devs[i].repl_bio = NULL; |
d4432c23 | 1115 | if (!rdev || test_bit(Faulty, &rdev->flags)) { |
6cce3b23 | 1116 | set_bit(R10BIO_Degraded, &r10_bio->state); |
d4432c23 N |
1117 | continue; |
1118 | } | |
1119 | if (test_bit(WriteErrorSeen, &rdev->flags)) { | |
1120 | sector_t first_bad; | |
1121 | sector_t dev_sector = r10_bio->devs[i].addr; | |
1122 | int bad_sectors; | |
1123 | int is_bad; | |
1124 | ||
1125 | is_bad = is_badblock(rdev, dev_sector, | |
1126 | max_sectors, | |
1127 | &first_bad, &bad_sectors); | |
1128 | if (is_bad < 0) { | |
1129 | /* Mustn't write here until the bad block | |
1130 | * is acknowledged | |
1131 | */ | |
1132 | atomic_inc(&rdev->nr_pending); | |
1133 | set_bit(BlockedBadBlocks, &rdev->flags); | |
1134 | blocked_rdev = rdev; | |
1135 | break; | |
1136 | } | |
1137 | if (is_bad && first_bad <= dev_sector) { | |
1138 | /* Cannot write here at all */ | |
1139 | bad_sectors -= (dev_sector - first_bad); | |
1140 | if (bad_sectors < max_sectors) | |
1141 | /* Mustn't write more than bad_sectors | |
1142 | * to other devices yet | |
1143 | */ | |
1144 | max_sectors = bad_sectors; | |
1145 | /* We don't set R10BIO_Degraded as that | |
1146 | * only applies if the disk is missing, | |
1147 | * so it might be re-added, and we want to | |
1148 | * know to recover this chunk. | |
1149 | * In this case the device is here, and the | |
1150 | * fact that this chunk is not in-sync is | |
1151 | * recorded in the bad block log. | |
1152 | */ | |
1153 | continue; | |
1154 | } | |
1155 | if (is_bad) { | |
1156 | int good_sectors = first_bad - dev_sector; | |
1157 | if (good_sectors < max_sectors) | |
1158 | max_sectors = good_sectors; | |
1159 | } | |
6cce3b23 | 1160 | } |
d4432c23 N |
1161 | r10_bio->devs[i].bio = bio; |
1162 | atomic_inc(&rdev->nr_pending); | |
475b0321 N |
1163 | if (rrdev) { |
1164 | r10_bio->devs[i].repl_bio = bio; | |
1165 | atomic_inc(&rrdev->nr_pending); | |
1166 | } | |
1da177e4 LT |
1167 | } |
1168 | rcu_read_unlock(); | |
1169 | ||
6bfe0b49 DW |
1170 | if (unlikely(blocked_rdev)) { |
1171 | /* Have to wait for this device to get unblocked, then retry */ | |
1172 | int j; | |
1173 | int d; | |
1174 | ||
475b0321 | 1175 | for (j = 0; j < i; j++) { |
6bfe0b49 DW |
1176 | if (r10_bio->devs[j].bio) { |
1177 | d = r10_bio->devs[j].devnum; | |
1178 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); | |
1179 | } | |
475b0321 | 1180 | if (r10_bio->devs[j].repl_bio) { |
4ca40c2c | 1181 | struct md_rdev *rdev; |
475b0321 | 1182 | d = r10_bio->devs[j].devnum; |
4ca40c2c N |
1183 | rdev = conf->mirrors[d].replacement; |
1184 | if (!rdev) { | |
1185 | /* Race with remove_disk */ | |
1186 | smp_mb(); | |
1187 | rdev = conf->mirrors[d].rdev; | |
1188 | } | |
1189 | rdev_dec_pending(rdev, mddev); | |
475b0321 N |
1190 | } |
1191 | } | |
6bfe0b49 DW |
1192 | allow_barrier(conf); |
1193 | md_wait_for_blocked_rdev(blocked_rdev, mddev); | |
1194 | wait_barrier(conf); | |
1195 | goto retry_write; | |
1196 | } | |
1197 | ||
d4432c23 N |
1198 | if (max_sectors < r10_bio->sectors) { |
1199 | /* We are splitting this into multiple parts, so | |
1200 | * we need to prepare for allocating another r10_bio. | |
1201 | */ | |
1202 | r10_bio->sectors = max_sectors; | |
1203 | spin_lock_irq(&conf->device_lock); | |
1204 | if (bio->bi_phys_segments == 0) | |
1205 | bio->bi_phys_segments = 2; | |
1206 | else | |
1207 | bio->bi_phys_segments++; | |
1208 | spin_unlock_irq(&conf->device_lock); | |
1209 | } | |
1210 | sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector; | |
1211 | ||
4e78064f | 1212 | atomic_set(&r10_bio->remaining, 1); |
d4432c23 | 1213 | bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0); |
06d91a5f | 1214 | |
1da177e4 LT |
1215 | for (i = 0; i < conf->copies; i++) { |
1216 | struct bio *mbio; | |
1217 | int d = r10_bio->devs[i].devnum; | |
1218 | if (!r10_bio->devs[i].bio) | |
1219 | continue; | |
1220 | ||
a167f663 | 1221 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
d4432c23 N |
1222 | md_trim_bio(mbio, r10_bio->sector - bio->bi_sector, |
1223 | max_sectors); | |
1da177e4 LT |
1224 | r10_bio->devs[i].bio = mbio; |
1225 | ||
d4432c23 N |
1226 | mbio->bi_sector = (r10_bio->devs[i].addr+ |
1227 | conf->mirrors[d].rdev->data_offset); | |
1da177e4 LT |
1228 | mbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
1229 | mbio->bi_end_io = raid10_end_write_request; | |
e9c7469b | 1230 | mbio->bi_rw = WRITE | do_sync | do_fua; |
1da177e4 LT |
1231 | mbio->bi_private = r10_bio; |
1232 | ||
1233 | atomic_inc(&r10_bio->remaining); | |
4e78064f N |
1234 | spin_lock_irqsave(&conf->device_lock, flags); |
1235 | bio_list_add(&conf->pending_bio_list, mbio); | |
34db0cd6 | 1236 | conf->pending_count++; |
4e78064f | 1237 | spin_unlock_irqrestore(&conf->device_lock, flags); |
475b0321 N |
1238 | |
1239 | if (!r10_bio->devs[i].repl_bio) | |
1240 | continue; | |
1241 | ||
1242 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); | |
1243 | md_trim_bio(mbio, r10_bio->sector - bio->bi_sector, | |
1244 | max_sectors); | |
1245 | r10_bio->devs[i].repl_bio = mbio; | |
1246 | ||
4ca40c2c N |
1247 | /* We are actively writing to the original device |
1248 | * so it cannot disappear, so the replacement cannot | |
1249 | * become NULL here | |
1250 | */ | |
475b0321 N |
1251 | mbio->bi_sector = (r10_bio->devs[i].addr+ |
1252 | conf->mirrors[d].replacement->data_offset); | |
1253 | mbio->bi_bdev = conf->mirrors[d].replacement->bdev; | |
1254 | mbio->bi_end_io = raid10_end_write_request; | |
1255 | mbio->bi_rw = WRITE | do_sync | do_fua; | |
1256 | mbio->bi_private = r10_bio; | |
1257 | ||
1258 | atomic_inc(&r10_bio->remaining); | |
1259 | spin_lock_irqsave(&conf->device_lock, flags); | |
1260 | bio_list_add(&conf->pending_bio_list, mbio); | |
1261 | conf->pending_count++; | |
1262 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 LT |
1263 | } |
1264 | ||
079fa166 N |
1265 | /* Don't remove the bias on 'remaining' (one_write_done) until |
1266 | * after checking if we need to go around again. | |
1267 | */ | |
a35e63ef | 1268 | |
d4432c23 | 1269 | if (sectors_handled < (bio->bi_size >> 9)) { |
079fa166 | 1270 | one_write_done(r10_bio); |
5e570289 | 1271 | /* We need another r10_bio. It has already been counted |
d4432c23 N |
1272 | * in bio->bi_phys_segments. |
1273 | */ | |
1274 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); | |
1275 | ||
1276 | r10_bio->master_bio = bio; | |
1277 | r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled; | |
1278 | ||
1279 | r10_bio->mddev = mddev; | |
1280 | r10_bio->sector = bio->bi_sector + sectors_handled; | |
1281 | r10_bio->state = 0; | |
1282 | goto retry_write; | |
1283 | } | |
079fa166 N |
1284 | one_write_done(r10_bio); |
1285 | ||
1286 | /* In case raid10d snuck in to freeze_array */ | |
1287 | wake_up(&conf->wait_barrier); | |
d4432c23 | 1288 | |
c3b328ac | 1289 | if (do_sync || !mddev->bitmap || !plugged) |
e3881a68 | 1290 | md_wakeup_thread(mddev->thread); |
1da177e4 LT |
1291 | } |
1292 | ||
fd01b88c | 1293 | static void status(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 1294 | { |
e879a879 | 1295 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
1296 | int i; |
1297 | ||
1298 | if (conf->near_copies < conf->raid_disks) | |
9d8f0363 | 1299 | seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); |
1da177e4 LT |
1300 | if (conf->near_copies > 1) |
1301 | seq_printf(seq, " %d near-copies", conf->near_copies); | |
c93983bf N |
1302 | if (conf->far_copies > 1) { |
1303 | if (conf->far_offset) | |
1304 | seq_printf(seq, " %d offset-copies", conf->far_copies); | |
1305 | else | |
1306 | seq_printf(seq, " %d far-copies", conf->far_copies); | |
1307 | } | |
1da177e4 | 1308 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, |
76186dd8 | 1309 | conf->raid_disks - mddev->degraded); |
1da177e4 LT |
1310 | for (i = 0; i < conf->raid_disks; i++) |
1311 | seq_printf(seq, "%s", | |
1312 | conf->mirrors[i].rdev && | |
b2d444d7 | 1313 | test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_"); |
1da177e4 LT |
1314 | seq_printf(seq, "]"); |
1315 | } | |
1316 | ||
700c7213 N |
1317 | /* check if there are enough drives for |
1318 | * every block to appear on atleast one. | |
1319 | * Don't consider the device numbered 'ignore' | |
1320 | * as we might be about to remove it. | |
1321 | */ | |
e879a879 | 1322 | static int enough(struct r10conf *conf, int ignore) |
700c7213 N |
1323 | { |
1324 | int first = 0; | |
1325 | ||
1326 | do { | |
1327 | int n = conf->copies; | |
1328 | int cnt = 0; | |
1329 | while (n--) { | |
1330 | if (conf->mirrors[first].rdev && | |
1331 | first != ignore) | |
1332 | cnt++; | |
1333 | first = (first+1) % conf->raid_disks; | |
1334 | } | |
1335 | if (cnt == 0) | |
1336 | return 0; | |
1337 | } while (first != 0); | |
1338 | return 1; | |
1339 | } | |
1340 | ||
fd01b88c | 1341 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1342 | { |
1343 | char b[BDEVNAME_SIZE]; | |
e879a879 | 1344 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
1345 | |
1346 | /* | |
1347 | * If it is not operational, then we have already marked it as dead | |
1348 | * else if it is the last working disks, ignore the error, let the | |
1349 | * next level up know. | |
1350 | * else mark the drive as failed | |
1351 | */ | |
b2d444d7 | 1352 | if (test_bit(In_sync, &rdev->flags) |
700c7213 | 1353 | && !enough(conf, rdev->raid_disk)) |
1da177e4 LT |
1354 | /* |
1355 | * Don't fail the drive, just return an IO error. | |
1da177e4 LT |
1356 | */ |
1357 | return; | |
c04be0aa N |
1358 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
1359 | unsigned long flags; | |
1360 | spin_lock_irqsave(&conf->device_lock, flags); | |
1da177e4 | 1361 | mddev->degraded++; |
c04be0aa | 1362 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 LT |
1363 | /* |
1364 | * if recovery is running, make sure it aborts. | |
1365 | */ | |
dfc70645 | 1366 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
1da177e4 | 1367 | } |
de393cde | 1368 | set_bit(Blocked, &rdev->flags); |
b2d444d7 | 1369 | set_bit(Faulty, &rdev->flags); |
850b2b42 | 1370 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
067032bc JP |
1371 | printk(KERN_ALERT |
1372 | "md/raid10:%s: Disk failure on %s, disabling device.\n" | |
1373 | "md/raid10:%s: Operation continuing on %d devices.\n", | |
128595ed N |
1374 | mdname(mddev), bdevname(rdev->bdev, b), |
1375 | mdname(mddev), conf->raid_disks - mddev->degraded); | |
1da177e4 LT |
1376 | } |
1377 | ||
e879a879 | 1378 | static void print_conf(struct r10conf *conf) |
1da177e4 LT |
1379 | { |
1380 | int i; | |
0f6d02d5 | 1381 | struct mirror_info *tmp; |
1da177e4 | 1382 | |
128595ed | 1383 | printk(KERN_DEBUG "RAID10 conf printout:\n"); |
1da177e4 | 1384 | if (!conf) { |
128595ed | 1385 | printk(KERN_DEBUG "(!conf)\n"); |
1da177e4 LT |
1386 | return; |
1387 | } | |
128595ed | 1388 | printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, |
1da177e4 LT |
1389 | conf->raid_disks); |
1390 | ||
1391 | for (i = 0; i < conf->raid_disks; i++) { | |
1392 | char b[BDEVNAME_SIZE]; | |
1393 | tmp = conf->mirrors + i; | |
1394 | if (tmp->rdev) | |
128595ed | 1395 | printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", |
b2d444d7 N |
1396 | i, !test_bit(In_sync, &tmp->rdev->flags), |
1397 | !test_bit(Faulty, &tmp->rdev->flags), | |
1da177e4 LT |
1398 | bdevname(tmp->rdev->bdev,b)); |
1399 | } | |
1400 | } | |
1401 | ||
e879a879 | 1402 | static void close_sync(struct r10conf *conf) |
1da177e4 | 1403 | { |
0a27ec96 N |
1404 | wait_barrier(conf); |
1405 | allow_barrier(conf); | |
1da177e4 LT |
1406 | |
1407 | mempool_destroy(conf->r10buf_pool); | |
1408 | conf->r10buf_pool = NULL; | |
1409 | } | |
1410 | ||
fd01b88c | 1411 | static int raid10_spare_active(struct mddev *mddev) |
1da177e4 LT |
1412 | { |
1413 | int i; | |
e879a879 | 1414 | struct r10conf *conf = mddev->private; |
0f6d02d5 | 1415 | struct mirror_info *tmp; |
6b965620 N |
1416 | int count = 0; |
1417 | unsigned long flags; | |
1da177e4 LT |
1418 | |
1419 | /* | |
1420 | * Find all non-in_sync disks within the RAID10 configuration | |
1421 | * and mark them in_sync | |
1422 | */ | |
1423 | for (i = 0; i < conf->raid_disks; i++) { | |
1424 | tmp = conf->mirrors + i; | |
4ca40c2c N |
1425 | if (tmp->replacement |
1426 | && tmp->replacement->recovery_offset == MaxSector | |
1427 | && !test_bit(Faulty, &tmp->replacement->flags) | |
1428 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { | |
1429 | /* Replacement has just become active */ | |
1430 | if (!tmp->rdev | |
1431 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) | |
1432 | count++; | |
1433 | if (tmp->rdev) { | |
1434 | /* Replaced device not technically faulty, | |
1435 | * but we need to be sure it gets removed | |
1436 | * and never re-added. | |
1437 | */ | |
1438 | set_bit(Faulty, &tmp->rdev->flags); | |
1439 | sysfs_notify_dirent_safe( | |
1440 | tmp->rdev->sysfs_state); | |
1441 | } | |
1442 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); | |
1443 | } else if (tmp->rdev | |
1444 | && !test_bit(Faulty, &tmp->rdev->flags) | |
1445 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { | |
6b965620 | 1446 | count++; |
e6ffbcb6 | 1447 | sysfs_notify_dirent(tmp->rdev->sysfs_state); |
1da177e4 LT |
1448 | } |
1449 | } | |
6b965620 N |
1450 | spin_lock_irqsave(&conf->device_lock, flags); |
1451 | mddev->degraded -= count; | |
1452 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 LT |
1453 | |
1454 | print_conf(conf); | |
6b965620 | 1455 | return count; |
1da177e4 LT |
1456 | } |
1457 | ||
1458 | ||
fd01b88c | 1459 | static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1460 | { |
e879a879 | 1461 | struct r10conf *conf = mddev->private; |
199050ea | 1462 | int err = -EEXIST; |
1da177e4 | 1463 | int mirror; |
6c2fce2e | 1464 | int first = 0; |
84707f38 | 1465 | int last = conf->raid_disks - 1; |
1da177e4 LT |
1466 | |
1467 | if (mddev->recovery_cp < MaxSector) | |
1468 | /* only hot-add to in-sync arrays, as recovery is | |
1469 | * very different from resync | |
1470 | */ | |
199050ea | 1471 | return -EBUSY; |
700c7213 | 1472 | if (!enough(conf, -1)) |
199050ea | 1473 | return -EINVAL; |
1da177e4 | 1474 | |
a53a6c85 | 1475 | if (rdev->raid_disk >= 0) |
6c2fce2e | 1476 | first = last = rdev->raid_disk; |
1da177e4 | 1477 | |
2c4193df | 1478 | if (rdev->saved_raid_disk >= first && |
6cce3b23 N |
1479 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) |
1480 | mirror = rdev->saved_raid_disk; | |
1481 | else | |
6c2fce2e | 1482 | mirror = first; |
2bb77736 | 1483 | for ( ; mirror <= last ; mirror++) { |
0f6d02d5 | 1484 | struct mirror_info *p = &conf->mirrors[mirror]; |
2bb77736 N |
1485 | if (p->recovery_disabled == mddev->recovery_disabled) |
1486 | continue; | |
b7044d41 N |
1487 | if (p->rdev) { |
1488 | if (!test_bit(WantReplacement, &p->rdev->flags) || | |
1489 | p->replacement != NULL) | |
1490 | continue; | |
1491 | clear_bit(In_sync, &rdev->flags); | |
1492 | set_bit(Replacement, &rdev->flags); | |
1493 | rdev->raid_disk = mirror; | |
1494 | err = 0; | |
1495 | disk_stack_limits(mddev->gendisk, rdev->bdev, | |
1496 | rdev->data_offset << 9); | |
1497 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { | |
1498 | blk_queue_max_segments(mddev->queue, 1); | |
1499 | blk_queue_segment_boundary(mddev->queue, | |
1500 | PAGE_CACHE_SIZE - 1); | |
1501 | } | |
1502 | conf->fullsync = 1; | |
1503 | rcu_assign_pointer(p->replacement, rdev); | |
1504 | break; | |
1505 | } | |
1da177e4 | 1506 | |
2bb77736 N |
1507 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
1508 | rdev->data_offset << 9); | |
1509 | /* as we don't honour merge_bvec_fn, we must | |
1510 | * never risk violating it, so limit | |
1511 | * ->max_segments to one lying with a single | |
1512 | * page, as a one page request is never in | |
1513 | * violation. | |
1514 | */ | |
1515 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { | |
1516 | blk_queue_max_segments(mddev->queue, 1); | |
1517 | blk_queue_segment_boundary(mddev->queue, | |
1518 | PAGE_CACHE_SIZE - 1); | |
1da177e4 LT |
1519 | } |
1520 | ||
2bb77736 | 1521 | p->head_position = 0; |
d890fa2b | 1522 | p->recovery_disabled = mddev->recovery_disabled - 1; |
2bb77736 N |
1523 | rdev->raid_disk = mirror; |
1524 | err = 0; | |
1525 | if (rdev->saved_raid_disk != mirror) | |
1526 | conf->fullsync = 1; | |
1527 | rcu_assign_pointer(p->rdev, rdev); | |
1528 | break; | |
1529 | } | |
1530 | ||
ac5e7113 | 1531 | md_integrity_add_rdev(rdev, mddev); |
1da177e4 | 1532 | print_conf(conf); |
199050ea | 1533 | return err; |
1da177e4 LT |
1534 | } |
1535 | ||
b8321b68 | 1536 | static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1537 | { |
e879a879 | 1538 | struct r10conf *conf = mddev->private; |
1da177e4 | 1539 | int err = 0; |
b8321b68 | 1540 | int number = rdev->raid_disk; |
c8ab903e N |
1541 | struct md_rdev **rdevp; |
1542 | struct mirror_info *p = conf->mirrors + number; | |
1da177e4 LT |
1543 | |
1544 | print_conf(conf); | |
c8ab903e N |
1545 | if (rdev == p->rdev) |
1546 | rdevp = &p->rdev; | |
1547 | else if (rdev == p->replacement) | |
1548 | rdevp = &p->replacement; | |
1549 | else | |
1550 | return 0; | |
1551 | ||
1552 | if (test_bit(In_sync, &rdev->flags) || | |
1553 | atomic_read(&rdev->nr_pending)) { | |
1554 | err = -EBUSY; | |
1555 | goto abort; | |
1556 | } | |
1557 | /* Only remove faulty devices if recovery | |
1558 | * is not possible. | |
1559 | */ | |
1560 | if (!test_bit(Faulty, &rdev->flags) && | |
1561 | mddev->recovery_disabled != p->recovery_disabled && | |
4ca40c2c | 1562 | (!p->replacement || p->replacement == rdev) && |
c8ab903e N |
1563 | enough(conf, -1)) { |
1564 | err = -EBUSY; | |
1565 | goto abort; | |
1da177e4 | 1566 | } |
c8ab903e N |
1567 | *rdevp = NULL; |
1568 | synchronize_rcu(); | |
1569 | if (atomic_read(&rdev->nr_pending)) { | |
1570 | /* lost the race, try later */ | |
1571 | err = -EBUSY; | |
1572 | *rdevp = rdev; | |
1573 | goto abort; | |
4ca40c2c N |
1574 | } else if (p->replacement) { |
1575 | /* We must have just cleared 'rdev' */ | |
1576 | p->rdev = p->replacement; | |
1577 | clear_bit(Replacement, &p->replacement->flags); | |
1578 | smp_mb(); /* Make sure other CPUs may see both as identical | |
1579 | * but will never see neither -- if they are careful. | |
1580 | */ | |
1581 | p->replacement = NULL; | |
1582 | clear_bit(WantReplacement, &rdev->flags); | |
1583 | } else | |
1584 | /* We might have just remove the Replacement as faulty | |
1585 | * Clear the flag just in case | |
1586 | */ | |
1587 | clear_bit(WantReplacement, &rdev->flags); | |
1588 | ||
c8ab903e N |
1589 | err = md_integrity_register(mddev); |
1590 | ||
1da177e4 LT |
1591 | abort: |
1592 | ||
1593 | print_conf(conf); | |
1594 | return err; | |
1595 | } | |
1596 | ||
1597 | ||
6712ecf8 | 1598 | static void end_sync_read(struct bio *bio, int error) |
1da177e4 | 1599 | { |
9f2c9d12 | 1600 | struct r10bio *r10_bio = bio->bi_private; |
e879a879 | 1601 | struct r10conf *conf = r10_bio->mddev->private; |
778ca018 | 1602 | int d; |
1da177e4 | 1603 | |
69335ef3 | 1604 | d = find_bio_disk(conf, r10_bio, bio, NULL, NULL); |
0eb3ff12 N |
1605 | |
1606 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
1607 | set_bit(R10BIO_Uptodate, &r10_bio->state); | |
e684e41d N |
1608 | else |
1609 | /* The write handler will notice the lack of | |
1610 | * R10BIO_Uptodate and record any errors etc | |
1611 | */ | |
4dbcdc75 N |
1612 | atomic_add(r10_bio->sectors, |
1613 | &conf->mirrors[d].rdev->corrected_errors); | |
1da177e4 LT |
1614 | |
1615 | /* for reconstruct, we always reschedule after a read. | |
1616 | * for resync, only after all reads | |
1617 | */ | |
73d5c38a | 1618 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); |
1da177e4 LT |
1619 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || |
1620 | atomic_dec_and_test(&r10_bio->remaining)) { | |
1621 | /* we have read all the blocks, | |
1622 | * do the comparison in process context in raid10d | |
1623 | */ | |
1624 | reschedule_retry(r10_bio); | |
1625 | } | |
1da177e4 LT |
1626 | } |
1627 | ||
9f2c9d12 | 1628 | static void end_sync_request(struct r10bio *r10_bio) |
1da177e4 | 1629 | { |
fd01b88c | 1630 | struct mddev *mddev = r10_bio->mddev; |
dfc70645 | 1631 | |
1da177e4 LT |
1632 | while (atomic_dec_and_test(&r10_bio->remaining)) { |
1633 | if (r10_bio->master_bio == NULL) { | |
1634 | /* the primary of several recovery bios */ | |
73d5c38a | 1635 | sector_t s = r10_bio->sectors; |
1a0b7cd8 N |
1636 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
1637 | test_bit(R10BIO_WriteError, &r10_bio->state)) | |
749c55e9 N |
1638 | reschedule_retry(r10_bio); |
1639 | else | |
1640 | put_buf(r10_bio); | |
73d5c38a | 1641 | md_done_sync(mddev, s, 1); |
1da177e4 LT |
1642 | break; |
1643 | } else { | |
9f2c9d12 | 1644 | struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; |
1a0b7cd8 N |
1645 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
1646 | test_bit(R10BIO_WriteError, &r10_bio->state)) | |
749c55e9 N |
1647 | reschedule_retry(r10_bio); |
1648 | else | |
1649 | put_buf(r10_bio); | |
1da177e4 LT |
1650 | r10_bio = r10_bio2; |
1651 | } | |
1652 | } | |
1da177e4 LT |
1653 | } |
1654 | ||
5e570289 N |
1655 | static void end_sync_write(struct bio *bio, int error) |
1656 | { | |
1657 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
9f2c9d12 | 1658 | struct r10bio *r10_bio = bio->bi_private; |
fd01b88c | 1659 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 1660 | struct r10conf *conf = mddev->private; |
5e570289 N |
1661 | int d; |
1662 | sector_t first_bad; | |
1663 | int bad_sectors; | |
1664 | int slot; | |
9ad1aefc | 1665 | int repl; |
4ca40c2c | 1666 | struct md_rdev *rdev = NULL; |
5e570289 | 1667 | |
9ad1aefc N |
1668 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
1669 | if (repl) | |
1670 | rdev = conf->mirrors[d].replacement; | |
4ca40c2c N |
1671 | if (!rdev) { |
1672 | smp_mb(); | |
9ad1aefc | 1673 | rdev = conf->mirrors[d].rdev; |
4ca40c2c | 1674 | } |
5e570289 N |
1675 | |
1676 | if (!uptodate) { | |
9ad1aefc N |
1677 | if (repl) |
1678 | md_error(mddev, rdev); | |
1679 | else { | |
1680 | set_bit(WriteErrorSeen, &rdev->flags); | |
b7044d41 N |
1681 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
1682 | set_bit(MD_RECOVERY_NEEDED, | |
1683 | &rdev->mddev->recovery); | |
9ad1aefc N |
1684 | set_bit(R10BIO_WriteError, &r10_bio->state); |
1685 | } | |
1686 | } else if (is_badblock(rdev, | |
5e570289 N |
1687 | r10_bio->devs[slot].addr, |
1688 | r10_bio->sectors, | |
1689 | &first_bad, &bad_sectors)) | |
1690 | set_bit(R10BIO_MadeGood, &r10_bio->state); | |
1691 | ||
9ad1aefc | 1692 | rdev_dec_pending(rdev, mddev); |
5e570289 N |
1693 | |
1694 | end_sync_request(r10_bio); | |
1695 | } | |
1696 | ||
1da177e4 LT |
1697 | /* |
1698 | * Note: sync and recover and handled very differently for raid10 | |
1699 | * This code is for resync. | |
1700 | * For resync, we read through virtual addresses and read all blocks. | |
1701 | * If there is any error, we schedule a write. The lowest numbered | |
1702 | * drive is authoritative. | |
1703 | * However requests come for physical address, so we need to map. | |
1704 | * For every physical address there are raid_disks/copies virtual addresses, | |
1705 | * which is always are least one, but is not necessarly an integer. | |
1706 | * This means that a physical address can span multiple chunks, so we may | |
1707 | * have to submit multiple io requests for a single sync request. | |
1708 | */ | |
1709 | /* | |
1710 | * We check if all blocks are in-sync and only write to blocks that | |
1711 | * aren't in sync | |
1712 | */ | |
9f2c9d12 | 1713 | static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
1da177e4 | 1714 | { |
e879a879 | 1715 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
1716 | int i, first; |
1717 | struct bio *tbio, *fbio; | |
1718 | ||
1719 | atomic_set(&r10_bio->remaining, 1); | |
1720 | ||
1721 | /* find the first device with a block */ | |
1722 | for (i=0; i<conf->copies; i++) | |
1723 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) | |
1724 | break; | |
1725 | ||
1726 | if (i == conf->copies) | |
1727 | goto done; | |
1728 | ||
1729 | first = i; | |
1730 | fbio = r10_bio->devs[i].bio; | |
1731 | ||
1732 | /* now find blocks with errors */ | |
0eb3ff12 N |
1733 | for (i=0 ; i < conf->copies ; i++) { |
1734 | int j, d; | |
1735 | int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); | |
1da177e4 | 1736 | |
1da177e4 | 1737 | tbio = r10_bio->devs[i].bio; |
0eb3ff12 N |
1738 | |
1739 | if (tbio->bi_end_io != end_sync_read) | |
1740 | continue; | |
1741 | if (i == first) | |
1da177e4 | 1742 | continue; |
0eb3ff12 N |
1743 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) { |
1744 | /* We know that the bi_io_vec layout is the same for | |
1745 | * both 'first' and 'i', so we just compare them. | |
1746 | * All vec entries are PAGE_SIZE; | |
1747 | */ | |
1748 | for (j = 0; j < vcnt; j++) | |
1749 | if (memcmp(page_address(fbio->bi_io_vec[j].bv_page), | |
1750 | page_address(tbio->bi_io_vec[j].bv_page), | |
1751 | PAGE_SIZE)) | |
1752 | break; | |
1753 | if (j == vcnt) | |
1754 | continue; | |
1755 | mddev->resync_mismatches += r10_bio->sectors; | |
f84ee364 N |
1756 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
1757 | /* Don't fix anything. */ | |
1758 | continue; | |
0eb3ff12 | 1759 | } |
f84ee364 N |
1760 | /* Ok, we need to write this bio, either to correct an |
1761 | * inconsistency or to correct an unreadable block. | |
1da177e4 LT |
1762 | * First we need to fixup bv_offset, bv_len and |
1763 | * bi_vecs, as the read request might have corrupted these | |
1764 | */ | |
1765 | tbio->bi_vcnt = vcnt; | |
1766 | tbio->bi_size = r10_bio->sectors << 9; | |
1767 | tbio->bi_idx = 0; | |
1768 | tbio->bi_phys_segments = 0; | |
1da177e4 LT |
1769 | tbio->bi_flags &= ~(BIO_POOL_MASK - 1); |
1770 | tbio->bi_flags |= 1 << BIO_UPTODATE; | |
1771 | tbio->bi_next = NULL; | |
1772 | tbio->bi_rw = WRITE; | |
1773 | tbio->bi_private = r10_bio; | |
1774 | tbio->bi_sector = r10_bio->devs[i].addr; | |
1775 | ||
1776 | for (j=0; j < vcnt ; j++) { | |
1777 | tbio->bi_io_vec[j].bv_offset = 0; | |
1778 | tbio->bi_io_vec[j].bv_len = PAGE_SIZE; | |
1779 | ||
1780 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), | |
1781 | page_address(fbio->bi_io_vec[j].bv_page), | |
1782 | PAGE_SIZE); | |
1783 | } | |
1784 | tbio->bi_end_io = end_sync_write; | |
1785 | ||
1786 | d = r10_bio->devs[i].devnum; | |
1787 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
1788 | atomic_inc(&r10_bio->remaining); | |
1789 | md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9); | |
1790 | ||
1791 | tbio->bi_sector += conf->mirrors[d].rdev->data_offset; | |
1792 | tbio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
1793 | generic_make_request(tbio); | |
1794 | } | |
1795 | ||
9ad1aefc N |
1796 | /* Now write out to any replacement devices |
1797 | * that are active | |
1798 | */ | |
1799 | for (i = 0; i < conf->copies; i++) { | |
1800 | int j, d; | |
1801 | int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); | |
1802 | ||
1803 | tbio = r10_bio->devs[i].repl_bio; | |
1804 | if (!tbio || !tbio->bi_end_io) | |
1805 | continue; | |
1806 | if (r10_bio->devs[i].bio->bi_end_io != end_sync_write | |
1807 | && r10_bio->devs[i].bio != fbio) | |
1808 | for (j = 0; j < vcnt; j++) | |
1809 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), | |
1810 | page_address(fbio->bi_io_vec[j].bv_page), | |
1811 | PAGE_SIZE); | |
1812 | d = r10_bio->devs[i].devnum; | |
1813 | atomic_inc(&r10_bio->remaining); | |
1814 | md_sync_acct(conf->mirrors[d].replacement->bdev, | |
1815 | tbio->bi_size >> 9); | |
1816 | generic_make_request(tbio); | |
1817 | } | |
1818 | ||
1da177e4 LT |
1819 | done: |
1820 | if (atomic_dec_and_test(&r10_bio->remaining)) { | |
1821 | md_done_sync(mddev, r10_bio->sectors, 1); | |
1822 | put_buf(r10_bio); | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | /* | |
1827 | * Now for the recovery code. | |
1828 | * Recovery happens across physical sectors. | |
1829 | * We recover all non-is_sync drives by finding the virtual address of | |
1830 | * each, and then choose a working drive that also has that virt address. | |
1831 | * There is a separate r10_bio for each non-in_sync drive. | |
1832 | * Only the first two slots are in use. The first for reading, | |
1833 | * The second for writing. | |
1834 | * | |
1835 | */ | |
9f2c9d12 | 1836 | static void fix_recovery_read_error(struct r10bio *r10_bio) |
5e570289 N |
1837 | { |
1838 | /* We got a read error during recovery. | |
1839 | * We repeat the read in smaller page-sized sections. | |
1840 | * If a read succeeds, write it to the new device or record | |
1841 | * a bad block if we cannot. | |
1842 | * If a read fails, record a bad block on both old and | |
1843 | * new devices. | |
1844 | */ | |
fd01b88c | 1845 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 1846 | struct r10conf *conf = mddev->private; |
5e570289 N |
1847 | struct bio *bio = r10_bio->devs[0].bio; |
1848 | sector_t sect = 0; | |
1849 | int sectors = r10_bio->sectors; | |
1850 | int idx = 0; | |
1851 | int dr = r10_bio->devs[0].devnum; | |
1852 | int dw = r10_bio->devs[1].devnum; | |
1853 | ||
1854 | while (sectors) { | |
1855 | int s = sectors; | |
3cb03002 | 1856 | struct md_rdev *rdev; |
5e570289 N |
1857 | sector_t addr; |
1858 | int ok; | |
1859 | ||
1860 | if (s > (PAGE_SIZE>>9)) | |
1861 | s = PAGE_SIZE >> 9; | |
1862 | ||
1863 | rdev = conf->mirrors[dr].rdev; | |
1864 | addr = r10_bio->devs[0].addr + sect, | |
1865 | ok = sync_page_io(rdev, | |
1866 | addr, | |
1867 | s << 9, | |
1868 | bio->bi_io_vec[idx].bv_page, | |
1869 | READ, false); | |
1870 | if (ok) { | |
1871 | rdev = conf->mirrors[dw].rdev; | |
1872 | addr = r10_bio->devs[1].addr + sect; | |
1873 | ok = sync_page_io(rdev, | |
1874 | addr, | |
1875 | s << 9, | |
1876 | bio->bi_io_vec[idx].bv_page, | |
1877 | WRITE, false); | |
b7044d41 | 1878 | if (!ok) { |
5e570289 | 1879 | set_bit(WriteErrorSeen, &rdev->flags); |
b7044d41 N |
1880 | if (!test_and_set_bit(WantReplacement, |
1881 | &rdev->flags)) | |
1882 | set_bit(MD_RECOVERY_NEEDED, | |
1883 | &rdev->mddev->recovery); | |
1884 | } | |
5e570289 N |
1885 | } |
1886 | if (!ok) { | |
1887 | /* We don't worry if we cannot set a bad block - | |
1888 | * it really is bad so there is no loss in not | |
1889 | * recording it yet | |
1890 | */ | |
1891 | rdev_set_badblocks(rdev, addr, s, 0); | |
1892 | ||
1893 | if (rdev != conf->mirrors[dw].rdev) { | |
1894 | /* need bad block on destination too */ | |
3cb03002 | 1895 | struct md_rdev *rdev2 = conf->mirrors[dw].rdev; |
5e570289 N |
1896 | addr = r10_bio->devs[1].addr + sect; |
1897 | ok = rdev_set_badblocks(rdev2, addr, s, 0); | |
1898 | if (!ok) { | |
1899 | /* just abort the recovery */ | |
1900 | printk(KERN_NOTICE | |
1901 | "md/raid10:%s: recovery aborted" | |
1902 | " due to read error\n", | |
1903 | mdname(mddev)); | |
1904 | ||
1905 | conf->mirrors[dw].recovery_disabled | |
1906 | = mddev->recovery_disabled; | |
1907 | set_bit(MD_RECOVERY_INTR, | |
1908 | &mddev->recovery); | |
1909 | break; | |
1910 | } | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | sectors -= s; | |
1915 | sect += s; | |
1916 | idx++; | |
1917 | } | |
1918 | } | |
1da177e4 | 1919 | |
9f2c9d12 | 1920 | static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
1da177e4 | 1921 | { |
e879a879 | 1922 | struct r10conf *conf = mddev->private; |
c65060ad | 1923 | int d; |
24afd80d | 1924 | struct bio *wbio, *wbio2; |
1da177e4 | 1925 | |
5e570289 N |
1926 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { |
1927 | fix_recovery_read_error(r10_bio); | |
1928 | end_sync_request(r10_bio); | |
1929 | return; | |
1930 | } | |
1931 | ||
c65060ad NK |
1932 | /* |
1933 | * share the pages with the first bio | |
1da177e4 LT |
1934 | * and submit the write request |
1935 | */ | |
1da177e4 | 1936 | d = r10_bio->devs[1].devnum; |
24afd80d N |
1937 | wbio = r10_bio->devs[1].bio; |
1938 | wbio2 = r10_bio->devs[1].repl_bio; | |
1939 | if (wbio->bi_end_io) { | |
1940 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
1941 | md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9); | |
1942 | generic_make_request(wbio); | |
1943 | } | |
1944 | if (wbio2 && wbio2->bi_end_io) { | |
1945 | atomic_inc(&conf->mirrors[d].replacement->nr_pending); | |
1946 | md_sync_acct(conf->mirrors[d].replacement->bdev, | |
1947 | wbio2->bi_size >> 9); | |
1948 | generic_make_request(wbio2); | |
1949 | } | |
1da177e4 LT |
1950 | } |
1951 | ||
1952 | ||
1e50915f RB |
1953 | /* |
1954 | * Used by fix_read_error() to decay the per rdev read_errors. | |
1955 | * We halve the read error count for every hour that has elapsed | |
1956 | * since the last recorded read error. | |
1957 | * | |
1958 | */ | |
fd01b88c | 1959 | static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev) |
1e50915f RB |
1960 | { |
1961 | struct timespec cur_time_mon; | |
1962 | unsigned long hours_since_last; | |
1963 | unsigned int read_errors = atomic_read(&rdev->read_errors); | |
1964 | ||
1965 | ktime_get_ts(&cur_time_mon); | |
1966 | ||
1967 | if (rdev->last_read_error.tv_sec == 0 && | |
1968 | rdev->last_read_error.tv_nsec == 0) { | |
1969 | /* first time we've seen a read error */ | |
1970 | rdev->last_read_error = cur_time_mon; | |
1971 | return; | |
1972 | } | |
1973 | ||
1974 | hours_since_last = (cur_time_mon.tv_sec - | |
1975 | rdev->last_read_error.tv_sec) / 3600; | |
1976 | ||
1977 | rdev->last_read_error = cur_time_mon; | |
1978 | ||
1979 | /* | |
1980 | * if hours_since_last is > the number of bits in read_errors | |
1981 | * just set read errors to 0. We do this to avoid | |
1982 | * overflowing the shift of read_errors by hours_since_last. | |
1983 | */ | |
1984 | if (hours_since_last >= 8 * sizeof(read_errors)) | |
1985 | atomic_set(&rdev->read_errors, 0); | |
1986 | else | |
1987 | atomic_set(&rdev->read_errors, read_errors >> hours_since_last); | |
1988 | } | |
1989 | ||
3cb03002 | 1990 | static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, |
58c54fcc N |
1991 | int sectors, struct page *page, int rw) |
1992 | { | |
1993 | sector_t first_bad; | |
1994 | int bad_sectors; | |
1995 | ||
1996 | if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors) | |
1997 | && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags))) | |
1998 | return -1; | |
1999 | if (sync_page_io(rdev, sector, sectors << 9, page, rw, false)) | |
2000 | /* success */ | |
2001 | return 1; | |
b7044d41 | 2002 | if (rw == WRITE) { |
58c54fcc | 2003 | set_bit(WriteErrorSeen, &rdev->flags); |
b7044d41 N |
2004 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
2005 | set_bit(MD_RECOVERY_NEEDED, | |
2006 | &rdev->mddev->recovery); | |
2007 | } | |
58c54fcc N |
2008 | /* need to record an error - either for the block or the device */ |
2009 | if (!rdev_set_badblocks(rdev, sector, sectors, 0)) | |
2010 | md_error(rdev->mddev, rdev); | |
2011 | return 0; | |
2012 | } | |
2013 | ||
1da177e4 LT |
2014 | /* |
2015 | * This is a kernel thread which: | |
2016 | * | |
2017 | * 1. Retries failed read operations on working mirrors. | |
2018 | * 2. Updates the raid superblock when problems encounter. | |
6814d536 | 2019 | * 3. Performs writes following reads for array synchronising. |
1da177e4 LT |
2020 | */ |
2021 | ||
e879a879 | 2022 | static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio) |
6814d536 N |
2023 | { |
2024 | int sect = 0; /* Offset from r10_bio->sector */ | |
2025 | int sectors = r10_bio->sectors; | |
3cb03002 | 2026 | struct md_rdev*rdev; |
1e50915f | 2027 | int max_read_errors = atomic_read(&mddev->max_corr_read_errors); |
0544a21d | 2028 | int d = r10_bio->devs[r10_bio->read_slot].devnum; |
1e50915f | 2029 | |
7c4e06ff N |
2030 | /* still own a reference to this rdev, so it cannot |
2031 | * have been cleared recently. | |
2032 | */ | |
2033 | rdev = conf->mirrors[d].rdev; | |
1e50915f | 2034 | |
7c4e06ff N |
2035 | if (test_bit(Faulty, &rdev->flags)) |
2036 | /* drive has already been failed, just ignore any | |
2037 | more fix_read_error() attempts */ | |
2038 | return; | |
1e50915f | 2039 | |
7c4e06ff N |
2040 | check_decay_read_errors(mddev, rdev); |
2041 | atomic_inc(&rdev->read_errors); | |
2042 | if (atomic_read(&rdev->read_errors) > max_read_errors) { | |
2043 | char b[BDEVNAME_SIZE]; | |
2044 | bdevname(rdev->bdev, b); | |
1e50915f | 2045 | |
7c4e06ff N |
2046 | printk(KERN_NOTICE |
2047 | "md/raid10:%s: %s: Raid device exceeded " | |
2048 | "read_error threshold [cur %d:max %d]\n", | |
2049 | mdname(mddev), b, | |
2050 | atomic_read(&rdev->read_errors), max_read_errors); | |
2051 | printk(KERN_NOTICE | |
2052 | "md/raid10:%s: %s: Failing raid device\n", | |
2053 | mdname(mddev), b); | |
2054 | md_error(mddev, conf->mirrors[d].rdev); | |
2055 | return; | |
1e50915f | 2056 | } |
1e50915f | 2057 | |
6814d536 N |
2058 | while(sectors) { |
2059 | int s = sectors; | |
2060 | int sl = r10_bio->read_slot; | |
2061 | int success = 0; | |
2062 | int start; | |
2063 | ||
2064 | if (s > (PAGE_SIZE>>9)) | |
2065 | s = PAGE_SIZE >> 9; | |
2066 | ||
2067 | rcu_read_lock(); | |
2068 | do { | |
8dbed5ce N |
2069 | sector_t first_bad; |
2070 | int bad_sectors; | |
2071 | ||
0544a21d | 2072 | d = r10_bio->devs[sl].devnum; |
6814d536 N |
2073 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
2074 | if (rdev && | |
8dbed5ce N |
2075 | test_bit(In_sync, &rdev->flags) && |
2076 | is_badblock(rdev, r10_bio->devs[sl].addr + sect, s, | |
2077 | &first_bad, &bad_sectors) == 0) { | |
6814d536 N |
2078 | atomic_inc(&rdev->nr_pending); |
2079 | rcu_read_unlock(); | |
2b193363 | 2080 | success = sync_page_io(rdev, |
6814d536 | 2081 | r10_bio->devs[sl].addr + |
ccebd4c4 | 2082 | sect, |
6814d536 | 2083 | s<<9, |
ccebd4c4 | 2084 | conf->tmppage, READ, false); |
6814d536 N |
2085 | rdev_dec_pending(rdev, mddev); |
2086 | rcu_read_lock(); | |
2087 | if (success) | |
2088 | break; | |
2089 | } | |
2090 | sl++; | |
2091 | if (sl == conf->copies) | |
2092 | sl = 0; | |
2093 | } while (!success && sl != r10_bio->read_slot); | |
2094 | rcu_read_unlock(); | |
2095 | ||
2096 | if (!success) { | |
58c54fcc N |
2097 | /* Cannot read from anywhere, just mark the block |
2098 | * as bad on the first device to discourage future | |
2099 | * reads. | |
2100 | */ | |
6814d536 | 2101 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; |
58c54fcc N |
2102 | rdev = conf->mirrors[dn].rdev; |
2103 | ||
2104 | if (!rdev_set_badblocks( | |
2105 | rdev, | |
2106 | r10_bio->devs[r10_bio->read_slot].addr | |
2107 | + sect, | |
2108 | s, 0)) | |
2109 | md_error(mddev, rdev); | |
6814d536 N |
2110 | break; |
2111 | } | |
2112 | ||
2113 | start = sl; | |
2114 | /* write it back and re-read */ | |
2115 | rcu_read_lock(); | |
2116 | while (sl != r10_bio->read_slot) { | |
67b8dc4b | 2117 | char b[BDEVNAME_SIZE]; |
0544a21d | 2118 | |
6814d536 N |
2119 | if (sl==0) |
2120 | sl = conf->copies; | |
2121 | sl--; | |
2122 | d = r10_bio->devs[sl].devnum; | |
2123 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
1294b9c9 N |
2124 | if (!rdev || |
2125 | !test_bit(In_sync, &rdev->flags)) | |
2126 | continue; | |
2127 | ||
2128 | atomic_inc(&rdev->nr_pending); | |
2129 | rcu_read_unlock(); | |
58c54fcc N |
2130 | if (r10_sync_page_io(rdev, |
2131 | r10_bio->devs[sl].addr + | |
2132 | sect, | |
2133 | s<<9, conf->tmppage, WRITE) | |
1294b9c9 N |
2134 | == 0) { |
2135 | /* Well, this device is dead */ | |
2136 | printk(KERN_NOTICE | |
2137 | "md/raid10:%s: read correction " | |
2138 | "write failed" | |
2139 | " (%d sectors at %llu on %s)\n", | |
2140 | mdname(mddev), s, | |
2141 | (unsigned long long)( | |
2142 | sect + rdev->data_offset), | |
2143 | bdevname(rdev->bdev, b)); | |
2144 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " | |
2145 | "drive\n", | |
2146 | mdname(mddev), | |
2147 | bdevname(rdev->bdev, b)); | |
6814d536 | 2148 | } |
1294b9c9 N |
2149 | rdev_dec_pending(rdev, mddev); |
2150 | rcu_read_lock(); | |
6814d536 N |
2151 | } |
2152 | sl = start; | |
2153 | while (sl != r10_bio->read_slot) { | |
1294b9c9 | 2154 | char b[BDEVNAME_SIZE]; |
0544a21d | 2155 | |
6814d536 N |
2156 | if (sl==0) |
2157 | sl = conf->copies; | |
2158 | sl--; | |
2159 | d = r10_bio->devs[sl].devnum; | |
2160 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
1294b9c9 N |
2161 | if (!rdev || |
2162 | !test_bit(In_sync, &rdev->flags)) | |
2163 | continue; | |
6814d536 | 2164 | |
1294b9c9 N |
2165 | atomic_inc(&rdev->nr_pending); |
2166 | rcu_read_unlock(); | |
58c54fcc N |
2167 | switch (r10_sync_page_io(rdev, |
2168 | r10_bio->devs[sl].addr + | |
2169 | sect, | |
2170 | s<<9, conf->tmppage, | |
2171 | READ)) { | |
2172 | case 0: | |
1294b9c9 N |
2173 | /* Well, this device is dead */ |
2174 | printk(KERN_NOTICE | |
2175 | "md/raid10:%s: unable to read back " | |
2176 | "corrected sectors" | |
2177 | " (%d sectors at %llu on %s)\n", | |
2178 | mdname(mddev), s, | |
2179 | (unsigned long long)( | |
2180 | sect + rdev->data_offset), | |
2181 | bdevname(rdev->bdev, b)); | |
2182 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " | |
2183 | "drive\n", | |
2184 | mdname(mddev), | |
2185 | bdevname(rdev->bdev, b)); | |
58c54fcc N |
2186 | break; |
2187 | case 1: | |
1294b9c9 N |
2188 | printk(KERN_INFO |
2189 | "md/raid10:%s: read error corrected" | |
2190 | " (%d sectors at %llu on %s)\n", | |
2191 | mdname(mddev), s, | |
2192 | (unsigned long long)( | |
2193 | sect + rdev->data_offset), | |
2194 | bdevname(rdev->bdev, b)); | |
2195 | atomic_add(s, &rdev->corrected_errors); | |
6814d536 | 2196 | } |
1294b9c9 N |
2197 | |
2198 | rdev_dec_pending(rdev, mddev); | |
2199 | rcu_read_lock(); | |
6814d536 N |
2200 | } |
2201 | rcu_read_unlock(); | |
2202 | ||
2203 | sectors -= s; | |
2204 | sect += s; | |
2205 | } | |
2206 | } | |
2207 | ||
bd870a16 N |
2208 | static void bi_complete(struct bio *bio, int error) |
2209 | { | |
2210 | complete((struct completion *)bio->bi_private); | |
2211 | } | |
2212 | ||
2213 | static int submit_bio_wait(int rw, struct bio *bio) | |
2214 | { | |
2215 | struct completion event; | |
2216 | rw |= REQ_SYNC; | |
2217 | ||
2218 | init_completion(&event); | |
2219 | bio->bi_private = &event; | |
2220 | bio->bi_end_io = bi_complete; | |
2221 | submit_bio(rw, bio); | |
2222 | wait_for_completion(&event); | |
2223 | ||
2224 | return test_bit(BIO_UPTODATE, &bio->bi_flags); | |
2225 | } | |
2226 | ||
9f2c9d12 | 2227 | static int narrow_write_error(struct r10bio *r10_bio, int i) |
bd870a16 N |
2228 | { |
2229 | struct bio *bio = r10_bio->master_bio; | |
fd01b88c | 2230 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 2231 | struct r10conf *conf = mddev->private; |
3cb03002 | 2232 | struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; |
bd870a16 N |
2233 | /* bio has the data to be written to slot 'i' where |
2234 | * we just recently had a write error. | |
2235 | * We repeatedly clone the bio and trim down to one block, | |
2236 | * then try the write. Where the write fails we record | |
2237 | * a bad block. | |
2238 | * It is conceivable that the bio doesn't exactly align with | |
2239 | * blocks. We must handle this. | |
2240 | * | |
2241 | * We currently own a reference to the rdev. | |
2242 | */ | |
2243 | ||
2244 | int block_sectors; | |
2245 | sector_t sector; | |
2246 | int sectors; | |
2247 | int sect_to_write = r10_bio->sectors; | |
2248 | int ok = 1; | |
2249 | ||
2250 | if (rdev->badblocks.shift < 0) | |
2251 | return 0; | |
2252 | ||
2253 | block_sectors = 1 << rdev->badblocks.shift; | |
2254 | sector = r10_bio->sector; | |
2255 | sectors = ((r10_bio->sector + block_sectors) | |
2256 | & ~(sector_t)(block_sectors - 1)) | |
2257 | - sector; | |
2258 | ||
2259 | while (sect_to_write) { | |
2260 | struct bio *wbio; | |
2261 | if (sectors > sect_to_write) | |
2262 | sectors = sect_to_write; | |
2263 | /* Write at 'sector' for 'sectors' */ | |
2264 | wbio = bio_clone_mddev(bio, GFP_NOIO, mddev); | |
2265 | md_trim_bio(wbio, sector - bio->bi_sector, sectors); | |
2266 | wbio->bi_sector = (r10_bio->devs[i].addr+ | |
2267 | rdev->data_offset+ | |
2268 | (sector - r10_bio->sector)); | |
2269 | wbio->bi_bdev = rdev->bdev; | |
2270 | if (submit_bio_wait(WRITE, wbio) == 0) | |
2271 | /* Failure! */ | |
2272 | ok = rdev_set_badblocks(rdev, sector, | |
2273 | sectors, 0) | |
2274 | && ok; | |
2275 | ||
2276 | bio_put(wbio); | |
2277 | sect_to_write -= sectors; | |
2278 | sector += sectors; | |
2279 | sectors = block_sectors; | |
2280 | } | |
2281 | return ok; | |
2282 | } | |
2283 | ||
9f2c9d12 | 2284 | static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) |
560f8e55 N |
2285 | { |
2286 | int slot = r10_bio->read_slot; | |
560f8e55 | 2287 | struct bio *bio; |
e879a879 | 2288 | struct r10conf *conf = mddev->private; |
abbf098e | 2289 | struct md_rdev *rdev = r10_bio->devs[slot].rdev; |
560f8e55 N |
2290 | char b[BDEVNAME_SIZE]; |
2291 | unsigned long do_sync; | |
856e08e2 | 2292 | int max_sectors; |
560f8e55 N |
2293 | |
2294 | /* we got a read error. Maybe the drive is bad. Maybe just | |
2295 | * the block and we can fix it. | |
2296 | * We freeze all other IO, and try reading the block from | |
2297 | * other devices. When we find one, we re-write | |
2298 | * and check it that fixes the read error. | |
2299 | * This is all done synchronously while the array is | |
2300 | * frozen. | |
2301 | */ | |
2302 | if (mddev->ro == 0) { | |
2303 | freeze_array(conf); | |
2304 | fix_read_error(conf, mddev, r10_bio); | |
2305 | unfreeze_array(conf); | |
2306 | } | |
abbf098e | 2307 | rdev_dec_pending(rdev, mddev); |
560f8e55 N |
2308 | |
2309 | bio = r10_bio->devs[slot].bio; | |
7399c31b | 2310 | bdevname(bio->bi_bdev, b); |
560f8e55 N |
2311 | r10_bio->devs[slot].bio = |
2312 | mddev->ro ? IO_BLOCKED : NULL; | |
7399c31b | 2313 | read_more: |
96c3fd1f N |
2314 | rdev = read_balance(conf, r10_bio, &max_sectors); |
2315 | if (rdev == NULL) { | |
560f8e55 N |
2316 | printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O" |
2317 | " read error for block %llu\n", | |
7399c31b | 2318 | mdname(mddev), b, |
560f8e55 N |
2319 | (unsigned long long)r10_bio->sector); |
2320 | raid_end_bio_io(r10_bio); | |
2321 | bio_put(bio); | |
2322 | return; | |
2323 | } | |
2324 | ||
2325 | do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC); | |
7399c31b N |
2326 | if (bio) |
2327 | bio_put(bio); | |
560f8e55 | 2328 | slot = r10_bio->read_slot; |
560f8e55 N |
2329 | printk_ratelimited( |
2330 | KERN_ERR | |
2331 | "md/raid10:%s: %s: redirecting" | |
2332 | "sector %llu to another mirror\n", | |
2333 | mdname(mddev), | |
2334 | bdevname(rdev->bdev, b), | |
2335 | (unsigned long long)r10_bio->sector); | |
2336 | bio = bio_clone_mddev(r10_bio->master_bio, | |
2337 | GFP_NOIO, mddev); | |
7399c31b N |
2338 | md_trim_bio(bio, |
2339 | r10_bio->sector - bio->bi_sector, | |
2340 | max_sectors); | |
560f8e55 | 2341 | r10_bio->devs[slot].bio = bio; |
abbf098e | 2342 | r10_bio->devs[slot].rdev = rdev; |
560f8e55 N |
2343 | bio->bi_sector = r10_bio->devs[slot].addr |
2344 | + rdev->data_offset; | |
2345 | bio->bi_bdev = rdev->bdev; | |
2346 | bio->bi_rw = READ | do_sync; | |
2347 | bio->bi_private = r10_bio; | |
2348 | bio->bi_end_io = raid10_end_read_request; | |
7399c31b N |
2349 | if (max_sectors < r10_bio->sectors) { |
2350 | /* Drat - have to split this up more */ | |
2351 | struct bio *mbio = r10_bio->master_bio; | |
2352 | int sectors_handled = | |
2353 | r10_bio->sector + max_sectors | |
2354 | - mbio->bi_sector; | |
2355 | r10_bio->sectors = max_sectors; | |
2356 | spin_lock_irq(&conf->device_lock); | |
2357 | if (mbio->bi_phys_segments == 0) | |
2358 | mbio->bi_phys_segments = 2; | |
2359 | else | |
2360 | mbio->bi_phys_segments++; | |
2361 | spin_unlock_irq(&conf->device_lock); | |
2362 | generic_make_request(bio); | |
2363 | bio = NULL; | |
2364 | ||
2365 | r10_bio = mempool_alloc(conf->r10bio_pool, | |
2366 | GFP_NOIO); | |
2367 | r10_bio->master_bio = mbio; | |
2368 | r10_bio->sectors = (mbio->bi_size >> 9) | |
2369 | - sectors_handled; | |
2370 | r10_bio->state = 0; | |
2371 | set_bit(R10BIO_ReadError, | |
2372 | &r10_bio->state); | |
2373 | r10_bio->mddev = mddev; | |
2374 | r10_bio->sector = mbio->bi_sector | |
2375 | + sectors_handled; | |
2376 | ||
2377 | goto read_more; | |
2378 | } else | |
2379 | generic_make_request(bio); | |
560f8e55 N |
2380 | } |
2381 | ||
e879a879 | 2382 | static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) |
749c55e9 N |
2383 | { |
2384 | /* Some sort of write request has finished and it | |
2385 | * succeeded in writing where we thought there was a | |
2386 | * bad block. So forget the bad block. | |
1a0b7cd8 N |
2387 | * Or possibly if failed and we need to record |
2388 | * a bad block. | |
749c55e9 N |
2389 | */ |
2390 | int m; | |
3cb03002 | 2391 | struct md_rdev *rdev; |
749c55e9 N |
2392 | |
2393 | if (test_bit(R10BIO_IsSync, &r10_bio->state) || | |
2394 | test_bit(R10BIO_IsRecover, &r10_bio->state)) { | |
1a0b7cd8 N |
2395 | for (m = 0; m < conf->copies; m++) { |
2396 | int dev = r10_bio->devs[m].devnum; | |
2397 | rdev = conf->mirrors[dev].rdev; | |
2398 | if (r10_bio->devs[m].bio == NULL) | |
2399 | continue; | |
2400 | if (test_bit(BIO_UPTODATE, | |
749c55e9 | 2401 | &r10_bio->devs[m].bio->bi_flags)) { |
749c55e9 N |
2402 | rdev_clear_badblocks( |
2403 | rdev, | |
2404 | r10_bio->devs[m].addr, | |
2405 | r10_bio->sectors); | |
1a0b7cd8 N |
2406 | } else { |
2407 | if (!rdev_set_badblocks( | |
2408 | rdev, | |
2409 | r10_bio->devs[m].addr, | |
2410 | r10_bio->sectors, 0)) | |
2411 | md_error(conf->mddev, rdev); | |
749c55e9 | 2412 | } |
9ad1aefc N |
2413 | rdev = conf->mirrors[dev].replacement; |
2414 | if (r10_bio->devs[m].repl_bio == NULL) | |
2415 | continue; | |
2416 | if (test_bit(BIO_UPTODATE, | |
2417 | &r10_bio->devs[m].repl_bio->bi_flags)) { | |
2418 | rdev_clear_badblocks( | |
2419 | rdev, | |
2420 | r10_bio->devs[m].addr, | |
2421 | r10_bio->sectors); | |
2422 | } else { | |
2423 | if (!rdev_set_badblocks( | |
2424 | rdev, | |
2425 | r10_bio->devs[m].addr, | |
2426 | r10_bio->sectors, 0)) | |
2427 | md_error(conf->mddev, rdev); | |
2428 | } | |
1a0b7cd8 | 2429 | } |
749c55e9 N |
2430 | put_buf(r10_bio); |
2431 | } else { | |
bd870a16 N |
2432 | for (m = 0; m < conf->copies; m++) { |
2433 | int dev = r10_bio->devs[m].devnum; | |
2434 | struct bio *bio = r10_bio->devs[m].bio; | |
2435 | rdev = conf->mirrors[dev].rdev; | |
2436 | if (bio == IO_MADE_GOOD) { | |
749c55e9 N |
2437 | rdev_clear_badblocks( |
2438 | rdev, | |
2439 | r10_bio->devs[m].addr, | |
2440 | r10_bio->sectors); | |
2441 | rdev_dec_pending(rdev, conf->mddev); | |
bd870a16 N |
2442 | } else if (bio != NULL && |
2443 | !test_bit(BIO_UPTODATE, &bio->bi_flags)) { | |
2444 | if (!narrow_write_error(r10_bio, m)) { | |
2445 | md_error(conf->mddev, rdev); | |
2446 | set_bit(R10BIO_Degraded, | |
2447 | &r10_bio->state); | |
2448 | } | |
2449 | rdev_dec_pending(rdev, conf->mddev); | |
749c55e9 | 2450 | } |
475b0321 N |
2451 | bio = r10_bio->devs[m].repl_bio; |
2452 | rdev = conf->mirrors[dev].replacement; | |
4ca40c2c | 2453 | if (rdev && bio == IO_MADE_GOOD) { |
475b0321 N |
2454 | rdev_clear_badblocks( |
2455 | rdev, | |
2456 | r10_bio->devs[m].addr, | |
2457 | r10_bio->sectors); | |
2458 | rdev_dec_pending(rdev, conf->mddev); | |
2459 | } | |
bd870a16 N |
2460 | } |
2461 | if (test_bit(R10BIO_WriteError, | |
2462 | &r10_bio->state)) | |
2463 | close_write(r10_bio); | |
749c55e9 N |
2464 | raid_end_bio_io(r10_bio); |
2465 | } | |
2466 | } | |
2467 | ||
fd01b88c | 2468 | static void raid10d(struct mddev *mddev) |
1da177e4 | 2469 | { |
9f2c9d12 | 2470 | struct r10bio *r10_bio; |
1da177e4 | 2471 | unsigned long flags; |
e879a879 | 2472 | struct r10conf *conf = mddev->private; |
1da177e4 | 2473 | struct list_head *head = &conf->retry_list; |
e1dfa0a2 | 2474 | struct blk_plug plug; |
1da177e4 LT |
2475 | |
2476 | md_check_recovery(mddev); | |
1da177e4 | 2477 | |
e1dfa0a2 | 2478 | blk_start_plug(&plug); |
1da177e4 | 2479 | for (;;) { |
6cce3b23 | 2480 | |
7eaceacc | 2481 | flush_pending_writes(conf); |
6cce3b23 | 2482 | |
a35e63ef N |
2483 | spin_lock_irqsave(&conf->device_lock, flags); |
2484 | if (list_empty(head)) { | |
2485 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 | 2486 | break; |
a35e63ef | 2487 | } |
9f2c9d12 | 2488 | r10_bio = list_entry(head->prev, struct r10bio, retry_list); |
1da177e4 | 2489 | list_del(head->prev); |
4443ae10 | 2490 | conf->nr_queued--; |
1da177e4 LT |
2491 | spin_unlock_irqrestore(&conf->device_lock, flags); |
2492 | ||
2493 | mddev = r10_bio->mddev; | |
070ec55d | 2494 | conf = mddev->private; |
bd870a16 N |
2495 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
2496 | test_bit(R10BIO_WriteError, &r10_bio->state)) | |
749c55e9 N |
2497 | handle_write_completed(conf, r10_bio); |
2498 | else if (test_bit(R10BIO_IsSync, &r10_bio->state)) | |
1da177e4 | 2499 | sync_request_write(mddev, r10_bio); |
7eaceacc | 2500 | else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) |
1da177e4 | 2501 | recovery_request_write(mddev, r10_bio); |
856e08e2 | 2502 | else if (test_bit(R10BIO_ReadError, &r10_bio->state)) |
560f8e55 | 2503 | handle_read_error(mddev, r10_bio); |
856e08e2 N |
2504 | else { |
2505 | /* just a partial read to be scheduled from a | |
2506 | * separate context | |
2507 | */ | |
2508 | int slot = r10_bio->read_slot; | |
2509 | generic_make_request(r10_bio->devs[slot].bio); | |
2510 | } | |
560f8e55 | 2511 | |
1d9d5241 | 2512 | cond_resched(); |
de393cde N |
2513 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
2514 | md_check_recovery(mddev); | |
1da177e4 | 2515 | } |
e1dfa0a2 | 2516 | blk_finish_plug(&plug); |
1da177e4 LT |
2517 | } |
2518 | ||
2519 | ||
e879a879 | 2520 | static int init_resync(struct r10conf *conf) |
1da177e4 LT |
2521 | { |
2522 | int buffs; | |
69335ef3 | 2523 | int i; |
1da177e4 LT |
2524 | |
2525 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; | |
b6385483 | 2526 | BUG_ON(conf->r10buf_pool); |
69335ef3 N |
2527 | conf->have_replacement = 0; |
2528 | for (i = 0; i < conf->raid_disks; i++) | |
2529 | if (conf->mirrors[i].replacement) | |
2530 | conf->have_replacement = 1; | |
1da177e4 LT |
2531 | conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); |
2532 | if (!conf->r10buf_pool) | |
2533 | return -ENOMEM; | |
2534 | conf->next_resync = 0; | |
2535 | return 0; | |
2536 | } | |
2537 | ||
2538 | /* | |
2539 | * perform a "sync" on one "block" | |
2540 | * | |
2541 | * We need to make sure that no normal I/O request - particularly write | |
2542 | * requests - conflict with active sync requests. | |
2543 | * | |
2544 | * This is achieved by tracking pending requests and a 'barrier' concept | |
2545 | * that can be installed to exclude normal IO requests. | |
2546 | * | |
2547 | * Resync and recovery are handled very differently. | |
2548 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. | |
2549 | * | |
2550 | * For resync, we iterate over virtual addresses, read all copies, | |
2551 | * and update if there are differences. If only one copy is live, | |
2552 | * skip it. | |
2553 | * For recovery, we iterate over physical addresses, read a good | |
2554 | * value for each non-in_sync drive, and over-write. | |
2555 | * | |
2556 | * So, for recovery we may have several outstanding complex requests for a | |
2557 | * given address, one for each out-of-sync device. We model this by allocating | |
2558 | * a number of r10_bio structures, one for each out-of-sync device. | |
2559 | * As we setup these structures, we collect all bio's together into a list | |
2560 | * which we then process collectively to add pages, and then process again | |
2561 | * to pass to generic_make_request. | |
2562 | * | |
2563 | * The r10_bio structures are linked using a borrowed master_bio pointer. | |
2564 | * This link is counted in ->remaining. When the r10_bio that points to NULL | |
2565 | * has its remaining count decremented to 0, the whole complex operation | |
2566 | * is complete. | |
2567 | * | |
2568 | */ | |
2569 | ||
fd01b88c | 2570 | static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, |
ab9d47e9 | 2571 | int *skipped, int go_faster) |
1da177e4 | 2572 | { |
e879a879 | 2573 | struct r10conf *conf = mddev->private; |
9f2c9d12 | 2574 | struct r10bio *r10_bio; |
1da177e4 LT |
2575 | struct bio *biolist = NULL, *bio; |
2576 | sector_t max_sector, nr_sectors; | |
1da177e4 | 2577 | int i; |
6cce3b23 | 2578 | int max_sync; |
57dab0bd | 2579 | sector_t sync_blocks; |
1da177e4 LT |
2580 | sector_t sectors_skipped = 0; |
2581 | int chunks_skipped = 0; | |
2582 | ||
2583 | if (!conf->r10buf_pool) | |
2584 | if (init_resync(conf)) | |
57afd89f | 2585 | return 0; |
1da177e4 LT |
2586 | |
2587 | skipped: | |
58c0fed4 | 2588 | max_sector = mddev->dev_sectors; |
1da177e4 LT |
2589 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
2590 | max_sector = mddev->resync_max_sectors; | |
2591 | if (sector_nr >= max_sector) { | |
6cce3b23 N |
2592 | /* If we aborted, we need to abort the |
2593 | * sync on the 'current' bitmap chucks (there can | |
2594 | * be several when recovering multiple devices). | |
2595 | * as we may have started syncing it but not finished. | |
2596 | * We can find the current address in | |
2597 | * mddev->curr_resync, but for recovery, | |
2598 | * we need to convert that to several | |
2599 | * virtual addresses. | |
2600 | */ | |
2601 | if (mddev->curr_resync < max_sector) { /* aborted */ | |
2602 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) | |
2603 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
2604 | &sync_blocks, 1); | |
2605 | else for (i=0; i<conf->raid_disks; i++) { | |
2606 | sector_t sect = | |
2607 | raid10_find_virt(conf, mddev->curr_resync, i); | |
2608 | bitmap_end_sync(mddev->bitmap, sect, | |
2609 | &sync_blocks, 1); | |
2610 | } | |
9ad1aefc N |
2611 | } else { |
2612 | /* completed sync */ | |
2613 | if ((!mddev->bitmap || conf->fullsync) | |
2614 | && conf->have_replacement | |
2615 | && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
2616 | /* Completed a full sync so the replacements | |
2617 | * are now fully recovered. | |
2618 | */ | |
2619 | for (i = 0; i < conf->raid_disks; i++) | |
2620 | if (conf->mirrors[i].replacement) | |
2621 | conf->mirrors[i].replacement | |
2622 | ->recovery_offset | |
2623 | = MaxSector; | |
2624 | } | |
6cce3b23 | 2625 | conf->fullsync = 0; |
9ad1aefc | 2626 | } |
6cce3b23 | 2627 | bitmap_close_sync(mddev->bitmap); |
1da177e4 | 2628 | close_sync(conf); |
57afd89f | 2629 | *skipped = 1; |
1da177e4 LT |
2630 | return sectors_skipped; |
2631 | } | |
2632 | if (chunks_skipped >= conf->raid_disks) { | |
2633 | /* if there has been nothing to do on any drive, | |
2634 | * then there is nothing to do at all.. | |
2635 | */ | |
57afd89f N |
2636 | *skipped = 1; |
2637 | return (max_sector - sector_nr) + sectors_skipped; | |
1da177e4 LT |
2638 | } |
2639 | ||
c6207277 N |
2640 | if (max_sector > mddev->resync_max) |
2641 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ | |
2642 | ||
1da177e4 LT |
2643 | /* make sure whole request will fit in a chunk - if chunks |
2644 | * are meaningful | |
2645 | */ | |
2646 | if (conf->near_copies < conf->raid_disks && | |
2647 | max_sector > (sector_nr | conf->chunk_mask)) | |
2648 | max_sector = (sector_nr | conf->chunk_mask) + 1; | |
2649 | /* | |
2650 | * If there is non-resync activity waiting for us then | |
2651 | * put in a delay to throttle resync. | |
2652 | */ | |
0a27ec96 | 2653 | if (!go_faster && conf->nr_waiting) |
1da177e4 | 2654 | msleep_interruptible(1000); |
1da177e4 LT |
2655 | |
2656 | /* Again, very different code for resync and recovery. | |
2657 | * Both must result in an r10bio with a list of bios that | |
2658 | * have bi_end_io, bi_sector, bi_bdev set, | |
2659 | * and bi_private set to the r10bio. | |
2660 | * For recovery, we may actually create several r10bios | |
2661 | * with 2 bios in each, that correspond to the bios in the main one. | |
2662 | * In this case, the subordinate r10bios link back through a | |
2663 | * borrowed master_bio pointer, and the counter in the master | |
2664 | * includes a ref from each subordinate. | |
2665 | */ | |
2666 | /* First, we decide what to do and set ->bi_end_io | |
2667 | * To end_sync_read if we want to read, and | |
2668 | * end_sync_write if we will want to write. | |
2669 | */ | |
2670 | ||
6cce3b23 | 2671 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
1da177e4 LT |
2672 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
2673 | /* recovery... the complicated one */ | |
e875ecea | 2674 | int j; |
1da177e4 LT |
2675 | r10_bio = NULL; |
2676 | ||
ab9d47e9 N |
2677 | for (i=0 ; i<conf->raid_disks; i++) { |
2678 | int still_degraded; | |
9f2c9d12 | 2679 | struct r10bio *rb2; |
ab9d47e9 N |
2680 | sector_t sect; |
2681 | int must_sync; | |
e875ecea | 2682 | int any_working; |
24afd80d N |
2683 | struct mirror_info *mirror = &conf->mirrors[i]; |
2684 | ||
2685 | if ((mirror->rdev == NULL || | |
2686 | test_bit(In_sync, &mirror->rdev->flags)) | |
2687 | && | |
2688 | (mirror->replacement == NULL || | |
2689 | test_bit(Faulty, | |
2690 | &mirror->replacement->flags))) | |
ab9d47e9 | 2691 | continue; |
1da177e4 | 2692 | |
ab9d47e9 N |
2693 | still_degraded = 0; |
2694 | /* want to reconstruct this device */ | |
2695 | rb2 = r10_bio; | |
2696 | sect = raid10_find_virt(conf, sector_nr, i); | |
24afd80d N |
2697 | /* Unless we are doing a full sync, or a replacement |
2698 | * we only need to recover the block if it is set in | |
2699 | * the bitmap | |
ab9d47e9 N |
2700 | */ |
2701 | must_sync = bitmap_start_sync(mddev->bitmap, sect, | |
2702 | &sync_blocks, 1); | |
2703 | if (sync_blocks < max_sync) | |
2704 | max_sync = sync_blocks; | |
2705 | if (!must_sync && | |
24afd80d | 2706 | mirror->replacement == NULL && |
ab9d47e9 N |
2707 | !conf->fullsync) { |
2708 | /* yep, skip the sync_blocks here, but don't assume | |
2709 | * that there will never be anything to do here | |
2710 | */ | |
2711 | chunks_skipped = -1; | |
2712 | continue; | |
2713 | } | |
6cce3b23 | 2714 | |
ab9d47e9 N |
2715 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
2716 | raise_barrier(conf, rb2 != NULL); | |
2717 | atomic_set(&r10_bio->remaining, 0); | |
18055569 | 2718 | |
ab9d47e9 N |
2719 | r10_bio->master_bio = (struct bio*)rb2; |
2720 | if (rb2) | |
2721 | atomic_inc(&rb2->remaining); | |
2722 | r10_bio->mddev = mddev; | |
2723 | set_bit(R10BIO_IsRecover, &r10_bio->state); | |
2724 | r10_bio->sector = sect; | |
1da177e4 | 2725 | |
ab9d47e9 N |
2726 | raid10_find_phys(conf, r10_bio); |
2727 | ||
2728 | /* Need to check if the array will still be | |
2729 | * degraded | |
2730 | */ | |
2731 | for (j=0; j<conf->raid_disks; j++) | |
2732 | if (conf->mirrors[j].rdev == NULL || | |
2733 | test_bit(Faulty, &conf->mirrors[j].rdev->flags)) { | |
2734 | still_degraded = 1; | |
87fc767b | 2735 | break; |
1da177e4 | 2736 | } |
ab9d47e9 N |
2737 | |
2738 | must_sync = bitmap_start_sync(mddev->bitmap, sect, | |
2739 | &sync_blocks, still_degraded); | |
2740 | ||
e875ecea | 2741 | any_working = 0; |
ab9d47e9 | 2742 | for (j=0; j<conf->copies;j++) { |
e875ecea | 2743 | int k; |
ab9d47e9 | 2744 | int d = r10_bio->devs[j].devnum; |
5e570289 | 2745 | sector_t from_addr, to_addr; |
3cb03002 | 2746 | struct md_rdev *rdev; |
40c356ce N |
2747 | sector_t sector, first_bad; |
2748 | int bad_sectors; | |
ab9d47e9 N |
2749 | if (!conf->mirrors[d].rdev || |
2750 | !test_bit(In_sync, &conf->mirrors[d].rdev->flags)) | |
2751 | continue; | |
2752 | /* This is where we read from */ | |
e875ecea | 2753 | any_working = 1; |
40c356ce N |
2754 | rdev = conf->mirrors[d].rdev; |
2755 | sector = r10_bio->devs[j].addr; | |
2756 | ||
2757 | if (is_badblock(rdev, sector, max_sync, | |
2758 | &first_bad, &bad_sectors)) { | |
2759 | if (first_bad > sector) | |
2760 | max_sync = first_bad - sector; | |
2761 | else { | |
2762 | bad_sectors -= (sector | |
2763 | - first_bad); | |
2764 | if (max_sync > bad_sectors) | |
2765 | max_sync = bad_sectors; | |
2766 | continue; | |
2767 | } | |
2768 | } | |
ab9d47e9 N |
2769 | bio = r10_bio->devs[0].bio; |
2770 | bio->bi_next = biolist; | |
2771 | biolist = bio; | |
2772 | bio->bi_private = r10_bio; | |
2773 | bio->bi_end_io = end_sync_read; | |
2774 | bio->bi_rw = READ; | |
5e570289 | 2775 | from_addr = r10_bio->devs[j].addr; |
24afd80d N |
2776 | bio->bi_sector = from_addr + rdev->data_offset; |
2777 | bio->bi_bdev = rdev->bdev; | |
2778 | atomic_inc(&rdev->nr_pending); | |
2779 | /* and we write to 'i' (if not in_sync) */ | |
ab9d47e9 N |
2780 | |
2781 | for (k=0; k<conf->copies; k++) | |
2782 | if (r10_bio->devs[k].devnum == i) | |
2783 | break; | |
2784 | BUG_ON(k == conf->copies); | |
5e570289 | 2785 | to_addr = r10_bio->devs[k].addr; |
ab9d47e9 | 2786 | r10_bio->devs[0].devnum = d; |
5e570289 | 2787 | r10_bio->devs[0].addr = from_addr; |
ab9d47e9 | 2788 | r10_bio->devs[1].devnum = i; |
5e570289 | 2789 | r10_bio->devs[1].addr = to_addr; |
ab9d47e9 | 2790 | |
24afd80d N |
2791 | rdev = mirror->rdev; |
2792 | if (!test_bit(In_sync, &rdev->flags)) { | |
2793 | bio = r10_bio->devs[1].bio; | |
2794 | bio->bi_next = biolist; | |
2795 | biolist = bio; | |
2796 | bio->bi_private = r10_bio; | |
2797 | bio->bi_end_io = end_sync_write; | |
2798 | bio->bi_rw = WRITE; | |
2799 | bio->bi_sector = to_addr | |
2800 | + rdev->data_offset; | |
2801 | bio->bi_bdev = rdev->bdev; | |
2802 | atomic_inc(&r10_bio->remaining); | |
2803 | } else | |
2804 | r10_bio->devs[1].bio->bi_end_io = NULL; | |
2805 | ||
2806 | /* and maybe write to replacement */ | |
2807 | bio = r10_bio->devs[1].repl_bio; | |
2808 | if (bio) | |
2809 | bio->bi_end_io = NULL; | |
2810 | rdev = mirror->replacement; | |
2811 | /* Note: if rdev != NULL, then bio | |
2812 | * cannot be NULL as r10buf_pool_alloc will | |
2813 | * have allocated it. | |
2814 | * So the second test here is pointless. | |
2815 | * But it keeps semantic-checkers happy, and | |
2816 | * this comment keeps human reviewers | |
2817 | * happy. | |
2818 | */ | |
2819 | if (rdev == NULL || bio == NULL || | |
2820 | test_bit(Faulty, &rdev->flags)) | |
2821 | break; | |
2822 | bio->bi_next = biolist; | |
2823 | biolist = bio; | |
2824 | bio->bi_private = r10_bio; | |
2825 | bio->bi_end_io = end_sync_write; | |
2826 | bio->bi_rw = WRITE; | |
2827 | bio->bi_sector = to_addr + rdev->data_offset; | |
2828 | bio->bi_bdev = rdev->bdev; | |
2829 | atomic_inc(&r10_bio->remaining); | |
ab9d47e9 N |
2830 | break; |
2831 | } | |
2832 | if (j == conf->copies) { | |
e875ecea N |
2833 | /* Cannot recover, so abort the recovery or |
2834 | * record a bad block */ | |
ab9d47e9 N |
2835 | put_buf(r10_bio); |
2836 | if (rb2) | |
2837 | atomic_dec(&rb2->remaining); | |
2838 | r10_bio = rb2; | |
e875ecea N |
2839 | if (any_working) { |
2840 | /* problem is that there are bad blocks | |
2841 | * on other device(s) | |
2842 | */ | |
2843 | int k; | |
2844 | for (k = 0; k < conf->copies; k++) | |
2845 | if (r10_bio->devs[k].devnum == i) | |
2846 | break; | |
24afd80d N |
2847 | if (!test_bit(In_sync, |
2848 | &mirror->rdev->flags) | |
2849 | && !rdev_set_badblocks( | |
2850 | mirror->rdev, | |
2851 | r10_bio->devs[k].addr, | |
2852 | max_sync, 0)) | |
2853 | any_working = 0; | |
2854 | if (mirror->replacement && | |
2855 | !rdev_set_badblocks( | |
2856 | mirror->replacement, | |
e875ecea N |
2857 | r10_bio->devs[k].addr, |
2858 | max_sync, 0)) | |
2859 | any_working = 0; | |
2860 | } | |
2861 | if (!any_working) { | |
2862 | if (!test_and_set_bit(MD_RECOVERY_INTR, | |
2863 | &mddev->recovery)) | |
2864 | printk(KERN_INFO "md/raid10:%s: insufficient " | |
2865 | "working devices for recovery.\n", | |
2866 | mdname(mddev)); | |
24afd80d | 2867 | mirror->recovery_disabled |
e875ecea N |
2868 | = mddev->recovery_disabled; |
2869 | } | |
ab9d47e9 | 2870 | break; |
1da177e4 | 2871 | } |
ab9d47e9 | 2872 | } |
1da177e4 LT |
2873 | if (biolist == NULL) { |
2874 | while (r10_bio) { | |
9f2c9d12 N |
2875 | struct r10bio *rb2 = r10_bio; |
2876 | r10_bio = (struct r10bio*) rb2->master_bio; | |
1da177e4 LT |
2877 | rb2->master_bio = NULL; |
2878 | put_buf(rb2); | |
2879 | } | |
2880 | goto giveup; | |
2881 | } | |
2882 | } else { | |
2883 | /* resync. Schedule a read for every block at this virt offset */ | |
2884 | int count = 0; | |
6cce3b23 | 2885 | |
78200d45 N |
2886 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
2887 | ||
6cce3b23 N |
2888 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, |
2889 | &sync_blocks, mddev->degraded) && | |
ab9d47e9 N |
2890 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, |
2891 | &mddev->recovery)) { | |
6cce3b23 N |
2892 | /* We can skip this block */ |
2893 | *skipped = 1; | |
2894 | return sync_blocks + sectors_skipped; | |
2895 | } | |
2896 | if (sync_blocks < max_sync) | |
2897 | max_sync = sync_blocks; | |
1da177e4 LT |
2898 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
2899 | ||
1da177e4 LT |
2900 | r10_bio->mddev = mddev; |
2901 | atomic_set(&r10_bio->remaining, 0); | |
6cce3b23 N |
2902 | raise_barrier(conf, 0); |
2903 | conf->next_resync = sector_nr; | |
1da177e4 LT |
2904 | |
2905 | r10_bio->master_bio = NULL; | |
2906 | r10_bio->sector = sector_nr; | |
2907 | set_bit(R10BIO_IsSync, &r10_bio->state); | |
2908 | raid10_find_phys(conf, r10_bio); | |
2909 | r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1; | |
2910 | ||
2911 | for (i=0; i<conf->copies; i++) { | |
2912 | int d = r10_bio->devs[i].devnum; | |
40c356ce N |
2913 | sector_t first_bad, sector; |
2914 | int bad_sectors; | |
2915 | ||
9ad1aefc N |
2916 | if (r10_bio->devs[i].repl_bio) |
2917 | r10_bio->devs[i].repl_bio->bi_end_io = NULL; | |
2918 | ||
1da177e4 LT |
2919 | bio = r10_bio->devs[i].bio; |
2920 | bio->bi_end_io = NULL; | |
af03b8e4 | 2921 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
1da177e4 | 2922 | if (conf->mirrors[d].rdev == NULL || |
b2d444d7 | 2923 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) |
1da177e4 | 2924 | continue; |
40c356ce N |
2925 | sector = r10_bio->devs[i].addr; |
2926 | if (is_badblock(conf->mirrors[d].rdev, | |
2927 | sector, max_sync, | |
2928 | &first_bad, &bad_sectors)) { | |
2929 | if (first_bad > sector) | |
2930 | max_sync = first_bad - sector; | |
2931 | else { | |
2932 | bad_sectors -= (sector - first_bad); | |
2933 | if (max_sync > bad_sectors) | |
2934 | max_sync = max_sync; | |
2935 | continue; | |
2936 | } | |
2937 | } | |
1da177e4 LT |
2938 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
2939 | atomic_inc(&r10_bio->remaining); | |
2940 | bio->bi_next = biolist; | |
2941 | biolist = bio; | |
2942 | bio->bi_private = r10_bio; | |
2943 | bio->bi_end_io = end_sync_read; | |
802ba064 | 2944 | bio->bi_rw = READ; |
40c356ce | 2945 | bio->bi_sector = sector + |
1da177e4 LT |
2946 | conf->mirrors[d].rdev->data_offset; |
2947 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
2948 | count++; | |
9ad1aefc N |
2949 | |
2950 | if (conf->mirrors[d].replacement == NULL || | |
2951 | test_bit(Faulty, | |
2952 | &conf->mirrors[d].replacement->flags)) | |
2953 | continue; | |
2954 | ||
2955 | /* Need to set up for writing to the replacement */ | |
2956 | bio = r10_bio->devs[i].repl_bio; | |
2957 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | |
2958 | ||
2959 | sector = r10_bio->devs[i].addr; | |
2960 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
2961 | bio->bi_next = biolist; | |
2962 | biolist = bio; | |
2963 | bio->bi_private = r10_bio; | |
2964 | bio->bi_end_io = end_sync_write; | |
2965 | bio->bi_rw = WRITE; | |
2966 | bio->bi_sector = sector + | |
2967 | conf->mirrors[d].replacement->data_offset; | |
2968 | bio->bi_bdev = conf->mirrors[d].replacement->bdev; | |
2969 | count++; | |
1da177e4 LT |
2970 | } |
2971 | ||
2972 | if (count < 2) { | |
2973 | for (i=0; i<conf->copies; i++) { | |
2974 | int d = r10_bio->devs[i].devnum; | |
2975 | if (r10_bio->devs[i].bio->bi_end_io) | |
ab9d47e9 N |
2976 | rdev_dec_pending(conf->mirrors[d].rdev, |
2977 | mddev); | |
9ad1aefc N |
2978 | if (r10_bio->devs[i].repl_bio && |
2979 | r10_bio->devs[i].repl_bio->bi_end_io) | |
2980 | rdev_dec_pending( | |
2981 | conf->mirrors[d].replacement, | |
2982 | mddev); | |
1da177e4 LT |
2983 | } |
2984 | put_buf(r10_bio); | |
2985 | biolist = NULL; | |
2986 | goto giveup; | |
2987 | } | |
2988 | } | |
2989 | ||
2990 | for (bio = biolist; bio ; bio=bio->bi_next) { | |
2991 | ||
2992 | bio->bi_flags &= ~(BIO_POOL_MASK - 1); | |
2993 | if (bio->bi_end_io) | |
2994 | bio->bi_flags |= 1 << BIO_UPTODATE; | |
2995 | bio->bi_vcnt = 0; | |
2996 | bio->bi_idx = 0; | |
2997 | bio->bi_phys_segments = 0; | |
1da177e4 LT |
2998 | bio->bi_size = 0; |
2999 | } | |
3000 | ||
3001 | nr_sectors = 0; | |
6cce3b23 N |
3002 | if (sector_nr + max_sync < max_sector) |
3003 | max_sector = sector_nr + max_sync; | |
1da177e4 LT |
3004 | do { |
3005 | struct page *page; | |
3006 | int len = PAGE_SIZE; | |
1da177e4 LT |
3007 | if (sector_nr + (len>>9) > max_sector) |
3008 | len = (max_sector - sector_nr) << 9; | |
3009 | if (len == 0) | |
3010 | break; | |
3011 | for (bio= biolist ; bio ; bio=bio->bi_next) { | |
ab9d47e9 | 3012 | struct bio *bio2; |
1da177e4 | 3013 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; |
ab9d47e9 N |
3014 | if (bio_add_page(bio, page, len, 0)) |
3015 | continue; | |
3016 | ||
3017 | /* stop here */ | |
3018 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; | |
3019 | for (bio2 = biolist; | |
3020 | bio2 && bio2 != bio; | |
3021 | bio2 = bio2->bi_next) { | |
3022 | /* remove last page from this bio */ | |
3023 | bio2->bi_vcnt--; | |
3024 | bio2->bi_size -= len; | |
3025 | bio2->bi_flags &= ~(1<< BIO_SEG_VALID); | |
1da177e4 | 3026 | } |
ab9d47e9 | 3027 | goto bio_full; |
1da177e4 LT |
3028 | } |
3029 | nr_sectors += len>>9; | |
3030 | sector_nr += len>>9; | |
3031 | } while (biolist->bi_vcnt < RESYNC_PAGES); | |
3032 | bio_full: | |
3033 | r10_bio->sectors = nr_sectors; | |
3034 | ||
3035 | while (biolist) { | |
3036 | bio = biolist; | |
3037 | biolist = biolist->bi_next; | |
3038 | ||
3039 | bio->bi_next = NULL; | |
3040 | r10_bio = bio->bi_private; | |
3041 | r10_bio->sectors = nr_sectors; | |
3042 | ||
3043 | if (bio->bi_end_io == end_sync_read) { | |
3044 | md_sync_acct(bio->bi_bdev, nr_sectors); | |
3045 | generic_make_request(bio); | |
3046 | } | |
3047 | } | |
3048 | ||
57afd89f N |
3049 | if (sectors_skipped) |
3050 | /* pretend they weren't skipped, it makes | |
3051 | * no important difference in this case | |
3052 | */ | |
3053 | md_done_sync(mddev, sectors_skipped, 1); | |
3054 | ||
1da177e4 LT |
3055 | return sectors_skipped + nr_sectors; |
3056 | giveup: | |
3057 | /* There is nowhere to write, so all non-sync | |
e875ecea N |
3058 | * drives must be failed or in resync, all drives |
3059 | * have a bad block, so try the next chunk... | |
1da177e4 | 3060 | */ |
09b4068a N |
3061 | if (sector_nr + max_sync < max_sector) |
3062 | max_sector = sector_nr + max_sync; | |
3063 | ||
3064 | sectors_skipped += (max_sector - sector_nr); | |
1da177e4 LT |
3065 | chunks_skipped ++; |
3066 | sector_nr = max_sector; | |
1da177e4 | 3067 | goto skipped; |
1da177e4 LT |
3068 | } |
3069 | ||
80c3a6ce | 3070 | static sector_t |
fd01b88c | 3071 | raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
80c3a6ce DW |
3072 | { |
3073 | sector_t size; | |
e879a879 | 3074 | struct r10conf *conf = mddev->private; |
80c3a6ce DW |
3075 | |
3076 | if (!raid_disks) | |
84707f38 | 3077 | raid_disks = conf->raid_disks; |
80c3a6ce | 3078 | if (!sectors) |
dab8b292 | 3079 | sectors = conf->dev_sectors; |
80c3a6ce DW |
3080 | |
3081 | size = sectors >> conf->chunk_shift; | |
3082 | sector_div(size, conf->far_copies); | |
3083 | size = size * raid_disks; | |
3084 | sector_div(size, conf->near_copies); | |
3085 | ||
3086 | return size << conf->chunk_shift; | |
3087 | } | |
3088 | ||
dab8b292 | 3089 | |
e879a879 | 3090 | static struct r10conf *setup_conf(struct mddev *mddev) |
1da177e4 | 3091 | { |
e879a879 | 3092 | struct r10conf *conf = NULL; |
c93983bf | 3093 | int nc, fc, fo; |
1da177e4 | 3094 | sector_t stride, size; |
dab8b292 | 3095 | int err = -EINVAL; |
1da177e4 | 3096 | |
f73ea873 MT |
3097 | if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) || |
3098 | !is_power_of_2(mddev->new_chunk_sectors)) { | |
128595ed N |
3099 | printk(KERN_ERR "md/raid10:%s: chunk size must be " |
3100 | "at least PAGE_SIZE(%ld) and be a power of 2.\n", | |
3101 | mdname(mddev), PAGE_SIZE); | |
dab8b292 | 3102 | goto out; |
1da177e4 | 3103 | } |
2604b703 | 3104 | |
f73ea873 MT |
3105 | nc = mddev->new_layout & 255; |
3106 | fc = (mddev->new_layout >> 8) & 255; | |
3107 | fo = mddev->new_layout & (1<<16); | |
dab8b292 | 3108 | |
1da177e4 | 3109 | if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks || |
f73ea873 | 3110 | (mddev->new_layout >> 17)) { |
128595ed | 3111 | printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n", |
f73ea873 | 3112 | mdname(mddev), mddev->new_layout); |
1da177e4 LT |
3113 | goto out; |
3114 | } | |
dab8b292 TM |
3115 | |
3116 | err = -ENOMEM; | |
e879a879 | 3117 | conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); |
dab8b292 | 3118 | if (!conf) |
1da177e4 | 3119 | goto out; |
dab8b292 | 3120 | |
4443ae10 | 3121 | conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks, |
dab8b292 TM |
3122 | GFP_KERNEL); |
3123 | if (!conf->mirrors) | |
3124 | goto out; | |
4443ae10 N |
3125 | |
3126 | conf->tmppage = alloc_page(GFP_KERNEL); | |
3127 | if (!conf->tmppage) | |
dab8b292 TM |
3128 | goto out; |
3129 | ||
1da177e4 | 3130 | |
64a742bc | 3131 | conf->raid_disks = mddev->raid_disks; |
1da177e4 LT |
3132 | conf->near_copies = nc; |
3133 | conf->far_copies = fc; | |
3134 | conf->copies = nc*fc; | |
c93983bf | 3135 | conf->far_offset = fo; |
dab8b292 TM |
3136 | conf->chunk_mask = mddev->new_chunk_sectors - 1; |
3137 | conf->chunk_shift = ffz(~mddev->new_chunk_sectors); | |
3138 | ||
3139 | conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, | |
3140 | r10bio_pool_free, conf); | |
3141 | if (!conf->r10bio_pool) | |
3142 | goto out; | |
3143 | ||
58c0fed4 | 3144 | size = mddev->dev_sectors >> conf->chunk_shift; |
64a742bc N |
3145 | sector_div(size, fc); |
3146 | size = size * conf->raid_disks; | |
3147 | sector_div(size, nc); | |
3148 | /* 'size' is now the number of chunks in the array */ | |
3149 | /* calculate "used chunks per device" in 'stride' */ | |
3150 | stride = size * conf->copies; | |
af03b8e4 N |
3151 | |
3152 | /* We need to round up when dividing by raid_disks to | |
3153 | * get the stride size. | |
3154 | */ | |
3155 | stride += conf->raid_disks - 1; | |
64a742bc | 3156 | sector_div(stride, conf->raid_disks); |
dab8b292 TM |
3157 | |
3158 | conf->dev_sectors = stride << conf->chunk_shift; | |
64a742bc | 3159 | |
c93983bf | 3160 | if (fo) |
64a742bc N |
3161 | stride = 1; |
3162 | else | |
c93983bf | 3163 | sector_div(stride, fc); |
64a742bc N |
3164 | conf->stride = stride << conf->chunk_shift; |
3165 | ||
1da177e4 | 3166 | |
e7e72bf6 | 3167 | spin_lock_init(&conf->device_lock); |
dab8b292 TM |
3168 | INIT_LIST_HEAD(&conf->retry_list); |
3169 | ||
3170 | spin_lock_init(&conf->resync_lock); | |
3171 | init_waitqueue_head(&conf->wait_barrier); | |
3172 | ||
3173 | conf->thread = md_register_thread(raid10d, mddev, NULL); | |
3174 | if (!conf->thread) | |
3175 | goto out; | |
3176 | ||
dab8b292 TM |
3177 | conf->mddev = mddev; |
3178 | return conf; | |
3179 | ||
3180 | out: | |
128595ed | 3181 | printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n", |
dab8b292 TM |
3182 | mdname(mddev)); |
3183 | if (conf) { | |
3184 | if (conf->r10bio_pool) | |
3185 | mempool_destroy(conf->r10bio_pool); | |
3186 | kfree(conf->mirrors); | |
3187 | safe_put_page(conf->tmppage); | |
3188 | kfree(conf); | |
3189 | } | |
3190 | return ERR_PTR(err); | |
3191 | } | |
3192 | ||
fd01b88c | 3193 | static int run(struct mddev *mddev) |
dab8b292 | 3194 | { |
e879a879 | 3195 | struct r10conf *conf; |
dab8b292 | 3196 | int i, disk_idx, chunk_size; |
0f6d02d5 | 3197 | struct mirror_info *disk; |
3cb03002 | 3198 | struct md_rdev *rdev; |
dab8b292 TM |
3199 | sector_t size; |
3200 | ||
3201 | /* | |
3202 | * copy the already verified devices into our private RAID10 | |
3203 | * bookkeeping area. [whatever we allocate in run(), | |
3204 | * should be freed in stop()] | |
3205 | */ | |
3206 | ||
3207 | if (mddev->private == NULL) { | |
3208 | conf = setup_conf(mddev); | |
3209 | if (IS_ERR(conf)) | |
3210 | return PTR_ERR(conf); | |
3211 | mddev->private = conf; | |
3212 | } | |
3213 | conf = mddev->private; | |
3214 | if (!conf) | |
3215 | goto out; | |
3216 | ||
dab8b292 TM |
3217 | mddev->thread = conf->thread; |
3218 | conf->thread = NULL; | |
3219 | ||
8f6c2e4b MP |
3220 | chunk_size = mddev->chunk_sectors << 9; |
3221 | blk_queue_io_min(mddev->queue, chunk_size); | |
3222 | if (conf->raid_disks % conf->near_copies) | |
3223 | blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks); | |
3224 | else | |
3225 | blk_queue_io_opt(mddev->queue, chunk_size * | |
3226 | (conf->raid_disks / conf->near_copies)); | |
3227 | ||
159ec1fc | 3228 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
34b343cf | 3229 | |
1da177e4 | 3230 | disk_idx = rdev->raid_disk; |
84707f38 | 3231 | if (disk_idx >= conf->raid_disks |
1da177e4 LT |
3232 | || disk_idx < 0) |
3233 | continue; | |
3234 | disk = conf->mirrors + disk_idx; | |
3235 | ||
56a2559b N |
3236 | if (test_bit(Replacement, &rdev->flags)) { |
3237 | if (disk->replacement) | |
3238 | goto out_free_conf; | |
3239 | disk->replacement = rdev; | |
3240 | } else { | |
3241 | if (disk->rdev) | |
3242 | goto out_free_conf; | |
3243 | disk->rdev = rdev; | |
3244 | } | |
3245 | ||
1da177e4 | 3246 | disk->rdev = rdev; |
8f6c2e4b MP |
3247 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
3248 | rdev->data_offset << 9); | |
1da177e4 | 3249 | /* as we don't honour merge_bvec_fn, we must never risk |
627a2d3c N |
3250 | * violating it, so limit max_segments to 1 lying |
3251 | * within a single page. | |
1da177e4 | 3252 | */ |
627a2d3c N |
3253 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { |
3254 | blk_queue_max_segments(mddev->queue, 1); | |
3255 | blk_queue_segment_boundary(mddev->queue, | |
3256 | PAGE_CACHE_SIZE - 1); | |
3257 | } | |
1da177e4 LT |
3258 | |
3259 | disk->head_position = 0; | |
1da177e4 | 3260 | } |
6d508242 | 3261 | /* need to check that every block has at least one working mirror */ |
700c7213 | 3262 | if (!enough(conf, -1)) { |
128595ed | 3263 | printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n", |
6d508242 | 3264 | mdname(mddev)); |
1da177e4 LT |
3265 | goto out_free_conf; |
3266 | } | |
3267 | ||
3268 | mddev->degraded = 0; | |
3269 | for (i = 0; i < conf->raid_disks; i++) { | |
3270 | ||
3271 | disk = conf->mirrors + i; | |
3272 | ||
56a2559b N |
3273 | if (!disk->rdev && disk->replacement) { |
3274 | /* The replacement is all we have - use it */ | |
3275 | disk->rdev = disk->replacement; | |
3276 | disk->replacement = NULL; | |
3277 | clear_bit(Replacement, &disk->rdev->flags); | |
3278 | } | |
3279 | ||
5fd6c1dc | 3280 | if (!disk->rdev || |
2e333e89 | 3281 | !test_bit(In_sync, &disk->rdev->flags)) { |
1da177e4 LT |
3282 | disk->head_position = 0; |
3283 | mddev->degraded++; | |
8c2e870a NB |
3284 | if (disk->rdev) |
3285 | conf->fullsync = 1; | |
1da177e4 | 3286 | } |
d890fa2b | 3287 | disk->recovery_disabled = mddev->recovery_disabled - 1; |
1da177e4 LT |
3288 | } |
3289 | ||
8c6ac868 | 3290 | if (mddev->recovery_cp != MaxSector) |
128595ed | 3291 | printk(KERN_NOTICE "md/raid10:%s: not clean" |
8c6ac868 AN |
3292 | " -- starting background reconstruction\n", |
3293 | mdname(mddev)); | |
1da177e4 | 3294 | printk(KERN_INFO |
128595ed | 3295 | "md/raid10:%s: active with %d out of %d devices\n", |
84707f38 N |
3296 | mdname(mddev), conf->raid_disks - mddev->degraded, |
3297 | conf->raid_disks); | |
1da177e4 LT |
3298 | /* |
3299 | * Ok, everything is just fine now | |
3300 | */ | |
dab8b292 TM |
3301 | mddev->dev_sectors = conf->dev_sectors; |
3302 | size = raid10_size(mddev, 0, 0); | |
3303 | md_set_array_sectors(mddev, size); | |
3304 | mddev->resync_max_sectors = size; | |
1da177e4 | 3305 | |
0d129228 N |
3306 | mddev->queue->backing_dev_info.congested_fn = raid10_congested; |
3307 | mddev->queue->backing_dev_info.congested_data = mddev; | |
7a5febe9 | 3308 | |
1da177e4 LT |
3309 | /* Calculate max read-ahead size. |
3310 | * We need to readahead at least twice a whole stripe.... | |
3311 | * maybe... | |
3312 | */ | |
3313 | { | |
9d8f0363 AN |
3314 | int stripe = conf->raid_disks * |
3315 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); | |
1da177e4 LT |
3316 | stripe /= conf->near_copies; |
3317 | if (mddev->queue->backing_dev_info.ra_pages < 2* stripe) | |
3318 | mddev->queue->backing_dev_info.ra_pages = 2* stripe; | |
3319 | } | |
3320 | ||
84707f38 | 3321 | if (conf->near_copies < conf->raid_disks) |
1da177e4 | 3322 | blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec); |
a91a2785 MP |
3323 | |
3324 | if (md_integrity_register(mddev)) | |
3325 | goto out_free_conf; | |
3326 | ||
1da177e4 LT |
3327 | return 0; |
3328 | ||
3329 | out_free_conf: | |
01f96c0a | 3330 | md_unregister_thread(&mddev->thread); |
1da177e4 LT |
3331 | if (conf->r10bio_pool) |
3332 | mempool_destroy(conf->r10bio_pool); | |
1345b1d8 | 3333 | safe_put_page(conf->tmppage); |
990a8baf | 3334 | kfree(conf->mirrors); |
1da177e4 LT |
3335 | kfree(conf); |
3336 | mddev->private = NULL; | |
3337 | out: | |
3338 | return -EIO; | |
3339 | } | |
3340 | ||
fd01b88c | 3341 | static int stop(struct mddev *mddev) |
1da177e4 | 3342 | { |
e879a879 | 3343 | struct r10conf *conf = mddev->private; |
1da177e4 | 3344 | |
409c57f3 N |
3345 | raise_barrier(conf, 0); |
3346 | lower_barrier(conf); | |
3347 | ||
01f96c0a | 3348 | md_unregister_thread(&mddev->thread); |
1da177e4 LT |
3349 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
3350 | if (conf->r10bio_pool) | |
3351 | mempool_destroy(conf->r10bio_pool); | |
990a8baf | 3352 | kfree(conf->mirrors); |
1da177e4 LT |
3353 | kfree(conf); |
3354 | mddev->private = NULL; | |
3355 | return 0; | |
3356 | } | |
3357 | ||
fd01b88c | 3358 | static void raid10_quiesce(struct mddev *mddev, int state) |
6cce3b23 | 3359 | { |
e879a879 | 3360 | struct r10conf *conf = mddev->private; |
6cce3b23 N |
3361 | |
3362 | switch(state) { | |
3363 | case 1: | |
3364 | raise_barrier(conf, 0); | |
3365 | break; | |
3366 | case 0: | |
3367 | lower_barrier(conf); | |
3368 | break; | |
3369 | } | |
6cce3b23 | 3370 | } |
1da177e4 | 3371 | |
fd01b88c | 3372 | static void *raid10_takeover_raid0(struct mddev *mddev) |
dab8b292 | 3373 | { |
3cb03002 | 3374 | struct md_rdev *rdev; |
e879a879 | 3375 | struct r10conf *conf; |
dab8b292 TM |
3376 | |
3377 | if (mddev->degraded > 0) { | |
128595ed N |
3378 | printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n", |
3379 | mdname(mddev)); | |
dab8b292 TM |
3380 | return ERR_PTR(-EINVAL); |
3381 | } | |
3382 | ||
dab8b292 TM |
3383 | /* Set new parameters */ |
3384 | mddev->new_level = 10; | |
3385 | /* new layout: far_copies = 1, near_copies = 2 */ | |
3386 | mddev->new_layout = (1<<8) + 2; | |
3387 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
3388 | mddev->delta_disks = mddev->raid_disks; | |
dab8b292 TM |
3389 | mddev->raid_disks *= 2; |
3390 | /* make sure it will be not marked as dirty */ | |
3391 | mddev->recovery_cp = MaxSector; | |
3392 | ||
3393 | conf = setup_conf(mddev); | |
02214dc5 | 3394 | if (!IS_ERR(conf)) { |
e93f68a1 N |
3395 | list_for_each_entry(rdev, &mddev->disks, same_set) |
3396 | if (rdev->raid_disk >= 0) | |
3397 | rdev->new_raid_disk = rdev->raid_disk * 2; | |
02214dc5 KW |
3398 | conf->barrier = 1; |
3399 | } | |
3400 | ||
dab8b292 TM |
3401 | return conf; |
3402 | } | |
3403 | ||
fd01b88c | 3404 | static void *raid10_takeover(struct mddev *mddev) |
dab8b292 | 3405 | { |
e373ab10 | 3406 | struct r0conf *raid0_conf; |
dab8b292 TM |
3407 | |
3408 | /* raid10 can take over: | |
3409 | * raid0 - providing it has only two drives | |
3410 | */ | |
3411 | if (mddev->level == 0) { | |
3412 | /* for raid0 takeover only one zone is supported */ | |
e373ab10 N |
3413 | raid0_conf = mddev->private; |
3414 | if (raid0_conf->nr_strip_zones > 1) { | |
128595ed N |
3415 | printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0" |
3416 | " with more than one zone.\n", | |
3417 | mdname(mddev)); | |
dab8b292 TM |
3418 | return ERR_PTR(-EINVAL); |
3419 | } | |
3420 | return raid10_takeover_raid0(mddev); | |
3421 | } | |
3422 | return ERR_PTR(-EINVAL); | |
3423 | } | |
3424 | ||
84fc4b56 | 3425 | static struct md_personality raid10_personality = |
1da177e4 LT |
3426 | { |
3427 | .name = "raid10", | |
2604b703 | 3428 | .level = 10, |
1da177e4 LT |
3429 | .owner = THIS_MODULE, |
3430 | .make_request = make_request, | |
3431 | .run = run, | |
3432 | .stop = stop, | |
3433 | .status = status, | |
3434 | .error_handler = error, | |
3435 | .hot_add_disk = raid10_add_disk, | |
3436 | .hot_remove_disk= raid10_remove_disk, | |
3437 | .spare_active = raid10_spare_active, | |
3438 | .sync_request = sync_request, | |
6cce3b23 | 3439 | .quiesce = raid10_quiesce, |
80c3a6ce | 3440 | .size = raid10_size, |
dab8b292 | 3441 | .takeover = raid10_takeover, |
1da177e4 LT |
3442 | }; |
3443 | ||
3444 | static int __init raid_init(void) | |
3445 | { | |
2604b703 | 3446 | return register_md_personality(&raid10_personality); |
1da177e4 LT |
3447 | } |
3448 | ||
3449 | static void raid_exit(void) | |
3450 | { | |
2604b703 | 3451 | unregister_md_personality(&raid10_personality); |
1da177e4 LT |
3452 | } |
3453 | ||
3454 | module_init(raid_init); | |
3455 | module_exit(raid_exit); | |
3456 | MODULE_LICENSE("GPL"); | |
0efb9e61 | 3457 | MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); |
1da177e4 | 3458 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ |
d9d166c2 | 3459 | MODULE_ALIAS("md-raid10"); |
2604b703 | 3460 | MODULE_ALIAS("md-level-10"); |
34db0cd6 N |
3461 | |
3462 | module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); |