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