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