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