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