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