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