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