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