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