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