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