Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * raid1.c : Multiple Devices driver for Linux | |
3 | * | |
4 | * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat | |
5 | * | |
6 | * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman | |
7 | * | |
8 | * RAID-1 management functions. | |
9 | * | |
10 | * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000 | |
11 | * | |
96de0e25 | 12 | * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk> |
1da177e4 LT |
13 | * Various fixes by Neil Brown <neilb@cse.unsw.edu.au> |
14 | * | |
191ea9b2 N |
15 | * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support |
16 | * bitmapped intelligence in resync: | |
17 | * | |
18 | * - bitmap marked during normal i/o | |
19 | * - bitmap used to skip nondirty blocks during sync | |
20 | * | |
21 | * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology: | |
22 | * - persistent bitmap code | |
23 | * | |
1da177e4 LT |
24 | * This program is free software; you can redistribute it and/or modify |
25 | * it under the terms of the GNU General Public License as published by | |
26 | * the Free Software Foundation; either version 2, or (at your option) | |
27 | * any later version. | |
28 | * | |
29 | * You should have received a copy of the GNU General Public License | |
30 | * (for example /usr/src/linux/COPYING); if not, write to the Free | |
31 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
32 | */ | |
33 | ||
5a0e3ad6 | 34 | #include <linux/slab.h> |
25570727 | 35 | #include <linux/delay.h> |
bff61975 | 36 | #include <linux/blkdev.h> |
056075c7 | 37 | #include <linux/module.h> |
bff61975 | 38 | #include <linux/seq_file.h> |
8bda470e | 39 | #include <linux/ratelimit.h> |
109e3765 | 40 | #include <trace/events/block.h> |
43b2e5d8 | 41 | #include "md.h" |
ef740c37 CH |
42 | #include "raid1.h" |
43 | #include "bitmap.h" | |
191ea9b2 | 44 | |
1da177e4 LT |
45 | /* |
46 | * Number of guaranteed r1bios in case of extreme VM load: | |
47 | */ | |
48 | #define NR_RAID1_BIOS 256 | |
49 | ||
473e87ce JB |
50 | /* when we get a read error on a read-only array, we redirect to another |
51 | * device without failing the first device, or trying to over-write to | |
52 | * correct the read error. To keep track of bad blocks on a per-bio | |
53 | * level, we store IO_BLOCKED in the appropriate 'bios' pointer | |
54 | */ | |
55 | #define IO_BLOCKED ((struct bio *)1) | |
56 | /* When we successfully write to a known bad-block, we need to remove the | |
57 | * bad-block marking which must be done from process context. So we record | |
58 | * the success by setting devs[n].bio to IO_MADE_GOOD | |
59 | */ | |
60 | #define IO_MADE_GOOD ((struct bio *)2) | |
61 | ||
62 | #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) | |
63 | ||
34db0cd6 N |
64 | /* When there are this many requests queue to be written by |
65 | * the raid1 thread, we become 'congested' to provide back-pressure | |
66 | * for writeback. | |
67 | */ | |
68 | static int max_queued_requests = 1024; | |
1da177e4 | 69 | |
79ef3a8a | 70 | static void allow_barrier(struct r1conf *conf, sector_t start_next_window, |
71 | sector_t bi_sector); | |
e8096360 | 72 | static void lower_barrier(struct r1conf *conf); |
1da177e4 | 73 | |
578b54ad N |
74 | #define raid1_log(md, fmt, args...) \ |
75 | do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid1 " fmt, ##args); } while (0) | |
76 | ||
dd0fc66f | 77 | static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 LT |
78 | { |
79 | struct pool_info *pi = data; | |
9f2c9d12 | 80 | int size = offsetof(struct r1bio, bios[pi->raid_disks]); |
1da177e4 LT |
81 | |
82 | /* allocate a r1bio with room for raid_disks entries in the bios array */ | |
7eaceacc | 83 | return kzalloc(size, gfp_flags); |
1da177e4 LT |
84 | } |
85 | ||
86 | static void r1bio_pool_free(void *r1_bio, void *data) | |
87 | { | |
88 | kfree(r1_bio); | |
89 | } | |
90 | ||
91 | #define RESYNC_BLOCK_SIZE (64*1024) | |
8e005f7c | 92 | #define RESYNC_DEPTH 32 |
1da177e4 LT |
93 | #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) |
94 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) | |
8e005f7c | 95 | #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH) |
96 | #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9) | |
c40f341f GR |
97 | #define CLUSTER_RESYNC_WINDOW (16 * RESYNC_WINDOW) |
98 | #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9) | |
8e005f7c | 99 | #define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS) |
1da177e4 | 100 | |
dd0fc66f | 101 | static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data) |
1da177e4 LT |
102 | { |
103 | struct pool_info *pi = data; | |
9f2c9d12 | 104 | struct r1bio *r1_bio; |
1da177e4 | 105 | struct bio *bio; |
da1aab3d | 106 | int need_pages; |
1da177e4 LT |
107 | int i, j; |
108 | ||
109 | r1_bio = r1bio_pool_alloc(gfp_flags, pi); | |
7eaceacc | 110 | if (!r1_bio) |
1da177e4 | 111 | return NULL; |
1da177e4 LT |
112 | |
113 | /* | |
114 | * Allocate bios : 1 for reading, n-1 for writing | |
115 | */ | |
116 | for (j = pi->raid_disks ; j-- ; ) { | |
6746557f | 117 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
1da177e4 LT |
118 | if (!bio) |
119 | goto out_free_bio; | |
120 | r1_bio->bios[j] = bio; | |
121 | } | |
122 | /* | |
123 | * Allocate RESYNC_PAGES data pages and attach them to | |
d11c171e N |
124 | * the first bio. |
125 | * If this is a user-requested check/repair, allocate | |
126 | * RESYNC_PAGES for each bio. | |
1da177e4 | 127 | */ |
d11c171e | 128 | if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) |
da1aab3d | 129 | need_pages = pi->raid_disks; |
d11c171e | 130 | else |
da1aab3d N |
131 | need_pages = 1; |
132 | for (j = 0; j < need_pages; j++) { | |
d11c171e | 133 | bio = r1_bio->bios[j]; |
a0787606 | 134 | bio->bi_vcnt = RESYNC_PAGES; |
d11c171e | 135 | |
a0787606 | 136 | if (bio_alloc_pages(bio, gfp_flags)) |
da1aab3d | 137 | goto out_free_pages; |
d11c171e N |
138 | } |
139 | /* If not user-requests, copy the page pointers to all bios */ | |
140 | if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) { | |
141 | for (i=0; i<RESYNC_PAGES ; i++) | |
142 | for (j=1; j<pi->raid_disks; j++) | |
143 | r1_bio->bios[j]->bi_io_vec[i].bv_page = | |
144 | r1_bio->bios[0]->bi_io_vec[i].bv_page; | |
1da177e4 LT |
145 | } |
146 | ||
147 | r1_bio->master_bio = NULL; | |
148 | ||
149 | return r1_bio; | |
150 | ||
da1aab3d | 151 | out_free_pages: |
491221f8 GJ |
152 | while (--j >= 0) |
153 | bio_free_pages(r1_bio->bios[j]); | |
da1aab3d | 154 | |
1da177e4 | 155 | out_free_bio: |
8f19ccb2 | 156 | while (++j < pi->raid_disks) |
1da177e4 LT |
157 | bio_put(r1_bio->bios[j]); |
158 | r1bio_pool_free(r1_bio, data); | |
159 | return NULL; | |
160 | } | |
161 | ||
162 | static void r1buf_pool_free(void *__r1_bio, void *data) | |
163 | { | |
164 | struct pool_info *pi = data; | |
d11c171e | 165 | int i,j; |
9f2c9d12 | 166 | struct r1bio *r1bio = __r1_bio; |
1da177e4 | 167 | |
d11c171e N |
168 | for (i = 0; i < RESYNC_PAGES; i++) |
169 | for (j = pi->raid_disks; j-- ;) { | |
170 | if (j == 0 || | |
171 | r1bio->bios[j]->bi_io_vec[i].bv_page != | |
172 | r1bio->bios[0]->bi_io_vec[i].bv_page) | |
1345b1d8 | 173 | safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page); |
d11c171e | 174 | } |
1da177e4 LT |
175 | for (i=0 ; i < pi->raid_disks; i++) |
176 | bio_put(r1bio->bios[i]); | |
177 | ||
178 | r1bio_pool_free(r1bio, data); | |
179 | } | |
180 | ||
e8096360 | 181 | static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio) |
1da177e4 LT |
182 | { |
183 | int i; | |
184 | ||
8f19ccb2 | 185 | for (i = 0; i < conf->raid_disks * 2; i++) { |
1da177e4 | 186 | struct bio **bio = r1_bio->bios + i; |
4367af55 | 187 | if (!BIO_SPECIAL(*bio)) |
1da177e4 LT |
188 | bio_put(*bio); |
189 | *bio = NULL; | |
190 | } | |
191 | } | |
192 | ||
9f2c9d12 | 193 | static void free_r1bio(struct r1bio *r1_bio) |
1da177e4 | 194 | { |
e8096360 | 195 | struct r1conf *conf = r1_bio->mddev->private; |
1da177e4 | 196 | |
1da177e4 LT |
197 | put_all_bios(conf, r1_bio); |
198 | mempool_free(r1_bio, conf->r1bio_pool); | |
199 | } | |
200 | ||
9f2c9d12 | 201 | static void put_buf(struct r1bio *r1_bio) |
1da177e4 | 202 | { |
e8096360 | 203 | struct r1conf *conf = r1_bio->mddev->private; |
3e198f78 N |
204 | int i; |
205 | ||
8f19ccb2 | 206 | for (i = 0; i < conf->raid_disks * 2; i++) { |
3e198f78 N |
207 | struct bio *bio = r1_bio->bios[i]; |
208 | if (bio->bi_end_io) | |
209 | rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev); | |
210 | } | |
1da177e4 LT |
211 | |
212 | mempool_free(r1_bio, conf->r1buf_pool); | |
213 | ||
17999be4 | 214 | lower_barrier(conf); |
1da177e4 LT |
215 | } |
216 | ||
9f2c9d12 | 217 | static void reschedule_retry(struct r1bio *r1_bio) |
1da177e4 LT |
218 | { |
219 | unsigned long flags; | |
fd01b88c | 220 | struct mddev *mddev = r1_bio->mddev; |
e8096360 | 221 | struct r1conf *conf = mddev->private; |
1da177e4 LT |
222 | |
223 | spin_lock_irqsave(&conf->device_lock, flags); | |
224 | list_add(&r1_bio->retry_list, &conf->retry_list); | |
ddaf22ab | 225 | conf->nr_queued ++; |
1da177e4 LT |
226 | spin_unlock_irqrestore(&conf->device_lock, flags); |
227 | ||
17999be4 | 228 | wake_up(&conf->wait_barrier); |
1da177e4 LT |
229 | md_wakeup_thread(mddev->thread); |
230 | } | |
231 | ||
232 | /* | |
233 | * raid_end_bio_io() is called when we have finished servicing a mirrored | |
234 | * operation and are ready to return a success/failure code to the buffer | |
235 | * cache layer. | |
236 | */ | |
9f2c9d12 | 237 | static void call_bio_endio(struct r1bio *r1_bio) |
d2eb35ac N |
238 | { |
239 | struct bio *bio = r1_bio->master_bio; | |
240 | int done; | |
e8096360 | 241 | struct r1conf *conf = r1_bio->mddev->private; |
79ef3a8a | 242 | sector_t start_next_window = r1_bio->start_next_window; |
4f024f37 | 243 | sector_t bi_sector = bio->bi_iter.bi_sector; |
d2eb35ac N |
244 | |
245 | if (bio->bi_phys_segments) { | |
246 | unsigned long flags; | |
247 | spin_lock_irqsave(&conf->device_lock, flags); | |
248 | bio->bi_phys_segments--; | |
249 | done = (bio->bi_phys_segments == 0); | |
250 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
79ef3a8a | 251 | /* |
252 | * make_request() might be waiting for | |
253 | * bi_phys_segments to decrease | |
254 | */ | |
255 | wake_up(&conf->wait_barrier); | |
d2eb35ac N |
256 | } else |
257 | done = 1; | |
258 | ||
259 | if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) | |
4246a0b6 CH |
260 | bio->bi_error = -EIO; |
261 | ||
d2eb35ac | 262 | if (done) { |
4246a0b6 | 263 | bio_endio(bio); |
d2eb35ac N |
264 | /* |
265 | * Wake up any possible resync thread that waits for the device | |
266 | * to go idle. | |
267 | */ | |
79ef3a8a | 268 | allow_barrier(conf, start_next_window, bi_sector); |
d2eb35ac N |
269 | } |
270 | } | |
271 | ||
9f2c9d12 | 272 | static void raid_end_bio_io(struct r1bio *r1_bio) |
1da177e4 LT |
273 | { |
274 | struct bio *bio = r1_bio->master_bio; | |
275 | ||
4b6d287f N |
276 | /* if nobody has done the final endio yet, do it now */ |
277 | if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { | |
36a4e1fe N |
278 | pr_debug("raid1: sync end %s on sectors %llu-%llu\n", |
279 | (bio_data_dir(bio) == WRITE) ? "write" : "read", | |
4f024f37 KO |
280 | (unsigned long long) bio->bi_iter.bi_sector, |
281 | (unsigned long long) bio_end_sector(bio) - 1); | |
4b6d287f | 282 | |
d2eb35ac | 283 | call_bio_endio(r1_bio); |
4b6d287f | 284 | } |
1da177e4 LT |
285 | free_r1bio(r1_bio); |
286 | } | |
287 | ||
288 | /* | |
289 | * Update disk head position estimator based on IRQ completion info. | |
290 | */ | |
9f2c9d12 | 291 | static inline void update_head_pos(int disk, struct r1bio *r1_bio) |
1da177e4 | 292 | { |
e8096360 | 293 | struct r1conf *conf = r1_bio->mddev->private; |
1da177e4 LT |
294 | |
295 | conf->mirrors[disk].head_position = | |
296 | r1_bio->sector + (r1_bio->sectors); | |
297 | } | |
298 | ||
ba3ae3be NK |
299 | /* |
300 | * Find the disk number which triggered given bio | |
301 | */ | |
9f2c9d12 | 302 | static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio) |
ba3ae3be NK |
303 | { |
304 | int mirror; | |
30194636 N |
305 | struct r1conf *conf = r1_bio->mddev->private; |
306 | int raid_disks = conf->raid_disks; | |
ba3ae3be | 307 | |
8f19ccb2 | 308 | for (mirror = 0; mirror < raid_disks * 2; mirror++) |
ba3ae3be NK |
309 | if (r1_bio->bios[mirror] == bio) |
310 | break; | |
311 | ||
8f19ccb2 | 312 | BUG_ON(mirror == raid_disks * 2); |
ba3ae3be NK |
313 | update_head_pos(mirror, r1_bio); |
314 | ||
315 | return mirror; | |
316 | } | |
317 | ||
4246a0b6 | 318 | static void raid1_end_read_request(struct bio *bio) |
1da177e4 | 319 | { |
4246a0b6 | 320 | int uptodate = !bio->bi_error; |
9f2c9d12 | 321 | struct r1bio *r1_bio = bio->bi_private; |
e8096360 | 322 | struct r1conf *conf = r1_bio->mddev->private; |
e5872d58 | 323 | struct md_rdev *rdev = conf->mirrors[r1_bio->read_disk].rdev; |
1da177e4 | 324 | |
1da177e4 LT |
325 | /* |
326 | * this branch is our 'one mirror IO has finished' event handler: | |
327 | */ | |
e5872d58 | 328 | update_head_pos(r1_bio->read_disk, r1_bio); |
ddaf22ab | 329 | |
dd00a99e N |
330 | if (uptodate) |
331 | set_bit(R1BIO_Uptodate, &r1_bio->state); | |
2e52d449 N |
332 | else if (test_bit(FailFast, &rdev->flags) && |
333 | test_bit(R1BIO_FailFast, &r1_bio->state)) | |
334 | /* This was a fail-fast read so we definitely | |
335 | * want to retry */ | |
336 | ; | |
dd00a99e N |
337 | else { |
338 | /* If all other devices have failed, we want to return | |
339 | * the error upwards rather than fail the last device. | |
340 | * Here we redefine "uptodate" to mean "Don't want to retry" | |
1da177e4 | 341 | */ |
dd00a99e N |
342 | unsigned long flags; |
343 | spin_lock_irqsave(&conf->device_lock, flags); | |
344 | if (r1_bio->mddev->degraded == conf->raid_disks || | |
345 | (r1_bio->mddev->degraded == conf->raid_disks-1 && | |
e5872d58 | 346 | test_bit(In_sync, &rdev->flags))) |
dd00a99e N |
347 | uptodate = 1; |
348 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
349 | } | |
1da177e4 | 350 | |
7ad4d4a6 | 351 | if (uptodate) { |
1da177e4 | 352 | raid_end_bio_io(r1_bio); |
e5872d58 | 353 | rdev_dec_pending(rdev, conf->mddev); |
7ad4d4a6 | 354 | } else { |
1da177e4 LT |
355 | /* |
356 | * oops, read error: | |
357 | */ | |
358 | char b[BDEVNAME_SIZE]; | |
1d41c216 N |
359 | pr_err_ratelimited("md/raid1:%s: %s: rescheduling sector %llu\n", |
360 | mdname(conf->mddev), | |
361 | bdevname(rdev->bdev, b), | |
362 | (unsigned long long)r1_bio->sector); | |
d2eb35ac | 363 | set_bit(R1BIO_ReadError, &r1_bio->state); |
1da177e4 | 364 | reschedule_retry(r1_bio); |
7ad4d4a6 | 365 | /* don't drop the reference on read_disk yet */ |
1da177e4 | 366 | } |
1da177e4 LT |
367 | } |
368 | ||
9f2c9d12 | 369 | static void close_write(struct r1bio *r1_bio) |
cd5ff9a1 N |
370 | { |
371 | /* it really is the end of this request */ | |
372 | if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { | |
373 | /* free extra copy of the data pages */ | |
374 | int i = r1_bio->behind_page_count; | |
375 | while (i--) | |
376 | safe_put_page(r1_bio->behind_bvecs[i].bv_page); | |
377 | kfree(r1_bio->behind_bvecs); | |
378 | r1_bio->behind_bvecs = NULL; | |
379 | } | |
380 | /* clear the bitmap if all writes complete successfully */ | |
381 | bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector, | |
382 | r1_bio->sectors, | |
383 | !test_bit(R1BIO_Degraded, &r1_bio->state), | |
384 | test_bit(R1BIO_BehindIO, &r1_bio->state)); | |
385 | md_write_end(r1_bio->mddev); | |
386 | } | |
387 | ||
9f2c9d12 | 388 | static void r1_bio_write_done(struct r1bio *r1_bio) |
4e78064f | 389 | { |
cd5ff9a1 N |
390 | if (!atomic_dec_and_test(&r1_bio->remaining)) |
391 | return; | |
392 | ||
393 | if (test_bit(R1BIO_WriteError, &r1_bio->state)) | |
394 | reschedule_retry(r1_bio); | |
395 | else { | |
396 | close_write(r1_bio); | |
4367af55 N |
397 | if (test_bit(R1BIO_MadeGood, &r1_bio->state)) |
398 | reschedule_retry(r1_bio); | |
399 | else | |
400 | raid_end_bio_io(r1_bio); | |
4e78064f N |
401 | } |
402 | } | |
403 | ||
4246a0b6 | 404 | static void raid1_end_write_request(struct bio *bio) |
1da177e4 | 405 | { |
9f2c9d12 | 406 | struct r1bio *r1_bio = bio->bi_private; |
e5872d58 | 407 | int behind = test_bit(R1BIO_BehindIO, &r1_bio->state); |
e8096360 | 408 | struct r1conf *conf = r1_bio->mddev->private; |
04b857f7 | 409 | struct bio *to_put = NULL; |
e5872d58 N |
410 | int mirror = find_bio_disk(r1_bio, bio); |
411 | struct md_rdev *rdev = conf->mirrors[mirror].rdev; | |
e3f948cd SL |
412 | bool discard_error; |
413 | ||
414 | discard_error = bio->bi_error && bio_op(bio) == REQ_OP_DISCARD; | |
1da177e4 | 415 | |
e9c7469b TH |
416 | /* |
417 | * 'one mirror IO has finished' event handler: | |
418 | */ | |
e3f948cd | 419 | if (bio->bi_error && !discard_error) { |
e5872d58 N |
420 | set_bit(WriteErrorSeen, &rdev->flags); |
421 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) | |
19d67169 N |
422 | set_bit(MD_RECOVERY_NEEDED, & |
423 | conf->mddev->recovery); | |
424 | ||
212e7eb7 N |
425 | if (test_bit(FailFast, &rdev->flags) && |
426 | (bio->bi_opf & MD_FAILFAST) && | |
427 | /* We never try FailFast to WriteMostly devices */ | |
428 | !test_bit(WriteMostly, &rdev->flags)) { | |
429 | md_error(r1_bio->mddev, rdev); | |
430 | if (!test_bit(Faulty, &rdev->flags)) | |
431 | /* This is the only remaining device, | |
432 | * We need to retry the write without | |
433 | * FailFast | |
434 | */ | |
435 | set_bit(R1BIO_WriteError, &r1_bio->state); | |
436 | else { | |
437 | /* Finished with this branch */ | |
438 | r1_bio->bios[mirror] = NULL; | |
439 | to_put = bio; | |
440 | } | |
441 | } else | |
442 | set_bit(R1BIO_WriteError, &r1_bio->state); | |
4367af55 | 443 | } else { |
1da177e4 | 444 | /* |
e9c7469b TH |
445 | * Set R1BIO_Uptodate in our master bio, so that we |
446 | * will return a good error code for to the higher | |
447 | * levels even if IO on some other mirrored buffer | |
448 | * fails. | |
449 | * | |
450 | * The 'master' represents the composite IO operation | |
451 | * to user-side. So if something waits for IO, then it | |
452 | * will wait for the 'master' bio. | |
1da177e4 | 453 | */ |
4367af55 N |
454 | sector_t first_bad; |
455 | int bad_sectors; | |
456 | ||
cd5ff9a1 N |
457 | r1_bio->bios[mirror] = NULL; |
458 | to_put = bio; | |
3056e3ae AL |
459 | /* |
460 | * Do not set R1BIO_Uptodate if the current device is | |
461 | * rebuilding or Faulty. This is because we cannot use | |
462 | * such device for properly reading the data back (we could | |
463 | * potentially use it, if the current write would have felt | |
464 | * before rdev->recovery_offset, but for simplicity we don't | |
465 | * check this here. | |
466 | */ | |
e5872d58 N |
467 | if (test_bit(In_sync, &rdev->flags) && |
468 | !test_bit(Faulty, &rdev->flags)) | |
3056e3ae | 469 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
e9c7469b | 470 | |
4367af55 | 471 | /* Maybe we can clear some bad blocks. */ |
e5872d58 | 472 | if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors, |
e3f948cd | 473 | &first_bad, &bad_sectors) && !discard_error) { |
4367af55 N |
474 | r1_bio->bios[mirror] = IO_MADE_GOOD; |
475 | set_bit(R1BIO_MadeGood, &r1_bio->state); | |
476 | } | |
477 | } | |
478 | ||
e9c7469b | 479 | if (behind) { |
e5872d58 | 480 | if (test_bit(WriteMostly, &rdev->flags)) |
e9c7469b TH |
481 | atomic_dec(&r1_bio->behind_remaining); |
482 | ||
483 | /* | |
484 | * In behind mode, we ACK the master bio once the I/O | |
485 | * has safely reached all non-writemostly | |
486 | * disks. Setting the Returned bit ensures that this | |
487 | * gets done only once -- we don't ever want to return | |
488 | * -EIO here, instead we'll wait | |
489 | */ | |
490 | if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) && | |
491 | test_bit(R1BIO_Uptodate, &r1_bio->state)) { | |
492 | /* Maybe we can return now */ | |
493 | if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) { | |
494 | struct bio *mbio = r1_bio->master_bio; | |
36a4e1fe N |
495 | pr_debug("raid1: behind end write sectors" |
496 | " %llu-%llu\n", | |
4f024f37 KO |
497 | (unsigned long long) mbio->bi_iter.bi_sector, |
498 | (unsigned long long) bio_end_sector(mbio) - 1); | |
d2eb35ac | 499 | call_bio_endio(r1_bio); |
4b6d287f N |
500 | } |
501 | } | |
502 | } | |
4367af55 | 503 | if (r1_bio->bios[mirror] == NULL) |
e5872d58 | 504 | rdev_dec_pending(rdev, conf->mddev); |
e9c7469b | 505 | |
1da177e4 | 506 | /* |
1da177e4 LT |
507 | * Let's see if all mirrored write operations have finished |
508 | * already. | |
509 | */ | |
af6d7b76 | 510 | r1_bio_write_done(r1_bio); |
c70810b3 | 511 | |
04b857f7 N |
512 | if (to_put) |
513 | bio_put(to_put); | |
1da177e4 LT |
514 | } |
515 | ||
1da177e4 LT |
516 | /* |
517 | * This routine returns the disk from which the requested read should | |
518 | * be done. There is a per-array 'next expected sequential IO' sector | |
519 | * number - if this matches on the next IO then we use the last disk. | |
520 | * There is also a per-disk 'last know head position' sector that is | |
521 | * maintained from IRQ contexts, both the normal and the resync IO | |
522 | * completion handlers update this position correctly. If there is no | |
523 | * perfect sequential match then we pick the disk whose head is closest. | |
524 | * | |
525 | * If there are 2 mirrors in the same 2 devices, performance degrades | |
526 | * because position is mirror, not device based. | |
527 | * | |
528 | * The rdev for the device selected will have nr_pending incremented. | |
529 | */ | |
e8096360 | 530 | static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors) |
1da177e4 | 531 | { |
af3a2cd6 | 532 | const sector_t this_sector = r1_bio->sector; |
d2eb35ac N |
533 | int sectors; |
534 | int best_good_sectors; | |
9dedf603 SL |
535 | int best_disk, best_dist_disk, best_pending_disk; |
536 | int has_nonrot_disk; | |
be4d3280 | 537 | int disk; |
76073054 | 538 | sector_t best_dist; |
9dedf603 | 539 | unsigned int min_pending; |
3cb03002 | 540 | struct md_rdev *rdev; |
f3ac8bf7 | 541 | int choose_first; |
12cee5a8 | 542 | int choose_next_idle; |
1da177e4 LT |
543 | |
544 | rcu_read_lock(); | |
545 | /* | |
8ddf9efe | 546 | * Check if we can balance. We can balance on the whole |
1da177e4 LT |
547 | * device if no resync is going on, or below the resync window. |
548 | * We take the first readable disk when above the resync window. | |
549 | */ | |
550 | retry: | |
d2eb35ac | 551 | sectors = r1_bio->sectors; |
76073054 | 552 | best_disk = -1; |
9dedf603 | 553 | best_dist_disk = -1; |
76073054 | 554 | best_dist = MaxSector; |
9dedf603 SL |
555 | best_pending_disk = -1; |
556 | min_pending = UINT_MAX; | |
d2eb35ac | 557 | best_good_sectors = 0; |
9dedf603 | 558 | has_nonrot_disk = 0; |
12cee5a8 | 559 | choose_next_idle = 0; |
2e52d449 | 560 | clear_bit(R1BIO_FailFast, &r1_bio->state); |
d2eb35ac | 561 | |
7d49ffcf GR |
562 | if ((conf->mddev->recovery_cp < this_sector + sectors) || |
563 | (mddev_is_clustered(conf->mddev) && | |
90382ed9 | 564 | md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector, |
7d49ffcf GR |
565 | this_sector + sectors))) |
566 | choose_first = 1; | |
567 | else | |
568 | choose_first = 0; | |
1da177e4 | 569 | |
be4d3280 | 570 | for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) { |
76073054 | 571 | sector_t dist; |
d2eb35ac N |
572 | sector_t first_bad; |
573 | int bad_sectors; | |
9dedf603 | 574 | unsigned int pending; |
12cee5a8 | 575 | bool nonrot; |
d2eb35ac | 576 | |
f3ac8bf7 N |
577 | rdev = rcu_dereference(conf->mirrors[disk].rdev); |
578 | if (r1_bio->bios[disk] == IO_BLOCKED | |
579 | || rdev == NULL | |
76073054 | 580 | || test_bit(Faulty, &rdev->flags)) |
f3ac8bf7 | 581 | continue; |
76073054 N |
582 | if (!test_bit(In_sync, &rdev->flags) && |
583 | rdev->recovery_offset < this_sector + sectors) | |
1da177e4 | 584 | continue; |
76073054 N |
585 | if (test_bit(WriteMostly, &rdev->flags)) { |
586 | /* Don't balance among write-mostly, just | |
587 | * use the first as a last resort */ | |
d1901ef0 | 588 | if (best_dist_disk < 0) { |
307729c8 N |
589 | if (is_badblock(rdev, this_sector, sectors, |
590 | &first_bad, &bad_sectors)) { | |
816b0acf | 591 | if (first_bad <= this_sector) |
307729c8 N |
592 | /* Cannot use this */ |
593 | continue; | |
594 | best_good_sectors = first_bad - this_sector; | |
595 | } else | |
596 | best_good_sectors = sectors; | |
d1901ef0 TH |
597 | best_dist_disk = disk; |
598 | best_pending_disk = disk; | |
307729c8 | 599 | } |
76073054 N |
600 | continue; |
601 | } | |
602 | /* This is a reasonable device to use. It might | |
603 | * even be best. | |
604 | */ | |
d2eb35ac N |
605 | if (is_badblock(rdev, this_sector, sectors, |
606 | &first_bad, &bad_sectors)) { | |
607 | if (best_dist < MaxSector) | |
608 | /* already have a better device */ | |
609 | continue; | |
610 | if (first_bad <= this_sector) { | |
611 | /* cannot read here. If this is the 'primary' | |
612 | * device, then we must not read beyond | |
613 | * bad_sectors from another device.. | |
614 | */ | |
615 | bad_sectors -= (this_sector - first_bad); | |
616 | if (choose_first && sectors > bad_sectors) | |
617 | sectors = bad_sectors; | |
618 | if (best_good_sectors > sectors) | |
619 | best_good_sectors = sectors; | |
620 | ||
621 | } else { | |
622 | sector_t good_sectors = first_bad - this_sector; | |
623 | if (good_sectors > best_good_sectors) { | |
624 | best_good_sectors = good_sectors; | |
625 | best_disk = disk; | |
626 | } | |
627 | if (choose_first) | |
628 | break; | |
629 | } | |
630 | continue; | |
631 | } else | |
632 | best_good_sectors = sectors; | |
633 | ||
2e52d449 N |
634 | if (best_disk >= 0) |
635 | /* At least two disks to choose from so failfast is OK */ | |
636 | set_bit(R1BIO_FailFast, &r1_bio->state); | |
637 | ||
12cee5a8 SL |
638 | nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev)); |
639 | has_nonrot_disk |= nonrot; | |
9dedf603 | 640 | pending = atomic_read(&rdev->nr_pending); |
76073054 | 641 | dist = abs(this_sector - conf->mirrors[disk].head_position); |
12cee5a8 | 642 | if (choose_first) { |
76073054 | 643 | best_disk = disk; |
1da177e4 LT |
644 | break; |
645 | } | |
12cee5a8 SL |
646 | /* Don't change to another disk for sequential reads */ |
647 | if (conf->mirrors[disk].next_seq_sect == this_sector | |
648 | || dist == 0) { | |
649 | int opt_iosize = bdev_io_opt(rdev->bdev) >> 9; | |
650 | struct raid1_info *mirror = &conf->mirrors[disk]; | |
651 | ||
652 | best_disk = disk; | |
653 | /* | |
654 | * If buffered sequential IO size exceeds optimal | |
655 | * iosize, check if there is idle disk. If yes, choose | |
656 | * the idle disk. read_balance could already choose an | |
657 | * idle disk before noticing it's a sequential IO in | |
658 | * this disk. This doesn't matter because this disk | |
659 | * will idle, next time it will be utilized after the | |
660 | * first disk has IO size exceeds optimal iosize. In | |
661 | * this way, iosize of the first disk will be optimal | |
662 | * iosize at least. iosize of the second disk might be | |
663 | * small, but not a big deal since when the second disk | |
664 | * starts IO, the first disk is likely still busy. | |
665 | */ | |
666 | if (nonrot && opt_iosize > 0 && | |
667 | mirror->seq_start != MaxSector && | |
668 | mirror->next_seq_sect > opt_iosize && | |
669 | mirror->next_seq_sect - opt_iosize >= | |
670 | mirror->seq_start) { | |
671 | choose_next_idle = 1; | |
672 | continue; | |
673 | } | |
674 | break; | |
675 | } | |
12cee5a8 SL |
676 | |
677 | if (choose_next_idle) | |
678 | continue; | |
9dedf603 SL |
679 | |
680 | if (min_pending > pending) { | |
681 | min_pending = pending; | |
682 | best_pending_disk = disk; | |
683 | } | |
684 | ||
76073054 N |
685 | if (dist < best_dist) { |
686 | best_dist = dist; | |
9dedf603 | 687 | best_dist_disk = disk; |
1da177e4 | 688 | } |
f3ac8bf7 | 689 | } |
1da177e4 | 690 | |
9dedf603 SL |
691 | /* |
692 | * If all disks are rotational, choose the closest disk. If any disk is | |
693 | * non-rotational, choose the disk with less pending request even the | |
694 | * disk is rotational, which might/might not be optimal for raids with | |
695 | * mixed ratation/non-rotational disks depending on workload. | |
696 | */ | |
697 | if (best_disk == -1) { | |
2e52d449 | 698 | if (has_nonrot_disk || min_pending == 0) |
9dedf603 SL |
699 | best_disk = best_pending_disk; |
700 | else | |
701 | best_disk = best_dist_disk; | |
702 | } | |
703 | ||
76073054 N |
704 | if (best_disk >= 0) { |
705 | rdev = rcu_dereference(conf->mirrors[best_disk].rdev); | |
8ddf9efe N |
706 | if (!rdev) |
707 | goto retry; | |
708 | atomic_inc(&rdev->nr_pending); | |
d2eb35ac | 709 | sectors = best_good_sectors; |
12cee5a8 SL |
710 | |
711 | if (conf->mirrors[best_disk].next_seq_sect != this_sector) | |
712 | conf->mirrors[best_disk].seq_start = this_sector; | |
713 | ||
be4d3280 | 714 | conf->mirrors[best_disk].next_seq_sect = this_sector + sectors; |
1da177e4 LT |
715 | } |
716 | rcu_read_unlock(); | |
d2eb35ac | 717 | *max_sectors = sectors; |
1da177e4 | 718 | |
76073054 | 719 | return best_disk; |
1da177e4 LT |
720 | } |
721 | ||
5c675f83 | 722 | static int raid1_congested(struct mddev *mddev, int bits) |
0d129228 | 723 | { |
e8096360 | 724 | struct r1conf *conf = mddev->private; |
0d129228 N |
725 | int i, ret = 0; |
726 | ||
4452226e | 727 | if ((bits & (1 << WB_async_congested)) && |
34db0cd6 N |
728 | conf->pending_count >= max_queued_requests) |
729 | return 1; | |
730 | ||
0d129228 | 731 | rcu_read_lock(); |
f53e29fc | 732 | for (i = 0; i < conf->raid_disks * 2; i++) { |
3cb03002 | 733 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
0d129228 | 734 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
165125e1 | 735 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
0d129228 | 736 | |
1ed7242e JB |
737 | BUG_ON(!q); |
738 | ||
0d129228 N |
739 | /* Note the '|| 1' - when read_balance prefers |
740 | * non-congested targets, it can be removed | |
741 | */ | |
4452226e | 742 | if ((bits & (1 << WB_async_congested)) || 1) |
0d129228 N |
743 | ret |= bdi_congested(&q->backing_dev_info, bits); |
744 | else | |
745 | ret &= bdi_congested(&q->backing_dev_info, bits); | |
746 | } | |
747 | } | |
748 | rcu_read_unlock(); | |
749 | return ret; | |
750 | } | |
0d129228 | 751 | |
e8096360 | 752 | static void flush_pending_writes(struct r1conf *conf) |
a35e63ef N |
753 | { |
754 | /* Any writes that have been queued but are awaiting | |
755 | * bitmap updates get flushed here. | |
a35e63ef | 756 | */ |
a35e63ef N |
757 | spin_lock_irq(&conf->device_lock); |
758 | ||
759 | if (conf->pending_bio_list.head) { | |
760 | struct bio *bio; | |
761 | bio = bio_list_get(&conf->pending_bio_list); | |
34db0cd6 | 762 | conf->pending_count = 0; |
a35e63ef N |
763 | spin_unlock_irq(&conf->device_lock); |
764 | /* flush any pending bitmap writes to | |
765 | * disk before proceeding w/ I/O */ | |
766 | bitmap_unplug(conf->mddev->bitmap); | |
34db0cd6 | 767 | wake_up(&conf->wait_barrier); |
a35e63ef N |
768 | |
769 | while (bio) { /* submit pending writes */ | |
770 | struct bio *next = bio->bi_next; | |
5e2c7a36 | 771 | struct md_rdev *rdev = (void*)bio->bi_bdev; |
a35e63ef | 772 | bio->bi_next = NULL; |
5e2c7a36 N |
773 | bio->bi_bdev = rdev->bdev; |
774 | if (test_bit(Faulty, &rdev->flags)) { | |
775 | bio->bi_error = -EIO; | |
776 | bio_endio(bio); | |
777 | } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) && | |
778 | !blk_queue_discard(bdev_get_queue(bio->bi_bdev)))) | |
2ff8cc2c | 779 | /* Just ignore it */ |
4246a0b6 | 780 | bio_endio(bio); |
2ff8cc2c SL |
781 | else |
782 | generic_make_request(bio); | |
a35e63ef N |
783 | bio = next; |
784 | } | |
a35e63ef N |
785 | } else |
786 | spin_unlock_irq(&conf->device_lock); | |
7eaceacc JA |
787 | } |
788 | ||
17999be4 N |
789 | /* Barriers.... |
790 | * Sometimes we need to suspend IO while we do something else, | |
791 | * either some resync/recovery, or reconfigure the array. | |
792 | * To do this we raise a 'barrier'. | |
793 | * The 'barrier' is a counter that can be raised multiple times | |
794 | * to count how many activities are happening which preclude | |
795 | * normal IO. | |
796 | * We can only raise the barrier if there is no pending IO. | |
797 | * i.e. if nr_pending == 0. | |
798 | * We choose only to raise the barrier if no-one is waiting for the | |
799 | * barrier to go down. This means that as soon as an IO request | |
800 | * is ready, no other operations which require a barrier will start | |
801 | * until the IO request has had a chance. | |
802 | * | |
803 | * So: regular IO calls 'wait_barrier'. When that returns there | |
804 | * is no backgroup IO happening, It must arrange to call | |
805 | * allow_barrier when it has finished its IO. | |
806 | * backgroup IO calls must call raise_barrier. Once that returns | |
807 | * there is no normal IO happeing. It must arrange to call | |
808 | * lower_barrier when the particular background IO completes. | |
1da177e4 | 809 | */ |
c2fd4c94 | 810 | static void raise_barrier(struct r1conf *conf, sector_t sector_nr) |
1da177e4 LT |
811 | { |
812 | spin_lock_irq(&conf->resync_lock); | |
17999be4 N |
813 | |
814 | /* Wait until no block IO is waiting */ | |
815 | wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting, | |
eed8c02e | 816 | conf->resync_lock); |
17999be4 N |
817 | |
818 | /* block any new IO from starting */ | |
819 | conf->barrier++; | |
c2fd4c94 | 820 | conf->next_resync = sector_nr; |
17999be4 | 821 | |
79ef3a8a | 822 | /* For these conditions we must wait: |
823 | * A: while the array is in frozen state | |
824 | * B: while barrier >= RESYNC_DEPTH, meaning resync reach | |
825 | * the max count which allowed. | |
826 | * C: next_resync + RESYNC_SECTORS > start_next_window, meaning | |
827 | * next resync will reach to the window which normal bios are | |
828 | * handling. | |
2f73d3c5 | 829 | * D: while there are any active requests in the current window. |
79ef3a8a | 830 | */ |
17999be4 | 831 | wait_event_lock_irq(conf->wait_barrier, |
b364e3d0 | 832 | !conf->array_frozen && |
79ef3a8a | 833 | conf->barrier < RESYNC_DEPTH && |
2f73d3c5 | 834 | conf->current_window_requests == 0 && |
79ef3a8a | 835 | (conf->start_next_window >= |
836 | conf->next_resync + RESYNC_SECTORS), | |
eed8c02e | 837 | conf->resync_lock); |
17999be4 | 838 | |
34e97f17 | 839 | conf->nr_pending++; |
17999be4 N |
840 | spin_unlock_irq(&conf->resync_lock); |
841 | } | |
842 | ||
e8096360 | 843 | static void lower_barrier(struct r1conf *conf) |
17999be4 N |
844 | { |
845 | unsigned long flags; | |
709ae487 | 846 | BUG_ON(conf->barrier <= 0); |
17999be4 N |
847 | spin_lock_irqsave(&conf->resync_lock, flags); |
848 | conf->barrier--; | |
34e97f17 | 849 | conf->nr_pending--; |
17999be4 N |
850 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
851 | wake_up(&conf->wait_barrier); | |
852 | } | |
853 | ||
79ef3a8a | 854 | static bool need_to_wait_for_sync(struct r1conf *conf, struct bio *bio) |
17999be4 | 855 | { |
79ef3a8a | 856 | bool wait = false; |
857 | ||
858 | if (conf->array_frozen || !bio) | |
859 | wait = true; | |
860 | else if (conf->barrier && bio_data_dir(bio) == WRITE) { | |
23554960 N |
861 | if ((conf->mddev->curr_resync_completed |
862 | >= bio_end_sector(bio)) || | |
f2c771a6 | 863 | (conf->start_next_window + NEXT_NORMALIO_DISTANCE |
23554960 | 864 | <= bio->bi_iter.bi_sector)) |
79ef3a8a | 865 | wait = false; |
866 | else | |
867 | wait = true; | |
868 | } | |
869 | ||
870 | return wait; | |
871 | } | |
872 | ||
873 | static sector_t wait_barrier(struct r1conf *conf, struct bio *bio) | |
874 | { | |
875 | sector_t sector = 0; | |
876 | ||
17999be4 | 877 | spin_lock_irq(&conf->resync_lock); |
79ef3a8a | 878 | if (need_to_wait_for_sync(conf, bio)) { |
17999be4 | 879 | conf->nr_waiting++; |
d6b42dcb N |
880 | /* Wait for the barrier to drop. |
881 | * However if there are already pending | |
882 | * requests (preventing the barrier from | |
883 | * rising completely), and the | |
5965b642 | 884 | * per-process bio queue isn't empty, |
d6b42dcb | 885 | * then don't wait, as we need to empty |
5965b642 N |
886 | * that queue to allow conf->start_next_window |
887 | * to increase. | |
d6b42dcb | 888 | */ |
578b54ad | 889 | raid1_log(conf->mddev, "wait barrier"); |
d6b42dcb | 890 | wait_event_lock_irq(conf->wait_barrier, |
b364e3d0 | 891 | !conf->array_frozen && |
892 | (!conf->barrier || | |
5965b642 N |
893 | ((conf->start_next_window < |
894 | conf->next_resync + RESYNC_SECTORS) && | |
895 | current->bio_list && | |
896 | !bio_list_empty(current->bio_list))), | |
eed8c02e | 897 | conf->resync_lock); |
17999be4 | 898 | conf->nr_waiting--; |
1da177e4 | 899 | } |
79ef3a8a | 900 | |
901 | if (bio && bio_data_dir(bio) == WRITE) { | |
e8ff8bf0 | 902 | if (bio->bi_iter.bi_sector >= conf->next_resync) { |
79ef3a8a | 903 | if (conf->start_next_window == MaxSector) |
904 | conf->start_next_window = | |
905 | conf->next_resync + | |
906 | NEXT_NORMALIO_DISTANCE; | |
907 | ||
908 | if ((conf->start_next_window + NEXT_NORMALIO_DISTANCE) | |
4f024f37 | 909 | <= bio->bi_iter.bi_sector) |
79ef3a8a | 910 | conf->next_window_requests++; |
911 | else | |
912 | conf->current_window_requests++; | |
79ef3a8a | 913 | sector = conf->start_next_window; |
41a336e0 | 914 | } |
79ef3a8a | 915 | } |
916 | ||
17999be4 | 917 | conf->nr_pending++; |
1da177e4 | 918 | spin_unlock_irq(&conf->resync_lock); |
79ef3a8a | 919 | return sector; |
1da177e4 LT |
920 | } |
921 | ||
79ef3a8a | 922 | static void allow_barrier(struct r1conf *conf, sector_t start_next_window, |
923 | sector_t bi_sector) | |
17999be4 N |
924 | { |
925 | unsigned long flags; | |
79ef3a8a | 926 | |
17999be4 N |
927 | spin_lock_irqsave(&conf->resync_lock, flags); |
928 | conf->nr_pending--; | |
79ef3a8a | 929 | if (start_next_window) { |
930 | if (start_next_window == conf->start_next_window) { | |
931 | if (conf->start_next_window + NEXT_NORMALIO_DISTANCE | |
932 | <= bi_sector) | |
933 | conf->next_window_requests--; | |
934 | else | |
935 | conf->current_window_requests--; | |
936 | } else | |
937 | conf->current_window_requests--; | |
938 | ||
939 | if (!conf->current_window_requests) { | |
940 | if (conf->next_window_requests) { | |
941 | conf->current_window_requests = | |
942 | conf->next_window_requests; | |
943 | conf->next_window_requests = 0; | |
944 | conf->start_next_window += | |
945 | NEXT_NORMALIO_DISTANCE; | |
946 | } else | |
947 | conf->start_next_window = MaxSector; | |
948 | } | |
949 | } | |
17999be4 N |
950 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
951 | wake_up(&conf->wait_barrier); | |
952 | } | |
953 | ||
e2d59925 | 954 | static void freeze_array(struct r1conf *conf, int extra) |
ddaf22ab N |
955 | { |
956 | /* stop syncio and normal IO and wait for everything to | |
957 | * go quite. | |
b364e3d0 | 958 | * We wait until nr_pending match nr_queued+extra |
1c830532 N |
959 | * This is called in the context of one normal IO request |
960 | * that has failed. Thus any sync request that might be pending | |
961 | * will be blocked by nr_pending, and we need to wait for | |
962 | * pending IO requests to complete or be queued for re-try. | |
e2d59925 | 963 | * Thus the number queued (nr_queued) plus this request (extra) |
1c830532 N |
964 | * must match the number of pending IOs (nr_pending) before |
965 | * we continue. | |
ddaf22ab N |
966 | */ |
967 | spin_lock_irq(&conf->resync_lock); | |
b364e3d0 | 968 | conf->array_frozen = 1; |
578b54ad | 969 | raid1_log(conf->mddev, "wait freeze"); |
eed8c02e | 970 | wait_event_lock_irq_cmd(conf->wait_barrier, |
e2d59925 | 971 | conf->nr_pending == conf->nr_queued+extra, |
eed8c02e LC |
972 | conf->resync_lock, |
973 | flush_pending_writes(conf)); | |
ddaf22ab N |
974 | spin_unlock_irq(&conf->resync_lock); |
975 | } | |
e8096360 | 976 | static void unfreeze_array(struct r1conf *conf) |
ddaf22ab N |
977 | { |
978 | /* reverse the effect of the freeze */ | |
979 | spin_lock_irq(&conf->resync_lock); | |
b364e3d0 | 980 | conf->array_frozen = 0; |
ddaf22ab N |
981 | wake_up(&conf->wait_barrier); |
982 | spin_unlock_irq(&conf->resync_lock); | |
983 | } | |
984 | ||
f72ffdd6 | 985 | /* duplicate the data pages for behind I/O |
4e78064f | 986 | */ |
9f2c9d12 | 987 | static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio) |
4b6d287f N |
988 | { |
989 | int i; | |
990 | struct bio_vec *bvec; | |
2ca68f5e | 991 | struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec), |
4b6d287f | 992 | GFP_NOIO); |
2ca68f5e | 993 | if (unlikely(!bvecs)) |
af6d7b76 | 994 | return; |
4b6d287f | 995 | |
cb34e057 | 996 | bio_for_each_segment_all(bvec, bio, i) { |
2ca68f5e N |
997 | bvecs[i] = *bvec; |
998 | bvecs[i].bv_page = alloc_page(GFP_NOIO); | |
999 | if (unlikely(!bvecs[i].bv_page)) | |
4b6d287f | 1000 | goto do_sync_io; |
2ca68f5e N |
1001 | memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset, |
1002 | kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len); | |
1003 | kunmap(bvecs[i].bv_page); | |
4b6d287f N |
1004 | kunmap(bvec->bv_page); |
1005 | } | |
2ca68f5e | 1006 | r1_bio->behind_bvecs = bvecs; |
af6d7b76 N |
1007 | r1_bio->behind_page_count = bio->bi_vcnt; |
1008 | set_bit(R1BIO_BehindIO, &r1_bio->state); | |
1009 | return; | |
4b6d287f N |
1010 | |
1011 | do_sync_io: | |
af6d7b76 | 1012 | for (i = 0; i < bio->bi_vcnt; i++) |
2ca68f5e N |
1013 | if (bvecs[i].bv_page) |
1014 | put_page(bvecs[i].bv_page); | |
1015 | kfree(bvecs); | |
4f024f37 KO |
1016 | pr_debug("%dB behind alloc failed, doing sync I/O\n", |
1017 | bio->bi_iter.bi_size); | |
4b6d287f N |
1018 | } |
1019 | ||
f54a9d0e N |
1020 | struct raid1_plug_cb { |
1021 | struct blk_plug_cb cb; | |
1022 | struct bio_list pending; | |
1023 | int pending_cnt; | |
1024 | }; | |
1025 | ||
1026 | static void raid1_unplug(struct blk_plug_cb *cb, bool from_schedule) | |
1027 | { | |
1028 | struct raid1_plug_cb *plug = container_of(cb, struct raid1_plug_cb, | |
1029 | cb); | |
1030 | struct mddev *mddev = plug->cb.data; | |
1031 | struct r1conf *conf = mddev->private; | |
1032 | struct bio *bio; | |
1033 | ||
874807a8 | 1034 | if (from_schedule || current->bio_list) { |
f54a9d0e N |
1035 | spin_lock_irq(&conf->device_lock); |
1036 | bio_list_merge(&conf->pending_bio_list, &plug->pending); | |
1037 | conf->pending_count += plug->pending_cnt; | |
1038 | spin_unlock_irq(&conf->device_lock); | |
ee0b0244 | 1039 | wake_up(&conf->wait_barrier); |
f54a9d0e N |
1040 | md_wakeup_thread(mddev->thread); |
1041 | kfree(plug); | |
1042 | return; | |
1043 | } | |
1044 | ||
1045 | /* we aren't scheduling, so we can do the write-out directly. */ | |
1046 | bio = bio_list_get(&plug->pending); | |
1047 | bitmap_unplug(mddev->bitmap); | |
1048 | wake_up(&conf->wait_barrier); | |
1049 | ||
1050 | while (bio) { /* submit pending writes */ | |
1051 | struct bio *next = bio->bi_next; | |
5e2c7a36 | 1052 | struct md_rdev *rdev = (void*)bio->bi_bdev; |
f54a9d0e | 1053 | bio->bi_next = NULL; |
5e2c7a36 N |
1054 | bio->bi_bdev = rdev->bdev; |
1055 | if (test_bit(Faulty, &rdev->flags)) { | |
1056 | bio->bi_error = -EIO; | |
1057 | bio_endio(bio); | |
1058 | } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) && | |
1059 | !blk_queue_discard(bdev_get_queue(bio->bi_bdev)))) | |
32f9f570 | 1060 | /* Just ignore it */ |
4246a0b6 | 1061 | bio_endio(bio); |
32f9f570 SL |
1062 | else |
1063 | generic_make_request(bio); | |
f54a9d0e N |
1064 | bio = next; |
1065 | } | |
1066 | kfree(plug); | |
1067 | } | |
1068 | ||
3b046a97 RL |
1069 | static void raid1_read_request(struct mddev *mddev, struct bio *bio, |
1070 | struct r1bio *r1_bio) | |
1da177e4 | 1071 | { |
e8096360 | 1072 | struct r1conf *conf = mddev->private; |
0eaf822c | 1073 | struct raid1_info *mirror; |
1da177e4 | 1074 | struct bio *read_bio; |
3b046a97 RL |
1075 | struct bitmap *bitmap = mddev->bitmap; |
1076 | const int op = bio_op(bio); | |
1077 | const unsigned long do_sync = (bio->bi_opf & REQ_SYNC); | |
1078 | int sectors_handled; | |
1079 | int max_sectors; | |
1080 | int rdisk; | |
1081 | ||
1082 | wait_barrier(conf, bio); | |
1083 | ||
1084 | read_again: | |
1085 | rdisk = read_balance(conf, r1_bio, &max_sectors); | |
1086 | ||
1087 | if (rdisk < 0) { | |
1088 | /* couldn't find anywhere to read from */ | |
1089 | raid_end_bio_io(r1_bio); | |
1090 | return; | |
1091 | } | |
1092 | mirror = conf->mirrors + rdisk; | |
1093 | ||
1094 | if (test_bit(WriteMostly, &mirror->rdev->flags) && | |
1095 | bitmap) { | |
1096 | /* | |
1097 | * Reading from a write-mostly device must take care not to | |
1098 | * over-take any writes that are 'behind' | |
1099 | */ | |
1100 | raid1_log(mddev, "wait behind writes"); | |
1101 | wait_event(bitmap->behind_wait, | |
1102 | atomic_read(&bitmap->behind_writes) == 0); | |
1103 | } | |
1104 | r1_bio->read_disk = rdisk; | |
1105 | r1_bio->start_next_window = 0; | |
1106 | ||
1107 | read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); | |
1108 | bio_trim(read_bio, r1_bio->sector - bio->bi_iter.bi_sector, | |
1109 | max_sectors); | |
1110 | ||
1111 | r1_bio->bios[rdisk] = read_bio; | |
1112 | ||
1113 | read_bio->bi_iter.bi_sector = r1_bio->sector + | |
1114 | mirror->rdev->data_offset; | |
1115 | read_bio->bi_bdev = mirror->rdev->bdev; | |
1116 | read_bio->bi_end_io = raid1_end_read_request; | |
1117 | bio_set_op_attrs(read_bio, op, do_sync); | |
1118 | if (test_bit(FailFast, &mirror->rdev->flags) && | |
1119 | test_bit(R1BIO_FailFast, &r1_bio->state)) | |
1120 | read_bio->bi_opf |= MD_FAILFAST; | |
1121 | read_bio->bi_private = r1_bio; | |
1122 | ||
1123 | if (mddev->gendisk) | |
1124 | trace_block_bio_remap(bdev_get_queue(read_bio->bi_bdev), | |
1125 | read_bio, disk_devt(mddev->gendisk), | |
1126 | r1_bio->sector); | |
1127 | ||
1128 | if (max_sectors < r1_bio->sectors) { | |
1129 | /* | |
1130 | * could not read all from this device, so we will need another | |
1131 | * r1_bio. | |
1132 | */ | |
1133 | sectors_handled = (r1_bio->sector + max_sectors | |
1134 | - bio->bi_iter.bi_sector); | |
1135 | r1_bio->sectors = max_sectors; | |
1136 | spin_lock_irq(&conf->device_lock); | |
1137 | if (bio->bi_phys_segments == 0) | |
1138 | bio->bi_phys_segments = 2; | |
1139 | else | |
1140 | bio->bi_phys_segments++; | |
1141 | spin_unlock_irq(&conf->device_lock); | |
1142 | ||
1143 | /* | |
1144 | * Cannot call generic_make_request directly as that will be | |
1145 | * queued in __make_request and subsequent mempool_alloc might | |
1146 | * block waiting for it. So hand bio over to raid1d. | |
1147 | */ | |
1148 | reschedule_retry(r1_bio); | |
1149 | ||
1150 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); | |
1151 | ||
1152 | r1_bio->master_bio = bio; | |
1153 | r1_bio->sectors = bio_sectors(bio) - sectors_handled; | |
1154 | r1_bio->state = 0; | |
1155 | r1_bio->mddev = mddev; | |
1156 | r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled; | |
1157 | goto read_again; | |
1158 | } else | |
1159 | generic_make_request(read_bio); | |
1160 | } | |
1161 | ||
1162 | static void raid1_write_request(struct mddev *mddev, struct bio *bio, | |
1163 | struct r1bio *r1_bio) | |
1164 | { | |
1165 | struct r1conf *conf = mddev->private; | |
1f68f0c4 | 1166 | int i, disks; |
3b046a97 | 1167 | struct bitmap *bitmap = mddev->bitmap; |
191ea9b2 | 1168 | unsigned long flags; |
796a5cf0 | 1169 | const int op = bio_op(bio); |
1eff9d32 JA |
1170 | const unsigned long do_sync = (bio->bi_opf & REQ_SYNC); |
1171 | const unsigned long do_flush_fua = (bio->bi_opf & | |
28a8f0d3 | 1172 | (REQ_PREFLUSH | REQ_FUA)); |
3cb03002 | 1173 | struct md_rdev *blocked_rdev; |
f54a9d0e N |
1174 | struct blk_plug_cb *cb; |
1175 | struct raid1_plug_cb *plug = NULL; | |
1f68f0c4 N |
1176 | int first_clone; |
1177 | int sectors_handled; | |
1178 | int max_sectors; | |
79ef3a8a | 1179 | sector_t start_next_window; |
191ea9b2 | 1180 | |
1da177e4 LT |
1181 | /* |
1182 | * Register the new request and wait if the reconstruction | |
1183 | * thread has put up a bar for new requests. | |
1184 | * Continue immediately if no resync is active currently. | |
1185 | */ | |
62de608d | 1186 | |
3d310eb7 N |
1187 | md_write_start(mddev, bio); /* wait on superblock update early */ |
1188 | ||
3b046a97 | 1189 | if ((bio_end_sector(bio) > mddev->suspend_lo && |
589a1c49 GR |
1190 | bio->bi_iter.bi_sector < mddev->suspend_hi) || |
1191 | (mddev_is_clustered(mddev) && | |
90382ed9 | 1192 | md_cluster_ops->area_resyncing(mddev, WRITE, |
3b046a97 RL |
1193 | bio->bi_iter.bi_sector, bio_end_sector(bio)))) { |
1194 | ||
1195 | /* | |
1196 | * As the suspend_* range is controlled by userspace, we want | |
1197 | * an interruptible wait. | |
6eef4b21 N |
1198 | */ |
1199 | DEFINE_WAIT(w); | |
1200 | for (;;) { | |
1201 | flush_signals(current); | |
1202 | prepare_to_wait(&conf->wait_barrier, | |
1203 | &w, TASK_INTERRUPTIBLE); | |
f73a1c7d | 1204 | if (bio_end_sector(bio) <= mddev->suspend_lo || |
589a1c49 GR |
1205 | bio->bi_iter.bi_sector >= mddev->suspend_hi || |
1206 | (mddev_is_clustered(mddev) && | |
90382ed9 | 1207 | !md_cluster_ops->area_resyncing(mddev, WRITE, |
3b046a97 RL |
1208 | bio->bi_iter.bi_sector, |
1209 | bio_end_sector(bio)))) | |
6eef4b21 N |
1210 | break; |
1211 | schedule(); | |
1212 | } | |
1213 | finish_wait(&conf->wait_barrier, &w); | |
1214 | } | |
79ef3a8a | 1215 | start_next_window = wait_barrier(conf, bio); |
1da177e4 | 1216 | |
34db0cd6 N |
1217 | if (conf->pending_count >= max_queued_requests) { |
1218 | md_wakeup_thread(mddev->thread); | |
578b54ad | 1219 | raid1_log(mddev, "wait queued"); |
34db0cd6 N |
1220 | wait_event(conf->wait_barrier, |
1221 | conf->pending_count < max_queued_requests); | |
1222 | } | |
1f68f0c4 | 1223 | /* first select target devices under rcu_lock and |
1da177e4 LT |
1224 | * inc refcount on their rdev. Record them by setting |
1225 | * bios[x] to bio | |
1f68f0c4 N |
1226 | * If there are known/acknowledged bad blocks on any device on |
1227 | * which we have seen a write error, we want to avoid writing those | |
1228 | * blocks. | |
1229 | * This potentially requires several writes to write around | |
1230 | * the bad blocks. Each set of writes gets it's own r1bio | |
1231 | * with a set of bios attached. | |
1da177e4 | 1232 | */ |
c3b328ac | 1233 | |
8f19ccb2 | 1234 | disks = conf->raid_disks * 2; |
6bfe0b49 | 1235 | retry_write: |
79ef3a8a | 1236 | r1_bio->start_next_window = start_next_window; |
6bfe0b49 | 1237 | blocked_rdev = NULL; |
1da177e4 | 1238 | rcu_read_lock(); |
1f68f0c4 | 1239 | max_sectors = r1_bio->sectors; |
1da177e4 | 1240 | for (i = 0; i < disks; i++) { |
3cb03002 | 1241 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
6bfe0b49 DW |
1242 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
1243 | atomic_inc(&rdev->nr_pending); | |
1244 | blocked_rdev = rdev; | |
1245 | break; | |
1246 | } | |
1f68f0c4 | 1247 | r1_bio->bios[i] = NULL; |
8ae12666 | 1248 | if (!rdev || test_bit(Faulty, &rdev->flags)) { |
8f19ccb2 N |
1249 | if (i < conf->raid_disks) |
1250 | set_bit(R1BIO_Degraded, &r1_bio->state); | |
1f68f0c4 N |
1251 | continue; |
1252 | } | |
1253 | ||
1254 | atomic_inc(&rdev->nr_pending); | |
1255 | if (test_bit(WriteErrorSeen, &rdev->flags)) { | |
1256 | sector_t first_bad; | |
1257 | int bad_sectors; | |
1258 | int is_bad; | |
1259 | ||
3b046a97 | 1260 | is_bad = is_badblock(rdev, r1_bio->sector, max_sectors, |
1f68f0c4 N |
1261 | &first_bad, &bad_sectors); |
1262 | if (is_bad < 0) { | |
1263 | /* mustn't write here until the bad block is | |
1264 | * acknowledged*/ | |
1265 | set_bit(BlockedBadBlocks, &rdev->flags); | |
1266 | blocked_rdev = rdev; | |
1267 | break; | |
1268 | } | |
1269 | if (is_bad && first_bad <= r1_bio->sector) { | |
1270 | /* Cannot write here at all */ | |
1271 | bad_sectors -= (r1_bio->sector - first_bad); | |
1272 | if (bad_sectors < max_sectors) | |
1273 | /* mustn't write more than bad_sectors | |
1274 | * to other devices yet | |
1275 | */ | |
1276 | max_sectors = bad_sectors; | |
03c902e1 | 1277 | rdev_dec_pending(rdev, mddev); |
1f68f0c4 N |
1278 | /* We don't set R1BIO_Degraded as that |
1279 | * only applies if the disk is | |
1280 | * missing, so it might be re-added, | |
1281 | * and we want to know to recover this | |
1282 | * chunk. | |
1283 | * In this case the device is here, | |
1284 | * and the fact that this chunk is not | |
1285 | * in-sync is recorded in the bad | |
1286 | * block log | |
1287 | */ | |
1288 | continue; | |
964147d5 | 1289 | } |
1f68f0c4 N |
1290 | if (is_bad) { |
1291 | int good_sectors = first_bad - r1_bio->sector; | |
1292 | if (good_sectors < max_sectors) | |
1293 | max_sectors = good_sectors; | |
1294 | } | |
1295 | } | |
1296 | r1_bio->bios[i] = bio; | |
1da177e4 LT |
1297 | } |
1298 | rcu_read_unlock(); | |
1299 | ||
6bfe0b49 DW |
1300 | if (unlikely(blocked_rdev)) { |
1301 | /* Wait for this device to become unblocked */ | |
1302 | int j; | |
79ef3a8a | 1303 | sector_t old = start_next_window; |
6bfe0b49 DW |
1304 | |
1305 | for (j = 0; j < i; j++) | |
1306 | if (r1_bio->bios[j]) | |
1307 | rdev_dec_pending(conf->mirrors[j].rdev, mddev); | |
1f68f0c4 | 1308 | r1_bio->state = 0; |
4f024f37 | 1309 | allow_barrier(conf, start_next_window, bio->bi_iter.bi_sector); |
578b54ad | 1310 | raid1_log(mddev, "wait rdev %d blocked", blocked_rdev->raid_disk); |
6bfe0b49 | 1311 | md_wait_for_blocked_rdev(blocked_rdev, mddev); |
79ef3a8a | 1312 | start_next_window = wait_barrier(conf, bio); |
1313 | /* | |
1314 | * We must make sure the multi r1bios of bio have | |
1315 | * the same value of bi_phys_segments | |
1316 | */ | |
1317 | if (bio->bi_phys_segments && old && | |
1318 | old != start_next_window) | |
1319 | /* Wait for the former r1bio(s) to complete */ | |
1320 | wait_event(conf->wait_barrier, | |
1321 | bio->bi_phys_segments == 1); | |
6bfe0b49 DW |
1322 | goto retry_write; |
1323 | } | |
1324 | ||
1f68f0c4 N |
1325 | if (max_sectors < r1_bio->sectors) { |
1326 | /* We are splitting this write into multiple parts, so | |
1327 | * we need to prepare for allocating another r1_bio. | |
1328 | */ | |
1329 | r1_bio->sectors = max_sectors; | |
1330 | spin_lock_irq(&conf->device_lock); | |
1331 | if (bio->bi_phys_segments == 0) | |
1332 | bio->bi_phys_segments = 2; | |
1333 | else | |
1334 | bio->bi_phys_segments++; | |
1335 | spin_unlock_irq(&conf->device_lock); | |
191ea9b2 | 1336 | } |
4f024f37 | 1337 | sectors_handled = r1_bio->sector + max_sectors - bio->bi_iter.bi_sector; |
4b6d287f | 1338 | |
4e78064f | 1339 | atomic_set(&r1_bio->remaining, 1); |
4b6d287f | 1340 | atomic_set(&r1_bio->behind_remaining, 0); |
06d91a5f | 1341 | |
1f68f0c4 | 1342 | first_clone = 1; |
1da177e4 LT |
1343 | for (i = 0; i < disks; i++) { |
1344 | struct bio *mbio; | |
1345 | if (!r1_bio->bios[i]) | |
1346 | continue; | |
1347 | ||
a167f663 | 1348 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
3b046a97 RL |
1349 | bio_trim(mbio, r1_bio->sector - bio->bi_iter.bi_sector, |
1350 | max_sectors); | |
1f68f0c4 N |
1351 | |
1352 | if (first_clone) { | |
1353 | /* do behind I/O ? | |
1354 | * Not if there are too many, or cannot | |
1355 | * allocate memory, or a reader on WriteMostly | |
1356 | * is waiting for behind writes to flush */ | |
1357 | if (bitmap && | |
1358 | (atomic_read(&bitmap->behind_writes) | |
1359 | < mddev->bitmap_info.max_write_behind) && | |
1360 | !waitqueue_active(&bitmap->behind_wait)) | |
1361 | alloc_behind_pages(mbio, r1_bio); | |
1362 | ||
1363 | bitmap_startwrite(bitmap, r1_bio->sector, | |
1364 | r1_bio->sectors, | |
1365 | test_bit(R1BIO_BehindIO, | |
1366 | &r1_bio->state)); | |
1367 | first_clone = 0; | |
1368 | } | |
2ca68f5e | 1369 | if (r1_bio->behind_bvecs) { |
4b6d287f N |
1370 | struct bio_vec *bvec; |
1371 | int j; | |
1372 | ||
cb34e057 KO |
1373 | /* |
1374 | * We trimmed the bio, so _all is legit | |
4b6d287f | 1375 | */ |
d74c6d51 | 1376 | bio_for_each_segment_all(bvec, mbio, j) |
2ca68f5e | 1377 | bvec->bv_page = r1_bio->behind_bvecs[j].bv_page; |
4b6d287f N |
1378 | if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags)) |
1379 | atomic_inc(&r1_bio->behind_remaining); | |
1380 | } | |
1381 | ||
1f68f0c4 N |
1382 | r1_bio->bios[i] = mbio; |
1383 | ||
4f024f37 | 1384 | mbio->bi_iter.bi_sector = (r1_bio->sector + |
1f68f0c4 | 1385 | conf->mirrors[i].rdev->data_offset); |
109e3765 | 1386 | mbio->bi_bdev = conf->mirrors[i].rdev->bdev; |
1f68f0c4 | 1387 | mbio->bi_end_io = raid1_end_write_request; |
288dab8a | 1388 | bio_set_op_attrs(mbio, op, do_flush_fua | do_sync); |
212e7eb7 N |
1389 | if (test_bit(FailFast, &conf->mirrors[i].rdev->flags) && |
1390 | !test_bit(WriteMostly, &conf->mirrors[i].rdev->flags) && | |
1391 | conf->raid_disks - mddev->degraded > 1) | |
1392 | mbio->bi_opf |= MD_FAILFAST; | |
1f68f0c4 N |
1393 | mbio->bi_private = r1_bio; |
1394 | ||
1da177e4 | 1395 | atomic_inc(&r1_bio->remaining); |
f54a9d0e | 1396 | |
109e3765 N |
1397 | if (mddev->gendisk) |
1398 | trace_block_bio_remap(bdev_get_queue(mbio->bi_bdev), | |
1399 | mbio, disk_devt(mddev->gendisk), | |
1400 | r1_bio->sector); | |
1401 | /* flush_pending_writes() needs access to the rdev so...*/ | |
1402 | mbio->bi_bdev = (void*)conf->mirrors[i].rdev; | |
1403 | ||
f54a9d0e N |
1404 | cb = blk_check_plugged(raid1_unplug, mddev, sizeof(*plug)); |
1405 | if (cb) | |
1406 | plug = container_of(cb, struct raid1_plug_cb, cb); | |
1407 | else | |
1408 | plug = NULL; | |
4e78064f | 1409 | spin_lock_irqsave(&conf->device_lock, flags); |
f54a9d0e N |
1410 | if (plug) { |
1411 | bio_list_add(&plug->pending, mbio); | |
1412 | plug->pending_cnt++; | |
1413 | } else { | |
1414 | bio_list_add(&conf->pending_bio_list, mbio); | |
1415 | conf->pending_count++; | |
1416 | } | |
4e78064f | 1417 | spin_unlock_irqrestore(&conf->device_lock, flags); |
f54a9d0e | 1418 | if (!plug) |
b357f04a | 1419 | md_wakeup_thread(mddev->thread); |
1da177e4 | 1420 | } |
079fa166 N |
1421 | /* Mustn't call r1_bio_write_done before this next test, |
1422 | * as it could result in the bio being freed. | |
1423 | */ | |
aa8b57aa | 1424 | if (sectors_handled < bio_sectors(bio)) { |
079fa166 | 1425 | r1_bio_write_done(r1_bio); |
1f68f0c4 N |
1426 | /* We need another r1_bio. It has already been counted |
1427 | * in bio->bi_phys_segments | |
1428 | */ | |
1429 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); | |
1430 | r1_bio->master_bio = bio; | |
aa8b57aa | 1431 | r1_bio->sectors = bio_sectors(bio) - sectors_handled; |
1f68f0c4 N |
1432 | r1_bio->state = 0; |
1433 | r1_bio->mddev = mddev; | |
4f024f37 | 1434 | r1_bio->sector = bio->bi_iter.bi_sector + sectors_handled; |
1f68f0c4 N |
1435 | goto retry_write; |
1436 | } | |
1437 | ||
079fa166 N |
1438 | r1_bio_write_done(r1_bio); |
1439 | ||
1440 | /* In case raid1d snuck in to freeze_array */ | |
1441 | wake_up(&conf->wait_barrier); | |
1da177e4 LT |
1442 | } |
1443 | ||
3b046a97 RL |
1444 | static void raid1_make_request(struct mddev *mddev, struct bio *bio) |
1445 | { | |
1446 | struct r1conf *conf = mddev->private; | |
1447 | struct r1bio *r1_bio; | |
1448 | ||
1449 | /* | |
1450 | * make_request() can abort the operation when read-ahead is being | |
1451 | * used and no empty request is available. | |
1452 | * | |
1453 | */ | |
1454 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); | |
1455 | ||
1456 | r1_bio->master_bio = bio; | |
1457 | r1_bio->sectors = bio_sectors(bio); | |
1458 | r1_bio->state = 0; | |
1459 | r1_bio->mddev = mddev; | |
1460 | r1_bio->sector = bio->bi_iter.bi_sector; | |
1461 | ||
1462 | /* | |
1463 | * We might need to issue multiple reads to different devices if there | |
1464 | * are bad blocks around, so we keep track of the number of reads in | |
1465 | * bio->bi_phys_segments. If this is 0, there is only one r1_bio and | |
1466 | * no locking will be needed when requests complete. If it is | |
1467 | * non-zero, then it is the number of not-completed requests. | |
1468 | */ | |
1469 | bio->bi_phys_segments = 0; | |
1470 | bio_clear_flag(bio, BIO_SEG_VALID); | |
1471 | ||
1472 | if (bio_data_dir(bio) == READ) | |
1473 | raid1_read_request(mddev, bio, r1_bio); | |
1474 | else | |
1475 | raid1_write_request(mddev, bio, r1_bio); | |
1476 | } | |
1477 | ||
849674e4 | 1478 | static void raid1_status(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 1479 | { |
e8096360 | 1480 | struct r1conf *conf = mddev->private; |
1da177e4 LT |
1481 | int i; |
1482 | ||
1483 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, | |
11ce99e6 | 1484 | conf->raid_disks - mddev->degraded); |
ddac7c7e N |
1485 | rcu_read_lock(); |
1486 | for (i = 0; i < conf->raid_disks; i++) { | |
3cb03002 | 1487 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
1da177e4 | 1488 | seq_printf(seq, "%s", |
ddac7c7e N |
1489 | rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
1490 | } | |
1491 | rcu_read_unlock(); | |
1da177e4 LT |
1492 | seq_printf(seq, "]"); |
1493 | } | |
1494 | ||
849674e4 | 1495 | static void raid1_error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1496 | { |
1497 | char b[BDEVNAME_SIZE]; | |
e8096360 | 1498 | struct r1conf *conf = mddev->private; |
423f04d6 | 1499 | unsigned long flags; |
1da177e4 LT |
1500 | |
1501 | /* | |
1502 | * If it is not operational, then we have already marked it as dead | |
1503 | * else if it is the last working disks, ignore the error, let the | |
1504 | * next level up know. | |
1505 | * else mark the drive as failed | |
1506 | */ | |
2e52d449 | 1507 | spin_lock_irqsave(&conf->device_lock, flags); |
b2d444d7 | 1508 | if (test_bit(In_sync, &rdev->flags) |
4044ba58 | 1509 | && (conf->raid_disks - mddev->degraded) == 1) { |
1da177e4 LT |
1510 | /* |
1511 | * Don't fail the drive, act as though we were just a | |
4044ba58 N |
1512 | * normal single drive. |
1513 | * However don't try a recovery from this drive as | |
1514 | * it is very likely to fail. | |
1da177e4 | 1515 | */ |
5389042f | 1516 | conf->recovery_disabled = mddev->recovery_disabled; |
2e52d449 | 1517 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 | 1518 | return; |
4044ba58 | 1519 | } |
de393cde | 1520 | set_bit(Blocked, &rdev->flags); |
c04be0aa | 1521 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
1da177e4 | 1522 | mddev->degraded++; |
dd00a99e | 1523 | set_bit(Faulty, &rdev->flags); |
dd00a99e N |
1524 | } else |
1525 | set_bit(Faulty, &rdev->flags); | |
423f04d6 | 1526 | spin_unlock_irqrestore(&conf->device_lock, flags); |
2446dba0 N |
1527 | /* |
1528 | * if recovery is running, make sure it aborts. | |
1529 | */ | |
1530 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
2953079c SL |
1531 | set_mask_bits(&mddev->sb_flags, 0, |
1532 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); | |
1d41c216 N |
1533 | pr_crit("md/raid1:%s: Disk failure on %s, disabling device.\n" |
1534 | "md/raid1:%s: Operation continuing on %d devices.\n", | |
1535 | mdname(mddev), bdevname(rdev->bdev, b), | |
1536 | mdname(mddev), conf->raid_disks - mddev->degraded); | |
1da177e4 LT |
1537 | } |
1538 | ||
e8096360 | 1539 | static void print_conf(struct r1conf *conf) |
1da177e4 LT |
1540 | { |
1541 | int i; | |
1da177e4 | 1542 | |
1d41c216 | 1543 | pr_debug("RAID1 conf printout:\n"); |
1da177e4 | 1544 | if (!conf) { |
1d41c216 | 1545 | pr_debug("(!conf)\n"); |
1da177e4 LT |
1546 | return; |
1547 | } | |
1d41c216 N |
1548 | pr_debug(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, |
1549 | conf->raid_disks); | |
1da177e4 | 1550 | |
ddac7c7e | 1551 | rcu_read_lock(); |
1da177e4 LT |
1552 | for (i = 0; i < conf->raid_disks; i++) { |
1553 | char b[BDEVNAME_SIZE]; | |
3cb03002 | 1554 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
ddac7c7e | 1555 | if (rdev) |
1d41c216 N |
1556 | pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n", |
1557 | i, !test_bit(In_sync, &rdev->flags), | |
1558 | !test_bit(Faulty, &rdev->flags), | |
1559 | bdevname(rdev->bdev,b)); | |
1da177e4 | 1560 | } |
ddac7c7e | 1561 | rcu_read_unlock(); |
1da177e4 LT |
1562 | } |
1563 | ||
e8096360 | 1564 | static void close_sync(struct r1conf *conf) |
1da177e4 | 1565 | { |
79ef3a8a | 1566 | wait_barrier(conf, NULL); |
1567 | allow_barrier(conf, 0, 0); | |
1da177e4 LT |
1568 | |
1569 | mempool_destroy(conf->r1buf_pool); | |
1570 | conf->r1buf_pool = NULL; | |
79ef3a8a | 1571 | |
669cc7ba | 1572 | spin_lock_irq(&conf->resync_lock); |
e8ff8bf0 | 1573 | conf->next_resync = MaxSector - 2 * NEXT_NORMALIO_DISTANCE; |
79ef3a8a | 1574 | conf->start_next_window = MaxSector; |
669cc7ba N |
1575 | conf->current_window_requests += |
1576 | conf->next_window_requests; | |
1577 | conf->next_window_requests = 0; | |
1578 | spin_unlock_irq(&conf->resync_lock); | |
1da177e4 LT |
1579 | } |
1580 | ||
fd01b88c | 1581 | static int raid1_spare_active(struct mddev *mddev) |
1da177e4 LT |
1582 | { |
1583 | int i; | |
e8096360 | 1584 | struct r1conf *conf = mddev->private; |
6b965620 N |
1585 | int count = 0; |
1586 | unsigned long flags; | |
1da177e4 LT |
1587 | |
1588 | /* | |
f72ffdd6 | 1589 | * Find all failed disks within the RAID1 configuration |
ddac7c7e N |
1590 | * and mark them readable. |
1591 | * Called under mddev lock, so rcu protection not needed. | |
423f04d6 N |
1592 | * device_lock used to avoid races with raid1_end_read_request |
1593 | * which expects 'In_sync' flags and ->degraded to be consistent. | |
1da177e4 | 1594 | */ |
423f04d6 | 1595 | spin_lock_irqsave(&conf->device_lock, flags); |
1da177e4 | 1596 | for (i = 0; i < conf->raid_disks; i++) { |
3cb03002 | 1597 | struct md_rdev *rdev = conf->mirrors[i].rdev; |
8c7a2c2b N |
1598 | struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev; |
1599 | if (repl | |
1aee41f6 | 1600 | && !test_bit(Candidate, &repl->flags) |
8c7a2c2b N |
1601 | && repl->recovery_offset == MaxSector |
1602 | && !test_bit(Faulty, &repl->flags) | |
1603 | && !test_and_set_bit(In_sync, &repl->flags)) { | |
1604 | /* replacement has just become active */ | |
1605 | if (!rdev || | |
1606 | !test_and_clear_bit(In_sync, &rdev->flags)) | |
1607 | count++; | |
1608 | if (rdev) { | |
1609 | /* Replaced device not technically | |
1610 | * faulty, but we need to be sure | |
1611 | * it gets removed and never re-added | |
1612 | */ | |
1613 | set_bit(Faulty, &rdev->flags); | |
1614 | sysfs_notify_dirent_safe( | |
1615 | rdev->sysfs_state); | |
1616 | } | |
1617 | } | |
ddac7c7e | 1618 | if (rdev |
61e4947c | 1619 | && rdev->recovery_offset == MaxSector |
ddac7c7e | 1620 | && !test_bit(Faulty, &rdev->flags) |
c04be0aa | 1621 | && !test_and_set_bit(In_sync, &rdev->flags)) { |
6b965620 | 1622 | count++; |
654e8b5a | 1623 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
1624 | } |
1625 | } | |
6b965620 N |
1626 | mddev->degraded -= count; |
1627 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 LT |
1628 | |
1629 | print_conf(conf); | |
6b965620 | 1630 | return count; |
1da177e4 LT |
1631 | } |
1632 | ||
fd01b88c | 1633 | static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1634 | { |
e8096360 | 1635 | struct r1conf *conf = mddev->private; |
199050ea | 1636 | int err = -EEXIST; |
41158c7e | 1637 | int mirror = 0; |
0eaf822c | 1638 | struct raid1_info *p; |
6c2fce2e | 1639 | int first = 0; |
30194636 | 1640 | int last = conf->raid_disks - 1; |
1da177e4 | 1641 | |
5389042f N |
1642 | if (mddev->recovery_disabled == conf->recovery_disabled) |
1643 | return -EBUSY; | |
1644 | ||
1501efad DW |
1645 | if (md_integrity_add_rdev(rdev, mddev)) |
1646 | return -ENXIO; | |
1647 | ||
6c2fce2e NB |
1648 | if (rdev->raid_disk >= 0) |
1649 | first = last = rdev->raid_disk; | |
1650 | ||
70bcecdb GR |
1651 | /* |
1652 | * find the disk ... but prefer rdev->saved_raid_disk | |
1653 | * if possible. | |
1654 | */ | |
1655 | if (rdev->saved_raid_disk >= 0 && | |
1656 | rdev->saved_raid_disk >= first && | |
1657 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) | |
1658 | first = last = rdev->saved_raid_disk; | |
1659 | ||
7ef449d1 N |
1660 | for (mirror = first; mirror <= last; mirror++) { |
1661 | p = conf->mirrors+mirror; | |
1662 | if (!p->rdev) { | |
1da177e4 | 1663 | |
9092c02d JB |
1664 | if (mddev->gendisk) |
1665 | disk_stack_limits(mddev->gendisk, rdev->bdev, | |
1666 | rdev->data_offset << 9); | |
1da177e4 LT |
1667 | |
1668 | p->head_position = 0; | |
1669 | rdev->raid_disk = mirror; | |
199050ea | 1670 | err = 0; |
6aea114a N |
1671 | /* As all devices are equivalent, we don't need a full recovery |
1672 | * if this was recently any drive of the array | |
1673 | */ | |
1674 | if (rdev->saved_raid_disk < 0) | |
41158c7e | 1675 | conf->fullsync = 1; |
d6065f7b | 1676 | rcu_assign_pointer(p->rdev, rdev); |
1da177e4 LT |
1677 | break; |
1678 | } | |
7ef449d1 N |
1679 | if (test_bit(WantReplacement, &p->rdev->flags) && |
1680 | p[conf->raid_disks].rdev == NULL) { | |
1681 | /* Add this device as a replacement */ | |
1682 | clear_bit(In_sync, &rdev->flags); | |
1683 | set_bit(Replacement, &rdev->flags); | |
1684 | rdev->raid_disk = mirror; | |
1685 | err = 0; | |
1686 | conf->fullsync = 1; | |
1687 | rcu_assign_pointer(p[conf->raid_disks].rdev, rdev); | |
1688 | break; | |
1689 | } | |
1690 | } | |
9092c02d | 1691 | if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev))) |
2ff8cc2c | 1692 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue); |
1da177e4 | 1693 | print_conf(conf); |
199050ea | 1694 | return err; |
1da177e4 LT |
1695 | } |
1696 | ||
b8321b68 | 1697 | static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 1698 | { |
e8096360 | 1699 | struct r1conf *conf = mddev->private; |
1da177e4 | 1700 | int err = 0; |
b8321b68 | 1701 | int number = rdev->raid_disk; |
0eaf822c | 1702 | struct raid1_info *p = conf->mirrors + number; |
1da177e4 | 1703 | |
b014f14c N |
1704 | if (rdev != p->rdev) |
1705 | p = conf->mirrors + conf->raid_disks + number; | |
1706 | ||
1da177e4 | 1707 | print_conf(conf); |
b8321b68 | 1708 | if (rdev == p->rdev) { |
b2d444d7 | 1709 | if (test_bit(In_sync, &rdev->flags) || |
1da177e4 LT |
1710 | atomic_read(&rdev->nr_pending)) { |
1711 | err = -EBUSY; | |
1712 | goto abort; | |
1713 | } | |
046abeed | 1714 | /* Only remove non-faulty devices if recovery |
dfc70645 N |
1715 | * is not possible. |
1716 | */ | |
1717 | if (!test_bit(Faulty, &rdev->flags) && | |
5389042f | 1718 | mddev->recovery_disabled != conf->recovery_disabled && |
dfc70645 N |
1719 | mddev->degraded < conf->raid_disks) { |
1720 | err = -EBUSY; | |
1721 | goto abort; | |
1722 | } | |
1da177e4 | 1723 | p->rdev = NULL; |
d787be40 N |
1724 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
1725 | synchronize_rcu(); | |
1726 | if (atomic_read(&rdev->nr_pending)) { | |
1727 | /* lost the race, try later */ | |
1728 | err = -EBUSY; | |
1729 | p->rdev = rdev; | |
1730 | goto abort; | |
1731 | } | |
1732 | } | |
1733 | if (conf->mirrors[conf->raid_disks + number].rdev) { | |
8c7a2c2b N |
1734 | /* We just removed a device that is being replaced. |
1735 | * Move down the replacement. We drain all IO before | |
1736 | * doing this to avoid confusion. | |
1737 | */ | |
1738 | struct md_rdev *repl = | |
1739 | conf->mirrors[conf->raid_disks + number].rdev; | |
e2d59925 | 1740 | freeze_array(conf, 0); |
8c7a2c2b N |
1741 | clear_bit(Replacement, &repl->flags); |
1742 | p->rdev = repl; | |
1743 | conf->mirrors[conf->raid_disks + number].rdev = NULL; | |
e2d59925 | 1744 | unfreeze_array(conf); |
8c7a2c2b N |
1745 | clear_bit(WantReplacement, &rdev->flags); |
1746 | } else | |
b014f14c | 1747 | clear_bit(WantReplacement, &rdev->flags); |
a91a2785 | 1748 | err = md_integrity_register(mddev); |
1da177e4 LT |
1749 | } |
1750 | abort: | |
1751 | ||
1752 | print_conf(conf); | |
1753 | return err; | |
1754 | } | |
1755 | ||
4246a0b6 | 1756 | static void end_sync_read(struct bio *bio) |
1da177e4 | 1757 | { |
9f2c9d12 | 1758 | struct r1bio *r1_bio = bio->bi_private; |
1da177e4 | 1759 | |
0fc280f6 | 1760 | update_head_pos(r1_bio->read_disk, r1_bio); |
ba3ae3be | 1761 | |
1da177e4 LT |
1762 | /* |
1763 | * we have read a block, now it needs to be re-written, | |
1764 | * or re-read if the read failed. | |
1765 | * We don't do much here, just schedule handling by raid1d | |
1766 | */ | |
4246a0b6 | 1767 | if (!bio->bi_error) |
1da177e4 | 1768 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
d11c171e N |
1769 | |
1770 | if (atomic_dec_and_test(&r1_bio->remaining)) | |
1771 | reschedule_retry(r1_bio); | |
1da177e4 LT |
1772 | } |
1773 | ||
4246a0b6 | 1774 | static void end_sync_write(struct bio *bio) |
1da177e4 | 1775 | { |
4246a0b6 | 1776 | int uptodate = !bio->bi_error; |
9f2c9d12 | 1777 | struct r1bio *r1_bio = bio->bi_private; |
fd01b88c | 1778 | struct mddev *mddev = r1_bio->mddev; |
e8096360 | 1779 | struct r1conf *conf = mddev->private; |
4367af55 N |
1780 | sector_t first_bad; |
1781 | int bad_sectors; | |
854abd75 | 1782 | struct md_rdev *rdev = conf->mirrors[find_bio_disk(r1_bio, bio)].rdev; |
ba3ae3be | 1783 | |
6b1117d5 | 1784 | if (!uptodate) { |
57dab0bd | 1785 | sector_t sync_blocks = 0; |
6b1117d5 N |
1786 | sector_t s = r1_bio->sector; |
1787 | long sectors_to_go = r1_bio->sectors; | |
1788 | /* make sure these bits doesn't get cleared. */ | |
1789 | do { | |
5e3db645 | 1790 | bitmap_end_sync(mddev->bitmap, s, |
6b1117d5 N |
1791 | &sync_blocks, 1); |
1792 | s += sync_blocks; | |
1793 | sectors_to_go -= sync_blocks; | |
1794 | } while (sectors_to_go > 0); | |
854abd75 N |
1795 | set_bit(WriteErrorSeen, &rdev->flags); |
1796 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) | |
19d67169 N |
1797 | set_bit(MD_RECOVERY_NEEDED, & |
1798 | mddev->recovery); | |
d8f05d29 | 1799 | set_bit(R1BIO_WriteError, &r1_bio->state); |
854abd75 | 1800 | } else if (is_badblock(rdev, r1_bio->sector, r1_bio->sectors, |
3a9f28a5 N |
1801 | &first_bad, &bad_sectors) && |
1802 | !is_badblock(conf->mirrors[r1_bio->read_disk].rdev, | |
1803 | r1_bio->sector, | |
1804 | r1_bio->sectors, | |
1805 | &first_bad, &bad_sectors) | |
1806 | ) | |
4367af55 | 1807 | set_bit(R1BIO_MadeGood, &r1_bio->state); |
e3b9703e | 1808 | |
1da177e4 | 1809 | if (atomic_dec_and_test(&r1_bio->remaining)) { |
4367af55 | 1810 | int s = r1_bio->sectors; |
d8f05d29 N |
1811 | if (test_bit(R1BIO_MadeGood, &r1_bio->state) || |
1812 | test_bit(R1BIO_WriteError, &r1_bio->state)) | |
4367af55 N |
1813 | reschedule_retry(r1_bio); |
1814 | else { | |
1815 | put_buf(r1_bio); | |
1816 | md_done_sync(mddev, s, uptodate); | |
1817 | } | |
1da177e4 | 1818 | } |
1da177e4 LT |
1819 | } |
1820 | ||
3cb03002 | 1821 | static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector, |
d8f05d29 N |
1822 | int sectors, struct page *page, int rw) |
1823 | { | |
796a5cf0 | 1824 | if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false)) |
d8f05d29 N |
1825 | /* success */ |
1826 | return 1; | |
19d67169 | 1827 | if (rw == WRITE) { |
d8f05d29 | 1828 | set_bit(WriteErrorSeen, &rdev->flags); |
19d67169 N |
1829 | if (!test_and_set_bit(WantReplacement, |
1830 | &rdev->flags)) | |
1831 | set_bit(MD_RECOVERY_NEEDED, & | |
1832 | rdev->mddev->recovery); | |
1833 | } | |
d8f05d29 N |
1834 | /* need to record an error - either for the block or the device */ |
1835 | if (!rdev_set_badblocks(rdev, sector, sectors, 0)) | |
1836 | md_error(rdev->mddev, rdev); | |
1837 | return 0; | |
1838 | } | |
1839 | ||
9f2c9d12 | 1840 | static int fix_sync_read_error(struct r1bio *r1_bio) |
1da177e4 | 1841 | { |
a68e5870 N |
1842 | /* Try some synchronous reads of other devices to get |
1843 | * good data, much like with normal read errors. Only | |
1844 | * read into the pages we already have so we don't | |
1845 | * need to re-issue the read request. | |
1846 | * We don't need to freeze the array, because being in an | |
1847 | * active sync request, there is no normal IO, and | |
1848 | * no overlapping syncs. | |
06f60385 N |
1849 | * We don't need to check is_badblock() again as we |
1850 | * made sure that anything with a bad block in range | |
1851 | * will have bi_end_io clear. | |
a68e5870 | 1852 | */ |
fd01b88c | 1853 | struct mddev *mddev = r1_bio->mddev; |
e8096360 | 1854 | struct r1conf *conf = mddev->private; |
a68e5870 N |
1855 | struct bio *bio = r1_bio->bios[r1_bio->read_disk]; |
1856 | sector_t sect = r1_bio->sector; | |
1857 | int sectors = r1_bio->sectors; | |
1858 | int idx = 0; | |
2e52d449 N |
1859 | struct md_rdev *rdev; |
1860 | ||
1861 | rdev = conf->mirrors[r1_bio->read_disk].rdev; | |
1862 | if (test_bit(FailFast, &rdev->flags)) { | |
1863 | /* Don't try recovering from here - just fail it | |
1864 | * ... unless it is the last working device of course */ | |
1865 | md_error(mddev, rdev); | |
1866 | if (test_bit(Faulty, &rdev->flags)) | |
1867 | /* Don't try to read from here, but make sure | |
1868 | * put_buf does it's thing | |
1869 | */ | |
1870 | bio->bi_end_io = end_sync_write; | |
1871 | } | |
a68e5870 N |
1872 | |
1873 | while(sectors) { | |
1874 | int s = sectors; | |
1875 | int d = r1_bio->read_disk; | |
1876 | int success = 0; | |
78d7f5f7 | 1877 | int start; |
a68e5870 N |
1878 | |
1879 | if (s > (PAGE_SIZE>>9)) | |
1880 | s = PAGE_SIZE >> 9; | |
1881 | do { | |
1882 | if (r1_bio->bios[d]->bi_end_io == end_sync_read) { | |
1883 | /* No rcu protection needed here devices | |
1884 | * can only be removed when no resync is | |
1885 | * active, and resync is currently active | |
1886 | */ | |
1887 | rdev = conf->mirrors[d].rdev; | |
9d3d8011 | 1888 | if (sync_page_io(rdev, sect, s<<9, |
a68e5870 | 1889 | bio->bi_io_vec[idx].bv_page, |
796a5cf0 | 1890 | REQ_OP_READ, 0, false)) { |
a68e5870 N |
1891 | success = 1; |
1892 | break; | |
1893 | } | |
1894 | } | |
1895 | d++; | |
8f19ccb2 | 1896 | if (d == conf->raid_disks * 2) |
a68e5870 N |
1897 | d = 0; |
1898 | } while (!success && d != r1_bio->read_disk); | |
1899 | ||
78d7f5f7 | 1900 | if (!success) { |
a68e5870 | 1901 | char b[BDEVNAME_SIZE]; |
3a9f28a5 N |
1902 | int abort = 0; |
1903 | /* Cannot read from anywhere, this block is lost. | |
1904 | * Record a bad block on each device. If that doesn't | |
1905 | * work just disable and interrupt the recovery. | |
1906 | * Don't fail devices as that won't really help. | |
1907 | */ | |
1d41c216 N |
1908 | pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n", |
1909 | mdname(mddev), | |
1910 | bdevname(bio->bi_bdev, b), | |
1911 | (unsigned long long)r1_bio->sector); | |
8f19ccb2 | 1912 | for (d = 0; d < conf->raid_disks * 2; d++) { |
3a9f28a5 N |
1913 | rdev = conf->mirrors[d].rdev; |
1914 | if (!rdev || test_bit(Faulty, &rdev->flags)) | |
1915 | continue; | |
1916 | if (!rdev_set_badblocks(rdev, sect, s, 0)) | |
1917 | abort = 1; | |
1918 | } | |
1919 | if (abort) { | |
d890fa2b N |
1920 | conf->recovery_disabled = |
1921 | mddev->recovery_disabled; | |
3a9f28a5 N |
1922 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
1923 | md_done_sync(mddev, r1_bio->sectors, 0); | |
1924 | put_buf(r1_bio); | |
1925 | return 0; | |
1926 | } | |
1927 | /* Try next page */ | |
1928 | sectors -= s; | |
1929 | sect += s; | |
1930 | idx++; | |
1931 | continue; | |
d11c171e | 1932 | } |
78d7f5f7 N |
1933 | |
1934 | start = d; | |
1935 | /* write it back and re-read */ | |
1936 | while (d != r1_bio->read_disk) { | |
1937 | if (d == 0) | |
8f19ccb2 | 1938 | d = conf->raid_disks * 2; |
78d7f5f7 N |
1939 | d--; |
1940 | if (r1_bio->bios[d]->bi_end_io != end_sync_read) | |
1941 | continue; | |
1942 | rdev = conf->mirrors[d].rdev; | |
d8f05d29 N |
1943 | if (r1_sync_page_io(rdev, sect, s, |
1944 | bio->bi_io_vec[idx].bv_page, | |
1945 | WRITE) == 0) { | |
78d7f5f7 N |
1946 | r1_bio->bios[d]->bi_end_io = NULL; |
1947 | rdev_dec_pending(rdev, mddev); | |
9d3d8011 | 1948 | } |
78d7f5f7 N |
1949 | } |
1950 | d = start; | |
1951 | while (d != r1_bio->read_disk) { | |
1952 | if (d == 0) | |
8f19ccb2 | 1953 | d = conf->raid_disks * 2; |
78d7f5f7 N |
1954 | d--; |
1955 | if (r1_bio->bios[d]->bi_end_io != end_sync_read) | |
1956 | continue; | |
1957 | rdev = conf->mirrors[d].rdev; | |
d8f05d29 N |
1958 | if (r1_sync_page_io(rdev, sect, s, |
1959 | bio->bi_io_vec[idx].bv_page, | |
1960 | READ) != 0) | |
9d3d8011 | 1961 | atomic_add(s, &rdev->corrected_errors); |
78d7f5f7 | 1962 | } |
a68e5870 N |
1963 | sectors -= s; |
1964 | sect += s; | |
1965 | idx ++; | |
1966 | } | |
78d7f5f7 | 1967 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
4246a0b6 | 1968 | bio->bi_error = 0; |
a68e5870 N |
1969 | return 1; |
1970 | } | |
1971 | ||
c95e6385 | 1972 | static void process_checks(struct r1bio *r1_bio) |
a68e5870 N |
1973 | { |
1974 | /* We have read all readable devices. If we haven't | |
1975 | * got the block, then there is no hope left. | |
1976 | * If we have, then we want to do a comparison | |
1977 | * and skip the write if everything is the same. | |
1978 | * If any blocks failed to read, then we need to | |
1979 | * attempt an over-write | |
1980 | */ | |
fd01b88c | 1981 | struct mddev *mddev = r1_bio->mddev; |
e8096360 | 1982 | struct r1conf *conf = mddev->private; |
a68e5870 N |
1983 | int primary; |
1984 | int i; | |
f4380a91 | 1985 | int vcnt; |
a68e5870 | 1986 | |
30bc9b53 N |
1987 | /* Fix variable parts of all bios */ |
1988 | vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9); | |
1989 | for (i = 0; i < conf->raid_disks * 2; i++) { | |
1990 | int j; | |
1991 | int size; | |
4246a0b6 | 1992 | int error; |
30bc9b53 N |
1993 | struct bio *b = r1_bio->bios[i]; |
1994 | if (b->bi_end_io != end_sync_read) | |
1995 | continue; | |
4246a0b6 CH |
1996 | /* fixup the bio for reuse, but preserve errno */ |
1997 | error = b->bi_error; | |
30bc9b53 | 1998 | bio_reset(b); |
4246a0b6 | 1999 | b->bi_error = error; |
30bc9b53 | 2000 | b->bi_vcnt = vcnt; |
4f024f37 KO |
2001 | b->bi_iter.bi_size = r1_bio->sectors << 9; |
2002 | b->bi_iter.bi_sector = r1_bio->sector + | |
30bc9b53 N |
2003 | conf->mirrors[i].rdev->data_offset; |
2004 | b->bi_bdev = conf->mirrors[i].rdev->bdev; | |
2005 | b->bi_end_io = end_sync_read; | |
2006 | b->bi_private = r1_bio; | |
2007 | ||
4f024f37 | 2008 | size = b->bi_iter.bi_size; |
30bc9b53 N |
2009 | for (j = 0; j < vcnt ; j++) { |
2010 | struct bio_vec *bi; | |
2011 | bi = &b->bi_io_vec[j]; | |
2012 | bi->bv_offset = 0; | |
2013 | if (size > PAGE_SIZE) | |
2014 | bi->bv_len = PAGE_SIZE; | |
2015 | else | |
2016 | bi->bv_len = size; | |
2017 | size -= PAGE_SIZE; | |
2018 | } | |
2019 | } | |
8f19ccb2 | 2020 | for (primary = 0; primary < conf->raid_disks * 2; primary++) |
a68e5870 | 2021 | if (r1_bio->bios[primary]->bi_end_io == end_sync_read && |
4246a0b6 | 2022 | !r1_bio->bios[primary]->bi_error) { |
a68e5870 N |
2023 | r1_bio->bios[primary]->bi_end_io = NULL; |
2024 | rdev_dec_pending(conf->mirrors[primary].rdev, mddev); | |
2025 | break; | |
2026 | } | |
2027 | r1_bio->read_disk = primary; | |
8f19ccb2 | 2028 | for (i = 0; i < conf->raid_disks * 2; i++) { |
78d7f5f7 | 2029 | int j; |
78d7f5f7 N |
2030 | struct bio *pbio = r1_bio->bios[primary]; |
2031 | struct bio *sbio = r1_bio->bios[i]; | |
4246a0b6 | 2032 | int error = sbio->bi_error; |
a68e5870 | 2033 | |
2aabaa65 | 2034 | if (sbio->bi_end_io != end_sync_read) |
78d7f5f7 | 2035 | continue; |
4246a0b6 CH |
2036 | /* Now we can 'fixup' the error value */ |
2037 | sbio->bi_error = 0; | |
78d7f5f7 | 2038 | |
4246a0b6 | 2039 | if (!error) { |
78d7f5f7 N |
2040 | for (j = vcnt; j-- ; ) { |
2041 | struct page *p, *s; | |
2042 | p = pbio->bi_io_vec[j].bv_page; | |
2043 | s = sbio->bi_io_vec[j].bv_page; | |
2044 | if (memcmp(page_address(p), | |
2045 | page_address(s), | |
5020ad7d | 2046 | sbio->bi_io_vec[j].bv_len)) |
78d7f5f7 | 2047 | break; |
69382e85 | 2048 | } |
78d7f5f7 N |
2049 | } else |
2050 | j = 0; | |
2051 | if (j >= 0) | |
7f7583d4 | 2052 | atomic64_add(r1_bio->sectors, &mddev->resync_mismatches); |
78d7f5f7 | 2053 | if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery) |
4246a0b6 | 2054 | && !error)) { |
78d7f5f7 N |
2055 | /* No need to write to this device. */ |
2056 | sbio->bi_end_io = NULL; | |
2057 | rdev_dec_pending(conf->mirrors[i].rdev, mddev); | |
2058 | continue; | |
2059 | } | |
d3b45c2a KO |
2060 | |
2061 | bio_copy_data(sbio, pbio); | |
78d7f5f7 | 2062 | } |
a68e5870 N |
2063 | } |
2064 | ||
9f2c9d12 | 2065 | static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio) |
a68e5870 | 2066 | { |
e8096360 | 2067 | struct r1conf *conf = mddev->private; |
a68e5870 | 2068 | int i; |
8f19ccb2 | 2069 | int disks = conf->raid_disks * 2; |
a68e5870 N |
2070 | struct bio *bio, *wbio; |
2071 | ||
2072 | bio = r1_bio->bios[r1_bio->read_disk]; | |
2073 | ||
a68e5870 N |
2074 | if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) |
2075 | /* ouch - failed to read all of that. */ | |
2076 | if (!fix_sync_read_error(r1_bio)) | |
2077 | return; | |
7ca78d57 N |
2078 | |
2079 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
c95e6385 N |
2080 | process_checks(r1_bio); |
2081 | ||
d11c171e N |
2082 | /* |
2083 | * schedule writes | |
2084 | */ | |
1da177e4 LT |
2085 | atomic_set(&r1_bio->remaining, 1); |
2086 | for (i = 0; i < disks ; i++) { | |
2087 | wbio = r1_bio->bios[i]; | |
3e198f78 N |
2088 | if (wbio->bi_end_io == NULL || |
2089 | (wbio->bi_end_io == end_sync_read && | |
2090 | (i == r1_bio->read_disk || | |
2091 | !test_bit(MD_RECOVERY_SYNC, &mddev->recovery)))) | |
1da177e4 LT |
2092 | continue; |
2093 | ||
796a5cf0 | 2094 | bio_set_op_attrs(wbio, REQ_OP_WRITE, 0); |
212e7eb7 N |
2095 | if (test_bit(FailFast, &conf->mirrors[i].rdev->flags)) |
2096 | wbio->bi_opf |= MD_FAILFAST; | |
2097 | ||
3e198f78 | 2098 | wbio->bi_end_io = end_sync_write; |
1da177e4 | 2099 | atomic_inc(&r1_bio->remaining); |
aa8b57aa | 2100 | md_sync_acct(conf->mirrors[i].rdev->bdev, bio_sectors(wbio)); |
191ea9b2 | 2101 | |
1da177e4 LT |
2102 | generic_make_request(wbio); |
2103 | } | |
2104 | ||
2105 | if (atomic_dec_and_test(&r1_bio->remaining)) { | |
191ea9b2 | 2106 | /* if we're here, all write(s) have completed, so clean up */ |
58e94ae1 N |
2107 | int s = r1_bio->sectors; |
2108 | if (test_bit(R1BIO_MadeGood, &r1_bio->state) || | |
2109 | test_bit(R1BIO_WriteError, &r1_bio->state)) | |
2110 | reschedule_retry(r1_bio); | |
2111 | else { | |
2112 | put_buf(r1_bio); | |
2113 | md_done_sync(mddev, s, 1); | |
2114 | } | |
1da177e4 LT |
2115 | } |
2116 | } | |
2117 | ||
2118 | /* | |
2119 | * This is a kernel thread which: | |
2120 | * | |
2121 | * 1. Retries failed read operations on working mirrors. | |
2122 | * 2. Updates the raid superblock when problems encounter. | |
d2eb35ac | 2123 | * 3. Performs writes following reads for array synchronising. |
1da177e4 LT |
2124 | */ |
2125 | ||
e8096360 | 2126 | static void fix_read_error(struct r1conf *conf, int read_disk, |
867868fb N |
2127 | sector_t sect, int sectors) |
2128 | { | |
fd01b88c | 2129 | struct mddev *mddev = conf->mddev; |
867868fb N |
2130 | while(sectors) { |
2131 | int s = sectors; | |
2132 | int d = read_disk; | |
2133 | int success = 0; | |
2134 | int start; | |
3cb03002 | 2135 | struct md_rdev *rdev; |
867868fb N |
2136 | |
2137 | if (s > (PAGE_SIZE>>9)) | |
2138 | s = PAGE_SIZE >> 9; | |
2139 | ||
2140 | do { | |
d2eb35ac N |
2141 | sector_t first_bad; |
2142 | int bad_sectors; | |
2143 | ||
707a6a42 N |
2144 | rcu_read_lock(); |
2145 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
867868fb | 2146 | if (rdev && |
da8840a7 | 2147 | (test_bit(In_sync, &rdev->flags) || |
2148 | (!test_bit(Faulty, &rdev->flags) && | |
2149 | rdev->recovery_offset >= sect + s)) && | |
d2eb35ac | 2150 | is_badblock(rdev, sect, s, |
707a6a42 N |
2151 | &first_bad, &bad_sectors) == 0) { |
2152 | atomic_inc(&rdev->nr_pending); | |
2153 | rcu_read_unlock(); | |
2154 | if (sync_page_io(rdev, sect, s<<9, | |
796a5cf0 | 2155 | conf->tmppage, REQ_OP_READ, 0, false)) |
707a6a42 N |
2156 | success = 1; |
2157 | rdev_dec_pending(rdev, mddev); | |
2158 | if (success) | |
2159 | break; | |
2160 | } else | |
2161 | rcu_read_unlock(); | |
2162 | d++; | |
2163 | if (d == conf->raid_disks * 2) | |
2164 | d = 0; | |
867868fb N |
2165 | } while (!success && d != read_disk); |
2166 | ||
2167 | if (!success) { | |
d8f05d29 | 2168 | /* Cannot read from anywhere - mark it bad */ |
3cb03002 | 2169 | struct md_rdev *rdev = conf->mirrors[read_disk].rdev; |
d8f05d29 N |
2170 | if (!rdev_set_badblocks(rdev, sect, s, 0)) |
2171 | md_error(mddev, rdev); | |
867868fb N |
2172 | break; |
2173 | } | |
2174 | /* write it back and re-read */ | |
2175 | start = d; | |
2176 | while (d != read_disk) { | |
2177 | if (d==0) | |
8f19ccb2 | 2178 | d = conf->raid_disks * 2; |
867868fb | 2179 | d--; |
707a6a42 N |
2180 | rcu_read_lock(); |
2181 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
867868fb | 2182 | if (rdev && |
707a6a42 N |
2183 | !test_bit(Faulty, &rdev->flags)) { |
2184 | atomic_inc(&rdev->nr_pending); | |
2185 | rcu_read_unlock(); | |
d8f05d29 N |
2186 | r1_sync_page_io(rdev, sect, s, |
2187 | conf->tmppage, WRITE); | |
707a6a42 N |
2188 | rdev_dec_pending(rdev, mddev); |
2189 | } else | |
2190 | rcu_read_unlock(); | |
867868fb N |
2191 | } |
2192 | d = start; | |
2193 | while (d != read_disk) { | |
2194 | char b[BDEVNAME_SIZE]; | |
2195 | if (d==0) | |
8f19ccb2 | 2196 | d = conf->raid_disks * 2; |
867868fb | 2197 | d--; |
707a6a42 N |
2198 | rcu_read_lock(); |
2199 | rdev = rcu_dereference(conf->mirrors[d].rdev); | |
867868fb | 2200 | if (rdev && |
b8cb6b4c | 2201 | !test_bit(Faulty, &rdev->flags)) { |
707a6a42 N |
2202 | atomic_inc(&rdev->nr_pending); |
2203 | rcu_read_unlock(); | |
d8f05d29 N |
2204 | if (r1_sync_page_io(rdev, sect, s, |
2205 | conf->tmppage, READ)) { | |
867868fb | 2206 | atomic_add(s, &rdev->corrected_errors); |
1d41c216 N |
2207 | pr_info("md/raid1:%s: read error corrected (%d sectors at %llu on %s)\n", |
2208 | mdname(mddev), s, | |
2209 | (unsigned long long)(sect + | |
2210 | rdev->data_offset), | |
2211 | bdevname(rdev->bdev, b)); | |
867868fb | 2212 | } |
707a6a42 N |
2213 | rdev_dec_pending(rdev, mddev); |
2214 | } else | |
2215 | rcu_read_unlock(); | |
867868fb N |
2216 | } |
2217 | sectors -= s; | |
2218 | sect += s; | |
2219 | } | |
2220 | } | |
2221 | ||
9f2c9d12 | 2222 | static int narrow_write_error(struct r1bio *r1_bio, int i) |
cd5ff9a1 | 2223 | { |
fd01b88c | 2224 | struct mddev *mddev = r1_bio->mddev; |
e8096360 | 2225 | struct r1conf *conf = mddev->private; |
3cb03002 | 2226 | struct md_rdev *rdev = conf->mirrors[i].rdev; |
cd5ff9a1 N |
2227 | |
2228 | /* bio has the data to be written to device 'i' where | |
2229 | * we just recently had a write error. | |
2230 | * We repeatedly clone the bio and trim down to one block, | |
2231 | * then try the write. Where the write fails we record | |
2232 | * a bad block. | |
2233 | * It is conceivable that the bio doesn't exactly align with | |
2234 | * blocks. We must handle this somehow. | |
2235 | * | |
2236 | * We currently own a reference on the rdev. | |
2237 | */ | |
2238 | ||
2239 | int block_sectors; | |
2240 | sector_t sector; | |
2241 | int sectors; | |
2242 | int sect_to_write = r1_bio->sectors; | |
2243 | int ok = 1; | |
2244 | ||
2245 | if (rdev->badblocks.shift < 0) | |
2246 | return 0; | |
2247 | ||
ab713cdc ND |
2248 | block_sectors = roundup(1 << rdev->badblocks.shift, |
2249 | bdev_logical_block_size(rdev->bdev) >> 9); | |
cd5ff9a1 N |
2250 | sector = r1_bio->sector; |
2251 | sectors = ((sector + block_sectors) | |
2252 | & ~(sector_t)(block_sectors - 1)) | |
2253 | - sector; | |
2254 | ||
cd5ff9a1 N |
2255 | while (sect_to_write) { |
2256 | struct bio *wbio; | |
2257 | if (sectors > sect_to_write) | |
2258 | sectors = sect_to_write; | |
2259 | /* Write at 'sector' for 'sectors'*/ | |
2260 | ||
b783863f KO |
2261 | if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { |
2262 | unsigned vcnt = r1_bio->behind_page_count; | |
2263 | struct bio_vec *vec = r1_bio->behind_bvecs; | |
2264 | ||
2265 | while (!vec->bv_page) { | |
2266 | vec++; | |
2267 | vcnt--; | |
2268 | } | |
2269 | ||
2270 | wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev); | |
2271 | memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec)); | |
2272 | ||
2273 | wbio->bi_vcnt = vcnt; | |
2274 | } else { | |
2275 | wbio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev); | |
2276 | } | |
2277 | ||
796a5cf0 | 2278 | bio_set_op_attrs(wbio, REQ_OP_WRITE, 0); |
4f024f37 KO |
2279 | wbio->bi_iter.bi_sector = r1_bio->sector; |
2280 | wbio->bi_iter.bi_size = r1_bio->sectors << 9; | |
cd5ff9a1 | 2281 | |
6678d83f | 2282 | bio_trim(wbio, sector - r1_bio->sector, sectors); |
4f024f37 | 2283 | wbio->bi_iter.bi_sector += rdev->data_offset; |
cd5ff9a1 | 2284 | wbio->bi_bdev = rdev->bdev; |
4e49ea4a MC |
2285 | |
2286 | if (submit_bio_wait(wbio) < 0) | |
cd5ff9a1 N |
2287 | /* failure! */ |
2288 | ok = rdev_set_badblocks(rdev, sector, | |
2289 | sectors, 0) | |
2290 | && ok; | |
2291 | ||
2292 | bio_put(wbio); | |
2293 | sect_to_write -= sectors; | |
2294 | sector += sectors; | |
2295 | sectors = block_sectors; | |
2296 | } | |
2297 | return ok; | |
2298 | } | |
2299 | ||
e8096360 | 2300 | static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio) |
62096bce N |
2301 | { |
2302 | int m; | |
2303 | int s = r1_bio->sectors; | |
8f19ccb2 | 2304 | for (m = 0; m < conf->raid_disks * 2 ; m++) { |
3cb03002 | 2305 | struct md_rdev *rdev = conf->mirrors[m].rdev; |
62096bce N |
2306 | struct bio *bio = r1_bio->bios[m]; |
2307 | if (bio->bi_end_io == NULL) | |
2308 | continue; | |
4246a0b6 | 2309 | if (!bio->bi_error && |
62096bce | 2310 | test_bit(R1BIO_MadeGood, &r1_bio->state)) { |
c6563a8c | 2311 | rdev_clear_badblocks(rdev, r1_bio->sector, s, 0); |
62096bce | 2312 | } |
4246a0b6 | 2313 | if (bio->bi_error && |
62096bce N |
2314 | test_bit(R1BIO_WriteError, &r1_bio->state)) { |
2315 | if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0)) | |
2316 | md_error(conf->mddev, rdev); | |
2317 | } | |
2318 | } | |
2319 | put_buf(r1_bio); | |
2320 | md_done_sync(conf->mddev, s, 1); | |
2321 | } | |
2322 | ||
e8096360 | 2323 | static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio) |
62096bce N |
2324 | { |
2325 | int m; | |
55ce74d4 | 2326 | bool fail = false; |
8f19ccb2 | 2327 | for (m = 0; m < conf->raid_disks * 2 ; m++) |
62096bce | 2328 | if (r1_bio->bios[m] == IO_MADE_GOOD) { |
3cb03002 | 2329 | struct md_rdev *rdev = conf->mirrors[m].rdev; |
62096bce N |
2330 | rdev_clear_badblocks(rdev, |
2331 | r1_bio->sector, | |
c6563a8c | 2332 | r1_bio->sectors, 0); |
62096bce N |
2333 | rdev_dec_pending(rdev, conf->mddev); |
2334 | } else if (r1_bio->bios[m] != NULL) { | |
2335 | /* This drive got a write error. We need to | |
2336 | * narrow down and record precise write | |
2337 | * errors. | |
2338 | */ | |
55ce74d4 | 2339 | fail = true; |
62096bce N |
2340 | if (!narrow_write_error(r1_bio, m)) { |
2341 | md_error(conf->mddev, | |
2342 | conf->mirrors[m].rdev); | |
2343 | /* an I/O failed, we can't clear the bitmap */ | |
2344 | set_bit(R1BIO_Degraded, &r1_bio->state); | |
2345 | } | |
2346 | rdev_dec_pending(conf->mirrors[m].rdev, | |
2347 | conf->mddev); | |
2348 | } | |
55ce74d4 N |
2349 | if (fail) { |
2350 | spin_lock_irq(&conf->device_lock); | |
2351 | list_add(&r1_bio->retry_list, &conf->bio_end_io_list); | |
ccfc7bf1 | 2352 | conf->nr_queued++; |
55ce74d4 N |
2353 | spin_unlock_irq(&conf->device_lock); |
2354 | md_wakeup_thread(conf->mddev->thread); | |
bd8688a1 N |
2355 | } else { |
2356 | if (test_bit(R1BIO_WriteError, &r1_bio->state)) | |
2357 | close_write(r1_bio); | |
55ce74d4 | 2358 | raid_end_bio_io(r1_bio); |
bd8688a1 | 2359 | } |
62096bce N |
2360 | } |
2361 | ||
e8096360 | 2362 | static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio) |
62096bce N |
2363 | { |
2364 | int disk; | |
2365 | int max_sectors; | |
fd01b88c | 2366 | struct mddev *mddev = conf->mddev; |
62096bce N |
2367 | struct bio *bio; |
2368 | char b[BDEVNAME_SIZE]; | |
3cb03002 | 2369 | struct md_rdev *rdev; |
109e3765 N |
2370 | dev_t bio_dev; |
2371 | sector_t bio_sector; | |
62096bce N |
2372 | |
2373 | clear_bit(R1BIO_ReadError, &r1_bio->state); | |
2374 | /* we got a read error. Maybe the drive is bad. Maybe just | |
2375 | * the block and we can fix it. | |
2376 | * We freeze all other IO, and try reading the block from | |
2377 | * other devices. When we find one, we re-write | |
2378 | * and check it that fixes the read error. | |
2379 | * This is all done synchronously while the array is | |
2380 | * frozen | |
2381 | */ | |
7449f699 TM |
2382 | |
2383 | bio = r1_bio->bios[r1_bio->read_disk]; | |
2384 | bdevname(bio->bi_bdev, b); | |
109e3765 N |
2385 | bio_dev = bio->bi_bdev->bd_dev; |
2386 | bio_sector = conf->mirrors[r1_bio->read_disk].rdev->data_offset + r1_bio->sector; | |
7449f699 TM |
2387 | bio_put(bio); |
2388 | r1_bio->bios[r1_bio->read_disk] = NULL; | |
2389 | ||
2e52d449 N |
2390 | rdev = conf->mirrors[r1_bio->read_disk].rdev; |
2391 | if (mddev->ro == 0 | |
2392 | && !test_bit(FailFast, &rdev->flags)) { | |
e2d59925 | 2393 | freeze_array(conf, 1); |
62096bce N |
2394 | fix_read_error(conf, r1_bio->read_disk, |
2395 | r1_bio->sector, r1_bio->sectors); | |
2396 | unfreeze_array(conf); | |
7449f699 TM |
2397 | } else { |
2398 | r1_bio->bios[r1_bio->read_disk] = IO_BLOCKED; | |
2399 | } | |
2400 | ||
2e52d449 | 2401 | rdev_dec_pending(rdev, conf->mddev); |
62096bce | 2402 | |
62096bce N |
2403 | read_more: |
2404 | disk = read_balance(conf, r1_bio, &max_sectors); | |
2405 | if (disk == -1) { | |
1d41c216 N |
2406 | pr_crit_ratelimited("md/raid1:%s: %s: unrecoverable I/O read error for block %llu\n", |
2407 | mdname(mddev), b, (unsigned long long)r1_bio->sector); | |
62096bce N |
2408 | raid_end_bio_io(r1_bio); |
2409 | } else { | |
2410 | const unsigned long do_sync | |
1eff9d32 | 2411 | = r1_bio->master_bio->bi_opf & REQ_SYNC; |
62096bce N |
2412 | r1_bio->read_disk = disk; |
2413 | bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev); | |
4f024f37 KO |
2414 | bio_trim(bio, r1_bio->sector - bio->bi_iter.bi_sector, |
2415 | max_sectors); | |
62096bce N |
2416 | r1_bio->bios[r1_bio->read_disk] = bio; |
2417 | rdev = conf->mirrors[disk].rdev; | |
1d41c216 N |
2418 | pr_info_ratelimited("md/raid1:%s: redirecting sector %llu to other mirror: %s\n", |
2419 | mdname(mddev), | |
2420 | (unsigned long long)r1_bio->sector, | |
2421 | bdevname(rdev->bdev, b)); | |
4f024f37 | 2422 | bio->bi_iter.bi_sector = r1_bio->sector + rdev->data_offset; |
62096bce N |
2423 | bio->bi_bdev = rdev->bdev; |
2424 | bio->bi_end_io = raid1_end_read_request; | |
796a5cf0 | 2425 | bio_set_op_attrs(bio, REQ_OP_READ, do_sync); |
2e52d449 N |
2426 | if (test_bit(FailFast, &rdev->flags) && |
2427 | test_bit(R1BIO_FailFast, &r1_bio->state)) | |
2428 | bio->bi_opf |= MD_FAILFAST; | |
62096bce N |
2429 | bio->bi_private = r1_bio; |
2430 | if (max_sectors < r1_bio->sectors) { | |
2431 | /* Drat - have to split this up more */ | |
2432 | struct bio *mbio = r1_bio->master_bio; | |
2433 | int sectors_handled = (r1_bio->sector + max_sectors | |
4f024f37 | 2434 | - mbio->bi_iter.bi_sector); |
62096bce N |
2435 | r1_bio->sectors = max_sectors; |
2436 | spin_lock_irq(&conf->device_lock); | |
2437 | if (mbio->bi_phys_segments == 0) | |
2438 | mbio->bi_phys_segments = 2; | |
2439 | else | |
2440 | mbio->bi_phys_segments++; | |
2441 | spin_unlock_irq(&conf->device_lock); | |
109e3765 N |
2442 | trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), |
2443 | bio, bio_dev, bio_sector); | |
62096bce N |
2444 | generic_make_request(bio); |
2445 | bio = NULL; | |
2446 | ||
2447 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); | |
2448 | ||
2449 | r1_bio->master_bio = mbio; | |
aa8b57aa | 2450 | r1_bio->sectors = bio_sectors(mbio) - sectors_handled; |
62096bce N |
2451 | r1_bio->state = 0; |
2452 | set_bit(R1BIO_ReadError, &r1_bio->state); | |
2453 | r1_bio->mddev = mddev; | |
4f024f37 KO |
2454 | r1_bio->sector = mbio->bi_iter.bi_sector + |
2455 | sectors_handled; | |
62096bce N |
2456 | |
2457 | goto read_more; | |
109e3765 N |
2458 | } else { |
2459 | trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), | |
2460 | bio, bio_dev, bio_sector); | |
62096bce | 2461 | generic_make_request(bio); |
109e3765 | 2462 | } |
62096bce N |
2463 | } |
2464 | } | |
2465 | ||
4ed8731d | 2466 | static void raid1d(struct md_thread *thread) |
1da177e4 | 2467 | { |
4ed8731d | 2468 | struct mddev *mddev = thread->mddev; |
9f2c9d12 | 2469 | struct r1bio *r1_bio; |
1da177e4 | 2470 | unsigned long flags; |
e8096360 | 2471 | struct r1conf *conf = mddev->private; |
1da177e4 | 2472 | struct list_head *head = &conf->retry_list; |
e1dfa0a2 | 2473 | struct blk_plug plug; |
1da177e4 LT |
2474 | |
2475 | md_check_recovery(mddev); | |
e1dfa0a2 | 2476 | |
55ce74d4 | 2477 | if (!list_empty_careful(&conf->bio_end_io_list) && |
2953079c | 2478 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
55ce74d4 N |
2479 | LIST_HEAD(tmp); |
2480 | spin_lock_irqsave(&conf->device_lock, flags); | |
2953079c | 2481 | if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
ccfc7bf1 ND |
2482 | while (!list_empty(&conf->bio_end_io_list)) { |
2483 | list_move(conf->bio_end_io_list.prev, &tmp); | |
2484 | conf->nr_queued--; | |
2485 | } | |
55ce74d4 N |
2486 | } |
2487 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
2488 | while (!list_empty(&tmp)) { | |
a452744b MP |
2489 | r1_bio = list_first_entry(&tmp, struct r1bio, |
2490 | retry_list); | |
55ce74d4 | 2491 | list_del(&r1_bio->retry_list); |
bd8688a1 N |
2492 | if (mddev->degraded) |
2493 | set_bit(R1BIO_Degraded, &r1_bio->state); | |
2494 | if (test_bit(R1BIO_WriteError, &r1_bio->state)) | |
2495 | close_write(r1_bio); | |
55ce74d4 N |
2496 | raid_end_bio_io(r1_bio); |
2497 | } | |
2498 | } | |
2499 | ||
e1dfa0a2 | 2500 | blk_start_plug(&plug); |
1da177e4 | 2501 | for (;;) { |
191ea9b2 | 2502 | |
0021b7bc | 2503 | flush_pending_writes(conf); |
191ea9b2 | 2504 | |
a35e63ef N |
2505 | spin_lock_irqsave(&conf->device_lock, flags); |
2506 | if (list_empty(head)) { | |
2507 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1da177e4 | 2508 | break; |
a35e63ef | 2509 | } |
9f2c9d12 | 2510 | r1_bio = list_entry(head->prev, struct r1bio, retry_list); |
1da177e4 | 2511 | list_del(head->prev); |
ddaf22ab | 2512 | conf->nr_queued--; |
1da177e4 LT |
2513 | spin_unlock_irqrestore(&conf->device_lock, flags); |
2514 | ||
2515 | mddev = r1_bio->mddev; | |
070ec55d | 2516 | conf = mddev->private; |
4367af55 | 2517 | if (test_bit(R1BIO_IsSync, &r1_bio->state)) { |
d8f05d29 | 2518 | if (test_bit(R1BIO_MadeGood, &r1_bio->state) || |
62096bce N |
2519 | test_bit(R1BIO_WriteError, &r1_bio->state)) |
2520 | handle_sync_write_finished(conf, r1_bio); | |
2521 | else | |
4367af55 | 2522 | sync_request_write(mddev, r1_bio); |
cd5ff9a1 | 2523 | } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) || |
62096bce N |
2524 | test_bit(R1BIO_WriteError, &r1_bio->state)) |
2525 | handle_write_finished(conf, r1_bio); | |
2526 | else if (test_bit(R1BIO_ReadError, &r1_bio->state)) | |
2527 | handle_read_error(conf, r1_bio); | |
2528 | else | |
d2eb35ac N |
2529 | /* just a partial read to be scheduled from separate |
2530 | * context | |
2531 | */ | |
2532 | generic_make_request(r1_bio->bios[r1_bio->read_disk]); | |
62096bce | 2533 | |
1d9d5241 | 2534 | cond_resched(); |
2953079c | 2535 | if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING)) |
de393cde | 2536 | md_check_recovery(mddev); |
1da177e4 | 2537 | } |
e1dfa0a2 | 2538 | blk_finish_plug(&plug); |
1da177e4 LT |
2539 | } |
2540 | ||
e8096360 | 2541 | static int init_resync(struct r1conf *conf) |
1da177e4 LT |
2542 | { |
2543 | int buffs; | |
2544 | ||
2545 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; | |
9e77c485 | 2546 | BUG_ON(conf->r1buf_pool); |
1da177e4 LT |
2547 | conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free, |
2548 | conf->poolinfo); | |
2549 | if (!conf->r1buf_pool) | |
2550 | return -ENOMEM; | |
2551 | conf->next_resync = 0; | |
2552 | return 0; | |
2553 | } | |
2554 | ||
2555 | /* | |
2556 | * perform a "sync" on one "block" | |
2557 | * | |
2558 | * We need to make sure that no normal I/O request - particularly write | |
2559 | * requests - conflict with active sync requests. | |
2560 | * | |
2561 | * This is achieved by tracking pending requests and a 'barrier' concept | |
2562 | * that can be installed to exclude normal IO requests. | |
2563 | */ | |
2564 | ||
849674e4 SL |
2565 | static sector_t raid1_sync_request(struct mddev *mddev, sector_t sector_nr, |
2566 | int *skipped) | |
1da177e4 | 2567 | { |
e8096360 | 2568 | struct r1conf *conf = mddev->private; |
9f2c9d12 | 2569 | struct r1bio *r1_bio; |
1da177e4 LT |
2570 | struct bio *bio; |
2571 | sector_t max_sector, nr_sectors; | |
3e198f78 | 2572 | int disk = -1; |
1da177e4 | 2573 | int i; |
3e198f78 N |
2574 | int wonly = -1; |
2575 | int write_targets = 0, read_targets = 0; | |
57dab0bd | 2576 | sector_t sync_blocks; |
e3b9703e | 2577 | int still_degraded = 0; |
06f60385 N |
2578 | int good_sectors = RESYNC_SECTORS; |
2579 | int min_bad = 0; /* number of sectors that are bad in all devices */ | |
1da177e4 LT |
2580 | |
2581 | if (!conf->r1buf_pool) | |
2582 | if (init_resync(conf)) | |
57afd89f | 2583 | return 0; |
1da177e4 | 2584 | |
58c0fed4 | 2585 | max_sector = mddev->dev_sectors; |
1da177e4 | 2586 | if (sector_nr >= max_sector) { |
191ea9b2 N |
2587 | /* If we aborted, we need to abort the |
2588 | * sync on the 'current' bitmap chunk (there will | |
2589 | * only be one in raid1 resync. | |
2590 | * We can find the current addess in mddev->curr_resync | |
2591 | */ | |
6a806c51 N |
2592 | if (mddev->curr_resync < max_sector) /* aborted */ |
2593 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
191ea9b2 | 2594 | &sync_blocks, 1); |
6a806c51 | 2595 | else /* completed sync */ |
191ea9b2 | 2596 | conf->fullsync = 0; |
6a806c51 N |
2597 | |
2598 | bitmap_close_sync(mddev->bitmap); | |
1da177e4 | 2599 | close_sync(conf); |
c40f341f GR |
2600 | |
2601 | if (mddev_is_clustered(mddev)) { | |
2602 | conf->cluster_sync_low = 0; | |
2603 | conf->cluster_sync_high = 0; | |
c40f341f | 2604 | } |
1da177e4 LT |
2605 | return 0; |
2606 | } | |
2607 | ||
07d84d10 N |
2608 | if (mddev->bitmap == NULL && |
2609 | mddev->recovery_cp == MaxSector && | |
6394cca5 | 2610 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
07d84d10 N |
2611 | conf->fullsync == 0) { |
2612 | *skipped = 1; | |
2613 | return max_sector - sector_nr; | |
2614 | } | |
6394cca5 N |
2615 | /* before building a request, check if we can skip these blocks.. |
2616 | * This call the bitmap_start_sync doesn't actually record anything | |
2617 | */ | |
e3b9703e | 2618 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
e5de485f | 2619 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
191ea9b2 N |
2620 | /* We can skip this block, and probably several more */ |
2621 | *skipped = 1; | |
2622 | return sync_blocks; | |
2623 | } | |
17999be4 | 2624 | |
7ac50447 TM |
2625 | /* |
2626 | * If there is non-resync activity waiting for a turn, then let it | |
2627 | * though before starting on this new sync request. | |
2628 | */ | |
2629 | if (conf->nr_waiting) | |
2630 | schedule_timeout_uninterruptible(1); | |
2631 | ||
c40f341f GR |
2632 | /* we are incrementing sector_nr below. To be safe, we check against |
2633 | * sector_nr + two times RESYNC_SECTORS | |
2634 | */ | |
2635 | ||
2636 | bitmap_cond_end_sync(mddev->bitmap, sector_nr, | |
2637 | mddev_is_clustered(mddev) && (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high)); | |
1c4588e9 | 2638 | r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO); |
17999be4 | 2639 | |
c2fd4c94 | 2640 | raise_barrier(conf, sector_nr); |
1da177e4 | 2641 | |
3e198f78 | 2642 | rcu_read_lock(); |
1da177e4 | 2643 | /* |
3e198f78 N |
2644 | * If we get a correctably read error during resync or recovery, |
2645 | * we might want to read from a different device. So we | |
2646 | * flag all drives that could conceivably be read from for READ, | |
2647 | * and any others (which will be non-In_sync devices) for WRITE. | |
2648 | * If a read fails, we try reading from something else for which READ | |
2649 | * is OK. | |
1da177e4 | 2650 | */ |
1da177e4 | 2651 | |
1da177e4 LT |
2652 | r1_bio->mddev = mddev; |
2653 | r1_bio->sector = sector_nr; | |
191ea9b2 | 2654 | r1_bio->state = 0; |
1da177e4 | 2655 | set_bit(R1BIO_IsSync, &r1_bio->state); |
1da177e4 | 2656 | |
8f19ccb2 | 2657 | for (i = 0; i < conf->raid_disks * 2; i++) { |
3cb03002 | 2658 | struct md_rdev *rdev; |
1da177e4 | 2659 | bio = r1_bio->bios[i]; |
2aabaa65 | 2660 | bio_reset(bio); |
1da177e4 | 2661 | |
3e198f78 N |
2662 | rdev = rcu_dereference(conf->mirrors[i].rdev); |
2663 | if (rdev == NULL || | |
06f60385 | 2664 | test_bit(Faulty, &rdev->flags)) { |
8f19ccb2 N |
2665 | if (i < conf->raid_disks) |
2666 | still_degraded = 1; | |
3e198f78 | 2667 | } else if (!test_bit(In_sync, &rdev->flags)) { |
796a5cf0 | 2668 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
1da177e4 LT |
2669 | bio->bi_end_io = end_sync_write; |
2670 | write_targets ++; | |
3e198f78 N |
2671 | } else { |
2672 | /* may need to read from here */ | |
06f60385 N |
2673 | sector_t first_bad = MaxSector; |
2674 | int bad_sectors; | |
2675 | ||
2676 | if (is_badblock(rdev, sector_nr, good_sectors, | |
2677 | &first_bad, &bad_sectors)) { | |
2678 | if (first_bad > sector_nr) | |
2679 | good_sectors = first_bad - sector_nr; | |
2680 | else { | |
2681 | bad_sectors -= (sector_nr - first_bad); | |
2682 | if (min_bad == 0 || | |
2683 | min_bad > bad_sectors) | |
2684 | min_bad = bad_sectors; | |
2685 | } | |
2686 | } | |
2687 | if (sector_nr < first_bad) { | |
2688 | if (test_bit(WriteMostly, &rdev->flags)) { | |
2689 | if (wonly < 0) | |
2690 | wonly = i; | |
2691 | } else { | |
2692 | if (disk < 0) | |
2693 | disk = i; | |
2694 | } | |
796a5cf0 | 2695 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
06f60385 N |
2696 | bio->bi_end_io = end_sync_read; |
2697 | read_targets++; | |
d57368af AL |
2698 | } else if (!test_bit(WriteErrorSeen, &rdev->flags) && |
2699 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && | |
2700 | !test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { | |
2701 | /* | |
2702 | * The device is suitable for reading (InSync), | |
2703 | * but has bad block(s) here. Let's try to correct them, | |
2704 | * if we are doing resync or repair. Otherwise, leave | |
2705 | * this device alone for this sync request. | |
2706 | */ | |
796a5cf0 | 2707 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
d57368af AL |
2708 | bio->bi_end_io = end_sync_write; |
2709 | write_targets++; | |
3e198f78 | 2710 | } |
3e198f78 | 2711 | } |
06f60385 N |
2712 | if (bio->bi_end_io) { |
2713 | atomic_inc(&rdev->nr_pending); | |
4f024f37 | 2714 | bio->bi_iter.bi_sector = sector_nr + rdev->data_offset; |
06f60385 N |
2715 | bio->bi_bdev = rdev->bdev; |
2716 | bio->bi_private = r1_bio; | |
2e52d449 N |
2717 | if (test_bit(FailFast, &rdev->flags)) |
2718 | bio->bi_opf |= MD_FAILFAST; | |
06f60385 | 2719 | } |
1da177e4 | 2720 | } |
3e198f78 N |
2721 | rcu_read_unlock(); |
2722 | if (disk < 0) | |
2723 | disk = wonly; | |
2724 | r1_bio->read_disk = disk; | |
191ea9b2 | 2725 | |
06f60385 N |
2726 | if (read_targets == 0 && min_bad > 0) { |
2727 | /* These sectors are bad on all InSync devices, so we | |
2728 | * need to mark them bad on all write targets | |
2729 | */ | |
2730 | int ok = 1; | |
8f19ccb2 | 2731 | for (i = 0 ; i < conf->raid_disks * 2 ; i++) |
06f60385 | 2732 | if (r1_bio->bios[i]->bi_end_io == end_sync_write) { |
a42f9d83 | 2733 | struct md_rdev *rdev = conf->mirrors[i].rdev; |
06f60385 N |
2734 | ok = rdev_set_badblocks(rdev, sector_nr, |
2735 | min_bad, 0 | |
2736 | ) && ok; | |
2737 | } | |
2953079c | 2738 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
06f60385 N |
2739 | *skipped = 1; |
2740 | put_buf(r1_bio); | |
2741 | ||
2742 | if (!ok) { | |
2743 | /* Cannot record the badblocks, so need to | |
2744 | * abort the resync. | |
2745 | * If there are multiple read targets, could just | |
2746 | * fail the really bad ones ??? | |
2747 | */ | |
2748 | conf->recovery_disabled = mddev->recovery_disabled; | |
2749 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
2750 | return 0; | |
2751 | } else | |
2752 | return min_bad; | |
2753 | ||
2754 | } | |
2755 | if (min_bad > 0 && min_bad < good_sectors) { | |
2756 | /* only resync enough to reach the next bad->good | |
2757 | * transition */ | |
2758 | good_sectors = min_bad; | |
2759 | } | |
2760 | ||
3e198f78 N |
2761 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0) |
2762 | /* extra read targets are also write targets */ | |
2763 | write_targets += read_targets-1; | |
2764 | ||
2765 | if (write_targets == 0 || read_targets == 0) { | |
1da177e4 LT |
2766 | /* There is nowhere to write, so all non-sync |
2767 | * drives must be failed - so we are finished | |
2768 | */ | |
b7219ccb N |
2769 | sector_t rv; |
2770 | if (min_bad > 0) | |
2771 | max_sector = sector_nr + min_bad; | |
2772 | rv = max_sector - sector_nr; | |
57afd89f | 2773 | *skipped = 1; |
1da177e4 | 2774 | put_buf(r1_bio); |
1da177e4 LT |
2775 | return rv; |
2776 | } | |
2777 | ||
c6207277 N |
2778 | if (max_sector > mddev->resync_max) |
2779 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ | |
06f60385 N |
2780 | if (max_sector > sector_nr + good_sectors) |
2781 | max_sector = sector_nr + good_sectors; | |
1da177e4 | 2782 | nr_sectors = 0; |
289e99e8 | 2783 | sync_blocks = 0; |
1da177e4 LT |
2784 | do { |
2785 | struct page *page; | |
2786 | int len = PAGE_SIZE; | |
2787 | if (sector_nr + (len>>9) > max_sector) | |
2788 | len = (max_sector - sector_nr) << 9; | |
2789 | if (len == 0) | |
2790 | break; | |
6a806c51 N |
2791 | if (sync_blocks == 0) { |
2792 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, | |
e5de485f N |
2793 | &sync_blocks, still_degraded) && |
2794 | !conf->fullsync && | |
2795 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) | |
6a806c51 | 2796 | break; |
7571ae88 | 2797 | if ((len >> 9) > sync_blocks) |
6a806c51 | 2798 | len = sync_blocks<<9; |
ab7a30c7 | 2799 | } |
191ea9b2 | 2800 | |
8f19ccb2 | 2801 | for (i = 0 ; i < conf->raid_disks * 2; i++) { |
1da177e4 LT |
2802 | bio = r1_bio->bios[i]; |
2803 | if (bio->bi_end_io) { | |
d11c171e | 2804 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; |
1da177e4 LT |
2805 | if (bio_add_page(bio, page, len, 0) == 0) { |
2806 | /* stop here */ | |
d11c171e | 2807 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; |
1da177e4 LT |
2808 | while (i > 0) { |
2809 | i--; | |
2810 | bio = r1_bio->bios[i]; | |
6a806c51 N |
2811 | if (bio->bi_end_io==NULL) |
2812 | continue; | |
1da177e4 LT |
2813 | /* remove last page from this bio */ |
2814 | bio->bi_vcnt--; | |
4f024f37 | 2815 | bio->bi_iter.bi_size -= len; |
b7c44ed9 | 2816 | bio_clear_flag(bio, BIO_SEG_VALID); |
1da177e4 LT |
2817 | } |
2818 | goto bio_full; | |
2819 | } | |
2820 | } | |
2821 | } | |
2822 | nr_sectors += len>>9; | |
2823 | sector_nr += len>>9; | |
191ea9b2 | 2824 | sync_blocks -= (len>>9); |
1da177e4 LT |
2825 | } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES); |
2826 | bio_full: | |
1da177e4 LT |
2827 | r1_bio->sectors = nr_sectors; |
2828 | ||
c40f341f GR |
2829 | if (mddev_is_clustered(mddev) && |
2830 | conf->cluster_sync_high < sector_nr + nr_sectors) { | |
2831 | conf->cluster_sync_low = mddev->curr_resync_completed; | |
2832 | conf->cluster_sync_high = conf->cluster_sync_low + CLUSTER_RESYNC_WINDOW_SECTORS; | |
2833 | /* Send resync message */ | |
2834 | md_cluster_ops->resync_info_update(mddev, | |
2835 | conf->cluster_sync_low, | |
2836 | conf->cluster_sync_high); | |
2837 | } | |
2838 | ||
d11c171e N |
2839 | /* For a user-requested sync, we read all readable devices and do a |
2840 | * compare | |
2841 | */ | |
2842 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { | |
2843 | atomic_set(&r1_bio->remaining, read_targets); | |
2d4f4f33 | 2844 | for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) { |
d11c171e N |
2845 | bio = r1_bio->bios[i]; |
2846 | if (bio->bi_end_io == end_sync_read) { | |
2d4f4f33 | 2847 | read_targets--; |
ddac7c7e | 2848 | md_sync_acct(bio->bi_bdev, nr_sectors); |
2e52d449 N |
2849 | if (read_targets == 1) |
2850 | bio->bi_opf &= ~MD_FAILFAST; | |
d11c171e N |
2851 | generic_make_request(bio); |
2852 | } | |
2853 | } | |
2854 | } else { | |
2855 | atomic_set(&r1_bio->remaining, 1); | |
2856 | bio = r1_bio->bios[r1_bio->read_disk]; | |
ddac7c7e | 2857 | md_sync_acct(bio->bi_bdev, nr_sectors); |
2e52d449 N |
2858 | if (read_targets == 1) |
2859 | bio->bi_opf &= ~MD_FAILFAST; | |
d11c171e | 2860 | generic_make_request(bio); |
1da177e4 | 2861 | |
d11c171e | 2862 | } |
1da177e4 LT |
2863 | return nr_sectors; |
2864 | } | |
2865 | ||
fd01b88c | 2866 | static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
80c3a6ce DW |
2867 | { |
2868 | if (sectors) | |
2869 | return sectors; | |
2870 | ||
2871 | return mddev->dev_sectors; | |
2872 | } | |
2873 | ||
e8096360 | 2874 | static struct r1conf *setup_conf(struct mddev *mddev) |
1da177e4 | 2875 | { |
e8096360 | 2876 | struct r1conf *conf; |
709ae487 | 2877 | int i; |
0eaf822c | 2878 | struct raid1_info *disk; |
3cb03002 | 2879 | struct md_rdev *rdev; |
709ae487 | 2880 | int err = -ENOMEM; |
1da177e4 | 2881 | |
e8096360 | 2882 | conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL); |
1da177e4 | 2883 | if (!conf) |
709ae487 | 2884 | goto abort; |
1da177e4 | 2885 | |
0eaf822c | 2886 | conf->mirrors = kzalloc(sizeof(struct raid1_info) |
8f19ccb2 | 2887 | * mddev->raid_disks * 2, |
1da177e4 LT |
2888 | GFP_KERNEL); |
2889 | if (!conf->mirrors) | |
709ae487 | 2890 | goto abort; |
1da177e4 | 2891 | |
ddaf22ab N |
2892 | conf->tmppage = alloc_page(GFP_KERNEL); |
2893 | if (!conf->tmppage) | |
709ae487 | 2894 | goto abort; |
ddaf22ab | 2895 | |
709ae487 | 2896 | conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL); |
1da177e4 | 2897 | if (!conf->poolinfo) |
709ae487 | 2898 | goto abort; |
8f19ccb2 | 2899 | conf->poolinfo->raid_disks = mddev->raid_disks * 2; |
1da177e4 LT |
2900 | conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, |
2901 | r1bio_pool_free, | |
2902 | conf->poolinfo); | |
2903 | if (!conf->r1bio_pool) | |
709ae487 N |
2904 | goto abort; |
2905 | ||
ed9bfdf1 | 2906 | conf->poolinfo->mddev = mddev; |
1da177e4 | 2907 | |
c19d5798 | 2908 | err = -EINVAL; |
e7e72bf6 | 2909 | spin_lock_init(&conf->device_lock); |
dafb20fa | 2910 | rdev_for_each(rdev, mddev) { |
aba336bd | 2911 | struct request_queue *q; |
709ae487 | 2912 | int disk_idx = rdev->raid_disk; |
1da177e4 LT |
2913 | if (disk_idx >= mddev->raid_disks |
2914 | || disk_idx < 0) | |
2915 | continue; | |
c19d5798 | 2916 | if (test_bit(Replacement, &rdev->flags)) |
02b898f2 | 2917 | disk = conf->mirrors + mddev->raid_disks + disk_idx; |
c19d5798 N |
2918 | else |
2919 | disk = conf->mirrors + disk_idx; | |
1da177e4 | 2920 | |
c19d5798 N |
2921 | if (disk->rdev) |
2922 | goto abort; | |
1da177e4 | 2923 | disk->rdev = rdev; |
aba336bd | 2924 | q = bdev_get_queue(rdev->bdev); |
1da177e4 LT |
2925 | |
2926 | disk->head_position = 0; | |
12cee5a8 | 2927 | disk->seq_start = MaxSector; |
1da177e4 LT |
2928 | } |
2929 | conf->raid_disks = mddev->raid_disks; | |
2930 | conf->mddev = mddev; | |
1da177e4 | 2931 | INIT_LIST_HEAD(&conf->retry_list); |
55ce74d4 | 2932 | INIT_LIST_HEAD(&conf->bio_end_io_list); |
1da177e4 LT |
2933 | |
2934 | spin_lock_init(&conf->resync_lock); | |
17999be4 | 2935 | init_waitqueue_head(&conf->wait_barrier); |
1da177e4 | 2936 | |
191ea9b2 | 2937 | bio_list_init(&conf->pending_bio_list); |
34db0cd6 | 2938 | conf->pending_count = 0; |
d890fa2b | 2939 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
191ea9b2 | 2940 | |
79ef3a8a | 2941 | conf->start_next_window = MaxSector; |
2942 | conf->current_window_requests = conf->next_window_requests = 0; | |
2943 | ||
c19d5798 | 2944 | err = -EIO; |
8f19ccb2 | 2945 | for (i = 0; i < conf->raid_disks * 2; i++) { |
1da177e4 LT |
2946 | |
2947 | disk = conf->mirrors + i; | |
2948 | ||
c19d5798 N |
2949 | if (i < conf->raid_disks && |
2950 | disk[conf->raid_disks].rdev) { | |
2951 | /* This slot has a replacement. */ | |
2952 | if (!disk->rdev) { | |
2953 | /* No original, just make the replacement | |
2954 | * a recovering spare | |
2955 | */ | |
2956 | disk->rdev = | |
2957 | disk[conf->raid_disks].rdev; | |
2958 | disk[conf->raid_disks].rdev = NULL; | |
2959 | } else if (!test_bit(In_sync, &disk->rdev->flags)) | |
2960 | /* Original is not in_sync - bad */ | |
2961 | goto abort; | |
2962 | } | |
2963 | ||
5fd6c1dc N |
2964 | if (!disk->rdev || |
2965 | !test_bit(In_sync, &disk->rdev->flags)) { | |
1da177e4 | 2966 | disk->head_position = 0; |
4f0a5e01 JB |
2967 | if (disk->rdev && |
2968 | (disk->rdev->saved_raid_disk < 0)) | |
918f0238 | 2969 | conf->fullsync = 1; |
be4d3280 | 2970 | } |
1da177e4 | 2971 | } |
709ae487 | 2972 | |
709ae487 | 2973 | err = -ENOMEM; |
0232605d | 2974 | conf->thread = md_register_thread(raid1d, mddev, "raid1"); |
1d41c216 | 2975 | if (!conf->thread) |
709ae487 | 2976 | goto abort; |
1da177e4 | 2977 | |
709ae487 N |
2978 | return conf; |
2979 | ||
2980 | abort: | |
2981 | if (conf) { | |
644df1a8 | 2982 | mempool_destroy(conf->r1bio_pool); |
709ae487 N |
2983 | kfree(conf->mirrors); |
2984 | safe_put_page(conf->tmppage); | |
2985 | kfree(conf->poolinfo); | |
2986 | kfree(conf); | |
2987 | } | |
2988 | return ERR_PTR(err); | |
2989 | } | |
2990 | ||
afa0f557 | 2991 | static void raid1_free(struct mddev *mddev, void *priv); |
849674e4 | 2992 | static int raid1_run(struct mddev *mddev) |
709ae487 | 2993 | { |
e8096360 | 2994 | struct r1conf *conf; |
709ae487 | 2995 | int i; |
3cb03002 | 2996 | struct md_rdev *rdev; |
5220ea1e | 2997 | int ret; |
2ff8cc2c | 2998 | bool discard_supported = false; |
709ae487 N |
2999 | |
3000 | if (mddev->level != 1) { | |
1d41c216 N |
3001 | pr_warn("md/raid1:%s: raid level not set to mirroring (%d)\n", |
3002 | mdname(mddev), mddev->level); | |
709ae487 N |
3003 | return -EIO; |
3004 | } | |
3005 | if (mddev->reshape_position != MaxSector) { | |
1d41c216 N |
3006 | pr_warn("md/raid1:%s: reshape_position set but not supported\n", |
3007 | mdname(mddev)); | |
709ae487 N |
3008 | return -EIO; |
3009 | } | |
1da177e4 | 3010 | /* |
709ae487 N |
3011 | * copy the already verified devices into our private RAID1 |
3012 | * bookkeeping area. [whatever we allocate in run(), | |
afa0f557 | 3013 | * should be freed in raid1_free()] |
1da177e4 | 3014 | */ |
709ae487 N |
3015 | if (mddev->private == NULL) |
3016 | conf = setup_conf(mddev); | |
3017 | else | |
3018 | conf = mddev->private; | |
1da177e4 | 3019 | |
709ae487 N |
3020 | if (IS_ERR(conf)) |
3021 | return PTR_ERR(conf); | |
1da177e4 | 3022 | |
c8dc9c65 | 3023 | if (mddev->queue) |
5026d7a9 PA |
3024 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
3025 | ||
dafb20fa | 3026 | rdev_for_each(rdev, mddev) { |
1ed7242e JB |
3027 | if (!mddev->gendisk) |
3028 | continue; | |
709ae487 N |
3029 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
3030 | rdev->data_offset << 9); | |
2ff8cc2c SL |
3031 | if (blk_queue_discard(bdev_get_queue(rdev->bdev))) |
3032 | discard_supported = true; | |
1da177e4 | 3033 | } |
191ea9b2 | 3034 | |
709ae487 N |
3035 | mddev->degraded = 0; |
3036 | for (i=0; i < conf->raid_disks; i++) | |
3037 | if (conf->mirrors[i].rdev == NULL || | |
3038 | !test_bit(In_sync, &conf->mirrors[i].rdev->flags) || | |
3039 | test_bit(Faulty, &conf->mirrors[i].rdev->flags)) | |
3040 | mddev->degraded++; | |
3041 | ||
3042 | if (conf->raid_disks - mddev->degraded == 1) | |
3043 | mddev->recovery_cp = MaxSector; | |
3044 | ||
8c6ac868 | 3045 | if (mddev->recovery_cp != MaxSector) |
1d41c216 N |
3046 | pr_info("md/raid1:%s: not clean -- starting background reconstruction\n", |
3047 | mdname(mddev)); | |
3048 | pr_info("md/raid1:%s: active with %d out of %d mirrors\n", | |
f72ffdd6 | 3049 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
1da177e4 | 3050 | mddev->raid_disks); |
709ae487 | 3051 | |
1da177e4 LT |
3052 | /* |
3053 | * Ok, everything is just fine now | |
3054 | */ | |
709ae487 N |
3055 | mddev->thread = conf->thread; |
3056 | conf->thread = NULL; | |
3057 | mddev->private = conf; | |
46533ff7 | 3058 | set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags); |
709ae487 | 3059 | |
1f403624 | 3060 | md_set_array_sectors(mddev, raid1_size(mddev, 0, 0)); |
1da177e4 | 3061 | |
1ed7242e | 3062 | if (mddev->queue) { |
2ff8cc2c SL |
3063 | if (discard_supported) |
3064 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, | |
3065 | mddev->queue); | |
3066 | else | |
3067 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, | |
3068 | mddev->queue); | |
1ed7242e | 3069 | } |
5220ea1e | 3070 | |
3071 | ret = md_integrity_register(mddev); | |
5aa61f42 N |
3072 | if (ret) { |
3073 | md_unregister_thread(&mddev->thread); | |
afa0f557 | 3074 | raid1_free(mddev, conf); |
5aa61f42 | 3075 | } |
5220ea1e | 3076 | return ret; |
1da177e4 LT |
3077 | } |
3078 | ||
afa0f557 | 3079 | static void raid1_free(struct mddev *mddev, void *priv) |
1da177e4 | 3080 | { |
afa0f557 | 3081 | struct r1conf *conf = priv; |
409c57f3 | 3082 | |
644df1a8 | 3083 | mempool_destroy(conf->r1bio_pool); |
990a8baf | 3084 | kfree(conf->mirrors); |
0fea7ed8 | 3085 | safe_put_page(conf->tmppage); |
990a8baf | 3086 | kfree(conf->poolinfo); |
1da177e4 | 3087 | kfree(conf); |
1da177e4 LT |
3088 | } |
3089 | ||
fd01b88c | 3090 | static int raid1_resize(struct mddev *mddev, sector_t sectors) |
1da177e4 LT |
3091 | { |
3092 | /* no resync is happening, and there is enough space | |
3093 | * on all devices, so we can resize. | |
3094 | * We need to make sure resync covers any new space. | |
3095 | * If the array is shrinking we should possibly wait until | |
3096 | * any io in the removed space completes, but it hardly seems | |
3097 | * worth it. | |
3098 | */ | |
a4a6125a N |
3099 | sector_t newsize = raid1_size(mddev, sectors, 0); |
3100 | if (mddev->external_size && | |
3101 | mddev->array_sectors > newsize) | |
b522adcd | 3102 | return -EINVAL; |
a4a6125a N |
3103 | if (mddev->bitmap) { |
3104 | int ret = bitmap_resize(mddev->bitmap, newsize, 0, 0); | |
3105 | if (ret) | |
3106 | return ret; | |
3107 | } | |
3108 | md_set_array_sectors(mddev, newsize); | |
f233ea5c | 3109 | set_capacity(mddev->gendisk, mddev->array_sectors); |
449aad3e | 3110 | revalidate_disk(mddev->gendisk); |
b522adcd | 3111 | if (sectors > mddev->dev_sectors && |
b098636c | 3112 | mddev->recovery_cp > mddev->dev_sectors) { |
58c0fed4 | 3113 | mddev->recovery_cp = mddev->dev_sectors; |
1da177e4 LT |
3114 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
3115 | } | |
b522adcd | 3116 | mddev->dev_sectors = sectors; |
4b5c7ae8 | 3117 | mddev->resync_max_sectors = sectors; |
1da177e4 LT |
3118 | return 0; |
3119 | } | |
3120 | ||
fd01b88c | 3121 | static int raid1_reshape(struct mddev *mddev) |
1da177e4 LT |
3122 | { |
3123 | /* We need to: | |
3124 | * 1/ resize the r1bio_pool | |
3125 | * 2/ resize conf->mirrors | |
3126 | * | |
3127 | * We allocate a new r1bio_pool if we can. | |
3128 | * Then raise a device barrier and wait until all IO stops. | |
3129 | * Then resize conf->mirrors and swap in the new r1bio pool. | |
6ea9c07c N |
3130 | * |
3131 | * At the same time, we "pack" the devices so that all the missing | |
3132 | * devices have the higher raid_disk numbers. | |
1da177e4 LT |
3133 | */ |
3134 | mempool_t *newpool, *oldpool; | |
3135 | struct pool_info *newpoolinfo; | |
0eaf822c | 3136 | struct raid1_info *newmirrors; |
e8096360 | 3137 | struct r1conf *conf = mddev->private; |
63c70c4f | 3138 | int cnt, raid_disks; |
c04be0aa | 3139 | unsigned long flags; |
b5470dc5 | 3140 | int d, d2, err; |
1da177e4 | 3141 | |
63c70c4f | 3142 | /* Cannot change chunk_size, layout, or level */ |
664e7c41 | 3143 | if (mddev->chunk_sectors != mddev->new_chunk_sectors || |
63c70c4f N |
3144 | mddev->layout != mddev->new_layout || |
3145 | mddev->level != mddev->new_level) { | |
664e7c41 | 3146 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
63c70c4f N |
3147 | mddev->new_layout = mddev->layout; |
3148 | mddev->new_level = mddev->level; | |
3149 | return -EINVAL; | |
3150 | } | |
3151 | ||
28c1b9fd GR |
3152 | if (!mddev_is_clustered(mddev)) { |
3153 | err = md_allow_write(mddev); | |
3154 | if (err) | |
3155 | return err; | |
3156 | } | |
2a2275d6 | 3157 | |
63c70c4f N |
3158 | raid_disks = mddev->raid_disks + mddev->delta_disks; |
3159 | ||
6ea9c07c N |
3160 | if (raid_disks < conf->raid_disks) { |
3161 | cnt=0; | |
3162 | for (d= 0; d < conf->raid_disks; d++) | |
3163 | if (conf->mirrors[d].rdev) | |
3164 | cnt++; | |
3165 | if (cnt > raid_disks) | |
1da177e4 | 3166 | return -EBUSY; |
6ea9c07c | 3167 | } |
1da177e4 LT |
3168 | |
3169 | newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL); | |
3170 | if (!newpoolinfo) | |
3171 | return -ENOMEM; | |
3172 | newpoolinfo->mddev = mddev; | |
8f19ccb2 | 3173 | newpoolinfo->raid_disks = raid_disks * 2; |
1da177e4 LT |
3174 | |
3175 | newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, | |
3176 | r1bio_pool_free, newpoolinfo); | |
3177 | if (!newpool) { | |
3178 | kfree(newpoolinfo); | |
3179 | return -ENOMEM; | |
3180 | } | |
0eaf822c | 3181 | newmirrors = kzalloc(sizeof(struct raid1_info) * raid_disks * 2, |
8f19ccb2 | 3182 | GFP_KERNEL); |
1da177e4 LT |
3183 | if (!newmirrors) { |
3184 | kfree(newpoolinfo); | |
3185 | mempool_destroy(newpool); | |
3186 | return -ENOMEM; | |
3187 | } | |
1da177e4 | 3188 | |
e2d59925 | 3189 | freeze_array(conf, 0); |
1da177e4 LT |
3190 | |
3191 | /* ok, everything is stopped */ | |
3192 | oldpool = conf->r1bio_pool; | |
3193 | conf->r1bio_pool = newpool; | |
6ea9c07c | 3194 | |
a88aa786 | 3195 | for (d = d2 = 0; d < conf->raid_disks; d++) { |
3cb03002 | 3196 | struct md_rdev *rdev = conf->mirrors[d].rdev; |
a88aa786 | 3197 | if (rdev && rdev->raid_disk != d2) { |
36fad858 | 3198 | sysfs_unlink_rdev(mddev, rdev); |
a88aa786 | 3199 | rdev->raid_disk = d2; |
36fad858 NK |
3200 | sysfs_unlink_rdev(mddev, rdev); |
3201 | if (sysfs_link_rdev(mddev, rdev)) | |
1d41c216 N |
3202 | pr_warn("md/raid1:%s: cannot register rd%d\n", |
3203 | mdname(mddev), rdev->raid_disk); | |
6ea9c07c | 3204 | } |
a88aa786 N |
3205 | if (rdev) |
3206 | newmirrors[d2++].rdev = rdev; | |
3207 | } | |
1da177e4 LT |
3208 | kfree(conf->mirrors); |
3209 | conf->mirrors = newmirrors; | |
3210 | kfree(conf->poolinfo); | |
3211 | conf->poolinfo = newpoolinfo; | |
3212 | ||
c04be0aa | 3213 | spin_lock_irqsave(&conf->device_lock, flags); |
1da177e4 | 3214 | mddev->degraded += (raid_disks - conf->raid_disks); |
c04be0aa | 3215 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 | 3216 | conf->raid_disks = mddev->raid_disks = raid_disks; |
63c70c4f | 3217 | mddev->delta_disks = 0; |
1da177e4 | 3218 | |
e2d59925 | 3219 | unfreeze_array(conf); |
1da177e4 | 3220 | |
985ca973 | 3221 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
1da177e4 LT |
3222 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
3223 | md_wakeup_thread(mddev->thread); | |
3224 | ||
3225 | mempool_destroy(oldpool); | |
3226 | return 0; | |
3227 | } | |
3228 | ||
fd01b88c | 3229 | static void raid1_quiesce(struct mddev *mddev, int state) |
36fa3063 | 3230 | { |
e8096360 | 3231 | struct r1conf *conf = mddev->private; |
36fa3063 N |
3232 | |
3233 | switch(state) { | |
6eef4b21 N |
3234 | case 2: /* wake for suspend */ |
3235 | wake_up(&conf->wait_barrier); | |
3236 | break; | |
9e6603da | 3237 | case 1: |
07169fd4 | 3238 | freeze_array(conf, 0); |
36fa3063 | 3239 | break; |
9e6603da | 3240 | case 0: |
07169fd4 | 3241 | unfreeze_array(conf); |
36fa3063 N |
3242 | break; |
3243 | } | |
36fa3063 N |
3244 | } |
3245 | ||
fd01b88c | 3246 | static void *raid1_takeover(struct mddev *mddev) |
709ae487 N |
3247 | { |
3248 | /* raid1 can take over: | |
3249 | * raid5 with 2 devices, any layout or chunk size | |
3250 | */ | |
3251 | if (mddev->level == 5 && mddev->raid_disks == 2) { | |
e8096360 | 3252 | struct r1conf *conf; |
709ae487 N |
3253 | mddev->new_level = 1; |
3254 | mddev->new_layout = 0; | |
3255 | mddev->new_chunk_sectors = 0; | |
3256 | conf = setup_conf(mddev); | |
6995f0b2 | 3257 | if (!IS_ERR(conf)) { |
07169fd4 | 3258 | /* Array must appear to be quiesced */ |
3259 | conf->array_frozen = 1; | |
6995f0b2 SL |
3260 | clear_bit(MD_HAS_JOURNAL, &mddev->flags); |
3261 | clear_bit(MD_JOURNAL_CLEAN, &mddev->flags); | |
3262 | } | |
709ae487 N |
3263 | return conf; |
3264 | } | |
3265 | return ERR_PTR(-EINVAL); | |
3266 | } | |
1da177e4 | 3267 | |
84fc4b56 | 3268 | static struct md_personality raid1_personality = |
1da177e4 LT |
3269 | { |
3270 | .name = "raid1", | |
2604b703 | 3271 | .level = 1, |
1da177e4 | 3272 | .owner = THIS_MODULE, |
849674e4 SL |
3273 | .make_request = raid1_make_request, |
3274 | .run = raid1_run, | |
afa0f557 | 3275 | .free = raid1_free, |
849674e4 SL |
3276 | .status = raid1_status, |
3277 | .error_handler = raid1_error, | |
1da177e4 LT |
3278 | .hot_add_disk = raid1_add_disk, |
3279 | .hot_remove_disk= raid1_remove_disk, | |
3280 | .spare_active = raid1_spare_active, | |
849674e4 | 3281 | .sync_request = raid1_sync_request, |
1da177e4 | 3282 | .resize = raid1_resize, |
80c3a6ce | 3283 | .size = raid1_size, |
63c70c4f | 3284 | .check_reshape = raid1_reshape, |
36fa3063 | 3285 | .quiesce = raid1_quiesce, |
709ae487 | 3286 | .takeover = raid1_takeover, |
5c675f83 | 3287 | .congested = raid1_congested, |
1da177e4 LT |
3288 | }; |
3289 | ||
3290 | static int __init raid_init(void) | |
3291 | { | |
2604b703 | 3292 | return register_md_personality(&raid1_personality); |
1da177e4 LT |
3293 | } |
3294 | ||
3295 | static void raid_exit(void) | |
3296 | { | |
2604b703 | 3297 | unregister_md_personality(&raid1_personality); |
1da177e4 LT |
3298 | } |
3299 | ||
3300 | module_init(raid_init); | |
3301 | module_exit(raid_exit); | |
3302 | MODULE_LICENSE("GPL"); | |
0efb9e61 | 3303 | MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD"); |
1da177e4 | 3304 | MODULE_ALIAS("md-personality-3"); /* RAID1 */ |
d9d166c2 | 3305 | MODULE_ALIAS("md-raid1"); |
2604b703 | 3306 | MODULE_ALIAS("md-level-1"); |
34db0cd6 N |
3307 | |
3308 | module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); |