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