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