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> |
3ea7daa5 | 27 | #include <linux/kthread.h> |
43b2e5d8 | 28 | #include "md.h" |
ef740c37 | 29 | #include "raid10.h" |
dab8b292 | 30 | #include "raid0.h" |
ef740c37 | 31 | #include "bitmap.h" |
1da177e4 LT |
32 | |
33 | /* | |
34 | * RAID10 provides a combination of RAID0 and RAID1 functionality. | |
35 | * The layout of data is defined by | |
36 | * chunk_size | |
37 | * raid_disks | |
38 | * near_copies (stored in low byte of layout) | |
39 | * far_copies (stored in second byte of layout) | |
c93983bf | 40 | * far_offset (stored in bit 16 of layout ) |
1da177e4 LT |
41 | * |
42 | * The data to be stored is divided into chunks using chunksize. | |
43 | * Each device is divided into far_copies sections. | |
44 | * In each section, chunks are laid out in a style similar to raid0, but | |
45 | * near_copies copies of each chunk is stored (each on a different drive). | |
46 | * The starting device for each section is offset near_copies from the starting | |
47 | * device of the previous section. | |
c93983bf | 48 | * Thus they are (near_copies*far_copies) of each chunk, and each is on a different |
1da177e4 LT |
49 | * drive. |
50 | * near_copies and far_copies must be at least one, and their product is at most | |
51 | * raid_disks. | |
c93983bf N |
52 | * |
53 | * If far_offset is true, then the far_copies are handled a bit differently. | |
54 | * The copies are still in different stripes, but instead of be very far apart | |
55 | * on disk, there are adjacent stripes. | |
1da177e4 LT |
56 | */ |
57 | ||
58 | /* | |
59 | * Number of guaranteed r10bios in case of extreme VM load: | |
60 | */ | |
61 | #define NR_RAID10_BIOS 256 | |
62 | ||
473e87ce JB |
63 | /* when we get a read error on a read-only array, we redirect to another |
64 | * device without failing the first device, or trying to over-write to | |
65 | * correct the read error. To keep track of bad blocks on a per-bio | |
66 | * level, we store IO_BLOCKED in the appropriate 'bios' pointer | |
67 | */ | |
68 | #define IO_BLOCKED ((struct bio *)1) | |
69 | /* When we successfully write to a known bad-block, we need to remove the | |
70 | * bad-block marking which must be done from process context. So we record | |
71 | * the success by setting devs[n].bio to IO_MADE_GOOD | |
72 | */ | |
73 | #define IO_MADE_GOOD ((struct bio *)2) | |
74 | ||
75 | #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) | |
76 | ||
77 | /* When there are this many requests queued to be written by | |
34db0cd6 N |
78 | * the raid10 thread, we become 'congested' to provide back-pressure |
79 | * for writeback. | |
80 | */ | |
81 | static int max_queued_requests = 1024; | |
82 | ||
e879a879 N |
83 | static void allow_barrier(struct r10conf *conf); |
84 | static void lower_barrier(struct r10conf *conf); | |
fae8cc5e | 85 | static int enough(struct r10conf *conf, int ignore); |
3ea7daa5 N |
86 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, |
87 | int *skipped); | |
88 | static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio); | |
89 | static void end_reshape_write(struct bio *bio, int error); | |
90 | static void end_reshape(struct r10conf *conf); | |
0a27ec96 | 91 | |
dd0fc66f | 92 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 | 93 | { |
e879a879 | 94 | struct r10conf *conf = data; |
9f2c9d12 | 95 | int size = offsetof(struct r10bio, devs[conf->copies]); |
1da177e4 | 96 | |
69335ef3 N |
97 | /* allocate a r10bio with room for raid_disks entries in the |
98 | * bios array */ | |
7eaceacc | 99 | return kzalloc(size, gfp_flags); |
1da177e4 LT |
100 | } |
101 | ||
102 | static void r10bio_pool_free(void *r10_bio, void *data) | |
103 | { | |
104 | kfree(r10_bio); | |
105 | } | |
106 | ||
0310fa21 | 107 | /* Maximum size of each resync request */ |
1da177e4 | 108 | #define RESYNC_BLOCK_SIZE (64*1024) |
1da177e4 | 109 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) |
0310fa21 N |
110 | /* amount of memory to reserve for resync requests */ |
111 | #define RESYNC_WINDOW (1024*1024) | |
112 | /* maximum number of concurrent requests, memory permitting */ | |
113 | #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) | |
1da177e4 LT |
114 | |
115 | /* | |
116 | * When performing a resync, we need to read and compare, so | |
117 | * we need as many pages are there are copies. | |
118 | * When performing a recovery, we need 2 bios, one for read, | |
119 | * one for write (we recover only one drive per r10buf) | |
120 | * | |
121 | */ | |
dd0fc66f | 122 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 | 123 | { |
e879a879 | 124 | struct r10conf *conf = data; |
1da177e4 | 125 | struct page *page; |
9f2c9d12 | 126 | struct r10bio *r10_bio; |
1da177e4 LT |
127 | struct bio *bio; |
128 | int i, j; | |
129 | int nalloc; | |
130 | ||
131 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); | |
7eaceacc | 132 | if (!r10_bio) |
1da177e4 | 133 | return NULL; |
1da177e4 | 134 | |
3ea7daa5 N |
135 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || |
136 | test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) | |
1da177e4 LT |
137 | nalloc = conf->copies; /* resync */ |
138 | else | |
139 | nalloc = 2; /* recovery */ | |
140 | ||
141 | /* | |
142 | * Allocate bios. | |
143 | */ | |
144 | for (j = nalloc ; j-- ; ) { | |
6746557f | 145 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
1da177e4 LT |
146 | if (!bio) |
147 | goto out_free_bio; | |
148 | r10_bio->devs[j].bio = bio; | |
69335ef3 N |
149 | if (!conf->have_replacement) |
150 | continue; | |
151 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); | |
152 | if (!bio) | |
153 | goto out_free_bio; | |
154 | r10_bio->devs[j].repl_bio = bio; | |
1da177e4 LT |
155 | } |
156 | /* | |
157 | * Allocate RESYNC_PAGES data pages and attach them | |
158 | * where needed. | |
159 | */ | |
160 | for (j = 0 ; j < nalloc; j++) { | |
69335ef3 | 161 | struct bio *rbio = r10_bio->devs[j].repl_bio; |
1da177e4 LT |
162 | bio = r10_bio->devs[j].bio; |
163 | for (i = 0; i < RESYNC_PAGES; i++) { | |
3ea7daa5 N |
164 | if (j > 0 && !test_bit(MD_RECOVERY_SYNC, |
165 | &conf->mddev->recovery)) { | |
166 | /* we can share bv_page's during recovery | |
167 | * and reshape */ | |
c65060ad NK |
168 | struct bio *rbio = r10_bio->devs[0].bio; |
169 | page = rbio->bi_io_vec[i].bv_page; | |
170 | get_page(page); | |
171 | } else | |
172 | page = alloc_page(gfp_flags); | |
1da177e4 LT |
173 | if (unlikely(!page)) |
174 | goto out_free_pages; | |
175 | ||
176 | bio->bi_io_vec[i].bv_page = page; | |
69335ef3 N |
177 | if (rbio) |
178 | rbio->bi_io_vec[i].bv_page = page; | |
1da177e4 LT |
179 | } |
180 | } | |
181 | ||
182 | return r10_bio; | |
183 | ||
184 | out_free_pages: | |
185 | for ( ; i > 0 ; i--) | |
1345b1d8 | 186 | safe_put_page(bio->bi_io_vec[i-1].bv_page); |
1da177e4 LT |
187 | while (j--) |
188 | for (i = 0; i < RESYNC_PAGES ; i++) | |
1345b1d8 | 189 | safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page); |
5fdd2cf8 | 190 | j = 0; |
1da177e4 | 191 | out_free_bio: |
5fdd2cf8 | 192 | for ( ; j < nalloc; j++) { |
193 | if (r10_bio->devs[j].bio) | |
194 | bio_put(r10_bio->devs[j].bio); | |
69335ef3 N |
195 | if (r10_bio->devs[j].repl_bio) |
196 | bio_put(r10_bio->devs[j].repl_bio); | |
197 | } | |
1da177e4 LT |
198 | r10bio_pool_free(r10_bio, conf); |
199 | return NULL; | |
200 | } | |
201 | ||
202 | static void r10buf_pool_free(void *__r10_bio, void *data) | |
203 | { | |
204 | int i; | |
e879a879 | 205 | struct r10conf *conf = data; |
9f2c9d12 | 206 | struct r10bio *r10bio = __r10_bio; |
1da177e4 LT |
207 | int j; |
208 | ||
209 | for (j=0; j < conf->copies; j++) { | |
210 | struct bio *bio = r10bio->devs[j].bio; | |
211 | if (bio) { | |
212 | for (i = 0; i < RESYNC_PAGES; i++) { | |
1345b1d8 | 213 | safe_put_page(bio->bi_io_vec[i].bv_page); |
1da177e4 LT |
214 | bio->bi_io_vec[i].bv_page = NULL; |
215 | } | |
216 | bio_put(bio); | |
217 | } | |
69335ef3 N |
218 | bio = r10bio->devs[j].repl_bio; |
219 | if (bio) | |
220 | bio_put(bio); | |
1da177e4 LT |
221 | } |
222 | r10bio_pool_free(r10bio, conf); | |
223 | } | |
224 | ||
e879a879 | 225 | static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio) |
1da177e4 LT |
226 | { |
227 | int i; | |
228 | ||
229 | for (i = 0; i < conf->copies; i++) { | |
230 | struct bio **bio = & r10_bio->devs[i].bio; | |
749c55e9 | 231 | if (!BIO_SPECIAL(*bio)) |
1da177e4 LT |
232 | bio_put(*bio); |
233 | *bio = NULL; | |
69335ef3 N |
234 | bio = &r10_bio->devs[i].repl_bio; |
235 | if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio)) | |
236 | bio_put(*bio); | |
237 | *bio = NULL; | |
1da177e4 LT |
238 | } |
239 | } | |
240 | ||
9f2c9d12 | 241 | static void free_r10bio(struct r10bio *r10_bio) |
1da177e4 | 242 | { |
e879a879 | 243 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 | 244 | |
1da177e4 LT |
245 | put_all_bios(conf, r10_bio); |
246 | mempool_free(r10_bio, conf->r10bio_pool); | |
247 | } | |
248 | ||
9f2c9d12 | 249 | static void put_buf(struct r10bio *r10_bio) |
1da177e4 | 250 | { |
e879a879 | 251 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 LT |
252 | |
253 | mempool_free(r10_bio, conf->r10buf_pool); | |
254 | ||
0a27ec96 | 255 | lower_barrier(conf); |
1da177e4 LT |
256 | } |
257 | ||
9f2c9d12 | 258 | static void reschedule_retry(struct r10bio *r10_bio) |
1da177e4 LT |
259 | { |
260 | unsigned long flags; | |
fd01b88c | 261 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 262 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
263 | |
264 | spin_lock_irqsave(&conf->device_lock, flags); | |
265 | list_add(&r10_bio->retry_list, &conf->retry_list); | |
4443ae10 | 266 | conf->nr_queued ++; |
1da177e4 LT |
267 | spin_unlock_irqrestore(&conf->device_lock, flags); |
268 | ||
388667be AJ |
269 | /* wake up frozen array... */ |
270 | wake_up(&conf->wait_barrier); | |
271 | ||
1da177e4 LT |
272 | md_wakeup_thread(mddev->thread); |
273 | } | |
274 | ||
275 | /* | |
276 | * raid_end_bio_io() is called when we have finished servicing a mirrored | |
277 | * operation and are ready to return a success/failure code to the buffer | |
278 | * cache layer. | |
279 | */ | |
9f2c9d12 | 280 | static void raid_end_bio_io(struct r10bio *r10_bio) |
1da177e4 LT |
281 | { |
282 | struct bio *bio = r10_bio->master_bio; | |
856e08e2 | 283 | int done; |
e879a879 | 284 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 | 285 | |
856e08e2 N |
286 | if (bio->bi_phys_segments) { |
287 | unsigned long flags; | |
288 | spin_lock_irqsave(&conf->device_lock, flags); | |
289 | bio->bi_phys_segments--; | |
290 | done = (bio->bi_phys_segments == 0); | |
291 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
292 | } else | |
293 | done = 1; | |
294 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) | |
295 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | |
296 | if (done) { | |
297 | bio_endio(bio, 0); | |
298 | /* | |
299 | * Wake up any possible resync thread that waits for the device | |
300 | * to go idle. | |
301 | */ | |
302 | allow_barrier(conf); | |
303 | } | |
1da177e4 LT |
304 | free_r10bio(r10_bio); |
305 | } | |
306 | ||
307 | /* | |
308 | * Update disk head position estimator based on IRQ completion info. | |
309 | */ | |
9f2c9d12 | 310 | static inline void update_head_pos(int slot, struct r10bio *r10_bio) |
1da177e4 | 311 | { |
e879a879 | 312 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 LT |
313 | |
314 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = | |
315 | r10_bio->devs[slot].addr + (r10_bio->sectors); | |
316 | } | |
317 | ||
778ca018 NK |
318 | /* |
319 | * Find the disk number which triggered given bio | |
320 | */ | |
e879a879 | 321 | static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio, |
69335ef3 | 322 | struct bio *bio, int *slotp, int *replp) |
778ca018 NK |
323 | { |
324 | int slot; | |
69335ef3 | 325 | int repl = 0; |
778ca018 | 326 | |
69335ef3 | 327 | for (slot = 0; slot < conf->copies; slot++) { |
778ca018 NK |
328 | if (r10_bio->devs[slot].bio == bio) |
329 | break; | |
69335ef3 N |
330 | if (r10_bio->devs[slot].repl_bio == bio) { |
331 | repl = 1; | |
332 | break; | |
333 | } | |
334 | } | |
778ca018 NK |
335 | |
336 | BUG_ON(slot == conf->copies); | |
337 | update_head_pos(slot, r10_bio); | |
338 | ||
749c55e9 N |
339 | if (slotp) |
340 | *slotp = slot; | |
69335ef3 N |
341 | if (replp) |
342 | *replp = repl; | |
778ca018 NK |
343 | return r10_bio->devs[slot].devnum; |
344 | } | |
345 | ||
6712ecf8 | 346 | static void raid10_end_read_request(struct bio *bio, int error) |
1da177e4 LT |
347 | { |
348 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
9f2c9d12 | 349 | struct r10bio *r10_bio = bio->bi_private; |
1da177e4 | 350 | int slot, dev; |
abbf098e | 351 | struct md_rdev *rdev; |
e879a879 | 352 | struct r10conf *conf = r10_bio->mddev->private; |
1da177e4 | 353 | |
1da177e4 LT |
354 | |
355 | slot = r10_bio->read_slot; | |
356 | dev = r10_bio->devs[slot].devnum; | |
abbf098e | 357 | rdev = r10_bio->devs[slot].rdev; |
1da177e4 LT |
358 | /* |
359 | * this branch is our 'one mirror IO has finished' event handler: | |
360 | */ | |
4443ae10 N |
361 | update_head_pos(slot, r10_bio); |
362 | ||
363 | if (uptodate) { | |
1da177e4 LT |
364 | /* |
365 | * Set R10BIO_Uptodate in our master bio, so that | |
366 | * we will return a good error code to the higher | |
367 | * levels even if IO on some other mirrored buffer fails. | |
368 | * | |
369 | * The 'master' represents the composite IO operation to | |
370 | * user-side. So if something waits for IO, then it will | |
371 | * wait for the 'master' bio. | |
372 | */ | |
373 | set_bit(R10BIO_Uptodate, &r10_bio->state); | |
fae8cc5e N |
374 | } else { |
375 | /* If all other devices that store this block have | |
376 | * failed, we want to return the error upwards rather | |
377 | * than fail the last device. Here we redefine | |
378 | * "uptodate" to mean "Don't want to retry" | |
379 | */ | |
380 | unsigned long flags; | |
381 | spin_lock_irqsave(&conf->device_lock, flags); | |
382 | if (!enough(conf, rdev->raid_disk)) | |
383 | uptodate = 1; | |
384 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
385 | } | |
386 | if (uptodate) { | |
1da177e4 | 387 | raid_end_bio_io(r10_bio); |
abbf098e | 388 | rdev_dec_pending(rdev, conf->mddev); |
4443ae10 | 389 | } else { |
1da177e4 | 390 | /* |
7c4e06ff | 391 | * oops, read error - keep the refcount on the rdev |
1da177e4 LT |
392 | */ |
393 | char b[BDEVNAME_SIZE]; | |
8bda470e CD |
394 | printk_ratelimited(KERN_ERR |
395 | "md/raid10:%s: %s: rescheduling sector %llu\n", | |
396 | mdname(conf->mddev), | |
abbf098e | 397 | bdevname(rdev->bdev, b), |
8bda470e | 398 | (unsigned long long)r10_bio->sector); |
856e08e2 | 399 | set_bit(R10BIO_ReadError, &r10_bio->state); |
1da177e4 LT |
400 | reschedule_retry(r10_bio); |
401 | } | |
1da177e4 LT |
402 | } |
403 | ||
9f2c9d12 | 404 | static void close_write(struct r10bio *r10_bio) |
bd870a16 N |
405 | { |
406 | /* clear the bitmap if all writes complete successfully */ | |
407 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, | |
408 | r10_bio->sectors, | |
409 | !test_bit(R10BIO_Degraded, &r10_bio->state), | |
410 | 0); | |
411 | md_write_end(r10_bio->mddev); | |
412 | } | |
413 | ||
9f2c9d12 | 414 | static void one_write_done(struct r10bio *r10_bio) |
19d5f834 N |
415 | { |
416 | if (atomic_dec_and_test(&r10_bio->remaining)) { | |
417 | if (test_bit(R10BIO_WriteError, &r10_bio->state)) | |
418 | reschedule_retry(r10_bio); | |
419 | else { | |
420 | close_write(r10_bio); | |
421 | if (test_bit(R10BIO_MadeGood, &r10_bio->state)) | |
422 | reschedule_retry(r10_bio); | |
423 | else | |
424 | raid_end_bio_io(r10_bio); | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
6712ecf8 | 429 | static void raid10_end_write_request(struct bio *bio, int error) |
1da177e4 LT |
430 | { |
431 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
9f2c9d12 | 432 | struct r10bio *r10_bio = bio->bi_private; |
778ca018 | 433 | int dev; |
749c55e9 | 434 | int dec_rdev = 1; |
e879a879 | 435 | struct r10conf *conf = r10_bio->mddev->private; |
475b0321 | 436 | int slot, repl; |
4ca40c2c | 437 | struct md_rdev *rdev = NULL; |
1da177e4 | 438 | |
475b0321 | 439 | dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
1da177e4 | 440 | |
475b0321 N |
441 | if (repl) |
442 | rdev = conf->mirrors[dev].replacement; | |
4ca40c2c N |
443 | if (!rdev) { |
444 | smp_rmb(); | |
445 | repl = 0; | |
475b0321 | 446 | rdev = conf->mirrors[dev].rdev; |
4ca40c2c | 447 | } |
1da177e4 LT |
448 | /* |
449 | * this branch is our 'one mirror IO has finished' event handler: | |
450 | */ | |
6cce3b23 | 451 | if (!uptodate) { |
475b0321 N |
452 | if (repl) |
453 | /* Never record new bad blocks to replacement, | |
454 | * just fail it. | |
455 | */ | |
456 | md_error(rdev->mddev, rdev); | |
457 | else { | |
458 | set_bit(WriteErrorSeen, &rdev->flags); | |
b7044d41 N |
459 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
460 | set_bit(MD_RECOVERY_NEEDED, | |
461 | &rdev->mddev->recovery); | |
475b0321 N |
462 | set_bit(R10BIO_WriteError, &r10_bio->state); |
463 | dec_rdev = 0; | |
464 | } | |
749c55e9 | 465 | } else { |
1da177e4 LT |
466 | /* |
467 | * Set R10BIO_Uptodate in our master bio, so that | |
468 | * we will return a good error code for to the higher | |
469 | * levels even if IO on some other mirrored buffer fails. | |
470 | * | |
471 | * The 'master' represents the composite IO operation to | |
472 | * user-side. So if something waits for IO, then it will | |
473 | * wait for the 'master' bio. | |
474 | */ | |
749c55e9 N |
475 | sector_t first_bad; |
476 | int bad_sectors; | |
477 | ||
1da177e4 LT |
478 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
479 | ||
749c55e9 | 480 | /* Maybe we can clear some bad blocks. */ |
475b0321 | 481 | if (is_badblock(rdev, |
749c55e9 N |
482 | r10_bio->devs[slot].addr, |
483 | r10_bio->sectors, | |
484 | &first_bad, &bad_sectors)) { | |
485 | bio_put(bio); | |
475b0321 N |
486 | if (repl) |
487 | r10_bio->devs[slot].repl_bio = IO_MADE_GOOD; | |
488 | else | |
489 | r10_bio->devs[slot].bio = IO_MADE_GOOD; | |
749c55e9 N |
490 | dec_rdev = 0; |
491 | set_bit(R10BIO_MadeGood, &r10_bio->state); | |
492 | } | |
493 | } | |
494 | ||
1da177e4 LT |
495 | /* |
496 | * | |
497 | * Let's see if all mirrored write operations have finished | |
498 | * already. | |
499 | */ | |
19d5f834 | 500 | one_write_done(r10_bio); |
749c55e9 N |
501 | if (dec_rdev) |
502 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); | |
1da177e4 LT |
503 | } |
504 | ||
1da177e4 LT |
505 | /* |
506 | * RAID10 layout manager | |
25985edc | 507 | * As well as the chunksize and raid_disks count, there are two |
1da177e4 LT |
508 | * parameters: near_copies and far_copies. |
509 | * near_copies * far_copies must be <= raid_disks. | |
510 | * Normally one of these will be 1. | |
511 | * If both are 1, we get raid0. | |
512 | * If near_copies == raid_disks, we get raid1. | |
513 | * | |
25985edc | 514 | * Chunks are laid out in raid0 style with near_copies copies of the |
1da177e4 LT |
515 | * first chunk, followed by near_copies copies of the next chunk and |
516 | * so on. | |
517 | * If far_copies > 1, then after 1/far_copies of the array has been assigned | |
518 | * as described above, we start again with a device offset of near_copies. | |
519 | * So we effectively have another copy of the whole array further down all | |
520 | * the drives, but with blocks on different drives. | |
521 | * With this layout, and block is never stored twice on the one device. | |
522 | * | |
523 | * raid10_find_phys finds the sector offset of a given virtual sector | |
c93983bf | 524 | * on each device that it is on. |
1da177e4 LT |
525 | * |
526 | * raid10_find_virt does the reverse mapping, from a device and a | |
527 | * sector offset to a virtual address | |
528 | */ | |
529 | ||
f8c9e74f | 530 | static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio) |
1da177e4 LT |
531 | { |
532 | int n,f; | |
533 | sector_t sector; | |
534 | sector_t chunk; | |
535 | sector_t stripe; | |
536 | int dev; | |
1da177e4 LT |
537 | int slot = 0; |
538 | ||
539 | /* now calculate first sector/dev */ | |
5cf00fcd N |
540 | chunk = r10bio->sector >> geo->chunk_shift; |
541 | sector = r10bio->sector & geo->chunk_mask; | |
1da177e4 | 542 | |
5cf00fcd | 543 | chunk *= geo->near_copies; |
1da177e4 | 544 | stripe = chunk; |
5cf00fcd N |
545 | dev = sector_div(stripe, geo->raid_disks); |
546 | if (geo->far_offset) | |
547 | stripe *= geo->far_copies; | |
1da177e4 | 548 | |
5cf00fcd | 549 | sector += stripe << geo->chunk_shift; |
1da177e4 LT |
550 | |
551 | /* and calculate all the others */ | |
5cf00fcd | 552 | for (n = 0; n < geo->near_copies; n++) { |
1da177e4 LT |
553 | int d = dev; |
554 | sector_t s = sector; | |
555 | r10bio->devs[slot].addr = sector; | |
556 | r10bio->devs[slot].devnum = d; | |
557 | slot++; | |
558 | ||
5cf00fcd N |
559 | for (f = 1; f < geo->far_copies; f++) { |
560 | d += geo->near_copies; | |
561 | if (d >= geo->raid_disks) | |
562 | d -= geo->raid_disks; | |
563 | s += geo->stride; | |
1da177e4 LT |
564 | r10bio->devs[slot].devnum = d; |
565 | r10bio->devs[slot].addr = s; | |
566 | slot++; | |
567 | } | |
568 | dev++; | |
5cf00fcd | 569 | if (dev >= geo->raid_disks) { |
1da177e4 | 570 | dev = 0; |
5cf00fcd | 571 | sector += (geo->chunk_mask + 1); |
1da177e4 LT |
572 | } |
573 | } | |
f8c9e74f N |
574 | } |
575 | ||
576 | static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio) | |
577 | { | |
578 | struct geom *geo = &conf->geo; | |
579 | ||
580 | if (conf->reshape_progress != MaxSector && | |
581 | ((r10bio->sector >= conf->reshape_progress) != | |
582 | conf->mddev->reshape_backwards)) { | |
583 | set_bit(R10BIO_Previous, &r10bio->state); | |
584 | geo = &conf->prev; | |
585 | } else | |
586 | clear_bit(R10BIO_Previous, &r10bio->state); | |
587 | ||
588 | __raid10_find_phys(geo, r10bio); | |
1da177e4 LT |
589 | } |
590 | ||
e879a879 | 591 | static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev) |
1da177e4 LT |
592 | { |
593 | sector_t offset, chunk, vchunk; | |
f8c9e74f N |
594 | /* Never use conf->prev as this is only called during resync |
595 | * or recovery, so reshape isn't happening | |
596 | */ | |
5cf00fcd | 597 | struct geom *geo = &conf->geo; |
1da177e4 | 598 | |
5cf00fcd N |
599 | offset = sector & geo->chunk_mask; |
600 | if (geo->far_offset) { | |
c93983bf | 601 | int fc; |
5cf00fcd N |
602 | chunk = sector >> geo->chunk_shift; |
603 | fc = sector_div(chunk, geo->far_copies); | |
604 | dev -= fc * geo->near_copies; | |
c93983bf | 605 | if (dev < 0) |
5cf00fcd | 606 | dev += geo->raid_disks; |
c93983bf | 607 | } else { |
5cf00fcd N |
608 | while (sector >= geo->stride) { |
609 | sector -= geo->stride; | |
610 | if (dev < geo->near_copies) | |
611 | dev += geo->raid_disks - geo->near_copies; | |
c93983bf | 612 | else |
5cf00fcd | 613 | dev -= geo->near_copies; |
c93983bf | 614 | } |
5cf00fcd | 615 | chunk = sector >> geo->chunk_shift; |
c93983bf | 616 | } |
5cf00fcd N |
617 | vchunk = chunk * geo->raid_disks + dev; |
618 | sector_div(vchunk, geo->near_copies); | |
619 | return (vchunk << geo->chunk_shift) + offset; | |
1da177e4 LT |
620 | } |
621 | ||
622 | /** | |
623 | * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged | |
624 | * @q: request queue | |
cc371e66 | 625 | * @bvm: properties of new bio |
1da177e4 LT |
626 | * @biovec: the request that could be merged to it. |
627 | * | |
628 | * Return amount of bytes we can accept at this offset | |
050b6615 N |
629 | * This requires checking for end-of-chunk if near_copies != raid_disks, |
630 | * and for subordinate merge_bvec_fns if merge_check_needed. | |
1da177e4 | 631 | */ |
cc371e66 AK |
632 | static int raid10_mergeable_bvec(struct request_queue *q, |
633 | struct bvec_merge_data *bvm, | |
634 | struct bio_vec *biovec) | |
1da177e4 | 635 | { |
fd01b88c | 636 | struct mddev *mddev = q->queuedata; |
050b6615 | 637 | struct r10conf *conf = mddev->private; |
cc371e66 | 638 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); |
1da177e4 | 639 | int max; |
3ea7daa5 | 640 | unsigned int chunk_sectors; |
cc371e66 | 641 | unsigned int bio_sectors = bvm->bi_size >> 9; |
5cf00fcd | 642 | struct geom *geo = &conf->geo; |
1da177e4 | 643 | |
3ea7daa5 | 644 | chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1; |
f8c9e74f N |
645 | if (conf->reshape_progress != MaxSector && |
646 | ((sector >= conf->reshape_progress) != | |
647 | conf->mddev->reshape_backwards)) | |
648 | geo = &conf->prev; | |
649 | ||
5cf00fcd | 650 | if (geo->near_copies < geo->raid_disks) { |
050b6615 N |
651 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) |
652 | + bio_sectors)) << 9; | |
653 | if (max < 0) | |
654 | /* bio_add cannot handle a negative return */ | |
655 | max = 0; | |
656 | if (max <= biovec->bv_len && bio_sectors == 0) | |
657 | return biovec->bv_len; | |
658 | } else | |
659 | max = biovec->bv_len; | |
660 | ||
661 | if (mddev->merge_check_needed) { | |
e0ee7785 N |
662 | struct { |
663 | struct r10bio r10_bio; | |
664 | struct r10dev devs[conf->copies]; | |
665 | } on_stack; | |
666 | struct r10bio *r10_bio = &on_stack.r10_bio; | |
050b6615 | 667 | int s; |
f8c9e74f N |
668 | if (conf->reshape_progress != MaxSector) { |
669 | /* Cannot give any guidance during reshape */ | |
670 | if (max <= biovec->bv_len && bio_sectors == 0) | |
671 | return biovec->bv_len; | |
672 | return 0; | |
673 | } | |
e0ee7785 N |
674 | r10_bio->sector = sector; |
675 | raid10_find_phys(conf, r10_bio); | |
050b6615 N |
676 | rcu_read_lock(); |
677 | for (s = 0; s < conf->copies; s++) { | |
e0ee7785 | 678 | int disk = r10_bio->devs[s].devnum; |
050b6615 N |
679 | struct md_rdev *rdev = rcu_dereference( |
680 | conf->mirrors[disk].rdev); | |
681 | if (rdev && !test_bit(Faulty, &rdev->flags)) { | |
682 | struct request_queue *q = | |
683 | bdev_get_queue(rdev->bdev); | |
684 | if (q->merge_bvec_fn) { | |
e0ee7785 | 685 | bvm->bi_sector = r10_bio->devs[s].addr |
050b6615 N |
686 | + rdev->data_offset; |
687 | bvm->bi_bdev = rdev->bdev; | |
688 | max = min(max, q->merge_bvec_fn( | |
689 | q, bvm, biovec)); | |
690 | } | |
691 | } | |
692 | rdev = rcu_dereference(conf->mirrors[disk].replacement); | |
693 | if (rdev && !test_bit(Faulty, &rdev->flags)) { | |
694 | struct request_queue *q = | |
695 | bdev_get_queue(rdev->bdev); | |
696 | if (q->merge_bvec_fn) { | |
e0ee7785 | 697 | bvm->bi_sector = r10_bio->devs[s].addr |
050b6615 N |
698 | + rdev->data_offset; |
699 | bvm->bi_bdev = rdev->bdev; | |
700 | max = min(max, q->merge_bvec_fn( | |
701 | q, bvm, biovec)); | |
702 | } | |
703 | } | |
704 | } | |
705 | rcu_read_unlock(); | |
706 | } | |
707 | return max; | |
1da177e4 LT |
708 | } |
709 | ||
710 | /* | |
711 | * This routine returns the disk from which the requested read should | |
712 | * be done. There is a per-array 'next expected sequential IO' sector | |
713 | * number - if this matches on the next IO then we use the last disk. | |
714 | * There is also a per-disk 'last know head position' sector that is | |
715 | * maintained from IRQ contexts, both the normal and the resync IO | |
716 | * completion handlers update this position correctly. If there is no | |
717 | * perfect sequential match then we pick the disk whose head is closest. | |
718 | * | |
719 | * If there are 2 mirrors in the same 2 devices, performance degrades | |
720 | * because position is mirror, not device based. | |
721 | * | |
722 | * The rdev for the device selected will have nr_pending incremented. | |
723 | */ | |
724 | ||
725 | /* | |
726 | * FIXME: possibly should rethink readbalancing and do it differently | |
727 | * depending on near_copies / far_copies geometry. | |
728 | */ | |
96c3fd1f N |
729 | static struct md_rdev *read_balance(struct r10conf *conf, |
730 | struct r10bio *r10_bio, | |
731 | int *max_sectors) | |
1da177e4 | 732 | { |
af3a2cd6 | 733 | const sector_t this_sector = r10_bio->sector; |
56d99121 | 734 | int disk, slot; |
856e08e2 N |
735 | int sectors = r10_bio->sectors; |
736 | int best_good_sectors; | |
56d99121 | 737 | sector_t new_distance, best_dist; |
3bbae04b | 738 | struct md_rdev *best_rdev, *rdev = NULL; |
56d99121 N |
739 | int do_balance; |
740 | int best_slot; | |
5cf00fcd | 741 | struct geom *geo = &conf->geo; |
1da177e4 LT |
742 | |
743 | raid10_find_phys(conf, r10_bio); | |
744 | rcu_read_lock(); | |
56d99121 | 745 | retry: |
856e08e2 | 746 | sectors = r10_bio->sectors; |
56d99121 | 747 | best_slot = -1; |
abbf098e | 748 | best_rdev = NULL; |
56d99121 | 749 | best_dist = MaxSector; |
856e08e2 | 750 | best_good_sectors = 0; |
56d99121 | 751 | do_balance = 1; |
1da177e4 LT |
752 | /* |
753 | * Check if we can balance. We can balance on the whole | |
6cce3b23 N |
754 | * device if no resync is going on (recovery is ok), or below |
755 | * the resync window. We take the first readable disk when | |
756 | * above the resync window. | |
1da177e4 LT |
757 | */ |
758 | if (conf->mddev->recovery_cp < MaxSector | |
56d99121 N |
759 | && (this_sector + sectors >= conf->next_resync)) |
760 | do_balance = 0; | |
1da177e4 | 761 | |
56d99121 | 762 | for (slot = 0; slot < conf->copies ; slot++) { |
856e08e2 N |
763 | sector_t first_bad; |
764 | int bad_sectors; | |
765 | sector_t dev_sector; | |
766 | ||
56d99121 N |
767 | if (r10_bio->devs[slot].bio == IO_BLOCKED) |
768 | continue; | |
1da177e4 | 769 | disk = r10_bio->devs[slot].devnum; |
abbf098e N |
770 | rdev = rcu_dereference(conf->mirrors[disk].replacement); |
771 | if (rdev == NULL || test_bit(Faulty, &rdev->flags) || | |
050b6615 | 772 | test_bit(Unmerged, &rdev->flags) || |
abbf098e N |
773 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) |
774 | rdev = rcu_dereference(conf->mirrors[disk].rdev); | |
050b6615 N |
775 | if (rdev == NULL || |
776 | test_bit(Faulty, &rdev->flags) || | |
777 | test_bit(Unmerged, &rdev->flags)) | |
abbf098e N |
778 | continue; |
779 | if (!test_bit(In_sync, &rdev->flags) && | |
780 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) | |
56d99121 N |
781 | continue; |
782 | ||
856e08e2 N |
783 | dev_sector = r10_bio->devs[slot].addr; |
784 | if (is_badblock(rdev, dev_sector, sectors, | |
785 | &first_bad, &bad_sectors)) { | |
786 | if (best_dist < MaxSector) | |
787 | /* Already have a better slot */ | |
788 | continue; | |
789 | if (first_bad <= dev_sector) { | |
790 | /* Cannot read here. If this is the | |
791 | * 'primary' device, then we must not read | |
792 | * beyond 'bad_sectors' from another device. | |
793 | */ | |
794 | bad_sectors -= (dev_sector - first_bad); | |
795 | if (!do_balance && sectors > bad_sectors) | |
796 | sectors = bad_sectors; | |
797 | if (best_good_sectors > sectors) | |
798 | best_good_sectors = sectors; | |
799 | } else { | |
800 | sector_t good_sectors = | |
801 | first_bad - dev_sector; | |
802 | if (good_sectors > best_good_sectors) { | |
803 | best_good_sectors = good_sectors; | |
804 | best_slot = slot; | |
abbf098e | 805 | best_rdev = rdev; |
856e08e2 N |
806 | } |
807 | if (!do_balance) | |
808 | /* Must read from here */ | |
809 | break; | |
810 | } | |
811 | continue; | |
812 | } else | |
813 | best_good_sectors = sectors; | |
814 | ||
56d99121 N |
815 | if (!do_balance) |
816 | break; | |
1da177e4 | 817 | |
22dfdf52 N |
818 | /* This optimisation is debatable, and completely destroys |
819 | * sequential read speed for 'far copies' arrays. So only | |
820 | * keep it for 'near' arrays, and review those later. | |
821 | */ | |
5cf00fcd | 822 | if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending)) |
1da177e4 | 823 | break; |
8ed3a195 KS |
824 | |
825 | /* for far > 1 always use the lowest address */ | |
5cf00fcd | 826 | if (geo->far_copies > 1) |
56d99121 | 827 | new_distance = r10_bio->devs[slot].addr; |
8ed3a195 | 828 | else |
56d99121 N |
829 | new_distance = abs(r10_bio->devs[slot].addr - |
830 | conf->mirrors[disk].head_position); | |
831 | if (new_distance < best_dist) { | |
832 | best_dist = new_distance; | |
833 | best_slot = slot; | |
abbf098e | 834 | best_rdev = rdev; |
1da177e4 LT |
835 | } |
836 | } | |
abbf098e | 837 | if (slot >= conf->copies) { |
56d99121 | 838 | slot = best_slot; |
abbf098e N |
839 | rdev = best_rdev; |
840 | } | |
1da177e4 | 841 | |
56d99121 | 842 | if (slot >= 0) { |
56d99121 N |
843 | atomic_inc(&rdev->nr_pending); |
844 | if (test_bit(Faulty, &rdev->flags)) { | |
845 | /* Cannot risk returning a device that failed | |
846 | * before we inc'ed nr_pending | |
847 | */ | |
848 | rdev_dec_pending(rdev, conf->mddev); | |
849 | goto retry; | |
850 | } | |
851 | r10_bio->read_slot = slot; | |
852 | } else | |
96c3fd1f | 853 | rdev = NULL; |
1da177e4 | 854 | rcu_read_unlock(); |
856e08e2 | 855 | *max_sectors = best_good_sectors; |
1da177e4 | 856 | |
96c3fd1f | 857 | return rdev; |
1da177e4 LT |
858 | } |
859 | ||
cc4d1efd | 860 | int md_raid10_congested(struct mddev *mddev, int bits) |
0d129228 | 861 | { |
e879a879 | 862 | struct r10conf *conf = mddev->private; |
0d129228 N |
863 | int i, ret = 0; |
864 | ||
34db0cd6 N |
865 | if ((bits & (1 << BDI_async_congested)) && |
866 | conf->pending_count >= max_queued_requests) | |
867 | return 1; | |
868 | ||
0d129228 | 869 | rcu_read_lock(); |
f8c9e74f N |
870 | for (i = 0; |
871 | (i < conf->geo.raid_disks || i < conf->prev.raid_disks) | |
872 | && ret == 0; | |
873 | i++) { | |
3cb03002 | 874 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
0d129228 | 875 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
165125e1 | 876 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
0d129228 N |
877 | |
878 | ret |= bdi_congested(&q->backing_dev_info, bits); | |
879 | } | |
880 | } | |
881 | rcu_read_unlock(); | |
882 | return ret; | |
883 | } | |
cc4d1efd JB |
884 | EXPORT_SYMBOL_GPL(md_raid10_congested); |
885 | ||
886 | static int raid10_congested(void *data, int bits) | |
887 | { | |
888 | struct mddev *mddev = data; | |
889 | ||
890 | return mddev_congested(mddev, bits) || | |
891 | md_raid10_congested(mddev, bits); | |
892 | } | |
0d129228 | 893 | |
e879a879 | 894 | static void flush_pending_writes(struct r10conf *conf) |
a35e63ef N |
895 | { |
896 | /* Any writes that have been queued but are awaiting | |
897 | * bitmap updates get flushed here. | |
a35e63ef | 898 | */ |
a35e63ef N |
899 | spin_lock_irq(&conf->device_lock); |
900 | ||
901 | if (conf->pending_bio_list.head) { | |
902 | struct bio *bio; | |
903 | bio = bio_list_get(&conf->pending_bio_list); | |
34db0cd6 | 904 | conf->pending_count = 0; |
a35e63ef N |
905 | spin_unlock_irq(&conf->device_lock); |
906 | /* flush any pending bitmap writes to disk | |
907 | * before proceeding w/ I/O */ | |
908 | bitmap_unplug(conf->mddev->bitmap); | |
34db0cd6 | 909 | wake_up(&conf->wait_barrier); |
a35e63ef N |
910 | |
911 | while (bio) { /* submit pending writes */ | |
912 | struct bio *next = bio->bi_next; | |
913 | bio->bi_next = NULL; | |
532a2a3f SL |
914 | if (unlikely((bio->bi_rw & REQ_DISCARD) && |
915 | !blk_queue_discard(bdev_get_queue(bio->bi_bdev)))) | |
916 | /* Just ignore it */ | |
917 | bio_endio(bio, 0); | |
918 | else | |
919 | generic_make_request(bio); | |
a35e63ef N |
920 | bio = next; |
921 | } | |
a35e63ef N |
922 | } else |
923 | spin_unlock_irq(&conf->device_lock); | |
a35e63ef | 924 | } |
7eaceacc | 925 | |
0a27ec96 N |
926 | /* Barriers.... |
927 | * Sometimes we need to suspend IO while we do something else, | |
928 | * either some resync/recovery, or reconfigure the array. | |
929 | * To do this we raise a 'barrier'. | |
930 | * The 'barrier' is a counter that can be raised multiple times | |
931 | * to count how many activities are happening which preclude | |
932 | * normal IO. | |
933 | * We can only raise the barrier if there is no pending IO. | |
934 | * i.e. if nr_pending == 0. | |
935 | * We choose only to raise the barrier if no-one is waiting for the | |
936 | * barrier to go down. This means that as soon as an IO request | |
937 | * is ready, no other operations which require a barrier will start | |
938 | * until the IO request has had a chance. | |
939 | * | |
940 | * So: regular IO calls 'wait_barrier'. When that returns there | |
941 | * is no backgroup IO happening, It must arrange to call | |
942 | * allow_barrier when it has finished its IO. | |
943 | * backgroup IO calls must call raise_barrier. Once that returns | |
944 | * there is no normal IO happeing. It must arrange to call | |
945 | * lower_barrier when the particular background IO completes. | |
1da177e4 | 946 | */ |
1da177e4 | 947 | |
e879a879 | 948 | static void raise_barrier(struct r10conf *conf, int force) |
1da177e4 | 949 | { |
6cce3b23 | 950 | BUG_ON(force && !conf->barrier); |
1da177e4 | 951 | spin_lock_irq(&conf->resync_lock); |
0a27ec96 | 952 | |
6cce3b23 N |
953 | /* Wait until no block IO is waiting (unless 'force') */ |
954 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, | |
eed8c02e | 955 | conf->resync_lock); |
0a27ec96 N |
956 | |
957 | /* block any new IO from starting */ | |
958 | conf->barrier++; | |
959 | ||
c3b328ac | 960 | /* Now wait for all pending IO to complete */ |
0a27ec96 N |
961 | wait_event_lock_irq(conf->wait_barrier, |
962 | !conf->nr_pending && conf->barrier < RESYNC_DEPTH, | |
eed8c02e | 963 | conf->resync_lock); |
0a27ec96 N |
964 | |
965 | spin_unlock_irq(&conf->resync_lock); | |
966 | } | |
967 | ||
e879a879 | 968 | static void lower_barrier(struct r10conf *conf) |
0a27ec96 N |
969 | { |
970 | unsigned long flags; | |
971 | spin_lock_irqsave(&conf->resync_lock, flags); | |
972 | conf->barrier--; | |
973 | spin_unlock_irqrestore(&conf->resync_lock, flags); | |
974 | wake_up(&conf->wait_barrier); | |
975 | } | |
976 | ||
e879a879 | 977 | static void wait_barrier(struct r10conf *conf) |
0a27ec96 N |
978 | { |
979 | spin_lock_irq(&conf->resync_lock); | |
980 | if (conf->barrier) { | |
981 | conf->nr_waiting++; | |
d6b42dcb N |
982 | /* Wait for the barrier to drop. |
983 | * However if there are already pending | |
984 | * requests (preventing the barrier from | |
985 | * rising completely), and the | |
986 | * pre-process bio queue isn't empty, | |
987 | * then don't wait, as we need to empty | |
988 | * that queue to get the nr_pending | |
989 | * count down. | |
990 | */ | |
991 | wait_event_lock_irq(conf->wait_barrier, | |
992 | !conf->barrier || | |
993 | (conf->nr_pending && | |
994 | current->bio_list && | |
995 | !bio_list_empty(current->bio_list)), | |
eed8c02e | 996 | conf->resync_lock); |
0a27ec96 | 997 | conf->nr_waiting--; |
1da177e4 | 998 | } |
0a27ec96 | 999 | conf->nr_pending++; |
1da177e4 LT |
1000 | spin_unlock_irq(&conf->resync_lock); |
1001 | } | |
1002 | ||
e879a879 | 1003 | static void allow_barrier(struct r10conf *conf) |
0a27ec96 N |
1004 | { |
1005 | unsigned long flags; | |
1006 | spin_lock_irqsave(&conf->resync_lock, flags); | |
1007 | conf->nr_pending--; | |
1008 | spin_unlock_irqrestore(&conf->resync_lock, flags); | |
1009 | wake_up(&conf->wait_barrier); | |
1010 | } | |
1011 | ||
e879a879 | 1012 | static void freeze_array(struct r10conf *conf) |
4443ae10 N |
1013 | { |
1014 | /* stop syncio and normal IO and wait for everything to | |
f188593e | 1015 | * go quiet. |
4443ae10 | 1016 | * We increment barrier and nr_waiting, and then |
1c830532 N |
1017 | * wait until nr_pending match nr_queued+1 |
1018 | * This is called in the context of one normal IO request | |
1019 | * that has failed. Thus any sync request that might be pending | |
1020 | * will be blocked by nr_pending, and we need to wait for | |
1021 | * pending IO requests to complete or be queued for re-try. | |
1022 | * Thus the number queued (nr_queued) plus this request (1) | |
1023 | * must match the number of pending IOs (nr_pending) before | |
1024 | * we continue. | |
4443ae10 N |
1025 | */ |
1026 | spin_lock_irq(&conf->resync_lock); | |
1027 | conf->barrier++; | |
1028 | conf->nr_waiting++; | |
eed8c02e LC |
1029 | wait_event_lock_irq_cmd(conf->wait_barrier, |
1030 | conf->nr_pending == conf->nr_queued+1, | |
1031 | conf->resync_lock, | |
1032 | flush_pending_writes(conf)); | |
c3b328ac | 1033 | |
4443ae10 N |
1034 | spin_unlock_irq(&conf->resync_lock); |
1035 | } | |
1036 | ||
e879a879 | 1037 | static void unfreeze_array(struct r10conf *conf) |
4443ae10 N |
1038 | { |
1039 | /* reverse the effect of the freeze */ | |
1040 | spin_lock_irq(&conf->resync_lock); | |
1041 | conf->barrier--; | |
1042 | conf->nr_waiting--; | |
1043 | wake_up(&conf->wait_barrier); | |
1044 | spin_unlock_irq(&conf->resync_lock); | |
1045 | } | |
1046 | ||
f8c9e74f N |
1047 | static sector_t choose_data_offset(struct r10bio *r10_bio, |
1048 | struct md_rdev *rdev) | |
1049 | { | |
1050 | if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) || | |
1051 | test_bit(R10BIO_Previous, &r10_bio->state)) | |
1052 | return rdev->data_offset; | |
1053 | else | |
1054 | return rdev->new_data_offset; | |
1055 | } | |
1056 | ||
57c67df4 N |
1057 | struct raid10_plug_cb { |
1058 | struct blk_plug_cb cb; | |
1059 | struct bio_list pending; | |
1060 | int pending_cnt; | |
1061 | }; | |
1062 | ||
1063 | static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule) | |
1064 | { | |
1065 | struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb, | |
1066 | cb); | |
1067 | struct mddev *mddev = plug->cb.data; | |
1068 | struct r10conf *conf = mddev->private; | |
1069 | struct bio *bio; | |
1070 | ||
1071 | if (from_schedule) { | |
1072 | spin_lock_irq(&conf->device_lock); | |
1073 | bio_list_merge(&conf->pending_bio_list, &plug->pending); | |
1074 | conf->pending_count += plug->pending_cnt; | |
1075 | spin_unlock_irq(&conf->device_lock); | |
1076 | md_wakeup_thread(mddev->thread); | |
1077 | kfree(plug); | |
1078 | return; | |
1079 | } | |
1080 | ||
1081 | /* we aren't scheduling, so we can do the write-out directly. */ | |
1082 | bio = bio_list_get(&plug->pending); | |
1083 | bitmap_unplug(mddev->bitmap); | |
1084 | wake_up(&conf->wait_barrier); | |
1085 | ||
1086 | while (bio) { /* submit pending writes */ | |
1087 | struct bio *next = bio->bi_next; | |
1088 | bio->bi_next = NULL; | |
1089 | generic_make_request(bio); | |
1090 | bio = next; | |
1091 | } | |
1092 | kfree(plug); | |
1093 | } | |
1094 | ||
b4fdcb02 | 1095 | static void make_request(struct mddev *mddev, struct bio * bio) |
1da177e4 | 1096 | { |
e879a879 | 1097 | struct r10conf *conf = mddev->private; |
9f2c9d12 | 1098 | struct r10bio *r10_bio; |
1da177e4 LT |
1099 | struct bio *read_bio; |
1100 | int i; | |
f8c9e74f | 1101 | sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask); |
5cf00fcd | 1102 | int chunk_sects = chunk_mask + 1; |
a362357b | 1103 | const int rw = bio_data_dir(bio); |
2c7d46ec | 1104 | const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); |
e9c7469b | 1105 | const unsigned long do_fua = (bio->bi_rw & REQ_FUA); |
532a2a3f SL |
1106 | const unsigned long do_discard = (bio->bi_rw |
1107 | & (REQ_DISCARD | REQ_SECURE)); | |
6cce3b23 | 1108 | unsigned long flags; |
3cb03002 | 1109 | struct md_rdev *blocked_rdev; |
57c67df4 N |
1110 | struct blk_plug_cb *cb; |
1111 | struct raid10_plug_cb *plug = NULL; | |
d4432c23 N |
1112 | int sectors_handled; |
1113 | int max_sectors; | |
3ea7daa5 | 1114 | int sectors; |
1da177e4 | 1115 | |
e9c7469b TH |
1116 | if (unlikely(bio->bi_rw & REQ_FLUSH)) { |
1117 | md_flush_request(mddev, bio); | |
5a7bbad2 | 1118 | return; |
e5dcdd80 N |
1119 | } |
1120 | ||
1da177e4 LT |
1121 | /* If this request crosses a chunk boundary, we need to |
1122 | * split it. This will only happen for 1 PAGE (or less) requests. | |
1123 | */ | |
5cf00fcd N |
1124 | if (unlikely((bio->bi_sector & chunk_mask) + (bio->bi_size >> 9) |
1125 | > chunk_sects | |
f8c9e74f N |
1126 | && (conf->geo.near_copies < conf->geo.raid_disks |
1127 | || conf->prev.near_copies < conf->prev.raid_disks))) { | |
1da177e4 LT |
1128 | struct bio_pair *bp; |
1129 | /* Sanity check -- queue functions should prevent this happening */ | |
532a2a3f | 1130 | if ((bio->bi_vcnt != 1 && bio->bi_vcnt != 0) || |
1da177e4 LT |
1131 | bio->bi_idx != 0) |
1132 | goto bad_map; | |
1133 | /* This is a one page bio that upper layers | |
1134 | * refuse to split for us, so we need to split it. | |
1135 | */ | |
6feef531 | 1136 | bp = bio_split(bio, |
1da177e4 | 1137 | chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); |
51e9ac77 N |
1138 | |
1139 | /* Each of these 'make_request' calls will call 'wait_barrier'. | |
1140 | * If the first succeeds but the second blocks due to the resync | |
1141 | * thread raising the barrier, we will deadlock because the | |
1142 | * IO to the underlying device will be queued in generic_make_request | |
1143 | * and will never complete, so will never reduce nr_pending. | |
1144 | * So increment nr_waiting here so no new raise_barriers will | |
1145 | * succeed, and so the second wait_barrier cannot block. | |
1146 | */ | |
1147 | spin_lock_irq(&conf->resync_lock); | |
1148 | conf->nr_waiting++; | |
1149 | spin_unlock_irq(&conf->resync_lock); | |
1150 | ||
5a7bbad2 CH |
1151 | make_request(mddev, &bp->bio1); |
1152 | make_request(mddev, &bp->bio2); | |
1da177e4 | 1153 | |
51e9ac77 N |
1154 | spin_lock_irq(&conf->resync_lock); |
1155 | conf->nr_waiting--; | |
1156 | wake_up(&conf->wait_barrier); | |
1157 | spin_unlock_irq(&conf->resync_lock); | |
1158 | ||
1da177e4 | 1159 | bio_pair_release(bp); |
5a7bbad2 | 1160 | return; |
1da177e4 | 1161 | bad_map: |
128595ed N |
1162 | printk("md/raid10:%s: make_request bug: can't convert block across chunks" |
1163 | " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2, | |
1da177e4 LT |
1164 | (unsigned long long)bio->bi_sector, bio->bi_size >> 10); |
1165 | ||
6712ecf8 | 1166 | bio_io_error(bio); |
5a7bbad2 | 1167 | return; |
1da177e4 LT |
1168 | } |
1169 | ||
3d310eb7 | 1170 | md_write_start(mddev, bio); |
06d91a5f | 1171 | |
1da177e4 LT |
1172 | /* |
1173 | * Register the new request and wait if the reconstruction | |
1174 | * thread has put up a bar for new requests. | |
1175 | * Continue immediately if no resync is active currently. | |
1176 | */ | |
0a27ec96 | 1177 | wait_barrier(conf); |
1da177e4 | 1178 | |
3ea7daa5 N |
1179 | sectors = bio->bi_size >> 9; |
1180 | while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && | |
1181 | bio->bi_sector < conf->reshape_progress && | |
1182 | bio->bi_sector + sectors > conf->reshape_progress) { | |
1183 | /* IO spans the reshape position. Need to wait for | |
1184 | * reshape to pass | |
1185 | */ | |
1186 | allow_barrier(conf); | |
1187 | wait_event(conf->wait_barrier, | |
1188 | conf->reshape_progress <= bio->bi_sector || | |
1189 | conf->reshape_progress >= bio->bi_sector + sectors); | |
1190 | wait_barrier(conf); | |
1191 | } | |
1192 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && | |
1193 | bio_data_dir(bio) == WRITE && | |
1194 | (mddev->reshape_backwards | |
1195 | ? (bio->bi_sector < conf->reshape_safe && | |
1196 | bio->bi_sector + sectors > conf->reshape_progress) | |
1197 | : (bio->bi_sector + sectors > conf->reshape_safe && | |
1198 | bio->bi_sector < conf->reshape_progress))) { | |
1199 | /* Need to update reshape_position in metadata */ | |
1200 | mddev->reshape_position = conf->reshape_progress; | |
1201 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
1202 | set_bit(MD_CHANGE_PENDING, &mddev->flags); | |
1203 | md_wakeup_thread(mddev->thread); | |
1204 | wait_event(mddev->sb_wait, | |
1205 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); | |
1206 | ||
1207 | conf->reshape_safe = mddev->reshape_position; | |
1208 | } | |
1209 | ||
1da177e4 LT |
1210 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
1211 | ||
1212 | r10_bio->master_bio = bio; | |
3ea7daa5 | 1213 | r10_bio->sectors = sectors; |
1da177e4 LT |
1214 | |
1215 | r10_bio->mddev = mddev; | |
1216 | r10_bio->sector = bio->bi_sector; | |
6cce3b23 | 1217 | r10_bio->state = 0; |
1da177e4 | 1218 | |
856e08e2 N |
1219 | /* We might need to issue multiple reads to different |
1220 | * devices if there are bad blocks around, so we keep | |
1221 | * track of the number of reads in bio->bi_phys_segments. | |
1222 | * If this is 0, there is only one r10_bio and no locking | |
1223 | * will be needed when the request completes. If it is | |
1224 | * non-zero, then it is the number of not-completed requests. | |
1225 | */ | |
1226 | bio->bi_phys_segments = 0; | |
1227 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); | |
1228 | ||
a362357b | 1229 | if (rw == READ) { |
1da177e4 LT |
1230 | /* |
1231 | * read balancing logic: | |
1232 | */ | |
96c3fd1f | 1233 | struct md_rdev *rdev; |
856e08e2 N |
1234 | int slot; |
1235 | ||
1236 | read_again: | |
96c3fd1f N |
1237 | rdev = read_balance(conf, r10_bio, &max_sectors); |
1238 | if (!rdev) { | |
1da177e4 | 1239 | raid_end_bio_io(r10_bio); |
5a7bbad2 | 1240 | return; |
1da177e4 | 1241 | } |
96c3fd1f | 1242 | slot = r10_bio->read_slot; |
1da177e4 | 1243 | |
a167f663 | 1244 | read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
856e08e2 N |
1245 | md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector, |
1246 | max_sectors); | |
1da177e4 LT |
1247 | |
1248 | r10_bio->devs[slot].bio = read_bio; | |
abbf098e | 1249 | r10_bio->devs[slot].rdev = rdev; |
1da177e4 LT |
1250 | |
1251 | read_bio->bi_sector = r10_bio->devs[slot].addr + | |
f8c9e74f | 1252 | choose_data_offset(r10_bio, rdev); |
96c3fd1f | 1253 | read_bio->bi_bdev = rdev->bdev; |
1da177e4 | 1254 | read_bio->bi_end_io = raid10_end_read_request; |
7b6d91da | 1255 | read_bio->bi_rw = READ | do_sync; |
1da177e4 LT |
1256 | read_bio->bi_private = r10_bio; |
1257 | ||
856e08e2 N |
1258 | if (max_sectors < r10_bio->sectors) { |
1259 | /* Could not read all from this device, so we will | |
1260 | * need another r10_bio. | |
1261 | */ | |
856e08e2 N |
1262 | sectors_handled = (r10_bio->sectors + max_sectors |
1263 | - bio->bi_sector); | |
1264 | r10_bio->sectors = max_sectors; | |
1265 | spin_lock_irq(&conf->device_lock); | |
1266 | if (bio->bi_phys_segments == 0) | |
1267 | bio->bi_phys_segments = 2; | |
1268 | else | |
1269 | bio->bi_phys_segments++; | |
1270 | spin_unlock(&conf->device_lock); | |
1271 | /* Cannot call generic_make_request directly | |
1272 | * as that will be queued in __generic_make_request | |
1273 | * and subsequent mempool_alloc might block | |
1274 | * waiting for it. so hand bio over to raid10d. | |
1275 | */ | |
1276 | reschedule_retry(r10_bio); | |
1277 | ||
1278 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); | |
1279 | ||
1280 | r10_bio->master_bio = bio; | |
1281 | r10_bio->sectors = ((bio->bi_size >> 9) | |
1282 | - sectors_handled); | |
1283 | r10_bio->state = 0; | |
1284 | r10_bio->mddev = mddev; | |
1285 | r10_bio->sector = bio->bi_sector + sectors_handled; | |
1286 | goto read_again; | |
1287 | } else | |
1288 | generic_make_request(read_bio); | |
5a7bbad2 | 1289 | return; |
1da177e4 LT |
1290 | } |
1291 | ||
1292 | /* | |
1293 | * WRITE: | |
1294 | */ | |
34db0cd6 N |
1295 | if (conf->pending_count >= max_queued_requests) { |
1296 | md_wakeup_thread(mddev->thread); | |
1297 | wait_event(conf->wait_barrier, | |
1298 | conf->pending_count < max_queued_requests); | |
1299 | } | |
6bfe0b49 | 1300 | /* first select target devices under rcu_lock and |
1da177e4 LT |
1301 | * inc refcount on their rdev. Record them by setting |
1302 | * bios[x] to bio | |
d4432c23 N |
1303 | * If there are known/acknowledged bad blocks on any device |
1304 | * on which we have seen a write error, we want to avoid | |
1305 | * writing to those blocks. This potentially requires several | |
1306 | * writes to write around the bad blocks. Each set of writes | |
1307 | * gets its own r10_bio with a set of bios attached. The number | |
1308 | * of r10_bios is recored in bio->bi_phys_segments just as with | |
1309 | * the read case. | |
1da177e4 | 1310 | */ |
c3b328ac | 1311 | |
69335ef3 | 1312 | r10_bio->read_slot = -1; /* make sure repl_bio gets freed */ |
1da177e4 | 1313 | raid10_find_phys(conf, r10_bio); |
d4432c23 | 1314 | retry_write: |
cb6969e8 | 1315 | blocked_rdev = NULL; |
1da177e4 | 1316 | rcu_read_lock(); |
d4432c23 N |
1317 | max_sectors = r10_bio->sectors; |
1318 | ||
1da177e4 LT |
1319 | for (i = 0; i < conf->copies; i++) { |
1320 | int d = r10_bio->devs[i].devnum; | |
3cb03002 | 1321 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
475b0321 N |
1322 | struct md_rdev *rrdev = rcu_dereference( |
1323 | conf->mirrors[d].replacement); | |
4ca40c2c N |
1324 | if (rdev == rrdev) |
1325 | rrdev = NULL; | |
6bfe0b49 DW |
1326 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
1327 | atomic_inc(&rdev->nr_pending); | |
1328 | blocked_rdev = rdev; | |
1329 | break; | |
1330 | } | |
475b0321 N |
1331 | if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) { |
1332 | atomic_inc(&rrdev->nr_pending); | |
1333 | blocked_rdev = rrdev; | |
1334 | break; | |
1335 | } | |
050b6615 N |
1336 | if (rrdev && (test_bit(Faulty, &rrdev->flags) |
1337 | || test_bit(Unmerged, &rrdev->flags))) | |
475b0321 N |
1338 | rrdev = NULL; |
1339 | ||
d4432c23 | 1340 | r10_bio->devs[i].bio = NULL; |
475b0321 | 1341 | r10_bio->devs[i].repl_bio = NULL; |
050b6615 N |
1342 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
1343 | test_bit(Unmerged, &rdev->flags)) { | |
6cce3b23 | 1344 | set_bit(R10BIO_Degraded, &r10_bio->state); |
d4432c23 N |
1345 | continue; |
1346 | } | |
1347 | if (test_bit(WriteErrorSeen, &rdev->flags)) { | |
1348 | sector_t first_bad; | |
1349 | sector_t dev_sector = r10_bio->devs[i].addr; | |
1350 | int bad_sectors; | |
1351 | int is_bad; | |
1352 | ||
1353 | is_bad = is_badblock(rdev, dev_sector, | |
1354 | max_sectors, | |
1355 | &first_bad, &bad_sectors); | |
1356 | if (is_bad < 0) { | |
1357 | /* Mustn't write here until the bad block | |
1358 | * is acknowledged | |
1359 | */ | |
1360 | atomic_inc(&rdev->nr_pending); | |
1361 | set_bit(BlockedBadBlocks, &rdev->flags); | |
1362 | blocked_rdev = rdev; | |
1363 | break; | |
1364 | } | |
1365 | if (is_bad && first_bad <= dev_sector) { | |
1366 | /* Cannot write here at all */ | |
1367 | bad_sectors -= (dev_sector - first_bad); | |
1368 | if (bad_sectors < max_sectors) | |
1369 | /* Mustn't write more than bad_sectors | |
1370 | * to other devices yet | |
1371 | */ | |
1372 | max_sectors = bad_sectors; | |
1373 | /* We don't set R10BIO_Degraded as that | |
1374 | * only applies if the disk is missing, | |
1375 | * so it might be re-added, and we want to | |
1376 | * know to recover this chunk. | |
1377 | * In this case the device is here, and the | |
1378 | * fact that this chunk is not in-sync is | |
1379 | * recorded in the bad block log. | |
1380 | */ | |
1381 | continue; | |
1382 | } | |
1383 | if (is_bad) { | |
1384 | int good_sectors = first_bad - dev_sector; | |
1385 | if (good_sectors < max_sectors) | |
1386 | max_sectors = good_sectors; | |
1387 | } | |
6cce3b23 | 1388 | } |
d4432c23 N |
1389 | r10_bio->devs[i].bio = bio; |
1390 | atomic_inc(&rdev->nr_pending); | |
475b0321 N |
1391 | if (rrdev) { |
1392 | r10_bio->devs[i].repl_bio = bio; | |
1393 | atomic_inc(&rrdev->nr_pending); | |
1394 | } | |
1da177e4 LT |
1395 | } |
1396 | rcu_read_unlock(); | |
1397 | ||
6bfe0b49 DW |
1398 | if (unlikely(blocked_rdev)) { |
1399 | /* Have to wait for this device to get unblocked, then retry */ | |
1400 | int j; | |
1401 | int d; | |
1402 | ||
475b0321 | 1403 | for (j = 0; j < i; j++) { |
6bfe0b49 DW |
1404 | if (r10_bio->devs[j].bio) { |
1405 | d = r10_bio->devs[j].devnum; | |
1406 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); | |
1407 | } | |
475b0321 | 1408 | if (r10_bio->devs[j].repl_bio) { |
4ca40c2c | 1409 | struct md_rdev *rdev; |
475b0321 | 1410 | d = r10_bio->devs[j].devnum; |
4ca40c2c N |
1411 | rdev = conf->mirrors[d].replacement; |
1412 | if (!rdev) { | |
1413 | /* Race with remove_disk */ | |
1414 | smp_mb(); | |
1415 | rdev = conf->mirrors[d].rdev; | |
1416 | } | |
1417 | rdev_dec_pending(rdev, mddev); | |
475b0321 N |
1418 | } |
1419 | } | |
6bfe0b49 DW |
1420 | allow_barrier(conf); |
1421 | md_wait_for_blocked_rdev(blocked_rdev, mddev); | |
1422 | wait_barrier(conf); | |
1423 | goto retry_write; | |
1424 | } | |
1425 | ||
d4432c23 N |
1426 | if (max_sectors < r10_bio->sectors) { |
1427 | /* We are splitting this into multiple parts, so | |
1428 | * we need to prepare for allocating another r10_bio. | |
1429 | */ | |
1430 | r10_bio->sectors = max_sectors; | |
1431 | spin_lock_irq(&conf->device_lock); | |
1432 | if (bio->bi_phys_segments == 0) | |
1433 | bio->bi_phys_segments = 2; | |
1434 | else | |
1435 | bio->bi_phys_segments++; | |
1436 | spin_unlock_irq(&conf->device_lock); | |
1437 | } | |
1438 | sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector; | |
1439 | ||
4e78064f | 1440 | atomic_set(&r10_bio->remaining, 1); |
d4432c23 | 1441 | bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0); |
06d91a5f | 1442 | |
1da177e4 LT |
1443 | for (i = 0; i < conf->copies; i++) { |
1444 | struct bio *mbio; | |
1445 | int d = r10_bio->devs[i].devnum; | |
1446 | if (!r10_bio->devs[i].bio) | |
1447 | continue; | |
1448 | ||
a167f663 | 1449 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
d4432c23 N |
1450 | md_trim_bio(mbio, r10_bio->sector - bio->bi_sector, |
1451 | max_sectors); | |
1da177e4 LT |
1452 | r10_bio->devs[i].bio = mbio; |
1453 | ||
d4432c23 | 1454 | mbio->bi_sector = (r10_bio->devs[i].addr+ |
f8c9e74f N |
1455 | choose_data_offset(r10_bio, |
1456 | conf->mirrors[d].rdev)); | |
1da177e4 LT |
1457 | mbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
1458 | mbio->bi_end_io = raid10_end_write_request; | |
532a2a3f | 1459 | mbio->bi_rw = WRITE | do_sync | do_fua | do_discard; |
1da177e4 LT |
1460 | mbio->bi_private = r10_bio; |
1461 | ||
1462 | atomic_inc(&r10_bio->remaining); | |
57c67df4 N |
1463 | |
1464 | cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug)); | |
1465 | if (cb) | |
1466 | plug = container_of(cb, struct raid10_plug_cb, cb); | |
1467 | else | |
1468 | plug = NULL; | |
4e78064f | 1469 | spin_lock_irqsave(&conf->device_lock, flags); |
57c67df4 N |
1470 | if (plug) { |
1471 | bio_list_add(&plug->pending, mbio); | |
1472 | plug->pending_cnt++; | |
1473 | } else { | |
1474 | bio_list_add(&conf->pending_bio_list, mbio); | |
1475 | conf->pending_count++; | |
1476 | } | |
4e78064f | 1477 | spin_unlock_irqrestore(&conf->device_lock, flags); |
57c67df4 | 1478 | if (!plug) |
b357f04a | 1479 | md_wakeup_thread(mddev->thread); |
475b0321 N |
1480 | |
1481 | if (!r10_bio->devs[i].repl_bio) | |
1482 | continue; | |
1483 | ||
1484 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); | |
1485 | md_trim_bio(mbio, r10_bio->sector - bio->bi_sector, | |
1486 | max_sectors); | |
1487 | r10_bio->devs[i].repl_bio = mbio; | |
1488 | ||
4ca40c2c N |
1489 | /* We are actively writing to the original device |
1490 | * so it cannot disappear, so the replacement cannot | |
1491 | * become NULL here | |
1492 | */ | |
f8c9e74f N |
1493 | mbio->bi_sector = (r10_bio->devs[i].addr + |
1494 | choose_data_offset( | |
1495 | r10_bio, | |
1496 | conf->mirrors[d].replacement)); | |
475b0321 N |
1497 | mbio->bi_bdev = conf->mirrors[d].replacement->bdev; |
1498 | mbio->bi_end_io = raid10_end_write_request; | |
532a2a3f | 1499 | mbio->bi_rw = WRITE | do_sync | do_fua | do_discard; |
475b0321 N |
1500 | mbio->bi_private = r10_bio; |
1501 | ||
1502 | atomic_inc(&r10_bio->remaining); | |
1503 | spin_lock_irqsave(&conf->device_lock, flags); | |
1504 | bio_list_add(&conf->pending_bio_list, mbio); | |
1505 | conf->pending_count++; | |
1506 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
b357f04a N |
1507 | if (!mddev_check_plugged(mddev)) |
1508 | md_wakeup_thread(mddev->thread); | |
1da177e4 LT |
1509 | } |
1510 | ||
079fa166 N |
1511 | /* Don't remove the bias on 'remaining' (one_write_done) until |
1512 | * after checking if we need to go around again. | |
1513 | */ | |
a35e63ef | 1514 | |
d4432c23 | 1515 | if (sectors_handled < (bio->bi_size >> 9)) { |
079fa166 | 1516 | one_write_done(r10_bio); |
5e570289 | 1517 | /* We need another r10_bio. It has already been counted |
d4432c23 N |
1518 | * in bio->bi_phys_segments. |
1519 | */ | |
1520 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); | |
1521 | ||
1522 | r10_bio->master_bio = bio; | |
1523 | r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled; | |
1524 | ||
1525 | r10_bio->mddev = mddev; | |
1526 | r10_bio->sector = bio->bi_sector + sectors_handled; | |
1527 | r10_bio->state = 0; | |
1528 | goto retry_write; | |
1529 | } | |
079fa166 N |
1530 | one_write_done(r10_bio); |
1531 | ||
1532 | /* In case raid10d snuck in to freeze_array */ | |
1533 | wake_up(&conf->wait_barrier); | |
1da177e4 LT |
1534 | } |
1535 | ||
fd01b88c | 1536 | static void status(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 1537 | { |
e879a879 | 1538 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
1539 | int i; |
1540 | ||
5cf00fcd | 1541 | if (conf->geo.near_copies < conf->geo.raid_disks) |
9d8f0363 | 1542 | seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); |
5cf00fcd N |
1543 | if (conf->geo.near_copies > 1) |
1544 | seq_printf(seq, " %d near-copies", conf->geo.near_copies); | |
1545 | if (conf->geo.far_copies > 1) { | |
1546 | if (conf->geo.far_offset) | |
1547 | seq_printf(seq, " %d offset-copies", conf->geo.far_copies); | |
c93983bf | 1548 | else |
5cf00fcd | 1549 | seq_printf(seq, " %d far-copies", conf->geo.far_copies); |
c93983bf | 1550 | } |
5cf00fcd N |
1551 | seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks, |
1552 | conf->geo.raid_disks - mddev->degraded); | |
1553 | for (i = 0; i < conf->geo.raid_disks; i++) | |
1da177e4 LT |
1554 | seq_printf(seq, "%s", |
1555 | conf->mirrors[i].rdev && | |
b2d444d7 | 1556 | test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_"); |
1da177e4 LT |
1557 | seq_printf(seq, "]"); |
1558 | } | |
1559 | ||
700c7213 N |
1560 | /* check if there are enough drives for |
1561 | * every block to appear on atleast one. | |
1562 | * Don't consider the device numbered 'ignore' | |
1563 | * as we might be about to remove it. | |
1564 | */ | |
f8c9e74f | 1565 | static int _enough(struct r10conf *conf, struct geom *geo, int ignore) |
700c7213 N |
1566 | { |
1567 | int first = 0; | |
1568 | ||
1569 | do { | |
1570 | int n = conf->copies; | |
1571 | int cnt = 0; | |
80b48124 | 1572 | int this = first; |
700c7213 | 1573 | while (n--) { |
80b48124 N |
1574 | if (conf->mirrors[this].rdev && |
1575 | this != ignore) | |
700c7213 | 1576 | cnt++; |
80b48124 | 1577 | this = (this+1) % geo->raid_disks; |
700c7213 N |
1578 | } |
1579 | if (cnt == 0) | |
1580 | return 0; | |
80b48124 | 1581 | first = (first + geo->near_copies) % geo->raid_disks; |
700c7213 N |
1582 | } while (first != 0); |
1583 | return 1; | |
1584 | } | |
1585 | ||
f8c9e74f N |
1586 | static int enough(struct r10conf *conf, int ignore) |
1587 | { | |
1588 | return _enough(conf, &conf->geo, ignore) && | |
1589 | _enough(conf, &conf->prev, ignore); | |
1590 | } | |
1591 | ||
fd01b88c | 1592 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1593 | { |
1594 | char b[BDEVNAME_SIZE]; | |
e879a879 | 1595 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
1596 | |
1597 | /* | |
1598 | * If it is not operational, then we have already marked it as dead | |
1599 | * else if it is the last working disks, ignore the error, let the | |
1600 | * next level up know. | |
1601 | * else mark the drive as failed | |
1602 | */ | |
b2d444d7 | 1603 | if (test_bit(In_sync, &rdev->flags) |
700c7213 | 1604 | && !enough(conf, rdev->raid_disk)) |
1da177e4 LT |
1605 | /* |
1606 | * Don't fail the drive, just return an IO error. | |
1da177e4 LT |
1607 | */ |
1608 | return; | |
c04be0aa N |
1609 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
1610 | unsigned long flags; | |
1611 | spin_lock_irqsave(&conf->device_lock, flags); | |
1da177e4 | 1612 | mddev->degraded++; |
c04be0aa | 1613 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 LT |
1614 | /* |
1615 | * if recovery is running, make sure it aborts. | |
1616 | */ | |
dfc70645 | 1617 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
1da177e4 | 1618 | } |
de393cde | 1619 | set_bit(Blocked, &rdev->flags); |
b2d444d7 | 1620 | set_bit(Faulty, &rdev->flags); |
850b2b42 | 1621 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
067032bc JP |
1622 | printk(KERN_ALERT |
1623 | "md/raid10:%s: Disk failure on %s, disabling device.\n" | |
1624 | "md/raid10:%s: Operation continuing on %d devices.\n", | |
128595ed | 1625 | mdname(mddev), bdevname(rdev->bdev, b), |
5cf00fcd | 1626 | mdname(mddev), conf->geo.raid_disks - mddev->degraded); |
1da177e4 LT |
1627 | } |
1628 | ||
e879a879 | 1629 | static void print_conf(struct r10conf *conf) |
1da177e4 LT |
1630 | { |
1631 | int i; | |
dc280d98 | 1632 | struct raid10_info *tmp; |
1da177e4 | 1633 | |
128595ed | 1634 | printk(KERN_DEBUG "RAID10 conf printout:\n"); |
1da177e4 | 1635 | if (!conf) { |
128595ed | 1636 | printk(KERN_DEBUG "(!conf)\n"); |
1da177e4 LT |
1637 | return; |
1638 | } | |
5cf00fcd N |
1639 | printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded, |
1640 | conf->geo.raid_disks); | |
1da177e4 | 1641 | |
5cf00fcd | 1642 | for (i = 0; i < conf->geo.raid_disks; i++) { |
1da177e4 LT |
1643 | char b[BDEVNAME_SIZE]; |
1644 | tmp = conf->mirrors + i; | |
1645 | if (tmp->rdev) | |
128595ed | 1646 | printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", |
b2d444d7 N |
1647 | i, !test_bit(In_sync, &tmp->rdev->flags), |
1648 | !test_bit(Faulty, &tmp->rdev->flags), | |
1da177e4 LT |
1649 | bdevname(tmp->rdev->bdev,b)); |
1650 | } | |
1651 | } | |
1652 | ||
e879a879 | 1653 | static void close_sync(struct r10conf *conf) |
1da177e4 | 1654 | { |
0a27ec96 N |
1655 | wait_barrier(conf); |
1656 | allow_barrier(conf); | |
1da177e4 LT |
1657 | |
1658 | mempool_destroy(conf->r10buf_pool); | |
1659 | conf->r10buf_pool = NULL; | |
1660 | } | |
1661 | ||
fd01b88c | 1662 | static int raid10_spare_active(struct mddev *mddev) |
1da177e4 LT |
1663 | { |
1664 | int i; | |
e879a879 | 1665 | struct r10conf *conf = mddev->private; |
dc280d98 | 1666 | struct raid10_info *tmp; |
6b965620 N |
1667 | int count = 0; |
1668 | unsigned long flags; | |
1da177e4 LT |
1669 | |
1670 | /* | |
1671 | * Find all non-in_sync disks within the RAID10 configuration | |
1672 | * and mark them in_sync | |
1673 | */ | |
5cf00fcd | 1674 | for (i = 0; i < conf->geo.raid_disks; i++) { |
1da177e4 | 1675 | tmp = conf->mirrors + i; |
4ca40c2c N |
1676 | if (tmp->replacement |
1677 | && tmp->replacement->recovery_offset == MaxSector | |
1678 | && !test_bit(Faulty, &tmp->replacement->flags) | |
1679 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { | |
1680 | /* Replacement has just become active */ | |
1681 | if (!tmp->rdev | |
1682 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) | |
1683 | count++; | |
1684 | if (tmp->rdev) { | |
1685 | /* Replaced device not technically faulty, | |
1686 | * but we need to be sure it gets removed | |
1687 | * and never re-added. | |
1688 | */ | |
1689 | set_bit(Faulty, &tmp->rdev->flags); | |
1690 | sysfs_notify_dirent_safe( | |
1691 | tmp->rdev->sysfs_state); | |
1692 | } | |
1693 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); | |
1694 | } else if (tmp->rdev | |
1695 | && !test_bit(Faulty, &tmp->rdev->flags) | |
1696 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { | |
6b965620 | 1697 | count++; |
2863b9eb | 1698 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
1da177e4 LT |
1699 | } |
1700 | } | |
6b965620 N |
1701 | spin_lock_irqsave(&conf->device_lock, flags); |
1702 | mddev->degraded -= count; | |
1703 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 LT |
1704 | |
1705 | print_conf(conf); | |
6b965620 | 1706 | return count; |
1da177e4 LT |
1707 | } |
1708 | ||
1709 | ||
fd01b88c | 1710 | static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1711 | { |
e879a879 | 1712 | struct r10conf *conf = mddev->private; |
199050ea | 1713 | int err = -EEXIST; |
1da177e4 | 1714 | int mirror; |
6c2fce2e | 1715 | int first = 0; |
5cf00fcd | 1716 | int last = conf->geo.raid_disks - 1; |
050b6615 | 1717 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
1da177e4 LT |
1718 | |
1719 | if (mddev->recovery_cp < MaxSector) | |
1720 | /* only hot-add to in-sync arrays, as recovery is | |
1721 | * very different from resync | |
1722 | */ | |
199050ea | 1723 | return -EBUSY; |
f8c9e74f | 1724 | if (rdev->saved_raid_disk < 0 && !_enough(conf, &conf->prev, -1)) |
199050ea | 1725 | return -EINVAL; |
1da177e4 | 1726 | |
a53a6c85 | 1727 | if (rdev->raid_disk >= 0) |
6c2fce2e | 1728 | first = last = rdev->raid_disk; |
1da177e4 | 1729 | |
050b6615 N |
1730 | if (q->merge_bvec_fn) { |
1731 | set_bit(Unmerged, &rdev->flags); | |
1732 | mddev->merge_check_needed = 1; | |
1733 | } | |
1734 | ||
2c4193df | 1735 | if (rdev->saved_raid_disk >= first && |
6cce3b23 N |
1736 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) |
1737 | mirror = rdev->saved_raid_disk; | |
1738 | else | |
6c2fce2e | 1739 | mirror = first; |
2bb77736 | 1740 | for ( ; mirror <= last ; mirror++) { |
dc280d98 | 1741 | struct raid10_info *p = &conf->mirrors[mirror]; |
2bb77736 N |
1742 | if (p->recovery_disabled == mddev->recovery_disabled) |
1743 | continue; | |
b7044d41 N |
1744 | if (p->rdev) { |
1745 | if (!test_bit(WantReplacement, &p->rdev->flags) || | |
1746 | p->replacement != NULL) | |
1747 | continue; | |
1748 | clear_bit(In_sync, &rdev->flags); | |
1749 | set_bit(Replacement, &rdev->flags); | |
1750 | rdev->raid_disk = mirror; | |
1751 | err = 0; | |
1752 | disk_stack_limits(mddev->gendisk, rdev->bdev, | |
1753 | rdev->data_offset << 9); | |
b7044d41 N |
1754 | conf->fullsync = 1; |
1755 | rcu_assign_pointer(p->replacement, rdev); | |
1756 | break; | |
1757 | } | |
1da177e4 | 1758 | |
2bb77736 N |
1759 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
1760 | rdev->data_offset << 9); | |
1da177e4 | 1761 | |
2bb77736 | 1762 | p->head_position = 0; |
d890fa2b | 1763 | p->recovery_disabled = mddev->recovery_disabled - 1; |
2bb77736 N |
1764 | rdev->raid_disk = mirror; |
1765 | err = 0; | |
1766 | if (rdev->saved_raid_disk != mirror) | |
1767 | conf->fullsync = 1; | |
1768 | rcu_assign_pointer(p->rdev, rdev); | |
1769 | break; | |
1770 | } | |
050b6615 N |
1771 | if (err == 0 && test_bit(Unmerged, &rdev->flags)) { |
1772 | /* Some requests might not have seen this new | |
1773 | * merge_bvec_fn. We must wait for them to complete | |
1774 | * before merging the device fully. | |
1775 | * First we make sure any code which has tested | |
1776 | * our function has submitted the request, then | |
1777 | * we wait for all outstanding requests to complete. | |
1778 | */ | |
1779 | synchronize_sched(); | |
1780 | raise_barrier(conf, 0); | |
1781 | lower_barrier(conf); | |
1782 | clear_bit(Unmerged, &rdev->flags); | |
1783 | } | |
ac5e7113 | 1784 | md_integrity_add_rdev(rdev, mddev); |
532a2a3f SL |
1785 | if (blk_queue_discard(bdev_get_queue(rdev->bdev))) |
1786 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue); | |
1787 | ||
1da177e4 | 1788 | print_conf(conf); |
199050ea | 1789 | return err; |
1da177e4 LT |
1790 | } |
1791 | ||
b8321b68 | 1792 | static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1793 | { |
e879a879 | 1794 | struct r10conf *conf = mddev->private; |
1da177e4 | 1795 | int err = 0; |
b8321b68 | 1796 | int number = rdev->raid_disk; |
c8ab903e | 1797 | struct md_rdev **rdevp; |
dc280d98 | 1798 | struct raid10_info *p = conf->mirrors + number; |
1da177e4 LT |
1799 | |
1800 | print_conf(conf); | |
c8ab903e N |
1801 | if (rdev == p->rdev) |
1802 | rdevp = &p->rdev; | |
1803 | else if (rdev == p->replacement) | |
1804 | rdevp = &p->replacement; | |
1805 | else | |
1806 | return 0; | |
1807 | ||
1808 | if (test_bit(In_sync, &rdev->flags) || | |
1809 | atomic_read(&rdev->nr_pending)) { | |
1810 | err = -EBUSY; | |
1811 | goto abort; | |
1812 | } | |
1813 | /* Only remove faulty devices if recovery | |
1814 | * is not possible. | |
1815 | */ | |
1816 | if (!test_bit(Faulty, &rdev->flags) && | |
1817 | mddev->recovery_disabled != p->recovery_disabled && | |
4ca40c2c | 1818 | (!p->replacement || p->replacement == rdev) && |
63aced61 | 1819 | number < conf->geo.raid_disks && |
c8ab903e N |
1820 | enough(conf, -1)) { |
1821 | err = -EBUSY; | |
1822 | goto abort; | |
1da177e4 | 1823 | } |
c8ab903e N |
1824 | *rdevp = NULL; |
1825 | synchronize_rcu(); | |
1826 | if (atomic_read(&rdev->nr_pending)) { | |
1827 | /* lost the race, try later */ | |
1828 | err = -EBUSY; | |
1829 | *rdevp = rdev; | |
1830 | goto abort; | |
4ca40c2c N |
1831 | } else if (p->replacement) { |
1832 | /* We must have just cleared 'rdev' */ | |
1833 | p->rdev = p->replacement; | |
1834 | clear_bit(Replacement, &p->replacement->flags); | |
1835 | smp_mb(); /* Make sure other CPUs may see both as identical | |
1836 | * but will never see neither -- if they are careful. | |
1837 | */ | |
1838 | p->replacement = NULL; | |
1839 | clear_bit(WantReplacement, &rdev->flags); | |
1840 | } else | |
1841 | /* We might have just remove the Replacement as faulty | |
1842 | * Clear the flag just in case | |
1843 | */ | |
1844 | clear_bit(WantReplacement, &rdev->flags); | |
1845 | ||
c8ab903e N |
1846 | err = md_integrity_register(mddev); |
1847 | ||
1da177e4 LT |
1848 | abort: |
1849 | ||
1850 | print_conf(conf); | |
1851 | return err; | |
1852 | } | |
1853 | ||
1854 | ||
6712ecf8 | 1855 | static void end_sync_read(struct bio *bio, int error) |
1da177e4 | 1856 | { |
9f2c9d12 | 1857 | struct r10bio *r10_bio = bio->bi_private; |
e879a879 | 1858 | struct r10conf *conf = r10_bio->mddev->private; |
778ca018 | 1859 | int d; |
1da177e4 | 1860 | |
3ea7daa5 N |
1861 | if (bio == r10_bio->master_bio) { |
1862 | /* this is a reshape read */ | |
1863 | d = r10_bio->read_slot; /* really the read dev */ | |
1864 | } else | |
1865 | d = find_bio_disk(conf, r10_bio, bio, NULL, NULL); | |
0eb3ff12 N |
1866 | |
1867 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
1868 | set_bit(R10BIO_Uptodate, &r10_bio->state); | |
e684e41d N |
1869 | else |
1870 | /* The write handler will notice the lack of | |
1871 | * R10BIO_Uptodate and record any errors etc | |
1872 | */ | |
4dbcdc75 N |
1873 | atomic_add(r10_bio->sectors, |
1874 | &conf->mirrors[d].rdev->corrected_errors); | |
1da177e4 LT |
1875 | |
1876 | /* for reconstruct, we always reschedule after a read. | |
1877 | * for resync, only after all reads | |
1878 | */ | |
73d5c38a | 1879 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); |
1da177e4 LT |
1880 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || |
1881 | atomic_dec_and_test(&r10_bio->remaining)) { | |
1882 | /* we have read all the blocks, | |
1883 | * do the comparison in process context in raid10d | |
1884 | */ | |
1885 | reschedule_retry(r10_bio); | |
1886 | } | |
1da177e4 LT |
1887 | } |
1888 | ||
9f2c9d12 | 1889 | static void end_sync_request(struct r10bio *r10_bio) |
1da177e4 | 1890 | { |
fd01b88c | 1891 | struct mddev *mddev = r10_bio->mddev; |
dfc70645 | 1892 | |
1da177e4 LT |
1893 | while (atomic_dec_and_test(&r10_bio->remaining)) { |
1894 | if (r10_bio->master_bio == NULL) { | |
1895 | /* the primary of several recovery bios */ | |
73d5c38a | 1896 | sector_t s = r10_bio->sectors; |
1a0b7cd8 N |
1897 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
1898 | test_bit(R10BIO_WriteError, &r10_bio->state)) | |
749c55e9 N |
1899 | reschedule_retry(r10_bio); |
1900 | else | |
1901 | put_buf(r10_bio); | |
73d5c38a | 1902 | md_done_sync(mddev, s, 1); |
1da177e4 LT |
1903 | break; |
1904 | } else { | |
9f2c9d12 | 1905 | struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; |
1a0b7cd8 N |
1906 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
1907 | test_bit(R10BIO_WriteError, &r10_bio->state)) | |
749c55e9 N |
1908 | reschedule_retry(r10_bio); |
1909 | else | |
1910 | put_buf(r10_bio); | |
1da177e4 LT |
1911 | r10_bio = r10_bio2; |
1912 | } | |
1913 | } | |
1da177e4 LT |
1914 | } |
1915 | ||
5e570289 N |
1916 | static void end_sync_write(struct bio *bio, int error) |
1917 | { | |
1918 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
9f2c9d12 | 1919 | struct r10bio *r10_bio = bio->bi_private; |
fd01b88c | 1920 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 1921 | struct r10conf *conf = mddev->private; |
5e570289 N |
1922 | int d; |
1923 | sector_t first_bad; | |
1924 | int bad_sectors; | |
1925 | int slot; | |
9ad1aefc | 1926 | int repl; |
4ca40c2c | 1927 | struct md_rdev *rdev = NULL; |
5e570289 | 1928 | |
9ad1aefc N |
1929 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
1930 | if (repl) | |
1931 | rdev = conf->mirrors[d].replacement; | |
547414d1 | 1932 | else |
9ad1aefc | 1933 | rdev = conf->mirrors[d].rdev; |
5e570289 N |
1934 | |
1935 | if (!uptodate) { | |
9ad1aefc N |
1936 | if (repl) |
1937 | md_error(mddev, rdev); | |
1938 | else { | |
1939 | set_bit(WriteErrorSeen, &rdev->flags); | |
b7044d41 N |
1940 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
1941 | set_bit(MD_RECOVERY_NEEDED, | |
1942 | &rdev->mddev->recovery); | |
9ad1aefc N |
1943 | set_bit(R10BIO_WriteError, &r10_bio->state); |
1944 | } | |
1945 | } else if (is_badblock(rdev, | |
5e570289 N |
1946 | r10_bio->devs[slot].addr, |
1947 | r10_bio->sectors, | |
1948 | &first_bad, &bad_sectors)) | |
1949 | set_bit(R10BIO_MadeGood, &r10_bio->state); | |
1950 | ||
9ad1aefc | 1951 | rdev_dec_pending(rdev, mddev); |
5e570289 N |
1952 | |
1953 | end_sync_request(r10_bio); | |
1954 | } | |
1955 | ||
1da177e4 LT |
1956 | /* |
1957 | * Note: sync and recover and handled very differently for raid10 | |
1958 | * This code is for resync. | |
1959 | * For resync, we read through virtual addresses and read all blocks. | |
1960 | * If there is any error, we schedule a write. The lowest numbered | |
1961 | * drive is authoritative. | |
1962 | * However requests come for physical address, so we need to map. | |
1963 | * For every physical address there are raid_disks/copies virtual addresses, | |
1964 | * which is always are least one, but is not necessarly an integer. | |
1965 | * This means that a physical address can span multiple chunks, so we may | |
1966 | * have to submit multiple io requests for a single sync request. | |
1967 | */ | |
1968 | /* | |
1969 | * We check if all blocks are in-sync and only write to blocks that | |
1970 | * aren't in sync | |
1971 | */ | |
9f2c9d12 | 1972 | static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
1da177e4 | 1973 | { |
e879a879 | 1974 | struct r10conf *conf = mddev->private; |
1da177e4 LT |
1975 | int i, first; |
1976 | struct bio *tbio, *fbio; | |
f4380a91 | 1977 | int vcnt; |
1da177e4 LT |
1978 | |
1979 | atomic_set(&r10_bio->remaining, 1); | |
1980 | ||
1981 | /* find the first device with a block */ | |
1982 | for (i=0; i<conf->copies; i++) | |
1983 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) | |
1984 | break; | |
1985 | ||
1986 | if (i == conf->copies) | |
1987 | goto done; | |
1988 | ||
1989 | first = i; | |
1990 | fbio = r10_bio->devs[i].bio; | |
1991 | ||
f4380a91 | 1992 | vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9); |
1da177e4 | 1993 | /* now find blocks with errors */ |
0eb3ff12 N |
1994 | for (i=0 ; i < conf->copies ; i++) { |
1995 | int j, d; | |
1da177e4 | 1996 | |
1da177e4 | 1997 | tbio = r10_bio->devs[i].bio; |
0eb3ff12 N |
1998 | |
1999 | if (tbio->bi_end_io != end_sync_read) | |
2000 | continue; | |
2001 | if (i == first) | |
1da177e4 | 2002 | continue; |
0eb3ff12 N |
2003 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) { |
2004 | /* We know that the bi_io_vec layout is the same for | |
2005 | * both 'first' and 'i', so we just compare them. | |
2006 | * All vec entries are PAGE_SIZE; | |
2007 | */ | |
2008 | for (j = 0; j < vcnt; j++) | |
2009 | if (memcmp(page_address(fbio->bi_io_vec[j].bv_page), | |
2010 | page_address(tbio->bi_io_vec[j].bv_page), | |
5020ad7d | 2011 | fbio->bi_io_vec[j].bv_len)) |
0eb3ff12 N |
2012 | break; |
2013 | if (j == vcnt) | |
2014 | continue; | |
7f7583d4 | 2015 | atomic64_add(r10_bio->sectors, &mddev->resync_mismatches); |
f84ee364 N |
2016 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
2017 | /* Don't fix anything. */ | |
2018 | continue; | |
0eb3ff12 | 2019 | } |
f84ee364 N |
2020 | /* Ok, we need to write this bio, either to correct an |
2021 | * inconsistency or to correct an unreadable block. | |
1da177e4 LT |
2022 | * First we need to fixup bv_offset, bv_len and |
2023 | * bi_vecs, as the read request might have corrupted these | |
2024 | */ | |
2025 | tbio->bi_vcnt = vcnt; | |
2026 | tbio->bi_size = r10_bio->sectors << 9; | |
2027 | tbio->bi_idx = 0; | |
2028 | tbio->bi_phys_segments = 0; | |
1da177e4 LT |
2029 | tbio->bi_flags &= ~(BIO_POOL_MASK - 1); |
2030 | tbio->bi_flags |= 1 << BIO_UPTODATE; | |
2031 | tbio->bi_next = NULL; | |
2032 | tbio->bi_rw = WRITE; | |
2033 | tbio->bi_private = r10_bio; | |
2034 | tbio->bi_sector = r10_bio->devs[i].addr; | |
2035 | ||
2036 | for (j=0; j < vcnt ; j++) { | |
2037 | tbio->bi_io_vec[j].bv_offset = 0; | |
2038 | tbio->bi_io_vec[j].bv_len = PAGE_SIZE; | |
2039 | ||
2040 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), | |
2041 | page_address(fbio->bi_io_vec[j].bv_page), | |
2042 | PAGE_SIZE); | |
2043 | } | |
2044 | tbio->bi_end_io = end_sync_write; | |
2045 | ||
2046 | d = r10_bio->devs[i].devnum; | |
2047 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
2048 | atomic_inc(&r10_bio->remaining); | |
2049 | md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9); | |
2050 | ||
2051 | tbio->bi_sector += conf->mirrors[d].rdev->data_offset; | |
2052 | tbio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
2053 | generic_make_request(tbio); | |
2054 | } | |
2055 | ||
9ad1aefc N |
2056 | /* Now write out to any replacement devices |
2057 | * that are active | |
2058 | */ | |
2059 | for (i = 0; i < conf->copies; i++) { | |
2060 | int j, d; | |
9ad1aefc N |
2061 | |
2062 | tbio = r10_bio->devs[i].repl_bio; | |
2063 | if (!tbio || !tbio->bi_end_io) | |
2064 | continue; | |
2065 | if (r10_bio->devs[i].bio->bi_end_io != end_sync_write | |
2066 | && r10_bio->devs[i].bio != fbio) | |
2067 | for (j = 0; j < vcnt; j++) | |
2068 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), | |
2069 | page_address(fbio->bi_io_vec[j].bv_page), | |
2070 | PAGE_SIZE); | |
2071 | d = r10_bio->devs[i].devnum; | |
2072 | atomic_inc(&r10_bio->remaining); | |
2073 | md_sync_acct(conf->mirrors[d].replacement->bdev, | |
2074 | tbio->bi_size >> 9); | |
2075 | generic_make_request(tbio); | |
2076 | } | |
2077 | ||
1da177e4 LT |
2078 | done: |
2079 | if (atomic_dec_and_test(&r10_bio->remaining)) { | |
2080 | md_done_sync(mddev, r10_bio->sectors, 1); | |
2081 | put_buf(r10_bio); | |
2082 | } | |
2083 | } | |
2084 | ||
2085 | /* | |
2086 | * Now for the recovery code. | |
2087 | * Recovery happens across physical sectors. | |
2088 | * We recover all non-is_sync drives by finding the virtual address of | |
2089 | * each, and then choose a working drive that also has that virt address. | |
2090 | * There is a separate r10_bio for each non-in_sync drive. | |
2091 | * Only the first two slots are in use. The first for reading, | |
2092 | * The second for writing. | |
2093 | * | |
2094 | */ | |
9f2c9d12 | 2095 | static void fix_recovery_read_error(struct r10bio *r10_bio) |
5e570289 N |
2096 | { |
2097 | /* We got a read error during recovery. | |
2098 | * We repeat the read in smaller page-sized sections. | |
2099 | * If a read succeeds, write it to the new device or record | |
2100 | * a bad block if we cannot. | |
2101 | * If a read fails, record a bad block on both old and | |
2102 | * new devices. | |
2103 | */ | |
fd01b88c | 2104 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 2105 | struct r10conf *conf = mddev->private; |
5e570289 N |
2106 | struct bio *bio = r10_bio->devs[0].bio; |
2107 | sector_t sect = 0; | |
2108 | int sectors = r10_bio->sectors; | |
2109 | int idx = 0; | |
2110 | int dr = r10_bio->devs[0].devnum; | |
2111 | int dw = r10_bio->devs[1].devnum; | |
2112 | ||
2113 | while (sectors) { | |
2114 | int s = sectors; | |
3cb03002 | 2115 | struct md_rdev *rdev; |
5e570289 N |
2116 | sector_t addr; |
2117 | int ok; | |
2118 | ||
2119 | if (s > (PAGE_SIZE>>9)) | |
2120 | s = PAGE_SIZE >> 9; | |
2121 | ||
2122 | rdev = conf->mirrors[dr].rdev; | |
2123 | addr = r10_bio->devs[0].addr + sect, | |
2124 | ok = sync_page_io(rdev, | |
2125 | addr, | |
2126 | s << 9, | |
2127 | bio->bi_io_vec[idx].bv_page, | |
2128 | READ, false); | |
2129 | if (ok) { | |
2130 | rdev = conf->mirrors[dw].rdev; | |
2131 | addr = r10_bio->devs[1].addr + sect; | |
2132 | ok = sync_page_io(rdev, | |
2133 | addr, | |
2134 | s << 9, | |
2135 | bio->bi_io_vec[idx].bv_page, | |
2136 | WRITE, false); | |
b7044d41 | 2137 | if (!ok) { |
5e570289 | 2138 | set_bit(WriteErrorSeen, &rdev->flags); |
b7044d41 N |
2139 | if (!test_and_set_bit(WantReplacement, |
2140 | &rdev->flags)) | |
2141 | set_bit(MD_RECOVERY_NEEDED, | |
2142 | &rdev->mddev->recovery); | |
2143 | } | |
5e570289 N |
2144 | } |
2145 | if (!ok) { | |
2146 | /* We don't worry if we cannot set a bad block - | |
2147 | * it really is bad so there is no loss in not | |
2148 | * recording it yet | |
2149 | */ | |
2150 | rdev_set_badblocks(rdev, addr, s, 0); | |
2151 | ||
2152 | if (rdev != conf->mirrors[dw].rdev) { | |
2153 | /* need bad block on destination too */ | |
3cb03002 | 2154 | struct md_rdev *rdev2 = conf->mirrors[dw].rdev; |
5e570289 N |
2155 | addr = r10_bio->devs[1].addr + sect; |
2156 | ok = rdev_set_badblocks(rdev2, addr, s, 0); | |
2157 | if (!ok) { | |
2158 | /* just abort the recovery */ | |
2159 | printk(KERN_NOTICE | |
2160 | "md/raid10:%s: recovery aborted" | |
2161 | " due to read error\n", | |
2162 | mdname(mddev)); | |
2163 | ||
2164 | conf->mirrors[dw].recovery_disabled | |
2165 | = mddev->recovery_disabled; | |
2166 | set_bit(MD_RECOVERY_INTR, | |
2167 | &mddev->recovery); | |
2168 | break; | |
2169 | } | |
2170 | } | |
2171 | } | |
2172 | ||
2173 | sectors -= s; | |
2174 | sect += s; | |
2175 | idx++; | |
2176 | } | |
2177 | } | |
1da177e4 | 2178 | |
9f2c9d12 | 2179 | static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
1da177e4 | 2180 | { |
e879a879 | 2181 | struct r10conf *conf = mddev->private; |
c65060ad | 2182 | int d; |
24afd80d | 2183 | struct bio *wbio, *wbio2; |
1da177e4 | 2184 | |
5e570289 N |
2185 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { |
2186 | fix_recovery_read_error(r10_bio); | |
2187 | end_sync_request(r10_bio); | |
2188 | return; | |
2189 | } | |
2190 | ||
c65060ad NK |
2191 | /* |
2192 | * share the pages with the first bio | |
1da177e4 LT |
2193 | * and submit the write request |
2194 | */ | |
1da177e4 | 2195 | d = r10_bio->devs[1].devnum; |
24afd80d N |
2196 | wbio = r10_bio->devs[1].bio; |
2197 | wbio2 = r10_bio->devs[1].repl_bio; | |
2198 | if (wbio->bi_end_io) { | |
2199 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
2200 | md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9); | |
2201 | generic_make_request(wbio); | |
2202 | } | |
2203 | if (wbio2 && wbio2->bi_end_io) { | |
2204 | atomic_inc(&conf->mirrors[d].replacement->nr_pending); | |
2205 | md_sync_acct(conf->mirrors[d].replacement->bdev, | |
2206 | wbio2->bi_size >> 9); | |
2207 | generic_make_request(wbio2); | |
2208 | } | |
1da177e4 LT |
2209 | } |
2210 | ||
2211 | ||
1e50915f RB |
2212 | /* |
2213 | * Used by fix_read_error() to decay the per rdev read_errors. | |
2214 | * We halve the read error count for every hour that has elapsed | |
2215 | * since the last recorded read error. | |
2216 | * | |
2217 | */ | |
fd01b88c | 2218 | static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev) |
1e50915f RB |
2219 | { |
2220 | struct timespec cur_time_mon; | |
2221 | unsigned long hours_since_last; | |
2222 | unsigned int read_errors = atomic_read(&rdev->read_errors); | |
2223 | ||
2224 | ktime_get_ts(&cur_time_mon); | |
2225 | ||
2226 | if (rdev->last_read_error.tv_sec == 0 && | |
2227 | rdev->last_read_error.tv_nsec == 0) { | |
2228 | /* first time we've seen a read error */ | |
2229 | rdev->last_read_error = cur_time_mon; | |
2230 | return; | |
2231 | } | |
2232 | ||
2233 | hours_since_last = (cur_time_mon.tv_sec - | |
2234 | rdev->last_read_error.tv_sec) / 3600; | |
2235 | ||
2236 | rdev->last_read_error = cur_time_mon; | |
2237 | ||
2238 | /* | |
2239 | * if hours_since_last is > the number of bits in read_errors | |
2240 | * just set read errors to 0. We do this to avoid | |
2241 | * overflowing the shift of read_errors by hours_since_last. | |
2242 | */ | |
2243 | if (hours_since_last >= 8 * sizeof(read_errors)) | |
2244 | atomic_set(&rdev->read_errors, 0); | |
2245 | else | |
2246 | atomic_set(&rdev->read_errors, read_errors >> hours_since_last); | |
2247 | } | |
2248 | ||
3cb03002 | 2249 | static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, |
58c54fcc N |
2250 | int sectors, struct page *page, int rw) |
2251 | { | |
2252 | sector_t first_bad; | |
2253 | int bad_sectors; | |
2254 | ||
2255 | if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors) | |
2256 | && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags))) | |
2257 | return -1; | |
2258 | if (sync_page_io(rdev, sector, sectors << 9, page, rw, false)) | |
2259 | /* success */ | |
2260 | return 1; | |
b7044d41 | 2261 | if (rw == WRITE) { |
58c54fcc | 2262 | set_bit(WriteErrorSeen, &rdev->flags); |
b7044d41 N |
2263 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
2264 | set_bit(MD_RECOVERY_NEEDED, | |
2265 | &rdev->mddev->recovery); | |
2266 | } | |
58c54fcc N |
2267 | /* need to record an error - either for the block or the device */ |
2268 | if (!rdev_set_badblocks(rdev, sector, sectors, 0)) | |
2269 | md_error(rdev->mddev, rdev); | |
2270 | return 0; | |
2271 | } | |
2272 | ||
1da177e4 LT |
2273 | /* |
2274 | * This is a kernel thread which: | |
2275 | * | |
2276 | * 1. Retries failed read operations on working mirrors. | |
2277 | * 2. Updates the raid superblock when problems encounter. | |
6814d536 | 2278 | * 3. Performs writes following reads for array synchronising. |
1da177e4 LT |
2279 | */ |
2280 | ||
e879a879 | 2281 | static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio) |
6814d536 N |
2282 | { |
2283 | int sect = 0; /* Offset from r10_bio->sector */ | |
2284 | int sectors = r10_bio->sectors; | |
3cb03002 | 2285 | struct md_rdev*rdev; |
1e50915f | 2286 | int max_read_errors = atomic_read(&mddev->max_corr_read_errors); |
0544a21d | 2287 | int d = r10_bio->devs[r10_bio->read_slot].devnum; |
1e50915f | 2288 | |
7c4e06ff N |
2289 | /* still own a reference to this rdev, so it cannot |
2290 | * have been cleared recently. | |
2291 | */ | |
2292 | rdev = conf->mirrors[d].rdev; | |
1e50915f | 2293 | |
7c4e06ff N |
2294 | if (test_bit(Faulty, &rdev->flags)) |
2295 | /* drive has already been failed, just ignore any | |
2296 | more fix_read_error() attempts */ | |
2297 | return; | |
1e50915f | 2298 | |
7c4e06ff N |
2299 | check_decay_read_errors(mddev, rdev); |
2300 | atomic_inc(&rdev->read_errors); | |
2301 | if (atomic_read(&rdev->read_errors) > max_read_errors) { | |
2302 | char b[BDEVNAME_SIZE]; | |
2303 | bdevname(rdev->bdev, b); | |
1e50915f | 2304 | |
7c4e06ff N |
2305 | printk(KERN_NOTICE |
2306 | "md/raid10:%s: %s: Raid device exceeded " | |
2307 | "read_error threshold [cur %d:max %d]\n", | |
2308 | mdname(mddev), b, | |
2309 | atomic_read(&rdev->read_errors), max_read_errors); | |
2310 | printk(KERN_NOTICE | |
2311 | "md/raid10:%s: %s: Failing raid device\n", | |
2312 | mdname(mddev), b); | |
2313 | md_error(mddev, conf->mirrors[d].rdev); | |
fae8cc5e | 2314 | r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED; |
7c4e06ff | 2315 | return; |
1e50915f | 2316 | } |
1e50915f | 2317 | |
6814d536 N |
2318 | while(sectors) { |
2319 | int s = sectors; | |
2320 | int sl = r10_bio->read_slot; | |
2321 | int success = 0; | |
2322 | int start; | |
2323 | ||
2324 | if (s > (PAGE_SIZE>>9)) | |
2325 | s = PAGE_SIZE >> 9; | |
2326 | ||
2327 | rcu_read_lock(); | |
2328 | do { | |
8dbed5ce N |
2329 | sector_t first_bad; |
2330 | int bad_sectors; | |
2331 | ||
0544a21d | 2332 | d = r10_bio->devs[sl].devnum; |
6814d536 N |
2333 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
2334 | if (rdev && | |
050b6615 | 2335 | !test_bit(Unmerged, &rdev->flags) && |
8dbed5ce N |
2336 | test_bit(In_sync, &rdev->flags) && |
2337 | is_badblock(rdev, r10_bio->devs[sl].addr + sect, s, | |
2338 | &first_bad, &bad_sectors) == 0) { | |
6814d536 N |
2339 | atomic_inc(&rdev->nr_pending); |
2340 | rcu_read_unlock(); | |
2b193363 | 2341 | success = sync_page_io(rdev, |
6814d536 | 2342 | r10_bio->devs[sl].addr + |
ccebd4c4 | 2343 | sect, |
6814d536 | 2344 | s<<9, |
ccebd4c4 | 2345 | conf->tmppage, READ, false); |
6814d536 N |
2346 | rdev_dec_pending(rdev, mddev); |
2347 | rcu_read_lock(); | |
2348 | if (success) | |
2349 | break; | |
2350 | } | |
2351 | sl++; | |
2352 | if (sl == conf->copies) | |
2353 | sl = 0; | |
2354 | } while (!success && sl != r10_bio->read_slot); | |
2355 | rcu_read_unlock(); | |
2356 | ||
2357 | if (!success) { | |
58c54fcc N |
2358 | /* Cannot read from anywhere, just mark the block |
2359 | * as bad on the first device to discourage future | |
2360 | * reads. | |
2361 | */ | |
6814d536 | 2362 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; |
58c54fcc N |
2363 | rdev = conf->mirrors[dn].rdev; |
2364 | ||
2365 | if (!rdev_set_badblocks( | |
2366 | rdev, | |
2367 | r10_bio->devs[r10_bio->read_slot].addr | |
2368 | + sect, | |
fae8cc5e | 2369 | s, 0)) { |
58c54fcc | 2370 | md_error(mddev, rdev); |
fae8cc5e N |
2371 | r10_bio->devs[r10_bio->read_slot].bio |
2372 | = IO_BLOCKED; | |
2373 | } | |
6814d536 N |
2374 | break; |
2375 | } | |
2376 | ||
2377 | start = sl; | |
2378 | /* write it back and re-read */ | |
2379 | rcu_read_lock(); | |
2380 | while (sl != r10_bio->read_slot) { | |
67b8dc4b | 2381 | char b[BDEVNAME_SIZE]; |
0544a21d | 2382 | |
6814d536 N |
2383 | if (sl==0) |
2384 | sl = conf->copies; | |
2385 | sl--; | |
2386 | d = r10_bio->devs[sl].devnum; | |
2387 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
1294b9c9 | 2388 | if (!rdev || |
050b6615 | 2389 | test_bit(Unmerged, &rdev->flags) || |
1294b9c9 N |
2390 | !test_bit(In_sync, &rdev->flags)) |
2391 | continue; | |
2392 | ||
2393 | atomic_inc(&rdev->nr_pending); | |
2394 | rcu_read_unlock(); | |
58c54fcc N |
2395 | if (r10_sync_page_io(rdev, |
2396 | r10_bio->devs[sl].addr + | |
2397 | sect, | |
055d3747 | 2398 | s, conf->tmppage, WRITE) |
1294b9c9 N |
2399 | == 0) { |
2400 | /* Well, this device is dead */ | |
2401 | printk(KERN_NOTICE | |
2402 | "md/raid10:%s: read correction " | |
2403 | "write failed" | |
2404 | " (%d sectors at %llu on %s)\n", | |
2405 | mdname(mddev), s, | |
2406 | (unsigned long long)( | |
f8c9e74f N |
2407 | sect + |
2408 | choose_data_offset(r10_bio, | |
2409 | rdev)), | |
1294b9c9 N |
2410 | bdevname(rdev->bdev, b)); |
2411 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " | |
2412 | "drive\n", | |
2413 | mdname(mddev), | |
2414 | bdevname(rdev->bdev, b)); | |
6814d536 | 2415 | } |
1294b9c9 N |
2416 | rdev_dec_pending(rdev, mddev); |
2417 | rcu_read_lock(); | |
6814d536 N |
2418 | } |
2419 | sl = start; | |
2420 | while (sl != r10_bio->read_slot) { | |
1294b9c9 | 2421 | char b[BDEVNAME_SIZE]; |
0544a21d | 2422 | |
6814d536 N |
2423 | if (sl==0) |
2424 | sl = conf->copies; | |
2425 | sl--; | |
2426 | d = r10_bio->devs[sl].devnum; | |
2427 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
1294b9c9 N |
2428 | if (!rdev || |
2429 | !test_bit(In_sync, &rdev->flags)) | |
2430 | continue; | |
6814d536 | 2431 | |
1294b9c9 N |
2432 | atomic_inc(&rdev->nr_pending); |
2433 | rcu_read_unlock(); | |
58c54fcc N |
2434 | switch (r10_sync_page_io(rdev, |
2435 | r10_bio->devs[sl].addr + | |
2436 | sect, | |
055d3747 | 2437 | s, conf->tmppage, |
58c54fcc N |
2438 | READ)) { |
2439 | case 0: | |
1294b9c9 N |
2440 | /* Well, this device is dead */ |
2441 | printk(KERN_NOTICE | |
2442 | "md/raid10:%s: unable to read back " | |
2443 | "corrected sectors" | |
2444 | " (%d sectors at %llu on %s)\n", | |
2445 | mdname(mddev), s, | |
2446 | (unsigned long long)( | |
f8c9e74f N |
2447 | sect + |
2448 | choose_data_offset(r10_bio, rdev)), | |
1294b9c9 N |
2449 | bdevname(rdev->bdev, b)); |
2450 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " | |
2451 | "drive\n", | |
2452 | mdname(mddev), | |
2453 | bdevname(rdev->bdev, b)); | |
58c54fcc N |
2454 | break; |
2455 | case 1: | |
1294b9c9 N |
2456 | printk(KERN_INFO |
2457 | "md/raid10:%s: read error corrected" | |
2458 | " (%d sectors at %llu on %s)\n", | |
2459 | mdname(mddev), s, | |
2460 | (unsigned long long)( | |
f8c9e74f N |
2461 | sect + |
2462 | choose_data_offset(r10_bio, rdev)), | |
1294b9c9 N |
2463 | bdevname(rdev->bdev, b)); |
2464 | atomic_add(s, &rdev->corrected_errors); | |
6814d536 | 2465 | } |
1294b9c9 N |
2466 | |
2467 | rdev_dec_pending(rdev, mddev); | |
2468 | rcu_read_lock(); | |
6814d536 N |
2469 | } |
2470 | rcu_read_unlock(); | |
2471 | ||
2472 | sectors -= s; | |
2473 | sect += s; | |
2474 | } | |
2475 | } | |
2476 | ||
bd870a16 N |
2477 | static void bi_complete(struct bio *bio, int error) |
2478 | { | |
2479 | complete((struct completion *)bio->bi_private); | |
2480 | } | |
2481 | ||
2482 | static int submit_bio_wait(int rw, struct bio *bio) | |
2483 | { | |
2484 | struct completion event; | |
2485 | rw |= REQ_SYNC; | |
2486 | ||
2487 | init_completion(&event); | |
2488 | bio->bi_private = &event; | |
2489 | bio->bi_end_io = bi_complete; | |
2490 | submit_bio(rw, bio); | |
2491 | wait_for_completion(&event); | |
2492 | ||
2493 | return test_bit(BIO_UPTODATE, &bio->bi_flags); | |
2494 | } | |
2495 | ||
9f2c9d12 | 2496 | static int narrow_write_error(struct r10bio *r10_bio, int i) |
bd870a16 N |
2497 | { |
2498 | struct bio *bio = r10_bio->master_bio; | |
fd01b88c | 2499 | struct mddev *mddev = r10_bio->mddev; |
e879a879 | 2500 | struct r10conf *conf = mddev->private; |
3cb03002 | 2501 | struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; |
bd870a16 N |
2502 | /* bio has the data to be written to slot 'i' where |
2503 | * we just recently had a write error. | |
2504 | * We repeatedly clone the bio and trim down to one block, | |
2505 | * then try the write. Where the write fails we record | |
2506 | * a bad block. | |
2507 | * It is conceivable that the bio doesn't exactly align with | |
2508 | * blocks. We must handle this. | |
2509 | * | |
2510 | * We currently own a reference to the rdev. | |
2511 | */ | |
2512 | ||
2513 | int block_sectors; | |
2514 | sector_t sector; | |
2515 | int sectors; | |
2516 | int sect_to_write = r10_bio->sectors; | |
2517 | int ok = 1; | |
2518 | ||
2519 | if (rdev->badblocks.shift < 0) | |
2520 | return 0; | |
2521 | ||
2522 | block_sectors = 1 << rdev->badblocks.shift; | |
2523 | sector = r10_bio->sector; | |
2524 | sectors = ((r10_bio->sector + block_sectors) | |
2525 | & ~(sector_t)(block_sectors - 1)) | |
2526 | - sector; | |
2527 | ||
2528 | while (sect_to_write) { | |
2529 | struct bio *wbio; | |
2530 | if (sectors > sect_to_write) | |
2531 | sectors = sect_to_write; | |
2532 | /* Write at 'sector' for 'sectors' */ | |
2533 | wbio = bio_clone_mddev(bio, GFP_NOIO, mddev); | |
2534 | md_trim_bio(wbio, sector - bio->bi_sector, sectors); | |
2535 | wbio->bi_sector = (r10_bio->devs[i].addr+ | |
f8c9e74f | 2536 | choose_data_offset(r10_bio, rdev) + |
bd870a16 N |
2537 | (sector - r10_bio->sector)); |
2538 | wbio->bi_bdev = rdev->bdev; | |
2539 | if (submit_bio_wait(WRITE, wbio) == 0) | |
2540 | /* Failure! */ | |
2541 | ok = rdev_set_badblocks(rdev, sector, | |
2542 | sectors, 0) | |
2543 | && ok; | |
2544 | ||
2545 | bio_put(wbio); | |
2546 | sect_to_write -= sectors; | |
2547 | sector += sectors; | |
2548 | sectors = block_sectors; | |
2549 | } | |
2550 | return ok; | |
2551 | } | |
2552 | ||
9f2c9d12 | 2553 | static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) |
560f8e55 N |
2554 | { |
2555 | int slot = r10_bio->read_slot; | |
560f8e55 | 2556 | struct bio *bio; |
e879a879 | 2557 | struct r10conf *conf = mddev->private; |
abbf098e | 2558 | struct md_rdev *rdev = r10_bio->devs[slot].rdev; |
560f8e55 N |
2559 | char b[BDEVNAME_SIZE]; |
2560 | unsigned long do_sync; | |
856e08e2 | 2561 | int max_sectors; |
560f8e55 N |
2562 | |
2563 | /* we got a read error. Maybe the drive is bad. Maybe just | |
2564 | * the block and we can fix it. | |
2565 | * We freeze all other IO, and try reading the block from | |
2566 | * other devices. When we find one, we re-write | |
2567 | * and check it that fixes the read error. | |
2568 | * This is all done synchronously while the array is | |
2569 | * frozen. | |
2570 | */ | |
fae8cc5e N |
2571 | bio = r10_bio->devs[slot].bio; |
2572 | bdevname(bio->bi_bdev, b); | |
2573 | bio_put(bio); | |
2574 | r10_bio->devs[slot].bio = NULL; | |
2575 | ||
560f8e55 N |
2576 | if (mddev->ro == 0) { |
2577 | freeze_array(conf); | |
2578 | fix_read_error(conf, mddev, r10_bio); | |
2579 | unfreeze_array(conf); | |
fae8cc5e N |
2580 | } else |
2581 | r10_bio->devs[slot].bio = IO_BLOCKED; | |
2582 | ||
abbf098e | 2583 | rdev_dec_pending(rdev, mddev); |
560f8e55 | 2584 | |
7399c31b | 2585 | read_more: |
96c3fd1f N |
2586 | rdev = read_balance(conf, r10_bio, &max_sectors); |
2587 | if (rdev == NULL) { | |
560f8e55 N |
2588 | printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O" |
2589 | " read error for block %llu\n", | |
7399c31b | 2590 | mdname(mddev), b, |
560f8e55 N |
2591 | (unsigned long long)r10_bio->sector); |
2592 | raid_end_bio_io(r10_bio); | |
560f8e55 N |
2593 | return; |
2594 | } | |
2595 | ||
2596 | do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC); | |
560f8e55 | 2597 | slot = r10_bio->read_slot; |
560f8e55 N |
2598 | printk_ratelimited( |
2599 | KERN_ERR | |
055d3747 | 2600 | "md/raid10:%s: %s: redirecting " |
560f8e55 N |
2601 | "sector %llu to another mirror\n", |
2602 | mdname(mddev), | |
2603 | bdevname(rdev->bdev, b), | |
2604 | (unsigned long long)r10_bio->sector); | |
2605 | bio = bio_clone_mddev(r10_bio->master_bio, | |
2606 | GFP_NOIO, mddev); | |
7399c31b N |
2607 | md_trim_bio(bio, |
2608 | r10_bio->sector - bio->bi_sector, | |
2609 | max_sectors); | |
560f8e55 | 2610 | r10_bio->devs[slot].bio = bio; |
abbf098e | 2611 | r10_bio->devs[slot].rdev = rdev; |
560f8e55 | 2612 | bio->bi_sector = r10_bio->devs[slot].addr |
f8c9e74f | 2613 | + choose_data_offset(r10_bio, rdev); |
560f8e55 N |
2614 | bio->bi_bdev = rdev->bdev; |
2615 | bio->bi_rw = READ | do_sync; | |
2616 | bio->bi_private = r10_bio; | |
2617 | bio->bi_end_io = raid10_end_read_request; | |
7399c31b N |
2618 | if (max_sectors < r10_bio->sectors) { |
2619 | /* Drat - have to split this up more */ | |
2620 | struct bio *mbio = r10_bio->master_bio; | |
2621 | int sectors_handled = | |
2622 | r10_bio->sector + max_sectors | |
2623 | - mbio->bi_sector; | |
2624 | r10_bio->sectors = max_sectors; | |
2625 | spin_lock_irq(&conf->device_lock); | |
2626 | if (mbio->bi_phys_segments == 0) | |
2627 | mbio->bi_phys_segments = 2; | |
2628 | else | |
2629 | mbio->bi_phys_segments++; | |
2630 | spin_unlock_irq(&conf->device_lock); | |
2631 | generic_make_request(bio); | |
7399c31b N |
2632 | |
2633 | r10_bio = mempool_alloc(conf->r10bio_pool, | |
2634 | GFP_NOIO); | |
2635 | r10_bio->master_bio = mbio; | |
2636 | r10_bio->sectors = (mbio->bi_size >> 9) | |
2637 | - sectors_handled; | |
2638 | r10_bio->state = 0; | |
2639 | set_bit(R10BIO_ReadError, | |
2640 | &r10_bio->state); | |
2641 | r10_bio->mddev = mddev; | |
2642 | r10_bio->sector = mbio->bi_sector | |
2643 | + sectors_handled; | |
2644 | ||
2645 | goto read_more; | |
2646 | } else | |
2647 | generic_make_request(bio); | |
560f8e55 N |
2648 | } |
2649 | ||
e879a879 | 2650 | static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) |
749c55e9 N |
2651 | { |
2652 | /* Some sort of write request has finished and it | |
2653 | * succeeded in writing where we thought there was a | |
2654 | * bad block. So forget the bad block. | |
1a0b7cd8 N |
2655 | * Or possibly if failed and we need to record |
2656 | * a bad block. | |
749c55e9 N |
2657 | */ |
2658 | int m; | |
3cb03002 | 2659 | struct md_rdev *rdev; |
749c55e9 N |
2660 | |
2661 | if (test_bit(R10BIO_IsSync, &r10_bio->state) || | |
2662 | test_bit(R10BIO_IsRecover, &r10_bio->state)) { | |
1a0b7cd8 N |
2663 | for (m = 0; m < conf->copies; m++) { |
2664 | int dev = r10_bio->devs[m].devnum; | |
2665 | rdev = conf->mirrors[dev].rdev; | |
2666 | if (r10_bio->devs[m].bio == NULL) | |
2667 | continue; | |
2668 | if (test_bit(BIO_UPTODATE, | |
749c55e9 | 2669 | &r10_bio->devs[m].bio->bi_flags)) { |
749c55e9 N |
2670 | rdev_clear_badblocks( |
2671 | rdev, | |
2672 | r10_bio->devs[m].addr, | |
c6563a8c | 2673 | r10_bio->sectors, 0); |
1a0b7cd8 N |
2674 | } else { |
2675 | if (!rdev_set_badblocks( | |
2676 | rdev, | |
2677 | r10_bio->devs[m].addr, | |
2678 | r10_bio->sectors, 0)) | |
2679 | md_error(conf->mddev, rdev); | |
749c55e9 | 2680 | } |
9ad1aefc N |
2681 | rdev = conf->mirrors[dev].replacement; |
2682 | if (r10_bio->devs[m].repl_bio == NULL) | |
2683 | continue; | |
2684 | if (test_bit(BIO_UPTODATE, | |
2685 | &r10_bio->devs[m].repl_bio->bi_flags)) { | |
2686 | rdev_clear_badblocks( | |
2687 | rdev, | |
2688 | r10_bio->devs[m].addr, | |
c6563a8c | 2689 | r10_bio->sectors, 0); |
9ad1aefc N |
2690 | } else { |
2691 | if (!rdev_set_badblocks( | |
2692 | rdev, | |
2693 | r10_bio->devs[m].addr, | |
2694 | r10_bio->sectors, 0)) | |
2695 | md_error(conf->mddev, rdev); | |
2696 | } | |
1a0b7cd8 | 2697 | } |
749c55e9 N |
2698 | put_buf(r10_bio); |
2699 | } else { | |
bd870a16 N |
2700 | for (m = 0; m < conf->copies; m++) { |
2701 | int dev = r10_bio->devs[m].devnum; | |
2702 | struct bio *bio = r10_bio->devs[m].bio; | |
2703 | rdev = conf->mirrors[dev].rdev; | |
2704 | if (bio == IO_MADE_GOOD) { | |
749c55e9 N |
2705 | rdev_clear_badblocks( |
2706 | rdev, | |
2707 | r10_bio->devs[m].addr, | |
c6563a8c | 2708 | r10_bio->sectors, 0); |
749c55e9 | 2709 | rdev_dec_pending(rdev, conf->mddev); |
bd870a16 N |
2710 | } else if (bio != NULL && |
2711 | !test_bit(BIO_UPTODATE, &bio->bi_flags)) { | |
2712 | if (!narrow_write_error(r10_bio, m)) { | |
2713 | md_error(conf->mddev, rdev); | |
2714 | set_bit(R10BIO_Degraded, | |
2715 | &r10_bio->state); | |
2716 | } | |
2717 | rdev_dec_pending(rdev, conf->mddev); | |
749c55e9 | 2718 | } |
475b0321 N |
2719 | bio = r10_bio->devs[m].repl_bio; |
2720 | rdev = conf->mirrors[dev].replacement; | |
4ca40c2c | 2721 | if (rdev && bio == IO_MADE_GOOD) { |
475b0321 N |
2722 | rdev_clear_badblocks( |
2723 | rdev, | |
2724 | r10_bio->devs[m].addr, | |
c6563a8c | 2725 | r10_bio->sectors, 0); |
475b0321 N |
2726 | rdev_dec_pending(rdev, conf->mddev); |
2727 | } | |
bd870a16 N |
2728 | } |
2729 | if (test_bit(R10BIO_WriteError, | |
2730 | &r10_bio->state)) | |
2731 | close_write(r10_bio); | |
749c55e9 N |
2732 | raid_end_bio_io(r10_bio); |
2733 | } | |
2734 | } | |
2735 | ||
4ed8731d | 2736 | static void raid10d(struct md_thread *thread) |
1da177e4 | 2737 | { |
4ed8731d | 2738 | struct mddev *mddev = thread->mddev; |
9f2c9d12 | 2739 | struct r10bio *r10_bio; |
1da177e4 | 2740 | unsigned long flags; |
e879a879 | 2741 | struct r10conf *conf = mddev->private; |
1da177e4 | 2742 | struct list_head *head = &conf->retry_list; |
e1dfa0a2 | 2743 | struct blk_plug plug; |
1da177e4 LT |
2744 | |
2745 | md_check_recovery(mddev); | |
1da177e4 | 2746 | |
e1dfa0a2 | 2747 | blk_start_plug(&plug); |
1da177e4 | 2748 | for (;;) { |
6cce3b23 | 2749 | |
0021b7bc | 2750 | flush_pending_writes(conf); |
6cce3b23 | 2751 | |
a35e63ef N |
2752 | spin_lock_irqsave(&conf->device_lock, flags); |
2753 | if (list_empty(head)) { | |
2754 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 | 2755 | break; |
a35e63ef | 2756 | } |
9f2c9d12 | 2757 | r10_bio = list_entry(head->prev, struct r10bio, retry_list); |
1da177e4 | 2758 | list_del(head->prev); |
4443ae10 | 2759 | conf->nr_queued--; |
1da177e4 LT |
2760 | spin_unlock_irqrestore(&conf->device_lock, flags); |
2761 | ||
2762 | mddev = r10_bio->mddev; | |
070ec55d | 2763 | conf = mddev->private; |
bd870a16 N |
2764 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
2765 | test_bit(R10BIO_WriteError, &r10_bio->state)) | |
749c55e9 | 2766 | handle_write_completed(conf, r10_bio); |
3ea7daa5 N |
2767 | else if (test_bit(R10BIO_IsReshape, &r10_bio->state)) |
2768 | reshape_request_write(mddev, r10_bio); | |
749c55e9 | 2769 | else if (test_bit(R10BIO_IsSync, &r10_bio->state)) |
1da177e4 | 2770 | sync_request_write(mddev, r10_bio); |
7eaceacc | 2771 | else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) |
1da177e4 | 2772 | recovery_request_write(mddev, r10_bio); |
856e08e2 | 2773 | else if (test_bit(R10BIO_ReadError, &r10_bio->state)) |
560f8e55 | 2774 | handle_read_error(mddev, r10_bio); |
856e08e2 N |
2775 | else { |
2776 | /* just a partial read to be scheduled from a | |
2777 | * separate context | |
2778 | */ | |
2779 | int slot = r10_bio->read_slot; | |
2780 | generic_make_request(r10_bio->devs[slot].bio); | |
2781 | } | |
560f8e55 | 2782 | |
1d9d5241 | 2783 | cond_resched(); |
de393cde N |
2784 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
2785 | md_check_recovery(mddev); | |
1da177e4 | 2786 | } |
e1dfa0a2 | 2787 | blk_finish_plug(&plug); |
1da177e4 LT |
2788 | } |
2789 | ||
2790 | ||
e879a879 | 2791 | static int init_resync(struct r10conf *conf) |
1da177e4 LT |
2792 | { |
2793 | int buffs; | |
69335ef3 | 2794 | int i; |
1da177e4 LT |
2795 | |
2796 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; | |
b6385483 | 2797 | BUG_ON(conf->r10buf_pool); |
69335ef3 | 2798 | conf->have_replacement = 0; |
5cf00fcd | 2799 | for (i = 0; i < conf->geo.raid_disks; i++) |
69335ef3 N |
2800 | if (conf->mirrors[i].replacement) |
2801 | conf->have_replacement = 1; | |
1da177e4 LT |
2802 | conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); |
2803 | if (!conf->r10buf_pool) | |
2804 | return -ENOMEM; | |
2805 | conf->next_resync = 0; | |
2806 | return 0; | |
2807 | } | |
2808 | ||
2809 | /* | |
2810 | * perform a "sync" on one "block" | |
2811 | * | |
2812 | * We need to make sure that no normal I/O request - particularly write | |
2813 | * requests - conflict with active sync requests. | |
2814 | * | |
2815 | * This is achieved by tracking pending requests and a 'barrier' concept | |
2816 | * that can be installed to exclude normal IO requests. | |
2817 | * | |
2818 | * Resync and recovery are handled very differently. | |
2819 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. | |
2820 | * | |
2821 | * For resync, we iterate over virtual addresses, read all copies, | |
2822 | * and update if there are differences. If only one copy is live, | |
2823 | * skip it. | |
2824 | * For recovery, we iterate over physical addresses, read a good | |
2825 | * value for each non-in_sync drive, and over-write. | |
2826 | * | |
2827 | * So, for recovery we may have several outstanding complex requests for a | |
2828 | * given address, one for each out-of-sync device. We model this by allocating | |
2829 | * a number of r10_bio structures, one for each out-of-sync device. | |
2830 | * As we setup these structures, we collect all bio's together into a list | |
2831 | * which we then process collectively to add pages, and then process again | |
2832 | * to pass to generic_make_request. | |
2833 | * | |
2834 | * The r10_bio structures are linked using a borrowed master_bio pointer. | |
2835 | * This link is counted in ->remaining. When the r10_bio that points to NULL | |
2836 | * has its remaining count decremented to 0, the whole complex operation | |
2837 | * is complete. | |
2838 | * | |
2839 | */ | |
2840 | ||
fd01b88c | 2841 | static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, |
ab9d47e9 | 2842 | int *skipped, int go_faster) |
1da177e4 | 2843 | { |
e879a879 | 2844 | struct r10conf *conf = mddev->private; |
9f2c9d12 | 2845 | struct r10bio *r10_bio; |
1da177e4 LT |
2846 | struct bio *biolist = NULL, *bio; |
2847 | sector_t max_sector, nr_sectors; | |
1da177e4 | 2848 | int i; |
6cce3b23 | 2849 | int max_sync; |
57dab0bd | 2850 | sector_t sync_blocks; |
1da177e4 LT |
2851 | sector_t sectors_skipped = 0; |
2852 | int chunks_skipped = 0; | |
5cf00fcd | 2853 | sector_t chunk_mask = conf->geo.chunk_mask; |
1da177e4 LT |
2854 | |
2855 | if (!conf->r10buf_pool) | |
2856 | if (init_resync(conf)) | |
57afd89f | 2857 | return 0; |
1da177e4 LT |
2858 | |
2859 | skipped: | |
58c0fed4 | 2860 | max_sector = mddev->dev_sectors; |
3ea7daa5 N |
2861 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
2862 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
1da177e4 LT |
2863 | max_sector = mddev->resync_max_sectors; |
2864 | if (sector_nr >= max_sector) { | |
6cce3b23 N |
2865 | /* If we aborted, we need to abort the |
2866 | * sync on the 'current' bitmap chucks (there can | |
2867 | * be several when recovering multiple devices). | |
2868 | * as we may have started syncing it but not finished. | |
2869 | * We can find the current address in | |
2870 | * mddev->curr_resync, but for recovery, | |
2871 | * we need to convert that to several | |
2872 | * virtual addresses. | |
2873 | */ | |
3ea7daa5 N |
2874 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
2875 | end_reshape(conf); | |
2876 | return 0; | |
2877 | } | |
2878 | ||
6cce3b23 N |
2879 | if (mddev->curr_resync < max_sector) { /* aborted */ |
2880 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) | |
2881 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
2882 | &sync_blocks, 1); | |
5cf00fcd | 2883 | else for (i = 0; i < conf->geo.raid_disks; i++) { |
6cce3b23 N |
2884 | sector_t sect = |
2885 | raid10_find_virt(conf, mddev->curr_resync, i); | |
2886 | bitmap_end_sync(mddev->bitmap, sect, | |
2887 | &sync_blocks, 1); | |
2888 | } | |
9ad1aefc N |
2889 | } else { |
2890 | /* completed sync */ | |
2891 | if ((!mddev->bitmap || conf->fullsync) | |
2892 | && conf->have_replacement | |
2893 | && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
2894 | /* Completed a full sync so the replacements | |
2895 | * are now fully recovered. | |
2896 | */ | |
5cf00fcd | 2897 | for (i = 0; i < conf->geo.raid_disks; i++) |
9ad1aefc N |
2898 | if (conf->mirrors[i].replacement) |
2899 | conf->mirrors[i].replacement | |
2900 | ->recovery_offset | |
2901 | = MaxSector; | |
2902 | } | |
6cce3b23 | 2903 | conf->fullsync = 0; |
9ad1aefc | 2904 | } |
6cce3b23 | 2905 | bitmap_close_sync(mddev->bitmap); |
1da177e4 | 2906 | close_sync(conf); |
57afd89f | 2907 | *skipped = 1; |
1da177e4 LT |
2908 | return sectors_skipped; |
2909 | } | |
3ea7daa5 N |
2910 | |
2911 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) | |
2912 | return reshape_request(mddev, sector_nr, skipped); | |
2913 | ||
5cf00fcd | 2914 | if (chunks_skipped >= conf->geo.raid_disks) { |
1da177e4 LT |
2915 | /* if there has been nothing to do on any drive, |
2916 | * then there is nothing to do at all.. | |
2917 | */ | |
57afd89f N |
2918 | *skipped = 1; |
2919 | return (max_sector - sector_nr) + sectors_skipped; | |
1da177e4 LT |
2920 | } |
2921 | ||
c6207277 N |
2922 | if (max_sector > mddev->resync_max) |
2923 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ | |
2924 | ||
1da177e4 LT |
2925 | /* make sure whole request will fit in a chunk - if chunks |
2926 | * are meaningful | |
2927 | */ | |
5cf00fcd N |
2928 | if (conf->geo.near_copies < conf->geo.raid_disks && |
2929 | max_sector > (sector_nr | chunk_mask)) | |
2930 | max_sector = (sector_nr | chunk_mask) + 1; | |
1da177e4 LT |
2931 | /* |
2932 | * If there is non-resync activity waiting for us then | |
2933 | * put in a delay to throttle resync. | |
2934 | */ | |
0a27ec96 | 2935 | if (!go_faster && conf->nr_waiting) |
1da177e4 | 2936 | msleep_interruptible(1000); |
1da177e4 LT |
2937 | |
2938 | /* Again, very different code for resync and recovery. | |
2939 | * Both must result in an r10bio with a list of bios that | |
2940 | * have bi_end_io, bi_sector, bi_bdev set, | |
2941 | * and bi_private set to the r10bio. | |
2942 | * For recovery, we may actually create several r10bios | |
2943 | * with 2 bios in each, that correspond to the bios in the main one. | |
2944 | * In this case, the subordinate r10bios link back through a | |
2945 | * borrowed master_bio pointer, and the counter in the master | |
2946 | * includes a ref from each subordinate. | |
2947 | */ | |
2948 | /* First, we decide what to do and set ->bi_end_io | |
2949 | * To end_sync_read if we want to read, and | |
2950 | * end_sync_write if we will want to write. | |
2951 | */ | |
2952 | ||
6cce3b23 | 2953 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
1da177e4 LT |
2954 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
2955 | /* recovery... the complicated one */ | |
e875ecea | 2956 | int j; |
1da177e4 LT |
2957 | r10_bio = NULL; |
2958 | ||
5cf00fcd | 2959 | for (i = 0 ; i < conf->geo.raid_disks; i++) { |
ab9d47e9 | 2960 | int still_degraded; |
9f2c9d12 | 2961 | struct r10bio *rb2; |
ab9d47e9 N |
2962 | sector_t sect; |
2963 | int must_sync; | |
e875ecea | 2964 | int any_working; |
dc280d98 | 2965 | struct raid10_info *mirror = &conf->mirrors[i]; |
24afd80d N |
2966 | |
2967 | if ((mirror->rdev == NULL || | |
2968 | test_bit(In_sync, &mirror->rdev->flags)) | |
2969 | && | |
2970 | (mirror->replacement == NULL || | |
2971 | test_bit(Faulty, | |
2972 | &mirror->replacement->flags))) | |
ab9d47e9 | 2973 | continue; |
1da177e4 | 2974 | |
ab9d47e9 N |
2975 | still_degraded = 0; |
2976 | /* want to reconstruct this device */ | |
2977 | rb2 = r10_bio; | |
2978 | sect = raid10_find_virt(conf, sector_nr, i); | |
fc448a18 N |
2979 | if (sect >= mddev->resync_max_sectors) { |
2980 | /* last stripe is not complete - don't | |
2981 | * try to recover this sector. | |
2982 | */ | |
2983 | continue; | |
2984 | } | |
24afd80d N |
2985 | /* Unless we are doing a full sync, or a replacement |
2986 | * we only need to recover the block if it is set in | |
2987 | * the bitmap | |
ab9d47e9 N |
2988 | */ |
2989 | must_sync = bitmap_start_sync(mddev->bitmap, sect, | |
2990 | &sync_blocks, 1); | |
2991 | if (sync_blocks < max_sync) | |
2992 | max_sync = sync_blocks; | |
2993 | if (!must_sync && | |
24afd80d | 2994 | mirror->replacement == NULL && |
ab9d47e9 N |
2995 | !conf->fullsync) { |
2996 | /* yep, skip the sync_blocks here, but don't assume | |
2997 | * that there will never be anything to do here | |
2998 | */ | |
2999 | chunks_skipped = -1; | |
3000 | continue; | |
3001 | } | |
6cce3b23 | 3002 | |
ab9d47e9 N |
3003 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
3004 | raise_barrier(conf, rb2 != NULL); | |
3005 | atomic_set(&r10_bio->remaining, 0); | |
18055569 | 3006 | |
ab9d47e9 N |
3007 | r10_bio->master_bio = (struct bio*)rb2; |
3008 | if (rb2) | |
3009 | atomic_inc(&rb2->remaining); | |
3010 | r10_bio->mddev = mddev; | |
3011 | set_bit(R10BIO_IsRecover, &r10_bio->state); | |
3012 | r10_bio->sector = sect; | |
1da177e4 | 3013 | |
ab9d47e9 N |
3014 | raid10_find_phys(conf, r10_bio); |
3015 | ||
3016 | /* Need to check if the array will still be | |
3017 | * degraded | |
3018 | */ | |
5cf00fcd | 3019 | for (j = 0; j < conf->geo.raid_disks; j++) |
ab9d47e9 N |
3020 | if (conf->mirrors[j].rdev == NULL || |
3021 | test_bit(Faulty, &conf->mirrors[j].rdev->flags)) { | |
3022 | still_degraded = 1; | |
87fc767b | 3023 | break; |
1da177e4 | 3024 | } |
ab9d47e9 N |
3025 | |
3026 | must_sync = bitmap_start_sync(mddev->bitmap, sect, | |
3027 | &sync_blocks, still_degraded); | |
3028 | ||
e875ecea | 3029 | any_working = 0; |
ab9d47e9 | 3030 | for (j=0; j<conf->copies;j++) { |
e875ecea | 3031 | int k; |
ab9d47e9 | 3032 | int d = r10_bio->devs[j].devnum; |
5e570289 | 3033 | sector_t from_addr, to_addr; |
3cb03002 | 3034 | struct md_rdev *rdev; |
40c356ce N |
3035 | sector_t sector, first_bad; |
3036 | int bad_sectors; | |
ab9d47e9 N |
3037 | if (!conf->mirrors[d].rdev || |
3038 | !test_bit(In_sync, &conf->mirrors[d].rdev->flags)) | |
3039 | continue; | |
3040 | /* This is where we read from */ | |
e875ecea | 3041 | any_working = 1; |
40c356ce N |
3042 | rdev = conf->mirrors[d].rdev; |
3043 | sector = r10_bio->devs[j].addr; | |
3044 | ||
3045 | if (is_badblock(rdev, sector, max_sync, | |
3046 | &first_bad, &bad_sectors)) { | |
3047 | if (first_bad > sector) | |
3048 | max_sync = first_bad - sector; | |
3049 | else { | |
3050 | bad_sectors -= (sector | |
3051 | - first_bad); | |
3052 | if (max_sync > bad_sectors) | |
3053 | max_sync = bad_sectors; | |
3054 | continue; | |
3055 | } | |
3056 | } | |
ab9d47e9 N |
3057 | bio = r10_bio->devs[0].bio; |
3058 | bio->bi_next = biolist; | |
3059 | biolist = bio; | |
3060 | bio->bi_private = r10_bio; | |
3061 | bio->bi_end_io = end_sync_read; | |
3062 | bio->bi_rw = READ; | |
5e570289 | 3063 | from_addr = r10_bio->devs[j].addr; |
24afd80d N |
3064 | bio->bi_sector = from_addr + rdev->data_offset; |
3065 | bio->bi_bdev = rdev->bdev; | |
3066 | atomic_inc(&rdev->nr_pending); | |
3067 | /* and we write to 'i' (if not in_sync) */ | |
ab9d47e9 N |
3068 | |
3069 | for (k=0; k<conf->copies; k++) | |
3070 | if (r10_bio->devs[k].devnum == i) | |
3071 | break; | |
3072 | BUG_ON(k == conf->copies); | |
5e570289 | 3073 | to_addr = r10_bio->devs[k].addr; |
ab9d47e9 | 3074 | r10_bio->devs[0].devnum = d; |
5e570289 | 3075 | r10_bio->devs[0].addr = from_addr; |
ab9d47e9 | 3076 | r10_bio->devs[1].devnum = i; |
5e570289 | 3077 | r10_bio->devs[1].addr = to_addr; |
ab9d47e9 | 3078 | |
24afd80d N |
3079 | rdev = mirror->rdev; |
3080 | if (!test_bit(In_sync, &rdev->flags)) { | |
3081 | bio = r10_bio->devs[1].bio; | |
3082 | bio->bi_next = biolist; | |
3083 | biolist = bio; | |
3084 | bio->bi_private = r10_bio; | |
3085 | bio->bi_end_io = end_sync_write; | |
3086 | bio->bi_rw = WRITE; | |
3087 | bio->bi_sector = to_addr | |
3088 | + rdev->data_offset; | |
3089 | bio->bi_bdev = rdev->bdev; | |
3090 | atomic_inc(&r10_bio->remaining); | |
3091 | } else | |
3092 | r10_bio->devs[1].bio->bi_end_io = NULL; | |
3093 | ||
3094 | /* and maybe write to replacement */ | |
3095 | bio = r10_bio->devs[1].repl_bio; | |
3096 | if (bio) | |
3097 | bio->bi_end_io = NULL; | |
3098 | rdev = mirror->replacement; | |
3099 | /* Note: if rdev != NULL, then bio | |
3100 | * cannot be NULL as r10buf_pool_alloc will | |
3101 | * have allocated it. | |
3102 | * So the second test here is pointless. | |
3103 | * But it keeps semantic-checkers happy, and | |
3104 | * this comment keeps human reviewers | |
3105 | * happy. | |
3106 | */ | |
3107 | if (rdev == NULL || bio == NULL || | |
3108 | test_bit(Faulty, &rdev->flags)) | |
3109 | break; | |
3110 | bio->bi_next = biolist; | |
3111 | biolist = bio; | |
3112 | bio->bi_private = r10_bio; | |
3113 | bio->bi_end_io = end_sync_write; | |
3114 | bio->bi_rw = WRITE; | |
3115 | bio->bi_sector = to_addr + rdev->data_offset; | |
3116 | bio->bi_bdev = rdev->bdev; | |
3117 | atomic_inc(&r10_bio->remaining); | |
ab9d47e9 N |
3118 | break; |
3119 | } | |
3120 | if (j == conf->copies) { | |
e875ecea N |
3121 | /* Cannot recover, so abort the recovery or |
3122 | * record a bad block */ | |
ab9d47e9 N |
3123 | put_buf(r10_bio); |
3124 | if (rb2) | |
3125 | atomic_dec(&rb2->remaining); | |
3126 | r10_bio = rb2; | |
e875ecea N |
3127 | if (any_working) { |
3128 | /* problem is that there are bad blocks | |
3129 | * on other device(s) | |
3130 | */ | |
3131 | int k; | |
3132 | for (k = 0; k < conf->copies; k++) | |
3133 | if (r10_bio->devs[k].devnum == i) | |
3134 | break; | |
24afd80d N |
3135 | if (!test_bit(In_sync, |
3136 | &mirror->rdev->flags) | |
3137 | && !rdev_set_badblocks( | |
3138 | mirror->rdev, | |
3139 | r10_bio->devs[k].addr, | |
3140 | max_sync, 0)) | |
3141 | any_working = 0; | |
3142 | if (mirror->replacement && | |
3143 | !rdev_set_badblocks( | |
3144 | mirror->replacement, | |
e875ecea N |
3145 | r10_bio->devs[k].addr, |
3146 | max_sync, 0)) | |
3147 | any_working = 0; | |
3148 | } | |
3149 | if (!any_working) { | |
3150 | if (!test_and_set_bit(MD_RECOVERY_INTR, | |
3151 | &mddev->recovery)) | |
3152 | printk(KERN_INFO "md/raid10:%s: insufficient " | |
3153 | "working devices for recovery.\n", | |
3154 | mdname(mddev)); | |
24afd80d | 3155 | mirror->recovery_disabled |
e875ecea N |
3156 | = mddev->recovery_disabled; |
3157 | } | |
ab9d47e9 | 3158 | break; |
1da177e4 | 3159 | } |
ab9d47e9 | 3160 | } |
1da177e4 LT |
3161 | if (biolist == NULL) { |
3162 | while (r10_bio) { | |
9f2c9d12 N |
3163 | struct r10bio *rb2 = r10_bio; |
3164 | r10_bio = (struct r10bio*) rb2->master_bio; | |
1da177e4 LT |
3165 | rb2->master_bio = NULL; |
3166 | put_buf(rb2); | |
3167 | } | |
3168 | goto giveup; | |
3169 | } | |
3170 | } else { | |
3171 | /* resync. Schedule a read for every block at this virt offset */ | |
3172 | int count = 0; | |
6cce3b23 | 3173 | |
78200d45 N |
3174 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
3175 | ||
6cce3b23 N |
3176 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, |
3177 | &sync_blocks, mddev->degraded) && | |
ab9d47e9 N |
3178 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, |
3179 | &mddev->recovery)) { | |
6cce3b23 N |
3180 | /* We can skip this block */ |
3181 | *skipped = 1; | |
3182 | return sync_blocks + sectors_skipped; | |
3183 | } | |
3184 | if (sync_blocks < max_sync) | |
3185 | max_sync = sync_blocks; | |
1da177e4 LT |
3186 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
3187 | ||
1da177e4 LT |
3188 | r10_bio->mddev = mddev; |
3189 | atomic_set(&r10_bio->remaining, 0); | |
6cce3b23 N |
3190 | raise_barrier(conf, 0); |
3191 | conf->next_resync = sector_nr; | |
1da177e4 LT |
3192 | |
3193 | r10_bio->master_bio = NULL; | |
3194 | r10_bio->sector = sector_nr; | |
3195 | set_bit(R10BIO_IsSync, &r10_bio->state); | |
3196 | raid10_find_phys(conf, r10_bio); | |
5cf00fcd | 3197 | r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1; |
1da177e4 | 3198 | |
5cf00fcd | 3199 | for (i = 0; i < conf->copies; i++) { |
1da177e4 | 3200 | int d = r10_bio->devs[i].devnum; |
40c356ce N |
3201 | sector_t first_bad, sector; |
3202 | int bad_sectors; | |
3203 | ||
9ad1aefc N |
3204 | if (r10_bio->devs[i].repl_bio) |
3205 | r10_bio->devs[i].repl_bio->bi_end_io = NULL; | |
3206 | ||
1da177e4 LT |
3207 | bio = r10_bio->devs[i].bio; |
3208 | bio->bi_end_io = NULL; | |
af03b8e4 | 3209 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
1da177e4 | 3210 | if (conf->mirrors[d].rdev == NULL || |
b2d444d7 | 3211 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) |
1da177e4 | 3212 | continue; |
40c356ce N |
3213 | sector = r10_bio->devs[i].addr; |
3214 | if (is_badblock(conf->mirrors[d].rdev, | |
3215 | sector, max_sync, | |
3216 | &first_bad, &bad_sectors)) { | |
3217 | if (first_bad > sector) | |
3218 | max_sync = first_bad - sector; | |
3219 | else { | |
3220 | bad_sectors -= (sector - first_bad); | |
3221 | if (max_sync > bad_sectors) | |
91502f09 | 3222 | max_sync = bad_sectors; |
40c356ce N |
3223 | continue; |
3224 | } | |
3225 | } | |
1da177e4 LT |
3226 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
3227 | atomic_inc(&r10_bio->remaining); | |
3228 | bio->bi_next = biolist; | |
3229 | biolist = bio; | |
3230 | bio->bi_private = r10_bio; | |
3231 | bio->bi_end_io = end_sync_read; | |
802ba064 | 3232 | bio->bi_rw = READ; |
40c356ce | 3233 | bio->bi_sector = sector + |
1da177e4 LT |
3234 | conf->mirrors[d].rdev->data_offset; |
3235 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; | |
3236 | count++; | |
9ad1aefc N |
3237 | |
3238 | if (conf->mirrors[d].replacement == NULL || | |
3239 | test_bit(Faulty, | |
3240 | &conf->mirrors[d].replacement->flags)) | |
3241 | continue; | |
3242 | ||
3243 | /* Need to set up for writing to the replacement */ | |
3244 | bio = r10_bio->devs[i].repl_bio; | |
3245 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | |
3246 | ||
3247 | sector = r10_bio->devs[i].addr; | |
3248 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); | |
3249 | bio->bi_next = biolist; | |
3250 | biolist = bio; | |
3251 | bio->bi_private = r10_bio; | |
3252 | bio->bi_end_io = end_sync_write; | |
3253 | bio->bi_rw = WRITE; | |
3254 | bio->bi_sector = sector + | |
3255 | conf->mirrors[d].replacement->data_offset; | |
3256 | bio->bi_bdev = conf->mirrors[d].replacement->bdev; | |
3257 | count++; | |
1da177e4 LT |
3258 | } |
3259 | ||
3260 | if (count < 2) { | |
3261 | for (i=0; i<conf->copies; i++) { | |
3262 | int d = r10_bio->devs[i].devnum; | |
3263 | if (r10_bio->devs[i].bio->bi_end_io) | |
ab9d47e9 N |
3264 | rdev_dec_pending(conf->mirrors[d].rdev, |
3265 | mddev); | |
9ad1aefc N |
3266 | if (r10_bio->devs[i].repl_bio && |
3267 | r10_bio->devs[i].repl_bio->bi_end_io) | |
3268 | rdev_dec_pending( | |
3269 | conf->mirrors[d].replacement, | |
3270 | mddev); | |
1da177e4 LT |
3271 | } |
3272 | put_buf(r10_bio); | |
3273 | biolist = NULL; | |
3274 | goto giveup; | |
3275 | } | |
3276 | } | |
3277 | ||
3278 | for (bio = biolist; bio ; bio=bio->bi_next) { | |
3279 | ||
3280 | bio->bi_flags &= ~(BIO_POOL_MASK - 1); | |
3281 | if (bio->bi_end_io) | |
3282 | bio->bi_flags |= 1 << BIO_UPTODATE; | |
3283 | bio->bi_vcnt = 0; | |
3284 | bio->bi_idx = 0; | |
3285 | bio->bi_phys_segments = 0; | |
1da177e4 LT |
3286 | bio->bi_size = 0; |
3287 | } | |
3288 | ||
3289 | nr_sectors = 0; | |
6cce3b23 N |
3290 | if (sector_nr + max_sync < max_sector) |
3291 | max_sector = sector_nr + max_sync; | |
1da177e4 LT |
3292 | do { |
3293 | struct page *page; | |
3294 | int len = PAGE_SIZE; | |
1da177e4 LT |
3295 | if (sector_nr + (len>>9) > max_sector) |
3296 | len = (max_sector - sector_nr) << 9; | |
3297 | if (len == 0) | |
3298 | break; | |
3299 | for (bio= biolist ; bio ; bio=bio->bi_next) { | |
ab9d47e9 | 3300 | struct bio *bio2; |
1da177e4 | 3301 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; |
ab9d47e9 N |
3302 | if (bio_add_page(bio, page, len, 0)) |
3303 | continue; | |
3304 | ||
3305 | /* stop here */ | |
3306 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; | |
3307 | for (bio2 = biolist; | |
3308 | bio2 && bio2 != bio; | |
3309 | bio2 = bio2->bi_next) { | |
3310 | /* remove last page from this bio */ | |
3311 | bio2->bi_vcnt--; | |
3312 | bio2->bi_size -= len; | |
3313 | bio2->bi_flags &= ~(1<< BIO_SEG_VALID); | |
1da177e4 | 3314 | } |
ab9d47e9 | 3315 | goto bio_full; |
1da177e4 LT |
3316 | } |
3317 | nr_sectors += len>>9; | |
3318 | sector_nr += len>>9; | |
3319 | } while (biolist->bi_vcnt < RESYNC_PAGES); | |
3320 | bio_full: | |
3321 | r10_bio->sectors = nr_sectors; | |
3322 | ||
3323 | while (biolist) { | |
3324 | bio = biolist; | |
3325 | biolist = biolist->bi_next; | |
3326 | ||
3327 | bio->bi_next = NULL; | |
3328 | r10_bio = bio->bi_private; | |
3329 | r10_bio->sectors = nr_sectors; | |
3330 | ||
3331 | if (bio->bi_end_io == end_sync_read) { | |
3332 | md_sync_acct(bio->bi_bdev, nr_sectors); | |
3333 | generic_make_request(bio); | |
3334 | } | |
3335 | } | |
3336 | ||
57afd89f N |
3337 | if (sectors_skipped) |
3338 | /* pretend they weren't skipped, it makes | |
3339 | * no important difference in this case | |
3340 | */ | |
3341 | md_done_sync(mddev, sectors_skipped, 1); | |
3342 | ||
1da177e4 LT |
3343 | return sectors_skipped + nr_sectors; |
3344 | giveup: | |
3345 | /* There is nowhere to write, so all non-sync | |
e875ecea N |
3346 | * drives must be failed or in resync, all drives |
3347 | * have a bad block, so try the next chunk... | |
1da177e4 | 3348 | */ |
09b4068a N |
3349 | if (sector_nr + max_sync < max_sector) |
3350 | max_sector = sector_nr + max_sync; | |
3351 | ||
3352 | sectors_skipped += (max_sector - sector_nr); | |
1da177e4 LT |
3353 | chunks_skipped ++; |
3354 | sector_nr = max_sector; | |
1da177e4 | 3355 | goto skipped; |
1da177e4 LT |
3356 | } |
3357 | ||
80c3a6ce | 3358 | static sector_t |
fd01b88c | 3359 | raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
80c3a6ce DW |
3360 | { |
3361 | sector_t size; | |
e879a879 | 3362 | struct r10conf *conf = mddev->private; |
80c3a6ce DW |
3363 | |
3364 | if (!raid_disks) | |
3ea7daa5 N |
3365 | raid_disks = min(conf->geo.raid_disks, |
3366 | conf->prev.raid_disks); | |
80c3a6ce | 3367 | if (!sectors) |
dab8b292 | 3368 | sectors = conf->dev_sectors; |
80c3a6ce | 3369 | |
5cf00fcd N |
3370 | size = sectors >> conf->geo.chunk_shift; |
3371 | sector_div(size, conf->geo.far_copies); | |
80c3a6ce | 3372 | size = size * raid_disks; |
5cf00fcd | 3373 | sector_div(size, conf->geo.near_copies); |
80c3a6ce | 3374 | |
5cf00fcd | 3375 | return size << conf->geo.chunk_shift; |
80c3a6ce DW |
3376 | } |
3377 | ||
6508fdbf N |
3378 | static void calc_sectors(struct r10conf *conf, sector_t size) |
3379 | { | |
3380 | /* Calculate the number of sectors-per-device that will | |
3381 | * actually be used, and set conf->dev_sectors and | |
3382 | * conf->stride | |
3383 | */ | |
3384 | ||
5cf00fcd N |
3385 | size = size >> conf->geo.chunk_shift; |
3386 | sector_div(size, conf->geo.far_copies); | |
3387 | size = size * conf->geo.raid_disks; | |
3388 | sector_div(size, conf->geo.near_copies); | |
6508fdbf N |
3389 | /* 'size' is now the number of chunks in the array */ |
3390 | /* calculate "used chunks per device" */ | |
3391 | size = size * conf->copies; | |
3392 | ||
3393 | /* We need to round up when dividing by raid_disks to | |
3394 | * get the stride size. | |
3395 | */ | |
5cf00fcd | 3396 | size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks); |
6508fdbf | 3397 | |
5cf00fcd | 3398 | conf->dev_sectors = size << conf->geo.chunk_shift; |
6508fdbf | 3399 | |
5cf00fcd N |
3400 | if (conf->geo.far_offset) |
3401 | conf->geo.stride = 1 << conf->geo.chunk_shift; | |
6508fdbf | 3402 | else { |
5cf00fcd N |
3403 | sector_div(size, conf->geo.far_copies); |
3404 | conf->geo.stride = size << conf->geo.chunk_shift; | |
6508fdbf N |
3405 | } |
3406 | } | |
dab8b292 | 3407 | |
deb200d0 N |
3408 | enum geo_type {geo_new, geo_old, geo_start}; |
3409 | static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new) | |
3410 | { | |
3411 | int nc, fc, fo; | |
3412 | int layout, chunk, disks; | |
3413 | switch (new) { | |
3414 | case geo_old: | |
3415 | layout = mddev->layout; | |
3416 | chunk = mddev->chunk_sectors; | |
3417 | disks = mddev->raid_disks - mddev->delta_disks; | |
3418 | break; | |
3419 | case geo_new: | |
3420 | layout = mddev->new_layout; | |
3421 | chunk = mddev->new_chunk_sectors; | |
3422 | disks = mddev->raid_disks; | |
3423 | break; | |
3424 | default: /* avoid 'may be unused' warnings */ | |
3425 | case geo_start: /* new when starting reshape - raid_disks not | |
3426 | * updated yet. */ | |
3427 | layout = mddev->new_layout; | |
3428 | chunk = mddev->new_chunk_sectors; | |
3429 | disks = mddev->raid_disks + mddev->delta_disks; | |
3430 | break; | |
3431 | } | |
3432 | if (layout >> 17) | |
3433 | return -1; | |
3434 | if (chunk < (PAGE_SIZE >> 9) || | |
3435 | !is_power_of_2(chunk)) | |
3436 | return -2; | |
3437 | nc = layout & 255; | |
3438 | fc = (layout >> 8) & 255; | |
3439 | fo = layout & (1<<16); | |
3440 | geo->raid_disks = disks; | |
3441 | geo->near_copies = nc; | |
3442 | geo->far_copies = fc; | |
3443 | geo->far_offset = fo; | |
3444 | geo->chunk_mask = chunk - 1; | |
3445 | geo->chunk_shift = ffz(~chunk); | |
3446 | return nc*fc; | |
3447 | } | |
3448 | ||
e879a879 | 3449 | static struct r10conf *setup_conf(struct mddev *mddev) |
1da177e4 | 3450 | { |
e879a879 | 3451 | struct r10conf *conf = NULL; |
dab8b292 | 3452 | int err = -EINVAL; |
deb200d0 N |
3453 | struct geom geo; |
3454 | int copies; | |
3455 | ||
3456 | copies = setup_geo(&geo, mddev, geo_new); | |
1da177e4 | 3457 | |
deb200d0 | 3458 | if (copies == -2) { |
128595ed N |
3459 | printk(KERN_ERR "md/raid10:%s: chunk size must be " |
3460 | "at least PAGE_SIZE(%ld) and be a power of 2.\n", | |
3461 | mdname(mddev), PAGE_SIZE); | |
dab8b292 | 3462 | goto out; |
1da177e4 | 3463 | } |
2604b703 | 3464 | |
deb200d0 | 3465 | if (copies < 2 || copies > mddev->raid_disks) { |
128595ed | 3466 | printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n", |
f73ea873 | 3467 | mdname(mddev), mddev->new_layout); |
1da177e4 LT |
3468 | goto out; |
3469 | } | |
dab8b292 TM |
3470 | |
3471 | err = -ENOMEM; | |
e879a879 | 3472 | conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); |
dab8b292 | 3473 | if (!conf) |
1da177e4 | 3474 | goto out; |
dab8b292 | 3475 | |
3ea7daa5 | 3476 | /* FIXME calc properly */ |
dc280d98 | 3477 | conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks + |
3ea7daa5 | 3478 | max(0,mddev->delta_disks)), |
dab8b292 TM |
3479 | GFP_KERNEL); |
3480 | if (!conf->mirrors) | |
3481 | goto out; | |
4443ae10 N |
3482 | |
3483 | conf->tmppage = alloc_page(GFP_KERNEL); | |
3484 | if (!conf->tmppage) | |
dab8b292 TM |
3485 | goto out; |
3486 | ||
deb200d0 N |
3487 | conf->geo = geo; |
3488 | conf->copies = copies; | |
dab8b292 TM |
3489 | conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, |
3490 | r10bio_pool_free, conf); | |
3491 | if (!conf->r10bio_pool) | |
3492 | goto out; | |
3493 | ||
6508fdbf | 3494 | calc_sectors(conf, mddev->dev_sectors); |
3ea7daa5 N |
3495 | if (mddev->reshape_position == MaxSector) { |
3496 | conf->prev = conf->geo; | |
3497 | conf->reshape_progress = MaxSector; | |
3498 | } else { | |
3499 | if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) { | |
3500 | err = -EINVAL; | |
3501 | goto out; | |
3502 | } | |
3503 | conf->reshape_progress = mddev->reshape_position; | |
3504 | if (conf->prev.far_offset) | |
3505 | conf->prev.stride = 1 << conf->prev.chunk_shift; | |
3506 | else | |
3507 | /* far_copies must be 1 */ | |
3508 | conf->prev.stride = conf->dev_sectors; | |
3509 | } | |
e7e72bf6 | 3510 | spin_lock_init(&conf->device_lock); |
dab8b292 TM |
3511 | INIT_LIST_HEAD(&conf->retry_list); |
3512 | ||
3513 | spin_lock_init(&conf->resync_lock); | |
3514 | init_waitqueue_head(&conf->wait_barrier); | |
3515 | ||
0232605d | 3516 | conf->thread = md_register_thread(raid10d, mddev, "raid10"); |
dab8b292 TM |
3517 | if (!conf->thread) |
3518 | goto out; | |
3519 | ||
dab8b292 TM |
3520 | conf->mddev = mddev; |
3521 | return conf; | |
3522 | ||
3523 | out: | |
3ea7daa5 N |
3524 | if (err == -ENOMEM) |
3525 | printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n", | |
3526 | mdname(mddev)); | |
dab8b292 TM |
3527 | if (conf) { |
3528 | if (conf->r10bio_pool) | |
3529 | mempool_destroy(conf->r10bio_pool); | |
3530 | kfree(conf->mirrors); | |
3531 | safe_put_page(conf->tmppage); | |
3532 | kfree(conf); | |
3533 | } | |
3534 | return ERR_PTR(err); | |
3535 | } | |
3536 | ||
fd01b88c | 3537 | static int run(struct mddev *mddev) |
dab8b292 | 3538 | { |
e879a879 | 3539 | struct r10conf *conf; |
dab8b292 | 3540 | int i, disk_idx, chunk_size; |
dc280d98 | 3541 | struct raid10_info *disk; |
3cb03002 | 3542 | struct md_rdev *rdev; |
dab8b292 | 3543 | sector_t size; |
3ea7daa5 N |
3544 | sector_t min_offset_diff = 0; |
3545 | int first = 1; | |
532a2a3f | 3546 | bool discard_supported = false; |
dab8b292 TM |
3547 | |
3548 | if (mddev->private == NULL) { | |
3549 | conf = setup_conf(mddev); | |
3550 | if (IS_ERR(conf)) | |
3551 | return PTR_ERR(conf); | |
3552 | mddev->private = conf; | |
3553 | } | |
3554 | conf = mddev->private; | |
3555 | if (!conf) | |
3556 | goto out; | |
3557 | ||
dab8b292 TM |
3558 | mddev->thread = conf->thread; |
3559 | conf->thread = NULL; | |
3560 | ||
8f6c2e4b | 3561 | chunk_size = mddev->chunk_sectors << 9; |
cc4d1efd | 3562 | if (mddev->queue) { |
532a2a3f SL |
3563 | blk_queue_max_discard_sectors(mddev->queue, |
3564 | mddev->chunk_sectors); | |
cc4d1efd JB |
3565 | blk_queue_io_min(mddev->queue, chunk_size); |
3566 | if (conf->geo.raid_disks % conf->geo.near_copies) | |
3567 | blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks); | |
3568 | else | |
3569 | blk_queue_io_opt(mddev->queue, chunk_size * | |
3570 | (conf->geo.raid_disks / conf->geo.near_copies)); | |
3571 | } | |
8f6c2e4b | 3572 | |
dafb20fa | 3573 | rdev_for_each(rdev, mddev) { |
3ea7daa5 | 3574 | long long diff; |
aba336bd | 3575 | struct request_queue *q; |
34b343cf | 3576 | |
1da177e4 | 3577 | disk_idx = rdev->raid_disk; |
f8c9e74f N |
3578 | if (disk_idx < 0) |
3579 | continue; | |
3580 | if (disk_idx >= conf->geo.raid_disks && | |
3581 | disk_idx >= conf->prev.raid_disks) | |
1da177e4 LT |
3582 | continue; |
3583 | disk = conf->mirrors + disk_idx; | |
3584 | ||
56a2559b N |
3585 | if (test_bit(Replacement, &rdev->flags)) { |
3586 | if (disk->replacement) | |
3587 | goto out_free_conf; | |
3588 | disk->replacement = rdev; | |
3589 | } else { | |
3590 | if (disk->rdev) | |
3591 | goto out_free_conf; | |
3592 | disk->rdev = rdev; | |
3593 | } | |
aba336bd N |
3594 | q = bdev_get_queue(rdev->bdev); |
3595 | if (q->merge_bvec_fn) | |
3596 | mddev->merge_check_needed = 1; | |
3ea7daa5 N |
3597 | diff = (rdev->new_data_offset - rdev->data_offset); |
3598 | if (!mddev->reshape_backwards) | |
3599 | diff = -diff; | |
3600 | if (diff < 0) | |
3601 | diff = 0; | |
3602 | if (first || diff < min_offset_diff) | |
3603 | min_offset_diff = diff; | |
56a2559b | 3604 | |
cc4d1efd JB |
3605 | if (mddev->gendisk) |
3606 | disk_stack_limits(mddev->gendisk, rdev->bdev, | |
3607 | rdev->data_offset << 9); | |
1da177e4 LT |
3608 | |
3609 | disk->head_position = 0; | |
532a2a3f SL |
3610 | |
3611 | if (blk_queue_discard(bdev_get_queue(rdev->bdev))) | |
3612 | discard_supported = true; | |
1da177e4 | 3613 | } |
3ea7daa5 | 3614 | |
532a2a3f SL |
3615 | if (discard_supported) |
3616 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue); | |
3617 | else | |
3618 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, mddev->queue); | |
3619 | ||
6d508242 | 3620 | /* need to check that every block has at least one working mirror */ |
700c7213 | 3621 | if (!enough(conf, -1)) { |
128595ed | 3622 | printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n", |
6d508242 | 3623 | mdname(mddev)); |
1da177e4 LT |
3624 | goto out_free_conf; |
3625 | } | |
3626 | ||
3ea7daa5 N |
3627 | if (conf->reshape_progress != MaxSector) { |
3628 | /* must ensure that shape change is supported */ | |
3629 | if (conf->geo.far_copies != 1 && | |
3630 | conf->geo.far_offset == 0) | |
3631 | goto out_free_conf; | |
3632 | if (conf->prev.far_copies != 1 && | |
3633 | conf->geo.far_offset == 0) | |
3634 | goto out_free_conf; | |
3635 | } | |
3636 | ||
1da177e4 | 3637 | mddev->degraded = 0; |
f8c9e74f N |
3638 | for (i = 0; |
3639 | i < conf->geo.raid_disks | |
3640 | || i < conf->prev.raid_disks; | |
3641 | i++) { | |
1da177e4 LT |
3642 | |
3643 | disk = conf->mirrors + i; | |
3644 | ||
56a2559b N |
3645 | if (!disk->rdev && disk->replacement) { |
3646 | /* The replacement is all we have - use it */ | |
3647 | disk->rdev = disk->replacement; | |
3648 | disk->replacement = NULL; | |
3649 | clear_bit(Replacement, &disk->rdev->flags); | |
3650 | } | |
3651 | ||
5fd6c1dc | 3652 | if (!disk->rdev || |
2e333e89 | 3653 | !test_bit(In_sync, &disk->rdev->flags)) { |
1da177e4 LT |
3654 | disk->head_position = 0; |
3655 | mddev->degraded++; | |
8c2e870a NB |
3656 | if (disk->rdev) |
3657 | conf->fullsync = 1; | |
1da177e4 | 3658 | } |
d890fa2b | 3659 | disk->recovery_disabled = mddev->recovery_disabled - 1; |
1da177e4 LT |
3660 | } |
3661 | ||
8c6ac868 | 3662 | if (mddev->recovery_cp != MaxSector) |
128595ed | 3663 | printk(KERN_NOTICE "md/raid10:%s: not clean" |
8c6ac868 AN |
3664 | " -- starting background reconstruction\n", |
3665 | mdname(mddev)); | |
1da177e4 | 3666 | printk(KERN_INFO |
128595ed | 3667 | "md/raid10:%s: active with %d out of %d devices\n", |
5cf00fcd N |
3668 | mdname(mddev), conf->geo.raid_disks - mddev->degraded, |
3669 | conf->geo.raid_disks); | |
1da177e4 LT |
3670 | /* |
3671 | * Ok, everything is just fine now | |
3672 | */ | |
dab8b292 TM |
3673 | mddev->dev_sectors = conf->dev_sectors; |
3674 | size = raid10_size(mddev, 0, 0); | |
3675 | md_set_array_sectors(mddev, size); | |
3676 | mddev->resync_max_sectors = size; | |
1da177e4 | 3677 | |
cc4d1efd | 3678 | if (mddev->queue) { |
5cf00fcd | 3679 | int stripe = conf->geo.raid_disks * |
9d8f0363 | 3680 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
cc4d1efd JB |
3681 | mddev->queue->backing_dev_info.congested_fn = raid10_congested; |
3682 | mddev->queue->backing_dev_info.congested_data = mddev; | |
3683 | ||
3684 | /* Calculate max read-ahead size. | |
3685 | * We need to readahead at least twice a whole stripe.... | |
3686 | * maybe... | |
3687 | */ | |
5cf00fcd | 3688 | stripe /= conf->geo.near_copies; |
3ea7daa5 N |
3689 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
3690 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
cc4d1efd | 3691 | blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec); |
1da177e4 LT |
3692 | } |
3693 | ||
a91a2785 MP |
3694 | |
3695 | if (md_integrity_register(mddev)) | |
3696 | goto out_free_conf; | |
3697 | ||
3ea7daa5 N |
3698 | if (conf->reshape_progress != MaxSector) { |
3699 | unsigned long before_length, after_length; | |
3700 | ||
3701 | before_length = ((1 << conf->prev.chunk_shift) * | |
3702 | conf->prev.far_copies); | |
3703 | after_length = ((1 << conf->geo.chunk_shift) * | |
3704 | conf->geo.far_copies); | |
3705 | ||
3706 | if (max(before_length, after_length) > min_offset_diff) { | |
3707 | /* This cannot work */ | |
3708 | printk("md/raid10: offset difference not enough to continue reshape\n"); | |
3709 | goto out_free_conf; | |
3710 | } | |
3711 | conf->offset_diff = min_offset_diff; | |
3712 | ||
3713 | conf->reshape_safe = conf->reshape_progress; | |
3714 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
3715 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
3716 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
3717 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
3718 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
3719 | "reshape"); | |
3720 | } | |
3721 | ||
1da177e4 LT |
3722 | return 0; |
3723 | ||
3724 | out_free_conf: | |
01f96c0a | 3725 | md_unregister_thread(&mddev->thread); |
1da177e4 LT |
3726 | if (conf->r10bio_pool) |
3727 | mempool_destroy(conf->r10bio_pool); | |
1345b1d8 | 3728 | safe_put_page(conf->tmppage); |
990a8baf | 3729 | kfree(conf->mirrors); |
1da177e4 LT |
3730 | kfree(conf); |
3731 | mddev->private = NULL; | |
3732 | out: | |
3733 | return -EIO; | |
3734 | } | |
3735 | ||
fd01b88c | 3736 | static int stop(struct mddev *mddev) |
1da177e4 | 3737 | { |
e879a879 | 3738 | struct r10conf *conf = mddev->private; |
1da177e4 | 3739 | |
409c57f3 N |
3740 | raise_barrier(conf, 0); |
3741 | lower_barrier(conf); | |
3742 | ||
01f96c0a | 3743 | md_unregister_thread(&mddev->thread); |
cc4d1efd JB |
3744 | if (mddev->queue) |
3745 | /* the unplug fn references 'conf'*/ | |
3746 | blk_sync_queue(mddev->queue); | |
3747 | ||
1da177e4 LT |
3748 | if (conf->r10bio_pool) |
3749 | mempool_destroy(conf->r10bio_pool); | |
990a8baf | 3750 | kfree(conf->mirrors); |
1da177e4 LT |
3751 | kfree(conf); |
3752 | mddev->private = NULL; | |
3753 | return 0; | |
3754 | } | |
3755 | ||
fd01b88c | 3756 | static void raid10_quiesce(struct mddev *mddev, int state) |
6cce3b23 | 3757 | { |
e879a879 | 3758 | struct r10conf *conf = mddev->private; |
6cce3b23 N |
3759 | |
3760 | switch(state) { | |
3761 | case 1: | |
3762 | raise_barrier(conf, 0); | |
3763 | break; | |
3764 | case 0: | |
3765 | lower_barrier(conf); | |
3766 | break; | |
3767 | } | |
6cce3b23 | 3768 | } |
1da177e4 | 3769 | |
006a09a0 N |
3770 | static int raid10_resize(struct mddev *mddev, sector_t sectors) |
3771 | { | |
3772 | /* Resize of 'far' arrays is not supported. | |
3773 | * For 'near' and 'offset' arrays we can set the | |
3774 | * number of sectors used to be an appropriate multiple | |
3775 | * of the chunk size. | |
3776 | * For 'offset', this is far_copies*chunksize. | |
3777 | * For 'near' the multiplier is the LCM of | |
3778 | * near_copies and raid_disks. | |
3779 | * So if far_copies > 1 && !far_offset, fail. | |
3780 | * Else find LCM(raid_disks, near_copy)*far_copies and | |
3781 | * multiply by chunk_size. Then round to this number. | |
3782 | * This is mostly done by raid10_size() | |
3783 | */ | |
3784 | struct r10conf *conf = mddev->private; | |
3785 | sector_t oldsize, size; | |
3786 | ||
f8c9e74f N |
3787 | if (mddev->reshape_position != MaxSector) |
3788 | return -EBUSY; | |
3789 | ||
5cf00fcd | 3790 | if (conf->geo.far_copies > 1 && !conf->geo.far_offset) |
006a09a0 N |
3791 | return -EINVAL; |
3792 | ||
3793 | oldsize = raid10_size(mddev, 0, 0); | |
3794 | size = raid10_size(mddev, sectors, 0); | |
a4a6125a N |
3795 | if (mddev->external_size && |
3796 | mddev->array_sectors > size) | |
006a09a0 | 3797 | return -EINVAL; |
a4a6125a N |
3798 | if (mddev->bitmap) { |
3799 | int ret = bitmap_resize(mddev->bitmap, size, 0, 0); | |
3800 | if (ret) | |
3801 | return ret; | |
3802 | } | |
3803 | md_set_array_sectors(mddev, size); | |
006a09a0 N |
3804 | set_capacity(mddev->gendisk, mddev->array_sectors); |
3805 | revalidate_disk(mddev->gendisk); | |
3806 | if (sectors > mddev->dev_sectors && | |
3807 | mddev->recovery_cp > oldsize) { | |
3808 | mddev->recovery_cp = oldsize; | |
3809 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
3810 | } | |
6508fdbf N |
3811 | calc_sectors(conf, sectors); |
3812 | mddev->dev_sectors = conf->dev_sectors; | |
006a09a0 N |
3813 | mddev->resync_max_sectors = size; |
3814 | return 0; | |
3815 | } | |
3816 | ||
fd01b88c | 3817 | static void *raid10_takeover_raid0(struct mddev *mddev) |
dab8b292 | 3818 | { |
3cb03002 | 3819 | struct md_rdev *rdev; |
e879a879 | 3820 | struct r10conf *conf; |
dab8b292 TM |
3821 | |
3822 | if (mddev->degraded > 0) { | |
128595ed N |
3823 | printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n", |
3824 | mdname(mddev)); | |
dab8b292 TM |
3825 | return ERR_PTR(-EINVAL); |
3826 | } | |
3827 | ||
dab8b292 TM |
3828 | /* Set new parameters */ |
3829 | mddev->new_level = 10; | |
3830 | /* new layout: far_copies = 1, near_copies = 2 */ | |
3831 | mddev->new_layout = (1<<8) + 2; | |
3832 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
3833 | mddev->delta_disks = mddev->raid_disks; | |
dab8b292 TM |
3834 | mddev->raid_disks *= 2; |
3835 | /* make sure it will be not marked as dirty */ | |
3836 | mddev->recovery_cp = MaxSector; | |
3837 | ||
3838 | conf = setup_conf(mddev); | |
02214dc5 | 3839 | if (!IS_ERR(conf)) { |
dafb20fa | 3840 | rdev_for_each(rdev, mddev) |
e93f68a1 N |
3841 | if (rdev->raid_disk >= 0) |
3842 | rdev->new_raid_disk = rdev->raid_disk * 2; | |
02214dc5 KW |
3843 | conf->barrier = 1; |
3844 | } | |
3845 | ||
dab8b292 TM |
3846 | return conf; |
3847 | } | |
3848 | ||
fd01b88c | 3849 | static void *raid10_takeover(struct mddev *mddev) |
dab8b292 | 3850 | { |
e373ab10 | 3851 | struct r0conf *raid0_conf; |
dab8b292 TM |
3852 | |
3853 | /* raid10 can take over: | |
3854 | * raid0 - providing it has only two drives | |
3855 | */ | |
3856 | if (mddev->level == 0) { | |
3857 | /* for raid0 takeover only one zone is supported */ | |
e373ab10 N |
3858 | raid0_conf = mddev->private; |
3859 | if (raid0_conf->nr_strip_zones > 1) { | |
128595ed N |
3860 | printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0" |
3861 | " with more than one zone.\n", | |
3862 | mdname(mddev)); | |
dab8b292 TM |
3863 | return ERR_PTR(-EINVAL); |
3864 | } | |
3865 | return raid10_takeover_raid0(mddev); | |
3866 | } | |
3867 | return ERR_PTR(-EINVAL); | |
3868 | } | |
3869 | ||
3ea7daa5 N |
3870 | static int raid10_check_reshape(struct mddev *mddev) |
3871 | { | |
3872 | /* Called when there is a request to change | |
3873 | * - layout (to ->new_layout) | |
3874 | * - chunk size (to ->new_chunk_sectors) | |
3875 | * - raid_disks (by delta_disks) | |
3876 | * or when trying to restart a reshape that was ongoing. | |
3877 | * | |
3878 | * We need to validate the request and possibly allocate | |
3879 | * space if that might be an issue later. | |
3880 | * | |
3881 | * Currently we reject any reshape of a 'far' mode array, | |
3882 | * allow chunk size to change if new is generally acceptable, | |
3883 | * allow raid_disks to increase, and allow | |
3884 | * a switch between 'near' mode and 'offset' mode. | |
3885 | */ | |
3886 | struct r10conf *conf = mddev->private; | |
3887 | struct geom geo; | |
3888 | ||
3889 | if (conf->geo.far_copies != 1 && !conf->geo.far_offset) | |
3890 | return -EINVAL; | |
3891 | ||
3892 | if (setup_geo(&geo, mddev, geo_start) != conf->copies) | |
3893 | /* mustn't change number of copies */ | |
3894 | return -EINVAL; | |
3895 | if (geo.far_copies > 1 && !geo.far_offset) | |
3896 | /* Cannot switch to 'far' mode */ | |
3897 | return -EINVAL; | |
3898 | ||
3899 | if (mddev->array_sectors & geo.chunk_mask) | |
3900 | /* not factor of array size */ | |
3901 | return -EINVAL; | |
3902 | ||
3ea7daa5 N |
3903 | if (!enough(conf, -1)) |
3904 | return -EINVAL; | |
3905 | ||
3906 | kfree(conf->mirrors_new); | |
3907 | conf->mirrors_new = NULL; | |
3908 | if (mddev->delta_disks > 0) { | |
3909 | /* allocate new 'mirrors' list */ | |
3910 | conf->mirrors_new = kzalloc( | |
dc280d98 | 3911 | sizeof(struct raid10_info) |
3ea7daa5 N |
3912 | *(mddev->raid_disks + |
3913 | mddev->delta_disks), | |
3914 | GFP_KERNEL); | |
3915 | if (!conf->mirrors_new) | |
3916 | return -ENOMEM; | |
3917 | } | |
3918 | return 0; | |
3919 | } | |
3920 | ||
3921 | /* | |
3922 | * Need to check if array has failed when deciding whether to: | |
3923 | * - start an array | |
3924 | * - remove non-faulty devices | |
3925 | * - add a spare | |
3926 | * - allow a reshape | |
3927 | * This determination is simple when no reshape is happening. | |
3928 | * However if there is a reshape, we need to carefully check | |
3929 | * both the before and after sections. | |
3930 | * This is because some failed devices may only affect one | |
3931 | * of the two sections, and some non-in_sync devices may | |
3932 | * be insync in the section most affected by failed devices. | |
3933 | */ | |
3934 | static int calc_degraded(struct r10conf *conf) | |
3935 | { | |
3936 | int degraded, degraded2; | |
3937 | int i; | |
3938 | ||
3939 | rcu_read_lock(); | |
3940 | degraded = 0; | |
3941 | /* 'prev' section first */ | |
3942 | for (i = 0; i < conf->prev.raid_disks; i++) { | |
3943 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); | |
3944 | if (!rdev || test_bit(Faulty, &rdev->flags)) | |
3945 | degraded++; | |
3946 | else if (!test_bit(In_sync, &rdev->flags)) | |
3947 | /* When we can reduce the number of devices in | |
3948 | * an array, this might not contribute to | |
3949 | * 'degraded'. It does now. | |
3950 | */ | |
3951 | degraded++; | |
3952 | } | |
3953 | rcu_read_unlock(); | |
3954 | if (conf->geo.raid_disks == conf->prev.raid_disks) | |
3955 | return degraded; | |
3956 | rcu_read_lock(); | |
3957 | degraded2 = 0; | |
3958 | for (i = 0; i < conf->geo.raid_disks; i++) { | |
3959 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); | |
3960 | if (!rdev || test_bit(Faulty, &rdev->flags)) | |
3961 | degraded2++; | |
3962 | else if (!test_bit(In_sync, &rdev->flags)) { | |
3963 | /* If reshape is increasing the number of devices, | |
3964 | * this section has already been recovered, so | |
3965 | * it doesn't contribute to degraded. | |
3966 | * else it does. | |
3967 | */ | |
3968 | if (conf->geo.raid_disks <= conf->prev.raid_disks) | |
3969 | degraded2++; | |
3970 | } | |
3971 | } | |
3972 | rcu_read_unlock(); | |
3973 | if (degraded2 > degraded) | |
3974 | return degraded2; | |
3975 | return degraded; | |
3976 | } | |
3977 | ||
3978 | static int raid10_start_reshape(struct mddev *mddev) | |
3979 | { | |
3980 | /* A 'reshape' has been requested. This commits | |
3981 | * the various 'new' fields and sets MD_RECOVER_RESHAPE | |
3982 | * This also checks if there are enough spares and adds them | |
3983 | * to the array. | |
3984 | * We currently require enough spares to make the final | |
3985 | * array non-degraded. We also require that the difference | |
3986 | * between old and new data_offset - on each device - is | |
3987 | * enough that we never risk over-writing. | |
3988 | */ | |
3989 | ||
3990 | unsigned long before_length, after_length; | |
3991 | sector_t min_offset_diff = 0; | |
3992 | int first = 1; | |
3993 | struct geom new; | |
3994 | struct r10conf *conf = mddev->private; | |
3995 | struct md_rdev *rdev; | |
3996 | int spares = 0; | |
bb63a701 | 3997 | int ret; |
3ea7daa5 N |
3998 | |
3999 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) | |
4000 | return -EBUSY; | |
4001 | ||
4002 | if (setup_geo(&new, mddev, geo_start) != conf->copies) | |
4003 | return -EINVAL; | |
4004 | ||
4005 | before_length = ((1 << conf->prev.chunk_shift) * | |
4006 | conf->prev.far_copies); | |
4007 | after_length = ((1 << conf->geo.chunk_shift) * | |
4008 | conf->geo.far_copies); | |
4009 | ||
4010 | rdev_for_each(rdev, mddev) { | |
4011 | if (!test_bit(In_sync, &rdev->flags) | |
4012 | && !test_bit(Faulty, &rdev->flags)) | |
4013 | spares++; | |
4014 | if (rdev->raid_disk >= 0) { | |
4015 | long long diff = (rdev->new_data_offset | |
4016 | - rdev->data_offset); | |
4017 | if (!mddev->reshape_backwards) | |
4018 | diff = -diff; | |
4019 | if (diff < 0) | |
4020 | diff = 0; | |
4021 | if (first || diff < min_offset_diff) | |
4022 | min_offset_diff = diff; | |
4023 | } | |
4024 | } | |
4025 | ||
4026 | if (max(before_length, after_length) > min_offset_diff) | |
4027 | return -EINVAL; | |
4028 | ||
4029 | if (spares < mddev->delta_disks) | |
4030 | return -EINVAL; | |
4031 | ||
4032 | conf->offset_diff = min_offset_diff; | |
4033 | spin_lock_irq(&conf->device_lock); | |
4034 | if (conf->mirrors_new) { | |
4035 | memcpy(conf->mirrors_new, conf->mirrors, | |
dc280d98 | 4036 | sizeof(struct raid10_info)*conf->prev.raid_disks); |
3ea7daa5 N |
4037 | smp_mb(); |
4038 | kfree(conf->mirrors_old); /* FIXME and elsewhere */ | |
4039 | conf->mirrors_old = conf->mirrors; | |
4040 | conf->mirrors = conf->mirrors_new; | |
4041 | conf->mirrors_new = NULL; | |
4042 | } | |
4043 | setup_geo(&conf->geo, mddev, geo_start); | |
4044 | smp_mb(); | |
4045 | if (mddev->reshape_backwards) { | |
4046 | sector_t size = raid10_size(mddev, 0, 0); | |
4047 | if (size < mddev->array_sectors) { | |
4048 | spin_unlock_irq(&conf->device_lock); | |
4049 | printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n", | |
4050 | mdname(mddev)); | |
4051 | return -EINVAL; | |
4052 | } | |
4053 | mddev->resync_max_sectors = size; | |
4054 | conf->reshape_progress = size; | |
4055 | } else | |
4056 | conf->reshape_progress = 0; | |
4057 | spin_unlock_irq(&conf->device_lock); | |
4058 | ||
bb63a701 N |
4059 | if (mddev->delta_disks && mddev->bitmap) { |
4060 | ret = bitmap_resize(mddev->bitmap, | |
4061 | raid10_size(mddev, 0, | |
4062 | conf->geo.raid_disks), | |
4063 | 0, 0); | |
4064 | if (ret) | |
4065 | goto abort; | |
4066 | } | |
3ea7daa5 N |
4067 | if (mddev->delta_disks > 0) { |
4068 | rdev_for_each(rdev, mddev) | |
4069 | if (rdev->raid_disk < 0 && | |
4070 | !test_bit(Faulty, &rdev->flags)) { | |
4071 | if (raid10_add_disk(mddev, rdev) == 0) { | |
4072 | if (rdev->raid_disk >= | |
4073 | conf->prev.raid_disks) | |
4074 | set_bit(In_sync, &rdev->flags); | |
4075 | else | |
4076 | rdev->recovery_offset = 0; | |
4077 | ||
4078 | if (sysfs_link_rdev(mddev, rdev)) | |
4079 | /* Failure here is OK */; | |
4080 | } | |
4081 | } else if (rdev->raid_disk >= conf->prev.raid_disks | |
4082 | && !test_bit(Faulty, &rdev->flags)) { | |
4083 | /* This is a spare that was manually added */ | |
4084 | set_bit(In_sync, &rdev->flags); | |
4085 | } | |
4086 | } | |
4087 | /* When a reshape changes the number of devices, | |
4088 | * ->degraded is measured against the larger of the | |
4089 | * pre and post numbers. | |
4090 | */ | |
4091 | spin_lock_irq(&conf->device_lock); | |
4092 | mddev->degraded = calc_degraded(conf); | |
4093 | spin_unlock_irq(&conf->device_lock); | |
4094 | mddev->raid_disks = conf->geo.raid_disks; | |
4095 | mddev->reshape_position = conf->reshape_progress; | |
4096 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
4097 | ||
4098 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
4099 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
4100 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
4101 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
4102 | ||
4103 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
4104 | "reshape"); | |
4105 | if (!mddev->sync_thread) { | |
bb63a701 N |
4106 | ret = -EAGAIN; |
4107 | goto abort; | |
3ea7daa5 N |
4108 | } |
4109 | conf->reshape_checkpoint = jiffies; | |
4110 | md_wakeup_thread(mddev->sync_thread); | |
4111 | md_new_event(mddev); | |
4112 | return 0; | |
bb63a701 N |
4113 | |
4114 | abort: | |
4115 | mddev->recovery = 0; | |
4116 | spin_lock_irq(&conf->device_lock); | |
4117 | conf->geo = conf->prev; | |
4118 | mddev->raid_disks = conf->geo.raid_disks; | |
4119 | rdev_for_each(rdev, mddev) | |
4120 | rdev->new_data_offset = rdev->data_offset; | |
4121 | smp_wmb(); | |
4122 | conf->reshape_progress = MaxSector; | |
4123 | mddev->reshape_position = MaxSector; | |
4124 | spin_unlock_irq(&conf->device_lock); | |
4125 | return ret; | |
3ea7daa5 N |
4126 | } |
4127 | ||
4128 | /* Calculate the last device-address that could contain | |
4129 | * any block from the chunk that includes the array-address 's' | |
4130 | * and report the next address. | |
4131 | * i.e. the address returned will be chunk-aligned and after | |
4132 | * any data that is in the chunk containing 's'. | |
4133 | */ | |
4134 | static sector_t last_dev_address(sector_t s, struct geom *geo) | |
4135 | { | |
4136 | s = (s | geo->chunk_mask) + 1; | |
4137 | s >>= geo->chunk_shift; | |
4138 | s *= geo->near_copies; | |
4139 | s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks); | |
4140 | s *= geo->far_copies; | |
4141 | s <<= geo->chunk_shift; | |
4142 | return s; | |
4143 | } | |
4144 | ||
4145 | /* Calculate the first device-address that could contain | |
4146 | * any block from the chunk that includes the array-address 's'. | |
4147 | * This too will be the start of a chunk | |
4148 | */ | |
4149 | static sector_t first_dev_address(sector_t s, struct geom *geo) | |
4150 | { | |
4151 | s >>= geo->chunk_shift; | |
4152 | s *= geo->near_copies; | |
4153 | sector_div(s, geo->raid_disks); | |
4154 | s *= geo->far_copies; | |
4155 | s <<= geo->chunk_shift; | |
4156 | return s; | |
4157 | } | |
4158 | ||
4159 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, | |
4160 | int *skipped) | |
4161 | { | |
4162 | /* We simply copy at most one chunk (smallest of old and new) | |
4163 | * at a time, possibly less if that exceeds RESYNC_PAGES, | |
4164 | * or we hit a bad block or something. | |
4165 | * This might mean we pause for normal IO in the middle of | |
4166 | * a chunk, but that is not a problem was mddev->reshape_position | |
4167 | * can record any location. | |
4168 | * | |
4169 | * If we will want to write to a location that isn't | |
4170 | * yet recorded as 'safe' (i.e. in metadata on disk) then | |
4171 | * we need to flush all reshape requests and update the metadata. | |
4172 | * | |
4173 | * When reshaping forwards (e.g. to more devices), we interpret | |
4174 | * 'safe' as the earliest block which might not have been copied | |
4175 | * down yet. We divide this by previous stripe size and multiply | |
4176 | * by previous stripe length to get lowest device offset that we | |
4177 | * cannot write to yet. | |
4178 | * We interpret 'sector_nr' as an address that we want to write to. | |
4179 | * From this we use last_device_address() to find where we might | |
4180 | * write to, and first_device_address on the 'safe' position. | |
4181 | * If this 'next' write position is after the 'safe' position, | |
4182 | * we must update the metadata to increase the 'safe' position. | |
4183 | * | |
4184 | * When reshaping backwards, we round in the opposite direction | |
4185 | * and perform the reverse test: next write position must not be | |
4186 | * less than current safe position. | |
4187 | * | |
4188 | * In all this the minimum difference in data offsets | |
4189 | * (conf->offset_diff - always positive) allows a bit of slack, | |
4190 | * so next can be after 'safe', but not by more than offset_disk | |
4191 | * | |
4192 | * We need to prepare all the bios here before we start any IO | |
4193 | * to ensure the size we choose is acceptable to all devices. | |
4194 | * The means one for each copy for write-out and an extra one for | |
4195 | * read-in. | |
4196 | * We store the read-in bio in ->master_bio and the others in | |
4197 | * ->devs[x].bio and ->devs[x].repl_bio. | |
4198 | */ | |
4199 | struct r10conf *conf = mddev->private; | |
4200 | struct r10bio *r10_bio; | |
4201 | sector_t next, safe, last; | |
4202 | int max_sectors; | |
4203 | int nr_sectors; | |
4204 | int s; | |
4205 | struct md_rdev *rdev; | |
4206 | int need_flush = 0; | |
4207 | struct bio *blist; | |
4208 | struct bio *bio, *read_bio; | |
4209 | int sectors_done = 0; | |
4210 | ||
4211 | if (sector_nr == 0) { | |
4212 | /* If restarting in the middle, skip the initial sectors */ | |
4213 | if (mddev->reshape_backwards && | |
4214 | conf->reshape_progress < raid10_size(mddev, 0, 0)) { | |
4215 | sector_nr = (raid10_size(mddev, 0, 0) | |
4216 | - conf->reshape_progress); | |
4217 | } else if (!mddev->reshape_backwards && | |
4218 | conf->reshape_progress > 0) | |
4219 | sector_nr = conf->reshape_progress; | |
4220 | if (sector_nr) { | |
4221 | mddev->curr_resync_completed = sector_nr; | |
4222 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); | |
4223 | *skipped = 1; | |
4224 | return sector_nr; | |
4225 | } | |
4226 | } | |
4227 | ||
4228 | /* We don't use sector_nr to track where we are up to | |
4229 | * as that doesn't work well for ->reshape_backwards. | |
4230 | * So just use ->reshape_progress. | |
4231 | */ | |
4232 | if (mddev->reshape_backwards) { | |
4233 | /* 'next' is the earliest device address that we might | |
4234 | * write to for this chunk in the new layout | |
4235 | */ | |
4236 | next = first_dev_address(conf->reshape_progress - 1, | |
4237 | &conf->geo); | |
4238 | ||
4239 | /* 'safe' is the last device address that we might read from | |
4240 | * in the old layout after a restart | |
4241 | */ | |
4242 | safe = last_dev_address(conf->reshape_safe - 1, | |
4243 | &conf->prev); | |
4244 | ||
4245 | if (next + conf->offset_diff < safe) | |
4246 | need_flush = 1; | |
4247 | ||
4248 | last = conf->reshape_progress - 1; | |
4249 | sector_nr = last & ~(sector_t)(conf->geo.chunk_mask | |
4250 | & conf->prev.chunk_mask); | |
4251 | if (sector_nr + RESYNC_BLOCK_SIZE/512 < last) | |
4252 | sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512; | |
4253 | } else { | |
4254 | /* 'next' is after the last device address that we | |
4255 | * might write to for this chunk in the new layout | |
4256 | */ | |
4257 | next = last_dev_address(conf->reshape_progress, &conf->geo); | |
4258 | ||
4259 | /* 'safe' is the earliest device address that we might | |
4260 | * read from in the old layout after a restart | |
4261 | */ | |
4262 | safe = first_dev_address(conf->reshape_safe, &conf->prev); | |
4263 | ||
4264 | /* Need to update metadata if 'next' might be beyond 'safe' | |
4265 | * as that would possibly corrupt data | |
4266 | */ | |
4267 | if (next > safe + conf->offset_diff) | |
4268 | need_flush = 1; | |
4269 | ||
4270 | sector_nr = conf->reshape_progress; | |
4271 | last = sector_nr | (conf->geo.chunk_mask | |
4272 | & conf->prev.chunk_mask); | |
4273 | ||
4274 | if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last) | |
4275 | last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1; | |
4276 | } | |
4277 | ||
4278 | if (need_flush || | |
4279 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { | |
4280 | /* Need to update reshape_position in metadata */ | |
4281 | wait_barrier(conf); | |
4282 | mddev->reshape_position = conf->reshape_progress; | |
4283 | if (mddev->reshape_backwards) | |
4284 | mddev->curr_resync_completed = raid10_size(mddev, 0, 0) | |
4285 | - conf->reshape_progress; | |
4286 | else | |
4287 | mddev->curr_resync_completed = conf->reshape_progress; | |
4288 | conf->reshape_checkpoint = jiffies; | |
4289 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
4290 | md_wakeup_thread(mddev->thread); | |
4291 | wait_event(mddev->sb_wait, mddev->flags == 0 || | |
4292 | kthread_should_stop()); | |
4293 | conf->reshape_safe = mddev->reshape_position; | |
4294 | allow_barrier(conf); | |
4295 | } | |
4296 | ||
4297 | read_more: | |
4298 | /* Now schedule reads for blocks from sector_nr to last */ | |
4299 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); | |
4300 | raise_barrier(conf, sectors_done != 0); | |
4301 | atomic_set(&r10_bio->remaining, 0); | |
4302 | r10_bio->mddev = mddev; | |
4303 | r10_bio->sector = sector_nr; | |
4304 | set_bit(R10BIO_IsReshape, &r10_bio->state); | |
4305 | r10_bio->sectors = last - sector_nr + 1; | |
4306 | rdev = read_balance(conf, r10_bio, &max_sectors); | |
4307 | BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state)); | |
4308 | ||
4309 | if (!rdev) { | |
4310 | /* Cannot read from here, so need to record bad blocks | |
4311 | * on all the target devices. | |
4312 | */ | |
4313 | // FIXME | |
4314 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
4315 | return sectors_done; | |
4316 | } | |
4317 | ||
4318 | read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev); | |
4319 | ||
4320 | read_bio->bi_bdev = rdev->bdev; | |
4321 | read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr | |
4322 | + rdev->data_offset); | |
4323 | read_bio->bi_private = r10_bio; | |
4324 | read_bio->bi_end_io = end_sync_read; | |
4325 | read_bio->bi_rw = READ; | |
4326 | read_bio->bi_flags &= ~(BIO_POOL_MASK - 1); | |
4327 | read_bio->bi_flags |= 1 << BIO_UPTODATE; | |
4328 | read_bio->bi_vcnt = 0; | |
4329 | read_bio->bi_idx = 0; | |
4330 | read_bio->bi_size = 0; | |
4331 | r10_bio->master_bio = read_bio; | |
4332 | r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum; | |
4333 | ||
4334 | /* Now find the locations in the new layout */ | |
4335 | __raid10_find_phys(&conf->geo, r10_bio); | |
4336 | ||
4337 | blist = read_bio; | |
4338 | read_bio->bi_next = NULL; | |
4339 | ||
4340 | for (s = 0; s < conf->copies*2; s++) { | |
4341 | struct bio *b; | |
4342 | int d = r10_bio->devs[s/2].devnum; | |
4343 | struct md_rdev *rdev2; | |
4344 | if (s&1) { | |
4345 | rdev2 = conf->mirrors[d].replacement; | |
4346 | b = r10_bio->devs[s/2].repl_bio; | |
4347 | } else { | |
4348 | rdev2 = conf->mirrors[d].rdev; | |
4349 | b = r10_bio->devs[s/2].bio; | |
4350 | } | |
4351 | if (!rdev2 || test_bit(Faulty, &rdev2->flags)) | |
4352 | continue; | |
4353 | b->bi_bdev = rdev2->bdev; | |
4354 | b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset; | |
4355 | b->bi_private = r10_bio; | |
4356 | b->bi_end_io = end_reshape_write; | |
4357 | b->bi_rw = WRITE; | |
4358 | b->bi_flags &= ~(BIO_POOL_MASK - 1); | |
4359 | b->bi_flags |= 1 << BIO_UPTODATE; | |
4360 | b->bi_next = blist; | |
4361 | b->bi_vcnt = 0; | |
4362 | b->bi_idx = 0; | |
4363 | b->bi_size = 0; | |
4364 | blist = b; | |
4365 | } | |
4366 | ||
4367 | /* Now add as many pages as possible to all of these bios. */ | |
4368 | ||
4369 | nr_sectors = 0; | |
4370 | for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) { | |
4371 | struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page; | |
4372 | int len = (max_sectors - s) << 9; | |
4373 | if (len > PAGE_SIZE) | |
4374 | len = PAGE_SIZE; | |
4375 | for (bio = blist; bio ; bio = bio->bi_next) { | |
4376 | struct bio *bio2; | |
4377 | if (bio_add_page(bio, page, len, 0)) | |
4378 | continue; | |
4379 | ||
4380 | /* Didn't fit, must stop */ | |
4381 | for (bio2 = blist; | |
4382 | bio2 && bio2 != bio; | |
4383 | bio2 = bio2->bi_next) { | |
4384 | /* Remove last page from this bio */ | |
4385 | bio2->bi_vcnt--; | |
4386 | bio2->bi_size -= len; | |
4387 | bio2->bi_flags &= ~(1<<BIO_SEG_VALID); | |
4388 | } | |
4389 | goto bio_full; | |
4390 | } | |
4391 | sector_nr += len >> 9; | |
4392 | nr_sectors += len >> 9; | |
4393 | } | |
4394 | bio_full: | |
4395 | r10_bio->sectors = nr_sectors; | |
4396 | ||
4397 | /* Now submit the read */ | |
4398 | md_sync_acct(read_bio->bi_bdev, r10_bio->sectors); | |
4399 | atomic_inc(&r10_bio->remaining); | |
4400 | read_bio->bi_next = NULL; | |
4401 | generic_make_request(read_bio); | |
4402 | sector_nr += nr_sectors; | |
4403 | sectors_done += nr_sectors; | |
4404 | if (sector_nr <= last) | |
4405 | goto read_more; | |
4406 | ||
4407 | /* Now that we have done the whole section we can | |
4408 | * update reshape_progress | |
4409 | */ | |
4410 | if (mddev->reshape_backwards) | |
4411 | conf->reshape_progress -= sectors_done; | |
4412 | else | |
4413 | conf->reshape_progress += sectors_done; | |
4414 | ||
4415 | return sectors_done; | |
4416 | } | |
4417 | ||
4418 | static void end_reshape_request(struct r10bio *r10_bio); | |
4419 | static int handle_reshape_read_error(struct mddev *mddev, | |
4420 | struct r10bio *r10_bio); | |
4421 | static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio) | |
4422 | { | |
4423 | /* Reshape read completed. Hopefully we have a block | |
4424 | * to write out. | |
4425 | * If we got a read error then we do sync 1-page reads from | |
4426 | * elsewhere until we find the data - or give up. | |
4427 | */ | |
4428 | struct r10conf *conf = mddev->private; | |
4429 | int s; | |
4430 | ||
4431 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) | |
4432 | if (handle_reshape_read_error(mddev, r10_bio) < 0) { | |
4433 | /* Reshape has been aborted */ | |
4434 | md_done_sync(mddev, r10_bio->sectors, 0); | |
4435 | return; | |
4436 | } | |
4437 | ||
4438 | /* We definitely have the data in the pages, schedule the | |
4439 | * writes. | |
4440 | */ | |
4441 | atomic_set(&r10_bio->remaining, 1); | |
4442 | for (s = 0; s < conf->copies*2; s++) { | |
4443 | struct bio *b; | |
4444 | int d = r10_bio->devs[s/2].devnum; | |
4445 | struct md_rdev *rdev; | |
4446 | if (s&1) { | |
4447 | rdev = conf->mirrors[d].replacement; | |
4448 | b = r10_bio->devs[s/2].repl_bio; | |
4449 | } else { | |
4450 | rdev = conf->mirrors[d].rdev; | |
4451 | b = r10_bio->devs[s/2].bio; | |
4452 | } | |
4453 | if (!rdev || test_bit(Faulty, &rdev->flags)) | |
4454 | continue; | |
4455 | atomic_inc(&rdev->nr_pending); | |
4456 | md_sync_acct(b->bi_bdev, r10_bio->sectors); | |
4457 | atomic_inc(&r10_bio->remaining); | |
4458 | b->bi_next = NULL; | |
4459 | generic_make_request(b); | |
4460 | } | |
4461 | end_reshape_request(r10_bio); | |
4462 | } | |
4463 | ||
4464 | static void end_reshape(struct r10conf *conf) | |
4465 | { | |
4466 | if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) | |
4467 | return; | |
4468 | ||
4469 | spin_lock_irq(&conf->device_lock); | |
4470 | conf->prev = conf->geo; | |
4471 | md_finish_reshape(conf->mddev); | |
4472 | smp_wmb(); | |
4473 | conf->reshape_progress = MaxSector; | |
4474 | spin_unlock_irq(&conf->device_lock); | |
4475 | ||
4476 | /* read-ahead size must cover two whole stripes, which is | |
4477 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices | |
4478 | */ | |
4479 | if (conf->mddev->queue) { | |
4480 | int stripe = conf->geo.raid_disks * | |
4481 | ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE); | |
4482 | stripe /= conf->geo.near_copies; | |
4483 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) | |
4484 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
4485 | } | |
4486 | conf->fullsync = 0; | |
4487 | } | |
4488 | ||
4489 | ||
4490 | static int handle_reshape_read_error(struct mddev *mddev, | |
4491 | struct r10bio *r10_bio) | |
4492 | { | |
4493 | /* Use sync reads to get the blocks from somewhere else */ | |
4494 | int sectors = r10_bio->sectors; | |
3ea7daa5 | 4495 | struct r10conf *conf = mddev->private; |
e0ee7785 N |
4496 | struct { |
4497 | struct r10bio r10_bio; | |
4498 | struct r10dev devs[conf->copies]; | |
4499 | } on_stack; | |
4500 | struct r10bio *r10b = &on_stack.r10_bio; | |
3ea7daa5 N |
4501 | int slot = 0; |
4502 | int idx = 0; | |
4503 | struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec; | |
4504 | ||
e0ee7785 N |
4505 | r10b->sector = r10_bio->sector; |
4506 | __raid10_find_phys(&conf->prev, r10b); | |
3ea7daa5 N |
4507 | |
4508 | while (sectors) { | |
4509 | int s = sectors; | |
4510 | int success = 0; | |
4511 | int first_slot = slot; | |
4512 | ||
4513 | if (s > (PAGE_SIZE >> 9)) | |
4514 | s = PAGE_SIZE >> 9; | |
4515 | ||
4516 | while (!success) { | |
e0ee7785 | 4517 | int d = r10b->devs[slot].devnum; |
3ea7daa5 N |
4518 | struct md_rdev *rdev = conf->mirrors[d].rdev; |
4519 | sector_t addr; | |
4520 | if (rdev == NULL || | |
4521 | test_bit(Faulty, &rdev->flags) || | |
4522 | !test_bit(In_sync, &rdev->flags)) | |
4523 | goto failed; | |
4524 | ||
e0ee7785 | 4525 | addr = r10b->devs[slot].addr + idx * PAGE_SIZE; |
3ea7daa5 N |
4526 | success = sync_page_io(rdev, |
4527 | addr, | |
4528 | s << 9, | |
4529 | bvec[idx].bv_page, | |
4530 | READ, false); | |
4531 | if (success) | |
4532 | break; | |
4533 | failed: | |
4534 | slot++; | |
4535 | if (slot >= conf->copies) | |
4536 | slot = 0; | |
4537 | if (slot == first_slot) | |
4538 | break; | |
4539 | } | |
4540 | if (!success) { | |
4541 | /* couldn't read this block, must give up */ | |
4542 | set_bit(MD_RECOVERY_INTR, | |
4543 | &mddev->recovery); | |
4544 | return -EIO; | |
4545 | } | |
4546 | sectors -= s; | |
4547 | idx++; | |
4548 | } | |
4549 | return 0; | |
4550 | } | |
4551 | ||
4552 | static void end_reshape_write(struct bio *bio, int error) | |
4553 | { | |
4554 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
4555 | struct r10bio *r10_bio = bio->bi_private; | |
4556 | struct mddev *mddev = r10_bio->mddev; | |
4557 | struct r10conf *conf = mddev->private; | |
4558 | int d; | |
4559 | int slot; | |
4560 | int repl; | |
4561 | struct md_rdev *rdev = NULL; | |
4562 | ||
4563 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); | |
4564 | if (repl) | |
4565 | rdev = conf->mirrors[d].replacement; | |
4566 | if (!rdev) { | |
4567 | smp_mb(); | |
4568 | rdev = conf->mirrors[d].rdev; | |
4569 | } | |
4570 | ||
4571 | if (!uptodate) { | |
4572 | /* FIXME should record badblock */ | |
4573 | md_error(mddev, rdev); | |
4574 | } | |
4575 | ||
4576 | rdev_dec_pending(rdev, mddev); | |
4577 | end_reshape_request(r10_bio); | |
4578 | } | |
4579 | ||
4580 | static void end_reshape_request(struct r10bio *r10_bio) | |
4581 | { | |
4582 | if (!atomic_dec_and_test(&r10_bio->remaining)) | |
4583 | return; | |
4584 | md_done_sync(r10_bio->mddev, r10_bio->sectors, 1); | |
4585 | bio_put(r10_bio->master_bio); | |
4586 | put_buf(r10_bio); | |
4587 | } | |
4588 | ||
4589 | static void raid10_finish_reshape(struct mddev *mddev) | |
4590 | { | |
4591 | struct r10conf *conf = mddev->private; | |
4592 | ||
4593 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
4594 | return; | |
4595 | ||
4596 | if (mddev->delta_disks > 0) { | |
4597 | sector_t size = raid10_size(mddev, 0, 0); | |
4598 | md_set_array_sectors(mddev, size); | |
4599 | if (mddev->recovery_cp > mddev->resync_max_sectors) { | |
4600 | mddev->recovery_cp = mddev->resync_max_sectors; | |
4601 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
4602 | } | |
4603 | mddev->resync_max_sectors = size; | |
4604 | set_capacity(mddev->gendisk, mddev->array_sectors); | |
4605 | revalidate_disk(mddev->gendisk); | |
63aced61 N |
4606 | } else { |
4607 | int d; | |
4608 | for (d = conf->geo.raid_disks ; | |
4609 | d < conf->geo.raid_disks - mddev->delta_disks; | |
4610 | d++) { | |
4611 | struct md_rdev *rdev = conf->mirrors[d].rdev; | |
4612 | if (rdev) | |
4613 | clear_bit(In_sync, &rdev->flags); | |
4614 | rdev = conf->mirrors[d].replacement; | |
4615 | if (rdev) | |
4616 | clear_bit(In_sync, &rdev->flags); | |
4617 | } | |
3ea7daa5 N |
4618 | } |
4619 | mddev->layout = mddev->new_layout; | |
4620 | mddev->chunk_sectors = 1 << conf->geo.chunk_shift; | |
4621 | mddev->reshape_position = MaxSector; | |
4622 | mddev->delta_disks = 0; | |
4623 | mddev->reshape_backwards = 0; | |
4624 | } | |
4625 | ||
84fc4b56 | 4626 | static struct md_personality raid10_personality = |
1da177e4 LT |
4627 | { |
4628 | .name = "raid10", | |
2604b703 | 4629 | .level = 10, |
1da177e4 LT |
4630 | .owner = THIS_MODULE, |
4631 | .make_request = make_request, | |
4632 | .run = run, | |
4633 | .stop = stop, | |
4634 | .status = status, | |
4635 | .error_handler = error, | |
4636 | .hot_add_disk = raid10_add_disk, | |
4637 | .hot_remove_disk= raid10_remove_disk, | |
4638 | .spare_active = raid10_spare_active, | |
4639 | .sync_request = sync_request, | |
6cce3b23 | 4640 | .quiesce = raid10_quiesce, |
80c3a6ce | 4641 | .size = raid10_size, |
006a09a0 | 4642 | .resize = raid10_resize, |
dab8b292 | 4643 | .takeover = raid10_takeover, |
3ea7daa5 N |
4644 | .check_reshape = raid10_check_reshape, |
4645 | .start_reshape = raid10_start_reshape, | |
4646 | .finish_reshape = raid10_finish_reshape, | |
1da177e4 LT |
4647 | }; |
4648 | ||
4649 | static int __init raid_init(void) | |
4650 | { | |
2604b703 | 4651 | return register_md_personality(&raid10_personality); |
1da177e4 LT |
4652 | } |
4653 | ||
4654 | static void raid_exit(void) | |
4655 | { | |
2604b703 | 4656 | unregister_md_personality(&raid10_personality); |
1da177e4 LT |
4657 | } |
4658 | ||
4659 | module_init(raid_init); | |
4660 | module_exit(raid_exit); | |
4661 | MODULE_LICENSE("GPL"); | |
0efb9e61 | 4662 | MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); |
1da177e4 | 4663 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ |
d9d166c2 | 4664 | MODULE_ALIAS("md-raid10"); |
2604b703 | 4665 | MODULE_ALIAS("md-level-10"); |
34db0cd6 N |
4666 | |
4667 | module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); |