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