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