Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * raid5.c : Multiple Devices driver for Linux | |
3 | * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman | |
4 | * Copyright (C) 1999, 2000 Ingo Molnar | |
16a53ecc | 5 | * Copyright (C) 2002, 2003 H. Peter Anvin |
1da177e4 | 6 | * |
16a53ecc N |
7 | * RAID-4/5/6 management functions. |
8 | * Thanks to Penguin Computing for making the RAID-6 development possible | |
9 | * by donating a test server! | |
1da177e4 LT |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2, or (at your option) | |
14 | * any later version. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * (for example /usr/src/linux/COPYING); if not, write to the Free | |
18 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
ae3c20cc N |
21 | /* |
22 | * BITMAP UNPLUGGING: | |
23 | * | |
24 | * The sequencing for updating the bitmap reliably is a little | |
25 | * subtle (and I got it wrong the first time) so it deserves some | |
26 | * explanation. | |
27 | * | |
28 | * We group bitmap updates into batches. Each batch has a number. | |
29 | * We may write out several batches at once, but that isn't very important. | |
7c13edc8 N |
30 | * conf->seq_write is the number of the last batch successfully written. |
31 | * conf->seq_flush is the number of the last batch that was closed to | |
ae3c20cc N |
32 | * new additions. |
33 | * When we discover that we will need to write to any block in a stripe | |
34 | * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq | |
7c13edc8 | 35 | * the number of the batch it will be in. This is seq_flush+1. |
ae3c20cc N |
36 | * When we are ready to do a write, if that batch hasn't been written yet, |
37 | * we plug the array and queue the stripe for later. | |
38 | * When an unplug happens, we increment bm_flush, thus closing the current | |
39 | * batch. | |
40 | * When we notice that bm_flush > bm_write, we write out all pending updates | |
41 | * to the bitmap, and advance bm_write to where bm_flush was. | |
42 | * This may occasionally write a bit out twice, but is sure never to | |
43 | * miss any bits. | |
44 | */ | |
1da177e4 | 45 | |
bff61975 | 46 | #include <linux/blkdev.h> |
f6705578 | 47 | #include <linux/kthread.h> |
f701d589 | 48 | #include <linux/raid/pq.h> |
91c00924 | 49 | #include <linux/async_tx.h> |
056075c7 | 50 | #include <linux/module.h> |
07a3b417 | 51 | #include <linux/async.h> |
bff61975 | 52 | #include <linux/seq_file.h> |
36d1c647 | 53 | #include <linux/cpu.h> |
5a0e3ad6 | 54 | #include <linux/slab.h> |
8bda470e | 55 | #include <linux/ratelimit.h> |
851c30c9 | 56 | #include <linux/nodemask.h> |
46d5b785 | 57 | #include <linux/flex_array.h> |
3f07c014 IM |
58 | #include <linux/sched/signal.h> |
59 | ||
a9add5d9 | 60 | #include <trace/events/block.h> |
aaf9f12e | 61 | #include <linux/list_sort.h> |
a9add5d9 | 62 | |
43b2e5d8 | 63 | #include "md.h" |
bff61975 | 64 | #include "raid5.h" |
54071b38 | 65 | #include "raid0.h" |
ef740c37 | 66 | #include "bitmap.h" |
ff875738 | 67 | #include "raid5-log.h" |
72626685 | 68 | |
394ed8e4 SL |
69 | #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED) |
70 | ||
851c30c9 SL |
71 | #define cpu_to_group(cpu) cpu_to_node(cpu) |
72 | #define ANY_GROUP NUMA_NO_NODE | |
73 | ||
8e0e99ba N |
74 | static bool devices_handle_discard_safely = false; |
75 | module_param(devices_handle_discard_safely, bool, 0644); | |
76 | MODULE_PARM_DESC(devices_handle_discard_safely, | |
77 | "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions"); | |
851c30c9 | 78 | static struct workqueue_struct *raid5_wq; |
1da177e4 | 79 | |
d1688a6d | 80 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
db298e19 N |
81 | { |
82 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; | |
83 | return &conf->stripe_hashtbl[hash]; | |
84 | } | |
1da177e4 | 85 | |
566c09c5 SL |
86 | static inline int stripe_hash_locks_hash(sector_t sect) |
87 | { | |
88 | return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK; | |
89 | } | |
90 | ||
91 | static inline void lock_device_hash_lock(struct r5conf *conf, int hash) | |
92 | { | |
93 | spin_lock_irq(conf->hash_locks + hash); | |
94 | spin_lock(&conf->device_lock); | |
95 | } | |
96 | ||
97 | static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) | |
98 | { | |
99 | spin_unlock(&conf->device_lock); | |
100 | spin_unlock_irq(conf->hash_locks + hash); | |
101 | } | |
102 | ||
103 | static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) | |
104 | { | |
105 | int i; | |
3d05f3ae | 106 | spin_lock_irq(conf->hash_locks); |
566c09c5 SL |
107 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
108 | spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); | |
109 | spin_lock(&conf->device_lock); | |
110 | } | |
111 | ||
112 | static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) | |
113 | { | |
114 | int i; | |
115 | spin_unlock(&conf->device_lock); | |
3d05f3ae JC |
116 | for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--) |
117 | spin_unlock(conf->hash_locks + i); | |
118 | spin_unlock_irq(conf->hash_locks); | |
566c09c5 SL |
119 | } |
120 | ||
d0dabf7e N |
121 | /* Find first data disk in a raid6 stripe */ |
122 | static inline int raid6_d0(struct stripe_head *sh) | |
123 | { | |
67cc2b81 N |
124 | if (sh->ddf_layout) |
125 | /* ddf always start from first device */ | |
126 | return 0; | |
127 | /* md starts just after Q block */ | |
d0dabf7e N |
128 | if (sh->qd_idx == sh->disks - 1) |
129 | return 0; | |
130 | else | |
131 | return sh->qd_idx + 1; | |
132 | } | |
16a53ecc N |
133 | static inline int raid6_next_disk(int disk, int raid_disks) |
134 | { | |
135 | disk++; | |
136 | return (disk < raid_disks) ? disk : 0; | |
137 | } | |
a4456856 | 138 | |
d0dabf7e N |
139 | /* When walking through the disks in a raid5, starting at raid6_d0, |
140 | * We need to map each disk to a 'slot', where the data disks are slot | |
141 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk | |
142 | * is raid_disks-1. This help does that mapping. | |
143 | */ | |
67cc2b81 N |
144 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
145 | int *count, int syndrome_disks) | |
d0dabf7e | 146 | { |
6629542e | 147 | int slot = *count; |
67cc2b81 | 148 | |
e4424fee | 149 | if (sh->ddf_layout) |
6629542e | 150 | (*count)++; |
d0dabf7e | 151 | if (idx == sh->pd_idx) |
67cc2b81 | 152 | return syndrome_disks; |
d0dabf7e | 153 | if (idx == sh->qd_idx) |
67cc2b81 | 154 | return syndrome_disks + 1; |
e4424fee | 155 | if (!sh->ddf_layout) |
6629542e | 156 | (*count)++; |
d0dabf7e N |
157 | return slot; |
158 | } | |
159 | ||
d1688a6d | 160 | static void print_raid5_conf (struct r5conf *conf); |
1da177e4 | 161 | |
600aa109 DW |
162 | static int stripe_operations_active(struct stripe_head *sh) |
163 | { | |
164 | return sh->check_state || sh->reconstruct_state || | |
165 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || | |
166 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
167 | } | |
168 | ||
535ae4eb SL |
169 | static bool stripe_is_lowprio(struct stripe_head *sh) |
170 | { | |
171 | return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) || | |
172 | test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) && | |
173 | !test_bit(STRIPE_R5C_CACHING, &sh->state); | |
174 | } | |
175 | ||
851c30c9 SL |
176 | static void raid5_wakeup_stripe_thread(struct stripe_head *sh) |
177 | { | |
178 | struct r5conf *conf = sh->raid_conf; | |
179 | struct r5worker_group *group; | |
bfc90cb0 | 180 | int thread_cnt; |
851c30c9 SL |
181 | int i, cpu = sh->cpu; |
182 | ||
183 | if (!cpu_online(cpu)) { | |
184 | cpu = cpumask_any(cpu_online_mask); | |
185 | sh->cpu = cpu; | |
186 | } | |
187 | ||
188 | if (list_empty(&sh->lru)) { | |
189 | struct r5worker_group *group; | |
190 | group = conf->worker_groups + cpu_to_group(cpu); | |
535ae4eb SL |
191 | if (stripe_is_lowprio(sh)) |
192 | list_add_tail(&sh->lru, &group->loprio_list); | |
193 | else | |
194 | list_add_tail(&sh->lru, &group->handle_list); | |
bfc90cb0 SL |
195 | group->stripes_cnt++; |
196 | sh->group = group; | |
851c30c9 SL |
197 | } |
198 | ||
199 | if (conf->worker_cnt_per_group == 0) { | |
200 | md_wakeup_thread(conf->mddev->thread); | |
201 | return; | |
202 | } | |
203 | ||
204 | group = conf->worker_groups + cpu_to_group(sh->cpu); | |
205 | ||
bfc90cb0 SL |
206 | group->workers[0].working = true; |
207 | /* at least one worker should run to avoid race */ | |
208 | queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); | |
209 | ||
210 | thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; | |
211 | /* wakeup more workers */ | |
212 | for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { | |
213 | if (group->workers[i].working == false) { | |
214 | group->workers[i].working = true; | |
215 | queue_work_on(sh->cpu, raid5_wq, | |
216 | &group->workers[i].work); | |
217 | thread_cnt--; | |
218 | } | |
219 | } | |
851c30c9 SL |
220 | } |
221 | ||
566c09c5 SL |
222 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, |
223 | struct list_head *temp_inactive_list) | |
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 SL |
235 | /* |
236 | * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with | |
237 | * data in journal, so they are not released to cached lists | |
238 | */ | |
239 | if (conf->quiesce && r5c_is_writeback(conf->log) && | |
240 | !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) { | |
241 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) | |
242 | r5c_make_stripe_write_out(sh); | |
243 | set_bit(STRIPE_HANDLE, &sh->state); | |
244 | } | |
1e6d690b | 245 | |
4eb788df SL |
246 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
247 | if (test_bit(STRIPE_DELAYED, &sh->state) && | |
ad3ab8b6 | 248 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
4eb788df | 249 | list_add_tail(&sh->lru, &conf->delayed_list); |
ad3ab8b6 | 250 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
4eb788df SL |
251 | sh->bm_seq - conf->seq_write > 0) |
252 | list_add_tail(&sh->lru, &conf->bitmap_list); | |
253 | else { | |
254 | clear_bit(STRIPE_DELAYED, &sh->state); | |
255 | clear_bit(STRIPE_BIT_DELAY, &sh->state); | |
851c30c9 | 256 | if (conf->worker_cnt_per_group == 0) { |
535ae4eb SL |
257 | if (stripe_is_lowprio(sh)) |
258 | list_add_tail(&sh->lru, | |
259 | &conf->loprio_list); | |
260 | else | |
261 | list_add_tail(&sh->lru, | |
262 | &conf->handle_list); | |
851c30c9 SL |
263 | } else { |
264 | raid5_wakeup_stripe_thread(sh); | |
265 | return; | |
266 | } | |
4eb788df SL |
267 | } |
268 | md_wakeup_thread(conf->mddev->thread); | |
269 | } else { | |
270 | BUG_ON(stripe_operations_active(sh)); | |
271 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
272 | if (atomic_dec_return(&conf->preread_active_stripes) | |
273 | < IO_THRESHOLD) | |
274 | md_wakeup_thread(conf->mddev->thread); | |
275 | atomic_dec(&conf->active_stripes); | |
1e6d690b SL |
276 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
277 | if (!r5c_is_writeback(conf->log)) | |
278 | list_add_tail(&sh->lru, temp_inactive_list); | |
279 | else { | |
280 | WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)); | |
281 | if (injournal == 0) | |
282 | list_add_tail(&sh->lru, temp_inactive_list); | |
283 | else if (injournal == conf->raid_disks - conf->max_degraded) { | |
284 | /* full stripe */ | |
285 | if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) | |
286 | atomic_inc(&conf->r5c_cached_full_stripes); | |
287 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) | |
288 | atomic_dec(&conf->r5c_cached_partial_stripes); | |
289 | list_add_tail(&sh->lru, &conf->r5c_full_stripe_list); | |
a39f7afd | 290 | r5c_check_cached_full_stripe(conf); |
03b047f4 SL |
291 | } else |
292 | /* | |
293 | * STRIPE_R5C_PARTIAL_STRIPE is set in | |
294 | * r5c_try_caching_write(). No need to | |
295 | * set it again. | |
296 | */ | |
1e6d690b | 297 | list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list); |
1e6d690b SL |
298 | } |
299 | } | |
1da177e4 LT |
300 | } |
301 | } | |
d0dabf7e | 302 | |
566c09c5 SL |
303 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, |
304 | struct list_head *temp_inactive_list) | |
4eb788df SL |
305 | { |
306 | if (atomic_dec_and_test(&sh->count)) | |
566c09c5 SL |
307 | do_release_stripe(conf, sh, temp_inactive_list); |
308 | } | |
309 | ||
310 | /* | |
311 | * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list | |
312 | * | |
313 | * Be careful: Only one task can add/delete stripes from temp_inactive_list at | |
314 | * given time. Adding stripes only takes device lock, while deleting stripes | |
315 | * only takes hash lock. | |
316 | */ | |
317 | static void release_inactive_stripe_list(struct r5conf *conf, | |
318 | struct list_head *temp_inactive_list, | |
319 | int hash) | |
320 | { | |
321 | int size; | |
6ab2a4b8 | 322 | bool do_wakeup = false; |
566c09c5 SL |
323 | unsigned long flags; |
324 | ||
325 | if (hash == NR_STRIPE_HASH_LOCKS) { | |
326 | size = NR_STRIPE_HASH_LOCKS; | |
327 | hash = NR_STRIPE_HASH_LOCKS - 1; | |
328 | } else | |
329 | size = 1; | |
330 | while (size) { | |
331 | struct list_head *list = &temp_inactive_list[size - 1]; | |
332 | ||
333 | /* | |
6d036f7d | 334 | * We don't hold any lock here yet, raid5_get_active_stripe() might |
566c09c5 SL |
335 | * remove stripes from the list |
336 | */ | |
337 | if (!list_empty_careful(list)) { | |
338 | spin_lock_irqsave(conf->hash_locks + hash, flags); | |
4bda556a SL |
339 | if (list_empty(conf->inactive_list + hash) && |
340 | !list_empty(list)) | |
341 | atomic_dec(&conf->empty_inactive_list_nr); | |
566c09c5 | 342 | list_splice_tail_init(list, conf->inactive_list + hash); |
6ab2a4b8 | 343 | do_wakeup = true; |
566c09c5 SL |
344 | spin_unlock_irqrestore(conf->hash_locks + hash, flags); |
345 | } | |
346 | size--; | |
347 | hash--; | |
348 | } | |
349 | ||
350 | if (do_wakeup) { | |
6ab2a4b8 | 351 | wake_up(&conf->wait_for_stripe); |
b1b46486 YL |
352 | if (atomic_read(&conf->active_stripes) == 0) |
353 | wake_up(&conf->wait_for_quiescent); | |
566c09c5 SL |
354 | if (conf->retry_read_aligned) |
355 | md_wakeup_thread(conf->mddev->thread); | |
356 | } | |
4eb788df SL |
357 | } |
358 | ||
773ca82f | 359 | /* should hold conf->device_lock already */ |
566c09c5 SL |
360 | static int release_stripe_list(struct r5conf *conf, |
361 | struct list_head *temp_inactive_list) | |
773ca82f | 362 | { |
eae8263f | 363 | struct stripe_head *sh, *t; |
773ca82f SL |
364 | int count = 0; |
365 | struct llist_node *head; | |
366 | ||
367 | head = llist_del_all(&conf->released_stripes); | |
d265d9dc | 368 | head = llist_reverse_order(head); |
eae8263f | 369 | llist_for_each_entry_safe(sh, t, head, release_list) { |
566c09c5 SL |
370 | int hash; |
371 | ||
773ca82f SL |
372 | /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */ |
373 | smp_mb(); | |
374 | clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state); | |
375 | /* | |
376 | * Don't worry the bit is set here, because if the bit is set | |
377 | * again, the count is always > 1. This is true for | |
378 | * STRIPE_ON_UNPLUG_LIST bit too. | |
379 | */ | |
566c09c5 SL |
380 | hash = sh->hash_lock_index; |
381 | __release_stripe(conf, sh, &temp_inactive_list[hash]); | |
773ca82f SL |
382 | count++; |
383 | } | |
384 | ||
385 | return count; | |
386 | } | |
387 | ||
6d036f7d | 388 | void raid5_release_stripe(struct stripe_head *sh) |
1da177e4 | 389 | { |
d1688a6d | 390 | struct r5conf *conf = sh->raid_conf; |
1da177e4 | 391 | unsigned long flags; |
566c09c5 SL |
392 | struct list_head list; |
393 | int hash; | |
773ca82f | 394 | bool wakeup; |
16a53ecc | 395 | |
cf170f3f ES |
396 | /* Avoid release_list until the last reference. |
397 | */ | |
398 | if (atomic_add_unless(&sh->count, -1, 1)) | |
399 | return; | |
400 | ||
ad4068de | 401 | if (unlikely(!conf->mddev->thread) || |
402 | test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state)) | |
773ca82f SL |
403 | goto slow_path; |
404 | wakeup = llist_add(&sh->release_list, &conf->released_stripes); | |
405 | if (wakeup) | |
406 | md_wakeup_thread(conf->mddev->thread); | |
407 | return; | |
408 | slow_path: | |
4eb788df | 409 | local_irq_save(flags); |
773ca82f | 410 | /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */ |
4eb788df | 411 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { |
566c09c5 SL |
412 | INIT_LIST_HEAD(&list); |
413 | hash = sh->hash_lock_index; | |
414 | do_release_stripe(conf, sh, &list); | |
4eb788df | 415 | spin_unlock(&conf->device_lock); |
566c09c5 | 416 | release_inactive_stripe_list(conf, &list, hash); |
4eb788df SL |
417 | } |
418 | local_irq_restore(flags); | |
1da177e4 LT |
419 | } |
420 | ||
fccddba0 | 421 | static inline void remove_hash(struct stripe_head *sh) |
1da177e4 | 422 | { |
45b4233c DW |
423 | pr_debug("remove_hash(), stripe %llu\n", |
424 | (unsigned long long)sh->sector); | |
1da177e4 | 425 | |
fccddba0 | 426 | hlist_del_init(&sh->hash); |
1da177e4 LT |
427 | } |
428 | ||
d1688a6d | 429 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
1da177e4 | 430 | { |
fccddba0 | 431 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
1da177e4 | 432 | |
45b4233c DW |
433 | pr_debug("insert_hash(), stripe %llu\n", |
434 | (unsigned long long)sh->sector); | |
1da177e4 | 435 | |
fccddba0 | 436 | hlist_add_head(&sh->hash, hp); |
1da177e4 LT |
437 | } |
438 | ||
1da177e4 | 439 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
566c09c5 | 440 | static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash) |
1da177e4 LT |
441 | { |
442 | struct stripe_head *sh = NULL; | |
443 | struct list_head *first; | |
444 | ||
566c09c5 | 445 | if (list_empty(conf->inactive_list + hash)) |
1da177e4 | 446 | goto out; |
566c09c5 | 447 | first = (conf->inactive_list + hash)->next; |
1da177e4 LT |
448 | sh = list_entry(first, struct stripe_head, lru); |
449 | list_del_init(first); | |
450 | remove_hash(sh); | |
451 | atomic_inc(&conf->active_stripes); | |
566c09c5 | 452 | BUG_ON(hash != sh->hash_lock_index); |
4bda556a SL |
453 | if (list_empty(conf->inactive_list + hash)) |
454 | atomic_inc(&conf->empty_inactive_list_nr); | |
1da177e4 LT |
455 | out: |
456 | return sh; | |
457 | } | |
458 | ||
e4e11e38 | 459 | static void shrink_buffers(struct stripe_head *sh) |
1da177e4 LT |
460 | { |
461 | struct page *p; | |
462 | int i; | |
e4e11e38 | 463 | int num = sh->raid_conf->pool_size; |
1da177e4 | 464 | |
e4e11e38 | 465 | for (i = 0; i < num ; i++) { |
d592a996 | 466 | WARN_ON(sh->dev[i].page != sh->dev[i].orig_page); |
1da177e4 LT |
467 | p = sh->dev[i].page; |
468 | if (!p) | |
469 | continue; | |
470 | sh->dev[i].page = NULL; | |
2d1f3b5d | 471 | put_page(p); |
1da177e4 LT |
472 | } |
473 | } | |
474 | ||
a9683a79 | 475 | static int grow_buffers(struct stripe_head *sh, gfp_t gfp) |
1da177e4 LT |
476 | { |
477 | int i; | |
e4e11e38 | 478 | int num = sh->raid_conf->pool_size; |
1da177e4 | 479 | |
e4e11e38 | 480 | for (i = 0; i < num; i++) { |
1da177e4 LT |
481 | struct page *page; |
482 | ||
a9683a79 | 483 | if (!(page = alloc_page(gfp))) { |
1da177e4 LT |
484 | return 1; |
485 | } | |
486 | sh->dev[i].page = page; | |
d592a996 | 487 | sh->dev[i].orig_page = page; |
1da177e4 | 488 | } |
3418d036 | 489 | |
1da177e4 LT |
490 | return 0; |
491 | } | |
492 | ||
784052ec | 493 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
d1688a6d | 494 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
911d4ee8 | 495 | struct stripe_head *sh); |
1da177e4 | 496 | |
b5663ba4 | 497 | static void init_stripe(struct stripe_head *sh, sector_t sector, int previous) |
1da177e4 | 498 | { |
d1688a6d | 499 | struct r5conf *conf = sh->raid_conf; |
566c09c5 | 500 | int i, seq; |
1da177e4 | 501 | |
78bafebd ES |
502 | BUG_ON(atomic_read(&sh->count) != 0); |
503 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); | |
600aa109 | 504 | BUG_ON(stripe_operations_active(sh)); |
59fc630b | 505 | BUG_ON(sh->batch_head); |
d84e0f10 | 506 | |
45b4233c | 507 | pr_debug("init_stripe called, stripe %llu\n", |
b8e6a15a | 508 | (unsigned long long)sector); |
566c09c5 SL |
509 | retry: |
510 | seq = read_seqcount_begin(&conf->gen_lock); | |
86b42c71 | 511 | sh->generation = conf->generation - previous; |
b5663ba4 | 512 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
1da177e4 | 513 | sh->sector = sector; |
911d4ee8 | 514 | stripe_set_idx(sector, conf, previous, sh); |
1da177e4 LT |
515 | sh->state = 0; |
516 | ||
7ecaa1e6 | 517 | for (i = sh->disks; i--; ) { |
1da177e4 LT |
518 | struct r5dev *dev = &sh->dev[i]; |
519 | ||
d84e0f10 | 520 | if (dev->toread || dev->read || dev->towrite || dev->written || |
1da177e4 | 521 | test_bit(R5_LOCKED, &dev->flags)) { |
cc6167b4 | 522 | pr_err("sector=%llx i=%d %p %p %p %p %d\n", |
1da177e4 | 523 | (unsigned long long)sh->sector, i, dev->toread, |
d84e0f10 | 524 | dev->read, dev->towrite, dev->written, |
1da177e4 | 525 | test_bit(R5_LOCKED, &dev->flags)); |
8cfa7b0f | 526 | WARN_ON(1); |
1da177e4 LT |
527 | } |
528 | dev->flags = 0; | |
784052ec | 529 | raid5_build_block(sh, i, previous); |
1da177e4 | 530 | } |
566c09c5 SL |
531 | if (read_seqcount_retry(&conf->gen_lock, seq)) |
532 | goto retry; | |
7a87f434 | 533 | sh->overwrite_disks = 0; |
1da177e4 | 534 | insert_hash(conf, sh); |
851c30c9 | 535 | sh->cpu = smp_processor_id(); |
da41ba65 | 536 | set_bit(STRIPE_BATCH_READY, &sh->state); |
1da177e4 LT |
537 | } |
538 | ||
d1688a6d | 539 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
86b42c71 | 540 | short generation) |
1da177e4 LT |
541 | { |
542 | struct stripe_head *sh; | |
543 | ||
45b4233c | 544 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
b67bfe0d | 545 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
86b42c71 | 546 | if (sh->sector == sector && sh->generation == generation) |
1da177e4 | 547 | return sh; |
45b4233c | 548 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
1da177e4 LT |
549 | return NULL; |
550 | } | |
551 | ||
674806d6 N |
552 | /* |
553 | * Need to check if array has failed when deciding whether to: | |
554 | * - start an array | |
555 | * - remove non-faulty devices | |
556 | * - add a spare | |
557 | * - allow a reshape | |
558 | * This determination is simple when no reshape is happening. | |
559 | * However if there is a reshape, we need to carefully check | |
560 | * both the before and after sections. | |
561 | * This is because some failed devices may only affect one | |
562 | * of the two sections, and some non-in_sync devices may | |
563 | * be insync in the section most affected by failed devices. | |
564 | */ | |
2e38a37f | 565 | int raid5_calc_degraded(struct r5conf *conf) |
674806d6 | 566 | { |
908f4fbd | 567 | int degraded, degraded2; |
674806d6 | 568 | int i; |
674806d6 N |
569 | |
570 | rcu_read_lock(); | |
571 | degraded = 0; | |
572 | for (i = 0; i < conf->previous_raid_disks; i++) { | |
3cb03002 | 573 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
e5c86471 N |
574 | if (rdev && test_bit(Faulty, &rdev->flags)) |
575 | rdev = rcu_dereference(conf->disks[i].replacement); | |
674806d6 N |
576 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
577 | degraded++; | |
578 | else if (test_bit(In_sync, &rdev->flags)) | |
579 | ; | |
580 | else | |
581 | /* not in-sync or faulty. | |
582 | * If the reshape increases the number of devices, | |
583 | * this is being recovered by the reshape, so | |
584 | * this 'previous' section is not in_sync. | |
585 | * If the number of devices is being reduced however, | |
586 | * the device can only be part of the array if | |
587 | * we are reverting a reshape, so this section will | |
588 | * be in-sync. | |
589 | */ | |
590 | if (conf->raid_disks >= conf->previous_raid_disks) | |
591 | degraded++; | |
592 | } | |
593 | rcu_read_unlock(); | |
908f4fbd N |
594 | if (conf->raid_disks == conf->previous_raid_disks) |
595 | return degraded; | |
674806d6 | 596 | rcu_read_lock(); |
908f4fbd | 597 | degraded2 = 0; |
674806d6 | 598 | for (i = 0; i < conf->raid_disks; i++) { |
3cb03002 | 599 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
e5c86471 N |
600 | if (rdev && test_bit(Faulty, &rdev->flags)) |
601 | rdev = rcu_dereference(conf->disks[i].replacement); | |
674806d6 | 602 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
908f4fbd | 603 | degraded2++; |
674806d6 N |
604 | else if (test_bit(In_sync, &rdev->flags)) |
605 | ; | |
606 | else | |
607 | /* not in-sync or faulty. | |
608 | * If reshape increases the number of devices, this | |
609 | * section has already been recovered, else it | |
610 | * almost certainly hasn't. | |
611 | */ | |
612 | if (conf->raid_disks <= conf->previous_raid_disks) | |
908f4fbd | 613 | degraded2++; |
674806d6 N |
614 | } |
615 | rcu_read_unlock(); | |
908f4fbd N |
616 | if (degraded2 > degraded) |
617 | return degraded2; | |
618 | return degraded; | |
619 | } | |
620 | ||
621 | static int has_failed(struct r5conf *conf) | |
622 | { | |
623 | int degraded; | |
624 | ||
625 | if (conf->mddev->reshape_position == MaxSector) | |
626 | return conf->mddev->degraded > conf->max_degraded; | |
627 | ||
2e38a37f | 628 | degraded = raid5_calc_degraded(conf); |
674806d6 N |
629 | if (degraded > conf->max_degraded) |
630 | return 1; | |
631 | return 0; | |
632 | } | |
633 | ||
6d036f7d SL |
634 | struct stripe_head * |
635 | raid5_get_active_stripe(struct r5conf *conf, sector_t sector, | |
636 | int previous, int noblock, int noquiesce) | |
1da177e4 LT |
637 | { |
638 | struct stripe_head *sh; | |
566c09c5 | 639 | int hash = stripe_hash_locks_hash(sector); |
ff00d3b4 | 640 | int inc_empty_inactive_list_flag; |
1da177e4 | 641 | |
45b4233c | 642 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
1da177e4 | 643 | |
566c09c5 | 644 | spin_lock_irq(conf->hash_locks + hash); |
1da177e4 LT |
645 | |
646 | do { | |
b1b46486 | 647 | wait_event_lock_irq(conf->wait_for_quiescent, |
a8c906ca | 648 | conf->quiesce == 0 || noquiesce, |
566c09c5 | 649 | *(conf->hash_locks + hash)); |
86b42c71 | 650 | sh = __find_stripe(conf, sector, conf->generation - previous); |
1da177e4 | 651 | if (!sh) { |
edbe83ab | 652 | if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) { |
566c09c5 | 653 | sh = get_free_stripe(conf, hash); |
713bc5c2 SL |
654 | if (!sh && !test_bit(R5_DID_ALLOC, |
655 | &conf->cache_state)) | |
edbe83ab N |
656 | set_bit(R5_ALLOC_MORE, |
657 | &conf->cache_state); | |
658 | } | |
1da177e4 LT |
659 | if (noblock && sh == NULL) |
660 | break; | |
a39f7afd SL |
661 | |
662 | r5c_check_stripe_cache_usage(conf); | |
1da177e4 | 663 | if (!sh) { |
5423399a N |
664 | set_bit(R5_INACTIVE_BLOCKED, |
665 | &conf->cache_state); | |
a39f7afd | 666 | r5l_wake_reclaim(conf->log, 0); |
6ab2a4b8 SL |
667 | wait_event_lock_irq( |
668 | conf->wait_for_stripe, | |
566c09c5 SL |
669 | !list_empty(conf->inactive_list + hash) && |
670 | (atomic_read(&conf->active_stripes) | |
671 | < (conf->max_nr_stripes * 3 / 4) | |
5423399a N |
672 | || !test_bit(R5_INACTIVE_BLOCKED, |
673 | &conf->cache_state)), | |
6ab2a4b8 | 674 | *(conf->hash_locks + hash)); |
5423399a N |
675 | clear_bit(R5_INACTIVE_BLOCKED, |
676 | &conf->cache_state); | |
7da9d450 | 677 | } else { |
b5663ba4 | 678 | init_stripe(sh, sector, previous); |
7da9d450 N |
679 | atomic_inc(&sh->count); |
680 | } | |
e240c183 | 681 | } else if (!atomic_inc_not_zero(&sh->count)) { |
6d183de4 | 682 | spin_lock(&conf->device_lock); |
e240c183 | 683 | if (!atomic_read(&sh->count)) { |
1da177e4 LT |
684 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
685 | atomic_inc(&conf->active_stripes); | |
5af9bef7 N |
686 | BUG_ON(list_empty(&sh->lru) && |
687 | !test_bit(STRIPE_EXPANDING, &sh->state)); | |
ff00d3b4 ZL |
688 | inc_empty_inactive_list_flag = 0; |
689 | if (!list_empty(conf->inactive_list + hash)) | |
690 | inc_empty_inactive_list_flag = 1; | |
16a53ecc | 691 | list_del_init(&sh->lru); |
ff00d3b4 ZL |
692 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
693 | atomic_inc(&conf->empty_inactive_list_nr); | |
bfc90cb0 SL |
694 | if (sh->group) { |
695 | sh->group->stripes_cnt--; | |
696 | sh->group = NULL; | |
697 | } | |
1da177e4 | 698 | } |
7da9d450 | 699 | atomic_inc(&sh->count); |
6d183de4 | 700 | spin_unlock(&conf->device_lock); |
1da177e4 LT |
701 | } |
702 | } while (sh == NULL); | |
703 | ||
566c09c5 | 704 | spin_unlock_irq(conf->hash_locks + hash); |
1da177e4 LT |
705 | return sh; |
706 | } | |
707 | ||
7a87f434 | 708 | static bool is_full_stripe_write(struct stripe_head *sh) |
709 | { | |
710 | BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded)); | |
711 | return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded); | |
712 | } | |
713 | ||
59fc630b | 714 | static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
715 | { | |
59fc630b | 716 | if (sh1 > sh2) { |
3d05f3ae | 717 | spin_lock_irq(&sh2->stripe_lock); |
59fc630b | 718 | spin_lock_nested(&sh1->stripe_lock, 1); |
719 | } else { | |
3d05f3ae | 720 | spin_lock_irq(&sh1->stripe_lock); |
59fc630b | 721 | spin_lock_nested(&sh2->stripe_lock, 1); |
722 | } | |
723 | } | |
724 | ||
725 | static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) | |
726 | { | |
727 | spin_unlock(&sh1->stripe_lock); | |
3d05f3ae | 728 | spin_unlock_irq(&sh2->stripe_lock); |
59fc630b | 729 | } |
730 | ||
731 | /* Only freshly new full stripe normal write stripe can be added to a batch list */ | |
732 | static bool stripe_can_batch(struct stripe_head *sh) | |
733 | { | |
9c3e333d SL |
734 | struct r5conf *conf = sh->raid_conf; |
735 | ||
3418d036 | 736 | if (conf->log || raid5_has_ppl(conf)) |
9c3e333d | 737 | return false; |
59fc630b | 738 | return test_bit(STRIPE_BATCH_READY, &sh->state) && |
d0852df5 | 739 | !test_bit(STRIPE_BITMAP_PENDING, &sh->state) && |
59fc630b | 740 | is_full_stripe_write(sh); |
741 | } | |
742 | ||
743 | /* we only do back search */ | |
744 | static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh) | |
745 | { | |
746 | struct stripe_head *head; | |
747 | sector_t head_sector, tmp_sec; | |
748 | int hash; | |
749 | int dd_idx; | |
ff00d3b4 | 750 | int inc_empty_inactive_list_flag; |
59fc630b | 751 | |
59fc630b | 752 | /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */ |
753 | tmp_sec = sh->sector; | |
754 | if (!sector_div(tmp_sec, conf->chunk_sectors)) | |
755 | return; | |
756 | head_sector = sh->sector - STRIPE_SECTORS; | |
757 | ||
758 | hash = stripe_hash_locks_hash(head_sector); | |
759 | spin_lock_irq(conf->hash_locks + hash); | |
760 | head = __find_stripe(conf, head_sector, conf->generation); | |
761 | if (head && !atomic_inc_not_zero(&head->count)) { | |
762 | spin_lock(&conf->device_lock); | |
763 | if (!atomic_read(&head->count)) { | |
764 | if (!test_bit(STRIPE_HANDLE, &head->state)) | |
765 | atomic_inc(&conf->active_stripes); | |
766 | BUG_ON(list_empty(&head->lru) && | |
767 | !test_bit(STRIPE_EXPANDING, &head->state)); | |
ff00d3b4 ZL |
768 | inc_empty_inactive_list_flag = 0; |
769 | if (!list_empty(conf->inactive_list + hash)) | |
770 | inc_empty_inactive_list_flag = 1; | |
59fc630b | 771 | list_del_init(&head->lru); |
ff00d3b4 ZL |
772 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
773 | atomic_inc(&conf->empty_inactive_list_nr); | |
59fc630b | 774 | if (head->group) { |
775 | head->group->stripes_cnt--; | |
776 | head->group = NULL; | |
777 | } | |
778 | } | |
779 | atomic_inc(&head->count); | |
780 | spin_unlock(&conf->device_lock); | |
781 | } | |
782 | spin_unlock_irq(conf->hash_locks + hash); | |
783 | ||
784 | if (!head) | |
785 | return; | |
786 | if (!stripe_can_batch(head)) | |
787 | goto out; | |
788 | ||
789 | lock_two_stripes(head, sh); | |
790 | /* clear_batch_ready clear the flag */ | |
791 | if (!stripe_can_batch(head) || !stripe_can_batch(sh)) | |
792 | goto unlock_out; | |
793 | ||
794 | if (sh->batch_head) | |
795 | goto unlock_out; | |
796 | ||
797 | dd_idx = 0; | |
798 | while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) | |
799 | dd_idx++; | |
1eff9d32 | 800 | if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf || |
796a5cf0 | 801 | bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite)) |
59fc630b | 802 | goto unlock_out; |
803 | ||
804 | if (head->batch_head) { | |
805 | spin_lock(&head->batch_head->batch_lock); | |
806 | /* This batch list is already running */ | |
807 | if (!stripe_can_batch(head)) { | |
808 | spin_unlock(&head->batch_head->batch_lock); | |
809 | goto unlock_out; | |
810 | } | |
811 | ||
812 | /* | |
813 | * at this point, head's BATCH_READY could be cleared, but we | |
814 | * can still add the stripe to batch list | |
815 | */ | |
816 | list_add(&sh->batch_list, &head->batch_list); | |
817 | spin_unlock(&head->batch_head->batch_lock); | |
818 | ||
819 | sh->batch_head = head->batch_head; | |
820 | } else { | |
821 | head->batch_head = head; | |
822 | sh->batch_head = head->batch_head; | |
823 | spin_lock(&head->batch_lock); | |
824 | list_add_tail(&sh->batch_list, &head->batch_list); | |
825 | spin_unlock(&head->batch_lock); | |
826 | } | |
827 | ||
828 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
829 | if (atomic_dec_return(&conf->preread_active_stripes) | |
830 | < IO_THRESHOLD) | |
831 | md_wakeup_thread(conf->mddev->thread); | |
832 | ||
2b6b2457 N |
833 | if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) { |
834 | int seq = sh->bm_seq; | |
835 | if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) && | |
836 | sh->batch_head->bm_seq > seq) | |
837 | seq = sh->batch_head->bm_seq; | |
838 | set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state); | |
839 | sh->batch_head->bm_seq = seq; | |
840 | } | |
841 | ||
59fc630b | 842 | atomic_inc(&sh->count); |
843 | unlock_out: | |
844 | unlock_two_stripes(head, sh); | |
845 | out: | |
6d036f7d | 846 | raid5_release_stripe(head); |
59fc630b | 847 | } |
848 | ||
05616be5 N |
849 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
850 | * in this stripe_head. | |
851 | */ | |
852 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) | |
853 | { | |
854 | sector_t progress = conf->reshape_progress; | |
855 | /* Need a memory barrier to make sure we see the value | |
856 | * of conf->generation, or ->data_offset that was set before | |
857 | * reshape_progress was updated. | |
858 | */ | |
859 | smp_rmb(); | |
860 | if (progress == MaxSector) | |
861 | return 0; | |
862 | if (sh->generation == conf->generation - 1) | |
863 | return 0; | |
864 | /* We are in a reshape, and this is a new-generation stripe, | |
865 | * so use new_data_offset. | |
866 | */ | |
867 | return 1; | |
868 | } | |
869 | ||
aaf9f12e | 870 | static void dispatch_bio_list(struct bio_list *tmp) |
765d704d | 871 | { |
765d704d SL |
872 | struct bio *bio; |
873 | ||
aaf9f12e SL |
874 | while ((bio = bio_list_pop(tmp))) |
875 | generic_make_request(bio); | |
876 | } | |
877 | ||
878 | static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b) | |
879 | { | |
880 | const struct r5pending_data *da = list_entry(a, | |
881 | struct r5pending_data, sibling); | |
882 | const struct r5pending_data *db = list_entry(b, | |
883 | struct r5pending_data, sibling); | |
884 | if (da->sector > db->sector) | |
885 | return 1; | |
886 | if (da->sector < db->sector) | |
887 | return -1; | |
888 | return 0; | |
889 | } | |
890 | ||
891 | static void dispatch_defer_bios(struct r5conf *conf, int target, | |
892 | struct bio_list *list) | |
893 | { | |
894 | struct r5pending_data *data; | |
895 | struct list_head *first, *next = NULL; | |
896 | int cnt = 0; | |
897 | ||
898 | if (conf->pending_data_cnt == 0) | |
899 | return; | |
900 | ||
901 | list_sort(NULL, &conf->pending_list, cmp_stripe); | |
902 | ||
903 | first = conf->pending_list.next; | |
904 | ||
905 | /* temporarily move the head */ | |
906 | if (conf->next_pending_data) | |
907 | list_move_tail(&conf->pending_list, | |
908 | &conf->next_pending_data->sibling); | |
909 | ||
910 | while (!list_empty(&conf->pending_list)) { | |
911 | data = list_first_entry(&conf->pending_list, | |
912 | struct r5pending_data, sibling); | |
913 | if (&data->sibling == first) | |
914 | first = data->sibling.next; | |
915 | next = data->sibling.next; | |
916 | ||
917 | bio_list_merge(list, &data->bios); | |
918 | list_move(&data->sibling, &conf->free_list); | |
919 | cnt++; | |
920 | if (cnt >= target) | |
921 | break; | |
922 | } | |
923 | conf->pending_data_cnt -= cnt; | |
924 | BUG_ON(conf->pending_data_cnt < 0 || cnt < target); | |
925 | ||
926 | if (next != &conf->pending_list) | |
927 | conf->next_pending_data = list_entry(next, | |
928 | struct r5pending_data, sibling); | |
929 | else | |
930 | conf->next_pending_data = NULL; | |
931 | /* list isn't empty */ | |
932 | if (first != &conf->pending_list) | |
933 | list_move_tail(&conf->pending_list, first); | |
934 | } | |
935 | ||
936 | static void flush_deferred_bios(struct r5conf *conf) | |
937 | { | |
938 | struct bio_list tmp = BIO_EMPTY_LIST; | |
939 | ||
940 | if (conf->pending_data_cnt == 0) | |
765d704d SL |
941 | return; |
942 | ||
765d704d | 943 | spin_lock(&conf->pending_bios_lock); |
aaf9f12e SL |
944 | dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp); |
945 | BUG_ON(conf->pending_data_cnt != 0); | |
765d704d SL |
946 | spin_unlock(&conf->pending_bios_lock); |
947 | ||
aaf9f12e | 948 | dispatch_bio_list(&tmp); |
765d704d SL |
949 | } |
950 | ||
aaf9f12e SL |
951 | static void defer_issue_bios(struct r5conf *conf, sector_t sector, |
952 | struct bio_list *bios) | |
765d704d | 953 | { |
aaf9f12e SL |
954 | struct bio_list tmp = BIO_EMPTY_LIST; |
955 | struct r5pending_data *ent; | |
956 | ||
765d704d | 957 | spin_lock(&conf->pending_bios_lock); |
aaf9f12e SL |
958 | ent = list_first_entry(&conf->free_list, struct r5pending_data, |
959 | sibling); | |
960 | list_move_tail(&ent->sibling, &conf->pending_list); | |
961 | ent->sector = sector; | |
962 | bio_list_init(&ent->bios); | |
963 | bio_list_merge(&ent->bios, bios); | |
964 | conf->pending_data_cnt++; | |
965 | if (conf->pending_data_cnt >= PENDING_IO_MAX) | |
966 | dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp); | |
967 | ||
765d704d | 968 | spin_unlock(&conf->pending_bios_lock); |
aaf9f12e SL |
969 | |
970 | dispatch_bio_list(&tmp); | |
765d704d SL |
971 | } |
972 | ||
6712ecf8 | 973 | static void |
4246a0b6 | 974 | raid5_end_read_request(struct bio *bi); |
6712ecf8 | 975 | static void |
4246a0b6 | 976 | raid5_end_write_request(struct bio *bi); |
91c00924 | 977 | |
c4e5ac0a | 978 | static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s) |
91c00924 | 979 | { |
d1688a6d | 980 | struct r5conf *conf = sh->raid_conf; |
91c00924 | 981 | int i, disks = sh->disks; |
59fc630b | 982 | struct stripe_head *head_sh = sh; |
aaf9f12e SL |
983 | struct bio_list pending_bios = BIO_EMPTY_LIST; |
984 | bool should_defer; | |
91c00924 DW |
985 | |
986 | might_sleep(); | |
987 | ||
ff875738 AP |
988 | if (log_stripe(sh, s) == 0) |
989 | return; | |
1e6d690b | 990 | |
aaf9f12e | 991 | should_defer = conf->batch_bio_dispatch && conf->group_cnt; |
1e6d690b | 992 | |
91c00924 | 993 | for (i = disks; i--; ) { |
796a5cf0 | 994 | int op, op_flags = 0; |
9a3e1101 | 995 | int replace_only = 0; |
977df362 N |
996 | struct bio *bi, *rbi; |
997 | struct md_rdev *rdev, *rrdev = NULL; | |
59fc630b | 998 | |
999 | sh = head_sh; | |
e9c7469b | 1000 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
796a5cf0 | 1001 | op = REQ_OP_WRITE; |
e9c7469b | 1002 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
70fd7614 | 1003 | op_flags = REQ_FUA; |
9e444768 | 1004 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
796a5cf0 | 1005 | op = REQ_OP_DISCARD; |
e9c7469b | 1006 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
796a5cf0 | 1007 | op = REQ_OP_READ; |
9a3e1101 N |
1008 | else if (test_and_clear_bit(R5_WantReplace, |
1009 | &sh->dev[i].flags)) { | |
796a5cf0 | 1010 | op = REQ_OP_WRITE; |
9a3e1101 N |
1011 | replace_only = 1; |
1012 | } else | |
91c00924 | 1013 | continue; |
bc0934f0 | 1014 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
796a5cf0 | 1015 | op_flags |= REQ_SYNC; |
91c00924 | 1016 | |
59fc630b | 1017 | again: |
91c00924 | 1018 | bi = &sh->dev[i].req; |
977df362 | 1019 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
91c00924 | 1020 | |
91c00924 | 1021 | rcu_read_lock(); |
9a3e1101 | 1022 | rrdev = rcu_dereference(conf->disks[i].replacement); |
dd054fce N |
1023 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
1024 | rdev = rcu_dereference(conf->disks[i].rdev); | |
1025 | if (!rdev) { | |
1026 | rdev = rrdev; | |
1027 | rrdev = NULL; | |
1028 | } | |
796a5cf0 | 1029 | if (op_is_write(op)) { |
9a3e1101 N |
1030 | if (replace_only) |
1031 | rdev = NULL; | |
dd054fce N |
1032 | if (rdev == rrdev) |
1033 | /* We raced and saw duplicates */ | |
1034 | rrdev = NULL; | |
9a3e1101 | 1035 | } else { |
59fc630b | 1036 | if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) |
9a3e1101 N |
1037 | rdev = rrdev; |
1038 | rrdev = NULL; | |
1039 | } | |
977df362 | 1040 | |
91c00924 DW |
1041 | if (rdev && test_bit(Faulty, &rdev->flags)) |
1042 | rdev = NULL; | |
1043 | if (rdev) | |
1044 | atomic_inc(&rdev->nr_pending); | |
977df362 N |
1045 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
1046 | rrdev = NULL; | |
1047 | if (rrdev) | |
1048 | atomic_inc(&rrdev->nr_pending); | |
91c00924 DW |
1049 | rcu_read_unlock(); |
1050 | ||
73e92e51 | 1051 | /* We have already checked bad blocks for reads. Now |
977df362 N |
1052 | * need to check for writes. We never accept write errors |
1053 | * on the replacement, so we don't to check rrdev. | |
73e92e51 | 1054 | */ |
796a5cf0 | 1055 | while (op_is_write(op) && rdev && |
73e92e51 N |
1056 | test_bit(WriteErrorSeen, &rdev->flags)) { |
1057 | sector_t first_bad; | |
1058 | int bad_sectors; | |
1059 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, | |
1060 | &first_bad, &bad_sectors); | |
1061 | if (!bad) | |
1062 | break; | |
1063 | ||
1064 | if (bad < 0) { | |
1065 | set_bit(BlockedBadBlocks, &rdev->flags); | |
1066 | if (!conf->mddev->external && | |
2953079c | 1067 | conf->mddev->sb_flags) { |
73e92e51 N |
1068 | /* It is very unlikely, but we might |
1069 | * still need to write out the | |
1070 | * bad block log - better give it | |
1071 | * a chance*/ | |
1072 | md_check_recovery(conf->mddev); | |
1073 | } | |
1850753d | 1074 | /* |
1075 | * Because md_wait_for_blocked_rdev | |
1076 | * will dec nr_pending, we must | |
1077 | * increment it first. | |
1078 | */ | |
1079 | atomic_inc(&rdev->nr_pending); | |
73e92e51 N |
1080 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
1081 | } else { | |
1082 | /* Acknowledged bad block - skip the write */ | |
1083 | rdev_dec_pending(rdev, conf->mddev); | |
1084 | rdev = NULL; | |
1085 | } | |
1086 | } | |
1087 | ||
91c00924 | 1088 | if (rdev) { |
9a3e1101 N |
1089 | if (s->syncing || s->expanding || s->expanded |
1090 | || s->replacing) | |
91c00924 DW |
1091 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
1092 | ||
2b7497f0 DW |
1093 | set_bit(STRIPE_IO_STARTED, &sh->state); |
1094 | ||
91c00924 | 1095 | bi->bi_bdev = rdev->bdev; |
796a5cf0 MC |
1096 | bio_set_op_attrs(bi, op, op_flags); |
1097 | bi->bi_end_io = op_is_write(op) | |
2f6db2a7 KO |
1098 | ? raid5_end_write_request |
1099 | : raid5_end_read_request; | |
1100 | bi->bi_private = sh; | |
1101 | ||
6296b960 | 1102 | pr_debug("%s: for %llu schedule op %d on disc %d\n", |
e46b272b | 1103 | __func__, (unsigned long long)sh->sector, |
1eff9d32 | 1104 | bi->bi_opf, i); |
91c00924 | 1105 | atomic_inc(&sh->count); |
59fc630b | 1106 | if (sh != head_sh) |
1107 | atomic_inc(&head_sh->count); | |
05616be5 | 1108 | if (use_new_offset(conf, sh)) |
4f024f37 | 1109 | bi->bi_iter.bi_sector = (sh->sector |
05616be5 N |
1110 | + rdev->new_data_offset); |
1111 | else | |
4f024f37 | 1112 | bi->bi_iter.bi_sector = (sh->sector |
05616be5 | 1113 | + rdev->data_offset); |
59fc630b | 1114 | if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) |
1eff9d32 | 1115 | bi->bi_opf |= REQ_NOMERGE; |
3f9e7c14 | 1116 | |
d592a996 SL |
1117 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
1118 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
86aa1397 SL |
1119 | |
1120 | if (!op_is_write(op) && | |
1121 | test_bit(R5_InJournal, &sh->dev[i].flags)) | |
1122 | /* | |
1123 | * issuing read for a page in journal, this | |
1124 | * must be preparing for prexor in rmw; read | |
1125 | * the data into orig_page | |
1126 | */ | |
1127 | sh->dev[i].vec.bv_page = sh->dev[i].orig_page; | |
1128 | else | |
1129 | sh->dev[i].vec.bv_page = sh->dev[i].page; | |
4997b72e | 1130 | bi->bi_vcnt = 1; |
91c00924 DW |
1131 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
1132 | bi->bi_io_vec[0].bv_offset = 0; | |
4f024f37 | 1133 | bi->bi_iter.bi_size = STRIPE_SIZE; |
37c61ff3 SL |
1134 | /* |
1135 | * If this is discard request, set bi_vcnt 0. We don't | |
1136 | * want to confuse SCSI because SCSI will replace payload | |
1137 | */ | |
796a5cf0 | 1138 | if (op == REQ_OP_DISCARD) |
37c61ff3 | 1139 | bi->bi_vcnt = 0; |
977df362 N |
1140 | if (rrdev) |
1141 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); | |
e3620a3a JB |
1142 | |
1143 | if (conf->mddev->gendisk) | |
1144 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), | |
1145 | bi, disk_devt(conf->mddev->gendisk), | |
1146 | sh->dev[i].sector); | |
aaf9f12e SL |
1147 | if (should_defer && op_is_write(op)) |
1148 | bio_list_add(&pending_bios, bi); | |
1149 | else | |
1150 | generic_make_request(bi); | |
977df362 N |
1151 | } |
1152 | if (rrdev) { | |
9a3e1101 N |
1153 | if (s->syncing || s->expanding || s->expanded |
1154 | || s->replacing) | |
977df362 N |
1155 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
1156 | ||
1157 | set_bit(STRIPE_IO_STARTED, &sh->state); | |
1158 | ||
1159 | rbi->bi_bdev = rrdev->bdev; | |
796a5cf0 MC |
1160 | bio_set_op_attrs(rbi, op, op_flags); |
1161 | BUG_ON(!op_is_write(op)); | |
2f6db2a7 KO |
1162 | rbi->bi_end_io = raid5_end_write_request; |
1163 | rbi->bi_private = sh; | |
1164 | ||
6296b960 | 1165 | pr_debug("%s: for %llu schedule op %d on " |
977df362 N |
1166 | "replacement disc %d\n", |
1167 | __func__, (unsigned long long)sh->sector, | |
1eff9d32 | 1168 | rbi->bi_opf, i); |
977df362 | 1169 | atomic_inc(&sh->count); |
59fc630b | 1170 | if (sh != head_sh) |
1171 | atomic_inc(&head_sh->count); | |
05616be5 | 1172 | if (use_new_offset(conf, sh)) |
4f024f37 | 1173 | rbi->bi_iter.bi_sector = (sh->sector |
05616be5 N |
1174 | + rrdev->new_data_offset); |
1175 | else | |
4f024f37 | 1176 | rbi->bi_iter.bi_sector = (sh->sector |
05616be5 | 1177 | + rrdev->data_offset); |
d592a996 SL |
1178 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
1179 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
1180 | sh->dev[i].rvec.bv_page = sh->dev[i].page; | |
4997b72e | 1181 | rbi->bi_vcnt = 1; |
977df362 N |
1182 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
1183 | rbi->bi_io_vec[0].bv_offset = 0; | |
4f024f37 | 1184 | rbi->bi_iter.bi_size = STRIPE_SIZE; |
37c61ff3 SL |
1185 | /* |
1186 | * If this is discard request, set bi_vcnt 0. We don't | |
1187 | * want to confuse SCSI because SCSI will replace payload | |
1188 | */ | |
796a5cf0 | 1189 | if (op == REQ_OP_DISCARD) |
37c61ff3 | 1190 | rbi->bi_vcnt = 0; |
e3620a3a JB |
1191 | if (conf->mddev->gendisk) |
1192 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), | |
1193 | rbi, disk_devt(conf->mddev->gendisk), | |
1194 | sh->dev[i].sector); | |
aaf9f12e SL |
1195 | if (should_defer && op_is_write(op)) |
1196 | bio_list_add(&pending_bios, rbi); | |
1197 | else | |
1198 | generic_make_request(rbi); | |
977df362 N |
1199 | } |
1200 | if (!rdev && !rrdev) { | |
796a5cf0 | 1201 | if (op_is_write(op)) |
91c00924 | 1202 | set_bit(STRIPE_DEGRADED, &sh->state); |
6296b960 | 1203 | pr_debug("skip op %d on disc %d for sector %llu\n", |
1eff9d32 | 1204 | bi->bi_opf, i, (unsigned long long)sh->sector); |
91c00924 DW |
1205 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
1206 | set_bit(STRIPE_HANDLE, &sh->state); | |
1207 | } | |
59fc630b | 1208 | |
1209 | if (!head_sh->batch_head) | |
1210 | continue; | |
1211 | sh = list_first_entry(&sh->batch_list, struct stripe_head, | |
1212 | batch_list); | |
1213 | if (sh != head_sh) | |
1214 | goto again; | |
91c00924 | 1215 | } |
aaf9f12e SL |
1216 | |
1217 | if (should_defer && !bio_list_empty(&pending_bios)) | |
1218 | defer_issue_bios(conf, head_sh->sector, &pending_bios); | |
91c00924 DW |
1219 | } |
1220 | ||
1221 | static struct dma_async_tx_descriptor * | |
d592a996 SL |
1222 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
1223 | sector_t sector, struct dma_async_tx_descriptor *tx, | |
1e6d690b | 1224 | struct stripe_head *sh, int no_skipcopy) |
91c00924 | 1225 | { |
7988613b KO |
1226 | struct bio_vec bvl; |
1227 | struct bvec_iter iter; | |
91c00924 | 1228 | struct page *bio_page; |
91c00924 | 1229 | int page_offset; |
a08abd8c | 1230 | struct async_submit_ctl submit; |
0403e382 | 1231 | enum async_tx_flags flags = 0; |
91c00924 | 1232 | |
4f024f37 KO |
1233 | if (bio->bi_iter.bi_sector >= sector) |
1234 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; | |
91c00924 | 1235 | else |
4f024f37 | 1236 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
a08abd8c | 1237 | |
0403e382 DW |
1238 | if (frombio) |
1239 | flags |= ASYNC_TX_FENCE; | |
1240 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); | |
1241 | ||
7988613b KO |
1242 | bio_for_each_segment(bvl, bio, iter) { |
1243 | int len = bvl.bv_len; | |
91c00924 DW |
1244 | int clen; |
1245 | int b_offset = 0; | |
1246 | ||
1247 | if (page_offset < 0) { | |
1248 | b_offset = -page_offset; | |
1249 | page_offset += b_offset; | |
1250 | len -= b_offset; | |
1251 | } | |
1252 | ||
1253 | if (len > 0 && page_offset + len > STRIPE_SIZE) | |
1254 | clen = STRIPE_SIZE - page_offset; | |
1255 | else | |
1256 | clen = len; | |
1257 | ||
1258 | if (clen > 0) { | |
7988613b KO |
1259 | b_offset += bvl.bv_offset; |
1260 | bio_page = bvl.bv_page; | |
d592a996 SL |
1261 | if (frombio) { |
1262 | if (sh->raid_conf->skip_copy && | |
1263 | b_offset == 0 && page_offset == 0 && | |
1e6d690b SL |
1264 | clen == STRIPE_SIZE && |
1265 | !no_skipcopy) | |
d592a996 SL |
1266 | *page = bio_page; |
1267 | else | |
1268 | tx = async_memcpy(*page, bio_page, page_offset, | |
a08abd8c | 1269 | b_offset, clen, &submit); |
d592a996 SL |
1270 | } else |
1271 | tx = async_memcpy(bio_page, *page, b_offset, | |
a08abd8c | 1272 | page_offset, clen, &submit); |
91c00924 | 1273 | } |
a08abd8c DW |
1274 | /* chain the operations */ |
1275 | submit.depend_tx = tx; | |
1276 | ||
91c00924 DW |
1277 | if (clen < len) /* hit end of page */ |
1278 | break; | |
1279 | page_offset += len; | |
1280 | } | |
1281 | ||
1282 | return tx; | |
1283 | } | |
1284 | ||
1285 | static void ops_complete_biofill(void *stripe_head_ref) | |
1286 | { | |
1287 | struct stripe_head *sh = stripe_head_ref; | |
e4d84909 | 1288 | int i; |
91c00924 | 1289 | |
e46b272b | 1290 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1291 | (unsigned long long)sh->sector); |
1292 | ||
1293 | /* clear completed biofills */ | |
1294 | for (i = sh->disks; i--; ) { | |
1295 | struct r5dev *dev = &sh->dev[i]; | |
91c00924 DW |
1296 | |
1297 | /* acknowledge completion of a biofill operation */ | |
e4d84909 DW |
1298 | /* and check if we need to reply to a read request, |
1299 | * new R5_Wantfill requests are held off until | |
83de75cc | 1300 | * !STRIPE_BIOFILL_RUN |
e4d84909 DW |
1301 | */ |
1302 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { | |
91c00924 | 1303 | struct bio *rbi, *rbi2; |
91c00924 | 1304 | |
91c00924 DW |
1305 | BUG_ON(!dev->read); |
1306 | rbi = dev->read; | |
1307 | dev->read = NULL; | |
4f024f37 | 1308 | while (rbi && rbi->bi_iter.bi_sector < |
91c00924 DW |
1309 | dev->sector + STRIPE_SECTORS) { |
1310 | rbi2 = r5_next_bio(rbi, dev->sector); | |
016c76ac | 1311 | bio_endio(rbi); |
91c00924 DW |
1312 | rbi = rbi2; |
1313 | } | |
1314 | } | |
1315 | } | |
83de75cc | 1316 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
91c00924 | 1317 | |
e4d84909 | 1318 | set_bit(STRIPE_HANDLE, &sh->state); |
6d036f7d | 1319 | raid5_release_stripe(sh); |
91c00924 DW |
1320 | } |
1321 | ||
1322 | static void ops_run_biofill(struct stripe_head *sh) | |
1323 | { | |
1324 | struct dma_async_tx_descriptor *tx = NULL; | |
a08abd8c | 1325 | struct async_submit_ctl submit; |
91c00924 DW |
1326 | int i; |
1327 | ||
59fc630b | 1328 | BUG_ON(sh->batch_head); |
e46b272b | 1329 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1330 | (unsigned long long)sh->sector); |
1331 | ||
1332 | for (i = sh->disks; i--; ) { | |
1333 | struct r5dev *dev = &sh->dev[i]; | |
1334 | if (test_bit(R5_Wantfill, &dev->flags)) { | |
1335 | struct bio *rbi; | |
b17459c0 | 1336 | spin_lock_irq(&sh->stripe_lock); |
91c00924 DW |
1337 | dev->read = rbi = dev->toread; |
1338 | dev->toread = NULL; | |
b17459c0 | 1339 | spin_unlock_irq(&sh->stripe_lock); |
4f024f37 | 1340 | while (rbi && rbi->bi_iter.bi_sector < |
91c00924 | 1341 | dev->sector + STRIPE_SECTORS) { |
d592a996 | 1342 | tx = async_copy_data(0, rbi, &dev->page, |
1e6d690b | 1343 | dev->sector, tx, sh, 0); |
91c00924 DW |
1344 | rbi = r5_next_bio(rbi, dev->sector); |
1345 | } | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | atomic_inc(&sh->count); | |
a08abd8c DW |
1350 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
1351 | async_trigger_callback(&submit); | |
91c00924 DW |
1352 | } |
1353 | ||
4e7d2c0a | 1354 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
91c00924 | 1355 | { |
4e7d2c0a | 1356 | struct r5dev *tgt; |
91c00924 | 1357 | |
4e7d2c0a DW |
1358 | if (target < 0) |
1359 | return; | |
91c00924 | 1360 | |
4e7d2c0a | 1361 | tgt = &sh->dev[target]; |
91c00924 DW |
1362 | set_bit(R5_UPTODATE, &tgt->flags); |
1363 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
1364 | clear_bit(R5_Wantcompute, &tgt->flags); | |
4e7d2c0a DW |
1365 | } |
1366 | ||
ac6b53b6 | 1367 | static void ops_complete_compute(void *stripe_head_ref) |
91c00924 DW |
1368 | { |
1369 | struct stripe_head *sh = stripe_head_ref; | |
91c00924 | 1370 | |
e46b272b | 1371 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1372 | (unsigned long long)sh->sector); |
1373 | ||
ac6b53b6 | 1374 | /* mark the computed target(s) as uptodate */ |
4e7d2c0a | 1375 | mark_target_uptodate(sh, sh->ops.target); |
ac6b53b6 | 1376 | mark_target_uptodate(sh, sh->ops.target2); |
4e7d2c0a | 1377 | |
ecc65c9b DW |
1378 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
1379 | if (sh->check_state == check_state_compute_run) | |
1380 | sh->check_state = check_state_compute_result; | |
91c00924 | 1381 | set_bit(STRIPE_HANDLE, &sh->state); |
6d036f7d | 1382 | raid5_release_stripe(sh); |
91c00924 DW |
1383 | } |
1384 | ||
d6f38f31 DW |
1385 | /* return a pointer to the address conversion region of the scribble buffer */ |
1386 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, | |
46d5b785 | 1387 | struct raid5_percpu *percpu, int i) |
d6f38f31 | 1388 | { |
46d5b785 | 1389 | void *addr; |
1390 | ||
1391 | addr = flex_array_get(percpu->scribble, i); | |
1392 | return addr + sizeof(struct page *) * (sh->disks + 2); | |
1393 | } | |
1394 | ||
1395 | /* return a pointer to the address conversion region of the scribble buffer */ | |
1396 | static struct page **to_addr_page(struct raid5_percpu *percpu, int i) | |
1397 | { | |
1398 | void *addr; | |
1399 | ||
1400 | addr = flex_array_get(percpu->scribble, i); | |
1401 | return addr; | |
d6f38f31 DW |
1402 | } |
1403 | ||
1404 | static struct dma_async_tx_descriptor * | |
1405 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) | |
91c00924 | 1406 | { |
91c00924 | 1407 | int disks = sh->disks; |
46d5b785 | 1408 | struct page **xor_srcs = to_addr_page(percpu, 0); |
91c00924 DW |
1409 | int target = sh->ops.target; |
1410 | struct r5dev *tgt = &sh->dev[target]; | |
1411 | struct page *xor_dest = tgt->page; | |
1412 | int count = 0; | |
1413 | struct dma_async_tx_descriptor *tx; | |
a08abd8c | 1414 | struct async_submit_ctl submit; |
91c00924 DW |
1415 | int i; |
1416 | ||
59fc630b | 1417 | BUG_ON(sh->batch_head); |
1418 | ||
91c00924 | 1419 | pr_debug("%s: stripe %llu block: %d\n", |
e46b272b | 1420 | __func__, (unsigned long long)sh->sector, target); |
91c00924 DW |
1421 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
1422 | ||
1423 | for (i = disks; i--; ) | |
1424 | if (i != target) | |
1425 | xor_srcs[count++] = sh->dev[i].page; | |
1426 | ||
1427 | atomic_inc(&sh->count); | |
1428 | ||
0403e382 | 1429 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, |
46d5b785 | 1430 | ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); |
91c00924 | 1431 | if (unlikely(count == 1)) |
a08abd8c | 1432 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
91c00924 | 1433 | else |
a08abd8c | 1434 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
91c00924 | 1435 | |
91c00924 DW |
1436 | return tx; |
1437 | } | |
1438 | ||
ac6b53b6 DW |
1439 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
1440 | * @srcs - (struct page *) array of size sh->disks | |
1441 | * @sh - stripe_head to parse | |
1442 | * | |
1443 | * Populates srcs in proper layout order for the stripe and returns the | |
1444 | * 'count' of sources to be used in a call to async_gen_syndrome. The P | |
1445 | * destination buffer is recorded in srcs[count] and the Q destination | |
1446 | * is recorded in srcs[count+1]]. | |
1447 | */ | |
584acdd4 MS |
1448 | static int set_syndrome_sources(struct page **srcs, |
1449 | struct stripe_head *sh, | |
1450 | int srctype) | |
ac6b53b6 DW |
1451 | { |
1452 | int disks = sh->disks; | |
1453 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); | |
1454 | int d0_idx = raid6_d0(sh); | |
1455 | int count; | |
1456 | int i; | |
1457 | ||
1458 | for (i = 0; i < disks; i++) | |
5dd33c9a | 1459 | srcs[i] = NULL; |
ac6b53b6 DW |
1460 | |
1461 | count = 0; | |
1462 | i = d0_idx; | |
1463 | do { | |
1464 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); | |
584acdd4 | 1465 | struct r5dev *dev = &sh->dev[i]; |
ac6b53b6 | 1466 | |
584acdd4 MS |
1467 | if (i == sh->qd_idx || i == sh->pd_idx || |
1468 | (srctype == SYNDROME_SRC_ALL) || | |
1469 | (srctype == SYNDROME_SRC_WANT_DRAIN && | |
1e6d690b SL |
1470 | (test_bit(R5_Wantdrain, &dev->flags) || |
1471 | test_bit(R5_InJournal, &dev->flags))) || | |
584acdd4 | 1472 | (srctype == SYNDROME_SRC_WRITTEN && |
0977762f SL |
1473 | (dev->written || |
1474 | test_bit(R5_InJournal, &dev->flags)))) { | |
1e6d690b SL |
1475 | if (test_bit(R5_InJournal, &dev->flags)) |
1476 | srcs[slot] = sh->dev[i].orig_page; | |
1477 | else | |
1478 | srcs[slot] = sh->dev[i].page; | |
1479 | } | |
ac6b53b6 DW |
1480 | i = raid6_next_disk(i, disks); |
1481 | } while (i != d0_idx); | |
ac6b53b6 | 1482 | |
e4424fee | 1483 | return syndrome_disks; |
ac6b53b6 DW |
1484 | } |
1485 | ||
1486 | static struct dma_async_tx_descriptor * | |
1487 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) | |
1488 | { | |
1489 | int disks = sh->disks; | |
46d5b785 | 1490 | struct page **blocks = to_addr_page(percpu, 0); |
ac6b53b6 DW |
1491 | int target; |
1492 | int qd_idx = sh->qd_idx; | |
1493 | struct dma_async_tx_descriptor *tx; | |
1494 | struct async_submit_ctl submit; | |
1495 | struct r5dev *tgt; | |
1496 | struct page *dest; | |
1497 | int i; | |
1498 | int count; | |
1499 | ||
59fc630b | 1500 | BUG_ON(sh->batch_head); |
ac6b53b6 DW |
1501 | if (sh->ops.target < 0) |
1502 | target = sh->ops.target2; | |
1503 | else if (sh->ops.target2 < 0) | |
1504 | target = sh->ops.target; | |
91c00924 | 1505 | else |
ac6b53b6 DW |
1506 | /* we should only have one valid target */ |
1507 | BUG(); | |
1508 | BUG_ON(target < 0); | |
1509 | pr_debug("%s: stripe %llu block: %d\n", | |
1510 | __func__, (unsigned long long)sh->sector, target); | |
1511 | ||
1512 | tgt = &sh->dev[target]; | |
1513 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
1514 | dest = tgt->page; | |
1515 | ||
1516 | atomic_inc(&sh->count); | |
1517 | ||
1518 | if (target == qd_idx) { | |
584acdd4 | 1519 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
ac6b53b6 DW |
1520 | blocks[count] = NULL; /* regenerating p is not necessary */ |
1521 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ | |
0403e382 DW |
1522 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1523 | ops_complete_compute, sh, | |
46d5b785 | 1524 | to_addr_conv(sh, percpu, 0)); |
ac6b53b6 DW |
1525 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
1526 | } else { | |
1527 | /* Compute any data- or p-drive using XOR */ | |
1528 | count = 0; | |
1529 | for (i = disks; i-- ; ) { | |
1530 | if (i == target || i == qd_idx) | |
1531 | continue; | |
1532 | blocks[count++] = sh->dev[i].page; | |
1533 | } | |
1534 | ||
0403e382 DW |
1535 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
1536 | NULL, ops_complete_compute, sh, | |
46d5b785 | 1537 | to_addr_conv(sh, percpu, 0)); |
ac6b53b6 DW |
1538 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
1539 | } | |
91c00924 | 1540 | |
91c00924 DW |
1541 | return tx; |
1542 | } | |
1543 | ||
ac6b53b6 DW |
1544 | static struct dma_async_tx_descriptor * |
1545 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) | |
1546 | { | |
1547 | int i, count, disks = sh->disks; | |
1548 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; | |
1549 | int d0_idx = raid6_d0(sh); | |
1550 | int faila = -1, failb = -1; | |
1551 | int target = sh->ops.target; | |
1552 | int target2 = sh->ops.target2; | |
1553 | struct r5dev *tgt = &sh->dev[target]; | |
1554 | struct r5dev *tgt2 = &sh->dev[target2]; | |
1555 | struct dma_async_tx_descriptor *tx; | |
46d5b785 | 1556 | struct page **blocks = to_addr_page(percpu, 0); |
ac6b53b6 DW |
1557 | struct async_submit_ctl submit; |
1558 | ||
59fc630b | 1559 | BUG_ON(sh->batch_head); |
ac6b53b6 DW |
1560 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
1561 | __func__, (unsigned long long)sh->sector, target, target2); | |
1562 | BUG_ON(target < 0 || target2 < 0); | |
1563 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); | |
1564 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); | |
1565 | ||
6c910a78 | 1566 | /* we need to open-code set_syndrome_sources to handle the |
ac6b53b6 DW |
1567 | * slot number conversion for 'faila' and 'failb' |
1568 | */ | |
1569 | for (i = 0; i < disks ; i++) | |
5dd33c9a | 1570 | blocks[i] = NULL; |
ac6b53b6 DW |
1571 | count = 0; |
1572 | i = d0_idx; | |
1573 | do { | |
1574 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); | |
1575 | ||
1576 | blocks[slot] = sh->dev[i].page; | |
1577 | ||
1578 | if (i == target) | |
1579 | faila = slot; | |
1580 | if (i == target2) | |
1581 | failb = slot; | |
1582 | i = raid6_next_disk(i, disks); | |
1583 | } while (i != d0_idx); | |
ac6b53b6 DW |
1584 | |
1585 | BUG_ON(faila == failb); | |
1586 | if (failb < faila) | |
1587 | swap(faila, failb); | |
1588 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", | |
1589 | __func__, (unsigned long long)sh->sector, faila, failb); | |
1590 | ||
1591 | atomic_inc(&sh->count); | |
1592 | ||
1593 | if (failb == syndrome_disks+1) { | |
1594 | /* Q disk is one of the missing disks */ | |
1595 | if (faila == syndrome_disks) { | |
1596 | /* Missing P+Q, just recompute */ | |
0403e382 DW |
1597 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1598 | ops_complete_compute, sh, | |
46d5b785 | 1599 | to_addr_conv(sh, percpu, 0)); |
e4424fee | 1600 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
ac6b53b6 DW |
1601 | STRIPE_SIZE, &submit); |
1602 | } else { | |
1603 | struct page *dest; | |
1604 | int data_target; | |
1605 | int qd_idx = sh->qd_idx; | |
1606 | ||
1607 | /* Missing D+Q: recompute D from P, then recompute Q */ | |
1608 | if (target == qd_idx) | |
1609 | data_target = target2; | |
1610 | else | |
1611 | data_target = target; | |
1612 | ||
1613 | count = 0; | |
1614 | for (i = disks; i-- ; ) { | |
1615 | if (i == data_target || i == qd_idx) | |
1616 | continue; | |
1617 | blocks[count++] = sh->dev[i].page; | |
1618 | } | |
1619 | dest = sh->dev[data_target].page; | |
0403e382 DW |
1620 | init_async_submit(&submit, |
1621 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, | |
1622 | NULL, NULL, NULL, | |
46d5b785 | 1623 | to_addr_conv(sh, percpu, 0)); |
ac6b53b6 DW |
1624 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
1625 | &submit); | |
1626 | ||
584acdd4 | 1627 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
0403e382 DW |
1628 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
1629 | ops_complete_compute, sh, | |
46d5b785 | 1630 | to_addr_conv(sh, percpu, 0)); |
ac6b53b6 DW |
1631 | return async_gen_syndrome(blocks, 0, count+2, |
1632 | STRIPE_SIZE, &submit); | |
1633 | } | |
ac6b53b6 | 1634 | } else { |
6c910a78 DW |
1635 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
1636 | ops_complete_compute, sh, | |
46d5b785 | 1637 | to_addr_conv(sh, percpu, 0)); |
6c910a78 DW |
1638 | if (failb == syndrome_disks) { |
1639 | /* We're missing D+P. */ | |
1640 | return async_raid6_datap_recov(syndrome_disks+2, | |
1641 | STRIPE_SIZE, faila, | |
1642 | blocks, &submit); | |
1643 | } else { | |
1644 | /* We're missing D+D. */ | |
1645 | return async_raid6_2data_recov(syndrome_disks+2, | |
1646 | STRIPE_SIZE, faila, failb, | |
1647 | blocks, &submit); | |
1648 | } | |
ac6b53b6 DW |
1649 | } |
1650 | } | |
1651 | ||
91c00924 DW |
1652 | static void ops_complete_prexor(void *stripe_head_ref) |
1653 | { | |
1654 | struct stripe_head *sh = stripe_head_ref; | |
1655 | ||
e46b272b | 1656 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 | 1657 | (unsigned long long)sh->sector); |
1e6d690b SL |
1658 | |
1659 | if (r5c_is_writeback(sh->raid_conf->log)) | |
1660 | /* | |
1661 | * raid5-cache write back uses orig_page during prexor. | |
1662 | * After prexor, it is time to free orig_page | |
1663 | */ | |
1664 | r5c_release_extra_page(sh); | |
91c00924 DW |
1665 | } |
1666 | ||
1667 | static struct dma_async_tx_descriptor * | |
584acdd4 MS |
1668 | ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, |
1669 | struct dma_async_tx_descriptor *tx) | |
91c00924 | 1670 | { |
91c00924 | 1671 | int disks = sh->disks; |
46d5b785 | 1672 | struct page **xor_srcs = to_addr_page(percpu, 0); |
91c00924 | 1673 | int count = 0, pd_idx = sh->pd_idx, i; |
a08abd8c | 1674 | struct async_submit_ctl submit; |
91c00924 DW |
1675 | |
1676 | /* existing parity data subtracted */ | |
1677 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; | |
1678 | ||
59fc630b | 1679 | BUG_ON(sh->batch_head); |
e46b272b | 1680 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1681 | (unsigned long long)sh->sector); |
1682 | ||
1683 | for (i = disks; i--; ) { | |
1684 | struct r5dev *dev = &sh->dev[i]; | |
1685 | /* Only process blocks that are known to be uptodate */ | |
1e6d690b SL |
1686 | if (test_bit(R5_InJournal, &dev->flags)) |
1687 | xor_srcs[count++] = dev->orig_page; | |
1688 | else if (test_bit(R5_Wantdrain, &dev->flags)) | |
91c00924 DW |
1689 | xor_srcs[count++] = dev->page; |
1690 | } | |
1691 | ||
0403e382 | 1692 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, |
46d5b785 | 1693 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
a08abd8c | 1694 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
91c00924 DW |
1695 | |
1696 | return tx; | |
1697 | } | |
1698 | ||
584acdd4 MS |
1699 | static struct dma_async_tx_descriptor * |
1700 | ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, | |
1701 | struct dma_async_tx_descriptor *tx) | |
1702 | { | |
1703 | struct page **blocks = to_addr_page(percpu, 0); | |
1704 | int count; | |
1705 | struct async_submit_ctl submit; | |
1706 | ||
1707 | pr_debug("%s: stripe %llu\n", __func__, | |
1708 | (unsigned long long)sh->sector); | |
1709 | ||
1710 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN); | |
1711 | ||
1712 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, | |
1713 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); | |
1714 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); | |
1715 | ||
1716 | return tx; | |
1717 | } | |
1718 | ||
91c00924 | 1719 | static struct dma_async_tx_descriptor * |
d8ee0728 | 1720 | ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) |
91c00924 | 1721 | { |
1e6d690b | 1722 | struct r5conf *conf = sh->raid_conf; |
91c00924 | 1723 | int disks = sh->disks; |
d8ee0728 | 1724 | int i; |
59fc630b | 1725 | struct stripe_head *head_sh = sh; |
91c00924 | 1726 | |
e46b272b | 1727 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1728 | (unsigned long long)sh->sector); |
1729 | ||
1730 | for (i = disks; i--; ) { | |
59fc630b | 1731 | struct r5dev *dev; |
91c00924 | 1732 | struct bio *chosen; |
91c00924 | 1733 | |
59fc630b | 1734 | sh = head_sh; |
1735 | if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { | |
91c00924 DW |
1736 | struct bio *wbi; |
1737 | ||
59fc630b | 1738 | again: |
1739 | dev = &sh->dev[i]; | |
1e6d690b SL |
1740 | /* |
1741 | * clear R5_InJournal, so when rewriting a page in | |
1742 | * journal, it is not skipped by r5l_log_stripe() | |
1743 | */ | |
1744 | clear_bit(R5_InJournal, &dev->flags); | |
b17459c0 | 1745 | spin_lock_irq(&sh->stripe_lock); |
91c00924 DW |
1746 | chosen = dev->towrite; |
1747 | dev->towrite = NULL; | |
7a87f434 | 1748 | sh->overwrite_disks = 0; |
91c00924 DW |
1749 | BUG_ON(dev->written); |
1750 | wbi = dev->written = chosen; | |
b17459c0 | 1751 | spin_unlock_irq(&sh->stripe_lock); |
d592a996 | 1752 | WARN_ON(dev->page != dev->orig_page); |
91c00924 | 1753 | |
4f024f37 | 1754 | while (wbi && wbi->bi_iter.bi_sector < |
91c00924 | 1755 | dev->sector + STRIPE_SECTORS) { |
1eff9d32 | 1756 | if (wbi->bi_opf & REQ_FUA) |
e9c7469b | 1757 | set_bit(R5_WantFUA, &dev->flags); |
1eff9d32 | 1758 | if (wbi->bi_opf & REQ_SYNC) |
bc0934f0 | 1759 | set_bit(R5_SyncIO, &dev->flags); |
796a5cf0 | 1760 | if (bio_op(wbi) == REQ_OP_DISCARD) |
620125f2 | 1761 | set_bit(R5_Discard, &dev->flags); |
d592a996 SL |
1762 | else { |
1763 | tx = async_copy_data(1, wbi, &dev->page, | |
1e6d690b SL |
1764 | dev->sector, tx, sh, |
1765 | r5c_is_writeback(conf->log)); | |
1766 | if (dev->page != dev->orig_page && | |
1767 | !r5c_is_writeback(conf->log)) { | |
d592a996 SL |
1768 | set_bit(R5_SkipCopy, &dev->flags); |
1769 | clear_bit(R5_UPTODATE, &dev->flags); | |
1770 | clear_bit(R5_OVERWRITE, &dev->flags); | |
1771 | } | |
1772 | } | |
91c00924 DW |
1773 | wbi = r5_next_bio(wbi, dev->sector); |
1774 | } | |
59fc630b | 1775 | |
1776 | if (head_sh->batch_head) { | |
1777 | sh = list_first_entry(&sh->batch_list, | |
1778 | struct stripe_head, | |
1779 | batch_list); | |
1780 | if (sh == head_sh) | |
1781 | continue; | |
1782 | goto again; | |
1783 | } | |
91c00924 DW |
1784 | } |
1785 | } | |
1786 | ||
1787 | return tx; | |
1788 | } | |
1789 | ||
ac6b53b6 | 1790 | static void ops_complete_reconstruct(void *stripe_head_ref) |
91c00924 DW |
1791 | { |
1792 | struct stripe_head *sh = stripe_head_ref; | |
ac6b53b6 DW |
1793 | int disks = sh->disks; |
1794 | int pd_idx = sh->pd_idx; | |
1795 | int qd_idx = sh->qd_idx; | |
1796 | int i; | |
9e444768 | 1797 | bool fua = false, sync = false, discard = false; |
91c00924 | 1798 | |
e46b272b | 1799 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1800 | (unsigned long long)sh->sector); |
1801 | ||
bc0934f0 | 1802 | for (i = disks; i--; ) { |
e9c7469b | 1803 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
bc0934f0 | 1804 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
9e444768 | 1805 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
bc0934f0 | 1806 | } |
e9c7469b | 1807 | |
91c00924 DW |
1808 | for (i = disks; i--; ) { |
1809 | struct r5dev *dev = &sh->dev[i]; | |
ac6b53b6 | 1810 | |
e9c7469b | 1811 | if (dev->written || i == pd_idx || i == qd_idx) { |
d592a996 | 1812 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) |
9e444768 | 1813 | set_bit(R5_UPTODATE, &dev->flags); |
e9c7469b TH |
1814 | if (fua) |
1815 | set_bit(R5_WantFUA, &dev->flags); | |
bc0934f0 SL |
1816 | if (sync) |
1817 | set_bit(R5_SyncIO, &dev->flags); | |
e9c7469b | 1818 | } |
91c00924 DW |
1819 | } |
1820 | ||
d8ee0728 DW |
1821 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
1822 | sh->reconstruct_state = reconstruct_state_drain_result; | |
1823 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) | |
1824 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; | |
1825 | else { | |
1826 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); | |
1827 | sh->reconstruct_state = reconstruct_state_result; | |
1828 | } | |
91c00924 DW |
1829 | |
1830 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 1831 | raid5_release_stripe(sh); |
91c00924 DW |
1832 | } |
1833 | ||
1834 | static void | |
ac6b53b6 DW |
1835 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
1836 | struct dma_async_tx_descriptor *tx) | |
91c00924 | 1837 | { |
91c00924 | 1838 | int disks = sh->disks; |
59fc630b | 1839 | struct page **xor_srcs; |
a08abd8c | 1840 | struct async_submit_ctl submit; |
59fc630b | 1841 | int count, pd_idx = sh->pd_idx, i; |
91c00924 | 1842 | struct page *xor_dest; |
d8ee0728 | 1843 | int prexor = 0; |
91c00924 | 1844 | unsigned long flags; |
59fc630b | 1845 | int j = 0; |
1846 | struct stripe_head *head_sh = sh; | |
1847 | int last_stripe; | |
91c00924 | 1848 | |
e46b272b | 1849 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1850 | (unsigned long long)sh->sector); |
1851 | ||
620125f2 SL |
1852 | for (i = 0; i < sh->disks; i++) { |
1853 | if (pd_idx == i) | |
1854 | continue; | |
1855 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) | |
1856 | break; | |
1857 | } | |
1858 | if (i >= sh->disks) { | |
1859 | atomic_inc(&sh->count); | |
620125f2 SL |
1860 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
1861 | ops_complete_reconstruct(sh); | |
1862 | return; | |
1863 | } | |
59fc630b | 1864 | again: |
1865 | count = 0; | |
1866 | xor_srcs = to_addr_page(percpu, j); | |
91c00924 DW |
1867 | /* check if prexor is active which means only process blocks |
1868 | * that are part of a read-modify-write (written) | |
1869 | */ | |
59fc630b | 1870 | if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
d8ee0728 | 1871 | prexor = 1; |
91c00924 DW |
1872 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
1873 | for (i = disks; i--; ) { | |
1874 | struct r5dev *dev = &sh->dev[i]; | |
1e6d690b SL |
1875 | if (head_sh->dev[i].written || |
1876 | test_bit(R5_InJournal, &head_sh->dev[i].flags)) | |
91c00924 DW |
1877 | xor_srcs[count++] = dev->page; |
1878 | } | |
1879 | } else { | |
1880 | xor_dest = sh->dev[pd_idx].page; | |
1881 | for (i = disks; i--; ) { | |
1882 | struct r5dev *dev = &sh->dev[i]; | |
1883 | if (i != pd_idx) | |
1884 | xor_srcs[count++] = dev->page; | |
1885 | } | |
1886 | } | |
1887 | ||
91c00924 DW |
1888 | /* 1/ if we prexor'd then the dest is reused as a source |
1889 | * 2/ if we did not prexor then we are redoing the parity | |
1890 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST | |
1891 | * for the synchronous xor case | |
1892 | */ | |
59fc630b | 1893 | last_stripe = !head_sh->batch_head || |
1894 | list_first_entry(&sh->batch_list, | |
1895 | struct stripe_head, batch_list) == head_sh; | |
1896 | if (last_stripe) { | |
1897 | flags = ASYNC_TX_ACK | | |
1898 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); | |
1899 | ||
1900 | atomic_inc(&head_sh->count); | |
1901 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, | |
1902 | to_addr_conv(sh, percpu, j)); | |
1903 | } else { | |
1904 | flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; | |
1905 | init_async_submit(&submit, flags, tx, NULL, NULL, | |
1906 | to_addr_conv(sh, percpu, j)); | |
1907 | } | |
91c00924 | 1908 | |
a08abd8c DW |
1909 | if (unlikely(count == 1)) |
1910 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); | |
1911 | else | |
1912 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); | |
59fc630b | 1913 | if (!last_stripe) { |
1914 | j++; | |
1915 | sh = list_first_entry(&sh->batch_list, struct stripe_head, | |
1916 | batch_list); | |
1917 | goto again; | |
1918 | } | |
91c00924 DW |
1919 | } |
1920 | ||
ac6b53b6 DW |
1921 | static void |
1922 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, | |
1923 | struct dma_async_tx_descriptor *tx) | |
1924 | { | |
1925 | struct async_submit_ctl submit; | |
59fc630b | 1926 | struct page **blocks; |
1927 | int count, i, j = 0; | |
1928 | struct stripe_head *head_sh = sh; | |
1929 | int last_stripe; | |
584acdd4 MS |
1930 | int synflags; |
1931 | unsigned long txflags; | |
ac6b53b6 DW |
1932 | |
1933 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); | |
1934 | ||
620125f2 SL |
1935 | for (i = 0; i < sh->disks; i++) { |
1936 | if (sh->pd_idx == i || sh->qd_idx == i) | |
1937 | continue; | |
1938 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) | |
1939 | break; | |
1940 | } | |
1941 | if (i >= sh->disks) { | |
1942 | atomic_inc(&sh->count); | |
620125f2 SL |
1943 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
1944 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); | |
1945 | ops_complete_reconstruct(sh); | |
1946 | return; | |
1947 | } | |
1948 | ||
59fc630b | 1949 | again: |
1950 | blocks = to_addr_page(percpu, j); | |
584acdd4 MS |
1951 | |
1952 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { | |
1953 | synflags = SYNDROME_SRC_WRITTEN; | |
1954 | txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; | |
1955 | } else { | |
1956 | synflags = SYNDROME_SRC_ALL; | |
1957 | txflags = ASYNC_TX_ACK; | |
1958 | } | |
1959 | ||
1960 | count = set_syndrome_sources(blocks, sh, synflags); | |
59fc630b | 1961 | last_stripe = !head_sh->batch_head || |
1962 | list_first_entry(&sh->batch_list, | |
1963 | struct stripe_head, batch_list) == head_sh; | |
1964 | ||
1965 | if (last_stripe) { | |
1966 | atomic_inc(&head_sh->count); | |
584acdd4 | 1967 | init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, |
59fc630b | 1968 | head_sh, to_addr_conv(sh, percpu, j)); |
1969 | } else | |
1970 | init_async_submit(&submit, 0, tx, NULL, NULL, | |
1971 | to_addr_conv(sh, percpu, j)); | |
48769695 | 1972 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
59fc630b | 1973 | if (!last_stripe) { |
1974 | j++; | |
1975 | sh = list_first_entry(&sh->batch_list, struct stripe_head, | |
1976 | batch_list); | |
1977 | goto again; | |
1978 | } | |
91c00924 DW |
1979 | } |
1980 | ||
1981 | static void ops_complete_check(void *stripe_head_ref) | |
1982 | { | |
1983 | struct stripe_head *sh = stripe_head_ref; | |
91c00924 | 1984 | |
e46b272b | 1985 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
1986 | (unsigned long long)sh->sector); |
1987 | ||
ecc65c9b | 1988 | sh->check_state = check_state_check_result; |
91c00924 | 1989 | set_bit(STRIPE_HANDLE, &sh->state); |
6d036f7d | 1990 | raid5_release_stripe(sh); |
91c00924 DW |
1991 | } |
1992 | ||
ac6b53b6 | 1993 | static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu) |
91c00924 | 1994 | { |
91c00924 | 1995 | int disks = sh->disks; |
ac6b53b6 DW |
1996 | int pd_idx = sh->pd_idx; |
1997 | int qd_idx = sh->qd_idx; | |
1998 | struct page *xor_dest; | |
46d5b785 | 1999 | struct page **xor_srcs = to_addr_page(percpu, 0); |
91c00924 | 2000 | struct dma_async_tx_descriptor *tx; |
a08abd8c | 2001 | struct async_submit_ctl submit; |
ac6b53b6 DW |
2002 | int count; |
2003 | int i; | |
91c00924 | 2004 | |
e46b272b | 2005 | pr_debug("%s: stripe %llu\n", __func__, |
91c00924 DW |
2006 | (unsigned long long)sh->sector); |
2007 | ||
59fc630b | 2008 | BUG_ON(sh->batch_head); |
ac6b53b6 DW |
2009 | count = 0; |
2010 | xor_dest = sh->dev[pd_idx].page; | |
2011 | xor_srcs[count++] = xor_dest; | |
91c00924 | 2012 | for (i = disks; i--; ) { |
ac6b53b6 DW |
2013 | if (i == pd_idx || i == qd_idx) |
2014 | continue; | |
2015 | xor_srcs[count++] = sh->dev[i].page; | |
91c00924 DW |
2016 | } |
2017 | ||
d6f38f31 | 2018 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
46d5b785 | 2019 | to_addr_conv(sh, percpu, 0)); |
099f53cb | 2020 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
a08abd8c | 2021 | &sh->ops.zero_sum_result, &submit); |
91c00924 | 2022 | |
91c00924 | 2023 | atomic_inc(&sh->count); |
a08abd8c DW |
2024 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
2025 | tx = async_trigger_callback(&submit); | |
91c00924 DW |
2026 | } |
2027 | ||
ac6b53b6 DW |
2028 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
2029 | { | |
46d5b785 | 2030 | struct page **srcs = to_addr_page(percpu, 0); |
ac6b53b6 DW |
2031 | struct async_submit_ctl submit; |
2032 | int count; | |
2033 | ||
2034 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, | |
2035 | (unsigned long long)sh->sector, checkp); | |
2036 | ||
59fc630b | 2037 | BUG_ON(sh->batch_head); |
584acdd4 | 2038 | count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL); |
ac6b53b6 DW |
2039 | if (!checkp) |
2040 | srcs[count] = NULL; | |
91c00924 | 2041 | |
91c00924 | 2042 | atomic_inc(&sh->count); |
ac6b53b6 | 2043 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
46d5b785 | 2044 | sh, to_addr_conv(sh, percpu, 0)); |
ac6b53b6 DW |
2045 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
2046 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); | |
91c00924 DW |
2047 | } |
2048 | ||
51acbcec | 2049 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
91c00924 DW |
2050 | { |
2051 | int overlap_clear = 0, i, disks = sh->disks; | |
2052 | struct dma_async_tx_descriptor *tx = NULL; | |
d1688a6d | 2053 | struct r5conf *conf = sh->raid_conf; |
ac6b53b6 | 2054 | int level = conf->level; |
d6f38f31 DW |
2055 | struct raid5_percpu *percpu; |
2056 | unsigned long cpu; | |
91c00924 | 2057 | |
d6f38f31 DW |
2058 | cpu = get_cpu(); |
2059 | percpu = per_cpu_ptr(conf->percpu, cpu); | |
83de75cc | 2060 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
91c00924 DW |
2061 | ops_run_biofill(sh); |
2062 | overlap_clear++; | |
2063 | } | |
2064 | ||
7b3a871e | 2065 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
ac6b53b6 DW |
2066 | if (level < 6) |
2067 | tx = ops_run_compute5(sh, percpu); | |
2068 | else { | |
2069 | if (sh->ops.target2 < 0 || sh->ops.target < 0) | |
2070 | tx = ops_run_compute6_1(sh, percpu); | |
2071 | else | |
2072 | tx = ops_run_compute6_2(sh, percpu); | |
2073 | } | |
2074 | /* terminate the chain if reconstruct is not set to be run */ | |
2075 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) | |
7b3a871e DW |
2076 | async_tx_ack(tx); |
2077 | } | |
91c00924 | 2078 | |
584acdd4 MS |
2079 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) { |
2080 | if (level < 6) | |
2081 | tx = ops_run_prexor5(sh, percpu, tx); | |
2082 | else | |
2083 | tx = ops_run_prexor6(sh, percpu, tx); | |
2084 | } | |
91c00924 | 2085 | |
ae1713e2 AP |
2086 | if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request)) |
2087 | tx = ops_run_partial_parity(sh, percpu, tx); | |
2088 | ||
600aa109 | 2089 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
d8ee0728 | 2090 | tx = ops_run_biodrain(sh, tx); |
91c00924 DW |
2091 | overlap_clear++; |
2092 | } | |
2093 | ||
ac6b53b6 DW |
2094 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
2095 | if (level < 6) | |
2096 | ops_run_reconstruct5(sh, percpu, tx); | |
2097 | else | |
2098 | ops_run_reconstruct6(sh, percpu, tx); | |
2099 | } | |
91c00924 | 2100 | |
ac6b53b6 DW |
2101 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
2102 | if (sh->check_state == check_state_run) | |
2103 | ops_run_check_p(sh, percpu); | |
2104 | else if (sh->check_state == check_state_run_q) | |
2105 | ops_run_check_pq(sh, percpu, 0); | |
2106 | else if (sh->check_state == check_state_run_pq) | |
2107 | ops_run_check_pq(sh, percpu, 1); | |
2108 | else | |
2109 | BUG(); | |
2110 | } | |
91c00924 | 2111 | |
59fc630b | 2112 | if (overlap_clear && !sh->batch_head) |
91c00924 DW |
2113 | for (i = disks; i--; ) { |
2114 | struct r5dev *dev = &sh->dev[i]; | |
2115 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) | |
2116 | wake_up(&sh->raid_conf->wait_for_overlap); | |
2117 | } | |
d6f38f31 | 2118 | put_cpu(); |
91c00924 DW |
2119 | } |
2120 | ||
845b9e22 AP |
2121 | static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh) |
2122 | { | |
2123 | if (sh->ppl_page) | |
2124 | __free_page(sh->ppl_page); | |
2125 | kmem_cache_free(sc, sh); | |
2126 | } | |
2127 | ||
5f9d1fde | 2128 | static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp, |
845b9e22 | 2129 | int disks, struct r5conf *conf) |
f18c1a35 N |
2130 | { |
2131 | struct stripe_head *sh; | |
5f9d1fde | 2132 | int i; |
f18c1a35 N |
2133 | |
2134 | sh = kmem_cache_zalloc(sc, gfp); | |
2135 | if (sh) { | |
2136 | spin_lock_init(&sh->stripe_lock); | |
2137 | spin_lock_init(&sh->batch_lock); | |
2138 | INIT_LIST_HEAD(&sh->batch_list); | |
2139 | INIT_LIST_HEAD(&sh->lru); | |
a39f7afd | 2140 | INIT_LIST_HEAD(&sh->r5c); |
d7bd398e | 2141 | INIT_LIST_HEAD(&sh->log_list); |
f18c1a35 | 2142 | atomic_set(&sh->count, 1); |
845b9e22 | 2143 | sh->raid_conf = conf; |
a39f7afd | 2144 | sh->log_start = MaxSector; |
5f9d1fde SL |
2145 | for (i = 0; i < disks; i++) { |
2146 | struct r5dev *dev = &sh->dev[i]; | |
2147 | ||
3a83f467 ML |
2148 | bio_init(&dev->req, &dev->vec, 1); |
2149 | bio_init(&dev->rreq, &dev->rvec, 1); | |
5f9d1fde | 2150 | } |
845b9e22 AP |
2151 | |
2152 | if (raid5_has_ppl(conf)) { | |
2153 | sh->ppl_page = alloc_page(gfp); | |
2154 | if (!sh->ppl_page) { | |
2155 | free_stripe(sc, sh); | |
2156 | sh = NULL; | |
2157 | } | |
2158 | } | |
f18c1a35 N |
2159 | } |
2160 | return sh; | |
2161 | } | |
486f0644 | 2162 | static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) |
1da177e4 LT |
2163 | { |
2164 | struct stripe_head *sh; | |
f18c1a35 | 2165 | |
845b9e22 | 2166 | sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf); |
3f294f4f N |
2167 | if (!sh) |
2168 | return 0; | |
6ce32846 | 2169 | |
a9683a79 | 2170 | if (grow_buffers(sh, gfp)) { |
e4e11e38 | 2171 | shrink_buffers(sh); |
845b9e22 | 2172 | free_stripe(conf->slab_cache, sh); |
3f294f4f N |
2173 | return 0; |
2174 | } | |
486f0644 N |
2175 | sh->hash_lock_index = |
2176 | conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; | |
3f294f4f | 2177 | /* we just created an active stripe so... */ |
3f294f4f | 2178 | atomic_inc(&conf->active_stripes); |
59fc630b | 2179 | |
6d036f7d | 2180 | raid5_release_stripe(sh); |
486f0644 | 2181 | conf->max_nr_stripes++; |
3f294f4f N |
2182 | return 1; |
2183 | } | |
2184 | ||
d1688a6d | 2185 | static int grow_stripes(struct r5conf *conf, int num) |
3f294f4f | 2186 | { |
e18b890b | 2187 | struct kmem_cache *sc; |
5e5e3e78 | 2188 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
1da177e4 | 2189 | |
f4be6b43 N |
2190 | if (conf->mddev->gendisk) |
2191 | sprintf(conf->cache_name[0], | |
2192 | "raid%d-%s", conf->level, mdname(conf->mddev)); | |
2193 | else | |
2194 | sprintf(conf->cache_name[0], | |
2195 | "raid%d-%p", conf->level, conf->mddev); | |
2196 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); | |
2197 | ||
ad01c9e3 N |
2198 | conf->active_name = 0; |
2199 | sc = kmem_cache_create(conf->cache_name[conf->active_name], | |
1da177e4 | 2200 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
20c2df83 | 2201 | 0, 0, NULL); |
1da177e4 LT |
2202 | if (!sc) |
2203 | return 1; | |
2204 | conf->slab_cache = sc; | |
ad01c9e3 | 2205 | conf->pool_size = devs; |
486f0644 N |
2206 | while (num--) |
2207 | if (!grow_one_stripe(conf, GFP_KERNEL)) | |
1da177e4 | 2208 | return 1; |
486f0644 | 2209 | |
1da177e4 LT |
2210 | return 0; |
2211 | } | |
29269553 | 2212 | |
d6f38f31 DW |
2213 | /** |
2214 | * scribble_len - return the required size of the scribble region | |
2215 | * @num - total number of disks in the array | |
2216 | * | |
2217 | * The size must be enough to contain: | |
2218 | * 1/ a struct page pointer for each device in the array +2 | |
2219 | * 2/ room to convert each entry in (1) to its corresponding dma | |
2220 | * (dma_map_page()) or page (page_address()) address. | |
2221 | * | |
2222 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we | |
2223 | * calculate over all devices (not just the data blocks), using zeros in place | |
2224 | * of the P and Q blocks. | |
2225 | */ | |
46d5b785 | 2226 | static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags) |
d6f38f31 | 2227 | { |
46d5b785 | 2228 | struct flex_array *ret; |
d6f38f31 DW |
2229 | size_t len; |
2230 | ||
2231 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); | |
46d5b785 | 2232 | ret = flex_array_alloc(len, cnt, flags); |
2233 | if (!ret) | |
2234 | return NULL; | |
2235 | /* always prealloc all elements, so no locking is required */ | |
2236 | if (flex_array_prealloc(ret, 0, cnt, flags)) { | |
2237 | flex_array_free(ret); | |
2238 | return NULL; | |
2239 | } | |
2240 | return ret; | |
d6f38f31 DW |
2241 | } |
2242 | ||
738a2738 N |
2243 | static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) |
2244 | { | |
2245 | unsigned long cpu; | |
2246 | int err = 0; | |
2247 | ||
27a353c0 SL |
2248 | /* |
2249 | * Never shrink. And mddev_suspend() could deadlock if this is called | |
2250 | * from raid5d. In that case, scribble_disks and scribble_sectors | |
2251 | * should equal to new_disks and new_sectors | |
2252 | */ | |
2253 | if (conf->scribble_disks >= new_disks && | |
2254 | conf->scribble_sectors >= new_sectors) | |
2255 | return 0; | |
738a2738 N |
2256 | mddev_suspend(conf->mddev); |
2257 | get_online_cpus(); | |
2258 | for_each_present_cpu(cpu) { | |
2259 | struct raid5_percpu *percpu; | |
2260 | struct flex_array *scribble; | |
2261 | ||
2262 | percpu = per_cpu_ptr(conf->percpu, cpu); | |
2263 | scribble = scribble_alloc(new_disks, | |
2264 | new_sectors / STRIPE_SECTORS, | |
2265 | GFP_NOIO); | |
2266 | ||
2267 | if (scribble) { | |
2268 | flex_array_free(percpu->scribble); | |
2269 | percpu->scribble = scribble; | |
2270 | } else { | |
2271 | err = -ENOMEM; | |
2272 | break; | |
2273 | } | |
2274 | } | |
2275 | put_online_cpus(); | |
2276 | mddev_resume(conf->mddev); | |
27a353c0 SL |
2277 | if (!err) { |
2278 | conf->scribble_disks = new_disks; | |
2279 | conf->scribble_sectors = new_sectors; | |
2280 | } | |
738a2738 N |
2281 | return err; |
2282 | } | |
2283 | ||
d1688a6d | 2284 | static int resize_stripes(struct r5conf *conf, int newsize) |
ad01c9e3 N |
2285 | { |
2286 | /* Make all the stripes able to hold 'newsize' devices. | |
2287 | * New slots in each stripe get 'page' set to a new page. | |
2288 | * | |
2289 | * This happens in stages: | |
2290 | * 1/ create a new kmem_cache and allocate the required number of | |
2291 | * stripe_heads. | |
83f0d77a | 2292 | * 2/ gather all the old stripe_heads and transfer the pages across |
ad01c9e3 N |
2293 | * to the new stripe_heads. This will have the side effect of |
2294 | * freezing the array as once all stripe_heads have been collected, | |
2295 | * no IO will be possible. Old stripe heads are freed once their | |
2296 | * pages have been transferred over, and the old kmem_cache is | |
2297 | * freed when all stripes are done. | |
2298 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, | |
3560741e | 2299 | * we simple return a failure status - no need to clean anything up. |
ad01c9e3 N |
2300 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
2301 | * If this fails, we don't bother trying the shrink the | |
2302 | * stripe_heads down again, we just leave them as they are. | |
2303 | * As each stripe_head is processed the new one is released into | |
2304 | * active service. | |
2305 | * | |
2306 | * Once step2 is started, we cannot afford to wait for a write, | |
2307 | * so we use GFP_NOIO allocations. | |
2308 | */ | |
2309 | struct stripe_head *osh, *nsh; | |
2310 | LIST_HEAD(newstripes); | |
2311 | struct disk_info *ndisks; | |
2214c260 | 2312 | int err = 0; |
e18b890b | 2313 | struct kmem_cache *sc; |
ad01c9e3 | 2314 | int i; |
566c09c5 | 2315 | int hash, cnt; |
ad01c9e3 | 2316 | |
2214c260 | 2317 | md_allow_write(conf->mddev); |
2a2275d6 | 2318 | |
ad01c9e3 N |
2319 | /* Step 1 */ |
2320 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], | |
2321 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), | |
20c2df83 | 2322 | 0, 0, NULL); |
ad01c9e3 N |
2323 | if (!sc) |
2324 | return -ENOMEM; | |
2325 | ||
2d5b569b N |
2326 | /* Need to ensure auto-resizing doesn't interfere */ |
2327 | mutex_lock(&conf->cache_size_mutex); | |
2328 | ||
ad01c9e3 | 2329 | for (i = conf->max_nr_stripes; i; i--) { |
845b9e22 | 2330 | nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf); |
ad01c9e3 N |
2331 | if (!nsh) |
2332 | break; | |
2333 | ||
ad01c9e3 N |
2334 | list_add(&nsh->lru, &newstripes); |
2335 | } | |
2336 | if (i) { | |
2337 | /* didn't get enough, give up */ | |
2338 | while (!list_empty(&newstripes)) { | |
2339 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
2340 | list_del(&nsh->lru); | |
845b9e22 | 2341 | free_stripe(sc, nsh); |
ad01c9e3 N |
2342 | } |
2343 | kmem_cache_destroy(sc); | |
2d5b569b | 2344 | mutex_unlock(&conf->cache_size_mutex); |
ad01c9e3 N |
2345 | return -ENOMEM; |
2346 | } | |
2347 | /* Step 2 - Must use GFP_NOIO now. | |
2348 | * OK, we have enough stripes, start collecting inactive | |
2349 | * stripes and copying them over | |
2350 | */ | |
566c09c5 SL |
2351 | hash = 0; |
2352 | cnt = 0; | |
ad01c9e3 | 2353 | list_for_each_entry(nsh, &newstripes, lru) { |
566c09c5 | 2354 | lock_device_hash_lock(conf, hash); |
6ab2a4b8 | 2355 | wait_event_cmd(conf->wait_for_stripe, |
566c09c5 SL |
2356 | !list_empty(conf->inactive_list + hash), |
2357 | unlock_device_hash_lock(conf, hash), | |
2358 | lock_device_hash_lock(conf, hash)); | |
2359 | osh = get_free_stripe(conf, hash); | |
2360 | unlock_device_hash_lock(conf, hash); | |
f18c1a35 | 2361 | |
d592a996 | 2362 | for(i=0; i<conf->pool_size; i++) { |
ad01c9e3 | 2363 | nsh->dev[i].page = osh->dev[i].page; |
d592a996 SL |
2364 | nsh->dev[i].orig_page = osh->dev[i].page; |
2365 | } | |
566c09c5 | 2366 | nsh->hash_lock_index = hash; |
845b9e22 | 2367 | free_stripe(conf->slab_cache, osh); |
566c09c5 SL |
2368 | cnt++; |
2369 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + | |
2370 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { | |
2371 | hash++; | |
2372 | cnt = 0; | |
2373 | } | |
ad01c9e3 N |
2374 | } |
2375 | kmem_cache_destroy(conf->slab_cache); | |
2376 | ||
2377 | /* Step 3. | |
2378 | * At this point, we are holding all the stripes so the array | |
2379 | * is completely stalled, so now is a good time to resize | |
d6f38f31 | 2380 | * conf->disks and the scribble region |
ad01c9e3 N |
2381 | */ |
2382 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); | |
2383 | if (ndisks) { | |
d7bd398e | 2384 | for (i = 0; i < conf->pool_size; i++) |
ad01c9e3 | 2385 | ndisks[i] = conf->disks[i]; |
d7bd398e SL |
2386 | |
2387 | for (i = conf->pool_size; i < newsize; i++) { | |
2388 | ndisks[i].extra_page = alloc_page(GFP_NOIO); | |
2389 | if (!ndisks[i].extra_page) | |
2390 | err = -ENOMEM; | |
2391 | } | |
2392 | ||
2393 | if (err) { | |
2394 | for (i = conf->pool_size; i < newsize; i++) | |
2395 | if (ndisks[i].extra_page) | |
2396 | put_page(ndisks[i].extra_page); | |
2397 | kfree(ndisks); | |
2398 | } else { | |
2399 | kfree(conf->disks); | |
2400 | conf->disks = ndisks; | |
2401 | } | |
ad01c9e3 N |
2402 | } else |
2403 | err = -ENOMEM; | |
2404 | ||
2d5b569b | 2405 | mutex_unlock(&conf->cache_size_mutex); |
583da48e DY |
2406 | |
2407 | conf->slab_cache = sc; | |
2408 | conf->active_name = 1-conf->active_name; | |
2409 | ||
ad01c9e3 N |
2410 | /* Step 4, return new stripes to service */ |
2411 | while(!list_empty(&newstripes)) { | |
2412 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
2413 | list_del_init(&nsh->lru); | |
d6f38f31 | 2414 | |
ad01c9e3 N |
2415 | for (i=conf->raid_disks; i < newsize; i++) |
2416 | if (nsh->dev[i].page == NULL) { | |
2417 | struct page *p = alloc_page(GFP_NOIO); | |
2418 | nsh->dev[i].page = p; | |
d592a996 | 2419 | nsh->dev[i].orig_page = p; |
ad01c9e3 N |
2420 | if (!p) |
2421 | err = -ENOMEM; | |
2422 | } | |
6d036f7d | 2423 | raid5_release_stripe(nsh); |
ad01c9e3 N |
2424 | } |
2425 | /* critical section pass, GFP_NOIO no longer needed */ | |
2426 | ||
6e9eac2d N |
2427 | if (!err) |
2428 | conf->pool_size = newsize; | |
ad01c9e3 N |
2429 | return err; |
2430 | } | |
1da177e4 | 2431 | |
486f0644 | 2432 | static int drop_one_stripe(struct r5conf *conf) |
1da177e4 LT |
2433 | { |
2434 | struct stripe_head *sh; | |
49895bcc | 2435 | int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; |
1da177e4 | 2436 | |
566c09c5 SL |
2437 | spin_lock_irq(conf->hash_locks + hash); |
2438 | sh = get_free_stripe(conf, hash); | |
2439 | spin_unlock_irq(conf->hash_locks + hash); | |
3f294f4f N |
2440 | if (!sh) |
2441 | return 0; | |
78bafebd | 2442 | BUG_ON(atomic_read(&sh->count)); |
e4e11e38 | 2443 | shrink_buffers(sh); |
845b9e22 | 2444 | free_stripe(conf->slab_cache, sh); |
3f294f4f | 2445 | atomic_dec(&conf->active_stripes); |
486f0644 | 2446 | conf->max_nr_stripes--; |
3f294f4f N |
2447 | return 1; |
2448 | } | |
2449 | ||
d1688a6d | 2450 | static void shrink_stripes(struct r5conf *conf) |
3f294f4f | 2451 | { |
486f0644 N |
2452 | while (conf->max_nr_stripes && |
2453 | drop_one_stripe(conf)) | |
2454 | ; | |
3f294f4f | 2455 | |
644df1a8 | 2456 | kmem_cache_destroy(conf->slab_cache); |
1da177e4 LT |
2457 | conf->slab_cache = NULL; |
2458 | } | |
2459 | ||
4246a0b6 | 2460 | static void raid5_end_read_request(struct bio * bi) |
1da177e4 | 2461 | { |
99c0fb5f | 2462 | struct stripe_head *sh = bi->bi_private; |
d1688a6d | 2463 | struct r5conf *conf = sh->raid_conf; |
7ecaa1e6 | 2464 | int disks = sh->disks, i; |
d6950432 | 2465 | char b[BDEVNAME_SIZE]; |
dd054fce | 2466 | struct md_rdev *rdev = NULL; |
05616be5 | 2467 | sector_t s; |
1da177e4 LT |
2468 | |
2469 | for (i=0 ; i<disks; i++) | |
2470 | if (bi == &sh->dev[i].req) | |
2471 | break; | |
2472 | ||
4246a0b6 | 2473 | pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", |
45b4233c | 2474 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
4246a0b6 | 2475 | bi->bi_error); |
1da177e4 | 2476 | if (i == disks) { |
5f9d1fde | 2477 | bio_reset(bi); |
1da177e4 | 2478 | BUG(); |
6712ecf8 | 2479 | return; |
1da177e4 | 2480 | } |
14a75d3e | 2481 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
dd054fce N |
2482 | /* If replacement finished while this request was outstanding, |
2483 | * 'replacement' might be NULL already. | |
2484 | * In that case it moved down to 'rdev'. | |
2485 | * rdev is not removed until all requests are finished. | |
2486 | */ | |
14a75d3e | 2487 | rdev = conf->disks[i].replacement; |
dd054fce | 2488 | if (!rdev) |
14a75d3e | 2489 | rdev = conf->disks[i].rdev; |
1da177e4 | 2490 | |
05616be5 N |
2491 | if (use_new_offset(conf, sh)) |
2492 | s = sh->sector + rdev->new_data_offset; | |
2493 | else | |
2494 | s = sh->sector + rdev->data_offset; | |
4246a0b6 | 2495 | if (!bi->bi_error) { |
1da177e4 | 2496 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
4e5314b5 | 2497 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
14a75d3e N |
2498 | /* Note that this cannot happen on a |
2499 | * replacement device. We just fail those on | |
2500 | * any error | |
2501 | */ | |
cc6167b4 N |
2502 | pr_info_ratelimited( |
2503 | "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n", | |
8bda470e | 2504 | mdname(conf->mddev), STRIPE_SECTORS, |
05616be5 | 2505 | (unsigned long long)s, |
8bda470e | 2506 | bdevname(rdev->bdev, b)); |
ddd5115f | 2507 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
4e5314b5 N |
2508 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
2509 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
3f9e7c14 | 2510 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
2511 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
2512 | ||
86aa1397 SL |
2513 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
2514 | /* | |
2515 | * end read for a page in journal, this | |
2516 | * must be preparing for prexor in rmw | |
2517 | */ | |
2518 | set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); | |
2519 | ||
14a75d3e N |
2520 | if (atomic_read(&rdev->read_errors)) |
2521 | atomic_set(&rdev->read_errors, 0); | |
1da177e4 | 2522 | } else { |
14a75d3e | 2523 | const char *bdn = bdevname(rdev->bdev, b); |
ba22dcbf | 2524 | int retry = 0; |
2e8ac303 | 2525 | int set_bad = 0; |
d6950432 | 2526 | |
1da177e4 | 2527 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
d6950432 | 2528 | atomic_inc(&rdev->read_errors); |
14a75d3e | 2529 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
cc6167b4 N |
2530 | pr_warn_ratelimited( |
2531 | "md/raid:%s: read error on replacement device (sector %llu on %s).\n", | |
14a75d3e | 2532 | mdname(conf->mddev), |
05616be5 | 2533 | (unsigned long long)s, |
14a75d3e | 2534 | bdn); |
2e8ac303 | 2535 | else if (conf->mddev->degraded >= conf->max_degraded) { |
2536 | set_bad = 1; | |
cc6167b4 N |
2537 | pr_warn_ratelimited( |
2538 | "md/raid:%s: read error not correctable (sector %llu on %s).\n", | |
8bda470e | 2539 | mdname(conf->mddev), |
05616be5 | 2540 | (unsigned long long)s, |
8bda470e | 2541 | bdn); |
2e8ac303 | 2542 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
4e5314b5 | 2543 | /* Oh, no!!! */ |
2e8ac303 | 2544 | set_bad = 1; |
cc6167b4 N |
2545 | pr_warn_ratelimited( |
2546 | "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n", | |
8bda470e | 2547 | mdname(conf->mddev), |
05616be5 | 2548 | (unsigned long long)s, |
8bda470e | 2549 | bdn); |
2e8ac303 | 2550 | } else if (atomic_read(&rdev->read_errors) |
ba22dcbf | 2551 | > conf->max_nr_stripes) |
cc6167b4 | 2552 | pr_warn("md/raid:%s: Too many read errors, failing device %s.\n", |
d6950432 | 2553 | mdname(conf->mddev), bdn); |
ba22dcbf N |
2554 | else |
2555 | retry = 1; | |
edfa1f65 BY |
2556 | if (set_bad && test_bit(In_sync, &rdev->flags) |
2557 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) | |
2558 | retry = 1; | |
ba22dcbf | 2559 | if (retry) |
3f9e7c14 | 2560 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
2561 | set_bit(R5_ReadError, &sh->dev[i].flags); | |
2562 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
2563 | } else | |
2564 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); | |
ba22dcbf | 2565 | else { |
4e5314b5 N |
2566 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
2567 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
2e8ac303 | 2568 | if (!(set_bad |
2569 | && test_bit(In_sync, &rdev->flags) | |
2570 | && rdev_set_badblocks( | |
2571 | rdev, sh->sector, STRIPE_SECTORS, 0))) | |
2572 | md_error(conf->mddev, rdev); | |
ba22dcbf | 2573 | } |
1da177e4 | 2574 | } |
14a75d3e | 2575 | rdev_dec_pending(rdev, conf->mddev); |
c9445555 | 2576 | bio_reset(bi); |
1da177e4 LT |
2577 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
2578 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 2579 | raid5_release_stripe(sh); |
1da177e4 LT |
2580 | } |
2581 | ||
4246a0b6 | 2582 | static void raid5_end_write_request(struct bio *bi) |
1da177e4 | 2583 | { |
99c0fb5f | 2584 | struct stripe_head *sh = bi->bi_private; |
d1688a6d | 2585 | struct r5conf *conf = sh->raid_conf; |
7ecaa1e6 | 2586 | int disks = sh->disks, i; |
977df362 | 2587 | struct md_rdev *uninitialized_var(rdev); |
b84db560 N |
2588 | sector_t first_bad; |
2589 | int bad_sectors; | |
977df362 | 2590 | int replacement = 0; |
1da177e4 | 2591 | |
977df362 N |
2592 | for (i = 0 ; i < disks; i++) { |
2593 | if (bi == &sh->dev[i].req) { | |
2594 | rdev = conf->disks[i].rdev; | |
1da177e4 | 2595 | break; |
977df362 N |
2596 | } |
2597 | if (bi == &sh->dev[i].rreq) { | |
2598 | rdev = conf->disks[i].replacement; | |
dd054fce N |
2599 | if (rdev) |
2600 | replacement = 1; | |
2601 | else | |
2602 | /* rdev was removed and 'replacement' | |
2603 | * replaced it. rdev is not removed | |
2604 | * until all requests are finished. | |
2605 | */ | |
2606 | rdev = conf->disks[i].rdev; | |
977df362 N |
2607 | break; |
2608 | } | |
2609 | } | |
4246a0b6 | 2610 | pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", |
1da177e4 | 2611 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
4246a0b6 | 2612 | bi->bi_error); |
1da177e4 | 2613 | if (i == disks) { |
5f9d1fde | 2614 | bio_reset(bi); |
1da177e4 | 2615 | BUG(); |
6712ecf8 | 2616 | return; |
1da177e4 LT |
2617 | } |
2618 | ||
977df362 | 2619 | if (replacement) { |
4246a0b6 | 2620 | if (bi->bi_error) |
977df362 N |
2621 | md_error(conf->mddev, rdev); |
2622 | else if (is_badblock(rdev, sh->sector, | |
2623 | STRIPE_SECTORS, | |
2624 | &first_bad, &bad_sectors)) | |
2625 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); | |
2626 | } else { | |
4246a0b6 | 2627 | if (bi->bi_error) { |
9f97e4b1 | 2628 | set_bit(STRIPE_DEGRADED, &sh->state); |
977df362 N |
2629 | set_bit(WriteErrorSeen, &rdev->flags); |
2630 | set_bit(R5_WriteError, &sh->dev[i].flags); | |
3a6de292 N |
2631 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
2632 | set_bit(MD_RECOVERY_NEEDED, | |
2633 | &rdev->mddev->recovery); | |
977df362 N |
2634 | } else if (is_badblock(rdev, sh->sector, |
2635 | STRIPE_SECTORS, | |
c0b32972 | 2636 | &first_bad, &bad_sectors)) { |
977df362 | 2637 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
c0b32972 N |
2638 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
2639 | /* That was a successful write so make | |
2640 | * sure it looks like we already did | |
2641 | * a re-write. | |
2642 | */ | |
2643 | set_bit(R5_ReWrite, &sh->dev[i].flags); | |
2644 | } | |
977df362 N |
2645 | } |
2646 | rdev_dec_pending(rdev, conf->mddev); | |
1da177e4 | 2647 | |
4246a0b6 | 2648 | if (sh->batch_head && bi->bi_error && !replacement) |
72ac7330 | 2649 | set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); |
2650 | ||
c9445555 | 2651 | bio_reset(bi); |
977df362 N |
2652 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
2653 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
1da177e4 | 2654 | set_bit(STRIPE_HANDLE, &sh->state); |
6d036f7d | 2655 | raid5_release_stripe(sh); |
59fc630b | 2656 | |
2657 | if (sh->batch_head && sh != sh->batch_head) | |
6d036f7d | 2658 | raid5_release_stripe(sh->batch_head); |
1da177e4 LT |
2659 | } |
2660 | ||
784052ec | 2661 | static void raid5_build_block(struct stripe_head *sh, int i, int previous) |
1da177e4 LT |
2662 | { |
2663 | struct r5dev *dev = &sh->dev[i]; | |
2664 | ||
1da177e4 | 2665 | dev->flags = 0; |
6d036f7d | 2666 | dev->sector = raid5_compute_blocknr(sh, i, previous); |
1da177e4 LT |
2667 | } |
2668 | ||
849674e4 | 2669 | static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 LT |
2670 | { |
2671 | char b[BDEVNAME_SIZE]; | |
d1688a6d | 2672 | struct r5conf *conf = mddev->private; |
908f4fbd | 2673 | unsigned long flags; |
0c55e022 | 2674 | pr_debug("raid456: error called\n"); |
1da177e4 | 2675 | |
908f4fbd N |
2676 | spin_lock_irqsave(&conf->device_lock, flags); |
2677 | clear_bit(In_sync, &rdev->flags); | |
2e38a37f | 2678 | mddev->degraded = raid5_calc_degraded(conf); |
908f4fbd N |
2679 | spin_unlock_irqrestore(&conf->device_lock, flags); |
2680 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); | |
2681 | ||
de393cde | 2682 | set_bit(Blocked, &rdev->flags); |
6f8d0c77 | 2683 | set_bit(Faulty, &rdev->flags); |
2953079c SL |
2684 | set_mask_bits(&mddev->sb_flags, 0, |
2685 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); | |
cc6167b4 N |
2686 | pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n" |
2687 | "md/raid:%s: Operation continuing on %d devices.\n", | |
2688 | mdname(mddev), | |
2689 | bdevname(rdev->bdev, b), | |
2690 | mdname(mddev), | |
2691 | conf->raid_disks - mddev->degraded); | |
2e38a37f | 2692 | r5c_update_on_rdev_error(mddev); |
16a53ecc | 2693 | } |
1da177e4 LT |
2694 | |
2695 | /* | |
2696 | * Input: a 'big' sector number, | |
2697 | * Output: index of the data and parity disk, and the sector # in them. | |
2698 | */ | |
6d036f7d SL |
2699 | sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
2700 | int previous, int *dd_idx, | |
2701 | struct stripe_head *sh) | |
1da177e4 | 2702 | { |
6e3b96ed | 2703 | sector_t stripe, stripe2; |
35f2a591 | 2704 | sector_t chunk_number; |
1da177e4 | 2705 | unsigned int chunk_offset; |
911d4ee8 | 2706 | int pd_idx, qd_idx; |
67cc2b81 | 2707 | int ddf_layout = 0; |
1da177e4 | 2708 | sector_t new_sector; |
e183eaed N |
2709 | int algorithm = previous ? conf->prev_algo |
2710 | : conf->algorithm; | |
09c9e5fa AN |
2711 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
2712 | : conf->chunk_sectors; | |
112bf897 N |
2713 | int raid_disks = previous ? conf->previous_raid_disks |
2714 | : conf->raid_disks; | |
2715 | int data_disks = raid_disks - conf->max_degraded; | |
1da177e4 LT |
2716 | |
2717 | /* First compute the information on this sector */ | |
2718 | ||
2719 | /* | |
2720 | * Compute the chunk number and the sector offset inside the chunk | |
2721 | */ | |
2722 | chunk_offset = sector_div(r_sector, sectors_per_chunk); | |
2723 | chunk_number = r_sector; | |
1da177e4 LT |
2724 | |
2725 | /* | |
2726 | * Compute the stripe number | |
2727 | */ | |
35f2a591 N |
2728 | stripe = chunk_number; |
2729 | *dd_idx = sector_div(stripe, data_disks); | |
6e3b96ed | 2730 | stripe2 = stripe; |
1da177e4 LT |
2731 | /* |
2732 | * Select the parity disk based on the user selected algorithm. | |
2733 | */ | |
84789554 | 2734 | pd_idx = qd_idx = -1; |
16a53ecc N |
2735 | switch(conf->level) { |
2736 | case 4: | |
911d4ee8 | 2737 | pd_idx = data_disks; |
16a53ecc N |
2738 | break; |
2739 | case 5: | |
e183eaed | 2740 | switch (algorithm) { |
1da177e4 | 2741 | case ALGORITHM_LEFT_ASYMMETRIC: |
6e3b96ed | 2742 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
911d4ee8 | 2743 | if (*dd_idx >= pd_idx) |
1da177e4 LT |
2744 | (*dd_idx)++; |
2745 | break; | |
2746 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
6e3b96ed | 2747 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 | 2748 | if (*dd_idx >= pd_idx) |
1da177e4 LT |
2749 | (*dd_idx)++; |
2750 | break; | |
2751 | case ALGORITHM_LEFT_SYMMETRIC: | |
6e3b96ed | 2752 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
911d4ee8 | 2753 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
1da177e4 LT |
2754 | break; |
2755 | case ALGORITHM_RIGHT_SYMMETRIC: | |
6e3b96ed | 2756 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 | 2757 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
1da177e4 | 2758 | break; |
99c0fb5f N |
2759 | case ALGORITHM_PARITY_0: |
2760 | pd_idx = 0; | |
2761 | (*dd_idx)++; | |
2762 | break; | |
2763 | case ALGORITHM_PARITY_N: | |
2764 | pd_idx = data_disks; | |
2765 | break; | |
1da177e4 | 2766 | default: |
99c0fb5f | 2767 | BUG(); |
16a53ecc N |
2768 | } |
2769 | break; | |
2770 | case 6: | |
2771 | ||
e183eaed | 2772 | switch (algorithm) { |
16a53ecc | 2773 | case ALGORITHM_LEFT_ASYMMETRIC: |
6e3b96ed | 2774 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
911d4ee8 N |
2775 | qd_idx = pd_idx + 1; |
2776 | if (pd_idx == raid_disks-1) { | |
99c0fb5f | 2777 | (*dd_idx)++; /* Q D D D P */ |
911d4ee8 N |
2778 | qd_idx = 0; |
2779 | } else if (*dd_idx >= pd_idx) | |
16a53ecc N |
2780 | (*dd_idx) += 2; /* D D P Q D */ |
2781 | break; | |
2782 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
6e3b96ed | 2783 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 N |
2784 | qd_idx = pd_idx + 1; |
2785 | if (pd_idx == raid_disks-1) { | |
99c0fb5f | 2786 | (*dd_idx)++; /* Q D D D P */ |
911d4ee8 N |
2787 | qd_idx = 0; |
2788 | } else if (*dd_idx >= pd_idx) | |
16a53ecc N |
2789 | (*dd_idx) += 2; /* D D P Q D */ |
2790 | break; | |
2791 | case ALGORITHM_LEFT_SYMMETRIC: | |
6e3b96ed | 2792 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
911d4ee8 N |
2793 | qd_idx = (pd_idx + 1) % raid_disks; |
2794 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; | |
16a53ecc N |
2795 | break; |
2796 | case ALGORITHM_RIGHT_SYMMETRIC: | |
6e3b96ed | 2797 | pd_idx = sector_div(stripe2, raid_disks); |
911d4ee8 N |
2798 | qd_idx = (pd_idx + 1) % raid_disks; |
2799 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; | |
16a53ecc | 2800 | break; |
99c0fb5f N |
2801 | |
2802 | case ALGORITHM_PARITY_0: | |
2803 | pd_idx = 0; | |
2804 | qd_idx = 1; | |
2805 | (*dd_idx) += 2; | |
2806 | break; | |
2807 | case ALGORITHM_PARITY_N: | |
2808 | pd_idx = data_disks; | |
2809 | qd_idx = data_disks + 1; | |
2810 | break; | |
2811 | ||
2812 | case ALGORITHM_ROTATING_ZERO_RESTART: | |
2813 | /* Exactly the same as RIGHT_ASYMMETRIC, but or | |
2814 | * of blocks for computing Q is different. | |
2815 | */ | |
6e3b96ed | 2816 | pd_idx = sector_div(stripe2, raid_disks); |
99c0fb5f N |
2817 | qd_idx = pd_idx + 1; |
2818 | if (pd_idx == raid_disks-1) { | |
2819 | (*dd_idx)++; /* Q D D D P */ | |
2820 | qd_idx = 0; | |
2821 | } else if (*dd_idx >= pd_idx) | |
2822 | (*dd_idx) += 2; /* D D P Q D */ | |
67cc2b81 | 2823 | ddf_layout = 1; |
99c0fb5f N |
2824 | break; |
2825 | ||
2826 | case ALGORITHM_ROTATING_N_RESTART: | |
2827 | /* Same a left_asymmetric, by first stripe is | |
2828 | * D D D P Q rather than | |
2829 | * Q D D D P | |
2830 | */ | |
6e3b96ed N |
2831 | stripe2 += 1; |
2832 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); | |
99c0fb5f N |
2833 | qd_idx = pd_idx + 1; |
2834 | if (pd_idx == raid_disks-1) { | |
2835 | (*dd_idx)++; /* Q D D D P */ | |
2836 | qd_idx = 0; | |
2837 | } else if (*dd_idx >= pd_idx) | |
2838 | (*dd_idx) += 2; /* D D P Q D */ | |
67cc2b81 | 2839 | ddf_layout = 1; |
99c0fb5f N |
2840 | break; |
2841 | ||
2842 | case ALGORITHM_ROTATING_N_CONTINUE: | |
2843 | /* Same as left_symmetric but Q is before P */ | |
6e3b96ed | 2844 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
99c0fb5f N |
2845 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
2846 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; | |
67cc2b81 | 2847 | ddf_layout = 1; |
99c0fb5f N |
2848 | break; |
2849 | ||
2850 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
2851 | /* RAID5 left_asymmetric, with Q on last device */ | |
6e3b96ed | 2852 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2853 | if (*dd_idx >= pd_idx) |
2854 | (*dd_idx)++; | |
2855 | qd_idx = raid_disks - 1; | |
2856 | break; | |
2857 | ||
2858 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
6e3b96ed | 2859 | pd_idx = sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2860 | if (*dd_idx >= pd_idx) |
2861 | (*dd_idx)++; | |
2862 | qd_idx = raid_disks - 1; | |
2863 | break; | |
2864 | ||
2865 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
6e3b96ed | 2866 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2867 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
2868 | qd_idx = raid_disks - 1; | |
2869 | break; | |
2870 | ||
2871 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
6e3b96ed | 2872 | pd_idx = sector_div(stripe2, raid_disks-1); |
99c0fb5f N |
2873 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
2874 | qd_idx = raid_disks - 1; | |
2875 | break; | |
2876 | ||
2877 | case ALGORITHM_PARITY_0_6: | |
2878 | pd_idx = 0; | |
2879 | (*dd_idx)++; | |
2880 | qd_idx = raid_disks - 1; | |
2881 | break; | |
2882 | ||
16a53ecc | 2883 | default: |
99c0fb5f | 2884 | BUG(); |
16a53ecc N |
2885 | } |
2886 | break; | |
1da177e4 LT |
2887 | } |
2888 | ||
911d4ee8 N |
2889 | if (sh) { |
2890 | sh->pd_idx = pd_idx; | |
2891 | sh->qd_idx = qd_idx; | |
67cc2b81 | 2892 | sh->ddf_layout = ddf_layout; |
911d4ee8 | 2893 | } |
1da177e4 LT |
2894 | /* |
2895 | * Finally, compute the new sector number | |
2896 | */ | |
2897 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; | |
2898 | return new_sector; | |
2899 | } | |
2900 | ||
6d036f7d | 2901 | sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous) |
1da177e4 | 2902 | { |
d1688a6d | 2903 | struct r5conf *conf = sh->raid_conf; |
b875e531 N |
2904 | int raid_disks = sh->disks; |
2905 | int data_disks = raid_disks - conf->max_degraded; | |
1da177e4 | 2906 | sector_t new_sector = sh->sector, check; |
09c9e5fa AN |
2907 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
2908 | : conf->chunk_sectors; | |
e183eaed N |
2909 | int algorithm = previous ? conf->prev_algo |
2910 | : conf->algorithm; | |
1da177e4 LT |
2911 | sector_t stripe; |
2912 | int chunk_offset; | |
35f2a591 N |
2913 | sector_t chunk_number; |
2914 | int dummy1, dd_idx = i; | |
1da177e4 | 2915 | sector_t r_sector; |
911d4ee8 | 2916 | struct stripe_head sh2; |
1da177e4 LT |
2917 | |
2918 | chunk_offset = sector_div(new_sector, sectors_per_chunk); | |
2919 | stripe = new_sector; | |
1da177e4 | 2920 | |
16a53ecc N |
2921 | if (i == sh->pd_idx) |
2922 | return 0; | |
2923 | switch(conf->level) { | |
2924 | case 4: break; | |
2925 | case 5: | |
e183eaed | 2926 | switch (algorithm) { |
1da177e4 LT |
2927 | case ALGORITHM_LEFT_ASYMMETRIC: |
2928 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
2929 | if (i > sh->pd_idx) | |
2930 | i--; | |
2931 | break; | |
2932 | case ALGORITHM_LEFT_SYMMETRIC: | |
2933 | case ALGORITHM_RIGHT_SYMMETRIC: | |
2934 | if (i < sh->pd_idx) | |
2935 | i += raid_disks; | |
2936 | i -= (sh->pd_idx + 1); | |
2937 | break; | |
99c0fb5f N |
2938 | case ALGORITHM_PARITY_0: |
2939 | i -= 1; | |
2940 | break; | |
2941 | case ALGORITHM_PARITY_N: | |
2942 | break; | |
1da177e4 | 2943 | default: |
99c0fb5f | 2944 | BUG(); |
16a53ecc N |
2945 | } |
2946 | break; | |
2947 | case 6: | |
d0dabf7e | 2948 | if (i == sh->qd_idx) |
16a53ecc | 2949 | return 0; /* It is the Q disk */ |
e183eaed | 2950 | switch (algorithm) { |
16a53ecc N |
2951 | case ALGORITHM_LEFT_ASYMMETRIC: |
2952 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
99c0fb5f N |
2953 | case ALGORITHM_ROTATING_ZERO_RESTART: |
2954 | case ALGORITHM_ROTATING_N_RESTART: | |
2955 | if (sh->pd_idx == raid_disks-1) | |
2956 | i--; /* Q D D D P */ | |
16a53ecc N |
2957 | else if (i > sh->pd_idx) |
2958 | i -= 2; /* D D P Q D */ | |
2959 | break; | |
2960 | case ALGORITHM_LEFT_SYMMETRIC: | |
2961 | case ALGORITHM_RIGHT_SYMMETRIC: | |
2962 | if (sh->pd_idx == raid_disks-1) | |
2963 | i--; /* Q D D D P */ | |
2964 | else { | |
2965 | /* D D P Q D */ | |
2966 | if (i < sh->pd_idx) | |
2967 | i += raid_disks; | |
2968 | i -= (sh->pd_idx + 2); | |
2969 | } | |
2970 | break; | |
99c0fb5f N |
2971 | case ALGORITHM_PARITY_0: |
2972 | i -= 2; | |
2973 | break; | |
2974 | case ALGORITHM_PARITY_N: | |
2975 | break; | |
2976 | case ALGORITHM_ROTATING_N_CONTINUE: | |
e4424fee | 2977 | /* Like left_symmetric, but P is before Q */ |
99c0fb5f N |
2978 | if (sh->pd_idx == 0) |
2979 | i--; /* P D D D Q */ | |
e4424fee N |
2980 | else { |
2981 | /* D D Q P D */ | |
2982 | if (i < sh->pd_idx) | |
2983 | i += raid_disks; | |
2984 | i -= (sh->pd_idx + 1); | |
2985 | } | |
99c0fb5f N |
2986 | break; |
2987 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
2988 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
2989 | if (i > sh->pd_idx) | |
2990 | i--; | |
2991 | break; | |
2992 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
2993 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
2994 | if (i < sh->pd_idx) | |
2995 | i += data_disks + 1; | |
2996 | i -= (sh->pd_idx + 1); | |
2997 | break; | |
2998 | case ALGORITHM_PARITY_0_6: | |
2999 | i -= 1; | |
3000 | break; | |
16a53ecc | 3001 | default: |
99c0fb5f | 3002 | BUG(); |
16a53ecc N |
3003 | } |
3004 | break; | |
1da177e4 LT |
3005 | } |
3006 | ||
3007 | chunk_number = stripe * data_disks + i; | |
35f2a591 | 3008 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
1da177e4 | 3009 | |
112bf897 | 3010 | check = raid5_compute_sector(conf, r_sector, |
784052ec | 3011 | previous, &dummy1, &sh2); |
911d4ee8 N |
3012 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
3013 | || sh2.qd_idx != sh->qd_idx) { | |
cc6167b4 N |
3014 | pr_warn("md/raid:%s: compute_blocknr: map not correct\n", |
3015 | mdname(conf->mddev)); | |
1da177e4 LT |
3016 | return 0; |
3017 | } | |
3018 | return r_sector; | |
3019 | } | |
3020 | ||
07e83364 SL |
3021 | /* |
3022 | * There are cases where we want handle_stripe_dirtying() and | |
3023 | * schedule_reconstruction() to delay towrite to some dev of a stripe. | |
3024 | * | |
3025 | * This function checks whether we want to delay the towrite. Specifically, | |
3026 | * we delay the towrite when: | |
3027 | * | |
3028 | * 1. degraded stripe has a non-overwrite to the missing dev, AND this | |
3029 | * stripe has data in journal (for other devices). | |
3030 | * | |
3031 | * In this case, when reading data for the non-overwrite dev, it is | |
3032 | * necessary to handle complex rmw of write back cache (prexor with | |
3033 | * orig_page, and xor with page). To keep read path simple, we would | |
3034 | * like to flush data in journal to RAID disks first, so complex rmw | |
3035 | * is handled in the write patch (handle_stripe_dirtying). | |
3036 | * | |
39b99586 SL |
3037 | * 2. when journal space is critical (R5C_LOG_CRITICAL=1) |
3038 | * | |
3039 | * It is important to be able to flush all stripes in raid5-cache. | |
3040 | * Therefore, we need reserve some space on the journal device for | |
3041 | * these flushes. If flush operation includes pending writes to the | |
3042 | * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe | |
3043 | * for the flush out. If we exclude these pending writes from flush | |
3044 | * operation, we only need (conf->max_degraded + 1) pages per stripe. | |
3045 | * Therefore, excluding pending writes in these cases enables more | |
3046 | * efficient use of the journal device. | |
3047 | * | |
3048 | * Note: To make sure the stripe makes progress, we only delay | |
3049 | * towrite for stripes with data already in journal (injournal > 0). | |
3050 | * When LOG_CRITICAL, stripes with injournal == 0 will be sent to | |
3051 | * no_space_stripes list. | |
3052 | * | |
07e83364 | 3053 | */ |
39b99586 SL |
3054 | static inline bool delay_towrite(struct r5conf *conf, |
3055 | struct r5dev *dev, | |
3056 | struct stripe_head_state *s) | |
07e83364 | 3057 | { |
39b99586 SL |
3058 | /* case 1 above */ |
3059 | if (!test_bit(R5_OVERWRITE, &dev->flags) && | |
3060 | !test_bit(R5_Insync, &dev->flags) && s->injournal) | |
3061 | return true; | |
3062 | /* case 2 above */ | |
3063 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && | |
3064 | s->injournal > 0) | |
3065 | return true; | |
3066 | return false; | |
07e83364 SL |
3067 | } |
3068 | ||
600aa109 | 3069 | static void |
c0f7bddb | 3070 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
600aa109 | 3071 | int rcw, int expand) |
e33129d8 | 3072 | { |
584acdd4 | 3073 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks; |
d1688a6d | 3074 | struct r5conf *conf = sh->raid_conf; |
c0f7bddb | 3075 | int level = conf->level; |
e33129d8 DW |
3076 | |
3077 | if (rcw) { | |
1e6d690b SL |
3078 | /* |
3079 | * In some cases, handle_stripe_dirtying initially decided to | |
3080 | * run rmw and allocates extra page for prexor. However, rcw is | |
3081 | * cheaper later on. We need to free the extra page now, | |
3082 | * because we won't be able to do that in ops_complete_prexor(). | |
3083 | */ | |
3084 | r5c_release_extra_page(sh); | |
e33129d8 DW |
3085 | |
3086 | for (i = disks; i--; ) { | |
3087 | struct r5dev *dev = &sh->dev[i]; | |
3088 | ||
39b99586 | 3089 | if (dev->towrite && !delay_towrite(conf, dev, s)) { |
e33129d8 | 3090 | set_bit(R5_LOCKED, &dev->flags); |
d8ee0728 | 3091 | set_bit(R5_Wantdrain, &dev->flags); |
e33129d8 DW |
3092 | if (!expand) |
3093 | clear_bit(R5_UPTODATE, &dev->flags); | |
600aa109 | 3094 | s->locked++; |
1e6d690b SL |
3095 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
3096 | set_bit(R5_LOCKED, &dev->flags); | |
3097 | s->locked++; | |
e33129d8 DW |
3098 | } |
3099 | } | |
ce7d363a N |
3100 | /* if we are not expanding this is a proper write request, and |
3101 | * there will be bios with new data to be drained into the | |
3102 | * stripe cache | |
3103 | */ | |
3104 | if (!expand) { | |
3105 | if (!s->locked) | |
3106 | /* False alarm, nothing to do */ | |
3107 | return; | |
3108 | sh->reconstruct_state = reconstruct_state_drain_run; | |
3109 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); | |
3110 | } else | |
3111 | sh->reconstruct_state = reconstruct_state_run; | |
3112 | ||
3113 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); | |
3114 | ||
c0f7bddb | 3115 | if (s->locked + conf->max_degraded == disks) |
8b3e6cdc | 3116 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
c0f7bddb | 3117 | atomic_inc(&conf->pending_full_writes); |
e33129d8 DW |
3118 | } else { |
3119 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || | |
3120 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); | |
584acdd4 MS |
3121 | BUG_ON(level == 6 && |
3122 | (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || | |
3123 | test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); | |
e33129d8 | 3124 | |
e33129d8 DW |
3125 | for (i = disks; i--; ) { |
3126 | struct r5dev *dev = &sh->dev[i]; | |
584acdd4 | 3127 | if (i == pd_idx || i == qd_idx) |
e33129d8 DW |
3128 | continue; |
3129 | ||
e33129d8 DW |
3130 | if (dev->towrite && |
3131 | (test_bit(R5_UPTODATE, &dev->flags) || | |
d8ee0728 DW |
3132 | test_bit(R5_Wantcompute, &dev->flags))) { |
3133 | set_bit(R5_Wantdrain, &dev->flags); | |
e33129d8 DW |
3134 | set_bit(R5_LOCKED, &dev->flags); |
3135 | clear_bit(R5_UPTODATE, &dev->flags); | |
600aa109 | 3136 | s->locked++; |
1e6d690b SL |
3137 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
3138 | set_bit(R5_LOCKED, &dev->flags); | |
3139 | s->locked++; | |
e33129d8 DW |
3140 | } |
3141 | } | |
ce7d363a N |
3142 | if (!s->locked) |
3143 | /* False alarm - nothing to do */ | |
3144 | return; | |
3145 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; | |
3146 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); | |
3147 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); | |
3148 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); | |
e33129d8 DW |
3149 | } |
3150 | ||
c0f7bddb | 3151 | /* keep the parity disk(s) locked while asynchronous operations |
e33129d8 DW |
3152 | * are in flight |
3153 | */ | |
3154 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); | |
3155 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
600aa109 | 3156 | s->locked++; |
e33129d8 | 3157 | |
c0f7bddb YT |
3158 | if (level == 6) { |
3159 | int qd_idx = sh->qd_idx; | |
3160 | struct r5dev *dev = &sh->dev[qd_idx]; | |
3161 | ||
3162 | set_bit(R5_LOCKED, &dev->flags); | |
3163 | clear_bit(R5_UPTODATE, &dev->flags); | |
3164 | s->locked++; | |
3165 | } | |
3166 | ||
845b9e22 | 3167 | if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page && |
3418d036 AP |
3168 | test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) && |
3169 | !test_bit(STRIPE_FULL_WRITE, &sh->state) && | |
3170 | test_bit(R5_Insync, &sh->dev[pd_idx].flags)) | |
3171 | set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request); | |
3172 | ||
600aa109 | 3173 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
e46b272b | 3174 | __func__, (unsigned long long)sh->sector, |
600aa109 | 3175 | s->locked, s->ops_request); |
e33129d8 | 3176 | } |
16a53ecc | 3177 | |
1da177e4 LT |
3178 | /* |
3179 | * Each stripe/dev can have one or more bion attached. | |
16a53ecc | 3180 | * toread/towrite point to the first in a chain. |
1da177e4 LT |
3181 | * The bi_next chain must be in order. |
3182 | */ | |
da41ba65 | 3183 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, |
3184 | int forwrite, int previous) | |
1da177e4 LT |
3185 | { |
3186 | struct bio **bip; | |
d1688a6d | 3187 | struct r5conf *conf = sh->raid_conf; |
72626685 | 3188 | int firstwrite=0; |
1da177e4 | 3189 | |
cbe47ec5 | 3190 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
4f024f37 | 3191 | (unsigned long long)bi->bi_iter.bi_sector, |
1da177e4 LT |
3192 | (unsigned long long)sh->sector); |
3193 | ||
b17459c0 | 3194 | spin_lock_irq(&sh->stripe_lock); |
59fc630b | 3195 | /* Don't allow new IO added to stripes in batch list */ |
3196 | if (sh->batch_head) | |
3197 | goto overlap; | |
72626685 | 3198 | if (forwrite) { |
1da177e4 | 3199 | bip = &sh->dev[dd_idx].towrite; |
7eaf7e8e | 3200 | if (*bip == NULL) |
72626685 N |
3201 | firstwrite = 1; |
3202 | } else | |
1da177e4 | 3203 | bip = &sh->dev[dd_idx].toread; |
4f024f37 KO |
3204 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
3205 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) | |
1da177e4 LT |
3206 | goto overlap; |
3207 | bip = & (*bip)->bi_next; | |
3208 | } | |
4f024f37 | 3209 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
1da177e4 LT |
3210 | goto overlap; |
3211 | ||
3418d036 AP |
3212 | if (forwrite && raid5_has_ppl(conf)) { |
3213 | /* | |
3214 | * With PPL only writes to consecutive data chunks within a | |
3215 | * stripe are allowed because for a single stripe_head we can | |
3216 | * only have one PPL entry at a time, which describes one data | |
3217 | * range. Not really an overlap, but wait_for_overlap can be | |
3218 | * used to handle this. | |
3219 | */ | |
3220 | sector_t sector; | |
3221 | sector_t first = 0; | |
3222 | sector_t last = 0; | |
3223 | int count = 0; | |
3224 | int i; | |
3225 | ||
3226 | for (i = 0; i < sh->disks; i++) { | |
3227 | if (i != sh->pd_idx && | |
3228 | (i == dd_idx || sh->dev[i].towrite)) { | |
3229 | sector = sh->dev[i].sector; | |
3230 | if (count == 0 || sector < first) | |
3231 | first = sector; | |
3232 | if (sector > last) | |
3233 | last = sector; | |
3234 | count++; | |
3235 | } | |
3236 | } | |
3237 | ||
3238 | if (first + conf->chunk_sectors * (count - 1) != last) | |
3239 | goto overlap; | |
3240 | } | |
3241 | ||
da41ba65 | 3242 | if (!forwrite || previous) |
3243 | clear_bit(STRIPE_BATCH_READY, &sh->state); | |
3244 | ||
78bafebd | 3245 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
1da177e4 LT |
3246 | if (*bip) |
3247 | bi->bi_next = *bip; | |
3248 | *bip = bi; | |
016c76ac | 3249 | bio_inc_remaining(bi); |
49728050 | 3250 | md_write_inc(conf->mddev, bi); |
72626685 | 3251 | |
1da177e4 LT |
3252 | if (forwrite) { |
3253 | /* check if page is covered */ | |
3254 | sector_t sector = sh->dev[dd_idx].sector; | |
3255 | for (bi=sh->dev[dd_idx].towrite; | |
3256 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && | |
4f024f37 | 3257 | bi && bi->bi_iter.bi_sector <= sector; |
1da177e4 | 3258 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
f73a1c7d KO |
3259 | if (bio_end_sector(bi) >= sector) |
3260 | sector = bio_end_sector(bi); | |
1da177e4 LT |
3261 | } |
3262 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) | |
7a87f434 | 3263 | if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) |
3264 | sh->overwrite_disks++; | |
1da177e4 | 3265 | } |
cbe47ec5 N |
3266 | |
3267 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", | |
4f024f37 | 3268 | (unsigned long long)(*bip)->bi_iter.bi_sector, |
cbe47ec5 N |
3269 | (unsigned long long)sh->sector, dd_idx); |
3270 | ||
3271 | if (conf->mddev->bitmap && firstwrite) { | |
d0852df5 N |
3272 | /* Cannot hold spinlock over bitmap_startwrite, |
3273 | * but must ensure this isn't added to a batch until | |
3274 | * we have added to the bitmap and set bm_seq. | |
3275 | * So set STRIPE_BITMAP_PENDING to prevent | |
3276 | * batching. | |
3277 | * If multiple add_stripe_bio() calls race here they | |
3278 | * much all set STRIPE_BITMAP_PENDING. So only the first one | |
3279 | * to complete "bitmap_startwrite" gets to set | |
3280 | * STRIPE_BIT_DELAY. This is important as once a stripe | |
3281 | * is added to a batch, STRIPE_BIT_DELAY cannot be changed | |
3282 | * any more. | |
3283 | */ | |
3284 | set_bit(STRIPE_BITMAP_PENDING, &sh->state); | |
3285 | spin_unlock_irq(&sh->stripe_lock); | |
cbe47ec5 N |
3286 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
3287 | STRIPE_SECTORS, 0); | |
d0852df5 N |
3288 | spin_lock_irq(&sh->stripe_lock); |
3289 | clear_bit(STRIPE_BITMAP_PENDING, &sh->state); | |
3290 | if (!sh->batch_head) { | |
3291 | sh->bm_seq = conf->seq_flush+1; | |
3292 | set_bit(STRIPE_BIT_DELAY, &sh->state); | |
3293 | } | |
cbe47ec5 | 3294 | } |
d0852df5 | 3295 | spin_unlock_irq(&sh->stripe_lock); |
59fc630b | 3296 | |
3297 | if (stripe_can_batch(sh)) | |
3298 | stripe_add_to_batch_list(conf, sh); | |
1da177e4 LT |
3299 | return 1; |
3300 | ||
3301 | overlap: | |
3302 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); | |
b17459c0 | 3303 | spin_unlock_irq(&sh->stripe_lock); |
1da177e4 LT |
3304 | return 0; |
3305 | } | |
3306 | ||
d1688a6d | 3307 | static void end_reshape(struct r5conf *conf); |
29269553 | 3308 | |
d1688a6d | 3309 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
911d4ee8 | 3310 | struct stripe_head *sh) |
ccfcc3c1 | 3311 | { |
784052ec | 3312 | int sectors_per_chunk = |
09c9e5fa | 3313 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
911d4ee8 | 3314 | int dd_idx; |
2d2063ce | 3315 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
112bf897 | 3316 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
2d2063ce | 3317 | |
112bf897 N |
3318 | raid5_compute_sector(conf, |
3319 | stripe * (disks - conf->max_degraded) | |
b875e531 | 3320 | *sectors_per_chunk + chunk_offset, |
112bf897 | 3321 | previous, |
911d4ee8 | 3322 | &dd_idx, sh); |
ccfcc3c1 N |
3323 | } |
3324 | ||
a4456856 | 3325 | static void |
d1688a6d | 3326 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
bd83d0a2 | 3327 | struct stripe_head_state *s, int disks) |
a4456856 DW |
3328 | { |
3329 | int i; | |
59fc630b | 3330 | BUG_ON(sh->batch_head); |
a4456856 DW |
3331 | for (i = disks; i--; ) { |
3332 | struct bio *bi; | |
3333 | int bitmap_end = 0; | |
3334 | ||
3335 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
3cb03002 | 3336 | struct md_rdev *rdev; |
a4456856 DW |
3337 | rcu_read_lock(); |
3338 | rdev = rcu_dereference(conf->disks[i].rdev); | |
f5b67ae8 N |
3339 | if (rdev && test_bit(In_sync, &rdev->flags) && |
3340 | !test_bit(Faulty, &rdev->flags)) | |
7f0da59b N |
3341 | atomic_inc(&rdev->nr_pending); |
3342 | else | |
3343 | rdev = NULL; | |
a4456856 | 3344 | rcu_read_unlock(); |
7f0da59b N |
3345 | if (rdev) { |
3346 | if (!rdev_set_badblocks( | |
3347 | rdev, | |
3348 | sh->sector, | |
3349 | STRIPE_SECTORS, 0)) | |
3350 | md_error(conf->mddev, rdev); | |
3351 | rdev_dec_pending(rdev, conf->mddev); | |
3352 | } | |
a4456856 | 3353 | } |
b17459c0 | 3354 | spin_lock_irq(&sh->stripe_lock); |
a4456856 DW |
3355 | /* fail all writes first */ |
3356 | bi = sh->dev[i].towrite; | |
3357 | sh->dev[i].towrite = NULL; | |
7a87f434 | 3358 | sh->overwrite_disks = 0; |
b17459c0 | 3359 | spin_unlock_irq(&sh->stripe_lock); |
1ed850f3 | 3360 | if (bi) |
a4456856 | 3361 | bitmap_end = 1; |
a4456856 | 3362 | |
ff875738 | 3363 | log_stripe_write_finished(sh); |
0576b1c6 | 3364 | |
a4456856 DW |
3365 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
3366 | wake_up(&conf->wait_for_overlap); | |
3367 | ||
4f024f37 | 3368 | while (bi && bi->bi_iter.bi_sector < |
a4456856 DW |
3369 | sh->dev[i].sector + STRIPE_SECTORS) { |
3370 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
4246a0b6 CH |
3371 | |
3372 | bi->bi_error = -EIO; | |
49728050 | 3373 | md_write_end(conf->mddev); |
016c76ac | 3374 | bio_endio(bi); |
a4456856 DW |
3375 | bi = nextbi; |
3376 | } | |
7eaf7e8e SL |
3377 | if (bitmap_end) |
3378 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
3379 | STRIPE_SECTORS, 0, 0); | |
3380 | bitmap_end = 0; | |
a4456856 DW |
3381 | /* and fail all 'written' */ |
3382 | bi = sh->dev[i].written; | |
3383 | sh->dev[i].written = NULL; | |
d592a996 SL |
3384 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
3385 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
3386 | sh->dev[i].page = sh->dev[i].orig_page; | |
3387 | } | |
3388 | ||
a4456856 | 3389 | if (bi) bitmap_end = 1; |
4f024f37 | 3390 | while (bi && bi->bi_iter.bi_sector < |
a4456856 DW |
3391 | sh->dev[i].sector + STRIPE_SECTORS) { |
3392 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); | |
4246a0b6 CH |
3393 | |
3394 | bi->bi_error = -EIO; | |
49728050 | 3395 | md_write_end(conf->mddev); |
016c76ac | 3396 | bio_endio(bi); |
a4456856 DW |
3397 | bi = bi2; |
3398 | } | |
3399 | ||
b5e98d65 DW |
3400 | /* fail any reads if this device is non-operational and |
3401 | * the data has not reached the cache yet. | |
3402 | */ | |
3403 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && | |
6e74a9cf | 3404 | s->failed > conf->max_degraded && |
b5e98d65 DW |
3405 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
3406 | test_bit(R5_ReadError, &sh->dev[i].flags))) { | |
143c4d05 | 3407 | spin_lock_irq(&sh->stripe_lock); |
a4456856 DW |
3408 | bi = sh->dev[i].toread; |
3409 | sh->dev[i].toread = NULL; | |
143c4d05 | 3410 | spin_unlock_irq(&sh->stripe_lock); |
a4456856 DW |
3411 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
3412 | wake_up(&conf->wait_for_overlap); | |
ebda780b SL |
3413 | if (bi) |
3414 | s->to_read--; | |
4f024f37 | 3415 | while (bi && bi->bi_iter.bi_sector < |
a4456856 DW |
3416 | sh->dev[i].sector + STRIPE_SECTORS) { |
3417 | struct bio *nextbi = | |
3418 | r5_next_bio(bi, sh->dev[i].sector); | |
4246a0b6 CH |
3419 | |
3420 | bi->bi_error = -EIO; | |
016c76ac | 3421 | bio_endio(bi); |
a4456856 DW |
3422 | bi = nextbi; |
3423 | } | |
3424 | } | |
a4456856 DW |
3425 | if (bitmap_end) |
3426 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
3427 | STRIPE_SECTORS, 0, 0); | |
8cfa7b0f N |
3428 | /* If we were in the middle of a write the parity block might |
3429 | * still be locked - so just clear all R5_LOCKED flags | |
3430 | */ | |
3431 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
a4456856 | 3432 | } |
ebda780b SL |
3433 | s->to_write = 0; |
3434 | s->written = 0; | |
a4456856 | 3435 | |
8b3e6cdc DW |
3436 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
3437 | if (atomic_dec_and_test(&conf->pending_full_writes)) | |
3438 | md_wakeup_thread(conf->mddev->thread); | |
a4456856 DW |
3439 | } |
3440 | ||
7f0da59b | 3441 | static void |
d1688a6d | 3442 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
7f0da59b N |
3443 | struct stripe_head_state *s) |
3444 | { | |
3445 | int abort = 0; | |
3446 | int i; | |
3447 | ||
59fc630b | 3448 | BUG_ON(sh->batch_head); |
7f0da59b | 3449 | clear_bit(STRIPE_SYNCING, &sh->state); |
f8dfcffd N |
3450 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
3451 | wake_up(&conf->wait_for_overlap); | |
7f0da59b | 3452 | s->syncing = 0; |
9a3e1101 | 3453 | s->replacing = 0; |
7f0da59b | 3454 | /* There is nothing more to do for sync/check/repair. |
18b9837e N |
3455 | * Don't even need to abort as that is handled elsewhere |
3456 | * if needed, and not always wanted e.g. if there is a known | |
3457 | * bad block here. | |
9a3e1101 | 3458 | * For recover/replace we need to record a bad block on all |
7f0da59b N |
3459 | * non-sync devices, or abort the recovery |
3460 | */ | |
18b9837e N |
3461 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
3462 | /* During recovery devices cannot be removed, so | |
3463 | * locking and refcounting of rdevs is not needed | |
3464 | */ | |
e50d3992 | 3465 | rcu_read_lock(); |
18b9837e | 3466 | for (i = 0; i < conf->raid_disks; i++) { |
e50d3992 | 3467 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
18b9837e N |
3468 | if (rdev |
3469 | && !test_bit(Faulty, &rdev->flags) | |
3470 | && !test_bit(In_sync, &rdev->flags) | |
3471 | && !rdev_set_badblocks(rdev, sh->sector, | |
3472 | STRIPE_SECTORS, 0)) | |
3473 | abort = 1; | |
e50d3992 | 3474 | rdev = rcu_dereference(conf->disks[i].replacement); |
18b9837e N |
3475 | if (rdev |
3476 | && !test_bit(Faulty, &rdev->flags) | |
3477 | && !test_bit(In_sync, &rdev->flags) | |
3478 | && !rdev_set_badblocks(rdev, sh->sector, | |
3479 | STRIPE_SECTORS, 0)) | |
3480 | abort = 1; | |
3481 | } | |
e50d3992 | 3482 | rcu_read_unlock(); |
18b9837e N |
3483 | if (abort) |
3484 | conf->recovery_disabled = | |
3485 | conf->mddev->recovery_disabled; | |
7f0da59b | 3486 | } |
18b9837e | 3487 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
7f0da59b N |
3488 | } |
3489 | ||
9a3e1101 N |
3490 | static int want_replace(struct stripe_head *sh, int disk_idx) |
3491 | { | |
3492 | struct md_rdev *rdev; | |
3493 | int rv = 0; | |
3f232d6a N |
3494 | |
3495 | rcu_read_lock(); | |
3496 | rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement); | |
9a3e1101 N |
3497 | if (rdev |
3498 | && !test_bit(Faulty, &rdev->flags) | |
3499 | && !test_bit(In_sync, &rdev->flags) | |
3500 | && (rdev->recovery_offset <= sh->sector | |
3501 | || rdev->mddev->recovery_cp <= sh->sector)) | |
3502 | rv = 1; | |
3f232d6a | 3503 | rcu_read_unlock(); |
9a3e1101 N |
3504 | return rv; |
3505 | } | |
3506 | ||
2c58f06e N |
3507 | static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, |
3508 | int disk_idx, int disks) | |
a4456856 | 3509 | { |
5599becc | 3510 | struct r5dev *dev = &sh->dev[disk_idx]; |
f2b3b44d N |
3511 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
3512 | &sh->dev[s->failed_num[1]] }; | |
ea664c82 | 3513 | int i; |
5599becc | 3514 | |
a79cfe12 N |
3515 | |
3516 | if (test_bit(R5_LOCKED, &dev->flags) || | |
3517 | test_bit(R5_UPTODATE, &dev->flags)) | |
3518 | /* No point reading this as we already have it or have | |
3519 | * decided to get it. | |
3520 | */ | |
3521 | return 0; | |
3522 | ||
3523 | if (dev->toread || | |
3524 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) | |
3525 | /* We need this block to directly satisfy a request */ | |
3526 | return 1; | |
3527 | ||
3528 | if (s->syncing || s->expanding || | |
3529 | (s->replacing && want_replace(sh, disk_idx))) | |
3530 | /* When syncing, or expanding we read everything. | |
3531 | * When replacing, we need the replaced block. | |
3532 | */ | |
3533 | return 1; | |
3534 | ||
3535 | if ((s->failed >= 1 && fdev[0]->toread) || | |
3536 | (s->failed >= 2 && fdev[1]->toread)) | |
3537 | /* If we want to read from a failed device, then | |
3538 | * we need to actually read every other device. | |
3539 | */ | |
3540 | return 1; | |
3541 | ||
a9d56950 N |
3542 | /* Sometimes neither read-modify-write nor reconstruct-write |
3543 | * cycles can work. In those cases we read every block we | |
3544 | * can. Then the parity-update is certain to have enough to | |
3545 | * work with. | |
3546 | * This can only be a problem when we need to write something, | |
3547 | * and some device has failed. If either of those tests | |
3548 | * fail we need look no further. | |
3549 | */ | |
3550 | if (!s->failed || !s->to_write) | |
3551 | return 0; | |
3552 | ||
3553 | if (test_bit(R5_Insync, &dev->flags) && | |
3554 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
3555 | /* Pre-reads at not permitted until after short delay | |
3556 | * to gather multiple requests. However if this | |
3560741e | 3557 | * device is no Insync, the block could only be computed |
a9d56950 N |
3558 | * and there is no need to delay that. |
3559 | */ | |
3560 | return 0; | |
ea664c82 | 3561 | |
36707bb2 | 3562 | for (i = 0; i < s->failed && i < 2; i++) { |
ea664c82 N |
3563 | if (fdev[i]->towrite && |
3564 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && | |
3565 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) | |
3566 | /* If we have a partial write to a failed | |
3567 | * device, then we will need to reconstruct | |
3568 | * the content of that device, so all other | |
3569 | * devices must be read. | |
3570 | */ | |
3571 | return 1; | |
3572 | } | |
3573 | ||
3574 | /* If we are forced to do a reconstruct-write, either because | |
3575 | * the current RAID6 implementation only supports that, or | |
3560741e | 3576 | * because parity cannot be trusted and we are currently |
ea664c82 N |
3577 | * recovering it, there is extra need to be careful. |
3578 | * If one of the devices that we would need to read, because | |
3579 | * it is not being overwritten (and maybe not written at all) | |
3580 | * is missing/faulty, then we need to read everything we can. | |
3581 | */ | |
3582 | if (sh->raid_conf->level != 6 && | |
3583 | sh->sector < sh->raid_conf->mddev->recovery_cp) | |
3584 | /* reconstruct-write isn't being forced */ | |
3585 | return 0; | |
36707bb2 | 3586 | for (i = 0; i < s->failed && i < 2; i++) { |
10d82c5f N |
3587 | if (s->failed_num[i] != sh->pd_idx && |
3588 | s->failed_num[i] != sh->qd_idx && | |
3589 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && | |
ea664c82 N |
3590 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
3591 | return 1; | |
3592 | } | |
3593 | ||
2c58f06e N |
3594 | return 0; |
3595 | } | |
3596 | ||
ba02684d SL |
3597 | /* fetch_block - checks the given member device to see if its data needs |
3598 | * to be read or computed to satisfy a request. | |
3599 | * | |
3600 | * Returns 1 when no more member devices need to be checked, otherwise returns | |
3601 | * 0 to tell the loop in handle_stripe_fill to continue | |
3602 | */ | |
2c58f06e N |
3603 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
3604 | int disk_idx, int disks) | |
3605 | { | |
3606 | struct r5dev *dev = &sh->dev[disk_idx]; | |
3607 | ||
3608 | /* is the data in this block needed, and can we get it? */ | |
3609 | if (need_this_block(sh, s, disk_idx, disks)) { | |
5599becc YT |
3610 | /* we would like to get this block, possibly by computing it, |
3611 | * otherwise read it if the backing disk is insync | |
3612 | */ | |
3613 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); | |
3614 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); | |
b0c783b3 | 3615 | BUG_ON(sh->batch_head); |
7471fb77 N |
3616 | |
3617 | /* | |
3618 | * In the raid6 case if the only non-uptodate disk is P | |
3619 | * then we already trusted P to compute the other failed | |
3620 | * drives. It is safe to compute rather than re-read P. | |
3621 | * In other cases we only compute blocks from failed | |
3622 | * devices, otherwise check/repair might fail to detect | |
3623 | * a real inconsistency. | |
3624 | */ | |
3625 | ||
5599becc | 3626 | if ((s->uptodate == disks - 1) && |
7471fb77 | 3627 | ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) || |
f2b3b44d | 3628 | (s->failed && (disk_idx == s->failed_num[0] || |
7471fb77 | 3629 | disk_idx == s->failed_num[1])))) { |
5599becc YT |
3630 | /* have disk failed, and we're requested to fetch it; |
3631 | * do compute it | |
a4456856 | 3632 | */ |
5599becc YT |
3633 | pr_debug("Computing stripe %llu block %d\n", |
3634 | (unsigned long long)sh->sector, disk_idx); | |
3635 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
3636 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
3637 | set_bit(R5_Wantcompute, &dev->flags); | |
3638 | sh->ops.target = disk_idx; | |
3639 | sh->ops.target2 = -1; /* no 2nd target */ | |
3640 | s->req_compute = 1; | |
93b3dbce N |
3641 | /* Careful: from this point on 'uptodate' is in the eye |
3642 | * of raid_run_ops which services 'compute' operations | |
3643 | * before writes. R5_Wantcompute flags a block that will | |
3644 | * be R5_UPTODATE by the time it is needed for a | |
3645 | * subsequent operation. | |
3646 | */ | |
5599becc YT |
3647 | s->uptodate++; |
3648 | return 1; | |
3649 | } else if (s->uptodate == disks-2 && s->failed >= 2) { | |
3650 | /* Computing 2-failure is *very* expensive; only | |
3651 | * do it if failed >= 2 | |
3652 | */ | |
3653 | int other; | |
3654 | for (other = disks; other--; ) { | |
3655 | if (other == disk_idx) | |
3656 | continue; | |
3657 | if (!test_bit(R5_UPTODATE, | |
3658 | &sh->dev[other].flags)) | |
3659 | break; | |
a4456856 | 3660 | } |
5599becc YT |
3661 | BUG_ON(other < 0); |
3662 | pr_debug("Computing stripe %llu blocks %d,%d\n", | |
3663 | (unsigned long long)sh->sector, | |
3664 | disk_idx, other); | |
3665 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
3666 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
3667 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); | |
3668 | set_bit(R5_Wantcompute, &sh->dev[other].flags); | |
3669 | sh->ops.target = disk_idx; | |
3670 | sh->ops.target2 = other; | |
3671 | s->uptodate += 2; | |
3672 | s->req_compute = 1; | |
3673 | return 1; | |
3674 | } else if (test_bit(R5_Insync, &dev->flags)) { | |
3675 | set_bit(R5_LOCKED, &dev->flags); | |
3676 | set_bit(R5_Wantread, &dev->flags); | |
3677 | s->locked++; | |
3678 | pr_debug("Reading block %d (sync=%d)\n", | |
3679 | disk_idx, s->syncing); | |
a4456856 DW |
3680 | } |
3681 | } | |
5599becc YT |
3682 | |
3683 | return 0; | |
3684 | } | |
3685 | ||
3686 | /** | |
93b3dbce | 3687 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
5599becc | 3688 | */ |
93b3dbce N |
3689 | static void handle_stripe_fill(struct stripe_head *sh, |
3690 | struct stripe_head_state *s, | |
3691 | int disks) | |
5599becc YT |
3692 | { |
3693 | int i; | |
3694 | ||
3695 | /* look for blocks to read/compute, skip this if a compute | |
3696 | * is already in flight, or if the stripe contents are in the | |
3697 | * midst of changing due to a write | |
3698 | */ | |
3699 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && | |
07e83364 SL |
3700 | !sh->reconstruct_state) { |
3701 | ||
3702 | /* | |
3703 | * For degraded stripe with data in journal, do not handle | |
3704 | * read requests yet, instead, flush the stripe to raid | |
3705 | * disks first, this avoids handling complex rmw of write | |
3706 | * back cache (prexor with orig_page, and then xor with | |
3707 | * page) in the read path | |
3708 | */ | |
3709 | if (s->injournal && s->failed) { | |
3710 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) | |
3711 | r5c_make_stripe_write_out(sh); | |
3712 | goto out; | |
3713 | } | |
3714 | ||
5599becc | 3715 | for (i = disks; i--; ) |
93b3dbce | 3716 | if (fetch_block(sh, s, i, disks)) |
5599becc | 3717 | break; |
07e83364 SL |
3718 | } |
3719 | out: | |
a4456856 DW |
3720 | set_bit(STRIPE_HANDLE, &sh->state); |
3721 | } | |
3722 | ||
787b76fa N |
3723 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
3724 | unsigned long handle_flags); | |
1fe797e6 | 3725 | /* handle_stripe_clean_event |
a4456856 DW |
3726 | * any written block on an uptodate or failed drive can be returned. |
3727 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but | |
3728 | * never LOCKED, so we don't need to test 'failed' directly. | |
3729 | */ | |
d1688a6d | 3730 | static void handle_stripe_clean_event(struct r5conf *conf, |
bd83d0a2 | 3731 | struct stripe_head *sh, int disks) |
a4456856 DW |
3732 | { |
3733 | int i; | |
3734 | struct r5dev *dev; | |
f8dfcffd | 3735 | int discard_pending = 0; |
59fc630b | 3736 | struct stripe_head *head_sh = sh; |
3737 | bool do_endio = false; | |
a4456856 DW |
3738 | |
3739 | for (i = disks; i--; ) | |
3740 | if (sh->dev[i].written) { | |
3741 | dev = &sh->dev[i]; | |
3742 | if (!test_bit(R5_LOCKED, &dev->flags) && | |
9e444768 | 3743 | (test_bit(R5_UPTODATE, &dev->flags) || |
d592a996 SL |
3744 | test_bit(R5_Discard, &dev->flags) || |
3745 | test_bit(R5_SkipCopy, &dev->flags))) { | |
a4456856 DW |
3746 | /* We can return any write requests */ |
3747 | struct bio *wbi, *wbi2; | |
45b4233c | 3748 | pr_debug("Return write for disc %d\n", i); |
ca64cae9 N |
3749 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
3750 | clear_bit(R5_UPTODATE, &dev->flags); | |
d592a996 SL |
3751 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
3752 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); | |
d592a996 | 3753 | } |
59fc630b | 3754 | do_endio = true; |
3755 | ||
3756 | returnbi: | |
3757 | dev->page = dev->orig_page; | |
a4456856 DW |
3758 | wbi = dev->written; |
3759 | dev->written = NULL; | |
4f024f37 | 3760 | while (wbi && wbi->bi_iter.bi_sector < |
a4456856 DW |
3761 | dev->sector + STRIPE_SECTORS) { |
3762 | wbi2 = r5_next_bio(wbi, dev->sector); | |
49728050 | 3763 | md_write_end(conf->mddev); |
016c76ac | 3764 | bio_endio(wbi); |
a4456856 DW |
3765 | wbi = wbi2; |
3766 | } | |
7eaf7e8e SL |
3767 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
3768 | STRIPE_SECTORS, | |
a4456856 | 3769 | !test_bit(STRIPE_DEGRADED, &sh->state), |
7eaf7e8e | 3770 | 0); |
59fc630b | 3771 | if (head_sh->batch_head) { |
3772 | sh = list_first_entry(&sh->batch_list, | |
3773 | struct stripe_head, | |
3774 | batch_list); | |
3775 | if (sh != head_sh) { | |
3776 | dev = &sh->dev[i]; | |
3777 | goto returnbi; | |
3778 | } | |
3779 | } | |
3780 | sh = head_sh; | |
3781 | dev = &sh->dev[i]; | |
f8dfcffd N |
3782 | } else if (test_bit(R5_Discard, &dev->flags)) |
3783 | discard_pending = 1; | |
3784 | } | |
f6bed0ef | 3785 | |
ff875738 | 3786 | log_stripe_write_finished(sh); |
0576b1c6 | 3787 | |
f8dfcffd N |
3788 | if (!discard_pending && |
3789 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { | |
b8a9d66d | 3790 | int hash; |
f8dfcffd N |
3791 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
3792 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); | |
3793 | if (sh->qd_idx >= 0) { | |
3794 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); | |
3795 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); | |
3796 | } | |
3797 | /* now that discard is done we can proceed with any sync */ | |
3798 | clear_bit(STRIPE_DISCARD, &sh->state); | |
d47648fc SL |
3799 | /* |
3800 | * SCSI discard will change some bio fields and the stripe has | |
3801 | * no updated data, so remove it from hash list and the stripe | |
3802 | * will be reinitialized | |
3803 | */ | |
59fc630b | 3804 | unhash: |
b8a9d66d RG |
3805 | hash = sh->hash_lock_index; |
3806 | spin_lock_irq(conf->hash_locks + hash); | |
d47648fc | 3807 | remove_hash(sh); |
b8a9d66d | 3808 | spin_unlock_irq(conf->hash_locks + hash); |
59fc630b | 3809 | if (head_sh->batch_head) { |
3810 | sh = list_first_entry(&sh->batch_list, | |
3811 | struct stripe_head, batch_list); | |
3812 | if (sh != head_sh) | |
3813 | goto unhash; | |
3814 | } | |
59fc630b | 3815 | sh = head_sh; |
3816 | ||
f8dfcffd N |
3817 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
3818 | set_bit(STRIPE_HANDLE, &sh->state); | |
3819 | ||
3820 | } | |
8b3e6cdc DW |
3821 | |
3822 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) | |
3823 | if (atomic_dec_and_test(&conf->pending_full_writes)) | |
3824 | md_wakeup_thread(conf->mddev->thread); | |
59fc630b | 3825 | |
787b76fa N |
3826 | if (head_sh->batch_head && do_endio) |
3827 | break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); | |
a4456856 DW |
3828 | } |
3829 | ||
86aa1397 SL |
3830 | /* |
3831 | * For RMW in write back cache, we need extra page in prexor to store the | |
3832 | * old data. This page is stored in dev->orig_page. | |
3833 | * | |
3834 | * This function checks whether we have data for prexor. The exact logic | |
3835 | * is: | |
3836 | * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE) | |
3837 | */ | |
3838 | static inline bool uptodate_for_rmw(struct r5dev *dev) | |
3839 | { | |
3840 | return (test_bit(R5_UPTODATE, &dev->flags)) && | |
3841 | (!test_bit(R5_InJournal, &dev->flags) || | |
3842 | test_bit(R5_OrigPageUPTDODATE, &dev->flags)); | |
3843 | } | |
3844 | ||
d7bd398e SL |
3845 | static int handle_stripe_dirtying(struct r5conf *conf, |
3846 | struct stripe_head *sh, | |
3847 | struct stripe_head_state *s, | |
3848 | int disks) | |
a4456856 DW |
3849 | { |
3850 | int rmw = 0, rcw = 0, i; | |
a7854487 AL |
3851 | sector_t recovery_cp = conf->mddev->recovery_cp; |
3852 | ||
584acdd4 | 3853 | /* Check whether resync is now happening or should start. |
a7854487 AL |
3854 | * If yes, then the array is dirty (after unclean shutdown or |
3855 | * initial creation), so parity in some stripes might be inconsistent. | |
3856 | * In this case, we need to always do reconstruct-write, to ensure | |
3857 | * that in case of drive failure or read-error correction, we | |
3858 | * generate correct data from the parity. | |
3859 | */ | |
584acdd4 | 3860 | if (conf->rmw_level == PARITY_DISABLE_RMW || |
26ac1073 N |
3861 | (recovery_cp < MaxSector && sh->sector >= recovery_cp && |
3862 | s->failed == 0)) { | |
a7854487 | 3863 | /* Calculate the real rcw later - for now make it |
c8ac1803 N |
3864 | * look like rcw is cheaper |
3865 | */ | |
3866 | rcw = 1; rmw = 2; | |
584acdd4 MS |
3867 | pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", |
3868 | conf->rmw_level, (unsigned long long)recovery_cp, | |
a7854487 | 3869 | (unsigned long long)sh->sector); |
c8ac1803 | 3870 | } else for (i = disks; i--; ) { |
a4456856 DW |
3871 | /* would I have to read this buffer for read_modify_write */ |
3872 | struct r5dev *dev = &sh->dev[i]; | |
39b99586 | 3873 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
07e83364 | 3874 | i == sh->pd_idx || i == sh->qd_idx || |
1e6d690b | 3875 | test_bit(R5_InJournal, &dev->flags)) && |
a4456856 | 3876 | !test_bit(R5_LOCKED, &dev->flags) && |
86aa1397 | 3877 | !(uptodate_for_rmw(dev) || |
f38e1219 | 3878 | test_bit(R5_Wantcompute, &dev->flags))) { |
a4456856 DW |
3879 | if (test_bit(R5_Insync, &dev->flags)) |
3880 | rmw++; | |
3881 | else | |
3882 | rmw += 2*disks; /* cannot read it */ | |
3883 | } | |
3884 | /* Would I have to read this buffer for reconstruct_write */ | |
584acdd4 MS |
3885 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
3886 | i != sh->pd_idx && i != sh->qd_idx && | |
a4456856 | 3887 | !test_bit(R5_LOCKED, &dev->flags) && |
f38e1219 | 3888 | !(test_bit(R5_UPTODATE, &dev->flags) || |
1e6d690b | 3889 | test_bit(R5_Wantcompute, &dev->flags))) { |
67f45548 N |
3890 | if (test_bit(R5_Insync, &dev->flags)) |
3891 | rcw++; | |
a4456856 DW |
3892 | else |
3893 | rcw += 2*disks; | |
3894 | } | |
3895 | } | |
1e6d690b | 3896 | |
39b99586 SL |
3897 | pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n", |
3898 | (unsigned long long)sh->sector, sh->state, rmw, rcw); | |
a4456856 | 3899 | set_bit(STRIPE_HANDLE, &sh->state); |
41257580 | 3900 | if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) { |
a4456856 | 3901 | /* prefer read-modify-write, but need to get some data */ |
e3620a3a JB |
3902 | if (conf->mddev->queue) |
3903 | blk_add_trace_msg(conf->mddev->queue, | |
3904 | "raid5 rmw %llu %d", | |
3905 | (unsigned long long)sh->sector, rmw); | |
a4456856 DW |
3906 | for (i = disks; i--; ) { |
3907 | struct r5dev *dev = &sh->dev[i]; | |
1e6d690b SL |
3908 | if (test_bit(R5_InJournal, &dev->flags) && |
3909 | dev->page == dev->orig_page && | |
3910 | !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { | |
3911 | /* alloc page for prexor */ | |
d7bd398e SL |
3912 | struct page *p = alloc_page(GFP_NOIO); |
3913 | ||
3914 | if (p) { | |
3915 | dev->orig_page = p; | |
3916 | continue; | |
3917 | } | |
3918 | ||
3919 | /* | |
3920 | * alloc_page() failed, try use | |
3921 | * disk_info->extra_page | |
3922 | */ | |
3923 | if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE, | |
3924 | &conf->cache_state)) { | |
3925 | r5c_use_extra_page(sh); | |
3926 | break; | |
3927 | } | |
1e6d690b | 3928 | |
d7bd398e SL |
3929 | /* extra_page in use, add to delayed_list */ |
3930 | set_bit(STRIPE_DELAYED, &sh->state); | |
3931 | s->waiting_extra_page = 1; | |
3932 | return -EAGAIN; | |
1e6d690b | 3933 | } |
d7bd398e | 3934 | } |
1e6d690b | 3935 | |
d7bd398e SL |
3936 | for (i = disks; i--; ) { |
3937 | struct r5dev *dev = &sh->dev[i]; | |
39b99586 | 3938 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
1e6d690b SL |
3939 | i == sh->pd_idx || i == sh->qd_idx || |
3940 | test_bit(R5_InJournal, &dev->flags)) && | |
a4456856 | 3941 | !test_bit(R5_LOCKED, &dev->flags) && |
86aa1397 | 3942 | !(uptodate_for_rmw(dev) || |
1e6d690b | 3943 | test_bit(R5_Wantcompute, &dev->flags)) && |
a4456856 | 3944 | test_bit(R5_Insync, &dev->flags)) { |
67f45548 N |
3945 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
3946 | &sh->state)) { | |
3947 | pr_debug("Read_old block %d for r-m-w\n", | |
3948 | i); | |
a4456856 DW |
3949 | set_bit(R5_LOCKED, &dev->flags); |
3950 | set_bit(R5_Wantread, &dev->flags); | |
3951 | s->locked++; | |
3952 | } else { | |
3953 | set_bit(STRIPE_DELAYED, &sh->state); | |
3954 | set_bit(STRIPE_HANDLE, &sh->state); | |
3955 | } | |
3956 | } | |
3957 | } | |
a9add5d9 | 3958 | } |
41257580 | 3959 | if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) { |
a4456856 | 3960 | /* want reconstruct write, but need to get some data */ |
a9add5d9 | 3961 | int qread =0; |
c8ac1803 | 3962 | rcw = 0; |
a4456856 DW |
3963 | for (i = disks; i--; ) { |
3964 | struct r5dev *dev = &sh->dev[i]; | |
3965 | if (!test_bit(R5_OVERWRITE, &dev->flags) && | |
c8ac1803 | 3966 | i != sh->pd_idx && i != sh->qd_idx && |
a4456856 | 3967 | !test_bit(R5_LOCKED, &dev->flags) && |
f38e1219 | 3968 | !(test_bit(R5_UPTODATE, &dev->flags) || |
c8ac1803 N |
3969 | test_bit(R5_Wantcompute, &dev->flags))) { |
3970 | rcw++; | |
67f45548 N |
3971 | if (test_bit(R5_Insync, &dev->flags) && |
3972 | test_bit(STRIPE_PREREAD_ACTIVE, | |
3973 | &sh->state)) { | |
45b4233c | 3974 | pr_debug("Read_old block " |
a4456856 DW |
3975 | "%d for Reconstruct\n", i); |
3976 | set_bit(R5_LOCKED, &dev->flags); | |
3977 | set_bit(R5_Wantread, &dev->flags); | |
3978 | s->locked++; | |
a9add5d9 | 3979 | qread++; |
a4456856 DW |
3980 | } else { |
3981 | set_bit(STRIPE_DELAYED, &sh->state); | |
3982 | set_bit(STRIPE_HANDLE, &sh->state); | |
3983 | } | |
3984 | } | |
3985 | } | |
e3620a3a | 3986 | if (rcw && conf->mddev->queue) |
a9add5d9 N |
3987 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
3988 | (unsigned long long)sh->sector, | |
3989 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); | |
c8ac1803 | 3990 | } |
b1b02fe9 N |
3991 | |
3992 | if (rcw > disks && rmw > disks && | |
3993 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
3994 | set_bit(STRIPE_DELAYED, &sh->state); | |
3995 | ||
a4456856 DW |
3996 | /* now if nothing is locked, and if we have enough data, |
3997 | * we can start a write request | |
3998 | */ | |
f38e1219 DW |
3999 | /* since handle_stripe can be called at any time we need to handle the |
4000 | * case where a compute block operation has been submitted and then a | |
ac6b53b6 DW |
4001 | * subsequent call wants to start a write request. raid_run_ops only |
4002 | * handles the case where compute block and reconstruct are requested | |
f38e1219 DW |
4003 | * simultaneously. If this is not the case then new writes need to be |
4004 | * held off until the compute completes. | |
4005 | */ | |
976ea8d4 DW |
4006 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
4007 | (s->locked == 0 && (rcw == 0 || rmw == 0) && | |
1e6d690b | 4008 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
c0f7bddb | 4009 | schedule_reconstruction(sh, s, rcw == 0, 0); |
d7bd398e | 4010 | return 0; |
a4456856 DW |
4011 | } |
4012 | ||
d1688a6d | 4013 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
a4456856 DW |
4014 | struct stripe_head_state *s, int disks) |
4015 | { | |
ecc65c9b | 4016 | struct r5dev *dev = NULL; |
bd2ab670 | 4017 | |
59fc630b | 4018 | BUG_ON(sh->batch_head); |
a4456856 | 4019 | set_bit(STRIPE_HANDLE, &sh->state); |
e89f8962 | 4020 | |
ecc65c9b DW |
4021 | switch (sh->check_state) { |
4022 | case check_state_idle: | |
4023 | /* start a new check operation if there are no failures */ | |
bd2ab670 | 4024 | if (s->failed == 0) { |
bd2ab670 | 4025 | BUG_ON(s->uptodate != disks); |
ecc65c9b DW |
4026 | sh->check_state = check_state_run; |
4027 | set_bit(STRIPE_OP_CHECK, &s->ops_request); | |
bd2ab670 | 4028 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
bd2ab670 | 4029 | s->uptodate--; |
ecc65c9b | 4030 | break; |
bd2ab670 | 4031 | } |
f2b3b44d | 4032 | dev = &sh->dev[s->failed_num[0]]; |
ecc65c9b DW |
4033 | /* fall through */ |
4034 | case check_state_compute_result: | |
4035 | sh->check_state = check_state_idle; | |
4036 | if (!dev) | |
4037 | dev = &sh->dev[sh->pd_idx]; | |
4038 | ||
4039 | /* check that a write has not made the stripe insync */ | |
4040 | if (test_bit(STRIPE_INSYNC, &sh->state)) | |
4041 | break; | |
c8894419 | 4042 | |
a4456856 | 4043 | /* either failed parity check, or recovery is happening */ |
a4456856 DW |
4044 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
4045 | BUG_ON(s->uptodate != disks); | |
4046 | ||
4047 | set_bit(R5_LOCKED, &dev->flags); | |
ecc65c9b | 4048 | s->locked++; |
a4456856 | 4049 | set_bit(R5_Wantwrite, &dev->flags); |
830ea016 | 4050 | |
a4456856 | 4051 | clear_bit(STRIPE_DEGRADED, &sh->state); |
a4456856 | 4052 | set_bit(STRIPE_INSYNC, &sh->state); |
ecc65c9b DW |
4053 | break; |
4054 | case check_state_run: | |
4055 | break; /* we will be called again upon completion */ | |
4056 | case check_state_check_result: | |
4057 | sh->check_state = check_state_idle; | |
4058 | ||
4059 | /* if a failure occurred during the check operation, leave | |
4060 | * STRIPE_INSYNC not set and let the stripe be handled again | |
4061 | */ | |
4062 | if (s->failed) | |
4063 | break; | |
4064 | ||
4065 | /* handle a successful check operation, if parity is correct | |
4066 | * we are done. Otherwise update the mismatch count and repair | |
4067 | * parity if !MD_RECOVERY_CHECK | |
4068 | */ | |
ad283ea4 | 4069 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
ecc65c9b DW |
4070 | /* parity is correct (on disc, |
4071 | * not in buffer any more) | |
4072 | */ | |
4073 | set_bit(STRIPE_INSYNC, &sh->state); | |
4074 | else { | |
7f7583d4 | 4075 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
ecc65c9b DW |
4076 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
4077 | /* don't try to repair!! */ | |
4078 | set_bit(STRIPE_INSYNC, &sh->state); | |
4079 | else { | |
4080 | sh->check_state = check_state_compute_run; | |
976ea8d4 | 4081 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
ecc65c9b DW |
4082 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
4083 | set_bit(R5_Wantcompute, | |
4084 | &sh->dev[sh->pd_idx].flags); | |
4085 | sh->ops.target = sh->pd_idx; | |
ac6b53b6 | 4086 | sh->ops.target2 = -1; |
ecc65c9b DW |
4087 | s->uptodate++; |
4088 | } | |
4089 | } | |
4090 | break; | |
4091 | case check_state_compute_run: | |
4092 | break; | |
4093 | default: | |
cc6167b4 | 4094 | pr_err("%s: unknown check_state: %d sector: %llu\n", |
ecc65c9b DW |
4095 | __func__, sh->check_state, |
4096 | (unsigned long long) sh->sector); | |
4097 | BUG(); | |
a4456856 DW |
4098 | } |
4099 | } | |
4100 | ||
d1688a6d | 4101 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
36d1c647 | 4102 | struct stripe_head_state *s, |
f2b3b44d | 4103 | int disks) |
a4456856 | 4104 | { |
a4456856 | 4105 | int pd_idx = sh->pd_idx; |
34e04e87 | 4106 | int qd_idx = sh->qd_idx; |
d82dfee0 | 4107 | struct r5dev *dev; |
a4456856 | 4108 | |
59fc630b | 4109 | BUG_ON(sh->batch_head); |
a4456856 DW |
4110 | set_bit(STRIPE_HANDLE, &sh->state); |
4111 | ||
4112 | BUG_ON(s->failed > 2); | |
d82dfee0 | 4113 | |
a4456856 DW |
4114 | /* Want to check and possibly repair P and Q. |
4115 | * However there could be one 'failed' device, in which | |
4116 | * case we can only check one of them, possibly using the | |
4117 | * other to generate missing data | |
4118 | */ | |
4119 | ||
d82dfee0 DW |
4120 | switch (sh->check_state) { |
4121 | case check_state_idle: | |
4122 | /* start a new check operation if there are < 2 failures */ | |
f2b3b44d | 4123 | if (s->failed == s->q_failed) { |
d82dfee0 | 4124 | /* The only possible failed device holds Q, so it |
a4456856 DW |
4125 | * makes sense to check P (If anything else were failed, |
4126 | * we would have used P to recreate it). | |
4127 | */ | |
d82dfee0 | 4128 | sh->check_state = check_state_run; |
a4456856 | 4129 | } |
f2b3b44d | 4130 | if (!s->q_failed && s->failed < 2) { |
d82dfee0 | 4131 | /* Q is not failed, and we didn't use it to generate |
a4456856 DW |
4132 | * anything, so it makes sense to check it |
4133 | */ | |
d82dfee0 DW |
4134 | if (sh->check_state == check_state_run) |
4135 | sh->check_state = check_state_run_pq; | |
4136 | else | |
4137 | sh->check_state = check_state_run_q; | |
a4456856 | 4138 | } |
a4456856 | 4139 | |
d82dfee0 DW |
4140 | /* discard potentially stale zero_sum_result */ |
4141 | sh->ops.zero_sum_result = 0; | |
a4456856 | 4142 | |
d82dfee0 DW |
4143 | if (sh->check_state == check_state_run) { |
4144 | /* async_xor_zero_sum destroys the contents of P */ | |
4145 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
4146 | s->uptodate--; | |
a4456856 | 4147 | } |
d82dfee0 DW |
4148 | if (sh->check_state >= check_state_run && |
4149 | sh->check_state <= check_state_run_pq) { | |
4150 | /* async_syndrome_zero_sum preserves P and Q, so | |
4151 | * no need to mark them !uptodate here | |
4152 | */ | |
4153 | set_bit(STRIPE_OP_CHECK, &s->ops_request); | |
4154 | break; | |
a4456856 DW |
4155 | } |
4156 | ||
d82dfee0 DW |
4157 | /* we have 2-disk failure */ |
4158 | BUG_ON(s->failed != 2); | |
4159 | /* fall through */ | |
4160 | case check_state_compute_result: | |
4161 | sh->check_state = check_state_idle; | |
a4456856 | 4162 | |
d82dfee0 DW |
4163 | /* check that a write has not made the stripe insync */ |
4164 | if (test_bit(STRIPE_INSYNC, &sh->state)) | |
4165 | break; | |
a4456856 DW |
4166 | |
4167 | /* now write out any block on a failed drive, | |
d82dfee0 | 4168 | * or P or Q if they were recomputed |
a4456856 | 4169 | */ |
d82dfee0 | 4170 | BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */ |
a4456856 | 4171 | if (s->failed == 2) { |
f2b3b44d | 4172 | dev = &sh->dev[s->failed_num[1]]; |
a4456856 DW |
4173 | s->locked++; |
4174 | set_bit(R5_LOCKED, &dev->flags); | |
4175 | set_bit(R5_Wantwrite, &dev->flags); | |
4176 | } | |
4177 | if (s->failed >= 1) { | |
f2b3b44d | 4178 | dev = &sh->dev[s->failed_num[0]]; |
a4456856 DW |
4179 | s->locked++; |
4180 | set_bit(R5_LOCKED, &dev->flags); | |
4181 | set_bit(R5_Wantwrite, &dev->flags); | |
4182 | } | |
d82dfee0 | 4183 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
a4456856 DW |
4184 | dev = &sh->dev[pd_idx]; |
4185 | s->locked++; | |
4186 | set_bit(R5_LOCKED, &dev->flags); | |
4187 | set_bit(R5_Wantwrite, &dev->flags); | |
4188 | } | |
d82dfee0 | 4189 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
a4456856 DW |
4190 | dev = &sh->dev[qd_idx]; |
4191 | s->locked++; | |
4192 | set_bit(R5_LOCKED, &dev->flags); | |
4193 | set_bit(R5_Wantwrite, &dev->flags); | |
4194 | } | |
4195 | clear_bit(STRIPE_DEGRADED, &sh->state); | |
4196 | ||
4197 | set_bit(STRIPE_INSYNC, &sh->state); | |
d82dfee0 DW |
4198 | break; |
4199 | case check_state_run: | |
4200 | case check_state_run_q: | |
4201 | case check_state_run_pq: | |
4202 | break; /* we will be called again upon completion */ | |
4203 | case check_state_check_result: | |
4204 | sh->check_state = check_state_idle; | |
4205 | ||
4206 | /* handle a successful check operation, if parity is correct | |
4207 | * we are done. Otherwise update the mismatch count and repair | |
4208 | * parity if !MD_RECOVERY_CHECK | |
4209 | */ | |
4210 | if (sh->ops.zero_sum_result == 0) { | |
4211 | /* both parities are correct */ | |
4212 | if (!s->failed) | |
4213 | set_bit(STRIPE_INSYNC, &sh->state); | |
4214 | else { | |
4215 | /* in contrast to the raid5 case we can validate | |
4216 | * parity, but still have a failure to write | |
4217 | * back | |
4218 | */ | |
4219 | sh->check_state = check_state_compute_result; | |
4220 | /* Returning at this point means that we may go | |
4221 | * off and bring p and/or q uptodate again so | |
4222 | * we make sure to check zero_sum_result again | |
4223 | * to verify if p or q need writeback | |
4224 | */ | |
4225 | } | |
4226 | } else { | |
7f7583d4 | 4227 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
d82dfee0 DW |
4228 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
4229 | /* don't try to repair!! */ | |
4230 | set_bit(STRIPE_INSYNC, &sh->state); | |
4231 | else { | |
4232 | int *target = &sh->ops.target; | |
4233 | ||
4234 | sh->ops.target = -1; | |
4235 | sh->ops.target2 = -1; | |
4236 | sh->check_state = check_state_compute_run; | |
4237 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); | |
4238 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); | |
4239 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { | |
4240 | set_bit(R5_Wantcompute, | |
4241 | &sh->dev[pd_idx].flags); | |
4242 | *target = pd_idx; | |
4243 | target = &sh->ops.target2; | |
4244 | s->uptodate++; | |
4245 | } | |
4246 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { | |
4247 | set_bit(R5_Wantcompute, | |
4248 | &sh->dev[qd_idx].flags); | |
4249 | *target = qd_idx; | |
4250 | s->uptodate++; | |
4251 | } | |
4252 | } | |
4253 | } | |
4254 | break; | |
4255 | case check_state_compute_run: | |
4256 | break; | |
4257 | default: | |
cc6167b4 N |
4258 | pr_warn("%s: unknown check_state: %d sector: %llu\n", |
4259 | __func__, sh->check_state, | |
4260 | (unsigned long long) sh->sector); | |
d82dfee0 | 4261 | BUG(); |
a4456856 DW |
4262 | } |
4263 | } | |
4264 | ||
d1688a6d | 4265 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
a4456856 DW |
4266 | { |
4267 | int i; | |
4268 | ||
4269 | /* We have read all the blocks in this stripe and now we need to | |
4270 | * copy some of them into a target stripe for expand. | |
4271 | */ | |
f0a50d37 | 4272 | struct dma_async_tx_descriptor *tx = NULL; |
59fc630b | 4273 | BUG_ON(sh->batch_head); |
a4456856 DW |
4274 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
4275 | for (i = 0; i < sh->disks; i++) | |
34e04e87 | 4276 | if (i != sh->pd_idx && i != sh->qd_idx) { |
911d4ee8 | 4277 | int dd_idx, j; |
a4456856 | 4278 | struct stripe_head *sh2; |
a08abd8c | 4279 | struct async_submit_ctl submit; |
a4456856 | 4280 | |
6d036f7d | 4281 | sector_t bn = raid5_compute_blocknr(sh, i, 1); |
911d4ee8 N |
4282 | sector_t s = raid5_compute_sector(conf, bn, 0, |
4283 | &dd_idx, NULL); | |
6d036f7d | 4284 | sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1); |
a4456856 DW |
4285 | if (sh2 == NULL) |
4286 | /* so far only the early blocks of this stripe | |
4287 | * have been requested. When later blocks | |
4288 | * get requested, we will try again | |
4289 | */ | |
4290 | continue; | |
4291 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || | |
4292 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { | |
4293 | /* must have already done this block */ | |
6d036f7d | 4294 | raid5_release_stripe(sh2); |
a4456856 DW |
4295 | continue; |
4296 | } | |
f0a50d37 DW |
4297 | |
4298 | /* place all the copies on one channel */ | |
a08abd8c | 4299 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
f0a50d37 | 4300 | tx = async_memcpy(sh2->dev[dd_idx].page, |
88ba2aa5 | 4301 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
a08abd8c | 4302 | &submit); |
f0a50d37 | 4303 | |
a4456856 DW |
4304 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
4305 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); | |
4306 | for (j = 0; j < conf->raid_disks; j++) | |
4307 | if (j != sh2->pd_idx && | |
86c374ba | 4308 | j != sh2->qd_idx && |
a4456856 DW |
4309 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
4310 | break; | |
4311 | if (j == conf->raid_disks) { | |
4312 | set_bit(STRIPE_EXPAND_READY, &sh2->state); | |
4313 | set_bit(STRIPE_HANDLE, &sh2->state); | |
4314 | } | |
6d036f7d | 4315 | raid5_release_stripe(sh2); |
f0a50d37 | 4316 | |
a4456856 | 4317 | } |
a2e08551 | 4318 | /* done submitting copies, wait for them to complete */ |
749586b7 | 4319 | async_tx_quiesce(&tx); |
a4456856 | 4320 | } |
1da177e4 LT |
4321 | |
4322 | /* | |
4323 | * handle_stripe - do things to a stripe. | |
4324 | * | |
9a3e1101 N |
4325 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
4326 | * state of various bits to see what needs to be done. | |
1da177e4 | 4327 | * Possible results: |
9a3e1101 N |
4328 | * return some read requests which now have data |
4329 | * return some write requests which are safely on storage | |
1da177e4 LT |
4330 | * schedule a read on some buffers |
4331 | * schedule a write of some buffers | |
4332 | * return confirmation of parity correctness | |
4333 | * | |
1da177e4 | 4334 | */ |
a4456856 | 4335 | |
acfe726b | 4336 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
1da177e4 | 4337 | { |
d1688a6d | 4338 | struct r5conf *conf = sh->raid_conf; |
f416885e | 4339 | int disks = sh->disks; |
474af965 N |
4340 | struct r5dev *dev; |
4341 | int i; | |
9a3e1101 | 4342 | int do_recovery = 0; |
1da177e4 | 4343 | |
acfe726b N |
4344 | memset(s, 0, sizeof(*s)); |
4345 | ||
dabc4ec6 | 4346 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; |
4347 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; | |
acfe726b N |
4348 | s->failed_num[0] = -1; |
4349 | s->failed_num[1] = -1; | |
6e74a9cf | 4350 | s->log_failed = r5l_log_disk_error(conf); |
1da177e4 | 4351 | |
acfe726b | 4352 | /* Now to look around and see what can be done */ |
1da177e4 | 4353 | rcu_read_lock(); |
16a53ecc | 4354 | for (i=disks; i--; ) { |
3cb03002 | 4355 | struct md_rdev *rdev; |
31c176ec N |
4356 | sector_t first_bad; |
4357 | int bad_sectors; | |
4358 | int is_bad = 0; | |
acfe726b | 4359 | |
16a53ecc | 4360 | dev = &sh->dev[i]; |
1da177e4 | 4361 | |
45b4233c | 4362 | pr_debug("check %d: state 0x%lx read %p write %p written %p\n", |
9a3e1101 N |
4363 | i, dev->flags, |
4364 | dev->toread, dev->towrite, dev->written); | |
6c0069c0 YT |
4365 | /* maybe we can reply to a read |
4366 | * | |
4367 | * new wantfill requests are only permitted while | |
4368 | * ops_complete_biofill is guaranteed to be inactive | |
4369 | */ | |
4370 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && | |
4371 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) | |
4372 | set_bit(R5_Wantfill, &dev->flags); | |
1da177e4 | 4373 | |
16a53ecc | 4374 | /* now count some things */ |
cc94015a N |
4375 | if (test_bit(R5_LOCKED, &dev->flags)) |
4376 | s->locked++; | |
4377 | if (test_bit(R5_UPTODATE, &dev->flags)) | |
4378 | s->uptodate++; | |
2d6e4ecc | 4379 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
cc94015a N |
4380 | s->compute++; |
4381 | BUG_ON(s->compute > 2); | |
2d6e4ecc | 4382 | } |
1da177e4 | 4383 | |
acfe726b | 4384 | if (test_bit(R5_Wantfill, &dev->flags)) |
cc94015a | 4385 | s->to_fill++; |
acfe726b | 4386 | else if (dev->toread) |
cc94015a | 4387 | s->to_read++; |
16a53ecc | 4388 | if (dev->towrite) { |
cc94015a | 4389 | s->to_write++; |
16a53ecc | 4390 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
cc94015a | 4391 | s->non_overwrite++; |
16a53ecc | 4392 | } |
a4456856 | 4393 | if (dev->written) |
cc94015a | 4394 | s->written++; |
14a75d3e N |
4395 | /* Prefer to use the replacement for reads, but only |
4396 | * if it is recovered enough and has no bad blocks. | |
4397 | */ | |
4398 | rdev = rcu_dereference(conf->disks[i].replacement); | |
4399 | if (rdev && !test_bit(Faulty, &rdev->flags) && | |
4400 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && | |
4401 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, | |
4402 | &first_bad, &bad_sectors)) | |
4403 | set_bit(R5_ReadRepl, &dev->flags); | |
4404 | else { | |
e6030cb0 | 4405 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
9a3e1101 | 4406 | set_bit(R5_NeedReplace, &dev->flags); |
e6030cb0 N |
4407 | else |
4408 | clear_bit(R5_NeedReplace, &dev->flags); | |
14a75d3e N |
4409 | rdev = rcu_dereference(conf->disks[i].rdev); |
4410 | clear_bit(R5_ReadRepl, &dev->flags); | |
4411 | } | |
9283d8c5 N |
4412 | if (rdev && test_bit(Faulty, &rdev->flags)) |
4413 | rdev = NULL; | |
31c176ec N |
4414 | if (rdev) { |
4415 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, | |
4416 | &first_bad, &bad_sectors); | |
4417 | if (s->blocked_rdev == NULL | |
4418 | && (test_bit(Blocked, &rdev->flags) | |
4419 | || is_bad < 0)) { | |
4420 | if (is_bad < 0) | |
4421 | set_bit(BlockedBadBlocks, | |
4422 | &rdev->flags); | |
4423 | s->blocked_rdev = rdev; | |
4424 | atomic_inc(&rdev->nr_pending); | |
4425 | } | |
6bfe0b49 | 4426 | } |
415e72d0 N |
4427 | clear_bit(R5_Insync, &dev->flags); |
4428 | if (!rdev) | |
4429 | /* Not in-sync */; | |
31c176ec N |
4430 | else if (is_bad) { |
4431 | /* also not in-sync */ | |
18b9837e N |
4432 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
4433 | test_bit(R5_UPTODATE, &dev->flags)) { | |
31c176ec N |
4434 | /* treat as in-sync, but with a read error |
4435 | * which we can now try to correct | |
4436 | */ | |
4437 | set_bit(R5_Insync, &dev->flags); | |
4438 | set_bit(R5_ReadError, &dev->flags); | |
4439 | } | |
4440 | } else if (test_bit(In_sync, &rdev->flags)) | |
415e72d0 | 4441 | set_bit(R5_Insync, &dev->flags); |
30d7a483 | 4442 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
415e72d0 | 4443 | /* in sync if before recovery_offset */ |
30d7a483 N |
4444 | set_bit(R5_Insync, &dev->flags); |
4445 | else if (test_bit(R5_UPTODATE, &dev->flags) && | |
4446 | test_bit(R5_Expanded, &dev->flags)) | |
4447 | /* If we've reshaped into here, we assume it is Insync. | |
4448 | * We will shortly update recovery_offset to make | |
4449 | * it official. | |
4450 | */ | |
4451 | set_bit(R5_Insync, &dev->flags); | |
4452 | ||
1cc03eb9 | 4453 | if (test_bit(R5_WriteError, &dev->flags)) { |
14a75d3e N |
4454 | /* This flag does not apply to '.replacement' |
4455 | * only to .rdev, so make sure to check that*/ | |
4456 | struct md_rdev *rdev2 = rcu_dereference( | |
4457 | conf->disks[i].rdev); | |
4458 | if (rdev2 == rdev) | |
4459 | clear_bit(R5_Insync, &dev->flags); | |
4460 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { | |
bc2607f3 | 4461 | s->handle_bad_blocks = 1; |
14a75d3e | 4462 | atomic_inc(&rdev2->nr_pending); |
bc2607f3 N |
4463 | } else |
4464 | clear_bit(R5_WriteError, &dev->flags); | |
4465 | } | |
1cc03eb9 | 4466 | if (test_bit(R5_MadeGood, &dev->flags)) { |
14a75d3e N |
4467 | /* This flag does not apply to '.replacement' |
4468 | * only to .rdev, so make sure to check that*/ | |
4469 | struct md_rdev *rdev2 = rcu_dereference( | |
4470 | conf->disks[i].rdev); | |
4471 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { | |
b84db560 | 4472 | s->handle_bad_blocks = 1; |
14a75d3e | 4473 | atomic_inc(&rdev2->nr_pending); |
b84db560 N |
4474 | } else |
4475 | clear_bit(R5_MadeGood, &dev->flags); | |
4476 | } | |
977df362 N |
4477 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
4478 | struct md_rdev *rdev2 = rcu_dereference( | |
4479 | conf->disks[i].replacement); | |
4480 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { | |
4481 | s->handle_bad_blocks = 1; | |
4482 | atomic_inc(&rdev2->nr_pending); | |
4483 | } else | |
4484 | clear_bit(R5_MadeGoodRepl, &dev->flags); | |
4485 | } | |
415e72d0 | 4486 | if (!test_bit(R5_Insync, &dev->flags)) { |
16a53ecc N |
4487 | /* The ReadError flag will just be confusing now */ |
4488 | clear_bit(R5_ReadError, &dev->flags); | |
4489 | clear_bit(R5_ReWrite, &dev->flags); | |
1da177e4 | 4490 | } |
415e72d0 N |
4491 | if (test_bit(R5_ReadError, &dev->flags)) |
4492 | clear_bit(R5_Insync, &dev->flags); | |
4493 | if (!test_bit(R5_Insync, &dev->flags)) { | |
cc94015a N |
4494 | if (s->failed < 2) |
4495 | s->failed_num[s->failed] = i; | |
4496 | s->failed++; | |
9a3e1101 N |
4497 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
4498 | do_recovery = 1; | |
415e72d0 | 4499 | } |
2ded3703 SL |
4500 | |
4501 | if (test_bit(R5_InJournal, &dev->flags)) | |
4502 | s->injournal++; | |
1e6d690b SL |
4503 | if (test_bit(R5_InJournal, &dev->flags) && dev->written) |
4504 | s->just_cached++; | |
1da177e4 | 4505 | } |
9a3e1101 N |
4506 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
4507 | /* If there is a failed device being replaced, | |
4508 | * we must be recovering. | |
4509 | * else if we are after recovery_cp, we must be syncing | |
c6d2e084 | 4510 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
9a3e1101 N |
4511 | * else we can only be replacing |
4512 | * sync and recovery both need to read all devices, and so | |
4513 | * use the same flag. | |
4514 | */ | |
4515 | if (do_recovery || | |
c6d2e084 | 4516 | sh->sector >= conf->mddev->recovery_cp || |
4517 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) | |
9a3e1101 N |
4518 | s->syncing = 1; |
4519 | else | |
4520 | s->replacing = 1; | |
4521 | } | |
1da177e4 | 4522 | rcu_read_unlock(); |
cc94015a N |
4523 | } |
4524 | ||
59fc630b | 4525 | static int clear_batch_ready(struct stripe_head *sh) |
4526 | { | |
b15a9dbd N |
4527 | /* Return '1' if this is a member of batch, or |
4528 | * '0' if it is a lone stripe or a head which can now be | |
4529 | * handled. | |
4530 | */ | |
59fc630b | 4531 | struct stripe_head *tmp; |
4532 | if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) | |
b15a9dbd | 4533 | return (sh->batch_head && sh->batch_head != sh); |
59fc630b | 4534 | spin_lock(&sh->stripe_lock); |
4535 | if (!sh->batch_head) { | |
4536 | spin_unlock(&sh->stripe_lock); | |
4537 | return 0; | |
4538 | } | |
4539 | ||
4540 | /* | |
4541 | * this stripe could be added to a batch list before we check | |
4542 | * BATCH_READY, skips it | |
4543 | */ | |
4544 | if (sh->batch_head != sh) { | |
4545 | spin_unlock(&sh->stripe_lock); | |
4546 | return 1; | |
4547 | } | |
4548 | spin_lock(&sh->batch_lock); | |
4549 | list_for_each_entry(tmp, &sh->batch_list, batch_list) | |
4550 | clear_bit(STRIPE_BATCH_READY, &tmp->state); | |
4551 | spin_unlock(&sh->batch_lock); | |
4552 | spin_unlock(&sh->stripe_lock); | |
4553 | ||
4554 | /* | |
4555 | * BATCH_READY is cleared, no new stripes can be added. | |
4556 | * batch_list can be accessed without lock | |
4557 | */ | |
4558 | return 0; | |
4559 | } | |
4560 | ||
3960ce79 N |
4561 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
4562 | unsigned long handle_flags) | |
72ac7330 | 4563 | { |
4e3d62ff | 4564 | struct stripe_head *sh, *next; |
72ac7330 | 4565 | int i; |
fb642b92 | 4566 | int do_wakeup = 0; |
72ac7330 | 4567 | |
bb27051f N |
4568 | list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { |
4569 | ||
72ac7330 | 4570 | list_del_init(&sh->batch_list); |
4571 | ||
fb3229d5 | 4572 | WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | |
1b956f7a N |
4573 | (1 << STRIPE_SYNCING) | |
4574 | (1 << STRIPE_REPLACED) | | |
1b956f7a N |
4575 | (1 << STRIPE_DELAYED) | |
4576 | (1 << STRIPE_BIT_DELAY) | | |
4577 | (1 << STRIPE_FULL_WRITE) | | |
4578 | (1 << STRIPE_BIOFILL_RUN) | | |
4579 | (1 << STRIPE_COMPUTE_RUN) | | |
4580 | (1 << STRIPE_OPS_REQ_PENDING) | | |
4581 | (1 << STRIPE_DISCARD) | | |
4582 | (1 << STRIPE_BATCH_READY) | | |
4583 | (1 << STRIPE_BATCH_ERR) | | |
fb3229d5 SL |
4584 | (1 << STRIPE_BITMAP_PENDING)), |
4585 | "stripe state: %lx\n", sh->state); | |
4586 | WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | | |
4587 | (1 << STRIPE_REPLACED)), | |
4588 | "head stripe state: %lx\n", head_sh->state); | |
1b956f7a N |
4589 | |
4590 | set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | | |
550da24f | 4591 | (1 << STRIPE_PREREAD_ACTIVE) | |
1b956f7a N |
4592 | (1 << STRIPE_DEGRADED)), |
4593 | head_sh->state & (1 << STRIPE_INSYNC)); | |
4594 | ||
72ac7330 | 4595 | sh->check_state = head_sh->check_state; |
4596 | sh->reconstruct_state = head_sh->reconstruct_state; | |
fb642b92 N |
4597 | for (i = 0; i < sh->disks; i++) { |
4598 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
4599 | do_wakeup = 1; | |
72ac7330 | 4600 | sh->dev[i].flags = head_sh->dev[i].flags & |
4601 | (~((1 << R5_WriteError) | (1 << R5_Overlap))); | |
fb642b92 | 4602 | } |
72ac7330 | 4603 | spin_lock_irq(&sh->stripe_lock); |
4604 | sh->batch_head = NULL; | |
4605 | spin_unlock_irq(&sh->stripe_lock); | |
3960ce79 N |
4606 | if (handle_flags == 0 || |
4607 | sh->state & handle_flags) | |
4608 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 4609 | raid5_release_stripe(sh); |
72ac7330 | 4610 | } |
fb642b92 N |
4611 | spin_lock_irq(&head_sh->stripe_lock); |
4612 | head_sh->batch_head = NULL; | |
4613 | spin_unlock_irq(&head_sh->stripe_lock); | |
4614 | for (i = 0; i < head_sh->disks; i++) | |
4615 | if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) | |
4616 | do_wakeup = 1; | |
3960ce79 N |
4617 | if (head_sh->state & handle_flags) |
4618 | set_bit(STRIPE_HANDLE, &head_sh->state); | |
fb642b92 N |
4619 | |
4620 | if (do_wakeup) | |
4621 | wake_up(&head_sh->raid_conf->wait_for_overlap); | |
72ac7330 | 4622 | } |
4623 | ||
cc94015a N |
4624 | static void handle_stripe(struct stripe_head *sh) |
4625 | { | |
4626 | struct stripe_head_state s; | |
d1688a6d | 4627 | struct r5conf *conf = sh->raid_conf; |
3687c061 | 4628 | int i; |
84789554 N |
4629 | int prexor; |
4630 | int disks = sh->disks; | |
474af965 | 4631 | struct r5dev *pdev, *qdev; |
cc94015a N |
4632 | |
4633 | clear_bit(STRIPE_HANDLE, &sh->state); | |
257a4b42 | 4634 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
cc94015a N |
4635 | /* already being handled, ensure it gets handled |
4636 | * again when current action finishes */ | |
4637 | set_bit(STRIPE_HANDLE, &sh->state); | |
4638 | return; | |
4639 | } | |
4640 | ||
59fc630b | 4641 | if (clear_batch_ready(sh) ) { |
4642 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); | |
4643 | return; | |
4644 | } | |
4645 | ||
4e3d62ff | 4646 | if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) |
3960ce79 | 4647 | break_stripe_batch_list(sh, 0); |
72ac7330 | 4648 | |
dabc4ec6 | 4649 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { |
f8dfcffd N |
4650 | spin_lock(&sh->stripe_lock); |
4651 | /* Cannot process 'sync' concurrently with 'discard' */ | |
4652 | if (!test_bit(STRIPE_DISCARD, &sh->state) && | |
4653 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { | |
4654 | set_bit(STRIPE_SYNCING, &sh->state); | |
4655 | clear_bit(STRIPE_INSYNC, &sh->state); | |
f94c0b66 | 4656 | clear_bit(STRIPE_REPLACED, &sh->state); |
f8dfcffd N |
4657 | } |
4658 | spin_unlock(&sh->stripe_lock); | |
cc94015a N |
4659 | } |
4660 | clear_bit(STRIPE_DELAYED, &sh->state); | |
4661 | ||
4662 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " | |
4663 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", | |
4664 | (unsigned long long)sh->sector, sh->state, | |
4665 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, | |
4666 | sh->check_state, sh->reconstruct_state); | |
3687c061 | 4667 | |
acfe726b | 4668 | analyse_stripe(sh, &s); |
c5a31000 | 4669 | |
b70abcb2 SL |
4670 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
4671 | goto finish; | |
4672 | ||
16d997b7 N |
4673 | if (s.handle_bad_blocks || |
4674 | test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) { | |
bc2607f3 N |
4675 | set_bit(STRIPE_HANDLE, &sh->state); |
4676 | goto finish; | |
4677 | } | |
4678 | ||
474af965 N |
4679 | if (unlikely(s.blocked_rdev)) { |
4680 | if (s.syncing || s.expanding || s.expanded || | |
9a3e1101 | 4681 | s.replacing || s.to_write || s.written) { |
474af965 N |
4682 | set_bit(STRIPE_HANDLE, &sh->state); |
4683 | goto finish; | |
4684 | } | |
4685 | /* There is nothing for the blocked_rdev to block */ | |
4686 | rdev_dec_pending(s.blocked_rdev, conf->mddev); | |
4687 | s.blocked_rdev = NULL; | |
4688 | } | |
4689 | ||
4690 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { | |
4691 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); | |
4692 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); | |
4693 | } | |
4694 | ||
4695 | pr_debug("locked=%d uptodate=%d to_read=%d" | |
4696 | " to_write=%d failed=%d failed_num=%d,%d\n", | |
4697 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, | |
4698 | s.failed_num[0], s.failed_num[1]); | |
4699 | /* check if the array has lost more than max_degraded devices and, | |
4700 | * if so, some requests might need to be failed. | |
4701 | */ | |
6e74a9cf | 4702 | if (s.failed > conf->max_degraded || s.log_failed) { |
9a3f530f N |
4703 | sh->check_state = 0; |
4704 | sh->reconstruct_state = 0; | |
626f2092 | 4705 | break_stripe_batch_list(sh, 0); |
9a3f530f | 4706 | if (s.to_read+s.to_write+s.written) |
bd83d0a2 | 4707 | handle_failed_stripe(conf, sh, &s, disks); |
9a3e1101 | 4708 | if (s.syncing + s.replacing) |
9a3f530f N |
4709 | handle_failed_sync(conf, sh, &s); |
4710 | } | |
474af965 | 4711 | |
84789554 N |
4712 | /* Now we check to see if any write operations have recently |
4713 | * completed | |
4714 | */ | |
4715 | prexor = 0; | |
4716 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) | |
4717 | prexor = 1; | |
4718 | if (sh->reconstruct_state == reconstruct_state_drain_result || | |
4719 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { | |
4720 | sh->reconstruct_state = reconstruct_state_idle; | |
4721 | ||
4722 | /* All the 'written' buffers and the parity block are ready to | |
4723 | * be written back to disk | |
4724 | */ | |
9e444768 SL |
4725 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
4726 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); | |
84789554 | 4727 | BUG_ON(sh->qd_idx >= 0 && |
9e444768 SL |
4728 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
4729 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); | |
84789554 N |
4730 | for (i = disks; i--; ) { |
4731 | struct r5dev *dev = &sh->dev[i]; | |
4732 | if (test_bit(R5_LOCKED, &dev->flags) && | |
4733 | (i == sh->pd_idx || i == sh->qd_idx || | |
1e6d690b SL |
4734 | dev->written || test_bit(R5_InJournal, |
4735 | &dev->flags))) { | |
84789554 N |
4736 | pr_debug("Writing block %d\n", i); |
4737 | set_bit(R5_Wantwrite, &dev->flags); | |
4738 | if (prexor) | |
4739 | continue; | |
9c4bdf69 N |
4740 | if (s.failed > 1) |
4741 | continue; | |
84789554 N |
4742 | if (!test_bit(R5_Insync, &dev->flags) || |
4743 | ((i == sh->pd_idx || i == sh->qd_idx) && | |
4744 | s.failed == 0)) | |
4745 | set_bit(STRIPE_INSYNC, &sh->state); | |
4746 | } | |
4747 | } | |
4748 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
4749 | s.dec_preread_active = 1; | |
4750 | } | |
4751 | ||
ef5b7c69 N |
4752 | /* |
4753 | * might be able to return some write requests if the parity blocks | |
4754 | * are safe, or on a failed drive | |
4755 | */ | |
4756 | pdev = &sh->dev[sh->pd_idx]; | |
4757 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) | |
4758 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); | |
4759 | qdev = &sh->dev[sh->qd_idx]; | |
4760 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) | |
4761 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) | |
4762 | || conf->level < 6; | |
4763 | ||
4764 | if (s.written && | |
4765 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) | |
4766 | && !test_bit(R5_LOCKED, &pdev->flags) | |
4767 | && (test_bit(R5_UPTODATE, &pdev->flags) || | |
4768 | test_bit(R5_Discard, &pdev->flags))))) && | |
4769 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) | |
4770 | && !test_bit(R5_LOCKED, &qdev->flags) | |
4771 | && (test_bit(R5_UPTODATE, &qdev->flags) || | |
4772 | test_bit(R5_Discard, &qdev->flags)))))) | |
bd83d0a2 | 4773 | handle_stripe_clean_event(conf, sh, disks); |
ef5b7c69 | 4774 | |
1e6d690b | 4775 | if (s.just_cached) |
bd83d0a2 | 4776 | r5c_handle_cached_data_endio(conf, sh, disks); |
ff875738 | 4777 | log_stripe_write_finished(sh); |
1e6d690b | 4778 | |
ef5b7c69 N |
4779 | /* Now we might consider reading some blocks, either to check/generate |
4780 | * parity, or to satisfy requests | |
4781 | * or to load a block that is being partially written. | |
4782 | */ | |
4783 | if (s.to_read || s.non_overwrite | |
4784 | || (conf->level == 6 && s.to_write && s.failed) | |
4785 | || (s.syncing && (s.uptodate + s.compute < disks)) | |
4786 | || s.replacing | |
4787 | || s.expanding) | |
4788 | handle_stripe_fill(sh, &s, disks); | |
4789 | ||
2ded3703 SL |
4790 | /* |
4791 | * When the stripe finishes full journal write cycle (write to journal | |
4792 | * and raid disk), this is the clean up procedure so it is ready for | |
4793 | * next operation. | |
4794 | */ | |
4795 | r5c_finish_stripe_write_out(conf, sh, &s); | |
4796 | ||
4797 | /* | |
4798 | * Now to consider new write requests, cache write back and what else, | |
4799 | * if anything should be read. We do not handle new writes when: | |
84789554 N |
4800 | * 1/ A 'write' operation (copy+xor) is already in flight. |
4801 | * 2/ A 'check' operation is in flight, as it may clobber the parity | |
4802 | * block. | |
2ded3703 | 4803 | * 3/ A r5c cache log write is in flight. |
84789554 | 4804 | */ |
2ded3703 SL |
4805 | |
4806 | if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { | |
4807 | if (!r5c_is_writeback(conf->log)) { | |
4808 | if (s.to_write) | |
4809 | handle_stripe_dirtying(conf, sh, &s, disks); | |
4810 | } else { /* write back cache */ | |
4811 | int ret = 0; | |
4812 | ||
4813 | /* First, try handle writes in caching phase */ | |
4814 | if (s.to_write) | |
4815 | ret = r5c_try_caching_write(conf, sh, &s, | |
4816 | disks); | |
4817 | /* | |
4818 | * If caching phase failed: ret == -EAGAIN | |
4819 | * OR | |
4820 | * stripe under reclaim: !caching && injournal | |
4821 | * | |
4822 | * fall back to handle_stripe_dirtying() | |
4823 | */ | |
4824 | if (ret == -EAGAIN || | |
4825 | /* stripe under reclaim: !caching && injournal */ | |
4826 | (!test_bit(STRIPE_R5C_CACHING, &sh->state) && | |
d7bd398e SL |
4827 | s.injournal > 0)) { |
4828 | ret = handle_stripe_dirtying(conf, sh, &s, | |
4829 | disks); | |
4830 | if (ret == -EAGAIN) | |
4831 | goto finish; | |
4832 | } | |
2ded3703 SL |
4833 | } |
4834 | } | |
84789554 N |
4835 | |
4836 | /* maybe we need to check and possibly fix the parity for this stripe | |
4837 | * Any reads will already have been scheduled, so we just see if enough | |
4838 | * data is available. The parity check is held off while parity | |
4839 | * dependent operations are in flight. | |
4840 | */ | |
4841 | if (sh->check_state || | |
4842 | (s.syncing && s.locked == 0 && | |
4843 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && | |
4844 | !test_bit(STRIPE_INSYNC, &sh->state))) { | |
4845 | if (conf->level == 6) | |
4846 | handle_parity_checks6(conf, sh, &s, disks); | |
4847 | else | |
4848 | handle_parity_checks5(conf, sh, &s, disks); | |
4849 | } | |
c5a31000 | 4850 | |
f94c0b66 N |
4851 | if ((s.replacing || s.syncing) && s.locked == 0 |
4852 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) | |
4853 | && !test_bit(STRIPE_REPLACED, &sh->state)) { | |
9a3e1101 N |
4854 | /* Write out to replacement devices where possible */ |
4855 | for (i = 0; i < conf->raid_disks; i++) | |
f94c0b66 N |
4856 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
4857 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); | |
9a3e1101 N |
4858 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
4859 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
4860 | s.locked++; | |
4861 | } | |
f94c0b66 N |
4862 | if (s.replacing) |
4863 | set_bit(STRIPE_INSYNC, &sh->state); | |
4864 | set_bit(STRIPE_REPLACED, &sh->state); | |
9a3e1101 N |
4865 | } |
4866 | if ((s.syncing || s.replacing) && s.locked == 0 && | |
f94c0b66 | 4867 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
9a3e1101 | 4868 | test_bit(STRIPE_INSYNC, &sh->state)) { |
c5a31000 N |
4869 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
4870 | clear_bit(STRIPE_SYNCING, &sh->state); | |
f8dfcffd N |
4871 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
4872 | wake_up(&conf->wait_for_overlap); | |
c5a31000 N |
4873 | } |
4874 | ||
4875 | /* If the failed drives are just a ReadError, then we might need | |
4876 | * to progress the repair/check process | |
4877 | */ | |
4878 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) | |
4879 | for (i = 0; i < s.failed; i++) { | |
4880 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; | |
4881 | if (test_bit(R5_ReadError, &dev->flags) | |
4882 | && !test_bit(R5_LOCKED, &dev->flags) | |
4883 | && test_bit(R5_UPTODATE, &dev->flags) | |
4884 | ) { | |
4885 | if (!test_bit(R5_ReWrite, &dev->flags)) { | |
4886 | set_bit(R5_Wantwrite, &dev->flags); | |
4887 | set_bit(R5_ReWrite, &dev->flags); | |
4888 | set_bit(R5_LOCKED, &dev->flags); | |
4889 | s.locked++; | |
4890 | } else { | |
4891 | /* let's read it back */ | |
4892 | set_bit(R5_Wantread, &dev->flags); | |
4893 | set_bit(R5_LOCKED, &dev->flags); | |
4894 | s.locked++; | |
4895 | } | |
4896 | } | |
4897 | } | |
4898 | ||
3687c061 N |
4899 | /* Finish reconstruct operations initiated by the expansion process */ |
4900 | if (sh->reconstruct_state == reconstruct_state_result) { | |
4901 | struct stripe_head *sh_src | |
6d036f7d | 4902 | = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1); |
3687c061 N |
4903 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
4904 | /* sh cannot be written until sh_src has been read. | |
4905 | * so arrange for sh to be delayed a little | |
4906 | */ | |
4907 | set_bit(STRIPE_DELAYED, &sh->state); | |
4908 | set_bit(STRIPE_HANDLE, &sh->state); | |
4909 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, | |
4910 | &sh_src->state)) | |
4911 | atomic_inc(&conf->preread_active_stripes); | |
6d036f7d | 4912 | raid5_release_stripe(sh_src); |
3687c061 N |
4913 | goto finish; |
4914 | } | |
4915 | if (sh_src) | |
6d036f7d | 4916 | raid5_release_stripe(sh_src); |
3687c061 N |
4917 | |
4918 | sh->reconstruct_state = reconstruct_state_idle; | |
4919 | clear_bit(STRIPE_EXPANDING, &sh->state); | |
4920 | for (i = conf->raid_disks; i--; ) { | |
4921 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
4922 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
4923 | s.locked++; | |
4924 | } | |
4925 | } | |
f416885e | 4926 | |
3687c061 N |
4927 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
4928 | !sh->reconstruct_state) { | |
4929 | /* Need to write out all blocks after computing parity */ | |
4930 | sh->disks = conf->raid_disks; | |
4931 | stripe_set_idx(sh->sector, conf, 0, sh); | |
4932 | schedule_reconstruction(sh, &s, 1, 1); | |
4933 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { | |
4934 | clear_bit(STRIPE_EXPAND_READY, &sh->state); | |
4935 | atomic_dec(&conf->reshape_stripes); | |
4936 | wake_up(&conf->wait_for_overlap); | |
4937 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); | |
4938 | } | |
4939 | ||
4940 | if (s.expanding && s.locked == 0 && | |
4941 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) | |
4942 | handle_stripe_expansion(conf, sh); | |
16a53ecc | 4943 | |
3687c061 | 4944 | finish: |
6bfe0b49 | 4945 | /* wait for this device to become unblocked */ |
5f066c63 N |
4946 | if (unlikely(s.blocked_rdev)) { |
4947 | if (conf->mddev->external) | |
4948 | md_wait_for_blocked_rdev(s.blocked_rdev, | |
4949 | conf->mddev); | |
4950 | else | |
4951 | /* Internal metadata will immediately | |
4952 | * be written by raid5d, so we don't | |
4953 | * need to wait here. | |
4954 | */ | |
4955 | rdev_dec_pending(s.blocked_rdev, | |
4956 | conf->mddev); | |
4957 | } | |
6bfe0b49 | 4958 | |
bc2607f3 N |
4959 | if (s.handle_bad_blocks) |
4960 | for (i = disks; i--; ) { | |
3cb03002 | 4961 | struct md_rdev *rdev; |
bc2607f3 N |
4962 | struct r5dev *dev = &sh->dev[i]; |
4963 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { | |
4964 | /* We own a safe reference to the rdev */ | |
4965 | rdev = conf->disks[i].rdev; | |
4966 | if (!rdev_set_badblocks(rdev, sh->sector, | |
4967 | STRIPE_SECTORS, 0)) | |
4968 | md_error(conf->mddev, rdev); | |
4969 | rdev_dec_pending(rdev, conf->mddev); | |
4970 | } | |
b84db560 N |
4971 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
4972 | rdev = conf->disks[i].rdev; | |
4973 | rdev_clear_badblocks(rdev, sh->sector, | |
c6563a8c | 4974 | STRIPE_SECTORS, 0); |
b84db560 N |
4975 | rdev_dec_pending(rdev, conf->mddev); |
4976 | } | |
977df362 N |
4977 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
4978 | rdev = conf->disks[i].replacement; | |
dd054fce N |
4979 | if (!rdev) |
4980 | /* rdev have been moved down */ | |
4981 | rdev = conf->disks[i].rdev; | |
977df362 | 4982 | rdev_clear_badblocks(rdev, sh->sector, |
c6563a8c | 4983 | STRIPE_SECTORS, 0); |
977df362 N |
4984 | rdev_dec_pending(rdev, conf->mddev); |
4985 | } | |
bc2607f3 N |
4986 | } |
4987 | ||
6c0069c0 YT |
4988 | if (s.ops_request) |
4989 | raid_run_ops(sh, s.ops_request); | |
4990 | ||
f0e43bcd | 4991 | ops_run_io(sh, &s); |
16a53ecc | 4992 | |
c5709ef6 | 4993 | if (s.dec_preread_active) { |
729a1866 | 4994 | /* We delay this until after ops_run_io so that if make_request |
e9c7469b | 4995 | * is waiting on a flush, it won't continue until the writes |
729a1866 N |
4996 | * have actually been submitted. |
4997 | */ | |
4998 | atomic_dec(&conf->preread_active_stripes); | |
4999 | if (atomic_read(&conf->preread_active_stripes) < | |
5000 | IO_THRESHOLD) | |
5001 | md_wakeup_thread(conf->mddev->thread); | |
5002 | } | |
5003 | ||
257a4b42 | 5004 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
16a53ecc N |
5005 | } |
5006 | ||
d1688a6d | 5007 | static void raid5_activate_delayed(struct r5conf *conf) |
16a53ecc N |
5008 | { |
5009 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { | |
5010 | while (!list_empty(&conf->delayed_list)) { | |
5011 | struct list_head *l = conf->delayed_list.next; | |
5012 | struct stripe_head *sh; | |
5013 | sh = list_entry(l, struct stripe_head, lru); | |
5014 | list_del_init(l); | |
5015 | clear_bit(STRIPE_DELAYED, &sh->state); | |
5016 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
5017 | atomic_inc(&conf->preread_active_stripes); | |
8b3e6cdc | 5018 | list_add_tail(&sh->lru, &conf->hold_list); |
851c30c9 | 5019 | raid5_wakeup_stripe_thread(sh); |
16a53ecc | 5020 | } |
482c0834 | 5021 | } |
16a53ecc N |
5022 | } |
5023 | ||
566c09c5 SL |
5024 | static void activate_bit_delay(struct r5conf *conf, |
5025 | struct list_head *temp_inactive_list) | |
16a53ecc N |
5026 | { |
5027 | /* device_lock is held */ | |
5028 | struct list_head head; | |
5029 | list_add(&head, &conf->bitmap_list); | |
5030 | list_del_init(&conf->bitmap_list); | |
5031 | while (!list_empty(&head)) { | |
5032 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); | |
566c09c5 | 5033 | int hash; |
16a53ecc N |
5034 | list_del_init(&sh->lru); |
5035 | atomic_inc(&sh->count); | |
566c09c5 SL |
5036 | hash = sh->hash_lock_index; |
5037 | __release_stripe(conf, sh, &temp_inactive_list[hash]); | |
16a53ecc N |
5038 | } |
5039 | } | |
5040 | ||
5c675f83 | 5041 | static int raid5_congested(struct mddev *mddev, int bits) |
f022b2fd | 5042 | { |
d1688a6d | 5043 | struct r5conf *conf = mddev->private; |
f022b2fd N |
5044 | |
5045 | /* No difference between reads and writes. Just check | |
5046 | * how busy the stripe_cache is | |
5047 | */ | |
3fa841d7 | 5048 | |
5423399a | 5049 | if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) |
f022b2fd | 5050 | return 1; |
a39f7afd SL |
5051 | |
5052 | /* Also checks whether there is pressure on r5cache log space */ | |
5053 | if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) | |
5054 | return 1; | |
f022b2fd N |
5055 | if (conf->quiesce) |
5056 | return 1; | |
4bda556a | 5057 | if (atomic_read(&conf->empty_inactive_list_nr)) |
f022b2fd N |
5058 | return 1; |
5059 | ||
5060 | return 0; | |
5061 | } | |
5062 | ||
fd01b88c | 5063 | static int in_chunk_boundary(struct mddev *mddev, struct bio *bio) |
f679623f | 5064 | { |
3cb5edf4 | 5065 | struct r5conf *conf = mddev->private; |
4f024f37 | 5066 | sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev); |
3cb5edf4 | 5067 | unsigned int chunk_sectors; |
aa8b57aa | 5068 | unsigned int bio_sectors = bio_sectors(bio); |
f679623f | 5069 | |
3cb5edf4 | 5070 | chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); |
f679623f RBJ |
5071 | return chunk_sectors >= |
5072 | ((sector & (chunk_sectors - 1)) + bio_sectors); | |
5073 | } | |
5074 | ||
46031f9a RBJ |
5075 | /* |
5076 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) | |
5077 | * later sampled by raid5d. | |
5078 | */ | |
d1688a6d | 5079 | static void add_bio_to_retry(struct bio *bi,struct r5conf *conf) |
46031f9a RBJ |
5080 | { |
5081 | unsigned long flags; | |
5082 | ||
5083 | spin_lock_irqsave(&conf->device_lock, flags); | |
5084 | ||
5085 | bi->bi_next = conf->retry_read_aligned_list; | |
5086 | conf->retry_read_aligned_list = bi; | |
5087 | ||
5088 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
5089 | md_wakeup_thread(conf->mddev->thread); | |
5090 | } | |
5091 | ||
0472a42b N |
5092 | static struct bio *remove_bio_from_retry(struct r5conf *conf, |
5093 | unsigned int *offset) | |
46031f9a RBJ |
5094 | { |
5095 | struct bio *bi; | |
5096 | ||
5097 | bi = conf->retry_read_aligned; | |
5098 | if (bi) { | |
0472a42b | 5099 | *offset = conf->retry_read_offset; |
46031f9a RBJ |
5100 | conf->retry_read_aligned = NULL; |
5101 | return bi; | |
5102 | } | |
5103 | bi = conf->retry_read_aligned_list; | |
5104 | if(bi) { | |
387bb173 | 5105 | conf->retry_read_aligned_list = bi->bi_next; |
46031f9a | 5106 | bi->bi_next = NULL; |
0472a42b | 5107 | *offset = 0; |
46031f9a RBJ |
5108 | } |
5109 | ||
5110 | return bi; | |
5111 | } | |
5112 | ||
f679623f RBJ |
5113 | /* |
5114 | * The "raid5_align_endio" should check if the read succeeded and if it | |
5115 | * did, call bio_endio on the original bio (having bio_put the new bio | |
5116 | * first). | |
5117 | * If the read failed.. | |
5118 | */ | |
4246a0b6 | 5119 | static void raid5_align_endio(struct bio *bi) |
f679623f RBJ |
5120 | { |
5121 | struct bio* raid_bi = bi->bi_private; | |
fd01b88c | 5122 | struct mddev *mddev; |
d1688a6d | 5123 | struct r5conf *conf; |
3cb03002 | 5124 | struct md_rdev *rdev; |
9b81c842 | 5125 | int error = bi->bi_error; |
46031f9a | 5126 | |
f679623f | 5127 | bio_put(bi); |
46031f9a | 5128 | |
46031f9a RBJ |
5129 | rdev = (void*)raid_bi->bi_next; |
5130 | raid_bi->bi_next = NULL; | |
2b7f2228 N |
5131 | mddev = rdev->mddev; |
5132 | conf = mddev->private; | |
46031f9a RBJ |
5133 | |
5134 | rdev_dec_pending(rdev, conf->mddev); | |
5135 | ||
9b81c842 | 5136 | if (!error) { |
4246a0b6 | 5137 | bio_endio(raid_bi); |
46031f9a | 5138 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
b1b46486 | 5139 | wake_up(&conf->wait_for_quiescent); |
6712ecf8 | 5140 | return; |
46031f9a RBJ |
5141 | } |
5142 | ||
45b4233c | 5143 | pr_debug("raid5_align_endio : io error...handing IO for a retry\n"); |
46031f9a RBJ |
5144 | |
5145 | add_bio_to_retry(raid_bi, conf); | |
f679623f RBJ |
5146 | } |
5147 | ||
7ef6b12a | 5148 | static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio) |
f679623f | 5149 | { |
d1688a6d | 5150 | struct r5conf *conf = mddev->private; |
8553fe7e | 5151 | int dd_idx; |
f679623f | 5152 | struct bio* align_bi; |
3cb03002 | 5153 | struct md_rdev *rdev; |
671488cc | 5154 | sector_t end_sector; |
f679623f RBJ |
5155 | |
5156 | if (!in_chunk_boundary(mddev, raid_bio)) { | |
7ef6b12a | 5157 | pr_debug("%s: non aligned\n", __func__); |
f679623f RBJ |
5158 | return 0; |
5159 | } | |
5160 | /* | |
d7a10308 | 5161 | * use bio_clone_fast to make a copy of the bio |
f679623f | 5162 | */ |
d7a10308 | 5163 | align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set); |
f679623f RBJ |
5164 | if (!align_bi) |
5165 | return 0; | |
5166 | /* | |
5167 | * set bi_end_io to a new function, and set bi_private to the | |
5168 | * original bio. | |
5169 | */ | |
5170 | align_bi->bi_end_io = raid5_align_endio; | |
5171 | align_bi->bi_private = raid_bio; | |
5172 | /* | |
5173 | * compute position | |
5174 | */ | |
4f024f37 KO |
5175 | align_bi->bi_iter.bi_sector = |
5176 | raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, | |
5177 | 0, &dd_idx, NULL); | |
f679623f | 5178 | |
f73a1c7d | 5179 | end_sector = bio_end_sector(align_bi); |
f679623f | 5180 | rcu_read_lock(); |
671488cc N |
5181 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
5182 | if (!rdev || test_bit(Faulty, &rdev->flags) || | |
5183 | rdev->recovery_offset < end_sector) { | |
5184 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); | |
5185 | if (rdev && | |
5186 | (test_bit(Faulty, &rdev->flags) || | |
5187 | !(test_bit(In_sync, &rdev->flags) || | |
5188 | rdev->recovery_offset >= end_sector))) | |
5189 | rdev = NULL; | |
5190 | } | |
03b047f4 SL |
5191 | |
5192 | if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) { | |
5193 | rcu_read_unlock(); | |
5194 | bio_put(align_bi); | |
5195 | return 0; | |
5196 | } | |
5197 | ||
671488cc | 5198 | if (rdev) { |
31c176ec N |
5199 | sector_t first_bad; |
5200 | int bad_sectors; | |
5201 | ||
f679623f RBJ |
5202 | atomic_inc(&rdev->nr_pending); |
5203 | rcu_read_unlock(); | |
46031f9a RBJ |
5204 | raid_bio->bi_next = (void*)rdev; |
5205 | align_bi->bi_bdev = rdev->bdev; | |
b7c44ed9 | 5206 | bio_clear_flag(align_bi, BIO_SEG_VALID); |
46031f9a | 5207 | |
7140aafc | 5208 | if (is_badblock(rdev, align_bi->bi_iter.bi_sector, |
4f024f37 | 5209 | bio_sectors(align_bi), |
31c176ec | 5210 | &first_bad, &bad_sectors)) { |
387bb173 NB |
5211 | bio_put(align_bi); |
5212 | rdev_dec_pending(rdev, mddev); | |
5213 | return 0; | |
5214 | } | |
5215 | ||
6c0544e2 | 5216 | /* No reshape active, so we can trust rdev->data_offset */ |
4f024f37 | 5217 | align_bi->bi_iter.bi_sector += rdev->data_offset; |
6c0544e2 | 5218 | |
46031f9a | 5219 | spin_lock_irq(&conf->device_lock); |
b1b46486 | 5220 | wait_event_lock_irq(conf->wait_for_quiescent, |
46031f9a | 5221 | conf->quiesce == 0, |
eed8c02e | 5222 | conf->device_lock); |
46031f9a RBJ |
5223 | atomic_inc(&conf->active_aligned_reads); |
5224 | spin_unlock_irq(&conf->device_lock); | |
5225 | ||
e3620a3a JB |
5226 | if (mddev->gendisk) |
5227 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), | |
5228 | align_bi, disk_devt(mddev->gendisk), | |
4f024f37 | 5229 | raid_bio->bi_iter.bi_sector); |
f679623f RBJ |
5230 | generic_make_request(align_bi); |
5231 | return 1; | |
5232 | } else { | |
5233 | rcu_read_unlock(); | |
46031f9a | 5234 | bio_put(align_bi); |
f679623f RBJ |
5235 | return 0; |
5236 | } | |
5237 | } | |
5238 | ||
7ef6b12a ML |
5239 | static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) |
5240 | { | |
5241 | struct bio *split; | |
dd7a8f5d N |
5242 | sector_t sector = raid_bio->bi_iter.bi_sector; |
5243 | unsigned chunk_sects = mddev->chunk_sectors; | |
5244 | unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); | |
7ef6b12a | 5245 | |
dd7a8f5d N |
5246 | if (sectors < bio_sectors(raid_bio)) { |
5247 | struct r5conf *conf = mddev->private; | |
5248 | split = bio_split(raid_bio, sectors, GFP_NOIO, conf->bio_split); | |
5249 | bio_chain(split, raid_bio); | |
5250 | generic_make_request(raid_bio); | |
5251 | raid_bio = split; | |
5252 | } | |
7ef6b12a | 5253 | |
dd7a8f5d N |
5254 | if (!raid5_read_one_chunk(mddev, raid_bio)) |
5255 | return raid_bio; | |
7ef6b12a ML |
5256 | |
5257 | return NULL; | |
5258 | } | |
5259 | ||
8b3e6cdc DW |
5260 | /* __get_priority_stripe - get the next stripe to process |
5261 | * | |
5262 | * Full stripe writes are allowed to pass preread active stripes up until | |
5263 | * the bypass_threshold is exceeded. In general the bypass_count | |
5264 | * increments when the handle_list is handled before the hold_list; however, it | |
5265 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a | |
5266 | * stripe with in flight i/o. The bypass_count will be reset when the | |
5267 | * head of the hold_list has changed, i.e. the head was promoted to the | |
5268 | * handle_list. | |
5269 | */ | |
851c30c9 | 5270 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
8b3e6cdc | 5271 | { |
535ae4eb | 5272 | struct stripe_head *sh, *tmp; |
851c30c9 | 5273 | struct list_head *handle_list = NULL; |
535ae4eb SL |
5274 | struct r5worker_group *wg; |
5275 | bool second_try = !r5c_is_writeback(conf->log); | |
5276 | bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state); | |
851c30c9 | 5277 | |
535ae4eb SL |
5278 | again: |
5279 | wg = NULL; | |
5280 | sh = NULL; | |
851c30c9 | 5281 | if (conf->worker_cnt_per_group == 0) { |
535ae4eb SL |
5282 | handle_list = try_loprio ? &conf->loprio_list : |
5283 | &conf->handle_list; | |
851c30c9 | 5284 | } else if (group != ANY_GROUP) { |
535ae4eb SL |
5285 | handle_list = try_loprio ? &conf->worker_groups[group].loprio_list : |
5286 | &conf->worker_groups[group].handle_list; | |
bfc90cb0 | 5287 | wg = &conf->worker_groups[group]; |
851c30c9 SL |
5288 | } else { |
5289 | int i; | |
5290 | for (i = 0; i < conf->group_cnt; i++) { | |
535ae4eb SL |
5291 | handle_list = try_loprio ? &conf->worker_groups[i].loprio_list : |
5292 | &conf->worker_groups[i].handle_list; | |
bfc90cb0 | 5293 | wg = &conf->worker_groups[i]; |
851c30c9 SL |
5294 | if (!list_empty(handle_list)) |
5295 | break; | |
5296 | } | |
5297 | } | |
8b3e6cdc DW |
5298 | |
5299 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", | |
5300 | __func__, | |
851c30c9 | 5301 | list_empty(handle_list) ? "empty" : "busy", |
8b3e6cdc DW |
5302 | list_empty(&conf->hold_list) ? "empty" : "busy", |
5303 | atomic_read(&conf->pending_full_writes), conf->bypass_count); | |
5304 | ||
851c30c9 SL |
5305 | if (!list_empty(handle_list)) { |
5306 | sh = list_entry(handle_list->next, typeof(*sh), lru); | |
8b3e6cdc DW |
5307 | |
5308 | if (list_empty(&conf->hold_list)) | |
5309 | conf->bypass_count = 0; | |
5310 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { | |
5311 | if (conf->hold_list.next == conf->last_hold) | |
5312 | conf->bypass_count++; | |
5313 | else { | |
5314 | conf->last_hold = conf->hold_list.next; | |
5315 | conf->bypass_count -= conf->bypass_threshold; | |
5316 | if (conf->bypass_count < 0) | |
5317 | conf->bypass_count = 0; | |
5318 | } | |
5319 | } | |
5320 | } else if (!list_empty(&conf->hold_list) && | |
5321 | ((conf->bypass_threshold && | |
5322 | conf->bypass_count > conf->bypass_threshold) || | |
5323 | atomic_read(&conf->pending_full_writes) == 0)) { | |
851c30c9 SL |
5324 | |
5325 | list_for_each_entry(tmp, &conf->hold_list, lru) { | |
5326 | if (conf->worker_cnt_per_group == 0 || | |
5327 | group == ANY_GROUP || | |
5328 | !cpu_online(tmp->cpu) || | |
5329 | cpu_to_group(tmp->cpu) == group) { | |
5330 | sh = tmp; | |
5331 | break; | |
5332 | } | |
5333 | } | |
5334 | ||
5335 | if (sh) { | |
5336 | conf->bypass_count -= conf->bypass_threshold; | |
5337 | if (conf->bypass_count < 0) | |
5338 | conf->bypass_count = 0; | |
5339 | } | |
bfc90cb0 | 5340 | wg = NULL; |
851c30c9 SL |
5341 | } |
5342 | ||
535ae4eb SL |
5343 | if (!sh) { |
5344 | if (second_try) | |
5345 | return NULL; | |
5346 | second_try = true; | |
5347 | try_loprio = !try_loprio; | |
5348 | goto again; | |
5349 | } | |
8b3e6cdc | 5350 | |
bfc90cb0 SL |
5351 | if (wg) { |
5352 | wg->stripes_cnt--; | |
5353 | sh->group = NULL; | |
5354 | } | |
8b3e6cdc | 5355 | list_del_init(&sh->lru); |
c7a6d35e | 5356 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
8b3e6cdc DW |
5357 | return sh; |
5358 | } | |
f679623f | 5359 | |
8811b596 SL |
5360 | struct raid5_plug_cb { |
5361 | struct blk_plug_cb cb; | |
5362 | struct list_head list; | |
566c09c5 | 5363 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
8811b596 SL |
5364 | }; |
5365 | ||
5366 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) | |
5367 | { | |
5368 | struct raid5_plug_cb *cb = container_of( | |
5369 | blk_cb, struct raid5_plug_cb, cb); | |
5370 | struct stripe_head *sh; | |
5371 | struct mddev *mddev = cb->cb.data; | |
5372 | struct r5conf *conf = mddev->private; | |
a9add5d9 | 5373 | int cnt = 0; |
566c09c5 | 5374 | int hash; |
8811b596 SL |
5375 | |
5376 | if (cb->list.next && !list_empty(&cb->list)) { | |
5377 | spin_lock_irq(&conf->device_lock); | |
5378 | while (!list_empty(&cb->list)) { | |
5379 | sh = list_first_entry(&cb->list, struct stripe_head, lru); | |
5380 | list_del_init(&sh->lru); | |
5381 | /* | |
5382 | * avoid race release_stripe_plug() sees | |
5383 | * STRIPE_ON_UNPLUG_LIST clear but the stripe | |
5384 | * is still in our list | |
5385 | */ | |
4e857c58 | 5386 | smp_mb__before_atomic(); |
8811b596 | 5387 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
773ca82f SL |
5388 | /* |
5389 | * STRIPE_ON_RELEASE_LIST could be set here. In that | |
5390 | * case, the count is always > 1 here | |
5391 | */ | |
566c09c5 SL |
5392 | hash = sh->hash_lock_index; |
5393 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); | |
a9add5d9 | 5394 | cnt++; |
8811b596 SL |
5395 | } |
5396 | spin_unlock_irq(&conf->device_lock); | |
5397 | } | |
566c09c5 SL |
5398 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
5399 | NR_STRIPE_HASH_LOCKS); | |
e3620a3a JB |
5400 | if (mddev->queue) |
5401 | trace_block_unplug(mddev->queue, cnt, !from_schedule); | |
8811b596 SL |
5402 | kfree(cb); |
5403 | } | |
5404 | ||
5405 | static void release_stripe_plug(struct mddev *mddev, | |
5406 | struct stripe_head *sh) | |
5407 | { | |
5408 | struct blk_plug_cb *blk_cb = blk_check_plugged( | |
5409 | raid5_unplug, mddev, | |
5410 | sizeof(struct raid5_plug_cb)); | |
5411 | struct raid5_plug_cb *cb; | |
5412 | ||
5413 | if (!blk_cb) { | |
6d036f7d | 5414 | raid5_release_stripe(sh); |
8811b596 SL |
5415 | return; |
5416 | } | |
5417 | ||
5418 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); | |
5419 | ||
566c09c5 SL |
5420 | if (cb->list.next == NULL) { |
5421 | int i; | |
8811b596 | 5422 | INIT_LIST_HEAD(&cb->list); |
566c09c5 SL |
5423 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
5424 | INIT_LIST_HEAD(cb->temp_inactive_list + i); | |
5425 | } | |
8811b596 SL |
5426 | |
5427 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) | |
5428 | list_add_tail(&sh->lru, &cb->list); | |
5429 | else | |
6d036f7d | 5430 | raid5_release_stripe(sh); |
8811b596 SL |
5431 | } |
5432 | ||
620125f2 SL |
5433 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
5434 | { | |
5435 | struct r5conf *conf = mddev->private; | |
5436 | sector_t logical_sector, last_sector; | |
5437 | struct stripe_head *sh; | |
620125f2 SL |
5438 | int stripe_sectors; |
5439 | ||
5440 | if (mddev->reshape_position != MaxSector) | |
5441 | /* Skip discard while reshape is happening */ | |
5442 | return; | |
5443 | ||
4f024f37 KO |
5444 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
5445 | last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9); | |
620125f2 SL |
5446 | |
5447 | bi->bi_next = NULL; | |
49728050 | 5448 | md_write_start(mddev, bi); |
620125f2 SL |
5449 | |
5450 | stripe_sectors = conf->chunk_sectors * | |
5451 | (conf->raid_disks - conf->max_degraded); | |
5452 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, | |
5453 | stripe_sectors); | |
5454 | sector_div(last_sector, stripe_sectors); | |
5455 | ||
5456 | logical_sector *= conf->chunk_sectors; | |
5457 | last_sector *= conf->chunk_sectors; | |
5458 | ||
5459 | for (; logical_sector < last_sector; | |
5460 | logical_sector += STRIPE_SECTORS) { | |
5461 | DEFINE_WAIT(w); | |
5462 | int d; | |
5463 | again: | |
6d036f7d | 5464 | sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0); |
620125f2 SL |
5465 | prepare_to_wait(&conf->wait_for_overlap, &w, |
5466 | TASK_UNINTERRUPTIBLE); | |
f8dfcffd N |
5467 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
5468 | if (test_bit(STRIPE_SYNCING, &sh->state)) { | |
6d036f7d | 5469 | raid5_release_stripe(sh); |
f8dfcffd N |
5470 | schedule(); |
5471 | goto again; | |
5472 | } | |
5473 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); | |
620125f2 SL |
5474 | spin_lock_irq(&sh->stripe_lock); |
5475 | for (d = 0; d < conf->raid_disks; d++) { | |
5476 | if (d == sh->pd_idx || d == sh->qd_idx) | |
5477 | continue; | |
5478 | if (sh->dev[d].towrite || sh->dev[d].toread) { | |
5479 | set_bit(R5_Overlap, &sh->dev[d].flags); | |
5480 | spin_unlock_irq(&sh->stripe_lock); | |
6d036f7d | 5481 | raid5_release_stripe(sh); |
620125f2 SL |
5482 | schedule(); |
5483 | goto again; | |
5484 | } | |
5485 | } | |
f8dfcffd | 5486 | set_bit(STRIPE_DISCARD, &sh->state); |
620125f2 | 5487 | finish_wait(&conf->wait_for_overlap, &w); |
7a87f434 | 5488 | sh->overwrite_disks = 0; |
620125f2 SL |
5489 | for (d = 0; d < conf->raid_disks; d++) { |
5490 | if (d == sh->pd_idx || d == sh->qd_idx) | |
5491 | continue; | |
5492 | sh->dev[d].towrite = bi; | |
5493 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); | |
016c76ac | 5494 | bio_inc_remaining(bi); |
49728050 | 5495 | md_write_inc(mddev, bi); |
7a87f434 | 5496 | sh->overwrite_disks++; |
620125f2 SL |
5497 | } |
5498 | spin_unlock_irq(&sh->stripe_lock); | |
5499 | if (conf->mddev->bitmap) { | |
5500 | for (d = 0; | |
5501 | d < conf->raid_disks - conf->max_degraded; | |
5502 | d++) | |
5503 | bitmap_startwrite(mddev->bitmap, | |
5504 | sh->sector, | |
5505 | STRIPE_SECTORS, | |
5506 | 0); | |
5507 | sh->bm_seq = conf->seq_flush + 1; | |
5508 | set_bit(STRIPE_BIT_DELAY, &sh->state); | |
5509 | } | |
5510 | ||
5511 | set_bit(STRIPE_HANDLE, &sh->state); | |
5512 | clear_bit(STRIPE_DELAYED, &sh->state); | |
5513 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
5514 | atomic_inc(&conf->preread_active_stripes); | |
5515 | release_stripe_plug(mddev, sh); | |
5516 | } | |
5517 | ||
49728050 | 5518 | md_write_end(mddev); |
016c76ac | 5519 | bio_endio(bi); |
620125f2 SL |
5520 | } |
5521 | ||
849674e4 | 5522 | static void raid5_make_request(struct mddev *mddev, struct bio * bi) |
1da177e4 | 5523 | { |
d1688a6d | 5524 | struct r5conf *conf = mddev->private; |
911d4ee8 | 5525 | int dd_idx; |
1da177e4 LT |
5526 | sector_t new_sector; |
5527 | sector_t logical_sector, last_sector; | |
5528 | struct stripe_head *sh; | |
a362357b | 5529 | const int rw = bio_data_dir(bi); |
27c0f68f SL |
5530 | DEFINE_WAIT(w); |
5531 | bool do_prepare; | |
3bddb7f8 | 5532 | bool do_flush = false; |
1da177e4 | 5533 | |
1eff9d32 | 5534 | if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { |
828cbe98 SL |
5535 | int ret = r5l_handle_flush_request(conf->log, bi); |
5536 | ||
5537 | if (ret == 0) | |
5538 | return; | |
5539 | if (ret == -ENODEV) { | |
5540 | md_flush_request(mddev, bi); | |
5541 | return; | |
5542 | } | |
5543 | /* ret == -EAGAIN, fallback */ | |
3bddb7f8 SL |
5544 | /* |
5545 | * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH, | |
5546 | * we need to flush journal device | |
5547 | */ | |
5548 | do_flush = bi->bi_opf & REQ_PREFLUSH; | |
e5dcdd80 N |
5549 | } |
5550 | ||
9ffc8f7c EM |
5551 | /* |
5552 | * If array is degraded, better not do chunk aligned read because | |
5553 | * later we might have to read it again in order to reconstruct | |
5554 | * data on failed drives. | |
5555 | */ | |
5556 | if (rw == READ && mddev->degraded == 0 && | |
7ef6b12a ML |
5557 | mddev->reshape_position == MaxSector) { |
5558 | bi = chunk_aligned_read(mddev, bi); | |
5559 | if (!bi) | |
5560 | return; | |
5561 | } | |
52488615 | 5562 | |
796a5cf0 | 5563 | if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { |
620125f2 SL |
5564 | make_discard_request(mddev, bi); |
5565 | return; | |
5566 | } | |
5567 | ||
4f024f37 | 5568 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
f73a1c7d | 5569 | last_sector = bio_end_sector(bi); |
1da177e4 | 5570 | bi->bi_next = NULL; |
49728050 | 5571 | md_write_start(mddev, bi); |
06d91a5f | 5572 | |
27c0f68f | 5573 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
1da177e4 | 5574 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
b5663ba4 | 5575 | int previous; |
c46501b2 | 5576 | int seq; |
b578d55f | 5577 | |
27c0f68f | 5578 | do_prepare = false; |
7ecaa1e6 | 5579 | retry: |
c46501b2 | 5580 | seq = read_seqcount_begin(&conf->gen_lock); |
b5663ba4 | 5581 | previous = 0; |
27c0f68f SL |
5582 | if (do_prepare) |
5583 | prepare_to_wait(&conf->wait_for_overlap, &w, | |
5584 | TASK_UNINTERRUPTIBLE); | |
b0f9ec04 | 5585 | if (unlikely(conf->reshape_progress != MaxSector)) { |
fef9c61f | 5586 | /* spinlock is needed as reshape_progress may be |
df8e7f76 N |
5587 | * 64bit on a 32bit platform, and so it might be |
5588 | * possible to see a half-updated value | |
aeb878b0 | 5589 | * Of course reshape_progress could change after |
df8e7f76 N |
5590 | * the lock is dropped, so once we get a reference |
5591 | * to the stripe that we think it is, we will have | |
5592 | * to check again. | |
5593 | */ | |
7ecaa1e6 | 5594 | spin_lock_irq(&conf->device_lock); |
2c810cdd | 5595 | if (mddev->reshape_backwards |
fef9c61f N |
5596 | ? logical_sector < conf->reshape_progress |
5597 | : logical_sector >= conf->reshape_progress) { | |
b5663ba4 N |
5598 | previous = 1; |
5599 | } else { | |
2c810cdd | 5600 | if (mddev->reshape_backwards |
fef9c61f N |
5601 | ? logical_sector < conf->reshape_safe |
5602 | : logical_sector >= conf->reshape_safe) { | |
b578d55f N |
5603 | spin_unlock_irq(&conf->device_lock); |
5604 | schedule(); | |
27c0f68f | 5605 | do_prepare = true; |
b578d55f N |
5606 | goto retry; |
5607 | } | |
5608 | } | |
7ecaa1e6 N |
5609 | spin_unlock_irq(&conf->device_lock); |
5610 | } | |
16a53ecc | 5611 | |
112bf897 N |
5612 | new_sector = raid5_compute_sector(conf, logical_sector, |
5613 | previous, | |
911d4ee8 | 5614 | &dd_idx, NULL); |
849674e4 | 5615 | pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n", |
c46501b2 | 5616 | (unsigned long long)new_sector, |
1da177e4 LT |
5617 | (unsigned long long)logical_sector); |
5618 | ||
6d036f7d | 5619 | sh = raid5_get_active_stripe(conf, new_sector, previous, |
1eff9d32 | 5620 | (bi->bi_opf & REQ_RAHEAD), 0); |
1da177e4 | 5621 | if (sh) { |
b0f9ec04 | 5622 | if (unlikely(previous)) { |
7ecaa1e6 | 5623 | /* expansion might have moved on while waiting for a |
df8e7f76 N |
5624 | * stripe, so we must do the range check again. |
5625 | * Expansion could still move past after this | |
5626 | * test, but as we are holding a reference to | |
5627 | * 'sh', we know that if that happens, | |
5628 | * STRIPE_EXPANDING will get set and the expansion | |
5629 | * won't proceed until we finish with the stripe. | |
7ecaa1e6 N |
5630 | */ |
5631 | int must_retry = 0; | |
5632 | spin_lock_irq(&conf->device_lock); | |
2c810cdd | 5633 | if (mddev->reshape_backwards |
b0f9ec04 N |
5634 | ? logical_sector >= conf->reshape_progress |
5635 | : logical_sector < conf->reshape_progress) | |
7ecaa1e6 N |
5636 | /* mismatch, need to try again */ |
5637 | must_retry = 1; | |
5638 | spin_unlock_irq(&conf->device_lock); | |
5639 | if (must_retry) { | |
6d036f7d | 5640 | raid5_release_stripe(sh); |
7a3ab908 | 5641 | schedule(); |
27c0f68f | 5642 | do_prepare = true; |
7ecaa1e6 N |
5643 | goto retry; |
5644 | } | |
5645 | } | |
c46501b2 N |
5646 | if (read_seqcount_retry(&conf->gen_lock, seq)) { |
5647 | /* Might have got the wrong stripe_head | |
5648 | * by accident | |
5649 | */ | |
6d036f7d | 5650 | raid5_release_stripe(sh); |
c46501b2 N |
5651 | goto retry; |
5652 | } | |
e62e58a5 | 5653 | |
ffd96e35 | 5654 | if (rw == WRITE && |
a5c308d4 | 5655 | logical_sector >= mddev->suspend_lo && |
e464eafd | 5656 | logical_sector < mddev->suspend_hi) { |
6d036f7d | 5657 | raid5_release_stripe(sh); |
e62e58a5 N |
5658 | /* As the suspend_* range is controlled by |
5659 | * userspace, we want an interruptible | |
5660 | * wait. | |
5661 | */ | |
5662 | flush_signals(current); | |
5663 | prepare_to_wait(&conf->wait_for_overlap, | |
5664 | &w, TASK_INTERRUPTIBLE); | |
5665 | if (logical_sector >= mddev->suspend_lo && | |
27c0f68f | 5666 | logical_sector < mddev->suspend_hi) { |
e62e58a5 | 5667 | schedule(); |
27c0f68f SL |
5668 | do_prepare = true; |
5669 | } | |
e464eafd N |
5670 | goto retry; |
5671 | } | |
7ecaa1e6 N |
5672 | |
5673 | if (test_bit(STRIPE_EXPANDING, &sh->state) || | |
da41ba65 | 5674 | !add_stripe_bio(sh, bi, dd_idx, rw, previous)) { |
7ecaa1e6 N |
5675 | /* Stripe is busy expanding or |
5676 | * add failed due to overlap. Flush everything | |
1da177e4 LT |
5677 | * and wait a while |
5678 | */ | |
482c0834 | 5679 | md_wakeup_thread(mddev->thread); |
6d036f7d | 5680 | raid5_release_stripe(sh); |
1da177e4 | 5681 | schedule(); |
27c0f68f | 5682 | do_prepare = true; |
1da177e4 LT |
5683 | goto retry; |
5684 | } | |
3bddb7f8 SL |
5685 | if (do_flush) { |
5686 | set_bit(STRIPE_R5C_PREFLUSH, &sh->state); | |
5687 | /* we only need flush for one stripe */ | |
5688 | do_flush = false; | |
5689 | } | |
5690 | ||
6ed3003c N |
5691 | set_bit(STRIPE_HANDLE, &sh->state); |
5692 | clear_bit(STRIPE_DELAYED, &sh->state); | |
59fc630b | 5693 | if ((!sh->batch_head || sh == sh->batch_head) && |
1eff9d32 | 5694 | (bi->bi_opf & REQ_SYNC) && |
729a1866 N |
5695 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
5696 | atomic_inc(&conf->preread_active_stripes); | |
8811b596 | 5697 | release_stripe_plug(mddev, sh); |
1da177e4 LT |
5698 | } else { |
5699 | /* cannot get stripe for read-ahead, just give-up */ | |
4246a0b6 | 5700 | bi->bi_error = -EIO; |
1da177e4 LT |
5701 | break; |
5702 | } | |
1da177e4 | 5703 | } |
27c0f68f | 5704 | finish_wait(&conf->wait_for_overlap, &w); |
7c13edc8 | 5705 | |
49728050 N |
5706 | if (rw == WRITE) |
5707 | md_write_end(mddev); | |
016c76ac | 5708 | bio_endio(bi); |
1da177e4 LT |
5709 | } |
5710 | ||
fd01b88c | 5711 | static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks); |
b522adcd | 5712 | |
fd01b88c | 5713 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped) |
1da177e4 | 5714 | { |
52c03291 N |
5715 | /* reshaping is quite different to recovery/resync so it is |
5716 | * handled quite separately ... here. | |
5717 | * | |
5718 | * On each call to sync_request, we gather one chunk worth of | |
5719 | * destination stripes and flag them as expanding. | |
5720 | * Then we find all the source stripes and request reads. | |
5721 | * As the reads complete, handle_stripe will copy the data | |
5722 | * into the destination stripe and release that stripe. | |
5723 | */ | |
d1688a6d | 5724 | struct r5conf *conf = mddev->private; |
1da177e4 | 5725 | struct stripe_head *sh; |
ccfcc3c1 | 5726 | sector_t first_sector, last_sector; |
f416885e N |
5727 | int raid_disks = conf->previous_raid_disks; |
5728 | int data_disks = raid_disks - conf->max_degraded; | |
5729 | int new_data_disks = conf->raid_disks - conf->max_degraded; | |
52c03291 N |
5730 | int i; |
5731 | int dd_idx; | |
c8f517c4 | 5732 | sector_t writepos, readpos, safepos; |
ec32a2bd | 5733 | sector_t stripe_addr; |
7a661381 | 5734 | int reshape_sectors; |
ab69ae12 | 5735 | struct list_head stripes; |
92140480 | 5736 | sector_t retn; |
52c03291 | 5737 | |
fef9c61f N |
5738 | if (sector_nr == 0) { |
5739 | /* If restarting in the middle, skip the initial sectors */ | |
2c810cdd | 5740 | if (mddev->reshape_backwards && |
fef9c61f N |
5741 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
5742 | sector_nr = raid5_size(mddev, 0, 0) | |
5743 | - conf->reshape_progress; | |
6cbd8148 N |
5744 | } else if (mddev->reshape_backwards && |
5745 | conf->reshape_progress == MaxSector) { | |
5746 | /* shouldn't happen, but just in case, finish up.*/ | |
5747 | sector_nr = MaxSector; | |
2c810cdd | 5748 | } else if (!mddev->reshape_backwards && |
fef9c61f N |
5749 | conf->reshape_progress > 0) |
5750 | sector_nr = conf->reshape_progress; | |
f416885e | 5751 | sector_div(sector_nr, new_data_disks); |
fef9c61f | 5752 | if (sector_nr) { |
8dee7211 N |
5753 | mddev->curr_resync_completed = sector_nr; |
5754 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); | |
fef9c61f | 5755 | *skipped = 1; |
92140480 N |
5756 | retn = sector_nr; |
5757 | goto finish; | |
fef9c61f | 5758 | } |
52c03291 N |
5759 | } |
5760 | ||
7a661381 N |
5761 | /* We need to process a full chunk at a time. |
5762 | * If old and new chunk sizes differ, we need to process the | |
5763 | * largest of these | |
5764 | */ | |
3cb5edf4 N |
5765 | |
5766 | reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); | |
7a661381 | 5767 | |
b5254dd5 N |
5768 | /* We update the metadata at least every 10 seconds, or when |
5769 | * the data about to be copied would over-write the source of | |
5770 | * the data at the front of the range. i.e. one new_stripe | |
5771 | * along from reshape_progress new_maps to after where | |
5772 | * reshape_safe old_maps to | |
52c03291 | 5773 | */ |
fef9c61f | 5774 | writepos = conf->reshape_progress; |
f416885e | 5775 | sector_div(writepos, new_data_disks); |
c8f517c4 N |
5776 | readpos = conf->reshape_progress; |
5777 | sector_div(readpos, data_disks); | |
fef9c61f | 5778 | safepos = conf->reshape_safe; |
f416885e | 5779 | sector_div(safepos, data_disks); |
2c810cdd | 5780 | if (mddev->reshape_backwards) { |
c74c0d76 N |
5781 | BUG_ON(writepos < reshape_sectors); |
5782 | writepos -= reshape_sectors; | |
c8f517c4 | 5783 | readpos += reshape_sectors; |
7a661381 | 5784 | safepos += reshape_sectors; |
fef9c61f | 5785 | } else { |
7a661381 | 5786 | writepos += reshape_sectors; |
c74c0d76 N |
5787 | /* readpos and safepos are worst-case calculations. |
5788 | * A negative number is overly pessimistic, and causes | |
5789 | * obvious problems for unsigned storage. So clip to 0. | |
5790 | */ | |
ed37d83e N |
5791 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
5792 | safepos -= min_t(sector_t, reshape_sectors, safepos); | |
fef9c61f | 5793 | } |
52c03291 | 5794 | |
b5254dd5 N |
5795 | /* Having calculated the 'writepos' possibly use it |
5796 | * to set 'stripe_addr' which is where we will write to. | |
5797 | */ | |
5798 | if (mddev->reshape_backwards) { | |
5799 | BUG_ON(conf->reshape_progress == 0); | |
5800 | stripe_addr = writepos; | |
5801 | BUG_ON((mddev->dev_sectors & | |
5802 | ~((sector_t)reshape_sectors - 1)) | |
5803 | - reshape_sectors - stripe_addr | |
5804 | != sector_nr); | |
5805 | } else { | |
5806 | BUG_ON(writepos != sector_nr + reshape_sectors); | |
5807 | stripe_addr = sector_nr; | |
5808 | } | |
5809 | ||
c8f517c4 N |
5810 | /* 'writepos' is the most advanced device address we might write. |
5811 | * 'readpos' is the least advanced device address we might read. | |
5812 | * 'safepos' is the least address recorded in the metadata as having | |
5813 | * been reshaped. | |
b5254dd5 N |
5814 | * If there is a min_offset_diff, these are adjusted either by |
5815 | * increasing the safepos/readpos if diff is negative, or | |
5816 | * increasing writepos if diff is positive. | |
5817 | * If 'readpos' is then behind 'writepos', there is no way that we can | |
c8f517c4 N |
5818 | * ensure safety in the face of a crash - that must be done by userspace |
5819 | * making a backup of the data. So in that case there is no particular | |
5820 | * rush to update metadata. | |
5821 | * Otherwise if 'safepos' is behind 'writepos', then we really need to | |
5822 | * update the metadata to advance 'safepos' to match 'readpos' so that | |
5823 | * we can be safe in the event of a crash. | |
5824 | * So we insist on updating metadata if safepos is behind writepos and | |
5825 | * readpos is beyond writepos. | |
5826 | * In any case, update the metadata every 10 seconds. | |
5827 | * Maybe that number should be configurable, but I'm not sure it is | |
5828 | * worth it.... maybe it could be a multiple of safemode_delay??? | |
5829 | */ | |
b5254dd5 N |
5830 | if (conf->min_offset_diff < 0) { |
5831 | safepos += -conf->min_offset_diff; | |
5832 | readpos += -conf->min_offset_diff; | |
5833 | } else | |
5834 | writepos += conf->min_offset_diff; | |
5835 | ||
2c810cdd | 5836 | if ((mddev->reshape_backwards |
c8f517c4 N |
5837 | ? (safepos > writepos && readpos < writepos) |
5838 | : (safepos < writepos && readpos > writepos)) || | |
5839 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { | |
52c03291 N |
5840 | /* Cannot proceed until we've updated the superblock... */ |
5841 | wait_event(conf->wait_for_overlap, | |
c91abf5a N |
5842 | atomic_read(&conf->reshape_stripes)==0 |
5843 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); | |
5844 | if (atomic_read(&conf->reshape_stripes) != 0) | |
5845 | return 0; | |
fef9c61f | 5846 | mddev->reshape_position = conf->reshape_progress; |
75d3da43 | 5847 | mddev->curr_resync_completed = sector_nr; |
c8f517c4 | 5848 | conf->reshape_checkpoint = jiffies; |
2953079c | 5849 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
52c03291 | 5850 | md_wakeup_thread(mddev->thread); |
2953079c | 5851 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
c91abf5a N |
5852 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
5853 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
5854 | return 0; | |
52c03291 | 5855 | spin_lock_irq(&conf->device_lock); |
fef9c61f | 5856 | conf->reshape_safe = mddev->reshape_position; |
52c03291 N |
5857 | spin_unlock_irq(&conf->device_lock); |
5858 | wake_up(&conf->wait_for_overlap); | |
acb180b0 | 5859 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
52c03291 N |
5860 | } |
5861 | ||
ab69ae12 | 5862 | INIT_LIST_HEAD(&stripes); |
7a661381 | 5863 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
52c03291 | 5864 | int j; |
a9f326eb | 5865 | int skipped_disk = 0; |
6d036f7d | 5866 | sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
52c03291 N |
5867 | set_bit(STRIPE_EXPANDING, &sh->state); |
5868 | atomic_inc(&conf->reshape_stripes); | |
5869 | /* If any of this stripe is beyond the end of the old | |
5870 | * array, then we need to zero those blocks | |
5871 | */ | |
5872 | for (j=sh->disks; j--;) { | |
5873 | sector_t s; | |
5874 | if (j == sh->pd_idx) | |
5875 | continue; | |
f416885e | 5876 | if (conf->level == 6 && |
d0dabf7e | 5877 | j == sh->qd_idx) |
f416885e | 5878 | continue; |
6d036f7d | 5879 | s = raid5_compute_blocknr(sh, j, 0); |
b522adcd | 5880 | if (s < raid5_size(mddev, 0, 0)) { |
a9f326eb | 5881 | skipped_disk = 1; |
52c03291 N |
5882 | continue; |
5883 | } | |
5884 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); | |
5885 | set_bit(R5_Expanded, &sh->dev[j].flags); | |
5886 | set_bit(R5_UPTODATE, &sh->dev[j].flags); | |
5887 | } | |
a9f326eb | 5888 | if (!skipped_disk) { |
52c03291 N |
5889 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
5890 | set_bit(STRIPE_HANDLE, &sh->state); | |
5891 | } | |
ab69ae12 | 5892 | list_add(&sh->lru, &stripes); |
52c03291 N |
5893 | } |
5894 | spin_lock_irq(&conf->device_lock); | |
2c810cdd | 5895 | if (mddev->reshape_backwards) |
7a661381 | 5896 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
fef9c61f | 5897 | else |
7a661381 | 5898 | conf->reshape_progress += reshape_sectors * new_data_disks; |
52c03291 N |
5899 | spin_unlock_irq(&conf->device_lock); |
5900 | /* Ok, those stripe are ready. We can start scheduling | |
5901 | * reads on the source stripes. | |
5902 | * The source stripes are determined by mapping the first and last | |
5903 | * block on the destination stripes. | |
5904 | */ | |
52c03291 | 5905 | first_sector = |
ec32a2bd | 5906 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
911d4ee8 | 5907 | 1, &dd_idx, NULL); |
52c03291 | 5908 | last_sector = |
0e6e0271 | 5909 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
09c9e5fa | 5910 | * new_data_disks - 1), |
911d4ee8 | 5911 | 1, &dd_idx, NULL); |
58c0fed4 AN |
5912 | if (last_sector >= mddev->dev_sectors) |
5913 | last_sector = mddev->dev_sectors - 1; | |
52c03291 | 5914 | while (first_sector <= last_sector) { |
6d036f7d | 5915 | sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1); |
52c03291 N |
5916 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
5917 | set_bit(STRIPE_HANDLE, &sh->state); | |
6d036f7d | 5918 | raid5_release_stripe(sh); |
52c03291 N |
5919 | first_sector += STRIPE_SECTORS; |
5920 | } | |
ab69ae12 N |
5921 | /* Now that the sources are clearly marked, we can release |
5922 | * the destination stripes | |
5923 | */ | |
5924 | while (!list_empty(&stripes)) { | |
5925 | sh = list_entry(stripes.next, struct stripe_head, lru); | |
5926 | list_del_init(&sh->lru); | |
6d036f7d | 5927 | raid5_release_stripe(sh); |
ab69ae12 | 5928 | } |
c6207277 N |
5929 | /* If this takes us to the resync_max point where we have to pause, |
5930 | * then we need to write out the superblock. | |
5931 | */ | |
7a661381 | 5932 | sector_nr += reshape_sectors; |
92140480 N |
5933 | retn = reshape_sectors; |
5934 | finish: | |
c5e19d90 N |
5935 | if (mddev->curr_resync_completed > mddev->resync_max || |
5936 | (sector_nr - mddev->curr_resync_completed) * 2 | |
c03f6a19 | 5937 | >= mddev->resync_max - mddev->curr_resync_completed) { |
c6207277 N |
5938 | /* Cannot proceed until we've updated the superblock... */ |
5939 | wait_event(conf->wait_for_overlap, | |
c91abf5a N |
5940 | atomic_read(&conf->reshape_stripes) == 0 |
5941 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); | |
5942 | if (atomic_read(&conf->reshape_stripes) != 0) | |
5943 | goto ret; | |
fef9c61f | 5944 | mddev->reshape_position = conf->reshape_progress; |
75d3da43 | 5945 | mddev->curr_resync_completed = sector_nr; |
c8f517c4 | 5946 | conf->reshape_checkpoint = jiffies; |
2953079c | 5947 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
c6207277 N |
5948 | md_wakeup_thread(mddev->thread); |
5949 | wait_event(mddev->sb_wait, | |
2953079c | 5950 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) |
c91abf5a N |
5951 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
5952 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) | |
5953 | goto ret; | |
c6207277 | 5954 | spin_lock_irq(&conf->device_lock); |
fef9c61f | 5955 | conf->reshape_safe = mddev->reshape_position; |
c6207277 N |
5956 | spin_unlock_irq(&conf->device_lock); |
5957 | wake_up(&conf->wait_for_overlap); | |
acb180b0 | 5958 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
c6207277 | 5959 | } |
c91abf5a | 5960 | ret: |
92140480 | 5961 | return retn; |
52c03291 N |
5962 | } |
5963 | ||
849674e4 SL |
5964 | static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, |
5965 | int *skipped) | |
52c03291 | 5966 | { |
d1688a6d | 5967 | struct r5conf *conf = mddev->private; |
52c03291 | 5968 | struct stripe_head *sh; |
58c0fed4 | 5969 | sector_t max_sector = mddev->dev_sectors; |
57dab0bd | 5970 | sector_t sync_blocks; |
16a53ecc N |
5971 | int still_degraded = 0; |
5972 | int i; | |
1da177e4 | 5973 | |
72626685 | 5974 | if (sector_nr >= max_sector) { |
1da177e4 | 5975 | /* just being told to finish up .. nothing much to do */ |
cea9c228 | 5976 | |
29269553 N |
5977 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
5978 | end_reshape(conf); | |
5979 | return 0; | |
5980 | } | |
72626685 N |
5981 | |
5982 | if (mddev->curr_resync < max_sector) /* aborted */ | |
5983 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
5984 | &sync_blocks, 1); | |
16a53ecc | 5985 | else /* completed sync */ |
72626685 N |
5986 | conf->fullsync = 0; |
5987 | bitmap_close_sync(mddev->bitmap); | |
5988 | ||
1da177e4 LT |
5989 | return 0; |
5990 | } | |
ccfcc3c1 | 5991 | |
64bd660b N |
5992 | /* Allow raid5_quiesce to complete */ |
5993 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); | |
5994 | ||
52c03291 N |
5995 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
5996 | return reshape_request(mddev, sector_nr, skipped); | |
f6705578 | 5997 | |
c6207277 N |
5998 | /* No need to check resync_max as we never do more than one |
5999 | * stripe, and as resync_max will always be on a chunk boundary, | |
6000 | * if the check in md_do_sync didn't fire, there is no chance | |
6001 | * of overstepping resync_max here | |
6002 | */ | |
6003 | ||
16a53ecc | 6004 | /* if there is too many failed drives and we are trying |
1da177e4 LT |
6005 | * to resync, then assert that we are finished, because there is |
6006 | * nothing we can do. | |
6007 | */ | |
3285edf1 | 6008 | if (mddev->degraded >= conf->max_degraded && |
16a53ecc | 6009 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
58c0fed4 | 6010 | sector_t rv = mddev->dev_sectors - sector_nr; |
57afd89f | 6011 | *skipped = 1; |
1da177e4 LT |
6012 | return rv; |
6013 | } | |
6f608040 | 6014 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
6015 | !conf->fullsync && | |
6016 | !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && | |
6017 | sync_blocks >= STRIPE_SECTORS) { | |
72626685 N |
6018 | /* we can skip this block, and probably more */ |
6019 | sync_blocks /= STRIPE_SECTORS; | |
6020 | *skipped = 1; | |
6021 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ | |
6022 | } | |
1da177e4 | 6023 | |
c40f341f | 6024 | bitmap_cond_end_sync(mddev->bitmap, sector_nr, false); |
b47490c9 | 6025 | |
6d036f7d | 6026 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0); |
1da177e4 | 6027 | if (sh == NULL) { |
6d036f7d | 6028 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0); |
1da177e4 | 6029 | /* make sure we don't swamp the stripe cache if someone else |
16a53ecc | 6030 | * is trying to get access |
1da177e4 | 6031 | */ |
66c006a5 | 6032 | schedule_timeout_uninterruptible(1); |
1da177e4 | 6033 | } |
16a53ecc | 6034 | /* Need to check if array will still be degraded after recovery/resync |
16d9cfab EM |
6035 | * Note in case of > 1 drive failures it's possible we're rebuilding |
6036 | * one drive while leaving another faulty drive in array. | |
16a53ecc | 6037 | */ |
16d9cfab EM |
6038 | rcu_read_lock(); |
6039 | for (i = 0; i < conf->raid_disks; i++) { | |
6040 | struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev); | |
6041 | ||
6042 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) | |
16a53ecc | 6043 | still_degraded = 1; |
16d9cfab EM |
6044 | } |
6045 | rcu_read_unlock(); | |
16a53ecc N |
6046 | |
6047 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); | |
6048 | ||
83206d66 | 6049 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
053f5b65 | 6050 | set_bit(STRIPE_HANDLE, &sh->state); |
1da177e4 | 6051 | |
6d036f7d | 6052 | raid5_release_stripe(sh); |
1da177e4 LT |
6053 | |
6054 | return STRIPE_SECTORS; | |
6055 | } | |
6056 | ||
0472a42b N |
6057 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio, |
6058 | unsigned int offset) | |
46031f9a RBJ |
6059 | { |
6060 | /* We may not be able to submit a whole bio at once as there | |
6061 | * may not be enough stripe_heads available. | |
6062 | * We cannot pre-allocate enough stripe_heads as we may need | |
6063 | * more than exist in the cache (if we allow ever large chunks). | |
6064 | * So we do one stripe head at a time and record in | |
6065 | * ->bi_hw_segments how many have been done. | |
6066 | * | |
6067 | * We *know* that this entire raid_bio is in one chunk, so | |
6068 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. | |
6069 | */ | |
6070 | struct stripe_head *sh; | |
911d4ee8 | 6071 | int dd_idx; |
46031f9a RBJ |
6072 | sector_t sector, logical_sector, last_sector; |
6073 | int scnt = 0; | |
46031f9a RBJ |
6074 | int handled = 0; |
6075 | ||
4f024f37 KO |
6076 | logical_sector = raid_bio->bi_iter.bi_sector & |
6077 | ~((sector_t)STRIPE_SECTORS-1); | |
112bf897 | 6078 | sector = raid5_compute_sector(conf, logical_sector, |
911d4ee8 | 6079 | 0, &dd_idx, NULL); |
f73a1c7d | 6080 | last_sector = bio_end_sector(raid_bio); |
46031f9a RBJ |
6081 | |
6082 | for (; logical_sector < last_sector; | |
387bb173 NB |
6083 | logical_sector += STRIPE_SECTORS, |
6084 | sector += STRIPE_SECTORS, | |
6085 | scnt++) { | |
46031f9a | 6086 | |
0472a42b | 6087 | if (scnt < offset) |
46031f9a RBJ |
6088 | /* already done this stripe */ |
6089 | continue; | |
6090 | ||
6d036f7d | 6091 | sh = raid5_get_active_stripe(conf, sector, 0, 1, 1); |
46031f9a RBJ |
6092 | |
6093 | if (!sh) { | |
6094 | /* failed to get a stripe - must wait */ | |
46031f9a | 6095 | conf->retry_read_aligned = raid_bio; |
0472a42b | 6096 | conf->retry_read_offset = scnt; |
46031f9a RBJ |
6097 | return handled; |
6098 | } | |
6099 | ||
da41ba65 | 6100 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { |
6d036f7d | 6101 | raid5_release_stripe(sh); |
387bb173 | 6102 | conf->retry_read_aligned = raid_bio; |
0472a42b | 6103 | conf->retry_read_offset = scnt; |
387bb173 NB |
6104 | return handled; |
6105 | } | |
6106 | ||
3f9e7c14 | 6107 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
36d1c647 | 6108 | handle_stripe(sh); |
6d036f7d | 6109 | raid5_release_stripe(sh); |
46031f9a RBJ |
6110 | handled++; |
6111 | } | |
016c76ac N |
6112 | |
6113 | bio_endio(raid_bio); | |
6114 | ||
46031f9a | 6115 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
b1b46486 | 6116 | wake_up(&conf->wait_for_quiescent); |
46031f9a RBJ |
6117 | return handled; |
6118 | } | |
6119 | ||
bfc90cb0 | 6120 | static int handle_active_stripes(struct r5conf *conf, int group, |
566c09c5 SL |
6121 | struct r5worker *worker, |
6122 | struct list_head *temp_inactive_list) | |
46a06401 SL |
6123 | { |
6124 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; | |
566c09c5 SL |
6125 | int i, batch_size = 0, hash; |
6126 | bool release_inactive = false; | |
46a06401 SL |
6127 | |
6128 | while (batch_size < MAX_STRIPE_BATCH && | |
851c30c9 | 6129 | (sh = __get_priority_stripe(conf, group)) != NULL) |
46a06401 SL |
6130 | batch[batch_size++] = sh; |
6131 | ||
566c09c5 SL |
6132 | if (batch_size == 0) { |
6133 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) | |
6134 | if (!list_empty(temp_inactive_list + i)) | |
6135 | break; | |
a8c34f91 SL |
6136 | if (i == NR_STRIPE_HASH_LOCKS) { |
6137 | spin_unlock_irq(&conf->device_lock); | |
6138 | r5l_flush_stripe_to_raid(conf->log); | |
6139 | spin_lock_irq(&conf->device_lock); | |
566c09c5 | 6140 | return batch_size; |
a8c34f91 | 6141 | } |
566c09c5 SL |
6142 | release_inactive = true; |
6143 | } | |
46a06401 SL |
6144 | spin_unlock_irq(&conf->device_lock); |
6145 | ||
566c09c5 SL |
6146 | release_inactive_stripe_list(conf, temp_inactive_list, |
6147 | NR_STRIPE_HASH_LOCKS); | |
6148 | ||
a8c34f91 | 6149 | r5l_flush_stripe_to_raid(conf->log); |
566c09c5 SL |
6150 | if (release_inactive) { |
6151 | spin_lock_irq(&conf->device_lock); | |
6152 | return 0; | |
6153 | } | |
6154 | ||
46a06401 SL |
6155 | for (i = 0; i < batch_size; i++) |
6156 | handle_stripe(batch[i]); | |
ff875738 | 6157 | log_write_stripe_run(conf); |
46a06401 SL |
6158 | |
6159 | cond_resched(); | |
6160 | ||
6161 | spin_lock_irq(&conf->device_lock); | |
566c09c5 SL |
6162 | for (i = 0; i < batch_size; i++) { |
6163 | hash = batch[i]->hash_lock_index; | |
6164 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); | |
6165 | } | |
46a06401 SL |
6166 | return batch_size; |
6167 | } | |
46031f9a | 6168 | |
851c30c9 SL |
6169 | static void raid5_do_work(struct work_struct *work) |
6170 | { | |
6171 | struct r5worker *worker = container_of(work, struct r5worker, work); | |
6172 | struct r5worker_group *group = worker->group; | |
6173 | struct r5conf *conf = group->conf; | |
16d997b7 | 6174 | struct mddev *mddev = conf->mddev; |
851c30c9 SL |
6175 | int group_id = group - conf->worker_groups; |
6176 | int handled; | |
6177 | struct blk_plug plug; | |
6178 | ||
6179 | pr_debug("+++ raid5worker active\n"); | |
6180 | ||
6181 | blk_start_plug(&plug); | |
6182 | handled = 0; | |
6183 | spin_lock_irq(&conf->device_lock); | |
6184 | while (1) { | |
6185 | int batch_size, released; | |
6186 | ||
566c09c5 | 6187 | released = release_stripe_list(conf, worker->temp_inactive_list); |
851c30c9 | 6188 | |
566c09c5 SL |
6189 | batch_size = handle_active_stripes(conf, group_id, worker, |
6190 | worker->temp_inactive_list); | |
bfc90cb0 | 6191 | worker->working = false; |
851c30c9 SL |
6192 | if (!batch_size && !released) |
6193 | break; | |
6194 | handled += batch_size; | |
16d997b7 N |
6195 | wait_event_lock_irq(mddev->sb_wait, |
6196 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags), | |
6197 | conf->device_lock); | |
851c30c9 SL |
6198 | } |
6199 | pr_debug("%d stripes handled\n", handled); | |
6200 | ||
6201 | spin_unlock_irq(&conf->device_lock); | |
6202 | blk_finish_plug(&plug); | |
6203 | ||
6204 | pr_debug("--- raid5worker inactive\n"); | |
6205 | } | |
6206 | ||
1da177e4 LT |
6207 | /* |
6208 | * This is our raid5 kernel thread. | |
6209 | * | |
6210 | * We scan the hash table for stripes which can be handled now. | |
6211 | * During the scan, completed stripes are saved for us by the interrupt | |
6212 | * handler, so that they will not have to wait for our next wakeup. | |
6213 | */ | |
4ed8731d | 6214 | static void raid5d(struct md_thread *thread) |
1da177e4 | 6215 | { |
4ed8731d | 6216 | struct mddev *mddev = thread->mddev; |
d1688a6d | 6217 | struct r5conf *conf = mddev->private; |
1da177e4 | 6218 | int handled; |
e1dfa0a2 | 6219 | struct blk_plug plug; |
1da177e4 | 6220 | |
45b4233c | 6221 | pr_debug("+++ raid5d active\n"); |
1da177e4 LT |
6222 | |
6223 | md_check_recovery(mddev); | |
1da177e4 | 6224 | |
e1dfa0a2 | 6225 | blk_start_plug(&plug); |
1da177e4 LT |
6226 | handled = 0; |
6227 | spin_lock_irq(&conf->device_lock); | |
6228 | while (1) { | |
46031f9a | 6229 | struct bio *bio; |
773ca82f | 6230 | int batch_size, released; |
0472a42b | 6231 | unsigned int offset; |
773ca82f | 6232 | |
566c09c5 | 6233 | released = release_stripe_list(conf, conf->temp_inactive_list); |
edbe83ab N |
6234 | if (released) |
6235 | clear_bit(R5_DID_ALLOC, &conf->cache_state); | |
1da177e4 | 6236 | |
0021b7bc | 6237 | if ( |
7c13edc8 N |
6238 | !list_empty(&conf->bitmap_list)) { |
6239 | /* Now is a good time to flush some bitmap updates */ | |
6240 | conf->seq_flush++; | |
700e432d | 6241 | spin_unlock_irq(&conf->device_lock); |
72626685 | 6242 | bitmap_unplug(mddev->bitmap); |
700e432d | 6243 | spin_lock_irq(&conf->device_lock); |
7c13edc8 | 6244 | conf->seq_write = conf->seq_flush; |
566c09c5 | 6245 | activate_bit_delay(conf, conf->temp_inactive_list); |
72626685 | 6246 | } |
0021b7bc | 6247 | raid5_activate_delayed(conf); |
72626685 | 6248 | |
0472a42b | 6249 | while ((bio = remove_bio_from_retry(conf, &offset))) { |
46031f9a RBJ |
6250 | int ok; |
6251 | spin_unlock_irq(&conf->device_lock); | |
0472a42b | 6252 | ok = retry_aligned_read(conf, bio, offset); |
46031f9a RBJ |
6253 | spin_lock_irq(&conf->device_lock); |
6254 | if (!ok) | |
6255 | break; | |
6256 | handled++; | |
6257 | } | |
6258 | ||
566c09c5 SL |
6259 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
6260 | conf->temp_inactive_list); | |
773ca82f | 6261 | if (!batch_size && !released) |
1da177e4 | 6262 | break; |
46a06401 | 6263 | handled += batch_size; |
1da177e4 | 6264 | |
2953079c | 6265 | if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) { |
46a06401 | 6266 | spin_unlock_irq(&conf->device_lock); |
de393cde | 6267 | md_check_recovery(mddev); |
46a06401 SL |
6268 | spin_lock_irq(&conf->device_lock); |
6269 | } | |
1da177e4 | 6270 | } |
45b4233c | 6271 | pr_debug("%d stripes handled\n", handled); |
1da177e4 LT |
6272 | |
6273 | spin_unlock_irq(&conf->device_lock); | |
2d5b569b N |
6274 | if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && |
6275 | mutex_trylock(&conf->cache_size_mutex)) { | |
edbe83ab N |
6276 | grow_one_stripe(conf, __GFP_NOWARN); |
6277 | /* Set flag even if allocation failed. This helps | |
6278 | * slow down allocation requests when mem is short | |
6279 | */ | |
6280 | set_bit(R5_DID_ALLOC, &conf->cache_state); | |
2d5b569b | 6281 | mutex_unlock(&conf->cache_size_mutex); |
edbe83ab | 6282 | } |
1da177e4 | 6283 | |
765d704d SL |
6284 | flush_deferred_bios(conf); |
6285 | ||
0576b1c6 SL |
6286 | r5l_flush_stripe_to_raid(conf->log); |
6287 | ||
c9f21aaf | 6288 | async_tx_issue_pending_all(); |
e1dfa0a2 | 6289 | blk_finish_plug(&plug); |
1da177e4 | 6290 | |
45b4233c | 6291 | pr_debug("--- raid5d inactive\n"); |
1da177e4 LT |
6292 | } |
6293 | ||
3f294f4f | 6294 | static ssize_t |
fd01b88c | 6295 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
3f294f4f | 6296 | { |
7b1485ba N |
6297 | struct r5conf *conf; |
6298 | int ret = 0; | |
6299 | spin_lock(&mddev->lock); | |
6300 | conf = mddev->private; | |
96de1e66 | 6301 | if (conf) |
edbe83ab | 6302 | ret = sprintf(page, "%d\n", conf->min_nr_stripes); |
7b1485ba N |
6303 | spin_unlock(&mddev->lock); |
6304 | return ret; | |
3f294f4f N |
6305 | } |
6306 | ||
c41d4ac4 | 6307 | int |
fd01b88c | 6308 | raid5_set_cache_size(struct mddev *mddev, int size) |
3f294f4f | 6309 | { |
d1688a6d | 6310 | struct r5conf *conf = mddev->private; |
b5470dc5 | 6311 | |
c41d4ac4 | 6312 | if (size <= 16 || size > 32768) |
3f294f4f | 6313 | return -EINVAL; |
486f0644 | 6314 | |
edbe83ab | 6315 | conf->min_nr_stripes = size; |
2d5b569b | 6316 | mutex_lock(&conf->cache_size_mutex); |
486f0644 N |
6317 | while (size < conf->max_nr_stripes && |
6318 | drop_one_stripe(conf)) | |
6319 | ; | |
2d5b569b | 6320 | mutex_unlock(&conf->cache_size_mutex); |
486f0644 | 6321 | |
2214c260 | 6322 | md_allow_write(mddev); |
486f0644 | 6323 | |
2d5b569b | 6324 | mutex_lock(&conf->cache_size_mutex); |
486f0644 N |
6325 | while (size > conf->max_nr_stripes) |
6326 | if (!grow_one_stripe(conf, GFP_KERNEL)) | |
6327 | break; | |
2d5b569b | 6328 | mutex_unlock(&conf->cache_size_mutex); |
486f0644 | 6329 | |
c41d4ac4 N |
6330 | return 0; |
6331 | } | |
6332 | EXPORT_SYMBOL(raid5_set_cache_size); | |
6333 | ||
6334 | static ssize_t | |
fd01b88c | 6335 | raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len) |
c41d4ac4 | 6336 | { |
6791875e | 6337 | struct r5conf *conf; |
c41d4ac4 N |
6338 | unsigned long new; |
6339 | int err; | |
6340 | ||
6341 | if (len >= PAGE_SIZE) | |
6342 | return -EINVAL; | |
b29bebd6 | 6343 | if (kstrtoul(page, 10, &new)) |
c41d4ac4 | 6344 | return -EINVAL; |
6791875e | 6345 | err = mddev_lock(mddev); |
c41d4ac4 N |
6346 | if (err) |
6347 | return err; | |
6791875e N |
6348 | conf = mddev->private; |
6349 | if (!conf) | |
6350 | err = -ENODEV; | |
6351 | else | |
6352 | err = raid5_set_cache_size(mddev, new); | |
6353 | mddev_unlock(mddev); | |
6354 | ||
6355 | return err ?: len; | |
3f294f4f | 6356 | } |
007583c9 | 6357 | |
96de1e66 N |
6358 | static struct md_sysfs_entry |
6359 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, | |
6360 | raid5_show_stripe_cache_size, | |
6361 | raid5_store_stripe_cache_size); | |
3f294f4f | 6362 | |
d06f191f MS |
6363 | static ssize_t |
6364 | raid5_show_rmw_level(struct mddev *mddev, char *page) | |
6365 | { | |
6366 | struct r5conf *conf = mddev->private; | |
6367 | if (conf) | |
6368 | return sprintf(page, "%d\n", conf->rmw_level); | |
6369 | else | |
6370 | return 0; | |
6371 | } | |
6372 | ||
6373 | static ssize_t | |
6374 | raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) | |
6375 | { | |
6376 | struct r5conf *conf = mddev->private; | |
6377 | unsigned long new; | |
6378 | ||
6379 | if (!conf) | |
6380 | return -ENODEV; | |
6381 | ||
6382 | if (len >= PAGE_SIZE) | |
6383 | return -EINVAL; | |
6384 | ||
6385 | if (kstrtoul(page, 10, &new)) | |
6386 | return -EINVAL; | |
6387 | ||
6388 | if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) | |
6389 | return -EINVAL; | |
6390 | ||
6391 | if (new != PARITY_DISABLE_RMW && | |
6392 | new != PARITY_ENABLE_RMW && | |
6393 | new != PARITY_PREFER_RMW) | |
6394 | return -EINVAL; | |
6395 | ||
6396 | conf->rmw_level = new; | |
6397 | return len; | |
6398 | } | |
6399 | ||
6400 | static struct md_sysfs_entry | |
6401 | raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, | |
6402 | raid5_show_rmw_level, | |
6403 | raid5_store_rmw_level); | |
6404 | ||
6405 | ||
8b3e6cdc | 6406 | static ssize_t |
fd01b88c | 6407 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
8b3e6cdc | 6408 | { |
7b1485ba N |
6409 | struct r5conf *conf; |
6410 | int ret = 0; | |
6411 | spin_lock(&mddev->lock); | |
6412 | conf = mddev->private; | |
8b3e6cdc | 6413 | if (conf) |
7b1485ba N |
6414 | ret = sprintf(page, "%d\n", conf->bypass_threshold); |
6415 | spin_unlock(&mddev->lock); | |
6416 | return ret; | |
8b3e6cdc DW |
6417 | } |
6418 | ||
6419 | static ssize_t | |
fd01b88c | 6420 | raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len) |
8b3e6cdc | 6421 | { |
6791875e | 6422 | struct r5conf *conf; |
4ef197d8 | 6423 | unsigned long new; |
6791875e N |
6424 | int err; |
6425 | ||
8b3e6cdc DW |
6426 | if (len >= PAGE_SIZE) |
6427 | return -EINVAL; | |
b29bebd6 | 6428 | if (kstrtoul(page, 10, &new)) |
8b3e6cdc | 6429 | return -EINVAL; |
6791875e N |
6430 | |
6431 | err = mddev_lock(mddev); | |
6432 | if (err) | |
6433 | return err; | |
6434 | conf = mddev->private; | |
6435 | if (!conf) | |
6436 | err = -ENODEV; | |
edbe83ab | 6437 | else if (new > conf->min_nr_stripes) |
6791875e N |
6438 | err = -EINVAL; |
6439 | else | |
6440 | conf->bypass_threshold = new; | |
6441 | mddev_unlock(mddev); | |
6442 | return err ?: len; | |
8b3e6cdc DW |
6443 | } |
6444 | ||
6445 | static struct md_sysfs_entry | |
6446 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, | |
6447 | S_IRUGO | S_IWUSR, | |
6448 | raid5_show_preread_threshold, | |
6449 | raid5_store_preread_threshold); | |
6450 | ||
d592a996 SL |
6451 | static ssize_t |
6452 | raid5_show_skip_copy(struct mddev *mddev, char *page) | |
6453 | { | |
7b1485ba N |
6454 | struct r5conf *conf; |
6455 | int ret = 0; | |
6456 | spin_lock(&mddev->lock); | |
6457 | conf = mddev->private; | |
d592a996 | 6458 | if (conf) |
7b1485ba N |
6459 | ret = sprintf(page, "%d\n", conf->skip_copy); |
6460 | spin_unlock(&mddev->lock); | |
6461 | return ret; | |
d592a996 SL |
6462 | } |
6463 | ||
6464 | static ssize_t | |
6465 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) | |
6466 | { | |
6791875e | 6467 | struct r5conf *conf; |
d592a996 | 6468 | unsigned long new; |
6791875e N |
6469 | int err; |
6470 | ||
d592a996 SL |
6471 | if (len >= PAGE_SIZE) |
6472 | return -EINVAL; | |
d592a996 SL |
6473 | if (kstrtoul(page, 10, &new)) |
6474 | return -EINVAL; | |
6475 | new = !!new; | |
6791875e N |
6476 | |
6477 | err = mddev_lock(mddev); | |
6478 | if (err) | |
6479 | return err; | |
6480 | conf = mddev->private; | |
6481 | if (!conf) | |
6482 | err = -ENODEV; | |
6483 | else if (new != conf->skip_copy) { | |
6484 | mddev_suspend(mddev); | |
6485 | conf->skip_copy = new; | |
6486 | if (new) | |
dc3b17cc | 6487 | mddev->queue->backing_dev_info->capabilities |= |
6791875e N |
6488 | BDI_CAP_STABLE_WRITES; |
6489 | else | |
dc3b17cc | 6490 | mddev->queue->backing_dev_info->capabilities &= |
6791875e N |
6491 | ~BDI_CAP_STABLE_WRITES; |
6492 | mddev_resume(mddev); | |
6493 | } | |
6494 | mddev_unlock(mddev); | |
6495 | return err ?: len; | |
d592a996 SL |
6496 | } |
6497 | ||
6498 | static struct md_sysfs_entry | |
6499 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, | |
6500 | raid5_show_skip_copy, | |
6501 | raid5_store_skip_copy); | |
6502 | ||
3f294f4f | 6503 | static ssize_t |
fd01b88c | 6504 | stripe_cache_active_show(struct mddev *mddev, char *page) |
3f294f4f | 6505 | { |
d1688a6d | 6506 | struct r5conf *conf = mddev->private; |
96de1e66 N |
6507 | if (conf) |
6508 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); | |
6509 | else | |
6510 | return 0; | |
3f294f4f N |
6511 | } |
6512 | ||
96de1e66 N |
6513 | static struct md_sysfs_entry |
6514 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); | |
3f294f4f | 6515 | |
b721420e SL |
6516 | static ssize_t |
6517 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) | |
6518 | { | |
7b1485ba N |
6519 | struct r5conf *conf; |
6520 | int ret = 0; | |
6521 | spin_lock(&mddev->lock); | |
6522 | conf = mddev->private; | |
b721420e | 6523 | if (conf) |
7b1485ba N |
6524 | ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); |
6525 | spin_unlock(&mddev->lock); | |
6526 | return ret; | |
b721420e SL |
6527 | } |
6528 | ||
60aaf933 | 6529 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
6530 | int *group_cnt, | |
6531 | int *worker_cnt_per_group, | |
6532 | struct r5worker_group **worker_groups); | |
b721420e SL |
6533 | static ssize_t |
6534 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) | |
6535 | { | |
6791875e | 6536 | struct r5conf *conf; |
b721420e SL |
6537 | unsigned long new; |
6538 | int err; | |
60aaf933 | 6539 | struct r5worker_group *new_groups, *old_groups; |
6540 | int group_cnt, worker_cnt_per_group; | |
b721420e SL |
6541 | |
6542 | if (len >= PAGE_SIZE) | |
6543 | return -EINVAL; | |
b721420e SL |
6544 | if (kstrtoul(page, 10, &new)) |
6545 | return -EINVAL; | |
6546 | ||
6791875e N |
6547 | err = mddev_lock(mddev); |
6548 | if (err) | |
6549 | return err; | |
6550 | conf = mddev->private; | |
6551 | if (!conf) | |
6552 | err = -ENODEV; | |
6553 | else if (new != conf->worker_cnt_per_group) { | |
6554 | mddev_suspend(mddev); | |
b721420e | 6555 | |
6791875e N |
6556 | old_groups = conf->worker_groups; |
6557 | if (old_groups) | |
6558 | flush_workqueue(raid5_wq); | |
d206dcfa | 6559 | |
6791875e N |
6560 | err = alloc_thread_groups(conf, new, |
6561 | &group_cnt, &worker_cnt_per_group, | |
6562 | &new_groups); | |
6563 | if (!err) { | |
6564 | spin_lock_irq(&conf->device_lock); | |
6565 | conf->group_cnt = group_cnt; | |
6566 | conf->worker_cnt_per_group = worker_cnt_per_group; | |
6567 | conf->worker_groups = new_groups; | |
6568 | spin_unlock_irq(&conf->device_lock); | |
b721420e | 6569 | |
6791875e N |
6570 | if (old_groups) |
6571 | kfree(old_groups[0].workers); | |
6572 | kfree(old_groups); | |
6573 | } | |
6574 | mddev_resume(mddev); | |
b721420e | 6575 | } |
6791875e | 6576 | mddev_unlock(mddev); |
b721420e | 6577 | |
6791875e | 6578 | return err ?: len; |
b721420e SL |
6579 | } |
6580 | ||
6581 | static struct md_sysfs_entry | |
6582 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, | |
6583 | raid5_show_group_thread_cnt, | |
6584 | raid5_store_group_thread_cnt); | |
6585 | ||
007583c9 | 6586 | static struct attribute *raid5_attrs[] = { |
3f294f4f N |
6587 | &raid5_stripecache_size.attr, |
6588 | &raid5_stripecache_active.attr, | |
8b3e6cdc | 6589 | &raid5_preread_bypass_threshold.attr, |
b721420e | 6590 | &raid5_group_thread_cnt.attr, |
d592a996 | 6591 | &raid5_skip_copy.attr, |
d06f191f | 6592 | &raid5_rmw_level.attr, |
2c7da14b | 6593 | &r5c_journal_mode.attr, |
3f294f4f N |
6594 | NULL, |
6595 | }; | |
007583c9 N |
6596 | static struct attribute_group raid5_attrs_group = { |
6597 | .name = NULL, | |
6598 | .attrs = raid5_attrs, | |
3f294f4f N |
6599 | }; |
6600 | ||
60aaf933 | 6601 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
6602 | int *group_cnt, | |
6603 | int *worker_cnt_per_group, | |
6604 | struct r5worker_group **worker_groups) | |
851c30c9 | 6605 | { |
566c09c5 | 6606 | int i, j, k; |
851c30c9 SL |
6607 | ssize_t size; |
6608 | struct r5worker *workers; | |
6609 | ||
60aaf933 | 6610 | *worker_cnt_per_group = cnt; |
851c30c9 | 6611 | if (cnt == 0) { |
60aaf933 | 6612 | *group_cnt = 0; |
6613 | *worker_groups = NULL; | |
851c30c9 SL |
6614 | return 0; |
6615 | } | |
60aaf933 | 6616 | *group_cnt = num_possible_nodes(); |
851c30c9 | 6617 | size = sizeof(struct r5worker) * cnt; |
60aaf933 | 6618 | workers = kzalloc(size * *group_cnt, GFP_NOIO); |
6619 | *worker_groups = kzalloc(sizeof(struct r5worker_group) * | |
6620 | *group_cnt, GFP_NOIO); | |
6621 | if (!*worker_groups || !workers) { | |
851c30c9 | 6622 | kfree(workers); |
60aaf933 | 6623 | kfree(*worker_groups); |
851c30c9 SL |
6624 | return -ENOMEM; |
6625 | } | |
6626 | ||
60aaf933 | 6627 | for (i = 0; i < *group_cnt; i++) { |
851c30c9 SL |
6628 | struct r5worker_group *group; |
6629 | ||
0c775d52 | 6630 | group = &(*worker_groups)[i]; |
851c30c9 | 6631 | INIT_LIST_HEAD(&group->handle_list); |
535ae4eb | 6632 | INIT_LIST_HEAD(&group->loprio_list); |
851c30c9 SL |
6633 | group->conf = conf; |
6634 | group->workers = workers + i * cnt; | |
6635 | ||
6636 | for (j = 0; j < cnt; j++) { | |
566c09c5 SL |
6637 | struct r5worker *worker = group->workers + j; |
6638 | worker->group = group; | |
6639 | INIT_WORK(&worker->work, raid5_do_work); | |
6640 | ||
6641 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) | |
6642 | INIT_LIST_HEAD(worker->temp_inactive_list + k); | |
851c30c9 SL |
6643 | } |
6644 | } | |
6645 | ||
6646 | return 0; | |
6647 | } | |
6648 | ||
6649 | static void free_thread_groups(struct r5conf *conf) | |
6650 | { | |
6651 | if (conf->worker_groups) | |
6652 | kfree(conf->worker_groups[0].workers); | |
6653 | kfree(conf->worker_groups); | |
6654 | conf->worker_groups = NULL; | |
6655 | } | |
6656 | ||
80c3a6ce | 6657 | static sector_t |
fd01b88c | 6658 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
80c3a6ce | 6659 | { |
d1688a6d | 6660 | struct r5conf *conf = mddev->private; |
80c3a6ce DW |
6661 | |
6662 | if (!sectors) | |
6663 | sectors = mddev->dev_sectors; | |
5e5e3e78 | 6664 | if (!raid_disks) |
7ec05478 | 6665 | /* size is defined by the smallest of previous and new size */ |
5e5e3e78 | 6666 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
80c3a6ce | 6667 | |
3cb5edf4 N |
6668 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
6669 | sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); | |
80c3a6ce DW |
6670 | return sectors * (raid_disks - conf->max_degraded); |
6671 | } | |
6672 | ||
789b5e03 ON |
6673 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
6674 | { | |
6675 | safe_put_page(percpu->spare_page); | |
46d5b785 | 6676 | if (percpu->scribble) |
6677 | flex_array_free(percpu->scribble); | |
789b5e03 ON |
6678 | percpu->spare_page = NULL; |
6679 | percpu->scribble = NULL; | |
6680 | } | |
6681 | ||
6682 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) | |
6683 | { | |
6684 | if (conf->level == 6 && !percpu->spare_page) | |
6685 | percpu->spare_page = alloc_page(GFP_KERNEL); | |
6686 | if (!percpu->scribble) | |
46d5b785 | 6687 | percpu->scribble = scribble_alloc(max(conf->raid_disks, |
738a2738 N |
6688 | conf->previous_raid_disks), |
6689 | max(conf->chunk_sectors, | |
6690 | conf->prev_chunk_sectors) | |
6691 | / STRIPE_SECTORS, | |
6692 | GFP_KERNEL); | |
789b5e03 ON |
6693 | |
6694 | if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) { | |
6695 | free_scratch_buffer(conf, percpu); | |
6696 | return -ENOMEM; | |
6697 | } | |
6698 | ||
6699 | return 0; | |
6700 | } | |
6701 | ||
29c6d1bb | 6702 | static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) |
36d1c647 | 6703 | { |
29c6d1bb SAS |
6704 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
6705 | ||
6706 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); | |
6707 | return 0; | |
6708 | } | |
36d1c647 | 6709 | |
29c6d1bb SAS |
6710 | static void raid5_free_percpu(struct r5conf *conf) |
6711 | { | |
36d1c647 DW |
6712 | if (!conf->percpu) |
6713 | return; | |
6714 | ||
29c6d1bb | 6715 | cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
36d1c647 DW |
6716 | free_percpu(conf->percpu); |
6717 | } | |
6718 | ||
d1688a6d | 6719 | static void free_conf(struct r5conf *conf) |
95fc17aa | 6720 | { |
d7bd398e SL |
6721 | int i; |
6722 | ||
ff875738 AP |
6723 | log_exit(conf); |
6724 | ||
30c89465 | 6725 | if (conf->shrinker.nr_deferred) |
edbe83ab | 6726 | unregister_shrinker(&conf->shrinker); |
5c7e81c3 | 6727 | |
851c30c9 | 6728 | free_thread_groups(conf); |
95fc17aa | 6729 | shrink_stripes(conf); |
36d1c647 | 6730 | raid5_free_percpu(conf); |
d7bd398e SL |
6731 | for (i = 0; i < conf->pool_size; i++) |
6732 | if (conf->disks[i].extra_page) | |
6733 | put_page(conf->disks[i].extra_page); | |
95fc17aa | 6734 | kfree(conf->disks); |
dd7a8f5d N |
6735 | if (conf->bio_split) |
6736 | bioset_free(conf->bio_split); | |
95fc17aa | 6737 | kfree(conf->stripe_hashtbl); |
aaf9f12e | 6738 | kfree(conf->pending_data); |
95fc17aa DW |
6739 | kfree(conf); |
6740 | } | |
6741 | ||
29c6d1bb | 6742 | static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) |
36d1c647 | 6743 | { |
29c6d1bb | 6744 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
36d1c647 DW |
6745 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
6746 | ||
29c6d1bb | 6747 | if (alloc_scratch_buffer(conf, percpu)) { |
cc6167b4 N |
6748 | pr_warn("%s: failed memory allocation for cpu%u\n", |
6749 | __func__, cpu); | |
29c6d1bb | 6750 | return -ENOMEM; |
36d1c647 | 6751 | } |
29c6d1bb | 6752 | return 0; |
36d1c647 | 6753 | } |
36d1c647 | 6754 | |
d1688a6d | 6755 | static int raid5_alloc_percpu(struct r5conf *conf) |
36d1c647 | 6756 | { |
789b5e03 | 6757 | int err = 0; |
36d1c647 | 6758 | |
789b5e03 ON |
6759 | conf->percpu = alloc_percpu(struct raid5_percpu); |
6760 | if (!conf->percpu) | |
36d1c647 | 6761 | return -ENOMEM; |
789b5e03 | 6762 | |
29c6d1bb | 6763 | err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
27a353c0 SL |
6764 | if (!err) { |
6765 | conf->scribble_disks = max(conf->raid_disks, | |
6766 | conf->previous_raid_disks); | |
6767 | conf->scribble_sectors = max(conf->chunk_sectors, | |
6768 | conf->prev_chunk_sectors); | |
6769 | } | |
36d1c647 DW |
6770 | return err; |
6771 | } | |
6772 | ||
edbe83ab N |
6773 | static unsigned long raid5_cache_scan(struct shrinker *shrink, |
6774 | struct shrink_control *sc) | |
6775 | { | |
6776 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); | |
2d5b569b N |
6777 | unsigned long ret = SHRINK_STOP; |
6778 | ||
6779 | if (mutex_trylock(&conf->cache_size_mutex)) { | |
6780 | ret= 0; | |
49895bcc N |
6781 | while (ret < sc->nr_to_scan && |
6782 | conf->max_nr_stripes > conf->min_nr_stripes) { | |
2d5b569b N |
6783 | if (drop_one_stripe(conf) == 0) { |
6784 | ret = SHRINK_STOP; | |
6785 | break; | |
6786 | } | |
6787 | ret++; | |
6788 | } | |
6789 | mutex_unlock(&conf->cache_size_mutex); | |
edbe83ab N |
6790 | } |
6791 | return ret; | |
6792 | } | |
6793 | ||
6794 | static unsigned long raid5_cache_count(struct shrinker *shrink, | |
6795 | struct shrink_control *sc) | |
6796 | { | |
6797 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); | |
6798 | ||
6799 | if (conf->max_nr_stripes < conf->min_nr_stripes) | |
6800 | /* unlikely, but not impossible */ | |
6801 | return 0; | |
6802 | return conf->max_nr_stripes - conf->min_nr_stripes; | |
6803 | } | |
6804 | ||
d1688a6d | 6805 | static struct r5conf *setup_conf(struct mddev *mddev) |
1da177e4 | 6806 | { |
d1688a6d | 6807 | struct r5conf *conf; |
5e5e3e78 | 6808 | int raid_disk, memory, max_disks; |
3cb03002 | 6809 | struct md_rdev *rdev; |
1da177e4 | 6810 | struct disk_info *disk; |
0232605d | 6811 | char pers_name[6]; |
566c09c5 | 6812 | int i; |
60aaf933 | 6813 | int group_cnt, worker_cnt_per_group; |
6814 | struct r5worker_group *new_group; | |
1da177e4 | 6815 | |
91adb564 N |
6816 | if (mddev->new_level != 5 |
6817 | && mddev->new_level != 4 | |
6818 | && mddev->new_level != 6) { | |
cc6167b4 N |
6819 | pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
6820 | mdname(mddev), mddev->new_level); | |
91adb564 | 6821 | return ERR_PTR(-EIO); |
1da177e4 | 6822 | } |
91adb564 N |
6823 | if ((mddev->new_level == 5 |
6824 | && !algorithm_valid_raid5(mddev->new_layout)) || | |
6825 | (mddev->new_level == 6 | |
6826 | && !algorithm_valid_raid6(mddev->new_layout))) { | |
cc6167b4 N |
6827 | pr_warn("md/raid:%s: layout %d not supported\n", |
6828 | mdname(mddev), mddev->new_layout); | |
91adb564 | 6829 | return ERR_PTR(-EIO); |
99c0fb5f | 6830 | } |
91adb564 | 6831 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
cc6167b4 N |
6832 | pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
6833 | mdname(mddev), mddev->raid_disks); | |
91adb564 | 6834 | return ERR_PTR(-EINVAL); |
4bbf3771 N |
6835 | } |
6836 | ||
664e7c41 AN |
6837 | if (!mddev->new_chunk_sectors || |
6838 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || | |
6839 | !is_power_of_2(mddev->new_chunk_sectors)) { | |
cc6167b4 N |
6840 | pr_warn("md/raid:%s: invalid chunk size %d\n", |
6841 | mdname(mddev), mddev->new_chunk_sectors << 9); | |
91adb564 | 6842 | return ERR_PTR(-EINVAL); |
f6705578 N |
6843 | } |
6844 | ||
d1688a6d | 6845 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
91adb564 | 6846 | if (conf == NULL) |
1da177e4 | 6847 | goto abort; |
aaf9f12e SL |
6848 | INIT_LIST_HEAD(&conf->free_list); |
6849 | INIT_LIST_HEAD(&conf->pending_list); | |
6850 | conf->pending_data = kzalloc(sizeof(struct r5pending_data) * | |
6851 | PENDING_IO_MAX, GFP_KERNEL); | |
6852 | if (!conf->pending_data) | |
6853 | goto abort; | |
6854 | for (i = 0; i < PENDING_IO_MAX; i++) | |
6855 | list_add(&conf->pending_data[i].sibling, &conf->free_list); | |
851c30c9 | 6856 | /* Don't enable multi-threading by default*/ |
60aaf933 | 6857 | if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group, |
6858 | &new_group)) { | |
6859 | conf->group_cnt = group_cnt; | |
6860 | conf->worker_cnt_per_group = worker_cnt_per_group; | |
6861 | conf->worker_groups = new_group; | |
6862 | } else | |
851c30c9 | 6863 | goto abort; |
f5efd45a | 6864 | spin_lock_init(&conf->device_lock); |
c46501b2 | 6865 | seqcount_init(&conf->gen_lock); |
2d5b569b | 6866 | mutex_init(&conf->cache_size_mutex); |
b1b46486 | 6867 | init_waitqueue_head(&conf->wait_for_quiescent); |
6ab2a4b8 | 6868 | init_waitqueue_head(&conf->wait_for_stripe); |
f5efd45a DW |
6869 | init_waitqueue_head(&conf->wait_for_overlap); |
6870 | INIT_LIST_HEAD(&conf->handle_list); | |
535ae4eb | 6871 | INIT_LIST_HEAD(&conf->loprio_list); |
f5efd45a DW |
6872 | INIT_LIST_HEAD(&conf->hold_list); |
6873 | INIT_LIST_HEAD(&conf->delayed_list); | |
6874 | INIT_LIST_HEAD(&conf->bitmap_list); | |
773ca82f | 6875 | init_llist_head(&conf->released_stripes); |
f5efd45a DW |
6876 | atomic_set(&conf->active_stripes, 0); |
6877 | atomic_set(&conf->preread_active_stripes, 0); | |
6878 | atomic_set(&conf->active_aligned_reads, 0); | |
765d704d SL |
6879 | spin_lock_init(&conf->pending_bios_lock); |
6880 | conf->batch_bio_dispatch = true; | |
6881 | rdev_for_each(rdev, mddev) { | |
6882 | if (test_bit(Journal, &rdev->flags)) | |
6883 | continue; | |
6884 | if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { | |
6885 | conf->batch_bio_dispatch = false; | |
6886 | break; | |
6887 | } | |
6888 | } | |
6889 | ||
f5efd45a | 6890 | conf->bypass_threshold = BYPASS_THRESHOLD; |
d890fa2b | 6891 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
91adb564 N |
6892 | |
6893 | conf->raid_disks = mddev->raid_disks; | |
6894 | if (mddev->reshape_position == MaxSector) | |
6895 | conf->previous_raid_disks = mddev->raid_disks; | |
6896 | else | |
f6705578 | 6897 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
5e5e3e78 | 6898 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
f6705578 | 6899 | |
5e5e3e78 | 6900 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
b55e6bfc | 6901 | GFP_KERNEL); |
d7bd398e | 6902 | |
b55e6bfc N |
6903 | if (!conf->disks) |
6904 | goto abort; | |
9ffae0cf | 6905 | |
d7bd398e SL |
6906 | for (i = 0; i < max_disks; i++) { |
6907 | conf->disks[i].extra_page = alloc_page(GFP_KERNEL); | |
6908 | if (!conf->disks[i].extra_page) | |
6909 | goto abort; | |
6910 | } | |
6911 | ||
dd7a8f5d N |
6912 | conf->bio_split = bioset_create(BIO_POOL_SIZE, 0); |
6913 | if (!conf->bio_split) | |
6914 | goto abort; | |
1da177e4 LT |
6915 | conf->mddev = mddev; |
6916 | ||
fccddba0 | 6917 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
1da177e4 | 6918 | goto abort; |
1da177e4 | 6919 | |
566c09c5 SL |
6920 | /* We init hash_locks[0] separately to that it can be used |
6921 | * as the reference lock in the spin_lock_nest_lock() call | |
6922 | * in lock_all_device_hash_locks_irq in order to convince | |
6923 | * lockdep that we know what we are doing. | |
6924 | */ | |
6925 | spin_lock_init(conf->hash_locks); | |
6926 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) | |
6927 | spin_lock_init(conf->hash_locks + i); | |
6928 | ||
6929 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) | |
6930 | INIT_LIST_HEAD(conf->inactive_list + i); | |
6931 | ||
6932 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) | |
6933 | INIT_LIST_HEAD(conf->temp_inactive_list + i); | |
6934 | ||
1e6d690b SL |
6935 | atomic_set(&conf->r5c_cached_full_stripes, 0); |
6936 | INIT_LIST_HEAD(&conf->r5c_full_stripe_list); | |
6937 | atomic_set(&conf->r5c_cached_partial_stripes, 0); | |
6938 | INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); | |
e33fbb9c SL |
6939 | atomic_set(&conf->r5c_flushing_full_stripes, 0); |
6940 | atomic_set(&conf->r5c_flushing_partial_stripes, 0); | |
1e6d690b | 6941 | |
36d1c647 | 6942 | conf->level = mddev->new_level; |
46d5b785 | 6943 | conf->chunk_sectors = mddev->new_chunk_sectors; |
36d1c647 DW |
6944 | if (raid5_alloc_percpu(conf) != 0) |
6945 | goto abort; | |
6946 | ||
0c55e022 | 6947 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
1da177e4 | 6948 | |
dafb20fa | 6949 | rdev_for_each(rdev, mddev) { |
1da177e4 | 6950 | raid_disk = rdev->raid_disk; |
5e5e3e78 | 6951 | if (raid_disk >= max_disks |
f2076e7d | 6952 | || raid_disk < 0 || test_bit(Journal, &rdev->flags)) |
1da177e4 LT |
6953 | continue; |
6954 | disk = conf->disks + raid_disk; | |
6955 | ||
17045f52 N |
6956 | if (test_bit(Replacement, &rdev->flags)) { |
6957 | if (disk->replacement) | |
6958 | goto abort; | |
6959 | disk->replacement = rdev; | |
6960 | } else { | |
6961 | if (disk->rdev) | |
6962 | goto abort; | |
6963 | disk->rdev = rdev; | |
6964 | } | |
1da177e4 | 6965 | |
b2d444d7 | 6966 | if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 | 6967 | char b[BDEVNAME_SIZE]; |
cc6167b4 N |
6968 | pr_info("md/raid:%s: device %s operational as raid disk %d\n", |
6969 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); | |
d6b212f4 | 6970 | } else if (rdev->saved_raid_disk != raid_disk) |
8c2e870a NB |
6971 | /* Cannot rely on bitmap to complete recovery */ |
6972 | conf->fullsync = 1; | |
1da177e4 LT |
6973 | } |
6974 | ||
91adb564 | 6975 | conf->level = mddev->new_level; |
584acdd4 | 6976 | if (conf->level == 6) { |
16a53ecc | 6977 | conf->max_degraded = 2; |
584acdd4 MS |
6978 | if (raid6_call.xor_syndrome) |
6979 | conf->rmw_level = PARITY_ENABLE_RMW; | |
6980 | else | |
6981 | conf->rmw_level = PARITY_DISABLE_RMW; | |
6982 | } else { | |
16a53ecc | 6983 | conf->max_degraded = 1; |
584acdd4 MS |
6984 | conf->rmw_level = PARITY_ENABLE_RMW; |
6985 | } | |
91adb564 | 6986 | conf->algorithm = mddev->new_layout; |
fef9c61f | 6987 | conf->reshape_progress = mddev->reshape_position; |
e183eaed | 6988 | if (conf->reshape_progress != MaxSector) { |
09c9e5fa | 6989 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
e183eaed | 6990 | conf->prev_algo = mddev->layout; |
5cac6bcb N |
6991 | } else { |
6992 | conf->prev_chunk_sectors = conf->chunk_sectors; | |
6993 | conf->prev_algo = conf->algorithm; | |
e183eaed | 6994 | } |
1da177e4 | 6995 | |
edbe83ab | 6996 | conf->min_nr_stripes = NR_STRIPES; |
ad5b0f76 SL |
6997 | if (mddev->reshape_position != MaxSector) { |
6998 | int stripes = max_t(int, | |
6999 | ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4, | |
7000 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4); | |
7001 | conf->min_nr_stripes = max(NR_STRIPES, stripes); | |
7002 | if (conf->min_nr_stripes != NR_STRIPES) | |
cc6167b4 | 7003 | pr_info("md/raid:%s: force stripe size %d for reshape\n", |
ad5b0f76 SL |
7004 | mdname(mddev), conf->min_nr_stripes); |
7005 | } | |
edbe83ab | 7006 | memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + |
5e5e3e78 | 7007 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
4bda556a | 7008 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
edbe83ab | 7009 | if (grow_stripes(conf, conf->min_nr_stripes)) { |
cc6167b4 N |
7010 | pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", |
7011 | mdname(mddev), memory); | |
91adb564 N |
7012 | goto abort; |
7013 | } else | |
cc6167b4 | 7014 | pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); |
edbe83ab N |
7015 | /* |
7016 | * Losing a stripe head costs more than the time to refill it, | |
7017 | * it reduces the queue depth and so can hurt throughput. | |
7018 | * So set it rather large, scaled by number of devices. | |
7019 | */ | |
7020 | conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4; | |
7021 | conf->shrinker.scan_objects = raid5_cache_scan; | |
7022 | conf->shrinker.count_objects = raid5_cache_count; | |
7023 | conf->shrinker.batch = 128; | |
7024 | conf->shrinker.flags = 0; | |
6a0f53ff | 7025 | if (register_shrinker(&conf->shrinker)) { |
cc6167b4 N |
7026 | pr_warn("md/raid:%s: couldn't register shrinker.\n", |
7027 | mdname(mddev)); | |
6a0f53ff CY |
7028 | goto abort; |
7029 | } | |
1da177e4 | 7030 | |
0232605d N |
7031 | sprintf(pers_name, "raid%d", mddev->new_level); |
7032 | conf->thread = md_register_thread(raid5d, mddev, pers_name); | |
91adb564 | 7033 | if (!conf->thread) { |
cc6167b4 N |
7034 | pr_warn("md/raid:%s: couldn't allocate thread.\n", |
7035 | mdname(mddev)); | |
16a53ecc N |
7036 | goto abort; |
7037 | } | |
91adb564 N |
7038 | |
7039 | return conf; | |
7040 | ||
7041 | abort: | |
7042 | if (conf) { | |
95fc17aa | 7043 | free_conf(conf); |
91adb564 N |
7044 | return ERR_PTR(-EIO); |
7045 | } else | |
7046 | return ERR_PTR(-ENOMEM); | |
7047 | } | |
7048 | ||
c148ffdc N |
7049 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
7050 | { | |
7051 | switch (algo) { | |
7052 | case ALGORITHM_PARITY_0: | |
7053 | if (raid_disk < max_degraded) | |
7054 | return 1; | |
7055 | break; | |
7056 | case ALGORITHM_PARITY_N: | |
7057 | if (raid_disk >= raid_disks - max_degraded) | |
7058 | return 1; | |
7059 | break; | |
7060 | case ALGORITHM_PARITY_0_6: | |
f72ffdd6 | 7061 | if (raid_disk == 0 || |
c148ffdc N |
7062 | raid_disk == raid_disks - 1) |
7063 | return 1; | |
7064 | break; | |
7065 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
7066 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
7067 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
7068 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
7069 | if (raid_disk == raid_disks - 1) | |
7070 | return 1; | |
7071 | } | |
7072 | return 0; | |
7073 | } | |
7074 | ||
849674e4 | 7075 | static int raid5_run(struct mddev *mddev) |
91adb564 | 7076 | { |
d1688a6d | 7077 | struct r5conf *conf; |
9f7c2220 | 7078 | int working_disks = 0; |
c148ffdc | 7079 | int dirty_parity_disks = 0; |
3cb03002 | 7080 | struct md_rdev *rdev; |
713cf5a6 | 7081 | struct md_rdev *journal_dev = NULL; |
c148ffdc | 7082 | sector_t reshape_offset = 0; |
17045f52 | 7083 | int i; |
b5254dd5 N |
7084 | long long min_offset_diff = 0; |
7085 | int first = 1; | |
91adb564 | 7086 | |
8c6ac868 | 7087 | if (mddev->recovery_cp != MaxSector) |
cc6167b4 N |
7088 | pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", |
7089 | mdname(mddev)); | |
b5254dd5 N |
7090 | |
7091 | rdev_for_each(rdev, mddev) { | |
7092 | long long diff; | |
713cf5a6 | 7093 | |
f2076e7d | 7094 | if (test_bit(Journal, &rdev->flags)) { |
713cf5a6 | 7095 | journal_dev = rdev; |
f2076e7d SL |
7096 | continue; |
7097 | } | |
b5254dd5 N |
7098 | if (rdev->raid_disk < 0) |
7099 | continue; | |
7100 | diff = (rdev->new_data_offset - rdev->data_offset); | |
7101 | if (first) { | |
7102 | min_offset_diff = diff; | |
7103 | first = 0; | |
7104 | } else if (mddev->reshape_backwards && | |
7105 | diff < min_offset_diff) | |
7106 | min_offset_diff = diff; | |
7107 | else if (!mddev->reshape_backwards && | |
7108 | diff > min_offset_diff) | |
7109 | min_offset_diff = diff; | |
7110 | } | |
7111 | ||
91adb564 N |
7112 | if (mddev->reshape_position != MaxSector) { |
7113 | /* Check that we can continue the reshape. | |
b5254dd5 N |
7114 | * Difficulties arise if the stripe we would write to |
7115 | * next is at or after the stripe we would read from next. | |
7116 | * For a reshape that changes the number of devices, this | |
7117 | * is only possible for a very short time, and mdadm makes | |
7118 | * sure that time appears to have past before assembling | |
7119 | * the array. So we fail if that time hasn't passed. | |
7120 | * For a reshape that keeps the number of devices the same | |
7121 | * mdadm must be monitoring the reshape can keeping the | |
7122 | * critical areas read-only and backed up. It will start | |
7123 | * the array in read-only mode, so we check for that. | |
91adb564 N |
7124 | */ |
7125 | sector_t here_new, here_old; | |
7126 | int old_disks; | |
18b00334 | 7127 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
05256d98 N |
7128 | int chunk_sectors; |
7129 | int new_data_disks; | |
91adb564 | 7130 | |
713cf5a6 | 7131 | if (journal_dev) { |
cc6167b4 N |
7132 | pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", |
7133 | mdname(mddev)); | |
713cf5a6 SL |
7134 | return -EINVAL; |
7135 | } | |
7136 | ||
88ce4930 | 7137 | if (mddev->new_level != mddev->level) { |
cc6167b4 N |
7138 | pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", |
7139 | mdname(mddev)); | |
91adb564 N |
7140 | return -EINVAL; |
7141 | } | |
91adb564 N |
7142 | old_disks = mddev->raid_disks - mddev->delta_disks; |
7143 | /* reshape_position must be on a new-stripe boundary, and one | |
7144 | * further up in new geometry must map after here in old | |
7145 | * geometry. | |
05256d98 N |
7146 | * If the chunk sizes are different, then as we perform reshape |
7147 | * in units of the largest of the two, reshape_position needs | |
7148 | * be a multiple of the largest chunk size times new data disks. | |
91adb564 N |
7149 | */ |
7150 | here_new = mddev->reshape_position; | |
05256d98 N |
7151 | chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); |
7152 | new_data_disks = mddev->raid_disks - max_degraded; | |
7153 | if (sector_div(here_new, chunk_sectors * new_data_disks)) { | |
cc6167b4 N |
7154 | pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", |
7155 | mdname(mddev)); | |
91adb564 N |
7156 | return -EINVAL; |
7157 | } | |
05256d98 | 7158 | reshape_offset = here_new * chunk_sectors; |
91adb564 N |
7159 | /* here_new is the stripe we will write to */ |
7160 | here_old = mddev->reshape_position; | |
05256d98 | 7161 | sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); |
91adb564 N |
7162 | /* here_old is the first stripe that we might need to read |
7163 | * from */ | |
67ac6011 N |
7164 | if (mddev->delta_disks == 0) { |
7165 | /* We cannot be sure it is safe to start an in-place | |
b5254dd5 | 7166 | * reshape. It is only safe if user-space is monitoring |
67ac6011 N |
7167 | * and taking constant backups. |
7168 | * mdadm always starts a situation like this in | |
7169 | * readonly mode so it can take control before | |
7170 | * allowing any writes. So just check for that. | |
7171 | */ | |
b5254dd5 N |
7172 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
7173 | abs(min_offset_diff) >= mddev->new_chunk_sectors) | |
7174 | /* not really in-place - so OK */; | |
7175 | else if (mddev->ro == 0) { | |
cc6167b4 N |
7176 | pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", |
7177 | mdname(mddev)); | |
67ac6011 N |
7178 | return -EINVAL; |
7179 | } | |
2c810cdd | 7180 | } else if (mddev->reshape_backwards |
05256d98 N |
7181 | ? (here_new * chunk_sectors + min_offset_diff <= |
7182 | here_old * chunk_sectors) | |
7183 | : (here_new * chunk_sectors >= | |
7184 | here_old * chunk_sectors + (-min_offset_diff))) { | |
91adb564 | 7185 | /* Reading from the same stripe as writing to - bad */ |
cc6167b4 N |
7186 | pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", |
7187 | mdname(mddev)); | |
91adb564 N |
7188 | return -EINVAL; |
7189 | } | |
cc6167b4 | 7190 | pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); |
91adb564 N |
7191 | /* OK, we should be able to continue; */ |
7192 | } else { | |
7193 | BUG_ON(mddev->level != mddev->new_level); | |
7194 | BUG_ON(mddev->layout != mddev->new_layout); | |
664e7c41 | 7195 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
91adb564 | 7196 | BUG_ON(mddev->delta_disks != 0); |
1da177e4 | 7197 | } |
91adb564 | 7198 | |
3418d036 AP |
7199 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && |
7200 | test_bit(MD_HAS_PPL, &mddev->flags)) { | |
7201 | pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n", | |
7202 | mdname(mddev)); | |
7203 | clear_bit(MD_HAS_PPL, &mddev->flags); | |
7204 | } | |
7205 | ||
245f46c2 N |
7206 | if (mddev->private == NULL) |
7207 | conf = setup_conf(mddev); | |
7208 | else | |
7209 | conf = mddev->private; | |
7210 | ||
91adb564 N |
7211 | if (IS_ERR(conf)) |
7212 | return PTR_ERR(conf); | |
7213 | ||
486b0f7b SL |
7214 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
7215 | if (!journal_dev) { | |
cc6167b4 N |
7216 | pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", |
7217 | mdname(mddev)); | |
486b0f7b SL |
7218 | mddev->ro = 1; |
7219 | set_disk_ro(mddev->gendisk, 1); | |
7220 | } else if (mddev->recovery_cp == MaxSector) | |
7221 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); | |
7dde2ad3 SL |
7222 | } |
7223 | ||
b5254dd5 | 7224 | conf->min_offset_diff = min_offset_diff; |
91adb564 N |
7225 | mddev->thread = conf->thread; |
7226 | conf->thread = NULL; | |
7227 | mddev->private = conf; | |
7228 | ||
17045f52 N |
7229 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
7230 | i++) { | |
7231 | rdev = conf->disks[i].rdev; | |
7232 | if (!rdev && conf->disks[i].replacement) { | |
7233 | /* The replacement is all we have yet */ | |
7234 | rdev = conf->disks[i].replacement; | |
7235 | conf->disks[i].replacement = NULL; | |
7236 | clear_bit(Replacement, &rdev->flags); | |
7237 | conf->disks[i].rdev = rdev; | |
7238 | } | |
7239 | if (!rdev) | |
c148ffdc | 7240 | continue; |
17045f52 N |
7241 | if (conf->disks[i].replacement && |
7242 | conf->reshape_progress != MaxSector) { | |
7243 | /* replacements and reshape simply do not mix. */ | |
cc6167b4 | 7244 | pr_warn("md: cannot handle concurrent replacement and reshape.\n"); |
17045f52 N |
7245 | goto abort; |
7246 | } | |
2f115882 | 7247 | if (test_bit(In_sync, &rdev->flags)) { |
91adb564 | 7248 | working_disks++; |
2f115882 N |
7249 | continue; |
7250 | } | |
c148ffdc N |
7251 | /* This disc is not fully in-sync. However if it |
7252 | * just stored parity (beyond the recovery_offset), | |
7253 | * when we don't need to be concerned about the | |
7254 | * array being dirty. | |
7255 | * When reshape goes 'backwards', we never have | |
7256 | * partially completed devices, so we only need | |
7257 | * to worry about reshape going forwards. | |
7258 | */ | |
7259 | /* Hack because v0.91 doesn't store recovery_offset properly. */ | |
7260 | if (mddev->major_version == 0 && | |
7261 | mddev->minor_version > 90) | |
7262 | rdev->recovery_offset = reshape_offset; | |
5026d7a9 | 7263 | |
c148ffdc N |
7264 | if (rdev->recovery_offset < reshape_offset) { |
7265 | /* We need to check old and new layout */ | |
7266 | if (!only_parity(rdev->raid_disk, | |
7267 | conf->algorithm, | |
7268 | conf->raid_disks, | |
7269 | conf->max_degraded)) | |
7270 | continue; | |
7271 | } | |
7272 | if (!only_parity(rdev->raid_disk, | |
7273 | conf->prev_algo, | |
7274 | conf->previous_raid_disks, | |
7275 | conf->max_degraded)) | |
7276 | continue; | |
7277 | dirty_parity_disks++; | |
7278 | } | |
91adb564 | 7279 | |
17045f52 N |
7280 | /* |
7281 | * 0 for a fully functional array, 1 or 2 for a degraded array. | |
7282 | */ | |
2e38a37f | 7283 | mddev->degraded = raid5_calc_degraded(conf); |
91adb564 | 7284 | |
674806d6 | 7285 | if (has_failed(conf)) { |
cc6167b4 | 7286 | pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", |
02c2de8c | 7287 | mdname(mddev), mddev->degraded, conf->raid_disks); |
1da177e4 LT |
7288 | goto abort; |
7289 | } | |
7290 | ||
91adb564 | 7291 | /* device size must be a multiple of chunk size */ |
9d8f0363 | 7292 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
91adb564 N |
7293 | mddev->resync_max_sectors = mddev->dev_sectors; |
7294 | ||
c148ffdc | 7295 | if (mddev->degraded > dirty_parity_disks && |
1da177e4 | 7296 | mddev->recovery_cp != MaxSector) { |
4536bf9b AP |
7297 | if (test_bit(MD_HAS_PPL, &mddev->flags)) |
7298 | pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n", | |
7299 | mdname(mddev)); | |
7300 | else if (mddev->ok_start_degraded) | |
cc6167b4 N |
7301 | pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", |
7302 | mdname(mddev)); | |
6ff8d8ec | 7303 | else { |
cc6167b4 N |
7304 | pr_crit("md/raid:%s: cannot start dirty degraded array.\n", |
7305 | mdname(mddev)); | |
6ff8d8ec N |
7306 | goto abort; |
7307 | } | |
1da177e4 LT |
7308 | } |
7309 | ||
cc6167b4 N |
7310 | pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", |
7311 | mdname(mddev), conf->level, | |
7312 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, | |
7313 | mddev->new_layout); | |
1da177e4 LT |
7314 | |
7315 | print_raid5_conf(conf); | |
7316 | ||
fef9c61f | 7317 | if (conf->reshape_progress != MaxSector) { |
fef9c61f | 7318 | conf->reshape_safe = conf->reshape_progress; |
f6705578 N |
7319 | atomic_set(&conf->reshape_stripes, 0); |
7320 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
7321 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
7322 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
7323 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
7324 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
0da3c619 | 7325 | "reshape"); |
f6705578 N |
7326 | } |
7327 | ||
1da177e4 | 7328 | /* Ok, everything is just fine now */ |
a64c876f N |
7329 | if (mddev->to_remove == &raid5_attrs_group) |
7330 | mddev->to_remove = NULL; | |
00bcb4ac N |
7331 | else if (mddev->kobj.sd && |
7332 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) | |
cc6167b4 N |
7333 | pr_warn("raid5: failed to create sysfs attributes for %s\n", |
7334 | mdname(mddev)); | |
4a5add49 | 7335 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
7a5febe9 | 7336 | |
4a5add49 | 7337 | if (mddev->queue) { |
9f7c2220 | 7338 | int chunk_size; |
4a5add49 N |
7339 | /* read-ahead size must cover two whole stripes, which |
7340 | * is 2 * (datadisks) * chunksize where 'n' is the | |
7341 | * number of raid devices | |
7342 | */ | |
7343 | int data_disks = conf->previous_raid_disks - conf->max_degraded; | |
7344 | int stripe = data_disks * | |
7345 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); | |
dc3b17cc JK |
7346 | if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
7347 | mddev->queue->backing_dev_info->ra_pages = 2 * stripe; | |
91adb564 | 7348 | |
9f7c2220 N |
7349 | chunk_size = mddev->chunk_sectors << 9; |
7350 | blk_queue_io_min(mddev->queue, chunk_size); | |
7351 | blk_queue_io_opt(mddev->queue, chunk_size * | |
7352 | (conf->raid_disks - conf->max_degraded)); | |
c78afc62 | 7353 | mddev->queue->limits.raid_partial_stripes_expensive = 1; |
620125f2 SL |
7354 | /* |
7355 | * We can only discard a whole stripe. It doesn't make sense to | |
7356 | * discard data disk but write parity disk | |
7357 | */ | |
7358 | stripe = stripe * PAGE_SIZE; | |
4ac6875e N |
7359 | /* Round up to power of 2, as discard handling |
7360 | * currently assumes that */ | |
7361 | while ((stripe-1) & stripe) | |
7362 | stripe = (stripe | (stripe-1)) + 1; | |
620125f2 SL |
7363 | mddev->queue->limits.discard_alignment = stripe; |
7364 | mddev->queue->limits.discard_granularity = stripe; | |
e8d7c332 | 7365 | |
5026d7a9 | 7366 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
3deff1a7 | 7367 | blk_queue_max_write_zeroes_sectors(mddev->queue, 0); |
5026d7a9 | 7368 | |
05616be5 | 7369 | rdev_for_each(rdev, mddev) { |
9f7c2220 N |
7370 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
7371 | rdev->data_offset << 9); | |
05616be5 N |
7372 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
7373 | rdev->new_data_offset << 9); | |
7374 | } | |
620125f2 | 7375 | |
48920ff2 CH |
7376 | /* |
7377 | * zeroing is required, otherwise data | |
7378 | * could be lost. Consider a scenario: discard a stripe | |
7379 | * (the stripe could be inconsistent if | |
7380 | * discard_zeroes_data is 0); write one disk of the | |
7381 | * stripe (the stripe could be inconsistent again | |
7382 | * depending on which disks are used to calculate | |
7383 | * parity); the disk is broken; The stripe data of this | |
7384 | * disk is lost. | |
7385 | * | |
7386 | * We only allow DISCARD if the sysadmin has confirmed that | |
7387 | * only safe devices are in use by setting a module parameter. | |
7388 | * A better idea might be to turn DISCARD into WRITE_ZEROES | |
7389 | * requests, as that is required to be safe. | |
7390 | */ | |
7391 | if (devices_handle_discard_safely && | |
e7597e69 JS |
7392 | mddev->queue->limits.max_discard_sectors >= (stripe >> 9) && |
7393 | mddev->queue->limits.discard_granularity >= stripe) | |
620125f2 SL |
7394 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
7395 | mddev->queue); | |
7396 | else | |
7397 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, | |
7398 | mddev->queue); | |
1dffdddd SL |
7399 | |
7400 | blk_queue_max_hw_sectors(mddev->queue, UINT_MAX); | |
9f7c2220 | 7401 | } |
23032a0e | 7402 | |
845b9e22 | 7403 | if (log_init(conf, journal_dev, raid5_has_ppl(conf))) |
ff875738 | 7404 | goto abort; |
5c7e81c3 | 7405 | |
1da177e4 LT |
7406 | return 0; |
7407 | abort: | |
01f96c0a | 7408 | md_unregister_thread(&mddev->thread); |
e4f869d9 N |
7409 | print_raid5_conf(conf); |
7410 | free_conf(conf); | |
1da177e4 | 7411 | mddev->private = NULL; |
cc6167b4 | 7412 | pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
1da177e4 LT |
7413 | return -EIO; |
7414 | } | |
7415 | ||
afa0f557 | 7416 | static void raid5_free(struct mddev *mddev, void *priv) |
1da177e4 | 7417 | { |
afa0f557 | 7418 | struct r5conf *conf = priv; |
1da177e4 | 7419 | |
95fc17aa | 7420 | free_conf(conf); |
a64c876f | 7421 | mddev->to_remove = &raid5_attrs_group; |
1da177e4 LT |
7422 | } |
7423 | ||
849674e4 | 7424 | static void raid5_status(struct seq_file *seq, struct mddev *mddev) |
1da177e4 | 7425 | { |
d1688a6d | 7426 | struct r5conf *conf = mddev->private; |
1da177e4 LT |
7427 | int i; |
7428 | ||
9d8f0363 | 7429 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
3cb5edf4 | 7430 | conf->chunk_sectors / 2, mddev->layout); |
02c2de8c | 7431 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
5fd13351 N |
7432 | rcu_read_lock(); |
7433 | for (i = 0; i < conf->raid_disks; i++) { | |
7434 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); | |
7435 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); | |
7436 | } | |
7437 | rcu_read_unlock(); | |
1da177e4 | 7438 | seq_printf (seq, "]"); |
1da177e4 LT |
7439 | } |
7440 | ||
d1688a6d | 7441 | static void print_raid5_conf (struct r5conf *conf) |
1da177e4 LT |
7442 | { |
7443 | int i; | |
7444 | struct disk_info *tmp; | |
7445 | ||
cc6167b4 | 7446 | pr_debug("RAID conf printout:\n"); |
1da177e4 | 7447 | if (!conf) { |
cc6167b4 | 7448 | pr_debug("(conf==NULL)\n"); |
1da177e4 LT |
7449 | return; |
7450 | } | |
cc6167b4 | 7451 | pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, |
0c55e022 N |
7452 | conf->raid_disks, |
7453 | conf->raid_disks - conf->mddev->degraded); | |
1da177e4 LT |
7454 | |
7455 | for (i = 0; i < conf->raid_disks; i++) { | |
7456 | char b[BDEVNAME_SIZE]; | |
7457 | tmp = conf->disks + i; | |
7458 | if (tmp->rdev) | |
cc6167b4 | 7459 | pr_debug(" disk %d, o:%d, dev:%s\n", |
0c55e022 N |
7460 | i, !test_bit(Faulty, &tmp->rdev->flags), |
7461 | bdevname(tmp->rdev->bdev, b)); | |
1da177e4 LT |
7462 | } |
7463 | } | |
7464 | ||
fd01b88c | 7465 | static int raid5_spare_active(struct mddev *mddev) |
1da177e4 LT |
7466 | { |
7467 | int i; | |
d1688a6d | 7468 | struct r5conf *conf = mddev->private; |
1da177e4 | 7469 | struct disk_info *tmp; |
6b965620 N |
7470 | int count = 0; |
7471 | unsigned long flags; | |
1da177e4 LT |
7472 | |
7473 | for (i = 0; i < conf->raid_disks; i++) { | |
7474 | tmp = conf->disks + i; | |
dd054fce N |
7475 | if (tmp->replacement |
7476 | && tmp->replacement->recovery_offset == MaxSector | |
7477 | && !test_bit(Faulty, &tmp->replacement->flags) | |
7478 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { | |
7479 | /* Replacement has just become active. */ | |
7480 | if (!tmp->rdev | |
7481 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) | |
7482 | count++; | |
7483 | if (tmp->rdev) { | |
7484 | /* Replaced device not technically faulty, | |
7485 | * but we need to be sure it gets removed | |
7486 | * and never re-added. | |
7487 | */ | |
7488 | set_bit(Faulty, &tmp->rdev->flags); | |
7489 | sysfs_notify_dirent_safe( | |
7490 | tmp->rdev->sysfs_state); | |
7491 | } | |
7492 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); | |
7493 | } else if (tmp->rdev | |
70fffd0b | 7494 | && tmp->rdev->recovery_offset == MaxSector |
b2d444d7 | 7495 | && !test_bit(Faulty, &tmp->rdev->flags) |
c04be0aa | 7496 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
6b965620 | 7497 | count++; |
43c73ca4 | 7498 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
1da177e4 LT |
7499 | } |
7500 | } | |
6b965620 | 7501 | spin_lock_irqsave(&conf->device_lock, flags); |
2e38a37f | 7502 | mddev->degraded = raid5_calc_degraded(conf); |
6b965620 | 7503 | spin_unlock_irqrestore(&conf->device_lock, flags); |
1da177e4 | 7504 | print_raid5_conf(conf); |
6b965620 | 7505 | return count; |
1da177e4 LT |
7506 | } |
7507 | ||
b8321b68 | 7508 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 7509 | { |
d1688a6d | 7510 | struct r5conf *conf = mddev->private; |
1da177e4 | 7511 | int err = 0; |
b8321b68 | 7512 | int number = rdev->raid_disk; |
657e3e4d | 7513 | struct md_rdev **rdevp; |
1da177e4 LT |
7514 | struct disk_info *p = conf->disks + number; |
7515 | ||
7516 | print_raid5_conf(conf); | |
f6b6ec5c | 7517 | if (test_bit(Journal, &rdev->flags) && conf->log) { |
c2bb6242 | 7518 | /* |
f6b6ec5c SL |
7519 | * we can't wait pending write here, as this is called in |
7520 | * raid5d, wait will deadlock. | |
84dd97a6 N |
7521 | * neilb: there is no locking about new writes here, |
7522 | * so this cannot be safe. | |
c2bb6242 | 7523 | */ |
84dd97a6 | 7524 | if (atomic_read(&conf->active_stripes)) { |
f6b6ec5c | 7525 | return -EBUSY; |
84dd97a6 | 7526 | } |
ff875738 | 7527 | log_exit(conf); |
f6b6ec5c | 7528 | return 0; |
c2bb6242 | 7529 | } |
657e3e4d N |
7530 | if (rdev == p->rdev) |
7531 | rdevp = &p->rdev; | |
7532 | else if (rdev == p->replacement) | |
7533 | rdevp = &p->replacement; | |
7534 | else | |
7535 | return 0; | |
7536 | ||
7537 | if (number >= conf->raid_disks && | |
7538 | conf->reshape_progress == MaxSector) | |
7539 | clear_bit(In_sync, &rdev->flags); | |
7540 | ||
7541 | if (test_bit(In_sync, &rdev->flags) || | |
7542 | atomic_read(&rdev->nr_pending)) { | |
7543 | err = -EBUSY; | |
7544 | goto abort; | |
7545 | } | |
7546 | /* Only remove non-faulty devices if recovery | |
7547 | * isn't possible. | |
7548 | */ | |
7549 | if (!test_bit(Faulty, &rdev->flags) && | |
7550 | mddev->recovery_disabled != conf->recovery_disabled && | |
7551 | !has_failed(conf) && | |
dd054fce | 7552 | (!p->replacement || p->replacement == rdev) && |
657e3e4d N |
7553 | number < conf->raid_disks) { |
7554 | err = -EBUSY; | |
7555 | goto abort; | |
7556 | } | |
7557 | *rdevp = NULL; | |
d787be40 N |
7558 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
7559 | synchronize_rcu(); | |
7560 | if (atomic_read(&rdev->nr_pending)) { | |
7561 | /* lost the race, try later */ | |
7562 | err = -EBUSY; | |
7563 | *rdevp = rdev; | |
7564 | } | |
7565 | } | |
6358c239 AP |
7566 | if (!err) { |
7567 | err = log_modify(conf, rdev, false); | |
7568 | if (err) | |
7569 | goto abort; | |
7570 | } | |
d787be40 | 7571 | if (p->replacement) { |
dd054fce N |
7572 | /* We must have just cleared 'rdev' */ |
7573 | p->rdev = p->replacement; | |
7574 | clear_bit(Replacement, &p->replacement->flags); | |
7575 | smp_mb(); /* Make sure other CPUs may see both as identical | |
7576 | * but will never see neither - if they are careful | |
7577 | */ | |
7578 | p->replacement = NULL; | |
6358c239 AP |
7579 | |
7580 | if (!err) | |
7581 | err = log_modify(conf, p->rdev, true); | |
e5bc9c3c GJ |
7582 | } |
7583 | ||
7584 | clear_bit(WantReplacement, &rdev->flags); | |
1da177e4 LT |
7585 | abort: |
7586 | ||
7587 | print_raid5_conf(conf); | |
7588 | return err; | |
7589 | } | |
7590 | ||
fd01b88c | 7591 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
1da177e4 | 7592 | { |
d1688a6d | 7593 | struct r5conf *conf = mddev->private; |
199050ea | 7594 | int err = -EEXIST; |
1da177e4 LT |
7595 | int disk; |
7596 | struct disk_info *p; | |
6c2fce2e NB |
7597 | int first = 0; |
7598 | int last = conf->raid_disks - 1; | |
1da177e4 | 7599 | |
f6b6ec5c | 7600 | if (test_bit(Journal, &rdev->flags)) { |
f6b6ec5c SL |
7601 | if (conf->log) |
7602 | return -EBUSY; | |
7603 | ||
7604 | rdev->raid_disk = 0; | |
7605 | /* | |
7606 | * The array is in readonly mode if journal is missing, so no | |
7607 | * write requests running. We should be safe | |
7608 | */ | |
845b9e22 | 7609 | log_init(conf, rdev, false); |
f6b6ec5c SL |
7610 | return 0; |
7611 | } | |
7f0da59b N |
7612 | if (mddev->recovery_disabled == conf->recovery_disabled) |
7613 | return -EBUSY; | |
7614 | ||
dc10c643 | 7615 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
1da177e4 | 7616 | /* no point adding a device */ |
199050ea | 7617 | return -EINVAL; |
1da177e4 | 7618 | |
6c2fce2e NB |
7619 | if (rdev->raid_disk >= 0) |
7620 | first = last = rdev->raid_disk; | |
1da177e4 LT |
7621 | |
7622 | /* | |
16a53ecc N |
7623 | * find the disk ... but prefer rdev->saved_raid_disk |
7624 | * if possible. | |
1da177e4 | 7625 | */ |
16a53ecc | 7626 | if (rdev->saved_raid_disk >= 0 && |
6c2fce2e | 7627 | rdev->saved_raid_disk >= first && |
16a53ecc | 7628 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
5cfb22a1 N |
7629 | first = rdev->saved_raid_disk; |
7630 | ||
7631 | for (disk = first; disk <= last; disk++) { | |
7bfec5f3 N |
7632 | p = conf->disks + disk; |
7633 | if (p->rdev == NULL) { | |
b2d444d7 | 7634 | clear_bit(In_sync, &rdev->flags); |
1da177e4 | 7635 | rdev->raid_disk = disk; |
72626685 N |
7636 | if (rdev->saved_raid_disk != disk) |
7637 | conf->fullsync = 1; | |
d6065f7b | 7638 | rcu_assign_pointer(p->rdev, rdev); |
6358c239 AP |
7639 | |
7640 | err = log_modify(conf, rdev, true); | |
7641 | ||
5cfb22a1 | 7642 | goto out; |
1da177e4 | 7643 | } |
5cfb22a1 N |
7644 | } |
7645 | for (disk = first; disk <= last; disk++) { | |
7646 | p = conf->disks + disk; | |
7bfec5f3 N |
7647 | if (test_bit(WantReplacement, &p->rdev->flags) && |
7648 | p->replacement == NULL) { | |
7649 | clear_bit(In_sync, &rdev->flags); | |
7650 | set_bit(Replacement, &rdev->flags); | |
7651 | rdev->raid_disk = disk; | |
7652 | err = 0; | |
7653 | conf->fullsync = 1; | |
7654 | rcu_assign_pointer(p->replacement, rdev); | |
7655 | break; | |
7656 | } | |
7657 | } | |
5cfb22a1 | 7658 | out: |
1da177e4 | 7659 | print_raid5_conf(conf); |
199050ea | 7660 | return err; |
1da177e4 LT |
7661 | } |
7662 | ||
fd01b88c | 7663 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
1da177e4 LT |
7664 | { |
7665 | /* no resync is happening, and there is enough space | |
7666 | * on all devices, so we can resize. | |
7667 | * We need to make sure resync covers any new space. | |
7668 | * If the array is shrinking we should possibly wait until | |
7669 | * any io in the removed space completes, but it hardly seems | |
7670 | * worth it. | |
7671 | */ | |
a4a6125a | 7672 | sector_t newsize; |
3cb5edf4 N |
7673 | struct r5conf *conf = mddev->private; |
7674 | ||
3418d036 | 7675 | if (conf->log || raid5_has_ppl(conf)) |
713cf5a6 | 7676 | return -EINVAL; |
3cb5edf4 | 7677 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
a4a6125a N |
7678 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
7679 | if (mddev->external_size && | |
7680 | mddev->array_sectors > newsize) | |
b522adcd | 7681 | return -EINVAL; |
a4a6125a N |
7682 | if (mddev->bitmap) { |
7683 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); | |
7684 | if (ret) | |
7685 | return ret; | |
7686 | } | |
7687 | md_set_array_sectors(mddev, newsize); | |
b098636c N |
7688 | if (sectors > mddev->dev_sectors && |
7689 | mddev->recovery_cp > mddev->dev_sectors) { | |
58c0fed4 | 7690 | mddev->recovery_cp = mddev->dev_sectors; |
1da177e4 LT |
7691 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
7692 | } | |
58c0fed4 | 7693 | mddev->dev_sectors = sectors; |
4b5c7ae8 | 7694 | mddev->resync_max_sectors = sectors; |
1da177e4 LT |
7695 | return 0; |
7696 | } | |
7697 | ||
fd01b88c | 7698 | static int check_stripe_cache(struct mddev *mddev) |
01ee22b4 N |
7699 | { |
7700 | /* Can only proceed if there are plenty of stripe_heads. | |
7701 | * We need a minimum of one full stripe,, and for sensible progress | |
7702 | * it is best to have about 4 times that. | |
7703 | * If we require 4 times, then the default 256 4K stripe_heads will | |
7704 | * allow for chunk sizes up to 256K, which is probably OK. | |
7705 | * If the chunk size is greater, user-space should request more | |
7706 | * stripe_heads first. | |
7707 | */ | |
d1688a6d | 7708 | struct r5conf *conf = mddev->private; |
01ee22b4 | 7709 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
edbe83ab | 7710 | > conf->min_nr_stripes || |
01ee22b4 | 7711 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
edbe83ab | 7712 | > conf->min_nr_stripes) { |
cc6167b4 N |
7713 | pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
7714 | mdname(mddev), | |
7715 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) | |
7716 | / STRIPE_SIZE)*4); | |
01ee22b4 N |
7717 | return 0; |
7718 | } | |
7719 | return 1; | |
7720 | } | |
7721 | ||
fd01b88c | 7722 | static int check_reshape(struct mddev *mddev) |
29269553 | 7723 | { |
d1688a6d | 7724 | struct r5conf *conf = mddev->private; |
29269553 | 7725 | |
3418d036 | 7726 | if (conf->log || raid5_has_ppl(conf)) |
713cf5a6 | 7727 | return -EINVAL; |
88ce4930 N |
7728 | if (mddev->delta_disks == 0 && |
7729 | mddev->new_layout == mddev->layout && | |
664e7c41 | 7730 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
50ac168a | 7731 | return 0; /* nothing to do */ |
674806d6 | 7732 | if (has_failed(conf)) |
ec32a2bd | 7733 | return -EINVAL; |
fdcfbbb6 | 7734 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
ec32a2bd N |
7735 | /* We might be able to shrink, but the devices must |
7736 | * be made bigger first. | |
7737 | * For raid6, 4 is the minimum size. | |
7738 | * Otherwise 2 is the minimum | |
7739 | */ | |
7740 | int min = 2; | |
7741 | if (mddev->level == 6) | |
7742 | min = 4; | |
7743 | if (mddev->raid_disks + mddev->delta_disks < min) | |
7744 | return -EINVAL; | |
7745 | } | |
29269553 | 7746 | |
01ee22b4 | 7747 | if (!check_stripe_cache(mddev)) |
29269553 | 7748 | return -ENOSPC; |
29269553 | 7749 | |
738a2738 N |
7750 | if (mddev->new_chunk_sectors > mddev->chunk_sectors || |
7751 | mddev->delta_disks > 0) | |
7752 | if (resize_chunks(conf, | |
7753 | conf->previous_raid_disks | |
7754 | + max(0, mddev->delta_disks), | |
7755 | max(mddev->new_chunk_sectors, | |
7756 | mddev->chunk_sectors) | |
7757 | ) < 0) | |
7758 | return -ENOMEM; | |
845b9e22 AP |
7759 | |
7760 | if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size) | |
7761 | return 0; /* never bother to shrink */ | |
e56108d6 N |
7762 | return resize_stripes(conf, (conf->previous_raid_disks |
7763 | + mddev->delta_disks)); | |
63c70c4f N |
7764 | } |
7765 | ||
fd01b88c | 7766 | static int raid5_start_reshape(struct mddev *mddev) |
63c70c4f | 7767 | { |
d1688a6d | 7768 | struct r5conf *conf = mddev->private; |
3cb03002 | 7769 | struct md_rdev *rdev; |
63c70c4f | 7770 | int spares = 0; |
c04be0aa | 7771 | unsigned long flags; |
63c70c4f | 7772 | |
f416885e | 7773 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
63c70c4f N |
7774 | return -EBUSY; |
7775 | ||
01ee22b4 N |
7776 | if (!check_stripe_cache(mddev)) |
7777 | return -ENOSPC; | |
7778 | ||
30b67645 N |
7779 | if (has_failed(conf)) |
7780 | return -EINVAL; | |
7781 | ||
c6563a8c | 7782 | rdev_for_each(rdev, mddev) { |
469518a3 N |
7783 | if (!test_bit(In_sync, &rdev->flags) |
7784 | && !test_bit(Faulty, &rdev->flags)) | |
29269553 | 7785 | spares++; |
c6563a8c | 7786 | } |
63c70c4f | 7787 | |
f416885e | 7788 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
29269553 N |
7789 | /* Not enough devices even to make a degraded array |
7790 | * of that size | |
7791 | */ | |
7792 | return -EINVAL; | |
7793 | ||
ec32a2bd N |
7794 | /* Refuse to reduce size of the array. Any reductions in |
7795 | * array size must be through explicit setting of array_size | |
7796 | * attribute. | |
7797 | */ | |
7798 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) | |
7799 | < mddev->array_sectors) { | |
cc6167b4 N |
7800 | pr_warn("md/raid:%s: array size must be reduced before number of disks\n", |
7801 | mdname(mddev)); | |
ec32a2bd N |
7802 | return -EINVAL; |
7803 | } | |
7804 | ||
f6705578 | 7805 | atomic_set(&conf->reshape_stripes, 0); |
29269553 | 7806 | spin_lock_irq(&conf->device_lock); |
c46501b2 | 7807 | write_seqcount_begin(&conf->gen_lock); |
29269553 | 7808 | conf->previous_raid_disks = conf->raid_disks; |
63c70c4f | 7809 | conf->raid_disks += mddev->delta_disks; |
09c9e5fa AN |
7810 | conf->prev_chunk_sectors = conf->chunk_sectors; |
7811 | conf->chunk_sectors = mddev->new_chunk_sectors; | |
88ce4930 N |
7812 | conf->prev_algo = conf->algorithm; |
7813 | conf->algorithm = mddev->new_layout; | |
05616be5 N |
7814 | conf->generation++; |
7815 | /* Code that selects data_offset needs to see the generation update | |
7816 | * if reshape_progress has been set - so a memory barrier needed. | |
7817 | */ | |
7818 | smp_mb(); | |
2c810cdd | 7819 | if (mddev->reshape_backwards) |
fef9c61f N |
7820 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
7821 | else | |
7822 | conf->reshape_progress = 0; | |
7823 | conf->reshape_safe = conf->reshape_progress; | |
c46501b2 | 7824 | write_seqcount_end(&conf->gen_lock); |
29269553 N |
7825 | spin_unlock_irq(&conf->device_lock); |
7826 | ||
4d77e3ba N |
7827 | /* Now make sure any requests that proceeded on the assumption |
7828 | * the reshape wasn't running - like Discard or Read - have | |
7829 | * completed. | |
7830 | */ | |
7831 | mddev_suspend(mddev); | |
7832 | mddev_resume(mddev); | |
7833 | ||
29269553 N |
7834 | /* Add some new drives, as many as will fit. |
7835 | * We know there are enough to make the newly sized array work. | |
3424bf6a N |
7836 | * Don't add devices if we are reducing the number of |
7837 | * devices in the array. This is because it is not possible | |
7838 | * to correctly record the "partially reconstructed" state of | |
7839 | * such devices during the reshape and confusion could result. | |
29269553 | 7840 | */ |
87a8dec9 | 7841 | if (mddev->delta_disks >= 0) { |
dafb20fa | 7842 | rdev_for_each(rdev, mddev) |
87a8dec9 N |
7843 | if (rdev->raid_disk < 0 && |
7844 | !test_bit(Faulty, &rdev->flags)) { | |
7845 | if (raid5_add_disk(mddev, rdev) == 0) { | |
87a8dec9 | 7846 | if (rdev->raid_disk |
9d4c7d87 | 7847 | >= conf->previous_raid_disks) |
87a8dec9 | 7848 | set_bit(In_sync, &rdev->flags); |
9d4c7d87 | 7849 | else |
87a8dec9 | 7850 | rdev->recovery_offset = 0; |
36fad858 NK |
7851 | |
7852 | if (sysfs_link_rdev(mddev, rdev)) | |
87a8dec9 | 7853 | /* Failure here is OK */; |
50da0840 | 7854 | } |
87a8dec9 N |
7855 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
7856 | && !test_bit(Faulty, &rdev->flags)) { | |
7857 | /* This is a spare that was manually added */ | |
7858 | set_bit(In_sync, &rdev->flags); | |
87a8dec9 | 7859 | } |
29269553 | 7860 | |
87a8dec9 N |
7861 | /* When a reshape changes the number of devices, |
7862 | * ->degraded is measured against the larger of the | |
7863 | * pre and post number of devices. | |
7864 | */ | |
ec32a2bd | 7865 | spin_lock_irqsave(&conf->device_lock, flags); |
2e38a37f | 7866 | mddev->degraded = raid5_calc_degraded(conf); |
ec32a2bd N |
7867 | spin_unlock_irqrestore(&conf->device_lock, flags); |
7868 | } | |
63c70c4f | 7869 | mddev->raid_disks = conf->raid_disks; |
e516402c | 7870 | mddev->reshape_position = conf->reshape_progress; |
2953079c | 7871 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
f6705578 | 7872 | |
29269553 N |
7873 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
7874 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
ea358cd0 | 7875 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
29269553 N |
7876 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
7877 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
7878 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
0da3c619 | 7879 | "reshape"); |
29269553 N |
7880 | if (!mddev->sync_thread) { |
7881 | mddev->recovery = 0; | |
7882 | spin_lock_irq(&conf->device_lock); | |
ba8805b9 | 7883 | write_seqcount_begin(&conf->gen_lock); |
29269553 | 7884 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
ba8805b9 N |
7885 | mddev->new_chunk_sectors = |
7886 | conf->chunk_sectors = conf->prev_chunk_sectors; | |
7887 | mddev->new_layout = conf->algorithm = conf->prev_algo; | |
05616be5 N |
7888 | rdev_for_each(rdev, mddev) |
7889 | rdev->new_data_offset = rdev->data_offset; | |
7890 | smp_wmb(); | |
ba8805b9 | 7891 | conf->generation --; |
fef9c61f | 7892 | conf->reshape_progress = MaxSector; |
1e3fa9bd | 7893 | mddev->reshape_position = MaxSector; |
ba8805b9 | 7894 | write_seqcount_end(&conf->gen_lock); |
29269553 N |
7895 | spin_unlock_irq(&conf->device_lock); |
7896 | return -EAGAIN; | |
7897 | } | |
c8f517c4 | 7898 | conf->reshape_checkpoint = jiffies; |
29269553 N |
7899 | md_wakeup_thread(mddev->sync_thread); |
7900 | md_new_event(mddev); | |
7901 | return 0; | |
7902 | } | |
29269553 | 7903 | |
ec32a2bd N |
7904 | /* This is called from the reshape thread and should make any |
7905 | * changes needed in 'conf' | |
7906 | */ | |
d1688a6d | 7907 | static void end_reshape(struct r5conf *conf) |
29269553 | 7908 | { |
29269553 | 7909 | |
f6705578 | 7910 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
05616be5 | 7911 | struct md_rdev *rdev; |
f6705578 | 7912 | |
f6705578 | 7913 | spin_lock_irq(&conf->device_lock); |
cea9c228 | 7914 | conf->previous_raid_disks = conf->raid_disks; |
05616be5 N |
7915 | rdev_for_each(rdev, conf->mddev) |
7916 | rdev->data_offset = rdev->new_data_offset; | |
7917 | smp_wmb(); | |
fef9c61f | 7918 | conf->reshape_progress = MaxSector; |
6cbd8148 | 7919 | conf->mddev->reshape_position = MaxSector; |
f6705578 | 7920 | spin_unlock_irq(&conf->device_lock); |
b0f9ec04 | 7921 | wake_up(&conf->wait_for_overlap); |
16a53ecc N |
7922 | |
7923 | /* read-ahead size must cover two whole stripes, which is | |
7924 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices | |
7925 | */ | |
4a5add49 | 7926 | if (conf->mddev->queue) { |
cea9c228 | 7927 | int data_disks = conf->raid_disks - conf->max_degraded; |
09c9e5fa | 7928 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
cea9c228 | 7929 | / PAGE_SIZE); |
dc3b17cc JK |
7930 | if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
7931 | conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe; | |
16a53ecc | 7932 | } |
29269553 | 7933 | } |
29269553 N |
7934 | } |
7935 | ||
ec32a2bd N |
7936 | /* This is called from the raid5d thread with mddev_lock held. |
7937 | * It makes config changes to the device. | |
7938 | */ | |
fd01b88c | 7939 | static void raid5_finish_reshape(struct mddev *mddev) |
cea9c228 | 7940 | { |
d1688a6d | 7941 | struct r5conf *conf = mddev->private; |
cea9c228 N |
7942 | |
7943 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { | |
7944 | ||
ec32a2bd N |
7945 | if (mddev->delta_disks > 0) { |
7946 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); | |
fe67d19a HM |
7947 | if (mddev->queue) { |
7948 | set_capacity(mddev->gendisk, mddev->array_sectors); | |
7949 | revalidate_disk(mddev->gendisk); | |
7950 | } | |
ec32a2bd N |
7951 | } else { |
7952 | int d; | |
908f4fbd | 7953 | spin_lock_irq(&conf->device_lock); |
2e38a37f | 7954 | mddev->degraded = raid5_calc_degraded(conf); |
908f4fbd | 7955 | spin_unlock_irq(&conf->device_lock); |
ec32a2bd N |
7956 | for (d = conf->raid_disks ; |
7957 | d < conf->raid_disks - mddev->delta_disks; | |
1a67dde0 | 7958 | d++) { |
3cb03002 | 7959 | struct md_rdev *rdev = conf->disks[d].rdev; |
da7613b8 N |
7960 | if (rdev) |
7961 | clear_bit(In_sync, &rdev->flags); | |
7962 | rdev = conf->disks[d].replacement; | |
7963 | if (rdev) | |
7964 | clear_bit(In_sync, &rdev->flags); | |
1a67dde0 | 7965 | } |
cea9c228 | 7966 | } |
88ce4930 | 7967 | mddev->layout = conf->algorithm; |
09c9e5fa | 7968 | mddev->chunk_sectors = conf->chunk_sectors; |
ec32a2bd N |
7969 | mddev->reshape_position = MaxSector; |
7970 | mddev->delta_disks = 0; | |
2c810cdd | 7971 | mddev->reshape_backwards = 0; |
cea9c228 N |
7972 | } |
7973 | } | |
7974 | ||
fd01b88c | 7975 | static void raid5_quiesce(struct mddev *mddev, int state) |
72626685 | 7976 | { |
d1688a6d | 7977 | struct r5conf *conf = mddev->private; |
72626685 N |
7978 | |
7979 | switch(state) { | |
e464eafd N |
7980 | case 2: /* resume for a suspend */ |
7981 | wake_up(&conf->wait_for_overlap); | |
7982 | break; | |
7983 | ||
72626685 | 7984 | case 1: /* stop all writes */ |
566c09c5 | 7985 | lock_all_device_hash_locks_irq(conf); |
64bd660b N |
7986 | /* '2' tells resync/reshape to pause so that all |
7987 | * active stripes can drain | |
7988 | */ | |
a39f7afd | 7989 | r5c_flush_cache(conf, INT_MAX); |
64bd660b | 7990 | conf->quiesce = 2; |
b1b46486 | 7991 | wait_event_cmd(conf->wait_for_quiescent, |
46031f9a RBJ |
7992 | atomic_read(&conf->active_stripes) == 0 && |
7993 | atomic_read(&conf->active_aligned_reads) == 0, | |
566c09c5 SL |
7994 | unlock_all_device_hash_locks_irq(conf), |
7995 | lock_all_device_hash_locks_irq(conf)); | |
64bd660b | 7996 | conf->quiesce = 1; |
566c09c5 | 7997 | unlock_all_device_hash_locks_irq(conf); |
64bd660b N |
7998 | /* allow reshape to continue */ |
7999 | wake_up(&conf->wait_for_overlap); | |
72626685 N |
8000 | break; |
8001 | ||
8002 | case 0: /* re-enable writes */ | |
566c09c5 | 8003 | lock_all_device_hash_locks_irq(conf); |
72626685 | 8004 | conf->quiesce = 0; |
b1b46486 | 8005 | wake_up(&conf->wait_for_quiescent); |
e464eafd | 8006 | wake_up(&conf->wait_for_overlap); |
566c09c5 | 8007 | unlock_all_device_hash_locks_irq(conf); |
72626685 N |
8008 | break; |
8009 | } | |
e6c033f7 | 8010 | r5l_quiesce(conf->log, state); |
72626685 | 8011 | } |
b15c2e57 | 8012 | |
fd01b88c | 8013 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
54071b38 | 8014 | { |
e373ab10 | 8015 | struct r0conf *raid0_conf = mddev->private; |
d76c8420 | 8016 | sector_t sectors; |
54071b38 | 8017 | |
f1b29bca | 8018 | /* for raid0 takeover only one zone is supported */ |
e373ab10 | 8019 | if (raid0_conf->nr_strip_zones > 1) { |
cc6167b4 N |
8020 | pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
8021 | mdname(mddev)); | |
f1b29bca DW |
8022 | return ERR_PTR(-EINVAL); |
8023 | } | |
8024 | ||
e373ab10 N |
8025 | sectors = raid0_conf->strip_zone[0].zone_end; |
8026 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); | |
3b71bd93 | 8027 | mddev->dev_sectors = sectors; |
f1b29bca | 8028 | mddev->new_level = level; |
54071b38 TM |
8029 | mddev->new_layout = ALGORITHM_PARITY_N; |
8030 | mddev->new_chunk_sectors = mddev->chunk_sectors; | |
8031 | mddev->raid_disks += 1; | |
8032 | mddev->delta_disks = 1; | |
8033 | /* make sure it will be not marked as dirty */ | |
8034 | mddev->recovery_cp = MaxSector; | |
8035 | ||
8036 | return setup_conf(mddev); | |
8037 | } | |
8038 | ||
fd01b88c | 8039 | static void *raid5_takeover_raid1(struct mddev *mddev) |
d562b0c4 N |
8040 | { |
8041 | int chunksect; | |
6995f0b2 | 8042 | void *ret; |
d562b0c4 N |
8043 | |
8044 | if (mddev->raid_disks != 2 || | |
8045 | mddev->degraded > 1) | |
8046 | return ERR_PTR(-EINVAL); | |
8047 | ||
8048 | /* Should check if there are write-behind devices? */ | |
8049 | ||
8050 | chunksect = 64*2; /* 64K by default */ | |
8051 | ||
8052 | /* The array must be an exact multiple of chunksize */ | |
8053 | while (chunksect && (mddev->array_sectors & (chunksect-1))) | |
8054 | chunksect >>= 1; | |
8055 | ||
8056 | if ((chunksect<<9) < STRIPE_SIZE) | |
8057 | /* array size does not allow a suitable chunk size */ | |
8058 | return ERR_PTR(-EINVAL); | |
8059 | ||
8060 | mddev->new_level = 5; | |
8061 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; | |
664e7c41 | 8062 | mddev->new_chunk_sectors = chunksect; |
d562b0c4 | 8063 | |
6995f0b2 | 8064 | ret = setup_conf(mddev); |
32cd7cbb | 8065 | if (!IS_ERR(ret)) |
394ed8e4 SL |
8066 | mddev_clear_unsupported_flags(mddev, |
8067 | UNSUPPORTED_MDDEV_FLAGS); | |
6995f0b2 | 8068 | return ret; |
d562b0c4 N |
8069 | } |
8070 | ||
fd01b88c | 8071 | static void *raid5_takeover_raid6(struct mddev *mddev) |
fc9739c6 N |
8072 | { |
8073 | int new_layout; | |
8074 | ||
8075 | switch (mddev->layout) { | |
8076 | case ALGORITHM_LEFT_ASYMMETRIC_6: | |
8077 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; | |
8078 | break; | |
8079 | case ALGORITHM_RIGHT_ASYMMETRIC_6: | |
8080 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; | |
8081 | break; | |
8082 | case ALGORITHM_LEFT_SYMMETRIC_6: | |
8083 | new_layout = ALGORITHM_LEFT_SYMMETRIC; | |
8084 | break; | |
8085 | case ALGORITHM_RIGHT_SYMMETRIC_6: | |
8086 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; | |
8087 | break; | |
8088 | case ALGORITHM_PARITY_0_6: | |
8089 | new_layout = ALGORITHM_PARITY_0; | |
8090 | break; | |
8091 | case ALGORITHM_PARITY_N: | |
8092 | new_layout = ALGORITHM_PARITY_N; | |
8093 | break; | |
8094 | default: | |
8095 | return ERR_PTR(-EINVAL); | |
8096 | } | |
8097 | mddev->new_level = 5; | |
8098 | mddev->new_layout = new_layout; | |
8099 | mddev->delta_disks = -1; | |
8100 | mddev->raid_disks -= 1; | |
8101 | return setup_conf(mddev); | |
8102 | } | |
8103 | ||
fd01b88c | 8104 | static int raid5_check_reshape(struct mddev *mddev) |
b3546035 | 8105 | { |
88ce4930 N |
8106 | /* For a 2-drive array, the layout and chunk size can be changed |
8107 | * immediately as not restriping is needed. | |
8108 | * For larger arrays we record the new value - after validation | |
8109 | * to be used by a reshape pass. | |
b3546035 | 8110 | */ |
d1688a6d | 8111 | struct r5conf *conf = mddev->private; |
597a711b | 8112 | int new_chunk = mddev->new_chunk_sectors; |
b3546035 | 8113 | |
597a711b | 8114 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
b3546035 N |
8115 | return -EINVAL; |
8116 | if (new_chunk > 0) { | |
0ba459d2 | 8117 | if (!is_power_of_2(new_chunk)) |
b3546035 | 8118 | return -EINVAL; |
597a711b | 8119 | if (new_chunk < (PAGE_SIZE>>9)) |
b3546035 | 8120 | return -EINVAL; |
597a711b | 8121 | if (mddev->array_sectors & (new_chunk-1)) |
b3546035 N |
8122 | /* not factor of array size */ |
8123 | return -EINVAL; | |
8124 | } | |
8125 | ||
8126 | /* They look valid */ | |
8127 | ||
88ce4930 | 8128 | if (mddev->raid_disks == 2) { |
597a711b N |
8129 | /* can make the change immediately */ |
8130 | if (mddev->new_layout >= 0) { | |
8131 | conf->algorithm = mddev->new_layout; | |
8132 | mddev->layout = mddev->new_layout; | |
88ce4930 N |
8133 | } |
8134 | if (new_chunk > 0) { | |
597a711b N |
8135 | conf->chunk_sectors = new_chunk ; |
8136 | mddev->chunk_sectors = new_chunk; | |
88ce4930 | 8137 | } |
2953079c | 8138 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
88ce4930 | 8139 | md_wakeup_thread(mddev->thread); |
b3546035 | 8140 | } |
50ac168a | 8141 | return check_reshape(mddev); |
88ce4930 N |
8142 | } |
8143 | ||
fd01b88c | 8144 | static int raid6_check_reshape(struct mddev *mddev) |
88ce4930 | 8145 | { |
597a711b | 8146 | int new_chunk = mddev->new_chunk_sectors; |
50ac168a | 8147 | |
597a711b | 8148 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
88ce4930 | 8149 | return -EINVAL; |
b3546035 | 8150 | if (new_chunk > 0) { |
0ba459d2 | 8151 | if (!is_power_of_2(new_chunk)) |
88ce4930 | 8152 | return -EINVAL; |
597a711b | 8153 | if (new_chunk < (PAGE_SIZE >> 9)) |
88ce4930 | 8154 | return -EINVAL; |
597a711b | 8155 | if (mddev->array_sectors & (new_chunk-1)) |
88ce4930 N |
8156 | /* not factor of array size */ |
8157 | return -EINVAL; | |
b3546035 | 8158 | } |
88ce4930 N |
8159 | |
8160 | /* They look valid */ | |
50ac168a | 8161 | return check_reshape(mddev); |
b3546035 N |
8162 | } |
8163 | ||
fd01b88c | 8164 | static void *raid5_takeover(struct mddev *mddev) |
d562b0c4 N |
8165 | { |
8166 | /* raid5 can take over: | |
f1b29bca | 8167 | * raid0 - if there is only one strip zone - make it a raid4 layout |
d562b0c4 N |
8168 | * raid1 - if there are two drives. We need to know the chunk size |
8169 | * raid4 - trivial - just use a raid4 layout. | |
8170 | * raid6 - Providing it is a *_6 layout | |
d562b0c4 | 8171 | */ |
f1b29bca DW |
8172 | if (mddev->level == 0) |
8173 | return raid45_takeover_raid0(mddev, 5); | |
d562b0c4 N |
8174 | if (mddev->level == 1) |
8175 | return raid5_takeover_raid1(mddev); | |
e9d4758f N |
8176 | if (mddev->level == 4) { |
8177 | mddev->new_layout = ALGORITHM_PARITY_N; | |
8178 | mddev->new_level = 5; | |
8179 | return setup_conf(mddev); | |
8180 | } | |
fc9739c6 N |
8181 | if (mddev->level == 6) |
8182 | return raid5_takeover_raid6(mddev); | |
d562b0c4 N |
8183 | |
8184 | return ERR_PTR(-EINVAL); | |
8185 | } | |
8186 | ||
fd01b88c | 8187 | static void *raid4_takeover(struct mddev *mddev) |
a78d38a1 | 8188 | { |
f1b29bca DW |
8189 | /* raid4 can take over: |
8190 | * raid0 - if there is only one strip zone | |
8191 | * raid5 - if layout is right | |
a78d38a1 | 8192 | */ |
f1b29bca DW |
8193 | if (mddev->level == 0) |
8194 | return raid45_takeover_raid0(mddev, 4); | |
a78d38a1 N |
8195 | if (mddev->level == 5 && |
8196 | mddev->layout == ALGORITHM_PARITY_N) { | |
8197 | mddev->new_layout = 0; | |
8198 | mddev->new_level = 4; | |
8199 | return setup_conf(mddev); | |
8200 | } | |
8201 | return ERR_PTR(-EINVAL); | |
8202 | } | |
d562b0c4 | 8203 | |
84fc4b56 | 8204 | static struct md_personality raid5_personality; |
245f46c2 | 8205 | |
fd01b88c | 8206 | static void *raid6_takeover(struct mddev *mddev) |
245f46c2 N |
8207 | { |
8208 | /* Currently can only take over a raid5. We map the | |
8209 | * personality to an equivalent raid6 personality | |
8210 | * with the Q block at the end. | |
8211 | */ | |
8212 | int new_layout; | |
8213 | ||
8214 | if (mddev->pers != &raid5_personality) | |
8215 | return ERR_PTR(-EINVAL); | |
8216 | if (mddev->degraded > 1) | |
8217 | return ERR_PTR(-EINVAL); | |
8218 | if (mddev->raid_disks > 253) | |
8219 | return ERR_PTR(-EINVAL); | |
8220 | if (mddev->raid_disks < 3) | |
8221 | return ERR_PTR(-EINVAL); | |
8222 | ||
8223 | switch (mddev->layout) { | |
8224 | case ALGORITHM_LEFT_ASYMMETRIC: | |
8225 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; | |
8226 | break; | |
8227 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
8228 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; | |
8229 | break; | |
8230 | case ALGORITHM_LEFT_SYMMETRIC: | |
8231 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; | |
8232 | break; | |
8233 | case ALGORITHM_RIGHT_SYMMETRIC: | |
8234 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; | |
8235 | break; | |
8236 | case ALGORITHM_PARITY_0: | |
8237 | new_layout = ALGORITHM_PARITY_0_6; | |
8238 | break; | |
8239 | case ALGORITHM_PARITY_N: | |
8240 | new_layout = ALGORITHM_PARITY_N; | |
8241 | break; | |
8242 | default: | |
8243 | return ERR_PTR(-EINVAL); | |
8244 | } | |
8245 | mddev->new_level = 6; | |
8246 | mddev->new_layout = new_layout; | |
8247 | mddev->delta_disks = 1; | |
8248 | mddev->raid_disks += 1; | |
8249 | return setup_conf(mddev); | |
8250 | } | |
8251 | ||
ba903a3e AP |
8252 | static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf) |
8253 | { | |
8254 | struct r5conf *conf; | |
8255 | int err; | |
8256 | ||
8257 | err = mddev_lock(mddev); | |
8258 | if (err) | |
8259 | return err; | |
8260 | conf = mddev->private; | |
8261 | if (!conf) { | |
8262 | mddev_unlock(mddev); | |
8263 | return -ENODEV; | |
8264 | } | |
8265 | ||
845b9e22 | 8266 | if (strncmp(buf, "ppl", 3) == 0) { |
0bb0c105 | 8267 | /* ppl only works with RAID 5 */ |
845b9e22 AP |
8268 | if (!raid5_has_ppl(conf) && conf->level == 5) { |
8269 | err = log_init(conf, NULL, true); | |
8270 | if (!err) { | |
8271 | err = resize_stripes(conf, conf->pool_size); | |
8272 | if (err) | |
8273 | log_exit(conf); | |
8274 | } | |
0bb0c105 SL |
8275 | } else |
8276 | err = -EINVAL; | |
8277 | } else if (strncmp(buf, "resync", 6) == 0) { | |
8278 | if (raid5_has_ppl(conf)) { | |
8279 | mddev_suspend(mddev); | |
8280 | log_exit(conf); | |
0bb0c105 | 8281 | mddev_resume(mddev); |
845b9e22 | 8282 | err = resize_stripes(conf, conf->pool_size); |
0bb0c105 SL |
8283 | } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) && |
8284 | r5l_log_disk_error(conf)) { | |
8285 | bool journal_dev_exists = false; | |
8286 | struct md_rdev *rdev; | |
8287 | ||
8288 | rdev_for_each(rdev, mddev) | |
8289 | if (test_bit(Journal, &rdev->flags)) { | |
8290 | journal_dev_exists = true; | |
8291 | break; | |
8292 | } | |
8293 | ||
8294 | if (!journal_dev_exists) { | |
8295 | mddev_suspend(mddev); | |
8296 | clear_bit(MD_HAS_JOURNAL, &mddev->flags); | |
8297 | mddev_resume(mddev); | |
8298 | } else /* need remove journal device first */ | |
8299 | err = -EBUSY; | |
8300 | } else | |
8301 | err = -EINVAL; | |
ba903a3e AP |
8302 | } else { |
8303 | err = -EINVAL; | |
8304 | } | |
8305 | ||
8306 | if (!err) | |
8307 | md_update_sb(mddev, 1); | |
8308 | ||
8309 | mddev_unlock(mddev); | |
8310 | ||
8311 | return err; | |
8312 | } | |
8313 | ||
84fc4b56 | 8314 | static struct md_personality raid6_personality = |
16a53ecc N |
8315 | { |
8316 | .name = "raid6", | |
8317 | .level = 6, | |
8318 | .owner = THIS_MODULE, | |
849674e4 SL |
8319 | .make_request = raid5_make_request, |
8320 | .run = raid5_run, | |
afa0f557 | 8321 | .free = raid5_free, |
849674e4 SL |
8322 | .status = raid5_status, |
8323 | .error_handler = raid5_error, | |
16a53ecc N |
8324 | .hot_add_disk = raid5_add_disk, |
8325 | .hot_remove_disk= raid5_remove_disk, | |
8326 | .spare_active = raid5_spare_active, | |
849674e4 | 8327 | .sync_request = raid5_sync_request, |
16a53ecc | 8328 | .resize = raid5_resize, |
80c3a6ce | 8329 | .size = raid5_size, |
50ac168a | 8330 | .check_reshape = raid6_check_reshape, |
f416885e | 8331 | .start_reshape = raid5_start_reshape, |
cea9c228 | 8332 | .finish_reshape = raid5_finish_reshape, |
16a53ecc | 8333 | .quiesce = raid5_quiesce, |
245f46c2 | 8334 | .takeover = raid6_takeover, |
5c675f83 | 8335 | .congested = raid5_congested, |
0bb0c105 | 8336 | .change_consistency_policy = raid5_change_consistency_policy, |
16a53ecc | 8337 | }; |
84fc4b56 | 8338 | static struct md_personality raid5_personality = |
1da177e4 LT |
8339 | { |
8340 | .name = "raid5", | |
2604b703 | 8341 | .level = 5, |
1da177e4 | 8342 | .owner = THIS_MODULE, |
849674e4 SL |
8343 | .make_request = raid5_make_request, |
8344 | .run = raid5_run, | |
afa0f557 | 8345 | .free = raid5_free, |
849674e4 SL |
8346 | .status = raid5_status, |
8347 | .error_handler = raid5_error, | |
1da177e4 LT |
8348 | .hot_add_disk = raid5_add_disk, |
8349 | .hot_remove_disk= raid5_remove_disk, | |
8350 | .spare_active = raid5_spare_active, | |
849674e4 | 8351 | .sync_request = raid5_sync_request, |
1da177e4 | 8352 | .resize = raid5_resize, |
80c3a6ce | 8353 | .size = raid5_size, |
63c70c4f N |
8354 | .check_reshape = raid5_check_reshape, |
8355 | .start_reshape = raid5_start_reshape, | |
cea9c228 | 8356 | .finish_reshape = raid5_finish_reshape, |
72626685 | 8357 | .quiesce = raid5_quiesce, |
d562b0c4 | 8358 | .takeover = raid5_takeover, |
5c675f83 | 8359 | .congested = raid5_congested, |
ba903a3e | 8360 | .change_consistency_policy = raid5_change_consistency_policy, |
1da177e4 LT |
8361 | }; |
8362 | ||
84fc4b56 | 8363 | static struct md_personality raid4_personality = |
1da177e4 | 8364 | { |
2604b703 N |
8365 | .name = "raid4", |
8366 | .level = 4, | |
8367 | .owner = THIS_MODULE, | |
849674e4 SL |
8368 | .make_request = raid5_make_request, |
8369 | .run = raid5_run, | |
afa0f557 | 8370 | .free = raid5_free, |
849674e4 SL |
8371 | .status = raid5_status, |
8372 | .error_handler = raid5_error, | |
2604b703 N |
8373 | .hot_add_disk = raid5_add_disk, |
8374 | .hot_remove_disk= raid5_remove_disk, | |
8375 | .spare_active = raid5_spare_active, | |
849674e4 | 8376 | .sync_request = raid5_sync_request, |
2604b703 | 8377 | .resize = raid5_resize, |
80c3a6ce | 8378 | .size = raid5_size, |
3d37890b N |
8379 | .check_reshape = raid5_check_reshape, |
8380 | .start_reshape = raid5_start_reshape, | |
cea9c228 | 8381 | .finish_reshape = raid5_finish_reshape, |
2604b703 | 8382 | .quiesce = raid5_quiesce, |
a78d38a1 | 8383 | .takeover = raid4_takeover, |
5c675f83 | 8384 | .congested = raid5_congested, |
0bb0c105 | 8385 | .change_consistency_policy = raid5_change_consistency_policy, |
2604b703 N |
8386 | }; |
8387 | ||
8388 | static int __init raid5_init(void) | |
8389 | { | |
29c6d1bb SAS |
8390 | int ret; |
8391 | ||
851c30c9 SL |
8392 | raid5_wq = alloc_workqueue("raid5wq", |
8393 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); | |
8394 | if (!raid5_wq) | |
8395 | return -ENOMEM; | |
29c6d1bb SAS |
8396 | |
8397 | ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, | |
8398 | "md/raid5:prepare", | |
8399 | raid456_cpu_up_prepare, | |
8400 | raid456_cpu_dead); | |
8401 | if (ret) { | |
8402 | destroy_workqueue(raid5_wq); | |
8403 | return ret; | |
8404 | } | |
16a53ecc | 8405 | register_md_personality(&raid6_personality); |
2604b703 N |
8406 | register_md_personality(&raid5_personality); |
8407 | register_md_personality(&raid4_personality); | |
8408 | return 0; | |
1da177e4 LT |
8409 | } |
8410 | ||
2604b703 | 8411 | static void raid5_exit(void) |
1da177e4 | 8412 | { |
16a53ecc | 8413 | unregister_md_personality(&raid6_personality); |
2604b703 N |
8414 | unregister_md_personality(&raid5_personality); |
8415 | unregister_md_personality(&raid4_personality); | |
29c6d1bb | 8416 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); |
851c30c9 | 8417 | destroy_workqueue(raid5_wq); |
1da177e4 LT |
8418 | } |
8419 | ||
8420 | module_init(raid5_init); | |
8421 | module_exit(raid5_exit); | |
8422 | MODULE_LICENSE("GPL"); | |
0efb9e61 | 8423 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
1da177e4 | 8424 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
d9d166c2 N |
8425 | MODULE_ALIAS("md-raid5"); |
8426 | MODULE_ALIAS("md-raid4"); | |
2604b703 N |
8427 | MODULE_ALIAS("md-level-5"); |
8428 | MODULE_ALIAS("md-level-4"); | |
16a53ecc N |
8429 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
8430 | MODULE_ALIAS("md-raid6"); | |
8431 | MODULE_ALIAS("md-level-6"); | |
8432 | ||
8433 | /* This used to be two separate modules, they were: */ | |
8434 | MODULE_ALIAS("raid5"); | |
8435 | MODULE_ALIAS("raid6"); |