Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * raid5.c : Multiple Devices driver for Linux | |
3 | * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman | |
4 | * Copyright (C) 1999, 2000 Ingo Molnar | |
16a53ecc | 5 | * Copyright (C) 2002, 2003 H. Peter Anvin |
1da177e4 | 6 | * |
16a53ecc N |
7 | * RAID-4/5/6 management functions. |
8 | * Thanks to Penguin Computing for making the RAID-6 development possible | |
9 | * by donating a test server! | |
1da177e4 LT |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2, or (at your option) | |
14 | * any later version. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * (for example /usr/src/linux/COPYING); if not, write to the Free | |
18 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
ae3c20cc N |
21 | /* |
22 | * BITMAP UNPLUGGING: | |
23 | * | |
24 | * The sequencing for updating the bitmap reliably is a little | |
25 | * subtle (and I got it wrong the first time) so it deserves some | |
26 | * explanation. | |
27 | * | |
28 | * We group bitmap updates into batches. Each batch has a number. | |
29 | * We may write out several batches at once, but that isn't very important. | |
7c13edc8 N |
30 | * conf->seq_write is the number of the last batch successfully written. |
31 | * conf->seq_flush is the number of the last batch that was closed to | |
ae3c20cc N |
32 | * new additions. |
33 | * When we discover that we will need to write to any block in a stripe | |
34 | * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq | |
7c13edc8 | 35 | * the number of the batch it will be in. This is seq_flush+1. |
ae3c20cc N |
36 | * When we are ready to do a write, if that batch hasn't been written yet, |
37 | * we plug the array and queue the stripe for later. | |
38 | * When an unplug happens, we increment bm_flush, thus closing the current | |
39 | * batch. | |
40 | * When we notice that bm_flush > bm_write, we write out all pending updates | |
41 | * to the bitmap, and advance bm_write to where bm_flush was. | |
42 | * This may occasionally write a bit out twice, but is sure never to | |
43 | * miss any bits. | |
44 | */ | |
1da177e4 | 45 | |
bff61975 | 46 | #include <linux/blkdev.h> |
f6705578 | 47 | #include <linux/kthread.h> |
f701d589 | 48 | #include <linux/raid/pq.h> |
91c00924 | 49 | #include <linux/async_tx.h> |
056075c7 | 50 | #include <linux/module.h> |
07a3b417 | 51 | #include <linux/async.h> |
bff61975 | 52 | #include <linux/seq_file.h> |
36d1c647 | 53 | #include <linux/cpu.h> |
5a0e3ad6 | 54 | #include <linux/slab.h> |
8bda470e | 55 | #include <linux/ratelimit.h> |
a9add5d9 N |
56 | #include <trace/events/block.h> |
57 | ||
43b2e5d8 | 58 | #include "md.h" |
bff61975 | 59 | #include "raid5.h" |
54071b38 | 60 | #include "raid0.h" |
ef740c37 | 61 | #include "bitmap.h" |
72626685 | 62 | |
1da177e4 LT |
63 | /* |
64 | * Stripe cache | |
65 | */ | |
66 | ||
67 | #define NR_STRIPES 256 | |
68 | #define STRIPE_SIZE PAGE_SIZE | |
69 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) | |
70 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) | |
71 | #define IO_THRESHOLD 1 | |
8b3e6cdc | 72 | #define BYPASS_THRESHOLD 1 |
fccddba0 | 73 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
1da177e4 LT |
74 | #define HASH_MASK (NR_HASH - 1) |
75 | ||
d1688a6d | 76 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
db298e19 N |
77 | { |
78 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; | |
79 | return &conf->stripe_hashtbl[hash]; | |
80 | } | |
1da177e4 LT |
81 | |
82 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector | |
83 | * order without overlap. There may be several bio's per stripe+device, and | |
84 | * a bio could span several devices. | |
85 | * When walking this list for a particular stripe+device, we must never proceed | |
86 | * beyond a bio that extends past this device, as the next bio might no longer | |
87 | * be valid. | |
db298e19 | 88 | * This function is used to determine the 'next' bio in the list, given the sector |
1da177e4 LT |
89 | * of the current stripe+device |
90 | */ | |
db298e19 N |
91 | static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector) |
92 | { | |
aa8b57aa | 93 | int sectors = bio_sectors(bio); |
db298e19 N |
94 | if (bio->bi_sector + sectors < sector + STRIPE_SECTORS) |
95 | return bio->bi_next; | |
96 | else | |
97 | return NULL; | |
98 | } | |
1da177e4 | 99 | |
960e739d | 100 | /* |
5b99c2ff JA |
101 | * We maintain a biased count of active stripes in the bottom 16 bits of |
102 | * bi_phys_segments, and a count of processed stripes in the upper 16 bits | |
960e739d | 103 | */ |
e7836bd6 | 104 | static inline int raid5_bi_processed_stripes(struct bio *bio) |
960e739d | 105 | { |
e7836bd6 SL |
106 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
107 | return (atomic_read(segments) >> 16) & 0xffff; | |
960e739d JA |
108 | } |
109 | ||
e7836bd6 | 110 | static inline int raid5_dec_bi_active_stripes(struct bio *bio) |
960e739d | 111 | { |
e7836bd6 SL |
112 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
113 | return atomic_sub_return(1, segments) & 0xffff; | |
960e739d JA |
114 | } |
115 | ||
e7836bd6 | 116 | static inline void raid5_inc_bi_active_stripes(struct bio *bio) |
960e739d | 117 | { |
e7836bd6 SL |
118 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
119 | atomic_inc(segments); | |
960e739d JA |
120 | } |
121 | ||
e7836bd6 SL |
122 | static inline void raid5_set_bi_processed_stripes(struct bio *bio, |
123 | unsigned int cnt) | |
960e739d | 124 | { |
e7836bd6 SL |
125 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
126 | int old, new; | |
960e739d | 127 | |
e7836bd6 SL |
128 | do { |
129 | old = atomic_read(segments); | |
130 | new = (old & 0xffff) | (cnt << 16); | |
131 | } while (atomic_cmpxchg(segments, old, new) != old); | |
960e739d JA |
132 | } |
133 | ||
e7836bd6 | 134 | static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt) |
960e739d | 135 | { |
e7836bd6 SL |
136 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
137 | atomic_set(segments, cnt); | |
960e739d JA |
138 | } |
139 | ||
d0dabf7e N |
140 | /* Find first data disk in a raid6 stripe */ |
141 | static inline int raid6_d0(struct stripe_head *sh) | |
142 | { | |
67cc2b81 N |
143 | if (sh->ddf_layout) |
144 | /* ddf always start from first device */ | |
145 | return 0; | |
146 | /* md starts just after Q block */ | |
d0dabf7e N |
147 | if (sh->qd_idx == sh->disks - 1) |
148 | return 0; | |
149 | else | |
150 | return sh->qd_idx + 1; | |
151 | } | |
16a53ecc N |
152 | static inline int raid6_next_disk(int disk, int raid_disks) |
153 | { | |
154 | disk++; | |
155 | return (disk < raid_disks) ? disk : 0; | |
156 | } | |
a4456856 | 157 | |
d0dabf7e N |
158 | /* When walking through the disks in a raid5, starting at raid6_d0, |
159 | * We need to map each disk to a 'slot', where the data disks are slot | |
160 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk | |
161 | * is raid_disks-1. This help does that mapping. | |
162 | */ | |
67cc2b81 N |
163 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
164 | int *count, int syndrome_disks) | |
d0dabf7e | 165 | { |
6629542e | 166 | int slot = *count; |
67cc2b81 | 167 | |
e4424fee | 168 | if (sh->ddf_layout) |
6629542e | 169 | (*count)++; |
d0dabf7e | 170 | if (idx == sh->pd_idx) |
67cc2b81 | 171 | return syndrome_disks; |
d0dabf7e | 172 | if (idx == sh->qd_idx) |
67cc2b81 | 173 | return syndrome_disks + 1; |
e4424fee | 174 | if (!sh->ddf_layout) |
6629542e | 175 | (*count)++; |
d0dabf7e N |
176 | return slot; |
177 | } | |
178 | ||
a4456856 DW |
179 | static void return_io(struct bio *return_bi) |
180 | { | |
181 | struct bio *bi = return_bi; | |
182 | while (bi) { | |
a4456856 DW |
183 | |
184 | return_bi = bi->bi_next; | |
185 | bi->bi_next = NULL; | |
186 | bi->bi_size = 0; | |
0a82a8d1 LT |
187 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
188 | bi, 0); | |
0e13fe23 | 189 | bio_endio(bi, 0); |
a4456856 DW |
190 | bi = return_bi; |
191 | } | |
192 | } | |
193 | ||
d1688a6d | 194 | static void print_raid5_conf (struct r5conf *conf); |
1da177e4 | 195 | |
600aa109 DW |
196 | static int stripe_operations_active(struct stripe_head *sh) |
197 | { | |
198 | return sh->check_state || sh->reconstruct_state || | |
199 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || | |
200 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
201 | } | |
202 | ||
4eb788df | 203 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh) |
1da177e4 | 204 | { |
4eb788df SL |
205 | BUG_ON(!list_empty(&sh->lru)); |
206 | BUG_ON(atomic_read(&conf->active_stripes)==0); | |
207 | if (test_bit(STRIPE_HANDLE, &sh->state)) { | |
208 | if (test_bit(STRIPE_DELAYED, &sh->state) && | |
209 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
210 | list_add_tail(&sh->lru, &conf->delayed_list); | |
211 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && | |
212 | sh->bm_seq - conf->seq_write > 0) | |
213 | list_add_tail(&sh->lru, &conf->bitmap_list); | |
214 | else { | |
215 | clear_bit(STRIPE_DELAYED, &sh->state); | |
216 | clear_bit(STRIPE_BIT_DELAY, &sh->state); | |
217 | list_add_tail(&sh->lru, &conf->handle_list); | |
218 | } | |
219 | md_wakeup_thread(conf->mddev->thread); | |
220 | } else { | |
221 | BUG_ON(stripe_operations_active(sh)); | |
222 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
223 | if (atomic_dec_return(&conf->preread_active_stripes) | |
224 | < IO_THRESHOLD) | |
225 | md_wakeup_thread(conf->mddev->thread); | |
226 | atomic_dec(&conf->active_stripes); | |
227 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { | |
228 | list_add_tail(&sh->lru, &conf->inactive_list); | |
229 | wake_up(&conf->wait_for_stripe); | |
230 | if (conf->retry_read_aligned) | |
231 | md_wakeup_thread(conf->mddev->thread); | |
1da177e4 LT |
232 | } |
233 | } | |
234 | } | |
d0dabf7e | 235 | |
4eb788df SL |
236 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh) |
237 | { | |
238 | if (atomic_dec_and_test(&sh->count)) | |
239 | do_release_stripe(conf, sh); | |
240 | } | |
241 | ||
1da177e4 LT |
242 | static void release_stripe(struct stripe_head *sh) |
243 | { | |
d1688a6d | 244 | struct r5conf *conf = sh->raid_conf; |
1da177e4 | 245 | unsigned long flags; |
16a53ecc | 246 | |
4eb788df SL |
247 | local_irq_save(flags); |
248 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { | |
249 | do_release_stripe(conf, sh); | |
250 | spin_unlock(&conf->device_lock); | |
251 | } | |
252 | local_irq_restore(flags); | |
1da177e4 LT |
253 | } |
254 | ||
fccddba0 | 255 | static inline void remove_hash(struct stripe_head *sh) |
1da177e4 | 256 | { |
45b4233c DW |
257 | pr_debug("remove_hash(), stripe %llu\n", |
258 | (unsigned long long)sh->sector); | |
1da177e4 | 259 | |
fccddba0 | 260 | hlist_del_init(&sh->hash); |
1da177e4 LT |
261 | } |
262 | ||
d1688a6d | 263 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
1da177e4 | 264 | { |
fccddba0 | 265 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
1da177e4 | 266 | |
45b4233c DW |
267 | pr_debug("insert_hash(), stripe %llu\n", |
268 | (unsigned long long)sh->sector); | |
1da177e4 | 269 | |
fccddba0 | 270 | hlist_add_head(&sh->hash, hp); |
1da177e4 LT |
271 | } |
272 | ||
273 | ||
274 | /* find an idle stripe, make sure it is unhashed, and return it. */ | |
d1688a6d | 275 | static struct stripe_head *get_free_stripe(struct r5conf *conf) |
1da177e4 LT |
276 | { |
277 | struct stripe_head *sh = NULL; | |
278 | struct list_head *first; | |
279 | ||
1da177e4 LT |
280 | if (list_empty(&conf->inactive_list)) |
281 | goto out; | |
282 | first = conf->inactive_list.next; | |
283 | sh = list_entry(first, struct stripe_head, lru); | |
284 | list_del_init(first); | |
285 | remove_hash(sh); | |
286 | atomic_inc(&conf->active_stripes); | |
287 | out: | |
288 | return sh; | |
289 | } | |
290 | ||
e4e11e38 | 291 | static void shrink_buffers(struct stripe_head *sh) |
1da177e4 LT |
292 | { |
293 | struct page *p; | |
294 | int i; | |
e4e11e38 | 295 | int num = sh->raid_conf->pool_size; |
1da177e4 | 296 | |
e4e11e38 | 297 | for (i = 0; i < num ; i++) { |
1da177e4 LT |
298 | p = sh->dev[i].page; |
299 | if (!p) | |
300 | continue; | |
301 | sh->dev[i].page = NULL; | |
2d1f3b5d | 302 | put_page(p); |
1da177e4 LT |
303 | } |
304 | } | |
305 | ||
e4e11e38 | 306 | static int grow_buffers(struct stripe_head *sh) |
1da177e4 LT |
307 | { |
308 | int i; | |
e4e11e38 | 309 | int num = sh->raid_conf->pool_size; |
1da177e4 | 310 | |
e4e11e38 | 311 | for (i = 0; i < num; i++) { |
1da177e4 LT |
312 | struct page *page; |
313 | ||
314 | if (!(page = alloc_page(GFP_KERNEL))) { | |
315 | return 1; | |
316 | } | |
317 | sh->dev[i].page = page; | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
784052ec | 322 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
d1688a6d | 323 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
911d4ee8 | 324 | struct stripe_head *sh); |
1da177e4 | 325 | |
b5663ba4 | 326 | static void init_stripe(struct stripe_head *sh, sector_t sector, int previous) |
1da177e4 | 327 | { |
d1688a6d | 328 | struct r5conf *conf = sh->raid_conf; |
7ecaa1e6 | 329 | int i; |
1da177e4 | 330 | |
78bafebd ES |
331 | BUG_ON(atomic_read(&sh->count) != 0); |
332 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); | |
600aa109 | 333 | BUG_ON(stripe_operations_active(sh)); |
d84e0f10 | 334 | |
45b4233c | 335 | pr_debug("init_stripe called, stripe %llu\n", |
1da177e4 LT |
336 | (unsigned long long)sh->sector); |
337 | ||
338 | remove_hash(sh); | |
16a53ecc | 339 | |
86b42c71 | 340 | sh->generation = conf->generation - previous; |
b5663ba4 | 341 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
1da177e4 | 342 | sh->sector = sector; |
911d4ee8 | 343 | stripe_set_idx(sector, conf, previous, sh); |
1da177e4 LT |
344 | sh->state = 0; |
345 | ||
7ecaa1e6 N |
346 | |
347 | for (i = sh->disks; i--; ) { | |
1da177e4 LT |
348 | struct r5dev *dev = &sh->dev[i]; |
349 | ||
d84e0f10 | 350 | if (dev->toread || dev->read || dev->towrite || dev->written || |
1da177e4 | 351 | test_bit(R5_LOCKED, &dev->flags)) { |
d84e0f10 | 352 | printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n", |
1da177e4 | 353 | (unsigned long long)sh->sector, i, dev->toread, |
d84e0f10 | 354 | dev->read, dev->towrite, dev->written, |
1da177e4 | 355 | test_bit(R5_LOCKED, &dev->flags)); |
8cfa7b0f | 356 | WARN_ON(1); |
1da177e4 LT |
357 | } |
358 | dev->flags = 0; | |
784052ec | 359 | raid5_build_block(sh, i, previous); |
1da177e4 LT |
360 | } |
361 | insert_hash(conf, sh); | |
362 | } | |
363 | ||
d1688a6d | 364 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
86b42c71 | 365 | short generation) |
1da177e4 LT |
366 | { |
367 | struct stripe_head *sh; | |
368 | ||
45b4233c | 369 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
b67bfe0d | 370 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
86b42c71 | 371 | if (sh->sector == sector && sh->generation == generation) |
1da177e4 | 372 | return sh; |
45b4233c | 373 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
1da177e4 LT |
374 | return NULL; |
375 | } | |
376 | ||
674806d6 N |
377 | /* |
378 | * Need to check if array has failed when deciding whether to: | |
379 | * - start an array | |
380 | * - remove non-faulty devices | |
381 | * - add a spare | |
382 | * - allow a reshape | |
383 | * This determination is simple when no reshape is happening. | |
384 | * However if there is a reshape, we need to carefully check | |
385 | * both the before and after sections. | |
386 | * This is because some failed devices may only affect one | |
387 | * of the two sections, and some non-in_sync devices may | |
388 | * be insync in the section most affected by failed devices. | |
389 | */ | |
908f4fbd | 390 | static int calc_degraded(struct r5conf *conf) |
674806d6 | 391 | { |
908f4fbd | 392 | int degraded, degraded2; |
674806d6 | 393 | int i; |
674806d6 N |
394 | |
395 | rcu_read_lock(); | |
396 | degraded = 0; | |
397 | for (i = 0; i < conf->previous_raid_disks; i++) { | |
3cb03002 | 398 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
e5c86471 N |
399 | if (rdev && test_bit(Faulty, &rdev->flags)) |
400 | rdev = rcu_dereference(conf->disks[i].replacement); | |
674806d6 N |
401 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
402 | degraded++; | |
403 | else if (test_bit(In_sync, &rdev->flags)) | |
404 | ; | |
405 | else | |
406 | /* not in-sync or faulty. | |
407 | * If the reshape increases the number of devices, | |
408 | * this is being recovered by the reshape, so | |
409 | * this 'previous' section is not in_sync. | |
410 | * If the number of devices is being reduced however, | |
411 | * the device can only be part of the array if | |
412 | * we are reverting a reshape, so this section will | |
413 | * be in-sync. | |
414 | */ | |
415 | if (conf->raid_disks >= conf->previous_raid_disks) | |
416 | degraded++; | |
417 | } | |
418 | rcu_read_unlock(); | |
908f4fbd N |
419 | if (conf->raid_disks == conf->previous_raid_disks) |
420 | return degraded; | |
674806d6 | 421 | rcu_read_lock(); |
908f4fbd | 422 | degraded2 = 0; |
674806d6 | 423 | for (i = 0; i < conf->raid_disks; i++) { |
3cb03002 | 424 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
e5c86471 N |
425 | if (rdev && test_bit(Faulty, &rdev->flags)) |
426 | rdev = rcu_dereference(conf->disks[i].replacement); | |
674806d6 | 427 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
908f4fbd | 428 | degraded2++; |
674806d6 N |
429 | else if (test_bit(In_sync, &rdev->flags)) |
430 | ; | |
431 | else | |
432 | /* not in-sync or faulty. | |
433 | * If reshape increases the number of devices, this | |
434 | * section has already been recovered, else it | |
435 | * almost certainly hasn't. | |
436 | */ | |
437 | if (conf->raid_disks <= conf->previous_raid_disks) | |
908f4fbd | 438 | degraded2++; |
674806d6 N |
439 | } |
440 | rcu_read_unlock(); | |
908f4fbd N |
441 | if (degraded2 > degraded) |
442 | return degraded2; | |
443 | return degraded; | |
444 | } | |
445 | ||
446 | static int has_failed(struct r5conf *conf) | |
447 | { | |
448 | int degraded; | |
449 | ||
450 | if (conf->mddev->reshape_position == MaxSector) | |
451 | return conf->mddev->degraded > conf->max_degraded; | |
452 | ||
453 | degraded = calc_degraded(conf); | |
674806d6 N |
454 | if (degraded > conf->max_degraded) |
455 | return 1; | |
456 | return 0; | |
457 | } | |
458 | ||
b5663ba4 | 459 | static struct stripe_head * |
d1688a6d | 460 | get_active_stripe(struct r5conf *conf, sector_t sector, |
a8c906ca | 461 | int previous, int noblock, int noquiesce) |
1da177e4 LT |
462 | { |
463 | struct stripe_head *sh; | |
464 | ||
45b4233c | 465 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
1da177e4 LT |
466 | |
467 | spin_lock_irq(&conf->device_lock); | |
468 | ||
469 | do { | |
72626685 | 470 | wait_event_lock_irq(conf->wait_for_stripe, |
a8c906ca | 471 | conf->quiesce == 0 || noquiesce, |
eed8c02e | 472 | conf->device_lock); |
86b42c71 | 473 | sh = __find_stripe(conf, sector, conf->generation - previous); |
1da177e4 LT |
474 | if (!sh) { |
475 | if (!conf->inactive_blocked) | |
476 | sh = get_free_stripe(conf); | |
477 | if (noblock && sh == NULL) | |
478 | break; | |
479 | if (!sh) { | |
480 | conf->inactive_blocked = 1; | |
481 | wait_event_lock_irq(conf->wait_for_stripe, | |
482 | !list_empty(&conf->inactive_list) && | |
5036805b N |
483 | (atomic_read(&conf->active_stripes) |
484 | < (conf->max_nr_stripes *3/4) | |
1da177e4 | 485 | || !conf->inactive_blocked), |
eed8c02e | 486 | conf->device_lock); |
1da177e4 LT |
487 | conf->inactive_blocked = 0; |
488 | } else | |
b5663ba4 | 489 | init_stripe(sh, sector, previous); |
1da177e4 LT |
490 | } else { |
491 | if (atomic_read(&sh->count)) { | |
ab69ae12 | 492 | BUG_ON(!list_empty(&sh->lru) |
8811b596 SL |
493 | && !test_bit(STRIPE_EXPANDING, &sh->state) |
494 | && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)); | |
1da177e4 LT |
495 | } else { |
496 | if (!test_bit(STRIPE_HANDLE, &sh->state)) | |
497 | atomic_inc(&conf->active_stripes); | |
ff4e8d9a N |
498 | if (list_empty(&sh->lru) && |
499 | !test_bit(STRIPE_EXPANDING, &sh->state)) | |
16a53ecc N |
500 | BUG(); |
501 | list_del_init(&sh->lru); | |
1da177e4 LT |
502 | } |
503 | } | |
504 | } while (sh == NULL); | |
505 | ||
506 | if (sh) | |
507 | atomic_inc(&sh->count); | |
508 | ||
509 | spin_unlock_irq(&conf->device_lock); | |
510 | return sh; | |
511 | } | |
512 | ||
05616be5 N |
513 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
514 | * in this stripe_head. | |
515 | */ | |
516 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) | |
517 | { | |
518 | sector_t progress = conf->reshape_progress; | |
519 | /* Need a memory barrier to make sure we see the value | |
520 | * of conf->generation, or ->data_offset that was set before | |
521 | * reshape_progress was updated. | |
522 | */ | |
523 | smp_rmb(); | |
524 | if (progress == MaxSector) | |
525 | return 0; | |
526 | if (sh->generation == conf->generation - 1) | |
527 | return 0; | |
528 | /* We are in a reshape, and this is a new-generation stripe, | |
529 | * so use new_data_offset. | |
530 | */ | |
531 | return 1; | |
532 | } | |
533 | ||
6712ecf8 N |
534 | static void |
535 | raid5_end_read_request(struct bio *bi, int error); | |
536 | static void | |
537 | raid5_end_write_request(struct bio *bi, int error); | |
91c00924 | 538 | |
c4e5ac0a | 539 | static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s) |
91c00924 | 540 | { |
d1688a6d | 541 | struct r5conf *conf = sh->raid_conf; |
91c00924 DW |
542 | int i, disks = sh->disks; |
543 | ||
544 | might_sleep(); | |
545 | ||
546 | for (i = disks; i--; ) { | |
547 | int rw; | |
9a3e1101 | 548 | int replace_only = 0; |
977df362 N |
549 | struct bio *bi, *rbi; |
550 | struct md_rdev *rdev, *rrdev = NULL; | |
e9c7469b TH |
551 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
552 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) | |
553 | rw = WRITE_FUA; | |
554 | else | |
555 | rw = WRITE; | |
9e444768 | 556 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
620125f2 | 557 | rw |= REQ_DISCARD; |
e9c7469b | 558 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
91c00924 | 559 | rw = READ; |
9a3e1101 N |
560 | else if (test_and_clear_bit(R5_WantReplace, |
561 | &sh->dev[i].flags)) { | |
562 | rw = WRITE; | |
563 | replace_only = 1; | |
564 | } else | |
91c00924 | 565 | continue; |
bc0934f0 SL |
566 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
567 | rw |= REQ_SYNC; | |
91c00924 DW |
568 | |
569 | bi = &sh->dev[i].req; | |
977df362 | 570 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
91c00924 | 571 | |
91c00924 | 572 | rcu_read_lock(); |
9a3e1101 | 573 | rrdev = rcu_dereference(conf->disks[i].replacement); |
dd054fce N |
574 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
575 | rdev = rcu_dereference(conf->disks[i].rdev); | |
576 | if (!rdev) { | |
577 | rdev = rrdev; | |
578 | rrdev = NULL; | |
579 | } | |
9a3e1101 N |
580 | if (rw & WRITE) { |
581 | if (replace_only) | |
582 | rdev = NULL; | |
dd054fce N |
583 | if (rdev == rrdev) |
584 | /* We raced and saw duplicates */ | |
585 | rrdev = NULL; | |
9a3e1101 | 586 | } else { |
dd054fce | 587 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
9a3e1101 N |
588 | rdev = rrdev; |
589 | rrdev = NULL; | |
590 | } | |
977df362 | 591 | |
91c00924 DW |
592 | if (rdev && test_bit(Faulty, &rdev->flags)) |
593 | rdev = NULL; | |
594 | if (rdev) | |
595 | atomic_inc(&rdev->nr_pending); | |
977df362 N |
596 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
597 | rrdev = NULL; | |
598 | if (rrdev) | |
599 | atomic_inc(&rrdev->nr_pending); | |
91c00924 DW |
600 | rcu_read_unlock(); |
601 | ||
73e92e51 | 602 | /* We have already checked bad blocks for reads. Now |
977df362 N |
603 | * need to check for writes. We never accept write errors |
604 | * on the replacement, so we don't to check rrdev. | |
73e92e51 N |
605 | */ |
606 | while ((rw & WRITE) && rdev && | |
607 | test_bit(WriteErrorSeen, &rdev->flags)) { | |
608 | sector_t first_bad; | |
609 | int bad_sectors; | |
610 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, | |
611 | &first_bad, &bad_sectors); | |
612 | if (!bad) | |
613 | break; | |
614 | ||
615 | if (bad < 0) { | |
616 | set_bit(BlockedBadBlocks, &rdev->flags); | |
617 | if (!conf->mddev->external && | |
618 | conf->mddev->flags) { | |
619 | /* It is very unlikely, but we might | |
620 | * still need to write out the | |
621 | * bad block log - better give it | |
622 | * a chance*/ | |
623 | md_check_recovery(conf->mddev); | |
624 | } | |
1850753d | 625 | /* |
626 | * Because md_wait_for_blocked_rdev | |
627 | * will dec nr_pending, we must | |
628 | * increment it first. | |
629 | */ | |
630 | atomic_inc(&rdev->nr_pending); | |
73e92e51 N |
631 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
632 | } else { | |
633 | /* Acknowledged bad block - skip the write */ | |
634 | rdev_dec_pending(rdev, conf->mddev); | |
635 | rdev = NULL; | |
636 | } | |
637 | } | |
638 | ||
91c00924 | 639 | if (rdev) { |
9a3e1101 N |
640 | if (s->syncing || s->expanding || s->expanded |
641 | || s->replacing) | |
91c00924 DW |
642 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
643 | ||
2b7497f0 DW |
644 | set_bit(STRIPE_IO_STARTED, &sh->state); |
645 | ||
2f6db2a7 | 646 | bio_reset(bi); |
91c00924 | 647 | bi->bi_bdev = rdev->bdev; |
2f6db2a7 KO |
648 | bi->bi_rw = rw; |
649 | bi->bi_end_io = (rw & WRITE) | |
650 | ? raid5_end_write_request | |
651 | : raid5_end_read_request; | |
652 | bi->bi_private = sh; | |
653 | ||
91c00924 | 654 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
e46b272b | 655 | __func__, (unsigned long long)sh->sector, |
91c00924 DW |
656 | bi->bi_rw, i); |
657 | atomic_inc(&sh->count); | |
05616be5 N |
658 | if (use_new_offset(conf, sh)) |
659 | bi->bi_sector = (sh->sector | |
660 | + rdev->new_data_offset); | |
661 | else | |
662 | bi->bi_sector = (sh->sector | |
663 | + rdev->data_offset); | |
3f9e7c14 | 664 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
665 | bi->bi_rw |= REQ_FLUSH; | |
666 | ||
4997b72e | 667 | bi->bi_vcnt = 1; |
91c00924 DW |
668 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
669 | bi->bi_io_vec[0].bv_offset = 0; | |
670 | bi->bi_size = STRIPE_SIZE; | |
977df362 N |
671 | if (rrdev) |
672 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); | |
e3620a3a JB |
673 | |
674 | if (conf->mddev->gendisk) | |
675 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), | |
676 | bi, disk_devt(conf->mddev->gendisk), | |
677 | sh->dev[i].sector); | |
91c00924 | 678 | generic_make_request(bi); |
977df362 N |
679 | } |
680 | if (rrdev) { | |
9a3e1101 N |
681 | if (s->syncing || s->expanding || s->expanded |
682 | || s->replacing) | |
977df362 N |
683 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
684 | ||
685 | set_bit(STRIPE_IO_STARTED, &sh->state); | |
686 | ||
2f6db2a7 | 687 | bio_reset(rbi); |
977df362 | 688 | rbi->bi_bdev = rrdev->bdev; |
2f6db2a7 KO |
689 | rbi->bi_rw = rw; |
690 | BUG_ON(!(rw & WRITE)); | |
691 | rbi->bi_end_io = raid5_end_write_request; | |
692 | rbi->bi_private = sh; | |
693 | ||
977df362 N |
694 | pr_debug("%s: for %llu schedule op %ld on " |
695 | "replacement disc %d\n", | |
696 | __func__, (unsigned long long)sh->sector, | |
697 | rbi->bi_rw, i); | |
698 | atomic_inc(&sh->count); | |
05616be5 N |
699 | if (use_new_offset(conf, sh)) |
700 | rbi->bi_sector = (sh->sector | |
701 | + rrdev->new_data_offset); | |
702 | else | |
703 | rbi->bi_sector = (sh->sector | |
704 | + rrdev->data_offset); | |
4997b72e | 705 | rbi->bi_vcnt = 1; |
977df362 N |
706 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
707 | rbi->bi_io_vec[0].bv_offset = 0; | |
708 | rbi->bi_size = STRIPE_SIZE; | |
e3620a3a JB |
709 | if (conf->mddev->gendisk) |
710 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), | |
711 | rbi, disk_devt(conf->mddev->gendisk), | |
712 | sh->dev[i].sector); | |
977df362 N |
713 | generic_make_request(rbi); |
714 | } | |
715 | if (!rdev && !rrdev) { | |
b062962e | 716 | if (rw & WRITE) |
91c00924 DW |
717 | set_bit(STRIPE_DEGRADED, &sh->state); |
718 | pr_debug("skip op %ld on disc %d for sector %llu\n", | |
719 | bi->bi_rw, i, (unsigned long long)sh->sector); | |
720 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
721 | set_bit(STRIPE_HANDLE, &sh->state); | |
722 | } | |
723 | } | |
724 | } | |
725 | ||
726 | static struct dma_async_tx_descriptor * | |
727 | async_copy_data(int frombio, struct bio *bio, struct page *page, | |
728 | sector_t sector, struct dma_async_tx_descriptor *tx) | |
729 | { | |
730 | struct bio_vec *bvl; | |
731 | struct page *bio_page; | |
732 | int i; | |
733 | int page_offset; | |
a08abd8c | 734 | struct async_submit_ctl submit; |
0403e382 | 735 | enum async_tx_flags flags = 0; |
91c00924 DW |
736 | |
737 | if (bio->bi_sector >= sector) | |
738 | page_offset = (signed)(bio->bi_sector - sector) * 512; | |
739 | else | |
740 | page_offset = (signed)(sector - bio->bi_sector) * -512; | |
a08abd8c | 741 | |
0403e382 DW |
742 | if (frombio) |
743 | flags |= ASYNC_TX_FENCE; | |
744 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); | |
745 | ||
91c00924 | 746 | bio_for_each_segment(bvl, bio, i) { |
fcde9075 | 747 | int len = bvl->bv_len; |
91c00924 DW |
748 | int clen; |
749 | int b_offset = 0; | |
750 | ||
751 | if (page_offset < 0) { | |
752 | b_offset = -page_offset; | |
753 | page_offset += b_offset; | |
754 | len -= b_offset; | |
755 | } | |
756 | ||
757 | if (len > 0 && page_offset + len > STRIPE_SIZE) | |
758 | clen = STRIPE_SIZE - page_offset; | |
759 | else | |
760 | clen = len; | |
761 | ||
762 | if (clen > 0) { | |
fcde9075 NK |
763 | b_offset += bvl->bv_offset; |
764 | bio_page = bvl->bv_page; | |
91c00924 DW |
765 | if (frombio) |
766 | tx = async_memcpy(page, bio_page, page_offset, | |
a08abd8c | 767 | b_offset, clen, &submit); |
91c00924 DW |
768 | else |
769 | tx = async_memcpy(bio_page, page, b_offset, | |
a08abd8c | 770 | page_offset, clen, &submit); |
91c00924 | 771 | } |
a08abd8c DW |
772 | /* chain the operations */ |
773 | submit.depend_tx = tx; | |
774 | ||
91c00924 DW |
775 | if (clen < len) /* hit end of page */ |
776 | break; | |
777 | page_offset += len; | |
778 | } | |
779 | ||
780 | return tx; | |
781 | } | |
782 | ||
783 | static void ops_complete_biofill(void *stripe_head_ref) | |
784 | { | |
785 | struct stripe_head *sh = stripe_head_ref; | |
786 | struct bio *return_bi = NULL; | |
e4d84909 | 787 | int i; |
91c00924 | 788 | |
e46b272b | 789 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
790 | (unsigned long long)sh->sector); |
791 | ||
792 | /* clear completed biofills */ | |
793 | for (i = sh->disks; i--; ) { | |
794 | struct r5dev *dev = &sh->dev[i]; | |
91c00924 DW |
795 | |
796 | /* acknowledge completion of a biofill operation */ | |
e4d84909 DW |
797 | /* and check if we need to reply to a read request, |
798 | * new R5_Wantfill requests are held off until | |
83de75cc | 799 | * !STRIPE_BIOFILL_RUN |
e4d84909 DW |
800 | */ |
801 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { | |
91c00924 | 802 | struct bio *rbi, *rbi2; |
91c00924 | 803 | |
91c00924 DW |
804 | BUG_ON(!dev->read); |
805 | rbi = dev->read; | |
806 | dev->read = NULL; | |
807 | while (rbi && rbi->bi_sector < | |
808 | dev->sector + STRIPE_SECTORS) { | |
809 | rbi2 = r5_next_bio(rbi, dev->sector); | |
e7836bd6 | 810 | if (!raid5_dec_bi_active_stripes(rbi)) { |
91c00924 DW |
811 | rbi->bi_next = return_bi; |
812 | return_bi = rbi; | |
813 | } | |
91c00924 DW |
814 | rbi = rbi2; |
815 | } | |
816 | } | |
817 | } | |
83de75cc | 818 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
91c00924 DW |
819 | |
820 | return_io(return_bi); | |
821 | ||
e4d84909 | 822 | set_bit(STRIPE_HANDLE, &sh->state); |
91c00924 DW |
823 | release_stripe(sh); |
824 | } | |
825 | ||
826 | static void ops_run_biofill(struct stripe_head *sh) | |
827 | { | |
828 | struct dma_async_tx_descriptor *tx = NULL; | |
a08abd8c | 829 | struct async_submit_ctl submit; |
91c00924 DW |
830 | int i; |
831 | ||
e46b272b | 832 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
833 | (unsigned long long)sh->sector); |
834 | ||
835 | for (i = sh->disks; i--; ) { | |
836 | struct r5dev *dev = &sh->dev[i]; | |
837 | if (test_bit(R5_Wantfill, &dev->flags)) { | |
838 | struct bio *rbi; | |
b17459c0 | 839 | spin_lock_irq(&sh->stripe_lock); |
91c00924 DW |
840 | dev->read = rbi = dev->toread; |
841 | dev->toread = NULL; | |
b17459c0 | 842 | spin_unlock_irq(&sh->stripe_lock); |
91c00924 DW |
843 | while (rbi && rbi->bi_sector < |
844 | dev->sector + STRIPE_SECTORS) { | |
845 | tx = async_copy_data(0, rbi, dev->page, | |
846 | dev->sector, tx); | |
847 | rbi = r5_next_bio(rbi, dev->sector); | |
848 | } | |
849 | } | |
850 | } | |
851 | ||
852 | atomic_inc(&sh->count); | |
a08abd8c DW |
853 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
854 | async_trigger_callback(&submit); | |
91c00924 DW |
855 | } |
856 | ||
4e7d2c0a | 857 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
91c00924 | 858 | { |
4e7d2c0a | 859 | struct r5dev *tgt; |
91c00924 | 860 | |
4e7d2c0a DW |
861 | if (target < 0) |
862 | return; | |
91c00924 | 863 | |
4e7d2c0a | 864 | tgt = &sh->dev[target]; |
91c00924 DW |
865 | set_bit(R5_UPTODATE, &tgt->flags); |
866 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
867 | clear_bit(R5_Wantcompute, &tgt->flags); | |
4e7d2c0a DW |
868 | } |
869 | ||
ac6b53b6 | 870 | static void ops_complete_compute(void *stripe_head_ref) |
91c00924 DW |
871 | { |
872 | struct stripe_head *sh = stripe_head_ref; | |
91c00924 | 873 | |
e46b272b | 874 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
875 | (unsigned long long)sh->sector); |
876 | ||
ac6b53b6 | 877 | /* mark the computed target(s) as uptodate */ |
4e7d2c0a | 878 | mark_target_uptodate(sh, sh->ops.target); |
ac6b53b6 | 879 | mark_target_uptodate(sh, sh->ops.target2); |
4e7d2c0a | 880 | |
ecc65c9b DW |
881 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
882 | if (sh->check_state == check_state_compute_run) | |
883 | sh->check_state = check_state_compute_result; | |
91c00924 DW |
884 | set_bit(STRIPE_HANDLE, &sh->state); |
885 | release_stripe(sh); | |
886 | } | |
887 | ||
d6f38f31 DW |
888 | /* return a pointer to the address conversion region of the scribble buffer */ |
889 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, | |
890 | struct raid5_percpu *percpu) | |
891 | { | |
892 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); | |
893 | } | |
894 | ||
895 | static struct dma_async_tx_descriptor * | |
896 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) | |
91c00924 | 897 | { |
91c00924 | 898 | int disks = sh->disks; |
d6f38f31 | 899 | struct page **xor_srcs = percpu->scribble; |
91c00924 DW |
900 | int target = sh->ops.target; |
901 | struct r5dev *tgt = &sh->dev[target]; | |
902 | struct page *xor_dest = tgt->page; | |
903 | int count = 0; | |
904 | struct dma_async_tx_descriptor *tx; | |
a08abd8c | 905 | struct async_submit_ctl submit; |
91c00924 DW |
906 | int i; |
907 | ||
908 | pr_debug("%s: stripe %llu block: %d\n", | |
e46b272b | 909 | __func__, (unsigned long long)sh->sector, target); |
91c00924 DW |
910 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
911 | ||
912 | for (i = disks; i--; ) | |
913 | if (i != target) | |
914 | xor_srcs[count++] = sh->dev[i].page; | |
915 | ||
916 | atomic_inc(&sh->count); | |
917 | ||
0403e382 | 918 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, |
ac6b53b6 | 919 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
91c00924 | 920 | if (unlikely(count == 1)) |
a08abd8c | 921 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
91c00924 | 922 | else |
a08abd8c | 923 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
91c00924 | 924 | |
91c00924 DW |
925 | return tx; |
926 | } | |
927 | ||
ac6b53b6 DW |
928 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
929 | * @srcs - (struct page *) array of size sh->disks | |
930 | * @sh - stripe_head to parse | |
931 | * | |
932 | * Populates srcs in proper layout order for the stripe and returns the | |
933 | * 'count' of sources to be used in a call to async_gen_syndrome. The P | |
934 | * destination buffer is recorded in srcs[count] and the Q destination | |
935 | * is recorded in srcs[count+1]]. | |
936 | */ | |
937 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) | |
938 | { | |
939 | int disks = sh->disks; | |
940 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); | |
941 | int d0_idx = raid6_d0(sh); | |
942 | int count; | |
943 | int i; | |
944 | ||
945 | for (i = 0; i < disks; i++) | |
5dd33c9a | 946 | srcs[i] = NULL; |
ac6b53b6 DW |
947 | |
948 | count = 0; | |
949 | i = d0_idx; | |
950 | do { | |
951 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); | |
952 | ||
953 | srcs[slot] = sh->dev[i].page; | |
954 | i = raid6_next_disk(i, disks); | |
955 | } while (i != d0_idx); | |
ac6b53b6 | 956 | |
e4424fee | 957 | return syndrome_disks; |
ac6b53b6 DW |
958 | } |
959 | ||
960 | static struct dma_async_tx_descriptor * | |
961 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) | |
962 | { | |
963 | int disks = sh->disks; | |
964 | struct page **blocks = percpu->scribble; | |
965 | int target; | |
966 | int qd_idx = sh->qd_idx; | |
967 | struct dma_async_tx_descriptor *tx; | |
968 | struct async_submit_ctl submit; | |
969 | struct r5dev *tgt; | |
970 | struct page *dest; | |
971 | int i; | |
972 | int count; | |
973 | ||
974 | if (sh->ops.target < 0) | |
975 | target = sh->ops.target2; | |
976 | else if (sh->ops.target2 < 0) | |
977 | target = sh->ops.target; | |
91c00924 | 978 | else |
ac6b53b6 DW |
979 | /* we should only have one valid target */ |
980 | BUG(); | |
981 | BUG_ON(target < 0); | |
982 | pr_debug("%s: stripe %llu block: %d\n", | |
983 | __func__, (unsigned long long)sh->sector, target); | |
984 | ||
985 | tgt = &sh->dev[target]; | |
986 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
987 | dest = tgt->page; | |
988 | ||
989 | atomic_inc(&sh->count); | |
990 | ||
991 | if (target == qd_idx) { | |
992 | count = set_syndrome_sources(blocks, sh); | |
993 | blocks[count] = NULL; /* regenerating p is not necessary */ | |
994 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ | |
0403e382 DW |
995 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
996 | ops_complete_compute, sh, | |
ac6b53b6 DW |
997 | to_addr_conv(sh, percpu)); |
998 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); | |
999 | } else { | |
1000 | /* Compute any data- or p-drive using XOR */ | |
1001 | count = 0; | |
1002 | for (i = disks; i-- ; ) { | |
1003 | if (i == target || i == qd_idx) | |
1004 | continue; | |
1005 | blocks[count++] = sh->dev[i].page; | |
1006 | } | |
1007 | ||
0403e382 DW |
1008 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
1009 | NULL, ops_complete_compute, sh, | |
ac6b53b6 DW |
1010 | to_addr_conv(sh, percpu)); |
1011 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); | |
1012 | } | |
91c00924 | 1013 | |
91c00924 DW |
1014 | return tx; |
1015 | } | |
1016 | ||
ac6b53b6 DW |
1017 | static struct dma_async_tx_descriptor * |
1018 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) | |
1019 | { | |
1020 | int i, count, disks = sh->disks; | |
1021 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; | |
1022 | int d0_idx = raid6_d0(sh); | |
1023 | int faila = -1, failb = -1; | |
1024 | int target = sh->ops.target; | |
1025 | int target2 = sh->ops.target2; | |
1026 | struct r5dev *tgt = &sh->dev[target]; | |
1027 | struct r5dev *tgt2 = &sh->dev[target2]; | |
1028 | struct dma_async_tx_descriptor *tx; | |
1029 | struct page **blocks = percpu->scribble; | |
1030 | struct async_submit_ctl submit; | |
1031 | ||
1032 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", | |
1033 | __func__, (unsigned long long)sh->sector, target, target2); | |
1034 | BUG_ON(target < 0 || target2 < 0); | |
1035 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
1036 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); | |
1037 | ||
6c910a78 | 1038 | /* we need to open-code set_syndrome_sources to handle the |
ac6b53b6 DW |
1039 | * slot number conversion for 'faila' and 'failb' |
1040 | */ | |
1041 | for (i = 0; i < disks ; i++) | |
5dd33c9a | 1042 | blocks[i] = NULL; |
ac6b53b6 DW |
1043 | count = 0; |
1044 | i = d0_idx; | |
1045 | do { | |
1046 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); | |
1047 | ||
1048 | blocks[slot] = sh->dev[i].page; | |
1049 | ||
1050 | if (i == target) | |
1051 | faila = slot; | |
1052 | if (i == target2) | |
1053 | failb = slot; | |
1054 | i = raid6_next_disk(i, disks); | |
1055 | } while (i != d0_idx); | |
ac6b53b6 DW |
1056 | |
1057 | BUG_ON(faila == failb); | |
1058 | if (failb < faila) | |
1059 | swap(faila, failb); | |
1060 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", | |
1061 | __func__, (unsigned long long)sh->sector, faila, failb); | |
1062 | ||
1063 | atomic_inc(&sh->count); | |
1064 | ||
1065 | if (failb == syndrome_disks+1) { | |
1066 | /* Q disk is one of the missing disks */ | |
1067 | if (faila == syndrome_disks) { | |
1068 | /* Missing P+Q, just recompute */ | |
0403e382 DW |
1069 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1070 | ops_complete_compute, sh, | |
1071 | to_addr_conv(sh, percpu)); | |
e4424fee | 1072 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
ac6b53b6 DW |
1073 | STRIPE_SIZE, &submit); |
1074 | } else { | |
1075 | struct page *dest; | |
1076 | int data_target; | |
1077 | int qd_idx = sh->qd_idx; | |
1078 | ||
1079 | /* Missing D+Q: recompute D from P, then recompute Q */ | |
1080 | if (target == qd_idx) | |
1081 | data_target = target2; | |
1082 | else | |
1083 | data_target = target; | |
1084 | ||
1085 | count = 0; | |
1086 | for (i = disks; i-- ; ) { | |
1087 | if (i == data_target || i == qd_idx) | |
1088 | continue; | |
1089 | blocks[count++] = sh->dev[i].page; | |
1090 | } | |
1091 | dest = sh->dev[data_target].page; | |
0403e382 DW |
1092 | init_async_submit(&submit, |
1093 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, | |
1094 | NULL, NULL, NULL, | |
1095 | to_addr_conv(sh, percpu)); | |
ac6b53b6 DW |
1096 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
1097 | &submit); | |
1098 | ||
1099 | count = set_syndrome_sources(blocks, sh); | |
0403e382 DW |
1100 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
1101 | ops_complete_compute, sh, | |
1102 | to_addr_conv(sh, percpu)); | |
ac6b53b6 DW |
1103 | return async_gen_syndrome(blocks, 0, count+2, |
1104 | STRIPE_SIZE, &submit); | |
1105 | } | |
ac6b53b6 | 1106 | } else { |
6c910a78 DW |
1107 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1108 | ops_complete_compute, sh, | |
1109 | to_addr_conv(sh, percpu)); | |
1110 | if (failb == syndrome_disks) { | |
1111 | /* We're missing D+P. */ | |
1112 | return async_raid6_datap_recov(syndrome_disks+2, | |
1113 | STRIPE_SIZE, faila, | |
1114 | blocks, &submit); | |
1115 | } else { | |
1116 | /* We're missing D+D. */ | |
1117 | return async_raid6_2data_recov(syndrome_disks+2, | |
1118 | STRIPE_SIZE, faila, failb, | |
1119 | blocks, &submit); | |
1120 | } | |
ac6b53b6 DW |
1121 | } |
1122 | } | |
1123 | ||
1124 | ||
91c00924 DW |
1125 | static void ops_complete_prexor(void *stripe_head_ref) |
1126 | { | |
1127 | struct stripe_head *sh = stripe_head_ref; | |
1128 | ||
e46b272b | 1129 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 | 1130 | (unsigned long long)sh->sector); |
91c00924 DW |
1131 | } |
1132 | ||
1133 | static struct dma_async_tx_descriptor * | |
d6f38f31 DW |
1134 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
1135 | struct dma_async_tx_descriptor *tx) | |
91c00924 | 1136 | { |
91c00924 | 1137 | int disks = sh->disks; |
d6f38f31 | 1138 | struct page **xor_srcs = percpu->scribble; |
91c00924 | 1139 | int count = 0, pd_idx = sh->pd_idx, i; |
a08abd8c | 1140 | struct async_submit_ctl submit; |
91c00924 DW |
1141 | |
1142 | /* existing parity data subtracted */ | |
1143 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; | |
1144 | ||
e46b272b | 1145 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1146 | (unsigned long long)sh->sector); |
1147 | ||
1148 | for (i = disks; i--; ) { | |
1149 | struct r5dev *dev = &sh->dev[i]; | |
1150 | /* Only process blocks that are known to be uptodate */ | |
d8ee0728 | 1151 | if (test_bit(R5_Wantdrain, &dev->flags)) |
91c00924 DW |
1152 | xor_srcs[count++] = dev->page; |
1153 | } | |
1154 | ||
0403e382 | 1155 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, |
d6f38f31 | 1156 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
a08abd8c | 1157 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
91c00924 DW |
1158 | |
1159 | return tx; | |
1160 | } | |
1161 | ||
1162 | static struct dma_async_tx_descriptor * | |
d8ee0728 | 1163 | ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) |
91c00924 DW |
1164 | { |
1165 | int disks = sh->disks; | |
d8ee0728 | 1166 | int i; |
91c00924 | 1167 | |
e46b272b | 1168 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1169 | (unsigned long long)sh->sector); |
1170 | ||
1171 | for (i = disks; i--; ) { | |
1172 | struct r5dev *dev = &sh->dev[i]; | |
1173 | struct bio *chosen; | |
91c00924 | 1174 | |
d8ee0728 | 1175 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
91c00924 DW |
1176 | struct bio *wbi; |
1177 | ||
b17459c0 | 1178 | spin_lock_irq(&sh->stripe_lock); |
91c00924 DW |
1179 | chosen = dev->towrite; |
1180 | dev->towrite = NULL; | |
1181 | BUG_ON(dev->written); | |
1182 | wbi = dev->written = chosen; | |
b17459c0 | 1183 | spin_unlock_irq(&sh->stripe_lock); |
91c00924 DW |
1184 | |
1185 | while (wbi && wbi->bi_sector < | |
1186 | dev->sector + STRIPE_SECTORS) { | |
e9c7469b TH |
1187 | if (wbi->bi_rw & REQ_FUA) |
1188 | set_bit(R5_WantFUA, &dev->flags); | |
bc0934f0 SL |
1189 | if (wbi->bi_rw & REQ_SYNC) |
1190 | set_bit(R5_SyncIO, &dev->flags); | |
9e444768 | 1191 | if (wbi->bi_rw & REQ_DISCARD) |
620125f2 | 1192 | set_bit(R5_Discard, &dev->flags); |
9e444768 | 1193 | else |
620125f2 SL |
1194 | tx = async_copy_data(1, wbi, dev->page, |
1195 | dev->sector, tx); | |
91c00924 DW |
1196 | wbi = r5_next_bio(wbi, dev->sector); |
1197 | } | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | return tx; | |
1202 | } | |
1203 | ||
ac6b53b6 | 1204 | static void ops_complete_reconstruct(void *stripe_head_ref) |
91c00924 DW |
1205 | { |
1206 | struct stripe_head *sh = stripe_head_ref; | |
ac6b53b6 DW |
1207 | int disks = sh->disks; |
1208 | int pd_idx = sh->pd_idx; | |
1209 | int qd_idx = sh->qd_idx; | |
1210 | int i; | |
9e444768 | 1211 | bool fua = false, sync = false, discard = false; |
91c00924 | 1212 | |
e46b272b | 1213 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1214 | (unsigned long long)sh->sector); |
1215 | ||
bc0934f0 | 1216 | for (i = disks; i--; ) { |
e9c7469b | 1217 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
bc0934f0 | 1218 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
9e444768 | 1219 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
bc0934f0 | 1220 | } |
e9c7469b | 1221 | |
91c00924 DW |
1222 | for (i = disks; i--; ) { |
1223 | struct r5dev *dev = &sh->dev[i]; | |
ac6b53b6 | 1224 | |
e9c7469b | 1225 | if (dev->written || i == pd_idx || i == qd_idx) { |
9e444768 SL |
1226 | if (!discard) |
1227 | set_bit(R5_UPTODATE, &dev->flags); | |
e9c7469b TH |
1228 | if (fua) |
1229 | set_bit(R5_WantFUA, &dev->flags); | |
bc0934f0 SL |
1230 | if (sync) |
1231 | set_bit(R5_SyncIO, &dev->flags); | |
e9c7469b | 1232 | } |
91c00924 DW |
1233 | } |
1234 | ||
d8ee0728 DW |
1235 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
1236 | sh->reconstruct_state = reconstruct_state_drain_result; | |
1237 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) | |
1238 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; | |
1239 | else { | |
1240 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); | |
1241 | sh->reconstruct_state = reconstruct_state_result; | |
1242 | } | |
91c00924 DW |
1243 | |
1244 | set_bit(STRIPE_HANDLE, &sh->state); | |
1245 | release_stripe(sh); | |
1246 | } | |
1247 | ||
1248 | static void | |
ac6b53b6 DW |
1249 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
1250 | struct dma_async_tx_descriptor *tx) | |
91c00924 | 1251 | { |
91c00924 | 1252 | int disks = sh->disks; |
d6f38f31 | 1253 | struct page **xor_srcs = percpu->scribble; |
a08abd8c | 1254 | struct async_submit_ctl submit; |
91c00924 DW |
1255 | int count = 0, pd_idx = sh->pd_idx, i; |
1256 | struct page *xor_dest; | |
d8ee0728 | 1257 | int prexor = 0; |
91c00924 | 1258 | unsigned long flags; |
91c00924 | 1259 | |
e46b272b | 1260 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1261 | (unsigned long long)sh->sector); |
1262 | ||
620125f2 SL |
1263 | for (i = 0; i < sh->disks; i++) { |
1264 | if (pd_idx == i) | |
1265 | continue; | |
1266 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) | |
1267 | break; | |
1268 | } | |
1269 | if (i >= sh->disks) { | |
1270 | atomic_inc(&sh->count); | |
620125f2 SL |
1271 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
1272 | ops_complete_reconstruct(sh); | |
1273 | return; | |
1274 | } | |
91c00924 DW |
1275 | /* check if prexor is active which means only process blocks |
1276 | * that are part of a read-modify-write (written) | |
1277 | */ | |
d8ee0728 DW |
1278 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
1279 | prexor = 1; | |
91c00924 DW |
1280 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
1281 | for (i = disks; i--; ) { | |
1282 | struct r5dev *dev = &sh->dev[i]; | |
1283 | if (dev->written) | |
1284 | xor_srcs[count++] = dev->page; | |
1285 | } | |
1286 | } else { | |
1287 | xor_dest = sh->dev[pd_idx].page; | |
1288 | for (i = disks; i--; ) { | |
1289 | struct r5dev *dev = &sh->dev[i]; | |
1290 | if (i != pd_idx) | |
1291 | xor_srcs[count++] = dev->page; | |
1292 | } | |
1293 | } | |
1294 | ||
91c00924 DW |
1295 | /* 1/ if we prexor'd then the dest is reused as a source |
1296 | * 2/ if we did not prexor then we are redoing the parity | |
1297 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST | |
1298 | * for the synchronous xor case | |
1299 | */ | |
88ba2aa5 | 1300 | flags = ASYNC_TX_ACK | |
91c00924 DW |
1301 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
1302 | ||
1303 | atomic_inc(&sh->count); | |
1304 | ||
ac6b53b6 | 1305 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
d6f38f31 | 1306 | to_addr_conv(sh, percpu)); |
a08abd8c DW |
1307 | if (unlikely(count == 1)) |
1308 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); | |
1309 | else | |
1310 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); | |
91c00924 DW |
1311 | } |
1312 | ||
ac6b53b6 DW |
1313 | static void |
1314 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, | |
1315 | struct dma_async_tx_descriptor *tx) | |
1316 | { | |
1317 | struct async_submit_ctl submit; | |
1318 | struct page **blocks = percpu->scribble; | |
620125f2 | 1319 | int count, i; |
ac6b53b6 DW |
1320 | |
1321 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); | |
1322 | ||
620125f2 SL |
1323 | for (i = 0; i < sh->disks; i++) { |
1324 | if (sh->pd_idx == i || sh->qd_idx == i) | |
1325 | continue; | |
1326 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) | |
1327 | break; | |
1328 | } | |
1329 | if (i >= sh->disks) { | |
1330 | atomic_inc(&sh->count); | |
620125f2 SL |
1331 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
1332 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); | |
1333 | ops_complete_reconstruct(sh); | |
1334 | return; | |
1335 | } | |
1336 | ||
ac6b53b6 DW |
1337 | count = set_syndrome_sources(blocks, sh); |
1338 | ||
1339 | atomic_inc(&sh->count); | |
1340 | ||
1341 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, | |
1342 | sh, to_addr_conv(sh, percpu)); | |
1343 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); | |
91c00924 DW |
1344 | } |
1345 | ||
1346 | static void ops_complete_check(void *stripe_head_ref) | |
1347 | { | |
1348 | struct stripe_head *sh = stripe_head_ref; | |
91c00924 | 1349 | |
e46b272b | 1350 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1351 | (unsigned long long)sh->sector); |
1352 | ||
ecc65c9b | 1353 | sh->check_state = check_state_check_result; |
91c00924 DW |
1354 | set_bit(STRIPE_HANDLE, &sh->state); |
1355 | release_stripe(sh); | |
1356 | } | |
1357 | ||
ac6b53b6 | 1358 | static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu) |
91c00924 | 1359 | { |
91c00924 | 1360 | int disks = sh->disks; |
ac6b53b6 DW |
1361 | int pd_idx = sh->pd_idx; |
1362 | int qd_idx = sh->qd_idx; | |
1363 | struct page *xor_dest; | |
d6f38f31 | 1364 | struct page **xor_srcs = percpu->scribble; |
91c00924 | 1365 | struct dma_async_tx_descriptor *tx; |
a08abd8c | 1366 | struct async_submit_ctl submit; |
ac6b53b6 DW |
1367 | int count; |
1368 | int i; | |
91c00924 | 1369 | |
e46b272b | 1370 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1371 | (unsigned long long)sh->sector); |
1372 | ||
ac6b53b6 DW |
1373 | count = 0; |
1374 | xor_dest = sh->dev[pd_idx].page; | |
1375 | xor_srcs[count++] = xor_dest; | |
91c00924 | 1376 | for (i = disks; i--; ) { |
ac6b53b6 DW |
1377 | if (i == pd_idx || i == qd_idx) |
1378 | continue; | |
1379 | xor_srcs[count++] = sh->dev[i].page; | |
91c00924 DW |
1380 | } |
1381 | ||
d6f38f31 DW |
1382 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
1383 | to_addr_conv(sh, percpu)); | |
099f53cb | 1384 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
a08abd8c | 1385 | &sh->ops.zero_sum_result, &submit); |
91c00924 | 1386 | |
91c00924 | 1387 | atomic_inc(&sh->count); |
a08abd8c DW |
1388 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
1389 | tx = async_trigger_callback(&submit); | |
91c00924 DW |
1390 | } |
1391 | ||
ac6b53b6 DW |
1392 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
1393 | { | |
1394 | struct page **srcs = percpu->scribble; | |
1395 | struct async_submit_ctl submit; | |
1396 | int count; | |
1397 | ||
1398 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, | |
1399 | (unsigned long long)sh->sector, checkp); | |
1400 | ||
1401 | count = set_syndrome_sources(srcs, sh); | |
1402 | if (!checkp) | |
1403 | srcs[count] = NULL; | |
91c00924 | 1404 | |
91c00924 | 1405 | atomic_inc(&sh->count); |
ac6b53b6 DW |
1406 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
1407 | sh, to_addr_conv(sh, percpu)); | |
1408 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, | |
1409 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); | |
91c00924 DW |
1410 | } |
1411 | ||
51acbcec | 1412 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
91c00924 DW |
1413 | { |
1414 | int overlap_clear = 0, i, disks = sh->disks; | |
1415 | struct dma_async_tx_descriptor *tx = NULL; | |
d1688a6d | 1416 | struct r5conf *conf = sh->raid_conf; |
ac6b53b6 | 1417 | int level = conf->level; |
d6f38f31 DW |
1418 | struct raid5_percpu *percpu; |
1419 | unsigned long cpu; | |
91c00924 | 1420 | |
d6f38f31 DW |
1421 | cpu = get_cpu(); |
1422 | percpu = per_cpu_ptr(conf->percpu, cpu); | |
83de75cc | 1423 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
91c00924 DW |
1424 | ops_run_biofill(sh); |
1425 | overlap_clear++; | |
1426 | } | |
1427 | ||
7b3a871e | 1428 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
ac6b53b6 DW |
1429 | if (level < 6) |
1430 | tx = ops_run_compute5(sh, percpu); | |
1431 | else { | |
1432 | if (sh->ops.target2 < 0 || sh->ops.target < 0) | |
1433 | tx = ops_run_compute6_1(sh, percpu); | |
1434 | else | |
1435 | tx = ops_run_compute6_2(sh, percpu); | |
1436 | } | |
1437 | /* terminate the chain if reconstruct is not set to be run */ | |
1438 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) | |
7b3a871e DW |
1439 | async_tx_ack(tx); |
1440 | } | |
91c00924 | 1441 | |
600aa109 | 1442 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
d6f38f31 | 1443 | tx = ops_run_prexor(sh, percpu, tx); |
91c00924 | 1444 | |
600aa109 | 1445 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
d8ee0728 | 1446 | tx = ops_run_biodrain(sh, tx); |
91c00924 DW |
1447 | overlap_clear++; |
1448 | } | |
1449 | ||
ac6b53b6 DW |
1450 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
1451 | if (level < 6) | |
1452 | ops_run_reconstruct5(sh, percpu, tx); | |
1453 | else | |
1454 | ops_run_reconstruct6(sh, percpu, tx); | |
1455 | } | |
91c00924 | 1456 | |
ac6b53b6 DW |
1457 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
1458 | if (sh->check_state == check_state_run) | |
1459 | ops_run_check_p(sh, percpu); | |
1460 | else if (sh->check_state == check_state_run_q) | |
1461 | ops_run_check_pq(sh, percpu, 0); | |
1462 | else if (sh->check_state == check_state_run_pq) | |
1463 | ops_run_check_pq(sh, percpu, 1); | |
1464 | else | |
1465 | BUG(); | |
1466 | } | |
91c00924 | 1467 | |
91c00924 DW |
1468 | if (overlap_clear) |
1469 | for (i = disks; i--; ) { | |
1470 | struct r5dev *dev = &sh->dev[i]; | |
1471 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) | |
1472 | wake_up(&sh->raid_conf->wait_for_overlap); | |
1473 | } | |
d6f38f31 | 1474 | put_cpu(); |
91c00924 DW |
1475 | } |
1476 | ||
d1688a6d | 1477 | static int grow_one_stripe(struct r5conf *conf) |
1da177e4 LT |
1478 | { |
1479 | struct stripe_head *sh; | |
6ce32846 | 1480 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
3f294f4f N |
1481 | if (!sh) |
1482 | return 0; | |
6ce32846 | 1483 | |
3f294f4f | 1484 | sh->raid_conf = conf; |
3f294f4f | 1485 | |
b17459c0 SL |
1486 | spin_lock_init(&sh->stripe_lock); |
1487 | ||
e4e11e38 N |
1488 | if (grow_buffers(sh)) { |
1489 | shrink_buffers(sh); | |
3f294f4f N |
1490 | kmem_cache_free(conf->slab_cache, sh); |
1491 | return 0; | |
1492 | } | |
1493 | /* we just created an active stripe so... */ | |
1494 | atomic_set(&sh->count, 1); | |
1495 | atomic_inc(&conf->active_stripes); | |
1496 | INIT_LIST_HEAD(&sh->lru); | |
1497 | release_stripe(sh); | |
1498 | return 1; | |
1499 | } | |
1500 | ||
d1688a6d | 1501 | static int grow_stripes(struct r5conf *conf, int num) |
3f294f4f | 1502 | { |
e18b890b | 1503 | struct kmem_cache *sc; |
5e5e3e78 | 1504 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
1da177e4 | 1505 | |
f4be6b43 N |
1506 | if (conf->mddev->gendisk) |
1507 | sprintf(conf->cache_name[0], | |
1508 | "raid%d-%s", conf->level, mdname(conf->mddev)); | |
1509 | else | |
1510 | sprintf(conf->cache_name[0], | |
1511 | "raid%d-%p", conf->level, conf->mddev); | |
1512 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); | |
1513 | ||
ad01c9e3 N |
1514 | conf->active_name = 0; |
1515 | sc = kmem_cache_create(conf->cache_name[conf->active_name], | |
1da177e4 | 1516 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
20c2df83 | 1517 | 0, 0, NULL); |
1da177e4 LT |
1518 | if (!sc) |
1519 | return 1; | |
1520 | conf->slab_cache = sc; | |
ad01c9e3 | 1521 | conf->pool_size = devs; |
16a53ecc | 1522 | while (num--) |
3f294f4f | 1523 | if (!grow_one_stripe(conf)) |
1da177e4 | 1524 | return 1; |
1da177e4 LT |
1525 | return 0; |
1526 | } | |
29269553 | 1527 | |
d6f38f31 DW |
1528 | /** |
1529 | * scribble_len - return the required size of the scribble region | |
1530 | * @num - total number of disks in the array | |
1531 | * | |
1532 | * The size must be enough to contain: | |
1533 | * 1/ a struct page pointer for each device in the array +2 | |
1534 | * 2/ room to convert each entry in (1) to its corresponding dma | |
1535 | * (dma_map_page()) or page (page_address()) address. | |
1536 | * | |
1537 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we | |
1538 | * calculate over all devices (not just the data blocks), using zeros in place | |
1539 | * of the P and Q blocks. | |
1540 | */ | |
1541 | static size_t scribble_len(int num) | |
1542 | { | |
1543 | size_t len; | |
1544 | ||
1545 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); | |
1546 | ||
1547 | return len; | |
1548 | } | |
1549 | ||
d1688a6d | 1550 | static int resize_stripes(struct r5conf *conf, int newsize) |
ad01c9e3 N |
1551 | { |
1552 | /* Make all the stripes able to hold 'newsize' devices. | |
1553 | * New slots in each stripe get 'page' set to a new page. | |
1554 | * | |
1555 | * This happens in stages: | |
1556 | * 1/ create a new kmem_cache and allocate the required number of | |
1557 | * stripe_heads. | |
83f0d77a | 1558 | * 2/ gather all the old stripe_heads and transfer the pages across |
ad01c9e3 N |
1559 | * to the new stripe_heads. This will have the side effect of |
1560 | * freezing the array as once all stripe_heads have been collected, | |
1561 | * no IO will be possible. Old stripe heads are freed once their | |
1562 | * pages have been transferred over, and the old kmem_cache is | |
1563 | * freed when all stripes are done. | |
1564 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, | |
1565 | * we simple return a failre status - no need to clean anything up. | |
1566 | * 4/ allocate new pages for the new slots in the new stripe_heads. | |
1567 | * If this fails, we don't bother trying the shrink the | |
1568 | * stripe_heads down again, we just leave them as they are. | |
1569 | * As each stripe_head is processed the new one is released into | |
1570 | * active service. | |
1571 | * | |
1572 | * Once step2 is started, we cannot afford to wait for a write, | |
1573 | * so we use GFP_NOIO allocations. | |
1574 | */ | |
1575 | struct stripe_head *osh, *nsh; | |
1576 | LIST_HEAD(newstripes); | |
1577 | struct disk_info *ndisks; | |
d6f38f31 | 1578 | unsigned long cpu; |
b5470dc5 | 1579 | int err; |
e18b890b | 1580 | struct kmem_cache *sc; |
ad01c9e3 N |
1581 | int i; |
1582 | ||
1583 | if (newsize <= conf->pool_size) | |
1584 | return 0; /* never bother to shrink */ | |
1585 | ||
b5470dc5 DW |
1586 | err = md_allow_write(conf->mddev); |
1587 | if (err) | |
1588 | return err; | |
2a2275d6 | 1589 | |
ad01c9e3 N |
1590 | /* Step 1 */ |
1591 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], | |
1592 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), | |
20c2df83 | 1593 | 0, 0, NULL); |
ad01c9e3 N |
1594 | if (!sc) |
1595 | return -ENOMEM; | |
1596 | ||
1597 | for (i = conf->max_nr_stripes; i; i--) { | |
6ce32846 | 1598 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
ad01c9e3 N |
1599 | if (!nsh) |
1600 | break; | |
1601 | ||
ad01c9e3 | 1602 | nsh->raid_conf = conf; |
cb13ff69 | 1603 | spin_lock_init(&nsh->stripe_lock); |
ad01c9e3 N |
1604 | |
1605 | list_add(&nsh->lru, &newstripes); | |
1606 | } | |
1607 | if (i) { | |
1608 | /* didn't get enough, give up */ | |
1609 | while (!list_empty(&newstripes)) { | |
1610 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
1611 | list_del(&nsh->lru); | |
1612 | kmem_cache_free(sc, nsh); | |
1613 | } | |
1614 | kmem_cache_destroy(sc); | |
1615 | return -ENOMEM; | |
1616 | } | |
1617 | /* Step 2 - Must use GFP_NOIO now. | |
1618 | * OK, we have enough stripes, start collecting inactive | |
1619 | * stripes and copying them over | |
1620 | */ | |
1621 | list_for_each_entry(nsh, &newstripes, lru) { | |
1622 | spin_lock_irq(&conf->device_lock); | |
1623 | wait_event_lock_irq(conf->wait_for_stripe, | |
1624 | !list_empty(&conf->inactive_list), | |
eed8c02e | 1625 | conf->device_lock); |
ad01c9e3 N |
1626 | osh = get_free_stripe(conf); |
1627 | spin_unlock_irq(&conf->device_lock); | |
1628 | atomic_set(&nsh->count, 1); | |
1629 | for(i=0; i<conf->pool_size; i++) | |
1630 | nsh->dev[i].page = osh->dev[i].page; | |
1631 | for( ; i<newsize; i++) | |
1632 | nsh->dev[i].page = NULL; | |
1633 | kmem_cache_free(conf->slab_cache, osh); | |
1634 | } | |
1635 | kmem_cache_destroy(conf->slab_cache); | |
1636 | ||
1637 | /* Step 3. | |
1638 | * At this point, we are holding all the stripes so the array | |
1639 | * is completely stalled, so now is a good time to resize | |
d6f38f31 | 1640 | * conf->disks and the scribble region |
ad01c9e3 N |
1641 | */ |
1642 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); | |
1643 | if (ndisks) { | |
1644 | for (i=0; i<conf->raid_disks; i++) | |
1645 | ndisks[i] = conf->disks[i]; | |
1646 | kfree(conf->disks); | |
1647 | conf->disks = ndisks; | |
1648 | } else | |
1649 | err = -ENOMEM; | |
1650 | ||
d6f38f31 DW |
1651 | get_online_cpus(); |
1652 | conf->scribble_len = scribble_len(newsize); | |
1653 | for_each_present_cpu(cpu) { | |
1654 | struct raid5_percpu *percpu; | |
1655 | void *scribble; | |
1656 | ||
1657 | percpu = per_cpu_ptr(conf->percpu, cpu); | |
1658 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); | |
1659 | ||
1660 | if (scribble) { | |
1661 | kfree(percpu->scribble); | |
1662 | percpu->scribble = scribble; | |
1663 | } else { | |
1664 | err = -ENOMEM; | |
1665 | break; | |
1666 | } | |
1667 | } | |
1668 | put_online_cpus(); | |
1669 | ||
ad01c9e3 N |
1670 | /* Step 4, return new stripes to service */ |
1671 | while(!list_empty(&newstripes)) { | |
1672 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
1673 | list_del_init(&nsh->lru); | |
d6f38f31 | 1674 | |
ad01c9e3 N |
1675 | for (i=conf->raid_disks; i < newsize; i++) |
1676 | if (nsh->dev[i].page == NULL) { | |
1677 | struct page *p = alloc_page(GFP_NOIO); | |
1678 | nsh->dev[i].page = p; | |
1679 | if (!p) | |
1680 | err = -ENOMEM; | |
1681 | } | |
1682 | release_stripe(nsh); | |
1683 | } | |
1684 | /* critical section pass, GFP_NOIO no longer needed */ | |
1685 | ||
1686 | conf->slab_cache = sc; | |
1687 | conf->active_name = 1-conf->active_name; | |
1688 | conf->pool_size = newsize; | |
1689 | return err; | |
1690 | } | |
1da177e4 | 1691 | |
d1688a6d | 1692 | static int drop_one_stripe(struct r5conf *conf) |
1da177e4 LT |
1693 | { |
1694 | struct stripe_head *sh; | |
1695 | ||
3f294f4f N |
1696 | spin_lock_irq(&conf->device_lock); |
1697 | sh = get_free_stripe(conf); | |
1698 | spin_unlock_irq(&conf->device_lock); | |
1699 | if (!sh) | |
1700 | return 0; | |
78bafebd | 1701 | BUG_ON(atomic_read(&sh->count)); |
e4e11e38 | 1702 | shrink_buffers(sh); |
3f294f4f N |
1703 | kmem_cache_free(conf->slab_cache, sh); |
1704 | atomic_dec(&conf->active_stripes); | |
1705 | return 1; | |
1706 | } | |
1707 | ||
d1688a6d | 1708 | static void shrink_stripes(struct r5conf *conf) |
3f294f4f N |
1709 | { |
1710 | while (drop_one_stripe(conf)) | |
1711 | ; | |
1712 | ||
29fc7e3e N |
1713 | if (conf->slab_cache) |
1714 | kmem_cache_destroy(conf->slab_cache); | |
1da177e4 LT |
1715 | conf->slab_cache = NULL; |
1716 | } | |
1717 | ||
6712ecf8 | 1718 | static void raid5_end_read_request(struct bio * bi, int error) |
1da177e4 | 1719 | { |
99c0fb5f | 1720 | struct stripe_head *sh = bi->bi_private; |
d1688a6d | 1721 | struct r5conf *conf = sh->raid_conf; |
7ecaa1e6 | 1722 | int disks = sh->disks, i; |
1da177e4 | 1723 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
d6950432 | 1724 | char b[BDEVNAME_SIZE]; |
dd054fce | 1725 | struct md_rdev *rdev = NULL; |
05616be5 | 1726 | sector_t s; |
1da177e4 LT |
1727 | |
1728 | for (i=0 ; i<disks; i++) | |
1729 | if (bi == &sh->dev[i].req) | |
1730 | break; | |
1731 | ||
45b4233c DW |
1732 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
1733 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), | |
1da177e4 LT |
1734 | uptodate); |
1735 | if (i == disks) { | |
1736 | BUG(); | |
6712ecf8 | 1737 | return; |
1da177e4 | 1738 | } |
14a75d3e | 1739 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
dd054fce N |
1740 | /* If replacement finished while this request was outstanding, |
1741 | * 'replacement' might be NULL already. | |
1742 | * In that case it moved down to 'rdev'. | |
1743 | * rdev is not removed until all requests are finished. | |
1744 | */ | |
14a75d3e | 1745 | rdev = conf->disks[i].replacement; |
dd054fce | 1746 | if (!rdev) |
14a75d3e | 1747 | rdev = conf->disks[i].rdev; |
1da177e4 | 1748 | |
05616be5 N |
1749 | if (use_new_offset(conf, sh)) |
1750 | s = sh->sector + rdev->new_data_offset; | |
1751 | else | |
1752 | s = sh->sector + rdev->data_offset; | |
1da177e4 | 1753 | if (uptodate) { |
1da177e4 | 1754 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
4e5314b5 | 1755 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
14a75d3e N |
1756 | /* Note that this cannot happen on a |
1757 | * replacement device. We just fail those on | |
1758 | * any error | |
1759 | */ | |
8bda470e CD |
1760 | printk_ratelimited( |
1761 | KERN_INFO | |
1762 | "md/raid:%s: read error corrected" | |
1763 | " (%lu sectors at %llu on %s)\n", | |
1764 | mdname(conf->mddev), STRIPE_SECTORS, | |
05616be5 | 1765 | (unsigned long long)s, |
8bda470e | 1766 | bdevname(rdev->bdev, b)); |
ddd5115f | 1767 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
4e5314b5 N |
1768 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
1769 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
3f9e7c14 | 1770 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
1771 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
1772 | ||
14a75d3e N |
1773 | if (atomic_read(&rdev->read_errors)) |
1774 | atomic_set(&rdev->read_errors, 0); | |
1da177e4 | 1775 | } else { |
14a75d3e | 1776 | const char *bdn = bdevname(rdev->bdev, b); |
ba22dcbf | 1777 | int retry = 0; |
2e8ac303 | 1778 | int set_bad = 0; |
d6950432 | 1779 | |
1da177e4 | 1780 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
d6950432 | 1781 | atomic_inc(&rdev->read_errors); |
14a75d3e N |
1782 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
1783 | printk_ratelimited( | |
1784 | KERN_WARNING | |
1785 | "md/raid:%s: read error on replacement device " | |
1786 | "(sector %llu on %s).\n", | |
1787 | mdname(conf->mddev), | |
05616be5 | 1788 | (unsigned long long)s, |
14a75d3e | 1789 | bdn); |
2e8ac303 | 1790 | else if (conf->mddev->degraded >= conf->max_degraded) { |
1791 | set_bad = 1; | |
8bda470e CD |
1792 | printk_ratelimited( |
1793 | KERN_WARNING | |
1794 | "md/raid:%s: read error not correctable " | |
1795 | "(sector %llu on %s).\n", | |
1796 | mdname(conf->mddev), | |
05616be5 | 1797 | (unsigned long long)s, |
8bda470e | 1798 | bdn); |
2e8ac303 | 1799 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
4e5314b5 | 1800 | /* Oh, no!!! */ |
2e8ac303 | 1801 | set_bad = 1; |
8bda470e CD |
1802 | printk_ratelimited( |
1803 | KERN_WARNING | |
1804 | "md/raid:%s: read error NOT corrected!! " | |
1805 | "(sector %llu on %s).\n", | |
1806 | mdname(conf->mddev), | |
05616be5 | 1807 | (unsigned long long)s, |
8bda470e | 1808 | bdn); |
2e8ac303 | 1809 | } else if (atomic_read(&rdev->read_errors) |
ba22dcbf | 1810 | > conf->max_nr_stripes) |
14f8d26b | 1811 | printk(KERN_WARNING |
0c55e022 | 1812 | "md/raid:%s: Too many read errors, failing device %s.\n", |
d6950432 | 1813 | mdname(conf->mddev), bdn); |
ba22dcbf N |
1814 | else |
1815 | retry = 1; | |
1816 | if (retry) | |
3f9e7c14 | 1817 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
1818 | set_bit(R5_ReadError, &sh->dev[i].flags); | |
1819 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
1820 | } else | |
1821 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
ba22dcbf | 1822 | else { |
4e5314b5 N |
1823 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
1824 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
2e8ac303 | 1825 | if (!(set_bad |
1826 | && test_bit(In_sync, &rdev->flags) | |
1827 | && rdev_set_badblocks( | |
1828 | rdev, sh->sector, STRIPE_SECTORS, 0))) | |
1829 | md_error(conf->mddev, rdev); | |
ba22dcbf | 1830 | } |
1da177e4 | 1831 | } |
14a75d3e | 1832 | rdev_dec_pending(rdev, conf->mddev); |
1da177e4 LT |
1833 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
1834 | set_bit(STRIPE_HANDLE, &sh->state); | |
1835 | release_stripe(sh); | |
1da177e4 LT |
1836 | } |
1837 | ||
d710e138 | 1838 | static void raid5_end_write_request(struct bio *bi, int error) |
1da177e4 | 1839 | { |
99c0fb5f | 1840 | struct stripe_head *sh = bi->bi_private; |
d1688a6d | 1841 | struct r5conf *conf = sh->raid_conf; |
7ecaa1e6 | 1842 | int disks = sh->disks, i; |
977df362 | 1843 | struct md_rdev *uninitialized_var(rdev); |
1da177e4 | 1844 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
b84db560 N |
1845 | sector_t first_bad; |
1846 | int bad_sectors; | |
977df362 | 1847 | int replacement = 0; |
1da177e4 | 1848 | |
977df362 N |
1849 | for (i = 0 ; i < disks; i++) { |
1850 | if (bi == &sh->dev[i].req) { | |
1851 | rdev = conf->disks[i].rdev; | |
1da177e4 | 1852 | break; |
977df362 N |
1853 | } |
1854 | if (bi == &sh->dev[i].rreq) { | |
1855 | rdev = conf->disks[i].replacement; | |
dd054fce N |
1856 | if (rdev) |
1857 | replacement = 1; | |
1858 | else | |
1859 | /* rdev was removed and 'replacement' | |
1860 | * replaced it. rdev is not removed | |
1861 | * until all requests are finished. | |
1862 | */ | |
1863 | rdev = conf->disks[i].rdev; | |
977df362 N |
1864 | break; |
1865 | } | |
1866 | } | |
45b4233c | 1867 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
1da177e4 LT |
1868 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
1869 | uptodate); | |
1870 | if (i == disks) { | |
1871 | BUG(); | |
6712ecf8 | 1872 | return; |
1da177e4 LT |
1873 | } |
1874 | ||
977df362 N |
1875 | if (replacement) { |
1876 | if (!uptodate) | |
1877 | md_error(conf->mddev, rdev); | |
1878 | else if (is_badblock(rdev, sh->sector, | |
1879 | STRIPE_SECTORS, | |
1880 | &first_bad, &bad_sectors)) | |
1881 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); | |
1882 | } else { | |
1883 | if (!uptodate) { | |
1884 | set_bit(WriteErrorSeen, &rdev->flags); | |
1885 | set_bit(R5_WriteError, &sh->dev[i].flags); | |
3a6de292 N |
1886 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
1887 | set_bit(MD_RECOVERY_NEEDED, | |
1888 | &rdev->mddev->recovery); | |
977df362 N |
1889 | } else if (is_badblock(rdev, sh->sector, |
1890 | STRIPE_SECTORS, | |
c0b32972 | 1891 | &first_bad, &bad_sectors)) { |
977df362 | 1892 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
c0b32972 N |
1893 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
1894 | /* That was a successful write so make | |
1895 | * sure it looks like we already did | |
1896 | * a re-write. | |
1897 | */ | |
1898 | set_bit(R5_ReWrite, &sh->dev[i].flags); | |
1899 | } | |
977df362 N |
1900 | } |
1901 | rdev_dec_pending(rdev, conf->mddev); | |
1da177e4 | 1902 | |
977df362 N |
1903 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
1904 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
1da177e4 | 1905 | set_bit(STRIPE_HANDLE, &sh->state); |
c04be0aa | 1906 | release_stripe(sh); |
1da177e4 LT |
1907 | } |
1908 | ||
784052ec | 1909 | static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous); |
1da177e4 | 1910 | |
784052ec | 1911 | static void raid5_build_block(struct stripe_head *sh, int i, int previous) |
1da177e4 LT |
1912 | { |
1913 | struct r5dev *dev = &sh->dev[i]; | |
1914 | ||
1915 | bio_init(&dev->req); | |
1916 | dev->req.bi_io_vec = &dev->vec; | |
1917 | dev->req.bi_vcnt++; | |
1918 | dev->req.bi_max_vecs++; | |
1da177e4 | 1919 | dev->req.bi_private = sh; |
995c4275 | 1920 | dev->vec.bv_page = dev->page; |
1da177e4 | 1921 | |
977df362 N |
1922 | bio_init(&dev->rreq); |
1923 | dev->rreq.bi_io_vec = &dev->rvec; | |
1924 | dev->rreq.bi_vcnt++; | |
1925 | dev->rreq.bi_max_vecs++; | |
1926 | dev->rreq.bi_private = sh; | |
1927 | dev->rvec.bv_page = dev->page; | |
1928 | ||
1da177e4 | 1929 | dev->flags = 0; |
784052ec | 1930 | dev->sector = compute_blocknr(sh, i, previous); |
1da177e4 LT |
1931 | } |
1932 | ||
fd01b88c | 1933 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
1934 | { |
1935 | char b[BDEVNAME_SIZE]; | |
d1688a6d | 1936 | struct r5conf *conf = mddev->private; |
908f4fbd | 1937 | unsigned long flags; |
0c55e022 | 1938 | pr_debug("raid456: error called\n"); |
1da177e4 | 1939 | |
908f4fbd N |
1940 | spin_lock_irqsave(&conf->device_lock, flags); |
1941 | clear_bit(In_sync, &rdev->flags); | |
1942 | mddev->degraded = calc_degraded(conf); | |
1943 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1944 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
1945 | ||
de393cde | 1946 | set_bit(Blocked, &rdev->flags); |
6f8d0c77 N |
1947 | set_bit(Faulty, &rdev->flags); |
1948 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
1949 | printk(KERN_ALERT | |
1950 | "md/raid:%s: Disk failure on %s, disabling device.\n" | |
1951 | "md/raid:%s: Operation continuing on %d devices.\n", | |
1952 | mdname(mddev), | |
1953 | bdevname(rdev->bdev, b), | |
1954 | mdname(mddev), | |
1955 | conf->raid_disks - mddev->degraded); | |
16a53ecc | 1956 | } |
1da177e4 LT |
1957 | |
1958 | /* | |
1959 | * Input: a 'big' sector number, | |
1960 | * Output: index of the data and parity disk, and the sector # in them. | |
1961 | */ | |
d1688a6d | 1962 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
911d4ee8 N |
1963 | int previous, int *dd_idx, |
1964 | struct stripe_head *sh) | |
1da177e4 | 1965 | { |
6e3b96ed | 1966 | sector_t stripe, stripe2; |
35f2a591 | 1967 | sector_t chunk_number; |
1da177e4 | 1968 | unsigned int chunk_offset; |
911d4ee8 | 1969 | int pd_idx, qd_idx; |
67cc2b81 | 1970 | int ddf_layout = 0; |
1da177e4 | 1971 | sector_t new_sector; |
e183eaed N |
1972 | int algorithm = previous ? conf->prev_algo |
1973 | : conf->algorithm; | |
09c9e5fa AN |
1974 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
1975 | : conf->chunk_sectors; | |
112bf897 N |
1976 | int raid_disks = previous ? conf->previous_raid_disks |
1977 | : conf->raid_disks; | |
1978 | int data_disks = raid_disks - conf->max_degraded; | |
1da177e4 LT |
1979 | |
1980 | /* First compute the information on this sector */ | |
1981 | ||
1982 | /* | |
1983 | * Compute the chunk number and the sector offset inside the chunk | |
1984 | */ | |
1985 | chunk_offset = sector_div(r_sector, sectors_per_chunk); | |
1986 | chunk_number = r_sector; | |
1da177e4 LT |
1987 | |
1988 | /* | |
1989 | * Compute the stripe number | |
1990 | */ | |
35f2a591 N |
1991 | stripe = chunk_number; |
1992 | *dd_idx = sector_div(stripe, data_disks); | |
6e3b96ed | 1993 | stripe2 = stripe; |
1da177e4 LT |
1994 | /* |
1995 | * Select the parity disk based on the user selected algorithm. | |
1996 | */ | |
84789554 | 1997 | pd_idx = qd_idx = -1; |
16a53ecc N |
1998 | switch(conf->level) { |
1999 | case 4: | |
911d4ee8 | 2000 | pd_idx = data_disks; |
16a53ecc N |
2001 | break; |
2002 | case 5: | |
e183eaed | 2003 | switch (algorithm) { |
1da177e4 | 2004 | case ALGORITHM_LEFT_ASYMMETRIC: |
6e3b96ed | 2005 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
911d4ee8 | 2006 | if (*dd_idx >= pd_idx) |
1da177e4 LT |
2007 | (*dd_idx)++; |
2008 | break; | |
2009 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
6e3b96ed | 2010 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 | 2011 | if (*dd_idx >= pd_idx) |
1da177e4 LT |
2012 | (*dd_idx)++; |
2013 | break; | |
2014 | case ALGORITHM_LEFT_SYMMETRIC: | |
6e3b96ed | 2015 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
911d4ee8 | 2016 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
1da177e4 LT |
2017 | break; |
2018 | case ALGORITHM_RIGHT_SYMMETRIC: | |
6e3b96ed | 2019 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 | 2020 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
1da177e4 | 2021 | break; |
99c0fb5f N |
2022 | case ALGORITHM_PARITY_0: |
2023 | pd_idx = 0; | |
2024 | (*dd_idx)++; | |
2025 | break; | |
2026 | case ALGORITHM_PARITY_N: | |
2027 | pd_idx = data_disks; | |
2028 | break; | |
1da177e4 | 2029 | default: |
99c0fb5f | 2030 | BUG(); |
16a53ecc N |
2031 | } |
2032 | break; | |
2033 | case 6: | |
2034 | ||
e183eaed | 2035 | switch (algorithm) { |
16a53ecc | 2036 | case ALGORITHM_LEFT_ASYMMETRIC: |
6e3b96ed | 2037 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
911d4ee8 N |
2038 | qd_idx = pd_idx + 1; |
2039 | if (pd_idx == raid_disks-1) { | |
99c0fb5f | 2040 | (*dd_idx)++; /* Q D D D P */ |
911d4ee8 N |
2041 | qd_idx = 0; |
2042 | } else if (*dd_idx >= pd_idx) | |
16a53ecc N |
2043 | (*dd_idx) += 2; /* D D P Q D */ |
2044 | break; | |
2045 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
6e3b96ed | 2046 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 N |
2047 | qd_idx = pd_idx + 1; |
2048 | if (pd_idx == raid_disks-1) { | |
99c0fb5f | 2049 | (*dd_idx)++; /* Q D D D P */ |
911d4ee8 N |
2050 | qd_idx = 0; |
2051 | } else if (*dd_idx >= pd_idx) | |
16a53ecc N |
2052 | (*dd_idx) += 2; /* D D P Q D */ |
2053 | break; | |
2054 | case ALGORITHM_LEFT_SYMMETRIC: | |
6e3b96ed | 2055 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
911d4ee8 N |
2056 | qd_idx = (pd_idx + 1) % raid_disks; |
2057 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; | |
16a53ecc N |
2058 | break; |
2059 | case ALGORITHM_RIGHT_SYMMETRIC: | |
6e3b96ed | 2060 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 N |
2061 | qd_idx = (pd_idx + 1) % raid_disks; |
2062 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; | |
16a53ecc | 2063 | break; |
99c0fb5f N |
2064 | |
2065 | case ALGORITHM_PARITY_0: | |
2066 | pd_idx = 0; | |
2067 | qd_idx = 1; | |
2068 | (*dd_idx) += 2; | |
2069 | break; | |
2070 | case ALGORITHM_PARITY_N: | |
2071 | pd_idx = data_disks; | |
2072 | qd_idx = data_disks + 1; | |
2073 | break; | |
2074 | ||
2075 | case ALGORITHM_ROTATING_ZERO_RESTART: | |
2076 | /* Exactly the same as RIGHT_ASYMMETRIC, but or | |
2077 | * of blocks for computing Q is different. | |
2078 | */ | |
6e3b96ed | 2079 | pd_idx = sector_div(stripe2, raid_disks); |
99c0fb5f N |
2080 | qd_idx = pd_idx + 1; |
2081 | if (pd_idx == raid_disks-1) { | |
2082 | (*dd_idx)++; /* Q D D D P */ | |
2083 | qd_idx = 0; | |
2084 | } else if (*dd_idx >= pd_idx) | |
2085 | (*dd_idx) += 2; /* D D P Q D */ | |
67cc2b81 | 2086 | ddf_layout = 1; |
99c0fb5f N |
2087 | break; |
2088 | ||
2089 | case ALGORITHM_ROTATING_N_RESTART: | |
2090 | /* Same a left_asymmetric, by first stripe is | |
2091 | * D D D P Q rather than | |
2092 | * Q D D D P | |
2093 | */ | |
6e3b96ed N |
2094 | stripe2 += 1; |
2095 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); | |
99c0fb5f N |
2096 | qd_idx = pd_idx + 1; |
2097 | if (pd_idx == raid_disks-1) { | |
2098 | (*dd_idx)++; /* Q D D D P */ | |
2099 | qd_idx = 0; | |
2100 | } else if (*dd_idx >= pd_idx) | |
2101 | (*dd_idx) += 2; /* D D P Q D */ | |
67cc2b81 | 2102 | ddf_layout = 1; |
99c0fb5f N |
2103 | break; |
2104 | ||
2105 | case ALGORITHM_ROTATING_N_CONTINUE: | |
2106 | /* Same as left_symmetric but Q is before P */ | |
6e3b96ed | 2107 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
99c0fb5f N |
2108 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
2109 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; | |
67cc2b81 | 2110 | ddf_layout = 1; |
99c0fb5f N |
2111 | break; |
2112 | ||
2113 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
2114 | /* RAID5 left_asymmetric, with Q on last device */ | |
6e3b96ed | 2115 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2116 | if (*dd_idx >= pd_idx) |
2117 | (*dd_idx)++; | |
2118 | qd_idx = raid_disks - 1; | |
2119 | break; | |
2120 | ||
2121 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
6e3b96ed | 2122 | pd_idx = sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2123 | if (*dd_idx >= pd_idx) |
2124 | (*dd_idx)++; | |
2125 | qd_idx = raid_disks - 1; | |
2126 | break; | |
2127 | ||
2128 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
6e3b96ed | 2129 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2130 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
2131 | qd_idx = raid_disks - 1; | |
2132 | break; | |
2133 | ||
2134 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
6e3b96ed | 2135 | pd_idx = sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2136 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
2137 | qd_idx = raid_disks - 1; | |
2138 | break; | |
2139 | ||
2140 | case ALGORITHM_PARITY_0_6: | |
2141 | pd_idx = 0; | |
2142 | (*dd_idx)++; | |
2143 | qd_idx = raid_disks - 1; | |
2144 | break; | |
2145 | ||
16a53ecc | 2146 | default: |
99c0fb5f | 2147 | BUG(); |
16a53ecc N |
2148 | } |
2149 | break; | |
1da177e4 LT |
2150 | } |
2151 | ||
911d4ee8 N |
2152 | if (sh) { |
2153 | sh->pd_idx = pd_idx; | |
2154 | sh->qd_idx = qd_idx; | |
67cc2b81 | 2155 | sh->ddf_layout = ddf_layout; |
911d4ee8 | 2156 | } |
1da177e4 LT |
2157 | /* |
2158 | * Finally, compute the new sector number | |
2159 | */ | |
2160 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; | |
2161 | return new_sector; | |
2162 | } | |
2163 | ||
2164 | ||
784052ec | 2165 | static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous) |
1da177e4 | 2166 | { |
d1688a6d | 2167 | struct r5conf *conf = sh->raid_conf; |
b875e531 N |
2168 | int raid_disks = sh->disks; |
2169 | int data_disks = raid_disks - conf->max_degraded; | |
1da177e4 | 2170 | sector_t new_sector = sh->sector, check; |
09c9e5fa AN |
2171 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
2172 | : conf->chunk_sectors; | |
e183eaed N |
2173 | int algorithm = previous ? conf->prev_algo |
2174 | : conf->algorithm; | |
1da177e4 LT |
2175 | sector_t stripe; |
2176 | int chunk_offset; | |
35f2a591 N |
2177 | sector_t chunk_number; |
2178 | int dummy1, dd_idx = i; | |
1da177e4 | 2179 | sector_t r_sector; |
911d4ee8 | 2180 | struct stripe_head sh2; |
1da177e4 | 2181 | |
16a53ecc | 2182 | |
1da177e4 LT |
2183 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
2184 | stripe = new_sector; | |
1da177e4 | 2185 | |
16a53ecc N |
2186 | if (i == sh->pd_idx) |
2187 | return 0; | |
2188 | switch(conf->level) { | |
2189 | case 4: break; | |
2190 | case 5: | |
e183eaed | 2191 | switch (algorithm) { |
1da177e4 LT |
2192 | case ALGORITHM_LEFT_ASYMMETRIC: |
2193 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
2194 | if (i > sh->pd_idx) | |
2195 | i--; | |
2196 | break; | |
2197 | case ALGORITHM_LEFT_SYMMETRIC: | |
2198 | case ALGORITHM_RIGHT_SYMMETRIC: | |
2199 | if (i < sh->pd_idx) | |
2200 | i += raid_disks; | |
2201 | i -= (sh->pd_idx + 1); | |
2202 | break; | |
99c0fb5f N |
2203 | case ALGORITHM_PARITY_0: |
2204 | i -= 1; | |
2205 | break; | |
2206 | case ALGORITHM_PARITY_N: | |
2207 | break; | |
1da177e4 | 2208 | default: |
99c0fb5f | 2209 | BUG(); |
16a53ecc N |
2210 | } |
2211 | break; | |
2212 | case 6: | |
d0dabf7e | 2213 | if (i == sh->qd_idx) |
16a53ecc | 2214 | return 0; /* It is the Q disk */ |
e183eaed | 2215 | switch (algorithm) { |
16a53ecc N |
2216 | case ALGORITHM_LEFT_ASYMMETRIC: |
2217 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
99c0fb5f N |
2218 | case ALGORITHM_ROTATING_ZERO_RESTART: |
2219 | case ALGORITHM_ROTATING_N_RESTART: | |
2220 | if (sh->pd_idx == raid_disks-1) | |
2221 | i--; /* Q D D D P */ | |
16a53ecc N |
2222 | else if (i > sh->pd_idx) |
2223 | i -= 2; /* D D P Q D */ | |
2224 | break; | |
2225 | case ALGORITHM_LEFT_SYMMETRIC: | |
2226 | case ALGORITHM_RIGHT_SYMMETRIC: | |
2227 | if (sh->pd_idx == raid_disks-1) | |
2228 | i--; /* Q D D D P */ | |
2229 | else { | |
2230 | /* D D P Q D */ | |
2231 | if (i < sh->pd_idx) | |
2232 | i += raid_disks; | |
2233 | i -= (sh->pd_idx + 2); | |
2234 | } | |
2235 | break; | |
99c0fb5f N |
2236 | case ALGORITHM_PARITY_0: |
2237 | i -= 2; | |
2238 | break; | |
2239 | case ALGORITHM_PARITY_N: | |
2240 | break; | |
2241 | case ALGORITHM_ROTATING_N_CONTINUE: | |
e4424fee | 2242 | /* Like left_symmetric, but P is before Q */ |
99c0fb5f N |
2243 | if (sh->pd_idx == 0) |
2244 | i--; /* P D D D Q */ | |
e4424fee N |
2245 | else { |
2246 | /* D D Q P D */ | |
2247 | if (i < sh->pd_idx) | |
2248 | i += raid_disks; | |
2249 | i -= (sh->pd_idx + 1); | |
2250 | } | |
99c0fb5f N |
2251 | break; |
2252 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
2253 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
2254 | if (i > sh->pd_idx) | |
2255 | i--; | |
2256 | break; | |
2257 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
2258 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
2259 | if (i < sh->pd_idx) | |
2260 | i += data_disks + 1; | |
2261 | i -= (sh->pd_idx + 1); | |
2262 | break; | |
2263 | case ALGORITHM_PARITY_0_6: | |
2264 | i -= 1; | |
2265 | break; | |
16a53ecc | 2266 | default: |
99c0fb5f | 2267 | BUG(); |
16a53ecc N |
2268 | } |
2269 | break; | |
1da177e4 LT |
2270 | } |
2271 | ||
2272 | chunk_number = stripe * data_disks + i; | |
35f2a591 | 2273 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
1da177e4 | 2274 | |
112bf897 | 2275 | check = raid5_compute_sector(conf, r_sector, |
784052ec | 2276 | previous, &dummy1, &sh2); |
911d4ee8 N |
2277 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
2278 | || sh2.qd_idx != sh->qd_idx) { | |
0c55e022 N |
2279 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
2280 | mdname(conf->mddev)); | |
1da177e4 LT |
2281 | return 0; |
2282 | } | |
2283 | return r_sector; | |
2284 | } | |
2285 | ||
2286 | ||
600aa109 | 2287 | static void |
c0f7bddb | 2288 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
600aa109 | 2289 | int rcw, int expand) |
e33129d8 DW |
2290 | { |
2291 | int i, pd_idx = sh->pd_idx, disks = sh->disks; | |
d1688a6d | 2292 | struct r5conf *conf = sh->raid_conf; |
c0f7bddb | 2293 | int level = conf->level; |
e33129d8 DW |
2294 | |
2295 | if (rcw) { | |
e33129d8 DW |
2296 | |
2297 | for (i = disks; i--; ) { | |
2298 | struct r5dev *dev = &sh->dev[i]; | |
2299 | ||
2300 | if (dev->towrite) { | |
2301 | set_bit(R5_LOCKED, &dev->flags); | |
d8ee0728 | 2302 | set_bit(R5_Wantdrain, &dev->flags); |
e33129d8 DW |
2303 | if (!expand) |
2304 | clear_bit(R5_UPTODATE, &dev->flags); | |
600aa109 | 2305 | s->locked++; |
e33129d8 DW |
2306 | } |
2307 | } | |
ce7d363a N |
2308 | /* if we are not expanding this is a proper write request, and |
2309 | * there will be bios with new data to be drained into the | |
2310 | * stripe cache | |
2311 | */ | |
2312 | if (!expand) { | |
2313 | if (!s->locked) | |
2314 | /* False alarm, nothing to do */ | |
2315 | return; | |
2316 | sh->reconstruct_state = reconstruct_state_drain_run; | |
2317 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); | |
2318 | } else | |
2319 | sh->reconstruct_state = reconstruct_state_run; | |
2320 | ||
2321 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); | |
2322 | ||
c0f7bddb | 2323 | if (s->locked + conf->max_degraded == disks) |
8b3e6cdc | 2324 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
c0f7bddb | 2325 | atomic_inc(&conf->pending_full_writes); |
e33129d8 | 2326 | } else { |
c0f7bddb | 2327 | BUG_ON(level == 6); |
e33129d8 DW |
2328 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
2329 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); | |
2330 | ||
e33129d8 DW |
2331 | for (i = disks; i--; ) { |
2332 | struct r5dev *dev = &sh->dev[i]; | |
2333 | if (i == pd_idx) | |
2334 | continue; | |
2335 | ||
e33129d8 DW |
2336 | if (dev->towrite && |
2337 | (test_bit(R5_UPTODATE, &dev->flags) || | |
d8ee0728 DW |
2338 | test_bit(R5_Wantcompute, &dev->flags))) { |
2339 | set_bit(R5_Wantdrain, &dev->flags); | |
e33129d8 DW |
2340 | set_bit(R5_LOCKED, &dev->flags); |
2341 | clear_bit(R5_UPTODATE, &dev->flags); | |
600aa109 | 2342 | s->locked++; |
e33129d8 DW |
2343 | } |
2344 | } | |
ce7d363a N |
2345 | if (!s->locked) |
2346 | /* False alarm - nothing to do */ | |
2347 | return; | |
2348 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; | |
2349 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); | |
2350 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); | |
2351 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); | |
e33129d8 DW |
2352 | } |
2353 | ||
c0f7bddb | 2354 | /* keep the parity disk(s) locked while asynchronous operations |
e33129d8 DW |
2355 | * are in flight |
2356 | */ | |
2357 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); | |
2358 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
600aa109 | 2359 | s->locked++; |
e33129d8 | 2360 | |
c0f7bddb YT |
2361 | if (level == 6) { |
2362 | int qd_idx = sh->qd_idx; | |
2363 | struct r5dev *dev = &sh->dev[qd_idx]; | |
2364 | ||
2365 | set_bit(R5_LOCKED, &dev->flags); | |
2366 | clear_bit(R5_UPTODATE, &dev->flags); | |
2367 | s->locked++; | |
2368 | } | |
2369 | ||
600aa109 | 2370 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
e46b272b | 2371 | __func__, (unsigned long long)sh->sector, |
600aa109 | 2372 | s->locked, s->ops_request); |
e33129d8 | 2373 | } |
16a53ecc | 2374 | |
1da177e4 LT |
2375 | /* |
2376 | * Each stripe/dev can have one or more bion attached. | |
16a53ecc | 2377 | * toread/towrite point to the first in a chain. |
1da177e4 LT |
2378 | * The bi_next chain must be in order. |
2379 | */ | |
2380 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) | |
2381 | { | |
2382 | struct bio **bip; | |
d1688a6d | 2383 | struct r5conf *conf = sh->raid_conf; |
72626685 | 2384 | int firstwrite=0; |
1da177e4 | 2385 | |
cbe47ec5 | 2386 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
1da177e4 LT |
2387 | (unsigned long long)bi->bi_sector, |
2388 | (unsigned long long)sh->sector); | |
2389 | ||
b17459c0 SL |
2390 | /* |
2391 | * If several bio share a stripe. The bio bi_phys_segments acts as a | |
2392 | * reference count to avoid race. The reference count should already be | |
2393 | * increased before this function is called (for example, in | |
2394 | * make_request()), so other bio sharing this stripe will not free the | |
2395 | * stripe. If a stripe is owned by one stripe, the stripe lock will | |
2396 | * protect it. | |
2397 | */ | |
2398 | spin_lock_irq(&sh->stripe_lock); | |
72626685 | 2399 | if (forwrite) { |
1da177e4 | 2400 | bip = &sh->dev[dd_idx].towrite; |
7eaf7e8e | 2401 | if (*bip == NULL) |
72626685 N |
2402 | firstwrite = 1; |
2403 | } else | |
1da177e4 LT |
2404 | bip = &sh->dev[dd_idx].toread; |
2405 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { | |
f73a1c7d | 2406 | if (bio_end_sector(*bip) > bi->bi_sector) |
1da177e4 LT |
2407 | goto overlap; |
2408 | bip = & (*bip)->bi_next; | |
2409 | } | |
f73a1c7d | 2410 | if (*bip && (*bip)->bi_sector < bio_end_sector(bi)) |
1da177e4 LT |
2411 | goto overlap; |
2412 | ||
78bafebd | 2413 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
1da177e4 LT |
2414 | if (*bip) |
2415 | bi->bi_next = *bip; | |
2416 | *bip = bi; | |
e7836bd6 | 2417 | raid5_inc_bi_active_stripes(bi); |
72626685 | 2418 | |
1da177e4 LT |
2419 | if (forwrite) { |
2420 | /* check if page is covered */ | |
2421 | sector_t sector = sh->dev[dd_idx].sector; | |
2422 | for (bi=sh->dev[dd_idx].towrite; | |
2423 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && | |
2424 | bi && bi->bi_sector <= sector; | |
2425 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { | |
f73a1c7d KO |
2426 | if (bio_end_sector(bi) >= sector) |
2427 | sector = bio_end_sector(bi); | |
1da177e4 LT |
2428 | } |
2429 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) | |
2430 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); | |
2431 | } | |
cbe47ec5 N |
2432 | |
2433 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", | |
2434 | (unsigned long long)(*bip)->bi_sector, | |
2435 | (unsigned long long)sh->sector, dd_idx); | |
b97390ae | 2436 | spin_unlock_irq(&sh->stripe_lock); |
cbe47ec5 N |
2437 | |
2438 | if (conf->mddev->bitmap && firstwrite) { | |
2439 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, | |
2440 | STRIPE_SECTORS, 0); | |
2441 | sh->bm_seq = conf->seq_flush+1; | |
2442 | set_bit(STRIPE_BIT_DELAY, &sh->state); | |
2443 | } | |
1da177e4 LT |
2444 | return 1; |
2445 | ||
2446 | overlap: | |
2447 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); | |
b17459c0 | 2448 | spin_unlock_irq(&sh->stripe_lock); |
1da177e4 LT |
2449 | return 0; |
2450 | } | |
2451 | ||
d1688a6d | 2452 | static void end_reshape(struct r5conf *conf); |
29269553 | 2453 | |
d1688a6d | 2454 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
911d4ee8 | 2455 | struct stripe_head *sh) |
ccfcc3c1 | 2456 | { |
784052ec | 2457 | int sectors_per_chunk = |
09c9e5fa | 2458 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
911d4ee8 | 2459 | int dd_idx; |
2d2063ce | 2460 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
112bf897 | 2461 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
2d2063ce | 2462 | |
112bf897 N |
2463 | raid5_compute_sector(conf, |
2464 | stripe * (disks - conf->max_degraded) | |
b875e531 | 2465 | *sectors_per_chunk + chunk_offset, |
112bf897 | 2466 | previous, |
911d4ee8 | 2467 | &dd_idx, sh); |
ccfcc3c1 N |
2468 | } |
2469 | ||
a4456856 | 2470 | static void |
d1688a6d | 2471 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
a4456856 DW |
2472 | struct stripe_head_state *s, int disks, |
2473 | struct bio **return_bi) | |
2474 | { | |
2475 | int i; | |
2476 | for (i = disks; i--; ) { | |
2477 | struct bio *bi; | |
2478 | int bitmap_end = 0; | |
2479 | ||
2480 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
3cb03002 | 2481 | struct md_rdev *rdev; |
a4456856 DW |
2482 | rcu_read_lock(); |
2483 | rdev = rcu_dereference(conf->disks[i].rdev); | |
2484 | if (rdev && test_bit(In_sync, &rdev->flags)) | |
7f0da59b N |
2485 | atomic_inc(&rdev->nr_pending); |
2486 | else | |
2487 | rdev = NULL; | |
a4456856 | 2488 | rcu_read_unlock(); |
7f0da59b N |
2489 | if (rdev) { |
2490 | if (!rdev_set_badblocks( | |
2491 | rdev, | |
2492 | sh->sector, | |
2493 | STRIPE_SECTORS, 0)) | |
2494 | md_error(conf->mddev, rdev); | |
2495 | rdev_dec_pending(rdev, conf->mddev); | |
2496 | } | |
a4456856 | 2497 | } |
b17459c0 | 2498 | spin_lock_irq(&sh->stripe_lock); |
a4456856 DW |
2499 | /* fail all writes first */ |
2500 | bi = sh->dev[i].towrite; | |
2501 | sh->dev[i].towrite = NULL; | |
b17459c0 | 2502 | spin_unlock_irq(&sh->stripe_lock); |
1ed850f3 | 2503 | if (bi) |
a4456856 | 2504 | bitmap_end = 1; |
a4456856 DW |
2505 | |
2506 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
2507 | wake_up(&conf->wait_for_overlap); | |
2508 | ||
2509 | while (bi && bi->bi_sector < | |
2510 | sh->dev[i].sector + STRIPE_SECTORS) { | |
2511 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
2512 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
e7836bd6 | 2513 | if (!raid5_dec_bi_active_stripes(bi)) { |
a4456856 DW |
2514 | md_write_end(conf->mddev); |
2515 | bi->bi_next = *return_bi; | |
2516 | *return_bi = bi; | |
2517 | } | |
2518 | bi = nextbi; | |
2519 | } | |
7eaf7e8e SL |
2520 | if (bitmap_end) |
2521 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
2522 | STRIPE_SECTORS, 0, 0); | |
2523 | bitmap_end = 0; | |
a4456856 DW |
2524 | /* and fail all 'written' */ |
2525 | bi = sh->dev[i].written; | |
2526 | sh->dev[i].written = NULL; | |
2527 | if (bi) bitmap_end = 1; | |
2528 | while (bi && bi->bi_sector < | |
2529 | sh->dev[i].sector + STRIPE_SECTORS) { | |
2530 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); | |
2531 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
e7836bd6 | 2532 | if (!raid5_dec_bi_active_stripes(bi)) { |
a4456856 DW |
2533 | md_write_end(conf->mddev); |
2534 | bi->bi_next = *return_bi; | |
2535 | *return_bi = bi; | |
2536 | } | |
2537 | bi = bi2; | |
2538 | } | |
2539 | ||
b5e98d65 DW |
2540 | /* fail any reads if this device is non-operational and |
2541 | * the data has not reached the cache yet. | |
2542 | */ | |
2543 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && | |
2544 | (!test_bit(R5_Insync, &sh->dev[i].flags) || | |
2545 | test_bit(R5_ReadError, &sh->dev[i].flags))) { | |
143c4d05 | 2546 | spin_lock_irq(&sh->stripe_lock); |
a4456856 DW |
2547 | bi = sh->dev[i].toread; |
2548 | sh->dev[i].toread = NULL; | |
143c4d05 | 2549 | spin_unlock_irq(&sh->stripe_lock); |
a4456856 DW |
2550 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
2551 | wake_up(&conf->wait_for_overlap); | |
a4456856 DW |
2552 | while (bi && bi->bi_sector < |
2553 | sh->dev[i].sector + STRIPE_SECTORS) { | |
2554 | struct bio *nextbi = | |
2555 | r5_next_bio(bi, sh->dev[i].sector); | |
2556 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
e7836bd6 | 2557 | if (!raid5_dec_bi_active_stripes(bi)) { |
a4456856 DW |
2558 | bi->bi_next = *return_bi; |
2559 | *return_bi = bi; | |
2560 | } | |
2561 | bi = nextbi; | |
2562 | } | |
2563 | } | |
a4456856 DW |
2564 | if (bitmap_end) |
2565 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
2566 | STRIPE_SECTORS, 0, 0); | |
8cfa7b0f N |
2567 | /* If we were in the middle of a write the parity block might |
2568 | * still be locked - so just clear all R5_LOCKED flags | |
2569 | */ | |
2570 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
a4456856 DW |
2571 | } |
2572 | ||
8b3e6cdc DW |
2573 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
2574 | if (atomic_dec_and_test(&conf->pending_full_writes)) | |
2575 | md_wakeup_thread(conf->mddev->thread); | |
a4456856 DW |
2576 | } |
2577 | ||
7f0da59b | 2578 | static void |
d1688a6d | 2579 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
7f0da59b N |
2580 | struct stripe_head_state *s) |
2581 | { | |
2582 | int abort = 0; | |
2583 | int i; | |
2584 | ||
7f0da59b | 2585 | clear_bit(STRIPE_SYNCING, &sh->state); |
f8dfcffd N |
2586 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
2587 | wake_up(&conf->wait_for_overlap); | |
7f0da59b | 2588 | s->syncing = 0; |
9a3e1101 | 2589 | s->replacing = 0; |
7f0da59b | 2590 | /* There is nothing more to do for sync/check/repair. |
18b9837e N |
2591 | * Don't even need to abort as that is handled elsewhere |
2592 | * if needed, and not always wanted e.g. if there is a known | |
2593 | * bad block here. | |
9a3e1101 | 2594 | * For recover/replace we need to record a bad block on all |
7f0da59b N |
2595 | * non-sync devices, or abort the recovery |
2596 | */ | |
18b9837e N |
2597 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
2598 | /* During recovery devices cannot be removed, so | |
2599 | * locking and refcounting of rdevs is not needed | |
2600 | */ | |
2601 | for (i = 0; i < conf->raid_disks; i++) { | |
2602 | struct md_rdev *rdev = conf->disks[i].rdev; | |
2603 | if (rdev | |
2604 | && !test_bit(Faulty, &rdev->flags) | |
2605 | && !test_bit(In_sync, &rdev->flags) | |
2606 | && !rdev_set_badblocks(rdev, sh->sector, | |
2607 | STRIPE_SECTORS, 0)) | |
2608 | abort = 1; | |
2609 | rdev = conf->disks[i].replacement; | |
2610 | if (rdev | |
2611 | && !test_bit(Faulty, &rdev->flags) | |
2612 | && !test_bit(In_sync, &rdev->flags) | |
2613 | && !rdev_set_badblocks(rdev, sh->sector, | |
2614 | STRIPE_SECTORS, 0)) | |
2615 | abort = 1; | |
2616 | } | |
2617 | if (abort) | |
2618 | conf->recovery_disabled = | |
2619 | conf->mddev->recovery_disabled; | |
7f0da59b | 2620 | } |
18b9837e | 2621 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
7f0da59b N |
2622 | } |
2623 | ||
9a3e1101 N |
2624 | static int want_replace(struct stripe_head *sh, int disk_idx) |
2625 | { | |
2626 | struct md_rdev *rdev; | |
2627 | int rv = 0; | |
2628 | /* Doing recovery so rcu locking not required */ | |
2629 | rdev = sh->raid_conf->disks[disk_idx].replacement; | |
2630 | if (rdev | |
2631 | && !test_bit(Faulty, &rdev->flags) | |
2632 | && !test_bit(In_sync, &rdev->flags) | |
2633 | && (rdev->recovery_offset <= sh->sector | |
2634 | || rdev->mddev->recovery_cp <= sh->sector)) | |
2635 | rv = 1; | |
2636 | ||
2637 | return rv; | |
2638 | } | |
2639 | ||
93b3dbce | 2640 | /* fetch_block - checks the given member device to see if its data needs |
1fe797e6 DW |
2641 | * to be read or computed to satisfy a request. |
2642 | * | |
2643 | * Returns 1 when no more member devices need to be checked, otherwise returns | |
93b3dbce | 2644 | * 0 to tell the loop in handle_stripe_fill to continue |
f38e1219 | 2645 | */ |
93b3dbce N |
2646 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
2647 | int disk_idx, int disks) | |
a4456856 | 2648 | { |
5599becc | 2649 | struct r5dev *dev = &sh->dev[disk_idx]; |
f2b3b44d N |
2650 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
2651 | &sh->dev[s->failed_num[1]] }; | |
5599becc | 2652 | |
93b3dbce | 2653 | /* is the data in this block needed, and can we get it? */ |
5599becc YT |
2654 | if (!test_bit(R5_LOCKED, &dev->flags) && |
2655 | !test_bit(R5_UPTODATE, &dev->flags) && | |
2656 | (dev->toread || | |
2657 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || | |
2658 | s->syncing || s->expanding || | |
9a3e1101 | 2659 | (s->replacing && want_replace(sh, disk_idx)) || |
5d35e09c N |
2660 | (s->failed >= 1 && fdev[0]->toread) || |
2661 | (s->failed >= 2 && fdev[1]->toread) || | |
93b3dbce N |
2662 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
2663 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || | |
2664 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { | |
5599becc YT |
2665 | /* we would like to get this block, possibly by computing it, |
2666 | * otherwise read it if the backing disk is insync | |
2667 | */ | |
2668 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); | |
2669 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); | |
2670 | if ((s->uptodate == disks - 1) && | |
f2b3b44d N |
2671 | (s->failed && (disk_idx == s->failed_num[0] || |
2672 | disk_idx == s->failed_num[1]))) { | |
5599becc YT |
2673 | /* have disk failed, and we're requested to fetch it; |
2674 | * do compute it | |
a4456856 | 2675 | */ |
5599becc YT |
2676 | pr_debug("Computing stripe %llu block %d\n", |
2677 | (unsigned long long)sh->sector, disk_idx); | |
2678 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
2679 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
2680 | set_bit(R5_Wantcompute, &dev->flags); | |
2681 | sh->ops.target = disk_idx; | |
2682 | sh->ops.target2 = -1; /* no 2nd target */ | |
2683 | s->req_compute = 1; | |
93b3dbce N |
2684 | /* Careful: from this point on 'uptodate' is in the eye |
2685 | * of raid_run_ops which services 'compute' operations | |
2686 | * before writes. R5_Wantcompute flags a block that will | |
2687 | * be R5_UPTODATE by the time it is needed for a | |
2688 | * subsequent operation. | |
2689 | */ | |
5599becc YT |
2690 | s->uptodate++; |
2691 | return 1; | |
2692 | } else if (s->uptodate == disks-2 && s->failed >= 2) { | |
2693 | /* Computing 2-failure is *very* expensive; only | |
2694 | * do it if failed >= 2 | |
2695 | */ | |
2696 | int other; | |
2697 | for (other = disks; other--; ) { | |
2698 | if (other == disk_idx) | |
2699 | continue; | |
2700 | if (!test_bit(R5_UPTODATE, | |
2701 | &sh->dev[other].flags)) | |
2702 | break; | |
a4456856 | 2703 | } |
5599becc YT |
2704 | BUG_ON(other < 0); |
2705 | pr_debug("Computing stripe %llu blocks %d,%d\n", | |
2706 | (unsigned long long)sh->sector, | |
2707 | disk_idx, other); | |
2708 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
2709 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
2710 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); | |
2711 | set_bit(R5_Wantcompute, &sh->dev[other].flags); | |
2712 | sh->ops.target = disk_idx; | |
2713 | sh->ops.target2 = other; | |
2714 | s->uptodate += 2; | |
2715 | s->req_compute = 1; | |
2716 | return 1; | |
2717 | } else if (test_bit(R5_Insync, &dev->flags)) { | |
2718 | set_bit(R5_LOCKED, &dev->flags); | |
2719 | set_bit(R5_Wantread, &dev->flags); | |
2720 | s->locked++; | |
2721 | pr_debug("Reading block %d (sync=%d)\n", | |
2722 | disk_idx, s->syncing); | |
a4456856 DW |
2723 | } |
2724 | } | |
5599becc YT |
2725 | |
2726 | return 0; | |
2727 | } | |
2728 | ||
2729 | /** | |
93b3dbce | 2730 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
5599becc | 2731 | */ |
93b3dbce N |
2732 | static void handle_stripe_fill(struct stripe_head *sh, |
2733 | struct stripe_head_state *s, | |
2734 | int disks) | |
5599becc YT |
2735 | { |
2736 | int i; | |
2737 | ||
2738 | /* look for blocks to read/compute, skip this if a compute | |
2739 | * is already in flight, or if the stripe contents are in the | |
2740 | * midst of changing due to a write | |
2741 | */ | |
2742 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && | |
2743 | !sh->reconstruct_state) | |
2744 | for (i = disks; i--; ) | |
93b3dbce | 2745 | if (fetch_block(sh, s, i, disks)) |
5599becc | 2746 | break; |
a4456856 DW |
2747 | set_bit(STRIPE_HANDLE, &sh->state); |
2748 | } | |
2749 | ||
2750 | ||
1fe797e6 | 2751 | /* handle_stripe_clean_event |
a4456856 DW |
2752 | * any written block on an uptodate or failed drive can be returned. |
2753 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but | |
2754 | * never LOCKED, so we don't need to test 'failed' directly. | |
2755 | */ | |
d1688a6d | 2756 | static void handle_stripe_clean_event(struct r5conf *conf, |
a4456856 DW |
2757 | struct stripe_head *sh, int disks, struct bio **return_bi) |
2758 | { | |
2759 | int i; | |
2760 | struct r5dev *dev; | |
f8dfcffd | 2761 | int discard_pending = 0; |
a4456856 DW |
2762 | |
2763 | for (i = disks; i--; ) | |
2764 | if (sh->dev[i].written) { | |
2765 | dev = &sh->dev[i]; | |
2766 | if (!test_bit(R5_LOCKED, &dev->flags) && | |
9e444768 | 2767 | (test_bit(R5_UPTODATE, &dev->flags) || |
ca64cae9 | 2768 | test_bit(R5_Discard, &dev->flags))) { |
a4456856 DW |
2769 | /* We can return any write requests */ |
2770 | struct bio *wbi, *wbi2; | |
45b4233c | 2771 | pr_debug("Return write for disc %d\n", i); |
ca64cae9 N |
2772 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
2773 | clear_bit(R5_UPTODATE, &dev->flags); | |
a4456856 DW |
2774 | wbi = dev->written; |
2775 | dev->written = NULL; | |
2776 | while (wbi && wbi->bi_sector < | |
2777 | dev->sector + STRIPE_SECTORS) { | |
2778 | wbi2 = r5_next_bio(wbi, dev->sector); | |
e7836bd6 | 2779 | if (!raid5_dec_bi_active_stripes(wbi)) { |
a4456856 DW |
2780 | md_write_end(conf->mddev); |
2781 | wbi->bi_next = *return_bi; | |
2782 | *return_bi = wbi; | |
2783 | } | |
2784 | wbi = wbi2; | |
2785 | } | |
7eaf7e8e SL |
2786 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
2787 | STRIPE_SECTORS, | |
a4456856 | 2788 | !test_bit(STRIPE_DEGRADED, &sh->state), |
7eaf7e8e | 2789 | 0); |
f8dfcffd N |
2790 | } else if (test_bit(R5_Discard, &dev->flags)) |
2791 | discard_pending = 1; | |
2792 | } | |
2793 | if (!discard_pending && | |
2794 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { | |
2795 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); | |
2796 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); | |
2797 | if (sh->qd_idx >= 0) { | |
2798 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); | |
2799 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); | |
2800 | } | |
2801 | /* now that discard is done we can proceed with any sync */ | |
2802 | clear_bit(STRIPE_DISCARD, &sh->state); | |
2803 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) | |
2804 | set_bit(STRIPE_HANDLE, &sh->state); | |
2805 | ||
2806 | } | |
8b3e6cdc DW |
2807 | |
2808 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) | |
2809 | if (atomic_dec_and_test(&conf->pending_full_writes)) | |
2810 | md_wakeup_thread(conf->mddev->thread); | |
a4456856 DW |
2811 | } |
2812 | ||
d1688a6d | 2813 | static void handle_stripe_dirtying(struct r5conf *conf, |
c8ac1803 N |
2814 | struct stripe_head *sh, |
2815 | struct stripe_head_state *s, | |
2816 | int disks) | |
a4456856 DW |
2817 | { |
2818 | int rmw = 0, rcw = 0, i; | |
a7854487 AL |
2819 | sector_t recovery_cp = conf->mddev->recovery_cp; |
2820 | ||
2821 | /* RAID6 requires 'rcw' in current implementation. | |
2822 | * Otherwise, check whether resync is now happening or should start. | |
2823 | * If yes, then the array is dirty (after unclean shutdown or | |
2824 | * initial creation), so parity in some stripes might be inconsistent. | |
2825 | * In this case, we need to always do reconstruct-write, to ensure | |
2826 | * that in case of drive failure or read-error correction, we | |
2827 | * generate correct data from the parity. | |
2828 | */ | |
2829 | if (conf->max_degraded == 2 || | |
2830 | (recovery_cp < MaxSector && sh->sector >= recovery_cp)) { | |
2831 | /* Calculate the real rcw later - for now make it | |
c8ac1803 N |
2832 | * look like rcw is cheaper |
2833 | */ | |
2834 | rcw = 1; rmw = 2; | |
a7854487 AL |
2835 | pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n", |
2836 | conf->max_degraded, (unsigned long long)recovery_cp, | |
2837 | (unsigned long long)sh->sector); | |
c8ac1803 | 2838 | } else for (i = disks; i--; ) { |
a4456856 DW |
2839 | /* would I have to read this buffer for read_modify_write */ |
2840 | struct r5dev *dev = &sh->dev[i]; | |
2841 | if ((dev->towrite || i == sh->pd_idx) && | |
2842 | !test_bit(R5_LOCKED, &dev->flags) && | |
f38e1219 DW |
2843 | !(test_bit(R5_UPTODATE, &dev->flags) || |
2844 | test_bit(R5_Wantcompute, &dev->flags))) { | |
a4456856 DW |
2845 | if (test_bit(R5_Insync, &dev->flags)) |
2846 | rmw++; | |
2847 | else | |
2848 | rmw += 2*disks; /* cannot read it */ | |
2849 | } | |
2850 | /* Would I have to read this buffer for reconstruct_write */ | |
2851 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && | |
2852 | !test_bit(R5_LOCKED, &dev->flags) && | |
f38e1219 DW |
2853 | !(test_bit(R5_UPTODATE, &dev->flags) || |
2854 | test_bit(R5_Wantcompute, &dev->flags))) { | |
2855 | if (test_bit(R5_Insync, &dev->flags)) rcw++; | |
a4456856 DW |
2856 | else |
2857 | rcw += 2*disks; | |
2858 | } | |
2859 | } | |
45b4233c | 2860 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
a4456856 DW |
2861 | (unsigned long long)sh->sector, rmw, rcw); |
2862 | set_bit(STRIPE_HANDLE, &sh->state); | |
a9add5d9 | 2863 | if (rmw < rcw && rmw > 0) { |
a4456856 | 2864 | /* prefer read-modify-write, but need to get some data */ |
e3620a3a JB |
2865 | if (conf->mddev->queue) |
2866 | blk_add_trace_msg(conf->mddev->queue, | |
2867 | "raid5 rmw %llu %d", | |
2868 | (unsigned long long)sh->sector, rmw); | |
a4456856 DW |
2869 | for (i = disks; i--; ) { |
2870 | struct r5dev *dev = &sh->dev[i]; | |
2871 | if ((dev->towrite || i == sh->pd_idx) && | |
2872 | !test_bit(R5_LOCKED, &dev->flags) && | |
f38e1219 DW |
2873 | !(test_bit(R5_UPTODATE, &dev->flags) || |
2874 | test_bit(R5_Wantcompute, &dev->flags)) && | |
a4456856 DW |
2875 | test_bit(R5_Insync, &dev->flags)) { |
2876 | if ( | |
2877 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
45b4233c | 2878 | pr_debug("Read_old block " |
a9add5d9 | 2879 | "%d for r-m-w\n", i); |
a4456856 DW |
2880 | set_bit(R5_LOCKED, &dev->flags); |
2881 | set_bit(R5_Wantread, &dev->flags); | |
2882 | s->locked++; | |
2883 | } else { | |
2884 | set_bit(STRIPE_DELAYED, &sh->state); | |
2885 | set_bit(STRIPE_HANDLE, &sh->state); | |
2886 | } | |
2887 | } | |
2888 | } | |
a9add5d9 | 2889 | } |
c8ac1803 | 2890 | if (rcw <= rmw && rcw > 0) { |
a4456856 | 2891 | /* want reconstruct write, but need to get some data */ |
a9add5d9 | 2892 | int qread =0; |
c8ac1803 | 2893 | rcw = 0; |
a4456856 DW |
2894 | for (i = disks; i--; ) { |
2895 | struct r5dev *dev = &sh->dev[i]; | |
2896 | if (!test_bit(R5_OVERWRITE, &dev->flags) && | |
c8ac1803 | 2897 | i != sh->pd_idx && i != sh->qd_idx && |
a4456856 | 2898 | !test_bit(R5_LOCKED, &dev->flags) && |
f38e1219 | 2899 | !(test_bit(R5_UPTODATE, &dev->flags) || |
c8ac1803 N |
2900 | test_bit(R5_Wantcompute, &dev->flags))) { |
2901 | rcw++; | |
2902 | if (!test_bit(R5_Insync, &dev->flags)) | |
2903 | continue; /* it's a failed drive */ | |
a4456856 DW |
2904 | if ( |
2905 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
45b4233c | 2906 | pr_debug("Read_old block " |
a4456856 DW |
2907 | "%d for Reconstruct\n", i); |
2908 | set_bit(R5_LOCKED, &dev->flags); | |
2909 | set_bit(R5_Wantread, &dev->flags); | |
2910 | s->locked++; | |
a9add5d9 | 2911 | qread++; |
a4456856 DW |
2912 | } else { |
2913 | set_bit(STRIPE_DELAYED, &sh->state); | |
2914 | set_bit(STRIPE_HANDLE, &sh->state); | |
2915 | } | |
2916 | } | |
2917 | } | |
e3620a3a | 2918 | if (rcw && conf->mddev->queue) |
a9add5d9 N |
2919 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
2920 | (unsigned long long)sh->sector, | |
2921 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); | |
c8ac1803 | 2922 | } |
a4456856 DW |
2923 | /* now if nothing is locked, and if we have enough data, |
2924 | * we can start a write request | |
2925 | */ | |
f38e1219 DW |
2926 | /* since handle_stripe can be called at any time we need to handle the |
2927 | * case where a compute block operation has been submitted and then a | |
ac6b53b6 DW |
2928 | * subsequent call wants to start a write request. raid_run_ops only |
2929 | * handles the case where compute block and reconstruct are requested | |
f38e1219 DW |
2930 | * simultaneously. If this is not the case then new writes need to be |
2931 | * held off until the compute completes. | |
2932 | */ | |
976ea8d4 DW |
2933 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
2934 | (s->locked == 0 && (rcw == 0 || rmw == 0) && | |
2935 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) | |
c0f7bddb | 2936 | schedule_reconstruction(sh, s, rcw == 0, 0); |
a4456856 DW |
2937 | } |
2938 | ||
d1688a6d | 2939 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
a4456856 DW |
2940 | struct stripe_head_state *s, int disks) |
2941 | { | |
ecc65c9b | 2942 | struct r5dev *dev = NULL; |
bd2ab670 | 2943 | |
a4456856 | 2944 | set_bit(STRIPE_HANDLE, &sh->state); |
e89f8962 | 2945 | |
ecc65c9b DW |
2946 | switch (sh->check_state) { |
2947 | case check_state_idle: | |
2948 | /* start a new check operation if there are no failures */ | |
bd2ab670 | 2949 | if (s->failed == 0) { |
bd2ab670 | 2950 | BUG_ON(s->uptodate != disks); |
ecc65c9b DW |
2951 | sh->check_state = check_state_run; |
2952 | set_bit(STRIPE_OP_CHECK, &s->ops_request); | |
bd2ab670 | 2953 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
bd2ab670 | 2954 | s->uptodate--; |
ecc65c9b | 2955 | break; |
bd2ab670 | 2956 | } |
f2b3b44d | 2957 | dev = &sh->dev[s->failed_num[0]]; |
ecc65c9b DW |
2958 | /* fall through */ |
2959 | case check_state_compute_result: | |
2960 | sh->check_state = check_state_idle; | |
2961 | if (!dev) | |
2962 | dev = &sh->dev[sh->pd_idx]; | |
2963 | ||
2964 | /* check that a write has not made the stripe insync */ | |
2965 | if (test_bit(STRIPE_INSYNC, &sh->state)) | |
2966 | break; | |
c8894419 | 2967 | |
a4456856 | 2968 | /* either failed parity check, or recovery is happening */ |
a4456856 DW |
2969 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
2970 | BUG_ON(s->uptodate != disks); | |
2971 | ||
2972 | set_bit(R5_LOCKED, &dev->flags); | |
ecc65c9b | 2973 | s->locked++; |
a4456856 | 2974 | set_bit(R5_Wantwrite, &dev->flags); |
830ea016 | 2975 | |
a4456856 | 2976 | clear_bit(STRIPE_DEGRADED, &sh->state); |
a4456856 | 2977 | set_bit(STRIPE_INSYNC, &sh->state); |
ecc65c9b DW |
2978 | break; |
2979 | case check_state_run: | |
2980 | break; /* we will be called again upon completion */ | |
2981 | case check_state_check_result: | |
2982 | sh->check_state = check_state_idle; | |
2983 | ||
2984 | /* if a failure occurred during the check operation, leave | |
2985 | * STRIPE_INSYNC not set and let the stripe be handled again | |
2986 | */ | |
2987 | if (s->failed) | |
2988 | break; | |
2989 | ||
2990 | /* handle a successful check operation, if parity is correct | |
2991 | * we are done. Otherwise update the mismatch count and repair | |
2992 | * parity if !MD_RECOVERY_CHECK | |
2993 | */ | |
ad283ea4 | 2994 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
ecc65c9b DW |
2995 | /* parity is correct (on disc, |
2996 | * not in buffer any more) | |
2997 | */ | |
2998 | set_bit(STRIPE_INSYNC, &sh->state); | |
2999 | else { | |
7f7583d4 | 3000 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
ecc65c9b DW |
3001 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
3002 | /* don't try to repair!! */ | |
3003 | set_bit(STRIPE_INSYNC, &sh->state); | |
3004 | else { | |
3005 | sh->check_state = check_state_compute_run; | |
976ea8d4 | 3006 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
ecc65c9b DW |
3007 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
3008 | set_bit(R5_Wantcompute, | |
3009 | &sh->dev[sh->pd_idx].flags); | |
3010 | sh->ops.target = sh->pd_idx; | |
ac6b53b6 | 3011 | sh->ops.target2 = -1; |
ecc65c9b DW |
3012 | s->uptodate++; |
3013 | } | |
3014 | } | |
3015 | break; | |
3016 | case check_state_compute_run: | |
3017 | break; | |
3018 | default: | |
3019 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", | |
3020 | __func__, sh->check_state, | |
3021 | (unsigned long long) sh->sector); | |
3022 | BUG(); | |
a4456856 DW |
3023 | } |
3024 | } | |
3025 | ||
3026 | ||
d1688a6d | 3027 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
36d1c647 | 3028 | struct stripe_head_state *s, |
f2b3b44d | 3029 | int disks) |
a4456856 | 3030 | { |
a4456856 | 3031 | int pd_idx = sh->pd_idx; |
34e04e87 | 3032 | int qd_idx = sh->qd_idx; |
d82dfee0 | 3033 | struct r5dev *dev; |
a4456856 DW |
3034 | |
3035 | set_bit(STRIPE_HANDLE, &sh->state); | |
3036 | ||
3037 | BUG_ON(s->failed > 2); | |
d82dfee0 | 3038 | |
a4456856 DW |
3039 | /* Want to check and possibly repair P and Q. |
3040 | * However there could be one 'failed' device, in which | |
3041 | * case we can only check one of them, possibly using the | |
3042 | * other to generate missing data | |
3043 | */ | |
3044 | ||
d82dfee0 DW |
3045 | switch (sh->check_state) { |
3046 | case check_state_idle: | |
3047 | /* start a new check operation if there are < 2 failures */ | |
f2b3b44d | 3048 | if (s->failed == s->q_failed) { |
d82dfee0 | 3049 | /* The only possible failed device holds Q, so it |
a4456856 DW |
3050 | * makes sense to check P (If anything else were failed, |
3051 | * we would have used P to recreate it). | |
3052 | */ | |
d82dfee0 | 3053 | sh->check_state = check_state_run; |
a4456856 | 3054 | } |
f2b3b44d | 3055 | if (!s->q_failed && s->failed < 2) { |
d82dfee0 | 3056 | /* Q is not failed, and we didn't use it to generate |
a4456856 DW |
3057 | * anything, so it makes sense to check it |
3058 | */ | |
d82dfee0 DW |
3059 | if (sh->check_state == check_state_run) |
3060 | sh->check_state = check_state_run_pq; | |
3061 | else | |
3062 | sh->check_state = check_state_run_q; | |
a4456856 | 3063 | } |
a4456856 | 3064 | |
d82dfee0 DW |
3065 | /* discard potentially stale zero_sum_result */ |
3066 | sh->ops.zero_sum_result = 0; | |
a4456856 | 3067 | |
d82dfee0 DW |
3068 | if (sh->check_state == check_state_run) { |
3069 | /* async_xor_zero_sum destroys the contents of P */ | |
3070 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
3071 | s->uptodate--; | |
a4456856 | 3072 | } |
d82dfee0 DW |
3073 | if (sh->check_state >= check_state_run && |
3074 | sh->check_state <= check_state_run_pq) { | |
3075 | /* async_syndrome_zero_sum preserves P and Q, so | |
3076 | * no need to mark them !uptodate here | |
3077 | */ | |
3078 | set_bit(STRIPE_OP_CHECK, &s->ops_request); | |
3079 | break; | |
a4456856 DW |
3080 | } |
3081 | ||
d82dfee0 DW |
3082 | /* we have 2-disk failure */ |
3083 | BUG_ON(s->failed != 2); | |
3084 | /* fall through */ | |
3085 | case check_state_compute_result: | |
3086 | sh->check_state = check_state_idle; | |
a4456856 | 3087 | |
d82dfee0 DW |
3088 | /* check that a write has not made the stripe insync */ |
3089 | if (test_bit(STRIPE_INSYNC, &sh->state)) | |
3090 | break; | |
a4456856 DW |
3091 | |
3092 | /* now write out any block on a failed drive, | |
d82dfee0 | 3093 | * or P or Q if they were recomputed |
a4456856 | 3094 | */ |
d82dfee0 | 3095 | BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */ |
a4456856 | 3096 | if (s->failed == 2) { |
f2b3b44d | 3097 | dev = &sh->dev[s->failed_num[1]]; |
a4456856 DW |
3098 | s->locked++; |
3099 | set_bit(R5_LOCKED, &dev->flags); | |
3100 | set_bit(R5_Wantwrite, &dev->flags); | |
3101 | } | |
3102 | if (s->failed >= 1) { | |
f2b3b44d | 3103 | dev = &sh->dev[s->failed_num[0]]; |
a4456856 DW |
3104 | s->locked++; |
3105 | set_bit(R5_LOCKED, &dev->flags); | |
3106 | set_bit(R5_Wantwrite, &dev->flags); | |
3107 | } | |
d82dfee0 | 3108 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
a4456856 DW |
3109 | dev = &sh->dev[pd_idx]; |
3110 | s->locked++; | |
3111 | set_bit(R5_LOCKED, &dev->flags); | |
3112 | set_bit(R5_Wantwrite, &dev->flags); | |
3113 | } | |
d82dfee0 | 3114 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
a4456856 DW |
3115 | dev = &sh->dev[qd_idx]; |
3116 | s->locked++; | |
3117 | set_bit(R5_LOCKED, &dev->flags); | |
3118 | set_bit(R5_Wantwrite, &dev->flags); | |
3119 | } | |
3120 | clear_bit(STRIPE_DEGRADED, &sh->state); | |
3121 | ||
3122 | set_bit(STRIPE_INSYNC, &sh->state); | |
d82dfee0 DW |
3123 | break; |
3124 | case check_state_run: | |
3125 | case check_state_run_q: | |
3126 | case check_state_run_pq: | |
3127 | break; /* we will be called again upon completion */ | |
3128 | case check_state_check_result: | |
3129 | sh->check_state = check_state_idle; | |
3130 | ||
3131 | /* handle a successful check operation, if parity is correct | |
3132 | * we are done. Otherwise update the mismatch count and repair | |
3133 | * parity if !MD_RECOVERY_CHECK | |
3134 | */ | |
3135 | if (sh->ops.zero_sum_result == 0) { | |
3136 | /* both parities are correct */ | |
3137 | if (!s->failed) | |
3138 | set_bit(STRIPE_INSYNC, &sh->state); | |
3139 | else { | |
3140 | /* in contrast to the raid5 case we can validate | |
3141 | * parity, but still have a failure to write | |
3142 | * back | |
3143 | */ | |
3144 | sh->check_state = check_state_compute_result; | |
3145 | /* Returning at this point means that we may go | |
3146 | * off and bring p and/or q uptodate again so | |
3147 | * we make sure to check zero_sum_result again | |
3148 | * to verify if p or q need writeback | |
3149 | */ | |
3150 | } | |
3151 | } else { | |
7f7583d4 | 3152 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
d82dfee0 DW |
3153 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
3154 | /* don't try to repair!! */ | |
3155 | set_bit(STRIPE_INSYNC, &sh->state); | |
3156 | else { | |
3157 | int *target = &sh->ops.target; | |
3158 | ||
3159 | sh->ops.target = -1; | |
3160 | sh->ops.target2 = -1; | |
3161 | sh->check_state = check_state_compute_run; | |
3162 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
3163 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
3164 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { | |
3165 | set_bit(R5_Wantcompute, | |
3166 | &sh->dev[pd_idx].flags); | |
3167 | *target = pd_idx; | |
3168 | target = &sh->ops.target2; | |
3169 | s->uptodate++; | |
3170 | } | |
3171 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { | |
3172 | set_bit(R5_Wantcompute, | |
3173 | &sh->dev[qd_idx].flags); | |
3174 | *target = qd_idx; | |
3175 | s->uptodate++; | |
3176 | } | |
3177 | } | |
3178 | } | |
3179 | break; | |
3180 | case check_state_compute_run: | |
3181 | break; | |
3182 | default: | |
3183 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", | |
3184 | __func__, sh->check_state, | |
3185 | (unsigned long long) sh->sector); | |
3186 | BUG(); | |
a4456856 DW |
3187 | } |
3188 | } | |
3189 | ||
d1688a6d | 3190 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
a4456856 DW |
3191 | { |
3192 | int i; | |
3193 | ||
3194 | /* We have read all the blocks in this stripe and now we need to | |
3195 | * copy some of them into a target stripe for expand. | |
3196 | */ | |
f0a50d37 | 3197 | struct dma_async_tx_descriptor *tx = NULL; |
a4456856 DW |
3198 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
3199 | for (i = 0; i < sh->disks; i++) | |
34e04e87 | 3200 | if (i != sh->pd_idx && i != sh->qd_idx) { |
911d4ee8 | 3201 | int dd_idx, j; |
a4456856 | 3202 | struct stripe_head *sh2; |
a08abd8c | 3203 | struct async_submit_ctl submit; |
a4456856 | 3204 | |
784052ec | 3205 | sector_t bn = compute_blocknr(sh, i, 1); |
911d4ee8 N |
3206 | sector_t s = raid5_compute_sector(conf, bn, 0, |
3207 | &dd_idx, NULL); | |
a8c906ca | 3208 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
a4456856 DW |
3209 | if (sh2 == NULL) |
3210 | /* so far only the early blocks of this stripe | |
3211 | * have been requested. When later blocks | |
3212 | * get requested, we will try again | |
3213 | */ | |
3214 | continue; | |
3215 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || | |
3216 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { | |
3217 | /* must have already done this block */ | |
3218 | release_stripe(sh2); | |
3219 | continue; | |
3220 | } | |
f0a50d37 DW |
3221 | |
3222 | /* place all the copies on one channel */ | |
a08abd8c | 3223 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
f0a50d37 | 3224 | tx = async_memcpy(sh2->dev[dd_idx].page, |
88ba2aa5 | 3225 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
a08abd8c | 3226 | &submit); |
f0a50d37 | 3227 | |
a4456856 DW |
3228 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
3229 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); | |
3230 | for (j = 0; j < conf->raid_disks; j++) | |
3231 | if (j != sh2->pd_idx && | |
86c374ba | 3232 | j != sh2->qd_idx && |
a4456856 DW |
3233 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
3234 | break; | |
3235 | if (j == conf->raid_disks) { | |
3236 | set_bit(STRIPE_EXPAND_READY, &sh2->state); | |
3237 | set_bit(STRIPE_HANDLE, &sh2->state); | |
3238 | } | |
3239 | release_stripe(sh2); | |
f0a50d37 | 3240 | |
a4456856 | 3241 | } |
a2e08551 | 3242 | /* done submitting copies, wait for them to complete */ |
749586b7 | 3243 | async_tx_quiesce(&tx); |
a4456856 | 3244 | } |
1da177e4 LT |
3245 | |
3246 | /* | |
3247 | * handle_stripe - do things to a stripe. | |
3248 | * | |
9a3e1101 N |
3249 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
3250 | * state of various bits to see what needs to be done. | |
1da177e4 | 3251 | * Possible results: |
9a3e1101 N |
3252 | * return some read requests which now have data |
3253 | * return some write requests which are safely on storage | |
1da177e4 LT |
3254 | * schedule a read on some buffers |
3255 | * schedule a write of some buffers | |
3256 | * return confirmation of parity correctness | |
3257 | * | |
1da177e4 | 3258 | */ |
a4456856 | 3259 | |
acfe726b | 3260 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
1da177e4 | 3261 | { |
d1688a6d | 3262 | struct r5conf *conf = sh->raid_conf; |
f416885e | 3263 | int disks = sh->disks; |
474af965 N |
3264 | struct r5dev *dev; |
3265 | int i; | |
9a3e1101 | 3266 | int do_recovery = 0; |
1da177e4 | 3267 | |
acfe726b N |
3268 | memset(s, 0, sizeof(*s)); |
3269 | ||
acfe726b N |
3270 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
3271 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); | |
3272 | s->failed_num[0] = -1; | |
3273 | s->failed_num[1] = -1; | |
1da177e4 | 3274 | |
acfe726b | 3275 | /* Now to look around and see what can be done */ |
1da177e4 | 3276 | rcu_read_lock(); |
16a53ecc | 3277 | for (i=disks; i--; ) { |
3cb03002 | 3278 | struct md_rdev *rdev; |
31c176ec N |
3279 | sector_t first_bad; |
3280 | int bad_sectors; | |
3281 | int is_bad = 0; | |
acfe726b | 3282 | |
16a53ecc | 3283 | dev = &sh->dev[i]; |
1da177e4 | 3284 | |
45b4233c | 3285 | pr_debug("check %d: state 0x%lx read %p write %p written %p\n", |
9a3e1101 N |
3286 | i, dev->flags, |
3287 | dev->toread, dev->towrite, dev->written); | |
6c0069c0 YT |
3288 | /* maybe we can reply to a read |
3289 | * | |
3290 | * new wantfill requests are only permitted while | |
3291 | * ops_complete_biofill is guaranteed to be inactive | |
3292 | */ | |
3293 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && | |
3294 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) | |
3295 | set_bit(R5_Wantfill, &dev->flags); | |
1da177e4 | 3296 | |
16a53ecc | 3297 | /* now count some things */ |
cc94015a N |
3298 | if (test_bit(R5_LOCKED, &dev->flags)) |
3299 | s->locked++; | |
3300 | if (test_bit(R5_UPTODATE, &dev->flags)) | |
3301 | s->uptodate++; | |
2d6e4ecc | 3302 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
cc94015a N |
3303 | s->compute++; |
3304 | BUG_ON(s->compute > 2); | |
2d6e4ecc | 3305 | } |
1da177e4 | 3306 | |
acfe726b | 3307 | if (test_bit(R5_Wantfill, &dev->flags)) |
cc94015a | 3308 | s->to_fill++; |
acfe726b | 3309 | else if (dev->toread) |
cc94015a | 3310 | s->to_read++; |
16a53ecc | 3311 | if (dev->towrite) { |
cc94015a | 3312 | s->to_write++; |
16a53ecc | 3313 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
cc94015a | 3314 | s->non_overwrite++; |
16a53ecc | 3315 | } |
a4456856 | 3316 | if (dev->written) |
cc94015a | 3317 | s->written++; |
14a75d3e N |
3318 | /* Prefer to use the replacement for reads, but only |
3319 | * if it is recovered enough and has no bad blocks. | |
3320 | */ | |
3321 | rdev = rcu_dereference(conf->disks[i].replacement); | |
3322 | if (rdev && !test_bit(Faulty, &rdev->flags) && | |
3323 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && | |
3324 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, | |
3325 | &first_bad, &bad_sectors)) | |
3326 | set_bit(R5_ReadRepl, &dev->flags); | |
3327 | else { | |
9a3e1101 N |
3328 | if (rdev) |
3329 | set_bit(R5_NeedReplace, &dev->flags); | |
14a75d3e N |
3330 | rdev = rcu_dereference(conf->disks[i].rdev); |
3331 | clear_bit(R5_ReadRepl, &dev->flags); | |
3332 | } | |
9283d8c5 N |
3333 | if (rdev && test_bit(Faulty, &rdev->flags)) |
3334 | rdev = NULL; | |
31c176ec N |
3335 | if (rdev) { |
3336 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, | |
3337 | &first_bad, &bad_sectors); | |
3338 | if (s->blocked_rdev == NULL | |
3339 | && (test_bit(Blocked, &rdev->flags) | |
3340 | || is_bad < 0)) { | |
3341 | if (is_bad < 0) | |
3342 | set_bit(BlockedBadBlocks, | |
3343 | &rdev->flags); | |
3344 | s->blocked_rdev = rdev; | |
3345 | atomic_inc(&rdev->nr_pending); | |
3346 | } | |
6bfe0b49 | 3347 | } |
415e72d0 N |
3348 | clear_bit(R5_Insync, &dev->flags); |
3349 | if (!rdev) | |
3350 | /* Not in-sync */; | |
31c176ec N |
3351 | else if (is_bad) { |
3352 | /* also not in-sync */ | |
18b9837e N |
3353 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
3354 | test_bit(R5_UPTODATE, &dev->flags)) { | |
31c176ec N |
3355 | /* treat as in-sync, but with a read error |
3356 | * which we can now try to correct | |
3357 | */ | |
3358 | set_bit(R5_Insync, &dev->flags); | |
3359 | set_bit(R5_ReadError, &dev->flags); | |
3360 | } | |
3361 | } else if (test_bit(In_sync, &rdev->flags)) | |
415e72d0 | 3362 | set_bit(R5_Insync, &dev->flags); |
30d7a483 | 3363 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
415e72d0 | 3364 | /* in sync if before recovery_offset */ |
30d7a483 N |
3365 | set_bit(R5_Insync, &dev->flags); |
3366 | else if (test_bit(R5_UPTODATE, &dev->flags) && | |
3367 | test_bit(R5_Expanded, &dev->flags)) | |
3368 | /* If we've reshaped into here, we assume it is Insync. | |
3369 | * We will shortly update recovery_offset to make | |
3370 | * it official. | |
3371 | */ | |
3372 | set_bit(R5_Insync, &dev->flags); | |
3373 | ||
5d8c71f9 | 3374 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
14a75d3e N |
3375 | /* This flag does not apply to '.replacement' |
3376 | * only to .rdev, so make sure to check that*/ | |
3377 | struct md_rdev *rdev2 = rcu_dereference( | |
3378 | conf->disks[i].rdev); | |
3379 | if (rdev2 == rdev) | |
3380 | clear_bit(R5_Insync, &dev->flags); | |
3381 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { | |
bc2607f3 | 3382 | s->handle_bad_blocks = 1; |
14a75d3e | 3383 | atomic_inc(&rdev2->nr_pending); |
bc2607f3 N |
3384 | } else |
3385 | clear_bit(R5_WriteError, &dev->flags); | |
3386 | } | |
5d8c71f9 | 3387 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
14a75d3e N |
3388 | /* This flag does not apply to '.replacement' |
3389 | * only to .rdev, so make sure to check that*/ | |
3390 | struct md_rdev *rdev2 = rcu_dereference( | |
3391 | conf->disks[i].rdev); | |
3392 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { | |
b84db560 | 3393 | s->handle_bad_blocks = 1; |
14a75d3e | 3394 | atomic_inc(&rdev2->nr_pending); |
b84db560 N |
3395 | } else |
3396 | clear_bit(R5_MadeGood, &dev->flags); | |
3397 | } | |
977df362 N |
3398 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
3399 | struct md_rdev *rdev2 = rcu_dereference( | |
3400 | conf->disks[i].replacement); | |
3401 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { | |
3402 | s->handle_bad_blocks = 1; | |
3403 | atomic_inc(&rdev2->nr_pending); | |
3404 | } else | |
3405 | clear_bit(R5_MadeGoodRepl, &dev->flags); | |
3406 | } | |
415e72d0 | 3407 | if (!test_bit(R5_Insync, &dev->flags)) { |
16a53ecc N |
3408 | /* The ReadError flag will just be confusing now */ |
3409 | clear_bit(R5_ReadError, &dev->flags); | |
3410 | clear_bit(R5_ReWrite, &dev->flags); | |
1da177e4 | 3411 | } |
415e72d0 N |
3412 | if (test_bit(R5_ReadError, &dev->flags)) |
3413 | clear_bit(R5_Insync, &dev->flags); | |
3414 | if (!test_bit(R5_Insync, &dev->flags)) { | |
cc94015a N |
3415 | if (s->failed < 2) |
3416 | s->failed_num[s->failed] = i; | |
3417 | s->failed++; | |
9a3e1101 N |
3418 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
3419 | do_recovery = 1; | |
415e72d0 | 3420 | } |
1da177e4 | 3421 | } |
9a3e1101 N |
3422 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
3423 | /* If there is a failed device being replaced, | |
3424 | * we must be recovering. | |
3425 | * else if we are after recovery_cp, we must be syncing | |
c6d2e084 | 3426 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
9a3e1101 N |
3427 | * else we can only be replacing |
3428 | * sync and recovery both need to read all devices, and so | |
3429 | * use the same flag. | |
3430 | */ | |
3431 | if (do_recovery || | |
c6d2e084 | 3432 | sh->sector >= conf->mddev->recovery_cp || |
3433 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) | |
9a3e1101 N |
3434 | s->syncing = 1; |
3435 | else | |
3436 | s->replacing = 1; | |
3437 | } | |
1da177e4 | 3438 | rcu_read_unlock(); |
cc94015a N |
3439 | } |
3440 | ||
3441 | static void handle_stripe(struct stripe_head *sh) | |
3442 | { | |
3443 | struct stripe_head_state s; | |
d1688a6d | 3444 | struct r5conf *conf = sh->raid_conf; |
3687c061 | 3445 | int i; |
84789554 N |
3446 | int prexor; |
3447 | int disks = sh->disks; | |
474af965 | 3448 | struct r5dev *pdev, *qdev; |
cc94015a N |
3449 | |
3450 | clear_bit(STRIPE_HANDLE, &sh->state); | |
257a4b42 | 3451 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
cc94015a N |
3452 | /* already being handled, ensure it gets handled |
3453 | * again when current action finishes */ | |
3454 | set_bit(STRIPE_HANDLE, &sh->state); | |
3455 | return; | |
3456 | } | |
3457 | ||
f8dfcffd N |
3458 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
3459 | spin_lock(&sh->stripe_lock); | |
3460 | /* Cannot process 'sync' concurrently with 'discard' */ | |
3461 | if (!test_bit(STRIPE_DISCARD, &sh->state) && | |
3462 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { | |
3463 | set_bit(STRIPE_SYNCING, &sh->state); | |
3464 | clear_bit(STRIPE_INSYNC, &sh->state); | |
f94c0b66 | 3465 | clear_bit(STRIPE_REPLACED, &sh->state); |
f8dfcffd N |
3466 | } |
3467 | spin_unlock(&sh->stripe_lock); | |
cc94015a N |
3468 | } |
3469 | clear_bit(STRIPE_DELAYED, &sh->state); | |
3470 | ||
3471 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " | |
3472 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", | |
3473 | (unsigned long long)sh->sector, sh->state, | |
3474 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, | |
3475 | sh->check_state, sh->reconstruct_state); | |
3687c061 | 3476 | |
acfe726b | 3477 | analyse_stripe(sh, &s); |
c5a31000 | 3478 | |
bc2607f3 N |
3479 | if (s.handle_bad_blocks) { |
3480 | set_bit(STRIPE_HANDLE, &sh->state); | |
3481 | goto finish; | |
3482 | } | |
3483 | ||
474af965 N |
3484 | if (unlikely(s.blocked_rdev)) { |
3485 | if (s.syncing || s.expanding || s.expanded || | |
9a3e1101 | 3486 | s.replacing || s.to_write || s.written) { |
474af965 N |
3487 | set_bit(STRIPE_HANDLE, &sh->state); |
3488 | goto finish; | |
3489 | } | |
3490 | /* There is nothing for the blocked_rdev to block */ | |
3491 | rdev_dec_pending(s.blocked_rdev, conf->mddev); | |
3492 | s.blocked_rdev = NULL; | |
3493 | } | |
3494 | ||
3495 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { | |
3496 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); | |
3497 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); | |
3498 | } | |
3499 | ||
3500 | pr_debug("locked=%d uptodate=%d to_read=%d" | |
3501 | " to_write=%d failed=%d failed_num=%d,%d\n", | |
3502 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, | |
3503 | s.failed_num[0], s.failed_num[1]); | |
3504 | /* check if the array has lost more than max_degraded devices and, | |
3505 | * if so, some requests might need to be failed. | |
3506 | */ | |
9a3f530f N |
3507 | if (s.failed > conf->max_degraded) { |
3508 | sh->check_state = 0; | |
3509 | sh->reconstruct_state = 0; | |
3510 | if (s.to_read+s.to_write+s.written) | |
3511 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); | |
9a3e1101 | 3512 | if (s.syncing + s.replacing) |
9a3f530f N |
3513 | handle_failed_sync(conf, sh, &s); |
3514 | } | |
474af965 | 3515 | |
84789554 N |
3516 | /* Now we check to see if any write operations have recently |
3517 | * completed | |
3518 | */ | |
3519 | prexor = 0; | |
3520 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) | |
3521 | prexor = 1; | |
3522 | if (sh->reconstruct_state == reconstruct_state_drain_result || | |
3523 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { | |
3524 | sh->reconstruct_state = reconstruct_state_idle; | |
3525 | ||
3526 | /* All the 'written' buffers and the parity block are ready to | |
3527 | * be written back to disk | |
3528 | */ | |
9e444768 SL |
3529 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
3530 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); | |
84789554 | 3531 | BUG_ON(sh->qd_idx >= 0 && |
9e444768 SL |
3532 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
3533 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); | |
84789554 N |
3534 | for (i = disks; i--; ) { |
3535 | struct r5dev *dev = &sh->dev[i]; | |
3536 | if (test_bit(R5_LOCKED, &dev->flags) && | |
3537 | (i == sh->pd_idx || i == sh->qd_idx || | |
3538 | dev->written)) { | |
3539 | pr_debug("Writing block %d\n", i); | |
3540 | set_bit(R5_Wantwrite, &dev->flags); | |
3541 | if (prexor) | |
3542 | continue; | |
3543 | if (!test_bit(R5_Insync, &dev->flags) || | |
3544 | ((i == sh->pd_idx || i == sh->qd_idx) && | |
3545 | s.failed == 0)) | |
3546 | set_bit(STRIPE_INSYNC, &sh->state); | |
3547 | } | |
3548 | } | |
3549 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
3550 | s.dec_preread_active = 1; | |
3551 | } | |
3552 | ||
ef5b7c69 N |
3553 | /* |
3554 | * might be able to return some write requests if the parity blocks | |
3555 | * are safe, or on a failed drive | |
3556 | */ | |
3557 | pdev = &sh->dev[sh->pd_idx]; | |
3558 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) | |
3559 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); | |
3560 | qdev = &sh->dev[sh->qd_idx]; | |
3561 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) | |
3562 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) | |
3563 | || conf->level < 6; | |
3564 | ||
3565 | if (s.written && | |
3566 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) | |
3567 | && !test_bit(R5_LOCKED, &pdev->flags) | |
3568 | && (test_bit(R5_UPTODATE, &pdev->flags) || | |
3569 | test_bit(R5_Discard, &pdev->flags))))) && | |
3570 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) | |
3571 | && !test_bit(R5_LOCKED, &qdev->flags) | |
3572 | && (test_bit(R5_UPTODATE, &qdev->flags) || | |
3573 | test_bit(R5_Discard, &qdev->flags)))))) | |
3574 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); | |
3575 | ||
3576 | /* Now we might consider reading some blocks, either to check/generate | |
3577 | * parity, or to satisfy requests | |
3578 | * or to load a block that is being partially written. | |
3579 | */ | |
3580 | if (s.to_read || s.non_overwrite | |
3581 | || (conf->level == 6 && s.to_write && s.failed) | |
3582 | || (s.syncing && (s.uptodate + s.compute < disks)) | |
3583 | || s.replacing | |
3584 | || s.expanding) | |
3585 | handle_stripe_fill(sh, &s, disks); | |
3586 | ||
84789554 N |
3587 | /* Now to consider new write requests and what else, if anything |
3588 | * should be read. We do not handle new writes when: | |
3589 | * 1/ A 'write' operation (copy+xor) is already in flight. | |
3590 | * 2/ A 'check' operation is in flight, as it may clobber the parity | |
3591 | * block. | |
3592 | */ | |
3593 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) | |
3594 | handle_stripe_dirtying(conf, sh, &s, disks); | |
3595 | ||
3596 | /* maybe we need to check and possibly fix the parity for this stripe | |
3597 | * Any reads will already have been scheduled, so we just see if enough | |
3598 | * data is available. The parity check is held off while parity | |
3599 | * dependent operations are in flight. | |
3600 | */ | |
3601 | if (sh->check_state || | |
3602 | (s.syncing && s.locked == 0 && | |
3603 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && | |
3604 | !test_bit(STRIPE_INSYNC, &sh->state))) { | |
3605 | if (conf->level == 6) | |
3606 | handle_parity_checks6(conf, sh, &s, disks); | |
3607 | else | |
3608 | handle_parity_checks5(conf, sh, &s, disks); | |
3609 | } | |
c5a31000 | 3610 | |
f94c0b66 N |
3611 | if ((s.replacing || s.syncing) && s.locked == 0 |
3612 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) | |
3613 | && !test_bit(STRIPE_REPLACED, &sh->state)) { | |
9a3e1101 N |
3614 | /* Write out to replacement devices where possible */ |
3615 | for (i = 0; i < conf->raid_disks; i++) | |
f94c0b66 N |
3616 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
3617 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
9a3e1101 N |
3618 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
3619 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
3620 | s.locked++; | |
3621 | } | |
f94c0b66 N |
3622 | if (s.replacing) |
3623 | set_bit(STRIPE_INSYNC, &sh->state); | |
3624 | set_bit(STRIPE_REPLACED, &sh->state); | |
9a3e1101 N |
3625 | } |
3626 | if ((s.syncing || s.replacing) && s.locked == 0 && | |
f94c0b66 | 3627 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
9a3e1101 | 3628 | test_bit(STRIPE_INSYNC, &sh->state)) { |
c5a31000 N |
3629 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
3630 | clear_bit(STRIPE_SYNCING, &sh->state); | |
f8dfcffd N |
3631 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
3632 | wake_up(&conf->wait_for_overlap); | |
c5a31000 N |
3633 | } |
3634 | ||
3635 | /* If the failed drives are just a ReadError, then we might need | |
3636 | * to progress the repair/check process | |
3637 | */ | |
3638 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) | |
3639 | for (i = 0; i < s.failed; i++) { | |
3640 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; | |
3641 | if (test_bit(R5_ReadError, &dev->flags) | |
3642 | && !test_bit(R5_LOCKED, &dev->flags) | |
3643 | && test_bit(R5_UPTODATE, &dev->flags) | |
3644 | ) { | |
3645 | if (!test_bit(R5_ReWrite, &dev->flags)) { | |
3646 | set_bit(R5_Wantwrite, &dev->flags); | |
3647 | set_bit(R5_ReWrite, &dev->flags); | |
3648 | set_bit(R5_LOCKED, &dev->flags); | |
3649 | s.locked++; | |
3650 | } else { | |
3651 | /* let's read it back */ | |
3652 | set_bit(R5_Wantread, &dev->flags); | |
3653 | set_bit(R5_LOCKED, &dev->flags); | |
3654 | s.locked++; | |
3655 | } | |
3656 | } | |
3657 | } | |
3658 | ||
3659 | ||
3687c061 N |
3660 | /* Finish reconstruct operations initiated by the expansion process */ |
3661 | if (sh->reconstruct_state == reconstruct_state_result) { | |
3662 | struct stripe_head *sh_src | |
3663 | = get_active_stripe(conf, sh->sector, 1, 1, 1); | |
3664 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { | |
3665 | /* sh cannot be written until sh_src has been read. | |
3666 | * so arrange for sh to be delayed a little | |
3667 | */ | |
3668 | set_bit(STRIPE_DELAYED, &sh->state); | |
3669 | set_bit(STRIPE_HANDLE, &sh->state); | |
3670 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, | |
3671 | &sh_src->state)) | |
3672 | atomic_inc(&conf->preread_active_stripes); | |
3673 | release_stripe(sh_src); | |
3674 | goto finish; | |
3675 | } | |
3676 | if (sh_src) | |
3677 | release_stripe(sh_src); | |
3678 | ||
3679 | sh->reconstruct_state = reconstruct_state_idle; | |
3680 | clear_bit(STRIPE_EXPANDING, &sh->state); | |
3681 | for (i = conf->raid_disks; i--; ) { | |
3682 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
3683 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
3684 | s.locked++; | |
3685 | } | |
3686 | } | |
f416885e | 3687 | |
3687c061 N |
3688 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
3689 | !sh->reconstruct_state) { | |
3690 | /* Need to write out all blocks after computing parity */ | |
3691 | sh->disks = conf->raid_disks; | |
3692 | stripe_set_idx(sh->sector, conf, 0, sh); | |
3693 | schedule_reconstruction(sh, &s, 1, 1); | |
3694 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { | |
3695 | clear_bit(STRIPE_EXPAND_READY, &sh->state); | |
3696 | atomic_dec(&conf->reshape_stripes); | |
3697 | wake_up(&conf->wait_for_overlap); | |
3698 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); | |
3699 | } | |
3700 | ||
3701 | if (s.expanding && s.locked == 0 && | |
3702 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) | |
3703 | handle_stripe_expansion(conf, sh); | |
16a53ecc | 3704 | |
3687c061 | 3705 | finish: |
6bfe0b49 | 3706 | /* wait for this device to become unblocked */ |
5f066c63 N |
3707 | if (unlikely(s.blocked_rdev)) { |
3708 | if (conf->mddev->external) | |
3709 | md_wait_for_blocked_rdev(s.blocked_rdev, | |
3710 | conf->mddev); | |
3711 | else | |
3712 | /* Internal metadata will immediately | |
3713 | * be written by raid5d, so we don't | |
3714 | * need to wait here. | |
3715 | */ | |
3716 | rdev_dec_pending(s.blocked_rdev, | |
3717 | conf->mddev); | |
3718 | } | |
6bfe0b49 | 3719 | |
bc2607f3 N |
3720 | if (s.handle_bad_blocks) |
3721 | for (i = disks; i--; ) { | |
3cb03002 | 3722 | struct md_rdev *rdev; |
bc2607f3 N |
3723 | struct r5dev *dev = &sh->dev[i]; |
3724 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { | |
3725 | /* We own a safe reference to the rdev */ | |
3726 | rdev = conf->disks[i].rdev; | |
3727 | if (!rdev_set_badblocks(rdev, sh->sector, | |
3728 | STRIPE_SECTORS, 0)) | |
3729 | md_error(conf->mddev, rdev); | |
3730 | rdev_dec_pending(rdev, conf->mddev); | |
3731 | } | |
b84db560 N |
3732 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
3733 | rdev = conf->disks[i].rdev; | |
3734 | rdev_clear_badblocks(rdev, sh->sector, | |
c6563a8c | 3735 | STRIPE_SECTORS, 0); |
b84db560 N |
3736 | rdev_dec_pending(rdev, conf->mddev); |
3737 | } | |
977df362 N |
3738 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
3739 | rdev = conf->disks[i].replacement; | |
dd054fce N |
3740 | if (!rdev) |
3741 | /* rdev have been moved down */ | |
3742 | rdev = conf->disks[i].rdev; | |
977df362 | 3743 | rdev_clear_badblocks(rdev, sh->sector, |
c6563a8c | 3744 | STRIPE_SECTORS, 0); |
977df362 N |
3745 | rdev_dec_pending(rdev, conf->mddev); |
3746 | } | |
bc2607f3 N |
3747 | } |
3748 | ||
6c0069c0 YT |
3749 | if (s.ops_request) |
3750 | raid_run_ops(sh, s.ops_request); | |
3751 | ||
f0e43bcd | 3752 | ops_run_io(sh, &s); |
16a53ecc | 3753 | |
c5709ef6 | 3754 | if (s.dec_preread_active) { |
729a1866 | 3755 | /* We delay this until after ops_run_io so that if make_request |
e9c7469b | 3756 | * is waiting on a flush, it won't continue until the writes |
729a1866 N |
3757 | * have actually been submitted. |
3758 | */ | |
3759 | atomic_dec(&conf->preread_active_stripes); | |
3760 | if (atomic_read(&conf->preread_active_stripes) < | |
3761 | IO_THRESHOLD) | |
3762 | md_wakeup_thread(conf->mddev->thread); | |
3763 | } | |
3764 | ||
c5709ef6 | 3765 | return_io(s.return_bi); |
16a53ecc | 3766 | |
257a4b42 | 3767 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
16a53ecc N |
3768 | } |
3769 | ||
d1688a6d | 3770 | static void raid5_activate_delayed(struct r5conf *conf) |
16a53ecc N |
3771 | { |
3772 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { | |
3773 | while (!list_empty(&conf->delayed_list)) { | |
3774 | struct list_head *l = conf->delayed_list.next; | |
3775 | struct stripe_head *sh; | |
3776 | sh = list_entry(l, struct stripe_head, lru); | |
3777 | list_del_init(l); | |
3778 | clear_bit(STRIPE_DELAYED, &sh->state); | |
3779 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
3780 | atomic_inc(&conf->preread_active_stripes); | |
8b3e6cdc | 3781 | list_add_tail(&sh->lru, &conf->hold_list); |
16a53ecc | 3782 | } |
482c0834 | 3783 | } |
16a53ecc N |
3784 | } |
3785 | ||
d1688a6d | 3786 | static void activate_bit_delay(struct r5conf *conf) |
16a53ecc N |
3787 | { |
3788 | /* device_lock is held */ | |
3789 | struct list_head head; | |
3790 | list_add(&head, &conf->bitmap_list); | |
3791 | list_del_init(&conf->bitmap_list); | |
3792 | while (!list_empty(&head)) { | |
3793 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); | |
3794 | list_del_init(&sh->lru); | |
3795 | atomic_inc(&sh->count); | |
3796 | __release_stripe(conf, sh); | |
3797 | } | |
3798 | } | |
3799 | ||
fd01b88c | 3800 | int md_raid5_congested(struct mddev *mddev, int bits) |
f022b2fd | 3801 | { |
d1688a6d | 3802 | struct r5conf *conf = mddev->private; |
f022b2fd N |
3803 | |
3804 | /* No difference between reads and writes. Just check | |
3805 | * how busy the stripe_cache is | |
3806 | */ | |
3fa841d7 | 3807 | |
f022b2fd N |
3808 | if (conf->inactive_blocked) |
3809 | return 1; | |
3810 | if (conf->quiesce) | |
3811 | return 1; | |
3812 | if (list_empty_careful(&conf->inactive_list)) | |
3813 | return 1; | |
3814 | ||
3815 | return 0; | |
3816 | } | |
11d8a6e3 N |
3817 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
3818 | ||
3819 | static int raid5_congested(void *data, int bits) | |
3820 | { | |
fd01b88c | 3821 | struct mddev *mddev = data; |
11d8a6e3 N |
3822 | |
3823 | return mddev_congested(mddev, bits) || | |
3824 | md_raid5_congested(mddev, bits); | |
3825 | } | |
f022b2fd | 3826 | |
23032a0e RBJ |
3827 | /* We want read requests to align with chunks where possible, |
3828 | * but write requests don't need to. | |
3829 | */ | |
cc371e66 AK |
3830 | static int raid5_mergeable_bvec(struct request_queue *q, |
3831 | struct bvec_merge_data *bvm, | |
3832 | struct bio_vec *biovec) | |
23032a0e | 3833 | { |
fd01b88c | 3834 | struct mddev *mddev = q->queuedata; |
cc371e66 | 3835 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); |
23032a0e | 3836 | int max; |
9d8f0363 | 3837 | unsigned int chunk_sectors = mddev->chunk_sectors; |
cc371e66 | 3838 | unsigned int bio_sectors = bvm->bi_size >> 9; |
23032a0e | 3839 | |
cc371e66 | 3840 | if ((bvm->bi_rw & 1) == WRITE) |
23032a0e RBJ |
3841 | return biovec->bv_len; /* always allow writes to be mergeable */ |
3842 | ||
664e7c41 AN |
3843 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
3844 | chunk_sectors = mddev->new_chunk_sectors; | |
23032a0e RBJ |
3845 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
3846 | if (max < 0) max = 0; | |
3847 | if (max <= biovec->bv_len && bio_sectors == 0) | |
3848 | return biovec->bv_len; | |
3849 | else | |
3850 | return max; | |
3851 | } | |
3852 | ||
f679623f | 3853 | |
fd01b88c | 3854 | static int in_chunk_boundary(struct mddev *mddev, struct bio *bio) |
f679623f RBJ |
3855 | { |
3856 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); | |
9d8f0363 | 3857 | unsigned int chunk_sectors = mddev->chunk_sectors; |
aa8b57aa | 3858 | unsigned int bio_sectors = bio_sectors(bio); |
f679623f | 3859 | |
664e7c41 AN |
3860 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
3861 | chunk_sectors = mddev->new_chunk_sectors; | |
f679623f RBJ |
3862 | return chunk_sectors >= |
3863 | ((sector & (chunk_sectors - 1)) + bio_sectors); | |
3864 | } | |
3865 | ||
46031f9a RBJ |
3866 | /* |
3867 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) | |
3868 | * later sampled by raid5d. | |
3869 | */ | |
d1688a6d | 3870 | static void add_bio_to_retry(struct bio *bi,struct r5conf *conf) |
46031f9a RBJ |
3871 | { |
3872 | unsigned long flags; | |
3873 | ||
3874 | spin_lock_irqsave(&conf->device_lock, flags); | |
3875 | ||
3876 | bi->bi_next = conf->retry_read_aligned_list; | |
3877 | conf->retry_read_aligned_list = bi; | |
3878 | ||
3879 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
3880 | md_wakeup_thread(conf->mddev->thread); | |
3881 | } | |
3882 | ||
3883 | ||
d1688a6d | 3884 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
46031f9a RBJ |
3885 | { |
3886 | struct bio *bi; | |
3887 | ||
3888 | bi = conf->retry_read_aligned; | |
3889 | if (bi) { | |
3890 | conf->retry_read_aligned = NULL; | |
3891 | return bi; | |
3892 | } | |
3893 | bi = conf->retry_read_aligned_list; | |
3894 | if(bi) { | |
387bb173 | 3895 | conf->retry_read_aligned_list = bi->bi_next; |
46031f9a | 3896 | bi->bi_next = NULL; |
960e739d JA |
3897 | /* |
3898 | * this sets the active strip count to 1 and the processed | |
3899 | * strip count to zero (upper 8 bits) | |
3900 | */ | |
e7836bd6 | 3901 | raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */ |
46031f9a RBJ |
3902 | } |
3903 | ||
3904 | return bi; | |
3905 | } | |
3906 | ||
3907 | ||
f679623f RBJ |
3908 | /* |
3909 | * The "raid5_align_endio" should check if the read succeeded and if it | |
3910 | * did, call bio_endio on the original bio (having bio_put the new bio | |
3911 | * first). | |
3912 | * If the read failed.. | |
3913 | */ | |
6712ecf8 | 3914 | static void raid5_align_endio(struct bio *bi, int error) |
f679623f RBJ |
3915 | { |
3916 | struct bio* raid_bi = bi->bi_private; | |
fd01b88c | 3917 | struct mddev *mddev; |
d1688a6d | 3918 | struct r5conf *conf; |
46031f9a | 3919 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
3cb03002 | 3920 | struct md_rdev *rdev; |
46031f9a | 3921 | |
f679623f | 3922 | bio_put(bi); |
46031f9a | 3923 | |
46031f9a RBJ |
3924 | rdev = (void*)raid_bi->bi_next; |
3925 | raid_bi->bi_next = NULL; | |
2b7f2228 N |
3926 | mddev = rdev->mddev; |
3927 | conf = mddev->private; | |
46031f9a RBJ |
3928 | |
3929 | rdev_dec_pending(rdev, conf->mddev); | |
3930 | ||
3931 | if (!error && uptodate) { | |
0a82a8d1 LT |
3932 | trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev), |
3933 | raid_bi, 0); | |
6712ecf8 | 3934 | bio_endio(raid_bi, 0); |
46031f9a RBJ |
3935 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
3936 | wake_up(&conf->wait_for_stripe); | |
6712ecf8 | 3937 | return; |
46031f9a RBJ |
3938 | } |
3939 | ||
3940 | ||
45b4233c | 3941 | pr_debug("raid5_align_endio : io error...handing IO for a retry\n"); |
46031f9a RBJ |
3942 | |
3943 | add_bio_to_retry(raid_bi, conf); | |
f679623f RBJ |
3944 | } |
3945 | ||
387bb173 NB |
3946 | static int bio_fits_rdev(struct bio *bi) |
3947 | { | |
165125e1 | 3948 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
387bb173 | 3949 | |
aa8b57aa | 3950 | if (bio_sectors(bi) > queue_max_sectors(q)) |
387bb173 NB |
3951 | return 0; |
3952 | blk_recount_segments(q, bi); | |
8a78362c | 3953 | if (bi->bi_phys_segments > queue_max_segments(q)) |
387bb173 NB |
3954 | return 0; |
3955 | ||
3956 | if (q->merge_bvec_fn) | |
3957 | /* it's too hard to apply the merge_bvec_fn at this stage, | |
3958 | * just just give up | |
3959 | */ | |
3960 | return 0; | |
3961 | ||
3962 | return 1; | |
3963 | } | |
3964 | ||
3965 | ||
fd01b88c | 3966 | static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio) |
f679623f | 3967 | { |
d1688a6d | 3968 | struct r5conf *conf = mddev->private; |
8553fe7e | 3969 | int dd_idx; |
f679623f | 3970 | struct bio* align_bi; |
3cb03002 | 3971 | struct md_rdev *rdev; |
671488cc | 3972 | sector_t end_sector; |
f679623f RBJ |
3973 | |
3974 | if (!in_chunk_boundary(mddev, raid_bio)) { | |
45b4233c | 3975 | pr_debug("chunk_aligned_read : non aligned\n"); |
f679623f RBJ |
3976 | return 0; |
3977 | } | |
3978 | /* | |
a167f663 | 3979 | * use bio_clone_mddev to make a copy of the bio |
f679623f | 3980 | */ |
a167f663 | 3981 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
f679623f RBJ |
3982 | if (!align_bi) |
3983 | return 0; | |
3984 | /* | |
3985 | * set bi_end_io to a new function, and set bi_private to the | |
3986 | * original bio. | |
3987 | */ | |
3988 | align_bi->bi_end_io = raid5_align_endio; | |
3989 | align_bi->bi_private = raid_bio; | |
3990 | /* | |
3991 | * compute position | |
3992 | */ | |
112bf897 N |
3993 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
3994 | 0, | |
911d4ee8 | 3995 | &dd_idx, NULL); |
f679623f | 3996 | |
f73a1c7d | 3997 | end_sector = bio_end_sector(align_bi); |
f679623f | 3998 | rcu_read_lock(); |
671488cc N |
3999 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
4000 | if (!rdev || test_bit(Faulty, &rdev->flags) || | |
4001 | rdev->recovery_offset < end_sector) { | |
4002 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); | |
4003 | if (rdev && | |
4004 | (test_bit(Faulty, &rdev->flags) || | |
4005 | !(test_bit(In_sync, &rdev->flags) || | |
4006 | rdev->recovery_offset >= end_sector))) | |
4007 | rdev = NULL; | |
4008 | } | |
4009 | if (rdev) { | |
31c176ec N |
4010 | sector_t first_bad; |
4011 | int bad_sectors; | |
4012 | ||
f679623f RBJ |
4013 | atomic_inc(&rdev->nr_pending); |
4014 | rcu_read_unlock(); | |
46031f9a RBJ |
4015 | raid_bio->bi_next = (void*)rdev; |
4016 | align_bi->bi_bdev = rdev->bdev; | |
4017 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); | |
46031f9a | 4018 | |
31c176ec | 4019 | if (!bio_fits_rdev(align_bi) || |
aa8b57aa | 4020 | is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi), |
31c176ec N |
4021 | &first_bad, &bad_sectors)) { |
4022 | /* too big in some way, or has a known bad block */ | |
387bb173 NB |
4023 | bio_put(align_bi); |
4024 | rdev_dec_pending(rdev, mddev); | |
4025 | return 0; | |
4026 | } | |
4027 | ||
6c0544e2 | 4028 | /* No reshape active, so we can trust rdev->data_offset */ |
4029 | align_bi->bi_sector += rdev->data_offset; | |
4030 | ||
46031f9a RBJ |
4031 | spin_lock_irq(&conf->device_lock); |
4032 | wait_event_lock_irq(conf->wait_for_stripe, | |
4033 | conf->quiesce == 0, | |
eed8c02e | 4034 | conf->device_lock); |
46031f9a RBJ |
4035 | atomic_inc(&conf->active_aligned_reads); |
4036 | spin_unlock_irq(&conf->device_lock); | |
4037 | ||
e3620a3a JB |
4038 | if (mddev->gendisk) |
4039 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), | |
4040 | align_bi, disk_devt(mddev->gendisk), | |
4041 | raid_bio->bi_sector); | |
f679623f RBJ |
4042 | generic_make_request(align_bi); |
4043 | return 1; | |
4044 | } else { | |
4045 | rcu_read_unlock(); | |
46031f9a | 4046 | bio_put(align_bi); |
f679623f RBJ |
4047 | return 0; |
4048 | } | |
4049 | } | |
4050 | ||
8b3e6cdc DW |
4051 | /* __get_priority_stripe - get the next stripe to process |
4052 | * | |
4053 | * Full stripe writes are allowed to pass preread active stripes up until | |
4054 | * the bypass_threshold is exceeded. In general the bypass_count | |
4055 | * increments when the handle_list is handled before the hold_list; however, it | |
4056 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a | |
4057 | * stripe with in flight i/o. The bypass_count will be reset when the | |
4058 | * head of the hold_list has changed, i.e. the head was promoted to the | |
4059 | * handle_list. | |
4060 | */ | |
d1688a6d | 4061 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
8b3e6cdc DW |
4062 | { |
4063 | struct stripe_head *sh; | |
4064 | ||
4065 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", | |
4066 | __func__, | |
4067 | list_empty(&conf->handle_list) ? "empty" : "busy", | |
4068 | list_empty(&conf->hold_list) ? "empty" : "busy", | |
4069 | atomic_read(&conf->pending_full_writes), conf->bypass_count); | |
4070 | ||
4071 | if (!list_empty(&conf->handle_list)) { | |
4072 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); | |
4073 | ||
4074 | if (list_empty(&conf->hold_list)) | |
4075 | conf->bypass_count = 0; | |
4076 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { | |
4077 | if (conf->hold_list.next == conf->last_hold) | |
4078 | conf->bypass_count++; | |
4079 | else { | |
4080 | conf->last_hold = conf->hold_list.next; | |
4081 | conf->bypass_count -= conf->bypass_threshold; | |
4082 | if (conf->bypass_count < 0) | |
4083 | conf->bypass_count = 0; | |
4084 | } | |
4085 | } | |
4086 | } else if (!list_empty(&conf->hold_list) && | |
4087 | ((conf->bypass_threshold && | |
4088 | conf->bypass_count > conf->bypass_threshold) || | |
4089 | atomic_read(&conf->pending_full_writes) == 0)) { | |
4090 | sh = list_entry(conf->hold_list.next, | |
4091 | typeof(*sh), lru); | |
4092 | conf->bypass_count -= conf->bypass_threshold; | |
4093 | if (conf->bypass_count < 0) | |
4094 | conf->bypass_count = 0; | |
4095 | } else | |
4096 | return NULL; | |
4097 | ||
4098 | list_del_init(&sh->lru); | |
4099 | atomic_inc(&sh->count); | |
4100 | BUG_ON(atomic_read(&sh->count) != 1); | |
4101 | return sh; | |
4102 | } | |
f679623f | 4103 | |
8811b596 SL |
4104 | struct raid5_plug_cb { |
4105 | struct blk_plug_cb cb; | |
4106 | struct list_head list; | |
4107 | }; | |
4108 | ||
4109 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) | |
4110 | { | |
4111 | struct raid5_plug_cb *cb = container_of( | |
4112 | blk_cb, struct raid5_plug_cb, cb); | |
4113 | struct stripe_head *sh; | |
4114 | struct mddev *mddev = cb->cb.data; | |
4115 | struct r5conf *conf = mddev->private; | |
a9add5d9 | 4116 | int cnt = 0; |
8811b596 SL |
4117 | |
4118 | if (cb->list.next && !list_empty(&cb->list)) { | |
4119 | spin_lock_irq(&conf->device_lock); | |
4120 | while (!list_empty(&cb->list)) { | |
4121 | sh = list_first_entry(&cb->list, struct stripe_head, lru); | |
4122 | list_del_init(&sh->lru); | |
4123 | /* | |
4124 | * avoid race release_stripe_plug() sees | |
4125 | * STRIPE_ON_UNPLUG_LIST clear but the stripe | |
4126 | * is still in our list | |
4127 | */ | |
4128 | smp_mb__before_clear_bit(); | |
4129 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); | |
4130 | __release_stripe(conf, sh); | |
a9add5d9 | 4131 | cnt++; |
8811b596 SL |
4132 | } |
4133 | spin_unlock_irq(&conf->device_lock); | |
4134 | } | |
e3620a3a JB |
4135 | if (mddev->queue) |
4136 | trace_block_unplug(mddev->queue, cnt, !from_schedule); | |
8811b596 SL |
4137 | kfree(cb); |
4138 | } | |
4139 | ||
4140 | static void release_stripe_plug(struct mddev *mddev, | |
4141 | struct stripe_head *sh) | |
4142 | { | |
4143 | struct blk_plug_cb *blk_cb = blk_check_plugged( | |
4144 | raid5_unplug, mddev, | |
4145 | sizeof(struct raid5_plug_cb)); | |
4146 | struct raid5_plug_cb *cb; | |
4147 | ||
4148 | if (!blk_cb) { | |
4149 | release_stripe(sh); | |
4150 | return; | |
4151 | } | |
4152 | ||
4153 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); | |
4154 | ||
4155 | if (cb->list.next == NULL) | |
4156 | INIT_LIST_HEAD(&cb->list); | |
4157 | ||
4158 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) | |
4159 | list_add_tail(&sh->lru, &cb->list); | |
4160 | else | |
4161 | release_stripe(sh); | |
4162 | } | |
4163 | ||
620125f2 SL |
4164 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
4165 | { | |
4166 | struct r5conf *conf = mddev->private; | |
4167 | sector_t logical_sector, last_sector; | |
4168 | struct stripe_head *sh; | |
4169 | int remaining; | |
4170 | int stripe_sectors; | |
4171 | ||
4172 | if (mddev->reshape_position != MaxSector) | |
4173 | /* Skip discard while reshape is happening */ | |
4174 | return; | |
4175 | ||
4176 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); | |
4177 | last_sector = bi->bi_sector + (bi->bi_size>>9); | |
4178 | ||
4179 | bi->bi_next = NULL; | |
4180 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ | |
4181 | ||
4182 | stripe_sectors = conf->chunk_sectors * | |
4183 | (conf->raid_disks - conf->max_degraded); | |
4184 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, | |
4185 | stripe_sectors); | |
4186 | sector_div(last_sector, stripe_sectors); | |
4187 | ||
4188 | logical_sector *= conf->chunk_sectors; | |
4189 | last_sector *= conf->chunk_sectors; | |
4190 | ||
4191 | for (; logical_sector < last_sector; | |
4192 | logical_sector += STRIPE_SECTORS) { | |
4193 | DEFINE_WAIT(w); | |
4194 | int d; | |
4195 | again: | |
4196 | sh = get_active_stripe(conf, logical_sector, 0, 0, 0); | |
4197 | prepare_to_wait(&conf->wait_for_overlap, &w, | |
4198 | TASK_UNINTERRUPTIBLE); | |
f8dfcffd N |
4199 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
4200 | if (test_bit(STRIPE_SYNCING, &sh->state)) { | |
4201 | release_stripe(sh); | |
4202 | schedule(); | |
4203 | goto again; | |
4204 | } | |
4205 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); | |
620125f2 SL |
4206 | spin_lock_irq(&sh->stripe_lock); |
4207 | for (d = 0; d < conf->raid_disks; d++) { | |
4208 | if (d == sh->pd_idx || d == sh->qd_idx) | |
4209 | continue; | |
4210 | if (sh->dev[d].towrite || sh->dev[d].toread) { | |
4211 | set_bit(R5_Overlap, &sh->dev[d].flags); | |
4212 | spin_unlock_irq(&sh->stripe_lock); | |
4213 | release_stripe(sh); | |
4214 | schedule(); | |
4215 | goto again; | |
4216 | } | |
4217 | } | |
f8dfcffd | 4218 | set_bit(STRIPE_DISCARD, &sh->state); |
620125f2 SL |
4219 | finish_wait(&conf->wait_for_overlap, &w); |
4220 | for (d = 0; d < conf->raid_disks; d++) { | |
4221 | if (d == sh->pd_idx || d == sh->qd_idx) | |
4222 | continue; | |
4223 | sh->dev[d].towrite = bi; | |
4224 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); | |
4225 | raid5_inc_bi_active_stripes(bi); | |
4226 | } | |
4227 | spin_unlock_irq(&sh->stripe_lock); | |
4228 | if (conf->mddev->bitmap) { | |
4229 | for (d = 0; | |
4230 | d < conf->raid_disks - conf->max_degraded; | |
4231 | d++) | |
4232 | bitmap_startwrite(mddev->bitmap, | |
4233 | sh->sector, | |
4234 | STRIPE_SECTORS, | |
4235 | 0); | |
4236 | sh->bm_seq = conf->seq_flush + 1; | |
4237 | set_bit(STRIPE_BIT_DELAY, &sh->state); | |
4238 | } | |
4239 | ||
4240 | set_bit(STRIPE_HANDLE, &sh->state); | |
4241 | clear_bit(STRIPE_DELAYED, &sh->state); | |
4242 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
4243 | atomic_inc(&conf->preread_active_stripes); | |
4244 | release_stripe_plug(mddev, sh); | |
4245 | } | |
4246 | ||
4247 | remaining = raid5_dec_bi_active_stripes(bi); | |
4248 | if (remaining == 0) { | |
4249 | md_write_end(mddev); | |
4250 | bio_endio(bi, 0); | |
4251 | } | |
4252 | } | |
4253 | ||
b4fdcb02 | 4254 | static void make_request(struct mddev *mddev, struct bio * bi) |
1da177e4 | 4255 | { |
d1688a6d | 4256 | struct r5conf *conf = mddev->private; |
911d4ee8 | 4257 | int dd_idx; |
1da177e4 LT |
4258 | sector_t new_sector; |
4259 | sector_t logical_sector, last_sector; | |
4260 | struct stripe_head *sh; | |
a362357b | 4261 | const int rw = bio_data_dir(bi); |
49077326 | 4262 | int remaining; |
1da177e4 | 4263 | |
e9c7469b TH |
4264 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
4265 | md_flush_request(mddev, bi); | |
5a7bbad2 | 4266 | return; |
e5dcdd80 N |
4267 | } |
4268 | ||
3d310eb7 | 4269 | md_write_start(mddev, bi); |
06d91a5f | 4270 | |
802ba064 | 4271 | if (rw == READ && |
52488615 | 4272 | mddev->reshape_position == MaxSector && |
21a52c6d | 4273 | chunk_aligned_read(mddev,bi)) |
5a7bbad2 | 4274 | return; |
52488615 | 4275 | |
620125f2 SL |
4276 | if (unlikely(bi->bi_rw & REQ_DISCARD)) { |
4277 | make_discard_request(mddev, bi); | |
4278 | return; | |
4279 | } | |
4280 | ||
1da177e4 | 4281 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
f73a1c7d | 4282 | last_sector = bio_end_sector(bi); |
1da177e4 LT |
4283 | bi->bi_next = NULL; |
4284 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ | |
06d91a5f | 4285 | |
1da177e4 LT |
4286 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
4287 | DEFINE_WAIT(w); | |
b5663ba4 | 4288 | int previous; |
b578d55f | 4289 | |
7ecaa1e6 | 4290 | retry: |
b5663ba4 | 4291 | previous = 0; |
b578d55f | 4292 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
b0f9ec04 | 4293 | if (unlikely(conf->reshape_progress != MaxSector)) { |
fef9c61f | 4294 | /* spinlock is needed as reshape_progress may be |
df8e7f76 N |
4295 | * 64bit on a 32bit platform, and so it might be |
4296 | * possible to see a half-updated value | |
aeb878b0 | 4297 | * Of course reshape_progress could change after |
df8e7f76 N |
4298 | * the lock is dropped, so once we get a reference |
4299 | * to the stripe that we think it is, we will have | |
4300 | * to check again. | |
4301 | */ | |
7ecaa1e6 | 4302 | spin_lock_irq(&conf->device_lock); |
2c810cdd | 4303 | if (mddev->reshape_backwards |
fef9c61f N |
4304 | ? logical_sector < conf->reshape_progress |
4305 | : logical_sector >= conf->reshape_progress) { | |
b5663ba4 N |
4306 | previous = 1; |
4307 | } else { | |
2c810cdd | 4308 | if (mddev->reshape_backwards |
fef9c61f N |
4309 | ? logical_sector < conf->reshape_safe |
4310 | : logical_sector >= conf->reshape_safe) { | |
b578d55f N |
4311 | spin_unlock_irq(&conf->device_lock); |
4312 | schedule(); | |
4313 | goto retry; | |
4314 | } | |
4315 | } | |
7ecaa1e6 N |
4316 | spin_unlock_irq(&conf->device_lock); |
4317 | } | |
16a53ecc | 4318 | |
112bf897 N |
4319 | new_sector = raid5_compute_sector(conf, logical_sector, |
4320 | previous, | |
911d4ee8 | 4321 | &dd_idx, NULL); |
0c55e022 | 4322 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
1da177e4 LT |
4323 | (unsigned long long)new_sector, |
4324 | (unsigned long long)logical_sector); | |
4325 | ||
b5663ba4 | 4326 | sh = get_active_stripe(conf, new_sector, previous, |
a8c906ca | 4327 | (bi->bi_rw&RWA_MASK), 0); |
1da177e4 | 4328 | if (sh) { |
b0f9ec04 | 4329 | if (unlikely(previous)) { |
7ecaa1e6 | 4330 | /* expansion might have moved on while waiting for a |
df8e7f76 N |
4331 | * stripe, so we must do the range check again. |
4332 | * Expansion could still move past after this | |
4333 | * test, but as we are holding a reference to | |
4334 | * 'sh', we know that if that happens, | |
4335 | * STRIPE_EXPANDING will get set and the expansion | |
4336 | * won't proceed until we finish with the stripe. | |
7ecaa1e6 N |
4337 | */ |
4338 | int must_retry = 0; | |
4339 | spin_lock_irq(&conf->device_lock); | |
2c810cdd | 4340 | if (mddev->reshape_backwards |
b0f9ec04 N |
4341 | ? logical_sector >= conf->reshape_progress |
4342 | : logical_sector < conf->reshape_progress) | |
7ecaa1e6 N |
4343 | /* mismatch, need to try again */ |
4344 | must_retry = 1; | |
4345 | spin_unlock_irq(&conf->device_lock); | |
4346 | if (must_retry) { | |
4347 | release_stripe(sh); | |
7a3ab908 | 4348 | schedule(); |
7ecaa1e6 N |
4349 | goto retry; |
4350 | } | |
4351 | } | |
e62e58a5 | 4352 | |
ffd96e35 | 4353 | if (rw == WRITE && |
a5c308d4 | 4354 | logical_sector >= mddev->suspend_lo && |
e464eafd N |
4355 | logical_sector < mddev->suspend_hi) { |
4356 | release_stripe(sh); | |
e62e58a5 N |
4357 | /* As the suspend_* range is controlled by |
4358 | * userspace, we want an interruptible | |
4359 | * wait. | |
4360 | */ | |
4361 | flush_signals(current); | |
4362 | prepare_to_wait(&conf->wait_for_overlap, | |
4363 | &w, TASK_INTERRUPTIBLE); | |
4364 | if (logical_sector >= mddev->suspend_lo && | |
4365 | logical_sector < mddev->suspend_hi) | |
4366 | schedule(); | |
e464eafd N |
4367 | goto retry; |
4368 | } | |
7ecaa1e6 N |
4369 | |
4370 | if (test_bit(STRIPE_EXPANDING, &sh->state) || | |
ffd96e35 | 4371 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
7ecaa1e6 N |
4372 | /* Stripe is busy expanding or |
4373 | * add failed due to overlap. Flush everything | |
1da177e4 LT |
4374 | * and wait a while |
4375 | */ | |
482c0834 | 4376 | md_wakeup_thread(mddev->thread); |
1da177e4 LT |
4377 | release_stripe(sh); |
4378 | schedule(); | |
4379 | goto retry; | |
4380 | } | |
4381 | finish_wait(&conf->wait_for_overlap, &w); | |
6ed3003c N |
4382 | set_bit(STRIPE_HANDLE, &sh->state); |
4383 | clear_bit(STRIPE_DELAYED, &sh->state); | |
a852d7b8 | 4384 | if ((bi->bi_rw & REQ_SYNC) && |
729a1866 N |
4385 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
4386 | atomic_inc(&conf->preread_active_stripes); | |
8811b596 | 4387 | release_stripe_plug(mddev, sh); |
1da177e4 LT |
4388 | } else { |
4389 | /* cannot get stripe for read-ahead, just give-up */ | |
4390 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
4391 | finish_wait(&conf->wait_for_overlap, &w); | |
4392 | break; | |
4393 | } | |
1da177e4 | 4394 | } |
7c13edc8 | 4395 | |
e7836bd6 | 4396 | remaining = raid5_dec_bi_active_stripes(bi); |
f6344757 | 4397 | if (remaining == 0) { |
1da177e4 | 4398 | |
16a53ecc | 4399 | if ( rw == WRITE ) |
1da177e4 | 4400 | md_write_end(mddev); |
6712ecf8 | 4401 | |
0a82a8d1 LT |
4402 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
4403 | bi, 0); | |
0e13fe23 | 4404 | bio_endio(bi, 0); |
1da177e4 | 4405 | } |
1da177e4 LT |
4406 | } |
4407 | ||
fd01b88c | 4408 | static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks); |
b522adcd | 4409 | |
fd01b88c | 4410 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped) |
1da177e4 | 4411 | { |
52c03291 N |
4412 | /* reshaping is quite different to recovery/resync so it is |
4413 | * handled quite separately ... here. | |
4414 | * | |
4415 | * On each call to sync_request, we gather one chunk worth of | |
4416 | * destination stripes and flag them as expanding. | |
4417 | * Then we find all the source stripes and request reads. | |
4418 | * As the reads complete, handle_stripe will copy the data | |
4419 | * into the destination stripe and release that stripe. | |
4420 | */ | |
d1688a6d | 4421 | struct r5conf *conf = mddev->private; |
1da177e4 | 4422 | struct stripe_head *sh; |
ccfcc3c1 | 4423 | sector_t first_sector, last_sector; |
f416885e N |
4424 | int raid_disks = conf->previous_raid_disks; |
4425 | int data_disks = raid_disks - conf->max_degraded; | |
4426 | int new_data_disks = conf->raid_disks - conf->max_degraded; | |
52c03291 N |
4427 | int i; |
4428 | int dd_idx; | |
c8f517c4 | 4429 | sector_t writepos, readpos, safepos; |
ec32a2bd | 4430 | sector_t stripe_addr; |
7a661381 | 4431 | int reshape_sectors; |
ab69ae12 | 4432 | struct list_head stripes; |
52c03291 | 4433 | |
fef9c61f N |
4434 | if (sector_nr == 0) { |
4435 | /* If restarting in the middle, skip the initial sectors */ | |
2c810cdd | 4436 | if (mddev->reshape_backwards && |
fef9c61f N |
4437 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
4438 | sector_nr = raid5_size(mddev, 0, 0) | |
4439 | - conf->reshape_progress; | |
2c810cdd | 4440 | } else if (!mddev->reshape_backwards && |
fef9c61f N |
4441 | conf->reshape_progress > 0) |
4442 | sector_nr = conf->reshape_progress; | |
f416885e | 4443 | sector_div(sector_nr, new_data_disks); |
fef9c61f | 4444 | if (sector_nr) { |
8dee7211 N |
4445 | mddev->curr_resync_completed = sector_nr; |
4446 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); | |
fef9c61f N |
4447 | *skipped = 1; |
4448 | return sector_nr; | |
4449 | } | |
52c03291 N |
4450 | } |
4451 | ||
7a661381 N |
4452 | /* We need to process a full chunk at a time. |
4453 | * If old and new chunk sizes differ, we need to process the | |
4454 | * largest of these | |
4455 | */ | |
664e7c41 AN |
4456 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
4457 | reshape_sectors = mddev->new_chunk_sectors; | |
7a661381 | 4458 | else |
9d8f0363 | 4459 | reshape_sectors = mddev->chunk_sectors; |
7a661381 | 4460 | |
b5254dd5 N |
4461 | /* We update the metadata at least every 10 seconds, or when |
4462 | * the data about to be copied would over-write the source of | |
4463 | * the data at the front of the range. i.e. one new_stripe | |
4464 | * along from reshape_progress new_maps to after where | |
4465 | * reshape_safe old_maps to | |
52c03291 | 4466 | */ |
fef9c61f | 4467 | writepos = conf->reshape_progress; |
f416885e | 4468 | sector_div(writepos, new_data_disks); |
c8f517c4 N |
4469 | readpos = conf->reshape_progress; |
4470 | sector_div(readpos, data_disks); | |
fef9c61f | 4471 | safepos = conf->reshape_safe; |
f416885e | 4472 | sector_div(safepos, data_disks); |
2c810cdd | 4473 | if (mddev->reshape_backwards) { |
ed37d83e | 4474 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
c8f517c4 | 4475 | readpos += reshape_sectors; |
7a661381 | 4476 | safepos += reshape_sectors; |
fef9c61f | 4477 | } else { |
7a661381 | 4478 | writepos += reshape_sectors; |
ed37d83e N |
4479 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
4480 | safepos -= min_t(sector_t, reshape_sectors, safepos); | |
fef9c61f | 4481 | } |
52c03291 | 4482 | |
b5254dd5 N |
4483 | /* Having calculated the 'writepos' possibly use it |
4484 | * to set 'stripe_addr' which is where we will write to. | |
4485 | */ | |
4486 | if (mddev->reshape_backwards) { | |
4487 | BUG_ON(conf->reshape_progress == 0); | |
4488 | stripe_addr = writepos; | |
4489 | BUG_ON((mddev->dev_sectors & | |
4490 | ~((sector_t)reshape_sectors - 1)) | |
4491 | - reshape_sectors - stripe_addr | |
4492 | != sector_nr); | |
4493 | } else { | |
4494 | BUG_ON(writepos != sector_nr + reshape_sectors); | |
4495 | stripe_addr = sector_nr; | |
4496 | } | |
4497 | ||
c8f517c4 N |
4498 | /* 'writepos' is the most advanced device address we might write. |
4499 | * 'readpos' is the least advanced device address we might read. | |
4500 | * 'safepos' is the least address recorded in the metadata as having | |
4501 | * been reshaped. | |
b5254dd5 N |
4502 | * If there is a min_offset_diff, these are adjusted either by |
4503 | * increasing the safepos/readpos if diff is negative, or | |
4504 | * increasing writepos if diff is positive. | |
4505 | * If 'readpos' is then behind 'writepos', there is no way that we can | |
c8f517c4 N |
4506 | * ensure safety in the face of a crash - that must be done by userspace |
4507 | * making a backup of the data. So in that case there is no particular | |
4508 | * rush to update metadata. | |
4509 | * Otherwise if 'safepos' is behind 'writepos', then we really need to | |
4510 | * update the metadata to advance 'safepos' to match 'readpos' so that | |
4511 | * we can be safe in the event of a crash. | |
4512 | * So we insist on updating metadata if safepos is behind writepos and | |
4513 | * readpos is beyond writepos. | |
4514 | * In any case, update the metadata every 10 seconds. | |
4515 | * Maybe that number should be configurable, but I'm not sure it is | |
4516 | * worth it.... maybe it could be a multiple of safemode_delay??? | |
4517 | */ | |
b5254dd5 N |
4518 | if (conf->min_offset_diff < 0) { |
4519 | safepos += -conf->min_offset_diff; | |
4520 | readpos += -conf->min_offset_diff; | |
4521 | } else | |
4522 | writepos += conf->min_offset_diff; | |
4523 | ||
2c810cdd | 4524 | if ((mddev->reshape_backwards |
c8f517c4 N |
4525 | ? (safepos > writepos && readpos < writepos) |
4526 | : (safepos < writepos && readpos > writepos)) || | |
4527 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { | |
52c03291 N |
4528 | /* Cannot proceed until we've updated the superblock... */ |
4529 | wait_event(conf->wait_for_overlap, | |
4530 | atomic_read(&conf->reshape_stripes)==0); | |
fef9c61f | 4531 | mddev->reshape_position = conf->reshape_progress; |
75d3da43 | 4532 | mddev->curr_resync_completed = sector_nr; |
c8f517c4 | 4533 | conf->reshape_checkpoint = jiffies; |
850b2b42 | 4534 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
52c03291 | 4535 | md_wakeup_thread(mddev->thread); |
850b2b42 | 4536 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
52c03291 N |
4537 | kthread_should_stop()); |
4538 | spin_lock_irq(&conf->device_lock); | |
fef9c61f | 4539 | conf->reshape_safe = mddev->reshape_position; |
52c03291 N |
4540 | spin_unlock_irq(&conf->device_lock); |
4541 | wake_up(&conf->wait_for_overlap); | |
acb180b0 | 4542 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
52c03291 N |
4543 | } |
4544 | ||
ab69ae12 | 4545 | INIT_LIST_HEAD(&stripes); |
7a661381 | 4546 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
52c03291 | 4547 | int j; |
a9f326eb | 4548 | int skipped_disk = 0; |
a8c906ca | 4549 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
52c03291 N |
4550 | set_bit(STRIPE_EXPANDING, &sh->state); |
4551 | atomic_inc(&conf->reshape_stripes); | |
4552 | /* If any of this stripe is beyond the end of the old | |
4553 | * array, then we need to zero those blocks | |
4554 | */ | |
4555 | for (j=sh->disks; j--;) { | |
4556 | sector_t s; | |
4557 | if (j == sh->pd_idx) | |
4558 | continue; | |
f416885e | 4559 | if (conf->level == 6 && |
d0dabf7e | 4560 | j == sh->qd_idx) |
f416885e | 4561 | continue; |
784052ec | 4562 | s = compute_blocknr(sh, j, 0); |
b522adcd | 4563 | if (s < raid5_size(mddev, 0, 0)) { |
a9f326eb | 4564 | skipped_disk = 1; |
52c03291 N |
4565 | continue; |
4566 | } | |
4567 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); | |
4568 | set_bit(R5_Expanded, &sh->dev[j].flags); | |
4569 | set_bit(R5_UPTODATE, &sh->dev[j].flags); | |
4570 | } | |
a9f326eb | 4571 | if (!skipped_disk) { |
52c03291 N |
4572 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
4573 | set_bit(STRIPE_HANDLE, &sh->state); | |
4574 | } | |
ab69ae12 | 4575 | list_add(&sh->lru, &stripes); |
52c03291 N |
4576 | } |
4577 | spin_lock_irq(&conf->device_lock); | |
2c810cdd | 4578 | if (mddev->reshape_backwards) |
7a661381 | 4579 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
fef9c61f | 4580 | else |
7a661381 | 4581 | conf->reshape_progress += reshape_sectors * new_data_disks; |
52c03291 N |
4582 | spin_unlock_irq(&conf->device_lock); |
4583 | /* Ok, those stripe are ready. We can start scheduling | |
4584 | * reads on the source stripes. | |
4585 | * The source stripes are determined by mapping the first and last | |
4586 | * block on the destination stripes. | |
4587 | */ | |
52c03291 | 4588 | first_sector = |
ec32a2bd | 4589 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
911d4ee8 | 4590 | 1, &dd_idx, NULL); |
52c03291 | 4591 | last_sector = |
0e6e0271 | 4592 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
09c9e5fa | 4593 | * new_data_disks - 1), |
911d4ee8 | 4594 | 1, &dd_idx, NULL); |
58c0fed4 AN |
4595 | if (last_sector >= mddev->dev_sectors) |
4596 | last_sector = mddev->dev_sectors - 1; | |
52c03291 | 4597 | while (first_sector <= last_sector) { |
a8c906ca | 4598 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
52c03291 N |
4599 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
4600 | set_bit(STRIPE_HANDLE, &sh->state); | |
4601 | release_stripe(sh); | |
4602 | first_sector += STRIPE_SECTORS; | |
4603 | } | |
ab69ae12 N |
4604 | /* Now that the sources are clearly marked, we can release |
4605 | * the destination stripes | |
4606 | */ | |
4607 | while (!list_empty(&stripes)) { | |
4608 | sh = list_entry(stripes.next, struct stripe_head, lru); | |
4609 | list_del_init(&sh->lru); | |
4610 | release_stripe(sh); | |
4611 | } | |
c6207277 N |
4612 | /* If this takes us to the resync_max point where we have to pause, |
4613 | * then we need to write out the superblock. | |
4614 | */ | |
7a661381 | 4615 | sector_nr += reshape_sectors; |
c03f6a19 N |
4616 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
4617 | >= mddev->resync_max - mddev->curr_resync_completed) { | |
c6207277 N |
4618 | /* Cannot proceed until we've updated the superblock... */ |
4619 | wait_event(conf->wait_for_overlap, | |
4620 | atomic_read(&conf->reshape_stripes) == 0); | |
fef9c61f | 4621 | mddev->reshape_position = conf->reshape_progress; |
75d3da43 | 4622 | mddev->curr_resync_completed = sector_nr; |
c8f517c4 | 4623 | conf->reshape_checkpoint = jiffies; |
c6207277 N |
4624 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
4625 | md_wakeup_thread(mddev->thread); | |
4626 | wait_event(mddev->sb_wait, | |
4627 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) | |
4628 | || kthread_should_stop()); | |
4629 | spin_lock_irq(&conf->device_lock); | |
fef9c61f | 4630 | conf->reshape_safe = mddev->reshape_position; |
c6207277 N |
4631 | spin_unlock_irq(&conf->device_lock); |
4632 | wake_up(&conf->wait_for_overlap); | |
acb180b0 | 4633 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
c6207277 | 4634 | } |
7a661381 | 4635 | return reshape_sectors; |
52c03291 N |
4636 | } |
4637 | ||
4638 | /* FIXME go_faster isn't used */ | |
fd01b88c | 4639 | static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster) |
52c03291 | 4640 | { |
d1688a6d | 4641 | struct r5conf *conf = mddev->private; |
52c03291 | 4642 | struct stripe_head *sh; |
58c0fed4 | 4643 | sector_t max_sector = mddev->dev_sectors; |
57dab0bd | 4644 | sector_t sync_blocks; |
16a53ecc N |
4645 | int still_degraded = 0; |
4646 | int i; | |
1da177e4 | 4647 | |
72626685 | 4648 | if (sector_nr >= max_sector) { |
1da177e4 | 4649 | /* just being told to finish up .. nothing much to do */ |
cea9c228 | 4650 | |
29269553 N |
4651 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
4652 | end_reshape(conf); | |
4653 | return 0; | |
4654 | } | |
72626685 N |
4655 | |
4656 | if (mddev->curr_resync < max_sector) /* aborted */ | |
4657 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
4658 | &sync_blocks, 1); | |
16a53ecc | 4659 | else /* completed sync */ |
72626685 N |
4660 | conf->fullsync = 0; |
4661 | bitmap_close_sync(mddev->bitmap); | |
4662 | ||
1da177e4 LT |
4663 | return 0; |
4664 | } | |
ccfcc3c1 | 4665 | |
64bd660b N |
4666 | /* Allow raid5_quiesce to complete */ |
4667 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); | |
4668 | ||
52c03291 N |
4669 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
4670 | return reshape_request(mddev, sector_nr, skipped); | |
f6705578 | 4671 | |
c6207277 N |
4672 | /* No need to check resync_max as we never do more than one |
4673 | * stripe, and as resync_max will always be on a chunk boundary, | |
4674 | * if the check in md_do_sync didn't fire, there is no chance | |
4675 | * of overstepping resync_max here | |
4676 | */ | |
4677 | ||
16a53ecc | 4678 | /* if there is too many failed drives and we are trying |
1da177e4 LT |
4679 | * to resync, then assert that we are finished, because there is |
4680 | * nothing we can do. | |
4681 | */ | |
3285edf1 | 4682 | if (mddev->degraded >= conf->max_degraded && |
16a53ecc | 4683 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
58c0fed4 | 4684 | sector_t rv = mddev->dev_sectors - sector_nr; |
57afd89f | 4685 | *skipped = 1; |
1da177e4 LT |
4686 | return rv; |
4687 | } | |
6f608040 | 4688 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
4689 | !conf->fullsync && | |
4690 | !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && | |
4691 | sync_blocks >= STRIPE_SECTORS) { | |
72626685 N |
4692 | /* we can skip this block, and probably more */ |
4693 | sync_blocks /= STRIPE_SECTORS; | |
4694 | *skipped = 1; | |
4695 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ | |
4696 | } | |
1da177e4 | 4697 | |
b47490c9 N |
4698 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
4699 | ||
a8c906ca | 4700 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
1da177e4 | 4701 | if (sh == NULL) { |
a8c906ca | 4702 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
1da177e4 | 4703 | /* make sure we don't swamp the stripe cache if someone else |
16a53ecc | 4704 | * is trying to get access |
1da177e4 | 4705 | */ |
66c006a5 | 4706 | schedule_timeout_uninterruptible(1); |
1da177e4 | 4707 | } |
16a53ecc N |
4708 | /* Need to check if array will still be degraded after recovery/resync |
4709 | * We don't need to check the 'failed' flag as when that gets set, | |
4710 | * recovery aborts. | |
4711 | */ | |
f001a70c | 4712 | for (i = 0; i < conf->raid_disks; i++) |
16a53ecc N |
4713 | if (conf->disks[i].rdev == NULL) |
4714 | still_degraded = 1; | |
4715 | ||
4716 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); | |
4717 | ||
83206d66 | 4718 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
1da177e4 | 4719 | |
1442577b | 4720 | handle_stripe(sh); |
1da177e4 LT |
4721 | release_stripe(sh); |
4722 | ||
4723 | return STRIPE_SECTORS; | |
4724 | } | |
4725 | ||
d1688a6d | 4726 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio) |
46031f9a RBJ |
4727 | { |
4728 | /* We may not be able to submit a whole bio at once as there | |
4729 | * may not be enough stripe_heads available. | |
4730 | * We cannot pre-allocate enough stripe_heads as we may need | |
4731 | * more than exist in the cache (if we allow ever large chunks). | |
4732 | * So we do one stripe head at a time and record in | |
4733 | * ->bi_hw_segments how many have been done. | |
4734 | * | |
4735 | * We *know* that this entire raid_bio is in one chunk, so | |
4736 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. | |
4737 | */ | |
4738 | struct stripe_head *sh; | |
911d4ee8 | 4739 | int dd_idx; |
46031f9a RBJ |
4740 | sector_t sector, logical_sector, last_sector; |
4741 | int scnt = 0; | |
4742 | int remaining; | |
4743 | int handled = 0; | |
4744 | ||
4745 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); | |
112bf897 | 4746 | sector = raid5_compute_sector(conf, logical_sector, |
911d4ee8 | 4747 | 0, &dd_idx, NULL); |
f73a1c7d | 4748 | last_sector = bio_end_sector(raid_bio); |
46031f9a RBJ |
4749 | |
4750 | for (; logical_sector < last_sector; | |
387bb173 NB |
4751 | logical_sector += STRIPE_SECTORS, |
4752 | sector += STRIPE_SECTORS, | |
4753 | scnt++) { | |
46031f9a | 4754 | |
e7836bd6 | 4755 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
46031f9a RBJ |
4756 | /* already done this stripe */ |
4757 | continue; | |
4758 | ||
a8c906ca | 4759 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
46031f9a RBJ |
4760 | |
4761 | if (!sh) { | |
4762 | /* failed to get a stripe - must wait */ | |
e7836bd6 | 4763 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
46031f9a RBJ |
4764 | conf->retry_read_aligned = raid_bio; |
4765 | return handled; | |
4766 | } | |
4767 | ||
387bb173 NB |
4768 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
4769 | release_stripe(sh); | |
e7836bd6 | 4770 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
387bb173 NB |
4771 | conf->retry_read_aligned = raid_bio; |
4772 | return handled; | |
4773 | } | |
4774 | ||
3f9e7c14 | 4775 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
36d1c647 | 4776 | handle_stripe(sh); |
46031f9a RBJ |
4777 | release_stripe(sh); |
4778 | handled++; | |
4779 | } | |
e7836bd6 | 4780 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
0a82a8d1 LT |
4781 | if (remaining == 0) { |
4782 | trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev), | |
4783 | raid_bio, 0); | |
0e13fe23 | 4784 | bio_endio(raid_bio, 0); |
0a82a8d1 | 4785 | } |
46031f9a RBJ |
4786 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
4787 | wake_up(&conf->wait_for_stripe); | |
4788 | return handled; | |
4789 | } | |
4790 | ||
46a06401 SL |
4791 | #define MAX_STRIPE_BATCH 8 |
4792 | static int handle_active_stripes(struct r5conf *conf) | |
4793 | { | |
4794 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; | |
4795 | int i, batch_size = 0; | |
4796 | ||
4797 | while (batch_size < MAX_STRIPE_BATCH && | |
4798 | (sh = __get_priority_stripe(conf)) != NULL) | |
4799 | batch[batch_size++] = sh; | |
4800 | ||
4801 | if (batch_size == 0) | |
4802 | return batch_size; | |
4803 | spin_unlock_irq(&conf->device_lock); | |
4804 | ||
4805 | for (i = 0; i < batch_size; i++) | |
4806 | handle_stripe(batch[i]); | |
4807 | ||
4808 | cond_resched(); | |
4809 | ||
4810 | spin_lock_irq(&conf->device_lock); | |
4811 | for (i = 0; i < batch_size; i++) | |
4812 | __release_stripe(conf, batch[i]); | |
4813 | return batch_size; | |
4814 | } | |
46031f9a | 4815 | |
1da177e4 LT |
4816 | /* |
4817 | * This is our raid5 kernel thread. | |
4818 | * | |
4819 | * We scan the hash table for stripes which can be handled now. | |
4820 | * During the scan, completed stripes are saved for us by the interrupt | |
4821 | * handler, so that they will not have to wait for our next wakeup. | |
4822 | */ | |
4ed8731d | 4823 | static void raid5d(struct md_thread *thread) |
1da177e4 | 4824 | { |
4ed8731d | 4825 | struct mddev *mddev = thread->mddev; |
d1688a6d | 4826 | struct r5conf *conf = mddev->private; |
1da177e4 | 4827 | int handled; |
e1dfa0a2 | 4828 | struct blk_plug plug; |
1da177e4 | 4829 | |
45b4233c | 4830 | pr_debug("+++ raid5d active\n"); |
1da177e4 LT |
4831 | |
4832 | md_check_recovery(mddev); | |
1da177e4 | 4833 | |
e1dfa0a2 | 4834 | blk_start_plug(&plug); |
1da177e4 LT |
4835 | handled = 0; |
4836 | spin_lock_irq(&conf->device_lock); | |
4837 | while (1) { | |
46031f9a | 4838 | struct bio *bio; |
46a06401 | 4839 | int batch_size; |
1da177e4 | 4840 | |
0021b7bc | 4841 | if ( |
7c13edc8 N |
4842 | !list_empty(&conf->bitmap_list)) { |
4843 | /* Now is a good time to flush some bitmap updates */ | |
4844 | conf->seq_flush++; | |
700e432d | 4845 | spin_unlock_irq(&conf->device_lock); |
72626685 | 4846 | bitmap_unplug(mddev->bitmap); |
700e432d | 4847 | spin_lock_irq(&conf->device_lock); |
7c13edc8 | 4848 | conf->seq_write = conf->seq_flush; |
72626685 N |
4849 | activate_bit_delay(conf); |
4850 | } | |
0021b7bc | 4851 | raid5_activate_delayed(conf); |
72626685 | 4852 | |
46031f9a RBJ |
4853 | while ((bio = remove_bio_from_retry(conf))) { |
4854 | int ok; | |
4855 | spin_unlock_irq(&conf->device_lock); | |
4856 | ok = retry_aligned_read(conf, bio); | |
4857 | spin_lock_irq(&conf->device_lock); | |
4858 | if (!ok) | |
4859 | break; | |
4860 | handled++; | |
4861 | } | |
4862 | ||
46a06401 SL |
4863 | batch_size = handle_active_stripes(conf); |
4864 | if (!batch_size) | |
1da177e4 | 4865 | break; |
46a06401 | 4866 | handled += batch_size; |
1da177e4 | 4867 | |
46a06401 SL |
4868 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { |
4869 | spin_unlock_irq(&conf->device_lock); | |
de393cde | 4870 | md_check_recovery(mddev); |
46a06401 SL |
4871 | spin_lock_irq(&conf->device_lock); |
4872 | } | |
1da177e4 | 4873 | } |
45b4233c | 4874 | pr_debug("%d stripes handled\n", handled); |
1da177e4 LT |
4875 | |
4876 | spin_unlock_irq(&conf->device_lock); | |
4877 | ||
c9f21aaf | 4878 | async_tx_issue_pending_all(); |
e1dfa0a2 | 4879 | blk_finish_plug(&plug); |
1da177e4 | 4880 | |
45b4233c | 4881 | pr_debug("--- raid5d inactive\n"); |
1da177e4 LT |
4882 | } |
4883 | ||
3f294f4f | 4884 | static ssize_t |
fd01b88c | 4885 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
3f294f4f | 4886 | { |
d1688a6d | 4887 | struct r5conf *conf = mddev->private; |
96de1e66 N |
4888 | if (conf) |
4889 | return sprintf(page, "%d\n", conf->max_nr_stripes); | |
4890 | else | |
4891 | return 0; | |
3f294f4f N |
4892 | } |
4893 | ||
c41d4ac4 | 4894 | int |
fd01b88c | 4895 | raid5_set_cache_size(struct mddev *mddev, int size) |
3f294f4f | 4896 | { |
d1688a6d | 4897 | struct r5conf *conf = mddev->private; |
b5470dc5 DW |
4898 | int err; |
4899 | ||
c41d4ac4 | 4900 | if (size <= 16 || size > 32768) |
3f294f4f | 4901 | return -EINVAL; |
c41d4ac4 | 4902 | while (size < conf->max_nr_stripes) { |
3f294f4f N |
4903 | if (drop_one_stripe(conf)) |
4904 | conf->max_nr_stripes--; | |
4905 | else | |
4906 | break; | |
4907 | } | |
b5470dc5 DW |
4908 | err = md_allow_write(mddev); |
4909 | if (err) | |
4910 | return err; | |
c41d4ac4 | 4911 | while (size > conf->max_nr_stripes) { |
3f294f4f N |
4912 | if (grow_one_stripe(conf)) |
4913 | conf->max_nr_stripes++; | |
4914 | else break; | |
4915 | } | |
c41d4ac4 N |
4916 | return 0; |
4917 | } | |
4918 | EXPORT_SYMBOL(raid5_set_cache_size); | |
4919 | ||
4920 | static ssize_t | |
fd01b88c | 4921 | raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len) |
c41d4ac4 | 4922 | { |
d1688a6d | 4923 | struct r5conf *conf = mddev->private; |
c41d4ac4 N |
4924 | unsigned long new; |
4925 | int err; | |
4926 | ||
4927 | if (len >= PAGE_SIZE) | |
4928 | return -EINVAL; | |
4929 | if (!conf) | |
4930 | return -ENODEV; | |
4931 | ||
b29bebd6 | 4932 | if (kstrtoul(page, 10, &new)) |
c41d4ac4 N |
4933 | return -EINVAL; |
4934 | err = raid5_set_cache_size(mddev, new); | |
4935 | if (err) | |
4936 | return err; | |
3f294f4f N |
4937 | return len; |
4938 | } | |
007583c9 | 4939 | |
96de1e66 N |
4940 | static struct md_sysfs_entry |
4941 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, | |
4942 | raid5_show_stripe_cache_size, | |
4943 | raid5_store_stripe_cache_size); | |
3f294f4f | 4944 | |
8b3e6cdc | 4945 | static ssize_t |
fd01b88c | 4946 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
8b3e6cdc | 4947 | { |
d1688a6d | 4948 | struct r5conf *conf = mddev->private; |
8b3e6cdc DW |
4949 | if (conf) |
4950 | return sprintf(page, "%d\n", conf->bypass_threshold); | |
4951 | else | |
4952 | return 0; | |
4953 | } | |
4954 | ||
4955 | static ssize_t | |
fd01b88c | 4956 | raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len) |
8b3e6cdc | 4957 | { |
d1688a6d | 4958 | struct r5conf *conf = mddev->private; |
4ef197d8 | 4959 | unsigned long new; |
8b3e6cdc DW |
4960 | if (len >= PAGE_SIZE) |
4961 | return -EINVAL; | |
4962 | if (!conf) | |
4963 | return -ENODEV; | |
4964 | ||
b29bebd6 | 4965 | if (kstrtoul(page, 10, &new)) |
8b3e6cdc | 4966 | return -EINVAL; |
4ef197d8 | 4967 | if (new > conf->max_nr_stripes) |
8b3e6cdc DW |
4968 | return -EINVAL; |
4969 | conf->bypass_threshold = new; | |
4970 | return len; | |
4971 | } | |
4972 | ||
4973 | static struct md_sysfs_entry | |
4974 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, | |
4975 | S_IRUGO | S_IWUSR, | |
4976 | raid5_show_preread_threshold, | |
4977 | raid5_store_preread_threshold); | |
4978 | ||
3f294f4f | 4979 | static ssize_t |
fd01b88c | 4980 | stripe_cache_active_show(struct mddev *mddev, char *page) |
3f294f4f | 4981 | { |
d1688a6d | 4982 | struct r5conf *conf = mddev->private; |
96de1e66 N |
4983 | if (conf) |
4984 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); | |
4985 | else | |
4986 | return 0; | |
3f294f4f N |
4987 | } |
4988 | ||
96de1e66 N |
4989 | static struct md_sysfs_entry |
4990 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); | |
3f294f4f | 4991 | |
007583c9 | 4992 | static struct attribute *raid5_attrs[] = { |
3f294f4f N |
4993 | &raid5_stripecache_size.attr, |
4994 | &raid5_stripecache_active.attr, | |
8b3e6cdc | 4995 | &raid5_preread_bypass_threshold.attr, |
3f294f4f N |
4996 | NULL, |
4997 | }; | |
007583c9 N |
4998 | static struct attribute_group raid5_attrs_group = { |
4999 | .name = NULL, | |
5000 | .attrs = raid5_attrs, | |
3f294f4f N |
5001 | }; |
5002 | ||
80c3a6ce | 5003 | static sector_t |
fd01b88c | 5004 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
80c3a6ce | 5005 | { |
d1688a6d | 5006 | struct r5conf *conf = mddev->private; |
80c3a6ce DW |
5007 | |
5008 | if (!sectors) | |
5009 | sectors = mddev->dev_sectors; | |
5e5e3e78 | 5010 | if (!raid_disks) |
7ec05478 | 5011 | /* size is defined by the smallest of previous and new size */ |
5e5e3e78 | 5012 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
80c3a6ce | 5013 | |
9d8f0363 | 5014 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
664e7c41 | 5015 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
80c3a6ce DW |
5016 | return sectors * (raid_disks - conf->max_degraded); |
5017 | } | |
5018 | ||
d1688a6d | 5019 | static void raid5_free_percpu(struct r5conf *conf) |
36d1c647 DW |
5020 | { |
5021 | struct raid5_percpu *percpu; | |
5022 | unsigned long cpu; | |
5023 | ||
5024 | if (!conf->percpu) | |
5025 | return; | |
5026 | ||
5027 | get_online_cpus(); | |
5028 | for_each_possible_cpu(cpu) { | |
5029 | percpu = per_cpu_ptr(conf->percpu, cpu); | |
5030 | safe_put_page(percpu->spare_page); | |
d6f38f31 | 5031 | kfree(percpu->scribble); |
36d1c647 DW |
5032 | } |
5033 | #ifdef CONFIG_HOTPLUG_CPU | |
5034 | unregister_cpu_notifier(&conf->cpu_notify); | |
5035 | #endif | |
5036 | put_online_cpus(); | |
5037 | ||
5038 | free_percpu(conf->percpu); | |
5039 | } | |
5040 | ||
d1688a6d | 5041 | static void free_conf(struct r5conf *conf) |
95fc17aa DW |
5042 | { |
5043 | shrink_stripes(conf); | |
36d1c647 | 5044 | raid5_free_percpu(conf); |
95fc17aa DW |
5045 | kfree(conf->disks); |
5046 | kfree(conf->stripe_hashtbl); | |
5047 | kfree(conf); | |
5048 | } | |
5049 | ||
36d1c647 DW |
5050 | #ifdef CONFIG_HOTPLUG_CPU |
5051 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, | |
5052 | void *hcpu) | |
5053 | { | |
d1688a6d | 5054 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
36d1c647 DW |
5055 | long cpu = (long)hcpu; |
5056 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); | |
5057 | ||
5058 | switch (action) { | |
5059 | case CPU_UP_PREPARE: | |
5060 | case CPU_UP_PREPARE_FROZEN: | |
d6f38f31 | 5061 | if (conf->level == 6 && !percpu->spare_page) |
36d1c647 | 5062 | percpu->spare_page = alloc_page(GFP_KERNEL); |
d6f38f31 DW |
5063 | if (!percpu->scribble) |
5064 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); | |
5065 | ||
5066 | if (!percpu->scribble || | |
5067 | (conf->level == 6 && !percpu->spare_page)) { | |
5068 | safe_put_page(percpu->spare_page); | |
5069 | kfree(percpu->scribble); | |
36d1c647 DW |
5070 | pr_err("%s: failed memory allocation for cpu%ld\n", |
5071 | __func__, cpu); | |
55af6bb5 | 5072 | return notifier_from_errno(-ENOMEM); |
36d1c647 DW |
5073 | } |
5074 | break; | |
5075 | case CPU_DEAD: | |
5076 | case CPU_DEAD_FROZEN: | |
5077 | safe_put_page(percpu->spare_page); | |
d6f38f31 | 5078 | kfree(percpu->scribble); |
36d1c647 | 5079 | percpu->spare_page = NULL; |
d6f38f31 | 5080 | percpu->scribble = NULL; |
36d1c647 DW |
5081 | break; |
5082 | default: | |
5083 | break; | |
5084 | } | |
5085 | return NOTIFY_OK; | |
5086 | } | |
5087 | #endif | |
5088 | ||
d1688a6d | 5089 | static int raid5_alloc_percpu(struct r5conf *conf) |
36d1c647 DW |
5090 | { |
5091 | unsigned long cpu; | |
5092 | struct page *spare_page; | |
a29d8b8e | 5093 | struct raid5_percpu __percpu *allcpus; |
d6f38f31 | 5094 | void *scribble; |
36d1c647 DW |
5095 | int err; |
5096 | ||
36d1c647 DW |
5097 | allcpus = alloc_percpu(struct raid5_percpu); |
5098 | if (!allcpus) | |
5099 | return -ENOMEM; | |
5100 | conf->percpu = allcpus; | |
5101 | ||
5102 | get_online_cpus(); | |
5103 | err = 0; | |
5104 | for_each_present_cpu(cpu) { | |
d6f38f31 DW |
5105 | if (conf->level == 6) { |
5106 | spare_page = alloc_page(GFP_KERNEL); | |
5107 | if (!spare_page) { | |
5108 | err = -ENOMEM; | |
5109 | break; | |
5110 | } | |
5111 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; | |
5112 | } | |
5e5e3e78 | 5113 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
d6f38f31 | 5114 | if (!scribble) { |
36d1c647 DW |
5115 | err = -ENOMEM; |
5116 | break; | |
5117 | } | |
d6f38f31 | 5118 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
36d1c647 DW |
5119 | } |
5120 | #ifdef CONFIG_HOTPLUG_CPU | |
5121 | conf->cpu_notify.notifier_call = raid456_cpu_notify; | |
5122 | conf->cpu_notify.priority = 0; | |
5123 | if (err == 0) | |
5124 | err = register_cpu_notifier(&conf->cpu_notify); | |
5125 | #endif | |
5126 | put_online_cpus(); | |
5127 | ||
5128 | return err; | |
5129 | } | |
5130 | ||
d1688a6d | 5131 | static struct r5conf *setup_conf(struct mddev *mddev) |
1da177e4 | 5132 | { |
d1688a6d | 5133 | struct r5conf *conf; |
5e5e3e78 | 5134 | int raid_disk, memory, max_disks; |
3cb03002 | 5135 | struct md_rdev *rdev; |
1da177e4 | 5136 | struct disk_info *disk; |
0232605d | 5137 | char pers_name[6]; |
1da177e4 | 5138 | |
91adb564 N |
5139 | if (mddev->new_level != 5 |
5140 | && mddev->new_level != 4 | |
5141 | && mddev->new_level != 6) { | |
0c55e022 | 5142 | printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
91adb564 N |
5143 | mdname(mddev), mddev->new_level); |
5144 | return ERR_PTR(-EIO); | |
1da177e4 | 5145 | } |
91adb564 N |
5146 | if ((mddev->new_level == 5 |
5147 | && !algorithm_valid_raid5(mddev->new_layout)) || | |
5148 | (mddev->new_level == 6 | |
5149 | && !algorithm_valid_raid6(mddev->new_layout))) { | |
0c55e022 | 5150 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
91adb564 N |
5151 | mdname(mddev), mddev->new_layout); |
5152 | return ERR_PTR(-EIO); | |
99c0fb5f | 5153 | } |
91adb564 | 5154 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
0c55e022 | 5155 | printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
91adb564 N |
5156 | mdname(mddev), mddev->raid_disks); |
5157 | return ERR_PTR(-EINVAL); | |
4bbf3771 N |
5158 | } |
5159 | ||
664e7c41 AN |
5160 | if (!mddev->new_chunk_sectors || |
5161 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || | |
5162 | !is_power_of_2(mddev->new_chunk_sectors)) { | |
0c55e022 N |
5163 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
5164 | mdname(mddev), mddev->new_chunk_sectors << 9); | |
91adb564 | 5165 | return ERR_PTR(-EINVAL); |
f6705578 N |
5166 | } |
5167 | ||
d1688a6d | 5168 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
91adb564 | 5169 | if (conf == NULL) |
1da177e4 | 5170 | goto abort; |
f5efd45a DW |
5171 | spin_lock_init(&conf->device_lock); |
5172 | init_waitqueue_head(&conf->wait_for_stripe); | |
5173 | init_waitqueue_head(&conf->wait_for_overlap); | |
5174 | INIT_LIST_HEAD(&conf->handle_list); | |
5175 | INIT_LIST_HEAD(&conf->hold_list); | |
5176 | INIT_LIST_HEAD(&conf->delayed_list); | |
5177 | INIT_LIST_HEAD(&conf->bitmap_list); | |
5178 | INIT_LIST_HEAD(&conf->inactive_list); | |
5179 | atomic_set(&conf->active_stripes, 0); | |
5180 | atomic_set(&conf->preread_active_stripes, 0); | |
5181 | atomic_set(&conf->active_aligned_reads, 0); | |
5182 | conf->bypass_threshold = BYPASS_THRESHOLD; | |
d890fa2b | 5183 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
91adb564 N |
5184 | |
5185 | conf->raid_disks = mddev->raid_disks; | |
5186 | if (mddev->reshape_position == MaxSector) | |
5187 | conf->previous_raid_disks = mddev->raid_disks; | |
5188 | else | |
f6705578 | 5189 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
5e5e3e78 N |
5190 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
5191 | conf->scribble_len = scribble_len(max_disks); | |
f6705578 | 5192 | |
5e5e3e78 | 5193 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
b55e6bfc N |
5194 | GFP_KERNEL); |
5195 | if (!conf->disks) | |
5196 | goto abort; | |
9ffae0cf | 5197 | |
1da177e4 LT |
5198 | conf->mddev = mddev; |
5199 | ||
fccddba0 | 5200 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
1da177e4 | 5201 | goto abort; |
1da177e4 | 5202 | |
36d1c647 DW |
5203 | conf->level = mddev->new_level; |
5204 | if (raid5_alloc_percpu(conf) != 0) | |
5205 | goto abort; | |
5206 | ||
0c55e022 | 5207 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
1da177e4 | 5208 | |
dafb20fa | 5209 | rdev_for_each(rdev, mddev) { |
1da177e4 | 5210 | raid_disk = rdev->raid_disk; |
5e5e3e78 | 5211 | if (raid_disk >= max_disks |
1da177e4 LT |
5212 | || raid_disk < 0) |
5213 | continue; | |
5214 | disk = conf->disks + raid_disk; | |
5215 | ||
17045f52 N |
5216 | if (test_bit(Replacement, &rdev->flags)) { |
5217 | if (disk->replacement) | |
5218 | goto abort; | |
5219 | disk->replacement = rdev; | |
5220 | } else { | |
5221 | if (disk->rdev) | |
5222 | goto abort; | |
5223 | disk->rdev = rdev; | |
5224 | } | |
1da177e4 | 5225 | |
b2d444d7 | 5226 | if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 | 5227 | char b[BDEVNAME_SIZE]; |
0c55e022 N |
5228 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
5229 | " disk %d\n", | |
5230 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); | |
d6b212f4 | 5231 | } else if (rdev->saved_raid_disk != raid_disk) |
8c2e870a NB |
5232 | /* Cannot rely on bitmap to complete recovery */ |
5233 | conf->fullsync = 1; | |
1da177e4 LT |
5234 | } |
5235 | ||
09c9e5fa | 5236 | conf->chunk_sectors = mddev->new_chunk_sectors; |
91adb564 | 5237 | conf->level = mddev->new_level; |
16a53ecc N |
5238 | if (conf->level == 6) |
5239 | conf->max_degraded = 2; | |
5240 | else | |
5241 | conf->max_degraded = 1; | |
91adb564 | 5242 | conf->algorithm = mddev->new_layout; |
1da177e4 | 5243 | conf->max_nr_stripes = NR_STRIPES; |
fef9c61f | 5244 | conf->reshape_progress = mddev->reshape_position; |
e183eaed | 5245 | if (conf->reshape_progress != MaxSector) { |
09c9e5fa | 5246 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
e183eaed N |
5247 | conf->prev_algo = mddev->layout; |
5248 | } | |
1da177e4 | 5249 | |
91adb564 | 5250 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
5e5e3e78 | 5251 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
91adb564 N |
5252 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
5253 | printk(KERN_ERR | |
0c55e022 N |
5254 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
5255 | mdname(mddev), memory); | |
91adb564 N |
5256 | goto abort; |
5257 | } else | |
0c55e022 N |
5258 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
5259 | mdname(mddev), memory); | |
1da177e4 | 5260 | |
0232605d N |
5261 | sprintf(pers_name, "raid%d", mddev->new_level); |
5262 | conf->thread = md_register_thread(raid5d, mddev, pers_name); | |
91adb564 N |
5263 | if (!conf->thread) { |
5264 | printk(KERN_ERR | |
0c55e022 | 5265 | "md/raid:%s: couldn't allocate thread.\n", |
91adb564 | 5266 | mdname(mddev)); |
16a53ecc N |
5267 | goto abort; |
5268 | } | |
91adb564 N |
5269 | |
5270 | return conf; | |
5271 | ||
5272 | abort: | |
5273 | if (conf) { | |
95fc17aa | 5274 | free_conf(conf); |
91adb564 N |
5275 | return ERR_PTR(-EIO); |
5276 | } else | |
5277 | return ERR_PTR(-ENOMEM); | |
5278 | } | |
5279 | ||
c148ffdc N |
5280 | |
5281 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) | |
5282 | { | |
5283 | switch (algo) { | |
5284 | case ALGORITHM_PARITY_0: | |
5285 | if (raid_disk < max_degraded) | |
5286 | return 1; | |
5287 | break; | |
5288 | case ALGORITHM_PARITY_N: | |
5289 | if (raid_disk >= raid_disks - max_degraded) | |
5290 | return 1; | |
5291 | break; | |
5292 | case ALGORITHM_PARITY_0_6: | |
5293 | if (raid_disk == 0 || | |
5294 | raid_disk == raid_disks - 1) | |
5295 | return 1; | |
5296 | break; | |
5297 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
5298 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
5299 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
5300 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
5301 | if (raid_disk == raid_disks - 1) | |
5302 | return 1; | |
5303 | } | |
5304 | return 0; | |
5305 | } | |
5306 | ||
fd01b88c | 5307 | static int run(struct mddev *mddev) |
91adb564 | 5308 | { |
d1688a6d | 5309 | struct r5conf *conf; |
9f7c2220 | 5310 | int working_disks = 0; |
c148ffdc | 5311 | int dirty_parity_disks = 0; |
3cb03002 | 5312 | struct md_rdev *rdev; |
c148ffdc | 5313 | sector_t reshape_offset = 0; |
17045f52 | 5314 | int i; |
b5254dd5 N |
5315 | long long min_offset_diff = 0; |
5316 | int first = 1; | |
91adb564 | 5317 | |
8c6ac868 | 5318 | if (mddev->recovery_cp != MaxSector) |
0c55e022 | 5319 | printk(KERN_NOTICE "md/raid:%s: not clean" |
8c6ac868 AN |
5320 | " -- starting background reconstruction\n", |
5321 | mdname(mddev)); | |
b5254dd5 N |
5322 | |
5323 | rdev_for_each(rdev, mddev) { | |
5324 | long long diff; | |
5325 | if (rdev->raid_disk < 0) | |
5326 | continue; | |
5327 | diff = (rdev->new_data_offset - rdev->data_offset); | |
5328 | if (first) { | |
5329 | min_offset_diff = diff; | |
5330 | first = 0; | |
5331 | } else if (mddev->reshape_backwards && | |
5332 | diff < min_offset_diff) | |
5333 | min_offset_diff = diff; | |
5334 | else if (!mddev->reshape_backwards && | |
5335 | diff > min_offset_diff) | |
5336 | min_offset_diff = diff; | |
5337 | } | |
5338 | ||
91adb564 N |
5339 | if (mddev->reshape_position != MaxSector) { |
5340 | /* Check that we can continue the reshape. | |
b5254dd5 N |
5341 | * Difficulties arise if the stripe we would write to |
5342 | * next is at or after the stripe we would read from next. | |
5343 | * For a reshape that changes the number of devices, this | |
5344 | * is only possible for a very short time, and mdadm makes | |
5345 | * sure that time appears to have past before assembling | |
5346 | * the array. So we fail if that time hasn't passed. | |
5347 | * For a reshape that keeps the number of devices the same | |
5348 | * mdadm must be monitoring the reshape can keeping the | |
5349 | * critical areas read-only and backed up. It will start | |
5350 | * the array in read-only mode, so we check for that. | |
91adb564 N |
5351 | */ |
5352 | sector_t here_new, here_old; | |
5353 | int old_disks; | |
18b00334 | 5354 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
91adb564 | 5355 | |
88ce4930 | 5356 | if (mddev->new_level != mddev->level) { |
0c55e022 | 5357 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
91adb564 N |
5358 | "required - aborting.\n", |
5359 | mdname(mddev)); | |
5360 | return -EINVAL; | |
5361 | } | |
91adb564 N |
5362 | old_disks = mddev->raid_disks - mddev->delta_disks; |
5363 | /* reshape_position must be on a new-stripe boundary, and one | |
5364 | * further up in new geometry must map after here in old | |
5365 | * geometry. | |
5366 | */ | |
5367 | here_new = mddev->reshape_position; | |
664e7c41 | 5368 | if (sector_div(here_new, mddev->new_chunk_sectors * |
91adb564 | 5369 | (mddev->raid_disks - max_degraded))) { |
0c55e022 N |
5370 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
5371 | "on a stripe boundary\n", mdname(mddev)); | |
91adb564 N |
5372 | return -EINVAL; |
5373 | } | |
c148ffdc | 5374 | reshape_offset = here_new * mddev->new_chunk_sectors; |
91adb564 N |
5375 | /* here_new is the stripe we will write to */ |
5376 | here_old = mddev->reshape_position; | |
9d8f0363 | 5377 | sector_div(here_old, mddev->chunk_sectors * |
91adb564 N |
5378 | (old_disks-max_degraded)); |
5379 | /* here_old is the first stripe that we might need to read | |
5380 | * from */ | |
67ac6011 | 5381 | if (mddev->delta_disks == 0) { |
b5254dd5 N |
5382 | if ((here_new * mddev->new_chunk_sectors != |
5383 | here_old * mddev->chunk_sectors)) { | |
5384 | printk(KERN_ERR "md/raid:%s: reshape position is" | |
5385 | " confused - aborting\n", mdname(mddev)); | |
5386 | return -EINVAL; | |
5387 | } | |
67ac6011 | 5388 | /* We cannot be sure it is safe to start an in-place |
b5254dd5 | 5389 | * reshape. It is only safe if user-space is monitoring |
67ac6011 N |
5390 | * and taking constant backups. |
5391 | * mdadm always starts a situation like this in | |
5392 | * readonly mode so it can take control before | |
5393 | * allowing any writes. So just check for that. | |
5394 | */ | |
b5254dd5 N |
5395 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
5396 | abs(min_offset_diff) >= mddev->new_chunk_sectors) | |
5397 | /* not really in-place - so OK */; | |
5398 | else if (mddev->ro == 0) { | |
5399 | printk(KERN_ERR "md/raid:%s: in-place reshape " | |
5400 | "must be started in read-only mode " | |
5401 | "- aborting\n", | |
0c55e022 | 5402 | mdname(mddev)); |
67ac6011 N |
5403 | return -EINVAL; |
5404 | } | |
2c810cdd | 5405 | } else if (mddev->reshape_backwards |
b5254dd5 | 5406 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
67ac6011 N |
5407 | here_old * mddev->chunk_sectors) |
5408 | : (here_new * mddev->new_chunk_sectors >= | |
b5254dd5 | 5409 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
91adb564 | 5410 | /* Reading from the same stripe as writing to - bad */ |
0c55e022 N |
5411 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
5412 | "auto-recovery - aborting.\n", | |
5413 | mdname(mddev)); | |
91adb564 N |
5414 | return -EINVAL; |
5415 | } | |
0c55e022 N |
5416 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
5417 | mdname(mddev)); | |
91adb564 N |
5418 | /* OK, we should be able to continue; */ |
5419 | } else { | |
5420 | BUG_ON(mddev->level != mddev->new_level); | |
5421 | BUG_ON(mddev->layout != mddev->new_layout); | |
664e7c41 | 5422 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
91adb564 | 5423 | BUG_ON(mddev->delta_disks != 0); |
1da177e4 | 5424 | } |
91adb564 | 5425 | |
245f46c2 N |
5426 | if (mddev->private == NULL) |
5427 | conf = setup_conf(mddev); | |
5428 | else | |
5429 | conf = mddev->private; | |
5430 | ||
91adb564 N |
5431 | if (IS_ERR(conf)) |
5432 | return PTR_ERR(conf); | |
5433 | ||
b5254dd5 | 5434 | conf->min_offset_diff = min_offset_diff; |
91adb564 N |
5435 | mddev->thread = conf->thread; |
5436 | conf->thread = NULL; | |
5437 | mddev->private = conf; | |
5438 | ||
17045f52 N |
5439 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
5440 | i++) { | |
5441 | rdev = conf->disks[i].rdev; | |
5442 | if (!rdev && conf->disks[i].replacement) { | |
5443 | /* The replacement is all we have yet */ | |
5444 | rdev = conf->disks[i].replacement; | |
5445 | conf->disks[i].replacement = NULL; | |
5446 | clear_bit(Replacement, &rdev->flags); | |
5447 | conf->disks[i].rdev = rdev; | |
5448 | } | |
5449 | if (!rdev) | |
c148ffdc | 5450 | continue; |
17045f52 N |
5451 | if (conf->disks[i].replacement && |
5452 | conf->reshape_progress != MaxSector) { | |
5453 | /* replacements and reshape simply do not mix. */ | |
5454 | printk(KERN_ERR "md: cannot handle concurrent " | |
5455 | "replacement and reshape.\n"); | |
5456 | goto abort; | |
5457 | } | |
2f115882 | 5458 | if (test_bit(In_sync, &rdev->flags)) { |
91adb564 | 5459 | working_disks++; |
2f115882 N |
5460 | continue; |
5461 | } | |
c148ffdc N |
5462 | /* This disc is not fully in-sync. However if it |
5463 | * just stored parity (beyond the recovery_offset), | |
5464 | * when we don't need to be concerned about the | |
5465 | * array being dirty. | |
5466 | * When reshape goes 'backwards', we never have | |
5467 | * partially completed devices, so we only need | |
5468 | * to worry about reshape going forwards. | |
5469 | */ | |
5470 | /* Hack because v0.91 doesn't store recovery_offset properly. */ | |
5471 | if (mddev->major_version == 0 && | |
5472 | mddev->minor_version > 90) | |
5473 | rdev->recovery_offset = reshape_offset; | |
5026d7a9 | 5474 | |
c148ffdc N |
5475 | if (rdev->recovery_offset < reshape_offset) { |
5476 | /* We need to check old and new layout */ | |
5477 | if (!only_parity(rdev->raid_disk, | |
5478 | conf->algorithm, | |
5479 | conf->raid_disks, | |
5480 | conf->max_degraded)) | |
5481 | continue; | |
5482 | } | |
5483 | if (!only_parity(rdev->raid_disk, | |
5484 | conf->prev_algo, | |
5485 | conf->previous_raid_disks, | |
5486 | conf->max_degraded)) | |
5487 | continue; | |
5488 | dirty_parity_disks++; | |
5489 | } | |
91adb564 | 5490 | |
17045f52 N |
5491 | /* |
5492 | * 0 for a fully functional array, 1 or 2 for a degraded array. | |
5493 | */ | |
908f4fbd | 5494 | mddev->degraded = calc_degraded(conf); |
91adb564 | 5495 | |
674806d6 | 5496 | if (has_failed(conf)) { |
0c55e022 | 5497 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
1da177e4 | 5498 | " (%d/%d failed)\n", |
02c2de8c | 5499 | mdname(mddev), mddev->degraded, conf->raid_disks); |
1da177e4 LT |
5500 | goto abort; |
5501 | } | |
5502 | ||
91adb564 | 5503 | /* device size must be a multiple of chunk size */ |
9d8f0363 | 5504 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
91adb564 N |
5505 | mddev->resync_max_sectors = mddev->dev_sectors; |
5506 | ||
c148ffdc | 5507 | if (mddev->degraded > dirty_parity_disks && |
1da177e4 | 5508 | mddev->recovery_cp != MaxSector) { |
6ff8d8ec N |
5509 | if (mddev->ok_start_degraded) |
5510 | printk(KERN_WARNING | |
0c55e022 N |
5511 | "md/raid:%s: starting dirty degraded array" |
5512 | " - data corruption possible.\n", | |
6ff8d8ec N |
5513 | mdname(mddev)); |
5514 | else { | |
5515 | printk(KERN_ERR | |
0c55e022 | 5516 | "md/raid:%s: cannot start dirty degraded array.\n", |
6ff8d8ec N |
5517 | mdname(mddev)); |
5518 | goto abort; | |
5519 | } | |
1da177e4 LT |
5520 | } |
5521 | ||
1da177e4 | 5522 | if (mddev->degraded == 0) |
0c55e022 N |
5523 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
5524 | " devices, algorithm %d\n", mdname(mddev), conf->level, | |
e183eaed N |
5525 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
5526 | mddev->new_layout); | |
1da177e4 | 5527 | else |
0c55e022 N |
5528 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
5529 | " out of %d devices, algorithm %d\n", | |
5530 | mdname(mddev), conf->level, | |
5531 | mddev->raid_disks - mddev->degraded, | |
5532 | mddev->raid_disks, mddev->new_layout); | |
1da177e4 LT |
5533 | |
5534 | print_raid5_conf(conf); | |
5535 | ||
fef9c61f | 5536 | if (conf->reshape_progress != MaxSector) { |
fef9c61f | 5537 | conf->reshape_safe = conf->reshape_progress; |
f6705578 N |
5538 | atomic_set(&conf->reshape_stripes, 0); |
5539 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
5540 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
5541 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
5542 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
5543 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
0da3c619 | 5544 | "reshape"); |
f6705578 N |
5545 | } |
5546 | ||
1da177e4 LT |
5547 | |
5548 | /* Ok, everything is just fine now */ | |
a64c876f N |
5549 | if (mddev->to_remove == &raid5_attrs_group) |
5550 | mddev->to_remove = NULL; | |
00bcb4ac N |
5551 | else if (mddev->kobj.sd && |
5552 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) | |
5e55e2f5 | 5553 | printk(KERN_WARNING |
4a5add49 | 5554 | "raid5: failed to create sysfs attributes for %s\n", |
5e55e2f5 | 5555 | mdname(mddev)); |
4a5add49 | 5556 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
7a5febe9 | 5557 | |
4a5add49 | 5558 | if (mddev->queue) { |
9f7c2220 | 5559 | int chunk_size; |
620125f2 | 5560 | bool discard_supported = true; |
4a5add49 N |
5561 | /* read-ahead size must cover two whole stripes, which |
5562 | * is 2 * (datadisks) * chunksize where 'n' is the | |
5563 | * number of raid devices | |
5564 | */ | |
5565 | int data_disks = conf->previous_raid_disks - conf->max_degraded; | |
5566 | int stripe = data_disks * | |
5567 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); | |
5568 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) | |
5569 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
91adb564 | 5570 | |
4a5add49 | 5571 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
f022b2fd | 5572 | |
11d8a6e3 N |
5573 | mddev->queue->backing_dev_info.congested_data = mddev; |
5574 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; | |
7a5febe9 | 5575 | |
9f7c2220 N |
5576 | chunk_size = mddev->chunk_sectors << 9; |
5577 | blk_queue_io_min(mddev->queue, chunk_size); | |
5578 | blk_queue_io_opt(mddev->queue, chunk_size * | |
5579 | (conf->raid_disks - conf->max_degraded)); | |
620125f2 SL |
5580 | /* |
5581 | * We can only discard a whole stripe. It doesn't make sense to | |
5582 | * discard data disk but write parity disk | |
5583 | */ | |
5584 | stripe = stripe * PAGE_SIZE; | |
4ac6875e N |
5585 | /* Round up to power of 2, as discard handling |
5586 | * currently assumes that */ | |
5587 | while ((stripe-1) & stripe) | |
5588 | stripe = (stripe | (stripe-1)) + 1; | |
620125f2 SL |
5589 | mddev->queue->limits.discard_alignment = stripe; |
5590 | mddev->queue->limits.discard_granularity = stripe; | |
5591 | /* | |
5592 | * unaligned part of discard request will be ignored, so can't | |
5593 | * guarantee discard_zerors_data | |
5594 | */ | |
5595 | mddev->queue->limits.discard_zeroes_data = 0; | |
8f6c2e4b | 5596 | |
5026d7a9 PA |
5597 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
5598 | ||
05616be5 | 5599 | rdev_for_each(rdev, mddev) { |
9f7c2220 N |
5600 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
5601 | rdev->data_offset << 9); | |
05616be5 N |
5602 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
5603 | rdev->new_data_offset << 9); | |
620125f2 SL |
5604 | /* |
5605 | * discard_zeroes_data is required, otherwise data | |
5606 | * could be lost. Consider a scenario: discard a stripe | |
5607 | * (the stripe could be inconsistent if | |
5608 | * discard_zeroes_data is 0); write one disk of the | |
5609 | * stripe (the stripe could be inconsistent again | |
5610 | * depending on which disks are used to calculate | |
5611 | * parity); the disk is broken; The stripe data of this | |
5612 | * disk is lost. | |
5613 | */ | |
5614 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || | |
5615 | !bdev_get_queue(rdev->bdev)-> | |
5616 | limits.discard_zeroes_data) | |
5617 | discard_supported = false; | |
05616be5 | 5618 | } |
620125f2 SL |
5619 | |
5620 | if (discard_supported && | |
5621 | mddev->queue->limits.max_discard_sectors >= stripe && | |
5622 | mddev->queue->limits.discard_granularity >= stripe) | |
5623 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, | |
5624 | mddev->queue); | |
5625 | else | |
5626 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, | |
5627 | mddev->queue); | |
9f7c2220 | 5628 | } |
23032a0e | 5629 | |
1da177e4 LT |
5630 | return 0; |
5631 | abort: | |
01f96c0a | 5632 | md_unregister_thread(&mddev->thread); |
e4f869d9 N |
5633 | print_raid5_conf(conf); |
5634 | free_conf(conf); | |
1da177e4 | 5635 | mddev->private = NULL; |
0c55e022 | 5636 | printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
1da177e4 LT |
5637 | return -EIO; |
5638 | } | |
5639 | ||
fd01b88c | 5640 | static int stop(struct mddev *mddev) |
1da177e4 | 5641 | { |
d1688a6d | 5642 | struct r5conf *conf = mddev->private; |
1da177e4 | 5643 | |
01f96c0a | 5644 | md_unregister_thread(&mddev->thread); |
11d8a6e3 N |
5645 | if (mddev->queue) |
5646 | mddev->queue->backing_dev_info.congested_fn = NULL; | |
95fc17aa | 5647 | free_conf(conf); |
a64c876f N |
5648 | mddev->private = NULL; |
5649 | mddev->to_remove = &raid5_attrs_group; | |
1da177e4 LT |
5650 | return 0; |
5651 | } | |
5652 | ||
fd01b88c | 5653 | static void status(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 5654 | { |
d1688a6d | 5655 | struct r5conf *conf = mddev->private; |
1da177e4 LT |
5656 | int i; |
5657 | ||
9d8f0363 AN |
5658 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
5659 | mddev->chunk_sectors / 2, mddev->layout); | |
02c2de8c | 5660 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
1da177e4 LT |
5661 | for (i = 0; i < conf->raid_disks; i++) |
5662 | seq_printf (seq, "%s", | |
5663 | conf->disks[i].rdev && | |
b2d444d7 | 5664 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
1da177e4 | 5665 | seq_printf (seq, "]"); |
1da177e4 LT |
5666 | } |
5667 | ||
d1688a6d | 5668 | static void print_raid5_conf (struct r5conf *conf) |
1da177e4 LT |
5669 | { |
5670 | int i; | |
5671 | struct disk_info *tmp; | |
5672 | ||
0c55e022 | 5673 | printk(KERN_DEBUG "RAID conf printout:\n"); |
1da177e4 LT |
5674 | if (!conf) { |
5675 | printk("(conf==NULL)\n"); | |
5676 | return; | |
5677 | } | |
0c55e022 N |
5678 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
5679 | conf->raid_disks, | |
5680 | conf->raid_disks - conf->mddev->degraded); | |
1da177e4 LT |
5681 | |
5682 | for (i = 0; i < conf->raid_disks; i++) { | |
5683 | char b[BDEVNAME_SIZE]; | |
5684 | tmp = conf->disks + i; | |
5685 | if (tmp->rdev) | |
0c55e022 N |
5686 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
5687 | i, !test_bit(Faulty, &tmp->rdev->flags), | |
5688 | bdevname(tmp->rdev->bdev, b)); | |
1da177e4 LT |
5689 | } |
5690 | } | |
5691 | ||
fd01b88c | 5692 | static int raid5_spare_active(struct mddev *mddev) |
1da177e4 LT |
5693 | { |
5694 | int i; | |
d1688a6d | 5695 | struct r5conf *conf = mddev->private; |
1da177e4 | 5696 | struct disk_info *tmp; |
6b965620 N |
5697 | int count = 0; |
5698 | unsigned long flags; | |
1da177e4 LT |
5699 | |
5700 | for (i = 0; i < conf->raid_disks; i++) { | |
5701 | tmp = conf->disks + i; | |
dd054fce N |
5702 | if (tmp->replacement |
5703 | && tmp->replacement->recovery_offset == MaxSector | |
5704 | && !test_bit(Faulty, &tmp->replacement->flags) | |
5705 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { | |
5706 | /* Replacement has just become active. */ | |
5707 | if (!tmp->rdev | |
5708 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) | |
5709 | count++; | |
5710 | if (tmp->rdev) { | |
5711 | /* Replaced device not technically faulty, | |
5712 | * but we need to be sure it gets removed | |
5713 | * and never re-added. | |
5714 | */ | |
5715 | set_bit(Faulty, &tmp->rdev->flags); | |
5716 | sysfs_notify_dirent_safe( | |
5717 | tmp->rdev->sysfs_state); | |
5718 | } | |
5719 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); | |
5720 | } else if (tmp->rdev | |
70fffd0b | 5721 | && tmp->rdev->recovery_offset == MaxSector |
b2d444d7 | 5722 | && !test_bit(Faulty, &tmp->rdev->flags) |
c04be0aa | 5723 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
6b965620 | 5724 | count++; |
43c73ca4 | 5725 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
1da177e4 LT |
5726 | } |
5727 | } | |
6b965620 | 5728 | spin_lock_irqsave(&conf->device_lock, flags); |
908f4fbd | 5729 | mddev->degraded = calc_degraded(conf); |
6b965620 | 5730 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 | 5731 | print_raid5_conf(conf); |
6b965620 | 5732 | return count; |
1da177e4 LT |
5733 | } |
5734 | ||
b8321b68 | 5735 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 5736 | { |
d1688a6d | 5737 | struct r5conf *conf = mddev->private; |
1da177e4 | 5738 | int err = 0; |
b8321b68 | 5739 | int number = rdev->raid_disk; |
657e3e4d | 5740 | struct md_rdev **rdevp; |
1da177e4 LT |
5741 | struct disk_info *p = conf->disks + number; |
5742 | ||
5743 | print_raid5_conf(conf); | |
657e3e4d N |
5744 | if (rdev == p->rdev) |
5745 | rdevp = &p->rdev; | |
5746 | else if (rdev == p->replacement) | |
5747 | rdevp = &p->replacement; | |
5748 | else | |
5749 | return 0; | |
5750 | ||
5751 | if (number >= conf->raid_disks && | |
5752 | conf->reshape_progress == MaxSector) | |
5753 | clear_bit(In_sync, &rdev->flags); | |
5754 | ||
5755 | if (test_bit(In_sync, &rdev->flags) || | |
5756 | atomic_read(&rdev->nr_pending)) { | |
5757 | err = -EBUSY; | |
5758 | goto abort; | |
5759 | } | |
5760 | /* Only remove non-faulty devices if recovery | |
5761 | * isn't possible. | |
5762 | */ | |
5763 | if (!test_bit(Faulty, &rdev->flags) && | |
5764 | mddev->recovery_disabled != conf->recovery_disabled && | |
5765 | !has_failed(conf) && | |
dd054fce | 5766 | (!p->replacement || p->replacement == rdev) && |
657e3e4d N |
5767 | number < conf->raid_disks) { |
5768 | err = -EBUSY; | |
5769 | goto abort; | |
5770 | } | |
5771 | *rdevp = NULL; | |
5772 | synchronize_rcu(); | |
5773 | if (atomic_read(&rdev->nr_pending)) { | |
5774 | /* lost the race, try later */ | |
5775 | err = -EBUSY; | |
5776 | *rdevp = rdev; | |
dd054fce N |
5777 | } else if (p->replacement) { |
5778 | /* We must have just cleared 'rdev' */ | |
5779 | p->rdev = p->replacement; | |
5780 | clear_bit(Replacement, &p->replacement->flags); | |
5781 | smp_mb(); /* Make sure other CPUs may see both as identical | |
5782 | * but will never see neither - if they are careful | |
5783 | */ | |
5784 | p->replacement = NULL; | |
5785 | clear_bit(WantReplacement, &rdev->flags); | |
5786 | } else | |
5787 | /* We might have just removed the Replacement as faulty- | |
5788 | * clear the bit just in case | |
5789 | */ | |
5790 | clear_bit(WantReplacement, &rdev->flags); | |
1da177e4 LT |
5791 | abort: |
5792 | ||
5793 | print_raid5_conf(conf); | |
5794 | return err; | |
5795 | } | |
5796 | ||
fd01b88c | 5797 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 5798 | { |
d1688a6d | 5799 | struct r5conf *conf = mddev->private; |
199050ea | 5800 | int err = -EEXIST; |
1da177e4 LT |
5801 | int disk; |
5802 | struct disk_info *p; | |
6c2fce2e NB |
5803 | int first = 0; |
5804 | int last = conf->raid_disks - 1; | |
1da177e4 | 5805 | |
7f0da59b N |
5806 | if (mddev->recovery_disabled == conf->recovery_disabled) |
5807 | return -EBUSY; | |
5808 | ||
dc10c643 | 5809 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
1da177e4 | 5810 | /* no point adding a device */ |
199050ea | 5811 | return -EINVAL; |
1da177e4 | 5812 | |
6c2fce2e NB |
5813 | if (rdev->raid_disk >= 0) |
5814 | first = last = rdev->raid_disk; | |
1da177e4 LT |
5815 | |
5816 | /* | |
16a53ecc N |
5817 | * find the disk ... but prefer rdev->saved_raid_disk |
5818 | * if possible. | |
1da177e4 | 5819 | */ |
16a53ecc | 5820 | if (rdev->saved_raid_disk >= 0 && |
6c2fce2e | 5821 | rdev->saved_raid_disk >= first && |
16a53ecc | 5822 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
5cfb22a1 N |
5823 | first = rdev->saved_raid_disk; |
5824 | ||
5825 | for (disk = first; disk <= last; disk++) { | |
7bfec5f3 N |
5826 | p = conf->disks + disk; |
5827 | if (p->rdev == NULL) { | |
b2d444d7 | 5828 | clear_bit(In_sync, &rdev->flags); |
1da177e4 | 5829 | rdev->raid_disk = disk; |
199050ea | 5830 | err = 0; |
72626685 N |
5831 | if (rdev->saved_raid_disk != disk) |
5832 | conf->fullsync = 1; | |
d6065f7b | 5833 | rcu_assign_pointer(p->rdev, rdev); |
5cfb22a1 | 5834 | goto out; |
1da177e4 | 5835 | } |
5cfb22a1 N |
5836 | } |
5837 | for (disk = first; disk <= last; disk++) { | |
5838 | p = conf->disks + disk; | |
7bfec5f3 N |
5839 | if (test_bit(WantReplacement, &p->rdev->flags) && |
5840 | p->replacement == NULL) { | |
5841 | clear_bit(In_sync, &rdev->flags); | |
5842 | set_bit(Replacement, &rdev->flags); | |
5843 | rdev->raid_disk = disk; | |
5844 | err = 0; | |
5845 | conf->fullsync = 1; | |
5846 | rcu_assign_pointer(p->replacement, rdev); | |
5847 | break; | |
5848 | } | |
5849 | } | |
5cfb22a1 | 5850 | out: |
1da177e4 | 5851 | print_raid5_conf(conf); |
199050ea | 5852 | return err; |
1da177e4 LT |
5853 | } |
5854 | ||
fd01b88c | 5855 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
1da177e4 LT |
5856 | { |
5857 | /* no resync is happening, and there is enough space | |
5858 | * on all devices, so we can resize. | |
5859 | * We need to make sure resync covers any new space. | |
5860 | * If the array is shrinking we should possibly wait until | |
5861 | * any io in the removed space completes, but it hardly seems | |
5862 | * worth it. | |
5863 | */ | |
a4a6125a | 5864 | sector_t newsize; |
9d8f0363 | 5865 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
a4a6125a N |
5866 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
5867 | if (mddev->external_size && | |
5868 | mddev->array_sectors > newsize) | |
b522adcd | 5869 | return -EINVAL; |
a4a6125a N |
5870 | if (mddev->bitmap) { |
5871 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); | |
5872 | if (ret) | |
5873 | return ret; | |
5874 | } | |
5875 | md_set_array_sectors(mddev, newsize); | |
f233ea5c | 5876 | set_capacity(mddev->gendisk, mddev->array_sectors); |
449aad3e | 5877 | revalidate_disk(mddev->gendisk); |
b098636c N |
5878 | if (sectors > mddev->dev_sectors && |
5879 | mddev->recovery_cp > mddev->dev_sectors) { | |
58c0fed4 | 5880 | mddev->recovery_cp = mddev->dev_sectors; |
1da177e4 LT |
5881 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
5882 | } | |
58c0fed4 | 5883 | mddev->dev_sectors = sectors; |
4b5c7ae8 | 5884 | mddev->resync_max_sectors = sectors; |
1da177e4 LT |
5885 | return 0; |
5886 | } | |
5887 | ||
fd01b88c | 5888 | static int check_stripe_cache(struct mddev *mddev) |
01ee22b4 N |
5889 | { |
5890 | /* Can only proceed if there are plenty of stripe_heads. | |
5891 | * We need a minimum of one full stripe,, and for sensible progress | |
5892 | * it is best to have about 4 times that. | |
5893 | * If we require 4 times, then the default 256 4K stripe_heads will | |
5894 | * allow for chunk sizes up to 256K, which is probably OK. | |
5895 | * If the chunk size is greater, user-space should request more | |
5896 | * stripe_heads first. | |
5897 | */ | |
d1688a6d | 5898 | struct r5conf *conf = mddev->private; |
01ee22b4 N |
5899 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
5900 | > conf->max_nr_stripes || | |
5901 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 | |
5902 | > conf->max_nr_stripes) { | |
0c55e022 N |
5903 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
5904 | mdname(mddev), | |
01ee22b4 N |
5905 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
5906 | / STRIPE_SIZE)*4); | |
5907 | return 0; | |
5908 | } | |
5909 | return 1; | |
5910 | } | |
5911 | ||
fd01b88c | 5912 | static int check_reshape(struct mddev *mddev) |
29269553 | 5913 | { |
d1688a6d | 5914 | struct r5conf *conf = mddev->private; |
29269553 | 5915 | |
88ce4930 N |
5916 | if (mddev->delta_disks == 0 && |
5917 | mddev->new_layout == mddev->layout && | |
664e7c41 | 5918 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
50ac168a | 5919 | return 0; /* nothing to do */ |
674806d6 | 5920 | if (has_failed(conf)) |
ec32a2bd | 5921 | return -EINVAL; |
fdcfbbb6 | 5922 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
ec32a2bd N |
5923 | /* We might be able to shrink, but the devices must |
5924 | * be made bigger first. | |
5925 | * For raid6, 4 is the minimum size. | |
5926 | * Otherwise 2 is the minimum | |
5927 | */ | |
5928 | int min = 2; | |
5929 | if (mddev->level == 6) | |
5930 | min = 4; | |
5931 | if (mddev->raid_disks + mddev->delta_disks < min) | |
5932 | return -EINVAL; | |
5933 | } | |
29269553 | 5934 | |
01ee22b4 | 5935 | if (!check_stripe_cache(mddev)) |
29269553 | 5936 | return -ENOSPC; |
29269553 | 5937 | |
e56108d6 N |
5938 | return resize_stripes(conf, (conf->previous_raid_disks |
5939 | + mddev->delta_disks)); | |
63c70c4f N |
5940 | } |
5941 | ||
fd01b88c | 5942 | static int raid5_start_reshape(struct mddev *mddev) |
63c70c4f | 5943 | { |
d1688a6d | 5944 | struct r5conf *conf = mddev->private; |
3cb03002 | 5945 | struct md_rdev *rdev; |
63c70c4f | 5946 | int spares = 0; |
c04be0aa | 5947 | unsigned long flags; |
63c70c4f | 5948 | |
f416885e | 5949 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
63c70c4f N |
5950 | return -EBUSY; |
5951 | ||
01ee22b4 N |
5952 | if (!check_stripe_cache(mddev)) |
5953 | return -ENOSPC; | |
5954 | ||
30b67645 N |
5955 | if (has_failed(conf)) |
5956 | return -EINVAL; | |
5957 | ||
c6563a8c | 5958 | rdev_for_each(rdev, mddev) { |
469518a3 N |
5959 | if (!test_bit(In_sync, &rdev->flags) |
5960 | && !test_bit(Faulty, &rdev->flags)) | |
29269553 | 5961 | spares++; |
c6563a8c | 5962 | } |
63c70c4f | 5963 | |
f416885e | 5964 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
29269553 N |
5965 | /* Not enough devices even to make a degraded array |
5966 | * of that size | |
5967 | */ | |
5968 | return -EINVAL; | |
5969 | ||
ec32a2bd N |
5970 | /* Refuse to reduce size of the array. Any reductions in |
5971 | * array size must be through explicit setting of array_size | |
5972 | * attribute. | |
5973 | */ | |
5974 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) | |
5975 | < mddev->array_sectors) { | |
0c55e022 | 5976 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
ec32a2bd N |
5977 | "before number of disks\n", mdname(mddev)); |
5978 | return -EINVAL; | |
5979 | } | |
5980 | ||
f6705578 | 5981 | atomic_set(&conf->reshape_stripes, 0); |
29269553 N |
5982 | spin_lock_irq(&conf->device_lock); |
5983 | conf->previous_raid_disks = conf->raid_disks; | |
63c70c4f | 5984 | conf->raid_disks += mddev->delta_disks; |
09c9e5fa AN |
5985 | conf->prev_chunk_sectors = conf->chunk_sectors; |
5986 | conf->chunk_sectors = mddev->new_chunk_sectors; | |
88ce4930 N |
5987 | conf->prev_algo = conf->algorithm; |
5988 | conf->algorithm = mddev->new_layout; | |
05616be5 N |
5989 | conf->generation++; |
5990 | /* Code that selects data_offset needs to see the generation update | |
5991 | * if reshape_progress has been set - so a memory barrier needed. | |
5992 | */ | |
5993 | smp_mb(); | |
2c810cdd | 5994 | if (mddev->reshape_backwards) |
fef9c61f N |
5995 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
5996 | else | |
5997 | conf->reshape_progress = 0; | |
5998 | conf->reshape_safe = conf->reshape_progress; | |
29269553 N |
5999 | spin_unlock_irq(&conf->device_lock); |
6000 | ||
6001 | /* Add some new drives, as many as will fit. | |
6002 | * We know there are enough to make the newly sized array work. | |
3424bf6a N |
6003 | * Don't add devices if we are reducing the number of |
6004 | * devices in the array. This is because it is not possible | |
6005 | * to correctly record the "partially reconstructed" state of | |
6006 | * such devices during the reshape and confusion could result. | |
29269553 | 6007 | */ |
87a8dec9 | 6008 | if (mddev->delta_disks >= 0) { |
dafb20fa | 6009 | rdev_for_each(rdev, mddev) |
87a8dec9 N |
6010 | if (rdev->raid_disk < 0 && |
6011 | !test_bit(Faulty, &rdev->flags)) { | |
6012 | if (raid5_add_disk(mddev, rdev) == 0) { | |
87a8dec9 | 6013 | if (rdev->raid_disk |
9d4c7d87 | 6014 | >= conf->previous_raid_disks) |
87a8dec9 | 6015 | set_bit(In_sync, &rdev->flags); |
9d4c7d87 | 6016 | else |
87a8dec9 | 6017 | rdev->recovery_offset = 0; |
36fad858 NK |
6018 | |
6019 | if (sysfs_link_rdev(mddev, rdev)) | |
87a8dec9 | 6020 | /* Failure here is OK */; |
50da0840 | 6021 | } |
87a8dec9 N |
6022 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
6023 | && !test_bit(Faulty, &rdev->flags)) { | |
6024 | /* This is a spare that was manually added */ | |
6025 | set_bit(In_sync, &rdev->flags); | |
87a8dec9 | 6026 | } |
29269553 | 6027 | |
87a8dec9 N |
6028 | /* When a reshape changes the number of devices, |
6029 | * ->degraded is measured against the larger of the | |
6030 | * pre and post number of devices. | |
6031 | */ | |
ec32a2bd | 6032 | spin_lock_irqsave(&conf->device_lock, flags); |
908f4fbd | 6033 | mddev->degraded = calc_degraded(conf); |
ec32a2bd N |
6034 | spin_unlock_irqrestore(&conf->device_lock, flags); |
6035 | } | |
63c70c4f | 6036 | mddev->raid_disks = conf->raid_disks; |
e516402c | 6037 | mddev->reshape_position = conf->reshape_progress; |
850b2b42 | 6038 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
f6705578 | 6039 | |
29269553 N |
6040 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
6041 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
6042 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
6043 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
6044 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
0da3c619 | 6045 | "reshape"); |
29269553 N |
6046 | if (!mddev->sync_thread) { |
6047 | mddev->recovery = 0; | |
6048 | spin_lock_irq(&conf->device_lock); | |
6049 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; | |
05616be5 N |
6050 | rdev_for_each(rdev, mddev) |
6051 | rdev->new_data_offset = rdev->data_offset; | |
6052 | smp_wmb(); | |
fef9c61f | 6053 | conf->reshape_progress = MaxSector; |
1e3fa9bd | 6054 | mddev->reshape_position = MaxSector; |
29269553 N |
6055 | spin_unlock_irq(&conf->device_lock); |
6056 | return -EAGAIN; | |
6057 | } | |
c8f517c4 | 6058 | conf->reshape_checkpoint = jiffies; |
29269553 N |
6059 | md_wakeup_thread(mddev->sync_thread); |
6060 | md_new_event(mddev); | |
6061 | return 0; | |
6062 | } | |
29269553 | 6063 | |
ec32a2bd N |
6064 | /* This is called from the reshape thread and should make any |
6065 | * changes needed in 'conf' | |
6066 | */ | |
d1688a6d | 6067 | static void end_reshape(struct r5conf *conf) |
29269553 | 6068 | { |
29269553 | 6069 | |
f6705578 | 6070 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
05616be5 | 6071 | struct md_rdev *rdev; |
f6705578 | 6072 | |
f6705578 | 6073 | spin_lock_irq(&conf->device_lock); |
cea9c228 | 6074 | conf->previous_raid_disks = conf->raid_disks; |
05616be5 N |
6075 | rdev_for_each(rdev, conf->mddev) |
6076 | rdev->data_offset = rdev->new_data_offset; | |
6077 | smp_wmb(); | |
fef9c61f | 6078 | conf->reshape_progress = MaxSector; |
f6705578 | 6079 | spin_unlock_irq(&conf->device_lock); |
b0f9ec04 | 6080 | wake_up(&conf->wait_for_overlap); |
16a53ecc N |
6081 | |
6082 | /* read-ahead size must cover two whole stripes, which is | |
6083 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices | |
6084 | */ | |
4a5add49 | 6085 | if (conf->mddev->queue) { |
cea9c228 | 6086 | int data_disks = conf->raid_disks - conf->max_degraded; |
09c9e5fa | 6087 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
cea9c228 | 6088 | / PAGE_SIZE); |
16a53ecc N |
6089 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
6090 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
6091 | } | |
29269553 | 6092 | } |
29269553 N |
6093 | } |
6094 | ||
ec32a2bd N |
6095 | /* This is called from the raid5d thread with mddev_lock held. |
6096 | * It makes config changes to the device. | |
6097 | */ | |
fd01b88c | 6098 | static void raid5_finish_reshape(struct mddev *mddev) |
cea9c228 | 6099 | { |
d1688a6d | 6100 | struct r5conf *conf = mddev->private; |
cea9c228 N |
6101 | |
6102 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
6103 | ||
ec32a2bd N |
6104 | if (mddev->delta_disks > 0) { |
6105 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); | |
6106 | set_capacity(mddev->gendisk, mddev->array_sectors); | |
449aad3e | 6107 | revalidate_disk(mddev->gendisk); |
ec32a2bd N |
6108 | } else { |
6109 | int d; | |
908f4fbd N |
6110 | spin_lock_irq(&conf->device_lock); |
6111 | mddev->degraded = calc_degraded(conf); | |
6112 | spin_unlock_irq(&conf->device_lock); | |
ec32a2bd N |
6113 | for (d = conf->raid_disks ; |
6114 | d < conf->raid_disks - mddev->delta_disks; | |
1a67dde0 | 6115 | d++) { |
3cb03002 | 6116 | struct md_rdev *rdev = conf->disks[d].rdev; |
da7613b8 N |
6117 | if (rdev) |
6118 | clear_bit(In_sync, &rdev->flags); | |
6119 | rdev = conf->disks[d].replacement; | |
6120 | if (rdev) | |
6121 | clear_bit(In_sync, &rdev->flags); | |
1a67dde0 | 6122 | } |
cea9c228 | 6123 | } |
88ce4930 | 6124 | mddev->layout = conf->algorithm; |
09c9e5fa | 6125 | mddev->chunk_sectors = conf->chunk_sectors; |
ec32a2bd N |
6126 | mddev->reshape_position = MaxSector; |
6127 | mddev->delta_disks = 0; | |
2c810cdd | 6128 | mddev->reshape_backwards = 0; |
cea9c228 N |
6129 | } |
6130 | } | |
6131 | ||
fd01b88c | 6132 | static void raid5_quiesce(struct mddev *mddev, int state) |
72626685 | 6133 | { |
d1688a6d | 6134 | struct r5conf *conf = mddev->private; |
72626685 N |
6135 | |
6136 | switch(state) { | |
e464eafd N |
6137 | case 2: /* resume for a suspend */ |
6138 | wake_up(&conf->wait_for_overlap); | |
6139 | break; | |
6140 | ||
72626685 N |
6141 | case 1: /* stop all writes */ |
6142 | spin_lock_irq(&conf->device_lock); | |
64bd660b N |
6143 | /* '2' tells resync/reshape to pause so that all |
6144 | * active stripes can drain | |
6145 | */ | |
6146 | conf->quiesce = 2; | |
72626685 | 6147 | wait_event_lock_irq(conf->wait_for_stripe, |
46031f9a RBJ |
6148 | atomic_read(&conf->active_stripes) == 0 && |
6149 | atomic_read(&conf->active_aligned_reads) == 0, | |
eed8c02e | 6150 | conf->device_lock); |
64bd660b | 6151 | conf->quiesce = 1; |
72626685 | 6152 | spin_unlock_irq(&conf->device_lock); |
64bd660b N |
6153 | /* allow reshape to continue */ |
6154 | wake_up(&conf->wait_for_overlap); | |
72626685 N |
6155 | break; |
6156 | ||
6157 | case 0: /* re-enable writes */ | |
6158 | spin_lock_irq(&conf->device_lock); | |
6159 | conf->quiesce = 0; | |
6160 | wake_up(&conf->wait_for_stripe); | |
e464eafd | 6161 | wake_up(&conf->wait_for_overlap); |
72626685 N |
6162 | spin_unlock_irq(&conf->device_lock); |
6163 | break; | |
6164 | } | |
72626685 | 6165 | } |
b15c2e57 | 6166 | |
d562b0c4 | 6167 | |
fd01b88c | 6168 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
54071b38 | 6169 | { |
e373ab10 | 6170 | struct r0conf *raid0_conf = mddev->private; |
d76c8420 | 6171 | sector_t sectors; |
54071b38 | 6172 | |
f1b29bca | 6173 | /* for raid0 takeover only one zone is supported */ |
e373ab10 | 6174 | if (raid0_conf->nr_strip_zones > 1) { |
0c55e022 N |
6175 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
6176 | mdname(mddev)); | |
f1b29bca DW |
6177 | return ERR_PTR(-EINVAL); |
6178 | } | |
6179 | ||
e373ab10 N |
6180 | sectors = raid0_conf->strip_zone[0].zone_end; |
6181 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); | |
3b71bd93 | 6182 | mddev->dev_sectors = sectors; |
f1b29bca | 6183 | mddev->new_level = level; |
54071b38 TM |
6184 | mddev->new_layout = ALGORITHM_PARITY_N; |
6185 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
6186 | mddev->raid_disks += 1; | |
6187 | mddev->delta_disks = 1; | |
6188 | /* make sure it will be not marked as dirty */ | |
6189 | mddev->recovery_cp = MaxSector; | |
6190 | ||
6191 | return setup_conf(mddev); | |
6192 | } | |
6193 | ||
6194 | ||
fd01b88c | 6195 | static void *raid5_takeover_raid1(struct mddev *mddev) |
d562b0c4 N |
6196 | { |
6197 | int chunksect; | |
6198 | ||
6199 | if (mddev->raid_disks != 2 || | |
6200 | mddev->degraded > 1) | |
6201 | return ERR_PTR(-EINVAL); | |
6202 | ||
6203 | /* Should check if there are write-behind devices? */ | |
6204 | ||
6205 | chunksect = 64*2; /* 64K by default */ | |
6206 | ||
6207 | /* The array must be an exact multiple of chunksize */ | |
6208 | while (chunksect && (mddev->array_sectors & (chunksect-1))) | |
6209 | chunksect >>= 1; | |
6210 | ||
6211 | if ((chunksect<<9) < STRIPE_SIZE) | |
6212 | /* array size does not allow a suitable chunk size */ | |
6213 | return ERR_PTR(-EINVAL); | |
6214 | ||
6215 | mddev->new_level = 5; | |
6216 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; | |
664e7c41 | 6217 | mddev->new_chunk_sectors = chunksect; |
d562b0c4 N |
6218 | |
6219 | return setup_conf(mddev); | |
6220 | } | |
6221 | ||
fd01b88c | 6222 | static void *raid5_takeover_raid6(struct mddev *mddev) |
fc9739c6 N |
6223 | { |
6224 | int new_layout; | |
6225 | ||
6226 | switch (mddev->layout) { | |
6227 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
6228 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; | |
6229 | break; | |
6230 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
6231 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; | |
6232 | break; | |
6233 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
6234 | new_layout = ALGORITHM_LEFT_SYMMETRIC; | |
6235 | break; | |
6236 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
6237 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; | |
6238 | break; | |
6239 | case ALGORITHM_PARITY_0_6: | |
6240 | new_layout = ALGORITHM_PARITY_0; | |
6241 | break; | |
6242 | case ALGORITHM_PARITY_N: | |
6243 | new_layout = ALGORITHM_PARITY_N; | |
6244 | break; | |
6245 | default: | |
6246 | return ERR_PTR(-EINVAL); | |
6247 | } | |
6248 | mddev->new_level = 5; | |
6249 | mddev->new_layout = new_layout; | |
6250 | mddev->delta_disks = -1; | |
6251 | mddev->raid_disks -= 1; | |
6252 | return setup_conf(mddev); | |
6253 | } | |
6254 | ||
d562b0c4 | 6255 | |
fd01b88c | 6256 | static int raid5_check_reshape(struct mddev *mddev) |
b3546035 | 6257 | { |
88ce4930 N |
6258 | /* For a 2-drive array, the layout and chunk size can be changed |
6259 | * immediately as not restriping is needed. | |
6260 | * For larger arrays we record the new value - after validation | |
6261 | * to be used by a reshape pass. | |
b3546035 | 6262 | */ |
d1688a6d | 6263 | struct r5conf *conf = mddev->private; |
597a711b | 6264 | int new_chunk = mddev->new_chunk_sectors; |
b3546035 | 6265 | |
597a711b | 6266 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
b3546035 N |
6267 | return -EINVAL; |
6268 | if (new_chunk > 0) { | |
0ba459d2 | 6269 | if (!is_power_of_2(new_chunk)) |
b3546035 | 6270 | return -EINVAL; |
597a711b | 6271 | if (new_chunk < (PAGE_SIZE>>9)) |
b3546035 | 6272 | return -EINVAL; |
597a711b | 6273 | if (mddev->array_sectors & (new_chunk-1)) |
b3546035 N |
6274 | /* not factor of array size */ |
6275 | return -EINVAL; | |
6276 | } | |
6277 | ||
6278 | /* They look valid */ | |
6279 | ||
88ce4930 | 6280 | if (mddev->raid_disks == 2) { |
597a711b N |
6281 | /* can make the change immediately */ |
6282 | if (mddev->new_layout >= 0) { | |
6283 | conf->algorithm = mddev->new_layout; | |
6284 | mddev->layout = mddev->new_layout; | |
88ce4930 N |
6285 | } |
6286 | if (new_chunk > 0) { | |
597a711b N |
6287 | conf->chunk_sectors = new_chunk ; |
6288 | mddev->chunk_sectors = new_chunk; | |
88ce4930 N |
6289 | } |
6290 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
6291 | md_wakeup_thread(mddev->thread); | |
b3546035 | 6292 | } |
50ac168a | 6293 | return check_reshape(mddev); |
88ce4930 N |
6294 | } |
6295 | ||
fd01b88c | 6296 | static int raid6_check_reshape(struct mddev *mddev) |
88ce4930 | 6297 | { |
597a711b | 6298 | int new_chunk = mddev->new_chunk_sectors; |
50ac168a | 6299 | |
597a711b | 6300 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
88ce4930 | 6301 | return -EINVAL; |
b3546035 | 6302 | if (new_chunk > 0) { |
0ba459d2 | 6303 | if (!is_power_of_2(new_chunk)) |
88ce4930 | 6304 | return -EINVAL; |
597a711b | 6305 | if (new_chunk < (PAGE_SIZE >> 9)) |
88ce4930 | 6306 | return -EINVAL; |
597a711b | 6307 | if (mddev->array_sectors & (new_chunk-1)) |
88ce4930 N |
6308 | /* not factor of array size */ |
6309 | return -EINVAL; | |
b3546035 | 6310 | } |
88ce4930 N |
6311 | |
6312 | /* They look valid */ | |
50ac168a | 6313 | return check_reshape(mddev); |
b3546035 N |
6314 | } |
6315 | ||
fd01b88c | 6316 | static void *raid5_takeover(struct mddev *mddev) |
d562b0c4 N |
6317 | { |
6318 | /* raid5 can take over: | |
f1b29bca | 6319 | * raid0 - if there is only one strip zone - make it a raid4 layout |
d562b0c4 N |
6320 | * raid1 - if there are two drives. We need to know the chunk size |
6321 | * raid4 - trivial - just use a raid4 layout. | |
6322 | * raid6 - Providing it is a *_6 layout | |
d562b0c4 | 6323 | */ |
f1b29bca DW |
6324 | if (mddev->level == 0) |
6325 | return raid45_takeover_raid0(mddev, 5); | |
d562b0c4 N |
6326 | if (mddev->level == 1) |
6327 | return raid5_takeover_raid1(mddev); | |
e9d4758f N |
6328 | if (mddev->level == 4) { |
6329 | mddev->new_layout = ALGORITHM_PARITY_N; | |
6330 | mddev->new_level = 5; | |
6331 | return setup_conf(mddev); | |
6332 | } | |
fc9739c6 N |
6333 | if (mddev->level == 6) |
6334 | return raid5_takeover_raid6(mddev); | |
d562b0c4 N |
6335 | |
6336 | return ERR_PTR(-EINVAL); | |
6337 | } | |
6338 | ||
fd01b88c | 6339 | static void *raid4_takeover(struct mddev *mddev) |
a78d38a1 | 6340 | { |
f1b29bca DW |
6341 | /* raid4 can take over: |
6342 | * raid0 - if there is only one strip zone | |
6343 | * raid5 - if layout is right | |
a78d38a1 | 6344 | */ |
f1b29bca DW |
6345 | if (mddev->level == 0) |
6346 | return raid45_takeover_raid0(mddev, 4); | |
a78d38a1 N |
6347 | if (mddev->level == 5 && |
6348 | mddev->layout == ALGORITHM_PARITY_N) { | |
6349 | mddev->new_layout = 0; | |
6350 | mddev->new_level = 4; | |
6351 | return setup_conf(mddev); | |
6352 | } | |
6353 | return ERR_PTR(-EINVAL); | |
6354 | } | |
d562b0c4 | 6355 | |
84fc4b56 | 6356 | static struct md_personality raid5_personality; |
245f46c2 | 6357 | |
fd01b88c | 6358 | static void *raid6_takeover(struct mddev *mddev) |
245f46c2 N |
6359 | { |
6360 | /* Currently can only take over a raid5. We map the | |
6361 | * personality to an equivalent raid6 personality | |
6362 | * with the Q block at the end. | |
6363 | */ | |
6364 | int new_layout; | |
6365 | ||
6366 | if (mddev->pers != &raid5_personality) | |
6367 | return ERR_PTR(-EINVAL); | |
6368 | if (mddev->degraded > 1) | |
6369 | return ERR_PTR(-EINVAL); | |
6370 | if (mddev->raid_disks > 253) | |
6371 | return ERR_PTR(-EINVAL); | |
6372 | if (mddev->raid_disks < 3) | |
6373 | return ERR_PTR(-EINVAL); | |
6374 | ||
6375 | switch (mddev->layout) { | |
6376 | case ALGORITHM_LEFT_ASYMMETRIC: | |
6377 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; | |
6378 | break; | |
6379 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
6380 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; | |
6381 | break; | |
6382 | case ALGORITHM_LEFT_SYMMETRIC: | |
6383 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; | |
6384 | break; | |
6385 | case ALGORITHM_RIGHT_SYMMETRIC: | |
6386 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; | |
6387 | break; | |
6388 | case ALGORITHM_PARITY_0: | |
6389 | new_layout = ALGORITHM_PARITY_0_6; | |
6390 | break; | |
6391 | case ALGORITHM_PARITY_N: | |
6392 | new_layout = ALGORITHM_PARITY_N; | |
6393 | break; | |
6394 | default: | |
6395 | return ERR_PTR(-EINVAL); | |
6396 | } | |
6397 | mddev->new_level = 6; | |
6398 | mddev->new_layout = new_layout; | |
6399 | mddev->delta_disks = 1; | |
6400 | mddev->raid_disks += 1; | |
6401 | return setup_conf(mddev); | |
6402 | } | |
6403 | ||
6404 | ||
84fc4b56 | 6405 | static struct md_personality raid6_personality = |
16a53ecc N |
6406 | { |
6407 | .name = "raid6", | |
6408 | .level = 6, | |
6409 | .owner = THIS_MODULE, | |
6410 | .make_request = make_request, | |
6411 | .run = run, | |
6412 | .stop = stop, | |
6413 | .status = status, | |
6414 | .error_handler = error, | |
6415 | .hot_add_disk = raid5_add_disk, | |
6416 | .hot_remove_disk= raid5_remove_disk, | |
6417 | .spare_active = raid5_spare_active, | |
6418 | .sync_request = sync_request, | |
6419 | .resize = raid5_resize, | |
80c3a6ce | 6420 | .size = raid5_size, |
50ac168a | 6421 | .check_reshape = raid6_check_reshape, |
f416885e | 6422 | .start_reshape = raid5_start_reshape, |
cea9c228 | 6423 | .finish_reshape = raid5_finish_reshape, |
16a53ecc | 6424 | .quiesce = raid5_quiesce, |
245f46c2 | 6425 | .takeover = raid6_takeover, |
16a53ecc | 6426 | }; |
84fc4b56 | 6427 | static struct md_personality raid5_personality = |
1da177e4 LT |
6428 | { |
6429 | .name = "raid5", | |
2604b703 | 6430 | .level = 5, |
1da177e4 LT |
6431 | .owner = THIS_MODULE, |
6432 | .make_request = make_request, | |
6433 | .run = run, | |
6434 | .stop = stop, | |
6435 | .status = status, | |
6436 | .error_handler = error, | |
6437 | .hot_add_disk = raid5_add_disk, | |
6438 | .hot_remove_disk= raid5_remove_disk, | |
6439 | .spare_active = raid5_spare_active, | |
6440 | .sync_request = sync_request, | |
6441 | .resize = raid5_resize, | |
80c3a6ce | 6442 | .size = raid5_size, |
63c70c4f N |
6443 | .check_reshape = raid5_check_reshape, |
6444 | .start_reshape = raid5_start_reshape, | |
cea9c228 | 6445 | .finish_reshape = raid5_finish_reshape, |
72626685 | 6446 | .quiesce = raid5_quiesce, |
d562b0c4 | 6447 | .takeover = raid5_takeover, |
1da177e4 LT |
6448 | }; |
6449 | ||
84fc4b56 | 6450 | static struct md_personality raid4_personality = |
1da177e4 | 6451 | { |
2604b703 N |
6452 | .name = "raid4", |
6453 | .level = 4, | |
6454 | .owner = THIS_MODULE, | |
6455 | .make_request = make_request, | |
6456 | .run = run, | |
6457 | .stop = stop, | |
6458 | .status = status, | |
6459 | .error_handler = error, | |
6460 | .hot_add_disk = raid5_add_disk, | |
6461 | .hot_remove_disk= raid5_remove_disk, | |
6462 | .spare_active = raid5_spare_active, | |
6463 | .sync_request = sync_request, | |
6464 | .resize = raid5_resize, | |
80c3a6ce | 6465 | .size = raid5_size, |
3d37890b N |
6466 | .check_reshape = raid5_check_reshape, |
6467 | .start_reshape = raid5_start_reshape, | |
cea9c228 | 6468 | .finish_reshape = raid5_finish_reshape, |
2604b703 | 6469 | .quiesce = raid5_quiesce, |
a78d38a1 | 6470 | .takeover = raid4_takeover, |
2604b703 N |
6471 | }; |
6472 | ||
6473 | static int __init raid5_init(void) | |
6474 | { | |
16a53ecc | 6475 | register_md_personality(&raid6_personality); |
2604b703 N |
6476 | register_md_personality(&raid5_personality); |
6477 | register_md_personality(&raid4_personality); | |
6478 | return 0; | |
1da177e4 LT |
6479 | } |
6480 | ||
2604b703 | 6481 | static void raid5_exit(void) |
1da177e4 | 6482 | { |
16a53ecc | 6483 | unregister_md_personality(&raid6_personality); |
2604b703 N |
6484 | unregister_md_personality(&raid5_personality); |
6485 | unregister_md_personality(&raid4_personality); | |
1da177e4 LT |
6486 | } |
6487 | ||
6488 | module_init(raid5_init); | |
6489 | module_exit(raid5_exit); | |
6490 | MODULE_LICENSE("GPL"); | |
0efb9e61 | 6491 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
1da177e4 | 6492 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
d9d166c2 N |
6493 | MODULE_ALIAS("md-raid5"); |
6494 | MODULE_ALIAS("md-raid4"); | |
2604b703 N |
6495 | MODULE_ALIAS("md-level-5"); |
6496 | MODULE_ALIAS("md-level-4"); | |
16a53ecc N |
6497 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
6498 | MODULE_ALIAS("md-raid6"); | |
6499 | MODULE_ALIAS("md-level-6"); | |
6500 | ||
6501 | /* This used to be two separate modules, they were: */ | |
6502 | MODULE_ALIAS("raid5"); | |
6503 | MODULE_ALIAS("raid6"); |