]>
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. | |
30 | * conf->bm_write is the number of the last batch successfully written. | |
31 | * conf->bm_flush is the number of the last batch that was closed to | |
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 | |
35 | * the number of the batch it will be in. This is bm_flush+1. | |
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 | |
1da177e4 LT |
46 | #include <linux/module.h> |
47 | #include <linux/slab.h> | |
1da177e4 LT |
48 | #include <linux/highmem.h> |
49 | #include <linux/bitops.h> | |
f6705578 | 50 | #include <linux/kthread.h> |
1da177e4 | 51 | #include <asm/atomic.h> |
16a53ecc | 52 | #include "raid6.h" |
1da177e4 | 53 | |
72626685 N |
54 | #include <linux/raid/bitmap.h> |
55 | ||
1da177e4 LT |
56 | /* |
57 | * Stripe cache | |
58 | */ | |
59 | ||
60 | #define NR_STRIPES 256 | |
61 | #define STRIPE_SIZE PAGE_SIZE | |
62 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) | |
63 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) | |
64 | #define IO_THRESHOLD 1 | |
fccddba0 | 65 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
1da177e4 LT |
66 | #define HASH_MASK (NR_HASH - 1) |
67 | ||
fccddba0 | 68 | #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])) |
1da177e4 LT |
69 | |
70 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector | |
71 | * order without overlap. There may be several bio's per stripe+device, and | |
72 | * a bio could span several devices. | |
73 | * When walking this list for a particular stripe+device, we must never proceed | |
74 | * beyond a bio that extends past this device, as the next bio might no longer | |
75 | * be valid. | |
76 | * This macro is used to determine the 'next' bio in the list, given the sector | |
77 | * of the current stripe+device | |
78 | */ | |
79 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) | |
80 | /* | |
81 | * The following can be used to debug the driver | |
82 | */ | |
83 | #define RAID5_DEBUG 0 | |
84 | #define RAID5_PARANOIA 1 | |
85 | #if RAID5_PARANOIA && defined(CONFIG_SMP) | |
86 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) | |
87 | #else | |
88 | # define CHECK_DEVLOCK() | |
89 | #endif | |
90 | ||
91 | #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x))) | |
92 | #if RAID5_DEBUG | |
93 | #define inline | |
94 | #define __inline__ | |
95 | #endif | |
96 | ||
16a53ecc N |
97 | #if !RAID6_USE_EMPTY_ZERO_PAGE |
98 | /* In .bss so it's zeroed */ | |
99 | const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256))); | |
100 | #endif | |
101 | ||
102 | static inline int raid6_next_disk(int disk, int raid_disks) | |
103 | { | |
104 | disk++; | |
105 | return (disk < raid_disks) ? disk : 0; | |
106 | } | |
1da177e4 LT |
107 | static void print_raid5_conf (raid5_conf_t *conf); |
108 | ||
858119e1 | 109 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
1da177e4 LT |
110 | { |
111 | if (atomic_dec_and_test(&sh->count)) { | |
78bafebd ES |
112 | BUG_ON(!list_empty(&sh->lru)); |
113 | BUG_ON(atomic_read(&conf->active_stripes)==0); | |
1da177e4 | 114 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
7c785b7a | 115 | if (test_bit(STRIPE_DELAYED, &sh->state)) { |
1da177e4 | 116 | list_add_tail(&sh->lru, &conf->delayed_list); |
7c785b7a N |
117 | blk_plug_device(conf->mddev->queue); |
118 | } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && | |
ae3c20cc | 119 | sh->bm_seq - conf->seq_write > 0) { |
72626685 | 120 | list_add_tail(&sh->lru, &conf->bitmap_list); |
7c785b7a N |
121 | blk_plug_device(conf->mddev->queue); |
122 | } else { | |
72626685 | 123 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
1da177e4 | 124 | list_add_tail(&sh->lru, &conf->handle_list); |
72626685 | 125 | } |
1da177e4 LT |
126 | md_wakeup_thread(conf->mddev->thread); |
127 | } else { | |
128 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
129 | atomic_dec(&conf->preread_active_stripes); | |
130 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) | |
131 | md_wakeup_thread(conf->mddev->thread); | |
132 | } | |
1da177e4 | 133 | atomic_dec(&conf->active_stripes); |
ccfcc3c1 N |
134 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
135 | list_add_tail(&sh->lru, &conf->inactive_list); | |
1da177e4 | 136 | wake_up(&conf->wait_for_stripe); |
ccfcc3c1 | 137 | } |
1da177e4 LT |
138 | } |
139 | } | |
140 | } | |
141 | static void release_stripe(struct stripe_head *sh) | |
142 | { | |
143 | raid5_conf_t *conf = sh->raid_conf; | |
144 | unsigned long flags; | |
16a53ecc | 145 | |
1da177e4 LT |
146 | spin_lock_irqsave(&conf->device_lock, flags); |
147 | __release_stripe(conf, sh); | |
148 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
149 | } | |
150 | ||
fccddba0 | 151 | static inline void remove_hash(struct stripe_head *sh) |
1da177e4 LT |
152 | { |
153 | PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector); | |
154 | ||
fccddba0 | 155 | hlist_del_init(&sh->hash); |
1da177e4 LT |
156 | } |
157 | ||
16a53ecc | 158 | static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh) |
1da177e4 | 159 | { |
fccddba0 | 160 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
1da177e4 LT |
161 | |
162 | PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector); | |
163 | ||
164 | CHECK_DEVLOCK(); | |
fccddba0 | 165 | hlist_add_head(&sh->hash, hp); |
1da177e4 LT |
166 | } |
167 | ||
168 | ||
169 | /* find an idle stripe, make sure it is unhashed, and return it. */ | |
170 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) | |
171 | { | |
172 | struct stripe_head *sh = NULL; | |
173 | struct list_head *first; | |
174 | ||
175 | CHECK_DEVLOCK(); | |
176 | if (list_empty(&conf->inactive_list)) | |
177 | goto out; | |
178 | first = conf->inactive_list.next; | |
179 | sh = list_entry(first, struct stripe_head, lru); | |
180 | list_del_init(first); | |
181 | remove_hash(sh); | |
182 | atomic_inc(&conf->active_stripes); | |
183 | out: | |
184 | return sh; | |
185 | } | |
186 | ||
187 | static void shrink_buffers(struct stripe_head *sh, int num) | |
188 | { | |
189 | struct page *p; | |
190 | int i; | |
191 | ||
192 | for (i=0; i<num ; i++) { | |
193 | p = sh->dev[i].page; | |
194 | if (!p) | |
195 | continue; | |
196 | sh->dev[i].page = NULL; | |
2d1f3b5d | 197 | put_page(p); |
1da177e4 LT |
198 | } |
199 | } | |
200 | ||
201 | static int grow_buffers(struct stripe_head *sh, int num) | |
202 | { | |
203 | int i; | |
204 | ||
205 | for (i=0; i<num; i++) { | |
206 | struct page *page; | |
207 | ||
208 | if (!(page = alloc_page(GFP_KERNEL))) { | |
209 | return 1; | |
210 | } | |
211 | sh->dev[i].page = page; | |
212 | } | |
213 | return 0; | |
214 | } | |
215 | ||
216 | static void raid5_build_block (struct stripe_head *sh, int i); | |
217 | ||
7ecaa1e6 | 218 | static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks) |
1da177e4 LT |
219 | { |
220 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 221 | int i; |
1da177e4 | 222 | |
78bafebd ES |
223 | BUG_ON(atomic_read(&sh->count) != 0); |
224 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); | |
1da177e4 LT |
225 | |
226 | CHECK_DEVLOCK(); | |
227 | PRINTK("init_stripe called, stripe %llu\n", | |
228 | (unsigned long long)sh->sector); | |
229 | ||
230 | remove_hash(sh); | |
16a53ecc | 231 | |
1da177e4 LT |
232 | sh->sector = sector; |
233 | sh->pd_idx = pd_idx; | |
234 | sh->state = 0; | |
235 | ||
7ecaa1e6 N |
236 | sh->disks = disks; |
237 | ||
238 | for (i = sh->disks; i--; ) { | |
1da177e4 LT |
239 | struct r5dev *dev = &sh->dev[i]; |
240 | ||
241 | if (dev->toread || dev->towrite || dev->written || | |
242 | test_bit(R5_LOCKED, &dev->flags)) { | |
243 | printk("sector=%llx i=%d %p %p %p %d\n", | |
244 | (unsigned long long)sh->sector, i, dev->toread, | |
245 | dev->towrite, dev->written, | |
246 | test_bit(R5_LOCKED, &dev->flags)); | |
247 | BUG(); | |
248 | } | |
249 | dev->flags = 0; | |
250 | raid5_build_block(sh, i); | |
251 | } | |
252 | insert_hash(conf, sh); | |
253 | } | |
254 | ||
7ecaa1e6 | 255 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks) |
1da177e4 LT |
256 | { |
257 | struct stripe_head *sh; | |
fccddba0 | 258 | struct hlist_node *hn; |
1da177e4 LT |
259 | |
260 | CHECK_DEVLOCK(); | |
261 | PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector); | |
fccddba0 | 262 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
7ecaa1e6 | 263 | if (sh->sector == sector && sh->disks == disks) |
1da177e4 LT |
264 | return sh; |
265 | PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector); | |
266 | return NULL; | |
267 | } | |
268 | ||
269 | static void unplug_slaves(mddev_t *mddev); | |
270 | static void raid5_unplug_device(request_queue_t *q); | |
271 | ||
7ecaa1e6 N |
272 | static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks, |
273 | int pd_idx, int noblock) | |
1da177e4 LT |
274 | { |
275 | struct stripe_head *sh; | |
276 | ||
277 | PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector); | |
278 | ||
279 | spin_lock_irq(&conf->device_lock); | |
280 | ||
281 | do { | |
72626685 N |
282 | wait_event_lock_irq(conf->wait_for_stripe, |
283 | conf->quiesce == 0, | |
284 | conf->device_lock, /* nothing */); | |
7ecaa1e6 | 285 | sh = __find_stripe(conf, sector, disks); |
1da177e4 LT |
286 | if (!sh) { |
287 | if (!conf->inactive_blocked) | |
288 | sh = get_free_stripe(conf); | |
289 | if (noblock && sh == NULL) | |
290 | break; | |
291 | if (!sh) { | |
292 | conf->inactive_blocked = 1; | |
293 | wait_event_lock_irq(conf->wait_for_stripe, | |
294 | !list_empty(&conf->inactive_list) && | |
5036805b N |
295 | (atomic_read(&conf->active_stripes) |
296 | < (conf->max_nr_stripes *3/4) | |
1da177e4 LT |
297 | || !conf->inactive_blocked), |
298 | conf->device_lock, | |
f4370781 | 299 | raid5_unplug_device(conf->mddev->queue) |
1da177e4 LT |
300 | ); |
301 | conf->inactive_blocked = 0; | |
302 | } else | |
7ecaa1e6 | 303 | init_stripe(sh, sector, pd_idx, disks); |
1da177e4 LT |
304 | } else { |
305 | if (atomic_read(&sh->count)) { | |
78bafebd | 306 | BUG_ON(!list_empty(&sh->lru)); |
1da177e4 LT |
307 | } else { |
308 | if (!test_bit(STRIPE_HANDLE, &sh->state)) | |
309 | atomic_inc(&conf->active_stripes); | |
ff4e8d9a N |
310 | if (list_empty(&sh->lru) && |
311 | !test_bit(STRIPE_EXPANDING, &sh->state)) | |
16a53ecc N |
312 | BUG(); |
313 | list_del_init(&sh->lru); | |
1da177e4 LT |
314 | } |
315 | } | |
316 | } while (sh == NULL); | |
317 | ||
318 | if (sh) | |
319 | atomic_inc(&sh->count); | |
320 | ||
321 | spin_unlock_irq(&conf->device_lock); | |
322 | return sh; | |
323 | } | |
324 | ||
3f294f4f | 325 | static int grow_one_stripe(raid5_conf_t *conf) |
1da177e4 LT |
326 | { |
327 | struct stripe_head *sh; | |
3f294f4f N |
328 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
329 | if (!sh) | |
330 | return 0; | |
331 | memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev)); | |
332 | sh->raid_conf = conf; | |
333 | spin_lock_init(&sh->lock); | |
334 | ||
335 | if (grow_buffers(sh, conf->raid_disks)) { | |
336 | shrink_buffers(sh, conf->raid_disks); | |
337 | kmem_cache_free(conf->slab_cache, sh); | |
338 | return 0; | |
339 | } | |
7ecaa1e6 | 340 | sh->disks = conf->raid_disks; |
3f294f4f N |
341 | /* we just created an active stripe so... */ |
342 | atomic_set(&sh->count, 1); | |
343 | atomic_inc(&conf->active_stripes); | |
344 | INIT_LIST_HEAD(&sh->lru); | |
345 | release_stripe(sh); | |
346 | return 1; | |
347 | } | |
348 | ||
349 | static int grow_stripes(raid5_conf_t *conf, int num) | |
350 | { | |
1da177e4 LT |
351 | kmem_cache_t *sc; |
352 | int devs = conf->raid_disks; | |
353 | ||
ad01c9e3 N |
354 | sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev)); |
355 | sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev)); | |
356 | conf->active_name = 0; | |
357 | sc = kmem_cache_create(conf->cache_name[conf->active_name], | |
1da177e4 LT |
358 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
359 | 0, 0, NULL, NULL); | |
360 | if (!sc) | |
361 | return 1; | |
362 | conf->slab_cache = sc; | |
ad01c9e3 | 363 | conf->pool_size = devs; |
16a53ecc | 364 | while (num--) |
3f294f4f | 365 | if (!grow_one_stripe(conf)) |
1da177e4 | 366 | return 1; |
1da177e4 LT |
367 | return 0; |
368 | } | |
29269553 N |
369 | |
370 | #ifdef CONFIG_MD_RAID5_RESHAPE | |
ad01c9e3 N |
371 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
372 | { | |
373 | /* Make all the stripes able to hold 'newsize' devices. | |
374 | * New slots in each stripe get 'page' set to a new page. | |
375 | * | |
376 | * This happens in stages: | |
377 | * 1/ create a new kmem_cache and allocate the required number of | |
378 | * stripe_heads. | |
379 | * 2/ gather all the old stripe_heads and tranfer the pages across | |
380 | * to the new stripe_heads. This will have the side effect of | |
381 | * freezing the array as once all stripe_heads have been collected, | |
382 | * no IO will be possible. Old stripe heads are freed once their | |
383 | * pages have been transferred over, and the old kmem_cache is | |
384 | * freed when all stripes are done. | |
385 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, | |
386 | * we simple return a failre status - no need to clean anything up. | |
387 | * 4/ allocate new pages for the new slots in the new stripe_heads. | |
388 | * If this fails, we don't bother trying the shrink the | |
389 | * stripe_heads down again, we just leave them as they are. | |
390 | * As each stripe_head is processed the new one is released into | |
391 | * active service. | |
392 | * | |
393 | * Once step2 is started, we cannot afford to wait for a write, | |
394 | * so we use GFP_NOIO allocations. | |
395 | */ | |
396 | struct stripe_head *osh, *nsh; | |
397 | LIST_HEAD(newstripes); | |
398 | struct disk_info *ndisks; | |
399 | int err = 0; | |
400 | kmem_cache_t *sc; | |
401 | int i; | |
402 | ||
403 | if (newsize <= conf->pool_size) | |
404 | return 0; /* never bother to shrink */ | |
405 | ||
406 | /* Step 1 */ | |
407 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], | |
408 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), | |
409 | 0, 0, NULL, NULL); | |
410 | if (!sc) | |
411 | return -ENOMEM; | |
412 | ||
413 | for (i = conf->max_nr_stripes; i; i--) { | |
414 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); | |
415 | if (!nsh) | |
416 | break; | |
417 | ||
418 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); | |
419 | ||
420 | nsh->raid_conf = conf; | |
421 | spin_lock_init(&nsh->lock); | |
422 | ||
423 | list_add(&nsh->lru, &newstripes); | |
424 | } | |
425 | if (i) { | |
426 | /* didn't get enough, give up */ | |
427 | while (!list_empty(&newstripes)) { | |
428 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
429 | list_del(&nsh->lru); | |
430 | kmem_cache_free(sc, nsh); | |
431 | } | |
432 | kmem_cache_destroy(sc); | |
433 | return -ENOMEM; | |
434 | } | |
435 | /* Step 2 - Must use GFP_NOIO now. | |
436 | * OK, we have enough stripes, start collecting inactive | |
437 | * stripes and copying them over | |
438 | */ | |
439 | list_for_each_entry(nsh, &newstripes, lru) { | |
440 | spin_lock_irq(&conf->device_lock); | |
441 | wait_event_lock_irq(conf->wait_for_stripe, | |
442 | !list_empty(&conf->inactive_list), | |
443 | conf->device_lock, | |
b3b46be3 | 444 | unplug_slaves(conf->mddev) |
ad01c9e3 N |
445 | ); |
446 | osh = get_free_stripe(conf); | |
447 | spin_unlock_irq(&conf->device_lock); | |
448 | atomic_set(&nsh->count, 1); | |
449 | for(i=0; i<conf->pool_size; i++) | |
450 | nsh->dev[i].page = osh->dev[i].page; | |
451 | for( ; i<newsize; i++) | |
452 | nsh->dev[i].page = NULL; | |
453 | kmem_cache_free(conf->slab_cache, osh); | |
454 | } | |
455 | kmem_cache_destroy(conf->slab_cache); | |
456 | ||
457 | /* Step 3. | |
458 | * At this point, we are holding all the stripes so the array | |
459 | * is completely stalled, so now is a good time to resize | |
460 | * conf->disks. | |
461 | */ | |
462 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); | |
463 | if (ndisks) { | |
464 | for (i=0; i<conf->raid_disks; i++) | |
465 | ndisks[i] = conf->disks[i]; | |
466 | kfree(conf->disks); | |
467 | conf->disks = ndisks; | |
468 | } else | |
469 | err = -ENOMEM; | |
470 | ||
471 | /* Step 4, return new stripes to service */ | |
472 | while(!list_empty(&newstripes)) { | |
473 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
474 | list_del_init(&nsh->lru); | |
475 | for (i=conf->raid_disks; i < newsize; i++) | |
476 | if (nsh->dev[i].page == NULL) { | |
477 | struct page *p = alloc_page(GFP_NOIO); | |
478 | nsh->dev[i].page = p; | |
479 | if (!p) | |
480 | err = -ENOMEM; | |
481 | } | |
482 | release_stripe(nsh); | |
483 | } | |
484 | /* critical section pass, GFP_NOIO no longer needed */ | |
485 | ||
486 | conf->slab_cache = sc; | |
487 | conf->active_name = 1-conf->active_name; | |
488 | conf->pool_size = newsize; | |
489 | return err; | |
490 | } | |
29269553 | 491 | #endif |
1da177e4 | 492 | |
3f294f4f | 493 | static int drop_one_stripe(raid5_conf_t *conf) |
1da177e4 LT |
494 | { |
495 | struct stripe_head *sh; | |
496 | ||
3f294f4f N |
497 | spin_lock_irq(&conf->device_lock); |
498 | sh = get_free_stripe(conf); | |
499 | spin_unlock_irq(&conf->device_lock); | |
500 | if (!sh) | |
501 | return 0; | |
78bafebd | 502 | BUG_ON(atomic_read(&sh->count)); |
ad01c9e3 | 503 | shrink_buffers(sh, conf->pool_size); |
3f294f4f N |
504 | kmem_cache_free(conf->slab_cache, sh); |
505 | atomic_dec(&conf->active_stripes); | |
506 | return 1; | |
507 | } | |
508 | ||
509 | static void shrink_stripes(raid5_conf_t *conf) | |
510 | { | |
511 | while (drop_one_stripe(conf)) | |
512 | ; | |
513 | ||
29fc7e3e N |
514 | if (conf->slab_cache) |
515 | kmem_cache_destroy(conf->slab_cache); | |
1da177e4 LT |
516 | conf->slab_cache = NULL; |
517 | } | |
518 | ||
4e5314b5 | 519 | static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done, |
1da177e4 LT |
520 | int error) |
521 | { | |
522 | struct stripe_head *sh = bi->bi_private; | |
523 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 524 | int disks = sh->disks, i; |
1da177e4 | 525 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
d6950432 N |
526 | char b[BDEVNAME_SIZE]; |
527 | mdk_rdev_t *rdev; | |
1da177e4 LT |
528 | |
529 | if (bi->bi_size) | |
530 | return 1; | |
531 | ||
532 | for (i=0 ; i<disks; i++) | |
533 | if (bi == &sh->dev[i].req) | |
534 | break; | |
535 | ||
536 | PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n", | |
537 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), | |
538 | uptodate); | |
539 | if (i == disks) { | |
540 | BUG(); | |
541 | return 0; | |
542 | } | |
543 | ||
544 | if (uptodate) { | |
545 | #if 0 | |
546 | struct bio *bio; | |
547 | unsigned long flags; | |
548 | spin_lock_irqsave(&conf->device_lock, flags); | |
549 | /* we can return a buffer if we bypassed the cache or | |
550 | * if the top buffer is not in highmem. If there are | |
551 | * multiple buffers, leave the extra work to | |
552 | * handle_stripe | |
553 | */ | |
554 | buffer = sh->bh_read[i]; | |
555 | if (buffer && | |
556 | (!PageHighMem(buffer->b_page) | |
557 | || buffer->b_page == bh->b_page ) | |
558 | ) { | |
559 | sh->bh_read[i] = buffer->b_reqnext; | |
560 | buffer->b_reqnext = NULL; | |
561 | } else | |
562 | buffer = NULL; | |
563 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
564 | if (sh->bh_page[i]==bh->b_page) | |
565 | set_buffer_uptodate(bh); | |
566 | if (buffer) { | |
567 | if (buffer->b_page != bh->b_page) | |
568 | memcpy(buffer->b_data, bh->b_data, bh->b_size); | |
569 | buffer->b_end_io(buffer, 1); | |
570 | } | |
571 | #else | |
572 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
4e5314b5 N |
573 | #endif |
574 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
d6950432 N |
575 | rdev = conf->disks[i].rdev; |
576 | printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n", | |
577 | mdname(conf->mddev), STRIPE_SECTORS, | |
578 | (unsigned long long)sh->sector + rdev->data_offset, | |
579 | bdevname(rdev->bdev, b)); | |
4e5314b5 N |
580 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
581 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
582 | } | |
ba22dcbf N |
583 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
584 | atomic_set(&conf->disks[i].rdev->read_errors, 0); | |
1da177e4 | 585 | } else { |
d6950432 | 586 | const char *bdn = bdevname(conf->disks[i].rdev->bdev, b); |
ba22dcbf | 587 | int retry = 0; |
d6950432 N |
588 | rdev = conf->disks[i].rdev; |
589 | ||
1da177e4 | 590 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
d6950432 | 591 | atomic_inc(&rdev->read_errors); |
ba22dcbf | 592 | if (conf->mddev->degraded) |
d6950432 N |
593 | printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n", |
594 | mdname(conf->mddev), | |
595 | (unsigned long long)sh->sector + rdev->data_offset, | |
596 | bdn); | |
ba22dcbf | 597 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
4e5314b5 | 598 | /* Oh, no!!! */ |
d6950432 N |
599 | printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n", |
600 | mdname(conf->mddev), | |
601 | (unsigned long long)sh->sector + rdev->data_offset, | |
602 | bdn); | |
603 | else if (atomic_read(&rdev->read_errors) | |
ba22dcbf | 604 | > conf->max_nr_stripes) |
14f8d26b | 605 | printk(KERN_WARNING |
d6950432 N |
606 | "raid5:%s: Too many read errors, failing device %s.\n", |
607 | mdname(conf->mddev), bdn); | |
ba22dcbf N |
608 | else |
609 | retry = 1; | |
610 | if (retry) | |
611 | set_bit(R5_ReadError, &sh->dev[i].flags); | |
612 | else { | |
4e5314b5 N |
613 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
614 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
d6950432 | 615 | md_error(conf->mddev, rdev); |
ba22dcbf | 616 | } |
1da177e4 LT |
617 | } |
618 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); | |
619 | #if 0 | |
620 | /* must restore b_page before unlocking buffer... */ | |
621 | if (sh->bh_page[i] != bh->b_page) { | |
622 | bh->b_page = sh->bh_page[i]; | |
623 | bh->b_data = page_address(bh->b_page); | |
624 | clear_buffer_uptodate(bh); | |
625 | } | |
626 | #endif | |
627 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
628 | set_bit(STRIPE_HANDLE, &sh->state); | |
629 | release_stripe(sh); | |
630 | return 0; | |
631 | } | |
632 | ||
633 | static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done, | |
634 | int error) | |
635 | { | |
636 | struct stripe_head *sh = bi->bi_private; | |
637 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 638 | int disks = sh->disks, i; |
1da177e4 LT |
639 | unsigned long flags; |
640 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); | |
641 | ||
642 | if (bi->bi_size) | |
643 | return 1; | |
644 | ||
645 | for (i=0 ; i<disks; i++) | |
646 | if (bi == &sh->dev[i].req) | |
647 | break; | |
648 | ||
649 | PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n", | |
650 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), | |
651 | uptodate); | |
652 | if (i == disks) { | |
653 | BUG(); | |
654 | return 0; | |
655 | } | |
656 | ||
657 | spin_lock_irqsave(&conf->device_lock, flags); | |
658 | if (!uptodate) | |
659 | md_error(conf->mddev, conf->disks[i].rdev); | |
660 | ||
661 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); | |
662 | ||
663 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
664 | set_bit(STRIPE_HANDLE, &sh->state); | |
665 | __release_stripe(conf, sh); | |
666 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
667 | return 0; | |
668 | } | |
669 | ||
670 | ||
671 | static sector_t compute_blocknr(struct stripe_head *sh, int i); | |
672 | ||
673 | static void raid5_build_block (struct stripe_head *sh, int i) | |
674 | { | |
675 | struct r5dev *dev = &sh->dev[i]; | |
676 | ||
677 | bio_init(&dev->req); | |
678 | dev->req.bi_io_vec = &dev->vec; | |
679 | dev->req.bi_vcnt++; | |
680 | dev->req.bi_max_vecs++; | |
681 | dev->vec.bv_page = dev->page; | |
682 | dev->vec.bv_len = STRIPE_SIZE; | |
683 | dev->vec.bv_offset = 0; | |
684 | ||
685 | dev->req.bi_sector = sh->sector; | |
686 | dev->req.bi_private = sh; | |
687 | ||
688 | dev->flags = 0; | |
16a53ecc | 689 | dev->sector = compute_blocknr(sh, i); |
1da177e4 LT |
690 | } |
691 | ||
692 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) | |
693 | { | |
694 | char b[BDEVNAME_SIZE]; | |
695 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
696 | PRINTK("raid5: error called\n"); | |
697 | ||
b2d444d7 | 698 | if (!test_bit(Faulty, &rdev->flags)) { |
850b2b42 | 699 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
b2d444d7 | 700 | if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 | 701 | mddev->degraded++; |
b2d444d7 | 702 | clear_bit(In_sync, &rdev->flags); |
1da177e4 LT |
703 | /* |
704 | * if recovery was running, make sure it aborts. | |
705 | */ | |
706 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); | |
707 | } | |
b2d444d7 | 708 | set_bit(Faulty, &rdev->flags); |
1da177e4 LT |
709 | printk (KERN_ALERT |
710 | "raid5: Disk failure on %s, disabling device." | |
711 | " Operation continuing on %d devices\n", | |
02c2de8c | 712 | bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded); |
1da177e4 | 713 | } |
16a53ecc | 714 | } |
1da177e4 LT |
715 | |
716 | /* | |
717 | * Input: a 'big' sector number, | |
718 | * Output: index of the data and parity disk, and the sector # in them. | |
719 | */ | |
720 | static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks, | |
721 | unsigned int data_disks, unsigned int * dd_idx, | |
722 | unsigned int * pd_idx, raid5_conf_t *conf) | |
723 | { | |
724 | long stripe; | |
725 | unsigned long chunk_number; | |
726 | unsigned int chunk_offset; | |
727 | sector_t new_sector; | |
728 | int sectors_per_chunk = conf->chunk_size >> 9; | |
729 | ||
730 | /* First compute the information on this sector */ | |
731 | ||
732 | /* | |
733 | * Compute the chunk number and the sector offset inside the chunk | |
734 | */ | |
735 | chunk_offset = sector_div(r_sector, sectors_per_chunk); | |
736 | chunk_number = r_sector; | |
737 | BUG_ON(r_sector != chunk_number); | |
738 | ||
739 | /* | |
740 | * Compute the stripe number | |
741 | */ | |
742 | stripe = chunk_number / data_disks; | |
743 | ||
744 | /* | |
745 | * Compute the data disk and parity disk indexes inside the stripe | |
746 | */ | |
747 | *dd_idx = chunk_number % data_disks; | |
748 | ||
749 | /* | |
750 | * Select the parity disk based on the user selected algorithm. | |
751 | */ | |
16a53ecc N |
752 | switch(conf->level) { |
753 | case 4: | |
1da177e4 | 754 | *pd_idx = data_disks; |
16a53ecc N |
755 | break; |
756 | case 5: | |
757 | switch (conf->algorithm) { | |
1da177e4 LT |
758 | case ALGORITHM_LEFT_ASYMMETRIC: |
759 | *pd_idx = data_disks - stripe % raid_disks; | |
760 | if (*dd_idx >= *pd_idx) | |
761 | (*dd_idx)++; | |
762 | break; | |
763 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
764 | *pd_idx = stripe % raid_disks; | |
765 | if (*dd_idx >= *pd_idx) | |
766 | (*dd_idx)++; | |
767 | break; | |
768 | case ALGORITHM_LEFT_SYMMETRIC: | |
769 | *pd_idx = data_disks - stripe % raid_disks; | |
770 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; | |
771 | break; | |
772 | case ALGORITHM_RIGHT_SYMMETRIC: | |
773 | *pd_idx = stripe % raid_disks; | |
774 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; | |
775 | break; | |
776 | default: | |
14f8d26b | 777 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
1da177e4 | 778 | conf->algorithm); |
16a53ecc N |
779 | } |
780 | break; | |
781 | case 6: | |
782 | ||
783 | /**** FIX THIS ****/ | |
784 | switch (conf->algorithm) { | |
785 | case ALGORITHM_LEFT_ASYMMETRIC: | |
786 | *pd_idx = raid_disks - 1 - (stripe % raid_disks); | |
787 | if (*pd_idx == raid_disks-1) | |
788 | (*dd_idx)++; /* Q D D D P */ | |
789 | else if (*dd_idx >= *pd_idx) | |
790 | (*dd_idx) += 2; /* D D P Q D */ | |
791 | break; | |
792 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
793 | *pd_idx = stripe % raid_disks; | |
794 | if (*pd_idx == raid_disks-1) | |
795 | (*dd_idx)++; /* Q D D D P */ | |
796 | else if (*dd_idx >= *pd_idx) | |
797 | (*dd_idx) += 2; /* D D P Q D */ | |
798 | break; | |
799 | case ALGORITHM_LEFT_SYMMETRIC: | |
800 | *pd_idx = raid_disks - 1 - (stripe % raid_disks); | |
801 | *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks; | |
802 | break; | |
803 | case ALGORITHM_RIGHT_SYMMETRIC: | |
804 | *pd_idx = stripe % raid_disks; | |
805 | *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks; | |
806 | break; | |
807 | default: | |
808 | printk (KERN_CRIT "raid6: unsupported algorithm %d\n", | |
809 | conf->algorithm); | |
810 | } | |
811 | break; | |
1da177e4 LT |
812 | } |
813 | ||
814 | /* | |
815 | * Finally, compute the new sector number | |
816 | */ | |
817 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; | |
818 | return new_sector; | |
819 | } | |
820 | ||
821 | ||
822 | static sector_t compute_blocknr(struct stripe_head *sh, int i) | |
823 | { | |
824 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 825 | int raid_disks = sh->disks, data_disks = raid_disks - 1; |
1da177e4 LT |
826 | sector_t new_sector = sh->sector, check; |
827 | int sectors_per_chunk = conf->chunk_size >> 9; | |
828 | sector_t stripe; | |
829 | int chunk_offset; | |
830 | int chunk_number, dummy1, dummy2, dd_idx = i; | |
831 | sector_t r_sector; | |
832 | ||
16a53ecc | 833 | |
1da177e4 LT |
834 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
835 | stripe = new_sector; | |
836 | BUG_ON(new_sector != stripe); | |
837 | ||
16a53ecc N |
838 | if (i == sh->pd_idx) |
839 | return 0; | |
840 | switch(conf->level) { | |
841 | case 4: break; | |
842 | case 5: | |
843 | switch (conf->algorithm) { | |
1da177e4 LT |
844 | case ALGORITHM_LEFT_ASYMMETRIC: |
845 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
846 | if (i > sh->pd_idx) | |
847 | i--; | |
848 | break; | |
849 | case ALGORITHM_LEFT_SYMMETRIC: | |
850 | case ALGORITHM_RIGHT_SYMMETRIC: | |
851 | if (i < sh->pd_idx) | |
852 | i += raid_disks; | |
853 | i -= (sh->pd_idx + 1); | |
854 | break; | |
855 | default: | |
14f8d26b | 856 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
16a53ecc N |
857 | conf->algorithm); |
858 | } | |
859 | break; | |
860 | case 6: | |
861 | data_disks = raid_disks - 2; | |
862 | if (i == raid6_next_disk(sh->pd_idx, raid_disks)) | |
863 | return 0; /* It is the Q disk */ | |
864 | switch (conf->algorithm) { | |
865 | case ALGORITHM_LEFT_ASYMMETRIC: | |
866 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
867 | if (sh->pd_idx == raid_disks-1) | |
868 | i--; /* Q D D D P */ | |
869 | else if (i > sh->pd_idx) | |
870 | i -= 2; /* D D P Q D */ | |
871 | break; | |
872 | case ALGORITHM_LEFT_SYMMETRIC: | |
873 | case ALGORITHM_RIGHT_SYMMETRIC: | |
874 | if (sh->pd_idx == raid_disks-1) | |
875 | i--; /* Q D D D P */ | |
876 | else { | |
877 | /* D D P Q D */ | |
878 | if (i < sh->pd_idx) | |
879 | i += raid_disks; | |
880 | i -= (sh->pd_idx + 2); | |
881 | } | |
882 | break; | |
883 | default: | |
884 | printk (KERN_CRIT "raid6: unsupported algorithm %d\n", | |
1da177e4 | 885 | conf->algorithm); |
16a53ecc N |
886 | } |
887 | break; | |
1da177e4 LT |
888 | } |
889 | ||
890 | chunk_number = stripe * data_disks + i; | |
891 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; | |
892 | ||
893 | check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf); | |
894 | if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) { | |
14f8d26b | 895 | printk(KERN_ERR "compute_blocknr: map not correct\n"); |
1da177e4 LT |
896 | return 0; |
897 | } | |
898 | return r_sector; | |
899 | } | |
900 | ||
901 | ||
902 | ||
903 | /* | |
16a53ecc N |
904 | * Copy data between a page in the stripe cache, and one or more bion |
905 | * The page could align with the middle of the bio, or there could be | |
906 | * several bion, each with several bio_vecs, which cover part of the page | |
907 | * Multiple bion are linked together on bi_next. There may be extras | |
908 | * at the end of this list. We ignore them. | |
1da177e4 LT |
909 | */ |
910 | static void copy_data(int frombio, struct bio *bio, | |
911 | struct page *page, | |
912 | sector_t sector) | |
913 | { | |
914 | char *pa = page_address(page); | |
915 | struct bio_vec *bvl; | |
916 | int i; | |
917 | int page_offset; | |
918 | ||
919 | if (bio->bi_sector >= sector) | |
920 | page_offset = (signed)(bio->bi_sector - sector) * 512; | |
921 | else | |
922 | page_offset = (signed)(sector - bio->bi_sector) * -512; | |
923 | bio_for_each_segment(bvl, bio, i) { | |
924 | int len = bio_iovec_idx(bio,i)->bv_len; | |
925 | int clen; | |
926 | int b_offset = 0; | |
927 | ||
928 | if (page_offset < 0) { | |
929 | b_offset = -page_offset; | |
930 | page_offset += b_offset; | |
931 | len -= b_offset; | |
932 | } | |
933 | ||
934 | if (len > 0 && page_offset + len > STRIPE_SIZE) | |
935 | clen = STRIPE_SIZE - page_offset; | |
936 | else clen = len; | |
16a53ecc | 937 | |
1da177e4 LT |
938 | if (clen > 0) { |
939 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); | |
940 | if (frombio) | |
941 | memcpy(pa+page_offset, ba+b_offset, clen); | |
942 | else | |
943 | memcpy(ba+b_offset, pa+page_offset, clen); | |
944 | __bio_kunmap_atomic(ba, KM_USER0); | |
945 | } | |
946 | if (clen < len) /* hit end of page */ | |
947 | break; | |
948 | page_offset += len; | |
949 | } | |
950 | } | |
951 | ||
952 | #define check_xor() do { \ | |
953 | if (count == MAX_XOR_BLOCKS) { \ | |
954 | xor_block(count, STRIPE_SIZE, ptr); \ | |
955 | count = 1; \ | |
956 | } \ | |
957 | } while(0) | |
958 | ||
959 | ||
960 | static void compute_block(struct stripe_head *sh, int dd_idx) | |
961 | { | |
7ecaa1e6 | 962 | int i, count, disks = sh->disks; |
1da177e4 LT |
963 | void *ptr[MAX_XOR_BLOCKS], *p; |
964 | ||
965 | PRINTK("compute_block, stripe %llu, idx %d\n", | |
966 | (unsigned long long)sh->sector, dd_idx); | |
967 | ||
968 | ptr[0] = page_address(sh->dev[dd_idx].page); | |
969 | memset(ptr[0], 0, STRIPE_SIZE); | |
970 | count = 1; | |
971 | for (i = disks ; i--; ) { | |
972 | if (i == dd_idx) | |
973 | continue; | |
974 | p = page_address(sh->dev[i].page); | |
975 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) | |
976 | ptr[count++] = p; | |
977 | else | |
14f8d26b | 978 | printk(KERN_ERR "compute_block() %d, stripe %llu, %d" |
1da177e4 LT |
979 | " not present\n", dd_idx, |
980 | (unsigned long long)sh->sector, i); | |
981 | ||
982 | check_xor(); | |
983 | } | |
984 | if (count != 1) | |
985 | xor_block(count, STRIPE_SIZE, ptr); | |
986 | set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); | |
987 | } | |
988 | ||
16a53ecc | 989 | static void compute_parity5(struct stripe_head *sh, int method) |
1da177e4 LT |
990 | { |
991 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 992 | int i, pd_idx = sh->pd_idx, disks = sh->disks, count; |
1da177e4 LT |
993 | void *ptr[MAX_XOR_BLOCKS]; |
994 | struct bio *chosen; | |
995 | ||
16a53ecc | 996 | PRINTK("compute_parity5, stripe %llu, method %d\n", |
1da177e4 LT |
997 | (unsigned long long)sh->sector, method); |
998 | ||
999 | count = 1; | |
1000 | ptr[0] = page_address(sh->dev[pd_idx].page); | |
1001 | switch(method) { | |
1002 | case READ_MODIFY_WRITE: | |
78bafebd | 1003 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags)); |
1da177e4 LT |
1004 | for (i=disks ; i-- ;) { |
1005 | if (i==pd_idx) | |
1006 | continue; | |
1007 | if (sh->dev[i].towrite && | |
1008 | test_bit(R5_UPTODATE, &sh->dev[i].flags)) { | |
1009 | ptr[count++] = page_address(sh->dev[i].page); | |
1010 | chosen = sh->dev[i].towrite; | |
1011 | sh->dev[i].towrite = NULL; | |
1012 | ||
1013 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1014 | wake_up(&conf->wait_for_overlap); | |
1015 | ||
78bafebd | 1016 | BUG_ON(sh->dev[i].written); |
1da177e4 LT |
1017 | sh->dev[i].written = chosen; |
1018 | check_xor(); | |
1019 | } | |
1020 | } | |
1021 | break; | |
1022 | case RECONSTRUCT_WRITE: | |
1023 | memset(ptr[0], 0, STRIPE_SIZE); | |
1024 | for (i= disks; i-- ;) | |
1025 | if (i!=pd_idx && sh->dev[i].towrite) { | |
1026 | chosen = sh->dev[i].towrite; | |
1027 | sh->dev[i].towrite = NULL; | |
1028 | ||
1029 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1030 | wake_up(&conf->wait_for_overlap); | |
1031 | ||
78bafebd | 1032 | BUG_ON(sh->dev[i].written); |
1da177e4 LT |
1033 | sh->dev[i].written = chosen; |
1034 | } | |
1035 | break; | |
1036 | case CHECK_PARITY: | |
1037 | break; | |
1038 | } | |
1039 | if (count>1) { | |
1040 | xor_block(count, STRIPE_SIZE, ptr); | |
1041 | count = 1; | |
1042 | } | |
1043 | ||
1044 | for (i = disks; i--;) | |
1045 | if (sh->dev[i].written) { | |
1046 | sector_t sector = sh->dev[i].sector; | |
1047 | struct bio *wbi = sh->dev[i].written; | |
1048 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { | |
1049 | copy_data(1, wbi, sh->dev[i].page, sector); | |
1050 | wbi = r5_next_bio(wbi, sector); | |
1051 | } | |
1052 | ||
1053 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
1054 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
1055 | } | |
1056 | ||
1057 | switch(method) { | |
1058 | case RECONSTRUCT_WRITE: | |
1059 | case CHECK_PARITY: | |
1060 | for (i=disks; i--;) | |
1061 | if (i != pd_idx) { | |
1062 | ptr[count++] = page_address(sh->dev[i].page); | |
1063 | check_xor(); | |
1064 | } | |
1065 | break; | |
1066 | case READ_MODIFY_WRITE: | |
1067 | for (i = disks; i--;) | |
1068 | if (sh->dev[i].written) { | |
1069 | ptr[count++] = page_address(sh->dev[i].page); | |
1070 | check_xor(); | |
1071 | } | |
1072 | } | |
1073 | if (count != 1) | |
1074 | xor_block(count, STRIPE_SIZE, ptr); | |
1075 | ||
1076 | if (method != CHECK_PARITY) { | |
1077 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
1078 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); | |
1079 | } else | |
1080 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
1081 | } | |
1082 | ||
16a53ecc N |
1083 | static void compute_parity6(struct stripe_head *sh, int method) |
1084 | { | |
1085 | raid6_conf_t *conf = sh->raid_conf; | |
1086 | int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count; | |
1087 | struct bio *chosen; | |
1088 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ | |
1089 | void *ptrs[disks]; | |
1090 | ||
1091 | qd_idx = raid6_next_disk(pd_idx, disks); | |
1092 | d0_idx = raid6_next_disk(qd_idx, disks); | |
1093 | ||
1094 | PRINTK("compute_parity, stripe %llu, method %d\n", | |
1095 | (unsigned long long)sh->sector, method); | |
1096 | ||
1097 | switch(method) { | |
1098 | case READ_MODIFY_WRITE: | |
1099 | BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */ | |
1100 | case RECONSTRUCT_WRITE: | |
1101 | for (i= disks; i-- ;) | |
1102 | if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) { | |
1103 | chosen = sh->dev[i].towrite; | |
1104 | sh->dev[i].towrite = NULL; | |
1105 | ||
1106 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1107 | wake_up(&conf->wait_for_overlap); | |
1108 | ||
1109 | if (sh->dev[i].written) BUG(); | |
1110 | sh->dev[i].written = chosen; | |
1111 | } | |
1112 | break; | |
1113 | case CHECK_PARITY: | |
1114 | BUG(); /* Not implemented yet */ | |
1115 | } | |
1116 | ||
1117 | for (i = disks; i--;) | |
1118 | if (sh->dev[i].written) { | |
1119 | sector_t sector = sh->dev[i].sector; | |
1120 | struct bio *wbi = sh->dev[i].written; | |
1121 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { | |
1122 | copy_data(1, wbi, sh->dev[i].page, sector); | |
1123 | wbi = r5_next_bio(wbi, sector); | |
1124 | } | |
1125 | ||
1126 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
1127 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
1128 | } | |
1129 | ||
1130 | // switch(method) { | |
1131 | // case RECONSTRUCT_WRITE: | |
1132 | // case CHECK_PARITY: | |
1133 | // case UPDATE_PARITY: | |
1134 | /* Note that unlike RAID-5, the ordering of the disks matters greatly. */ | |
1135 | /* FIX: Is this ordering of drives even remotely optimal? */ | |
1136 | count = 0; | |
1137 | i = d0_idx; | |
1138 | do { | |
1139 | ptrs[count++] = page_address(sh->dev[i].page); | |
1140 | if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags)) | |
1141 | printk("block %d/%d not uptodate on parity calc\n", i,count); | |
1142 | i = raid6_next_disk(i, disks); | |
1143 | } while ( i != d0_idx ); | |
1144 | // break; | |
1145 | // } | |
1146 | ||
1147 | raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs); | |
1148 | ||
1149 | switch(method) { | |
1150 | case RECONSTRUCT_WRITE: | |
1151 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
1152 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); | |
1153 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); | |
1154 | set_bit(R5_LOCKED, &sh->dev[qd_idx].flags); | |
1155 | break; | |
1156 | case UPDATE_PARITY: | |
1157 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
1158 | set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags); | |
1159 | break; | |
1160 | } | |
1161 | } | |
1162 | ||
1163 | ||
1164 | /* Compute one missing block */ | |
1165 | static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero) | |
1166 | { | |
1167 | raid6_conf_t *conf = sh->raid_conf; | |
1168 | int i, count, disks = conf->raid_disks; | |
1169 | void *ptr[MAX_XOR_BLOCKS], *p; | |
1170 | int pd_idx = sh->pd_idx; | |
1171 | int qd_idx = raid6_next_disk(pd_idx, disks); | |
1172 | ||
1173 | PRINTK("compute_block_1, stripe %llu, idx %d\n", | |
1174 | (unsigned long long)sh->sector, dd_idx); | |
1175 | ||
1176 | if ( dd_idx == qd_idx ) { | |
1177 | /* We're actually computing the Q drive */ | |
1178 | compute_parity6(sh, UPDATE_PARITY); | |
1179 | } else { | |
1180 | ptr[0] = page_address(sh->dev[dd_idx].page); | |
1181 | if (!nozero) memset(ptr[0], 0, STRIPE_SIZE); | |
1182 | count = 1; | |
1183 | for (i = disks ; i--; ) { | |
1184 | if (i == dd_idx || i == qd_idx) | |
1185 | continue; | |
1186 | p = page_address(sh->dev[i].page); | |
1187 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) | |
1188 | ptr[count++] = p; | |
1189 | else | |
1190 | printk("compute_block() %d, stripe %llu, %d" | |
1191 | " not present\n", dd_idx, | |
1192 | (unsigned long long)sh->sector, i); | |
1193 | ||
1194 | check_xor(); | |
1195 | } | |
1196 | if (count != 1) | |
1197 | xor_block(count, STRIPE_SIZE, ptr); | |
1198 | if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); | |
1199 | else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | /* Compute two missing blocks */ | |
1204 | static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2) | |
1205 | { | |
1206 | raid6_conf_t *conf = sh->raid_conf; | |
1207 | int i, count, disks = conf->raid_disks; | |
1208 | int pd_idx = sh->pd_idx; | |
1209 | int qd_idx = raid6_next_disk(pd_idx, disks); | |
1210 | int d0_idx = raid6_next_disk(qd_idx, disks); | |
1211 | int faila, failb; | |
1212 | ||
1213 | /* faila and failb are disk numbers relative to d0_idx */ | |
1214 | /* pd_idx become disks-2 and qd_idx become disks-1 */ | |
1215 | faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx; | |
1216 | failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx; | |
1217 | ||
1218 | BUG_ON(faila == failb); | |
1219 | if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; } | |
1220 | ||
1221 | PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n", | |
1222 | (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb); | |
1223 | ||
1224 | if ( failb == disks-1 ) { | |
1225 | /* Q disk is one of the missing disks */ | |
1226 | if ( faila == disks-2 ) { | |
1227 | /* Missing P+Q, just recompute */ | |
1228 | compute_parity6(sh, UPDATE_PARITY); | |
1229 | return; | |
1230 | } else { | |
1231 | /* We're missing D+Q; recompute D from P */ | |
1232 | compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0); | |
1233 | compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */ | |
1234 | return; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | /* We're missing D+P or D+D; build pointer table */ | |
1239 | { | |
1240 | /**** FIX THIS: This could be very bad if disks is close to 256 ****/ | |
1241 | void *ptrs[disks]; | |
1242 | ||
1243 | count = 0; | |
1244 | i = d0_idx; | |
1245 | do { | |
1246 | ptrs[count++] = page_address(sh->dev[i].page); | |
1247 | i = raid6_next_disk(i, disks); | |
1248 | if (i != dd_idx1 && i != dd_idx2 && | |
1249 | !test_bit(R5_UPTODATE, &sh->dev[i].flags)) | |
1250 | printk("compute_2 with missing block %d/%d\n", count, i); | |
1251 | } while ( i != d0_idx ); | |
1252 | ||
1253 | if ( failb == disks-2 ) { | |
1254 | /* We're missing D+P. */ | |
1255 | raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs); | |
1256 | } else { | |
1257 | /* We're missing D+D. */ | |
1258 | raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs); | |
1259 | } | |
1260 | ||
1261 | /* Both the above update both missing blocks */ | |
1262 | set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags); | |
1263 | set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags); | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | ||
1268 | ||
1da177e4 LT |
1269 | /* |
1270 | * Each stripe/dev can have one or more bion attached. | |
16a53ecc | 1271 | * toread/towrite point to the first in a chain. |
1da177e4 LT |
1272 | * The bi_next chain must be in order. |
1273 | */ | |
1274 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) | |
1275 | { | |
1276 | struct bio **bip; | |
1277 | raid5_conf_t *conf = sh->raid_conf; | |
72626685 | 1278 | int firstwrite=0; |
1da177e4 LT |
1279 | |
1280 | PRINTK("adding bh b#%llu to stripe s#%llu\n", | |
1281 | (unsigned long long)bi->bi_sector, | |
1282 | (unsigned long long)sh->sector); | |
1283 | ||
1284 | ||
1285 | spin_lock(&sh->lock); | |
1286 | spin_lock_irq(&conf->device_lock); | |
72626685 | 1287 | if (forwrite) { |
1da177e4 | 1288 | bip = &sh->dev[dd_idx].towrite; |
72626685 N |
1289 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
1290 | firstwrite = 1; | |
1291 | } else | |
1da177e4 LT |
1292 | bip = &sh->dev[dd_idx].toread; |
1293 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { | |
1294 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) | |
1295 | goto overlap; | |
1296 | bip = & (*bip)->bi_next; | |
1297 | } | |
1298 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) | |
1299 | goto overlap; | |
1300 | ||
78bafebd | 1301 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
1da177e4 LT |
1302 | if (*bip) |
1303 | bi->bi_next = *bip; | |
1304 | *bip = bi; | |
1305 | bi->bi_phys_segments ++; | |
1306 | spin_unlock_irq(&conf->device_lock); | |
1307 | spin_unlock(&sh->lock); | |
1308 | ||
1309 | PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n", | |
1310 | (unsigned long long)bi->bi_sector, | |
1311 | (unsigned long long)sh->sector, dd_idx); | |
1312 | ||
72626685 | 1313 | if (conf->mddev->bitmap && firstwrite) { |
72626685 N |
1314 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
1315 | STRIPE_SECTORS, 0); | |
ae3c20cc | 1316 | sh->bm_seq = conf->seq_flush+1; |
72626685 N |
1317 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
1318 | } | |
1319 | ||
1da177e4 LT |
1320 | if (forwrite) { |
1321 | /* check if page is covered */ | |
1322 | sector_t sector = sh->dev[dd_idx].sector; | |
1323 | for (bi=sh->dev[dd_idx].towrite; | |
1324 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && | |
1325 | bi && bi->bi_sector <= sector; | |
1326 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { | |
1327 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) | |
1328 | sector = bi->bi_sector + (bi->bi_size>>9); | |
1329 | } | |
1330 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) | |
1331 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); | |
1332 | } | |
1333 | return 1; | |
1334 | ||
1335 | overlap: | |
1336 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); | |
1337 | spin_unlock_irq(&conf->device_lock); | |
1338 | spin_unlock(&sh->lock); | |
1339 | return 0; | |
1340 | } | |
1341 | ||
29269553 N |
1342 | static void end_reshape(raid5_conf_t *conf); |
1343 | ||
16a53ecc N |
1344 | static int page_is_zero(struct page *p) |
1345 | { | |
1346 | char *a = page_address(p); | |
1347 | return ((*(u32*)a) == 0 && | |
1348 | memcmp(a, a+4, STRIPE_SIZE-4)==0); | |
1349 | } | |
1350 | ||
ccfcc3c1 N |
1351 | static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks) |
1352 | { | |
1353 | int sectors_per_chunk = conf->chunk_size >> 9; | |
ccfcc3c1 | 1354 | int pd_idx, dd_idx; |
2d2063ce CQH |
1355 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
1356 | ||
ccfcc3c1 N |
1357 | raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk |
1358 | + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf); | |
1359 | return pd_idx; | |
1360 | } | |
1361 | ||
1da177e4 LT |
1362 | |
1363 | /* | |
1364 | * handle_stripe - do things to a stripe. | |
1365 | * | |
1366 | * We lock the stripe and then examine the state of various bits | |
1367 | * to see what needs to be done. | |
1368 | * Possible results: | |
1369 | * return some read request which now have data | |
1370 | * return some write requests which are safely on disc | |
1371 | * schedule a read on some buffers | |
1372 | * schedule a write of some buffers | |
1373 | * return confirmation of parity correctness | |
1374 | * | |
1375 | * Parity calculations are done inside the stripe lock | |
1376 | * buffers are taken off read_list or write_list, and bh_cache buffers | |
1377 | * get BH_Lock set before the stripe lock is released. | |
1378 | * | |
1379 | */ | |
1380 | ||
16a53ecc | 1381 | static void handle_stripe5(struct stripe_head *sh) |
1da177e4 LT |
1382 | { |
1383 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 1384 | int disks = sh->disks; |
1da177e4 LT |
1385 | struct bio *return_bi= NULL; |
1386 | struct bio *bi; | |
1387 | int i; | |
ccfcc3c1 | 1388 | int syncing, expanding, expanded; |
1da177e4 LT |
1389 | int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0; |
1390 | int non_overwrite = 0; | |
1391 | int failed_num=0; | |
1392 | struct r5dev *dev; | |
1393 | ||
1394 | PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n", | |
1395 | (unsigned long long)sh->sector, atomic_read(&sh->count), | |
1396 | sh->pd_idx); | |
1397 | ||
1398 | spin_lock(&sh->lock); | |
1399 | clear_bit(STRIPE_HANDLE, &sh->state); | |
1400 | clear_bit(STRIPE_DELAYED, &sh->state); | |
1401 | ||
1402 | syncing = test_bit(STRIPE_SYNCING, &sh->state); | |
ccfcc3c1 N |
1403 | expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
1404 | expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); | |
1da177e4 LT |
1405 | /* Now to look around and see what can be done */ |
1406 | ||
9910f16a | 1407 | rcu_read_lock(); |
1da177e4 LT |
1408 | for (i=disks; i--; ) { |
1409 | mdk_rdev_t *rdev; | |
1410 | dev = &sh->dev[i]; | |
1411 | clear_bit(R5_Insync, &dev->flags); | |
1da177e4 LT |
1412 | |
1413 | PRINTK("check %d: state 0x%lx read %p write %p written %p\n", | |
1414 | i, dev->flags, dev->toread, dev->towrite, dev->written); | |
1415 | /* maybe we can reply to a read */ | |
1416 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { | |
1417 | struct bio *rbi, *rbi2; | |
1418 | PRINTK("Return read for disc %d\n", i); | |
1419 | spin_lock_irq(&conf->device_lock); | |
1420 | rbi = dev->toread; | |
1421 | dev->toread = NULL; | |
1422 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) | |
1423 | wake_up(&conf->wait_for_overlap); | |
1424 | spin_unlock_irq(&conf->device_lock); | |
1425 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
1426 | copy_data(0, rbi, dev->page, dev->sector); | |
1427 | rbi2 = r5_next_bio(rbi, dev->sector); | |
1428 | spin_lock_irq(&conf->device_lock); | |
1429 | if (--rbi->bi_phys_segments == 0) { | |
1430 | rbi->bi_next = return_bi; | |
1431 | return_bi = rbi; | |
1432 | } | |
1433 | spin_unlock_irq(&conf->device_lock); | |
1434 | rbi = rbi2; | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | /* now count some things */ | |
1439 | if (test_bit(R5_LOCKED, &dev->flags)) locked++; | |
1440 | if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++; | |
1441 | ||
1442 | ||
1443 | if (dev->toread) to_read++; | |
1444 | if (dev->towrite) { | |
1445 | to_write++; | |
1446 | if (!test_bit(R5_OVERWRITE, &dev->flags)) | |
1447 | non_overwrite++; | |
1448 | } | |
1449 | if (dev->written) written++; | |
9910f16a | 1450 | rdev = rcu_dereference(conf->disks[i].rdev); |
b2d444d7 | 1451 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
14f8d26b | 1452 | /* The ReadError flag will just be confusing now */ |
4e5314b5 N |
1453 | clear_bit(R5_ReadError, &dev->flags); |
1454 | clear_bit(R5_ReWrite, &dev->flags); | |
1455 | } | |
b2d444d7 | 1456 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
4e5314b5 | 1457 | || test_bit(R5_ReadError, &dev->flags)) { |
1da177e4 LT |
1458 | failed++; |
1459 | failed_num = i; | |
1460 | } else | |
1461 | set_bit(R5_Insync, &dev->flags); | |
1462 | } | |
9910f16a | 1463 | rcu_read_unlock(); |
1da177e4 LT |
1464 | PRINTK("locked=%d uptodate=%d to_read=%d" |
1465 | " to_write=%d failed=%d failed_num=%d\n", | |
1466 | locked, uptodate, to_read, to_write, failed, failed_num); | |
1467 | /* check if the array has lost two devices and, if so, some requests might | |
1468 | * need to be failed | |
1469 | */ | |
1470 | if (failed > 1 && to_read+to_write+written) { | |
1da177e4 | 1471 | for (i=disks; i--; ) { |
72626685 | 1472 | int bitmap_end = 0; |
4e5314b5 N |
1473 | |
1474 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
9910f16a N |
1475 | mdk_rdev_t *rdev; |
1476 | rcu_read_lock(); | |
1477 | rdev = rcu_dereference(conf->disks[i].rdev); | |
b2d444d7 | 1478 | if (rdev && test_bit(In_sync, &rdev->flags)) |
4e5314b5 N |
1479 | /* multiple read failures in one stripe */ |
1480 | md_error(conf->mddev, rdev); | |
9910f16a | 1481 | rcu_read_unlock(); |
4e5314b5 N |
1482 | } |
1483 | ||
72626685 | 1484 | spin_lock_irq(&conf->device_lock); |
1da177e4 LT |
1485 | /* fail all writes first */ |
1486 | bi = sh->dev[i].towrite; | |
1487 | sh->dev[i].towrite = NULL; | |
72626685 | 1488 | if (bi) { to_write--; bitmap_end = 1; } |
1da177e4 LT |
1489 | |
1490 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1491 | wake_up(&conf->wait_for_overlap); | |
1492 | ||
1493 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
1494 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
1495 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1496 | if (--bi->bi_phys_segments == 0) { | |
1497 | md_write_end(conf->mddev); | |
1498 | bi->bi_next = return_bi; | |
1499 | return_bi = bi; | |
1500 | } | |
1501 | bi = nextbi; | |
1502 | } | |
1503 | /* and fail all 'written' */ | |
1504 | bi = sh->dev[i].written; | |
1505 | sh->dev[i].written = NULL; | |
72626685 | 1506 | if (bi) bitmap_end = 1; |
1da177e4 LT |
1507 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { |
1508 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); | |
1509 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1510 | if (--bi->bi_phys_segments == 0) { | |
1511 | md_write_end(conf->mddev); | |
1512 | bi->bi_next = return_bi; | |
1513 | return_bi = bi; | |
1514 | } | |
1515 | bi = bi2; | |
1516 | } | |
1517 | ||
1518 | /* fail any reads if this device is non-operational */ | |
4e5314b5 N |
1519 | if (!test_bit(R5_Insync, &sh->dev[i].flags) || |
1520 | test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
1da177e4 LT |
1521 | bi = sh->dev[i].toread; |
1522 | sh->dev[i].toread = NULL; | |
1523 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1524 | wake_up(&conf->wait_for_overlap); | |
1525 | if (bi) to_read--; | |
1526 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
1527 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
1528 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1529 | if (--bi->bi_phys_segments == 0) { | |
1530 | bi->bi_next = return_bi; | |
1531 | return_bi = bi; | |
1532 | } | |
1533 | bi = nextbi; | |
1534 | } | |
1535 | } | |
72626685 N |
1536 | spin_unlock_irq(&conf->device_lock); |
1537 | if (bitmap_end) | |
1538 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
1539 | STRIPE_SECTORS, 0, 0); | |
1da177e4 | 1540 | } |
1da177e4 LT |
1541 | } |
1542 | if (failed > 1 && syncing) { | |
1543 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); | |
1544 | clear_bit(STRIPE_SYNCING, &sh->state); | |
1545 | syncing = 0; | |
1546 | } | |
1547 | ||
1548 | /* might be able to return some write requests if the parity block | |
1549 | * is safe, or on a failed drive | |
1550 | */ | |
1551 | dev = &sh->dev[sh->pd_idx]; | |
1552 | if ( written && | |
1553 | ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) && | |
1554 | test_bit(R5_UPTODATE, &dev->flags)) | |
1555 | || (failed == 1 && failed_num == sh->pd_idx)) | |
1556 | ) { | |
1557 | /* any written block on an uptodate or failed drive can be returned. | |
1558 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but | |
1559 | * never LOCKED, so we don't need to test 'failed' directly. | |
1560 | */ | |
1561 | for (i=disks; i--; ) | |
1562 | if (sh->dev[i].written) { | |
1563 | dev = &sh->dev[i]; | |
1564 | if (!test_bit(R5_LOCKED, &dev->flags) && | |
1565 | test_bit(R5_UPTODATE, &dev->flags) ) { | |
1566 | /* We can return any write requests */ | |
1567 | struct bio *wbi, *wbi2; | |
72626685 | 1568 | int bitmap_end = 0; |
1da177e4 LT |
1569 | PRINTK("Return write for disc %d\n", i); |
1570 | spin_lock_irq(&conf->device_lock); | |
1571 | wbi = dev->written; | |
1572 | dev->written = NULL; | |
1573 | while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
1574 | wbi2 = r5_next_bio(wbi, dev->sector); | |
1575 | if (--wbi->bi_phys_segments == 0) { | |
1576 | md_write_end(conf->mddev); | |
1577 | wbi->bi_next = return_bi; | |
1578 | return_bi = wbi; | |
1579 | } | |
1580 | wbi = wbi2; | |
1581 | } | |
72626685 N |
1582 | if (dev->towrite == NULL) |
1583 | bitmap_end = 1; | |
1da177e4 | 1584 | spin_unlock_irq(&conf->device_lock); |
72626685 N |
1585 | if (bitmap_end) |
1586 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
1587 | STRIPE_SECTORS, | |
1588 | !test_bit(STRIPE_DEGRADED, &sh->state), 0); | |
1da177e4 LT |
1589 | } |
1590 | } | |
1591 | } | |
1592 | ||
1593 | /* Now we might consider reading some blocks, either to check/generate | |
1594 | * parity, or to satisfy requests | |
1595 | * or to load a block that is being partially written. | |
1596 | */ | |
ccfcc3c1 | 1597 | if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) { |
1da177e4 LT |
1598 | for (i=disks; i--;) { |
1599 | dev = &sh->dev[i]; | |
1600 | if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1601 | (dev->toread || | |
1602 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || | |
1603 | syncing || | |
ccfcc3c1 | 1604 | expanding || |
1da177e4 LT |
1605 | (failed && (sh->dev[failed_num].toread || |
1606 | (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags)))) | |
1607 | ) | |
1608 | ) { | |
1609 | /* we would like to get this block, possibly | |
1610 | * by computing it, but we might not be able to | |
1611 | */ | |
1612 | if (uptodate == disks-1) { | |
1613 | PRINTK("Computing block %d\n", i); | |
1614 | compute_block(sh, i); | |
1615 | uptodate++; | |
1616 | } else if (test_bit(R5_Insync, &dev->flags)) { | |
1617 | set_bit(R5_LOCKED, &dev->flags); | |
1618 | set_bit(R5_Wantread, &dev->flags); | |
1619 | #if 0 | |
1620 | /* if I am just reading this block and we don't have | |
1621 | a failed drive, or any pending writes then sidestep the cache */ | |
1622 | if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext && | |
1623 | ! syncing && !failed && !to_write) { | |
1624 | sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page; | |
1625 | sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data; | |
1626 | } | |
1627 | #endif | |
1628 | locked++; | |
1629 | PRINTK("Reading block %d (sync=%d)\n", | |
1630 | i, syncing); | |
1da177e4 LT |
1631 | } |
1632 | } | |
1633 | } | |
1634 | set_bit(STRIPE_HANDLE, &sh->state); | |
1635 | } | |
1636 | ||
1637 | /* now to consider writing and what else, if anything should be read */ | |
1638 | if (to_write) { | |
1639 | int rmw=0, rcw=0; | |
1640 | for (i=disks ; i--;) { | |
1641 | /* would I have to read this buffer for read_modify_write */ | |
1642 | dev = &sh->dev[i]; | |
1643 | if ((dev->towrite || i == sh->pd_idx) && | |
1644 | (!test_bit(R5_LOCKED, &dev->flags) | |
1645 | #if 0 | |
1646 | || sh->bh_page[i]!=bh->b_page | |
1647 | #endif | |
1648 | ) && | |
1649 | !test_bit(R5_UPTODATE, &dev->flags)) { | |
1650 | if (test_bit(R5_Insync, &dev->flags) | |
1651 | /* && !(!mddev->insync && i == sh->pd_idx) */ | |
1652 | ) | |
1653 | rmw++; | |
1654 | else rmw += 2*disks; /* cannot read it */ | |
1655 | } | |
1656 | /* Would I have to read this buffer for reconstruct_write */ | |
1657 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && | |
1658 | (!test_bit(R5_LOCKED, &dev->flags) | |
1659 | #if 0 | |
1660 | || sh->bh_page[i] != bh->b_page | |
1661 | #endif | |
1662 | ) && | |
1663 | !test_bit(R5_UPTODATE, &dev->flags)) { | |
1664 | if (test_bit(R5_Insync, &dev->flags)) rcw++; | |
1665 | else rcw += 2*disks; | |
1666 | } | |
1667 | } | |
1668 | PRINTK("for sector %llu, rmw=%d rcw=%d\n", | |
1669 | (unsigned long long)sh->sector, rmw, rcw); | |
1670 | set_bit(STRIPE_HANDLE, &sh->state); | |
1671 | if (rmw < rcw && rmw > 0) | |
1672 | /* prefer read-modify-write, but need to get some data */ | |
1673 | for (i=disks; i--;) { | |
1674 | dev = &sh->dev[i]; | |
1675 | if ((dev->towrite || i == sh->pd_idx) && | |
1676 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1677 | test_bit(R5_Insync, &dev->flags)) { | |
1678 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1679 | { | |
1680 | PRINTK("Read_old block %d for r-m-w\n", i); | |
1681 | set_bit(R5_LOCKED, &dev->flags); | |
1682 | set_bit(R5_Wantread, &dev->flags); | |
1683 | locked++; | |
1684 | } else { | |
1685 | set_bit(STRIPE_DELAYED, &sh->state); | |
1686 | set_bit(STRIPE_HANDLE, &sh->state); | |
1687 | } | |
1688 | } | |
1689 | } | |
1690 | if (rcw <= rmw && rcw > 0) | |
1691 | /* want reconstruct write, but need to get some data */ | |
1692 | for (i=disks; i--;) { | |
1693 | dev = &sh->dev[i]; | |
1694 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && | |
1695 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1696 | test_bit(R5_Insync, &dev->flags)) { | |
1697 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1698 | { | |
1699 | PRINTK("Read_old block %d for Reconstruct\n", i); | |
1700 | set_bit(R5_LOCKED, &dev->flags); | |
1701 | set_bit(R5_Wantread, &dev->flags); | |
1702 | locked++; | |
1703 | } else { | |
1704 | set_bit(STRIPE_DELAYED, &sh->state); | |
1705 | set_bit(STRIPE_HANDLE, &sh->state); | |
1706 | } | |
1707 | } | |
1708 | } | |
1709 | /* now if nothing is locked, and if we have enough data, we can start a write request */ | |
72626685 N |
1710 | if (locked == 0 && (rcw == 0 ||rmw == 0) && |
1711 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { | |
1da177e4 | 1712 | PRINTK("Computing parity...\n"); |
16a53ecc | 1713 | compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE); |
1da177e4 LT |
1714 | /* now every locked buffer is ready to be written */ |
1715 | for (i=disks; i--;) | |
1716 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { | |
1717 | PRINTK("Writing block %d\n", i); | |
1718 | locked++; | |
1719 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
1720 | if (!test_bit(R5_Insync, &sh->dev[i].flags) | |
1721 | || (i==sh->pd_idx && failed == 0)) | |
1722 | set_bit(STRIPE_INSYNC, &sh->state); | |
1723 | } | |
1724 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
1725 | atomic_dec(&conf->preread_active_stripes); | |
1726 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) | |
1727 | md_wakeup_thread(conf->mddev->thread); | |
1728 | } | |
1729 | } | |
1730 | } | |
1731 | ||
1732 | /* maybe we need to check and possibly fix the parity for this stripe | |
1733 | * Any reads will already have been scheduled, so we just see if enough data | |
1734 | * is available | |
1735 | */ | |
1736 | if (syncing && locked == 0 && | |
14f8d26b | 1737 | !test_bit(STRIPE_INSYNC, &sh->state)) { |
1da177e4 LT |
1738 | set_bit(STRIPE_HANDLE, &sh->state); |
1739 | if (failed == 0) { | |
78bafebd | 1740 | BUG_ON(uptodate != disks); |
16a53ecc | 1741 | compute_parity5(sh, CHECK_PARITY); |
1da177e4 | 1742 | uptodate--; |
16a53ecc | 1743 | if (page_is_zero(sh->dev[sh->pd_idx].page)) { |
1da177e4 LT |
1744 | /* parity is correct (on disc, not in buffer any more) */ |
1745 | set_bit(STRIPE_INSYNC, &sh->state); | |
9d88883e N |
1746 | } else { |
1747 | conf->mddev->resync_mismatches += STRIPE_SECTORS; | |
1748 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) | |
1749 | /* don't try to repair!! */ | |
1750 | set_bit(STRIPE_INSYNC, &sh->state); | |
14f8d26b N |
1751 | else { |
1752 | compute_block(sh, sh->pd_idx); | |
1753 | uptodate++; | |
1754 | } | |
1da177e4 LT |
1755 | } |
1756 | } | |
1757 | if (!test_bit(STRIPE_INSYNC, &sh->state)) { | |
14f8d26b | 1758 | /* either failed parity check, or recovery is happening */ |
1da177e4 LT |
1759 | if (failed==0) |
1760 | failed_num = sh->pd_idx; | |
1da177e4 | 1761 | dev = &sh->dev[failed_num]; |
14f8d26b N |
1762 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
1763 | BUG_ON(uptodate != disks); | |
1764 | ||
1da177e4 LT |
1765 | set_bit(R5_LOCKED, &dev->flags); |
1766 | set_bit(R5_Wantwrite, &dev->flags); | |
72626685 | 1767 | clear_bit(STRIPE_DEGRADED, &sh->state); |
1da177e4 LT |
1768 | locked++; |
1769 | set_bit(STRIPE_INSYNC, &sh->state); | |
1da177e4 LT |
1770 | } |
1771 | } | |
1772 | if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { | |
1773 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); | |
1774 | clear_bit(STRIPE_SYNCING, &sh->state); | |
1775 | } | |
4e5314b5 N |
1776 | |
1777 | /* If the failed drive is just a ReadError, then we might need to progress | |
1778 | * the repair/check process | |
1779 | */ | |
ba22dcbf N |
1780 | if (failed == 1 && ! conf->mddev->ro && |
1781 | test_bit(R5_ReadError, &sh->dev[failed_num].flags) | |
4e5314b5 N |
1782 | && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags) |
1783 | && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags) | |
1784 | ) { | |
1785 | dev = &sh->dev[failed_num]; | |
1786 | if (!test_bit(R5_ReWrite, &dev->flags)) { | |
1787 | set_bit(R5_Wantwrite, &dev->flags); | |
1788 | set_bit(R5_ReWrite, &dev->flags); | |
1789 | set_bit(R5_LOCKED, &dev->flags); | |
ccfcc3c1 | 1790 | locked++; |
4e5314b5 N |
1791 | } else { |
1792 | /* let's read it back */ | |
1793 | set_bit(R5_Wantread, &dev->flags); | |
1794 | set_bit(R5_LOCKED, &dev->flags); | |
ccfcc3c1 | 1795 | locked++; |
4e5314b5 N |
1796 | } |
1797 | } | |
1798 | ||
ccfcc3c1 N |
1799 | if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) { |
1800 | /* Need to write out all blocks after computing parity */ | |
1801 | sh->disks = conf->raid_disks; | |
1802 | sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks); | |
16a53ecc | 1803 | compute_parity5(sh, RECONSTRUCT_WRITE); |
ccfcc3c1 N |
1804 | for (i= conf->raid_disks; i--;) { |
1805 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
1806 | locked++; | |
1807 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
1808 | } | |
1809 | clear_bit(STRIPE_EXPANDING, &sh->state); | |
1810 | } else if (expanded) { | |
1811 | clear_bit(STRIPE_EXPAND_READY, &sh->state); | |
f6705578 | 1812 | atomic_dec(&conf->reshape_stripes); |
ccfcc3c1 N |
1813 | wake_up(&conf->wait_for_overlap); |
1814 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); | |
1815 | } | |
1816 | ||
1817 | if (expanding && locked == 0) { | |
1818 | /* We have read all the blocks in this stripe and now we need to | |
1819 | * copy some of them into a target stripe for expand. | |
1820 | */ | |
1821 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); | |
1822 | for (i=0; i< sh->disks; i++) | |
1823 | if (i != sh->pd_idx) { | |
1824 | int dd_idx, pd_idx, j; | |
1825 | struct stripe_head *sh2; | |
1826 | ||
1827 | sector_t bn = compute_blocknr(sh, i); | |
1828 | sector_t s = raid5_compute_sector(bn, conf->raid_disks, | |
1829 | conf->raid_disks-1, | |
1830 | &dd_idx, &pd_idx, conf); | |
1831 | sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1); | |
1832 | if (sh2 == NULL) | |
1833 | /* so far only the early blocks of this stripe | |
1834 | * have been requested. When later blocks | |
1835 | * get requested, we will try again | |
1836 | */ | |
1837 | continue; | |
1838 | if(!test_bit(STRIPE_EXPANDING, &sh2->state) || | |
1839 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { | |
1840 | /* must have already done this block */ | |
1841 | release_stripe(sh2); | |
1842 | continue; | |
1843 | } | |
1844 | memcpy(page_address(sh2->dev[dd_idx].page), | |
1845 | page_address(sh->dev[i].page), | |
1846 | STRIPE_SIZE); | |
1847 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); | |
1848 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); | |
1849 | for (j=0; j<conf->raid_disks; j++) | |
1850 | if (j != sh2->pd_idx && | |
1851 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) | |
1852 | break; | |
1853 | if (j == conf->raid_disks) { | |
1854 | set_bit(STRIPE_EXPAND_READY, &sh2->state); | |
1855 | set_bit(STRIPE_HANDLE, &sh2->state); | |
1856 | } | |
1857 | release_stripe(sh2); | |
1858 | } | |
1859 | } | |
1860 | ||
1da177e4 LT |
1861 | spin_unlock(&sh->lock); |
1862 | ||
1863 | while ((bi=return_bi)) { | |
1864 | int bytes = bi->bi_size; | |
1865 | ||
1866 | return_bi = bi->bi_next; | |
1867 | bi->bi_next = NULL; | |
1868 | bi->bi_size = 0; | |
1869 | bi->bi_end_io(bi, bytes, 0); | |
1870 | } | |
1871 | for (i=disks; i-- ;) { | |
1872 | int rw; | |
1873 | struct bio *bi; | |
1874 | mdk_rdev_t *rdev; | |
1875 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) | |
1876 | rw = 1; | |
1877 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) | |
1878 | rw = 0; | |
1879 | else | |
1880 | continue; | |
1881 | ||
1882 | bi = &sh->dev[i].req; | |
1883 | ||
1884 | bi->bi_rw = rw; | |
1885 | if (rw) | |
1886 | bi->bi_end_io = raid5_end_write_request; | |
1887 | else | |
1888 | bi->bi_end_io = raid5_end_read_request; | |
1889 | ||
1890 | rcu_read_lock(); | |
d6065f7b | 1891 | rdev = rcu_dereference(conf->disks[i].rdev); |
b2d444d7 | 1892 | if (rdev && test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
1893 | rdev = NULL; |
1894 | if (rdev) | |
1895 | atomic_inc(&rdev->nr_pending); | |
1896 | rcu_read_unlock(); | |
1897 | ||
1898 | if (rdev) { | |
ccfcc3c1 | 1899 | if (syncing || expanding || expanded) |
1da177e4 LT |
1900 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
1901 | ||
1902 | bi->bi_bdev = rdev->bdev; | |
1903 | PRINTK("for %llu schedule op %ld on disc %d\n", | |
1904 | (unsigned long long)sh->sector, bi->bi_rw, i); | |
1905 | atomic_inc(&sh->count); | |
1906 | bi->bi_sector = sh->sector + rdev->data_offset; | |
1907 | bi->bi_flags = 1 << BIO_UPTODATE; | |
1908 | bi->bi_vcnt = 1; | |
1909 | bi->bi_max_vecs = 1; | |
1910 | bi->bi_idx = 0; | |
1911 | bi->bi_io_vec = &sh->dev[i].vec; | |
1912 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; | |
1913 | bi->bi_io_vec[0].bv_offset = 0; | |
1914 | bi->bi_size = STRIPE_SIZE; | |
1915 | bi->bi_next = NULL; | |
4dbcdc75 N |
1916 | if (rw == WRITE && |
1917 | test_bit(R5_ReWrite, &sh->dev[i].flags)) | |
1918 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); | |
1da177e4 LT |
1919 | generic_make_request(bi); |
1920 | } else { | |
72626685 N |
1921 | if (rw == 1) |
1922 | set_bit(STRIPE_DEGRADED, &sh->state); | |
1da177e4 LT |
1923 | PRINTK("skip op %ld on disc %d for sector %llu\n", |
1924 | bi->bi_rw, i, (unsigned long long)sh->sector); | |
1925 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
1926 | set_bit(STRIPE_HANDLE, &sh->state); | |
1927 | } | |
1928 | } | |
1929 | } | |
1930 | ||
16a53ecc | 1931 | static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page) |
1da177e4 | 1932 | { |
16a53ecc N |
1933 | raid6_conf_t *conf = sh->raid_conf; |
1934 | int disks = conf->raid_disks; | |
1935 | struct bio *return_bi= NULL; | |
1936 | struct bio *bi; | |
1937 | int i; | |
1938 | int syncing; | |
1939 | int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0; | |
1940 | int non_overwrite = 0; | |
1941 | int failed_num[2] = {0, 0}; | |
1942 | struct r5dev *dev, *pdev, *qdev; | |
1943 | int pd_idx = sh->pd_idx; | |
1944 | int qd_idx = raid6_next_disk(pd_idx, disks); | |
1945 | int p_failed, q_failed; | |
1da177e4 | 1946 | |
16a53ecc N |
1947 | PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n", |
1948 | (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count), | |
1949 | pd_idx, qd_idx); | |
72626685 | 1950 | |
16a53ecc N |
1951 | spin_lock(&sh->lock); |
1952 | clear_bit(STRIPE_HANDLE, &sh->state); | |
1953 | clear_bit(STRIPE_DELAYED, &sh->state); | |
1954 | ||
1955 | syncing = test_bit(STRIPE_SYNCING, &sh->state); | |
1956 | /* Now to look around and see what can be done */ | |
1da177e4 LT |
1957 | |
1958 | rcu_read_lock(); | |
16a53ecc N |
1959 | for (i=disks; i--; ) { |
1960 | mdk_rdev_t *rdev; | |
1961 | dev = &sh->dev[i]; | |
1962 | clear_bit(R5_Insync, &dev->flags); | |
1da177e4 | 1963 | |
16a53ecc N |
1964 | PRINTK("check %d: state 0x%lx read %p write %p written %p\n", |
1965 | i, dev->flags, dev->toread, dev->towrite, dev->written); | |
1966 | /* maybe we can reply to a read */ | |
1967 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { | |
1968 | struct bio *rbi, *rbi2; | |
1969 | PRINTK("Return read for disc %d\n", i); | |
1970 | spin_lock_irq(&conf->device_lock); | |
1971 | rbi = dev->toread; | |
1972 | dev->toread = NULL; | |
1973 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) | |
1974 | wake_up(&conf->wait_for_overlap); | |
1975 | spin_unlock_irq(&conf->device_lock); | |
1976 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
1977 | copy_data(0, rbi, dev->page, dev->sector); | |
1978 | rbi2 = r5_next_bio(rbi, dev->sector); | |
1979 | spin_lock_irq(&conf->device_lock); | |
1980 | if (--rbi->bi_phys_segments == 0) { | |
1981 | rbi->bi_next = return_bi; | |
1982 | return_bi = rbi; | |
1983 | } | |
1984 | spin_unlock_irq(&conf->device_lock); | |
1985 | rbi = rbi2; | |
1986 | } | |
1987 | } | |
1da177e4 | 1988 | |
16a53ecc N |
1989 | /* now count some things */ |
1990 | if (test_bit(R5_LOCKED, &dev->flags)) locked++; | |
1991 | if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++; | |
1da177e4 | 1992 | |
16a53ecc N |
1993 | |
1994 | if (dev->toread) to_read++; | |
1995 | if (dev->towrite) { | |
1996 | to_write++; | |
1997 | if (!test_bit(R5_OVERWRITE, &dev->flags)) | |
1998 | non_overwrite++; | |
1999 | } | |
2000 | if (dev->written) written++; | |
2001 | rdev = rcu_dereference(conf->disks[i].rdev); | |
2002 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { | |
2003 | /* The ReadError flag will just be confusing now */ | |
2004 | clear_bit(R5_ReadError, &dev->flags); | |
2005 | clear_bit(R5_ReWrite, &dev->flags); | |
1da177e4 | 2006 | } |
16a53ecc N |
2007 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
2008 | || test_bit(R5_ReadError, &dev->flags)) { | |
2009 | if ( failed < 2 ) | |
2010 | failed_num[failed] = i; | |
2011 | failed++; | |
2012 | } else | |
2013 | set_bit(R5_Insync, &dev->flags); | |
1da177e4 LT |
2014 | } |
2015 | rcu_read_unlock(); | |
16a53ecc N |
2016 | PRINTK("locked=%d uptodate=%d to_read=%d" |
2017 | " to_write=%d failed=%d failed_num=%d,%d\n", | |
2018 | locked, uptodate, to_read, to_write, failed, | |
2019 | failed_num[0], failed_num[1]); | |
2020 | /* check if the array has lost >2 devices and, if so, some requests might | |
2021 | * need to be failed | |
2022 | */ | |
2023 | if (failed > 2 && to_read+to_write+written) { | |
2024 | for (i=disks; i--; ) { | |
2025 | int bitmap_end = 0; | |
1da177e4 | 2026 | |
16a53ecc N |
2027 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
2028 | mdk_rdev_t *rdev; | |
2029 | rcu_read_lock(); | |
2030 | rdev = rcu_dereference(conf->disks[i].rdev); | |
2031 | if (rdev && test_bit(In_sync, &rdev->flags)) | |
2032 | /* multiple read failures in one stripe */ | |
2033 | md_error(conf->mddev, rdev); | |
2034 | rcu_read_unlock(); | |
2035 | } | |
1da177e4 | 2036 | |
16a53ecc N |
2037 | spin_lock_irq(&conf->device_lock); |
2038 | /* fail all writes first */ | |
2039 | bi = sh->dev[i].towrite; | |
2040 | sh->dev[i].towrite = NULL; | |
2041 | if (bi) { to_write--; bitmap_end = 1; } | |
1da177e4 | 2042 | |
16a53ecc N |
2043 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
2044 | wake_up(&conf->wait_for_overlap); | |
2045 | ||
2046 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
2047 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
2048 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
2049 | if (--bi->bi_phys_segments == 0) { | |
2050 | md_write_end(conf->mddev); | |
2051 | bi->bi_next = return_bi; | |
2052 | return_bi = bi; | |
2053 | } | |
2054 | bi = nextbi; | |
2055 | } | |
2056 | /* and fail all 'written' */ | |
2057 | bi = sh->dev[i].written; | |
2058 | sh->dev[i].written = NULL; | |
2059 | if (bi) bitmap_end = 1; | |
2060 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { | |
2061 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); | |
2062 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
2063 | if (--bi->bi_phys_segments == 0) { | |
2064 | md_write_end(conf->mddev); | |
2065 | bi->bi_next = return_bi; | |
2066 | return_bi = bi; | |
2067 | } | |
2068 | bi = bi2; | |
2069 | } | |
2070 | ||
2071 | /* fail any reads if this device is non-operational */ | |
2072 | if (!test_bit(R5_Insync, &sh->dev[i].flags) || | |
2073 | test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
2074 | bi = sh->dev[i].toread; | |
2075 | sh->dev[i].toread = NULL; | |
2076 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
2077 | wake_up(&conf->wait_for_overlap); | |
2078 | if (bi) to_read--; | |
2079 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
2080 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
2081 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
2082 | if (--bi->bi_phys_segments == 0) { | |
2083 | bi->bi_next = return_bi; | |
2084 | return_bi = bi; | |
2085 | } | |
2086 | bi = nextbi; | |
2087 | } | |
2088 | } | |
2089 | spin_unlock_irq(&conf->device_lock); | |
2090 | if (bitmap_end) | |
2091 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
2092 | STRIPE_SECTORS, 0, 0); | |
2093 | } | |
2094 | } | |
2095 | if (failed > 2 && syncing) { | |
2096 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); | |
2097 | clear_bit(STRIPE_SYNCING, &sh->state); | |
2098 | syncing = 0; | |
2099 | } | |
2100 | ||
2101 | /* | |
2102 | * might be able to return some write requests if the parity blocks | |
2103 | * are safe, or on a failed drive | |
2104 | */ | |
2105 | pdev = &sh->dev[pd_idx]; | |
2106 | p_failed = (failed >= 1 && failed_num[0] == pd_idx) | |
2107 | || (failed >= 2 && failed_num[1] == pd_idx); | |
2108 | qdev = &sh->dev[qd_idx]; | |
2109 | q_failed = (failed >= 1 && failed_num[0] == qd_idx) | |
2110 | || (failed >= 2 && failed_num[1] == qd_idx); | |
2111 | ||
2112 | if ( written && | |
2113 | ( p_failed || ((test_bit(R5_Insync, &pdev->flags) | |
2114 | && !test_bit(R5_LOCKED, &pdev->flags) | |
2115 | && test_bit(R5_UPTODATE, &pdev->flags))) ) && | |
2116 | ( q_failed || ((test_bit(R5_Insync, &qdev->flags) | |
2117 | && !test_bit(R5_LOCKED, &qdev->flags) | |
2118 | && test_bit(R5_UPTODATE, &qdev->flags))) ) ) { | |
2119 | /* any written block on an uptodate or failed drive can be | |
2120 | * returned. Note that if we 'wrote' to a failed drive, | |
2121 | * it will be UPTODATE, but never LOCKED, so we don't need | |
2122 | * to test 'failed' directly. | |
2123 | */ | |
2124 | for (i=disks; i--; ) | |
2125 | if (sh->dev[i].written) { | |
2126 | dev = &sh->dev[i]; | |
2127 | if (!test_bit(R5_LOCKED, &dev->flags) && | |
2128 | test_bit(R5_UPTODATE, &dev->flags) ) { | |
2129 | /* We can return any write requests */ | |
2130 | int bitmap_end = 0; | |
2131 | struct bio *wbi, *wbi2; | |
2132 | PRINTK("Return write for stripe %llu disc %d\n", | |
2133 | (unsigned long long)sh->sector, i); | |
2134 | spin_lock_irq(&conf->device_lock); | |
2135 | wbi = dev->written; | |
2136 | dev->written = NULL; | |
2137 | while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
2138 | wbi2 = r5_next_bio(wbi, dev->sector); | |
2139 | if (--wbi->bi_phys_segments == 0) { | |
2140 | md_write_end(conf->mddev); | |
2141 | wbi->bi_next = return_bi; | |
2142 | return_bi = wbi; | |
2143 | } | |
2144 | wbi = wbi2; | |
2145 | } | |
2146 | if (dev->towrite == NULL) | |
2147 | bitmap_end = 1; | |
2148 | spin_unlock_irq(&conf->device_lock); | |
2149 | if (bitmap_end) | |
2150 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
2151 | STRIPE_SECTORS, | |
2152 | !test_bit(STRIPE_DEGRADED, &sh->state), 0); | |
2153 | } | |
2154 | } | |
2155 | } | |
2156 | ||
2157 | /* Now we might consider reading some blocks, either to check/generate | |
2158 | * parity, or to satisfy requests | |
2159 | * or to load a block that is being partially written. | |
2160 | */ | |
2161 | if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) { | |
2162 | for (i=disks; i--;) { | |
2163 | dev = &sh->dev[i]; | |
2164 | if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
2165 | (dev->toread || | |
2166 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || | |
2167 | syncing || | |
2168 | (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) || | |
2169 | (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write)) | |
2170 | ) | |
2171 | ) { | |
2172 | /* we would like to get this block, possibly | |
2173 | * by computing it, but we might not be able to | |
2174 | */ | |
2175 | if (uptodate == disks-1) { | |
2176 | PRINTK("Computing stripe %llu block %d\n", | |
2177 | (unsigned long long)sh->sector, i); | |
2178 | compute_block_1(sh, i, 0); | |
2179 | uptodate++; | |
2180 | } else if ( uptodate == disks-2 && failed >= 2 ) { | |
2181 | /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */ | |
2182 | int other; | |
2183 | for (other=disks; other--;) { | |
2184 | if ( other == i ) | |
2185 | continue; | |
2186 | if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) ) | |
2187 | break; | |
2188 | } | |
2189 | BUG_ON(other < 0); | |
2190 | PRINTK("Computing stripe %llu blocks %d,%d\n", | |
2191 | (unsigned long long)sh->sector, i, other); | |
2192 | compute_block_2(sh, i, other); | |
2193 | uptodate += 2; | |
2194 | } else if (test_bit(R5_Insync, &dev->flags)) { | |
2195 | set_bit(R5_LOCKED, &dev->flags); | |
2196 | set_bit(R5_Wantread, &dev->flags); | |
2197 | #if 0 | |
2198 | /* if I am just reading this block and we don't have | |
2199 | a failed drive, or any pending writes then sidestep the cache */ | |
2200 | if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext && | |
2201 | ! syncing && !failed && !to_write) { | |
2202 | sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page; | |
2203 | sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data; | |
2204 | } | |
2205 | #endif | |
2206 | locked++; | |
2207 | PRINTK("Reading block %d (sync=%d)\n", | |
2208 | i, syncing); | |
2209 | } | |
2210 | } | |
2211 | } | |
2212 | set_bit(STRIPE_HANDLE, &sh->state); | |
2213 | } | |
2214 | ||
2215 | /* now to consider writing and what else, if anything should be read */ | |
2216 | if (to_write) { | |
2217 | int rcw=0, must_compute=0; | |
2218 | for (i=disks ; i--;) { | |
2219 | dev = &sh->dev[i]; | |
2220 | /* Would I have to read this buffer for reconstruct_write */ | |
2221 | if (!test_bit(R5_OVERWRITE, &dev->flags) | |
2222 | && i != pd_idx && i != qd_idx | |
2223 | && (!test_bit(R5_LOCKED, &dev->flags) | |
2224 | #if 0 | |
2225 | || sh->bh_page[i] != bh->b_page | |
2226 | #endif | |
2227 | ) && | |
2228 | !test_bit(R5_UPTODATE, &dev->flags)) { | |
2229 | if (test_bit(R5_Insync, &dev->flags)) rcw++; | |
2230 | else { | |
2231 | PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags); | |
2232 | must_compute++; | |
2233 | } | |
2234 | } | |
2235 | } | |
2236 | PRINTK("for sector %llu, rcw=%d, must_compute=%d\n", | |
2237 | (unsigned long long)sh->sector, rcw, must_compute); | |
2238 | set_bit(STRIPE_HANDLE, &sh->state); | |
2239 | ||
2240 | if (rcw > 0) | |
2241 | /* want reconstruct write, but need to get some data */ | |
2242 | for (i=disks; i--;) { | |
2243 | dev = &sh->dev[i]; | |
2244 | if (!test_bit(R5_OVERWRITE, &dev->flags) | |
2245 | && !(failed == 0 && (i == pd_idx || i == qd_idx)) | |
2246 | && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
2247 | test_bit(R5_Insync, &dev->flags)) { | |
2248 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
2249 | { | |
2250 | PRINTK("Read_old stripe %llu block %d for Reconstruct\n", | |
2251 | (unsigned long long)sh->sector, i); | |
2252 | set_bit(R5_LOCKED, &dev->flags); | |
2253 | set_bit(R5_Wantread, &dev->flags); | |
2254 | locked++; | |
2255 | } else { | |
2256 | PRINTK("Request delayed stripe %llu block %d for Reconstruct\n", | |
2257 | (unsigned long long)sh->sector, i); | |
2258 | set_bit(STRIPE_DELAYED, &sh->state); | |
2259 | set_bit(STRIPE_HANDLE, &sh->state); | |
2260 | } | |
2261 | } | |
2262 | } | |
2263 | /* now if nothing is locked, and if we have enough data, we can start a write request */ | |
2264 | if (locked == 0 && rcw == 0 && | |
2265 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { | |
2266 | if ( must_compute > 0 ) { | |
2267 | /* We have failed blocks and need to compute them */ | |
2268 | switch ( failed ) { | |
2269 | case 0: BUG(); | |
2270 | case 1: compute_block_1(sh, failed_num[0], 0); break; | |
2271 | case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break; | |
2272 | default: BUG(); /* This request should have been failed? */ | |
2273 | } | |
2274 | } | |
2275 | ||
2276 | PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector); | |
2277 | compute_parity6(sh, RECONSTRUCT_WRITE); | |
2278 | /* now every locked buffer is ready to be written */ | |
2279 | for (i=disks; i--;) | |
2280 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { | |
2281 | PRINTK("Writing stripe %llu block %d\n", | |
2282 | (unsigned long long)sh->sector, i); | |
2283 | locked++; | |
2284 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
2285 | } | |
2286 | /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */ | |
2287 | set_bit(STRIPE_INSYNC, &sh->state); | |
2288 | ||
2289 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
2290 | atomic_dec(&conf->preread_active_stripes); | |
2291 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) | |
2292 | md_wakeup_thread(conf->mddev->thread); | |
2293 | } | |
2294 | } | |
2295 | } | |
2296 | ||
2297 | /* maybe we need to check and possibly fix the parity for this stripe | |
2298 | * Any reads will already have been scheduled, so we just see if enough data | |
2299 | * is available | |
2300 | */ | |
2301 | if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) { | |
2302 | int update_p = 0, update_q = 0; | |
2303 | struct r5dev *dev; | |
2304 | ||
2305 | set_bit(STRIPE_HANDLE, &sh->state); | |
2306 | ||
2307 | BUG_ON(failed>2); | |
2308 | BUG_ON(uptodate < disks); | |
2309 | /* Want to check and possibly repair P and Q. | |
2310 | * However there could be one 'failed' device, in which | |
2311 | * case we can only check one of them, possibly using the | |
2312 | * other to generate missing data | |
2313 | */ | |
2314 | ||
2315 | /* If !tmp_page, we cannot do the calculations, | |
2316 | * but as we have set STRIPE_HANDLE, we will soon be called | |
2317 | * by stripe_handle with a tmp_page - just wait until then. | |
2318 | */ | |
2319 | if (tmp_page) { | |
2320 | if (failed == q_failed) { | |
2321 | /* The only possible failed device holds 'Q', so it makes | |
2322 | * sense to check P (If anything else were failed, we would | |
2323 | * have used P to recreate it). | |
2324 | */ | |
2325 | compute_block_1(sh, pd_idx, 1); | |
2326 | if (!page_is_zero(sh->dev[pd_idx].page)) { | |
2327 | compute_block_1(sh,pd_idx,0); | |
2328 | update_p = 1; | |
2329 | } | |
2330 | } | |
2331 | if (!q_failed && failed < 2) { | |
2332 | /* q is not failed, and we didn't use it to generate | |
2333 | * anything, so it makes sense to check it | |
2334 | */ | |
2335 | memcpy(page_address(tmp_page), | |
2336 | page_address(sh->dev[qd_idx].page), | |
2337 | STRIPE_SIZE); | |
2338 | compute_parity6(sh, UPDATE_PARITY); | |
2339 | if (memcmp(page_address(tmp_page), | |
2340 | page_address(sh->dev[qd_idx].page), | |
2341 | STRIPE_SIZE)!= 0) { | |
2342 | clear_bit(STRIPE_INSYNC, &sh->state); | |
2343 | update_q = 1; | |
2344 | } | |
2345 | } | |
2346 | if (update_p || update_q) { | |
2347 | conf->mddev->resync_mismatches += STRIPE_SECTORS; | |
2348 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) | |
2349 | /* don't try to repair!! */ | |
2350 | update_p = update_q = 0; | |
2351 | } | |
2352 | ||
2353 | /* now write out any block on a failed drive, | |
2354 | * or P or Q if they need it | |
2355 | */ | |
2356 | ||
2357 | if (failed == 2) { | |
2358 | dev = &sh->dev[failed_num[1]]; | |
2359 | locked++; | |
2360 | set_bit(R5_LOCKED, &dev->flags); | |
2361 | set_bit(R5_Wantwrite, &dev->flags); | |
2362 | } | |
2363 | if (failed >= 1) { | |
2364 | dev = &sh->dev[failed_num[0]]; | |
2365 | locked++; | |
2366 | set_bit(R5_LOCKED, &dev->flags); | |
2367 | set_bit(R5_Wantwrite, &dev->flags); | |
2368 | } | |
2369 | ||
2370 | if (update_p) { | |
2371 | dev = &sh->dev[pd_idx]; | |
2372 | locked ++; | |
2373 | set_bit(R5_LOCKED, &dev->flags); | |
2374 | set_bit(R5_Wantwrite, &dev->flags); | |
2375 | } | |
2376 | if (update_q) { | |
2377 | dev = &sh->dev[qd_idx]; | |
2378 | locked++; | |
2379 | set_bit(R5_LOCKED, &dev->flags); | |
2380 | set_bit(R5_Wantwrite, &dev->flags); | |
2381 | } | |
2382 | clear_bit(STRIPE_DEGRADED, &sh->state); | |
2383 | ||
2384 | set_bit(STRIPE_INSYNC, &sh->state); | |
2385 | } | |
2386 | } | |
2387 | ||
2388 | if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { | |
2389 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); | |
2390 | clear_bit(STRIPE_SYNCING, &sh->state); | |
2391 | } | |
2392 | ||
2393 | /* If the failed drives are just a ReadError, then we might need | |
2394 | * to progress the repair/check process | |
2395 | */ | |
2396 | if (failed <= 2 && ! conf->mddev->ro) | |
2397 | for (i=0; i<failed;i++) { | |
2398 | dev = &sh->dev[failed_num[i]]; | |
2399 | if (test_bit(R5_ReadError, &dev->flags) | |
2400 | && !test_bit(R5_LOCKED, &dev->flags) | |
2401 | && test_bit(R5_UPTODATE, &dev->flags) | |
2402 | ) { | |
2403 | if (!test_bit(R5_ReWrite, &dev->flags)) { | |
2404 | set_bit(R5_Wantwrite, &dev->flags); | |
2405 | set_bit(R5_ReWrite, &dev->flags); | |
2406 | set_bit(R5_LOCKED, &dev->flags); | |
2407 | } else { | |
2408 | /* let's read it back */ | |
2409 | set_bit(R5_Wantread, &dev->flags); | |
2410 | set_bit(R5_LOCKED, &dev->flags); | |
2411 | } | |
2412 | } | |
2413 | } | |
2414 | spin_unlock(&sh->lock); | |
2415 | ||
2416 | while ((bi=return_bi)) { | |
2417 | int bytes = bi->bi_size; | |
2418 | ||
2419 | return_bi = bi->bi_next; | |
2420 | bi->bi_next = NULL; | |
2421 | bi->bi_size = 0; | |
2422 | bi->bi_end_io(bi, bytes, 0); | |
2423 | } | |
2424 | for (i=disks; i-- ;) { | |
2425 | int rw; | |
2426 | struct bio *bi; | |
2427 | mdk_rdev_t *rdev; | |
2428 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) | |
2429 | rw = 1; | |
2430 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) | |
2431 | rw = 0; | |
2432 | else | |
2433 | continue; | |
2434 | ||
2435 | bi = &sh->dev[i].req; | |
2436 | ||
2437 | bi->bi_rw = rw; | |
2438 | if (rw) | |
2439 | bi->bi_end_io = raid5_end_write_request; | |
2440 | else | |
2441 | bi->bi_end_io = raid5_end_read_request; | |
2442 | ||
2443 | rcu_read_lock(); | |
2444 | rdev = rcu_dereference(conf->disks[i].rdev); | |
2445 | if (rdev && test_bit(Faulty, &rdev->flags)) | |
2446 | rdev = NULL; | |
2447 | if (rdev) | |
2448 | atomic_inc(&rdev->nr_pending); | |
2449 | rcu_read_unlock(); | |
2450 | ||
2451 | if (rdev) { | |
2452 | if (syncing) | |
2453 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); | |
2454 | ||
2455 | bi->bi_bdev = rdev->bdev; | |
2456 | PRINTK("for %llu schedule op %ld on disc %d\n", | |
2457 | (unsigned long long)sh->sector, bi->bi_rw, i); | |
2458 | atomic_inc(&sh->count); | |
2459 | bi->bi_sector = sh->sector + rdev->data_offset; | |
2460 | bi->bi_flags = 1 << BIO_UPTODATE; | |
2461 | bi->bi_vcnt = 1; | |
2462 | bi->bi_max_vecs = 1; | |
2463 | bi->bi_idx = 0; | |
2464 | bi->bi_io_vec = &sh->dev[i].vec; | |
2465 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; | |
2466 | bi->bi_io_vec[0].bv_offset = 0; | |
2467 | bi->bi_size = STRIPE_SIZE; | |
2468 | bi->bi_next = NULL; | |
2469 | if (rw == WRITE && | |
2470 | test_bit(R5_ReWrite, &sh->dev[i].flags)) | |
2471 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); | |
2472 | generic_make_request(bi); | |
2473 | } else { | |
2474 | if (rw == 1) | |
2475 | set_bit(STRIPE_DEGRADED, &sh->state); | |
2476 | PRINTK("skip op %ld on disc %d for sector %llu\n", | |
2477 | bi->bi_rw, i, (unsigned long long)sh->sector); | |
2478 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
2479 | set_bit(STRIPE_HANDLE, &sh->state); | |
2480 | } | |
2481 | } | |
2482 | } | |
2483 | ||
2484 | static void handle_stripe(struct stripe_head *sh, struct page *tmp_page) | |
2485 | { | |
2486 | if (sh->raid_conf->level == 6) | |
2487 | handle_stripe6(sh, tmp_page); | |
2488 | else | |
2489 | handle_stripe5(sh); | |
2490 | } | |
2491 | ||
2492 | ||
2493 | ||
2494 | static void raid5_activate_delayed(raid5_conf_t *conf) | |
2495 | { | |
2496 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { | |
2497 | while (!list_empty(&conf->delayed_list)) { | |
2498 | struct list_head *l = conf->delayed_list.next; | |
2499 | struct stripe_head *sh; | |
2500 | sh = list_entry(l, struct stripe_head, lru); | |
2501 | list_del_init(l); | |
2502 | clear_bit(STRIPE_DELAYED, &sh->state); | |
2503 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
2504 | atomic_inc(&conf->preread_active_stripes); | |
2505 | list_add_tail(&sh->lru, &conf->handle_list); | |
2506 | } | |
2507 | } | |
2508 | } | |
2509 | ||
2510 | static void activate_bit_delay(raid5_conf_t *conf) | |
2511 | { | |
2512 | /* device_lock is held */ | |
2513 | struct list_head head; | |
2514 | list_add(&head, &conf->bitmap_list); | |
2515 | list_del_init(&conf->bitmap_list); | |
2516 | while (!list_empty(&head)) { | |
2517 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); | |
2518 | list_del_init(&sh->lru); | |
2519 | atomic_inc(&sh->count); | |
2520 | __release_stripe(conf, sh); | |
2521 | } | |
2522 | } | |
2523 | ||
2524 | static void unplug_slaves(mddev_t *mddev) | |
2525 | { | |
2526 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2527 | int i; | |
2528 | ||
2529 | rcu_read_lock(); | |
2530 | for (i=0; i<mddev->raid_disks; i++) { | |
2531 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); | |
2532 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { | |
2533 | request_queue_t *r_queue = bdev_get_queue(rdev->bdev); | |
2534 | ||
2535 | atomic_inc(&rdev->nr_pending); | |
2536 | rcu_read_unlock(); | |
2537 | ||
2538 | if (r_queue->unplug_fn) | |
2539 | r_queue->unplug_fn(r_queue); | |
2540 | ||
2541 | rdev_dec_pending(rdev, mddev); | |
2542 | rcu_read_lock(); | |
2543 | } | |
2544 | } | |
2545 | rcu_read_unlock(); | |
2546 | } | |
2547 | ||
2548 | static void raid5_unplug_device(request_queue_t *q) | |
2549 | { | |
2550 | mddev_t *mddev = q->queuedata; | |
2551 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2552 | unsigned long flags; | |
2553 | ||
2554 | spin_lock_irqsave(&conf->device_lock, flags); | |
2555 | ||
2556 | if (blk_remove_plug(q)) { | |
2557 | conf->seq_flush++; | |
2558 | raid5_activate_delayed(conf); | |
72626685 | 2559 | } |
1da177e4 LT |
2560 | md_wakeup_thread(mddev->thread); |
2561 | ||
2562 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
2563 | ||
2564 | unplug_slaves(mddev); | |
2565 | } | |
2566 | ||
2567 | static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk, | |
2568 | sector_t *error_sector) | |
2569 | { | |
2570 | mddev_t *mddev = q->queuedata; | |
2571 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2572 | int i, ret = 0; | |
2573 | ||
2574 | rcu_read_lock(); | |
2575 | for (i=0; i<mddev->raid_disks && ret == 0; i++) { | |
d6065f7b | 2576 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
b2d444d7 | 2577 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
1da177e4 LT |
2578 | struct block_device *bdev = rdev->bdev; |
2579 | request_queue_t *r_queue = bdev_get_queue(bdev); | |
2580 | ||
2581 | if (!r_queue->issue_flush_fn) | |
2582 | ret = -EOPNOTSUPP; | |
2583 | else { | |
2584 | atomic_inc(&rdev->nr_pending); | |
2585 | rcu_read_unlock(); | |
2586 | ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, | |
2587 | error_sector); | |
2588 | rdev_dec_pending(rdev, mddev); | |
2589 | rcu_read_lock(); | |
2590 | } | |
2591 | } | |
2592 | } | |
2593 | rcu_read_unlock(); | |
2594 | return ret; | |
2595 | } | |
2596 | ||
7ecaa1e6 | 2597 | static int make_request(request_queue_t *q, struct bio * bi) |
1da177e4 LT |
2598 | { |
2599 | mddev_t *mddev = q->queuedata; | |
2600 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1da177e4 LT |
2601 | unsigned int dd_idx, pd_idx; |
2602 | sector_t new_sector; | |
2603 | sector_t logical_sector, last_sector; | |
2604 | struct stripe_head *sh; | |
a362357b | 2605 | const int rw = bio_data_dir(bi); |
f6344757 | 2606 | int remaining; |
1da177e4 | 2607 | |
e5dcdd80 N |
2608 | if (unlikely(bio_barrier(bi))) { |
2609 | bio_endio(bi, bi->bi_size, -EOPNOTSUPP); | |
2610 | return 0; | |
2611 | } | |
2612 | ||
3d310eb7 | 2613 | md_write_start(mddev, bi); |
06d91a5f | 2614 | |
a362357b JA |
2615 | disk_stat_inc(mddev->gendisk, ios[rw]); |
2616 | disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi)); | |
1da177e4 LT |
2617 | |
2618 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); | |
2619 | last_sector = bi->bi_sector + (bi->bi_size>>9); | |
2620 | bi->bi_next = NULL; | |
2621 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ | |
06d91a5f | 2622 | |
1da177e4 LT |
2623 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
2624 | DEFINE_WAIT(w); | |
16a53ecc | 2625 | int disks, data_disks; |
b578d55f | 2626 | |
7ecaa1e6 | 2627 | retry: |
b578d55f | 2628 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
7ecaa1e6 N |
2629 | if (likely(conf->expand_progress == MaxSector)) |
2630 | disks = conf->raid_disks; | |
2631 | else { | |
df8e7f76 N |
2632 | /* spinlock is needed as expand_progress may be |
2633 | * 64bit on a 32bit platform, and so it might be | |
2634 | * possible to see a half-updated value | |
2635 | * Ofcourse expand_progress could change after | |
2636 | * the lock is dropped, so once we get a reference | |
2637 | * to the stripe that we think it is, we will have | |
2638 | * to check again. | |
2639 | */ | |
7ecaa1e6 N |
2640 | spin_lock_irq(&conf->device_lock); |
2641 | disks = conf->raid_disks; | |
2642 | if (logical_sector >= conf->expand_progress) | |
2643 | disks = conf->previous_raid_disks; | |
b578d55f N |
2644 | else { |
2645 | if (logical_sector >= conf->expand_lo) { | |
2646 | spin_unlock_irq(&conf->device_lock); | |
2647 | schedule(); | |
2648 | goto retry; | |
2649 | } | |
2650 | } | |
7ecaa1e6 N |
2651 | spin_unlock_irq(&conf->device_lock); |
2652 | } | |
16a53ecc N |
2653 | data_disks = disks - conf->max_degraded; |
2654 | ||
2655 | new_sector = raid5_compute_sector(logical_sector, disks, data_disks, | |
7ecaa1e6 | 2656 | &dd_idx, &pd_idx, conf); |
1da177e4 LT |
2657 | PRINTK("raid5: make_request, sector %llu logical %llu\n", |
2658 | (unsigned long long)new_sector, | |
2659 | (unsigned long long)logical_sector); | |
2660 | ||
7ecaa1e6 | 2661 | sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK)); |
1da177e4 | 2662 | if (sh) { |
7ecaa1e6 N |
2663 | if (unlikely(conf->expand_progress != MaxSector)) { |
2664 | /* expansion might have moved on while waiting for a | |
df8e7f76 N |
2665 | * stripe, so we must do the range check again. |
2666 | * Expansion could still move past after this | |
2667 | * test, but as we are holding a reference to | |
2668 | * 'sh', we know that if that happens, | |
2669 | * STRIPE_EXPANDING will get set and the expansion | |
2670 | * won't proceed until we finish with the stripe. | |
7ecaa1e6 N |
2671 | */ |
2672 | int must_retry = 0; | |
2673 | spin_lock_irq(&conf->device_lock); | |
2674 | if (logical_sector < conf->expand_progress && | |
2675 | disks == conf->previous_raid_disks) | |
2676 | /* mismatch, need to try again */ | |
2677 | must_retry = 1; | |
2678 | spin_unlock_irq(&conf->device_lock); | |
2679 | if (must_retry) { | |
2680 | release_stripe(sh); | |
2681 | goto retry; | |
2682 | } | |
2683 | } | |
e464eafd N |
2684 | /* FIXME what if we get a false positive because these |
2685 | * are being updated. | |
2686 | */ | |
2687 | if (logical_sector >= mddev->suspend_lo && | |
2688 | logical_sector < mddev->suspend_hi) { | |
2689 | release_stripe(sh); | |
2690 | schedule(); | |
2691 | goto retry; | |
2692 | } | |
7ecaa1e6 N |
2693 | |
2694 | if (test_bit(STRIPE_EXPANDING, &sh->state) || | |
2695 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { | |
2696 | /* Stripe is busy expanding or | |
2697 | * add failed due to overlap. Flush everything | |
1da177e4 LT |
2698 | * and wait a while |
2699 | */ | |
2700 | raid5_unplug_device(mddev->queue); | |
2701 | release_stripe(sh); | |
2702 | schedule(); | |
2703 | goto retry; | |
2704 | } | |
2705 | finish_wait(&conf->wait_for_overlap, &w); | |
16a53ecc | 2706 | handle_stripe(sh, NULL); |
1da177e4 | 2707 | release_stripe(sh); |
1da177e4 LT |
2708 | } else { |
2709 | /* cannot get stripe for read-ahead, just give-up */ | |
2710 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
2711 | finish_wait(&conf->wait_for_overlap, &w); | |
2712 | break; | |
2713 | } | |
2714 | ||
2715 | } | |
2716 | spin_lock_irq(&conf->device_lock); | |
f6344757 N |
2717 | remaining = --bi->bi_phys_segments; |
2718 | spin_unlock_irq(&conf->device_lock); | |
2719 | if (remaining == 0) { | |
1da177e4 LT |
2720 | int bytes = bi->bi_size; |
2721 | ||
16a53ecc | 2722 | if ( rw == WRITE ) |
1da177e4 LT |
2723 | md_write_end(mddev); |
2724 | bi->bi_size = 0; | |
2725 | bi->bi_end_io(bi, bytes, 0); | |
2726 | } | |
1da177e4 LT |
2727 | return 0; |
2728 | } | |
2729 | ||
52c03291 | 2730 | static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped) |
1da177e4 | 2731 | { |
52c03291 N |
2732 | /* reshaping is quite different to recovery/resync so it is |
2733 | * handled quite separately ... here. | |
2734 | * | |
2735 | * On each call to sync_request, we gather one chunk worth of | |
2736 | * destination stripes and flag them as expanding. | |
2737 | * Then we find all the source stripes and request reads. | |
2738 | * As the reads complete, handle_stripe will copy the data | |
2739 | * into the destination stripe and release that stripe. | |
2740 | */ | |
1da177e4 LT |
2741 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
2742 | struct stripe_head *sh; | |
ccfcc3c1 N |
2743 | int pd_idx; |
2744 | sector_t first_sector, last_sector; | |
52c03291 N |
2745 | int raid_disks; |
2746 | int data_disks; | |
2747 | int i; | |
2748 | int dd_idx; | |
2749 | sector_t writepos, safepos, gap; | |
2750 | ||
2751 | if (sector_nr == 0 && | |
2752 | conf->expand_progress != 0) { | |
2753 | /* restarting in the middle, skip the initial sectors */ | |
2754 | sector_nr = conf->expand_progress; | |
2755 | sector_div(sector_nr, conf->raid_disks-1); | |
2756 | *skipped = 1; | |
2757 | return sector_nr; | |
2758 | } | |
2759 | ||
2760 | /* we update the metadata when there is more than 3Meg | |
2761 | * in the block range (that is rather arbitrary, should | |
2762 | * probably be time based) or when the data about to be | |
2763 | * copied would over-write the source of the data at | |
2764 | * the front of the range. | |
2765 | * i.e. one new_stripe forward from expand_progress new_maps | |
2766 | * to after where expand_lo old_maps to | |
2767 | */ | |
2768 | writepos = conf->expand_progress + | |
2769 | conf->chunk_size/512*(conf->raid_disks-1); | |
2770 | sector_div(writepos, conf->raid_disks-1); | |
2771 | safepos = conf->expand_lo; | |
2772 | sector_div(safepos, conf->previous_raid_disks-1); | |
2773 | gap = conf->expand_progress - conf->expand_lo; | |
2774 | ||
2775 | if (writepos >= safepos || | |
2776 | gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) { | |
2777 | /* Cannot proceed until we've updated the superblock... */ | |
2778 | wait_event(conf->wait_for_overlap, | |
2779 | atomic_read(&conf->reshape_stripes)==0); | |
2780 | mddev->reshape_position = conf->expand_progress; | |
850b2b42 | 2781 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
52c03291 | 2782 | md_wakeup_thread(mddev->thread); |
850b2b42 | 2783 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
52c03291 N |
2784 | kthread_should_stop()); |
2785 | spin_lock_irq(&conf->device_lock); | |
2786 | conf->expand_lo = mddev->reshape_position; | |
2787 | spin_unlock_irq(&conf->device_lock); | |
2788 | wake_up(&conf->wait_for_overlap); | |
2789 | } | |
2790 | ||
2791 | for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) { | |
2792 | int j; | |
2793 | int skipped = 0; | |
2794 | pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks); | |
2795 | sh = get_active_stripe(conf, sector_nr+i, | |
2796 | conf->raid_disks, pd_idx, 0); | |
2797 | set_bit(STRIPE_EXPANDING, &sh->state); | |
2798 | atomic_inc(&conf->reshape_stripes); | |
2799 | /* If any of this stripe is beyond the end of the old | |
2800 | * array, then we need to zero those blocks | |
2801 | */ | |
2802 | for (j=sh->disks; j--;) { | |
2803 | sector_t s; | |
2804 | if (j == sh->pd_idx) | |
2805 | continue; | |
2806 | s = compute_blocknr(sh, j); | |
2807 | if (s < (mddev->array_size<<1)) { | |
2808 | skipped = 1; | |
2809 | continue; | |
2810 | } | |
2811 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); | |
2812 | set_bit(R5_Expanded, &sh->dev[j].flags); | |
2813 | set_bit(R5_UPTODATE, &sh->dev[j].flags); | |
2814 | } | |
2815 | if (!skipped) { | |
2816 | set_bit(STRIPE_EXPAND_READY, &sh->state); | |
2817 | set_bit(STRIPE_HANDLE, &sh->state); | |
2818 | } | |
2819 | release_stripe(sh); | |
2820 | } | |
2821 | spin_lock_irq(&conf->device_lock); | |
2822 | conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1); | |
2823 | spin_unlock_irq(&conf->device_lock); | |
2824 | /* Ok, those stripe are ready. We can start scheduling | |
2825 | * reads on the source stripes. | |
2826 | * The source stripes are determined by mapping the first and last | |
2827 | * block on the destination stripes. | |
2828 | */ | |
2829 | raid_disks = conf->previous_raid_disks; | |
2830 | data_disks = raid_disks - 1; | |
2831 | first_sector = | |
2832 | raid5_compute_sector(sector_nr*(conf->raid_disks-1), | |
2833 | raid_disks, data_disks, | |
2834 | &dd_idx, &pd_idx, conf); | |
2835 | last_sector = | |
2836 | raid5_compute_sector((sector_nr+conf->chunk_size/512) | |
2837 | *(conf->raid_disks-1) -1, | |
2838 | raid_disks, data_disks, | |
2839 | &dd_idx, &pd_idx, conf); | |
2840 | if (last_sector >= (mddev->size<<1)) | |
2841 | last_sector = (mddev->size<<1)-1; | |
2842 | while (first_sector <= last_sector) { | |
2843 | pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks); | |
2844 | sh = get_active_stripe(conf, first_sector, | |
2845 | conf->previous_raid_disks, pd_idx, 0); | |
2846 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); | |
2847 | set_bit(STRIPE_HANDLE, &sh->state); | |
2848 | release_stripe(sh); | |
2849 | first_sector += STRIPE_SECTORS; | |
2850 | } | |
2851 | return conf->chunk_size>>9; | |
2852 | } | |
2853 | ||
2854 | /* FIXME go_faster isn't used */ | |
2855 | static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) | |
2856 | { | |
2857 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
2858 | struct stripe_head *sh; | |
2859 | int pd_idx; | |
1da177e4 | 2860 | int raid_disks = conf->raid_disks; |
72626685 N |
2861 | sector_t max_sector = mddev->size << 1; |
2862 | int sync_blocks; | |
16a53ecc N |
2863 | int still_degraded = 0; |
2864 | int i; | |
1da177e4 | 2865 | |
72626685 | 2866 | if (sector_nr >= max_sector) { |
1da177e4 LT |
2867 | /* just being told to finish up .. nothing much to do */ |
2868 | unplug_slaves(mddev); | |
29269553 N |
2869 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
2870 | end_reshape(conf); | |
2871 | return 0; | |
2872 | } | |
72626685 N |
2873 | |
2874 | if (mddev->curr_resync < max_sector) /* aborted */ | |
2875 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
2876 | &sync_blocks, 1); | |
16a53ecc | 2877 | else /* completed sync */ |
72626685 N |
2878 | conf->fullsync = 0; |
2879 | bitmap_close_sync(mddev->bitmap); | |
2880 | ||
1da177e4 LT |
2881 | return 0; |
2882 | } | |
ccfcc3c1 | 2883 | |
52c03291 N |
2884 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
2885 | return reshape_request(mddev, sector_nr, skipped); | |
f6705578 | 2886 | |
16a53ecc | 2887 | /* if there is too many failed drives and we are trying |
1da177e4 LT |
2888 | * to resync, then assert that we are finished, because there is |
2889 | * nothing we can do. | |
2890 | */ | |
3285edf1 | 2891 | if (mddev->degraded >= conf->max_degraded && |
16a53ecc | 2892 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
57afd89f N |
2893 | sector_t rv = (mddev->size << 1) - sector_nr; |
2894 | *skipped = 1; | |
1da177e4 LT |
2895 | return rv; |
2896 | } | |
72626685 | 2897 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
3855ad9f | 2898 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
72626685 N |
2899 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
2900 | /* we can skip this block, and probably more */ | |
2901 | sync_blocks /= STRIPE_SECTORS; | |
2902 | *skipped = 1; | |
2903 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ | |
2904 | } | |
1da177e4 | 2905 | |
ccfcc3c1 | 2906 | pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks); |
7ecaa1e6 | 2907 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1); |
1da177e4 | 2908 | if (sh == NULL) { |
7ecaa1e6 | 2909 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0); |
1da177e4 | 2910 | /* make sure we don't swamp the stripe cache if someone else |
16a53ecc | 2911 | * is trying to get access |
1da177e4 | 2912 | */ |
66c006a5 | 2913 | schedule_timeout_uninterruptible(1); |
1da177e4 | 2914 | } |
16a53ecc N |
2915 | /* Need to check if array will still be degraded after recovery/resync |
2916 | * We don't need to check the 'failed' flag as when that gets set, | |
2917 | * recovery aborts. | |
2918 | */ | |
2919 | for (i=0; i<mddev->raid_disks; i++) | |
2920 | if (conf->disks[i].rdev == NULL) | |
2921 | still_degraded = 1; | |
2922 | ||
2923 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); | |
2924 | ||
2925 | spin_lock(&sh->lock); | |
1da177e4 LT |
2926 | set_bit(STRIPE_SYNCING, &sh->state); |
2927 | clear_bit(STRIPE_INSYNC, &sh->state); | |
2928 | spin_unlock(&sh->lock); | |
2929 | ||
16a53ecc | 2930 | handle_stripe(sh, NULL); |
1da177e4 LT |
2931 | release_stripe(sh); |
2932 | ||
2933 | return STRIPE_SECTORS; | |
2934 | } | |
2935 | ||
2936 | /* | |
2937 | * This is our raid5 kernel thread. | |
2938 | * | |
2939 | * We scan the hash table for stripes which can be handled now. | |
2940 | * During the scan, completed stripes are saved for us by the interrupt | |
2941 | * handler, so that they will not have to wait for our next wakeup. | |
2942 | */ | |
2943 | static void raid5d (mddev_t *mddev) | |
2944 | { | |
2945 | struct stripe_head *sh; | |
2946 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2947 | int handled; | |
2948 | ||
2949 | PRINTK("+++ raid5d active\n"); | |
2950 | ||
2951 | md_check_recovery(mddev); | |
1da177e4 LT |
2952 | |
2953 | handled = 0; | |
2954 | spin_lock_irq(&conf->device_lock); | |
2955 | while (1) { | |
2956 | struct list_head *first; | |
2957 | ||
ae3c20cc | 2958 | if (conf->seq_flush != conf->seq_write) { |
72626685 | 2959 | int seq = conf->seq_flush; |
700e432d | 2960 | spin_unlock_irq(&conf->device_lock); |
72626685 | 2961 | bitmap_unplug(mddev->bitmap); |
700e432d | 2962 | spin_lock_irq(&conf->device_lock); |
72626685 N |
2963 | conf->seq_write = seq; |
2964 | activate_bit_delay(conf); | |
2965 | } | |
2966 | ||
1da177e4 LT |
2967 | if (list_empty(&conf->handle_list) && |
2968 | atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD && | |
2969 | !blk_queue_plugged(mddev->queue) && | |
2970 | !list_empty(&conf->delayed_list)) | |
2971 | raid5_activate_delayed(conf); | |
2972 | ||
2973 | if (list_empty(&conf->handle_list)) | |
2974 | break; | |
2975 | ||
2976 | first = conf->handle_list.next; | |
2977 | sh = list_entry(first, struct stripe_head, lru); | |
2978 | ||
2979 | list_del_init(first); | |
2980 | atomic_inc(&sh->count); | |
78bafebd | 2981 | BUG_ON(atomic_read(&sh->count)!= 1); |
1da177e4 LT |
2982 | spin_unlock_irq(&conf->device_lock); |
2983 | ||
2984 | handled++; | |
16a53ecc | 2985 | handle_stripe(sh, conf->spare_page); |
1da177e4 LT |
2986 | release_stripe(sh); |
2987 | ||
2988 | spin_lock_irq(&conf->device_lock); | |
2989 | } | |
2990 | PRINTK("%d stripes handled\n", handled); | |
2991 | ||
2992 | spin_unlock_irq(&conf->device_lock); | |
2993 | ||
2994 | unplug_slaves(mddev); | |
2995 | ||
2996 | PRINTK("--- raid5d inactive\n"); | |
2997 | } | |
2998 | ||
3f294f4f | 2999 | static ssize_t |
007583c9 | 3000 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
3f294f4f | 3001 | { |
007583c9 | 3002 | raid5_conf_t *conf = mddev_to_conf(mddev); |
96de1e66 N |
3003 | if (conf) |
3004 | return sprintf(page, "%d\n", conf->max_nr_stripes); | |
3005 | else | |
3006 | return 0; | |
3f294f4f N |
3007 | } |
3008 | ||
3009 | static ssize_t | |
007583c9 | 3010 | raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len) |
3f294f4f | 3011 | { |
007583c9 | 3012 | raid5_conf_t *conf = mddev_to_conf(mddev); |
3f294f4f N |
3013 | char *end; |
3014 | int new; | |
3015 | if (len >= PAGE_SIZE) | |
3016 | return -EINVAL; | |
96de1e66 N |
3017 | if (!conf) |
3018 | return -ENODEV; | |
3f294f4f N |
3019 | |
3020 | new = simple_strtoul(page, &end, 10); | |
3021 | if (!*page || (*end && *end != '\n') ) | |
3022 | return -EINVAL; | |
3023 | if (new <= 16 || new > 32768) | |
3024 | return -EINVAL; | |
3025 | while (new < conf->max_nr_stripes) { | |
3026 | if (drop_one_stripe(conf)) | |
3027 | conf->max_nr_stripes--; | |
3028 | else | |
3029 | break; | |
3030 | } | |
3031 | while (new > conf->max_nr_stripes) { | |
3032 | if (grow_one_stripe(conf)) | |
3033 | conf->max_nr_stripes++; | |
3034 | else break; | |
3035 | } | |
3036 | return len; | |
3037 | } | |
007583c9 | 3038 | |
96de1e66 N |
3039 | static struct md_sysfs_entry |
3040 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, | |
3041 | raid5_show_stripe_cache_size, | |
3042 | raid5_store_stripe_cache_size); | |
3f294f4f N |
3043 | |
3044 | static ssize_t | |
96de1e66 | 3045 | stripe_cache_active_show(mddev_t *mddev, char *page) |
3f294f4f | 3046 | { |
007583c9 | 3047 | raid5_conf_t *conf = mddev_to_conf(mddev); |
96de1e66 N |
3048 | if (conf) |
3049 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); | |
3050 | else | |
3051 | return 0; | |
3f294f4f N |
3052 | } |
3053 | ||
96de1e66 N |
3054 | static struct md_sysfs_entry |
3055 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); | |
3f294f4f | 3056 | |
007583c9 | 3057 | static struct attribute *raid5_attrs[] = { |
3f294f4f N |
3058 | &raid5_stripecache_size.attr, |
3059 | &raid5_stripecache_active.attr, | |
3060 | NULL, | |
3061 | }; | |
007583c9 N |
3062 | static struct attribute_group raid5_attrs_group = { |
3063 | .name = NULL, | |
3064 | .attrs = raid5_attrs, | |
3f294f4f N |
3065 | }; |
3066 | ||
72626685 | 3067 | static int run(mddev_t *mddev) |
1da177e4 LT |
3068 | { |
3069 | raid5_conf_t *conf; | |
3070 | int raid_disk, memory; | |
3071 | mdk_rdev_t *rdev; | |
3072 | struct disk_info *disk; | |
3073 | struct list_head *tmp; | |
02c2de8c | 3074 | int working_disks = 0; |
1da177e4 | 3075 | |
16a53ecc N |
3076 | if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) { |
3077 | printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n", | |
14f8d26b | 3078 | mdname(mddev), mddev->level); |
1da177e4 LT |
3079 | return -EIO; |
3080 | } | |
3081 | ||
f6705578 N |
3082 | if (mddev->reshape_position != MaxSector) { |
3083 | /* Check that we can continue the reshape. | |
3084 | * Currently only disks can change, it must | |
3085 | * increase, and we must be past the point where | |
3086 | * a stripe over-writes itself | |
3087 | */ | |
3088 | sector_t here_new, here_old; | |
3089 | int old_disks; | |
3090 | ||
3091 | if (mddev->new_level != mddev->level || | |
3092 | mddev->new_layout != mddev->layout || | |
3093 | mddev->new_chunk != mddev->chunk_size) { | |
3094 | printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n", | |
3095 | mdname(mddev)); | |
3096 | return -EINVAL; | |
3097 | } | |
3098 | if (mddev->delta_disks <= 0) { | |
3099 | printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n", | |
3100 | mdname(mddev)); | |
3101 | return -EINVAL; | |
3102 | } | |
3103 | old_disks = mddev->raid_disks - mddev->delta_disks; | |
3104 | /* reshape_position must be on a new-stripe boundary, and one | |
3105 | * further up in new geometry must map after here in old geometry. | |
3106 | */ | |
3107 | here_new = mddev->reshape_position; | |
3108 | if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) { | |
3109 | printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n"); | |
3110 | return -EINVAL; | |
3111 | } | |
3112 | /* here_new is the stripe we will write to */ | |
3113 | here_old = mddev->reshape_position; | |
3114 | sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1)); | |
3115 | /* here_old is the first stripe that we might need to read from */ | |
3116 | if (here_new >= here_old) { | |
3117 | /* Reading from the same stripe as writing to - bad */ | |
3118 | printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n"); | |
3119 | return -EINVAL; | |
3120 | } | |
3121 | printk(KERN_INFO "raid5: reshape will continue\n"); | |
3122 | /* OK, we should be able to continue; */ | |
3123 | } | |
3124 | ||
3125 | ||
b55e6bfc | 3126 | mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL); |
1da177e4 LT |
3127 | if ((conf = mddev->private) == NULL) |
3128 | goto abort; | |
f6705578 N |
3129 | if (mddev->reshape_position == MaxSector) { |
3130 | conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks; | |
3131 | } else { | |
3132 | conf->raid_disks = mddev->raid_disks; | |
3133 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; | |
3134 | } | |
3135 | ||
3136 | conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info), | |
b55e6bfc N |
3137 | GFP_KERNEL); |
3138 | if (!conf->disks) | |
3139 | goto abort; | |
9ffae0cf | 3140 | |
1da177e4 LT |
3141 | conf->mddev = mddev; |
3142 | ||
fccddba0 | 3143 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
1da177e4 | 3144 | goto abort; |
1da177e4 | 3145 | |
16a53ecc N |
3146 | if (mddev->level == 6) { |
3147 | conf->spare_page = alloc_page(GFP_KERNEL); | |
3148 | if (!conf->spare_page) | |
3149 | goto abort; | |
3150 | } | |
1da177e4 LT |
3151 | spin_lock_init(&conf->device_lock); |
3152 | init_waitqueue_head(&conf->wait_for_stripe); | |
3153 | init_waitqueue_head(&conf->wait_for_overlap); | |
3154 | INIT_LIST_HEAD(&conf->handle_list); | |
3155 | INIT_LIST_HEAD(&conf->delayed_list); | |
72626685 | 3156 | INIT_LIST_HEAD(&conf->bitmap_list); |
1da177e4 LT |
3157 | INIT_LIST_HEAD(&conf->inactive_list); |
3158 | atomic_set(&conf->active_stripes, 0); | |
3159 | atomic_set(&conf->preread_active_stripes, 0); | |
3160 | ||
1da177e4 LT |
3161 | PRINTK("raid5: run(%s) called.\n", mdname(mddev)); |
3162 | ||
3163 | ITERATE_RDEV(mddev,rdev,tmp) { | |
3164 | raid_disk = rdev->raid_disk; | |
f6705578 | 3165 | if (raid_disk >= conf->raid_disks |
1da177e4 LT |
3166 | || raid_disk < 0) |
3167 | continue; | |
3168 | disk = conf->disks + raid_disk; | |
3169 | ||
3170 | disk->rdev = rdev; | |
3171 | ||
b2d444d7 | 3172 | if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
3173 | char b[BDEVNAME_SIZE]; |
3174 | printk(KERN_INFO "raid5: device %s operational as raid" | |
3175 | " disk %d\n", bdevname(rdev->bdev,b), | |
3176 | raid_disk); | |
02c2de8c | 3177 | working_disks++; |
1da177e4 LT |
3178 | } |
3179 | } | |
3180 | ||
1da177e4 | 3181 | /* |
16a53ecc | 3182 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
1da177e4 | 3183 | */ |
02c2de8c | 3184 | mddev->degraded = conf->raid_disks - working_disks; |
1da177e4 LT |
3185 | conf->mddev = mddev; |
3186 | conf->chunk_size = mddev->chunk_size; | |
3187 | conf->level = mddev->level; | |
16a53ecc N |
3188 | if (conf->level == 6) |
3189 | conf->max_degraded = 2; | |
3190 | else | |
3191 | conf->max_degraded = 1; | |
1da177e4 LT |
3192 | conf->algorithm = mddev->layout; |
3193 | conf->max_nr_stripes = NR_STRIPES; | |
f6705578 | 3194 | conf->expand_progress = mddev->reshape_position; |
1da177e4 LT |
3195 | |
3196 | /* device size must be a multiple of chunk size */ | |
3197 | mddev->size &= ~(mddev->chunk_size/1024 -1); | |
b1581566 | 3198 | mddev->resync_max_sectors = mddev->size << 1; |
1da177e4 | 3199 | |
16a53ecc N |
3200 | if (conf->level == 6 && conf->raid_disks < 4) { |
3201 | printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n", | |
3202 | mdname(mddev), conf->raid_disks); | |
3203 | goto abort; | |
3204 | } | |
1da177e4 LT |
3205 | if (!conf->chunk_size || conf->chunk_size % 4) { |
3206 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", | |
3207 | conf->chunk_size, mdname(mddev)); | |
3208 | goto abort; | |
3209 | } | |
3210 | if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) { | |
3211 | printk(KERN_ERR | |
3212 | "raid5: unsupported parity algorithm %d for %s\n", | |
3213 | conf->algorithm, mdname(mddev)); | |
3214 | goto abort; | |
3215 | } | |
16a53ecc | 3216 | if (mddev->degraded > conf->max_degraded) { |
1da177e4 LT |
3217 | printk(KERN_ERR "raid5: not enough operational devices for %s" |
3218 | " (%d/%d failed)\n", | |
02c2de8c | 3219 | mdname(mddev), mddev->degraded, conf->raid_disks); |
1da177e4 LT |
3220 | goto abort; |
3221 | } | |
3222 | ||
16a53ecc | 3223 | if (mddev->degraded > 0 && |
1da177e4 | 3224 | mddev->recovery_cp != MaxSector) { |
6ff8d8ec N |
3225 | if (mddev->ok_start_degraded) |
3226 | printk(KERN_WARNING | |
3227 | "raid5: starting dirty degraded array: %s" | |
3228 | "- data corruption possible.\n", | |
3229 | mdname(mddev)); | |
3230 | else { | |
3231 | printk(KERN_ERR | |
3232 | "raid5: cannot start dirty degraded array for %s\n", | |
3233 | mdname(mddev)); | |
3234 | goto abort; | |
3235 | } | |
1da177e4 LT |
3236 | } |
3237 | ||
3238 | { | |
3239 | mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5"); | |
3240 | if (!mddev->thread) { | |
3241 | printk(KERN_ERR | |
3242 | "raid5: couldn't allocate thread for %s\n", | |
3243 | mdname(mddev)); | |
3244 | goto abort; | |
3245 | } | |
3246 | } | |
5036805b | 3247 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
1da177e4 LT |
3248 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
3249 | if (grow_stripes(conf, conf->max_nr_stripes)) { | |
3250 | printk(KERN_ERR | |
3251 | "raid5: couldn't allocate %dkB for buffers\n", memory); | |
3252 | shrink_stripes(conf); | |
3253 | md_unregister_thread(mddev->thread); | |
3254 | goto abort; | |
3255 | } else | |
3256 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", | |
3257 | memory, mdname(mddev)); | |
3258 | ||
3259 | if (mddev->degraded == 0) | |
3260 | printk("raid5: raid level %d set %s active with %d out of %d" | |
3261 | " devices, algorithm %d\n", conf->level, mdname(mddev), | |
3262 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, | |
3263 | conf->algorithm); | |
3264 | else | |
3265 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" | |
3266 | " out of %d devices, algorithm %d\n", conf->level, | |
3267 | mdname(mddev), mddev->raid_disks - mddev->degraded, | |
3268 | mddev->raid_disks, conf->algorithm); | |
3269 | ||
3270 | print_raid5_conf(conf); | |
3271 | ||
f6705578 N |
3272 | if (conf->expand_progress != MaxSector) { |
3273 | printk("...ok start reshape thread\n"); | |
b578d55f | 3274 | conf->expand_lo = conf->expand_progress; |
f6705578 N |
3275 | atomic_set(&conf->reshape_stripes, 0); |
3276 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
3277 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
3278 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
3279 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
3280 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
3281 | "%s_reshape"); | |
f6705578 N |
3282 | } |
3283 | ||
1da177e4 | 3284 | /* read-ahead size must cover two whole stripes, which is |
16a53ecc | 3285 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
1da177e4 LT |
3286 | */ |
3287 | { | |
16a53ecc N |
3288 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
3289 | int stripe = data_disks * | |
8932c2e0 | 3290 | (mddev->chunk_size / PAGE_SIZE); |
1da177e4 LT |
3291 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
3292 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
3293 | } | |
3294 | ||
3295 | /* Ok, everything is just fine now */ | |
007583c9 | 3296 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group); |
7a5febe9 N |
3297 | |
3298 | mddev->queue->unplug_fn = raid5_unplug_device; | |
3299 | mddev->queue->issue_flush_fn = raid5_issue_flush; | |
16a53ecc N |
3300 | mddev->array_size = mddev->size * (conf->previous_raid_disks - |
3301 | conf->max_degraded); | |
7a5febe9 | 3302 | |
1da177e4 LT |
3303 | return 0; |
3304 | abort: | |
3305 | if (conf) { | |
3306 | print_raid5_conf(conf); | |
16a53ecc | 3307 | safe_put_page(conf->spare_page); |
b55e6bfc | 3308 | kfree(conf->disks); |
fccddba0 | 3309 | kfree(conf->stripe_hashtbl); |
1da177e4 LT |
3310 | kfree(conf); |
3311 | } | |
3312 | mddev->private = NULL; | |
3313 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); | |
3314 | return -EIO; | |
3315 | } | |
3316 | ||
3317 | ||
3318 | ||
3f294f4f | 3319 | static int stop(mddev_t *mddev) |
1da177e4 LT |
3320 | { |
3321 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
3322 | ||
3323 | md_unregister_thread(mddev->thread); | |
3324 | mddev->thread = NULL; | |
3325 | shrink_stripes(conf); | |
fccddba0 | 3326 | kfree(conf->stripe_hashtbl); |
1da177e4 | 3327 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
007583c9 | 3328 | sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); |
b55e6bfc | 3329 | kfree(conf->disks); |
96de1e66 | 3330 | kfree(conf); |
1da177e4 LT |
3331 | mddev->private = NULL; |
3332 | return 0; | |
3333 | } | |
3334 | ||
3335 | #if RAID5_DEBUG | |
16a53ecc | 3336 | static void print_sh (struct seq_file *seq, struct stripe_head *sh) |
1da177e4 LT |
3337 | { |
3338 | int i; | |
3339 | ||
16a53ecc N |
3340 | seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n", |
3341 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); | |
3342 | seq_printf(seq, "sh %llu, count %d.\n", | |
3343 | (unsigned long long)sh->sector, atomic_read(&sh->count)); | |
3344 | seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector); | |
7ecaa1e6 | 3345 | for (i = 0; i < sh->disks; i++) { |
16a53ecc N |
3346 | seq_printf(seq, "(cache%d: %p %ld) ", |
3347 | i, sh->dev[i].page, sh->dev[i].flags); | |
1da177e4 | 3348 | } |
16a53ecc | 3349 | seq_printf(seq, "\n"); |
1da177e4 LT |
3350 | } |
3351 | ||
16a53ecc | 3352 | static void printall (struct seq_file *seq, raid5_conf_t *conf) |
1da177e4 LT |
3353 | { |
3354 | struct stripe_head *sh; | |
fccddba0 | 3355 | struct hlist_node *hn; |
1da177e4 LT |
3356 | int i; |
3357 | ||
3358 | spin_lock_irq(&conf->device_lock); | |
3359 | for (i = 0; i < NR_HASH; i++) { | |
fccddba0 | 3360 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
1da177e4 LT |
3361 | if (sh->raid_conf != conf) |
3362 | continue; | |
16a53ecc | 3363 | print_sh(seq, sh); |
1da177e4 LT |
3364 | } |
3365 | } | |
3366 | spin_unlock_irq(&conf->device_lock); | |
3367 | } | |
3368 | #endif | |
3369 | ||
3370 | static void status (struct seq_file *seq, mddev_t *mddev) | |
3371 | { | |
3372 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
3373 | int i; | |
3374 | ||
3375 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); | |
02c2de8c | 3376 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
1da177e4 LT |
3377 | for (i = 0; i < conf->raid_disks; i++) |
3378 | seq_printf (seq, "%s", | |
3379 | conf->disks[i].rdev && | |
b2d444d7 | 3380 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
1da177e4 LT |
3381 | seq_printf (seq, "]"); |
3382 | #if RAID5_DEBUG | |
16a53ecc N |
3383 | seq_printf (seq, "\n"); |
3384 | printall(seq, conf); | |
1da177e4 LT |
3385 | #endif |
3386 | } | |
3387 | ||
3388 | static void print_raid5_conf (raid5_conf_t *conf) | |
3389 | { | |
3390 | int i; | |
3391 | struct disk_info *tmp; | |
3392 | ||
3393 | printk("RAID5 conf printout:\n"); | |
3394 | if (!conf) { | |
3395 | printk("(conf==NULL)\n"); | |
3396 | return; | |
3397 | } | |
02c2de8c N |
3398 | printk(" --- rd:%d wd:%d\n", conf->raid_disks, |
3399 | conf->raid_disks - conf->mddev->degraded); | |
1da177e4 LT |
3400 | |
3401 | for (i = 0; i < conf->raid_disks; i++) { | |
3402 | char b[BDEVNAME_SIZE]; | |
3403 | tmp = conf->disks + i; | |
3404 | if (tmp->rdev) | |
3405 | printk(" disk %d, o:%d, dev:%s\n", | |
b2d444d7 | 3406 | i, !test_bit(Faulty, &tmp->rdev->flags), |
1da177e4 LT |
3407 | bdevname(tmp->rdev->bdev,b)); |
3408 | } | |
3409 | } | |
3410 | ||
3411 | static int raid5_spare_active(mddev_t *mddev) | |
3412 | { | |
3413 | int i; | |
3414 | raid5_conf_t *conf = mddev->private; | |
3415 | struct disk_info *tmp; | |
3416 | ||
3417 | for (i = 0; i < conf->raid_disks; i++) { | |
3418 | tmp = conf->disks + i; | |
3419 | if (tmp->rdev | |
b2d444d7 N |
3420 | && !test_bit(Faulty, &tmp->rdev->flags) |
3421 | && !test_bit(In_sync, &tmp->rdev->flags)) { | |
1da177e4 | 3422 | mddev->degraded--; |
b2d444d7 | 3423 | set_bit(In_sync, &tmp->rdev->flags); |
1da177e4 LT |
3424 | } |
3425 | } | |
3426 | print_raid5_conf(conf); | |
3427 | return 0; | |
3428 | } | |
3429 | ||
3430 | static int raid5_remove_disk(mddev_t *mddev, int number) | |
3431 | { | |
3432 | raid5_conf_t *conf = mddev->private; | |
3433 | int err = 0; | |
3434 | mdk_rdev_t *rdev; | |
3435 | struct disk_info *p = conf->disks + number; | |
3436 | ||
3437 | print_raid5_conf(conf); | |
3438 | rdev = p->rdev; | |
3439 | if (rdev) { | |
b2d444d7 | 3440 | if (test_bit(In_sync, &rdev->flags) || |
1da177e4 LT |
3441 | atomic_read(&rdev->nr_pending)) { |
3442 | err = -EBUSY; | |
3443 | goto abort; | |
3444 | } | |
3445 | p->rdev = NULL; | |
fbd568a3 | 3446 | synchronize_rcu(); |
1da177e4 LT |
3447 | if (atomic_read(&rdev->nr_pending)) { |
3448 | /* lost the race, try later */ | |
3449 | err = -EBUSY; | |
3450 | p->rdev = rdev; | |
3451 | } | |
3452 | } | |
3453 | abort: | |
3454 | ||
3455 | print_raid5_conf(conf); | |
3456 | return err; | |
3457 | } | |
3458 | ||
3459 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) | |
3460 | { | |
3461 | raid5_conf_t *conf = mddev->private; | |
3462 | int found = 0; | |
3463 | int disk; | |
3464 | struct disk_info *p; | |
3465 | ||
16a53ecc | 3466 | if (mddev->degraded > conf->max_degraded) |
1da177e4 LT |
3467 | /* no point adding a device */ |
3468 | return 0; | |
3469 | ||
3470 | /* | |
16a53ecc N |
3471 | * find the disk ... but prefer rdev->saved_raid_disk |
3472 | * if possible. | |
1da177e4 | 3473 | */ |
16a53ecc N |
3474 | if (rdev->saved_raid_disk >= 0 && |
3475 | conf->disks[rdev->saved_raid_disk].rdev == NULL) | |
3476 | disk = rdev->saved_raid_disk; | |
3477 | else | |
3478 | disk = 0; | |
3479 | for ( ; disk < conf->raid_disks; disk++) | |
1da177e4 | 3480 | if ((p=conf->disks + disk)->rdev == NULL) { |
b2d444d7 | 3481 | clear_bit(In_sync, &rdev->flags); |
1da177e4 LT |
3482 | rdev->raid_disk = disk; |
3483 | found = 1; | |
72626685 N |
3484 | if (rdev->saved_raid_disk != disk) |
3485 | conf->fullsync = 1; | |
d6065f7b | 3486 | rcu_assign_pointer(p->rdev, rdev); |
1da177e4 LT |
3487 | break; |
3488 | } | |
3489 | print_raid5_conf(conf); | |
3490 | return found; | |
3491 | } | |
3492 | ||
3493 | static int raid5_resize(mddev_t *mddev, sector_t sectors) | |
3494 | { | |
3495 | /* no resync is happening, and there is enough space | |
3496 | * on all devices, so we can resize. | |
3497 | * We need to make sure resync covers any new space. | |
3498 | * If the array is shrinking we should possibly wait until | |
3499 | * any io in the removed space completes, but it hardly seems | |
3500 | * worth it. | |
3501 | */ | |
16a53ecc N |
3502 | raid5_conf_t *conf = mddev_to_conf(mddev); |
3503 | ||
1da177e4 | 3504 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
16a53ecc | 3505 | mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1; |
1da177e4 LT |
3506 | set_capacity(mddev->gendisk, mddev->array_size << 1); |
3507 | mddev->changed = 1; | |
3508 | if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) { | |
3509 | mddev->recovery_cp = mddev->size << 1; | |
3510 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
3511 | } | |
3512 | mddev->size = sectors /2; | |
4b5c7ae8 | 3513 | mddev->resync_max_sectors = sectors; |
1da177e4 LT |
3514 | return 0; |
3515 | } | |
3516 | ||
29269553 | 3517 | #ifdef CONFIG_MD_RAID5_RESHAPE |
63c70c4f | 3518 | static int raid5_check_reshape(mddev_t *mddev) |
29269553 N |
3519 | { |
3520 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
3521 | int err; | |
29269553 | 3522 | |
63c70c4f N |
3523 | if (mddev->delta_disks < 0 || |
3524 | mddev->new_level != mddev->level) | |
3525 | return -EINVAL; /* Cannot shrink array or change level yet */ | |
3526 | if (mddev->delta_disks == 0) | |
29269553 N |
3527 | return 0; /* nothing to do */ |
3528 | ||
3529 | /* Can only proceed if there are plenty of stripe_heads. | |
3530 | * We need a minimum of one full stripe,, and for sensible progress | |
3531 | * it is best to have about 4 times that. | |
3532 | * If we require 4 times, then the default 256 4K stripe_heads will | |
3533 | * allow for chunk sizes up to 256K, which is probably OK. | |
3534 | * If the chunk size is greater, user-space should request more | |
3535 | * stripe_heads first. | |
3536 | */ | |
63c70c4f N |
3537 | if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes || |
3538 | (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) { | |
29269553 N |
3539 | printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n", |
3540 | (mddev->chunk_size / STRIPE_SIZE)*4); | |
3541 | return -ENOSPC; | |
3542 | } | |
3543 | ||
63c70c4f N |
3544 | err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
3545 | if (err) | |
3546 | return err; | |
3547 | ||
3548 | /* looks like we might be able to manage this */ | |
3549 | return 0; | |
3550 | } | |
3551 | ||
3552 | static int raid5_start_reshape(mddev_t *mddev) | |
3553 | { | |
3554 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
3555 | mdk_rdev_t *rdev; | |
3556 | struct list_head *rtmp; | |
3557 | int spares = 0; | |
3558 | int added_devices = 0; | |
3559 | ||
3560 | if (mddev->degraded || | |
3561 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) | |
3562 | return -EBUSY; | |
3563 | ||
29269553 N |
3564 | ITERATE_RDEV(mddev, rdev, rtmp) |
3565 | if (rdev->raid_disk < 0 && | |
3566 | !test_bit(Faulty, &rdev->flags)) | |
3567 | spares++; | |
63c70c4f N |
3568 | |
3569 | if (spares < mddev->delta_disks-1) | |
29269553 N |
3570 | /* Not enough devices even to make a degraded array |
3571 | * of that size | |
3572 | */ | |
3573 | return -EINVAL; | |
3574 | ||
f6705578 | 3575 | atomic_set(&conf->reshape_stripes, 0); |
29269553 N |
3576 | spin_lock_irq(&conf->device_lock); |
3577 | conf->previous_raid_disks = conf->raid_disks; | |
63c70c4f | 3578 | conf->raid_disks += mddev->delta_disks; |
29269553 | 3579 | conf->expand_progress = 0; |
b578d55f | 3580 | conf->expand_lo = 0; |
29269553 N |
3581 | spin_unlock_irq(&conf->device_lock); |
3582 | ||
3583 | /* Add some new drives, as many as will fit. | |
3584 | * We know there are enough to make the newly sized array work. | |
3585 | */ | |
3586 | ITERATE_RDEV(mddev, rdev, rtmp) | |
3587 | if (rdev->raid_disk < 0 && | |
3588 | !test_bit(Faulty, &rdev->flags)) { | |
3589 | if (raid5_add_disk(mddev, rdev)) { | |
3590 | char nm[20]; | |
3591 | set_bit(In_sync, &rdev->flags); | |
29269553 | 3592 | added_devices++; |
5fd6c1dc | 3593 | rdev->recovery_offset = 0; |
29269553 N |
3594 | sprintf(nm, "rd%d", rdev->raid_disk); |
3595 | sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); | |
3596 | } else | |
3597 | break; | |
3598 | } | |
3599 | ||
63c70c4f N |
3600 | mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices; |
3601 | mddev->raid_disks = conf->raid_disks; | |
f6705578 | 3602 | mddev->reshape_position = 0; |
850b2b42 | 3603 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
f6705578 | 3604 | |
29269553 N |
3605 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
3606 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
3607 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
3608 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
3609 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
3610 | "%s_reshape"); | |
3611 | if (!mddev->sync_thread) { | |
3612 | mddev->recovery = 0; | |
3613 | spin_lock_irq(&conf->device_lock); | |
3614 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; | |
3615 | conf->expand_progress = MaxSector; | |
3616 | spin_unlock_irq(&conf->device_lock); | |
3617 | return -EAGAIN; | |
3618 | } | |
3619 | md_wakeup_thread(mddev->sync_thread); | |
3620 | md_new_event(mddev); | |
3621 | return 0; | |
3622 | } | |
3623 | #endif | |
3624 | ||
3625 | static void end_reshape(raid5_conf_t *conf) | |
3626 | { | |
3627 | struct block_device *bdev; | |
3628 | ||
f6705578 N |
3629 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
3630 | conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1); | |
3631 | set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1); | |
3632 | conf->mddev->changed = 1; | |
3633 | ||
3634 | bdev = bdget_disk(conf->mddev->gendisk, 0); | |
3635 | if (bdev) { | |
3636 | mutex_lock(&bdev->bd_inode->i_mutex); | |
3637 | i_size_write(bdev->bd_inode, conf->mddev->array_size << 10); | |
3638 | mutex_unlock(&bdev->bd_inode->i_mutex); | |
3639 | bdput(bdev); | |
3640 | } | |
3641 | spin_lock_irq(&conf->device_lock); | |
3642 | conf->expand_progress = MaxSector; | |
3643 | spin_unlock_irq(&conf->device_lock); | |
3644 | conf->mddev->reshape_position = MaxSector; | |
16a53ecc N |
3645 | |
3646 | /* read-ahead size must cover two whole stripes, which is | |
3647 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices | |
3648 | */ | |
3649 | { | |
3650 | int data_disks = conf->previous_raid_disks - conf->max_degraded; | |
3651 | int stripe = data_disks * | |
3652 | (conf->mddev->chunk_size / PAGE_SIZE); | |
3653 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) | |
3654 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
3655 | } | |
29269553 | 3656 | } |
29269553 N |
3657 | } |
3658 | ||
72626685 N |
3659 | static void raid5_quiesce(mddev_t *mddev, int state) |
3660 | { | |
3661 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
3662 | ||
3663 | switch(state) { | |
e464eafd N |
3664 | case 2: /* resume for a suspend */ |
3665 | wake_up(&conf->wait_for_overlap); | |
3666 | break; | |
3667 | ||
72626685 N |
3668 | case 1: /* stop all writes */ |
3669 | spin_lock_irq(&conf->device_lock); | |
3670 | conf->quiesce = 1; | |
3671 | wait_event_lock_irq(conf->wait_for_stripe, | |
3672 | atomic_read(&conf->active_stripes) == 0, | |
3673 | conf->device_lock, /* nothing */); | |
3674 | spin_unlock_irq(&conf->device_lock); | |
3675 | break; | |
3676 | ||
3677 | case 0: /* re-enable writes */ | |
3678 | spin_lock_irq(&conf->device_lock); | |
3679 | conf->quiesce = 0; | |
3680 | wake_up(&conf->wait_for_stripe); | |
e464eafd | 3681 | wake_up(&conf->wait_for_overlap); |
72626685 N |
3682 | spin_unlock_irq(&conf->device_lock); |
3683 | break; | |
3684 | } | |
72626685 | 3685 | } |
b15c2e57 | 3686 | |
16a53ecc N |
3687 | static struct mdk_personality raid6_personality = |
3688 | { | |
3689 | .name = "raid6", | |
3690 | .level = 6, | |
3691 | .owner = THIS_MODULE, | |
3692 | .make_request = make_request, | |
3693 | .run = run, | |
3694 | .stop = stop, | |
3695 | .status = status, | |
3696 | .error_handler = error, | |
3697 | .hot_add_disk = raid5_add_disk, | |
3698 | .hot_remove_disk= raid5_remove_disk, | |
3699 | .spare_active = raid5_spare_active, | |
3700 | .sync_request = sync_request, | |
3701 | .resize = raid5_resize, | |
3702 | .quiesce = raid5_quiesce, | |
3703 | }; | |
2604b703 | 3704 | static struct mdk_personality raid5_personality = |
1da177e4 LT |
3705 | { |
3706 | .name = "raid5", | |
2604b703 | 3707 | .level = 5, |
1da177e4 LT |
3708 | .owner = THIS_MODULE, |
3709 | .make_request = make_request, | |
3710 | .run = run, | |
3711 | .stop = stop, | |
3712 | .status = status, | |
3713 | .error_handler = error, | |
3714 | .hot_add_disk = raid5_add_disk, | |
3715 | .hot_remove_disk= raid5_remove_disk, | |
3716 | .spare_active = raid5_spare_active, | |
3717 | .sync_request = sync_request, | |
3718 | .resize = raid5_resize, | |
29269553 | 3719 | #ifdef CONFIG_MD_RAID5_RESHAPE |
63c70c4f N |
3720 | .check_reshape = raid5_check_reshape, |
3721 | .start_reshape = raid5_start_reshape, | |
29269553 | 3722 | #endif |
72626685 | 3723 | .quiesce = raid5_quiesce, |
1da177e4 LT |
3724 | }; |
3725 | ||
2604b703 | 3726 | static struct mdk_personality raid4_personality = |
1da177e4 | 3727 | { |
2604b703 N |
3728 | .name = "raid4", |
3729 | .level = 4, | |
3730 | .owner = THIS_MODULE, | |
3731 | .make_request = make_request, | |
3732 | .run = run, | |
3733 | .stop = stop, | |
3734 | .status = status, | |
3735 | .error_handler = error, | |
3736 | .hot_add_disk = raid5_add_disk, | |
3737 | .hot_remove_disk= raid5_remove_disk, | |
3738 | .spare_active = raid5_spare_active, | |
3739 | .sync_request = sync_request, | |
3740 | .resize = raid5_resize, | |
3741 | .quiesce = raid5_quiesce, | |
3742 | }; | |
3743 | ||
3744 | static int __init raid5_init(void) | |
3745 | { | |
16a53ecc N |
3746 | int e; |
3747 | ||
3748 | e = raid6_select_algo(); | |
3749 | if ( e ) | |
3750 | return e; | |
3751 | register_md_personality(&raid6_personality); | |
2604b703 N |
3752 | register_md_personality(&raid5_personality); |
3753 | register_md_personality(&raid4_personality); | |
3754 | return 0; | |
1da177e4 LT |
3755 | } |
3756 | ||
2604b703 | 3757 | static void raid5_exit(void) |
1da177e4 | 3758 | { |
16a53ecc | 3759 | unregister_md_personality(&raid6_personality); |
2604b703 N |
3760 | unregister_md_personality(&raid5_personality); |
3761 | unregister_md_personality(&raid4_personality); | |
1da177e4 LT |
3762 | } |
3763 | ||
3764 | module_init(raid5_init); | |
3765 | module_exit(raid5_exit); | |
3766 | MODULE_LICENSE("GPL"); | |
3767 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ | |
d9d166c2 N |
3768 | MODULE_ALIAS("md-raid5"); |
3769 | MODULE_ALIAS("md-raid4"); | |
2604b703 N |
3770 | MODULE_ALIAS("md-level-5"); |
3771 | MODULE_ALIAS("md-level-4"); | |
16a53ecc N |
3772 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
3773 | MODULE_ALIAS("md-raid6"); | |
3774 | MODULE_ALIAS("md-level-6"); | |
3775 | ||
3776 | /* This used to be two separate modules, they were: */ | |
3777 | MODULE_ALIAS("raid5"); | |
3778 | MODULE_ALIAS("raid6"); |