Commit | Line | Data |
---|---|---|
af1a8899 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * raid5.c : Multiple Devices driver for Linux | |
4 | * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman | |
5 | * Copyright (C) 1999, 2000 Ingo Molnar | |
16a53ecc | 6 | * Copyright (C) 2002, 2003 H. Peter Anvin |
1da177e4 | 7 | * |
16a53ecc N |
8 | * RAID-4/5/6 management functions. |
9 | * Thanks to Penguin Computing for making the RAID-6 development possible | |
10 | * by donating a test server! | |
1da177e4 LT |
11 | */ |
12 | ||
ae3c20cc N |
13 | /* |
14 | * BITMAP UNPLUGGING: | |
15 | * | |
16 | * The sequencing for updating the bitmap reliably is a little | |
17 | * subtle (and I got it wrong the first time) so it deserves some | |
18 | * explanation. | |
19 | * | |
20 | * We group bitmap updates into batches. Each batch has a number. | |
21 | * We may write out several batches at once, but that isn't very important. | |
7c13edc8 N |
22 | * conf->seq_write is the number of the last batch successfully written. |
23 | * conf->seq_flush is the number of the last batch that was closed to | |
ae3c20cc N |
24 | * new additions. |
25 | * When we discover that we will need to write to any block in a stripe | |
26 | * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq | |
7c13edc8 | 27 | * the number of the batch it will be in. This is seq_flush+1. |
ae3c20cc N |
28 | * When we are ready to do a write, if that batch hasn't been written yet, |
29 | * we plug the array and queue the stripe for later. | |
30 | * When an unplug happens, we increment bm_flush, thus closing the current | |
31 | * batch. | |
32 | * When we notice that bm_flush > bm_write, we write out all pending updates | |
33 | * to the bitmap, and advance bm_write to where bm_flush was. | |
34 | * This may occasionally write a bit out twice, but is sure never to | |
35 | * miss any bits. | |
36 | */ | |
1da177e4 | 37 | |
bff61975 | 38 | #include <linux/blkdev.h> |
f6705578 | 39 | #include <linux/kthread.h> |
f701d589 | 40 | #include <linux/raid/pq.h> |
91c00924 | 41 | #include <linux/async_tx.h> |
056075c7 | 42 | #include <linux/module.h> |
07a3b417 | 43 | #include <linux/async.h> |
bff61975 | 44 | #include <linux/seq_file.h> |
36d1c647 | 45 | #include <linux/cpu.h> |
5a0e3ad6 | 46 | #include <linux/slab.h> |
8bda470e | 47 | #include <linux/ratelimit.h> |
851c30c9 | 48 | #include <linux/nodemask.h> |
3f07c014 | 49 | |
a9add5d9 | 50 | #include <trace/events/block.h> |
aaf9f12e | 51 | #include <linux/list_sort.h> |
a9add5d9 | 52 | |
43b2e5d8 | 53 | #include "md.h" |
bff61975 | 54 | #include "raid5.h" |
54071b38 | 55 | #include "raid0.h" |
935fe098 | 56 | #include "md-bitmap.h" |
ff875738 | 57 | #include "raid5-log.h" |
72626685 | 58 | |
394ed8e4 SL |
59 | #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED) |
60 | ||
851c30c9 SL |
61 | #define cpu_to_group(cpu) cpu_to_node(cpu) |
62 | #define ANY_GROUP NUMA_NO_NODE | |
63 | ||
7e55c60a LG |
64 | #define RAID5_MAX_REQ_STRIPES 256 |
65 | ||
8e0e99ba N |
66 | static bool devices_handle_discard_safely = false; |
67 | module_param(devices_handle_discard_safely, bool, 0644); | |
68 | MODULE_PARM_DESC(devices_handle_discard_safely, | |
69 | "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions"); | |
851c30c9 | 70 | static struct workqueue_struct *raid5_wq; |
1da177e4 | 71 | |
b42cd7b3 YK |
72 | static void raid5_quiesce(struct mddev *mddev, int quiesce); |
73 | ||
d1688a6d | 74 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
db298e19 | 75 | { |
c911c46c | 76 | int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK; |
db298e19 N |
77 | return &conf->stripe_hashtbl[hash]; |
78 | } | |
1da177e4 | 79 | |
c911c46c | 80 | static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect) |
566c09c5 | 81 | { |
c911c46c | 82 | return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK; |
566c09c5 SL |
83 | } |
84 | ||
85 | static inline void lock_device_hash_lock(struct r5conf *conf, int hash) | |
4631f39f | 86 | __acquires(&conf->device_lock) |
566c09c5 SL |
87 | { |
88 | spin_lock_irq(conf->hash_locks + hash); | |
89 | spin_lock(&conf->device_lock); | |
90 | } | |
91 | ||
92 | static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) | |
4631f39f | 93 | __releases(&conf->device_lock) |
566c09c5 SL |
94 | { |
95 | spin_unlock(&conf->device_lock); | |
96 | spin_unlock_irq(conf->hash_locks + hash); | |
97 | } | |
98 | ||
99 | static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) | |
4631f39f | 100 | __acquires(&conf->device_lock) |
566c09c5 SL |
101 | { |
102 | int i; | |
3d05f3ae | 103 | spin_lock_irq(conf->hash_locks); |
566c09c5 SL |
104 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
105 | spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); | |
106 | spin_lock(&conf->device_lock); | |
107 | } | |
108 | ||
109 | static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) | |
4631f39f | 110 | __releases(&conf->device_lock) |
566c09c5 SL |
111 | { |
112 | int i; | |
113 | spin_unlock(&conf->device_lock); | |
3d05f3ae JC |
114 | for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--) |
115 | spin_unlock(conf->hash_locks + i); | |
116 | spin_unlock_irq(conf->hash_locks); | |
566c09c5 SL |
117 | } |
118 | ||
d0dabf7e N |
119 | /* Find first data disk in a raid6 stripe */ |
120 | static inline int raid6_d0(struct stripe_head *sh) | |
121 | { | |
67cc2b81 N |
122 | if (sh->ddf_layout) |
123 | /* ddf always start from first device */ | |
124 | return 0; | |
125 | /* md starts just after Q block */ | |
d0dabf7e N |
126 | if (sh->qd_idx == sh->disks - 1) |
127 | return 0; | |
128 | else | |
129 | return sh->qd_idx + 1; | |
130 | } | |
16a53ecc N |
131 | static inline int raid6_next_disk(int disk, int raid_disks) |
132 | { | |
133 | disk++; | |
134 | return (disk < raid_disks) ? disk : 0; | |
135 | } | |
a4456856 | 136 | |
d0dabf7e N |
137 | /* When walking through the disks in a raid5, starting at raid6_d0, |
138 | * We need to map each disk to a 'slot', where the data disks are slot | |
139 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk | |
140 | * is raid_disks-1. This help does that mapping. | |
141 | */ | |
67cc2b81 N |
142 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
143 | int *count, int syndrome_disks) | |
d0dabf7e | 144 | { |
6629542e | 145 | int slot = *count; |
67cc2b81 | 146 | |
e4424fee | 147 | if (sh->ddf_layout) |
6629542e | 148 | (*count)++; |
d0dabf7e | 149 | if (idx == sh->pd_idx) |
67cc2b81 | 150 | return syndrome_disks; |
d0dabf7e | 151 | if (idx == sh->qd_idx) |
67cc2b81 | 152 | return syndrome_disks + 1; |
e4424fee | 153 | if (!sh->ddf_layout) |
6629542e | 154 | (*count)++; |
d0dabf7e N |
155 | return slot; |
156 | } | |
157 | ||
2314c2e3 | 158 | static void print_raid5_conf(struct r5conf *conf); |
1da177e4 | 159 | |
600aa109 DW |
160 | static int stripe_operations_active(struct stripe_head *sh) |
161 | { | |
162 | return sh->check_state || sh->reconstruct_state || | |
163 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || | |
164 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
165 | } | |
166 | ||
535ae4eb SL |
167 | static bool stripe_is_lowprio(struct stripe_head *sh) |
168 | { | |
169 | return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) || | |
170 | test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) && | |
171 | !test_bit(STRIPE_R5C_CACHING, &sh->state); | |
172 | } | |
173 | ||
851c30c9 | 174 | static void raid5_wakeup_stripe_thread(struct stripe_head *sh) |
4631f39f | 175 | __must_hold(&sh->raid_conf->device_lock) |
851c30c9 SL |
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); | |
535ae4eb SL |
190 | if (stripe_is_lowprio(sh)) |
191 | list_add_tail(&sh->lru, &group->loprio_list); | |
192 | else | |
193 | list_add_tail(&sh->lru, &group->handle_list); | |
bfc90cb0 SL |
194 | group->stripes_cnt++; |
195 | sh->group = group; | |
851c30c9 SL |
196 | } |
197 | ||
198 | if (conf->worker_cnt_per_group == 0) { | |
199 | md_wakeup_thread(conf->mddev->thread); | |
200 | return; | |
201 | } | |
202 | ||
203 | group = conf->worker_groups + cpu_to_group(sh->cpu); | |
204 | ||
bfc90cb0 SL |
205 | group->workers[0].working = true; |
206 | /* at least one worker should run to avoid race */ | |
207 | queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); | |
208 | ||
209 | thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; | |
210 | /* wakeup more workers */ | |
211 | for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { | |
212 | if (group->workers[i].working == false) { | |
213 | group->workers[i].working = true; | |
214 | queue_work_on(sh->cpu, raid5_wq, | |
215 | &group->workers[i].work); | |
216 | thread_cnt--; | |
217 | } | |
218 | } | |
851c30c9 SL |
219 | } |
220 | ||
566c09c5 SL |
221 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, |
222 | struct list_head *temp_inactive_list) | |
4631f39f | 223 | __must_hold(&conf->device_lock) |
1da177e4 | 224 | { |
1e6d690b SL |
225 | int i; |
226 | int injournal = 0; /* number of date pages with R5_InJournal */ | |
227 | ||
4eb788df SL |
228 | BUG_ON(!list_empty(&sh->lru)); |
229 | BUG_ON(atomic_read(&conf->active_stripes)==0); | |
1e6d690b SL |
230 | |
231 | if (r5c_is_writeback(conf->log)) | |
232 | for (i = sh->disks; i--; ) | |
233 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) | |
234 | injournal++; | |
a39f7afd | 235 | /* |
5ddf0440 SL |
236 | * In the following cases, the stripe cannot be released to cached |
237 | * lists. Therefore, we make the stripe write out and set | |
238 | * STRIPE_HANDLE: | |
239 | * 1. when quiesce in r5c write back; | |
240 | * 2. when resync is requested fot the stripe. | |
a39f7afd | 241 | */ |
5ddf0440 SL |
242 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) || |
243 | (conf->quiesce && r5c_is_writeback(conf->log) && | |
244 | !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) { | |
a39f7afd SL |
245 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
246 | r5c_make_stripe_write_out(sh); | |
247 | set_bit(STRIPE_HANDLE, &sh->state); | |
248 | } | |
1e6d690b | 249 | |
4eb788df SL |
250 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
251 | if (test_bit(STRIPE_DELAYED, &sh->state) && | |
ad3ab8b6 | 252 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
4eb788df | 253 | list_add_tail(&sh->lru, &conf->delayed_list); |
ad3ab8b6 | 254 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
4eb788df SL |
255 | sh->bm_seq - conf->seq_write > 0) |
256 | list_add_tail(&sh->lru, &conf->bitmap_list); | |
257 | else { | |
258 | clear_bit(STRIPE_DELAYED, &sh->state); | |
259 | clear_bit(STRIPE_BIT_DELAY, &sh->state); | |
851c30c9 | 260 | if (conf->worker_cnt_per_group == 0) { |
535ae4eb SL |
261 | if (stripe_is_lowprio(sh)) |
262 | list_add_tail(&sh->lru, | |
263 | &conf->loprio_list); | |
264 | else | |
265 | list_add_tail(&sh->lru, | |
266 | &conf->handle_list); | |
851c30c9 SL |
267 | } else { |
268 | raid5_wakeup_stripe_thread(sh); | |
269 | return; | |
270 | } | |
4eb788df SL |
271 | } |
272 | md_wakeup_thread(conf->mddev->thread); | |
273 | } else { | |
274 | BUG_ON(stripe_operations_active(sh)); | |
275 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
276 | if (atomic_dec_return(&conf->preread_active_stripes) | |
277 | < IO_THRESHOLD) | |
278 | md_wakeup_thread(conf->mddev->thread); | |
279 | atomic_dec(&conf->active_stripes); | |
1e6d690b SL |
280 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
281 | if (!r5c_is_writeback(conf->log)) | |
282 | list_add_tail(&sh->lru, temp_inactive_list); | |
283 | else { | |
284 | WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)); | |
285 | if (injournal == 0) | |
286 | list_add_tail(&sh->lru, temp_inactive_list); | |
287 | else if (injournal == conf->raid_disks - conf->max_degraded) { | |
288 | /* full stripe */ | |
289 | if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) | |
290 | atomic_inc(&conf->r5c_cached_full_stripes); | |
291 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) | |
292 | atomic_dec(&conf->r5c_cached_partial_stripes); | |
293 | list_add_tail(&sh->lru, &conf->r5c_full_stripe_list); | |
a39f7afd | 294 | r5c_check_cached_full_stripe(conf); |
03b047f4 SL |
295 | } else |
296 | /* | |
297 | * STRIPE_R5C_PARTIAL_STRIPE is set in | |
298 | * r5c_try_caching_write(). No need to | |
299 | * set it again. | |
300 | */ | |
1e6d690b | 301 | list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list); |
1e6d690b SL |
302 | } |
303 | } | |
1da177e4 LT |
304 | } |
305 | } | |
d0dabf7e | 306 | |
566c09c5 SL |
307 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, |
308 | struct list_head *temp_inactive_list) | |
4631f39f | 309 | __must_hold(&conf->device_lock) |
4eb788df SL |
310 | { |
311 | if (atomic_dec_and_test(&sh->count)) | |
566c09c5 SL |
312 | do_release_stripe(conf, sh, temp_inactive_list); |
313 | } | |
314 | ||
315 | /* | |
316 | * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list | |
317 | * | |
318 | * Be careful: Only one task can add/delete stripes from temp_inactive_list at | |
319 | * given time. Adding stripes only takes device lock, while deleting stripes | |
320 | * only takes hash lock. | |
321 | */ | |
322 | static void release_inactive_stripe_list(struct r5conf *conf, | |
323 | struct list_head *temp_inactive_list, | |
324 | int hash) | |
325 | { | |
326 | int size; | |
6ab2a4b8 | 327 | bool do_wakeup = false; |
566c09c5 SL |
328 | unsigned long flags; |
329 | ||
330 | if (hash == NR_STRIPE_HASH_LOCKS) { | |
331 | size = NR_STRIPE_HASH_LOCKS; | |
332 | hash = NR_STRIPE_HASH_LOCKS - 1; | |
333 | } else | |
334 | size = 1; | |
335 | while (size) { | |
336 | struct list_head *list = &temp_inactive_list[size - 1]; | |
337 | ||
338 | /* | |
6d036f7d | 339 | * We don't hold any lock here yet, raid5_get_active_stripe() might |
566c09c5 SL |
340 | * remove stripes from the list |
341 | */ | |
342 | if (!list_empty_careful(list)) { | |
343 | spin_lock_irqsave(conf->hash_locks + hash, flags); | |
4bda556a SL |
344 | if (list_empty(conf->inactive_list + hash) && |
345 | !list_empty(list)) | |
346 | atomic_dec(&conf->empty_inactive_list_nr); | |
566c09c5 | 347 | list_splice_tail_init(list, conf->inactive_list + hash); |
6ab2a4b8 | 348 | do_wakeup = true; |
566c09c5 SL |
349 | spin_unlock_irqrestore(conf->hash_locks + hash, flags); |
350 | } | |
351 | size--; | |
352 | hash--; | |
353 | } | |
354 | ||
355 | if (do_wakeup) { | |
6ab2a4b8 | 356 | wake_up(&conf->wait_for_stripe); |
b1b46486 YL |
357 | if (atomic_read(&conf->active_stripes) == 0) |
358 | wake_up(&conf->wait_for_quiescent); | |
566c09c5 SL |
359 | if (conf->retry_read_aligned) |
360 | md_wakeup_thread(conf->mddev->thread); | |
361 | } | |
4eb788df SL |
362 | } |
363 | ||
566c09c5 SL |
364 | static int release_stripe_list(struct r5conf *conf, |
365 | struct list_head *temp_inactive_list) | |
4631f39f | 366 | __must_hold(&conf->device_lock) |
773ca82f | 367 | { |
eae8263f | 368 | struct stripe_head *sh, *t; |
773ca82f SL |
369 | int count = 0; |
370 | struct llist_node *head; | |
371 | ||
372 | head = llist_del_all(&conf->released_stripes); | |
d265d9dc | 373 | head = llist_reverse_order(head); |
eae8263f | 374 | llist_for_each_entry_safe(sh, t, head, release_list) { |
566c09c5 SL |
375 | int hash; |
376 | ||
773ca82f SL |
377 | /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */ |
378 | smp_mb(); | |
379 | clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state); | |
380 | /* | |
381 | * Don't worry the bit is set here, because if the bit is set | |
382 | * again, the count is always > 1. This is true for | |
383 | * STRIPE_ON_UNPLUG_LIST bit too. | |
384 | */ | |
566c09c5 SL |
385 | hash = sh->hash_lock_index; |
386 | __release_stripe(conf, sh, &temp_inactive_list[hash]); | |
773ca82f SL |
387 | count++; |
388 | } | |
389 | ||
390 | return count; | |
391 | } | |
392 | ||
6d036f7d | 393 | void raid5_release_stripe(struct stripe_head *sh) |
1da177e4 | 394 | { |
d1688a6d | 395 | struct r5conf *conf = sh->raid_conf; |
1da177e4 | 396 | unsigned long flags; |
566c09c5 SL |
397 | struct list_head list; |
398 | int hash; | |
773ca82f | 399 | bool wakeup; |
16a53ecc | 400 | |
cf170f3f ES |
401 | /* Avoid release_list until the last reference. |
402 | */ | |
403 | if (atomic_add_unless(&sh->count, -1, 1)) | |
404 | return; | |
405 | ||
ad4068de | 406 | if (unlikely(!conf->mddev->thread) || |
407 | test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state)) | |
773ca82f SL |
408 | goto slow_path; |
409 | wakeup = llist_add(&sh->release_list, &conf->released_stripes); | |
410 | if (wakeup) | |
411 | md_wakeup_thread(conf->mddev->thread); | |
412 | return; | |
413 | slow_path: | |
773ca82f | 414 | /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */ |
685dbcaa | 415 | if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) { |
566c09c5 SL |
416 | INIT_LIST_HEAD(&list); |
417 | hash = sh->hash_lock_index; | |
418 | do_release_stripe(conf, sh, &list); | |
08edaaa6 | 419 | spin_unlock_irqrestore(&conf->device_lock, flags); |
566c09c5 | 420 | release_inactive_stripe_list(conf, &list, hash); |
4eb788df | 421 | } |
1da177e4 LT |
422 | } |
423 | ||
fccddba0 | 424 | static inline void remove_hash(struct stripe_head *sh) |
1da177e4 | 425 | { |
45b4233c DW |
426 | pr_debug("remove_hash(), stripe %llu\n", |
427 | (unsigned long long)sh->sector); | |
1da177e4 | 428 | |
fccddba0 | 429 | hlist_del_init(&sh->hash); |
1da177e4 LT |
430 | } |
431 | ||
d1688a6d | 432 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
1da177e4 | 433 | { |
fccddba0 | 434 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
1da177e4 | 435 | |
45b4233c DW |
436 | pr_debug("insert_hash(), stripe %llu\n", |
437 | (unsigned long long)sh->sector); | |
1da177e4 | 438 | |
fccddba0 | 439 | hlist_add_head(&sh->hash, hp); |
1da177e4 LT |
440 | } |
441 | ||
1da177e4 | 442 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
566c09c5 | 443 | static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash) |
1da177e4 LT |
444 | { |
445 | struct stripe_head *sh = NULL; | |
446 | struct list_head *first; | |
447 | ||
566c09c5 | 448 | if (list_empty(conf->inactive_list + hash)) |
1da177e4 | 449 | goto out; |
566c09c5 | 450 | first = (conf->inactive_list + hash)->next; |
1da177e4 LT |
451 | sh = list_entry(first, struct stripe_head, lru); |
452 | list_del_init(first); | |
453 | remove_hash(sh); | |
454 | atomic_inc(&conf->active_stripes); | |
566c09c5 | 455 | BUG_ON(hash != sh->hash_lock_index); |
4bda556a SL |
456 | if (list_empty(conf->inactive_list + hash)) |
457 | atomic_inc(&conf->empty_inactive_list_nr); | |
1da177e4 LT |
458 | out: |
459 | return sh; | |
460 | } | |
461 | ||
046169f0 YY |
462 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
463 | static void free_stripe_pages(struct stripe_head *sh) | |
1da177e4 | 464 | { |
046169f0 | 465 | int i; |
1da177e4 | 466 | struct page *p; |
046169f0 YY |
467 | |
468 | /* Have not allocate page pool */ | |
469 | if (!sh->pages) | |
470 | return; | |
471 | ||
472 | for (i = 0; i < sh->nr_pages; i++) { | |
473 | p = sh->pages[i]; | |
474 | if (p) | |
475 | put_page(p); | |
476 | sh->pages[i] = NULL; | |
477 | } | |
478 | } | |
479 | ||
480 | static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp) | |
481 | { | |
482 | int i; | |
483 | struct page *p; | |
484 | ||
485 | for (i = 0; i < sh->nr_pages; i++) { | |
486 | /* The page have allocated. */ | |
487 | if (sh->pages[i]) | |
488 | continue; | |
489 | ||
490 | p = alloc_page(gfp); | |
491 | if (!p) { | |
492 | free_stripe_pages(sh); | |
493 | return -ENOMEM; | |
494 | } | |
495 | sh->pages[i] = p; | |
496 | } | |
497 | return 0; | |
498 | } | |
499 | ||
500 | static int | |
501 | init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks) | |
502 | { | |
503 | int nr_pages, cnt; | |
504 | ||
505 | if (sh->pages) | |
506 | return 0; | |
507 | ||
508 | /* Each of the sh->dev[i] need one conf->stripe_size */ | |
509 | cnt = PAGE_SIZE / conf->stripe_size; | |
510 | nr_pages = (disks + cnt - 1) / cnt; | |
511 | ||
512 | sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
513 | if (!sh->pages) | |
514 | return -ENOMEM; | |
515 | sh->nr_pages = nr_pages; | |
516 | sh->stripes_per_page = cnt; | |
517 | return 0; | |
518 | } | |
519 | #endif | |
520 | ||
521 | static void shrink_buffers(struct stripe_head *sh) | |
522 | { | |
1da177e4 | 523 | int i; |
e4e11e38 | 524 | int num = sh->raid_conf->pool_size; |
1da177e4 | 525 | |
046169f0 | 526 | #if PAGE_SIZE == DEFAULT_STRIPE_SIZE |
e4e11e38 | 527 | for (i = 0; i < num ; i++) { |
046169f0 YY |
528 | struct page *p; |
529 | ||
d592a996 | 530 | WARN_ON(sh->dev[i].page != sh->dev[i].orig_page); |
1da177e4 LT |
531 | p = sh->dev[i].page; |
532 | if (!p) | |
533 | continue; | |
534 | sh->dev[i].page = NULL; | |
2d1f3b5d | 535 | put_page(p); |
1da177e4 | 536 | } |
046169f0 YY |
537 | #else |
538 | for (i = 0; i < num; i++) | |
539 | sh->dev[i].page = NULL; | |
540 | free_stripe_pages(sh); /* Free pages */ | |
541 | #endif | |
1da177e4 LT |
542 | } |
543 | ||
a9683a79 | 544 | static int grow_buffers(struct stripe_head *sh, gfp_t gfp) |
1da177e4 LT |
545 | { |
546 | int i; | |
e4e11e38 | 547 | int num = sh->raid_conf->pool_size; |
1da177e4 | 548 | |
046169f0 | 549 | #if PAGE_SIZE == DEFAULT_STRIPE_SIZE |
e4e11e38 | 550 | for (i = 0; i < num; i++) { |
1da177e4 LT |
551 | struct page *page; |
552 | ||
a9683a79 | 553 | if (!(page = alloc_page(gfp))) { |
1da177e4 LT |
554 | return 1; |
555 | } | |
556 | sh->dev[i].page = page; | |
d592a996 | 557 | sh->dev[i].orig_page = page; |
7aba13b7 | 558 | sh->dev[i].offset = 0; |
1da177e4 | 559 | } |
046169f0 YY |
560 | #else |
561 | if (alloc_stripe_pages(sh, gfp)) | |
562 | return -ENOMEM; | |
3418d036 | 563 | |
046169f0 YY |
564 | for (i = 0; i < num; i++) { |
565 | sh->dev[i].page = raid5_get_dev_page(sh, i); | |
566 | sh->dev[i].orig_page = sh->dev[i].page; | |
567 | sh->dev[i].offset = raid5_get_page_offset(sh, i); | |
568 | } | |
569 | #endif | |
1da177e4 LT |
570 | return 0; |
571 | } | |
572 | ||
d1688a6d | 573 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
911d4ee8 | 574 | struct stripe_head *sh); |
1da177e4 | 575 | |
b5663ba4 | 576 | static void init_stripe(struct stripe_head *sh, sector_t sector, int previous) |
1da177e4 | 577 | { |
d1688a6d | 578 | struct r5conf *conf = sh->raid_conf; |
566c09c5 | 579 | int i, seq; |
1da177e4 | 580 | |
78bafebd ES |
581 | BUG_ON(atomic_read(&sh->count) != 0); |
582 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); | |
600aa109 | 583 | BUG_ON(stripe_operations_active(sh)); |
59fc630b | 584 | BUG_ON(sh->batch_head); |
d84e0f10 | 585 | |
45b4233c | 586 | pr_debug("init_stripe called, stripe %llu\n", |
b8e6a15a | 587 | (unsigned long long)sector); |
566c09c5 SL |
588 | retry: |
589 | seq = read_seqcount_begin(&conf->gen_lock); | |
86b42c71 | 590 | sh->generation = conf->generation - previous; |
b5663ba4 | 591 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
1da177e4 | 592 | sh->sector = sector; |
911d4ee8 | 593 | stripe_set_idx(sector, conf, previous, sh); |
1da177e4 LT |
594 | sh->state = 0; |
595 | ||
7ecaa1e6 | 596 | for (i = sh->disks; i--; ) { |
1da177e4 LT |
597 | struct r5dev *dev = &sh->dev[i]; |
598 | ||
d84e0f10 | 599 | if (dev->toread || dev->read || dev->towrite || dev->written || |
1da177e4 | 600 | test_bit(R5_LOCKED, &dev->flags)) { |
cc6167b4 | 601 | pr_err("sector=%llx i=%d %p %p %p %p %d\n", |
1da177e4 | 602 | (unsigned long long)sh->sector, i, dev->toread, |
d84e0f10 | 603 | dev->read, dev->towrite, dev->written, |
1da177e4 | 604 | test_bit(R5_LOCKED, &dev->flags)); |
8cfa7b0f | 605 | WARN_ON(1); |
1da177e4 LT |
606 | } |
607 | dev->flags = 0; | |
27a4ff8f | 608 | dev->sector = raid5_compute_blocknr(sh, i, previous); |
1da177e4 | 609 | } |
566c09c5 SL |
610 | if (read_seqcount_retry(&conf->gen_lock, seq)) |
611 | goto retry; | |
7a87f434 | 612 | sh->overwrite_disks = 0; |
1da177e4 | 613 | insert_hash(conf, sh); |
851c30c9 | 614 | sh->cpu = smp_processor_id(); |
da41ba65 | 615 | set_bit(STRIPE_BATCH_READY, &sh->state); |
1da177e4 LT |
616 | } |
617 | ||
d1688a6d | 618 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
86b42c71 | 619 | short generation) |
1da177e4 LT |
620 | { |
621 | struct stripe_head *sh; | |
622 | ||
45b4233c | 623 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
b67bfe0d | 624 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
86b42c71 | 625 | if (sh->sector == sector && sh->generation == generation) |
1da177e4 | 626 | return sh; |
45b4233c | 627 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
1da177e4 LT |
628 | return NULL; |
629 | } | |
630 | ||
1baa1126 LG |
631 | static struct stripe_head *find_get_stripe(struct r5conf *conf, |
632 | sector_t sector, short generation, int hash) | |
633 | { | |
634 | int inc_empty_inactive_list_flag; | |
635 | struct stripe_head *sh; | |
636 | ||
637 | sh = __find_stripe(conf, sector, generation); | |
638 | if (!sh) | |
639 | return NULL; | |
640 | ||
641 | if (atomic_inc_not_zero(&sh->count)) | |
642 | return sh; | |
643 | ||
644 | /* | |
645 | * Slow path. The reference count is zero which means the stripe must | |
646 | * be on a list (sh->lru). Must remove the stripe from the list that | |
647 | * references it with the device_lock held. | |
648 | */ | |
649 | ||
650 | spin_lock(&conf->device_lock); | |
651 | if (!atomic_read(&sh->count)) { | |
652 | if (!test_bit(STRIPE_HANDLE, &sh->state)) | |
653 | atomic_inc(&conf->active_stripes); | |
654 | BUG_ON(list_empty(&sh->lru) && | |
655 | !test_bit(STRIPE_EXPANDING, &sh->state)); | |
656 | inc_empty_inactive_list_flag = 0; | |
657 | if (!list_empty(conf->inactive_list + hash)) | |
658 | inc_empty_inactive_list_flag = 1; | |
659 | list_del_init(&sh->lru); | |
660 | if (list_empty(conf->inactive_list + hash) && | |
661 | inc_empty_inactive_list_flag) | |
662 | atomic_inc(&conf->empty_inactive_list_nr); | |
663 | if (sh->group) { | |
664 | sh->group->stripes_cnt--; | |
665 | sh->group = NULL; | |
666 | } | |
667 | } | |
668 | atomic_inc(&sh->count); | |
669 | spin_unlock(&conf->device_lock); | |
670 | ||
671 | return sh; | |
672 | } | |
673 | ||
674806d6 N |
674 | /* |
675 | * Need to check if array has failed when deciding whether to: | |
676 | * - start an array | |
677 | * - remove non-faulty devices | |
678 | * - add a spare | |
679 | * - allow a reshape | |
680 | * This determination is simple when no reshape is happening. | |
681 | * However if there is a reshape, we need to carefully check | |
682 | * both the before and after sections. | |
683 | * This is because some failed devices may only affect one | |
684 | * of the two sections, and some non-in_sync devices may | |
685 | * be insync in the section most affected by failed devices. | |
4631f39f LG |
686 | * |
687 | * Most calls to this function hold &conf->device_lock. Calls | |
688 | * in raid5_run() do not require the lock as no other threads | |
689 | * have been started yet. | |
674806d6 | 690 | */ |
2e38a37f | 691 | int raid5_calc_degraded(struct r5conf *conf) |
674806d6 | 692 | { |
908f4fbd | 693 | int degraded, degraded2; |
674806d6 | 694 | int i; |
674806d6 | 695 | |
674806d6 N |
696 | degraded = 0; |
697 | for (i = 0; i < conf->previous_raid_disks; i++) { | |
ad860670 YK |
698 | struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev); |
699 | ||
e5c86471 | 700 | if (rdev && test_bit(Faulty, &rdev->flags)) |
ad860670 | 701 | rdev = READ_ONCE(conf->disks[i].replacement); |
674806d6 N |
702 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
703 | degraded++; | |
704 | else if (test_bit(In_sync, &rdev->flags)) | |
705 | ; | |
706 | else | |
707 | /* not in-sync or faulty. | |
708 | * If the reshape increases the number of devices, | |
709 | * this is being recovered by the reshape, so | |
710 | * this 'previous' section is not in_sync. | |
711 | * If the number of devices is being reduced however, | |
712 | * the device can only be part of the array if | |
713 | * we are reverting a reshape, so this section will | |
714 | * be in-sync. | |
715 | */ | |
716 | if (conf->raid_disks >= conf->previous_raid_disks) | |
717 | degraded++; | |
718 | } | |
908f4fbd N |
719 | if (conf->raid_disks == conf->previous_raid_disks) |
720 | return degraded; | |
908f4fbd | 721 | degraded2 = 0; |
674806d6 | 722 | for (i = 0; i < conf->raid_disks; i++) { |
ad860670 YK |
723 | struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev); |
724 | ||
e5c86471 | 725 | if (rdev && test_bit(Faulty, &rdev->flags)) |
ad860670 | 726 | rdev = READ_ONCE(conf->disks[i].replacement); |
674806d6 | 727 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
908f4fbd | 728 | degraded2++; |
674806d6 N |
729 | else if (test_bit(In_sync, &rdev->flags)) |
730 | ; | |
731 | else | |
732 | /* not in-sync or faulty. | |
733 | * If reshape increases the number of devices, this | |
734 | * section has already been recovered, else it | |
735 | * almost certainly hasn't. | |
736 | */ | |
737 | if (conf->raid_disks <= conf->previous_raid_disks) | |
908f4fbd | 738 | degraded2++; |
674806d6 | 739 | } |
908f4fbd N |
740 | if (degraded2 > degraded) |
741 | return degraded2; | |
742 | return degraded; | |
743 | } | |
744 | ||
57668f0a | 745 | static bool has_failed(struct r5conf *conf) |
908f4fbd | 746 | { |
57668f0a | 747 | int degraded = conf->mddev->degraded; |
908f4fbd | 748 | |
57668f0a MT |
749 | if (test_bit(MD_BROKEN, &conf->mddev->flags)) |
750 | return true; | |
908f4fbd | 751 | |
57668f0a MT |
752 | if (conf->mddev->reshape_position != MaxSector) |
753 | degraded = raid5_calc_degraded(conf); | |
754 | ||
755 | return degraded > conf->max_degraded; | |
674806d6 N |
756 | } |
757 | ||
df6b0e20 LG |
758 | enum stripe_result { |
759 | STRIPE_SUCCESS = 0, | |
760 | STRIPE_RETRY, | |
761 | STRIPE_SCHEDULE_AND_RETRY, | |
762 | STRIPE_FAIL, | |
41425f96 | 763 | STRIPE_WAIT_RESHAPE, |
df6b0e20 LG |
764 | }; |
765 | ||
766 | struct stripe_request_ctx { | |
767 | /* a reference to the last stripe_head for batching */ | |
768 | struct stripe_head *batch_last; | |
769 | ||
770 | /* first sector in the request */ | |
771 | sector_t first_sector; | |
772 | ||
773 | /* last sector in the request */ | |
774 | sector_t last_sector; | |
775 | ||
776 | /* | |
777 | * bitmap to track stripe sectors that have been added to stripes | |
778 | * add one to account for unaligned requests | |
779 | */ | |
780 | DECLARE_BITMAP(sectors_to_do, RAID5_MAX_REQ_STRIPES + 1); | |
781 | ||
782 | /* the request had REQ_PREFLUSH, cleared after the first stripe_head */ | |
783 | bool do_flush; | |
784 | }; | |
785 | ||
3514da58 LG |
786 | /* |
787 | * Block until another thread clears R5_INACTIVE_BLOCKED or | |
788 | * there are fewer than 3/4 the maximum number of active stripes | |
789 | * and there is an inactive stripe available. | |
790 | */ | |
791 | static bool is_inactive_blocked(struct r5conf *conf, int hash) | |
792 | { | |
3514da58 LG |
793 | if (list_empty(conf->inactive_list + hash)) |
794 | return false; | |
795 | ||
796 | if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) | |
797 | return true; | |
798 | ||
f9287c3e LG |
799 | return (atomic_read(&conf->active_stripes) < |
800 | (conf->max_nr_stripes * 3 / 4)); | |
3514da58 LG |
801 | } |
802 | ||
2f2d51ef | 803 | struct stripe_head *raid5_get_active_stripe(struct r5conf *conf, |
20313b1b | 804 | struct stripe_request_ctx *ctx, sector_t sector, |
2f2d51ef | 805 | unsigned int flags) |
1da177e4 LT |
806 | { |
807 | struct stripe_head *sh; | |
c911c46c | 808 | int hash = stripe_hash_locks_hash(conf, sector); |
2f2d51ef | 809 | int previous = !!(flags & R5_GAS_PREVIOUS); |
1da177e4 | 810 | |
45b4233c | 811 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
1da177e4 | 812 | |
566c09c5 | 813 | spin_lock_irq(conf->hash_locks + hash); |
1da177e4 | 814 | |
b6d56144 | 815 | for (;;) { |
2f2d51ef | 816 | if (!(flags & R5_GAS_NOQUIESCE) && conf->quiesce) { |
b6d56144 LG |
817 | /* |
818 | * Must release the reference to batch_last before | |
819 | * waiting, on quiesce, otherwise the batch_last will | |
820 | * hold a reference to a stripe and raid5_quiesce() | |
821 | * will deadlock waiting for active_stripes to go to | |
822 | * zero. | |
823 | */ | |
824 | if (ctx && ctx->batch_last) { | |
825 | raid5_release_stripe(ctx->batch_last); | |
826 | ctx->batch_last = NULL; | |
827 | } | |
828 | ||
829 | wait_event_lock_irq(conf->wait_for_quiescent, | |
830 | !conf->quiesce, | |
831 | *(conf->hash_locks + hash)); | |
20313b1b LG |
832 | } |
833 | ||
b6d56144 LG |
834 | sh = find_get_stripe(conf, sector, conf->generation - previous, |
835 | hash); | |
836 | if (sh) | |
837 | break; | |
838 | ||
839 | if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) { | |
840 | sh = get_free_stripe(conf, hash); | |
841 | if (sh) { | |
842 | r5c_check_stripe_cache_usage(conf); | |
843 | init_stripe(sh, sector, previous); | |
844 | atomic_inc(&sh->count); | |
845 | break; | |
846 | } | |
20313b1b | 847 | |
b6d56144 LG |
848 | if (!test_bit(R5_DID_ALLOC, &conf->cache_state)) |
849 | set_bit(R5_ALLOC_MORE, &conf->cache_state); | |
850 | } | |
1baa1126 | 851 | |
2f2d51ef | 852 | if (flags & R5_GAS_NOBLOCK) |
b6d56144 | 853 | break; |
5165ed40 | 854 | |
b6d56144 LG |
855 | set_bit(R5_INACTIVE_BLOCKED, &conf->cache_state); |
856 | r5l_wake_reclaim(conf->log, 0); | |
2fd7b0f6 DJ |
857 | |
858 | /* release batch_last before wait to avoid risk of deadlock */ | |
859 | if (ctx && ctx->batch_last) { | |
860 | raid5_release_stripe(ctx->batch_last); | |
861 | ctx->batch_last = NULL; | |
862 | } | |
863 | ||
b6d56144 LG |
864 | wait_event_lock_irq(conf->wait_for_stripe, |
865 | is_inactive_blocked(conf, hash), | |
866 | *(conf->hash_locks + hash)); | |
867 | clear_bit(R5_INACTIVE_BLOCKED, &conf->cache_state); | |
5165ed40 LG |
868 | } |
869 | ||
566c09c5 | 870 | spin_unlock_irq(conf->hash_locks + hash); |
1da177e4 LT |
871 | return sh; |
872 | } | |
873 | ||
7a87f434 | 874 | static bool is_full_stripe_write(struct stripe_head *sh) |
875 | { | |
876 | BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded)); | |
877 | return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded); | |
878 | } | |
879 | ||
59fc630b | 880 | static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
368ecade CH |
881 | __acquires(&sh1->stripe_lock) |
882 | __acquires(&sh2->stripe_lock) | |
59fc630b | 883 | { |
59fc630b | 884 | if (sh1 > sh2) { |
3d05f3ae | 885 | spin_lock_irq(&sh2->stripe_lock); |
59fc630b | 886 | spin_lock_nested(&sh1->stripe_lock, 1); |
887 | } else { | |
3d05f3ae | 888 | spin_lock_irq(&sh1->stripe_lock); |
59fc630b | 889 | spin_lock_nested(&sh2->stripe_lock, 1); |
890 | } | |
891 | } | |
892 | ||
893 | static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) | |
368ecade CH |
894 | __releases(&sh1->stripe_lock) |
895 | __releases(&sh2->stripe_lock) | |
59fc630b | 896 | { |
897 | spin_unlock(&sh1->stripe_lock); | |
3d05f3ae | 898 | spin_unlock_irq(&sh2->stripe_lock); |
59fc630b | 899 | } |
900 | ||
901 | /* Only freshly new full stripe normal write stripe can be added to a batch list */ | |
902 | static bool stripe_can_batch(struct stripe_head *sh) | |
903 | { | |
9c3e333d SL |
904 | struct r5conf *conf = sh->raid_conf; |
905 | ||
e254de6b | 906 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
9c3e333d | 907 | return false; |
59fc630b | 908 | return test_bit(STRIPE_BATCH_READY, &sh->state) && |
cd5fc653 | 909 | is_full_stripe_write(sh); |
59fc630b | 910 | } |
911 | ||
912 | /* we only do back search */ | |
3312e6c8 LG |
913 | static void stripe_add_to_batch_list(struct r5conf *conf, |
914 | struct stripe_head *sh, struct stripe_head *last_sh) | |
59fc630b | 915 | { |
916 | struct stripe_head *head; | |
917 | sector_t head_sector, tmp_sec; | |
918 | int hash; | |
919 | int dd_idx; | |
920 | ||
59fc630b | 921 | /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */ |
922 | tmp_sec = sh->sector; | |
923 | if (!sector_div(tmp_sec, conf->chunk_sectors)) | |
924 | return; | |
c911c46c | 925 | head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf); |
59fc630b | 926 | |
3312e6c8 LG |
927 | if (last_sh && head_sector == last_sh->sector) { |
928 | head = last_sh; | |
929 | atomic_inc(&head->count); | |
930 | } else { | |
931 | hash = stripe_hash_locks_hash(conf, head_sector); | |
932 | spin_lock_irq(conf->hash_locks + hash); | |
933 | head = find_get_stripe(conf, head_sector, conf->generation, | |
934 | hash); | |
935 | spin_unlock_irq(conf->hash_locks + hash); | |
936 | if (!head) | |
937 | return; | |
938 | if (!stripe_can_batch(head)) | |
939 | goto out; | |
940 | } | |
59fc630b | 941 | |
942 | lock_two_stripes(head, sh); | |
943 | /* clear_batch_ready clear the flag */ | |
944 | if (!stripe_can_batch(head) || !stripe_can_batch(sh)) | |
945 | goto unlock_out; | |
946 | ||
947 | if (sh->batch_head) | |
948 | goto unlock_out; | |
949 | ||
950 | dd_idx = 0; | |
951 | while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) | |
952 | dd_idx++; | |
1eff9d32 | 953 | if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf || |
796a5cf0 | 954 | bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite)) |
59fc630b | 955 | goto unlock_out; |
956 | ||
957 | if (head->batch_head) { | |
958 | spin_lock(&head->batch_head->batch_lock); | |
959 | /* This batch list is already running */ | |
960 | if (!stripe_can_batch(head)) { | |
961 | spin_unlock(&head->batch_head->batch_lock); | |
962 | goto unlock_out; | |
963 | } | |
3664847d SL |
964 | /* |
965 | * We must assign batch_head of this stripe within the | |
966 | * batch_lock, otherwise clear_batch_ready of batch head | |
967 | * stripe could clear BATCH_READY bit of this stripe and | |
968 | * this stripe->batch_head doesn't get assigned, which | |
969 | * could confuse clear_batch_ready for this stripe | |
970 | */ | |
971 | sh->batch_head = head->batch_head; | |
59fc630b | 972 | |
973 | /* | |
974 | * at this point, head's BATCH_READY could be cleared, but we | |
975 | * can still add the stripe to batch list | |
976 | */ | |
977 | list_add(&sh->batch_list, &head->batch_list); | |
978 | spin_unlock(&head->batch_head->batch_lock); | |
59fc630b | 979 | } else { |
980 | head->batch_head = head; | |
981 | sh->batch_head = head->batch_head; | |
982 | spin_lock(&head->batch_lock); | |
983 | list_add_tail(&sh->batch_list, &head->batch_list); | |
984 | spin_unlock(&head->batch_lock); | |
985 | } | |
986 | ||
987 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
988 | if (atomic_dec_return(&conf->preread_active_stripes) | |
989 | < IO_THRESHOLD) | |
990 | md_wakeup_thread(conf->mddev->thread); | |
991 | ||
2b6b2457 N |
992 | if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) { |
993 | int seq = sh->bm_seq; | |
994 | if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) && | |
995 | sh->batch_head->bm_seq > seq) | |
996 | seq = sh->batch_head->bm_seq; | |
997 | set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state); | |
998 | sh->batch_head->bm_seq = seq; | |
999 | } | |
1000 | ||
59fc630b | 1001 | atomic_inc(&sh->count); |
1002 | unlock_out: | |
1003 | unlock_two_stripes(head, sh); | |
1004 | out: | |
6d036f7d | 1005 | raid5_release_stripe(head); |
59fc630b | 1006 | } |
1007 | ||
05616be5 N |
1008 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
1009 | * in this stripe_head. | |
1010 | */ | |
1011 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) | |
1012 | { | |
1013 | sector_t progress = conf->reshape_progress; | |
1014 | /* Need a memory barrier to make sure we see the value | |
1015 | * of conf->generation, or ->data_offset that was set before | |
1016 | * reshape_progress was updated. | |
1017 | */ | |
1018 | smp_rmb(); | |
1019 | if (progress == MaxSector) | |
1020 | return 0; | |
1021 | if (sh->generation == conf->generation - 1) | |
1022 | return 0; | |
1023 | /* We are in a reshape, and this is a new-generation stripe, | |
1024 | * so use new_data_offset. | |
1025 | */ | |
1026 | return 1; | |
1027 | } | |
1028 | ||
aaf9f12e | 1029 | static void dispatch_bio_list(struct bio_list *tmp) |
765d704d | 1030 | { |
765d704d SL |
1031 | struct bio *bio; |
1032 | ||
aaf9f12e | 1033 | while ((bio = bio_list_pop(tmp))) |
ed00aabd | 1034 | submit_bio_noacct(bio); |
aaf9f12e SL |
1035 | } |
1036 | ||
4f0f586b ST |
1037 | static int cmp_stripe(void *priv, const struct list_head *a, |
1038 | const struct list_head *b) | |
aaf9f12e SL |
1039 | { |
1040 | const struct r5pending_data *da = list_entry(a, | |
1041 | struct r5pending_data, sibling); | |
1042 | const struct r5pending_data *db = list_entry(b, | |
1043 | struct r5pending_data, sibling); | |
1044 | if (da->sector > db->sector) | |
1045 | return 1; | |
1046 | if (da->sector < db->sector) | |
1047 | return -1; | |
1048 | return 0; | |
1049 | } | |
1050 | ||
1051 | static void dispatch_defer_bios(struct r5conf *conf, int target, | |
1052 | struct bio_list *list) | |
1053 | { | |
1054 | struct r5pending_data *data; | |
1055 | struct list_head *first, *next = NULL; | |
1056 | int cnt = 0; | |
1057 | ||
1058 | if (conf->pending_data_cnt == 0) | |
1059 | return; | |
1060 | ||
1061 | list_sort(NULL, &conf->pending_list, cmp_stripe); | |
1062 | ||
1063 | first = conf->pending_list.next; | |
1064 | ||
1065 | /* temporarily move the head */ | |
1066 | if (conf->next_pending_data) | |
1067 | list_move_tail(&conf->pending_list, | |
1068 | &conf->next_pending_data->sibling); | |
1069 | ||
1070 | while (!list_empty(&conf->pending_list)) { | |
1071 | data = list_first_entry(&conf->pending_list, | |
1072 | struct r5pending_data, sibling); | |
1073 | if (&data->sibling == first) | |
1074 | first = data->sibling.next; | |
1075 | next = data->sibling.next; | |
1076 | ||
1077 | bio_list_merge(list, &data->bios); | |
1078 | list_move(&data->sibling, &conf->free_list); | |
1079 | cnt++; | |
1080 | if (cnt >= target) | |
1081 | break; | |
1082 | } | |
1083 | conf->pending_data_cnt -= cnt; | |
1084 | BUG_ON(conf->pending_data_cnt < 0 || cnt < target); | |
1085 | ||
1086 | if (next != &conf->pending_list) | |
1087 | conf->next_pending_data = list_entry(next, | |
1088 | struct r5pending_data, sibling); | |
1089 | else | |
1090 | conf->next_pending_data = NULL; | |
1091 | /* list isn't empty */ | |
1092 | if (first != &conf->pending_list) | |
1093 | list_move_tail(&conf->pending_list, first); | |
1094 | } | |
1095 | ||
1096 | static void flush_deferred_bios(struct r5conf *conf) | |
1097 | { | |
1098 | struct bio_list tmp = BIO_EMPTY_LIST; | |
1099 | ||
1100 | if (conf->pending_data_cnt == 0) | |
765d704d SL |
1101 | return; |
1102 | ||
765d704d | 1103 | spin_lock(&conf->pending_bios_lock); |
aaf9f12e SL |
1104 | dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp); |
1105 | BUG_ON(conf->pending_data_cnt != 0); | |
765d704d SL |
1106 | spin_unlock(&conf->pending_bios_lock); |
1107 | ||
aaf9f12e | 1108 | dispatch_bio_list(&tmp); |
765d704d SL |
1109 | } |
1110 | ||
aaf9f12e SL |
1111 | static void defer_issue_bios(struct r5conf *conf, sector_t sector, |
1112 | struct bio_list *bios) | |
765d704d | 1113 | { |
aaf9f12e SL |
1114 | struct bio_list tmp = BIO_EMPTY_LIST; |
1115 | struct r5pending_data *ent; | |
1116 | ||
765d704d | 1117 | spin_lock(&conf->pending_bios_lock); |
aaf9f12e SL |
1118 | ent = list_first_entry(&conf->free_list, struct r5pending_data, |
1119 | sibling); | |
1120 | list_move_tail(&ent->sibling, &conf->pending_list); | |
1121 | ent->sector = sector; | |
1122 | bio_list_init(&ent->bios); | |
1123 | bio_list_merge(&ent->bios, bios); | |
1124 | conf->pending_data_cnt++; | |
1125 | if (conf->pending_data_cnt >= PENDING_IO_MAX) | |
1126 | dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp); | |
1127 | ||
765d704d | 1128 | spin_unlock(&conf->pending_bios_lock); |
aaf9f12e SL |
1129 | |
1130 | dispatch_bio_list(&tmp); | |
765d704d SL |
1131 | } |
1132 | ||
6712ecf8 | 1133 | static void |
4246a0b6 | 1134 | raid5_end_read_request(struct bio *bi); |
6712ecf8 | 1135 | static void |
4246a0b6 | 1136 | raid5_end_write_request(struct bio *bi); |
91c00924 | 1137 | |
c4e5ac0a | 1138 | static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s) |
91c00924 | 1139 | { |
d1688a6d | 1140 | struct r5conf *conf = sh->raid_conf; |
91c00924 | 1141 | int i, disks = sh->disks; |
59fc630b | 1142 | struct stripe_head *head_sh = sh; |
aaf9f12e | 1143 | struct bio_list pending_bios = BIO_EMPTY_LIST; |
03a6b195 | 1144 | struct r5dev *dev; |
aaf9f12e | 1145 | bool should_defer; |
91c00924 DW |
1146 | |
1147 | might_sleep(); | |
1148 | ||
ff875738 AP |
1149 | if (log_stripe(sh, s) == 0) |
1150 | return; | |
1e6d690b | 1151 | |
aaf9f12e | 1152 | should_defer = conf->batch_bio_dispatch && conf->group_cnt; |
1e6d690b | 1153 | |
91c00924 | 1154 | for (i = disks; i--; ) { |
a9010741 BVA |
1155 | enum req_op op; |
1156 | blk_opf_t op_flags = 0; | |
9a3e1101 | 1157 | int replace_only = 0; |
977df362 N |
1158 | struct bio *bi, *rbi; |
1159 | struct md_rdev *rdev, *rrdev = NULL; | |
59fc630b | 1160 | |
1161 | sh = head_sh; | |
e9c7469b | 1162 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
796a5cf0 | 1163 | op = REQ_OP_WRITE; |
e9c7469b | 1164 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
70fd7614 | 1165 | op_flags = REQ_FUA; |
9e444768 | 1166 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
796a5cf0 | 1167 | op = REQ_OP_DISCARD; |
e9c7469b | 1168 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
796a5cf0 | 1169 | op = REQ_OP_READ; |
9a3e1101 N |
1170 | else if (test_and_clear_bit(R5_WantReplace, |
1171 | &sh->dev[i].flags)) { | |
796a5cf0 | 1172 | op = REQ_OP_WRITE; |
9a3e1101 N |
1173 | replace_only = 1; |
1174 | } else | |
91c00924 | 1175 | continue; |
bc0934f0 | 1176 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
796a5cf0 | 1177 | op_flags |= REQ_SYNC; |
91c00924 | 1178 | |
59fc630b | 1179 | again: |
03a6b195 CH |
1180 | dev = &sh->dev[i]; |
1181 | bi = &dev->req; | |
1182 | rbi = &dev->rreq; /* For writing to replacement */ | |
91c00924 | 1183 | |
ad860670 YK |
1184 | rdev = conf->disks[i].rdev; |
1185 | rrdev = conf->disks[i].replacement; | |
796a5cf0 | 1186 | if (op_is_write(op)) { |
9a3e1101 N |
1187 | if (replace_only) |
1188 | rdev = NULL; | |
dd054fce N |
1189 | if (rdev == rrdev) |
1190 | /* We raced and saw duplicates */ | |
1191 | rrdev = NULL; | |
9a3e1101 | 1192 | } else { |
59fc630b | 1193 | if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) |
9a3e1101 N |
1194 | rdev = rrdev; |
1195 | rrdev = NULL; | |
1196 | } | |
977df362 | 1197 | |
91c00924 DW |
1198 | if (rdev && test_bit(Faulty, &rdev->flags)) |
1199 | rdev = NULL; | |
1200 | if (rdev) | |
1201 | atomic_inc(&rdev->nr_pending); | |
977df362 N |
1202 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
1203 | rrdev = NULL; | |
1204 | if (rrdev) | |
1205 | atomic_inc(&rrdev->nr_pending); | |
91c00924 | 1206 | |
73e92e51 | 1207 | /* We have already checked bad blocks for reads. Now |
977df362 N |
1208 | * need to check for writes. We never accept write errors |
1209 | * on the replacement, so we don't to check rrdev. | |
73e92e51 | 1210 | */ |
796a5cf0 | 1211 | while (op_is_write(op) && rdev && |
73e92e51 | 1212 | test_bit(WriteErrorSeen, &rdev->flags)) { |
3a0f007b YK |
1213 | int bad = rdev_has_badblock(rdev, sh->sector, |
1214 | RAID5_STRIPE_SECTORS(conf)); | |
73e92e51 N |
1215 | if (!bad) |
1216 | break; | |
1217 | ||
1218 | if (bad < 0) { | |
1219 | set_bit(BlockedBadBlocks, &rdev->flags); | |
1220 | if (!conf->mddev->external && | |
2953079c | 1221 | conf->mddev->sb_flags) { |
73e92e51 N |
1222 | /* It is very unlikely, but we might |
1223 | * still need to write out the | |
1224 | * bad block log - better give it | |
1225 | * a chance*/ | |
1226 | md_check_recovery(conf->mddev); | |
1227 | } | |
1850753d | 1228 | /* |
1229 | * Because md_wait_for_blocked_rdev | |
1230 | * will dec nr_pending, we must | |
1231 | * increment it first. | |
1232 | */ | |
1233 | atomic_inc(&rdev->nr_pending); | |
73e92e51 N |
1234 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
1235 | } else { | |
1236 | /* Acknowledged bad block - skip the write */ | |
1237 | rdev_dec_pending(rdev, conf->mddev); | |
1238 | rdev = NULL; | |
1239 | } | |
1240 | } | |
1241 | ||
91c00924 | 1242 | if (rdev) { |
2b7497f0 DW |
1243 | set_bit(STRIPE_IO_STARTED, &sh->state); |
1244 | ||
03a6b195 | 1245 | bio_init(bi, rdev->bdev, &dev->vec, 1, op | op_flags); |
796a5cf0 | 1246 | bi->bi_end_io = op_is_write(op) |
2f6db2a7 KO |
1247 | ? raid5_end_write_request |
1248 | : raid5_end_read_request; | |
1249 | bi->bi_private = sh; | |
1250 | ||
6296b960 | 1251 | pr_debug("%s: for %llu schedule op %d on disc %d\n", |
e46b272b | 1252 | __func__, (unsigned long long)sh->sector, |
1eff9d32 | 1253 | bi->bi_opf, i); |
91c00924 | 1254 | atomic_inc(&sh->count); |
59fc630b | 1255 | if (sh != head_sh) |
1256 | atomic_inc(&head_sh->count); | |
05616be5 | 1257 | if (use_new_offset(conf, sh)) |
4f024f37 | 1258 | bi->bi_iter.bi_sector = (sh->sector |
05616be5 N |
1259 | + rdev->new_data_offset); |
1260 | else | |
4f024f37 | 1261 | bi->bi_iter.bi_sector = (sh->sector |
05616be5 | 1262 | + rdev->data_offset); |
59fc630b | 1263 | if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) |
1eff9d32 | 1264 | bi->bi_opf |= REQ_NOMERGE; |
3f9e7c14 | 1265 | |
d592a996 SL |
1266 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
1267 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
86aa1397 SL |
1268 | |
1269 | if (!op_is_write(op) && | |
1270 | test_bit(R5_InJournal, &sh->dev[i].flags)) | |
1271 | /* | |
1272 | * issuing read for a page in journal, this | |
1273 | * must be preparing for prexor in rmw; read | |
1274 | * the data into orig_page | |
1275 | */ | |
1276 | sh->dev[i].vec.bv_page = sh->dev[i].orig_page; | |
1277 | else | |
1278 | sh->dev[i].vec.bv_page = sh->dev[i].page; | |
4997b72e | 1279 | bi->bi_vcnt = 1; |
c911c46c | 1280 | bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); |
7aba13b7 | 1281 | bi->bi_io_vec[0].bv_offset = sh->dev[i].offset; |
c911c46c | 1282 | bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); |
37c61ff3 SL |
1283 | /* |
1284 | * If this is discard request, set bi_vcnt 0. We don't | |
1285 | * want to confuse SCSI because SCSI will replace payload | |
1286 | */ | |
796a5cf0 | 1287 | if (op == REQ_OP_DISCARD) |
37c61ff3 | 1288 | bi->bi_vcnt = 0; |
977df362 N |
1289 | if (rrdev) |
1290 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); | |
e3620a3a | 1291 | |
c396b90e | 1292 | mddev_trace_remap(conf->mddev, bi, sh->dev[i].sector); |
aaf9f12e SL |
1293 | if (should_defer && op_is_write(op)) |
1294 | bio_list_add(&pending_bios, bi); | |
1295 | else | |
ed00aabd | 1296 | submit_bio_noacct(bi); |
977df362 N |
1297 | } |
1298 | if (rrdev) { | |
977df362 N |
1299 | set_bit(STRIPE_IO_STARTED, &sh->state); |
1300 | ||
03a6b195 | 1301 | bio_init(rbi, rrdev->bdev, &dev->rvec, 1, op | op_flags); |
796a5cf0 | 1302 | BUG_ON(!op_is_write(op)); |
2f6db2a7 KO |
1303 | rbi->bi_end_io = raid5_end_write_request; |
1304 | rbi->bi_private = sh; | |
1305 | ||
6296b960 | 1306 | pr_debug("%s: for %llu schedule op %d on " |
977df362 N |
1307 | "replacement disc %d\n", |
1308 | __func__, (unsigned long long)sh->sector, | |
1eff9d32 | 1309 | rbi->bi_opf, i); |
977df362 | 1310 | atomic_inc(&sh->count); |
59fc630b | 1311 | if (sh != head_sh) |
1312 | atomic_inc(&head_sh->count); | |
05616be5 | 1313 | if (use_new_offset(conf, sh)) |
4f024f37 | 1314 | rbi->bi_iter.bi_sector = (sh->sector |
05616be5 N |
1315 | + rrdev->new_data_offset); |
1316 | else | |
4f024f37 | 1317 | rbi->bi_iter.bi_sector = (sh->sector |
05616be5 | 1318 | + rrdev->data_offset); |
d592a996 SL |
1319 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
1320 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
1321 | sh->dev[i].rvec.bv_page = sh->dev[i].page; | |
4997b72e | 1322 | rbi->bi_vcnt = 1; |
c911c46c | 1323 | rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); |
7aba13b7 | 1324 | rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset; |
c911c46c | 1325 | rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); |
37c61ff3 SL |
1326 | /* |
1327 | * If this is discard request, set bi_vcnt 0. We don't | |
1328 | * want to confuse SCSI because SCSI will replace payload | |
1329 | */ | |
796a5cf0 | 1330 | if (op == REQ_OP_DISCARD) |
37c61ff3 | 1331 | rbi->bi_vcnt = 0; |
c396b90e | 1332 | mddev_trace_remap(conf->mddev, rbi, sh->dev[i].sector); |
aaf9f12e SL |
1333 | if (should_defer && op_is_write(op)) |
1334 | bio_list_add(&pending_bios, rbi); | |
1335 | else | |
ed00aabd | 1336 | submit_bio_noacct(rbi); |
977df362 N |
1337 | } |
1338 | if (!rdev && !rrdev) { | |
6296b960 | 1339 | pr_debug("skip op %d on disc %d for sector %llu\n", |
1eff9d32 | 1340 | bi->bi_opf, i, (unsigned long long)sh->sector); |
91c00924 DW |
1341 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
1342 | set_bit(STRIPE_HANDLE, &sh->state); | |
1343 | } | |
59fc630b | 1344 | |
1345 | if (!head_sh->batch_head) | |
1346 | continue; | |
1347 | sh = list_first_entry(&sh->batch_list, struct stripe_head, | |
1348 | batch_list); | |
1349 | if (sh != head_sh) | |
1350 | goto again; | |
91c00924 | 1351 | } |
aaf9f12e SL |
1352 | |
1353 | if (should_defer && !bio_list_empty(&pending_bios)) | |
1354 | defer_issue_bios(conf, head_sh->sector, &pending_bios); | |
91c00924 DW |
1355 | } |
1356 | ||
1357 | static struct dma_async_tx_descriptor * | |
d592a996 | 1358 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
248728dd | 1359 | unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx, |
1e6d690b | 1360 | struct stripe_head *sh, int no_skipcopy) |
91c00924 | 1361 | { |
7988613b KO |
1362 | struct bio_vec bvl; |
1363 | struct bvec_iter iter; | |
91c00924 | 1364 | struct page *bio_page; |
91c00924 | 1365 | int page_offset; |
a08abd8c | 1366 | struct async_submit_ctl submit; |
0403e382 | 1367 | enum async_tx_flags flags = 0; |
c911c46c | 1368 | struct r5conf *conf = sh->raid_conf; |
91c00924 | 1369 | |
4f024f37 KO |
1370 | if (bio->bi_iter.bi_sector >= sector) |
1371 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; | |
91c00924 | 1372 | else |
4f024f37 | 1373 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
a08abd8c | 1374 | |
0403e382 DW |
1375 | if (frombio) |
1376 | flags |= ASYNC_TX_FENCE; | |
1377 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); | |
1378 | ||
7988613b KO |
1379 | bio_for_each_segment(bvl, bio, iter) { |
1380 | int len = bvl.bv_len; | |
91c00924 DW |
1381 | int clen; |
1382 | int b_offset = 0; | |
1383 | ||
1384 | if (page_offset < 0) { | |
1385 | b_offset = -page_offset; | |
1386 | page_offset += b_offset; | |
1387 | len -= b_offset; | |
1388 | } | |
1389 | ||
c911c46c YY |
1390 | if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf)) |
1391 | clen = RAID5_STRIPE_SIZE(conf) - page_offset; | |
91c00924 DW |
1392 | else |
1393 | clen = len; | |
1394 | ||
1395 | if (clen > 0) { | |
7988613b KO |
1396 | b_offset += bvl.bv_offset; |
1397 | bio_page = bvl.bv_page; | |
d592a996 | 1398 | if (frombio) { |
c911c46c | 1399 | if (conf->skip_copy && |
d592a996 | 1400 | b_offset == 0 && page_offset == 0 && |
c911c46c | 1401 | clen == RAID5_STRIPE_SIZE(conf) && |
1e6d690b | 1402 | !no_skipcopy) |
d592a996 SL |
1403 | *page = bio_page; |
1404 | else | |
248728dd | 1405 | tx = async_memcpy(*page, bio_page, page_offset + poff, |
a08abd8c | 1406 | b_offset, clen, &submit); |
d592a996 SL |
1407 | } else |
1408 | tx = async_memcpy(bio_page, *page, b_offset, | |
248728dd | 1409 | page_offset + poff, clen, &submit); |
91c00924 | 1410 | } |
a08abd8c DW |
1411 | /* chain the operations */ |
1412 | submit.depend_tx = tx; | |
1413 | ||
91c00924 DW |
1414 | if (clen < len) /* hit end of page */ |
1415 | break; | |
1416 | page_offset += len; | |
1417 | } | |
1418 | ||
1419 | return tx; | |
1420 | } | |
1421 | ||
1422 | static void ops_complete_biofill(void *stripe_head_ref) | |
1423 | { | |
1424 | struct stripe_head *sh = stripe_head_ref; | |
e4d84909 | 1425 | int i; |
c911c46c | 1426 | struct r5conf *conf = sh->raid_conf; |
91c00924 | 1427 | |
e46b272b | 1428 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1429 | (unsigned long long)sh->sector); |
1430 | ||
1431 | /* clear completed biofills */ | |
1432 | for (i = sh->disks; i--; ) { | |
1433 | struct r5dev *dev = &sh->dev[i]; | |
91c00924 DW |
1434 | |
1435 | /* acknowledge completion of a biofill operation */ | |
e4d84909 DW |
1436 | /* and check if we need to reply to a read request, |
1437 | * new R5_Wantfill requests are held off until | |
83de75cc | 1438 | * !STRIPE_BIOFILL_RUN |
e4d84909 DW |
1439 | */ |
1440 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { | |
91c00924 | 1441 | struct bio *rbi, *rbi2; |
91c00924 | 1442 | |
91c00924 DW |
1443 | BUG_ON(!dev->read); |
1444 | rbi = dev->read; | |
1445 | dev->read = NULL; | |
4f024f37 | 1446 | while (rbi && rbi->bi_iter.bi_sector < |
c911c46c YY |
1447 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
1448 | rbi2 = r5_next_bio(conf, rbi, dev->sector); | |
016c76ac | 1449 | bio_endio(rbi); |
91c00924 DW |
1450 | rbi = rbi2; |
1451 | } | |
1452 | } | |
1453 | } | |
83de75cc | 1454 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
91c00924 | 1455 | |
e4d84909 | 1456 | set_bit(STRIPE_HANDLE, &sh->state); |
6d036f7d | 1457 | raid5_release_stripe(sh); |
91c00924 DW |
1458 | } |
1459 | ||
1460 | static void ops_run_biofill(struct stripe_head *sh) | |
1461 | { | |
1462 | struct dma_async_tx_descriptor *tx = NULL; | |
a08abd8c | 1463 | struct async_submit_ctl submit; |
91c00924 | 1464 | int i; |
c911c46c | 1465 | struct r5conf *conf = sh->raid_conf; |
91c00924 | 1466 | |
59fc630b | 1467 | BUG_ON(sh->batch_head); |
e46b272b | 1468 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1469 | (unsigned long long)sh->sector); |
1470 | ||
1471 | for (i = sh->disks; i--; ) { | |
1472 | struct r5dev *dev = &sh->dev[i]; | |
1473 | if (test_bit(R5_Wantfill, &dev->flags)) { | |
1474 | struct bio *rbi; | |
b17459c0 | 1475 | spin_lock_irq(&sh->stripe_lock); |
91c00924 DW |
1476 | dev->read = rbi = dev->toread; |
1477 | dev->toread = NULL; | |
b17459c0 | 1478 | spin_unlock_irq(&sh->stripe_lock); |
4f024f37 | 1479 | while (rbi && rbi->bi_iter.bi_sector < |
c911c46c | 1480 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
d592a996 | 1481 | tx = async_copy_data(0, rbi, &dev->page, |
248728dd | 1482 | dev->offset, |
1e6d690b | 1483 | dev->sector, tx, sh, 0); |
c911c46c | 1484 | rbi = r5_next_bio(conf, rbi, dev->sector); |
91c00924 DW |
1485 | } |
1486 | } | |
1487 | } | |
1488 | ||
1489 | atomic_inc(&sh->count); | |
a08abd8c DW |
1490 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
1491 | async_trigger_callback(&submit); | |
91c00924 DW |
1492 | } |
1493 | ||
4e7d2c0a | 1494 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
91c00924 | 1495 | { |
4e7d2c0a | 1496 | struct r5dev *tgt; |
91c00924 | 1497 | |
4e7d2c0a DW |
1498 | if (target < 0) |
1499 | return; | |
91c00924 | 1500 | |
4e7d2c0a | 1501 | tgt = &sh->dev[target]; |
91c00924 DW |
1502 | set_bit(R5_UPTODATE, &tgt->flags); |
1503 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
1504 | clear_bit(R5_Wantcompute, &tgt->flags); | |
4e7d2c0a DW |
1505 | } |
1506 | ||
ac6b53b6 | 1507 | static void ops_complete_compute(void *stripe_head_ref) |
91c00924 DW |
1508 | { |
1509 | struct stripe_head *sh = stripe_head_ref; | |
91c00924 | 1510 | |
e46b272b | 1511 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1512 | (unsigned long long)sh->sector); |
1513 | ||
ac6b53b6 | 1514 | /* mark the computed target(s) as uptodate */ |
4e7d2c0a | 1515 | mark_target_uptodate(sh, sh->ops.target); |
ac6b53b6 | 1516 | mark_target_uptodate(sh, sh->ops.target2); |
4e7d2c0a | 1517 | |
ecc65c9b DW |
1518 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
1519 | if (sh->check_state == check_state_compute_run) | |
1520 | sh->check_state = check_state_compute_result; | |
91c00924 | 1521 | set_bit(STRIPE_HANDLE, &sh->state); |
6d036f7d | 1522 | raid5_release_stripe(sh); |
91c00924 DW |
1523 | } |
1524 | ||
d6f38f31 | 1525 | /* return a pointer to the address conversion region of the scribble buffer */ |
b330e6a4 | 1526 | static struct page **to_addr_page(struct raid5_percpu *percpu, int i) |
d6f38f31 | 1527 | { |
b330e6a4 | 1528 | return percpu->scribble + i * percpu->scribble_obj_size; |
46d5b785 | 1529 | } |
1530 | ||
1531 | /* return a pointer to the address conversion region of the scribble buffer */ | |
b330e6a4 KO |
1532 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
1533 | struct raid5_percpu *percpu, int i) | |
46d5b785 | 1534 | { |
b330e6a4 | 1535 | return (void *) (to_addr_page(percpu, i) + sh->disks + 2); |
d6f38f31 DW |
1536 | } |
1537 | ||
7aba13b7 YY |
1538 | /* |
1539 | * Return a pointer to record offset address. | |
1540 | */ | |
1541 | static unsigned int * | |
1542 | to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu) | |
1543 | { | |
1544 | return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2); | |
1545 | } | |
1546 | ||
d6f38f31 DW |
1547 | static struct dma_async_tx_descriptor * |
1548 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) | |
91c00924 | 1549 | { |
91c00924 | 1550 | int disks = sh->disks; |
46d5b785 | 1551 | struct page **xor_srcs = to_addr_page(percpu, 0); |
7aba13b7 | 1552 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
91c00924 DW |
1553 | int target = sh->ops.target; |
1554 | struct r5dev *tgt = &sh->dev[target]; | |
1555 | struct page *xor_dest = tgt->page; | |
7aba13b7 | 1556 | unsigned int off_dest = tgt->offset; |
91c00924 DW |
1557 | int count = 0; |
1558 | struct dma_async_tx_descriptor *tx; | |
a08abd8c | 1559 | struct async_submit_ctl submit; |
91c00924 DW |
1560 | int i; |
1561 | ||
59fc630b | 1562 | BUG_ON(sh->batch_head); |
1563 | ||
91c00924 | 1564 | pr_debug("%s: stripe %llu block: %d\n", |
e46b272b | 1565 | __func__, (unsigned long long)sh->sector, target); |
91c00924 DW |
1566 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
1567 | ||
7aba13b7 YY |
1568 | for (i = disks; i--; ) { |
1569 | if (i != target) { | |
1570 | off_srcs[count] = sh->dev[i].offset; | |
91c00924 | 1571 | xor_srcs[count++] = sh->dev[i].page; |
7aba13b7 YY |
1572 | } |
1573 | } | |
91c00924 DW |
1574 | |
1575 | atomic_inc(&sh->count); | |
1576 | ||
0403e382 | 1577 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, |
46d5b785 | 1578 | ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); |
91c00924 | 1579 | if (unlikely(count == 1)) |
7aba13b7 | 1580 | tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0], |
c911c46c | 1581 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
91c00924 | 1582 | else |
a7c224a8 | 1583 | tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
c911c46c | 1584 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
91c00924 | 1585 | |
91c00924 DW |
1586 | return tx; |
1587 | } | |
1588 | ||
ac6b53b6 DW |
1589 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
1590 | * @srcs - (struct page *) array of size sh->disks | |
d69454bc | 1591 | * @offs - (unsigned int) array of offset for each page |
ac6b53b6 DW |
1592 | * @sh - stripe_head to parse |
1593 | * | |
1594 | * Populates srcs in proper layout order for the stripe and returns the | |
1595 | * 'count' of sources to be used in a call to async_gen_syndrome. The P | |
1596 | * destination buffer is recorded in srcs[count] and the Q destination | |
1597 | * is recorded in srcs[count+1]]. | |
1598 | */ | |
584acdd4 | 1599 | static int set_syndrome_sources(struct page **srcs, |
d69454bc | 1600 | unsigned int *offs, |
584acdd4 MS |
1601 | struct stripe_head *sh, |
1602 | int srctype) | |
ac6b53b6 DW |
1603 | { |
1604 | int disks = sh->disks; | |
1605 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); | |
1606 | int d0_idx = raid6_d0(sh); | |
1607 | int count; | |
1608 | int i; | |
1609 | ||
1610 | for (i = 0; i < disks; i++) | |
5dd33c9a | 1611 | srcs[i] = NULL; |
ac6b53b6 DW |
1612 | |
1613 | count = 0; | |
1614 | i = d0_idx; | |
1615 | do { | |
1616 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); | |
584acdd4 | 1617 | struct r5dev *dev = &sh->dev[i]; |
ac6b53b6 | 1618 | |
584acdd4 MS |
1619 | if (i == sh->qd_idx || i == sh->pd_idx || |
1620 | (srctype == SYNDROME_SRC_ALL) || | |
1621 | (srctype == SYNDROME_SRC_WANT_DRAIN && | |
1e6d690b SL |
1622 | (test_bit(R5_Wantdrain, &dev->flags) || |
1623 | test_bit(R5_InJournal, &dev->flags))) || | |
584acdd4 | 1624 | (srctype == SYNDROME_SRC_WRITTEN && |
0977762f SL |
1625 | (dev->written || |
1626 | test_bit(R5_InJournal, &dev->flags)))) { | |
1e6d690b SL |
1627 | if (test_bit(R5_InJournal, &dev->flags)) |
1628 | srcs[slot] = sh->dev[i].orig_page; | |
1629 | else | |
1630 | srcs[slot] = sh->dev[i].page; | |
d69454bc YY |
1631 | /* |
1632 | * For R5_InJournal, PAGE_SIZE must be 4KB and will | |
1633 | * not shared page. In that case, dev[i].offset | |
1634 | * is 0. | |
1635 | */ | |
1636 | offs[slot] = sh->dev[i].offset; | |
1e6d690b | 1637 | } |
ac6b53b6 DW |
1638 | i = raid6_next_disk(i, disks); |
1639 | } while (i != d0_idx); | |
ac6b53b6 | 1640 | |
e4424fee | 1641 | return syndrome_disks; |
ac6b53b6 DW |
1642 | } |
1643 | ||
1644 | static struct dma_async_tx_descriptor * | |
1645 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) | |
1646 | { | |
1647 | int disks = sh->disks; | |
46d5b785 | 1648 | struct page **blocks = to_addr_page(percpu, 0); |
a7c224a8 | 1649 | unsigned int *offs = to_addr_offs(sh, percpu); |
ac6b53b6 DW |
1650 | int target; |
1651 | int qd_idx = sh->qd_idx; | |
1652 | struct dma_async_tx_descriptor *tx; | |
1653 | struct async_submit_ctl submit; | |
1654 | struct r5dev *tgt; | |
1655 | struct page *dest; | |
a7c224a8 | 1656 | unsigned int dest_off; |
ac6b53b6 DW |
1657 | int i; |
1658 | int count; | |
1659 | ||
59fc630b | 1660 | BUG_ON(sh->batch_head); |
ac6b53b6 DW |
1661 | if (sh->ops.target < 0) |
1662 | target = sh->ops.target2; | |
1663 | else if (sh->ops.target2 < 0) | |
1664 | target = sh->ops.target; | |
91c00924 | 1665 | else |
ac6b53b6 DW |
1666 | /* we should only have one valid target */ |
1667 | BUG(); | |
1668 | BUG_ON(target < 0); | |
1669 | pr_debug("%s: stripe %llu block: %d\n", | |
1670 | __func__, (unsigned long long)sh->sector, target); | |
1671 | ||
1672 | tgt = &sh->dev[target]; | |
1673 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
1674 | dest = tgt->page; | |
a7c224a8 | 1675 | dest_off = tgt->offset; |
ac6b53b6 DW |
1676 | |
1677 | atomic_inc(&sh->count); | |
1678 | ||
1679 | if (target == qd_idx) { | |
d69454bc | 1680 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); |
ac6b53b6 DW |
1681 | blocks[count] = NULL; /* regenerating p is not necessary */ |
1682 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ | |
0403e382 DW |
1683 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1684 | ops_complete_compute, sh, | |
46d5b785 | 1685 | to_addr_conv(sh, percpu, 0)); |
d69454bc | 1686 | tx = async_gen_syndrome(blocks, offs, count+2, |
c911c46c | 1687 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
ac6b53b6 DW |
1688 | } else { |
1689 | /* Compute any data- or p-drive using XOR */ | |
1690 | count = 0; | |
1691 | for (i = disks; i-- ; ) { | |
1692 | if (i == target || i == qd_idx) | |
1693 | continue; | |
a7c224a8 | 1694 | offs[count] = sh->dev[i].offset; |
ac6b53b6 DW |
1695 | blocks[count++] = sh->dev[i].page; |
1696 | } | |
1697 | ||
0403e382 DW |
1698 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
1699 | NULL, ops_complete_compute, sh, | |
46d5b785 | 1700 | to_addr_conv(sh, percpu, 0)); |
a7c224a8 | 1701 | tx = async_xor_offs(dest, dest_off, blocks, offs, count, |
c911c46c | 1702 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
ac6b53b6 | 1703 | } |
91c00924 | 1704 | |
91c00924 DW |
1705 | return tx; |
1706 | } | |
1707 | ||
ac6b53b6 DW |
1708 | static struct dma_async_tx_descriptor * |
1709 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) | |
1710 | { | |
1711 | int i, count, disks = sh->disks; | |
1712 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; | |
1713 | int d0_idx = raid6_d0(sh); | |
1714 | int faila = -1, failb = -1; | |
1715 | int target = sh->ops.target; | |
1716 | int target2 = sh->ops.target2; | |
1717 | struct r5dev *tgt = &sh->dev[target]; | |
1718 | struct r5dev *tgt2 = &sh->dev[target2]; | |
1719 | struct dma_async_tx_descriptor *tx; | |
46d5b785 | 1720 | struct page **blocks = to_addr_page(percpu, 0); |
a7c224a8 | 1721 | unsigned int *offs = to_addr_offs(sh, percpu); |
ac6b53b6 DW |
1722 | struct async_submit_ctl submit; |
1723 | ||
59fc630b | 1724 | BUG_ON(sh->batch_head); |
ac6b53b6 DW |
1725 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
1726 | __func__, (unsigned long long)sh->sector, target, target2); | |
1727 | BUG_ON(target < 0 || target2 < 0); | |
1728 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
1729 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); | |
1730 | ||
6c910a78 | 1731 | /* we need to open-code set_syndrome_sources to handle the |
ac6b53b6 DW |
1732 | * slot number conversion for 'faila' and 'failb' |
1733 | */ | |
a7c224a8 YY |
1734 | for (i = 0; i < disks ; i++) { |
1735 | offs[i] = 0; | |
5dd33c9a | 1736 | blocks[i] = NULL; |
a7c224a8 | 1737 | } |
ac6b53b6 DW |
1738 | count = 0; |
1739 | i = d0_idx; | |
1740 | do { | |
1741 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); | |
1742 | ||
a7c224a8 | 1743 | offs[slot] = sh->dev[i].offset; |
ac6b53b6 DW |
1744 | blocks[slot] = sh->dev[i].page; |
1745 | ||
1746 | if (i == target) | |
1747 | faila = slot; | |
1748 | if (i == target2) | |
1749 | failb = slot; | |
1750 | i = raid6_next_disk(i, disks); | |
1751 | } while (i != d0_idx); | |
ac6b53b6 DW |
1752 | |
1753 | BUG_ON(faila == failb); | |
1754 | if (failb < faila) | |
1755 | swap(faila, failb); | |
1756 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", | |
1757 | __func__, (unsigned long long)sh->sector, faila, failb); | |
1758 | ||
1759 | atomic_inc(&sh->count); | |
1760 | ||
1761 | if (failb == syndrome_disks+1) { | |
1762 | /* Q disk is one of the missing disks */ | |
1763 | if (faila == syndrome_disks) { | |
1764 | /* Missing P+Q, just recompute */ | |
0403e382 DW |
1765 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1766 | ops_complete_compute, sh, | |
46d5b785 | 1767 | to_addr_conv(sh, percpu, 0)); |
d69454bc | 1768 | return async_gen_syndrome(blocks, offs, syndrome_disks+2, |
c911c46c YY |
1769 | RAID5_STRIPE_SIZE(sh->raid_conf), |
1770 | &submit); | |
ac6b53b6 DW |
1771 | } else { |
1772 | struct page *dest; | |
a7c224a8 | 1773 | unsigned int dest_off; |
ac6b53b6 DW |
1774 | int data_target; |
1775 | int qd_idx = sh->qd_idx; | |
1776 | ||
1777 | /* Missing D+Q: recompute D from P, then recompute Q */ | |
1778 | if (target == qd_idx) | |
1779 | data_target = target2; | |
1780 | else | |
1781 | data_target = target; | |
1782 | ||
1783 | count = 0; | |
1784 | for (i = disks; i-- ; ) { | |
1785 | if (i == data_target || i == qd_idx) | |
1786 | continue; | |
a7c224a8 | 1787 | offs[count] = sh->dev[i].offset; |
ac6b53b6 DW |
1788 | blocks[count++] = sh->dev[i].page; |
1789 | } | |
1790 | dest = sh->dev[data_target].page; | |
a7c224a8 | 1791 | dest_off = sh->dev[data_target].offset; |
0403e382 DW |
1792 | init_async_submit(&submit, |
1793 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, | |
1794 | NULL, NULL, NULL, | |
46d5b785 | 1795 | to_addr_conv(sh, percpu, 0)); |
a7c224a8 | 1796 | tx = async_xor_offs(dest, dest_off, blocks, offs, count, |
c911c46c | 1797 | RAID5_STRIPE_SIZE(sh->raid_conf), |
ac6b53b6 DW |
1798 | &submit); |
1799 | ||
d69454bc | 1800 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); |
0403e382 DW |
1801 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
1802 | ops_complete_compute, sh, | |
46d5b785 | 1803 | to_addr_conv(sh, percpu, 0)); |
d69454bc | 1804 | return async_gen_syndrome(blocks, offs, count+2, |
c911c46c YY |
1805 | RAID5_STRIPE_SIZE(sh->raid_conf), |
1806 | &submit); | |
ac6b53b6 | 1807 | } |
ac6b53b6 | 1808 | } else { |
6c910a78 DW |
1809 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1810 | ops_complete_compute, sh, | |
46d5b785 | 1811 | to_addr_conv(sh, percpu, 0)); |
6c910a78 DW |
1812 | if (failb == syndrome_disks) { |
1813 | /* We're missing D+P. */ | |
1814 | return async_raid6_datap_recov(syndrome_disks+2, | |
c911c46c YY |
1815 | RAID5_STRIPE_SIZE(sh->raid_conf), |
1816 | faila, | |
4f86ff55 | 1817 | blocks, offs, &submit); |
6c910a78 DW |
1818 | } else { |
1819 | /* We're missing D+D. */ | |
1820 | return async_raid6_2data_recov(syndrome_disks+2, | |
c911c46c YY |
1821 | RAID5_STRIPE_SIZE(sh->raid_conf), |
1822 | faila, failb, | |
4f86ff55 | 1823 | blocks, offs, &submit); |
6c910a78 | 1824 | } |
ac6b53b6 DW |
1825 | } |
1826 | } | |
1827 | ||
91c00924 DW |
1828 | static void ops_complete_prexor(void *stripe_head_ref) |
1829 | { | |
1830 | struct stripe_head *sh = stripe_head_ref; | |
1831 | ||
e46b272b | 1832 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 | 1833 | (unsigned long long)sh->sector); |
1e6d690b SL |
1834 | |
1835 | if (r5c_is_writeback(sh->raid_conf->log)) | |
1836 | /* | |
1837 | * raid5-cache write back uses orig_page during prexor. | |
1838 | * After prexor, it is time to free orig_page | |
1839 | */ | |
1840 | r5c_release_extra_page(sh); | |
91c00924 DW |
1841 | } |
1842 | ||
1843 | static struct dma_async_tx_descriptor * | |
584acdd4 MS |
1844 | ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, |
1845 | struct dma_async_tx_descriptor *tx) | |
91c00924 | 1846 | { |
91c00924 | 1847 | int disks = sh->disks; |
46d5b785 | 1848 | struct page **xor_srcs = to_addr_page(percpu, 0); |
a7c224a8 | 1849 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
91c00924 | 1850 | int count = 0, pd_idx = sh->pd_idx, i; |
a08abd8c | 1851 | struct async_submit_ctl submit; |
91c00924 DW |
1852 | |
1853 | /* existing parity data subtracted */ | |
a7c224a8 | 1854 | unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset; |
91c00924 DW |
1855 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
1856 | ||
59fc630b | 1857 | BUG_ON(sh->batch_head); |
e46b272b | 1858 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1859 | (unsigned long long)sh->sector); |
1860 | ||
1861 | for (i = disks; i--; ) { | |
1862 | struct r5dev *dev = &sh->dev[i]; | |
1863 | /* Only process blocks that are known to be uptodate */ | |
a7c224a8 YY |
1864 | if (test_bit(R5_InJournal, &dev->flags)) { |
1865 | /* | |
1866 | * For this case, PAGE_SIZE must be equal to 4KB and | |
1867 | * page offset is zero. | |
1868 | */ | |
1869 | off_srcs[count] = dev->offset; | |
1e6d690b | 1870 | xor_srcs[count++] = dev->orig_page; |
a7c224a8 YY |
1871 | } else if (test_bit(R5_Wantdrain, &dev->flags)) { |
1872 | off_srcs[count] = dev->offset; | |
91c00924 | 1873 | xor_srcs[count++] = dev->page; |
a7c224a8 | 1874 | } |
91c00924 DW |
1875 | } |
1876 | ||
0403e382 | 1877 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, |
46d5b785 | 1878 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
a7c224a8 | 1879 | tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
c911c46c | 1880 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
91c00924 DW |
1881 | |
1882 | return tx; | |
1883 | } | |
1884 | ||
584acdd4 MS |
1885 | static struct dma_async_tx_descriptor * |
1886 | ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, | |
1887 | struct dma_async_tx_descriptor *tx) | |
1888 | { | |
1889 | struct page **blocks = to_addr_page(percpu, 0); | |
d69454bc | 1890 | unsigned int *offs = to_addr_offs(sh, percpu); |
584acdd4 MS |
1891 | int count; |
1892 | struct async_submit_ctl submit; | |
1893 | ||
1894 | pr_debug("%s: stripe %llu\n", __func__, | |
1895 | (unsigned long long)sh->sector); | |
1896 | ||
d69454bc | 1897 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN); |
584acdd4 MS |
1898 | |
1899 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, | |
1900 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); | |
d69454bc | 1901 | tx = async_gen_syndrome(blocks, offs, count+2, |
c911c46c | 1902 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
584acdd4 MS |
1903 | |
1904 | return tx; | |
1905 | } | |
1906 | ||
91c00924 | 1907 | static struct dma_async_tx_descriptor * |
d8ee0728 | 1908 | ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) |
91c00924 | 1909 | { |
1e6d690b | 1910 | struct r5conf *conf = sh->raid_conf; |
91c00924 | 1911 | int disks = sh->disks; |
d8ee0728 | 1912 | int i; |
59fc630b | 1913 | struct stripe_head *head_sh = sh; |
91c00924 | 1914 | |
e46b272b | 1915 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1916 | (unsigned long long)sh->sector); |
1917 | ||
1918 | for (i = disks; i--; ) { | |
59fc630b | 1919 | struct r5dev *dev; |
91c00924 | 1920 | struct bio *chosen; |
91c00924 | 1921 | |
59fc630b | 1922 | sh = head_sh; |
1923 | if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { | |
91c00924 DW |
1924 | struct bio *wbi; |
1925 | ||
59fc630b | 1926 | again: |
1927 | dev = &sh->dev[i]; | |
1e6d690b SL |
1928 | /* |
1929 | * clear R5_InJournal, so when rewriting a page in | |
1930 | * journal, it is not skipped by r5l_log_stripe() | |
1931 | */ | |
1932 | clear_bit(R5_InJournal, &dev->flags); | |
b17459c0 | 1933 | spin_lock_irq(&sh->stripe_lock); |
91c00924 DW |
1934 | chosen = dev->towrite; |
1935 | dev->towrite = NULL; | |
7a87f434 | 1936 | sh->overwrite_disks = 0; |
91c00924 DW |
1937 | BUG_ON(dev->written); |
1938 | wbi = dev->written = chosen; | |
b17459c0 | 1939 | spin_unlock_irq(&sh->stripe_lock); |
d592a996 | 1940 | WARN_ON(dev->page != dev->orig_page); |
91c00924 | 1941 | |
4f024f37 | 1942 | while (wbi && wbi->bi_iter.bi_sector < |
c911c46c | 1943 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
1eff9d32 | 1944 | if (wbi->bi_opf & REQ_FUA) |
e9c7469b | 1945 | set_bit(R5_WantFUA, &dev->flags); |
1eff9d32 | 1946 | if (wbi->bi_opf & REQ_SYNC) |
bc0934f0 | 1947 | set_bit(R5_SyncIO, &dev->flags); |
796a5cf0 | 1948 | if (bio_op(wbi) == REQ_OP_DISCARD) |
620125f2 | 1949 | set_bit(R5_Discard, &dev->flags); |
d592a996 SL |
1950 | else { |
1951 | tx = async_copy_data(1, wbi, &dev->page, | |
248728dd | 1952 | dev->offset, |
1e6d690b SL |
1953 | dev->sector, tx, sh, |
1954 | r5c_is_writeback(conf->log)); | |
1955 | if (dev->page != dev->orig_page && | |
1956 | !r5c_is_writeback(conf->log)) { | |
d592a996 SL |
1957 | set_bit(R5_SkipCopy, &dev->flags); |
1958 | clear_bit(R5_UPTODATE, &dev->flags); | |
1959 | clear_bit(R5_OVERWRITE, &dev->flags); | |
1960 | } | |
1961 | } | |
c911c46c | 1962 | wbi = r5_next_bio(conf, wbi, dev->sector); |
91c00924 | 1963 | } |
59fc630b | 1964 | |
1965 | if (head_sh->batch_head) { | |
1966 | sh = list_first_entry(&sh->batch_list, | |
1967 | struct stripe_head, | |
1968 | batch_list); | |
1969 | if (sh == head_sh) | |
1970 | continue; | |
1971 | goto again; | |
1972 | } | |
91c00924 DW |
1973 | } |
1974 | } | |
1975 | ||
1976 | return tx; | |
1977 | } | |
1978 | ||
ac6b53b6 | 1979 | static void ops_complete_reconstruct(void *stripe_head_ref) |
91c00924 DW |
1980 | { |
1981 | struct stripe_head *sh = stripe_head_ref; | |
ac6b53b6 DW |
1982 | int disks = sh->disks; |
1983 | int pd_idx = sh->pd_idx; | |
1984 | int qd_idx = sh->qd_idx; | |
1985 | int i; | |
9e444768 | 1986 | bool fua = false, sync = false, discard = false; |
91c00924 | 1987 | |
e46b272b | 1988 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1989 | (unsigned long long)sh->sector); |
1990 | ||
bc0934f0 | 1991 | for (i = disks; i--; ) { |
e9c7469b | 1992 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
bc0934f0 | 1993 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
9e444768 | 1994 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
bc0934f0 | 1995 | } |
e9c7469b | 1996 | |
91c00924 DW |
1997 | for (i = disks; i--; ) { |
1998 | struct r5dev *dev = &sh->dev[i]; | |
ac6b53b6 | 1999 | |
e9c7469b | 2000 | if (dev->written || i == pd_idx || i == qd_idx) { |
235b6003 | 2001 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) { |
9e444768 | 2002 | set_bit(R5_UPTODATE, &dev->flags); |
235b6003 N |
2003 | if (test_bit(STRIPE_EXPAND_READY, &sh->state)) |
2004 | set_bit(R5_Expanded, &dev->flags); | |
2005 | } | |
e9c7469b TH |
2006 | if (fua) |
2007 | set_bit(R5_WantFUA, &dev->flags); | |
bc0934f0 SL |
2008 | if (sync) |
2009 | set_bit(R5_SyncIO, &dev->flags); | |
e9c7469b | 2010 | } |
91c00924 DW |
2011 | } |
2012 | ||
d8ee0728 DW |
2013 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
2014 | sh->reconstruct_state = reconstruct_state_drain_result; | |
2015 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) | |
2016 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; | |
2017 | else { | |
2018 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); | |
2019 | sh->reconstruct_state = reconstruct_state_result; | |
2020 | } | |
91c00924 DW |
2021 | |
2022 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 2023 | raid5_release_stripe(sh); |
91c00924 DW |
2024 | } |
2025 | ||
2026 | static void | |
ac6b53b6 DW |
2027 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
2028 | struct dma_async_tx_descriptor *tx) | |
91c00924 | 2029 | { |
91c00924 | 2030 | int disks = sh->disks; |
59fc630b | 2031 | struct page **xor_srcs; |
7aba13b7 | 2032 | unsigned int *off_srcs; |
a08abd8c | 2033 | struct async_submit_ctl submit; |
59fc630b | 2034 | int count, pd_idx = sh->pd_idx, i; |
91c00924 | 2035 | struct page *xor_dest; |
7aba13b7 | 2036 | unsigned int off_dest; |
d8ee0728 | 2037 | int prexor = 0; |
91c00924 | 2038 | unsigned long flags; |
59fc630b | 2039 | int j = 0; |
2040 | struct stripe_head *head_sh = sh; | |
2041 | int last_stripe; | |
91c00924 | 2042 | |
e46b272b | 2043 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
2044 | (unsigned long long)sh->sector); |
2045 | ||
620125f2 SL |
2046 | for (i = 0; i < sh->disks; i++) { |
2047 | if (pd_idx == i) | |
2048 | continue; | |
2049 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) | |
2050 | break; | |
2051 | } | |
2052 | if (i >= sh->disks) { | |
2053 | atomic_inc(&sh->count); | |
620125f2 SL |
2054 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
2055 | ops_complete_reconstruct(sh); | |
2056 | return; | |
2057 | } | |
59fc630b | 2058 | again: |
2059 | count = 0; | |
2060 | xor_srcs = to_addr_page(percpu, j); | |
7aba13b7 | 2061 | off_srcs = to_addr_offs(sh, percpu); |
91c00924 DW |
2062 | /* check if prexor is active which means only process blocks |
2063 | * that are part of a read-modify-write (written) | |
2064 | */ | |
59fc630b | 2065 | if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
d8ee0728 | 2066 | prexor = 1; |
7aba13b7 | 2067 | off_dest = off_srcs[count] = sh->dev[pd_idx].offset; |
91c00924 DW |
2068 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
2069 | for (i = disks; i--; ) { | |
2070 | struct r5dev *dev = &sh->dev[i]; | |
1e6d690b | 2071 | if (head_sh->dev[i].written || |
7aba13b7 YY |
2072 | test_bit(R5_InJournal, &head_sh->dev[i].flags)) { |
2073 | off_srcs[count] = dev->offset; | |
91c00924 | 2074 | xor_srcs[count++] = dev->page; |
7aba13b7 | 2075 | } |
91c00924 DW |
2076 | } |
2077 | } else { | |
2078 | xor_dest = sh->dev[pd_idx].page; | |
7aba13b7 | 2079 | off_dest = sh->dev[pd_idx].offset; |
91c00924 DW |
2080 | for (i = disks; i--; ) { |
2081 | struct r5dev *dev = &sh->dev[i]; | |
7aba13b7 YY |
2082 | if (i != pd_idx) { |
2083 | off_srcs[count] = dev->offset; | |
91c00924 | 2084 | xor_srcs[count++] = dev->page; |
7aba13b7 | 2085 | } |
91c00924 DW |
2086 | } |
2087 | } | |
2088 | ||
91c00924 DW |
2089 | /* 1/ if we prexor'd then the dest is reused as a source |
2090 | * 2/ if we did not prexor then we are redoing the parity | |
2091 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST | |
2092 | * for the synchronous xor case | |
2093 | */ | |
59fc630b | 2094 | last_stripe = !head_sh->batch_head || |
2095 | list_first_entry(&sh->batch_list, | |
2096 | struct stripe_head, batch_list) == head_sh; | |
2097 | if (last_stripe) { | |
2098 | flags = ASYNC_TX_ACK | | |
2099 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); | |
2100 | ||
2101 | atomic_inc(&head_sh->count); | |
2102 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, | |
2103 | to_addr_conv(sh, percpu, j)); | |
2104 | } else { | |
2105 | flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; | |
2106 | init_async_submit(&submit, flags, tx, NULL, NULL, | |
2107 | to_addr_conv(sh, percpu, j)); | |
2108 | } | |
91c00924 | 2109 | |
a08abd8c | 2110 | if (unlikely(count == 1)) |
7aba13b7 | 2111 | tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0], |
c911c46c | 2112 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
a08abd8c | 2113 | else |
a7c224a8 | 2114 | tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
c911c46c | 2115 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
59fc630b | 2116 | if (!last_stripe) { |
2117 | j++; | |
2118 | sh = list_first_entry(&sh->batch_list, struct stripe_head, | |
2119 | batch_list); | |
2120 | goto again; | |
2121 | } | |
91c00924 DW |
2122 | } |
2123 | ||
ac6b53b6 DW |
2124 | static void |
2125 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, | |
2126 | struct dma_async_tx_descriptor *tx) | |
2127 | { | |
2128 | struct async_submit_ctl submit; | |
59fc630b | 2129 | struct page **blocks; |
d69454bc | 2130 | unsigned int *offs; |
59fc630b | 2131 | int count, i, j = 0; |
2132 | struct stripe_head *head_sh = sh; | |
2133 | int last_stripe; | |
584acdd4 MS |
2134 | int synflags; |
2135 | unsigned long txflags; | |
ac6b53b6 DW |
2136 | |
2137 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); | |
2138 | ||
620125f2 SL |
2139 | for (i = 0; i < sh->disks; i++) { |
2140 | if (sh->pd_idx == i || sh->qd_idx == i) | |
2141 | continue; | |
2142 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) | |
2143 | break; | |
2144 | } | |
2145 | if (i >= sh->disks) { | |
2146 | atomic_inc(&sh->count); | |
620125f2 SL |
2147 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
2148 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); | |
2149 | ops_complete_reconstruct(sh); | |
2150 | return; | |
2151 | } | |
2152 | ||
59fc630b | 2153 | again: |
2154 | blocks = to_addr_page(percpu, j); | |
d69454bc | 2155 | offs = to_addr_offs(sh, percpu); |
584acdd4 MS |
2156 | |
2157 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { | |
2158 | synflags = SYNDROME_SRC_WRITTEN; | |
2159 | txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; | |
2160 | } else { | |
2161 | synflags = SYNDROME_SRC_ALL; | |
2162 | txflags = ASYNC_TX_ACK; | |
2163 | } | |
2164 | ||
d69454bc | 2165 | count = set_syndrome_sources(blocks, offs, sh, synflags); |
59fc630b | 2166 | last_stripe = !head_sh->batch_head || |
2167 | list_first_entry(&sh->batch_list, | |
2168 | struct stripe_head, batch_list) == head_sh; | |
2169 | ||
2170 | if (last_stripe) { | |
2171 | atomic_inc(&head_sh->count); | |
584acdd4 | 2172 | init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, |
59fc630b | 2173 | head_sh, to_addr_conv(sh, percpu, j)); |
2174 | } else | |
2175 | init_async_submit(&submit, 0, tx, NULL, NULL, | |
2176 | to_addr_conv(sh, percpu, j)); | |
d69454bc | 2177 | tx = async_gen_syndrome(blocks, offs, count+2, |
c911c46c | 2178 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
59fc630b | 2179 | if (!last_stripe) { |
2180 | j++; | |
2181 | sh = list_first_entry(&sh->batch_list, struct stripe_head, | |
2182 | batch_list); | |
2183 | goto again; | |
2184 | } | |
91c00924 DW |
2185 | } |
2186 | ||
2187 | static void ops_complete_check(void *stripe_head_ref) | |
2188 | { | |
2189 | struct stripe_head *sh = stripe_head_ref; | |
91c00924 | 2190 | |
e46b272b | 2191 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
2192 | (unsigned long long)sh->sector); |
2193 | ||
ecc65c9b | 2194 | sh->check_state = check_state_check_result; |
91c00924 | 2195 | set_bit(STRIPE_HANDLE, &sh->state); |
6d036f7d | 2196 | raid5_release_stripe(sh); |
91c00924 DW |
2197 | } |
2198 | ||
ac6b53b6 | 2199 | static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu) |
91c00924 | 2200 | { |
91c00924 | 2201 | int disks = sh->disks; |
ac6b53b6 DW |
2202 | int pd_idx = sh->pd_idx; |
2203 | int qd_idx = sh->qd_idx; | |
2204 | struct page *xor_dest; | |
a7c224a8 | 2205 | unsigned int off_dest; |
46d5b785 | 2206 | struct page **xor_srcs = to_addr_page(percpu, 0); |
a7c224a8 | 2207 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
91c00924 | 2208 | struct dma_async_tx_descriptor *tx; |
a08abd8c | 2209 | struct async_submit_ctl submit; |
ac6b53b6 DW |
2210 | int count; |
2211 | int i; | |
91c00924 | 2212 | |
e46b272b | 2213 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
2214 | (unsigned long long)sh->sector); |
2215 | ||
59fc630b | 2216 | BUG_ON(sh->batch_head); |
ac6b53b6 DW |
2217 | count = 0; |
2218 | xor_dest = sh->dev[pd_idx].page; | |
a7c224a8 YY |
2219 | off_dest = sh->dev[pd_idx].offset; |
2220 | off_srcs[count] = off_dest; | |
ac6b53b6 | 2221 | xor_srcs[count++] = xor_dest; |
91c00924 | 2222 | for (i = disks; i--; ) { |
ac6b53b6 DW |
2223 | if (i == pd_idx || i == qd_idx) |
2224 | continue; | |
a7c224a8 | 2225 | off_srcs[count] = sh->dev[i].offset; |
ac6b53b6 | 2226 | xor_srcs[count++] = sh->dev[i].page; |
91c00924 DW |
2227 | } |
2228 | ||
d6f38f31 | 2229 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
46d5b785 | 2230 | to_addr_conv(sh, percpu, 0)); |
a7c224a8 | 2231 | tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
c911c46c | 2232 | RAID5_STRIPE_SIZE(sh->raid_conf), |
a08abd8c | 2233 | &sh->ops.zero_sum_result, &submit); |
91c00924 | 2234 | |
91c00924 | 2235 | atomic_inc(&sh->count); |
a08abd8c DW |
2236 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
2237 | tx = async_trigger_callback(&submit); | |
91c00924 DW |
2238 | } |
2239 | ||
ac6b53b6 DW |
2240 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
2241 | { | |
46d5b785 | 2242 | struct page **srcs = to_addr_page(percpu, 0); |
d69454bc | 2243 | unsigned int *offs = to_addr_offs(sh, percpu); |
ac6b53b6 DW |
2244 | struct async_submit_ctl submit; |
2245 | int count; | |
2246 | ||
2247 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, | |
2248 | (unsigned long long)sh->sector, checkp); | |
2249 | ||
59fc630b | 2250 | BUG_ON(sh->batch_head); |
d69454bc | 2251 | count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL); |
ac6b53b6 DW |
2252 | if (!checkp) |
2253 | srcs[count] = NULL; | |
91c00924 | 2254 | |
91c00924 | 2255 | atomic_inc(&sh->count); |
ac6b53b6 | 2256 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
46d5b785 | 2257 | sh, to_addr_conv(sh, percpu, 0)); |
d69454bc | 2258 | async_syndrome_val(srcs, offs, count+2, |
c911c46c | 2259 | RAID5_STRIPE_SIZE(sh->raid_conf), |
d69454bc | 2260 | &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit); |
91c00924 DW |
2261 | } |
2262 | ||
51acbcec | 2263 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
91c00924 DW |
2264 | { |
2265 | int overlap_clear = 0, i, disks = sh->disks; | |
2266 | struct dma_async_tx_descriptor *tx = NULL; | |
d1688a6d | 2267 | struct r5conf *conf = sh->raid_conf; |
ac6b53b6 | 2268 | int level = conf->level; |
d6f38f31 | 2269 | struct raid5_percpu *percpu; |
91c00924 | 2270 | |
770b1d21 DB |
2271 | local_lock(&conf->percpu->lock); |
2272 | percpu = this_cpu_ptr(conf->percpu); | |
83de75cc | 2273 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
91c00924 DW |
2274 | ops_run_biofill(sh); |
2275 | overlap_clear++; | |
2276 | } | |
2277 | ||
7b3a871e | 2278 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
ac6b53b6 DW |
2279 | if (level < 6) |
2280 | tx = ops_run_compute5(sh, percpu); | |
2281 | else { | |
2282 | if (sh->ops.target2 < 0 || sh->ops.target < 0) | |
2283 | tx = ops_run_compute6_1(sh, percpu); | |
2284 | else | |
2285 | tx = ops_run_compute6_2(sh, percpu); | |
2286 | } | |
2287 | /* terminate the chain if reconstruct is not set to be run */ | |
2288 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) | |
7b3a871e DW |
2289 | async_tx_ack(tx); |
2290 | } | |
91c00924 | 2291 | |
584acdd4 MS |
2292 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) { |
2293 | if (level < 6) | |
2294 | tx = ops_run_prexor5(sh, percpu, tx); | |
2295 | else | |
2296 | tx = ops_run_prexor6(sh, percpu, tx); | |
2297 | } | |
91c00924 | 2298 | |
ae1713e2 AP |
2299 | if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request)) |
2300 | tx = ops_run_partial_parity(sh, percpu, tx); | |
2301 | ||
600aa109 | 2302 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
d8ee0728 | 2303 | tx = ops_run_biodrain(sh, tx); |
91c00924 DW |
2304 | overlap_clear++; |
2305 | } | |
2306 | ||
ac6b53b6 DW |
2307 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
2308 | if (level < 6) | |
2309 | ops_run_reconstruct5(sh, percpu, tx); | |
2310 | else | |
2311 | ops_run_reconstruct6(sh, percpu, tx); | |
2312 | } | |
91c00924 | 2313 | |
ac6b53b6 DW |
2314 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
2315 | if (sh->check_state == check_state_run) | |
2316 | ops_run_check_p(sh, percpu); | |
2317 | else if (sh->check_state == check_state_run_q) | |
2318 | ops_run_check_pq(sh, percpu, 0); | |
2319 | else if (sh->check_state == check_state_run_pq) | |
2320 | ops_run_check_pq(sh, percpu, 1); | |
2321 | else | |
2322 | BUG(); | |
2323 | } | |
91c00924 | 2324 | |
770b1d21 | 2325 | if (overlap_clear && !sh->batch_head) { |
91c00924 DW |
2326 | for (i = disks; i--; ) { |
2327 | struct r5dev *dev = &sh->dev[i]; | |
2328 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) | |
e6a03207 | 2329 | wake_up_bit(&dev->flags, R5_Overlap); |
91c00924 | 2330 | } |
770b1d21 DB |
2331 | } |
2332 | local_unlock(&conf->percpu->lock); | |
91c00924 DW |
2333 | } |
2334 | ||
845b9e22 AP |
2335 | static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh) |
2336 | { | |
046169f0 YY |
2337 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
2338 | kfree(sh->pages); | |
2339 | #endif | |
845b9e22 AP |
2340 | if (sh->ppl_page) |
2341 | __free_page(sh->ppl_page); | |
2342 | kmem_cache_free(sc, sh); | |
2343 | } | |
2344 | ||
5f9d1fde | 2345 | static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp, |
845b9e22 | 2346 | int disks, struct r5conf *conf) |
f18c1a35 N |
2347 | { |
2348 | struct stripe_head *sh; | |
2349 | ||
2350 | sh = kmem_cache_zalloc(sc, gfp); | |
2351 | if (sh) { | |
2352 | spin_lock_init(&sh->stripe_lock); | |
2353 | spin_lock_init(&sh->batch_lock); | |
2354 | INIT_LIST_HEAD(&sh->batch_list); | |
2355 | INIT_LIST_HEAD(&sh->lru); | |
a39f7afd | 2356 | INIT_LIST_HEAD(&sh->r5c); |
d7bd398e | 2357 | INIT_LIST_HEAD(&sh->log_list); |
f18c1a35 | 2358 | atomic_set(&sh->count, 1); |
845b9e22 | 2359 | sh->raid_conf = conf; |
a39f7afd | 2360 | sh->log_start = MaxSector; |
845b9e22 AP |
2361 | |
2362 | if (raid5_has_ppl(conf)) { | |
2363 | sh->ppl_page = alloc_page(gfp); | |
2364 | if (!sh->ppl_page) { | |
2365 | free_stripe(sc, sh); | |
046169f0 | 2366 | return NULL; |
845b9e22 AP |
2367 | } |
2368 | } | |
046169f0 YY |
2369 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
2370 | if (init_stripe_shared_pages(sh, conf, disks)) { | |
2371 | free_stripe(sc, sh); | |
2372 | return NULL; | |
2373 | } | |
2374 | #endif | |
f18c1a35 N |
2375 | } |
2376 | return sh; | |
2377 | } | |
486f0644 | 2378 | static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) |
1da177e4 LT |
2379 | { |
2380 | struct stripe_head *sh; | |
f18c1a35 | 2381 | |
845b9e22 | 2382 | sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf); |
3f294f4f N |
2383 | if (!sh) |
2384 | return 0; | |
6ce32846 | 2385 | |
a9683a79 | 2386 | if (grow_buffers(sh, gfp)) { |
e4e11e38 | 2387 | shrink_buffers(sh); |
845b9e22 | 2388 | free_stripe(conf->slab_cache, sh); |
3f294f4f N |
2389 | return 0; |
2390 | } | |
486f0644 N |
2391 | sh->hash_lock_index = |
2392 | conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; | |
3f294f4f | 2393 | /* we just created an active stripe so... */ |
3f294f4f | 2394 | atomic_inc(&conf->active_stripes); |
59fc630b | 2395 | |
6d036f7d | 2396 | raid5_release_stripe(sh); |
dfd2bf43 | 2397 | WRITE_ONCE(conf->max_nr_stripes, conf->max_nr_stripes + 1); |
3f294f4f N |
2398 | return 1; |
2399 | } | |
2400 | ||
d1688a6d | 2401 | static int grow_stripes(struct r5conf *conf, int num) |
3f294f4f | 2402 | { |
e18b890b | 2403 | struct kmem_cache *sc; |
53b8d89d | 2404 | size_t namelen = sizeof(conf->cache_name[0]); |
5e5e3e78 | 2405 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
1da177e4 | 2406 | |
176df894 | 2407 | if (mddev_is_dm(conf->mddev)) |
53b8d89d | 2408 | snprintf(conf->cache_name[0], namelen, |
176df894 | 2409 | "raid%d-%p", conf->level, conf->mddev); |
f4be6b43 | 2410 | else |
53b8d89d | 2411 | snprintf(conf->cache_name[0], namelen, |
176df894 | 2412 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
53b8d89d | 2413 | snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]); |
f4be6b43 | 2414 | |
ad01c9e3 N |
2415 | conf->active_name = 0; |
2416 | sc = kmem_cache_create(conf->cache_name[conf->active_name], | |
2f088dfc | 2417 | struct_size_t(struct stripe_head, dev, devs), |
20c2df83 | 2418 | 0, 0, NULL); |
1da177e4 LT |
2419 | if (!sc) |
2420 | return 1; | |
2421 | conf->slab_cache = sc; | |
ad01c9e3 | 2422 | conf->pool_size = devs; |
486f0644 N |
2423 | while (num--) |
2424 | if (!grow_one_stripe(conf, GFP_KERNEL)) | |
1da177e4 | 2425 | return 1; |
486f0644 | 2426 | |
1da177e4 LT |
2427 | return 0; |
2428 | } | |
29269553 | 2429 | |
d6f38f31 | 2430 | /** |
7f8a30e5 CL |
2431 | * scribble_alloc - allocate percpu scribble buffer for required size |
2432 | * of the scribble region | |
2aada5b1 DLM |
2433 | * @percpu: from for_each_present_cpu() of the caller |
2434 | * @num: total number of disks in the array | |
2435 | * @cnt: scribble objs count for required size of the scribble region | |
d6f38f31 | 2436 | * |
7f8a30e5 | 2437 | * The scribble buffer size must be enough to contain: |
d6f38f31 DW |
2438 | * 1/ a struct page pointer for each device in the array +2 |
2439 | * 2/ room to convert each entry in (1) to its corresponding dma | |
2440 | * (dma_map_page()) or page (page_address()) address. | |
2441 | * | |
2442 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we | |
2443 | * calculate over all devices (not just the data blocks), using zeros in place | |
2444 | * of the P and Q blocks. | |
2445 | */ | |
b330e6a4 | 2446 | static int scribble_alloc(struct raid5_percpu *percpu, |
ba54d4d4 | 2447 | int num, int cnt) |
d6f38f31 | 2448 | { |
b330e6a4 | 2449 | size_t obj_size = |
7aba13b7 YY |
2450 | sizeof(struct page *) * (num + 2) + |
2451 | sizeof(addr_conv_t) * (num + 2) + | |
2452 | sizeof(unsigned int) * (num + 2); | |
b330e6a4 | 2453 | void *scribble; |
d6f38f31 | 2454 | |
ba54d4d4 CL |
2455 | /* |
2456 | * If here is in raid array suspend context, it is in memalloc noio | |
2457 | * context as well, there is no potential recursive memory reclaim | |
2458 | * I/Os with the GFP_KERNEL flag. | |
2459 | */ | |
2460 | scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL); | |
b330e6a4 KO |
2461 | if (!scribble) |
2462 | return -ENOMEM; | |
2463 | ||
2464 | kvfree(percpu->scribble); | |
2465 | ||
2466 | percpu->scribble = scribble; | |
2467 | percpu->scribble_obj_size = obj_size; | |
2468 | return 0; | |
d6f38f31 DW |
2469 | } |
2470 | ||
738a2738 N |
2471 | static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) |
2472 | { | |
2473 | unsigned long cpu; | |
2474 | int err = 0; | |
2475 | ||
b42cd7b3 | 2476 | /* Never shrink. */ |
27a353c0 SL |
2477 | if (conf->scribble_disks >= new_disks && |
2478 | conf->scribble_sectors >= new_sectors) | |
2479 | return 0; | |
b42cd7b3 YK |
2480 | |
2481 | raid5_quiesce(conf->mddev, true); | |
252034e0 | 2482 | cpus_read_lock(); |
b330e6a4 | 2483 | |
738a2738 N |
2484 | for_each_present_cpu(cpu) { |
2485 | struct raid5_percpu *percpu; | |
738a2738 N |
2486 | |
2487 | percpu = per_cpu_ptr(conf->percpu, cpu); | |
b330e6a4 | 2488 | err = scribble_alloc(percpu, new_disks, |
c911c46c | 2489 | new_sectors / RAID5_STRIPE_SECTORS(conf)); |
b330e6a4 | 2490 | if (err) |
738a2738 | 2491 | break; |
738a2738 | 2492 | } |
b330e6a4 | 2493 | |
252034e0 | 2494 | cpus_read_unlock(); |
b42cd7b3 YK |
2495 | raid5_quiesce(conf->mddev, false); |
2496 | ||
27a353c0 SL |
2497 | if (!err) { |
2498 | conf->scribble_disks = new_disks; | |
2499 | conf->scribble_sectors = new_sectors; | |
2500 | } | |
738a2738 N |
2501 | return err; |
2502 | } | |
2503 | ||
d1688a6d | 2504 | static int resize_stripes(struct r5conf *conf, int newsize) |
ad01c9e3 N |
2505 | { |
2506 | /* Make all the stripes able to hold 'newsize' devices. | |
2507 | * New slots in each stripe get 'page' set to a new page. | |
2508 | * | |
2509 | * This happens in stages: | |
2510 | * 1/ create a new kmem_cache and allocate the required number of | |
2511 | * stripe_heads. | |
83f0d77a | 2512 | * 2/ gather all the old stripe_heads and transfer the pages across |
ad01c9e3 N |
2513 | * to the new stripe_heads. This will have the side effect of |
2514 | * freezing the array as once all stripe_heads have been collected, | |
2515 | * no IO will be possible. Old stripe heads are freed once their | |
2516 | * pages have been transferred over, and the old kmem_cache is | |
2517 | * freed when all stripes are done. | |
2518 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, | |
3560741e | 2519 | * we simple return a failure status - no need to clean anything up. |
ad01c9e3 N |
2520 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
2521 | * If this fails, we don't bother trying the shrink the | |
2522 | * stripe_heads down again, we just leave them as they are. | |
2523 | * As each stripe_head is processed the new one is released into | |
2524 | * active service. | |
2525 | * | |
2526 | * Once step2 is started, we cannot afford to wait for a write, | |
2527 | * so we use GFP_NOIO allocations. | |
2528 | */ | |
2529 | struct stripe_head *osh, *nsh; | |
2530 | LIST_HEAD(newstripes); | |
2531 | struct disk_info *ndisks; | |
2214c260 | 2532 | int err = 0; |
e18b890b | 2533 | struct kmem_cache *sc; |
ad01c9e3 | 2534 | int i; |
566c09c5 | 2535 | int hash, cnt; |
ad01c9e3 | 2536 | |
2214c260 | 2537 | md_allow_write(conf->mddev); |
2a2275d6 | 2538 | |
ad01c9e3 N |
2539 | /* Step 1 */ |
2540 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], | |
2f088dfc | 2541 | struct_size_t(struct stripe_head, dev, newsize), |
20c2df83 | 2542 | 0, 0, NULL); |
ad01c9e3 N |
2543 | if (!sc) |
2544 | return -ENOMEM; | |
2545 | ||
2d5b569b N |
2546 | /* Need to ensure auto-resizing doesn't interfere */ |
2547 | mutex_lock(&conf->cache_size_mutex); | |
2548 | ||
ad01c9e3 | 2549 | for (i = conf->max_nr_stripes; i; i--) { |
845b9e22 | 2550 | nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf); |
ad01c9e3 N |
2551 | if (!nsh) |
2552 | break; | |
2553 | ||
ad01c9e3 N |
2554 | list_add(&nsh->lru, &newstripes); |
2555 | } | |
2556 | if (i) { | |
2557 | /* didn't get enough, give up */ | |
2558 | while (!list_empty(&newstripes)) { | |
2559 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
2560 | list_del(&nsh->lru); | |
845b9e22 | 2561 | free_stripe(sc, nsh); |
ad01c9e3 N |
2562 | } |
2563 | kmem_cache_destroy(sc); | |
2d5b569b | 2564 | mutex_unlock(&conf->cache_size_mutex); |
ad01c9e3 N |
2565 | return -ENOMEM; |
2566 | } | |
2567 | /* Step 2 - Must use GFP_NOIO now. | |
2568 | * OK, we have enough stripes, start collecting inactive | |
2569 | * stripes and copying them over | |
2570 | */ | |
566c09c5 SL |
2571 | hash = 0; |
2572 | cnt = 0; | |
ad01c9e3 | 2573 | list_for_each_entry(nsh, &newstripes, lru) { |
566c09c5 | 2574 | lock_device_hash_lock(conf, hash); |
6ab2a4b8 | 2575 | wait_event_cmd(conf->wait_for_stripe, |
566c09c5 SL |
2576 | !list_empty(conf->inactive_list + hash), |
2577 | unlock_device_hash_lock(conf, hash), | |
2578 | lock_device_hash_lock(conf, hash)); | |
2579 | osh = get_free_stripe(conf, hash); | |
2580 | unlock_device_hash_lock(conf, hash); | |
f18c1a35 | 2581 | |
f16acaf3 YY |
2582 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
2583 | for (i = 0; i < osh->nr_pages; i++) { | |
2584 | nsh->pages[i] = osh->pages[i]; | |
2585 | osh->pages[i] = NULL; | |
2586 | } | |
2587 | #endif | |
d592a996 | 2588 | for(i=0; i<conf->pool_size; i++) { |
ad01c9e3 | 2589 | nsh->dev[i].page = osh->dev[i].page; |
d592a996 | 2590 | nsh->dev[i].orig_page = osh->dev[i].page; |
7aba13b7 | 2591 | nsh->dev[i].offset = osh->dev[i].offset; |
d592a996 | 2592 | } |
566c09c5 | 2593 | nsh->hash_lock_index = hash; |
845b9e22 | 2594 | free_stripe(conf->slab_cache, osh); |
566c09c5 SL |
2595 | cnt++; |
2596 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + | |
2597 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { | |
2598 | hash++; | |
2599 | cnt = 0; | |
2600 | } | |
ad01c9e3 N |
2601 | } |
2602 | kmem_cache_destroy(conf->slab_cache); | |
2603 | ||
2604 | /* Step 3. | |
2605 | * At this point, we are holding all the stripes so the array | |
2606 | * is completely stalled, so now is a good time to resize | |
d6f38f31 | 2607 | * conf->disks and the scribble region |
ad01c9e3 | 2608 | */ |
6396bb22 | 2609 | ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO); |
ad01c9e3 | 2610 | if (ndisks) { |
d7bd398e | 2611 | for (i = 0; i < conf->pool_size; i++) |
ad01c9e3 | 2612 | ndisks[i] = conf->disks[i]; |
d7bd398e SL |
2613 | |
2614 | for (i = conf->pool_size; i < newsize; i++) { | |
2615 | ndisks[i].extra_page = alloc_page(GFP_NOIO); | |
2616 | if (!ndisks[i].extra_page) | |
2617 | err = -ENOMEM; | |
2618 | } | |
2619 | ||
2620 | if (err) { | |
2621 | for (i = conf->pool_size; i < newsize; i++) | |
2622 | if (ndisks[i].extra_page) | |
2623 | put_page(ndisks[i].extra_page); | |
2624 | kfree(ndisks); | |
2625 | } else { | |
2626 | kfree(conf->disks); | |
2627 | conf->disks = ndisks; | |
2628 | } | |
ad01c9e3 N |
2629 | } else |
2630 | err = -ENOMEM; | |
2631 | ||
583da48e DY |
2632 | conf->slab_cache = sc; |
2633 | conf->active_name = 1-conf->active_name; | |
2634 | ||
ad01c9e3 N |
2635 | /* Step 4, return new stripes to service */ |
2636 | while(!list_empty(&newstripes)) { | |
2637 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
2638 | list_del_init(&nsh->lru); | |
d6f38f31 | 2639 | |
f16acaf3 YY |
2640 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
2641 | for (i = 0; i < nsh->nr_pages; i++) { | |
2642 | if (nsh->pages[i]) | |
2643 | continue; | |
2644 | nsh->pages[i] = alloc_page(GFP_NOIO); | |
2645 | if (!nsh->pages[i]) | |
2646 | err = -ENOMEM; | |
2647 | } | |
2648 | ||
2649 | for (i = conf->raid_disks; i < newsize; i++) { | |
2650 | if (nsh->dev[i].page) | |
2651 | continue; | |
2652 | nsh->dev[i].page = raid5_get_dev_page(nsh, i); | |
2653 | nsh->dev[i].orig_page = nsh->dev[i].page; | |
2654 | nsh->dev[i].offset = raid5_get_page_offset(nsh, i); | |
2655 | } | |
2656 | #else | |
ad01c9e3 N |
2657 | for (i=conf->raid_disks; i < newsize; i++) |
2658 | if (nsh->dev[i].page == NULL) { | |
2659 | struct page *p = alloc_page(GFP_NOIO); | |
2660 | nsh->dev[i].page = p; | |
d592a996 | 2661 | nsh->dev[i].orig_page = p; |
7aba13b7 | 2662 | nsh->dev[i].offset = 0; |
ad01c9e3 N |
2663 | if (!p) |
2664 | err = -ENOMEM; | |
2665 | } | |
f16acaf3 | 2666 | #endif |
6d036f7d | 2667 | raid5_release_stripe(nsh); |
ad01c9e3 N |
2668 | } |
2669 | /* critical section pass, GFP_NOIO no longer needed */ | |
2670 | ||
6e9eac2d N |
2671 | if (!err) |
2672 | conf->pool_size = newsize; | |
b44c018c SL |
2673 | mutex_unlock(&conf->cache_size_mutex); |
2674 | ||
ad01c9e3 N |
2675 | return err; |
2676 | } | |
1da177e4 | 2677 | |
486f0644 | 2678 | static int drop_one_stripe(struct r5conf *conf) |
1da177e4 LT |
2679 | { |
2680 | struct stripe_head *sh; | |
49895bcc | 2681 | int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; |
1da177e4 | 2682 | |
566c09c5 SL |
2683 | spin_lock_irq(conf->hash_locks + hash); |
2684 | sh = get_free_stripe(conf, hash); | |
2685 | spin_unlock_irq(conf->hash_locks + hash); | |
3f294f4f N |
2686 | if (!sh) |
2687 | return 0; | |
78bafebd | 2688 | BUG_ON(atomic_read(&sh->count)); |
e4e11e38 | 2689 | shrink_buffers(sh); |
845b9e22 | 2690 | free_stripe(conf->slab_cache, sh); |
3f294f4f | 2691 | atomic_dec(&conf->active_stripes); |
dfd2bf43 | 2692 | WRITE_ONCE(conf->max_nr_stripes, conf->max_nr_stripes - 1); |
3f294f4f N |
2693 | return 1; |
2694 | } | |
2695 | ||
d1688a6d | 2696 | static void shrink_stripes(struct r5conf *conf) |
3f294f4f | 2697 | { |
486f0644 N |
2698 | while (conf->max_nr_stripes && |
2699 | drop_one_stripe(conf)) | |
2700 | ; | |
3f294f4f | 2701 | |
644df1a8 | 2702 | kmem_cache_destroy(conf->slab_cache); |
1da177e4 LT |
2703 | conf->slab_cache = NULL; |
2704 | } | |
2705 | ||
4246a0b6 | 2706 | static void raid5_end_read_request(struct bio * bi) |
1da177e4 | 2707 | { |
99c0fb5f | 2708 | struct stripe_head *sh = bi->bi_private; |
d1688a6d | 2709 | struct r5conf *conf = sh->raid_conf; |
7ecaa1e6 | 2710 | int disks = sh->disks, i; |
dd054fce | 2711 | struct md_rdev *rdev = NULL; |
05616be5 | 2712 | sector_t s; |
1da177e4 LT |
2713 | |
2714 | for (i=0 ; i<disks; i++) | |
2715 | if (bi == &sh->dev[i].req) | |
2716 | break; | |
2717 | ||
4246a0b6 | 2718 | pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", |
45b4233c | 2719 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
4e4cbee9 | 2720 | bi->bi_status); |
1da177e4 LT |
2721 | if (i == disks) { |
2722 | BUG(); | |
6712ecf8 | 2723 | return; |
1da177e4 | 2724 | } |
14a75d3e | 2725 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
dd054fce N |
2726 | /* If replacement finished while this request was outstanding, |
2727 | * 'replacement' might be NULL already. | |
2728 | * In that case it moved down to 'rdev'. | |
2729 | * rdev is not removed until all requests are finished. | |
2730 | */ | |
ad860670 | 2731 | rdev = conf->disks[i].replacement; |
dd054fce | 2732 | if (!rdev) |
ad860670 | 2733 | rdev = conf->disks[i].rdev; |
1da177e4 | 2734 | |
05616be5 N |
2735 | if (use_new_offset(conf, sh)) |
2736 | s = sh->sector + rdev->new_data_offset; | |
2737 | else | |
2738 | s = sh->sector + rdev->data_offset; | |
4e4cbee9 | 2739 | if (!bi->bi_status) { |
1da177e4 | 2740 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
4e5314b5 | 2741 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
14a75d3e N |
2742 | /* Note that this cannot happen on a |
2743 | * replacement device. We just fail those on | |
2744 | * any error | |
2745 | */ | |
cc6167b4 | 2746 | pr_info_ratelimited( |
913cce5a | 2747 | "md/raid:%s: read error corrected (%lu sectors at %llu on %pg)\n", |
c911c46c | 2748 | mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf), |
05616be5 | 2749 | (unsigned long long)s, |
913cce5a | 2750 | rdev->bdev); |
c911c46c | 2751 | atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors); |
4e5314b5 N |
2752 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
2753 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
3f9e7c14 | 2754 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
2755 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
2756 | ||
86aa1397 SL |
2757 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
2758 | /* | |
2759 | * end read for a page in journal, this | |
2760 | * must be preparing for prexor in rmw | |
2761 | */ | |
2762 | set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); | |
2763 | ||
14a75d3e N |
2764 | if (atomic_read(&rdev->read_errors)) |
2765 | atomic_set(&rdev->read_errors, 0); | |
1da177e4 | 2766 | } else { |
ba22dcbf | 2767 | int retry = 0; |
2e8ac303 | 2768 | int set_bad = 0; |
d6950432 | 2769 | |
1da177e4 | 2770 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
b76b4715 NC |
2771 | if (!(bi->bi_status == BLK_STS_PROTECTION)) |
2772 | atomic_inc(&rdev->read_errors); | |
14a75d3e | 2773 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
cc6167b4 | 2774 | pr_warn_ratelimited( |
913cce5a | 2775 | "md/raid:%s: read error on replacement device (sector %llu on %pg).\n", |
14a75d3e | 2776 | mdname(conf->mddev), |
05616be5 | 2777 | (unsigned long long)s, |
913cce5a | 2778 | rdev->bdev); |
2e8ac303 | 2779 | else if (conf->mddev->degraded >= conf->max_degraded) { |
2780 | set_bad = 1; | |
cc6167b4 | 2781 | pr_warn_ratelimited( |
913cce5a | 2782 | "md/raid:%s: read error not correctable (sector %llu on %pg).\n", |
8bda470e | 2783 | mdname(conf->mddev), |
05616be5 | 2784 | (unsigned long long)s, |
913cce5a | 2785 | rdev->bdev); |
2e8ac303 | 2786 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
4e5314b5 | 2787 | /* Oh, no!!! */ |
2e8ac303 | 2788 | set_bad = 1; |
cc6167b4 | 2789 | pr_warn_ratelimited( |
913cce5a | 2790 | "md/raid:%s: read error NOT corrected!! (sector %llu on %pg).\n", |
8bda470e | 2791 | mdname(conf->mddev), |
05616be5 | 2792 | (unsigned long long)s, |
913cce5a | 2793 | rdev->bdev); |
2e8ac303 | 2794 | } else if (atomic_read(&rdev->read_errors) |
0009fad0 NC |
2795 | > conf->max_nr_stripes) { |
2796 | if (!test_bit(Faulty, &rdev->flags)) { | |
2797 | pr_warn("md/raid:%s: %d read_errors > %d stripes\n", | |
2798 | mdname(conf->mddev), | |
2799 | atomic_read(&rdev->read_errors), | |
2800 | conf->max_nr_stripes); | |
913cce5a CH |
2801 | pr_warn("md/raid:%s: Too many read errors, failing device %pg.\n", |
2802 | mdname(conf->mddev), rdev->bdev); | |
0009fad0 NC |
2803 | } |
2804 | } else | |
ba22dcbf | 2805 | retry = 1; |
edfa1f65 BY |
2806 | if (set_bad && test_bit(In_sync, &rdev->flags) |
2807 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) | |
2808 | retry = 1; | |
ba22dcbf | 2809 | if (retry) |
143f6e73 XN |
2810 | if (sh->qd_idx >= 0 && sh->pd_idx == i) |
2811 | set_bit(R5_ReadError, &sh->dev[i].flags); | |
2812 | else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { | |
3f9e7c14 | 2813 | set_bit(R5_ReadError, &sh->dev[i].flags); |
2814 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
2815 | } else | |
2816 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
ba22dcbf | 2817 | else { |
4e5314b5 N |
2818 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
2819 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
2e8ac303 | 2820 | if (!(set_bad |
2821 | && test_bit(In_sync, &rdev->flags) | |
2822 | && rdev_set_badblocks( | |
c911c46c | 2823 | rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0))) |
2e8ac303 | 2824 | md_error(conf->mddev, rdev); |
ba22dcbf | 2825 | } |
1da177e4 | 2826 | } |
14a75d3e | 2827 | rdev_dec_pending(rdev, conf->mddev); |
03a6b195 | 2828 | bio_uninit(bi); |
1da177e4 LT |
2829 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
2830 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 2831 | raid5_release_stripe(sh); |
1da177e4 LT |
2832 | } |
2833 | ||
4246a0b6 | 2834 | static void raid5_end_write_request(struct bio *bi) |
1da177e4 | 2835 | { |
99c0fb5f | 2836 | struct stripe_head *sh = bi->bi_private; |
d1688a6d | 2837 | struct r5conf *conf = sh->raid_conf; |
7ecaa1e6 | 2838 | int disks = sh->disks, i; |
3f649ab7 | 2839 | struct md_rdev *rdev; |
977df362 | 2840 | int replacement = 0; |
1da177e4 | 2841 | |
977df362 N |
2842 | for (i = 0 ; i < disks; i++) { |
2843 | if (bi == &sh->dev[i].req) { | |
ad860670 | 2844 | rdev = conf->disks[i].rdev; |
1da177e4 | 2845 | break; |
977df362 N |
2846 | } |
2847 | if (bi == &sh->dev[i].rreq) { | |
ad860670 | 2848 | rdev = conf->disks[i].replacement; |
dd054fce N |
2849 | if (rdev) |
2850 | replacement = 1; | |
2851 | else | |
2852 | /* rdev was removed and 'replacement' | |
2853 | * replaced it. rdev is not removed | |
2854 | * until all requests are finished. | |
2855 | */ | |
ad860670 | 2856 | rdev = conf->disks[i].rdev; |
977df362 N |
2857 | break; |
2858 | } | |
2859 | } | |
4246a0b6 | 2860 | pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", |
1da177e4 | 2861 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
4e4cbee9 | 2862 | bi->bi_status); |
1da177e4 LT |
2863 | if (i == disks) { |
2864 | BUG(); | |
6712ecf8 | 2865 | return; |
1da177e4 LT |
2866 | } |
2867 | ||
977df362 | 2868 | if (replacement) { |
4e4cbee9 | 2869 | if (bi->bi_status) |
977df362 | 2870 | md_error(conf->mddev, rdev); |
3a0f007b YK |
2871 | else if (rdev_has_badblock(rdev, sh->sector, |
2872 | RAID5_STRIPE_SECTORS(conf))) | |
977df362 N |
2873 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
2874 | } else { | |
4e4cbee9 | 2875 | if (bi->bi_status) { |
977df362 N |
2876 | set_bit(WriteErrorSeen, &rdev->flags); |
2877 | set_bit(R5_WriteError, &sh->dev[i].flags); | |
3a6de292 N |
2878 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
2879 | set_bit(MD_RECOVERY_NEEDED, | |
2880 | &rdev->mddev->recovery); | |
3a0f007b YK |
2881 | } else if (rdev_has_badblock(rdev, sh->sector, |
2882 | RAID5_STRIPE_SECTORS(conf))) { | |
977df362 | 2883 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
c0b32972 N |
2884 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
2885 | /* That was a successful write so make | |
2886 | * sure it looks like we already did | |
2887 | * a re-write. | |
2888 | */ | |
2889 | set_bit(R5_ReWrite, &sh->dev[i].flags); | |
2890 | } | |
977df362 N |
2891 | } |
2892 | rdev_dec_pending(rdev, conf->mddev); | |
1da177e4 | 2893 | |
4e4cbee9 | 2894 | if (sh->batch_head && bi->bi_status && !replacement) |
72ac7330 | 2895 | set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); |
2896 | ||
03a6b195 | 2897 | bio_uninit(bi); |
977df362 N |
2898 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
2899 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
1da177e4 | 2900 | set_bit(STRIPE_HANDLE, &sh->state); |
59fc630b | 2901 | |
2902 | if (sh->batch_head && sh != sh->batch_head) | |
6d036f7d | 2903 | raid5_release_stripe(sh->batch_head); |
10421247 | 2904 | raid5_release_stripe(sh); |
1da177e4 LT |
2905 | } |
2906 | ||
849674e4 | 2907 | static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 2908 | { |
d1688a6d | 2909 | struct r5conf *conf = mddev->private; |
908f4fbd | 2910 | unsigned long flags; |
0c55e022 | 2911 | pr_debug("raid456: error called\n"); |
1da177e4 | 2912 | |
913cce5a CH |
2913 | pr_crit("md/raid:%s: Disk failure on %pg, disabling device.\n", |
2914 | mdname(mddev), rdev->bdev); | |
57668f0a | 2915 | |
908f4fbd | 2916 | spin_lock_irqsave(&conf->device_lock, flags); |
57668f0a MT |
2917 | set_bit(Faulty, &rdev->flags); |
2918 | clear_bit(In_sync, &rdev->flags); | |
2919 | mddev->degraded = raid5_calc_degraded(conf); | |
fb73b357 | 2920 | |
57668f0a MT |
2921 | if (has_failed(conf)) { |
2922 | set_bit(MD_BROKEN, &conf->mddev->flags); | |
fb73b357 | 2923 | conf->recovery_disabled = mddev->recovery_disabled; |
57668f0a MT |
2924 | |
2925 | pr_crit("md/raid:%s: Cannot continue operation (%d/%d failed).\n", | |
2926 | mdname(mddev), mddev->degraded, conf->raid_disks); | |
2927 | } else { | |
2928 | pr_crit("md/raid:%s: Operation continuing on %d devices.\n", | |
2929 | mdname(mddev), conf->raid_disks - mddev->degraded); | |
fb73b357 MT |
2930 | } |
2931 | ||
908f4fbd N |
2932 | spin_unlock_irqrestore(&conf->device_lock, flags); |
2933 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
2934 | ||
de393cde | 2935 | set_bit(Blocked, &rdev->flags); |
2953079c SL |
2936 | set_mask_bits(&mddev->sb_flags, 0, |
2937 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); | |
70d466f7 | 2938 | r5c_update_on_rdev_error(mddev, rdev); |
16a53ecc | 2939 | } |
1da177e4 LT |
2940 | |
2941 | /* | |
2942 | * Input: a 'big' sector number, | |
2943 | * Output: index of the data and parity disk, and the sector # in them. | |
2944 | */ | |
6d036f7d SL |
2945 | sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
2946 | int previous, int *dd_idx, | |
2947 | struct stripe_head *sh) | |
1da177e4 | 2948 | { |
6e3b96ed | 2949 | sector_t stripe, stripe2; |
35f2a591 | 2950 | sector_t chunk_number; |
1da177e4 | 2951 | unsigned int chunk_offset; |
911d4ee8 | 2952 | int pd_idx, qd_idx; |
67cc2b81 | 2953 | int ddf_layout = 0; |
1da177e4 | 2954 | sector_t new_sector; |
e183eaed N |
2955 | int algorithm = previous ? conf->prev_algo |
2956 | : conf->algorithm; | |
09c9e5fa AN |
2957 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
2958 | : conf->chunk_sectors; | |
112bf897 N |
2959 | int raid_disks = previous ? conf->previous_raid_disks |
2960 | : conf->raid_disks; | |
2961 | int data_disks = raid_disks - conf->max_degraded; | |
1da177e4 LT |
2962 | |
2963 | /* First compute the information on this sector */ | |
2964 | ||
2965 | /* | |
2966 | * Compute the chunk number and the sector offset inside the chunk | |
2967 | */ | |
2968 | chunk_offset = sector_div(r_sector, sectors_per_chunk); | |
2969 | chunk_number = r_sector; | |
1da177e4 LT |
2970 | |
2971 | /* | |
2972 | * Compute the stripe number | |
2973 | */ | |
35f2a591 N |
2974 | stripe = chunk_number; |
2975 | *dd_idx = sector_div(stripe, data_disks); | |
6e3b96ed | 2976 | stripe2 = stripe; |
1da177e4 LT |
2977 | /* |
2978 | * Select the parity disk based on the user selected algorithm. | |
2979 | */ | |
84789554 | 2980 | pd_idx = qd_idx = -1; |
16a53ecc N |
2981 | switch(conf->level) { |
2982 | case 4: | |
911d4ee8 | 2983 | pd_idx = data_disks; |
16a53ecc N |
2984 | break; |
2985 | case 5: | |
e183eaed | 2986 | switch (algorithm) { |
1da177e4 | 2987 | case ALGORITHM_LEFT_ASYMMETRIC: |
6e3b96ed | 2988 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
911d4ee8 | 2989 | if (*dd_idx >= pd_idx) |
1da177e4 LT |
2990 | (*dd_idx)++; |
2991 | break; | |
2992 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
6e3b96ed | 2993 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 | 2994 | if (*dd_idx >= pd_idx) |
1da177e4 LT |
2995 | (*dd_idx)++; |
2996 | break; | |
2997 | case ALGORITHM_LEFT_SYMMETRIC: | |
6e3b96ed | 2998 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
911d4ee8 | 2999 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
1da177e4 LT |
3000 | break; |
3001 | case ALGORITHM_RIGHT_SYMMETRIC: | |
6e3b96ed | 3002 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 | 3003 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
1da177e4 | 3004 | break; |
99c0fb5f N |
3005 | case ALGORITHM_PARITY_0: |
3006 | pd_idx = 0; | |
3007 | (*dd_idx)++; | |
3008 | break; | |
3009 | case ALGORITHM_PARITY_N: | |
3010 | pd_idx = data_disks; | |
3011 | break; | |
1da177e4 | 3012 | default: |
99c0fb5f | 3013 | BUG(); |
16a53ecc N |
3014 | } |
3015 | break; | |
3016 | case 6: | |
3017 | ||
e183eaed | 3018 | switch (algorithm) { |
16a53ecc | 3019 | case ALGORITHM_LEFT_ASYMMETRIC: |
6e3b96ed | 3020 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
911d4ee8 N |
3021 | qd_idx = pd_idx + 1; |
3022 | if (pd_idx == raid_disks-1) { | |
99c0fb5f | 3023 | (*dd_idx)++; /* Q D D D P */ |
911d4ee8 N |
3024 | qd_idx = 0; |
3025 | } else if (*dd_idx >= pd_idx) | |
16a53ecc N |
3026 | (*dd_idx) += 2; /* D D P Q D */ |
3027 | break; | |
3028 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
6e3b96ed | 3029 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 N |
3030 | qd_idx = pd_idx + 1; |
3031 | if (pd_idx == raid_disks-1) { | |
99c0fb5f | 3032 | (*dd_idx)++; /* Q D D D P */ |
911d4ee8 N |
3033 | qd_idx = 0; |
3034 | } else if (*dd_idx >= pd_idx) | |
16a53ecc N |
3035 | (*dd_idx) += 2; /* D D P Q D */ |
3036 | break; | |
3037 | case ALGORITHM_LEFT_SYMMETRIC: | |
6e3b96ed | 3038 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
911d4ee8 N |
3039 | qd_idx = (pd_idx + 1) % raid_disks; |
3040 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; | |
16a53ecc N |
3041 | break; |
3042 | case ALGORITHM_RIGHT_SYMMETRIC: | |
6e3b96ed | 3043 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 N |
3044 | qd_idx = (pd_idx + 1) % raid_disks; |
3045 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; | |
16a53ecc | 3046 | break; |
99c0fb5f N |
3047 | |
3048 | case ALGORITHM_PARITY_0: | |
3049 | pd_idx = 0; | |
3050 | qd_idx = 1; | |
3051 | (*dd_idx) += 2; | |
3052 | break; | |
3053 | case ALGORITHM_PARITY_N: | |
3054 | pd_idx = data_disks; | |
3055 | qd_idx = data_disks + 1; | |
3056 | break; | |
3057 | ||
3058 | case ALGORITHM_ROTATING_ZERO_RESTART: | |
3059 | /* Exactly the same as RIGHT_ASYMMETRIC, but or | |
3060 | * of blocks for computing Q is different. | |
3061 | */ | |
6e3b96ed | 3062 | pd_idx = sector_div(stripe2, raid_disks); |
99c0fb5f N |
3063 | qd_idx = pd_idx + 1; |
3064 | if (pd_idx == raid_disks-1) { | |
3065 | (*dd_idx)++; /* Q D D D P */ | |
3066 | qd_idx = 0; | |
3067 | } else if (*dd_idx >= pd_idx) | |
3068 | (*dd_idx) += 2; /* D D P Q D */ | |
67cc2b81 | 3069 | ddf_layout = 1; |
99c0fb5f N |
3070 | break; |
3071 | ||
3072 | case ALGORITHM_ROTATING_N_RESTART: | |
3073 | /* Same a left_asymmetric, by first stripe is | |
3074 | * D D D P Q rather than | |
3075 | * Q D D D P | |
3076 | */ | |
6e3b96ed N |
3077 | stripe2 += 1; |
3078 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); | |
99c0fb5f N |
3079 | qd_idx = pd_idx + 1; |
3080 | if (pd_idx == raid_disks-1) { | |
3081 | (*dd_idx)++; /* Q D D D P */ | |
3082 | qd_idx = 0; | |
3083 | } else if (*dd_idx >= pd_idx) | |
3084 | (*dd_idx) += 2; /* D D P Q D */ | |
67cc2b81 | 3085 | ddf_layout = 1; |
99c0fb5f N |
3086 | break; |
3087 | ||
3088 | case ALGORITHM_ROTATING_N_CONTINUE: | |
3089 | /* Same as left_symmetric but Q is before P */ | |
6e3b96ed | 3090 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
99c0fb5f N |
3091 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
3092 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; | |
67cc2b81 | 3093 | ddf_layout = 1; |
99c0fb5f N |
3094 | break; |
3095 | ||
3096 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
3097 | /* RAID5 left_asymmetric, with Q on last device */ | |
6e3b96ed | 3098 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
3099 | if (*dd_idx >= pd_idx) |
3100 | (*dd_idx)++; | |
3101 | qd_idx = raid_disks - 1; | |
3102 | break; | |
3103 | ||
3104 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
6e3b96ed | 3105 | pd_idx = sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
3106 | if (*dd_idx >= pd_idx) |
3107 | (*dd_idx)++; | |
3108 | qd_idx = raid_disks - 1; | |
3109 | break; | |
3110 | ||
3111 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
6e3b96ed | 3112 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
3113 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
3114 | qd_idx = raid_disks - 1; | |
3115 | break; | |
3116 | ||
3117 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
6e3b96ed | 3118 | pd_idx = sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
3119 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
3120 | qd_idx = raid_disks - 1; | |
3121 | break; | |
3122 | ||
3123 | case ALGORITHM_PARITY_0_6: | |
3124 | pd_idx = 0; | |
3125 | (*dd_idx)++; | |
3126 | qd_idx = raid_disks - 1; | |
3127 | break; | |
3128 | ||
16a53ecc | 3129 | default: |
99c0fb5f | 3130 | BUG(); |
16a53ecc N |
3131 | } |
3132 | break; | |
1da177e4 LT |
3133 | } |
3134 | ||
911d4ee8 N |
3135 | if (sh) { |
3136 | sh->pd_idx = pd_idx; | |
3137 | sh->qd_idx = qd_idx; | |
67cc2b81 | 3138 | sh->ddf_layout = ddf_layout; |
911d4ee8 | 3139 | } |
1da177e4 LT |
3140 | /* |
3141 | * Finally, compute the new sector number | |
3142 | */ | |
3143 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; | |
3144 | return new_sector; | |
3145 | } | |
3146 | ||
6d036f7d | 3147 | sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous) |
1da177e4 | 3148 | { |
d1688a6d | 3149 | struct r5conf *conf = sh->raid_conf; |
b875e531 N |
3150 | int raid_disks = sh->disks; |
3151 | int data_disks = raid_disks - conf->max_degraded; | |
1da177e4 | 3152 | sector_t new_sector = sh->sector, check; |
09c9e5fa AN |
3153 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
3154 | : conf->chunk_sectors; | |
e183eaed N |
3155 | int algorithm = previous ? conf->prev_algo |
3156 | : conf->algorithm; | |
1da177e4 LT |
3157 | sector_t stripe; |
3158 | int chunk_offset; | |
35f2a591 N |
3159 | sector_t chunk_number; |
3160 | int dummy1, dd_idx = i; | |
1da177e4 | 3161 | sector_t r_sector; |
911d4ee8 | 3162 | struct stripe_head sh2; |
1da177e4 LT |
3163 | |
3164 | chunk_offset = sector_div(new_sector, sectors_per_chunk); | |
3165 | stripe = new_sector; | |
1da177e4 | 3166 | |
16a53ecc N |
3167 | if (i == sh->pd_idx) |
3168 | return 0; | |
3169 | switch(conf->level) { | |
3170 | case 4: break; | |
3171 | case 5: | |
e183eaed | 3172 | switch (algorithm) { |
1da177e4 LT |
3173 | case ALGORITHM_LEFT_ASYMMETRIC: |
3174 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
3175 | if (i > sh->pd_idx) | |
3176 | i--; | |
3177 | break; | |
3178 | case ALGORITHM_LEFT_SYMMETRIC: | |
3179 | case ALGORITHM_RIGHT_SYMMETRIC: | |
3180 | if (i < sh->pd_idx) | |
3181 | i += raid_disks; | |
3182 | i -= (sh->pd_idx + 1); | |
3183 | break; | |
99c0fb5f N |
3184 | case ALGORITHM_PARITY_0: |
3185 | i -= 1; | |
3186 | break; | |
3187 | case ALGORITHM_PARITY_N: | |
3188 | break; | |
1da177e4 | 3189 | default: |
99c0fb5f | 3190 | BUG(); |
16a53ecc N |
3191 | } |
3192 | break; | |
3193 | case 6: | |
d0dabf7e | 3194 | if (i == sh->qd_idx) |
16a53ecc | 3195 | return 0; /* It is the Q disk */ |
e183eaed | 3196 | switch (algorithm) { |
16a53ecc N |
3197 | case ALGORITHM_LEFT_ASYMMETRIC: |
3198 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
99c0fb5f N |
3199 | case ALGORITHM_ROTATING_ZERO_RESTART: |
3200 | case ALGORITHM_ROTATING_N_RESTART: | |
3201 | if (sh->pd_idx == raid_disks-1) | |
3202 | i--; /* Q D D D P */ | |
16a53ecc N |
3203 | else if (i > sh->pd_idx) |
3204 | i -= 2; /* D D P Q D */ | |
3205 | break; | |
3206 | case ALGORITHM_LEFT_SYMMETRIC: | |
3207 | case ALGORITHM_RIGHT_SYMMETRIC: | |
3208 | if (sh->pd_idx == raid_disks-1) | |
3209 | i--; /* Q D D D P */ | |
3210 | else { | |
3211 | /* D D P Q D */ | |
3212 | if (i < sh->pd_idx) | |
3213 | i += raid_disks; | |
3214 | i -= (sh->pd_idx + 2); | |
3215 | } | |
3216 | break; | |
99c0fb5f N |
3217 | case ALGORITHM_PARITY_0: |
3218 | i -= 2; | |
3219 | break; | |
3220 | case ALGORITHM_PARITY_N: | |
3221 | break; | |
3222 | case ALGORITHM_ROTATING_N_CONTINUE: | |
e4424fee | 3223 | /* Like left_symmetric, but P is before Q */ |
99c0fb5f N |
3224 | if (sh->pd_idx == 0) |
3225 | i--; /* P D D D Q */ | |
e4424fee N |
3226 | else { |
3227 | /* D D Q P D */ | |
3228 | if (i < sh->pd_idx) | |
3229 | i += raid_disks; | |
3230 | i -= (sh->pd_idx + 1); | |
3231 | } | |
99c0fb5f N |
3232 | break; |
3233 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
3234 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
3235 | if (i > sh->pd_idx) | |
3236 | i--; | |
3237 | break; | |
3238 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
3239 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
3240 | if (i < sh->pd_idx) | |
3241 | i += data_disks + 1; | |
3242 | i -= (sh->pd_idx + 1); | |
3243 | break; | |
3244 | case ALGORITHM_PARITY_0_6: | |
3245 | i -= 1; | |
3246 | break; | |
16a53ecc | 3247 | default: |
99c0fb5f | 3248 | BUG(); |
16a53ecc N |
3249 | } |
3250 | break; | |
1da177e4 LT |
3251 | } |
3252 | ||
3253 | chunk_number = stripe * data_disks + i; | |
35f2a591 | 3254 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
1da177e4 | 3255 | |
112bf897 | 3256 | check = raid5_compute_sector(conf, r_sector, |
784052ec | 3257 | previous, &dummy1, &sh2); |
911d4ee8 N |
3258 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
3259 | || sh2.qd_idx != sh->qd_idx) { | |
cc6167b4 N |
3260 | pr_warn("md/raid:%s: compute_blocknr: map not correct\n", |
3261 | mdname(conf->mddev)); | |
1da177e4 LT |
3262 | return 0; |
3263 | } | |
3264 | return r_sector; | |
3265 | } | |
3266 | ||
07e83364 SL |
3267 | /* |
3268 | * There are cases where we want handle_stripe_dirtying() and | |
3269 | * schedule_reconstruction() to delay towrite to some dev of a stripe. | |
3270 | * | |
3271 | * This function checks whether we want to delay the towrite. Specifically, | |
3272 | * we delay the towrite when: | |
3273 | * | |
3274 | * 1. degraded stripe has a non-overwrite to the missing dev, AND this | |
3275 | * stripe has data in journal (for other devices). | |
3276 | * | |
3277 | * In this case, when reading data for the non-overwrite dev, it is | |
3278 | * necessary to handle complex rmw of write back cache (prexor with | |
3279 | * orig_page, and xor with page). To keep read path simple, we would | |
3280 | * like to flush data in journal to RAID disks first, so complex rmw | |
3281 | * is handled in the write patch (handle_stripe_dirtying). | |
3282 | * | |
39b99586 SL |
3283 | * 2. when journal space is critical (R5C_LOG_CRITICAL=1) |
3284 | * | |
3285 | * It is important to be able to flush all stripes in raid5-cache. | |
3286 | * Therefore, we need reserve some space on the journal device for | |
3287 | * these flushes. If flush operation includes pending writes to the | |
3288 | * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe | |
3289 | * for the flush out. If we exclude these pending writes from flush | |
3290 | * operation, we only need (conf->max_degraded + 1) pages per stripe. | |
3291 | * Therefore, excluding pending writes in these cases enables more | |
3292 | * efficient use of the journal device. | |
3293 | * | |
3294 | * Note: To make sure the stripe makes progress, we only delay | |
3295 | * towrite for stripes with data already in journal (injournal > 0). | |
3296 | * When LOG_CRITICAL, stripes with injournal == 0 will be sent to | |
3297 | * no_space_stripes list. | |
3298 | * | |
70d466f7 SL |
3299 | * 3. during journal failure |
3300 | * In journal failure, we try to flush all cached data to raid disks | |
3301 | * based on data in stripe cache. The array is read-only to upper | |
3302 | * layers, so we would skip all pending writes. | |
3303 | * | |
07e83364 | 3304 | */ |
39b99586 SL |
3305 | static inline bool delay_towrite(struct r5conf *conf, |
3306 | struct r5dev *dev, | |
3307 | struct stripe_head_state *s) | |
07e83364 | 3308 | { |
39b99586 SL |
3309 | /* case 1 above */ |
3310 | if (!test_bit(R5_OVERWRITE, &dev->flags) && | |
3311 | !test_bit(R5_Insync, &dev->flags) && s->injournal) | |
3312 | return true; | |
3313 | /* case 2 above */ | |
3314 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && | |
3315 | s->injournal > 0) | |
3316 | return true; | |
70d466f7 SL |
3317 | /* case 3 above */ |
3318 | if (s->log_failed && s->injournal) | |
3319 | return true; | |
39b99586 | 3320 | return false; |
07e83364 SL |
3321 | } |
3322 | ||
600aa109 | 3323 | static void |
c0f7bddb | 3324 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
600aa109 | 3325 | int rcw, int expand) |
e33129d8 | 3326 | { |
584acdd4 | 3327 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks; |
d1688a6d | 3328 | struct r5conf *conf = sh->raid_conf; |
c0f7bddb | 3329 | int level = conf->level; |
e33129d8 DW |
3330 | |
3331 | if (rcw) { | |
1e6d690b SL |
3332 | /* |
3333 | * In some cases, handle_stripe_dirtying initially decided to | |
3334 | * run rmw and allocates extra page for prexor. However, rcw is | |
3335 | * cheaper later on. We need to free the extra page now, | |
3336 | * because we won't be able to do that in ops_complete_prexor(). | |
3337 | */ | |
3338 | r5c_release_extra_page(sh); | |
e33129d8 DW |
3339 | |
3340 | for (i = disks; i--; ) { | |
3341 | struct r5dev *dev = &sh->dev[i]; | |
3342 | ||
39b99586 | 3343 | if (dev->towrite && !delay_towrite(conf, dev, s)) { |
e33129d8 | 3344 | set_bit(R5_LOCKED, &dev->flags); |
d8ee0728 | 3345 | set_bit(R5_Wantdrain, &dev->flags); |
e33129d8 DW |
3346 | if (!expand) |
3347 | clear_bit(R5_UPTODATE, &dev->flags); | |
600aa109 | 3348 | s->locked++; |
1e6d690b SL |
3349 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
3350 | set_bit(R5_LOCKED, &dev->flags); | |
3351 | s->locked++; | |
e33129d8 DW |
3352 | } |
3353 | } | |
ce7d363a N |
3354 | /* if we are not expanding this is a proper write request, and |
3355 | * there will be bios with new data to be drained into the | |
3356 | * stripe cache | |
3357 | */ | |
3358 | if (!expand) { | |
3359 | if (!s->locked) | |
3360 | /* False alarm, nothing to do */ | |
3361 | return; | |
3362 | sh->reconstruct_state = reconstruct_state_drain_run; | |
3363 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); | |
3364 | } else | |
3365 | sh->reconstruct_state = reconstruct_state_run; | |
3366 | ||
3367 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); | |
3368 | ||
c0f7bddb | 3369 | if (s->locked + conf->max_degraded == disks) |
8b3e6cdc | 3370 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
c0f7bddb | 3371 | atomic_inc(&conf->pending_full_writes); |
e33129d8 DW |
3372 | } else { |
3373 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || | |
3374 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); | |
584acdd4 MS |
3375 | BUG_ON(level == 6 && |
3376 | (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || | |
3377 | test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); | |
e33129d8 | 3378 | |
e33129d8 DW |
3379 | for (i = disks; i--; ) { |
3380 | struct r5dev *dev = &sh->dev[i]; | |
584acdd4 | 3381 | if (i == pd_idx || i == qd_idx) |
e33129d8 DW |
3382 | continue; |
3383 | ||
e33129d8 DW |
3384 | if (dev->towrite && |
3385 | (test_bit(R5_UPTODATE, &dev->flags) || | |
d8ee0728 DW |
3386 | test_bit(R5_Wantcompute, &dev->flags))) { |
3387 | set_bit(R5_Wantdrain, &dev->flags); | |
e33129d8 DW |
3388 | set_bit(R5_LOCKED, &dev->flags); |
3389 | clear_bit(R5_UPTODATE, &dev->flags); | |
600aa109 | 3390 | s->locked++; |
1e6d690b SL |
3391 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
3392 | set_bit(R5_LOCKED, &dev->flags); | |
3393 | s->locked++; | |
e33129d8 DW |
3394 | } |
3395 | } | |
ce7d363a N |
3396 | if (!s->locked) |
3397 | /* False alarm - nothing to do */ | |
3398 | return; | |
3399 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; | |
3400 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); | |
3401 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); | |
3402 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); | |
e33129d8 DW |
3403 | } |
3404 | ||
c0f7bddb | 3405 | /* keep the parity disk(s) locked while asynchronous operations |
e33129d8 DW |
3406 | * are in flight |
3407 | */ | |
3408 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); | |
3409 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
600aa109 | 3410 | s->locked++; |
e33129d8 | 3411 | |
c0f7bddb YT |
3412 | if (level == 6) { |
3413 | int qd_idx = sh->qd_idx; | |
3414 | struct r5dev *dev = &sh->dev[qd_idx]; | |
3415 | ||
3416 | set_bit(R5_LOCKED, &dev->flags); | |
3417 | clear_bit(R5_UPTODATE, &dev->flags); | |
3418 | s->locked++; | |
3419 | } | |
3420 | ||
845b9e22 | 3421 | if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page && |
3418d036 AP |
3422 | test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) && |
3423 | !test_bit(STRIPE_FULL_WRITE, &sh->state) && | |
3424 | test_bit(R5_Insync, &sh->dev[pd_idx].flags)) | |
3425 | set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request); | |
3426 | ||
600aa109 | 3427 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
e46b272b | 3428 | __func__, (unsigned long long)sh->sector, |
600aa109 | 3429 | s->locked, s->ops_request); |
e33129d8 | 3430 | } |
16a53ecc | 3431 | |
4ad1d984 LG |
3432 | static bool stripe_bio_overlaps(struct stripe_head *sh, struct bio *bi, |
3433 | int dd_idx, int forwrite) | |
1da177e4 | 3434 | { |
d1688a6d | 3435 | struct r5conf *conf = sh->raid_conf; |
4ad1d984 | 3436 | struct bio **bip; |
1da177e4 | 3437 | |
4ad1d984 LG |
3438 | pr_debug("checking bi b#%llu to stripe s#%llu\n", |
3439 | bi->bi_iter.bi_sector, sh->sector); | |
1da177e4 | 3440 | |
59fc630b | 3441 | /* Don't allow new IO added to stripes in batch list */ |
3442 | if (sh->batch_head) | |
4ad1d984 LG |
3443 | return true; |
3444 | ||
3445 | if (forwrite) | |
1da177e4 | 3446 | bip = &sh->dev[dd_idx].towrite; |
4ad1d984 | 3447 | else |
1da177e4 | 3448 | bip = &sh->dev[dd_idx].toread; |
4ad1d984 | 3449 | |
4f024f37 KO |
3450 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
3451 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) | |
4ad1d984 LG |
3452 | return true; |
3453 | bip = &(*bip)->bi_next; | |
1da177e4 | 3454 | } |
4ad1d984 | 3455 | |
4f024f37 | 3456 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
4ad1d984 | 3457 | return true; |
1da177e4 | 3458 | |
3418d036 AP |
3459 | if (forwrite && raid5_has_ppl(conf)) { |
3460 | /* | |
3461 | * With PPL only writes to consecutive data chunks within a | |
3462 | * stripe are allowed because for a single stripe_head we can | |
3463 | * only have one PPL entry at a time, which describes one data | |
e6a03207 | 3464 | * range. Not really an overlap, but R5_Overlap can be |
3418d036 AP |
3465 | * used to handle this. |
3466 | */ | |
3467 | sector_t sector; | |
3468 | sector_t first = 0; | |
3469 | sector_t last = 0; | |
3470 | int count = 0; | |
3471 | int i; | |
3472 | ||
3473 | for (i = 0; i < sh->disks; i++) { | |
3474 | if (i != sh->pd_idx && | |
3475 | (i == dd_idx || sh->dev[i].towrite)) { | |
3476 | sector = sh->dev[i].sector; | |
3477 | if (count == 0 || sector < first) | |
3478 | first = sector; | |
3479 | if (sector > last) | |
3480 | last = sector; | |
3481 | count++; | |
3482 | } | |
3483 | } | |
3484 | ||
3485 | if (first + conf->chunk_sectors * (count - 1) != last) | |
4ad1d984 LG |
3486 | return true; |
3487 | } | |
3488 | ||
3489 | return false; | |
3490 | } | |
3491 | ||
3492 | static void __add_stripe_bio(struct stripe_head *sh, struct bio *bi, | |
3493 | int dd_idx, int forwrite, int previous) | |
3494 | { | |
3495 | struct r5conf *conf = sh->raid_conf; | |
3496 | struct bio **bip; | |
3497 | int firstwrite = 0; | |
3498 | ||
3499 | if (forwrite) { | |
3500 | bip = &sh->dev[dd_idx].towrite; | |
3501 | if (!*bip) | |
3502 | firstwrite = 1; | |
3503 | } else { | |
3504 | bip = &sh->dev[dd_idx].toread; | |
3418d036 AP |
3505 | } |
3506 | ||
4ad1d984 LG |
3507 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) |
3508 | bip = &(*bip)->bi_next; | |
3509 | ||
da41ba65 | 3510 | if (!forwrite || previous) |
3511 | clear_bit(STRIPE_BATCH_READY, &sh->state); | |
3512 | ||
78bafebd | 3513 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
1da177e4 LT |
3514 | if (*bip) |
3515 | bi->bi_next = *bip; | |
3516 | *bip = bi; | |
016c76ac | 3517 | bio_inc_remaining(bi); |
49728050 | 3518 | md_write_inc(conf->mddev, bi); |
72626685 | 3519 | |
1da177e4 LT |
3520 | if (forwrite) { |
3521 | /* check if page is covered */ | |
3522 | sector_t sector = sh->dev[dd_idx].sector; | |
3523 | for (bi=sh->dev[dd_idx].towrite; | |
c911c46c | 3524 | sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) && |
4f024f37 | 3525 | bi && bi->bi_iter.bi_sector <= sector; |
c911c46c | 3526 | bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) { |
f73a1c7d KO |
3527 | if (bio_end_sector(bi) >= sector) |
3528 | sector = bio_end_sector(bi); | |
1da177e4 | 3529 | } |
c911c46c | 3530 | if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf)) |
7a87f434 | 3531 | if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) |
3532 | sh->overwrite_disks++; | |
1da177e4 | 3533 | } |
cbe47ec5 | 3534 | |
df1b620a LG |
3535 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d, logical %llu\n", |
3536 | (*bip)->bi_iter.bi_sector, sh->sector, dd_idx, | |
3537 | sh->dev[dd_idx].sector); | |
cbe47ec5 | 3538 | |
cd5fc653 YK |
3539 | if (conf->mddev->bitmap && firstwrite && !sh->batch_head) { |
3540 | sh->bm_seq = conf->seq_flush+1; | |
3541 | set_bit(STRIPE_BIT_DELAY, &sh->state); | |
cbe47ec5 | 3542 | } |
4ad1d984 | 3543 | } |
59fc630b | 3544 | |
4ad1d984 LG |
3545 | /* |
3546 | * Each stripe/dev can have one or more bios attached. | |
3547 | * toread/towrite point to the first in a chain. | |
3548 | * The bi_next chain must be in order. | |
3549 | */ | |
3550 | static bool add_stripe_bio(struct stripe_head *sh, struct bio *bi, | |
3551 | int dd_idx, int forwrite, int previous) | |
3552 | { | |
3553 | spin_lock_irq(&sh->stripe_lock); | |
1da177e4 | 3554 | |
4ad1d984 LG |
3555 | if (stripe_bio_overlaps(sh, bi, dd_idx, forwrite)) { |
3556 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); | |
3557 | spin_unlock_irq(&sh->stripe_lock); | |
3558 | return false; | |
3559 | } | |
3560 | ||
3561 | __add_stripe_bio(sh, bi, dd_idx, forwrite, previous); | |
b17459c0 | 3562 | spin_unlock_irq(&sh->stripe_lock); |
4ad1d984 | 3563 | return true; |
1da177e4 LT |
3564 | } |
3565 | ||
d1688a6d | 3566 | static void end_reshape(struct r5conf *conf); |
29269553 | 3567 | |
d1688a6d | 3568 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
911d4ee8 | 3569 | struct stripe_head *sh) |
ccfcc3c1 | 3570 | { |
784052ec | 3571 | int sectors_per_chunk = |
09c9e5fa | 3572 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
911d4ee8 | 3573 | int dd_idx; |
2d2063ce | 3574 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
112bf897 | 3575 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
2d2063ce | 3576 | |
112bf897 N |
3577 | raid5_compute_sector(conf, |
3578 | stripe * (disks - conf->max_degraded) | |
b875e531 | 3579 | *sectors_per_chunk + chunk_offset, |
112bf897 | 3580 | previous, |
911d4ee8 | 3581 | &dd_idx, sh); |
ccfcc3c1 N |
3582 | } |
3583 | ||
a4456856 | 3584 | static void |
d1688a6d | 3585 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
bd83d0a2 | 3586 | struct stripe_head_state *s, int disks) |
a4456856 DW |
3587 | { |
3588 | int i; | |
59fc630b | 3589 | BUG_ON(sh->batch_head); |
a4456856 DW |
3590 | for (i = disks; i--; ) { |
3591 | struct bio *bi; | |
a4456856 DW |
3592 | |
3593 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
ad860670 YK |
3594 | struct md_rdev *rdev = conf->disks[i].rdev; |
3595 | ||
f5b67ae8 N |
3596 | if (rdev && test_bit(In_sync, &rdev->flags) && |
3597 | !test_bit(Faulty, &rdev->flags)) | |
7f0da59b N |
3598 | atomic_inc(&rdev->nr_pending); |
3599 | else | |
3600 | rdev = NULL; | |
7f0da59b N |
3601 | if (rdev) { |
3602 | if (!rdev_set_badblocks( | |
3603 | rdev, | |
3604 | sh->sector, | |
c911c46c | 3605 | RAID5_STRIPE_SECTORS(conf), 0)) |
7f0da59b N |
3606 | md_error(conf->mddev, rdev); |
3607 | rdev_dec_pending(rdev, conf->mddev); | |
3608 | } | |
a4456856 | 3609 | } |
b17459c0 | 3610 | spin_lock_irq(&sh->stripe_lock); |
a4456856 DW |
3611 | /* fail all writes first */ |
3612 | bi = sh->dev[i].towrite; | |
3613 | sh->dev[i].towrite = NULL; | |
7a87f434 | 3614 | sh->overwrite_disks = 0; |
b17459c0 | 3615 | spin_unlock_irq(&sh->stripe_lock); |
a4456856 | 3616 | |
ff875738 | 3617 | log_stripe_write_finished(sh); |
0576b1c6 | 3618 | |
a4456856 | 3619 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
e6a03207 | 3620 | wake_up_bit(&sh->dev[i].flags, R5_Overlap); |
a4456856 | 3621 | |
4f024f37 | 3622 | while (bi && bi->bi_iter.bi_sector < |
c911c46c YY |
3623 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
3624 | struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector); | |
4246a0b6 | 3625 | |
49728050 | 3626 | md_write_end(conf->mddev); |
6308d8e3 | 3627 | bio_io_error(bi); |
a4456856 DW |
3628 | bi = nextbi; |
3629 | } | |
3630 | /* and fail all 'written' */ | |
3631 | bi = sh->dev[i].written; | |
3632 | sh->dev[i].written = NULL; | |
d592a996 SL |
3633 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
3634 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
3635 | sh->dev[i].page = sh->dev[i].orig_page; | |
3636 | } | |
3637 | ||
4f024f37 | 3638 | while (bi && bi->bi_iter.bi_sector < |
c911c46c YY |
3639 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
3640 | struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector); | |
4246a0b6 | 3641 | |
49728050 | 3642 | md_write_end(conf->mddev); |
6308d8e3 | 3643 | bio_io_error(bi); |
a4456856 DW |
3644 | bi = bi2; |
3645 | } | |
3646 | ||
b5e98d65 DW |
3647 | /* fail any reads if this device is non-operational and |
3648 | * the data has not reached the cache yet. | |
3649 | */ | |
3650 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && | |
6e74a9cf | 3651 | s->failed > conf->max_degraded && |
b5e98d65 DW |
3652 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
3653 | test_bit(R5_ReadError, &sh->dev[i].flags))) { | |
143c4d05 | 3654 | spin_lock_irq(&sh->stripe_lock); |
a4456856 DW |
3655 | bi = sh->dev[i].toread; |
3656 | sh->dev[i].toread = NULL; | |
143c4d05 | 3657 | spin_unlock_irq(&sh->stripe_lock); |
a4456856 | 3658 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
e6a03207 | 3659 | wake_up_bit(&sh->dev[i].flags, R5_Overlap); |
ebda780b SL |
3660 | if (bi) |
3661 | s->to_read--; | |
4f024f37 | 3662 | while (bi && bi->bi_iter.bi_sector < |
c911c46c | 3663 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
a4456856 | 3664 | struct bio *nextbi = |
c911c46c | 3665 | r5_next_bio(conf, bi, sh->dev[i].sector); |
4246a0b6 | 3666 | |
6308d8e3 | 3667 | bio_io_error(bi); |
a4456856 DW |
3668 | bi = nextbi; |
3669 | } | |
3670 | } | |
8cfa7b0f N |
3671 | /* If we were in the middle of a write the parity block might |
3672 | * still be locked - so just clear all R5_LOCKED flags | |
3673 | */ | |
3674 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
a4456856 | 3675 | } |
ebda780b SL |
3676 | s->to_write = 0; |
3677 | s->written = 0; | |
a4456856 | 3678 | |
8b3e6cdc DW |
3679 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
3680 | if (atomic_dec_and_test(&conf->pending_full_writes)) | |
3681 | md_wakeup_thread(conf->mddev->thread); | |
a4456856 DW |
3682 | } |
3683 | ||
7f0da59b | 3684 | static void |
d1688a6d | 3685 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
7f0da59b N |
3686 | struct stripe_head_state *s) |
3687 | { | |
3688 | int abort = 0; | |
3689 | int i; | |
3690 | ||
59fc630b | 3691 | BUG_ON(sh->batch_head); |
7f0da59b | 3692 | clear_bit(STRIPE_SYNCING, &sh->state); |
f8dfcffd | 3693 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
e6a03207 | 3694 | wake_up_bit(&sh->dev[sh->pd_idx].flags, R5_Overlap); |
7f0da59b | 3695 | s->syncing = 0; |
9a3e1101 | 3696 | s->replacing = 0; |
7f0da59b | 3697 | /* There is nothing more to do for sync/check/repair. |
18b9837e N |
3698 | * Don't even need to abort as that is handled elsewhere |
3699 | * if needed, and not always wanted e.g. if there is a known | |
3700 | * bad block here. | |
9a3e1101 | 3701 | * For recover/replace we need to record a bad block on all |
7f0da59b N |
3702 | * non-sync devices, or abort the recovery |
3703 | */ | |
18b9837e N |
3704 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
3705 | /* During recovery devices cannot be removed, so | |
3706 | * locking and refcounting of rdevs is not needed | |
3707 | */ | |
3708 | for (i = 0; i < conf->raid_disks; i++) { | |
ad860670 YK |
3709 | struct md_rdev *rdev = conf->disks[i].rdev; |
3710 | ||
18b9837e N |
3711 | if (rdev |
3712 | && !test_bit(Faulty, &rdev->flags) | |
3713 | && !test_bit(In_sync, &rdev->flags) | |
3714 | && !rdev_set_badblocks(rdev, sh->sector, | |
c911c46c | 3715 | RAID5_STRIPE_SECTORS(conf), 0)) |
18b9837e | 3716 | abort = 1; |
ad860670 YK |
3717 | rdev = conf->disks[i].replacement; |
3718 | ||
18b9837e N |
3719 | if (rdev |
3720 | && !test_bit(Faulty, &rdev->flags) | |
3721 | && !test_bit(In_sync, &rdev->flags) | |
3722 | && !rdev_set_badblocks(rdev, sh->sector, | |
c911c46c | 3723 | RAID5_STRIPE_SECTORS(conf), 0)) |
18b9837e N |
3724 | abort = 1; |
3725 | } | |
3726 | if (abort) | |
3727 | conf->recovery_disabled = | |
3728 | conf->mddev->recovery_disabled; | |
7f0da59b | 3729 | } |
c911c46c | 3730 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort); |
7f0da59b N |
3731 | } |
3732 | ||
9a3e1101 N |
3733 | static int want_replace(struct stripe_head *sh, int disk_idx) |
3734 | { | |
3735 | struct md_rdev *rdev; | |
3736 | int rv = 0; | |
3f232d6a | 3737 | |
ad860670 | 3738 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
9a3e1101 N |
3739 | if (rdev |
3740 | && !test_bit(Faulty, &rdev->flags) | |
3741 | && !test_bit(In_sync, &rdev->flags) | |
3742 | && (rdev->recovery_offset <= sh->sector | |
3743 | || rdev->mddev->recovery_cp <= sh->sector)) | |
3744 | rv = 1; | |
9a3e1101 N |
3745 | return rv; |
3746 | } | |
3747 | ||
2c58f06e N |
3748 | static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, |
3749 | int disk_idx, int disks) | |
a4456856 | 3750 | { |
5599becc | 3751 | struct r5dev *dev = &sh->dev[disk_idx]; |
f2b3b44d N |
3752 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
3753 | &sh->dev[s->failed_num[1]] }; | |
ea664c82 | 3754 | int i; |
45a4d8fd | 3755 | bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW); |
5599becc | 3756 | |
a79cfe12 N |
3757 | |
3758 | if (test_bit(R5_LOCKED, &dev->flags) || | |
3759 | test_bit(R5_UPTODATE, &dev->flags)) | |
3760 | /* No point reading this as we already have it or have | |
3761 | * decided to get it. | |
3762 | */ | |
3763 | return 0; | |
3764 | ||
3765 | if (dev->toread || | |
3766 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) | |
3767 | /* We need this block to directly satisfy a request */ | |
3768 | return 1; | |
3769 | ||
3770 | if (s->syncing || s->expanding || | |
3771 | (s->replacing && want_replace(sh, disk_idx))) | |
3772 | /* When syncing, or expanding we read everything. | |
3773 | * When replacing, we need the replaced block. | |
3774 | */ | |
3775 | return 1; | |
3776 | ||
3777 | if ((s->failed >= 1 && fdev[0]->toread) || | |
3778 | (s->failed >= 2 && fdev[1]->toread)) | |
3779 | /* If we want to read from a failed device, then | |
3780 | * we need to actually read every other device. | |
3781 | */ | |
3782 | return 1; | |
3783 | ||
a9d56950 N |
3784 | /* Sometimes neither read-modify-write nor reconstruct-write |
3785 | * cycles can work. In those cases we read every block we | |
3786 | * can. Then the parity-update is certain to have enough to | |
3787 | * work with. | |
3788 | * This can only be a problem when we need to write something, | |
3789 | * and some device has failed. If either of those tests | |
3790 | * fail we need look no further. | |
3791 | */ | |
3792 | if (!s->failed || !s->to_write) | |
3793 | return 0; | |
3794 | ||
3795 | if (test_bit(R5_Insync, &dev->flags) && | |
3796 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
3797 | /* Pre-reads at not permitted until after short delay | |
3798 | * to gather multiple requests. However if this | |
3560741e | 3799 | * device is no Insync, the block could only be computed |
a9d56950 N |
3800 | * and there is no need to delay that. |
3801 | */ | |
3802 | return 0; | |
ea664c82 | 3803 | |
36707bb2 | 3804 | for (i = 0; i < s->failed && i < 2; i++) { |
ea664c82 N |
3805 | if (fdev[i]->towrite && |
3806 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && | |
3807 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) | |
3808 | /* If we have a partial write to a failed | |
3809 | * device, then we will need to reconstruct | |
3810 | * the content of that device, so all other | |
3811 | * devices must be read. | |
3812 | */ | |
3813 | return 1; | |
45a4d8fd CP |
3814 | |
3815 | if (s->failed >= 2 && | |
3816 | (fdev[i]->towrite || | |
3817 | s->failed_num[i] == sh->pd_idx || | |
3818 | s->failed_num[i] == sh->qd_idx) && | |
3819 | !test_bit(R5_UPTODATE, &fdev[i]->flags)) | |
3820 | /* In max degraded raid6, If the failed disk is P, Q, | |
3821 | * or we want to read the failed disk, we need to do | |
3822 | * reconstruct-write. | |
3823 | */ | |
3824 | force_rcw = true; | |
ea664c82 N |
3825 | } |
3826 | ||
45a4d8fd CP |
3827 | /* If we are forced to do a reconstruct-write, because parity |
3828 | * cannot be trusted and we are currently recovering it, there | |
3829 | * is extra need to be careful. | |
ea664c82 N |
3830 | * If one of the devices that we would need to read, because |
3831 | * it is not being overwritten (and maybe not written at all) | |
3832 | * is missing/faulty, then we need to read everything we can. | |
3833 | */ | |
45a4d8fd | 3834 | if (!force_rcw && |
ea664c82 N |
3835 | sh->sector < sh->raid_conf->mddev->recovery_cp) |
3836 | /* reconstruct-write isn't being forced */ | |
3837 | return 0; | |
36707bb2 | 3838 | for (i = 0; i < s->failed && i < 2; i++) { |
10d82c5f N |
3839 | if (s->failed_num[i] != sh->pd_idx && |
3840 | s->failed_num[i] != sh->qd_idx && | |
3841 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && | |
ea664c82 N |
3842 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
3843 | return 1; | |
3844 | } | |
3845 | ||
2c58f06e N |
3846 | return 0; |
3847 | } | |
3848 | ||
ba02684d SL |
3849 | /* fetch_block - checks the given member device to see if its data needs |
3850 | * to be read or computed to satisfy a request. | |
3851 | * | |
3852 | * Returns 1 when no more member devices need to be checked, otherwise returns | |
3853 | * 0 to tell the loop in handle_stripe_fill to continue | |
3854 | */ | |
2c58f06e N |
3855 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
3856 | int disk_idx, int disks) | |
3857 | { | |
3858 | struct r5dev *dev = &sh->dev[disk_idx]; | |
3859 | ||
3860 | /* is the data in this block needed, and can we get it? */ | |
3861 | if (need_this_block(sh, s, disk_idx, disks)) { | |
5599becc YT |
3862 | /* we would like to get this block, possibly by computing it, |
3863 | * otherwise read it if the backing disk is insync | |
3864 | */ | |
3865 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); | |
3866 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); | |
b0c783b3 | 3867 | BUG_ON(sh->batch_head); |
7471fb77 N |
3868 | |
3869 | /* | |
3870 | * In the raid6 case if the only non-uptodate disk is P | |
3871 | * then we already trusted P to compute the other failed | |
3872 | * drives. It is safe to compute rather than re-read P. | |
3873 | * In other cases we only compute blocks from failed | |
3874 | * devices, otherwise check/repair might fail to detect | |
3875 | * a real inconsistency. | |
3876 | */ | |
3877 | ||
5599becc | 3878 | if ((s->uptodate == disks - 1) && |
7471fb77 | 3879 | ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) || |
f2b3b44d | 3880 | (s->failed && (disk_idx == s->failed_num[0] || |
7471fb77 | 3881 | disk_idx == s->failed_num[1])))) { |
5599becc YT |
3882 | /* have disk failed, and we're requested to fetch it; |
3883 | * do compute it | |
a4456856 | 3884 | */ |
5599becc YT |
3885 | pr_debug("Computing stripe %llu block %d\n", |
3886 | (unsigned long long)sh->sector, disk_idx); | |
3887 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
3888 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
3889 | set_bit(R5_Wantcompute, &dev->flags); | |
3890 | sh->ops.target = disk_idx; | |
3891 | sh->ops.target2 = -1; /* no 2nd target */ | |
3892 | s->req_compute = 1; | |
93b3dbce N |
3893 | /* Careful: from this point on 'uptodate' is in the eye |
3894 | * of raid_run_ops which services 'compute' operations | |
3895 | * before writes. R5_Wantcompute flags a block that will | |
3896 | * be R5_UPTODATE by the time it is needed for a | |
3897 | * subsequent operation. | |
3898 | */ | |
5599becc YT |
3899 | s->uptodate++; |
3900 | return 1; | |
3901 | } else if (s->uptodate == disks-2 && s->failed >= 2) { | |
3902 | /* Computing 2-failure is *very* expensive; only | |
3903 | * do it if failed >= 2 | |
3904 | */ | |
3905 | int other; | |
3906 | for (other = disks; other--; ) { | |
3907 | if (other == disk_idx) | |
3908 | continue; | |
3909 | if (!test_bit(R5_UPTODATE, | |
3910 | &sh->dev[other].flags)) | |
3911 | break; | |
a4456856 | 3912 | } |
5599becc YT |
3913 | BUG_ON(other < 0); |
3914 | pr_debug("Computing stripe %llu blocks %d,%d\n", | |
3915 | (unsigned long long)sh->sector, | |
3916 | disk_idx, other); | |
3917 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
3918 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
3919 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); | |
3920 | set_bit(R5_Wantcompute, &sh->dev[other].flags); | |
3921 | sh->ops.target = disk_idx; | |
3922 | sh->ops.target2 = other; | |
3923 | s->uptodate += 2; | |
3924 | s->req_compute = 1; | |
3925 | return 1; | |
3926 | } else if (test_bit(R5_Insync, &dev->flags)) { | |
3927 | set_bit(R5_LOCKED, &dev->flags); | |
3928 | set_bit(R5_Wantread, &dev->flags); | |
3929 | s->locked++; | |
3930 | pr_debug("Reading block %d (sync=%d)\n", | |
3931 | disk_idx, s->syncing); | |
a4456856 DW |
3932 | } |
3933 | } | |
5599becc YT |
3934 | |
3935 | return 0; | |
3936 | } | |
3937 | ||
2aada5b1 | 3938 | /* |
93b3dbce | 3939 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
5599becc | 3940 | */ |
93b3dbce N |
3941 | static void handle_stripe_fill(struct stripe_head *sh, |
3942 | struct stripe_head_state *s, | |
3943 | int disks) | |
5599becc YT |
3944 | { |
3945 | int i; | |
3946 | ||
3947 | /* look for blocks to read/compute, skip this if a compute | |
3948 | * is already in flight, or if the stripe contents are in the | |
3949 | * midst of changing due to a write | |
3950 | */ | |
3951 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && | |
07e83364 SL |
3952 | !sh->reconstruct_state) { |
3953 | ||
3954 | /* | |
3955 | * For degraded stripe with data in journal, do not handle | |
3956 | * read requests yet, instead, flush the stripe to raid | |
3957 | * disks first, this avoids handling complex rmw of write | |
3958 | * back cache (prexor with orig_page, and then xor with | |
3959 | * page) in the read path | |
3960 | */ | |
e2eed85b | 3961 | if (s->to_read && s->injournal && s->failed) { |
07e83364 SL |
3962 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
3963 | r5c_make_stripe_write_out(sh); | |
3964 | goto out; | |
3965 | } | |
3966 | ||
5599becc | 3967 | for (i = disks; i--; ) |
93b3dbce | 3968 | if (fetch_block(sh, s, i, disks)) |
5599becc | 3969 | break; |
07e83364 SL |
3970 | } |
3971 | out: | |
a4456856 DW |
3972 | set_bit(STRIPE_HANDLE, &sh->state); |
3973 | } | |
3974 | ||
787b76fa N |
3975 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
3976 | unsigned long handle_flags); | |
1fe797e6 | 3977 | /* handle_stripe_clean_event |
a4456856 DW |
3978 | * any written block on an uptodate or failed drive can be returned. |
3979 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but | |
3980 | * never LOCKED, so we don't need to test 'failed' directly. | |
3981 | */ | |
d1688a6d | 3982 | static void handle_stripe_clean_event(struct r5conf *conf, |
bd83d0a2 | 3983 | struct stripe_head *sh, int disks) |
a4456856 DW |
3984 | { |
3985 | int i; | |
3986 | struct r5dev *dev; | |
f8dfcffd | 3987 | int discard_pending = 0; |
59fc630b | 3988 | struct stripe_head *head_sh = sh; |
3989 | bool do_endio = false; | |
a4456856 DW |
3990 | |
3991 | for (i = disks; i--; ) | |
3992 | if (sh->dev[i].written) { | |
3993 | dev = &sh->dev[i]; | |
3994 | if (!test_bit(R5_LOCKED, &dev->flags) && | |
9e444768 | 3995 | (test_bit(R5_UPTODATE, &dev->flags) || |
d592a996 SL |
3996 | test_bit(R5_Discard, &dev->flags) || |
3997 | test_bit(R5_SkipCopy, &dev->flags))) { | |
a4456856 DW |
3998 | /* We can return any write requests */ |
3999 | struct bio *wbi, *wbi2; | |
45b4233c | 4000 | pr_debug("Return write for disc %d\n", i); |
ca64cae9 N |
4001 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
4002 | clear_bit(R5_UPTODATE, &dev->flags); | |
d592a996 SL |
4003 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
4004 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); | |
d592a996 | 4005 | } |
59fc630b | 4006 | do_endio = true; |
4007 | ||
4008 | returnbi: | |
4009 | dev->page = dev->orig_page; | |
a4456856 DW |
4010 | wbi = dev->written; |
4011 | dev->written = NULL; | |
4f024f37 | 4012 | while (wbi && wbi->bi_iter.bi_sector < |
c911c46c YY |
4013 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
4014 | wbi2 = r5_next_bio(conf, wbi, dev->sector); | |
49728050 | 4015 | md_write_end(conf->mddev); |
016c76ac | 4016 | bio_endio(wbi); |
a4456856 DW |
4017 | wbi = wbi2; |
4018 | } | |
cd5fc653 | 4019 | |
59fc630b | 4020 | if (head_sh->batch_head) { |
4021 | sh = list_first_entry(&sh->batch_list, | |
4022 | struct stripe_head, | |
4023 | batch_list); | |
4024 | if (sh != head_sh) { | |
4025 | dev = &sh->dev[i]; | |
4026 | goto returnbi; | |
4027 | } | |
4028 | } | |
4029 | sh = head_sh; | |
4030 | dev = &sh->dev[i]; | |
f8dfcffd N |
4031 | } else if (test_bit(R5_Discard, &dev->flags)) |
4032 | discard_pending = 1; | |
4033 | } | |
f6bed0ef | 4034 | |
ff875738 | 4035 | log_stripe_write_finished(sh); |
0576b1c6 | 4036 | |
f8dfcffd N |
4037 | if (!discard_pending && |
4038 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { | |
b8a9d66d | 4039 | int hash; |
f8dfcffd N |
4040 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
4041 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); | |
4042 | if (sh->qd_idx >= 0) { | |
4043 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); | |
4044 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); | |
4045 | } | |
4046 | /* now that discard is done we can proceed with any sync */ | |
4047 | clear_bit(STRIPE_DISCARD, &sh->state); | |
d47648fc SL |
4048 | /* |
4049 | * SCSI discard will change some bio fields and the stripe has | |
4050 | * no updated data, so remove it from hash list and the stripe | |
4051 | * will be reinitialized | |
4052 | */ | |
59fc630b | 4053 | unhash: |
b8a9d66d RG |
4054 | hash = sh->hash_lock_index; |
4055 | spin_lock_irq(conf->hash_locks + hash); | |
d47648fc | 4056 | remove_hash(sh); |
b8a9d66d | 4057 | spin_unlock_irq(conf->hash_locks + hash); |
59fc630b | 4058 | if (head_sh->batch_head) { |
4059 | sh = list_first_entry(&sh->batch_list, | |
4060 | struct stripe_head, batch_list); | |
4061 | if (sh != head_sh) | |
4062 | goto unhash; | |
4063 | } | |
59fc630b | 4064 | sh = head_sh; |
4065 | ||
f8dfcffd N |
4066 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
4067 | set_bit(STRIPE_HANDLE, &sh->state); | |
4068 | ||
4069 | } | |
8b3e6cdc DW |
4070 | |
4071 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) | |
4072 | if (atomic_dec_and_test(&conf->pending_full_writes)) | |
4073 | md_wakeup_thread(conf->mddev->thread); | |
59fc630b | 4074 | |
787b76fa N |
4075 | if (head_sh->batch_head && do_endio) |
4076 | break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); | |
a4456856 DW |
4077 | } |
4078 | ||
86aa1397 SL |
4079 | /* |
4080 | * For RMW in write back cache, we need extra page in prexor to store the | |
4081 | * old data. This page is stored in dev->orig_page. | |
4082 | * | |
4083 | * This function checks whether we have data for prexor. The exact logic | |
4084 | * is: | |
4085 | * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE) | |
4086 | */ | |
4087 | static inline bool uptodate_for_rmw(struct r5dev *dev) | |
4088 | { | |
4089 | return (test_bit(R5_UPTODATE, &dev->flags)) && | |
4090 | (!test_bit(R5_InJournal, &dev->flags) || | |
4091 | test_bit(R5_OrigPageUPTDODATE, &dev->flags)); | |
4092 | } | |
4093 | ||
d7bd398e SL |
4094 | static int handle_stripe_dirtying(struct r5conf *conf, |
4095 | struct stripe_head *sh, | |
4096 | struct stripe_head_state *s, | |
4097 | int disks) | |
a4456856 DW |
4098 | { |
4099 | int rmw = 0, rcw = 0, i; | |
a7854487 AL |
4100 | sector_t recovery_cp = conf->mddev->recovery_cp; |
4101 | ||
584acdd4 | 4102 | /* Check whether resync is now happening or should start. |
a7854487 AL |
4103 | * If yes, then the array is dirty (after unclean shutdown or |
4104 | * initial creation), so parity in some stripes might be inconsistent. | |
4105 | * In this case, we need to always do reconstruct-write, to ensure | |
4106 | * that in case of drive failure or read-error correction, we | |
4107 | * generate correct data from the parity. | |
4108 | */ | |
584acdd4 | 4109 | if (conf->rmw_level == PARITY_DISABLE_RMW || |
26ac1073 N |
4110 | (recovery_cp < MaxSector && sh->sector >= recovery_cp && |
4111 | s->failed == 0)) { | |
a7854487 | 4112 | /* Calculate the real rcw later - for now make it |
c8ac1803 N |
4113 | * look like rcw is cheaper |
4114 | */ | |
4115 | rcw = 1; rmw = 2; | |
584acdd4 MS |
4116 | pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", |
4117 | conf->rmw_level, (unsigned long long)recovery_cp, | |
a7854487 | 4118 | (unsigned long long)sh->sector); |
c8ac1803 | 4119 | } else for (i = disks; i--; ) { |
a4456856 DW |
4120 | /* would I have to read this buffer for read_modify_write */ |
4121 | struct r5dev *dev = &sh->dev[i]; | |
39b99586 | 4122 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
07e83364 | 4123 | i == sh->pd_idx || i == sh->qd_idx || |
1e6d690b | 4124 | test_bit(R5_InJournal, &dev->flags)) && |
a4456856 | 4125 | !test_bit(R5_LOCKED, &dev->flags) && |
86aa1397 | 4126 | !(uptodate_for_rmw(dev) || |
f38e1219 | 4127 | test_bit(R5_Wantcompute, &dev->flags))) { |
a4456856 DW |
4128 | if (test_bit(R5_Insync, &dev->flags)) |
4129 | rmw++; | |
4130 | else | |
4131 | rmw += 2*disks; /* cannot read it */ | |
4132 | } | |
4133 | /* Would I have to read this buffer for reconstruct_write */ | |
584acdd4 MS |
4134 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
4135 | i != sh->pd_idx && i != sh->qd_idx && | |
a4456856 | 4136 | !test_bit(R5_LOCKED, &dev->flags) && |
f38e1219 | 4137 | !(test_bit(R5_UPTODATE, &dev->flags) || |
1e6d690b | 4138 | test_bit(R5_Wantcompute, &dev->flags))) { |
67f45548 N |
4139 | if (test_bit(R5_Insync, &dev->flags)) |
4140 | rcw++; | |
a4456856 DW |
4141 | else |
4142 | rcw += 2*disks; | |
4143 | } | |
4144 | } | |
1e6d690b | 4145 | |
39b99586 SL |
4146 | pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n", |
4147 | (unsigned long long)sh->sector, sh->state, rmw, rcw); | |
a4456856 | 4148 | set_bit(STRIPE_HANDLE, &sh->state); |
41257580 | 4149 | if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) { |
a4456856 | 4150 | /* prefer read-modify-write, but need to get some data */ |
28be4fd3 CH |
4151 | mddev_add_trace_msg(conf->mddev, "raid5 rmw %llu %d", |
4152 | sh->sector, rmw); | |
4153 | ||
a4456856 DW |
4154 | for (i = disks; i--; ) { |
4155 | struct r5dev *dev = &sh->dev[i]; | |
1e6d690b SL |
4156 | if (test_bit(R5_InJournal, &dev->flags) && |
4157 | dev->page == dev->orig_page && | |
4158 | !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { | |
4159 | /* alloc page for prexor */ | |
d7bd398e SL |
4160 | struct page *p = alloc_page(GFP_NOIO); |
4161 | ||
4162 | if (p) { | |
4163 | dev->orig_page = p; | |
4164 | continue; | |
4165 | } | |
4166 | ||
4167 | /* | |
4168 | * alloc_page() failed, try use | |
4169 | * disk_info->extra_page | |
4170 | */ | |
4171 | if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE, | |
4172 | &conf->cache_state)) { | |
4173 | r5c_use_extra_page(sh); | |
4174 | break; | |
4175 | } | |
1e6d690b | 4176 | |
d7bd398e SL |
4177 | /* extra_page in use, add to delayed_list */ |
4178 | set_bit(STRIPE_DELAYED, &sh->state); | |
4179 | s->waiting_extra_page = 1; | |
4180 | return -EAGAIN; | |
1e6d690b | 4181 | } |
d7bd398e | 4182 | } |
1e6d690b | 4183 | |
d7bd398e SL |
4184 | for (i = disks; i--; ) { |
4185 | struct r5dev *dev = &sh->dev[i]; | |
39b99586 | 4186 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
1e6d690b SL |
4187 | i == sh->pd_idx || i == sh->qd_idx || |
4188 | test_bit(R5_InJournal, &dev->flags)) && | |
a4456856 | 4189 | !test_bit(R5_LOCKED, &dev->flags) && |
86aa1397 | 4190 | !(uptodate_for_rmw(dev) || |
1e6d690b | 4191 | test_bit(R5_Wantcompute, &dev->flags)) && |
a4456856 | 4192 | test_bit(R5_Insync, &dev->flags)) { |
67f45548 N |
4193 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
4194 | &sh->state)) { | |
4195 | pr_debug("Read_old block %d for r-m-w\n", | |
4196 | i); | |
a4456856 DW |
4197 | set_bit(R5_LOCKED, &dev->flags); |
4198 | set_bit(R5_Wantread, &dev->flags); | |
4199 | s->locked++; | |
e3914d59 | 4200 | } else |
a4456856 | 4201 | set_bit(STRIPE_DELAYED, &sh->state); |
a4456856 DW |
4202 | } |
4203 | } | |
a9add5d9 | 4204 | } |
41257580 | 4205 | if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) { |
a4456856 | 4206 | /* want reconstruct write, but need to get some data */ |
a9add5d9 | 4207 | int qread =0; |
c8ac1803 | 4208 | rcw = 0; |
a4456856 DW |
4209 | for (i = disks; i--; ) { |
4210 | struct r5dev *dev = &sh->dev[i]; | |
4211 | if (!test_bit(R5_OVERWRITE, &dev->flags) && | |
c8ac1803 | 4212 | i != sh->pd_idx && i != sh->qd_idx && |
a4456856 | 4213 | !test_bit(R5_LOCKED, &dev->flags) && |
f38e1219 | 4214 | !(test_bit(R5_UPTODATE, &dev->flags) || |
c8ac1803 N |
4215 | test_bit(R5_Wantcompute, &dev->flags))) { |
4216 | rcw++; | |
67f45548 N |
4217 | if (test_bit(R5_Insync, &dev->flags) && |
4218 | test_bit(STRIPE_PREREAD_ACTIVE, | |
4219 | &sh->state)) { | |
45b4233c | 4220 | pr_debug("Read_old block " |
a4456856 DW |
4221 | "%d for Reconstruct\n", i); |
4222 | set_bit(R5_LOCKED, &dev->flags); | |
4223 | set_bit(R5_Wantread, &dev->flags); | |
4224 | s->locked++; | |
a9add5d9 | 4225 | qread++; |
e3914d59 | 4226 | } else |
a4456856 | 4227 | set_bit(STRIPE_DELAYED, &sh->state); |
a4456856 DW |
4228 | } |
4229 | } | |
176df894 | 4230 | if (rcw && !mddev_is_dm(conf->mddev)) |
396799eb CH |
4231 | blk_add_trace_msg(conf->mddev->gendisk->queue, |
4232 | "raid5 rcw %llu %d %d %d", | |
4233 | (unsigned long long)sh->sector, rcw, qread, | |
4234 | test_bit(STRIPE_DELAYED, &sh->state)); | |
c8ac1803 | 4235 | } |
b1b02fe9 N |
4236 | |
4237 | if (rcw > disks && rmw > disks && | |
4238 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
4239 | set_bit(STRIPE_DELAYED, &sh->state); | |
4240 | ||
a4456856 DW |
4241 | /* now if nothing is locked, and if we have enough data, |
4242 | * we can start a write request | |
4243 | */ | |
f38e1219 DW |
4244 | /* since handle_stripe can be called at any time we need to handle the |
4245 | * case where a compute block operation has been submitted and then a | |
ac6b53b6 DW |
4246 | * subsequent call wants to start a write request. raid_run_ops only |
4247 | * handles the case where compute block and reconstruct are requested | |
f38e1219 DW |
4248 | * simultaneously. If this is not the case then new writes need to be |
4249 | * held off until the compute completes. | |
4250 | */ | |
976ea8d4 DW |
4251 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
4252 | (s->locked == 0 && (rcw == 0 || rmw == 0) && | |
1e6d690b | 4253 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
c0f7bddb | 4254 | schedule_reconstruction(sh, s, rcw == 0, 0); |
d7bd398e | 4255 | return 0; |
a4456856 DW |
4256 | } |
4257 | ||
d1688a6d | 4258 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
a4456856 DW |
4259 | struct stripe_head_state *s, int disks) |
4260 | { | |
ecc65c9b | 4261 | struct r5dev *dev = NULL; |
bd2ab670 | 4262 | |
59fc630b | 4263 | BUG_ON(sh->batch_head); |
a4456856 | 4264 | set_bit(STRIPE_HANDLE, &sh->state); |
e89f8962 | 4265 | |
ecc65c9b DW |
4266 | switch (sh->check_state) { |
4267 | case check_state_idle: | |
4268 | /* start a new check operation if there are no failures */ | |
bd2ab670 | 4269 | if (s->failed == 0) { |
bd2ab670 | 4270 | BUG_ON(s->uptodate != disks); |
ecc65c9b DW |
4271 | sh->check_state = check_state_run; |
4272 | set_bit(STRIPE_OP_CHECK, &s->ops_request); | |
bd2ab670 | 4273 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
bd2ab670 | 4274 | s->uptodate--; |
ecc65c9b | 4275 | break; |
bd2ab670 | 4276 | } |
f2b3b44d | 4277 | dev = &sh->dev[s->failed_num[0]]; |
df561f66 | 4278 | fallthrough; |
ecc65c9b DW |
4279 | case check_state_compute_result: |
4280 | sh->check_state = check_state_idle; | |
4281 | if (!dev) | |
4282 | dev = &sh->dev[sh->pd_idx]; | |
4283 | ||
4284 | /* check that a write has not made the stripe insync */ | |
4285 | if (test_bit(STRIPE_INSYNC, &sh->state)) | |
4286 | break; | |
c8894419 | 4287 | |
a4456856 | 4288 | /* either failed parity check, or recovery is happening */ |
a4456856 DW |
4289 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
4290 | BUG_ON(s->uptodate != disks); | |
4291 | ||
4292 | set_bit(R5_LOCKED, &dev->flags); | |
ecc65c9b | 4293 | s->locked++; |
a4456856 | 4294 | set_bit(R5_Wantwrite, &dev->flags); |
830ea016 | 4295 | |
a4456856 | 4296 | set_bit(STRIPE_INSYNC, &sh->state); |
ecc65c9b DW |
4297 | break; |
4298 | case check_state_run: | |
4299 | break; /* we will be called again upon completion */ | |
4300 | case check_state_check_result: | |
4301 | sh->check_state = check_state_idle; | |
4302 | ||
4303 | /* if a failure occurred during the check operation, leave | |
4304 | * STRIPE_INSYNC not set and let the stripe be handled again | |
4305 | */ | |
4306 | if (s->failed) | |
4307 | break; | |
4308 | ||
4309 | /* handle a successful check operation, if parity is correct | |
4310 | * we are done. Otherwise update the mismatch count and repair | |
4311 | * parity if !MD_RECOVERY_CHECK | |
4312 | */ | |
ad283ea4 | 4313 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
ecc65c9b DW |
4314 | /* parity is correct (on disc, |
4315 | * not in buffer any more) | |
4316 | */ | |
4317 | set_bit(STRIPE_INSYNC, &sh->state); | |
4318 | else { | |
c911c46c | 4319 | atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); |
e1539036 | 4320 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { |
ecc65c9b DW |
4321 | /* don't try to repair!! */ |
4322 | set_bit(STRIPE_INSYNC, &sh->state); | |
e1539036 N |
4323 | pr_warn_ratelimited("%s: mismatch sector in range " |
4324 | "%llu-%llu\n", mdname(conf->mddev), | |
4325 | (unsigned long long) sh->sector, | |
4326 | (unsigned long long) sh->sector + | |
c911c46c | 4327 | RAID5_STRIPE_SECTORS(conf)); |
e1539036 | 4328 | } else { |
ecc65c9b | 4329 | sh->check_state = check_state_compute_run; |
976ea8d4 | 4330 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
ecc65c9b DW |
4331 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
4332 | set_bit(R5_Wantcompute, | |
4333 | &sh->dev[sh->pd_idx].flags); | |
4334 | sh->ops.target = sh->pd_idx; | |
ac6b53b6 | 4335 | sh->ops.target2 = -1; |
ecc65c9b DW |
4336 | s->uptodate++; |
4337 | } | |
4338 | } | |
4339 | break; | |
4340 | case check_state_compute_run: | |
4341 | break; | |
4342 | default: | |
cc6167b4 | 4343 | pr_err("%s: unknown check_state: %d sector: %llu\n", |
ecc65c9b DW |
4344 | __func__, sh->check_state, |
4345 | (unsigned long long) sh->sector); | |
4346 | BUG(); | |
a4456856 DW |
4347 | } |
4348 | } | |
4349 | ||
d1688a6d | 4350 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
36d1c647 | 4351 | struct stripe_head_state *s, |
f2b3b44d | 4352 | int disks) |
a4456856 | 4353 | { |
a4456856 | 4354 | int pd_idx = sh->pd_idx; |
34e04e87 | 4355 | int qd_idx = sh->qd_idx; |
d82dfee0 | 4356 | struct r5dev *dev; |
a4456856 | 4357 | |
59fc630b | 4358 | BUG_ON(sh->batch_head); |
a4456856 DW |
4359 | set_bit(STRIPE_HANDLE, &sh->state); |
4360 | ||
4361 | BUG_ON(s->failed > 2); | |
d82dfee0 | 4362 | |
a4456856 DW |
4363 | /* Want to check and possibly repair P and Q. |
4364 | * However there could be one 'failed' device, in which | |
4365 | * case we can only check one of them, possibly using the | |
4366 | * other to generate missing data | |
4367 | */ | |
4368 | ||
d82dfee0 DW |
4369 | switch (sh->check_state) { |
4370 | case check_state_idle: | |
4371 | /* start a new check operation if there are < 2 failures */ | |
f2b3b44d | 4372 | if (s->failed == s->q_failed) { |
d82dfee0 | 4373 | /* The only possible failed device holds Q, so it |
a4456856 DW |
4374 | * makes sense to check P (If anything else were failed, |
4375 | * we would have used P to recreate it). | |
4376 | */ | |
d82dfee0 | 4377 | sh->check_state = check_state_run; |
a4456856 | 4378 | } |
f2b3b44d | 4379 | if (!s->q_failed && s->failed < 2) { |
d82dfee0 | 4380 | /* Q is not failed, and we didn't use it to generate |
a4456856 DW |
4381 | * anything, so it makes sense to check it |
4382 | */ | |
d82dfee0 DW |
4383 | if (sh->check_state == check_state_run) |
4384 | sh->check_state = check_state_run_pq; | |
4385 | else | |
4386 | sh->check_state = check_state_run_q; | |
a4456856 | 4387 | } |
a4456856 | 4388 | |
d82dfee0 DW |
4389 | /* discard potentially stale zero_sum_result */ |
4390 | sh->ops.zero_sum_result = 0; | |
a4456856 | 4391 | |
d82dfee0 DW |
4392 | if (sh->check_state == check_state_run) { |
4393 | /* async_xor_zero_sum destroys the contents of P */ | |
4394 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
4395 | s->uptodate--; | |
a4456856 | 4396 | } |
d82dfee0 DW |
4397 | if (sh->check_state >= check_state_run && |
4398 | sh->check_state <= check_state_run_pq) { | |
4399 | /* async_syndrome_zero_sum preserves P and Q, so | |
4400 | * no need to mark them !uptodate here | |
4401 | */ | |
4402 | set_bit(STRIPE_OP_CHECK, &s->ops_request); | |
4403 | break; | |
a4456856 DW |
4404 | } |
4405 | ||
d82dfee0 DW |
4406 | /* we have 2-disk failure */ |
4407 | BUG_ON(s->failed != 2); | |
df561f66 | 4408 | fallthrough; |
d82dfee0 DW |
4409 | case check_state_compute_result: |
4410 | sh->check_state = check_state_idle; | |
a4456856 | 4411 | |
d82dfee0 DW |
4412 | /* check that a write has not made the stripe insync */ |
4413 | if (test_bit(STRIPE_INSYNC, &sh->state)) | |
4414 | break; | |
a4456856 DW |
4415 | |
4416 | /* now write out any block on a failed drive, | |
d82dfee0 | 4417 | * or P or Q if they were recomputed |
a4456856 | 4418 | */ |
b2176a1d | 4419 | dev = NULL; |
a4456856 | 4420 | if (s->failed == 2) { |
f2b3b44d | 4421 | dev = &sh->dev[s->failed_num[1]]; |
a4456856 DW |
4422 | s->locked++; |
4423 | set_bit(R5_LOCKED, &dev->flags); | |
4424 | set_bit(R5_Wantwrite, &dev->flags); | |
4425 | } | |
4426 | if (s->failed >= 1) { | |
f2b3b44d | 4427 | dev = &sh->dev[s->failed_num[0]]; |
a4456856 DW |
4428 | s->locked++; |
4429 | set_bit(R5_LOCKED, &dev->flags); | |
4430 | set_bit(R5_Wantwrite, &dev->flags); | |
4431 | } | |
d82dfee0 | 4432 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
a4456856 DW |
4433 | dev = &sh->dev[pd_idx]; |
4434 | s->locked++; | |
4435 | set_bit(R5_LOCKED, &dev->flags); | |
4436 | set_bit(R5_Wantwrite, &dev->flags); | |
4437 | } | |
d82dfee0 | 4438 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
a4456856 DW |
4439 | dev = &sh->dev[qd_idx]; |
4440 | s->locked++; | |
4441 | set_bit(R5_LOCKED, &dev->flags); | |
4442 | set_bit(R5_Wantwrite, &dev->flags); | |
4443 | } | |
b2176a1d NC |
4444 | if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags), |
4445 | "%s: disk%td not up to date\n", | |
4446 | mdname(conf->mddev), | |
4447 | dev - (struct r5dev *) &sh->dev)) { | |
4448 | clear_bit(R5_LOCKED, &dev->flags); | |
4449 | clear_bit(R5_Wantwrite, &dev->flags); | |
4450 | s->locked--; | |
4451 | } | |
a4456856 DW |
4452 | |
4453 | set_bit(STRIPE_INSYNC, &sh->state); | |
d82dfee0 DW |
4454 | break; |
4455 | case check_state_run: | |
4456 | case check_state_run_q: | |
4457 | case check_state_run_pq: | |
4458 | break; /* we will be called again upon completion */ | |
4459 | case check_state_check_result: | |
4460 | sh->check_state = check_state_idle; | |
4461 | ||
4462 | /* handle a successful check operation, if parity is correct | |
4463 | * we are done. Otherwise update the mismatch count and repair | |
4464 | * parity if !MD_RECOVERY_CHECK | |
4465 | */ | |
4466 | if (sh->ops.zero_sum_result == 0) { | |
a25d8c32 SL |
4467 | /* both parities are correct */ |
4468 | if (!s->failed) | |
4469 | set_bit(STRIPE_INSYNC, &sh->state); | |
4470 | else { | |
4471 | /* in contrast to the raid5 case we can validate | |
4472 | * parity, but still have a failure to write | |
4473 | * back | |
4474 | */ | |
4475 | sh->check_state = check_state_compute_result; | |
4476 | /* Returning at this point means that we may go | |
4477 | * off and bring p and/or q uptodate again so | |
4478 | * we make sure to check zero_sum_result again | |
4479 | * to verify if p or q need writeback | |
4480 | */ | |
4481 | } | |
d82dfee0 | 4482 | } else { |
c911c46c | 4483 | atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); |
e1539036 | 4484 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { |
d82dfee0 DW |
4485 | /* don't try to repair!! */ |
4486 | set_bit(STRIPE_INSYNC, &sh->state); | |
e1539036 N |
4487 | pr_warn_ratelimited("%s: mismatch sector in range " |
4488 | "%llu-%llu\n", mdname(conf->mddev), | |
4489 | (unsigned long long) sh->sector, | |
4490 | (unsigned long long) sh->sector + | |
c911c46c | 4491 | RAID5_STRIPE_SECTORS(conf)); |
e1539036 | 4492 | } else { |
d82dfee0 DW |
4493 | int *target = &sh->ops.target; |
4494 | ||
4495 | sh->ops.target = -1; | |
4496 | sh->ops.target2 = -1; | |
4497 | sh->check_state = check_state_compute_run; | |
4498 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
4499 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
4500 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { | |
4501 | set_bit(R5_Wantcompute, | |
4502 | &sh->dev[pd_idx].flags); | |
4503 | *target = pd_idx; | |
4504 | target = &sh->ops.target2; | |
4505 | s->uptodate++; | |
4506 | } | |
4507 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { | |
4508 | set_bit(R5_Wantcompute, | |
4509 | &sh->dev[qd_idx].flags); | |
4510 | *target = qd_idx; | |
4511 | s->uptodate++; | |
4512 | } | |
4513 | } | |
4514 | } | |
4515 | break; | |
4516 | case check_state_compute_run: | |
4517 | break; | |
4518 | default: | |
cc6167b4 N |
4519 | pr_warn("%s: unknown check_state: %d sector: %llu\n", |
4520 | __func__, sh->check_state, | |
4521 | (unsigned long long) sh->sector); | |
d82dfee0 | 4522 | BUG(); |
a4456856 DW |
4523 | } |
4524 | } | |
4525 | ||
d1688a6d | 4526 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
a4456856 DW |
4527 | { |
4528 | int i; | |
4529 | ||
4530 | /* We have read all the blocks in this stripe and now we need to | |
4531 | * copy some of them into a target stripe for expand. | |
4532 | */ | |
f0a50d37 | 4533 | struct dma_async_tx_descriptor *tx = NULL; |
59fc630b | 4534 | BUG_ON(sh->batch_head); |
a4456856 DW |
4535 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
4536 | for (i = 0; i < sh->disks; i++) | |
34e04e87 | 4537 | if (i != sh->pd_idx && i != sh->qd_idx) { |
911d4ee8 | 4538 | int dd_idx, j; |
a4456856 | 4539 | struct stripe_head *sh2; |
a08abd8c | 4540 | struct async_submit_ctl submit; |
a4456856 | 4541 | |
6d036f7d | 4542 | sector_t bn = raid5_compute_blocknr(sh, i, 1); |
911d4ee8 N |
4543 | sector_t s = raid5_compute_sector(conf, bn, 0, |
4544 | &dd_idx, NULL); | |
2f2d51ef LG |
4545 | sh2 = raid5_get_active_stripe(conf, NULL, s, |
4546 | R5_GAS_NOBLOCK | R5_GAS_NOQUIESCE); | |
a4456856 DW |
4547 | if (sh2 == NULL) |
4548 | /* so far only the early blocks of this stripe | |
4549 | * have been requested. When later blocks | |
4550 | * get requested, we will try again | |
4551 | */ | |
4552 | continue; | |
4553 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || | |
4554 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { | |
4555 | /* must have already done this block */ | |
6d036f7d | 4556 | raid5_release_stripe(sh2); |
a4456856 DW |
4557 | continue; |
4558 | } | |
f0a50d37 DW |
4559 | |
4560 | /* place all the copies on one channel */ | |
a08abd8c | 4561 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
f0a50d37 | 4562 | tx = async_memcpy(sh2->dev[dd_idx].page, |
7aba13b7 YY |
4563 | sh->dev[i].page, sh2->dev[dd_idx].offset, |
4564 | sh->dev[i].offset, RAID5_STRIPE_SIZE(conf), | |
a08abd8c | 4565 | &submit); |
f0a50d37 | 4566 | |
a4456856 DW |
4567 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
4568 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); | |
4569 | for (j = 0; j < conf->raid_disks; j++) | |
4570 | if (j != sh2->pd_idx && | |
86c374ba | 4571 | j != sh2->qd_idx && |
a4456856 DW |
4572 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
4573 | break; | |
4574 | if (j == conf->raid_disks) { | |
4575 | set_bit(STRIPE_EXPAND_READY, &sh2->state); | |
4576 | set_bit(STRIPE_HANDLE, &sh2->state); | |
4577 | } | |
6d036f7d | 4578 | raid5_release_stripe(sh2); |
f0a50d37 | 4579 | |
a4456856 | 4580 | } |
a2e08551 | 4581 | /* done submitting copies, wait for them to complete */ |
749586b7 | 4582 | async_tx_quiesce(&tx); |
a4456856 | 4583 | } |
1da177e4 LT |
4584 | |
4585 | /* | |
4586 | * handle_stripe - do things to a stripe. | |
4587 | * | |
9a3e1101 N |
4588 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
4589 | * state of various bits to see what needs to be done. | |
1da177e4 | 4590 | * Possible results: |
9a3e1101 N |
4591 | * return some read requests which now have data |
4592 | * return some write requests which are safely on storage | |
1da177e4 LT |
4593 | * schedule a read on some buffers |
4594 | * schedule a write of some buffers | |
4595 | * return confirmation of parity correctness | |
4596 | * | |
1da177e4 | 4597 | */ |
a4456856 | 4598 | |
acfe726b | 4599 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
1da177e4 | 4600 | { |
d1688a6d | 4601 | struct r5conf *conf = sh->raid_conf; |
f416885e | 4602 | int disks = sh->disks; |
474af965 N |
4603 | struct r5dev *dev; |
4604 | int i; | |
9a3e1101 | 4605 | int do_recovery = 0; |
1da177e4 | 4606 | |
acfe726b N |
4607 | memset(s, 0, sizeof(*s)); |
4608 | ||
dabc4ec6 | 4609 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; |
4610 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; | |
acfe726b N |
4611 | s->failed_num[0] = -1; |
4612 | s->failed_num[1] = -1; | |
6e74a9cf | 4613 | s->log_failed = r5l_log_disk_error(conf); |
1da177e4 | 4614 | |
acfe726b | 4615 | /* Now to look around and see what can be done */ |
16a53ecc | 4616 | for (i=disks; i--; ) { |
3cb03002 | 4617 | struct md_rdev *rdev; |
31c176ec | 4618 | int is_bad = 0; |
acfe726b | 4619 | |
16a53ecc | 4620 | dev = &sh->dev[i]; |
1da177e4 | 4621 | |
45b4233c | 4622 | pr_debug("check %d: state 0x%lx read %p write %p written %p\n", |
9a3e1101 N |
4623 | i, dev->flags, |
4624 | dev->toread, dev->towrite, dev->written); | |
6c0069c0 YT |
4625 | /* maybe we can reply to a read |
4626 | * | |
4627 | * new wantfill requests are only permitted while | |
4628 | * ops_complete_biofill is guaranteed to be inactive | |
4629 | */ | |
4630 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && | |
4631 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) | |
4632 | set_bit(R5_Wantfill, &dev->flags); | |
1da177e4 | 4633 | |
16a53ecc | 4634 | /* now count some things */ |
cc94015a N |
4635 | if (test_bit(R5_LOCKED, &dev->flags)) |
4636 | s->locked++; | |
4637 | if (test_bit(R5_UPTODATE, &dev->flags)) | |
4638 | s->uptodate++; | |
2d6e4ecc | 4639 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
cc94015a N |
4640 | s->compute++; |
4641 | BUG_ON(s->compute > 2); | |
2d6e4ecc | 4642 | } |
1da177e4 | 4643 | |
acfe726b | 4644 | if (test_bit(R5_Wantfill, &dev->flags)) |
cc94015a | 4645 | s->to_fill++; |
acfe726b | 4646 | else if (dev->toread) |
cc94015a | 4647 | s->to_read++; |
16a53ecc | 4648 | if (dev->towrite) { |
cc94015a | 4649 | s->to_write++; |
16a53ecc | 4650 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
cc94015a | 4651 | s->non_overwrite++; |
16a53ecc | 4652 | } |
a4456856 | 4653 | if (dev->written) |
cc94015a | 4654 | s->written++; |
14a75d3e N |
4655 | /* Prefer to use the replacement for reads, but only |
4656 | * if it is recovered enough and has no bad blocks. | |
4657 | */ | |
ad860670 | 4658 | rdev = conf->disks[i].replacement; |
14a75d3e | 4659 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
c911c46c | 4660 | rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) && |
3a0f007b YK |
4661 | !rdev_has_badblock(rdev, sh->sector, |
4662 | RAID5_STRIPE_SECTORS(conf))) | |
14a75d3e N |
4663 | set_bit(R5_ReadRepl, &dev->flags); |
4664 | else { | |
e6030cb0 | 4665 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
9a3e1101 | 4666 | set_bit(R5_NeedReplace, &dev->flags); |
e6030cb0 N |
4667 | else |
4668 | clear_bit(R5_NeedReplace, &dev->flags); | |
ad860670 | 4669 | rdev = conf->disks[i].rdev; |
14a75d3e N |
4670 | clear_bit(R5_ReadRepl, &dev->flags); |
4671 | } | |
9283d8c5 N |
4672 | if (rdev && test_bit(Faulty, &rdev->flags)) |
4673 | rdev = NULL; | |
31c176ec | 4674 | if (rdev) { |
3a0f007b YK |
4675 | is_bad = rdev_has_badblock(rdev, sh->sector, |
4676 | RAID5_STRIPE_SECTORS(conf)); | |
649bfec6 | 4677 | if (s->blocked_rdev == NULL) { |
31c176ec | 4678 | if (is_bad < 0) |
649bfec6 YK |
4679 | set_bit(BlockedBadBlocks, &rdev->flags); |
4680 | if (rdev_blocked(rdev)) { | |
4681 | s->blocked_rdev = rdev; | |
4682 | atomic_inc(&rdev->nr_pending); | |
4683 | } | |
31c176ec | 4684 | } |
6bfe0b49 | 4685 | } |
415e72d0 N |
4686 | clear_bit(R5_Insync, &dev->flags); |
4687 | if (!rdev) | |
4688 | /* Not in-sync */; | |
31c176ec N |
4689 | else if (is_bad) { |
4690 | /* also not in-sync */ | |
18b9837e N |
4691 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
4692 | test_bit(R5_UPTODATE, &dev->flags)) { | |
31c176ec N |
4693 | /* treat as in-sync, but with a read error |
4694 | * which we can now try to correct | |
4695 | */ | |
4696 | set_bit(R5_Insync, &dev->flags); | |
4697 | set_bit(R5_ReadError, &dev->flags); | |
4698 | } | |
4699 | } else if (test_bit(In_sync, &rdev->flags)) | |
415e72d0 | 4700 | set_bit(R5_Insync, &dev->flags); |
c911c46c | 4701 | else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset) |
415e72d0 | 4702 | /* in sync if before recovery_offset */ |
30d7a483 N |
4703 | set_bit(R5_Insync, &dev->flags); |
4704 | else if (test_bit(R5_UPTODATE, &dev->flags) && | |
4705 | test_bit(R5_Expanded, &dev->flags)) | |
4706 | /* If we've reshaped into here, we assume it is Insync. | |
4707 | * We will shortly update recovery_offset to make | |
4708 | * it official. | |
4709 | */ | |
4710 | set_bit(R5_Insync, &dev->flags); | |
4711 | ||
1cc03eb9 | 4712 | if (test_bit(R5_WriteError, &dev->flags)) { |
14a75d3e N |
4713 | /* This flag does not apply to '.replacement' |
4714 | * only to .rdev, so make sure to check that*/ | |
ad860670 YK |
4715 | struct md_rdev *rdev2 = conf->disks[i].rdev; |
4716 | ||
14a75d3e N |
4717 | if (rdev2 == rdev) |
4718 | clear_bit(R5_Insync, &dev->flags); | |
4719 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { | |
bc2607f3 | 4720 | s->handle_bad_blocks = 1; |
14a75d3e | 4721 | atomic_inc(&rdev2->nr_pending); |
bc2607f3 N |
4722 | } else |
4723 | clear_bit(R5_WriteError, &dev->flags); | |
4724 | } | |
1cc03eb9 | 4725 | if (test_bit(R5_MadeGood, &dev->flags)) { |
14a75d3e N |
4726 | /* This flag does not apply to '.replacement' |
4727 | * only to .rdev, so make sure to check that*/ | |
ad860670 YK |
4728 | struct md_rdev *rdev2 = conf->disks[i].rdev; |
4729 | ||
14a75d3e | 4730 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
b84db560 | 4731 | s->handle_bad_blocks = 1; |
14a75d3e | 4732 | atomic_inc(&rdev2->nr_pending); |
b84db560 N |
4733 | } else |
4734 | clear_bit(R5_MadeGood, &dev->flags); | |
4735 | } | |
977df362 | 4736 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
ad860670 YK |
4737 | struct md_rdev *rdev2 = conf->disks[i].replacement; |
4738 | ||
977df362 N |
4739 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
4740 | s->handle_bad_blocks = 1; | |
4741 | atomic_inc(&rdev2->nr_pending); | |
4742 | } else | |
4743 | clear_bit(R5_MadeGoodRepl, &dev->flags); | |
4744 | } | |
415e72d0 | 4745 | if (!test_bit(R5_Insync, &dev->flags)) { |
16a53ecc N |
4746 | /* The ReadError flag will just be confusing now */ |
4747 | clear_bit(R5_ReadError, &dev->flags); | |
4748 | clear_bit(R5_ReWrite, &dev->flags); | |
1da177e4 | 4749 | } |
415e72d0 N |
4750 | if (test_bit(R5_ReadError, &dev->flags)) |
4751 | clear_bit(R5_Insync, &dev->flags); | |
4752 | if (!test_bit(R5_Insync, &dev->flags)) { | |
cc94015a N |
4753 | if (s->failed < 2) |
4754 | s->failed_num[s->failed] = i; | |
4755 | s->failed++; | |
9a3e1101 N |
4756 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
4757 | do_recovery = 1; | |
d63e2fc8 | 4758 | else if (!rdev) { |
ad860670 | 4759 | rdev = conf->disks[i].replacement; |
d63e2fc8 BC |
4760 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
4761 | do_recovery = 1; | |
4762 | } | |
415e72d0 | 4763 | } |
2ded3703 SL |
4764 | |
4765 | if (test_bit(R5_InJournal, &dev->flags)) | |
4766 | s->injournal++; | |
1e6d690b SL |
4767 | if (test_bit(R5_InJournal, &dev->flags) && dev->written) |
4768 | s->just_cached++; | |
1da177e4 | 4769 | } |
9a3e1101 N |
4770 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
4771 | /* If there is a failed device being replaced, | |
4772 | * we must be recovering. | |
4773 | * else if we are after recovery_cp, we must be syncing | |
c6d2e084 | 4774 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
9a3e1101 N |
4775 | * else we can only be replacing |
4776 | * sync and recovery both need to read all devices, and so | |
4777 | * use the same flag. | |
4778 | */ | |
4779 | if (do_recovery || | |
c6d2e084 | 4780 | sh->sector >= conf->mddev->recovery_cp || |
4781 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) | |
9a3e1101 N |
4782 | s->syncing = 1; |
4783 | else | |
4784 | s->replacing = 1; | |
4785 | } | |
cc94015a N |
4786 | } |
4787 | ||
cb9902db GJ |
4788 | /* |
4789 | * Return '1' if this is a member of batch, or '0' if it is a lone stripe or | |
4790 | * a head which can now be handled. | |
4791 | */ | |
59fc630b | 4792 | static int clear_batch_ready(struct stripe_head *sh) |
4793 | { | |
4794 | struct stripe_head *tmp; | |
4795 | if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) | |
b15a9dbd | 4796 | return (sh->batch_head && sh->batch_head != sh); |
59fc630b | 4797 | spin_lock(&sh->stripe_lock); |
4798 | if (!sh->batch_head) { | |
4799 | spin_unlock(&sh->stripe_lock); | |
4800 | return 0; | |
4801 | } | |
4802 | ||
4803 | /* | |
4804 | * this stripe could be added to a batch list before we check | |
4805 | * BATCH_READY, skips it | |
4806 | */ | |
4807 | if (sh->batch_head != sh) { | |
4808 | spin_unlock(&sh->stripe_lock); | |
4809 | return 1; | |
4810 | } | |
4811 | spin_lock(&sh->batch_lock); | |
4812 | list_for_each_entry(tmp, &sh->batch_list, batch_list) | |
4813 | clear_bit(STRIPE_BATCH_READY, &tmp->state); | |
4814 | spin_unlock(&sh->batch_lock); | |
4815 | spin_unlock(&sh->stripe_lock); | |
4816 | ||
4817 | /* | |
4818 | * BATCH_READY is cleared, no new stripes can be added. | |
4819 | * batch_list can be accessed without lock | |
4820 | */ | |
4821 | return 0; | |
4822 | } | |
4823 | ||
3960ce79 N |
4824 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
4825 | unsigned long handle_flags) | |
72ac7330 | 4826 | { |
4e3d62ff | 4827 | struct stripe_head *sh, *next; |
72ac7330 | 4828 | int i; |
4829 | ||
bb27051f N |
4830 | list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { |
4831 | ||
72ac7330 | 4832 | list_del_init(&sh->batch_list); |
4833 | ||
fb3229d5 | 4834 | WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | |
1b956f7a N |
4835 | (1 << STRIPE_SYNCING) | |
4836 | (1 << STRIPE_REPLACED) | | |
1b956f7a N |
4837 | (1 << STRIPE_DELAYED) | |
4838 | (1 << STRIPE_BIT_DELAY) | | |
4839 | (1 << STRIPE_FULL_WRITE) | | |
4840 | (1 << STRIPE_BIOFILL_RUN) | | |
4841 | (1 << STRIPE_COMPUTE_RUN) | | |
1b956f7a N |
4842 | (1 << STRIPE_DISCARD) | |
4843 | (1 << STRIPE_BATCH_READY) | | |
cd5fc653 | 4844 | (1 << STRIPE_BATCH_ERR)), |
fb3229d5 SL |
4845 | "stripe state: %lx\n", sh->state); |
4846 | WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | | |
4847 | (1 << STRIPE_REPLACED)), | |
4848 | "head stripe state: %lx\n", head_sh->state); | |
1b956f7a N |
4849 | |
4850 | set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | | |
550da24f | 4851 | (1 << STRIPE_PREREAD_ACTIVE) | |
184a09eb | 4852 | (1 << STRIPE_ON_UNPLUG_LIST)), |
1b956f7a N |
4853 | head_sh->state & (1 << STRIPE_INSYNC)); |
4854 | ||
72ac7330 | 4855 | sh->check_state = head_sh->check_state; |
4856 | sh->reconstruct_state = head_sh->reconstruct_state; | |
448ec638 AC |
4857 | spin_lock_irq(&sh->stripe_lock); |
4858 | sh->batch_head = NULL; | |
4859 | spin_unlock_irq(&sh->stripe_lock); | |
fb642b92 N |
4860 | for (i = 0; i < sh->disks; i++) { |
4861 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
e6a03207 | 4862 | wake_up_bit(&sh->dev[i].flags, R5_Overlap); |
72ac7330 | 4863 | sh->dev[i].flags = head_sh->dev[i].flags & |
4864 | (~((1 << R5_WriteError) | (1 << R5_Overlap))); | |
fb642b92 | 4865 | } |
3960ce79 N |
4866 | if (handle_flags == 0 || |
4867 | sh->state & handle_flags) | |
4868 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 4869 | raid5_release_stripe(sh); |
72ac7330 | 4870 | } |
fb642b92 N |
4871 | spin_lock_irq(&head_sh->stripe_lock); |
4872 | head_sh->batch_head = NULL; | |
4873 | spin_unlock_irq(&head_sh->stripe_lock); | |
4874 | for (i = 0; i < head_sh->disks; i++) | |
4875 | if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) | |
e6a03207 | 4876 | wake_up_bit(&head_sh->dev[i].flags, R5_Overlap); |
3960ce79 N |
4877 | if (head_sh->state & handle_flags) |
4878 | set_bit(STRIPE_HANDLE, &head_sh->state); | |
72ac7330 | 4879 | } |
4880 | ||
cc94015a N |
4881 | static void handle_stripe(struct stripe_head *sh) |
4882 | { | |
4883 | struct stripe_head_state s; | |
d1688a6d | 4884 | struct r5conf *conf = sh->raid_conf; |
3687c061 | 4885 | int i; |
84789554 N |
4886 | int prexor; |
4887 | int disks = sh->disks; | |
474af965 | 4888 | struct r5dev *pdev, *qdev; |
cc94015a N |
4889 | |
4890 | clear_bit(STRIPE_HANDLE, &sh->state); | |
a377a472 GJ |
4891 | |
4892 | /* | |
4893 | * handle_stripe should not continue handle the batched stripe, only | |
4894 | * the head of batch list or lone stripe can continue. Otherwise we | |
4895 | * could see break_stripe_batch_list warns about the STRIPE_ACTIVE | |
4896 | * is set for the batched stripe. | |
4897 | */ | |
4898 | if (clear_batch_ready(sh)) | |
4899 | return; | |
4900 | ||
257a4b42 | 4901 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
cc94015a N |
4902 | /* already being handled, ensure it gets handled |
4903 | * again when current action finishes */ | |
4904 | set_bit(STRIPE_HANDLE, &sh->state); | |
4905 | return; | |
4906 | } | |
4907 | ||
4e3d62ff | 4908 | if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) |
3960ce79 | 4909 | break_stripe_batch_list(sh, 0); |
72ac7330 | 4910 | |
dabc4ec6 | 4911 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { |
f8dfcffd | 4912 | spin_lock(&sh->stripe_lock); |
5ddf0440 SL |
4913 | /* |
4914 | * Cannot process 'sync' concurrently with 'discard'. | |
4915 | * Flush data in r5cache before 'sync'. | |
4916 | */ | |
4917 | if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) && | |
4918 | !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) && | |
4919 | !test_bit(STRIPE_DISCARD, &sh->state) && | |
f8dfcffd N |
4920 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
4921 | set_bit(STRIPE_SYNCING, &sh->state); | |
4922 | clear_bit(STRIPE_INSYNC, &sh->state); | |
f94c0b66 | 4923 | clear_bit(STRIPE_REPLACED, &sh->state); |
f8dfcffd N |
4924 | } |
4925 | spin_unlock(&sh->stripe_lock); | |
cc94015a N |
4926 | } |
4927 | clear_bit(STRIPE_DELAYED, &sh->state); | |
4928 | ||
4929 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " | |
4930 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", | |
4931 | (unsigned long long)sh->sector, sh->state, | |
4932 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, | |
4933 | sh->check_state, sh->reconstruct_state); | |
3687c061 | 4934 | |
acfe726b | 4935 | analyse_stripe(sh, &s); |
c5a31000 | 4936 | |
b70abcb2 SL |
4937 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
4938 | goto finish; | |
4939 | ||
16d997b7 N |
4940 | if (s.handle_bad_blocks || |
4941 | test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) { | |
bc2607f3 N |
4942 | set_bit(STRIPE_HANDLE, &sh->state); |
4943 | goto finish; | |
4944 | } | |
4945 | ||
474af965 N |
4946 | if (unlikely(s.blocked_rdev)) { |
4947 | if (s.syncing || s.expanding || s.expanded || | |
9a3e1101 | 4948 | s.replacing || s.to_write || s.written) { |
474af965 N |
4949 | set_bit(STRIPE_HANDLE, &sh->state); |
4950 | goto finish; | |
4951 | } | |
4952 | /* There is nothing for the blocked_rdev to block */ | |
4953 | rdev_dec_pending(s.blocked_rdev, conf->mddev); | |
4954 | s.blocked_rdev = NULL; | |
4955 | } | |
4956 | ||
4957 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { | |
4958 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); | |
4959 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); | |
4960 | } | |
4961 | ||
4962 | pr_debug("locked=%d uptodate=%d to_read=%d" | |
4963 | " to_write=%d failed=%d failed_num=%d,%d\n", | |
4964 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, | |
4965 | s.failed_num[0], s.failed_num[1]); | |
70d466f7 SL |
4966 | /* |
4967 | * check if the array has lost more than max_degraded devices and, | |
474af965 | 4968 | * if so, some requests might need to be failed. |
70d466f7 SL |
4969 | * |
4970 | * When journal device failed (log_failed), we will only process | |
4971 | * the stripe if there is data need write to raid disks | |
474af965 | 4972 | */ |
70d466f7 SL |
4973 | if (s.failed > conf->max_degraded || |
4974 | (s.log_failed && s.injournal == 0)) { | |
9a3f530f N |
4975 | sh->check_state = 0; |
4976 | sh->reconstruct_state = 0; | |
626f2092 | 4977 | break_stripe_batch_list(sh, 0); |
9a3f530f | 4978 | if (s.to_read+s.to_write+s.written) |
bd83d0a2 | 4979 | handle_failed_stripe(conf, sh, &s, disks); |
9a3e1101 | 4980 | if (s.syncing + s.replacing) |
9a3f530f N |
4981 | handle_failed_sync(conf, sh, &s); |
4982 | } | |
474af965 | 4983 | |
84789554 N |
4984 | /* Now we check to see if any write operations have recently |
4985 | * completed | |
4986 | */ | |
4987 | prexor = 0; | |
4988 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) | |
4989 | prexor = 1; | |
4990 | if (sh->reconstruct_state == reconstruct_state_drain_result || | |
4991 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { | |
4992 | sh->reconstruct_state = reconstruct_state_idle; | |
4993 | ||
4994 | /* All the 'written' buffers and the parity block are ready to | |
4995 | * be written back to disk | |
4996 | */ | |
9e444768 SL |
4997 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
4998 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); | |
84789554 | 4999 | BUG_ON(sh->qd_idx >= 0 && |
9e444768 SL |
5000 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
5001 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); | |
84789554 N |
5002 | for (i = disks; i--; ) { |
5003 | struct r5dev *dev = &sh->dev[i]; | |
5004 | if (test_bit(R5_LOCKED, &dev->flags) && | |
5005 | (i == sh->pd_idx || i == sh->qd_idx || | |
1e6d690b SL |
5006 | dev->written || test_bit(R5_InJournal, |
5007 | &dev->flags))) { | |
84789554 N |
5008 | pr_debug("Writing block %d\n", i); |
5009 | set_bit(R5_Wantwrite, &dev->flags); | |
5010 | if (prexor) | |
5011 | continue; | |
9c4bdf69 N |
5012 | if (s.failed > 1) |
5013 | continue; | |
84789554 N |
5014 | if (!test_bit(R5_Insync, &dev->flags) || |
5015 | ((i == sh->pd_idx || i == sh->qd_idx) && | |
5016 | s.failed == 0)) | |
5017 | set_bit(STRIPE_INSYNC, &sh->state); | |
5018 | } | |
5019 | } | |
5020 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
5021 | s.dec_preread_active = 1; | |
5022 | } | |
5023 | ||
ef5b7c69 N |
5024 | /* |
5025 | * might be able to return some write requests if the parity blocks | |
5026 | * are safe, or on a failed drive | |
5027 | */ | |
5028 | pdev = &sh->dev[sh->pd_idx]; | |
5029 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) | |
5030 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); | |
5031 | qdev = &sh->dev[sh->qd_idx]; | |
5032 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) | |
5033 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) | |
5034 | || conf->level < 6; | |
5035 | ||
5036 | if (s.written && | |
5037 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) | |
5038 | && !test_bit(R5_LOCKED, &pdev->flags) | |
5039 | && (test_bit(R5_UPTODATE, &pdev->flags) || | |
5040 | test_bit(R5_Discard, &pdev->flags))))) && | |
5041 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) | |
5042 | && !test_bit(R5_LOCKED, &qdev->flags) | |
5043 | && (test_bit(R5_UPTODATE, &qdev->flags) || | |
5044 | test_bit(R5_Discard, &qdev->flags)))))) | |
bd83d0a2 | 5045 | handle_stripe_clean_event(conf, sh, disks); |
ef5b7c69 | 5046 | |
1e6d690b | 5047 | if (s.just_cached) |
bd83d0a2 | 5048 | r5c_handle_cached_data_endio(conf, sh, disks); |
ff875738 | 5049 | log_stripe_write_finished(sh); |
1e6d690b | 5050 | |
ef5b7c69 N |
5051 | /* Now we might consider reading some blocks, either to check/generate |
5052 | * parity, or to satisfy requests | |
5053 | * or to load a block that is being partially written. | |
5054 | */ | |
5055 | if (s.to_read || s.non_overwrite | |
a1c6ae3d | 5056 | || (s.to_write && s.failed) |
ef5b7c69 N |
5057 | || (s.syncing && (s.uptodate + s.compute < disks)) |
5058 | || s.replacing | |
5059 | || s.expanding) | |
5060 | handle_stripe_fill(sh, &s, disks); | |
5061 | ||
2ded3703 SL |
5062 | /* |
5063 | * When the stripe finishes full journal write cycle (write to journal | |
5064 | * and raid disk), this is the clean up procedure so it is ready for | |
5065 | * next operation. | |
5066 | */ | |
5067 | r5c_finish_stripe_write_out(conf, sh, &s); | |
5068 | ||
5069 | /* | |
5070 | * Now to consider new write requests, cache write back and what else, | |
5071 | * if anything should be read. We do not handle new writes when: | |
84789554 N |
5072 | * 1/ A 'write' operation (copy+xor) is already in flight. |
5073 | * 2/ A 'check' operation is in flight, as it may clobber the parity | |
5074 | * block. | |
2ded3703 | 5075 | * 3/ A r5c cache log write is in flight. |
84789554 | 5076 | */ |
2ded3703 SL |
5077 | |
5078 | if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { | |
5079 | if (!r5c_is_writeback(conf->log)) { | |
5080 | if (s.to_write) | |
5081 | handle_stripe_dirtying(conf, sh, &s, disks); | |
5082 | } else { /* write back cache */ | |
5083 | int ret = 0; | |
5084 | ||
5085 | /* First, try handle writes in caching phase */ | |
5086 | if (s.to_write) | |
5087 | ret = r5c_try_caching_write(conf, sh, &s, | |
5088 | disks); | |
5089 | /* | |
5090 | * If caching phase failed: ret == -EAGAIN | |
5091 | * OR | |
5092 | * stripe under reclaim: !caching && injournal | |
5093 | * | |
5094 | * fall back to handle_stripe_dirtying() | |
5095 | */ | |
5096 | if (ret == -EAGAIN || | |
5097 | /* stripe under reclaim: !caching && injournal */ | |
5098 | (!test_bit(STRIPE_R5C_CACHING, &sh->state) && | |
d7bd398e SL |
5099 | s.injournal > 0)) { |
5100 | ret = handle_stripe_dirtying(conf, sh, &s, | |
5101 | disks); | |
5102 | if (ret == -EAGAIN) | |
5103 | goto finish; | |
5104 | } | |
2ded3703 SL |
5105 | } |
5106 | } | |
84789554 N |
5107 | |
5108 | /* maybe we need to check and possibly fix the parity for this stripe | |
5109 | * Any reads will already have been scheduled, so we just see if enough | |
5110 | * data is available. The parity check is held off while parity | |
5111 | * dependent operations are in flight. | |
5112 | */ | |
5113 | if (sh->check_state || | |
5114 | (s.syncing && s.locked == 0 && | |
5115 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && | |
5116 | !test_bit(STRIPE_INSYNC, &sh->state))) { | |
5117 | if (conf->level == 6) | |
5118 | handle_parity_checks6(conf, sh, &s, disks); | |
5119 | else | |
5120 | handle_parity_checks5(conf, sh, &s, disks); | |
5121 | } | |
c5a31000 | 5122 | |
f94c0b66 N |
5123 | if ((s.replacing || s.syncing) && s.locked == 0 |
5124 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) | |
5125 | && !test_bit(STRIPE_REPLACED, &sh->state)) { | |
9a3e1101 N |
5126 | /* Write out to replacement devices where possible */ |
5127 | for (i = 0; i < conf->raid_disks; i++) | |
f94c0b66 N |
5128 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
5129 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
9a3e1101 N |
5130 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
5131 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
5132 | s.locked++; | |
5133 | } | |
f94c0b66 N |
5134 | if (s.replacing) |
5135 | set_bit(STRIPE_INSYNC, &sh->state); | |
5136 | set_bit(STRIPE_REPLACED, &sh->state); | |
9a3e1101 N |
5137 | } |
5138 | if ((s.syncing || s.replacing) && s.locked == 0 && | |
f94c0b66 | 5139 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
9a3e1101 | 5140 | test_bit(STRIPE_INSYNC, &sh->state)) { |
c911c46c | 5141 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); |
c5a31000 | 5142 | clear_bit(STRIPE_SYNCING, &sh->state); |
f8dfcffd | 5143 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
e6a03207 | 5144 | wake_up_bit(&sh->dev[sh->pd_idx].flags, R5_Overlap); |
c5a31000 N |
5145 | } |
5146 | ||
5147 | /* If the failed drives are just a ReadError, then we might need | |
5148 | * to progress the repair/check process | |
5149 | */ | |
5150 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) | |
5151 | for (i = 0; i < s.failed; i++) { | |
5152 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; | |
5153 | if (test_bit(R5_ReadError, &dev->flags) | |
5154 | && !test_bit(R5_LOCKED, &dev->flags) | |
5155 | && test_bit(R5_UPTODATE, &dev->flags) | |
5156 | ) { | |
5157 | if (!test_bit(R5_ReWrite, &dev->flags)) { | |
5158 | set_bit(R5_Wantwrite, &dev->flags); | |
5159 | set_bit(R5_ReWrite, &dev->flags); | |
3a31cf3d | 5160 | } else |
c5a31000 N |
5161 | /* let's read it back */ |
5162 | set_bit(R5_Wantread, &dev->flags); | |
3a31cf3d GJ |
5163 | set_bit(R5_LOCKED, &dev->flags); |
5164 | s.locked++; | |
c5a31000 N |
5165 | } |
5166 | } | |
5167 | ||
3687c061 N |
5168 | /* Finish reconstruct operations initiated by the expansion process */ |
5169 | if (sh->reconstruct_state == reconstruct_state_result) { | |
5170 | struct stripe_head *sh_src | |
2f2d51ef LG |
5171 | = raid5_get_active_stripe(conf, NULL, sh->sector, |
5172 | R5_GAS_PREVIOUS | R5_GAS_NOBLOCK | | |
5173 | R5_GAS_NOQUIESCE); | |
3687c061 N |
5174 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
5175 | /* sh cannot be written until sh_src has been read. | |
5176 | * so arrange for sh to be delayed a little | |
5177 | */ | |
5178 | set_bit(STRIPE_DELAYED, &sh->state); | |
5179 | set_bit(STRIPE_HANDLE, &sh->state); | |
5180 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, | |
5181 | &sh_src->state)) | |
5182 | atomic_inc(&conf->preread_active_stripes); | |
6d036f7d | 5183 | raid5_release_stripe(sh_src); |
3687c061 N |
5184 | goto finish; |
5185 | } | |
5186 | if (sh_src) | |
6d036f7d | 5187 | raid5_release_stripe(sh_src); |
3687c061 N |
5188 | |
5189 | sh->reconstruct_state = reconstruct_state_idle; | |
5190 | clear_bit(STRIPE_EXPANDING, &sh->state); | |
5191 | for (i = conf->raid_disks; i--; ) { | |
5192 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
5193 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
5194 | s.locked++; | |
5195 | } | |
5196 | } | |
f416885e | 5197 | |
3687c061 N |
5198 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
5199 | !sh->reconstruct_state) { | |
5200 | /* Need to write out all blocks after computing parity */ | |
5201 | sh->disks = conf->raid_disks; | |
5202 | stripe_set_idx(sh->sector, conf, 0, sh); | |
5203 | schedule_reconstruction(sh, &s, 1, 1); | |
5204 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { | |
5205 | clear_bit(STRIPE_EXPAND_READY, &sh->state); | |
5206 | atomic_dec(&conf->reshape_stripes); | |
6f039cc4 | 5207 | wake_up(&conf->wait_for_reshape); |
c911c46c | 5208 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); |
3687c061 N |
5209 | } |
5210 | ||
5211 | if (s.expanding && s.locked == 0 && | |
5212 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) | |
5213 | handle_stripe_expansion(conf, sh); | |
16a53ecc | 5214 | |
3687c061 | 5215 | finish: |
6bfe0b49 | 5216 | /* wait for this device to become unblocked */ |
5f066c63 N |
5217 | if (unlikely(s.blocked_rdev)) { |
5218 | if (conf->mddev->external) | |
5219 | md_wait_for_blocked_rdev(s.blocked_rdev, | |
5220 | conf->mddev); | |
5221 | else | |
5222 | /* Internal metadata will immediately | |
5223 | * be written by raid5d, so we don't | |
5224 | * need to wait here. | |
5225 | */ | |
5226 | rdev_dec_pending(s.blocked_rdev, | |
5227 | conf->mddev); | |
5228 | } | |
6bfe0b49 | 5229 | |
bc2607f3 N |
5230 | if (s.handle_bad_blocks) |
5231 | for (i = disks; i--; ) { | |
3cb03002 | 5232 | struct md_rdev *rdev; |
bc2607f3 N |
5233 | struct r5dev *dev = &sh->dev[i]; |
5234 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { | |
5235 | /* We own a safe reference to the rdev */ | |
ad860670 | 5236 | rdev = conf->disks[i].rdev; |
bc2607f3 | 5237 | if (!rdev_set_badblocks(rdev, sh->sector, |
c911c46c | 5238 | RAID5_STRIPE_SECTORS(conf), 0)) |
bc2607f3 N |
5239 | md_error(conf->mddev, rdev); |
5240 | rdev_dec_pending(rdev, conf->mddev); | |
5241 | } | |
b84db560 | 5242 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
ad860670 | 5243 | rdev = conf->disks[i].rdev; |
b84db560 | 5244 | rdev_clear_badblocks(rdev, sh->sector, |
c911c46c | 5245 | RAID5_STRIPE_SECTORS(conf), 0); |
b84db560 N |
5246 | rdev_dec_pending(rdev, conf->mddev); |
5247 | } | |
977df362 | 5248 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
ad860670 | 5249 | rdev = conf->disks[i].replacement; |
dd054fce N |
5250 | if (!rdev) |
5251 | /* rdev have been moved down */ | |
ad860670 | 5252 | rdev = conf->disks[i].rdev; |
977df362 | 5253 | rdev_clear_badblocks(rdev, sh->sector, |
c911c46c | 5254 | RAID5_STRIPE_SECTORS(conf), 0); |
977df362 N |
5255 | rdev_dec_pending(rdev, conf->mddev); |
5256 | } | |
bc2607f3 N |
5257 | } |
5258 | ||
6c0069c0 YT |
5259 | if (s.ops_request) |
5260 | raid_run_ops(sh, s.ops_request); | |
5261 | ||
f0e43bcd | 5262 | ops_run_io(sh, &s); |
16a53ecc | 5263 | |
c5709ef6 | 5264 | if (s.dec_preread_active) { |
729a1866 | 5265 | /* We delay this until after ops_run_io so that if make_request |
e9c7469b | 5266 | * is waiting on a flush, it won't continue until the writes |
729a1866 N |
5267 | * have actually been submitted. |
5268 | */ | |
5269 | atomic_dec(&conf->preread_active_stripes); | |
5270 | if (atomic_read(&conf->preread_active_stripes) < | |
5271 | IO_THRESHOLD) | |
5272 | md_wakeup_thread(conf->mddev->thread); | |
5273 | } | |
5274 | ||
257a4b42 | 5275 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
16a53ecc N |
5276 | } |
5277 | ||
d1688a6d | 5278 | static void raid5_activate_delayed(struct r5conf *conf) |
4631f39f | 5279 | __must_hold(&conf->device_lock) |
16a53ecc N |
5280 | { |
5281 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { | |
5282 | while (!list_empty(&conf->delayed_list)) { | |
5283 | struct list_head *l = conf->delayed_list.next; | |
5284 | struct stripe_head *sh; | |
5285 | sh = list_entry(l, struct stripe_head, lru); | |
5286 | list_del_init(l); | |
5287 | clear_bit(STRIPE_DELAYED, &sh->state); | |
5288 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
5289 | atomic_inc(&conf->preread_active_stripes); | |
8b3e6cdc | 5290 | list_add_tail(&sh->lru, &conf->hold_list); |
851c30c9 | 5291 | raid5_wakeup_stripe_thread(sh); |
16a53ecc | 5292 | } |
482c0834 | 5293 | } |
16a53ecc N |
5294 | } |
5295 | ||
566c09c5 | 5296 | static void activate_bit_delay(struct r5conf *conf, |
4631f39f LG |
5297 | struct list_head *temp_inactive_list) |
5298 | __must_hold(&conf->device_lock) | |
16a53ecc | 5299 | { |
16a53ecc N |
5300 | struct list_head head; |
5301 | list_add(&head, &conf->bitmap_list); | |
5302 | list_del_init(&conf->bitmap_list); | |
5303 | while (!list_empty(&head)) { | |
5304 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); | |
566c09c5 | 5305 | int hash; |
16a53ecc N |
5306 | list_del_init(&sh->lru); |
5307 | atomic_inc(&sh->count); | |
566c09c5 SL |
5308 | hash = sh->hash_lock_index; |
5309 | __release_stripe(conf, sh, &temp_inactive_list[hash]); | |
16a53ecc N |
5310 | } |
5311 | } | |
5312 | ||
fd01b88c | 5313 | static int in_chunk_boundary(struct mddev *mddev, struct bio *bio) |
f679623f | 5314 | { |
3cb5edf4 | 5315 | struct r5conf *conf = mddev->private; |
10433d04 | 5316 | sector_t sector = bio->bi_iter.bi_sector; |
3cb5edf4 | 5317 | unsigned int chunk_sectors; |
aa8b57aa | 5318 | unsigned int bio_sectors = bio_sectors(bio); |
f679623f | 5319 | |
3cb5edf4 | 5320 | chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); |
f679623f RBJ |
5321 | return chunk_sectors >= |
5322 | ((sector & (chunk_sectors - 1)) + bio_sectors); | |
5323 | } | |
5324 | ||
46031f9a RBJ |
5325 | /* |
5326 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) | |
5327 | * later sampled by raid5d. | |
5328 | */ | |
d1688a6d | 5329 | static void add_bio_to_retry(struct bio *bi,struct r5conf *conf) |
46031f9a RBJ |
5330 | { |
5331 | unsigned long flags; | |
5332 | ||
5333 | spin_lock_irqsave(&conf->device_lock, flags); | |
5334 | ||
5335 | bi->bi_next = conf->retry_read_aligned_list; | |
5336 | conf->retry_read_aligned_list = bi; | |
5337 | ||
5338 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
5339 | md_wakeup_thread(conf->mddev->thread); | |
5340 | } | |
5341 | ||
0472a42b N |
5342 | static struct bio *remove_bio_from_retry(struct r5conf *conf, |
5343 | unsigned int *offset) | |
46031f9a RBJ |
5344 | { |
5345 | struct bio *bi; | |
5346 | ||
5347 | bi = conf->retry_read_aligned; | |
5348 | if (bi) { | |
0472a42b | 5349 | *offset = conf->retry_read_offset; |
46031f9a RBJ |
5350 | conf->retry_read_aligned = NULL; |
5351 | return bi; | |
5352 | } | |
5353 | bi = conf->retry_read_aligned_list; | |
5354 | if(bi) { | |
387bb173 | 5355 | conf->retry_read_aligned_list = bi->bi_next; |
46031f9a | 5356 | bi->bi_next = NULL; |
0472a42b | 5357 | *offset = 0; |
46031f9a RBJ |
5358 | } |
5359 | ||
5360 | return bi; | |
5361 | } | |
5362 | ||
f679623f RBJ |
5363 | /* |
5364 | * The "raid5_align_endio" should check if the read succeeded and if it | |
5365 | * did, call bio_endio on the original bio (having bio_put the new bio | |
5366 | * first). | |
5367 | * If the read failed.. | |
5368 | */ | |
4246a0b6 | 5369 | static void raid5_align_endio(struct bio *bi) |
f679623f | 5370 | { |
05048cbc YK |
5371 | struct bio *raid_bi = bi->bi_private; |
5372 | struct md_rdev *rdev = (void *)raid_bi->bi_next; | |
5373 | struct mddev *mddev = rdev->mddev; | |
5374 | struct r5conf *conf = mddev->private; | |
4e4cbee9 | 5375 | blk_status_t error = bi->bi_status; |
46031f9a | 5376 | |
f679623f | 5377 | bio_put(bi); |
46031f9a | 5378 | raid_bi->bi_next = NULL; |
46031f9a RBJ |
5379 | rdev_dec_pending(rdev, conf->mddev); |
5380 | ||
9b81c842 | 5381 | if (!error) { |
4246a0b6 | 5382 | bio_endio(raid_bi); |
46031f9a | 5383 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
b1b46486 | 5384 | wake_up(&conf->wait_for_quiescent); |
6712ecf8 | 5385 | return; |
46031f9a RBJ |
5386 | } |
5387 | ||
45b4233c | 5388 | pr_debug("raid5_align_endio : io error...handing IO for a retry\n"); |
46031f9a RBJ |
5389 | |
5390 | add_bio_to_retry(raid_bi, conf); | |
f679623f RBJ |
5391 | } |
5392 | ||
7ef6b12a | 5393 | static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio) |
f679623f | 5394 | { |
d1688a6d | 5395 | struct r5conf *conf = mddev->private; |
e82ed3a4 | 5396 | struct bio *align_bio; |
3cb03002 | 5397 | struct md_rdev *rdev; |
3a0f007b YK |
5398 | sector_t sector, end_sector; |
5399 | int dd_idx; | |
97ae2725 | 5400 | bool did_inc; |
f679623f RBJ |
5401 | |
5402 | if (!in_chunk_boundary(mddev, raid_bio)) { | |
7ef6b12a | 5403 | pr_debug("%s: non aligned\n", __func__); |
f679623f RBJ |
5404 | return 0; |
5405 | } | |
f679623f | 5406 | |
e82ed3a4 CH |
5407 | sector = raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, 0, |
5408 | &dd_idx, NULL); | |
8557dc27 | 5409 | end_sector = sector + bio_sectors(raid_bio); |
e82ed3a4 | 5410 | |
e82ed3a4 | 5411 | if (r5c_big_stripe_cached(conf, sector)) |
ad860670 | 5412 | return 0; |
e82ed3a4 | 5413 | |
ad860670 | 5414 | rdev = conf->disks[dd_idx].replacement; |
671488cc N |
5415 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
5416 | rdev->recovery_offset < end_sector) { | |
ad860670 | 5417 | rdev = conf->disks[dd_idx].rdev; |
e82ed3a4 | 5418 | if (!rdev) |
ad860670 | 5419 | return 0; |
e82ed3a4 | 5420 | if (test_bit(Faulty, &rdev->flags) || |
671488cc | 5421 | !(test_bit(In_sync, &rdev->flags) || |
e82ed3a4 | 5422 | rdev->recovery_offset >= end_sector)) |
ad860670 | 5423 | return 0; |
671488cc | 5424 | } |
03b047f4 | 5425 | |
e82ed3a4 | 5426 | atomic_inc(&rdev->nr_pending); |
e82ed3a4 | 5427 | |
3a0f007b | 5428 | if (rdev_has_badblock(rdev, sector, bio_sectors(raid_bio))) { |
e82ed3a4 | 5429 | rdev_dec_pending(rdev, mddev); |
03b047f4 SL |
5430 | return 0; |
5431 | } | |
5432 | ||
05048cbc | 5433 | md_account_bio(mddev, &raid_bio); |
1147f58e | 5434 | raid_bio->bi_next = (void *)rdev; |
1147f58e | 5435 | |
05048cbc YK |
5436 | align_bio = bio_alloc_clone(rdev->bdev, raid_bio, GFP_NOIO, |
5437 | &mddev->bio_set); | |
e82ed3a4 | 5438 | align_bio->bi_end_io = raid5_align_endio; |
05048cbc | 5439 | align_bio->bi_private = raid_bio; |
e82ed3a4 CH |
5440 | align_bio->bi_iter.bi_sector = sector; |
5441 | ||
e82ed3a4 CH |
5442 | /* No reshape active, so we can trust rdev->data_offset */ |
5443 | align_bio->bi_iter.bi_sector += rdev->data_offset; | |
31c176ec | 5444 | |
97ae2725 GO |
5445 | did_inc = false; |
5446 | if (conf->quiesce == 0) { | |
5447 | atomic_inc(&conf->active_aligned_reads); | |
5448 | did_inc = true; | |
5449 | } | |
5450 | /* need a memory barrier to detect the race with raid5_quiesce() */ | |
5451 | if (!did_inc || smp_load_acquire(&conf->quiesce) != 0) { | |
5452 | /* quiesce is in progress, so we need to undo io activation and wait | |
5453 | * for it to finish | |
5454 | */ | |
5455 | if (did_inc && atomic_dec_and_test(&conf->active_aligned_reads)) | |
5456 | wake_up(&conf->wait_for_quiescent); | |
5457 | spin_lock_irq(&conf->device_lock); | |
5458 | wait_event_lock_irq(conf->wait_for_quiescent, conf->quiesce == 0, | |
5459 | conf->device_lock); | |
5460 | atomic_inc(&conf->active_aligned_reads); | |
5461 | spin_unlock_irq(&conf->device_lock); | |
5462 | } | |
387bb173 | 5463 | |
c396b90e | 5464 | mddev_trace_remap(mddev, align_bio, raid_bio->bi_iter.bi_sector); |
e82ed3a4 CH |
5465 | submit_bio_noacct(align_bio); |
5466 | return 1; | |
f679623f RBJ |
5467 | } |
5468 | ||
7ef6b12a ML |
5469 | static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) |
5470 | { | |
5471 | struct bio *split; | |
dd7a8f5d N |
5472 | sector_t sector = raid_bio->bi_iter.bi_sector; |
5473 | unsigned chunk_sects = mddev->chunk_sectors; | |
5474 | unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); | |
7ef6b12a | 5475 | |
dd7a8f5d N |
5476 | if (sectors < bio_sectors(raid_bio)) { |
5477 | struct r5conf *conf = mddev->private; | |
afeee514 | 5478 | split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split); |
dd7a8f5d | 5479 | bio_chain(split, raid_bio); |
ed00aabd | 5480 | submit_bio_noacct(raid_bio); |
dd7a8f5d N |
5481 | raid_bio = split; |
5482 | } | |
7ef6b12a | 5483 | |
dd7a8f5d N |
5484 | if (!raid5_read_one_chunk(mddev, raid_bio)) |
5485 | return raid_bio; | |
7ef6b12a ML |
5486 | |
5487 | return NULL; | |
5488 | } | |
5489 | ||
8b3e6cdc DW |
5490 | /* __get_priority_stripe - get the next stripe to process |
5491 | * | |
5492 | * Full stripe writes are allowed to pass preread active stripes up until | |
5493 | * the bypass_threshold is exceeded. In general the bypass_count | |
5494 | * increments when the handle_list is handled before the hold_list; however, it | |
5495 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a | |
5496 | * stripe with in flight i/o. The bypass_count will be reset when the | |
5497 | * head of the hold_list has changed, i.e. the head was promoted to the | |
5498 | * handle_list. | |
5499 | */ | |
851c30c9 | 5500 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
4631f39f | 5501 | __must_hold(&conf->device_lock) |
8b3e6cdc | 5502 | { |
535ae4eb | 5503 | struct stripe_head *sh, *tmp; |
851c30c9 | 5504 | struct list_head *handle_list = NULL; |
535ae4eb | 5505 | struct r5worker_group *wg; |
70d466f7 SL |
5506 | bool second_try = !r5c_is_writeback(conf->log) && |
5507 | !r5l_log_disk_error(conf); | |
5508 | bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) || | |
5509 | r5l_log_disk_error(conf); | |
851c30c9 | 5510 | |
535ae4eb SL |
5511 | again: |
5512 | wg = NULL; | |
5513 | sh = NULL; | |
851c30c9 | 5514 | if (conf->worker_cnt_per_group == 0) { |
535ae4eb SL |
5515 | handle_list = try_loprio ? &conf->loprio_list : |
5516 | &conf->handle_list; | |
851c30c9 | 5517 | } else if (group != ANY_GROUP) { |
535ae4eb SL |
5518 | handle_list = try_loprio ? &conf->worker_groups[group].loprio_list : |
5519 | &conf->worker_groups[group].handle_list; | |
bfc90cb0 | 5520 | wg = &conf->worker_groups[group]; |
851c30c9 SL |
5521 | } else { |
5522 | int i; | |
5523 | for (i = 0; i < conf->group_cnt; i++) { | |
535ae4eb SL |
5524 | handle_list = try_loprio ? &conf->worker_groups[i].loprio_list : |
5525 | &conf->worker_groups[i].handle_list; | |
bfc90cb0 | 5526 | wg = &conf->worker_groups[i]; |
851c30c9 SL |
5527 | if (!list_empty(handle_list)) |
5528 | break; | |
5529 | } | |
5530 | } | |
8b3e6cdc DW |
5531 | |
5532 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", | |
5533 | __func__, | |
851c30c9 | 5534 | list_empty(handle_list) ? "empty" : "busy", |
8b3e6cdc DW |
5535 | list_empty(&conf->hold_list) ? "empty" : "busy", |
5536 | atomic_read(&conf->pending_full_writes), conf->bypass_count); | |
5537 | ||
851c30c9 SL |
5538 | if (!list_empty(handle_list)) { |
5539 | sh = list_entry(handle_list->next, typeof(*sh), lru); | |
8b3e6cdc DW |
5540 | |
5541 | if (list_empty(&conf->hold_list)) | |
5542 | conf->bypass_count = 0; | |
5543 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { | |
5544 | if (conf->hold_list.next == conf->last_hold) | |
5545 | conf->bypass_count++; | |
5546 | else { | |
5547 | conf->last_hold = conf->hold_list.next; | |
5548 | conf->bypass_count -= conf->bypass_threshold; | |
5549 | if (conf->bypass_count < 0) | |
5550 | conf->bypass_count = 0; | |
5551 | } | |
5552 | } | |
5553 | } else if (!list_empty(&conf->hold_list) && | |
5554 | ((conf->bypass_threshold && | |
5555 | conf->bypass_count > conf->bypass_threshold) || | |
5556 | atomic_read(&conf->pending_full_writes) == 0)) { | |
851c30c9 SL |
5557 | |
5558 | list_for_each_entry(tmp, &conf->hold_list, lru) { | |
5559 | if (conf->worker_cnt_per_group == 0 || | |
5560 | group == ANY_GROUP || | |
5561 | !cpu_online(tmp->cpu) || | |
5562 | cpu_to_group(tmp->cpu) == group) { | |
5563 | sh = tmp; | |
5564 | break; | |
5565 | } | |
5566 | } | |
5567 | ||
5568 | if (sh) { | |
5569 | conf->bypass_count -= conf->bypass_threshold; | |
5570 | if (conf->bypass_count < 0) | |
5571 | conf->bypass_count = 0; | |
5572 | } | |
bfc90cb0 | 5573 | wg = NULL; |
851c30c9 SL |
5574 | } |
5575 | ||
535ae4eb SL |
5576 | if (!sh) { |
5577 | if (second_try) | |
5578 | return NULL; | |
5579 | second_try = true; | |
5580 | try_loprio = !try_loprio; | |
5581 | goto again; | |
5582 | } | |
8b3e6cdc | 5583 | |
bfc90cb0 SL |
5584 | if (wg) { |
5585 | wg->stripes_cnt--; | |
5586 | sh->group = NULL; | |
5587 | } | |
8b3e6cdc | 5588 | list_del_init(&sh->lru); |
c7a6d35e | 5589 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
8b3e6cdc DW |
5590 | return sh; |
5591 | } | |
f679623f | 5592 | |
8811b596 SL |
5593 | struct raid5_plug_cb { |
5594 | struct blk_plug_cb cb; | |
5595 | struct list_head list; | |
566c09c5 | 5596 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
8811b596 SL |
5597 | }; |
5598 | ||
5599 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) | |
5600 | { | |
5601 | struct raid5_plug_cb *cb = container_of( | |
5602 | blk_cb, struct raid5_plug_cb, cb); | |
5603 | struct stripe_head *sh; | |
5604 | struct mddev *mddev = cb->cb.data; | |
5605 | struct r5conf *conf = mddev->private; | |
a9add5d9 | 5606 | int cnt = 0; |
566c09c5 | 5607 | int hash; |
8811b596 SL |
5608 | |
5609 | if (cb->list.next && !list_empty(&cb->list)) { | |
5610 | spin_lock_irq(&conf->device_lock); | |
5611 | while (!list_empty(&cb->list)) { | |
5612 | sh = list_first_entry(&cb->list, struct stripe_head, lru); | |
5613 | list_del_init(&sh->lru); | |
5614 | /* | |
5615 | * avoid race release_stripe_plug() sees | |
5616 | * STRIPE_ON_UNPLUG_LIST clear but the stripe | |
5617 | * is still in our list | |
5618 | */ | |
4e857c58 | 5619 | smp_mb__before_atomic(); |
8811b596 | 5620 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
773ca82f SL |
5621 | /* |
5622 | * STRIPE_ON_RELEASE_LIST could be set here. In that | |
5623 | * case, the count is always > 1 here | |
5624 | */ | |
566c09c5 SL |
5625 | hash = sh->hash_lock_index; |
5626 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); | |
a9add5d9 | 5627 | cnt++; |
8811b596 SL |
5628 | } |
5629 | spin_unlock_irq(&conf->device_lock); | |
5630 | } | |
566c09c5 SL |
5631 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
5632 | NR_STRIPE_HASH_LOCKS); | |
176df894 | 5633 | if (!mddev_is_dm(mddev)) |
396799eb | 5634 | trace_block_unplug(mddev->gendisk->queue, cnt, !from_schedule); |
8811b596 SL |
5635 | kfree(cb); |
5636 | } | |
5637 | ||
5638 | static void release_stripe_plug(struct mddev *mddev, | |
5639 | struct stripe_head *sh) | |
5640 | { | |
5641 | struct blk_plug_cb *blk_cb = blk_check_plugged( | |
5642 | raid5_unplug, mddev, | |
5643 | sizeof(struct raid5_plug_cb)); | |
5644 | struct raid5_plug_cb *cb; | |
5645 | ||
5646 | if (!blk_cb) { | |
6d036f7d | 5647 | raid5_release_stripe(sh); |
8811b596 SL |
5648 | return; |
5649 | } | |
5650 | ||
5651 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); | |
5652 | ||
566c09c5 SL |
5653 | if (cb->list.next == NULL) { |
5654 | int i; | |
8811b596 | 5655 | INIT_LIST_HEAD(&cb->list); |
566c09c5 SL |
5656 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
5657 | INIT_LIST_HEAD(cb->temp_inactive_list + i); | |
5658 | } | |
8811b596 SL |
5659 | |
5660 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) | |
5661 | list_add_tail(&sh->lru, &cb->list); | |
5662 | else | |
6d036f7d | 5663 | raid5_release_stripe(sh); |
8811b596 SL |
5664 | } |
5665 | ||
620125f2 SL |
5666 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
5667 | { | |
5668 | struct r5conf *conf = mddev->private; | |
5669 | sector_t logical_sector, last_sector; | |
5670 | struct stripe_head *sh; | |
620125f2 SL |
5671 | int stripe_sectors; |
5672 | ||
bf2c411b VV |
5673 | /* We need to handle this when io_uring supports discard/trim */ |
5674 | if (WARN_ON_ONCE(bi->bi_opf & REQ_NOWAIT)) | |
5675 | return; | |
5676 | ||
620125f2 SL |
5677 | if (mddev->reshape_position != MaxSector) |
5678 | /* Skip discard while reshape is happening */ | |
5679 | return; | |
5680 | ||
c911c46c | 5681 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); |
b0f01ecf | 5682 | last_sector = bio_end_sector(bi); |
620125f2 SL |
5683 | |
5684 | bi->bi_next = NULL; | |
620125f2 SL |
5685 | |
5686 | stripe_sectors = conf->chunk_sectors * | |
5687 | (conf->raid_disks - conf->max_degraded); | |
5688 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, | |
5689 | stripe_sectors); | |
5690 | sector_div(last_sector, stripe_sectors); | |
5691 | ||
5692 | logical_sector *= conf->chunk_sectors; | |
5693 | last_sector *= conf->chunk_sectors; | |
5694 | ||
5695 | for (; logical_sector < last_sector; | |
c911c46c | 5696 | logical_sector += RAID5_STRIPE_SECTORS(conf)) { |
620125f2 SL |
5697 | DEFINE_WAIT(w); |
5698 | int d; | |
5699 | again: | |
2f2d51ef | 5700 | sh = raid5_get_active_stripe(conf, NULL, logical_sector, 0); |
f8dfcffd N |
5701 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
5702 | if (test_bit(STRIPE_SYNCING, &sh->state)) { | |
6d036f7d | 5703 | raid5_release_stripe(sh); |
e6a03207 AP |
5704 | wait_on_bit(&sh->dev[sh->pd_idx].flags, R5_Overlap, |
5705 | TASK_UNINTERRUPTIBLE); | |
f8dfcffd N |
5706 | goto again; |
5707 | } | |
5708 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); | |
620125f2 SL |
5709 | spin_lock_irq(&sh->stripe_lock); |
5710 | for (d = 0; d < conf->raid_disks; d++) { | |
5711 | if (d == sh->pd_idx || d == sh->qd_idx) | |
5712 | continue; | |
5713 | if (sh->dev[d].towrite || sh->dev[d].toread) { | |
5714 | set_bit(R5_Overlap, &sh->dev[d].flags); | |
5715 | spin_unlock_irq(&sh->stripe_lock); | |
6d036f7d | 5716 | raid5_release_stripe(sh); |
e6a03207 AP |
5717 | wait_on_bit(&sh->dev[d].flags, R5_Overlap, |
5718 | TASK_UNINTERRUPTIBLE); | |
620125f2 SL |
5719 | goto again; |
5720 | } | |
5721 | } | |
f8dfcffd | 5722 | set_bit(STRIPE_DISCARD, &sh->state); |
7a87f434 | 5723 | sh->overwrite_disks = 0; |
620125f2 SL |
5724 | for (d = 0; d < conf->raid_disks; d++) { |
5725 | if (d == sh->pd_idx || d == sh->qd_idx) | |
5726 | continue; | |
5727 | sh->dev[d].towrite = bi; | |
5728 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); | |
016c76ac | 5729 | bio_inc_remaining(bi); |
49728050 | 5730 | md_write_inc(mddev, bi); |
7a87f434 | 5731 | sh->overwrite_disks++; |
620125f2 SL |
5732 | } |
5733 | spin_unlock_irq(&sh->stripe_lock); | |
5734 | if (conf->mddev->bitmap) { | |
620125f2 SL |
5735 | sh->bm_seq = conf->seq_flush + 1; |
5736 | set_bit(STRIPE_BIT_DELAY, &sh->state); | |
5737 | } | |
5738 | ||
5739 | set_bit(STRIPE_HANDLE, &sh->state); | |
5740 | clear_bit(STRIPE_DELAYED, &sh->state); | |
5741 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
5742 | atomic_inc(&conf->preread_active_stripes); | |
5743 | release_stripe_plug(mddev, sh); | |
5744 | } | |
5745 | ||
016c76ac | 5746 | bio_endio(bi); |
620125f2 SL |
5747 | } |
5748 | ||
a8bb304c LG |
5749 | static bool ahead_of_reshape(struct mddev *mddev, sector_t sector, |
5750 | sector_t reshape_sector) | |
5751 | { | |
5752 | return mddev->reshape_backwards ? sector < reshape_sector : | |
5753 | sector >= reshape_sector; | |
5754 | } | |
5755 | ||
486f6055 LG |
5756 | static bool range_ahead_of_reshape(struct mddev *mddev, sector_t min, |
5757 | sector_t max, sector_t reshape_sector) | |
5758 | { | |
5759 | return mddev->reshape_backwards ? max < reshape_sector : | |
5760 | min >= reshape_sector; | |
5761 | } | |
5762 | ||
5763 | static bool stripe_ahead_of_reshape(struct mddev *mddev, struct r5conf *conf, | |
5764 | struct stripe_head *sh) | |
5765 | { | |
5766 | sector_t max_sector = 0, min_sector = MaxSector; | |
5767 | bool ret = false; | |
5768 | int dd_idx; | |
5769 | ||
5770 | for (dd_idx = 0; dd_idx < sh->disks; dd_idx++) { | |
c467e97f | 5771 | if (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) |
486f6055 LG |
5772 | continue; |
5773 | ||
5774 | min_sector = min(min_sector, sh->dev[dd_idx].sector); | |
c467e97f | 5775 | max_sector = max(max_sector, sh->dev[dd_idx].sector); |
486f6055 LG |
5776 | } |
5777 | ||
5778 | spin_lock_irq(&conf->device_lock); | |
5779 | ||
5780 | if (!range_ahead_of_reshape(mddev, min_sector, max_sector, | |
5781 | conf->reshape_progress)) | |
5782 | /* mismatch, need to try again */ | |
5783 | ret = true; | |
5784 | ||
5785 | spin_unlock_irq(&conf->device_lock); | |
5786 | ||
5787 | return ret; | |
5788 | } | |
5789 | ||
7e55c60a LG |
5790 | static int add_all_stripe_bios(struct r5conf *conf, |
5791 | struct stripe_request_ctx *ctx, struct stripe_head *sh, | |
5792 | struct bio *bi, int forwrite, int previous) | |
5793 | { | |
5794 | int dd_idx; | |
7e55c60a LG |
5795 | |
5796 | spin_lock_irq(&sh->stripe_lock); | |
5797 | ||
5798 | for (dd_idx = 0; dd_idx < sh->disks; dd_idx++) { | |
5799 | struct r5dev *dev = &sh->dev[dd_idx]; | |
5800 | ||
5801 | if (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) | |
5802 | continue; | |
5803 | ||
5804 | if (dev->sector < ctx->first_sector || | |
5805 | dev->sector >= ctx->last_sector) | |
5806 | continue; | |
5807 | ||
5808 | if (stripe_bio_overlaps(sh, bi, dd_idx, forwrite)) { | |
5809 | set_bit(R5_Overlap, &dev->flags); | |
e6a03207 AP |
5810 | spin_unlock_irq(&sh->stripe_lock); |
5811 | raid5_release_stripe(sh); | |
5812 | /* release batch_last before wait to avoid risk of deadlock */ | |
5813 | if (ctx->batch_last) { | |
5814 | raid5_release_stripe(ctx->batch_last); | |
5815 | ctx->batch_last = NULL; | |
5816 | } | |
5817 | md_wakeup_thread(conf->mddev->thread); | |
5818 | wait_on_bit(&dev->flags, R5_Overlap, TASK_UNINTERRUPTIBLE); | |
5819 | return 0; | |
7e55c60a LG |
5820 | } |
5821 | } | |
5822 | ||
7e55c60a LG |
5823 | for (dd_idx = 0; dd_idx < sh->disks; dd_idx++) { |
5824 | struct r5dev *dev = &sh->dev[dd_idx]; | |
5825 | ||
5826 | if (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) | |
5827 | continue; | |
5828 | ||
5829 | if (dev->sector < ctx->first_sector || | |
5830 | dev->sector >= ctx->last_sector) | |
5831 | continue; | |
5832 | ||
5833 | __add_stripe_bio(sh, bi, dd_idx, forwrite, previous); | |
5834 | clear_bit((dev->sector - ctx->first_sector) >> | |
5835 | RAID5_STRIPE_SHIFT(conf), ctx->sectors_to_do); | |
5836 | } | |
5837 | ||
7e55c60a | 5838 | spin_unlock_irq(&sh->stripe_lock); |
e6a03207 | 5839 | return 1; |
7e55c60a LG |
5840 | } |
5841 | ||
25b3a823 BM |
5842 | enum reshape_loc { |
5843 | LOC_NO_RESHAPE, | |
5844 | LOC_AHEAD_OF_RESHAPE, | |
5845 | LOC_INSIDE_RESHAPE, | |
5846 | LOC_BEHIND_RESHAPE, | |
5847 | }; | |
5848 | ||
5849 | static enum reshape_loc get_reshape_loc(struct mddev *mddev, | |
5850 | struct r5conf *conf, sector_t logical_sector) | |
5851 | { | |
5852 | sector_t reshape_progress, reshape_safe; | |
1320fe87 YK |
5853 | |
5854 | if (likely(conf->reshape_progress == MaxSector)) | |
5855 | return LOC_NO_RESHAPE; | |
25b3a823 BM |
5856 | /* |
5857 | * Spinlock is needed as reshape_progress may be | |
5858 | * 64bit on a 32bit platform, and so it might be | |
5859 | * possible to see a half-updated value | |
5860 | * Of course reshape_progress could change after | |
5861 | * the lock is dropped, so once we get a reference | |
5862 | * to the stripe that we think it is, we will have | |
5863 | * to check again. | |
5864 | */ | |
5865 | spin_lock_irq(&conf->device_lock); | |
5866 | reshape_progress = conf->reshape_progress; | |
5867 | reshape_safe = conf->reshape_safe; | |
5868 | spin_unlock_irq(&conf->device_lock); | |
5869 | if (reshape_progress == MaxSector) | |
5870 | return LOC_NO_RESHAPE; | |
5871 | if (ahead_of_reshape(mddev, logical_sector, reshape_progress)) | |
5872 | return LOC_AHEAD_OF_RESHAPE; | |
5873 | if (ahead_of_reshape(mddev, logical_sector, reshape_safe)) | |
5874 | return LOC_INSIDE_RESHAPE; | |
5875 | return LOC_BEHIND_RESHAPE; | |
5876 | } | |
5877 | ||
9c89f604 YK |
5878 | static void raid5_bitmap_sector(struct mddev *mddev, sector_t *offset, |
5879 | unsigned long *sectors) | |
5880 | { | |
5881 | struct r5conf *conf = mddev->private; | |
5882 | sector_t start = *offset; | |
5883 | sector_t end = start + *sectors; | |
5884 | sector_t prev_start = start; | |
5885 | sector_t prev_end = end; | |
5886 | int sectors_per_chunk; | |
5887 | enum reshape_loc loc; | |
5888 | int dd_idx; | |
5889 | ||
5890 | sectors_per_chunk = conf->chunk_sectors * | |
5891 | (conf->raid_disks - conf->max_degraded); | |
5892 | start = round_down(start, sectors_per_chunk); | |
5893 | end = round_up(end, sectors_per_chunk); | |
5894 | ||
5895 | start = raid5_compute_sector(conf, start, 0, &dd_idx, NULL); | |
5896 | end = raid5_compute_sector(conf, end, 0, &dd_idx, NULL); | |
5897 | ||
5898 | /* | |
5899 | * For LOC_INSIDE_RESHAPE, this IO will wait for reshape to make | |
5900 | * progress, hence it's the same as LOC_BEHIND_RESHAPE. | |
5901 | */ | |
5902 | loc = get_reshape_loc(mddev, conf, prev_start); | |
5903 | if (likely(loc != LOC_AHEAD_OF_RESHAPE)) { | |
5904 | *offset = start; | |
5905 | *sectors = end - start; | |
5906 | return; | |
5907 | } | |
5908 | ||
5909 | sectors_per_chunk = conf->prev_chunk_sectors * | |
5910 | (conf->previous_raid_disks - conf->max_degraded); | |
5911 | prev_start = round_down(prev_start, sectors_per_chunk); | |
5912 | prev_end = round_down(prev_end, sectors_per_chunk); | |
5913 | ||
5914 | prev_start = raid5_compute_sector(conf, prev_start, 1, &dd_idx, NULL); | |
5915 | prev_end = raid5_compute_sector(conf, prev_end, 1, &dd_idx, NULL); | |
5916 | ||
5917 | /* | |
5918 | * for LOC_AHEAD_OF_RESHAPE, reshape can make progress before this IO | |
5919 | * is handled in make_stripe_request(), we can't know this here hence | |
5920 | * we set bits for both. | |
5921 | */ | |
5922 | *offset = min(start, prev_start); | |
5923 | *sectors = max(end, prev_end) - *offset; | |
5924 | } | |
5925 | ||
f4aec6a0 LG |
5926 | static enum stripe_result make_stripe_request(struct mddev *mddev, |
5927 | struct r5conf *conf, struct stripe_request_ctx *ctx, | |
4f354560 | 5928 | sector_t logical_sector, struct bio *bi) |
f4aec6a0 LG |
5929 | { |
5930 | const int rw = bio_data_dir(bi); | |
5931 | enum stripe_result ret; | |
5932 | struct stripe_head *sh; | |
1320fe87 | 5933 | enum reshape_loc loc; |
f4aec6a0 | 5934 | sector_t new_sector; |
2f2d51ef | 5935 | int previous = 0, flags = 0; |
4f354560 LG |
5936 | int seq, dd_idx; |
5937 | ||
5938 | seq = read_seqcount_begin(&conf->gen_lock); | |
1320fe87 YK |
5939 | loc = get_reshape_loc(mddev, conf, logical_sector); |
5940 | if (loc == LOC_INSIDE_RESHAPE) { | |
5941 | ret = STRIPE_SCHEDULE_AND_RETRY; | |
5942 | goto out; | |
f4aec6a0 | 5943 | } |
1320fe87 YK |
5944 | if (loc == LOC_AHEAD_OF_RESHAPE) |
5945 | previous = 1; | |
f4aec6a0 LG |
5946 | |
5947 | new_sector = raid5_compute_sector(conf, logical_sector, previous, | |
5948 | &dd_idx, NULL); | |
5949 | pr_debug("raid456: %s, sector %llu logical %llu\n", __func__, | |
5950 | new_sector, logical_sector); | |
5951 | ||
2f2d51ef LG |
5952 | if (previous) |
5953 | flags |= R5_GAS_PREVIOUS; | |
5954 | if (bi->bi_opf & REQ_RAHEAD) | |
5955 | flags |= R5_GAS_NOBLOCK; | |
5956 | sh = raid5_get_active_stripe(conf, ctx, new_sector, flags); | |
f4aec6a0 LG |
5957 | if (unlikely(!sh)) { |
5958 | /* cannot get stripe, just give-up */ | |
5959 | bi->bi_status = BLK_STS_IOERR; | |
5960 | return STRIPE_FAIL; | |
5961 | } | |
5962 | ||
486f6055 LG |
5963 | if (unlikely(previous) && |
5964 | stripe_ahead_of_reshape(mddev, conf, sh)) { | |
f4aec6a0 | 5965 | /* |
486f6055 | 5966 | * Expansion moved on while waiting for a stripe. |
f4aec6a0 LG |
5967 | * Expansion could still move past after this |
5968 | * test, but as we are holding a reference to | |
5969 | * 'sh', we know that if that happens, | |
5970 | * STRIPE_EXPANDING will get set and the expansion | |
5971 | * won't proceed until we finish with the stripe. | |
5972 | */ | |
486f6055 LG |
5973 | ret = STRIPE_SCHEDULE_AND_RETRY; |
5974 | goto out_release; | |
f4aec6a0 LG |
5975 | } |
5976 | ||
5977 | if (read_seqcount_retry(&conf->gen_lock, seq)) { | |
5978 | /* Might have got the wrong stripe_head by accident */ | |
5979 | ret = STRIPE_RETRY; | |
5980 | goto out_release; | |
5981 | } | |
5982 | ||
e6a03207 | 5983 | if (test_bit(STRIPE_EXPANDING, &sh->state)) { |
f4aec6a0 LG |
5984 | md_wakeup_thread(mddev->thread); |
5985 | ret = STRIPE_SCHEDULE_AND_RETRY; | |
5986 | goto out_release; | |
5987 | } | |
5988 | ||
e6a03207 AP |
5989 | if (!add_all_stripe_bios(conf, ctx, sh, bi, rw, previous)) { |
5990 | ret = STRIPE_RETRY; | |
5991 | goto out; | |
5992 | } | |
5993 | ||
3312e6c8 LG |
5994 | if (stripe_can_batch(sh)) { |
5995 | stripe_add_to_batch_list(conf, sh, ctx->batch_last); | |
5996 | if (ctx->batch_last) | |
5997 | raid5_release_stripe(ctx->batch_last); | |
5998 | atomic_inc(&sh->count); | |
5999 | ctx->batch_last = sh; | |
6000 | } | |
f4aec6a0 LG |
6001 | |
6002 | if (ctx->do_flush) { | |
6003 | set_bit(STRIPE_R5C_PREFLUSH, &sh->state); | |
6004 | /* we only need flush for one stripe */ | |
6005 | ctx->do_flush = false; | |
6006 | } | |
6007 | ||
6008 | set_bit(STRIPE_HANDLE, &sh->state); | |
6009 | clear_bit(STRIPE_DELAYED, &sh->state); | |
6010 | if ((!sh->batch_head || sh == sh->batch_head) && | |
6011 | (bi->bi_opf & REQ_SYNC) && | |
6012 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
6013 | atomic_inc(&conf->preread_active_stripes); | |
6014 | ||
6015 | release_stripe_plug(mddev, sh); | |
6016 | return STRIPE_SUCCESS; | |
6017 | ||
6018 | out_release: | |
6019 | raid5_release_stripe(sh); | |
41425f96 YK |
6020 | out: |
6021 | if (ret == STRIPE_SCHEDULE_AND_RETRY && reshape_interrupted(mddev)) { | |
6022 | bi->bi_status = BLK_STS_RESOURCE; | |
6023 | ret = STRIPE_WAIT_RESHAPE; | |
6024 | pr_err_ratelimited("dm-raid456: io across reshape position while reshape can't make progress"); | |
6025 | } | |
f4aec6a0 LG |
6026 | return ret; |
6027 | } | |
6028 | ||
fc05e06e JK |
6029 | /* |
6030 | * If the bio covers multiple data disks, find sector within the bio that has | |
6031 | * the lowest chunk offset in the first chunk. | |
6032 | */ | |
6033 | static sector_t raid5_bio_lowest_chunk_sector(struct r5conf *conf, | |
6034 | struct bio *bi) | |
6035 | { | |
6036 | int sectors_per_chunk = conf->chunk_sectors; | |
6037 | int raid_disks = conf->raid_disks; | |
6038 | int dd_idx; | |
6039 | struct stripe_head sh; | |
6040 | unsigned int chunk_offset; | |
6041 | sector_t r_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); | |
6042 | sector_t sector; | |
6043 | ||
6044 | /* We pass in fake stripe_head to get back parity disk numbers */ | |
6045 | sector = raid5_compute_sector(conf, r_sector, 0, &dd_idx, &sh); | |
6046 | chunk_offset = sector_div(sector, sectors_per_chunk); | |
6047 | if (sectors_per_chunk - chunk_offset >= bio_sectors(bi)) | |
6048 | return r_sector; | |
6049 | /* | |
6050 | * Bio crosses to the next data disk. Check whether it's in the same | |
6051 | * chunk. | |
6052 | */ | |
6053 | dd_idx++; | |
6054 | while (dd_idx == sh.pd_idx || dd_idx == sh.qd_idx) | |
6055 | dd_idx++; | |
6056 | if (dd_idx >= raid_disks) | |
6057 | return r_sector; | |
6058 | return r_sector + sectors_per_chunk - chunk_offset; | |
6059 | } | |
6060 | ||
cc27b0c7 | 6061 | static bool raid5_make_request(struct mddev *mddev, struct bio * bi) |
1da177e4 | 6062 | { |
ee1aa06b | 6063 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
0e4aac73 | 6064 | bool on_wq; |
d1688a6d | 6065 | struct r5conf *conf = mddev->private; |
7e55c60a | 6066 | sector_t logical_sector; |
f4aec6a0 | 6067 | struct stripe_request_ctx ctx = {}; |
a362357b | 6068 | const int rw = bio_data_dir(bi); |
f4aec6a0 | 6069 | enum stripe_result res; |
b9f91d80 | 6070 | int s, stripe_cnt; |
1da177e4 | 6071 | |
1eff9d32 | 6072 | if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { |
1532d9e8 | 6073 | int ret = log_handle_flush_request(conf, bi); |
828cbe98 SL |
6074 | |
6075 | if (ret == 0) | |
cc27b0c7 | 6076 | return true; |
828cbe98 | 6077 | if (ret == -ENODEV) { |
775d7831 DJ |
6078 | if (md_flush_request(mddev, bi)) |
6079 | return true; | |
828cbe98 SL |
6080 | } |
6081 | /* ret == -EAGAIN, fallback */ | |
3bddb7f8 SL |
6082 | /* |
6083 | * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH, | |
6084 | * we need to flush journal device | |
6085 | */ | |
f4aec6a0 | 6086 | ctx.do_flush = bi->bi_opf & REQ_PREFLUSH; |
e5dcdd80 N |
6087 | } |
6088 | ||
03e792ea | 6089 | md_write_start(mddev, bi); |
9ffc8f7c EM |
6090 | /* |
6091 | * If array is degraded, better not do chunk aligned read because | |
6092 | * later we might have to read it again in order to reconstruct | |
6093 | * data on failed drives. | |
6094 | */ | |
6095 | if (rw == READ && mddev->degraded == 0 && | |
7ef6b12a ML |
6096 | mddev->reshape_position == MaxSector) { |
6097 | bi = chunk_aligned_read(mddev, bi); | |
6098 | if (!bi) | |
cc27b0c7 | 6099 | return true; |
7ef6b12a | 6100 | } |
52488615 | 6101 | |
796a5cf0 | 6102 | if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { |
620125f2 | 6103 | make_discard_request(mddev, bi); |
cc27b0c7 N |
6104 | md_write_end(mddev); |
6105 | return true; | |
620125f2 SL |
6106 | } |
6107 | ||
c911c46c | 6108 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); |
7e55c60a LG |
6109 | ctx.first_sector = logical_sector; |
6110 | ctx.last_sector = bio_end_sector(bi); | |
1da177e4 | 6111 | bi->bi_next = NULL; |
06d91a5f | 6112 | |
b9f91d80 LG |
6113 | stripe_cnt = DIV_ROUND_UP_SECTOR_T(ctx.last_sector - logical_sector, |
6114 | RAID5_STRIPE_SECTORS(conf)); | |
6115 | bitmap_set(ctx.sectors_to_do, 0, stripe_cnt); | |
7e55c60a | 6116 | |
df1b620a LG |
6117 | pr_debug("raid456: %s, logical %llu to %llu\n", __func__, |
6118 | bi->bi_iter.bi_sector, ctx.last_sector); | |
6119 | ||
bf2c411b VV |
6120 | /* Bail out if conflicts with reshape and REQ_NOWAIT is set */ |
6121 | if ((bi->bi_opf & REQ_NOWAIT) && | |
25b3a823 | 6122 | get_reshape_loc(mddev, conf, logical_sector) == LOC_INSIDE_RESHAPE) { |
bf2c411b VV |
6123 | bio_wouldblock_error(bi); |
6124 | if (rw == WRITE) | |
6125 | md_write_end(mddev); | |
6126 | return true; | |
6127 | } | |
1147f58e | 6128 | md_account_bio(mddev, &bi); |
ee1aa06b | 6129 | |
fc05e06e JK |
6130 | /* |
6131 | * Lets start with the stripe with the lowest chunk offset in the first | |
6132 | * chunk. That has the best chances of creating IOs adjacent to | |
6133 | * previous IOs in case of sequential IO and thus creates the most | |
6134 | * sequential IO pattern. We don't bother with the optimization when | |
6135 | * reshaping as the performance benefit is not worth the complexity. | |
6136 | */ | |
0e4aac73 | 6137 | if (likely(conf->reshape_progress == MaxSector)) { |
fc05e06e | 6138 | logical_sector = raid5_bio_lowest_chunk_sector(conf, bi); |
0e4aac73 AP |
6139 | on_wq = false; |
6140 | } else { | |
6f039cc4 | 6141 | add_wait_queue(&conf->wait_for_reshape, &wait); |
0e4aac73 AP |
6142 | on_wq = true; |
6143 | } | |
fc05e06e JK |
6144 | s = (logical_sector - ctx.first_sector) >> RAID5_STRIPE_SHIFT(conf); |
6145 | ||
7e55c60a | 6146 | while (1) { |
f4aec6a0 | 6147 | res = make_stripe_request(mddev, conf, &ctx, logical_sector, |
4f354560 | 6148 | bi); |
41425f96 | 6149 | if (res == STRIPE_FAIL || res == STRIPE_WAIT_RESHAPE) |
27fb7010 | 6150 | break; |
3bddb7f8 | 6151 | |
f4aec6a0 | 6152 | if (res == STRIPE_RETRY) |
0a2d1694 | 6153 | continue; |
27fb7010 | 6154 | |
f4aec6a0 | 6155 | if (res == STRIPE_SCHEDULE_AND_RETRY) { |
0e4aac73 | 6156 | WARN_ON_ONCE(!on_wq); |
3312e6c8 LG |
6157 | /* |
6158 | * Must release the reference to batch_last before | |
6159 | * scheduling and waiting for work to be done, | |
6160 | * otherwise the batch_last stripe head could prevent | |
6161 | * raid5_activate_delayed() from making progress | |
6162 | * and thus deadlocking. | |
6163 | */ | |
6164 | if (ctx.batch_last) { | |
6165 | raid5_release_stripe(ctx.batch_last); | |
6166 | ctx.batch_last = NULL; | |
6167 | } | |
6168 | ||
ee1aa06b LG |
6169 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, |
6170 | MAX_SCHEDULE_TIMEOUT); | |
0a2d1694 | 6171 | continue; |
1da177e4 | 6172 | } |
0a2d1694 | 6173 | |
fc05e06e | 6174 | s = find_next_bit_wrap(ctx.sectors_to_do, stripe_cnt, s); |
b9f91d80 | 6175 | if (s == stripe_cnt) |
7e55c60a LG |
6176 | break; |
6177 | ||
6178 | logical_sector = ctx.first_sector + | |
6179 | (s << RAID5_STRIPE_SHIFT(conf)); | |
1da177e4 | 6180 | } |
0e4aac73 | 6181 | if (unlikely(on_wq)) |
6f039cc4 | 6182 | remove_wait_queue(&conf->wait_for_reshape, &wait); |
7c13edc8 | 6183 | |
3312e6c8 LG |
6184 | if (ctx.batch_last) |
6185 | raid5_release_stripe(ctx.batch_last); | |
6186 | ||
49728050 N |
6187 | if (rw == WRITE) |
6188 | md_write_end(mddev); | |
41425f96 YK |
6189 | if (res == STRIPE_WAIT_RESHAPE) { |
6190 | md_free_cloned_bio(bi); | |
6191 | return false; | |
6192 | } | |
6193 | ||
016c76ac | 6194 | bio_endio(bi); |
cc27b0c7 | 6195 | return true; |
1da177e4 LT |
6196 | } |
6197 | ||
fd01b88c | 6198 | static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks); |
b522adcd | 6199 | |
fd01b88c | 6200 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped) |
1da177e4 | 6201 | { |
52c03291 N |
6202 | /* reshaping is quite different to recovery/resync so it is |
6203 | * handled quite separately ... here. | |
6204 | * | |
6205 | * On each call to sync_request, we gather one chunk worth of | |
6206 | * destination stripes and flag them as expanding. | |
6207 | * Then we find all the source stripes and request reads. | |
6208 | * As the reads complete, handle_stripe will copy the data | |
6209 | * into the destination stripe and release that stripe. | |
6210 | */ | |
d1688a6d | 6211 | struct r5conf *conf = mddev->private; |
1da177e4 | 6212 | struct stripe_head *sh; |
db0505d3 | 6213 | struct md_rdev *rdev; |
ccfcc3c1 | 6214 | sector_t first_sector, last_sector; |
f416885e N |
6215 | int raid_disks = conf->previous_raid_disks; |
6216 | int data_disks = raid_disks - conf->max_degraded; | |
6217 | int new_data_disks = conf->raid_disks - conf->max_degraded; | |
52c03291 N |
6218 | int i; |
6219 | int dd_idx; | |
c8f517c4 | 6220 | sector_t writepos, readpos, safepos; |
ec32a2bd | 6221 | sector_t stripe_addr; |
7a661381 | 6222 | int reshape_sectors; |
ab69ae12 | 6223 | struct list_head stripes; |
92140480 | 6224 | sector_t retn; |
52c03291 | 6225 | |
fef9c61f N |
6226 | if (sector_nr == 0) { |
6227 | /* If restarting in the middle, skip the initial sectors */ | |
2c810cdd | 6228 | if (mddev->reshape_backwards && |
fef9c61f N |
6229 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
6230 | sector_nr = raid5_size(mddev, 0, 0) | |
6231 | - conf->reshape_progress; | |
6cbd8148 N |
6232 | } else if (mddev->reshape_backwards && |
6233 | conf->reshape_progress == MaxSector) { | |
6234 | /* shouldn't happen, but just in case, finish up.*/ | |
6235 | sector_nr = MaxSector; | |
2c810cdd | 6236 | } else if (!mddev->reshape_backwards && |
fef9c61f N |
6237 | conf->reshape_progress > 0) |
6238 | sector_nr = conf->reshape_progress; | |
f416885e | 6239 | sector_div(sector_nr, new_data_disks); |
fef9c61f | 6240 | if (sector_nr) { |
8dee7211 | 6241 | mddev->curr_resync_completed = sector_nr; |
e1a86dbb | 6242 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
fef9c61f | 6243 | *skipped = 1; |
92140480 N |
6244 | retn = sector_nr; |
6245 | goto finish; | |
fef9c61f | 6246 | } |
52c03291 N |
6247 | } |
6248 | ||
7a661381 N |
6249 | /* We need to process a full chunk at a time. |
6250 | * If old and new chunk sizes differ, we need to process the | |
6251 | * largest of these | |
6252 | */ | |
3cb5edf4 N |
6253 | |
6254 | reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); | |
7a661381 | 6255 | |
b5254dd5 N |
6256 | /* We update the metadata at least every 10 seconds, or when |
6257 | * the data about to be copied would over-write the source of | |
6258 | * the data at the front of the range. i.e. one new_stripe | |
6259 | * along from reshape_progress new_maps to after where | |
6260 | * reshape_safe old_maps to | |
52c03291 | 6261 | */ |
fef9c61f | 6262 | writepos = conf->reshape_progress; |
f416885e | 6263 | sector_div(writepos, new_data_disks); |
c8f517c4 N |
6264 | readpos = conf->reshape_progress; |
6265 | sector_div(readpos, data_disks); | |
fef9c61f | 6266 | safepos = conf->reshape_safe; |
f416885e | 6267 | sector_div(safepos, data_disks); |
2c810cdd | 6268 | if (mddev->reshape_backwards) { |
305a5170 YK |
6269 | if (WARN_ON(writepos < reshape_sectors)) |
6270 | return MaxSector; | |
6271 | ||
c74c0d76 | 6272 | writepos -= reshape_sectors; |
c8f517c4 | 6273 | readpos += reshape_sectors; |
7a661381 | 6274 | safepos += reshape_sectors; |
fef9c61f | 6275 | } else { |
7a661381 | 6276 | writepos += reshape_sectors; |
c74c0d76 N |
6277 | /* readpos and safepos are worst-case calculations. |
6278 | * A negative number is overly pessimistic, and causes | |
6279 | * obvious problems for unsigned storage. So clip to 0. | |
6280 | */ | |
ed37d83e N |
6281 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
6282 | safepos -= min_t(sector_t, reshape_sectors, safepos); | |
fef9c61f | 6283 | } |
52c03291 | 6284 | |
b5254dd5 N |
6285 | /* Having calculated the 'writepos' possibly use it |
6286 | * to set 'stripe_addr' which is where we will write to. | |
6287 | */ | |
6288 | if (mddev->reshape_backwards) { | |
305a5170 YK |
6289 | if (WARN_ON(conf->reshape_progress == 0)) |
6290 | return MaxSector; | |
6291 | ||
b5254dd5 | 6292 | stripe_addr = writepos; |
305a5170 YK |
6293 | if (WARN_ON((mddev->dev_sectors & |
6294 | ~((sector_t)reshape_sectors - 1)) - | |
6295 | reshape_sectors - stripe_addr != sector_nr)) | |
6296 | return MaxSector; | |
b5254dd5 | 6297 | } else { |
305a5170 YK |
6298 | if (WARN_ON(writepos != sector_nr + reshape_sectors)) |
6299 | return MaxSector; | |
6300 | ||
b5254dd5 N |
6301 | stripe_addr = sector_nr; |
6302 | } | |
6303 | ||
c8f517c4 N |
6304 | /* 'writepos' is the most advanced device address we might write. |
6305 | * 'readpos' is the least advanced device address we might read. | |
6306 | * 'safepos' is the least address recorded in the metadata as having | |
6307 | * been reshaped. | |
b5254dd5 N |
6308 | * If there is a min_offset_diff, these are adjusted either by |
6309 | * increasing the safepos/readpos if diff is negative, or | |
6310 | * increasing writepos if diff is positive. | |
6311 | * If 'readpos' is then behind 'writepos', there is no way that we can | |
c8f517c4 N |
6312 | * ensure safety in the face of a crash - that must be done by userspace |
6313 | * making a backup of the data. So in that case there is no particular | |
6314 | * rush to update metadata. | |
6315 | * Otherwise if 'safepos' is behind 'writepos', then we really need to | |
6316 | * update the metadata to advance 'safepos' to match 'readpos' so that | |
6317 | * we can be safe in the event of a crash. | |
6318 | * So we insist on updating metadata if safepos is behind writepos and | |
6319 | * readpos is beyond writepos. | |
6320 | * In any case, update the metadata every 10 seconds. | |
6321 | * Maybe that number should be configurable, but I'm not sure it is | |
6322 | * worth it.... maybe it could be a multiple of safemode_delay??? | |
6323 | */ | |
b5254dd5 N |
6324 | if (conf->min_offset_diff < 0) { |
6325 | safepos += -conf->min_offset_diff; | |
6326 | readpos += -conf->min_offset_diff; | |
6327 | } else | |
6328 | writepos += conf->min_offset_diff; | |
6329 | ||
2c810cdd | 6330 | if ((mddev->reshape_backwards |
c8f517c4 N |
6331 | ? (safepos > writepos && readpos < writepos) |
6332 | : (safepos < writepos && readpos > writepos)) || | |
6333 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { | |
52c03291 | 6334 | /* Cannot proceed until we've updated the superblock... */ |
6f039cc4 | 6335 | wait_event(conf->wait_for_reshape, |
c91abf5a N |
6336 | atomic_read(&conf->reshape_stripes)==0 |
6337 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); | |
6338 | if (atomic_read(&conf->reshape_stripes) != 0) | |
6339 | return 0; | |
fef9c61f | 6340 | mddev->reshape_position = conf->reshape_progress; |
75d3da43 | 6341 | mddev->curr_resync_completed = sector_nr; |
db0505d3 N |
6342 | if (!mddev->reshape_backwards) |
6343 | /* Can update recovery_offset */ | |
6344 | rdev_for_each(rdev, mddev) | |
6345 | if (rdev->raid_disk >= 0 && | |
6346 | !test_bit(Journal, &rdev->flags) && | |
6347 | !test_bit(In_sync, &rdev->flags) && | |
6348 | rdev->recovery_offset < sector_nr) | |
6349 | rdev->recovery_offset = sector_nr; | |
6350 | ||
c8f517c4 | 6351 | conf->reshape_checkpoint = jiffies; |
2953079c | 6352 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
52c03291 | 6353 | md_wakeup_thread(mddev->thread); |
2953079c | 6354 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
c91abf5a N |
6355 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
6356 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
6357 | return 0; | |
52c03291 | 6358 | spin_lock_irq(&conf->device_lock); |
fef9c61f | 6359 | conf->reshape_safe = mddev->reshape_position; |
52c03291 | 6360 | spin_unlock_irq(&conf->device_lock); |
6f039cc4 | 6361 | wake_up(&conf->wait_for_reshape); |
e1a86dbb | 6362 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
52c03291 N |
6363 | } |
6364 | ||
ab69ae12 | 6365 | INIT_LIST_HEAD(&stripes); |
c911c46c | 6366 | for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) { |
52c03291 | 6367 | int j; |
a9f326eb | 6368 | int skipped_disk = 0; |
2f2d51ef LG |
6369 | sh = raid5_get_active_stripe(conf, NULL, stripe_addr+i, |
6370 | R5_GAS_NOQUIESCE); | |
52c03291 N |
6371 | set_bit(STRIPE_EXPANDING, &sh->state); |
6372 | atomic_inc(&conf->reshape_stripes); | |
6373 | /* If any of this stripe is beyond the end of the old | |
6374 | * array, then we need to zero those blocks | |
6375 | */ | |
6376 | for (j=sh->disks; j--;) { | |
6377 | sector_t s; | |
6378 | if (j == sh->pd_idx) | |
6379 | continue; | |
f416885e | 6380 | if (conf->level == 6 && |
d0dabf7e | 6381 | j == sh->qd_idx) |
f416885e | 6382 | continue; |
6d036f7d | 6383 | s = raid5_compute_blocknr(sh, j, 0); |
b522adcd | 6384 | if (s < raid5_size(mddev, 0, 0)) { |
a9f326eb | 6385 | skipped_disk = 1; |
52c03291 N |
6386 | continue; |
6387 | } | |
c911c46c | 6388 | memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf)); |
52c03291 N |
6389 | set_bit(R5_Expanded, &sh->dev[j].flags); |
6390 | set_bit(R5_UPTODATE, &sh->dev[j].flags); | |
6391 | } | |
a9f326eb | 6392 | if (!skipped_disk) { |
52c03291 N |
6393 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
6394 | set_bit(STRIPE_HANDLE, &sh->state); | |
6395 | } | |
ab69ae12 | 6396 | list_add(&sh->lru, &stripes); |
52c03291 N |
6397 | } |
6398 | spin_lock_irq(&conf->device_lock); | |
2c810cdd | 6399 | if (mddev->reshape_backwards) |
7a661381 | 6400 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
fef9c61f | 6401 | else |
7a661381 | 6402 | conf->reshape_progress += reshape_sectors * new_data_disks; |
52c03291 N |
6403 | spin_unlock_irq(&conf->device_lock); |
6404 | /* Ok, those stripe are ready. We can start scheduling | |
6405 | * reads on the source stripes. | |
6406 | * The source stripes are determined by mapping the first and last | |
6407 | * block on the destination stripes. | |
6408 | */ | |
52c03291 | 6409 | first_sector = |
ec32a2bd | 6410 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
911d4ee8 | 6411 | 1, &dd_idx, NULL); |
52c03291 | 6412 | last_sector = |
0e6e0271 | 6413 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
09c9e5fa | 6414 | * new_data_disks - 1), |
911d4ee8 | 6415 | 1, &dd_idx, NULL); |
58c0fed4 AN |
6416 | if (last_sector >= mddev->dev_sectors) |
6417 | last_sector = mddev->dev_sectors - 1; | |
52c03291 | 6418 | while (first_sector <= last_sector) { |
2f2d51ef LG |
6419 | sh = raid5_get_active_stripe(conf, NULL, first_sector, |
6420 | R5_GAS_PREVIOUS | R5_GAS_NOQUIESCE); | |
52c03291 N |
6421 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
6422 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 6423 | raid5_release_stripe(sh); |
c911c46c | 6424 | first_sector += RAID5_STRIPE_SECTORS(conf); |
52c03291 | 6425 | } |
ab69ae12 N |
6426 | /* Now that the sources are clearly marked, we can release |
6427 | * the destination stripes | |
6428 | */ | |
6429 | while (!list_empty(&stripes)) { | |
6430 | sh = list_entry(stripes.next, struct stripe_head, lru); | |
6431 | list_del_init(&sh->lru); | |
6d036f7d | 6432 | raid5_release_stripe(sh); |
ab69ae12 | 6433 | } |
c6207277 N |
6434 | /* If this takes us to the resync_max point where we have to pause, |
6435 | * then we need to write out the superblock. | |
6436 | */ | |
7a661381 | 6437 | sector_nr += reshape_sectors; |
92140480 N |
6438 | retn = reshape_sectors; |
6439 | finish: | |
c5e19d90 N |
6440 | if (mddev->curr_resync_completed > mddev->resync_max || |
6441 | (sector_nr - mddev->curr_resync_completed) * 2 | |
c03f6a19 | 6442 | >= mddev->resync_max - mddev->curr_resync_completed) { |
c6207277 | 6443 | /* Cannot proceed until we've updated the superblock... */ |
6f039cc4 | 6444 | wait_event(conf->wait_for_reshape, |
c91abf5a N |
6445 | atomic_read(&conf->reshape_stripes) == 0 |
6446 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); | |
6447 | if (atomic_read(&conf->reshape_stripes) != 0) | |
6448 | goto ret; | |
fef9c61f | 6449 | mddev->reshape_position = conf->reshape_progress; |
75d3da43 | 6450 | mddev->curr_resync_completed = sector_nr; |
db0505d3 N |
6451 | if (!mddev->reshape_backwards) |
6452 | /* Can update recovery_offset */ | |
6453 | rdev_for_each(rdev, mddev) | |
6454 | if (rdev->raid_disk >= 0 && | |
6455 | !test_bit(Journal, &rdev->flags) && | |
6456 | !test_bit(In_sync, &rdev->flags) && | |
6457 | rdev->recovery_offset < sector_nr) | |
6458 | rdev->recovery_offset = sector_nr; | |
c8f517c4 | 6459 | conf->reshape_checkpoint = jiffies; |
2953079c | 6460 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
c6207277 N |
6461 | md_wakeup_thread(mddev->thread); |
6462 | wait_event(mddev->sb_wait, | |
2953079c | 6463 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) |
c91abf5a N |
6464 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
6465 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
6466 | goto ret; | |
c6207277 | 6467 | spin_lock_irq(&conf->device_lock); |
fef9c61f | 6468 | conf->reshape_safe = mddev->reshape_position; |
c6207277 | 6469 | spin_unlock_irq(&conf->device_lock); |
6f039cc4 | 6470 | wake_up(&conf->wait_for_reshape); |
e1a86dbb | 6471 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
c6207277 | 6472 | } |
c91abf5a | 6473 | ret: |
92140480 | 6474 | return retn; |
52c03291 N |
6475 | } |
6476 | ||
849674e4 | 6477 | static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, |
bc49694a | 6478 | sector_t max_sector, int *skipped) |
52c03291 | 6479 | { |
d1688a6d | 6480 | struct r5conf *conf = mddev->private; |
52c03291 | 6481 | struct stripe_head *sh; |
57dab0bd | 6482 | sector_t sync_blocks; |
fe6a19d4 | 6483 | bool still_degraded = false; |
16a53ecc | 6484 | int i; |
1da177e4 | 6485 | |
72626685 | 6486 | if (sector_nr >= max_sector) { |
1da177e4 | 6487 | /* just being told to finish up .. nothing much to do */ |
cea9c228 | 6488 | |
29269553 N |
6489 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
6490 | end_reshape(conf); | |
6491 | return 0; | |
6492 | } | |
72626685 N |
6493 | |
6494 | if (mddev->curr_resync < max_sector) /* aborted */ | |
1415f402 YK |
6495 | mddev->bitmap_ops->end_sync(mddev, mddev->curr_resync, |
6496 | &sync_blocks); | |
16a53ecc | 6497 | else /* completed sync */ |
72626685 | 6498 | conf->fullsync = 0; |
077b18ab | 6499 | mddev->bitmap_ops->close_sync(mddev); |
72626685 | 6500 | |
1da177e4 LT |
6501 | return 0; |
6502 | } | |
ccfcc3c1 | 6503 | |
64bd660b | 6504 | /* Allow raid5_quiesce to complete */ |
6f039cc4 | 6505 | wait_event(conf->wait_for_reshape, conf->quiesce != 2); |
64bd660b | 6506 | |
52c03291 N |
6507 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
6508 | return reshape_request(mddev, sector_nr, skipped); | |
f6705578 | 6509 | |
c6207277 N |
6510 | /* No need to check resync_max as we never do more than one |
6511 | * stripe, and as resync_max will always be on a chunk boundary, | |
6512 | * if the check in md_do_sync didn't fire, there is no chance | |
6513 | * of overstepping resync_max here | |
6514 | */ | |
6515 | ||
16a53ecc | 6516 | /* if there is too many failed drives and we are trying |
1da177e4 LT |
6517 | * to resync, then assert that we are finished, because there is |
6518 | * nothing we can do. | |
6519 | */ | |
3285edf1 | 6520 | if (mddev->degraded >= conf->max_degraded && |
16a53ecc | 6521 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
58c0fed4 | 6522 | sector_t rv = mddev->dev_sectors - sector_nr; |
57afd89f | 6523 | *skipped = 1; |
1da177e4 LT |
6524 | return rv; |
6525 | } | |
6f608040 | 6526 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
6527 | !conf->fullsync && | |
fe6a19d4 YK |
6528 | !mddev->bitmap_ops->start_sync(mddev, sector_nr, &sync_blocks, |
6529 | true) && | |
c911c46c | 6530 | sync_blocks >= RAID5_STRIPE_SECTORS(conf)) { |
72626685 | 6531 | /* we can skip this block, and probably more */ |
83c3e5e1 | 6532 | do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf)); |
72626685 | 6533 | *skipped = 1; |
c911c46c YY |
6534 | /* keep things rounded to whole stripes */ |
6535 | return sync_blocks * RAID5_STRIPE_SECTORS(conf); | |
72626685 | 6536 | } |
1da177e4 | 6537 | |
15db1eca | 6538 | mddev->bitmap_ops->cond_end_sync(mddev, sector_nr, false); |
b47490c9 | 6539 | |
2f2d51ef LG |
6540 | sh = raid5_get_active_stripe(conf, NULL, sector_nr, |
6541 | R5_GAS_NOBLOCK); | |
1da177e4 | 6542 | if (sh == NULL) { |
2f2d51ef | 6543 | sh = raid5_get_active_stripe(conf, NULL, sector_nr, 0); |
1da177e4 | 6544 | /* make sure we don't swamp the stripe cache if someone else |
16a53ecc | 6545 | * is trying to get access |
1da177e4 | 6546 | */ |
66c006a5 | 6547 | schedule_timeout_uninterruptible(1); |
1da177e4 | 6548 | } |
16a53ecc | 6549 | /* Need to check if array will still be degraded after recovery/resync |
16d9cfab EM |
6550 | * Note in case of > 1 drive failures it's possible we're rebuilding |
6551 | * one drive while leaving another faulty drive in array. | |
16a53ecc | 6552 | */ |
16d9cfab | 6553 | for (i = 0; i < conf->raid_disks; i++) { |
ad860670 | 6554 | struct md_rdev *rdev = conf->disks[i].rdev; |
16d9cfab EM |
6555 | |
6556 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) | |
fe6a19d4 | 6557 | still_degraded = true; |
16d9cfab | 6558 | } |
16a53ecc | 6559 | |
fe6a19d4 YK |
6560 | mddev->bitmap_ops->start_sync(mddev, sector_nr, &sync_blocks, |
6561 | still_degraded); | |
16a53ecc | 6562 | |
83206d66 | 6563 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
053f5b65 | 6564 | set_bit(STRIPE_HANDLE, &sh->state); |
1da177e4 | 6565 | |
6d036f7d | 6566 | raid5_release_stripe(sh); |
1da177e4 | 6567 | |
c911c46c | 6568 | return RAID5_STRIPE_SECTORS(conf); |
1da177e4 LT |
6569 | } |
6570 | ||
0472a42b N |
6571 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio, |
6572 | unsigned int offset) | |
46031f9a RBJ |
6573 | { |
6574 | /* We may not be able to submit a whole bio at once as there | |
6575 | * may not be enough stripe_heads available. | |
6576 | * We cannot pre-allocate enough stripe_heads as we may need | |
6577 | * more than exist in the cache (if we allow ever large chunks). | |
6578 | * So we do one stripe head at a time and record in | |
6579 | * ->bi_hw_segments how many have been done. | |
6580 | * | |
6581 | * We *know* that this entire raid_bio is in one chunk, so | |
6582 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. | |
6583 | */ | |
6584 | struct stripe_head *sh; | |
911d4ee8 | 6585 | int dd_idx; |
46031f9a RBJ |
6586 | sector_t sector, logical_sector, last_sector; |
6587 | int scnt = 0; | |
46031f9a RBJ |
6588 | int handled = 0; |
6589 | ||
4f024f37 | 6590 | logical_sector = raid_bio->bi_iter.bi_sector & |
c911c46c | 6591 | ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); |
112bf897 | 6592 | sector = raid5_compute_sector(conf, logical_sector, |
911d4ee8 | 6593 | 0, &dd_idx, NULL); |
f73a1c7d | 6594 | last_sector = bio_end_sector(raid_bio); |
46031f9a RBJ |
6595 | |
6596 | for (; logical_sector < last_sector; | |
c911c46c YY |
6597 | logical_sector += RAID5_STRIPE_SECTORS(conf), |
6598 | sector += RAID5_STRIPE_SECTORS(conf), | |
387bb173 | 6599 | scnt++) { |
46031f9a | 6600 | |
0472a42b | 6601 | if (scnt < offset) |
46031f9a RBJ |
6602 | /* already done this stripe */ |
6603 | continue; | |
6604 | ||
2f2d51ef LG |
6605 | sh = raid5_get_active_stripe(conf, NULL, sector, |
6606 | R5_GAS_NOBLOCK | R5_GAS_NOQUIESCE); | |
46031f9a RBJ |
6607 | if (!sh) { |
6608 | /* failed to get a stripe - must wait */ | |
46031f9a | 6609 | conf->retry_read_aligned = raid_bio; |
0472a42b | 6610 | conf->retry_read_offset = scnt; |
46031f9a RBJ |
6611 | return handled; |
6612 | } | |
6613 | ||
da41ba65 | 6614 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { |
6d036f7d | 6615 | raid5_release_stripe(sh); |
387bb173 | 6616 | conf->retry_read_aligned = raid_bio; |
0472a42b | 6617 | conf->retry_read_offset = scnt; |
387bb173 NB |
6618 | return handled; |
6619 | } | |
6620 | ||
3f9e7c14 | 6621 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
36d1c647 | 6622 | handle_stripe(sh); |
6d036f7d | 6623 | raid5_release_stripe(sh); |
46031f9a RBJ |
6624 | handled++; |
6625 | } | |
016c76ac N |
6626 | |
6627 | bio_endio(raid_bio); | |
6628 | ||
46031f9a | 6629 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
b1b46486 | 6630 | wake_up(&conf->wait_for_quiescent); |
46031f9a RBJ |
6631 | return handled; |
6632 | } | |
6633 | ||
bfc90cb0 | 6634 | static int handle_active_stripes(struct r5conf *conf, int group, |
566c09c5 SL |
6635 | struct r5worker *worker, |
6636 | struct list_head *temp_inactive_list) | |
4631f39f | 6637 | __must_hold(&conf->device_lock) |
46a06401 SL |
6638 | { |
6639 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; | |
566c09c5 SL |
6640 | int i, batch_size = 0, hash; |
6641 | bool release_inactive = false; | |
46a06401 SL |
6642 | |
6643 | while (batch_size < MAX_STRIPE_BATCH && | |
851c30c9 | 6644 | (sh = __get_priority_stripe(conf, group)) != NULL) |
46a06401 SL |
6645 | batch[batch_size++] = sh; |
6646 | ||
566c09c5 SL |
6647 | if (batch_size == 0) { |
6648 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) | |
6649 | if (!list_empty(temp_inactive_list + i)) | |
6650 | break; | |
a8c34f91 SL |
6651 | if (i == NR_STRIPE_HASH_LOCKS) { |
6652 | spin_unlock_irq(&conf->device_lock); | |
1532d9e8 | 6653 | log_flush_stripe_to_raid(conf); |
a8c34f91 | 6654 | spin_lock_irq(&conf->device_lock); |
566c09c5 | 6655 | return batch_size; |
a8c34f91 | 6656 | } |
566c09c5 SL |
6657 | release_inactive = true; |
6658 | } | |
46a06401 SL |
6659 | spin_unlock_irq(&conf->device_lock); |
6660 | ||
566c09c5 SL |
6661 | release_inactive_stripe_list(conf, temp_inactive_list, |
6662 | NR_STRIPE_HASH_LOCKS); | |
6663 | ||
a8c34f91 | 6664 | r5l_flush_stripe_to_raid(conf->log); |
566c09c5 SL |
6665 | if (release_inactive) { |
6666 | spin_lock_irq(&conf->device_lock); | |
6667 | return 0; | |
6668 | } | |
6669 | ||
46a06401 SL |
6670 | for (i = 0; i < batch_size; i++) |
6671 | handle_stripe(batch[i]); | |
ff875738 | 6672 | log_write_stripe_run(conf); |
46a06401 SL |
6673 | |
6674 | cond_resched(); | |
6675 | ||
6676 | spin_lock_irq(&conf->device_lock); | |
566c09c5 SL |
6677 | for (i = 0; i < batch_size; i++) { |
6678 | hash = batch[i]->hash_lock_index; | |
6679 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); | |
6680 | } | |
46a06401 SL |
6681 | return batch_size; |
6682 | } | |
46031f9a | 6683 | |
851c30c9 SL |
6684 | static void raid5_do_work(struct work_struct *work) |
6685 | { | |
6686 | struct r5worker *worker = container_of(work, struct r5worker, work); | |
6687 | struct r5worker_group *group = worker->group; | |
6688 | struct r5conf *conf = group->conf; | |
16d997b7 | 6689 | struct mddev *mddev = conf->mddev; |
851c30c9 SL |
6690 | int group_id = group - conf->worker_groups; |
6691 | int handled; | |
6692 | struct blk_plug plug; | |
6693 | ||
6694 | pr_debug("+++ raid5worker active\n"); | |
6695 | ||
6696 | blk_start_plug(&plug); | |
6697 | handled = 0; | |
6698 | spin_lock_irq(&conf->device_lock); | |
6699 | while (1) { | |
6700 | int batch_size, released; | |
6701 | ||
566c09c5 | 6702 | released = release_stripe_list(conf, worker->temp_inactive_list); |
851c30c9 | 6703 | |
566c09c5 SL |
6704 | batch_size = handle_active_stripes(conf, group_id, worker, |
6705 | worker->temp_inactive_list); | |
bfc90cb0 | 6706 | worker->working = false; |
851c30c9 SL |
6707 | if (!batch_size && !released) |
6708 | break; | |
6709 | handled += batch_size; | |
16d997b7 N |
6710 | wait_event_lock_irq(mddev->sb_wait, |
6711 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags), | |
6712 | conf->device_lock); | |
851c30c9 SL |
6713 | } |
6714 | pr_debug("%d stripes handled\n", handled); | |
6715 | ||
6716 | spin_unlock_irq(&conf->device_lock); | |
7e96d559 | 6717 | |
9c72a18e SL |
6718 | flush_deferred_bios(conf); |
6719 | ||
6720 | r5l_flush_stripe_to_raid(conf->log); | |
6721 | ||
7e96d559 | 6722 | async_tx_issue_pending_all(); |
851c30c9 SL |
6723 | blk_finish_plug(&plug); |
6724 | ||
6725 | pr_debug("--- raid5worker inactive\n"); | |
6726 | } | |
6727 | ||
1da177e4 LT |
6728 | /* |
6729 | * This is our raid5 kernel thread. | |
6730 | * | |
6731 | * We scan the hash table for stripes which can be handled now. | |
6732 | * During the scan, completed stripes are saved for us by the interrupt | |
6733 | * handler, so that they will not have to wait for our next wakeup. | |
6734 | */ | |
4ed8731d | 6735 | static void raid5d(struct md_thread *thread) |
1da177e4 | 6736 | { |
4ed8731d | 6737 | struct mddev *mddev = thread->mddev; |
d1688a6d | 6738 | struct r5conf *conf = mddev->private; |
1da177e4 | 6739 | int handled; |
e1dfa0a2 | 6740 | struct blk_plug plug; |
1da177e4 | 6741 | |
45b4233c | 6742 | pr_debug("+++ raid5d active\n"); |
1da177e4 LT |
6743 | |
6744 | md_check_recovery(mddev); | |
1da177e4 | 6745 | |
e1dfa0a2 | 6746 | blk_start_plug(&plug); |
1da177e4 LT |
6747 | handled = 0; |
6748 | spin_lock_irq(&conf->device_lock); | |
6749 | while (1) { | |
46031f9a | 6750 | struct bio *bio; |
773ca82f | 6751 | int batch_size, released; |
0472a42b | 6752 | unsigned int offset; |
773ca82f | 6753 | |
151f66bb YK |
6754 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
6755 | break; | |
6756 | ||
566c09c5 | 6757 | released = release_stripe_list(conf, conf->temp_inactive_list); |
edbe83ab N |
6758 | if (released) |
6759 | clear_bit(R5_DID_ALLOC, &conf->cache_state); | |
1da177e4 | 6760 | |
0021b7bc | 6761 | if ( |
7c13edc8 N |
6762 | !list_empty(&conf->bitmap_list)) { |
6763 | /* Now is a good time to flush some bitmap updates */ | |
6764 | conf->seq_flush++; | |
700e432d | 6765 | spin_unlock_irq(&conf->device_lock); |
3c9883e7 | 6766 | mddev->bitmap_ops->unplug(mddev, true); |
700e432d | 6767 | spin_lock_irq(&conf->device_lock); |
7c13edc8 | 6768 | conf->seq_write = conf->seq_flush; |
566c09c5 | 6769 | activate_bit_delay(conf, conf->temp_inactive_list); |
72626685 | 6770 | } |
0021b7bc | 6771 | raid5_activate_delayed(conf); |
72626685 | 6772 | |
0472a42b | 6773 | while ((bio = remove_bio_from_retry(conf, &offset))) { |
46031f9a RBJ |
6774 | int ok; |
6775 | spin_unlock_irq(&conf->device_lock); | |
0472a42b | 6776 | ok = retry_aligned_read(conf, bio, offset); |
46031f9a RBJ |
6777 | spin_lock_irq(&conf->device_lock); |
6778 | if (!ok) | |
6779 | break; | |
6780 | handled++; | |
6781 | } | |
6782 | ||
566c09c5 SL |
6783 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
6784 | conf->temp_inactive_list); | |
773ca82f | 6785 | if (!batch_size && !released) |
1da177e4 | 6786 | break; |
46a06401 | 6787 | handled += batch_size; |
1da177e4 | 6788 | |
2953079c | 6789 | if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) { |
46a06401 | 6790 | spin_unlock_irq(&conf->device_lock); |
de393cde | 6791 | md_check_recovery(mddev); |
46a06401 SL |
6792 | spin_lock_irq(&conf->device_lock); |
6793 | } | |
1da177e4 | 6794 | } |
45b4233c | 6795 | pr_debug("%d stripes handled\n", handled); |
1da177e4 LT |
6796 | |
6797 | spin_unlock_irq(&conf->device_lock); | |
2d5b569b N |
6798 | if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && |
6799 | mutex_trylock(&conf->cache_size_mutex)) { | |
edbe83ab N |
6800 | grow_one_stripe(conf, __GFP_NOWARN); |
6801 | /* Set flag even if allocation failed. This helps | |
6802 | * slow down allocation requests when mem is short | |
6803 | */ | |
6804 | set_bit(R5_DID_ALLOC, &conf->cache_state); | |
2d5b569b | 6805 | mutex_unlock(&conf->cache_size_mutex); |
edbe83ab | 6806 | } |
1da177e4 | 6807 | |
765d704d SL |
6808 | flush_deferred_bios(conf); |
6809 | ||
0576b1c6 SL |
6810 | r5l_flush_stripe_to_raid(conf->log); |
6811 | ||
c9f21aaf | 6812 | async_tx_issue_pending_all(); |
e1dfa0a2 | 6813 | blk_finish_plug(&plug); |
1da177e4 | 6814 | |
45b4233c | 6815 | pr_debug("--- raid5d inactive\n"); |
1da177e4 LT |
6816 | } |
6817 | ||
3f294f4f | 6818 | static ssize_t |
fd01b88c | 6819 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
3f294f4f | 6820 | { |
7b1485ba N |
6821 | struct r5conf *conf; |
6822 | int ret = 0; | |
6823 | spin_lock(&mddev->lock); | |
6824 | conf = mddev->private; | |
96de1e66 | 6825 | if (conf) |
edbe83ab | 6826 | ret = sprintf(page, "%d\n", conf->min_nr_stripes); |
7b1485ba N |
6827 | spin_unlock(&mddev->lock); |
6828 | return ret; | |
3f294f4f N |
6829 | } |
6830 | ||
c41d4ac4 | 6831 | int |
fd01b88c | 6832 | raid5_set_cache_size(struct mddev *mddev, int size) |
3f294f4f | 6833 | { |
483cbbed | 6834 | int result = 0; |
d1688a6d | 6835 | struct r5conf *conf = mddev->private; |
b5470dc5 | 6836 | |
c41d4ac4 | 6837 | if (size <= 16 || size > 32768) |
3f294f4f | 6838 | return -EINVAL; |
486f0644 | 6839 | |
dfd2bf43 | 6840 | WRITE_ONCE(conf->min_nr_stripes, size); |
2d5b569b | 6841 | mutex_lock(&conf->cache_size_mutex); |
486f0644 N |
6842 | while (size < conf->max_nr_stripes && |
6843 | drop_one_stripe(conf)) | |
6844 | ; | |
2d5b569b | 6845 | mutex_unlock(&conf->cache_size_mutex); |
486f0644 | 6846 | |
2214c260 | 6847 | md_allow_write(mddev); |
486f0644 | 6848 | |
2d5b569b | 6849 | mutex_lock(&conf->cache_size_mutex); |
486f0644 | 6850 | while (size > conf->max_nr_stripes) |
483cbbed | 6851 | if (!grow_one_stripe(conf, GFP_KERNEL)) { |
dfd2bf43 | 6852 | WRITE_ONCE(conf->min_nr_stripes, conf->max_nr_stripes); |
483cbbed | 6853 | result = -ENOMEM; |
486f0644 | 6854 | break; |
483cbbed | 6855 | } |
2d5b569b | 6856 | mutex_unlock(&conf->cache_size_mutex); |
486f0644 | 6857 | |
483cbbed | 6858 | return result; |
c41d4ac4 N |
6859 | } |
6860 | EXPORT_SYMBOL(raid5_set_cache_size); | |
6861 | ||
6862 | static ssize_t | |
fd01b88c | 6863 | raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len) |
c41d4ac4 | 6864 | { |
6791875e | 6865 | struct r5conf *conf; |
c41d4ac4 N |
6866 | unsigned long new; |
6867 | int err; | |
6868 | ||
6869 | if (len >= PAGE_SIZE) | |
6870 | return -EINVAL; | |
b29bebd6 | 6871 | if (kstrtoul(page, 10, &new)) |
c41d4ac4 | 6872 | return -EINVAL; |
6791875e | 6873 | err = mddev_lock(mddev); |
c41d4ac4 N |
6874 | if (err) |
6875 | return err; | |
6791875e N |
6876 | conf = mddev->private; |
6877 | if (!conf) | |
6878 | err = -ENODEV; | |
6879 | else | |
6880 | err = raid5_set_cache_size(mddev, new); | |
6881 | mddev_unlock(mddev); | |
6882 | ||
6883 | return err ?: len; | |
3f294f4f | 6884 | } |
007583c9 | 6885 | |
96de1e66 N |
6886 | static struct md_sysfs_entry |
6887 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, | |
6888 | raid5_show_stripe_cache_size, | |
6889 | raid5_store_stripe_cache_size); | |
3f294f4f | 6890 | |
d06f191f MS |
6891 | static ssize_t |
6892 | raid5_show_rmw_level(struct mddev *mddev, char *page) | |
6893 | { | |
6894 | struct r5conf *conf = mddev->private; | |
6895 | if (conf) | |
6896 | return sprintf(page, "%d\n", conf->rmw_level); | |
6897 | else | |
6898 | return 0; | |
6899 | } | |
6900 | ||
6901 | static ssize_t | |
6902 | raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) | |
6903 | { | |
6904 | struct r5conf *conf = mddev->private; | |
6905 | unsigned long new; | |
6906 | ||
6907 | if (!conf) | |
6908 | return -ENODEV; | |
6909 | ||
6910 | if (len >= PAGE_SIZE) | |
6911 | return -EINVAL; | |
6912 | ||
6913 | if (kstrtoul(page, 10, &new)) | |
6914 | return -EINVAL; | |
6915 | ||
6916 | if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) | |
6917 | return -EINVAL; | |
6918 | ||
6919 | if (new != PARITY_DISABLE_RMW && | |
6920 | new != PARITY_ENABLE_RMW && | |
6921 | new != PARITY_PREFER_RMW) | |
6922 | return -EINVAL; | |
6923 | ||
6924 | conf->rmw_level = new; | |
6925 | return len; | |
6926 | } | |
6927 | ||
6928 | static struct md_sysfs_entry | |
6929 | raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, | |
6930 | raid5_show_rmw_level, | |
6931 | raid5_store_rmw_level); | |
6932 | ||
3b5408b9 YY |
6933 | static ssize_t |
6934 | raid5_show_stripe_size(struct mddev *mddev, char *page) | |
6935 | { | |
6936 | struct r5conf *conf; | |
6937 | int ret = 0; | |
6938 | ||
6939 | spin_lock(&mddev->lock); | |
6940 | conf = mddev->private; | |
6941 | if (conf) | |
6942 | ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf)); | |
6943 | spin_unlock(&mddev->lock); | |
6944 | return ret; | |
6945 | } | |
6946 | ||
6947 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE | |
6948 | static ssize_t | |
6949 | raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len) | |
6950 | { | |
6951 | struct r5conf *conf; | |
6952 | unsigned long new; | |
6953 | int err; | |
38912584 | 6954 | int size; |
3b5408b9 YY |
6955 | |
6956 | if (len >= PAGE_SIZE) | |
6957 | return -EINVAL; | |
6958 | if (kstrtoul(page, 10, &new)) | |
6959 | return -EINVAL; | |
6960 | ||
6961 | /* | |
6962 | * The value should not be bigger than PAGE_SIZE. It requires to | |
6af10a33 YY |
6963 | * be multiple of DEFAULT_STRIPE_SIZE and the value should be power |
6964 | * of two. | |
3b5408b9 | 6965 | */ |
6af10a33 YY |
6966 | if (new % DEFAULT_STRIPE_SIZE != 0 || |
6967 | new > PAGE_SIZE || new == 0 || | |
6968 | new != roundup_pow_of_two(new)) | |
3b5408b9 YY |
6969 | return -EINVAL; |
6970 | ||
e28ca92f | 6971 | err = mddev_suspend_and_lock(mddev); |
3b5408b9 YY |
6972 | if (err) |
6973 | return err; | |
6974 | ||
6975 | conf = mddev->private; | |
6976 | if (!conf) { | |
6977 | err = -ENODEV; | |
6978 | goto out_unlock; | |
6979 | } | |
6980 | ||
6981 | if (new == conf->stripe_size) | |
6982 | goto out_unlock; | |
6983 | ||
6984 | pr_debug("md/raid: change stripe_size from %lu to %lu\n", | |
6985 | conf->stripe_size, new); | |
6986 | ||
61c90765 YK |
6987 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
6988 | mddev->reshape_position != MaxSector || mddev->sysfs_active) { | |
38912584 YY |
6989 | err = -EBUSY; |
6990 | goto out_unlock; | |
6991 | } | |
6992 | ||
38912584 YY |
6993 | mutex_lock(&conf->cache_size_mutex); |
6994 | size = conf->max_nr_stripes; | |
6995 | ||
6996 | shrink_stripes(conf); | |
6997 | ||
3b5408b9 YY |
6998 | conf->stripe_size = new; |
6999 | conf->stripe_shift = ilog2(new) - 9; | |
7000 | conf->stripe_sectors = new >> 9; | |
38912584 YY |
7001 | if (grow_stripes(conf, size)) { |
7002 | pr_warn("md/raid:%s: couldn't allocate buffers\n", | |
7003 | mdname(mddev)); | |
7004 | err = -ENOMEM; | |
7005 | } | |
7006 | mutex_unlock(&conf->cache_size_mutex); | |
3b5408b9 YY |
7007 | |
7008 | out_unlock: | |
e28ca92f | 7009 | mddev_unlock_and_resume(mddev); |
3b5408b9 YY |
7010 | return err ?: len; |
7011 | } | |
7012 | ||
7013 | static struct md_sysfs_entry | |
7014 | raid5_stripe_size = __ATTR(stripe_size, 0644, | |
7015 | raid5_show_stripe_size, | |
7016 | raid5_store_stripe_size); | |
7017 | #else | |
7018 | static struct md_sysfs_entry | |
7019 | raid5_stripe_size = __ATTR(stripe_size, 0444, | |
7020 | raid5_show_stripe_size, | |
7021 | NULL); | |
7022 | #endif | |
d06f191f | 7023 | |
8b3e6cdc | 7024 | static ssize_t |
fd01b88c | 7025 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
8b3e6cdc | 7026 | { |
7b1485ba N |
7027 | struct r5conf *conf; |
7028 | int ret = 0; | |
7029 | spin_lock(&mddev->lock); | |
7030 | conf = mddev->private; | |
8b3e6cdc | 7031 | if (conf) |
7b1485ba N |
7032 | ret = sprintf(page, "%d\n", conf->bypass_threshold); |
7033 | spin_unlock(&mddev->lock); | |
7034 | return ret; | |
8b3e6cdc DW |
7035 | } |
7036 | ||
7037 | static ssize_t | |
fd01b88c | 7038 | raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len) |
8b3e6cdc | 7039 | { |
6791875e | 7040 | struct r5conf *conf; |
4ef197d8 | 7041 | unsigned long new; |
6791875e N |
7042 | int err; |
7043 | ||
8b3e6cdc DW |
7044 | if (len >= PAGE_SIZE) |
7045 | return -EINVAL; | |
b29bebd6 | 7046 | if (kstrtoul(page, 10, &new)) |
8b3e6cdc | 7047 | return -EINVAL; |
6791875e N |
7048 | |
7049 | err = mddev_lock(mddev); | |
7050 | if (err) | |
7051 | return err; | |
7052 | conf = mddev->private; | |
7053 | if (!conf) | |
7054 | err = -ENODEV; | |
edbe83ab | 7055 | else if (new > conf->min_nr_stripes) |
6791875e N |
7056 | err = -EINVAL; |
7057 | else | |
7058 | conf->bypass_threshold = new; | |
7059 | mddev_unlock(mddev); | |
7060 | return err ?: len; | |
8b3e6cdc DW |
7061 | } |
7062 | ||
7063 | static struct md_sysfs_entry | |
7064 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, | |
7065 | S_IRUGO | S_IWUSR, | |
7066 | raid5_show_preread_threshold, | |
7067 | raid5_store_preread_threshold); | |
7068 | ||
d592a996 SL |
7069 | static ssize_t |
7070 | raid5_show_skip_copy(struct mddev *mddev, char *page) | |
7071 | { | |
7b1485ba N |
7072 | struct r5conf *conf; |
7073 | int ret = 0; | |
7074 | spin_lock(&mddev->lock); | |
7075 | conf = mddev->private; | |
d592a996 | 7076 | if (conf) |
7b1485ba N |
7077 | ret = sprintf(page, "%d\n", conf->skip_copy); |
7078 | spin_unlock(&mddev->lock); | |
7079 | return ret; | |
d592a996 SL |
7080 | } |
7081 | ||
7082 | static ssize_t | |
7083 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) | |
7084 | { | |
6791875e | 7085 | struct r5conf *conf; |
d592a996 | 7086 | unsigned long new; |
6791875e N |
7087 | int err; |
7088 | ||
d592a996 SL |
7089 | if (len >= PAGE_SIZE) |
7090 | return -EINVAL; | |
d592a996 SL |
7091 | if (kstrtoul(page, 10, &new)) |
7092 | return -EINVAL; | |
7093 | new = !!new; | |
6791875e | 7094 | |
e28ca92f | 7095 | err = mddev_suspend_and_lock(mddev); |
6791875e N |
7096 | if (err) |
7097 | return err; | |
7098 | conf = mddev->private; | |
7099 | if (!conf) | |
7100 | err = -ENODEV; | |
7101 | else if (new != conf->skip_copy) { | |
396799eb | 7102 | struct request_queue *q = mddev->gendisk->queue; |
1a02f3a7 | 7103 | struct queue_limits lim = queue_limits_start_update(q); |
1cb039f3 | 7104 | |
6791875e N |
7105 | conf->skip_copy = new; |
7106 | if (new) | |
1a02f3a7 | 7107 | lim.features |= BLK_FEAT_STABLE_WRITES; |
6791875e | 7108 | else |
1a02f3a7 CH |
7109 | lim.features &= ~BLK_FEAT_STABLE_WRITES; |
7110 | err = queue_limits_commit_update(q, &lim); | |
6791875e | 7111 | } |
e28ca92f | 7112 | mddev_unlock_and_resume(mddev); |
6791875e | 7113 | return err ?: len; |
d592a996 SL |
7114 | } |
7115 | ||
7116 | static struct md_sysfs_entry | |
7117 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, | |
7118 | raid5_show_skip_copy, | |
7119 | raid5_store_skip_copy); | |
7120 | ||
3f294f4f | 7121 | static ssize_t |
fd01b88c | 7122 | stripe_cache_active_show(struct mddev *mddev, char *page) |
3f294f4f | 7123 | { |
d1688a6d | 7124 | struct r5conf *conf = mddev->private; |
96de1e66 N |
7125 | if (conf) |
7126 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); | |
7127 | else | |
7128 | return 0; | |
3f294f4f N |
7129 | } |
7130 | ||
96de1e66 N |
7131 | static struct md_sysfs_entry |
7132 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); | |
3f294f4f | 7133 | |
b721420e SL |
7134 | static ssize_t |
7135 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) | |
7136 | { | |
7b1485ba N |
7137 | struct r5conf *conf; |
7138 | int ret = 0; | |
7139 | spin_lock(&mddev->lock); | |
7140 | conf = mddev->private; | |
b721420e | 7141 | if (conf) |
7b1485ba N |
7142 | ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); |
7143 | spin_unlock(&mddev->lock); | |
7144 | return ret; | |
b721420e SL |
7145 | } |
7146 | ||
60aaf933 | 7147 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
7148 | int *group_cnt, | |
60aaf933 | 7149 | struct r5worker_group **worker_groups); |
b721420e SL |
7150 | static ssize_t |
7151 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) | |
7152 | { | |
6791875e | 7153 | struct r5conf *conf; |
7d5d7b50 | 7154 | unsigned int new; |
b721420e | 7155 | int err; |
60aaf933 | 7156 | struct r5worker_group *new_groups, *old_groups; |
d2c9ad41 | 7157 | int group_cnt; |
b721420e SL |
7158 | |
7159 | if (len >= PAGE_SIZE) | |
7160 | return -EINVAL; | |
7d5d7b50 SL |
7161 | if (kstrtouint(page, 10, &new)) |
7162 | return -EINVAL; | |
7163 | /* 8192 should be big enough */ | |
7164 | if (new > 8192) | |
b721420e SL |
7165 | return -EINVAL; |
7166 | ||
e28ca92f | 7167 | err = mddev_suspend_and_lock(mddev); |
6791875e N |
7168 | if (err) |
7169 | return err; | |
fa1944bb XN |
7170 | raid5_quiesce(mddev, true); |
7171 | ||
6791875e N |
7172 | conf = mddev->private; |
7173 | if (!conf) | |
7174 | err = -ENODEV; | |
7175 | else if (new != conf->worker_cnt_per_group) { | |
6791875e N |
7176 | old_groups = conf->worker_groups; |
7177 | if (old_groups) | |
7178 | flush_workqueue(raid5_wq); | |
d206dcfa | 7179 | |
d2c9ad41 | 7180 | err = alloc_thread_groups(conf, new, &group_cnt, &new_groups); |
6791875e N |
7181 | if (!err) { |
7182 | spin_lock_irq(&conf->device_lock); | |
7183 | conf->group_cnt = group_cnt; | |
d2c9ad41 | 7184 | conf->worker_cnt_per_group = new; |
6791875e N |
7185 | conf->worker_groups = new_groups; |
7186 | spin_unlock_irq(&conf->device_lock); | |
b721420e | 7187 | |
6791875e N |
7188 | if (old_groups) |
7189 | kfree(old_groups[0].workers); | |
7190 | kfree(old_groups); | |
7191 | } | |
b721420e | 7192 | } |
fa1944bb XN |
7193 | |
7194 | raid5_quiesce(mddev, false); | |
e28ca92f | 7195 | mddev_unlock_and_resume(mddev); |
b721420e | 7196 | |
6791875e | 7197 | return err ?: len; |
b721420e SL |
7198 | } |
7199 | ||
7200 | static struct md_sysfs_entry | |
7201 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, | |
7202 | raid5_show_group_thread_cnt, | |
7203 | raid5_store_group_thread_cnt); | |
7204 | ||
007583c9 | 7205 | static struct attribute *raid5_attrs[] = { |
3f294f4f N |
7206 | &raid5_stripecache_size.attr, |
7207 | &raid5_stripecache_active.attr, | |
8b3e6cdc | 7208 | &raid5_preread_bypass_threshold.attr, |
b721420e | 7209 | &raid5_group_thread_cnt.attr, |
d592a996 | 7210 | &raid5_skip_copy.attr, |
d06f191f | 7211 | &raid5_rmw_level.attr, |
3b5408b9 | 7212 | &raid5_stripe_size.attr, |
2c7da14b | 7213 | &r5c_journal_mode.attr, |
a596d086 | 7214 | &ppl_write_hint.attr, |
3f294f4f N |
7215 | NULL, |
7216 | }; | |
c32dc040 | 7217 | static const struct attribute_group raid5_attrs_group = { |
007583c9 N |
7218 | .name = NULL, |
7219 | .attrs = raid5_attrs, | |
3f294f4f N |
7220 | }; |
7221 | ||
d2c9ad41 | 7222 | static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt, |
60aaf933 | 7223 | struct r5worker_group **worker_groups) |
851c30c9 | 7224 | { |
566c09c5 | 7225 | int i, j, k; |
851c30c9 SL |
7226 | ssize_t size; |
7227 | struct r5worker *workers; | |
7228 | ||
851c30c9 | 7229 | if (cnt == 0) { |
60aaf933 | 7230 | *group_cnt = 0; |
7231 | *worker_groups = NULL; | |
851c30c9 SL |
7232 | return 0; |
7233 | } | |
60aaf933 | 7234 | *group_cnt = num_possible_nodes(); |
851c30c9 | 7235 | size = sizeof(struct r5worker) * cnt; |
6396bb22 KC |
7236 | workers = kcalloc(size, *group_cnt, GFP_NOIO); |
7237 | *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group), | |
7238 | GFP_NOIO); | |
60aaf933 | 7239 | if (!*worker_groups || !workers) { |
851c30c9 | 7240 | kfree(workers); |
60aaf933 | 7241 | kfree(*worker_groups); |
851c30c9 SL |
7242 | return -ENOMEM; |
7243 | } | |
7244 | ||
60aaf933 | 7245 | for (i = 0; i < *group_cnt; i++) { |
851c30c9 SL |
7246 | struct r5worker_group *group; |
7247 | ||
0c775d52 | 7248 | group = &(*worker_groups)[i]; |
851c30c9 | 7249 | INIT_LIST_HEAD(&group->handle_list); |
535ae4eb | 7250 | INIT_LIST_HEAD(&group->loprio_list); |
851c30c9 SL |
7251 | group->conf = conf; |
7252 | group->workers = workers + i * cnt; | |
7253 | ||
7254 | for (j = 0; j < cnt; j++) { | |
566c09c5 SL |
7255 | struct r5worker *worker = group->workers + j; |
7256 | worker->group = group; | |
7257 | INIT_WORK(&worker->work, raid5_do_work); | |
7258 | ||
7259 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) | |
7260 | INIT_LIST_HEAD(worker->temp_inactive_list + k); | |
851c30c9 SL |
7261 | } |
7262 | } | |
7263 | ||
7264 | return 0; | |
7265 | } | |
7266 | ||
7267 | static void free_thread_groups(struct r5conf *conf) | |
7268 | { | |
7269 | if (conf->worker_groups) | |
7270 | kfree(conf->worker_groups[0].workers); | |
7271 | kfree(conf->worker_groups); | |
7272 | conf->worker_groups = NULL; | |
7273 | } | |
7274 | ||
80c3a6ce | 7275 | static sector_t |
fd01b88c | 7276 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
80c3a6ce | 7277 | { |
d1688a6d | 7278 | struct r5conf *conf = mddev->private; |
80c3a6ce DW |
7279 | |
7280 | if (!sectors) | |
7281 | sectors = mddev->dev_sectors; | |
5e5e3e78 | 7282 | if (!raid_disks) |
7ec05478 | 7283 | /* size is defined by the smallest of previous and new size */ |
5e5e3e78 | 7284 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
80c3a6ce | 7285 | |
3cb5edf4 N |
7286 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
7287 | sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); | |
80c3a6ce DW |
7288 | return sectors * (raid_disks - conf->max_degraded); |
7289 | } | |
7290 | ||
789b5e03 ON |
7291 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
7292 | { | |
7293 | safe_put_page(percpu->spare_page); | |
789b5e03 | 7294 | percpu->spare_page = NULL; |
b330e6a4 | 7295 | kvfree(percpu->scribble); |
789b5e03 ON |
7296 | percpu->scribble = NULL; |
7297 | } | |
7298 | ||
7299 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) | |
7300 | { | |
b330e6a4 | 7301 | if (conf->level == 6 && !percpu->spare_page) { |
789b5e03 | 7302 | percpu->spare_page = alloc_page(GFP_KERNEL); |
b330e6a4 KO |
7303 | if (!percpu->spare_page) |
7304 | return -ENOMEM; | |
7305 | } | |
7306 | ||
7307 | if (scribble_alloc(percpu, | |
7308 | max(conf->raid_disks, | |
7309 | conf->previous_raid_disks), | |
7310 | max(conf->chunk_sectors, | |
7311 | conf->prev_chunk_sectors) | |
c911c46c | 7312 | / RAID5_STRIPE_SECTORS(conf))) { |
789b5e03 ON |
7313 | free_scratch_buffer(conf, percpu); |
7314 | return -ENOMEM; | |
7315 | } | |
7316 | ||
770b1d21 | 7317 | local_lock_init(&percpu->lock); |
789b5e03 ON |
7318 | return 0; |
7319 | } | |
7320 | ||
29c6d1bb | 7321 | static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) |
36d1c647 | 7322 | { |
29c6d1bb SAS |
7323 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
7324 | ||
7325 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); | |
7326 | return 0; | |
7327 | } | |
36d1c647 | 7328 | |
29c6d1bb SAS |
7329 | static void raid5_free_percpu(struct r5conf *conf) |
7330 | { | |
36d1c647 DW |
7331 | if (!conf->percpu) |
7332 | return; | |
7333 | ||
29c6d1bb | 7334 | cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
36d1c647 DW |
7335 | free_percpu(conf->percpu); |
7336 | } | |
7337 | ||
d1688a6d | 7338 | static void free_conf(struct r5conf *conf) |
95fc17aa | 7339 | { |
d7bd398e SL |
7340 | int i; |
7341 | ||
ff875738 AP |
7342 | log_exit(conf); |
7343 | ||
86298d8b | 7344 | shrinker_free(conf->shrinker); |
851c30c9 | 7345 | free_thread_groups(conf); |
95fc17aa | 7346 | shrink_stripes(conf); |
36d1c647 | 7347 | raid5_free_percpu(conf); |
d7bd398e SL |
7348 | for (i = 0; i < conf->pool_size; i++) |
7349 | if (conf->disks[i].extra_page) | |
7350 | put_page(conf->disks[i].extra_page); | |
95fc17aa | 7351 | kfree(conf->disks); |
afeee514 | 7352 | bioset_exit(&conf->bio_split); |
95fc17aa | 7353 | kfree(conf->stripe_hashtbl); |
aaf9f12e | 7354 | kfree(conf->pending_data); |
95fc17aa DW |
7355 | kfree(conf); |
7356 | } | |
7357 | ||
29c6d1bb | 7358 | static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) |
36d1c647 | 7359 | { |
29c6d1bb | 7360 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
36d1c647 DW |
7361 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
7362 | ||
29c6d1bb | 7363 | if (alloc_scratch_buffer(conf, percpu)) { |
cc6167b4 N |
7364 | pr_warn("%s: failed memory allocation for cpu%u\n", |
7365 | __func__, cpu); | |
29c6d1bb | 7366 | return -ENOMEM; |
36d1c647 | 7367 | } |
29c6d1bb | 7368 | return 0; |
36d1c647 | 7369 | } |
36d1c647 | 7370 | |
d1688a6d | 7371 | static int raid5_alloc_percpu(struct r5conf *conf) |
36d1c647 | 7372 | { |
789b5e03 | 7373 | int err = 0; |
36d1c647 | 7374 | |
789b5e03 ON |
7375 | conf->percpu = alloc_percpu(struct raid5_percpu); |
7376 | if (!conf->percpu) | |
36d1c647 | 7377 | return -ENOMEM; |
789b5e03 | 7378 | |
29c6d1bb | 7379 | err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
27a353c0 SL |
7380 | if (!err) { |
7381 | conf->scribble_disks = max(conf->raid_disks, | |
7382 | conf->previous_raid_disks); | |
7383 | conf->scribble_sectors = max(conf->chunk_sectors, | |
7384 | conf->prev_chunk_sectors); | |
7385 | } | |
36d1c647 DW |
7386 | return err; |
7387 | } | |
7388 | ||
edbe83ab N |
7389 | static unsigned long raid5_cache_scan(struct shrinker *shrink, |
7390 | struct shrink_control *sc) | |
7391 | { | |
86298d8b | 7392 | struct r5conf *conf = shrink->private_data; |
2d5b569b N |
7393 | unsigned long ret = SHRINK_STOP; |
7394 | ||
7395 | if (mutex_trylock(&conf->cache_size_mutex)) { | |
7396 | ret= 0; | |
49895bcc N |
7397 | while (ret < sc->nr_to_scan && |
7398 | conf->max_nr_stripes > conf->min_nr_stripes) { | |
2d5b569b N |
7399 | if (drop_one_stripe(conf) == 0) { |
7400 | ret = SHRINK_STOP; | |
7401 | break; | |
7402 | } | |
7403 | ret++; | |
7404 | } | |
7405 | mutex_unlock(&conf->cache_size_mutex); | |
edbe83ab N |
7406 | } |
7407 | return ret; | |
7408 | } | |
7409 | ||
7410 | static unsigned long raid5_cache_count(struct shrinker *shrink, | |
7411 | struct shrink_control *sc) | |
7412 | { | |
86298d8b | 7413 | struct r5conf *conf = shrink->private_data; |
dfd2bf43 GDH |
7414 | int max_stripes = READ_ONCE(conf->max_nr_stripes); |
7415 | int min_stripes = READ_ONCE(conf->min_nr_stripes); | |
edbe83ab | 7416 | |
dfd2bf43 | 7417 | if (max_stripes < min_stripes) |
edbe83ab N |
7418 | /* unlikely, but not impossible */ |
7419 | return 0; | |
dfd2bf43 | 7420 | return max_stripes - min_stripes; |
edbe83ab N |
7421 | } |
7422 | ||
d1688a6d | 7423 | static struct r5conf *setup_conf(struct mddev *mddev) |
1da177e4 | 7424 | { |
d1688a6d | 7425 | struct r5conf *conf; |
5e5e3e78 | 7426 | int raid_disk, memory, max_disks; |
3cb03002 | 7427 | struct md_rdev *rdev; |
1da177e4 | 7428 | struct disk_info *disk; |
0232605d | 7429 | char pers_name[6]; |
566c09c5 | 7430 | int i; |
d2c9ad41 | 7431 | int group_cnt; |
60aaf933 | 7432 | struct r5worker_group *new_group; |
8fbcba6b | 7433 | int ret = -ENOMEM; |
1da177e4 | 7434 | |
91adb564 N |
7435 | if (mddev->new_level != 5 |
7436 | && mddev->new_level != 4 | |
7437 | && mddev->new_level != 6) { | |
cc6167b4 N |
7438 | pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
7439 | mdname(mddev), mddev->new_level); | |
91adb564 | 7440 | return ERR_PTR(-EIO); |
1da177e4 | 7441 | } |
91adb564 N |
7442 | if ((mddev->new_level == 5 |
7443 | && !algorithm_valid_raid5(mddev->new_layout)) || | |
7444 | (mddev->new_level == 6 | |
7445 | && !algorithm_valid_raid6(mddev->new_layout))) { | |
cc6167b4 N |
7446 | pr_warn("md/raid:%s: layout %d not supported\n", |
7447 | mdname(mddev), mddev->new_layout); | |
91adb564 | 7448 | return ERR_PTR(-EIO); |
99c0fb5f | 7449 | } |
91adb564 | 7450 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
cc6167b4 N |
7451 | pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
7452 | mdname(mddev), mddev->raid_disks); | |
91adb564 | 7453 | return ERR_PTR(-EINVAL); |
4bbf3771 N |
7454 | } |
7455 | ||
664e7c41 AN |
7456 | if (!mddev->new_chunk_sectors || |
7457 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || | |
7458 | !is_power_of_2(mddev->new_chunk_sectors)) { | |
cc6167b4 N |
7459 | pr_warn("md/raid:%s: invalid chunk size %d\n", |
7460 | mdname(mddev), mddev->new_chunk_sectors << 9); | |
91adb564 | 7461 | return ERR_PTR(-EINVAL); |
f6705578 N |
7462 | } |
7463 | ||
d1688a6d | 7464 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
91adb564 | 7465 | if (conf == NULL) |
1da177e4 | 7466 | goto abort; |
c911c46c | 7467 | |
e2368582 YY |
7468 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
7469 | conf->stripe_size = DEFAULT_STRIPE_SIZE; | |
7470 | conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9; | |
7471 | conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9; | |
7472 | #endif | |
aaf9f12e SL |
7473 | INIT_LIST_HEAD(&conf->free_list); |
7474 | INIT_LIST_HEAD(&conf->pending_list); | |
6396bb22 KC |
7475 | conf->pending_data = kcalloc(PENDING_IO_MAX, |
7476 | sizeof(struct r5pending_data), | |
7477 | GFP_KERNEL); | |
aaf9f12e SL |
7478 | if (!conf->pending_data) |
7479 | goto abort; | |
7480 | for (i = 0; i < PENDING_IO_MAX; i++) | |
7481 | list_add(&conf->pending_data[i].sibling, &conf->free_list); | |
851c30c9 | 7482 | /* Don't enable multi-threading by default*/ |
d2c9ad41 | 7483 | if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) { |
60aaf933 | 7484 | conf->group_cnt = group_cnt; |
d2c9ad41 | 7485 | conf->worker_cnt_per_group = 0; |
60aaf933 | 7486 | conf->worker_groups = new_group; |
7487 | } else | |
851c30c9 | 7488 | goto abort; |
f5efd45a | 7489 | spin_lock_init(&conf->device_lock); |
0a87b25f | 7490 | seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock); |
2d5b569b | 7491 | mutex_init(&conf->cache_size_mutex); |
8fbcba6b | 7492 | |
b1b46486 | 7493 | init_waitqueue_head(&conf->wait_for_quiescent); |
6ab2a4b8 | 7494 | init_waitqueue_head(&conf->wait_for_stripe); |
6f039cc4 | 7495 | init_waitqueue_head(&conf->wait_for_reshape); |
f5efd45a | 7496 | INIT_LIST_HEAD(&conf->handle_list); |
535ae4eb | 7497 | INIT_LIST_HEAD(&conf->loprio_list); |
f5efd45a DW |
7498 | INIT_LIST_HEAD(&conf->hold_list); |
7499 | INIT_LIST_HEAD(&conf->delayed_list); | |
7500 | INIT_LIST_HEAD(&conf->bitmap_list); | |
773ca82f | 7501 | init_llist_head(&conf->released_stripes); |
f5efd45a DW |
7502 | atomic_set(&conf->active_stripes, 0); |
7503 | atomic_set(&conf->preread_active_stripes, 0); | |
7504 | atomic_set(&conf->active_aligned_reads, 0); | |
765d704d SL |
7505 | spin_lock_init(&conf->pending_bios_lock); |
7506 | conf->batch_bio_dispatch = true; | |
7507 | rdev_for_each(rdev, mddev) { | |
7508 | if (test_bit(Journal, &rdev->flags)) | |
7509 | continue; | |
10f0d2a5 | 7510 | if (bdev_nonrot(rdev->bdev)) { |
765d704d SL |
7511 | conf->batch_bio_dispatch = false; |
7512 | break; | |
7513 | } | |
7514 | } | |
7515 | ||
f5efd45a | 7516 | conf->bypass_threshold = BYPASS_THRESHOLD; |
d890fa2b | 7517 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
91adb564 N |
7518 | |
7519 | conf->raid_disks = mddev->raid_disks; | |
7520 | if (mddev->reshape_position == MaxSector) | |
7521 | conf->previous_raid_disks = mddev->raid_disks; | |
7522 | else | |
f6705578 | 7523 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
5e5e3e78 | 7524 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
f6705578 | 7525 | |
6396bb22 | 7526 | conf->disks = kcalloc(max_disks, sizeof(struct disk_info), |
b55e6bfc | 7527 | GFP_KERNEL); |
d7bd398e | 7528 | |
b55e6bfc N |
7529 | if (!conf->disks) |
7530 | goto abort; | |
9ffae0cf | 7531 | |
d7bd398e SL |
7532 | for (i = 0; i < max_disks; i++) { |
7533 | conf->disks[i].extra_page = alloc_page(GFP_KERNEL); | |
7534 | if (!conf->disks[i].extra_page) | |
7535 | goto abort; | |
7536 | } | |
7537 | ||
afeee514 KO |
7538 | ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); |
7539 | if (ret) | |
dd7a8f5d | 7540 | goto abort; |
1da177e4 LT |
7541 | conf->mddev = mddev; |
7542 | ||
5f7ef487 DC |
7543 | ret = -ENOMEM; |
7544 | conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
7545 | if (!conf->stripe_hashtbl) | |
1da177e4 | 7546 | goto abort; |
1da177e4 | 7547 | |
566c09c5 SL |
7548 | /* We init hash_locks[0] separately to that it can be used |
7549 | * as the reference lock in the spin_lock_nest_lock() call | |
7550 | * in lock_all_device_hash_locks_irq in order to convince | |
7551 | * lockdep that we know what we are doing. | |
7552 | */ | |
7553 | spin_lock_init(conf->hash_locks); | |
7554 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) | |
7555 | spin_lock_init(conf->hash_locks + i); | |
7556 | ||
7557 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) | |
7558 | INIT_LIST_HEAD(conf->inactive_list + i); | |
7559 | ||
7560 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) | |
7561 | INIT_LIST_HEAD(conf->temp_inactive_list + i); | |
7562 | ||
1e6d690b SL |
7563 | atomic_set(&conf->r5c_cached_full_stripes, 0); |
7564 | INIT_LIST_HEAD(&conf->r5c_full_stripe_list); | |
7565 | atomic_set(&conf->r5c_cached_partial_stripes, 0); | |
7566 | INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); | |
e33fbb9c SL |
7567 | atomic_set(&conf->r5c_flushing_full_stripes, 0); |
7568 | atomic_set(&conf->r5c_flushing_partial_stripes, 0); | |
1e6d690b | 7569 | |
36d1c647 | 7570 | conf->level = mddev->new_level; |
46d5b785 | 7571 | conf->chunk_sectors = mddev->new_chunk_sectors; |
8fbcba6b LG |
7572 | ret = raid5_alloc_percpu(conf); |
7573 | if (ret) | |
36d1c647 DW |
7574 | goto abort; |
7575 | ||
0c55e022 | 7576 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
1da177e4 | 7577 | |
8fbcba6b | 7578 | ret = -EIO; |
dafb20fa | 7579 | rdev_for_each(rdev, mddev) { |
1da177e4 | 7580 | raid_disk = rdev->raid_disk; |
5e5e3e78 | 7581 | if (raid_disk >= max_disks |
f2076e7d | 7582 | || raid_disk < 0 || test_bit(Journal, &rdev->flags)) |
1da177e4 LT |
7583 | continue; |
7584 | disk = conf->disks + raid_disk; | |
7585 | ||
17045f52 N |
7586 | if (test_bit(Replacement, &rdev->flags)) { |
7587 | if (disk->replacement) | |
7588 | goto abort; | |
2314c2e3 | 7589 | disk->replacement = rdev; |
17045f52 N |
7590 | } else { |
7591 | if (disk->rdev) | |
7592 | goto abort; | |
2314c2e3 | 7593 | disk->rdev = rdev; |
17045f52 | 7594 | } |
1da177e4 | 7595 | |
b2d444d7 | 7596 | if (test_bit(In_sync, &rdev->flags)) { |
913cce5a CH |
7597 | pr_info("md/raid:%s: device %pg operational as raid disk %d\n", |
7598 | mdname(mddev), rdev->bdev, raid_disk); | |
d6b212f4 | 7599 | } else if (rdev->saved_raid_disk != raid_disk) |
8c2e870a NB |
7600 | /* Cannot rely on bitmap to complete recovery */ |
7601 | conf->fullsync = 1; | |
1da177e4 LT |
7602 | } |
7603 | ||
91adb564 | 7604 | conf->level = mddev->new_level; |
584acdd4 | 7605 | if (conf->level == 6) { |
16a53ecc | 7606 | conf->max_degraded = 2; |
584acdd4 MS |
7607 | if (raid6_call.xor_syndrome) |
7608 | conf->rmw_level = PARITY_ENABLE_RMW; | |
7609 | else | |
7610 | conf->rmw_level = PARITY_DISABLE_RMW; | |
7611 | } else { | |
16a53ecc | 7612 | conf->max_degraded = 1; |
584acdd4 MS |
7613 | conf->rmw_level = PARITY_ENABLE_RMW; |
7614 | } | |
91adb564 | 7615 | conf->algorithm = mddev->new_layout; |
fef9c61f | 7616 | conf->reshape_progress = mddev->reshape_position; |
e183eaed | 7617 | if (conf->reshape_progress != MaxSector) { |
09c9e5fa | 7618 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
e183eaed | 7619 | conf->prev_algo = mddev->layout; |
5cac6bcb N |
7620 | } else { |
7621 | conf->prev_chunk_sectors = conf->chunk_sectors; | |
7622 | conf->prev_algo = conf->algorithm; | |
e183eaed | 7623 | } |
1da177e4 | 7624 | |
edbe83ab | 7625 | conf->min_nr_stripes = NR_STRIPES; |
ad5b0f76 SL |
7626 | if (mddev->reshape_position != MaxSector) { |
7627 | int stripes = max_t(int, | |
c911c46c YY |
7628 | ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4, |
7629 | ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4); | |
ad5b0f76 SL |
7630 | conf->min_nr_stripes = max(NR_STRIPES, stripes); |
7631 | if (conf->min_nr_stripes != NR_STRIPES) | |
cc6167b4 | 7632 | pr_info("md/raid:%s: force stripe size %d for reshape\n", |
ad5b0f76 SL |
7633 | mdname(mddev), conf->min_nr_stripes); |
7634 | } | |
edbe83ab | 7635 | memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + |
5e5e3e78 | 7636 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
4bda556a | 7637 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
edbe83ab | 7638 | if (grow_stripes(conf, conf->min_nr_stripes)) { |
cc6167b4 N |
7639 | pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", |
7640 | mdname(mddev), memory); | |
8fbcba6b | 7641 | ret = -ENOMEM; |
91adb564 N |
7642 | goto abort; |
7643 | } else | |
cc6167b4 | 7644 | pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); |
edbe83ab N |
7645 | /* |
7646 | * Losing a stripe head costs more than the time to refill it, | |
7647 | * it reduces the queue depth and so can hurt throughput. | |
7648 | * So set it rather large, scaled by number of devices. | |
7649 | */ | |
86298d8b QZ |
7650 | conf->shrinker = shrinker_alloc(0, "md-raid5:%s", mdname(mddev)); |
7651 | if (!conf->shrinker) { | |
7652 | ret = -ENOMEM; | |
7653 | pr_warn("md/raid:%s: couldn't allocate shrinker.\n", | |
cc6167b4 | 7654 | mdname(mddev)); |
6a0f53ff CY |
7655 | goto abort; |
7656 | } | |
1da177e4 | 7657 | |
86298d8b QZ |
7658 | conf->shrinker->seeks = DEFAULT_SEEKS * conf->raid_disks * 4; |
7659 | conf->shrinker->scan_objects = raid5_cache_scan; | |
7660 | conf->shrinker->count_objects = raid5_cache_count; | |
7661 | conf->shrinker->batch = 128; | |
7662 | conf->shrinker->private_data = conf; | |
7663 | ||
7664 | shrinker_register(conf->shrinker); | |
7665 | ||
0232605d | 7666 | sprintf(pers_name, "raid%d", mddev->new_level); |
44693154 YK |
7667 | rcu_assign_pointer(conf->thread, |
7668 | md_register_thread(raid5d, mddev, pers_name)); | |
91adb564 | 7669 | if (!conf->thread) { |
cc6167b4 N |
7670 | pr_warn("md/raid:%s: couldn't allocate thread.\n", |
7671 | mdname(mddev)); | |
8fbcba6b | 7672 | ret = -ENOMEM; |
16a53ecc N |
7673 | goto abort; |
7674 | } | |
91adb564 N |
7675 | |
7676 | return conf; | |
7677 | ||
7678 | abort: | |
8fbcba6b | 7679 | if (conf) |
95fc17aa | 7680 | free_conf(conf); |
8fbcba6b | 7681 | return ERR_PTR(ret); |
91adb564 N |
7682 | } |
7683 | ||
c148ffdc N |
7684 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
7685 | { | |
7686 | switch (algo) { | |
7687 | case ALGORITHM_PARITY_0: | |
7688 | if (raid_disk < max_degraded) | |
7689 | return 1; | |
7690 | break; | |
7691 | case ALGORITHM_PARITY_N: | |
7692 | if (raid_disk >= raid_disks - max_degraded) | |
7693 | return 1; | |
7694 | break; | |
7695 | case ALGORITHM_PARITY_0_6: | |
f72ffdd6 | 7696 | if (raid_disk == 0 || |
c148ffdc N |
7697 | raid_disk == raid_disks - 1) |
7698 | return 1; | |
7699 | break; | |
7700 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
7701 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
7702 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
7703 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
7704 | if (raid_disk == raid_disks - 1) | |
7705 | return 1; | |
7706 | } | |
7707 | return 0; | |
7708 | } | |
7709 | ||
f63f1735 | 7710 | static int raid5_set_limits(struct mddev *mddev) |
16ef5101 | 7711 | { |
f63f1735 CH |
7712 | struct r5conf *conf = mddev->private; |
7713 | struct queue_limits lim; | |
7714 | int data_disks, stripe; | |
7715 | struct md_rdev *rdev; | |
7716 | ||
7717 | /* | |
7718 | * The read-ahead size must cover two whole stripes, which is | |
7719 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices. | |
7720 | */ | |
7721 | data_disks = conf->previous_raid_disks - conf->max_degraded; | |
7722 | ||
7723 | /* | |
7724 | * We can only discard a whole stripe. It doesn't make sense to | |
7725 | * discard data disk but write parity disk | |
7726 | */ | |
7727 | stripe = roundup_pow_of_two(data_disks * (mddev->chunk_sectors << 9)); | |
7728 | ||
573d5abf | 7729 | md_init_stacking_limits(&lim); |
f63f1735 CH |
7730 | lim.io_min = mddev->chunk_sectors << 9; |
7731 | lim.io_opt = lim.io_min * (conf->raid_disks - conf->max_degraded); | |
7d4dec52 | 7732 | lim.features |= BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE; |
f63f1735 CH |
7733 | lim.discard_granularity = stripe; |
7734 | lim.max_write_zeroes_sectors = 0; | |
c6e56cf6 | 7735 | mddev_stack_rdev_limits(mddev, &lim, 0); |
f63f1735 CH |
7736 | rdev_for_each(rdev, mddev) |
7737 | queue_limits_stack_bdev(&lim, rdev->bdev, rdev->new_data_offset, | |
7738 | mddev->gendisk->disk_name); | |
7739 | ||
7740 | /* | |
7741 | * Zeroing is required for discard, otherwise data could be lost. | |
7742 | * | |
7743 | * Consider a scenario: discard a stripe (the stripe could be | |
7744 | * inconsistent if discard_zeroes_data is 0); write one disk of the | |
7745 | * stripe (the stripe could be inconsistent again depending on which | |
7746 | * disks are used to calculate parity); the disk is broken; The stripe | |
7747 | * data of this disk is lost. | |
7748 | * | |
7749 | * We only allow DISCARD if the sysadmin has confirmed that only safe | |
7750 | * devices are in use by setting a module parameter. A better idea | |
7751 | * might be to turn DISCARD into WRITE_ZEROES requests, as that is | |
7752 | * required to be safe. | |
7753 | */ | |
7754 | if (!devices_handle_discard_safely || | |
7755 | lim.max_discard_sectors < (stripe >> 9) || | |
7756 | lim.discard_granularity < stripe) | |
7757 | lim.max_hw_discard_sectors = 0; | |
7758 | ||
7759 | /* | |
7760 | * Requests require having a bitmap for each stripe. | |
7761 | * Limit the max sectors based on this. | |
7762 | */ | |
7763 | lim.max_hw_sectors = RAID5_MAX_REQ_STRIPES << RAID5_STRIPE_SHIFT(conf); | |
7764 | ||
7765 | /* No restrictions on the number of segments in the request */ | |
7766 | lim.max_segments = USHRT_MAX; | |
7767 | ||
396799eb | 7768 | return queue_limits_set(mddev->gendisk->queue, &lim); |
16ef5101 CH |
7769 | } |
7770 | ||
849674e4 | 7771 | static int raid5_run(struct mddev *mddev) |
91adb564 | 7772 | { |
d1688a6d | 7773 | struct r5conf *conf; |
c148ffdc | 7774 | int dirty_parity_disks = 0; |
3cb03002 | 7775 | struct md_rdev *rdev; |
713cf5a6 | 7776 | struct md_rdev *journal_dev = NULL; |
c148ffdc | 7777 | sector_t reshape_offset = 0; |
c567c86b | 7778 | int i; |
b5254dd5 N |
7779 | long long min_offset_diff = 0; |
7780 | int first = 1; | |
f63f1735 | 7781 | int ret = -EIO; |
91adb564 | 7782 | |
8c6ac868 | 7783 | if (mddev->recovery_cp != MaxSector) |
cc6167b4 N |
7784 | pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", |
7785 | mdname(mddev)); | |
b5254dd5 N |
7786 | |
7787 | rdev_for_each(rdev, mddev) { | |
7788 | long long diff; | |
713cf5a6 | 7789 | |
f2076e7d | 7790 | if (test_bit(Journal, &rdev->flags)) { |
713cf5a6 | 7791 | journal_dev = rdev; |
f2076e7d SL |
7792 | continue; |
7793 | } | |
b5254dd5 N |
7794 | if (rdev->raid_disk < 0) |
7795 | continue; | |
7796 | diff = (rdev->new_data_offset - rdev->data_offset); | |
7797 | if (first) { | |
7798 | min_offset_diff = diff; | |
7799 | first = 0; | |
7800 | } else if (mddev->reshape_backwards && | |
7801 | diff < min_offset_diff) | |
7802 | min_offset_diff = diff; | |
7803 | else if (!mddev->reshape_backwards && | |
7804 | diff > min_offset_diff) | |
7805 | min_offset_diff = diff; | |
7806 | } | |
7807 | ||
230b55fa N |
7808 | if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) && |
7809 | (mddev->bitmap_info.offset || mddev->bitmap_info.file)) { | |
7810 | pr_notice("md/raid:%s: array cannot have both journal and bitmap\n", | |
7811 | mdname(mddev)); | |
c567c86b | 7812 | return -EINVAL; |
230b55fa N |
7813 | } |
7814 | ||
91adb564 N |
7815 | if (mddev->reshape_position != MaxSector) { |
7816 | /* Check that we can continue the reshape. | |
b5254dd5 N |
7817 | * Difficulties arise if the stripe we would write to |
7818 | * next is at or after the stripe we would read from next. | |
7819 | * For a reshape that changes the number of devices, this | |
7820 | * is only possible for a very short time, and mdadm makes | |
7821 | * sure that time appears to have past before assembling | |
7822 | * the array. So we fail if that time hasn't passed. | |
7823 | * For a reshape that keeps the number of devices the same | |
7824 | * mdadm must be monitoring the reshape can keeping the | |
7825 | * critical areas read-only and backed up. It will start | |
7826 | * the array in read-only mode, so we check for that. | |
91adb564 N |
7827 | */ |
7828 | sector_t here_new, here_old; | |
7829 | int old_disks; | |
18b00334 | 7830 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
05256d98 N |
7831 | int chunk_sectors; |
7832 | int new_data_disks; | |
91adb564 | 7833 | |
713cf5a6 | 7834 | if (journal_dev) { |
cc6167b4 N |
7835 | pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", |
7836 | mdname(mddev)); | |
c567c86b | 7837 | return -EINVAL; |
713cf5a6 SL |
7838 | } |
7839 | ||
88ce4930 | 7840 | if (mddev->new_level != mddev->level) { |
cc6167b4 N |
7841 | pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", |
7842 | mdname(mddev)); | |
c567c86b | 7843 | return -EINVAL; |
91adb564 | 7844 | } |
91adb564 N |
7845 | old_disks = mddev->raid_disks - mddev->delta_disks; |
7846 | /* reshape_position must be on a new-stripe boundary, and one | |
7847 | * further up in new geometry must map after here in old | |
7848 | * geometry. | |
05256d98 N |
7849 | * If the chunk sizes are different, then as we perform reshape |
7850 | * in units of the largest of the two, reshape_position needs | |
7851 | * be a multiple of the largest chunk size times new data disks. | |
91adb564 N |
7852 | */ |
7853 | here_new = mddev->reshape_position; | |
05256d98 N |
7854 | chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); |
7855 | new_data_disks = mddev->raid_disks - max_degraded; | |
7856 | if (sector_div(here_new, chunk_sectors * new_data_disks)) { | |
cc6167b4 N |
7857 | pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", |
7858 | mdname(mddev)); | |
c567c86b | 7859 | return -EINVAL; |
91adb564 | 7860 | } |
05256d98 | 7861 | reshape_offset = here_new * chunk_sectors; |
91adb564 N |
7862 | /* here_new is the stripe we will write to */ |
7863 | here_old = mddev->reshape_position; | |
05256d98 | 7864 | sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); |
91adb564 N |
7865 | /* here_old is the first stripe that we might need to read |
7866 | * from */ | |
67ac6011 N |
7867 | if (mddev->delta_disks == 0) { |
7868 | /* We cannot be sure it is safe to start an in-place | |
b5254dd5 | 7869 | * reshape. It is only safe if user-space is monitoring |
67ac6011 N |
7870 | * and taking constant backups. |
7871 | * mdadm always starts a situation like this in | |
7872 | * readonly mode so it can take control before | |
7873 | * allowing any writes. So just check for that. | |
7874 | */ | |
b5254dd5 N |
7875 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
7876 | abs(min_offset_diff) >= mddev->new_chunk_sectors) | |
7877 | /* not really in-place - so OK */; | |
7878 | else if (mddev->ro == 0) { | |
cc6167b4 N |
7879 | pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", |
7880 | mdname(mddev)); | |
c567c86b | 7881 | return -EINVAL; |
67ac6011 | 7882 | } |
2c810cdd | 7883 | } else if (mddev->reshape_backwards |
05256d98 N |
7884 | ? (here_new * chunk_sectors + min_offset_diff <= |
7885 | here_old * chunk_sectors) | |
7886 | : (here_new * chunk_sectors >= | |
7887 | here_old * chunk_sectors + (-min_offset_diff))) { | |
91adb564 | 7888 | /* Reading from the same stripe as writing to - bad */ |
cc6167b4 N |
7889 | pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", |
7890 | mdname(mddev)); | |
c567c86b | 7891 | return -EINVAL; |
91adb564 | 7892 | } |
cc6167b4 | 7893 | pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); |
91adb564 N |
7894 | /* OK, we should be able to continue; */ |
7895 | } else { | |
7896 | BUG_ON(mddev->level != mddev->new_level); | |
7897 | BUG_ON(mddev->layout != mddev->new_layout); | |
664e7c41 | 7898 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
91adb564 | 7899 | BUG_ON(mddev->delta_disks != 0); |
1da177e4 | 7900 | } |
91adb564 | 7901 | |
3418d036 AP |
7902 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && |
7903 | test_bit(MD_HAS_PPL, &mddev->flags)) { | |
7904 | pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n", | |
7905 | mdname(mddev)); | |
7906 | clear_bit(MD_HAS_PPL, &mddev->flags); | |
ddc08823 | 7907 | clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags); |
3418d036 AP |
7908 | } |
7909 | ||
245f46c2 N |
7910 | if (mddev->private == NULL) |
7911 | conf = setup_conf(mddev); | |
7912 | else | |
7913 | conf = mddev->private; | |
7914 | ||
c567c86b YK |
7915 | if (IS_ERR(conf)) |
7916 | return PTR_ERR(conf); | |
91adb564 | 7917 | |
486b0f7b SL |
7918 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
7919 | if (!journal_dev) { | |
cc6167b4 N |
7920 | pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", |
7921 | mdname(mddev)); | |
486b0f7b SL |
7922 | mddev->ro = 1; |
7923 | set_disk_ro(mddev->gendisk, 1); | |
7924 | } else if (mddev->recovery_cp == MaxSector) | |
7925 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); | |
7dde2ad3 SL |
7926 | } |
7927 | ||
b5254dd5 | 7928 | conf->min_offset_diff = min_offset_diff; |
44693154 YK |
7929 | rcu_assign_pointer(mddev->thread, conf->thread); |
7930 | rcu_assign_pointer(conf->thread, NULL); | |
91adb564 N |
7931 | mddev->private = conf; |
7932 | ||
17045f52 N |
7933 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
7934 | i++) { | |
ad860670 | 7935 | rdev = conf->disks[i].rdev; |
17045f52 | 7936 | if (!rdev) |
c148ffdc | 7937 | continue; |
ad860670 | 7938 | if (conf->disks[i].replacement && |
17045f52 N |
7939 | conf->reshape_progress != MaxSector) { |
7940 | /* replacements and reshape simply do not mix. */ | |
cc6167b4 | 7941 | pr_warn("md: cannot handle concurrent replacement and reshape.\n"); |
17045f52 N |
7942 | goto abort; |
7943 | } | |
7bc43612 | 7944 | if (test_bit(In_sync, &rdev->flags)) |
2f115882 | 7945 | continue; |
c148ffdc N |
7946 | /* This disc is not fully in-sync. However if it |
7947 | * just stored parity (beyond the recovery_offset), | |
7948 | * when we don't need to be concerned about the | |
7949 | * array being dirty. | |
7950 | * When reshape goes 'backwards', we never have | |
7951 | * partially completed devices, so we only need | |
7952 | * to worry about reshape going forwards. | |
7953 | */ | |
7954 | /* Hack because v0.91 doesn't store recovery_offset properly. */ | |
7955 | if (mddev->major_version == 0 && | |
7956 | mddev->minor_version > 90) | |
7957 | rdev->recovery_offset = reshape_offset; | |
5026d7a9 | 7958 | |
c148ffdc N |
7959 | if (rdev->recovery_offset < reshape_offset) { |
7960 | /* We need to check old and new layout */ | |
7961 | if (!only_parity(rdev->raid_disk, | |
7962 | conf->algorithm, | |
7963 | conf->raid_disks, | |
7964 | conf->max_degraded)) | |
7965 | continue; | |
7966 | } | |
7967 | if (!only_parity(rdev->raid_disk, | |
7968 | conf->prev_algo, | |
7969 | conf->previous_raid_disks, | |
7970 | conf->max_degraded)) | |
7971 | continue; | |
7972 | dirty_parity_disks++; | |
7973 | } | |
91adb564 | 7974 | |
17045f52 N |
7975 | /* |
7976 | * 0 for a fully functional array, 1 or 2 for a degraded array. | |
7977 | */ | |
2e38a37f | 7978 | mddev->degraded = raid5_calc_degraded(conf); |
91adb564 | 7979 | |
674806d6 | 7980 | if (has_failed(conf)) { |
cc6167b4 | 7981 | pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", |
02c2de8c | 7982 | mdname(mddev), mddev->degraded, conf->raid_disks); |
1da177e4 LT |
7983 | goto abort; |
7984 | } | |
7985 | ||
91adb564 | 7986 | /* device size must be a multiple of chunk size */ |
c5eec74f | 7987 | mddev->dev_sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
91adb564 N |
7988 | mddev->resync_max_sectors = mddev->dev_sectors; |
7989 | ||
c148ffdc | 7990 | if (mddev->degraded > dirty_parity_disks && |
1da177e4 | 7991 | mddev->recovery_cp != MaxSector) { |
4536bf9b AP |
7992 | if (test_bit(MD_HAS_PPL, &mddev->flags)) |
7993 | pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n", | |
7994 | mdname(mddev)); | |
7995 | else if (mddev->ok_start_degraded) | |
cc6167b4 N |
7996 | pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", |
7997 | mdname(mddev)); | |
6ff8d8ec | 7998 | else { |
cc6167b4 N |
7999 | pr_crit("md/raid:%s: cannot start dirty degraded array.\n", |
8000 | mdname(mddev)); | |
6ff8d8ec N |
8001 | goto abort; |
8002 | } | |
1da177e4 LT |
8003 | } |
8004 | ||
cc6167b4 N |
8005 | pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", |
8006 | mdname(mddev), conf->level, | |
8007 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, | |
8008 | mddev->new_layout); | |
1da177e4 LT |
8009 | |
8010 | print_raid5_conf(conf); | |
8011 | ||
fef9c61f | 8012 | if (conf->reshape_progress != MaxSector) { |
fef9c61f | 8013 | conf->reshape_safe = conf->reshape_progress; |
f6705578 N |
8014 | atomic_set(&conf->reshape_stripes, 0); |
8015 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
8016 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
8017 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
ad39c081 | 8018 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
f6705578 N |
8019 | } |
8020 | ||
1da177e4 | 8021 | /* Ok, everything is just fine now */ |
a64c876f N |
8022 | if (mddev->to_remove == &raid5_attrs_group) |
8023 | mddev->to_remove = NULL; | |
00bcb4ac N |
8024 | else if (mddev->kobj.sd && |
8025 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) | |
cc6167b4 N |
8026 | pr_warn("raid5: failed to create sysfs attributes for %s\n", |
8027 | mdname(mddev)); | |
4a5add49 | 8028 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
7a5febe9 | 8029 | |
176df894 | 8030 | if (!mddev_is_dm(mddev)) { |
f63f1735 CH |
8031 | ret = raid5_set_limits(mddev); |
8032 | if (ret) | |
8033 | goto abort; | |
9f7c2220 | 8034 | } |
23032a0e | 8035 | |
845b9e22 | 8036 | if (log_init(conf, journal_dev, raid5_has_ppl(conf))) |
ff875738 | 8037 | goto abort; |
5c7e81c3 | 8038 | |
1da177e4 LT |
8039 | return 0; |
8040 | abort: | |
7eb8ff02 | 8041 | md_unregister_thread(mddev, &mddev->thread); |
e4f869d9 N |
8042 | print_raid5_conf(conf); |
8043 | free_conf(conf); | |
1da177e4 | 8044 | mddev->private = NULL; |
cc6167b4 | 8045 | pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
f63f1735 | 8046 | return ret; |
1da177e4 LT |
8047 | } |
8048 | ||
afa0f557 | 8049 | static void raid5_free(struct mddev *mddev, void *priv) |
1da177e4 | 8050 | { |
afa0f557 | 8051 | struct r5conf *conf = priv; |
1da177e4 | 8052 | |
95fc17aa | 8053 | free_conf(conf); |
a64c876f | 8054 | mddev->to_remove = &raid5_attrs_group; |
1da177e4 LT |
8055 | } |
8056 | ||
849674e4 | 8057 | static void raid5_status(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 8058 | { |
d1688a6d | 8059 | struct r5conf *conf = mddev->private; |
1da177e4 LT |
8060 | int i; |
8061 | ||
ad860670 YK |
8062 | lockdep_assert_held(&mddev->lock); |
8063 | ||
9d8f0363 | 8064 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
3cb5edf4 | 8065 | conf->chunk_sectors / 2, mddev->layout); |
02c2de8c | 8066 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
5fd13351 | 8067 | for (i = 0; i < conf->raid_disks; i++) { |
ad860670 YK |
8068 | struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev); |
8069 | ||
5fd13351 N |
8070 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
8071 | } | |
1da177e4 | 8072 | seq_printf (seq, "]"); |
1da177e4 LT |
8073 | } |
8074 | ||
2314c2e3 | 8075 | static void print_raid5_conf(struct r5conf *conf) |
1da177e4 | 8076 | { |
b0920ede | 8077 | struct md_rdev *rdev; |
1da177e4 | 8078 | int i; |
1da177e4 | 8079 | |
cc6167b4 | 8080 | pr_debug("RAID conf printout:\n"); |
1da177e4 | 8081 | if (!conf) { |
cc6167b4 | 8082 | pr_debug("(conf==NULL)\n"); |
1da177e4 LT |
8083 | return; |
8084 | } | |
cc6167b4 | 8085 | pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, |
0c55e022 N |
8086 | conf->raid_disks, |
8087 | conf->raid_disks - conf->mddev->degraded); | |
1da177e4 LT |
8088 | |
8089 | for (i = 0; i < conf->raid_disks; i++) { | |
2314c2e3 | 8090 | rdev = conf->disks[i].rdev; |
b0920ede | 8091 | if (rdev) |
913cce5a | 8092 | pr_debug(" disk %d, o:%d, dev:%pg\n", |
b0920ede | 8093 | i, !test_bit(Faulty, &rdev->flags), |
913cce5a | 8094 | rdev->bdev); |
1da177e4 LT |
8095 | } |
8096 | } | |
8097 | ||
fd01b88c | 8098 | static int raid5_spare_active(struct mddev *mddev) |
1da177e4 LT |
8099 | { |
8100 | int i; | |
d1688a6d | 8101 | struct r5conf *conf = mddev->private; |
9aeb7f99 | 8102 | struct md_rdev *rdev, *replacement; |
6b965620 N |
8103 | int count = 0; |
8104 | unsigned long flags; | |
1da177e4 LT |
8105 | |
8106 | for (i = 0; i < conf->raid_disks; i++) { | |
ad860670 YK |
8107 | rdev = conf->disks[i].rdev; |
8108 | replacement = conf->disks[i].replacement; | |
9aeb7f99 LG |
8109 | if (replacement |
8110 | && replacement->recovery_offset == MaxSector | |
8111 | && !test_bit(Faulty, &replacement->flags) | |
8112 | && !test_and_set_bit(In_sync, &replacement->flags)) { | |
dd054fce | 8113 | /* Replacement has just become active. */ |
9aeb7f99 LG |
8114 | if (!rdev |
8115 | || !test_and_clear_bit(In_sync, &rdev->flags)) | |
dd054fce | 8116 | count++; |
9aeb7f99 | 8117 | if (rdev) { |
dd054fce N |
8118 | /* Replaced device not technically faulty, |
8119 | * but we need to be sure it gets removed | |
8120 | * and never re-added. | |
8121 | */ | |
9aeb7f99 | 8122 | set_bit(Faulty, &rdev->flags); |
dd054fce | 8123 | sysfs_notify_dirent_safe( |
9aeb7f99 | 8124 | rdev->sysfs_state); |
dd054fce | 8125 | } |
9aeb7f99 LG |
8126 | sysfs_notify_dirent_safe(replacement->sysfs_state); |
8127 | } else if (rdev | |
8128 | && rdev->recovery_offset == MaxSector | |
8129 | && !test_bit(Faulty, &rdev->flags) | |
8130 | && !test_and_set_bit(In_sync, &rdev->flags)) { | |
6b965620 | 8131 | count++; |
9aeb7f99 | 8132 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
1da177e4 LT |
8133 | } |
8134 | } | |
6b965620 | 8135 | spin_lock_irqsave(&conf->device_lock, flags); |
2e38a37f | 8136 | mddev->degraded = raid5_calc_degraded(conf); |
6b965620 | 8137 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 | 8138 | print_raid5_conf(conf); |
6b965620 | 8139 | return count; |
1da177e4 LT |
8140 | } |
8141 | ||
b8321b68 | 8142 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 8143 | { |
d1688a6d | 8144 | struct r5conf *conf = mddev->private; |
1da177e4 | 8145 | int err = 0; |
b8321b68 | 8146 | int number = rdev->raid_disk; |
ad860670 | 8147 | struct md_rdev **rdevp; |
1ebc2cec | 8148 | struct disk_info *p; |
b0920ede | 8149 | struct md_rdev *tmp; |
1da177e4 LT |
8150 | |
8151 | print_raid5_conf(conf); | |
f6b6ec5c | 8152 | if (test_bit(Journal, &rdev->flags) && conf->log) { |
c2bb6242 | 8153 | /* |
f6b6ec5c SL |
8154 | * we can't wait pending write here, as this is called in |
8155 | * raid5d, wait will deadlock. | |
84dd97a6 N |
8156 | * neilb: there is no locking about new writes here, |
8157 | * so this cannot be safe. | |
c2bb6242 | 8158 | */ |
70d466f7 SL |
8159 | if (atomic_read(&conf->active_stripes) || |
8160 | atomic_read(&conf->r5c_cached_full_stripes) || | |
8161 | atomic_read(&conf->r5c_cached_partial_stripes)) { | |
f6b6ec5c | 8162 | return -EBUSY; |
84dd97a6 | 8163 | } |
ff875738 | 8164 | log_exit(conf); |
f6b6ec5c | 8165 | return 0; |
c2bb6242 | 8166 | } |
1ebc2cec MP |
8167 | if (unlikely(number >= conf->pool_size)) |
8168 | return 0; | |
8169 | p = conf->disks + number; | |
ad860670 | 8170 | if (rdev == p->rdev) |
657e3e4d | 8171 | rdevp = &p->rdev; |
ad860670 | 8172 | else if (rdev == p->replacement) |
657e3e4d N |
8173 | rdevp = &p->replacement; |
8174 | else | |
8175 | return 0; | |
8176 | ||
8177 | if (number >= conf->raid_disks && | |
8178 | conf->reshape_progress == MaxSector) | |
8179 | clear_bit(In_sync, &rdev->flags); | |
8180 | ||
8181 | if (test_bit(In_sync, &rdev->flags) || | |
8182 | atomic_read(&rdev->nr_pending)) { | |
8183 | err = -EBUSY; | |
8184 | goto abort; | |
8185 | } | |
8186 | /* Only remove non-faulty devices if recovery | |
8187 | * isn't possible. | |
8188 | */ | |
8189 | if (!test_bit(Faulty, &rdev->flags) && | |
8190 | mddev->recovery_disabled != conf->recovery_disabled && | |
8191 | !has_failed(conf) && | |
ad860670 | 8192 | (!p->replacement || p->replacement == rdev) && |
657e3e4d N |
8193 | number < conf->raid_disks) { |
8194 | err = -EBUSY; | |
8195 | goto abort; | |
8196 | } | |
ad860670 | 8197 | WRITE_ONCE(*rdevp, NULL); |
6358c239 AP |
8198 | if (!err) { |
8199 | err = log_modify(conf, rdev, false); | |
8200 | if (err) | |
8201 | goto abort; | |
8202 | } | |
b0920ede | 8203 | |
ad860670 | 8204 | tmp = p->replacement; |
b0920ede | 8205 | if (tmp) { |
dd054fce | 8206 | /* We must have just cleared 'rdev' */ |
ad860670 | 8207 | WRITE_ONCE(p->rdev, tmp); |
b0920ede | 8208 | clear_bit(Replacement, &tmp->flags); |
ad860670 | 8209 | WRITE_ONCE(p->replacement, NULL); |
6358c239 AP |
8210 | |
8211 | if (!err) | |
b0920ede | 8212 | err = log_modify(conf, tmp, true); |
e5bc9c3c GJ |
8213 | } |
8214 | ||
8215 | clear_bit(WantReplacement, &rdev->flags); | |
1da177e4 LT |
8216 | abort: |
8217 | ||
8218 | print_raid5_conf(conf); | |
8219 | return err; | |
8220 | } | |
8221 | ||
fd01b88c | 8222 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 8223 | { |
d1688a6d | 8224 | struct r5conf *conf = mddev->private; |
d9771f5e | 8225 | int ret, err = -EEXIST; |
1da177e4 LT |
8226 | int disk; |
8227 | struct disk_info *p; | |
9aeb7f99 | 8228 | struct md_rdev *tmp; |
6c2fce2e NB |
8229 | int first = 0; |
8230 | int last = conf->raid_disks - 1; | |
1da177e4 | 8231 | |
f6b6ec5c | 8232 | if (test_bit(Journal, &rdev->flags)) { |
f6b6ec5c SL |
8233 | if (conf->log) |
8234 | return -EBUSY; | |
8235 | ||
8236 | rdev->raid_disk = 0; | |
8237 | /* | |
8238 | * The array is in readonly mode if journal is missing, so no | |
8239 | * write requests running. We should be safe | |
8240 | */ | |
d9771f5e XN |
8241 | ret = log_init(conf, rdev, false); |
8242 | if (ret) | |
8243 | return ret; | |
8244 | ||
8245 | ret = r5l_start(conf->log); | |
8246 | if (ret) | |
8247 | return ret; | |
8248 | ||
f6b6ec5c SL |
8249 | return 0; |
8250 | } | |
7f0da59b N |
8251 | if (mddev->recovery_disabled == conf->recovery_disabled) |
8252 | return -EBUSY; | |
8253 | ||
dc10c643 | 8254 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
1da177e4 | 8255 | /* no point adding a device */ |
199050ea | 8256 | return -EINVAL; |
1da177e4 | 8257 | |
6c2fce2e NB |
8258 | if (rdev->raid_disk >= 0) |
8259 | first = last = rdev->raid_disk; | |
1da177e4 LT |
8260 | |
8261 | /* | |
16a53ecc N |
8262 | * find the disk ... but prefer rdev->saved_raid_disk |
8263 | * if possible. | |
1da177e4 | 8264 | */ |
a20d636b | 8265 | if (rdev->saved_raid_disk >= first && |
617b3658 | 8266 | rdev->saved_raid_disk <= last && |
16a53ecc | 8267 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
5cfb22a1 N |
8268 | first = rdev->saved_raid_disk; |
8269 | ||
8270 | for (disk = first; disk <= last; disk++) { | |
7bfec5f3 N |
8271 | p = conf->disks + disk; |
8272 | if (p->rdev == NULL) { | |
b2d444d7 | 8273 | clear_bit(In_sync, &rdev->flags); |
1da177e4 | 8274 | rdev->raid_disk = disk; |
72626685 N |
8275 | if (rdev->saved_raid_disk != disk) |
8276 | conf->fullsync = 1; | |
ad860670 | 8277 | WRITE_ONCE(p->rdev, rdev); |
6358c239 AP |
8278 | |
8279 | err = log_modify(conf, rdev, true); | |
8280 | ||
5cfb22a1 | 8281 | goto out; |
1da177e4 | 8282 | } |
5cfb22a1 N |
8283 | } |
8284 | for (disk = first; disk <= last; disk++) { | |
8285 | p = conf->disks + disk; | |
ad860670 | 8286 | tmp = p->rdev; |
9aeb7f99 | 8287 | if (test_bit(WantReplacement, &tmp->flags) && |
46038b30 | 8288 | mddev->reshape_position == MaxSector && |
7bfec5f3 N |
8289 | p->replacement == NULL) { |
8290 | clear_bit(In_sync, &rdev->flags); | |
8291 | set_bit(Replacement, &rdev->flags); | |
8292 | rdev->raid_disk = disk; | |
8293 | err = 0; | |
8294 | conf->fullsync = 1; | |
ad860670 | 8295 | WRITE_ONCE(p->replacement, rdev); |
7bfec5f3 N |
8296 | break; |
8297 | } | |
8298 | } | |
5cfb22a1 | 8299 | out: |
1da177e4 | 8300 | print_raid5_conf(conf); |
199050ea | 8301 | return err; |
1da177e4 LT |
8302 | } |
8303 | ||
fd01b88c | 8304 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
1da177e4 LT |
8305 | { |
8306 | /* no resync is happening, and there is enough space | |
8307 | * on all devices, so we can resize. | |
8308 | * We need to make sure resync covers any new space. | |
8309 | * If the array is shrinking we should possibly wait until | |
8310 | * any io in the removed space completes, but it hardly seems | |
8311 | * worth it. | |
8312 | */ | |
a4a6125a | 8313 | sector_t newsize; |
3cb5edf4 | 8314 | struct r5conf *conf = mddev->private; |
e1791dae | 8315 | int ret; |
3cb5edf4 | 8316 | |
e254de6b | 8317 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
713cf5a6 | 8318 | return -EINVAL; |
3cb5edf4 | 8319 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
a4a6125a N |
8320 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
8321 | if (mddev->external_size && | |
8322 | mddev->array_sectors > newsize) | |
b522adcd | 8323 | return -EINVAL; |
e1791dae | 8324 | |
77c09640 | 8325 | ret = mddev->bitmap_ops->resize(mddev, sectors, 0, false); |
e1791dae YK |
8326 | if (ret) |
8327 | return ret; | |
8328 | ||
a4a6125a | 8329 | md_set_array_sectors(mddev, newsize); |
b098636c N |
8330 | if (sectors > mddev->dev_sectors && |
8331 | mddev->recovery_cp > mddev->dev_sectors) { | |
58c0fed4 | 8332 | mddev->recovery_cp = mddev->dev_sectors; |
1da177e4 LT |
8333 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
8334 | } | |
58c0fed4 | 8335 | mddev->dev_sectors = sectors; |
4b5c7ae8 | 8336 | mddev->resync_max_sectors = sectors; |
1da177e4 LT |
8337 | return 0; |
8338 | } | |
8339 | ||
fd01b88c | 8340 | static int check_stripe_cache(struct mddev *mddev) |
01ee22b4 N |
8341 | { |
8342 | /* Can only proceed if there are plenty of stripe_heads. | |
8343 | * We need a minimum of one full stripe,, and for sensible progress | |
8344 | * it is best to have about 4 times that. | |
8345 | * If we require 4 times, then the default 256 4K stripe_heads will | |
8346 | * allow for chunk sizes up to 256K, which is probably OK. | |
8347 | * If the chunk size is greater, user-space should request more | |
8348 | * stripe_heads first. | |
8349 | */ | |
d1688a6d | 8350 | struct r5conf *conf = mddev->private; |
c911c46c | 8351 | if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 |
edbe83ab | 8352 | > conf->min_nr_stripes || |
c911c46c | 8353 | ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 |
edbe83ab | 8354 | > conf->min_nr_stripes) { |
cc6167b4 N |
8355 | pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
8356 | mdname(mddev), | |
8357 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) | |
c911c46c | 8358 | / RAID5_STRIPE_SIZE(conf))*4); |
01ee22b4 N |
8359 | return 0; |
8360 | } | |
8361 | return 1; | |
8362 | } | |
8363 | ||
fd01b88c | 8364 | static int check_reshape(struct mddev *mddev) |
29269553 | 8365 | { |
d1688a6d | 8366 | struct r5conf *conf = mddev->private; |
29269553 | 8367 | |
e254de6b | 8368 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
713cf5a6 | 8369 | return -EINVAL; |
88ce4930 N |
8370 | if (mddev->delta_disks == 0 && |
8371 | mddev->new_layout == mddev->layout && | |
664e7c41 | 8372 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
50ac168a | 8373 | return 0; /* nothing to do */ |
674806d6 | 8374 | if (has_failed(conf)) |
ec32a2bd | 8375 | return -EINVAL; |
fdcfbbb6 | 8376 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
ec32a2bd N |
8377 | /* We might be able to shrink, but the devices must |
8378 | * be made bigger first. | |
8379 | * For raid6, 4 is the minimum size. | |
8380 | * Otherwise 2 is the minimum | |
8381 | */ | |
8382 | int min = 2; | |
8383 | if (mddev->level == 6) | |
8384 | min = 4; | |
8385 | if (mddev->raid_disks + mddev->delta_disks < min) | |
8386 | return -EINVAL; | |
8387 | } | |
29269553 | 8388 | |
01ee22b4 | 8389 | if (!check_stripe_cache(mddev)) |
29269553 | 8390 | return -ENOSPC; |
29269553 | 8391 | |
738a2738 N |
8392 | if (mddev->new_chunk_sectors > mddev->chunk_sectors || |
8393 | mddev->delta_disks > 0) | |
8394 | if (resize_chunks(conf, | |
8395 | conf->previous_raid_disks | |
8396 | + max(0, mddev->delta_disks), | |
8397 | max(mddev->new_chunk_sectors, | |
8398 | mddev->chunk_sectors) | |
8399 | ) < 0) | |
8400 | return -ENOMEM; | |
845b9e22 AP |
8401 | |
8402 | if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size) | |
8403 | return 0; /* never bother to shrink */ | |
e56108d6 N |
8404 | return resize_stripes(conf, (conf->previous_raid_disks |
8405 | + mddev->delta_disks)); | |
63c70c4f N |
8406 | } |
8407 | ||
fd01b88c | 8408 | static int raid5_start_reshape(struct mddev *mddev) |
63c70c4f | 8409 | { |
d1688a6d | 8410 | struct r5conf *conf = mddev->private; |
3cb03002 | 8411 | struct md_rdev *rdev; |
63c70c4f | 8412 | int spares = 0; |
75aa7a1b | 8413 | int i; |
c04be0aa | 8414 | unsigned long flags; |
63c70c4f | 8415 | |
f416885e | 8416 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
63c70c4f N |
8417 | return -EBUSY; |
8418 | ||
01ee22b4 N |
8419 | if (!check_stripe_cache(mddev)) |
8420 | return -ENOSPC; | |
8421 | ||
30b67645 N |
8422 | if (has_failed(conf)) |
8423 | return -EINVAL; | |
8424 | ||
75aa7a1b YK |
8425 | /* raid5 can't handle concurrent reshape and recovery */ |
8426 | if (mddev->recovery_cp < MaxSector) | |
8427 | return -EBUSY; | |
8428 | for (i = 0; i < conf->raid_disks; i++) | |
ad860670 | 8429 | if (conf->disks[i].replacement) |
75aa7a1b YK |
8430 | return -EBUSY; |
8431 | ||
c6563a8c | 8432 | rdev_for_each(rdev, mddev) { |
469518a3 N |
8433 | if (!test_bit(In_sync, &rdev->flags) |
8434 | && !test_bit(Faulty, &rdev->flags)) | |
29269553 | 8435 | spares++; |
c6563a8c | 8436 | } |
63c70c4f | 8437 | |
f416885e | 8438 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
29269553 N |
8439 | /* Not enough devices even to make a degraded array |
8440 | * of that size | |
8441 | */ | |
8442 | return -EINVAL; | |
8443 | ||
ec32a2bd N |
8444 | /* Refuse to reduce size of the array. Any reductions in |
8445 | * array size must be through explicit setting of array_size | |
8446 | * attribute. | |
8447 | */ | |
8448 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) | |
8449 | < mddev->array_sectors) { | |
cc6167b4 N |
8450 | pr_warn("md/raid:%s: array size must be reduced before number of disks\n", |
8451 | mdname(mddev)); | |
ec32a2bd N |
8452 | return -EINVAL; |
8453 | } | |
8454 | ||
f6705578 | 8455 | atomic_set(&conf->reshape_stripes, 0); |
29269553 | 8456 | spin_lock_irq(&conf->device_lock); |
c46501b2 | 8457 | write_seqcount_begin(&conf->gen_lock); |
29269553 | 8458 | conf->previous_raid_disks = conf->raid_disks; |
63c70c4f | 8459 | conf->raid_disks += mddev->delta_disks; |
09c9e5fa AN |
8460 | conf->prev_chunk_sectors = conf->chunk_sectors; |
8461 | conf->chunk_sectors = mddev->new_chunk_sectors; | |
88ce4930 N |
8462 | conf->prev_algo = conf->algorithm; |
8463 | conf->algorithm = mddev->new_layout; | |
05616be5 N |
8464 | conf->generation++; |
8465 | /* Code that selects data_offset needs to see the generation update | |
8466 | * if reshape_progress has been set - so a memory barrier needed. | |
8467 | */ | |
8468 | smp_mb(); | |
2c810cdd | 8469 | if (mddev->reshape_backwards) |
fef9c61f N |
8470 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
8471 | else | |
8472 | conf->reshape_progress = 0; | |
8473 | conf->reshape_safe = conf->reshape_progress; | |
c46501b2 | 8474 | write_seqcount_end(&conf->gen_lock); |
29269553 N |
8475 | spin_unlock_irq(&conf->device_lock); |
8476 | ||
4d77e3ba N |
8477 | /* Now make sure any requests that proceeded on the assumption |
8478 | * the reshape wasn't running - like Discard or Read - have | |
8479 | * completed. | |
8480 | */ | |
b42cd7b3 YK |
8481 | raid5_quiesce(mddev, true); |
8482 | raid5_quiesce(mddev, false); | |
4d77e3ba | 8483 | |
29269553 N |
8484 | /* Add some new drives, as many as will fit. |
8485 | * We know there are enough to make the newly sized array work. | |
3424bf6a N |
8486 | * Don't add devices if we are reducing the number of |
8487 | * devices in the array. This is because it is not possible | |
8488 | * to correctly record the "partially reconstructed" state of | |
8489 | * such devices during the reshape and confusion could result. | |
29269553 | 8490 | */ |
87a8dec9 | 8491 | if (mddev->delta_disks >= 0) { |
dafb20fa | 8492 | rdev_for_each(rdev, mddev) |
87a8dec9 N |
8493 | if (rdev->raid_disk < 0 && |
8494 | !test_bit(Faulty, &rdev->flags)) { | |
8495 | if (raid5_add_disk(mddev, rdev) == 0) { | |
87a8dec9 | 8496 | if (rdev->raid_disk |
9d4c7d87 | 8497 | >= conf->previous_raid_disks) |
87a8dec9 | 8498 | set_bit(In_sync, &rdev->flags); |
9d4c7d87 | 8499 | else |
87a8dec9 | 8500 | rdev->recovery_offset = 0; |
36fad858 | 8501 | |
2aada5b1 DLM |
8502 | /* Failure here is OK */ |
8503 | sysfs_link_rdev(mddev, rdev); | |
50da0840 | 8504 | } |
87a8dec9 N |
8505 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
8506 | && !test_bit(Faulty, &rdev->flags)) { | |
8507 | /* This is a spare that was manually added */ | |
8508 | set_bit(In_sync, &rdev->flags); | |
87a8dec9 | 8509 | } |
29269553 | 8510 | |
87a8dec9 N |
8511 | /* When a reshape changes the number of devices, |
8512 | * ->degraded is measured against the larger of the | |
8513 | * pre and post number of devices. | |
8514 | */ | |
ec32a2bd | 8515 | spin_lock_irqsave(&conf->device_lock, flags); |
2e38a37f | 8516 | mddev->degraded = raid5_calc_degraded(conf); |
ec32a2bd N |
8517 | spin_unlock_irqrestore(&conf->device_lock, flags); |
8518 | } | |
63c70c4f | 8519 | mddev->raid_disks = conf->raid_disks; |
e516402c | 8520 | mddev->reshape_position = conf->reshape_progress; |
2953079c | 8521 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
f6705578 | 8522 | |
29269553 N |
8523 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
8524 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
ea358cd0 | 8525 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
29269553 | 8526 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
ad39c081 | 8527 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
c8f517c4 | 8528 | conf->reshape_checkpoint = jiffies; |
54679486 | 8529 | md_new_event(); |
29269553 N |
8530 | return 0; |
8531 | } | |
29269553 | 8532 | |
ec32a2bd N |
8533 | /* This is called from the reshape thread and should make any |
8534 | * changes needed in 'conf' | |
8535 | */ | |
d1688a6d | 8536 | static void end_reshape(struct r5conf *conf) |
29269553 | 8537 | { |
29269553 | 8538 | |
f6705578 | 8539 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
db0505d3 | 8540 | struct md_rdev *rdev; |
f6705578 | 8541 | |
f6705578 | 8542 | spin_lock_irq(&conf->device_lock); |
cea9c228 | 8543 | conf->previous_raid_disks = conf->raid_disks; |
b5d27718 | 8544 | md_finish_reshape(conf->mddev); |
05616be5 | 8545 | smp_wmb(); |
fef9c61f | 8546 | conf->reshape_progress = MaxSector; |
6cbd8148 | 8547 | conf->mddev->reshape_position = MaxSector; |
db0505d3 N |
8548 | rdev_for_each(rdev, conf->mddev) |
8549 | if (rdev->raid_disk >= 0 && | |
8550 | !test_bit(Journal, &rdev->flags) && | |
8551 | !test_bit(In_sync, &rdev->flags)) | |
8552 | rdev->recovery_offset = MaxSector; | |
f6705578 | 8553 | spin_unlock_irq(&conf->device_lock); |
6f039cc4 | 8554 | wake_up(&conf->wait_for_reshape); |
16a53ecc | 8555 | |
f63f1735 CH |
8556 | mddev_update_io_opt(conf->mddev, |
8557 | conf->raid_disks - conf->max_degraded); | |
29269553 | 8558 | } |
29269553 N |
8559 | } |
8560 | ||
ec32a2bd N |
8561 | /* This is called from the raid5d thread with mddev_lock held. |
8562 | * It makes config changes to the device. | |
8563 | */ | |
fd01b88c | 8564 | static void raid5_finish_reshape(struct mddev *mddev) |
cea9c228 | 8565 | { |
d1688a6d | 8566 | struct r5conf *conf = mddev->private; |
9aeb7f99 | 8567 | struct md_rdev *rdev; |
cea9c228 N |
8568 | |
8569 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
8570 | ||
8876391e | 8571 | if (mddev->delta_disks <= 0) { |
ec32a2bd | 8572 | int d; |
908f4fbd | 8573 | spin_lock_irq(&conf->device_lock); |
2e38a37f | 8574 | mddev->degraded = raid5_calc_degraded(conf); |
908f4fbd | 8575 | spin_unlock_irq(&conf->device_lock); |
ec32a2bd N |
8576 | for (d = conf->raid_disks ; |
8577 | d < conf->raid_disks - mddev->delta_disks; | |
1a67dde0 | 8578 | d++) { |
ad860670 | 8579 | rdev = conf->disks[d].rdev; |
da7613b8 N |
8580 | if (rdev) |
8581 | clear_bit(In_sync, &rdev->flags); | |
ad860670 | 8582 | rdev = conf->disks[d].replacement; |
da7613b8 N |
8583 | if (rdev) |
8584 | clear_bit(In_sync, &rdev->flags); | |
1a67dde0 | 8585 | } |
cea9c228 | 8586 | } |
88ce4930 | 8587 | mddev->layout = conf->algorithm; |
09c9e5fa | 8588 | mddev->chunk_sectors = conf->chunk_sectors; |
ec32a2bd N |
8589 | mddev->reshape_position = MaxSector; |
8590 | mddev->delta_disks = 0; | |
2c810cdd | 8591 | mddev->reshape_backwards = 0; |
cea9c228 N |
8592 | } |
8593 | } | |
8594 | ||
b03e0ccb | 8595 | static void raid5_quiesce(struct mddev *mddev, int quiesce) |
72626685 | 8596 | { |
d1688a6d | 8597 | struct r5conf *conf = mddev->private; |
72626685 | 8598 | |
b03e0ccb N |
8599 | if (quiesce) { |
8600 | /* stop all writes */ | |
566c09c5 | 8601 | lock_all_device_hash_locks_irq(conf); |
64bd660b N |
8602 | /* '2' tells resync/reshape to pause so that all |
8603 | * active stripes can drain | |
8604 | */ | |
a39f7afd | 8605 | r5c_flush_cache(conf, INT_MAX); |
97ae2725 GO |
8606 | /* need a memory barrier to make sure read_one_chunk() sees |
8607 | * quiesce started and reverts to slow (locked) path. | |
8608 | */ | |
8609 | smp_store_release(&conf->quiesce, 2); | |
b1b46486 | 8610 | wait_event_cmd(conf->wait_for_quiescent, |
46031f9a RBJ |
8611 | atomic_read(&conf->active_stripes) == 0 && |
8612 | atomic_read(&conf->active_aligned_reads) == 0, | |
566c09c5 SL |
8613 | unlock_all_device_hash_locks_irq(conf), |
8614 | lock_all_device_hash_locks_irq(conf)); | |
64bd660b | 8615 | conf->quiesce = 1; |
566c09c5 | 8616 | unlock_all_device_hash_locks_irq(conf); |
64bd660b | 8617 | /* allow reshape to continue */ |
6f039cc4 | 8618 | wake_up(&conf->wait_for_reshape); |
b03e0ccb N |
8619 | } else { |
8620 | /* re-enable writes */ | |
566c09c5 | 8621 | lock_all_device_hash_locks_irq(conf); |
72626685 | 8622 | conf->quiesce = 0; |
b1b46486 | 8623 | wake_up(&conf->wait_for_quiescent); |
6f039cc4 | 8624 | wake_up(&conf->wait_for_reshape); |
566c09c5 | 8625 | unlock_all_device_hash_locks_irq(conf); |
72626685 | 8626 | } |
1532d9e8 | 8627 | log_quiesce(conf, quiesce); |
72626685 | 8628 | } |
b15c2e57 | 8629 | |
fd01b88c | 8630 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
54071b38 | 8631 | { |
e373ab10 | 8632 | struct r0conf *raid0_conf = mddev->private; |
d76c8420 | 8633 | sector_t sectors; |
54071b38 | 8634 | |
f1b29bca | 8635 | /* for raid0 takeover only one zone is supported */ |
e373ab10 | 8636 | if (raid0_conf->nr_strip_zones > 1) { |
cc6167b4 N |
8637 | pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
8638 | mdname(mddev)); | |
f1b29bca DW |
8639 | return ERR_PTR(-EINVAL); |
8640 | } | |
8641 | ||
e373ab10 N |
8642 | sectors = raid0_conf->strip_zone[0].zone_end; |
8643 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); | |
3b71bd93 | 8644 | mddev->dev_sectors = sectors; |
f1b29bca | 8645 | mddev->new_level = level; |
54071b38 TM |
8646 | mddev->new_layout = ALGORITHM_PARITY_N; |
8647 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
8648 | mddev->raid_disks += 1; | |
8649 | mddev->delta_disks = 1; | |
8650 | /* make sure it will be not marked as dirty */ | |
8651 | mddev->recovery_cp = MaxSector; | |
8652 | ||
8653 | return setup_conf(mddev); | |
8654 | } | |
8655 | ||
fd01b88c | 8656 | static void *raid5_takeover_raid1(struct mddev *mddev) |
d562b0c4 N |
8657 | { |
8658 | int chunksect; | |
6995f0b2 | 8659 | void *ret; |
d562b0c4 N |
8660 | |
8661 | if (mddev->raid_disks != 2 || | |
8662 | mddev->degraded > 1) | |
8663 | return ERR_PTR(-EINVAL); | |
8664 | ||
8665 | /* Should check if there are write-behind devices? */ | |
8666 | ||
8667 | chunksect = 64*2; /* 64K by default */ | |
8668 | ||
8669 | /* The array must be an exact multiple of chunksize */ | |
8670 | while (chunksect && (mddev->array_sectors & (chunksect-1))) | |
8671 | chunksect >>= 1; | |
8672 | ||
c911c46c | 8673 | if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private)) |
d562b0c4 N |
8674 | /* array size does not allow a suitable chunk size */ |
8675 | return ERR_PTR(-EINVAL); | |
8676 | ||
8677 | mddev->new_level = 5; | |
8678 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; | |
664e7c41 | 8679 | mddev->new_chunk_sectors = chunksect; |
d562b0c4 | 8680 | |
6995f0b2 | 8681 | ret = setup_conf(mddev); |
32cd7cbb | 8682 | if (!IS_ERR(ret)) |
394ed8e4 SL |
8683 | mddev_clear_unsupported_flags(mddev, |
8684 | UNSUPPORTED_MDDEV_FLAGS); | |
6995f0b2 | 8685 | return ret; |
d562b0c4 N |
8686 | } |
8687 | ||
fd01b88c | 8688 | static void *raid5_takeover_raid6(struct mddev *mddev) |
fc9739c6 N |
8689 | { |
8690 | int new_layout; | |
8691 | ||
8692 | switch (mddev->layout) { | |
8693 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
8694 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; | |
8695 | break; | |
8696 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
8697 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; | |
8698 | break; | |
8699 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
8700 | new_layout = ALGORITHM_LEFT_SYMMETRIC; | |
8701 | break; | |
8702 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
8703 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; | |
8704 | break; | |
8705 | case ALGORITHM_PARITY_0_6: | |
8706 | new_layout = ALGORITHM_PARITY_0; | |
8707 | break; | |
8708 | case ALGORITHM_PARITY_N: | |
8709 | new_layout = ALGORITHM_PARITY_N; | |
8710 | break; | |
8711 | default: | |
8712 | return ERR_PTR(-EINVAL); | |
8713 | } | |
8714 | mddev->new_level = 5; | |
8715 | mddev->new_layout = new_layout; | |
8716 | mddev->delta_disks = -1; | |
8717 | mddev->raid_disks -= 1; | |
8718 | return setup_conf(mddev); | |
8719 | } | |
8720 | ||
fd01b88c | 8721 | static int raid5_check_reshape(struct mddev *mddev) |
b3546035 | 8722 | { |
88ce4930 N |
8723 | /* For a 2-drive array, the layout and chunk size can be changed |
8724 | * immediately as not restriping is needed. | |
8725 | * For larger arrays we record the new value - after validation | |
8726 | * to be used by a reshape pass. | |
b3546035 | 8727 | */ |
d1688a6d | 8728 | struct r5conf *conf = mddev->private; |
597a711b | 8729 | int new_chunk = mddev->new_chunk_sectors; |
b3546035 | 8730 | |
597a711b | 8731 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
b3546035 N |
8732 | return -EINVAL; |
8733 | if (new_chunk > 0) { | |
0ba459d2 | 8734 | if (!is_power_of_2(new_chunk)) |
b3546035 | 8735 | return -EINVAL; |
597a711b | 8736 | if (new_chunk < (PAGE_SIZE>>9)) |
b3546035 | 8737 | return -EINVAL; |
597a711b | 8738 | if (mddev->array_sectors & (new_chunk-1)) |
b3546035 N |
8739 | /* not factor of array size */ |
8740 | return -EINVAL; | |
8741 | } | |
8742 | ||
8743 | /* They look valid */ | |
8744 | ||
88ce4930 | 8745 | if (mddev->raid_disks == 2) { |
597a711b N |
8746 | /* can make the change immediately */ |
8747 | if (mddev->new_layout >= 0) { | |
8748 | conf->algorithm = mddev->new_layout; | |
8749 | mddev->layout = mddev->new_layout; | |
88ce4930 N |
8750 | } |
8751 | if (new_chunk > 0) { | |
597a711b N |
8752 | conf->chunk_sectors = new_chunk ; |
8753 | mddev->chunk_sectors = new_chunk; | |
88ce4930 | 8754 | } |
2953079c | 8755 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
88ce4930 | 8756 | md_wakeup_thread(mddev->thread); |
b3546035 | 8757 | } |
50ac168a | 8758 | return check_reshape(mddev); |
88ce4930 N |
8759 | } |
8760 | ||
fd01b88c | 8761 | static int raid6_check_reshape(struct mddev *mddev) |
88ce4930 | 8762 | { |
597a711b | 8763 | int new_chunk = mddev->new_chunk_sectors; |
50ac168a | 8764 | |
597a711b | 8765 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
88ce4930 | 8766 | return -EINVAL; |
b3546035 | 8767 | if (new_chunk > 0) { |
0ba459d2 | 8768 | if (!is_power_of_2(new_chunk)) |
88ce4930 | 8769 | return -EINVAL; |
597a711b | 8770 | if (new_chunk < (PAGE_SIZE >> 9)) |
88ce4930 | 8771 | return -EINVAL; |
597a711b | 8772 | if (mddev->array_sectors & (new_chunk-1)) |
88ce4930 N |
8773 | /* not factor of array size */ |
8774 | return -EINVAL; | |
b3546035 | 8775 | } |
88ce4930 N |
8776 | |
8777 | /* They look valid */ | |
50ac168a | 8778 | return check_reshape(mddev); |
b3546035 N |
8779 | } |
8780 | ||
fd01b88c | 8781 | static void *raid5_takeover(struct mddev *mddev) |
d562b0c4 N |
8782 | { |
8783 | /* raid5 can take over: | |
f1b29bca | 8784 | * raid0 - if there is only one strip zone - make it a raid4 layout |
d562b0c4 N |
8785 | * raid1 - if there are two drives. We need to know the chunk size |
8786 | * raid4 - trivial - just use a raid4 layout. | |
8787 | * raid6 - Providing it is a *_6 layout | |
d562b0c4 | 8788 | */ |
f1b29bca DW |
8789 | if (mddev->level == 0) |
8790 | return raid45_takeover_raid0(mddev, 5); | |
d562b0c4 N |
8791 | if (mddev->level == 1) |
8792 | return raid5_takeover_raid1(mddev); | |
e9d4758f N |
8793 | if (mddev->level == 4) { |
8794 | mddev->new_layout = ALGORITHM_PARITY_N; | |
8795 | mddev->new_level = 5; | |
8796 | return setup_conf(mddev); | |
8797 | } | |
fc9739c6 N |
8798 | if (mddev->level == 6) |
8799 | return raid5_takeover_raid6(mddev); | |
d562b0c4 N |
8800 | |
8801 | return ERR_PTR(-EINVAL); | |
8802 | } | |
8803 | ||
fd01b88c | 8804 | static void *raid4_takeover(struct mddev *mddev) |
a78d38a1 | 8805 | { |
f1b29bca DW |
8806 | /* raid4 can take over: |
8807 | * raid0 - if there is only one strip zone | |
8808 | * raid5 - if layout is right | |
a78d38a1 | 8809 | */ |
f1b29bca DW |
8810 | if (mddev->level == 0) |
8811 | return raid45_takeover_raid0(mddev, 4); | |
a78d38a1 N |
8812 | if (mddev->level == 5 && |
8813 | mddev->layout == ALGORITHM_PARITY_N) { | |
8814 | mddev->new_layout = 0; | |
8815 | mddev->new_level = 4; | |
8816 | return setup_conf(mddev); | |
8817 | } | |
8818 | return ERR_PTR(-EINVAL); | |
8819 | } | |
d562b0c4 | 8820 | |
84fc4b56 | 8821 | static struct md_personality raid5_personality; |
245f46c2 | 8822 | |
fd01b88c | 8823 | static void *raid6_takeover(struct mddev *mddev) |
245f46c2 N |
8824 | { |
8825 | /* Currently can only take over a raid5. We map the | |
8826 | * personality to an equivalent raid6 personality | |
8827 | * with the Q block at the end. | |
8828 | */ | |
8829 | int new_layout; | |
8830 | ||
8831 | if (mddev->pers != &raid5_personality) | |
8832 | return ERR_PTR(-EINVAL); | |
8833 | if (mddev->degraded > 1) | |
8834 | return ERR_PTR(-EINVAL); | |
8835 | if (mddev->raid_disks > 253) | |
8836 | return ERR_PTR(-EINVAL); | |
8837 | if (mddev->raid_disks < 3) | |
8838 | return ERR_PTR(-EINVAL); | |
8839 | ||
8840 | switch (mddev->layout) { | |
8841 | case ALGORITHM_LEFT_ASYMMETRIC: | |
8842 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; | |
8843 | break; | |
8844 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
8845 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; | |
8846 | break; | |
8847 | case ALGORITHM_LEFT_SYMMETRIC: | |
8848 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; | |
8849 | break; | |
8850 | case ALGORITHM_RIGHT_SYMMETRIC: | |
8851 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; | |
8852 | break; | |
8853 | case ALGORITHM_PARITY_0: | |
8854 | new_layout = ALGORITHM_PARITY_0_6; | |
8855 | break; | |
8856 | case ALGORITHM_PARITY_N: | |
8857 | new_layout = ALGORITHM_PARITY_N; | |
8858 | break; | |
8859 | default: | |
8860 | return ERR_PTR(-EINVAL); | |
8861 | } | |
8862 | mddev->new_level = 6; | |
8863 | mddev->new_layout = new_layout; | |
8864 | mddev->delta_disks = 1; | |
8865 | mddev->raid_disks += 1; | |
8866 | return setup_conf(mddev); | |
8867 | } | |
8868 | ||
ba903a3e AP |
8869 | static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf) |
8870 | { | |
8871 | struct r5conf *conf; | |
8872 | int err; | |
8873 | ||
e28ca92f | 8874 | err = mddev_suspend_and_lock(mddev); |
ba903a3e AP |
8875 | if (err) |
8876 | return err; | |
8877 | conf = mddev->private; | |
8878 | if (!conf) { | |
e28ca92f | 8879 | mddev_unlock_and_resume(mddev); |
ba903a3e AP |
8880 | return -ENODEV; |
8881 | } | |
8882 | ||
845b9e22 | 8883 | if (strncmp(buf, "ppl", 3) == 0) { |
0bb0c105 | 8884 | /* ppl only works with RAID 5 */ |
845b9e22 AP |
8885 | if (!raid5_has_ppl(conf) && conf->level == 5) { |
8886 | err = log_init(conf, NULL, true); | |
8887 | if (!err) { | |
8888 | err = resize_stripes(conf, conf->pool_size); | |
e28ca92f | 8889 | if (err) |
845b9e22 AP |
8890 | log_exit(conf); |
8891 | } | |
0bb0c105 SL |
8892 | } else |
8893 | err = -EINVAL; | |
8894 | } else if (strncmp(buf, "resync", 6) == 0) { | |
8895 | if (raid5_has_ppl(conf)) { | |
0bb0c105 | 8896 | log_exit(conf); |
845b9e22 | 8897 | err = resize_stripes(conf, conf->pool_size); |
0bb0c105 SL |
8898 | } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) && |
8899 | r5l_log_disk_error(conf)) { | |
8900 | bool journal_dev_exists = false; | |
8901 | struct md_rdev *rdev; | |
8902 | ||
8903 | rdev_for_each(rdev, mddev) | |
8904 | if (test_bit(Journal, &rdev->flags)) { | |
8905 | journal_dev_exists = true; | |
8906 | break; | |
8907 | } | |
8908 | ||
e28ca92f | 8909 | if (!journal_dev_exists) |
0bb0c105 | 8910 | clear_bit(MD_HAS_JOURNAL, &mddev->flags); |
e28ca92f | 8911 | else /* need remove journal device first */ |
0bb0c105 SL |
8912 | err = -EBUSY; |
8913 | } else | |
8914 | err = -EINVAL; | |
ba903a3e AP |
8915 | } else { |
8916 | err = -EINVAL; | |
8917 | } | |
8918 | ||
8919 | if (!err) | |
8920 | md_update_sb(mddev, 1); | |
8921 | ||
e28ca92f | 8922 | mddev_unlock_and_resume(mddev); |
ba903a3e AP |
8923 | |
8924 | return err; | |
8925 | } | |
8926 | ||
d5d885fd SL |
8927 | static int raid5_start(struct mddev *mddev) |
8928 | { | |
8929 | struct r5conf *conf = mddev->private; | |
8930 | ||
8931 | return r5l_start(conf->log); | |
8932 | } | |
8933 | ||
41425f96 YK |
8934 | /* |
8935 | * This is only used for dm-raid456, caller already frozen sync_thread, hence | |
8936 | * if rehsape is still in progress, io that is waiting for reshape can never be | |
8937 | * done now, hence wake up and handle those IO. | |
8938 | */ | |
8939 | static void raid5_prepare_suspend(struct mddev *mddev) | |
8940 | { | |
8941 | struct r5conf *conf = mddev->private; | |
8942 | ||
6f039cc4 | 8943 | wake_up(&conf->wait_for_reshape); |
41425f96 YK |
8944 | } |
8945 | ||
84fc4b56 | 8946 | static struct md_personality raid6_personality = |
16a53ecc | 8947 | { |
3d44e1d1 YK |
8948 | .head = { |
8949 | .type = MD_PERSONALITY, | |
8950 | .id = ID_RAID6, | |
8951 | .name = "raid6", | |
8952 | .owner = THIS_MODULE, | |
8953 | }, | |
8954 | ||
849674e4 SL |
8955 | .make_request = raid5_make_request, |
8956 | .run = raid5_run, | |
d5d885fd | 8957 | .start = raid5_start, |
afa0f557 | 8958 | .free = raid5_free, |
849674e4 SL |
8959 | .status = raid5_status, |
8960 | .error_handler = raid5_error, | |
16a53ecc N |
8961 | .hot_add_disk = raid5_add_disk, |
8962 | .hot_remove_disk= raid5_remove_disk, | |
8963 | .spare_active = raid5_spare_active, | |
849674e4 | 8964 | .sync_request = raid5_sync_request, |
16a53ecc | 8965 | .resize = raid5_resize, |
80c3a6ce | 8966 | .size = raid5_size, |
50ac168a | 8967 | .check_reshape = raid6_check_reshape, |
f416885e | 8968 | .start_reshape = raid5_start_reshape, |
cea9c228 | 8969 | .finish_reshape = raid5_finish_reshape, |
16a53ecc | 8970 | .quiesce = raid5_quiesce, |
245f46c2 | 8971 | .takeover = raid6_takeover, |
0bb0c105 | 8972 | .change_consistency_policy = raid5_change_consistency_policy, |
41425f96 | 8973 | .prepare_suspend = raid5_prepare_suspend, |
9c89f604 | 8974 | .bitmap_sector = raid5_bitmap_sector, |
16a53ecc | 8975 | }; |
84fc4b56 | 8976 | static struct md_personality raid5_personality = |
1da177e4 | 8977 | { |
3d44e1d1 YK |
8978 | .head = { |
8979 | .type = MD_PERSONALITY, | |
8980 | .id = ID_RAID5, | |
8981 | .name = "raid5", | |
8982 | .owner = THIS_MODULE, | |
8983 | }, | |
8984 | ||
849674e4 SL |
8985 | .make_request = raid5_make_request, |
8986 | .run = raid5_run, | |
d5d885fd | 8987 | .start = raid5_start, |
afa0f557 | 8988 | .free = raid5_free, |
849674e4 SL |
8989 | .status = raid5_status, |
8990 | .error_handler = raid5_error, | |
1da177e4 LT |
8991 | .hot_add_disk = raid5_add_disk, |
8992 | .hot_remove_disk= raid5_remove_disk, | |
8993 | .spare_active = raid5_spare_active, | |
849674e4 | 8994 | .sync_request = raid5_sync_request, |
1da177e4 | 8995 | .resize = raid5_resize, |
80c3a6ce | 8996 | .size = raid5_size, |
63c70c4f N |
8997 | .check_reshape = raid5_check_reshape, |
8998 | .start_reshape = raid5_start_reshape, | |
cea9c228 | 8999 | .finish_reshape = raid5_finish_reshape, |
72626685 | 9000 | .quiesce = raid5_quiesce, |
d562b0c4 | 9001 | .takeover = raid5_takeover, |
ba903a3e | 9002 | .change_consistency_policy = raid5_change_consistency_policy, |
41425f96 | 9003 | .prepare_suspend = raid5_prepare_suspend, |
9c89f604 | 9004 | .bitmap_sector = raid5_bitmap_sector, |
1da177e4 LT |
9005 | }; |
9006 | ||
84fc4b56 | 9007 | static struct md_personality raid4_personality = |
1da177e4 | 9008 | { |
3d44e1d1 YK |
9009 | .head = { |
9010 | .type = MD_PERSONALITY, | |
9011 | .id = ID_RAID4, | |
9012 | .name = "raid4", | |
9013 | .owner = THIS_MODULE, | |
9014 | }, | |
9015 | ||
849674e4 SL |
9016 | .make_request = raid5_make_request, |
9017 | .run = raid5_run, | |
d5d885fd | 9018 | .start = raid5_start, |
afa0f557 | 9019 | .free = raid5_free, |
849674e4 SL |
9020 | .status = raid5_status, |
9021 | .error_handler = raid5_error, | |
2604b703 N |
9022 | .hot_add_disk = raid5_add_disk, |
9023 | .hot_remove_disk= raid5_remove_disk, | |
9024 | .spare_active = raid5_spare_active, | |
849674e4 | 9025 | .sync_request = raid5_sync_request, |
2604b703 | 9026 | .resize = raid5_resize, |
80c3a6ce | 9027 | .size = raid5_size, |
3d37890b N |
9028 | .check_reshape = raid5_check_reshape, |
9029 | .start_reshape = raid5_start_reshape, | |
cea9c228 | 9030 | .finish_reshape = raid5_finish_reshape, |
2604b703 | 9031 | .quiesce = raid5_quiesce, |
a78d38a1 | 9032 | .takeover = raid4_takeover, |
0bb0c105 | 9033 | .change_consistency_policy = raid5_change_consistency_policy, |
41425f96 | 9034 | .prepare_suspend = raid5_prepare_suspend, |
9c89f604 | 9035 | .bitmap_sector = raid5_bitmap_sector, |
2604b703 N |
9036 | }; |
9037 | ||
9038 | static int __init raid5_init(void) | |
9039 | { | |
29c6d1bb SAS |
9040 | int ret; |
9041 | ||
851c30c9 SL |
9042 | raid5_wq = alloc_workqueue("raid5wq", |
9043 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); | |
9044 | if (!raid5_wq) | |
9045 | return -ENOMEM; | |
29c6d1bb SAS |
9046 | |
9047 | ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, | |
9048 | "md/raid5:prepare", | |
9049 | raid456_cpu_up_prepare, | |
9050 | raid456_cpu_dead); | |
3d44e1d1 YK |
9051 | if (ret) |
9052 | goto err_destroy_wq; | |
9053 | ||
9054 | ret = register_md_submodule(&raid6_personality.head); | |
9055 | if (ret) | |
9056 | goto err_cpuhp_remove; | |
9057 | ||
9058 | ret = register_md_submodule(&raid5_personality.head); | |
9059 | if (ret) | |
9060 | goto err_unregister_raid6; | |
9061 | ||
9062 | ret = register_md_submodule(&raid4_personality.head); | |
9063 | if (ret) | |
9064 | goto err_unregister_raid5; | |
9065 | ||
2604b703 | 9066 | return 0; |
3d44e1d1 YK |
9067 | |
9068 | err_unregister_raid5: | |
9069 | unregister_md_submodule(&raid5_personality.head); | |
9070 | err_unregister_raid6: | |
9071 | unregister_md_submodule(&raid6_personality.head); | |
9072 | err_cpuhp_remove: | |
9073 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); | |
9074 | err_destroy_wq: | |
9075 | destroy_workqueue(raid5_wq); | |
9076 | return ret; | |
1da177e4 LT |
9077 | } |
9078 | ||
3d44e1d1 | 9079 | static void __exit raid5_exit(void) |
1da177e4 | 9080 | { |
3d44e1d1 YK |
9081 | unregister_md_submodule(&raid6_personality.head); |
9082 | unregister_md_submodule(&raid5_personality.head); | |
9083 | unregister_md_submodule(&raid4_personality.head); | |
29c6d1bb | 9084 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); |
851c30c9 | 9085 | destroy_workqueue(raid5_wq); |
1da177e4 LT |
9086 | } |
9087 | ||
9088 | module_init(raid5_init); | |
9089 | module_exit(raid5_exit); | |
9090 | MODULE_LICENSE("GPL"); | |
0efb9e61 | 9091 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
1da177e4 | 9092 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
d9d166c2 N |
9093 | MODULE_ALIAS("md-raid5"); |
9094 | MODULE_ALIAS("md-raid4"); | |
2604b703 N |
9095 | MODULE_ALIAS("md-level-5"); |
9096 | MODULE_ALIAS("md-level-4"); | |
16a53ecc N |
9097 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
9098 | MODULE_ALIAS("md-raid6"); | |
9099 | MODULE_ALIAS("md-level-6"); | |
9100 | ||
9101 | /* This used to be two separate modules, they were: */ | |
9102 | MODULE_ALIAS("raid5"); | |
9103 | MODULE_ALIAS("raid6"); |