]>
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 | |
5 | * | |
6 | * RAID-5 management functions. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * (for example /usr/src/linux/COPYING); if not, write to the Free | |
15 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | */ | |
17 | ||
18 | ||
19 | #include <linux/config.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/raid/raid5.h> | |
23 | #include <linux/highmem.h> | |
24 | #include <linux/bitops.h> | |
f6705578 | 25 | #include <linux/kthread.h> |
1da177e4 LT |
26 | #include <asm/atomic.h> |
27 | ||
72626685 N |
28 | #include <linux/raid/bitmap.h> |
29 | ||
1da177e4 LT |
30 | /* |
31 | * Stripe cache | |
32 | */ | |
33 | ||
34 | #define NR_STRIPES 256 | |
35 | #define STRIPE_SIZE PAGE_SIZE | |
36 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) | |
37 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) | |
38 | #define IO_THRESHOLD 1 | |
fccddba0 | 39 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
1da177e4 LT |
40 | #define HASH_MASK (NR_HASH - 1) |
41 | ||
fccddba0 | 42 | #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])) |
1da177e4 LT |
43 | |
44 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector | |
45 | * order without overlap. There may be several bio's per stripe+device, and | |
46 | * a bio could span several devices. | |
47 | * When walking this list for a particular stripe+device, we must never proceed | |
48 | * beyond a bio that extends past this device, as the next bio might no longer | |
49 | * be valid. | |
50 | * This macro is used to determine the 'next' bio in the list, given the sector | |
51 | * of the current stripe+device | |
52 | */ | |
53 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) | |
54 | /* | |
55 | * The following can be used to debug the driver | |
56 | */ | |
57 | #define RAID5_DEBUG 0 | |
58 | #define RAID5_PARANOIA 1 | |
59 | #if RAID5_PARANOIA && defined(CONFIG_SMP) | |
60 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) | |
61 | #else | |
62 | # define CHECK_DEVLOCK() | |
63 | #endif | |
64 | ||
65 | #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x))) | |
66 | #if RAID5_DEBUG | |
67 | #define inline | |
68 | #define __inline__ | |
69 | #endif | |
70 | ||
71 | static void print_raid5_conf (raid5_conf_t *conf); | |
72 | ||
858119e1 | 73 | static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
1da177e4 LT |
74 | { |
75 | if (atomic_dec_and_test(&sh->count)) { | |
78bafebd ES |
76 | BUG_ON(!list_empty(&sh->lru)); |
77 | BUG_ON(atomic_read(&conf->active_stripes)==0); | |
1da177e4 LT |
78 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
79 | if (test_bit(STRIPE_DELAYED, &sh->state)) | |
80 | list_add_tail(&sh->lru, &conf->delayed_list); | |
72626685 N |
81 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
82 | conf->seq_write == sh->bm_seq) | |
83 | list_add_tail(&sh->lru, &conf->bitmap_list); | |
84 | else { | |
85 | clear_bit(STRIPE_BIT_DELAY, &sh->state); | |
1da177e4 | 86 | list_add_tail(&sh->lru, &conf->handle_list); |
72626685 | 87 | } |
1da177e4 LT |
88 | md_wakeup_thread(conf->mddev->thread); |
89 | } else { | |
90 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
91 | atomic_dec(&conf->preread_active_stripes); | |
92 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) | |
93 | md_wakeup_thread(conf->mddev->thread); | |
94 | } | |
1da177e4 | 95 | atomic_dec(&conf->active_stripes); |
ccfcc3c1 N |
96 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
97 | list_add_tail(&sh->lru, &conf->inactive_list); | |
1da177e4 | 98 | wake_up(&conf->wait_for_stripe); |
ccfcc3c1 | 99 | } |
1da177e4 LT |
100 | } |
101 | } | |
102 | } | |
103 | static void release_stripe(struct stripe_head *sh) | |
104 | { | |
105 | raid5_conf_t *conf = sh->raid_conf; | |
106 | unsigned long flags; | |
107 | ||
108 | spin_lock_irqsave(&conf->device_lock, flags); | |
109 | __release_stripe(conf, sh); | |
110 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
111 | } | |
112 | ||
fccddba0 | 113 | static inline void remove_hash(struct stripe_head *sh) |
1da177e4 LT |
114 | { |
115 | PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector); | |
116 | ||
fccddba0 | 117 | hlist_del_init(&sh->hash); |
1da177e4 LT |
118 | } |
119 | ||
858119e1 | 120 | static void insert_hash(raid5_conf_t *conf, struct stripe_head *sh) |
1da177e4 | 121 | { |
fccddba0 | 122 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
1da177e4 LT |
123 | |
124 | PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector); | |
125 | ||
126 | CHECK_DEVLOCK(); | |
fccddba0 | 127 | hlist_add_head(&sh->hash, hp); |
1da177e4 LT |
128 | } |
129 | ||
130 | ||
131 | /* find an idle stripe, make sure it is unhashed, and return it. */ | |
132 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) | |
133 | { | |
134 | struct stripe_head *sh = NULL; | |
135 | struct list_head *first; | |
136 | ||
137 | CHECK_DEVLOCK(); | |
138 | if (list_empty(&conf->inactive_list)) | |
139 | goto out; | |
140 | first = conf->inactive_list.next; | |
141 | sh = list_entry(first, struct stripe_head, lru); | |
142 | list_del_init(first); | |
143 | remove_hash(sh); | |
144 | atomic_inc(&conf->active_stripes); | |
145 | out: | |
146 | return sh; | |
147 | } | |
148 | ||
149 | static void shrink_buffers(struct stripe_head *sh, int num) | |
150 | { | |
151 | struct page *p; | |
152 | int i; | |
153 | ||
154 | for (i=0; i<num ; i++) { | |
155 | p = sh->dev[i].page; | |
156 | if (!p) | |
157 | continue; | |
158 | sh->dev[i].page = NULL; | |
2d1f3b5d | 159 | put_page(p); |
1da177e4 LT |
160 | } |
161 | } | |
162 | ||
163 | static int grow_buffers(struct stripe_head *sh, int num) | |
164 | { | |
165 | int i; | |
166 | ||
167 | for (i=0; i<num; i++) { | |
168 | struct page *page; | |
169 | ||
170 | if (!(page = alloc_page(GFP_KERNEL))) { | |
171 | return 1; | |
172 | } | |
173 | sh->dev[i].page = page; | |
174 | } | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static void raid5_build_block (struct stripe_head *sh, int i); | |
179 | ||
7ecaa1e6 | 180 | static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks) |
1da177e4 LT |
181 | { |
182 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 183 | int i; |
1da177e4 | 184 | |
78bafebd ES |
185 | BUG_ON(atomic_read(&sh->count) != 0); |
186 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); | |
1da177e4 LT |
187 | |
188 | CHECK_DEVLOCK(); | |
189 | PRINTK("init_stripe called, stripe %llu\n", | |
190 | (unsigned long long)sh->sector); | |
191 | ||
192 | remove_hash(sh); | |
193 | ||
194 | sh->sector = sector; | |
195 | sh->pd_idx = pd_idx; | |
196 | sh->state = 0; | |
197 | ||
7ecaa1e6 N |
198 | sh->disks = disks; |
199 | ||
200 | for (i = sh->disks; i--; ) { | |
1da177e4 LT |
201 | struct r5dev *dev = &sh->dev[i]; |
202 | ||
203 | if (dev->toread || dev->towrite || dev->written || | |
204 | test_bit(R5_LOCKED, &dev->flags)) { | |
205 | printk("sector=%llx i=%d %p %p %p %d\n", | |
206 | (unsigned long long)sh->sector, i, dev->toread, | |
207 | dev->towrite, dev->written, | |
208 | test_bit(R5_LOCKED, &dev->flags)); | |
209 | BUG(); | |
210 | } | |
211 | dev->flags = 0; | |
212 | raid5_build_block(sh, i); | |
213 | } | |
214 | insert_hash(conf, sh); | |
215 | } | |
216 | ||
7ecaa1e6 | 217 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks) |
1da177e4 LT |
218 | { |
219 | struct stripe_head *sh; | |
fccddba0 | 220 | struct hlist_node *hn; |
1da177e4 LT |
221 | |
222 | CHECK_DEVLOCK(); | |
223 | PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector); | |
fccddba0 | 224 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
7ecaa1e6 | 225 | if (sh->sector == sector && sh->disks == disks) |
1da177e4 LT |
226 | return sh; |
227 | PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector); | |
228 | return NULL; | |
229 | } | |
230 | ||
231 | static void unplug_slaves(mddev_t *mddev); | |
232 | static void raid5_unplug_device(request_queue_t *q); | |
233 | ||
7ecaa1e6 N |
234 | static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks, |
235 | int pd_idx, int noblock) | |
1da177e4 LT |
236 | { |
237 | struct stripe_head *sh; | |
238 | ||
239 | PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector); | |
240 | ||
241 | spin_lock_irq(&conf->device_lock); | |
242 | ||
243 | do { | |
72626685 N |
244 | wait_event_lock_irq(conf->wait_for_stripe, |
245 | conf->quiesce == 0, | |
246 | conf->device_lock, /* nothing */); | |
7ecaa1e6 | 247 | sh = __find_stripe(conf, sector, disks); |
1da177e4 LT |
248 | if (!sh) { |
249 | if (!conf->inactive_blocked) | |
250 | sh = get_free_stripe(conf); | |
251 | if (noblock && sh == NULL) | |
252 | break; | |
253 | if (!sh) { | |
254 | conf->inactive_blocked = 1; | |
255 | wait_event_lock_irq(conf->wait_for_stripe, | |
256 | !list_empty(&conf->inactive_list) && | |
5036805b N |
257 | (atomic_read(&conf->active_stripes) |
258 | < (conf->max_nr_stripes *3/4) | |
1da177e4 LT |
259 | || !conf->inactive_blocked), |
260 | conf->device_lock, | |
b3b46be3 | 261 | unplug_slaves(conf->mddev) |
1da177e4 LT |
262 | ); |
263 | conf->inactive_blocked = 0; | |
264 | } else | |
7ecaa1e6 | 265 | init_stripe(sh, sector, pd_idx, disks); |
1da177e4 LT |
266 | } else { |
267 | if (atomic_read(&sh->count)) { | |
78bafebd | 268 | BUG_ON(!list_empty(&sh->lru)); |
1da177e4 LT |
269 | } else { |
270 | if (!test_bit(STRIPE_HANDLE, &sh->state)) | |
271 | atomic_inc(&conf->active_stripes); | |
ccfcc3c1 N |
272 | if (!list_empty(&sh->lru)) |
273 | list_del_init(&sh->lru); | |
1da177e4 LT |
274 | } |
275 | } | |
276 | } while (sh == NULL); | |
277 | ||
278 | if (sh) | |
279 | atomic_inc(&sh->count); | |
280 | ||
281 | spin_unlock_irq(&conf->device_lock); | |
282 | return sh; | |
283 | } | |
284 | ||
3f294f4f | 285 | static int grow_one_stripe(raid5_conf_t *conf) |
1da177e4 LT |
286 | { |
287 | struct stripe_head *sh; | |
3f294f4f N |
288 | sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL); |
289 | if (!sh) | |
290 | return 0; | |
291 | memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev)); | |
292 | sh->raid_conf = conf; | |
293 | spin_lock_init(&sh->lock); | |
294 | ||
295 | if (grow_buffers(sh, conf->raid_disks)) { | |
296 | shrink_buffers(sh, conf->raid_disks); | |
297 | kmem_cache_free(conf->slab_cache, sh); | |
298 | return 0; | |
299 | } | |
7ecaa1e6 | 300 | sh->disks = conf->raid_disks; |
3f294f4f N |
301 | /* we just created an active stripe so... */ |
302 | atomic_set(&sh->count, 1); | |
303 | atomic_inc(&conf->active_stripes); | |
304 | INIT_LIST_HEAD(&sh->lru); | |
305 | release_stripe(sh); | |
306 | return 1; | |
307 | } | |
308 | ||
309 | static int grow_stripes(raid5_conf_t *conf, int num) | |
310 | { | |
1da177e4 LT |
311 | kmem_cache_t *sc; |
312 | int devs = conf->raid_disks; | |
313 | ||
ad01c9e3 N |
314 | sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev)); |
315 | sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev)); | |
316 | conf->active_name = 0; | |
317 | sc = kmem_cache_create(conf->cache_name[conf->active_name], | |
1da177e4 LT |
318 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
319 | 0, 0, NULL, NULL); | |
320 | if (!sc) | |
321 | return 1; | |
322 | conf->slab_cache = sc; | |
ad01c9e3 | 323 | conf->pool_size = devs; |
1da177e4 | 324 | while (num--) { |
3f294f4f | 325 | if (!grow_one_stripe(conf)) |
1da177e4 | 326 | return 1; |
1da177e4 LT |
327 | } |
328 | return 0; | |
329 | } | |
29269553 N |
330 | |
331 | #ifdef CONFIG_MD_RAID5_RESHAPE | |
ad01c9e3 N |
332 | static int resize_stripes(raid5_conf_t *conf, int newsize) |
333 | { | |
334 | /* Make all the stripes able to hold 'newsize' devices. | |
335 | * New slots in each stripe get 'page' set to a new page. | |
336 | * | |
337 | * This happens in stages: | |
338 | * 1/ create a new kmem_cache and allocate the required number of | |
339 | * stripe_heads. | |
340 | * 2/ gather all the old stripe_heads and tranfer the pages across | |
341 | * to the new stripe_heads. This will have the side effect of | |
342 | * freezing the array as once all stripe_heads have been collected, | |
343 | * no IO will be possible. Old stripe heads are freed once their | |
344 | * pages have been transferred over, and the old kmem_cache is | |
345 | * freed when all stripes are done. | |
346 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, | |
347 | * we simple return a failre status - no need to clean anything up. | |
348 | * 4/ allocate new pages for the new slots in the new stripe_heads. | |
349 | * If this fails, we don't bother trying the shrink the | |
350 | * stripe_heads down again, we just leave them as they are. | |
351 | * As each stripe_head is processed the new one is released into | |
352 | * active service. | |
353 | * | |
354 | * Once step2 is started, we cannot afford to wait for a write, | |
355 | * so we use GFP_NOIO allocations. | |
356 | */ | |
357 | struct stripe_head *osh, *nsh; | |
358 | LIST_HEAD(newstripes); | |
359 | struct disk_info *ndisks; | |
360 | int err = 0; | |
361 | kmem_cache_t *sc; | |
362 | int i; | |
363 | ||
364 | if (newsize <= conf->pool_size) | |
365 | return 0; /* never bother to shrink */ | |
366 | ||
367 | /* Step 1 */ | |
368 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], | |
369 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), | |
370 | 0, 0, NULL, NULL); | |
371 | if (!sc) | |
372 | return -ENOMEM; | |
373 | ||
374 | for (i = conf->max_nr_stripes; i; i--) { | |
375 | nsh = kmem_cache_alloc(sc, GFP_KERNEL); | |
376 | if (!nsh) | |
377 | break; | |
378 | ||
379 | memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev)); | |
380 | ||
381 | nsh->raid_conf = conf; | |
382 | spin_lock_init(&nsh->lock); | |
383 | ||
384 | list_add(&nsh->lru, &newstripes); | |
385 | } | |
386 | if (i) { | |
387 | /* didn't get enough, give up */ | |
388 | while (!list_empty(&newstripes)) { | |
389 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
390 | list_del(&nsh->lru); | |
391 | kmem_cache_free(sc, nsh); | |
392 | } | |
393 | kmem_cache_destroy(sc); | |
394 | return -ENOMEM; | |
395 | } | |
396 | /* Step 2 - Must use GFP_NOIO now. | |
397 | * OK, we have enough stripes, start collecting inactive | |
398 | * stripes and copying them over | |
399 | */ | |
400 | list_for_each_entry(nsh, &newstripes, lru) { | |
401 | spin_lock_irq(&conf->device_lock); | |
402 | wait_event_lock_irq(conf->wait_for_stripe, | |
403 | !list_empty(&conf->inactive_list), | |
404 | conf->device_lock, | |
b3b46be3 | 405 | unplug_slaves(conf->mddev) |
ad01c9e3 N |
406 | ); |
407 | osh = get_free_stripe(conf); | |
408 | spin_unlock_irq(&conf->device_lock); | |
409 | atomic_set(&nsh->count, 1); | |
410 | for(i=0; i<conf->pool_size; i++) | |
411 | nsh->dev[i].page = osh->dev[i].page; | |
412 | for( ; i<newsize; i++) | |
413 | nsh->dev[i].page = NULL; | |
414 | kmem_cache_free(conf->slab_cache, osh); | |
415 | } | |
416 | kmem_cache_destroy(conf->slab_cache); | |
417 | ||
418 | /* Step 3. | |
419 | * At this point, we are holding all the stripes so the array | |
420 | * is completely stalled, so now is a good time to resize | |
421 | * conf->disks. | |
422 | */ | |
423 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); | |
424 | if (ndisks) { | |
425 | for (i=0; i<conf->raid_disks; i++) | |
426 | ndisks[i] = conf->disks[i]; | |
427 | kfree(conf->disks); | |
428 | conf->disks = ndisks; | |
429 | } else | |
430 | err = -ENOMEM; | |
431 | ||
432 | /* Step 4, return new stripes to service */ | |
433 | while(!list_empty(&newstripes)) { | |
434 | nsh = list_entry(newstripes.next, struct stripe_head, lru); | |
435 | list_del_init(&nsh->lru); | |
436 | for (i=conf->raid_disks; i < newsize; i++) | |
437 | if (nsh->dev[i].page == NULL) { | |
438 | struct page *p = alloc_page(GFP_NOIO); | |
439 | nsh->dev[i].page = p; | |
440 | if (!p) | |
441 | err = -ENOMEM; | |
442 | } | |
443 | release_stripe(nsh); | |
444 | } | |
445 | /* critical section pass, GFP_NOIO no longer needed */ | |
446 | ||
447 | conf->slab_cache = sc; | |
448 | conf->active_name = 1-conf->active_name; | |
449 | conf->pool_size = newsize; | |
450 | return err; | |
451 | } | |
29269553 | 452 | #endif |
1da177e4 | 453 | |
3f294f4f | 454 | static int drop_one_stripe(raid5_conf_t *conf) |
1da177e4 LT |
455 | { |
456 | struct stripe_head *sh; | |
457 | ||
3f294f4f N |
458 | spin_lock_irq(&conf->device_lock); |
459 | sh = get_free_stripe(conf); | |
460 | spin_unlock_irq(&conf->device_lock); | |
461 | if (!sh) | |
462 | return 0; | |
78bafebd | 463 | BUG_ON(atomic_read(&sh->count)); |
ad01c9e3 | 464 | shrink_buffers(sh, conf->pool_size); |
3f294f4f N |
465 | kmem_cache_free(conf->slab_cache, sh); |
466 | atomic_dec(&conf->active_stripes); | |
467 | return 1; | |
468 | } | |
469 | ||
470 | static void shrink_stripes(raid5_conf_t *conf) | |
471 | { | |
472 | while (drop_one_stripe(conf)) | |
473 | ; | |
474 | ||
29fc7e3e N |
475 | if (conf->slab_cache) |
476 | kmem_cache_destroy(conf->slab_cache); | |
1da177e4 LT |
477 | conf->slab_cache = NULL; |
478 | } | |
479 | ||
4e5314b5 | 480 | static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done, |
1da177e4 LT |
481 | int error) |
482 | { | |
483 | struct stripe_head *sh = bi->bi_private; | |
484 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 485 | int disks = sh->disks, i; |
1da177e4 LT |
486 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
487 | ||
488 | if (bi->bi_size) | |
489 | return 1; | |
490 | ||
491 | for (i=0 ; i<disks; i++) | |
492 | if (bi == &sh->dev[i].req) | |
493 | break; | |
494 | ||
495 | PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n", | |
496 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), | |
497 | uptodate); | |
498 | if (i == disks) { | |
499 | BUG(); | |
500 | return 0; | |
501 | } | |
502 | ||
503 | if (uptodate) { | |
504 | #if 0 | |
505 | struct bio *bio; | |
506 | unsigned long flags; | |
507 | spin_lock_irqsave(&conf->device_lock, flags); | |
508 | /* we can return a buffer if we bypassed the cache or | |
509 | * if the top buffer is not in highmem. If there are | |
510 | * multiple buffers, leave the extra work to | |
511 | * handle_stripe | |
512 | */ | |
513 | buffer = sh->bh_read[i]; | |
514 | if (buffer && | |
515 | (!PageHighMem(buffer->b_page) | |
516 | || buffer->b_page == bh->b_page ) | |
517 | ) { | |
518 | sh->bh_read[i] = buffer->b_reqnext; | |
519 | buffer->b_reqnext = NULL; | |
520 | } else | |
521 | buffer = NULL; | |
522 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
523 | if (sh->bh_page[i]==bh->b_page) | |
524 | set_buffer_uptodate(bh); | |
525 | if (buffer) { | |
526 | if (buffer->b_page != bh->b_page) | |
527 | memcpy(buffer->b_data, bh->b_data, bh->b_size); | |
528 | buffer->b_end_io(buffer, 1); | |
529 | } | |
530 | #else | |
531 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
4e5314b5 N |
532 | #endif |
533 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
14f8d26b | 534 | printk(KERN_INFO "raid5: read error corrected!!\n"); |
4e5314b5 N |
535 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
536 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
537 | } | |
ba22dcbf N |
538 | if (atomic_read(&conf->disks[i].rdev->read_errors)) |
539 | atomic_set(&conf->disks[i].rdev->read_errors, 0); | |
1da177e4 | 540 | } else { |
ba22dcbf | 541 | int retry = 0; |
1da177e4 | 542 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
ba22dcbf N |
543 | atomic_inc(&conf->disks[i].rdev->read_errors); |
544 | if (conf->mddev->degraded) | |
14f8d26b | 545 | printk(KERN_WARNING "raid5: read error not correctable.\n"); |
ba22dcbf | 546 | else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) |
4e5314b5 | 547 | /* Oh, no!!! */ |
14f8d26b | 548 | printk(KERN_WARNING "raid5: read error NOT corrected!!\n"); |
ba22dcbf N |
549 | else if (atomic_read(&conf->disks[i].rdev->read_errors) |
550 | > conf->max_nr_stripes) | |
14f8d26b N |
551 | printk(KERN_WARNING |
552 | "raid5: Too many read errors, failing device.\n"); | |
ba22dcbf N |
553 | else |
554 | retry = 1; | |
555 | if (retry) | |
556 | set_bit(R5_ReadError, &sh->dev[i].flags); | |
557 | else { | |
4e5314b5 N |
558 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
559 | clear_bit(R5_ReWrite, &sh->dev[i].flags); | |
560 | md_error(conf->mddev, conf->disks[i].rdev); | |
ba22dcbf | 561 | } |
1da177e4 LT |
562 | } |
563 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); | |
564 | #if 0 | |
565 | /* must restore b_page before unlocking buffer... */ | |
566 | if (sh->bh_page[i] != bh->b_page) { | |
567 | bh->b_page = sh->bh_page[i]; | |
568 | bh->b_data = page_address(bh->b_page); | |
569 | clear_buffer_uptodate(bh); | |
570 | } | |
571 | #endif | |
572 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
573 | set_bit(STRIPE_HANDLE, &sh->state); | |
574 | release_stripe(sh); | |
575 | return 0; | |
576 | } | |
577 | ||
578 | static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done, | |
579 | int error) | |
580 | { | |
581 | struct stripe_head *sh = bi->bi_private; | |
582 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 583 | int disks = sh->disks, i; |
1da177e4 LT |
584 | unsigned long flags; |
585 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); | |
586 | ||
587 | if (bi->bi_size) | |
588 | return 1; | |
589 | ||
590 | for (i=0 ; i<disks; i++) | |
591 | if (bi == &sh->dev[i].req) | |
592 | break; | |
593 | ||
594 | PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n", | |
595 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), | |
596 | uptodate); | |
597 | if (i == disks) { | |
598 | BUG(); | |
599 | return 0; | |
600 | } | |
601 | ||
602 | spin_lock_irqsave(&conf->device_lock, flags); | |
603 | if (!uptodate) | |
604 | md_error(conf->mddev, conf->disks[i].rdev); | |
605 | ||
606 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); | |
607 | ||
608 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
609 | set_bit(STRIPE_HANDLE, &sh->state); | |
610 | __release_stripe(conf, sh); | |
611 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
612 | return 0; | |
613 | } | |
614 | ||
615 | ||
616 | static sector_t compute_blocknr(struct stripe_head *sh, int i); | |
617 | ||
618 | static void raid5_build_block (struct stripe_head *sh, int i) | |
619 | { | |
620 | struct r5dev *dev = &sh->dev[i]; | |
621 | ||
622 | bio_init(&dev->req); | |
623 | dev->req.bi_io_vec = &dev->vec; | |
624 | dev->req.bi_vcnt++; | |
625 | dev->req.bi_max_vecs++; | |
626 | dev->vec.bv_page = dev->page; | |
627 | dev->vec.bv_len = STRIPE_SIZE; | |
628 | dev->vec.bv_offset = 0; | |
629 | ||
630 | dev->req.bi_sector = sh->sector; | |
631 | dev->req.bi_private = sh; | |
632 | ||
633 | dev->flags = 0; | |
634 | if (i != sh->pd_idx) | |
635 | dev->sector = compute_blocknr(sh, i); | |
636 | } | |
637 | ||
638 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) | |
639 | { | |
640 | char b[BDEVNAME_SIZE]; | |
641 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
642 | PRINTK("raid5: error called\n"); | |
643 | ||
b2d444d7 | 644 | if (!test_bit(Faulty, &rdev->flags)) { |
1da177e4 | 645 | mddev->sb_dirty = 1; |
b2d444d7 | 646 | if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
647 | conf->working_disks--; |
648 | mddev->degraded++; | |
649 | conf->failed_disks++; | |
b2d444d7 | 650 | clear_bit(In_sync, &rdev->flags); |
1da177e4 LT |
651 | /* |
652 | * if recovery was running, make sure it aborts. | |
653 | */ | |
654 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); | |
655 | } | |
b2d444d7 | 656 | set_bit(Faulty, &rdev->flags); |
1da177e4 LT |
657 | printk (KERN_ALERT |
658 | "raid5: Disk failure on %s, disabling device." | |
659 | " Operation continuing on %d devices\n", | |
660 | bdevname(rdev->bdev,b), conf->working_disks); | |
661 | } | |
662 | } | |
663 | ||
664 | /* | |
665 | * Input: a 'big' sector number, | |
666 | * Output: index of the data and parity disk, and the sector # in them. | |
667 | */ | |
668 | static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks, | |
669 | unsigned int data_disks, unsigned int * dd_idx, | |
670 | unsigned int * pd_idx, raid5_conf_t *conf) | |
671 | { | |
672 | long stripe; | |
673 | unsigned long chunk_number; | |
674 | unsigned int chunk_offset; | |
675 | sector_t new_sector; | |
676 | int sectors_per_chunk = conf->chunk_size >> 9; | |
677 | ||
678 | /* First compute the information on this sector */ | |
679 | ||
680 | /* | |
681 | * Compute the chunk number and the sector offset inside the chunk | |
682 | */ | |
683 | chunk_offset = sector_div(r_sector, sectors_per_chunk); | |
684 | chunk_number = r_sector; | |
685 | BUG_ON(r_sector != chunk_number); | |
686 | ||
687 | /* | |
688 | * Compute the stripe number | |
689 | */ | |
690 | stripe = chunk_number / data_disks; | |
691 | ||
692 | /* | |
693 | * Compute the data disk and parity disk indexes inside the stripe | |
694 | */ | |
695 | *dd_idx = chunk_number % data_disks; | |
696 | ||
697 | /* | |
698 | * Select the parity disk based on the user selected algorithm. | |
699 | */ | |
700 | if (conf->level == 4) | |
701 | *pd_idx = data_disks; | |
702 | else switch (conf->algorithm) { | |
703 | case ALGORITHM_LEFT_ASYMMETRIC: | |
704 | *pd_idx = data_disks - stripe % raid_disks; | |
705 | if (*dd_idx >= *pd_idx) | |
706 | (*dd_idx)++; | |
707 | break; | |
708 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
709 | *pd_idx = stripe % raid_disks; | |
710 | if (*dd_idx >= *pd_idx) | |
711 | (*dd_idx)++; | |
712 | break; | |
713 | case ALGORITHM_LEFT_SYMMETRIC: | |
714 | *pd_idx = data_disks - stripe % raid_disks; | |
715 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; | |
716 | break; | |
717 | case ALGORITHM_RIGHT_SYMMETRIC: | |
718 | *pd_idx = stripe % raid_disks; | |
719 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; | |
720 | break; | |
721 | default: | |
14f8d26b | 722 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
1da177e4 LT |
723 | conf->algorithm); |
724 | } | |
725 | ||
726 | /* | |
727 | * Finally, compute the new sector number | |
728 | */ | |
729 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; | |
730 | return new_sector; | |
731 | } | |
732 | ||
733 | ||
734 | static sector_t compute_blocknr(struct stripe_head *sh, int i) | |
735 | { | |
736 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 737 | int raid_disks = sh->disks, data_disks = raid_disks - 1; |
1da177e4 LT |
738 | sector_t new_sector = sh->sector, check; |
739 | int sectors_per_chunk = conf->chunk_size >> 9; | |
740 | sector_t stripe; | |
741 | int chunk_offset; | |
742 | int chunk_number, dummy1, dummy2, dd_idx = i; | |
743 | sector_t r_sector; | |
744 | ||
745 | chunk_offset = sector_div(new_sector, sectors_per_chunk); | |
746 | stripe = new_sector; | |
747 | BUG_ON(new_sector != stripe); | |
748 | ||
749 | ||
750 | switch (conf->algorithm) { | |
751 | case ALGORITHM_LEFT_ASYMMETRIC: | |
752 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
753 | if (i > sh->pd_idx) | |
754 | i--; | |
755 | break; | |
756 | case ALGORITHM_LEFT_SYMMETRIC: | |
757 | case ALGORITHM_RIGHT_SYMMETRIC: | |
758 | if (i < sh->pd_idx) | |
759 | i += raid_disks; | |
760 | i -= (sh->pd_idx + 1); | |
761 | break; | |
762 | default: | |
14f8d26b | 763 | printk(KERN_ERR "raid5: unsupported algorithm %d\n", |
1da177e4 LT |
764 | conf->algorithm); |
765 | } | |
766 | ||
767 | chunk_number = stripe * data_disks + i; | |
768 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; | |
769 | ||
770 | check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf); | |
771 | if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) { | |
14f8d26b | 772 | printk(KERN_ERR "compute_blocknr: map not correct\n"); |
1da177e4 LT |
773 | return 0; |
774 | } | |
775 | return r_sector; | |
776 | } | |
777 | ||
778 | ||
779 | ||
780 | /* | |
781 | * Copy data between a page in the stripe cache, and a bio. | |
782 | * There are no alignment or size guarantees between the page or the | |
783 | * bio except that there is some overlap. | |
784 | * All iovecs in the bio must be considered. | |
785 | */ | |
786 | static void copy_data(int frombio, struct bio *bio, | |
787 | struct page *page, | |
788 | sector_t sector) | |
789 | { | |
790 | char *pa = page_address(page); | |
791 | struct bio_vec *bvl; | |
792 | int i; | |
793 | int page_offset; | |
794 | ||
795 | if (bio->bi_sector >= sector) | |
796 | page_offset = (signed)(bio->bi_sector - sector) * 512; | |
797 | else | |
798 | page_offset = (signed)(sector - bio->bi_sector) * -512; | |
799 | bio_for_each_segment(bvl, bio, i) { | |
800 | int len = bio_iovec_idx(bio,i)->bv_len; | |
801 | int clen; | |
802 | int b_offset = 0; | |
803 | ||
804 | if (page_offset < 0) { | |
805 | b_offset = -page_offset; | |
806 | page_offset += b_offset; | |
807 | len -= b_offset; | |
808 | } | |
809 | ||
810 | if (len > 0 && page_offset + len > STRIPE_SIZE) | |
811 | clen = STRIPE_SIZE - page_offset; | |
812 | else clen = len; | |
813 | ||
814 | if (clen > 0) { | |
815 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); | |
816 | if (frombio) | |
817 | memcpy(pa+page_offset, ba+b_offset, clen); | |
818 | else | |
819 | memcpy(ba+b_offset, pa+page_offset, clen); | |
820 | __bio_kunmap_atomic(ba, KM_USER0); | |
821 | } | |
822 | if (clen < len) /* hit end of page */ | |
823 | break; | |
824 | page_offset += len; | |
825 | } | |
826 | } | |
827 | ||
828 | #define check_xor() do { \ | |
829 | if (count == MAX_XOR_BLOCKS) { \ | |
830 | xor_block(count, STRIPE_SIZE, ptr); \ | |
831 | count = 1; \ | |
832 | } \ | |
833 | } while(0) | |
834 | ||
835 | ||
836 | static void compute_block(struct stripe_head *sh, int dd_idx) | |
837 | { | |
7ecaa1e6 | 838 | int i, count, disks = sh->disks; |
1da177e4 LT |
839 | void *ptr[MAX_XOR_BLOCKS], *p; |
840 | ||
841 | PRINTK("compute_block, stripe %llu, idx %d\n", | |
842 | (unsigned long long)sh->sector, dd_idx); | |
843 | ||
844 | ptr[0] = page_address(sh->dev[dd_idx].page); | |
845 | memset(ptr[0], 0, STRIPE_SIZE); | |
846 | count = 1; | |
847 | for (i = disks ; i--; ) { | |
848 | if (i == dd_idx) | |
849 | continue; | |
850 | p = page_address(sh->dev[i].page); | |
851 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) | |
852 | ptr[count++] = p; | |
853 | else | |
14f8d26b | 854 | printk(KERN_ERR "compute_block() %d, stripe %llu, %d" |
1da177e4 LT |
855 | " not present\n", dd_idx, |
856 | (unsigned long long)sh->sector, i); | |
857 | ||
858 | check_xor(); | |
859 | } | |
860 | if (count != 1) | |
861 | xor_block(count, STRIPE_SIZE, ptr); | |
862 | set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); | |
863 | } | |
864 | ||
865 | static void compute_parity(struct stripe_head *sh, int method) | |
866 | { | |
867 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 868 | int i, pd_idx = sh->pd_idx, disks = sh->disks, count; |
1da177e4 LT |
869 | void *ptr[MAX_XOR_BLOCKS]; |
870 | struct bio *chosen; | |
871 | ||
872 | PRINTK("compute_parity, stripe %llu, method %d\n", | |
873 | (unsigned long long)sh->sector, method); | |
874 | ||
875 | count = 1; | |
876 | ptr[0] = page_address(sh->dev[pd_idx].page); | |
877 | switch(method) { | |
878 | case READ_MODIFY_WRITE: | |
78bafebd | 879 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags)); |
1da177e4 LT |
880 | for (i=disks ; i-- ;) { |
881 | if (i==pd_idx) | |
882 | continue; | |
883 | if (sh->dev[i].towrite && | |
884 | test_bit(R5_UPTODATE, &sh->dev[i].flags)) { | |
885 | ptr[count++] = page_address(sh->dev[i].page); | |
886 | chosen = sh->dev[i].towrite; | |
887 | sh->dev[i].towrite = NULL; | |
888 | ||
889 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
890 | wake_up(&conf->wait_for_overlap); | |
891 | ||
78bafebd | 892 | BUG_ON(sh->dev[i].written); |
1da177e4 LT |
893 | sh->dev[i].written = chosen; |
894 | check_xor(); | |
895 | } | |
896 | } | |
897 | break; | |
898 | case RECONSTRUCT_WRITE: | |
899 | memset(ptr[0], 0, STRIPE_SIZE); | |
900 | for (i= disks; i-- ;) | |
901 | if (i!=pd_idx && sh->dev[i].towrite) { | |
902 | chosen = sh->dev[i].towrite; | |
903 | sh->dev[i].towrite = NULL; | |
904 | ||
905 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
906 | wake_up(&conf->wait_for_overlap); | |
907 | ||
78bafebd | 908 | BUG_ON(sh->dev[i].written); |
1da177e4 LT |
909 | sh->dev[i].written = chosen; |
910 | } | |
911 | break; | |
912 | case CHECK_PARITY: | |
913 | break; | |
914 | } | |
915 | if (count>1) { | |
916 | xor_block(count, STRIPE_SIZE, ptr); | |
917 | count = 1; | |
918 | } | |
919 | ||
920 | for (i = disks; i--;) | |
921 | if (sh->dev[i].written) { | |
922 | sector_t sector = sh->dev[i].sector; | |
923 | struct bio *wbi = sh->dev[i].written; | |
924 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { | |
925 | copy_data(1, wbi, sh->dev[i].page, sector); | |
926 | wbi = r5_next_bio(wbi, sector); | |
927 | } | |
928 | ||
929 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
930 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
931 | } | |
932 | ||
933 | switch(method) { | |
934 | case RECONSTRUCT_WRITE: | |
935 | case CHECK_PARITY: | |
936 | for (i=disks; i--;) | |
937 | if (i != pd_idx) { | |
938 | ptr[count++] = page_address(sh->dev[i].page); | |
939 | check_xor(); | |
940 | } | |
941 | break; | |
942 | case READ_MODIFY_WRITE: | |
943 | for (i = disks; i--;) | |
944 | if (sh->dev[i].written) { | |
945 | ptr[count++] = page_address(sh->dev[i].page); | |
946 | check_xor(); | |
947 | } | |
948 | } | |
949 | if (count != 1) | |
950 | xor_block(count, STRIPE_SIZE, ptr); | |
951 | ||
952 | if (method != CHECK_PARITY) { | |
953 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
954 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); | |
955 | } else | |
956 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
957 | } | |
958 | ||
959 | /* | |
960 | * Each stripe/dev can have one or more bion attached. | |
961 | * toread/towrite point to the first in a chain. | |
962 | * The bi_next chain must be in order. | |
963 | */ | |
964 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) | |
965 | { | |
966 | struct bio **bip; | |
967 | raid5_conf_t *conf = sh->raid_conf; | |
72626685 | 968 | int firstwrite=0; |
1da177e4 LT |
969 | |
970 | PRINTK("adding bh b#%llu to stripe s#%llu\n", | |
971 | (unsigned long long)bi->bi_sector, | |
972 | (unsigned long long)sh->sector); | |
973 | ||
974 | ||
975 | spin_lock(&sh->lock); | |
976 | spin_lock_irq(&conf->device_lock); | |
72626685 | 977 | if (forwrite) { |
1da177e4 | 978 | bip = &sh->dev[dd_idx].towrite; |
72626685 N |
979 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
980 | firstwrite = 1; | |
981 | } else | |
1da177e4 LT |
982 | bip = &sh->dev[dd_idx].toread; |
983 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { | |
984 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) | |
985 | goto overlap; | |
986 | bip = & (*bip)->bi_next; | |
987 | } | |
988 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) | |
989 | goto overlap; | |
990 | ||
78bafebd | 991 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
1da177e4 LT |
992 | if (*bip) |
993 | bi->bi_next = *bip; | |
994 | *bip = bi; | |
995 | bi->bi_phys_segments ++; | |
996 | spin_unlock_irq(&conf->device_lock); | |
997 | spin_unlock(&sh->lock); | |
998 | ||
999 | PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n", | |
1000 | (unsigned long long)bi->bi_sector, | |
1001 | (unsigned long long)sh->sector, dd_idx); | |
1002 | ||
72626685 N |
1003 | if (conf->mddev->bitmap && firstwrite) { |
1004 | sh->bm_seq = conf->seq_write; | |
1005 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, | |
1006 | STRIPE_SECTORS, 0); | |
1007 | set_bit(STRIPE_BIT_DELAY, &sh->state); | |
1008 | } | |
1009 | ||
1da177e4 LT |
1010 | if (forwrite) { |
1011 | /* check if page is covered */ | |
1012 | sector_t sector = sh->dev[dd_idx].sector; | |
1013 | for (bi=sh->dev[dd_idx].towrite; | |
1014 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && | |
1015 | bi && bi->bi_sector <= sector; | |
1016 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { | |
1017 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) | |
1018 | sector = bi->bi_sector + (bi->bi_size>>9); | |
1019 | } | |
1020 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) | |
1021 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); | |
1022 | } | |
1023 | return 1; | |
1024 | ||
1025 | overlap: | |
1026 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); | |
1027 | spin_unlock_irq(&conf->device_lock); | |
1028 | spin_unlock(&sh->lock); | |
1029 | return 0; | |
1030 | } | |
1031 | ||
29269553 N |
1032 | static void end_reshape(raid5_conf_t *conf); |
1033 | ||
ccfcc3c1 N |
1034 | static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks) |
1035 | { | |
1036 | int sectors_per_chunk = conf->chunk_size >> 9; | |
1037 | sector_t x = stripe; | |
1038 | int pd_idx, dd_idx; | |
1039 | int chunk_offset = sector_div(x, sectors_per_chunk); | |
1040 | stripe = x; | |
1041 | raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk | |
1042 | + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf); | |
1043 | return pd_idx; | |
1044 | } | |
1045 | ||
1da177e4 LT |
1046 | |
1047 | /* | |
1048 | * handle_stripe - do things to a stripe. | |
1049 | * | |
1050 | * We lock the stripe and then examine the state of various bits | |
1051 | * to see what needs to be done. | |
1052 | * Possible results: | |
1053 | * return some read request which now have data | |
1054 | * return some write requests which are safely on disc | |
1055 | * schedule a read on some buffers | |
1056 | * schedule a write of some buffers | |
1057 | * return confirmation of parity correctness | |
1058 | * | |
1059 | * Parity calculations are done inside the stripe lock | |
1060 | * buffers are taken off read_list or write_list, and bh_cache buffers | |
1061 | * get BH_Lock set before the stripe lock is released. | |
1062 | * | |
1063 | */ | |
1064 | ||
1065 | static void handle_stripe(struct stripe_head *sh) | |
1066 | { | |
1067 | raid5_conf_t *conf = sh->raid_conf; | |
7ecaa1e6 | 1068 | int disks = sh->disks; |
1da177e4 LT |
1069 | struct bio *return_bi= NULL; |
1070 | struct bio *bi; | |
1071 | int i; | |
ccfcc3c1 | 1072 | int syncing, expanding, expanded; |
1da177e4 LT |
1073 | int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0; |
1074 | int non_overwrite = 0; | |
1075 | int failed_num=0; | |
1076 | struct r5dev *dev; | |
1077 | ||
1078 | PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n", | |
1079 | (unsigned long long)sh->sector, atomic_read(&sh->count), | |
1080 | sh->pd_idx); | |
1081 | ||
1082 | spin_lock(&sh->lock); | |
1083 | clear_bit(STRIPE_HANDLE, &sh->state); | |
1084 | clear_bit(STRIPE_DELAYED, &sh->state); | |
1085 | ||
1086 | syncing = test_bit(STRIPE_SYNCING, &sh->state); | |
ccfcc3c1 N |
1087 | expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
1088 | expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); | |
1da177e4 LT |
1089 | /* Now to look around and see what can be done */ |
1090 | ||
9910f16a | 1091 | rcu_read_lock(); |
1da177e4 LT |
1092 | for (i=disks; i--; ) { |
1093 | mdk_rdev_t *rdev; | |
1094 | dev = &sh->dev[i]; | |
1095 | clear_bit(R5_Insync, &dev->flags); | |
1da177e4 LT |
1096 | |
1097 | PRINTK("check %d: state 0x%lx read %p write %p written %p\n", | |
1098 | i, dev->flags, dev->toread, dev->towrite, dev->written); | |
1099 | /* maybe we can reply to a read */ | |
1100 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { | |
1101 | struct bio *rbi, *rbi2; | |
1102 | PRINTK("Return read for disc %d\n", i); | |
1103 | spin_lock_irq(&conf->device_lock); | |
1104 | rbi = dev->toread; | |
1105 | dev->toread = NULL; | |
1106 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) | |
1107 | wake_up(&conf->wait_for_overlap); | |
1108 | spin_unlock_irq(&conf->device_lock); | |
1109 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
1110 | copy_data(0, rbi, dev->page, dev->sector); | |
1111 | rbi2 = r5_next_bio(rbi, dev->sector); | |
1112 | spin_lock_irq(&conf->device_lock); | |
1113 | if (--rbi->bi_phys_segments == 0) { | |
1114 | rbi->bi_next = return_bi; | |
1115 | return_bi = rbi; | |
1116 | } | |
1117 | spin_unlock_irq(&conf->device_lock); | |
1118 | rbi = rbi2; | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | /* now count some things */ | |
1123 | if (test_bit(R5_LOCKED, &dev->flags)) locked++; | |
1124 | if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++; | |
1125 | ||
1126 | ||
1127 | if (dev->toread) to_read++; | |
1128 | if (dev->towrite) { | |
1129 | to_write++; | |
1130 | if (!test_bit(R5_OVERWRITE, &dev->flags)) | |
1131 | non_overwrite++; | |
1132 | } | |
1133 | if (dev->written) written++; | |
9910f16a | 1134 | rdev = rcu_dereference(conf->disks[i].rdev); |
b2d444d7 | 1135 | if (!rdev || !test_bit(In_sync, &rdev->flags)) { |
14f8d26b | 1136 | /* The ReadError flag will just be confusing now */ |
4e5314b5 N |
1137 | clear_bit(R5_ReadError, &dev->flags); |
1138 | clear_bit(R5_ReWrite, &dev->flags); | |
1139 | } | |
b2d444d7 | 1140 | if (!rdev || !test_bit(In_sync, &rdev->flags) |
4e5314b5 | 1141 | || test_bit(R5_ReadError, &dev->flags)) { |
1da177e4 LT |
1142 | failed++; |
1143 | failed_num = i; | |
1144 | } else | |
1145 | set_bit(R5_Insync, &dev->flags); | |
1146 | } | |
9910f16a | 1147 | rcu_read_unlock(); |
1da177e4 LT |
1148 | PRINTK("locked=%d uptodate=%d to_read=%d" |
1149 | " to_write=%d failed=%d failed_num=%d\n", | |
1150 | locked, uptodate, to_read, to_write, failed, failed_num); | |
1151 | /* check if the array has lost two devices and, if so, some requests might | |
1152 | * need to be failed | |
1153 | */ | |
1154 | if (failed > 1 && to_read+to_write+written) { | |
1da177e4 | 1155 | for (i=disks; i--; ) { |
72626685 | 1156 | int bitmap_end = 0; |
4e5314b5 N |
1157 | |
1158 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
9910f16a N |
1159 | mdk_rdev_t *rdev; |
1160 | rcu_read_lock(); | |
1161 | rdev = rcu_dereference(conf->disks[i].rdev); | |
b2d444d7 | 1162 | if (rdev && test_bit(In_sync, &rdev->flags)) |
4e5314b5 N |
1163 | /* multiple read failures in one stripe */ |
1164 | md_error(conf->mddev, rdev); | |
9910f16a | 1165 | rcu_read_unlock(); |
4e5314b5 N |
1166 | } |
1167 | ||
72626685 | 1168 | spin_lock_irq(&conf->device_lock); |
1da177e4 LT |
1169 | /* fail all writes first */ |
1170 | bi = sh->dev[i].towrite; | |
1171 | sh->dev[i].towrite = NULL; | |
72626685 | 1172 | if (bi) { to_write--; bitmap_end = 1; } |
1da177e4 LT |
1173 | |
1174 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1175 | wake_up(&conf->wait_for_overlap); | |
1176 | ||
1177 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
1178 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
1179 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1180 | if (--bi->bi_phys_segments == 0) { | |
1181 | md_write_end(conf->mddev); | |
1182 | bi->bi_next = return_bi; | |
1183 | return_bi = bi; | |
1184 | } | |
1185 | bi = nextbi; | |
1186 | } | |
1187 | /* and fail all 'written' */ | |
1188 | bi = sh->dev[i].written; | |
1189 | sh->dev[i].written = NULL; | |
72626685 | 1190 | if (bi) bitmap_end = 1; |
1da177e4 LT |
1191 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { |
1192 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); | |
1193 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1194 | if (--bi->bi_phys_segments == 0) { | |
1195 | md_write_end(conf->mddev); | |
1196 | bi->bi_next = return_bi; | |
1197 | return_bi = bi; | |
1198 | } | |
1199 | bi = bi2; | |
1200 | } | |
1201 | ||
1202 | /* fail any reads if this device is non-operational */ | |
4e5314b5 N |
1203 | if (!test_bit(R5_Insync, &sh->dev[i].flags) || |
1204 | test_bit(R5_ReadError, &sh->dev[i].flags)) { | |
1da177e4 LT |
1205 | bi = sh->dev[i].toread; |
1206 | sh->dev[i].toread = NULL; | |
1207 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1208 | wake_up(&conf->wait_for_overlap); | |
1209 | if (bi) to_read--; | |
1210 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
1211 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
1212 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1213 | if (--bi->bi_phys_segments == 0) { | |
1214 | bi->bi_next = return_bi; | |
1215 | return_bi = bi; | |
1216 | } | |
1217 | bi = nextbi; | |
1218 | } | |
1219 | } | |
72626685 N |
1220 | spin_unlock_irq(&conf->device_lock); |
1221 | if (bitmap_end) | |
1222 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
1223 | STRIPE_SECTORS, 0, 0); | |
1da177e4 | 1224 | } |
1da177e4 LT |
1225 | } |
1226 | if (failed > 1 && syncing) { | |
1227 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); | |
1228 | clear_bit(STRIPE_SYNCING, &sh->state); | |
1229 | syncing = 0; | |
1230 | } | |
1231 | ||
1232 | /* might be able to return some write requests if the parity block | |
1233 | * is safe, or on a failed drive | |
1234 | */ | |
1235 | dev = &sh->dev[sh->pd_idx]; | |
1236 | if ( written && | |
1237 | ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) && | |
1238 | test_bit(R5_UPTODATE, &dev->flags)) | |
1239 | || (failed == 1 && failed_num == sh->pd_idx)) | |
1240 | ) { | |
1241 | /* any written block on an uptodate or failed drive can be returned. | |
1242 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but | |
1243 | * never LOCKED, so we don't need to test 'failed' directly. | |
1244 | */ | |
1245 | for (i=disks; i--; ) | |
1246 | if (sh->dev[i].written) { | |
1247 | dev = &sh->dev[i]; | |
1248 | if (!test_bit(R5_LOCKED, &dev->flags) && | |
1249 | test_bit(R5_UPTODATE, &dev->flags) ) { | |
1250 | /* We can return any write requests */ | |
1251 | struct bio *wbi, *wbi2; | |
72626685 | 1252 | int bitmap_end = 0; |
1da177e4 LT |
1253 | PRINTK("Return write for disc %d\n", i); |
1254 | spin_lock_irq(&conf->device_lock); | |
1255 | wbi = dev->written; | |
1256 | dev->written = NULL; | |
1257 | while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
1258 | wbi2 = r5_next_bio(wbi, dev->sector); | |
1259 | if (--wbi->bi_phys_segments == 0) { | |
1260 | md_write_end(conf->mddev); | |
1261 | wbi->bi_next = return_bi; | |
1262 | return_bi = wbi; | |
1263 | } | |
1264 | wbi = wbi2; | |
1265 | } | |
72626685 N |
1266 | if (dev->towrite == NULL) |
1267 | bitmap_end = 1; | |
1da177e4 | 1268 | spin_unlock_irq(&conf->device_lock); |
72626685 N |
1269 | if (bitmap_end) |
1270 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
1271 | STRIPE_SECTORS, | |
1272 | !test_bit(STRIPE_DEGRADED, &sh->state), 0); | |
1da177e4 LT |
1273 | } |
1274 | } | |
1275 | } | |
1276 | ||
1277 | /* Now we might consider reading some blocks, either to check/generate | |
1278 | * parity, or to satisfy requests | |
1279 | * or to load a block that is being partially written. | |
1280 | */ | |
ccfcc3c1 | 1281 | if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) { |
1da177e4 LT |
1282 | for (i=disks; i--;) { |
1283 | dev = &sh->dev[i]; | |
1284 | if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1285 | (dev->toread || | |
1286 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || | |
1287 | syncing || | |
ccfcc3c1 | 1288 | expanding || |
1da177e4 LT |
1289 | (failed && (sh->dev[failed_num].toread || |
1290 | (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags)))) | |
1291 | ) | |
1292 | ) { | |
1293 | /* we would like to get this block, possibly | |
1294 | * by computing it, but we might not be able to | |
1295 | */ | |
1296 | if (uptodate == disks-1) { | |
1297 | PRINTK("Computing block %d\n", i); | |
1298 | compute_block(sh, i); | |
1299 | uptodate++; | |
1300 | } else if (test_bit(R5_Insync, &dev->flags)) { | |
1301 | set_bit(R5_LOCKED, &dev->flags); | |
1302 | set_bit(R5_Wantread, &dev->flags); | |
1303 | #if 0 | |
1304 | /* if I am just reading this block and we don't have | |
1305 | a failed drive, or any pending writes then sidestep the cache */ | |
1306 | if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext && | |
1307 | ! syncing && !failed && !to_write) { | |
1308 | sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page; | |
1309 | sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data; | |
1310 | } | |
1311 | #endif | |
1312 | locked++; | |
1313 | PRINTK("Reading block %d (sync=%d)\n", | |
1314 | i, syncing); | |
1da177e4 LT |
1315 | } |
1316 | } | |
1317 | } | |
1318 | set_bit(STRIPE_HANDLE, &sh->state); | |
1319 | } | |
1320 | ||
1321 | /* now to consider writing and what else, if anything should be read */ | |
1322 | if (to_write) { | |
1323 | int rmw=0, rcw=0; | |
1324 | for (i=disks ; i--;) { | |
1325 | /* would I have to read this buffer for read_modify_write */ | |
1326 | dev = &sh->dev[i]; | |
1327 | if ((dev->towrite || i == sh->pd_idx) && | |
1328 | (!test_bit(R5_LOCKED, &dev->flags) | |
1329 | #if 0 | |
1330 | || sh->bh_page[i]!=bh->b_page | |
1331 | #endif | |
1332 | ) && | |
1333 | !test_bit(R5_UPTODATE, &dev->flags)) { | |
1334 | if (test_bit(R5_Insync, &dev->flags) | |
1335 | /* && !(!mddev->insync && i == sh->pd_idx) */ | |
1336 | ) | |
1337 | rmw++; | |
1338 | else rmw += 2*disks; /* cannot read it */ | |
1339 | } | |
1340 | /* Would I have to read this buffer for reconstruct_write */ | |
1341 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && | |
1342 | (!test_bit(R5_LOCKED, &dev->flags) | |
1343 | #if 0 | |
1344 | || sh->bh_page[i] != bh->b_page | |
1345 | #endif | |
1346 | ) && | |
1347 | !test_bit(R5_UPTODATE, &dev->flags)) { | |
1348 | if (test_bit(R5_Insync, &dev->flags)) rcw++; | |
1349 | else rcw += 2*disks; | |
1350 | } | |
1351 | } | |
1352 | PRINTK("for sector %llu, rmw=%d rcw=%d\n", | |
1353 | (unsigned long long)sh->sector, rmw, rcw); | |
1354 | set_bit(STRIPE_HANDLE, &sh->state); | |
1355 | if (rmw < rcw && rmw > 0) | |
1356 | /* prefer read-modify-write, but need to get some data */ | |
1357 | for (i=disks; i--;) { | |
1358 | dev = &sh->dev[i]; | |
1359 | if ((dev->towrite || i == sh->pd_idx) && | |
1360 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1361 | test_bit(R5_Insync, &dev->flags)) { | |
1362 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1363 | { | |
1364 | PRINTK("Read_old block %d for r-m-w\n", i); | |
1365 | set_bit(R5_LOCKED, &dev->flags); | |
1366 | set_bit(R5_Wantread, &dev->flags); | |
1367 | locked++; | |
1368 | } else { | |
1369 | set_bit(STRIPE_DELAYED, &sh->state); | |
1370 | set_bit(STRIPE_HANDLE, &sh->state); | |
1371 | } | |
1372 | } | |
1373 | } | |
1374 | if (rcw <= rmw && rcw > 0) | |
1375 | /* want reconstruct write, but need to get some data */ | |
1376 | for (i=disks; i--;) { | |
1377 | dev = &sh->dev[i]; | |
1378 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && | |
1379 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1380 | test_bit(R5_Insync, &dev->flags)) { | |
1381 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1382 | { | |
1383 | PRINTK("Read_old block %d for Reconstruct\n", i); | |
1384 | set_bit(R5_LOCKED, &dev->flags); | |
1385 | set_bit(R5_Wantread, &dev->flags); | |
1386 | locked++; | |
1387 | } else { | |
1388 | set_bit(STRIPE_DELAYED, &sh->state); | |
1389 | set_bit(STRIPE_HANDLE, &sh->state); | |
1390 | } | |
1391 | } | |
1392 | } | |
1393 | /* now if nothing is locked, and if we have enough data, we can start a write request */ | |
72626685 N |
1394 | if (locked == 0 && (rcw == 0 ||rmw == 0) && |
1395 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { | |
1da177e4 LT |
1396 | PRINTK("Computing parity...\n"); |
1397 | compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE); | |
1398 | /* now every locked buffer is ready to be written */ | |
1399 | for (i=disks; i--;) | |
1400 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { | |
1401 | PRINTK("Writing block %d\n", i); | |
1402 | locked++; | |
1403 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
1404 | if (!test_bit(R5_Insync, &sh->dev[i].flags) | |
1405 | || (i==sh->pd_idx && failed == 0)) | |
1406 | set_bit(STRIPE_INSYNC, &sh->state); | |
1407 | } | |
1408 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
1409 | atomic_dec(&conf->preread_active_stripes); | |
1410 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) | |
1411 | md_wakeup_thread(conf->mddev->thread); | |
1412 | } | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | /* maybe we need to check and possibly fix the parity for this stripe | |
1417 | * Any reads will already have been scheduled, so we just see if enough data | |
1418 | * is available | |
1419 | */ | |
1420 | if (syncing && locked == 0 && | |
14f8d26b | 1421 | !test_bit(STRIPE_INSYNC, &sh->state)) { |
1da177e4 LT |
1422 | set_bit(STRIPE_HANDLE, &sh->state); |
1423 | if (failed == 0) { | |
1424 | char *pagea; | |
78bafebd | 1425 | BUG_ON(uptodate != disks); |
1da177e4 LT |
1426 | compute_parity(sh, CHECK_PARITY); |
1427 | uptodate--; | |
1428 | pagea = page_address(sh->dev[sh->pd_idx].page); | |
1429 | if ((*(u32*)pagea) == 0 && | |
1430 | !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) { | |
1431 | /* parity is correct (on disc, not in buffer any more) */ | |
1432 | set_bit(STRIPE_INSYNC, &sh->state); | |
9d88883e N |
1433 | } else { |
1434 | conf->mddev->resync_mismatches += STRIPE_SECTORS; | |
1435 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) | |
1436 | /* don't try to repair!! */ | |
1437 | set_bit(STRIPE_INSYNC, &sh->state); | |
14f8d26b N |
1438 | else { |
1439 | compute_block(sh, sh->pd_idx); | |
1440 | uptodate++; | |
1441 | } | |
1da177e4 LT |
1442 | } |
1443 | } | |
1444 | if (!test_bit(STRIPE_INSYNC, &sh->state)) { | |
14f8d26b | 1445 | /* either failed parity check, or recovery is happening */ |
1da177e4 LT |
1446 | if (failed==0) |
1447 | failed_num = sh->pd_idx; | |
1da177e4 | 1448 | dev = &sh->dev[failed_num]; |
14f8d26b N |
1449 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
1450 | BUG_ON(uptodate != disks); | |
1451 | ||
1da177e4 LT |
1452 | set_bit(R5_LOCKED, &dev->flags); |
1453 | set_bit(R5_Wantwrite, &dev->flags); | |
72626685 | 1454 | clear_bit(STRIPE_DEGRADED, &sh->state); |
1da177e4 LT |
1455 | locked++; |
1456 | set_bit(STRIPE_INSYNC, &sh->state); | |
1da177e4 LT |
1457 | } |
1458 | } | |
1459 | if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { | |
1460 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); | |
1461 | clear_bit(STRIPE_SYNCING, &sh->state); | |
1462 | } | |
4e5314b5 N |
1463 | |
1464 | /* If the failed drive is just a ReadError, then we might need to progress | |
1465 | * the repair/check process | |
1466 | */ | |
ba22dcbf N |
1467 | if (failed == 1 && ! conf->mddev->ro && |
1468 | test_bit(R5_ReadError, &sh->dev[failed_num].flags) | |
4e5314b5 N |
1469 | && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags) |
1470 | && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags) | |
1471 | ) { | |
1472 | dev = &sh->dev[failed_num]; | |
1473 | if (!test_bit(R5_ReWrite, &dev->flags)) { | |
1474 | set_bit(R5_Wantwrite, &dev->flags); | |
1475 | set_bit(R5_ReWrite, &dev->flags); | |
1476 | set_bit(R5_LOCKED, &dev->flags); | |
ccfcc3c1 | 1477 | locked++; |
4e5314b5 N |
1478 | } else { |
1479 | /* let's read it back */ | |
1480 | set_bit(R5_Wantread, &dev->flags); | |
1481 | set_bit(R5_LOCKED, &dev->flags); | |
ccfcc3c1 | 1482 | locked++; |
4e5314b5 N |
1483 | } |
1484 | } | |
1485 | ||
ccfcc3c1 N |
1486 | if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) { |
1487 | /* Need to write out all blocks after computing parity */ | |
1488 | sh->disks = conf->raid_disks; | |
1489 | sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks); | |
1490 | compute_parity(sh, RECONSTRUCT_WRITE); | |
1491 | for (i= conf->raid_disks; i--;) { | |
1492 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
1493 | locked++; | |
1494 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
1495 | } | |
1496 | clear_bit(STRIPE_EXPANDING, &sh->state); | |
1497 | } else if (expanded) { | |
1498 | clear_bit(STRIPE_EXPAND_READY, &sh->state); | |
f6705578 | 1499 | atomic_dec(&conf->reshape_stripes); |
ccfcc3c1 N |
1500 | wake_up(&conf->wait_for_overlap); |
1501 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); | |
1502 | } | |
1503 | ||
1504 | if (expanding && locked == 0) { | |
1505 | /* We have read all the blocks in this stripe and now we need to | |
1506 | * copy some of them into a target stripe for expand. | |
1507 | */ | |
1508 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); | |
1509 | for (i=0; i< sh->disks; i++) | |
1510 | if (i != sh->pd_idx) { | |
1511 | int dd_idx, pd_idx, j; | |
1512 | struct stripe_head *sh2; | |
1513 | ||
1514 | sector_t bn = compute_blocknr(sh, i); | |
1515 | sector_t s = raid5_compute_sector(bn, conf->raid_disks, | |
1516 | conf->raid_disks-1, | |
1517 | &dd_idx, &pd_idx, conf); | |
1518 | sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1); | |
1519 | if (sh2 == NULL) | |
1520 | /* so far only the early blocks of this stripe | |
1521 | * have been requested. When later blocks | |
1522 | * get requested, we will try again | |
1523 | */ | |
1524 | continue; | |
1525 | if(!test_bit(STRIPE_EXPANDING, &sh2->state) || | |
1526 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { | |
1527 | /* must have already done this block */ | |
1528 | release_stripe(sh2); | |
1529 | continue; | |
1530 | } | |
1531 | memcpy(page_address(sh2->dev[dd_idx].page), | |
1532 | page_address(sh->dev[i].page), | |
1533 | STRIPE_SIZE); | |
1534 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); | |
1535 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); | |
1536 | for (j=0; j<conf->raid_disks; j++) | |
1537 | if (j != sh2->pd_idx && | |
1538 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) | |
1539 | break; | |
1540 | if (j == conf->raid_disks) { | |
1541 | set_bit(STRIPE_EXPAND_READY, &sh2->state); | |
1542 | set_bit(STRIPE_HANDLE, &sh2->state); | |
1543 | } | |
1544 | release_stripe(sh2); | |
1545 | } | |
1546 | } | |
1547 | ||
1da177e4 LT |
1548 | spin_unlock(&sh->lock); |
1549 | ||
1550 | while ((bi=return_bi)) { | |
1551 | int bytes = bi->bi_size; | |
1552 | ||
1553 | return_bi = bi->bi_next; | |
1554 | bi->bi_next = NULL; | |
1555 | bi->bi_size = 0; | |
1556 | bi->bi_end_io(bi, bytes, 0); | |
1557 | } | |
1558 | for (i=disks; i-- ;) { | |
1559 | int rw; | |
1560 | struct bio *bi; | |
1561 | mdk_rdev_t *rdev; | |
1562 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) | |
1563 | rw = 1; | |
1564 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) | |
1565 | rw = 0; | |
1566 | else | |
1567 | continue; | |
1568 | ||
1569 | bi = &sh->dev[i].req; | |
1570 | ||
1571 | bi->bi_rw = rw; | |
1572 | if (rw) | |
1573 | bi->bi_end_io = raid5_end_write_request; | |
1574 | else | |
1575 | bi->bi_end_io = raid5_end_read_request; | |
1576 | ||
1577 | rcu_read_lock(); | |
d6065f7b | 1578 | rdev = rcu_dereference(conf->disks[i].rdev); |
b2d444d7 | 1579 | if (rdev && test_bit(Faulty, &rdev->flags)) |
1da177e4 LT |
1580 | rdev = NULL; |
1581 | if (rdev) | |
1582 | atomic_inc(&rdev->nr_pending); | |
1583 | rcu_read_unlock(); | |
1584 | ||
1585 | if (rdev) { | |
ccfcc3c1 | 1586 | if (syncing || expanding || expanded) |
1da177e4 LT |
1587 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
1588 | ||
1589 | bi->bi_bdev = rdev->bdev; | |
1590 | PRINTK("for %llu schedule op %ld on disc %d\n", | |
1591 | (unsigned long long)sh->sector, bi->bi_rw, i); | |
1592 | atomic_inc(&sh->count); | |
1593 | bi->bi_sector = sh->sector + rdev->data_offset; | |
1594 | bi->bi_flags = 1 << BIO_UPTODATE; | |
1595 | bi->bi_vcnt = 1; | |
1596 | bi->bi_max_vecs = 1; | |
1597 | bi->bi_idx = 0; | |
1598 | bi->bi_io_vec = &sh->dev[i].vec; | |
1599 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; | |
1600 | bi->bi_io_vec[0].bv_offset = 0; | |
1601 | bi->bi_size = STRIPE_SIZE; | |
1602 | bi->bi_next = NULL; | |
4dbcdc75 N |
1603 | if (rw == WRITE && |
1604 | test_bit(R5_ReWrite, &sh->dev[i].flags)) | |
1605 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); | |
1da177e4 LT |
1606 | generic_make_request(bi); |
1607 | } else { | |
72626685 N |
1608 | if (rw == 1) |
1609 | set_bit(STRIPE_DEGRADED, &sh->state); | |
1da177e4 LT |
1610 | PRINTK("skip op %ld on disc %d for sector %llu\n", |
1611 | bi->bi_rw, i, (unsigned long long)sh->sector); | |
1612 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
1613 | set_bit(STRIPE_HANDLE, &sh->state); | |
1614 | } | |
1615 | } | |
1616 | } | |
1617 | ||
858119e1 | 1618 | static void raid5_activate_delayed(raid5_conf_t *conf) |
1da177e4 LT |
1619 | { |
1620 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { | |
1621 | while (!list_empty(&conf->delayed_list)) { | |
1622 | struct list_head *l = conf->delayed_list.next; | |
1623 | struct stripe_head *sh; | |
1624 | sh = list_entry(l, struct stripe_head, lru); | |
1625 | list_del_init(l); | |
1626 | clear_bit(STRIPE_DELAYED, &sh->state); | |
1627 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1628 | atomic_inc(&conf->preread_active_stripes); | |
1629 | list_add_tail(&sh->lru, &conf->handle_list); | |
1630 | } | |
1631 | } | |
1632 | } | |
1633 | ||
858119e1 | 1634 | static void activate_bit_delay(raid5_conf_t *conf) |
72626685 N |
1635 | { |
1636 | /* device_lock is held */ | |
1637 | struct list_head head; | |
1638 | list_add(&head, &conf->bitmap_list); | |
1639 | list_del_init(&conf->bitmap_list); | |
1640 | while (!list_empty(&head)) { | |
1641 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); | |
1642 | list_del_init(&sh->lru); | |
1643 | atomic_inc(&sh->count); | |
1644 | __release_stripe(conf, sh); | |
1645 | } | |
1646 | } | |
1647 | ||
1da177e4 LT |
1648 | static void unplug_slaves(mddev_t *mddev) |
1649 | { | |
1650 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1651 | int i; | |
1652 | ||
1653 | rcu_read_lock(); | |
1654 | for (i=0; i<mddev->raid_disks; i++) { | |
d6065f7b | 1655 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
b2d444d7 | 1656 | if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) { |
1da177e4 LT |
1657 | request_queue_t *r_queue = bdev_get_queue(rdev->bdev); |
1658 | ||
1659 | atomic_inc(&rdev->nr_pending); | |
1660 | rcu_read_unlock(); | |
1661 | ||
1662 | if (r_queue->unplug_fn) | |
1663 | r_queue->unplug_fn(r_queue); | |
1664 | ||
1665 | rdev_dec_pending(rdev, mddev); | |
1666 | rcu_read_lock(); | |
1667 | } | |
1668 | } | |
1669 | rcu_read_unlock(); | |
1670 | } | |
1671 | ||
1672 | static void raid5_unplug_device(request_queue_t *q) | |
1673 | { | |
1674 | mddev_t *mddev = q->queuedata; | |
1675 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1676 | unsigned long flags; | |
1677 | ||
1678 | spin_lock_irqsave(&conf->device_lock, flags); | |
1679 | ||
72626685 N |
1680 | if (blk_remove_plug(q)) { |
1681 | conf->seq_flush++; | |
1da177e4 | 1682 | raid5_activate_delayed(conf); |
72626685 | 1683 | } |
1da177e4 LT |
1684 | md_wakeup_thread(mddev->thread); |
1685 | ||
1686 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1687 | ||
1688 | unplug_slaves(mddev); | |
1689 | } | |
1690 | ||
1691 | static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk, | |
1692 | sector_t *error_sector) | |
1693 | { | |
1694 | mddev_t *mddev = q->queuedata; | |
1695 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1696 | int i, ret = 0; | |
1697 | ||
1698 | rcu_read_lock(); | |
1699 | for (i=0; i<mddev->raid_disks && ret == 0; i++) { | |
d6065f7b | 1700 | mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev); |
b2d444d7 | 1701 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
1da177e4 LT |
1702 | struct block_device *bdev = rdev->bdev; |
1703 | request_queue_t *r_queue = bdev_get_queue(bdev); | |
1704 | ||
1705 | if (!r_queue->issue_flush_fn) | |
1706 | ret = -EOPNOTSUPP; | |
1707 | else { | |
1708 | atomic_inc(&rdev->nr_pending); | |
1709 | rcu_read_unlock(); | |
1710 | ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, | |
1711 | error_sector); | |
1712 | rdev_dec_pending(rdev, mddev); | |
1713 | rcu_read_lock(); | |
1714 | } | |
1715 | } | |
1716 | } | |
1717 | rcu_read_unlock(); | |
1718 | return ret; | |
1719 | } | |
1720 | ||
1721 | static inline void raid5_plug_device(raid5_conf_t *conf) | |
1722 | { | |
1723 | spin_lock_irq(&conf->device_lock); | |
1724 | blk_plug_device(conf->mddev->queue); | |
1725 | spin_unlock_irq(&conf->device_lock); | |
1726 | } | |
1727 | ||
7ecaa1e6 | 1728 | static int make_request(request_queue_t *q, struct bio * bi) |
1da177e4 LT |
1729 | { |
1730 | mddev_t *mddev = q->queuedata; | |
1731 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1da177e4 LT |
1732 | unsigned int dd_idx, pd_idx; |
1733 | sector_t new_sector; | |
1734 | sector_t logical_sector, last_sector; | |
1735 | struct stripe_head *sh; | |
a362357b | 1736 | const int rw = bio_data_dir(bi); |
f6344757 | 1737 | int remaining; |
1da177e4 | 1738 | |
e5dcdd80 N |
1739 | if (unlikely(bio_barrier(bi))) { |
1740 | bio_endio(bi, bi->bi_size, -EOPNOTSUPP); | |
1741 | return 0; | |
1742 | } | |
1743 | ||
3d310eb7 | 1744 | md_write_start(mddev, bi); |
06d91a5f | 1745 | |
a362357b JA |
1746 | disk_stat_inc(mddev->gendisk, ios[rw]); |
1747 | disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi)); | |
1da177e4 LT |
1748 | |
1749 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); | |
1750 | last_sector = bi->bi_sector + (bi->bi_size>>9); | |
1751 | bi->bi_next = NULL; | |
1752 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ | |
06d91a5f | 1753 | |
1da177e4 LT |
1754 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
1755 | DEFINE_WAIT(w); | |
7ecaa1e6 | 1756 | int disks; |
b578d55f | 1757 | |
7ecaa1e6 | 1758 | retry: |
b578d55f | 1759 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
7ecaa1e6 N |
1760 | if (likely(conf->expand_progress == MaxSector)) |
1761 | disks = conf->raid_disks; | |
1762 | else { | |
df8e7f76 N |
1763 | /* spinlock is needed as expand_progress may be |
1764 | * 64bit on a 32bit platform, and so it might be | |
1765 | * possible to see a half-updated value | |
1766 | * Ofcourse expand_progress could change after | |
1767 | * the lock is dropped, so once we get a reference | |
1768 | * to the stripe that we think it is, we will have | |
1769 | * to check again. | |
1770 | */ | |
7ecaa1e6 N |
1771 | spin_lock_irq(&conf->device_lock); |
1772 | disks = conf->raid_disks; | |
1773 | if (logical_sector >= conf->expand_progress) | |
1774 | disks = conf->previous_raid_disks; | |
b578d55f N |
1775 | else { |
1776 | if (logical_sector >= conf->expand_lo) { | |
1777 | spin_unlock_irq(&conf->device_lock); | |
1778 | schedule(); | |
1779 | goto retry; | |
1780 | } | |
1781 | } | |
7ecaa1e6 N |
1782 | spin_unlock_irq(&conf->device_lock); |
1783 | } | |
1784 | new_sector = raid5_compute_sector(logical_sector, disks, disks - 1, | |
1785 | &dd_idx, &pd_idx, conf); | |
1da177e4 LT |
1786 | PRINTK("raid5: make_request, sector %llu logical %llu\n", |
1787 | (unsigned long long)new_sector, | |
1788 | (unsigned long long)logical_sector); | |
1789 | ||
7ecaa1e6 | 1790 | sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK)); |
1da177e4 | 1791 | if (sh) { |
7ecaa1e6 N |
1792 | if (unlikely(conf->expand_progress != MaxSector)) { |
1793 | /* expansion might have moved on while waiting for a | |
df8e7f76 N |
1794 | * stripe, so we must do the range check again. |
1795 | * Expansion could still move past after this | |
1796 | * test, but as we are holding a reference to | |
1797 | * 'sh', we know that if that happens, | |
1798 | * STRIPE_EXPANDING will get set and the expansion | |
1799 | * won't proceed until we finish with the stripe. | |
7ecaa1e6 N |
1800 | */ |
1801 | int must_retry = 0; | |
1802 | spin_lock_irq(&conf->device_lock); | |
1803 | if (logical_sector < conf->expand_progress && | |
1804 | disks == conf->previous_raid_disks) | |
1805 | /* mismatch, need to try again */ | |
1806 | must_retry = 1; | |
1807 | spin_unlock_irq(&conf->device_lock); | |
1808 | if (must_retry) { | |
1809 | release_stripe(sh); | |
1810 | goto retry; | |
1811 | } | |
1812 | } | |
e464eafd N |
1813 | /* FIXME what if we get a false positive because these |
1814 | * are being updated. | |
1815 | */ | |
1816 | if (logical_sector >= mddev->suspend_lo && | |
1817 | logical_sector < mddev->suspend_hi) { | |
1818 | release_stripe(sh); | |
1819 | schedule(); | |
1820 | goto retry; | |
1821 | } | |
7ecaa1e6 N |
1822 | |
1823 | if (test_bit(STRIPE_EXPANDING, &sh->state) || | |
1824 | !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { | |
1825 | /* Stripe is busy expanding or | |
1826 | * add failed due to overlap. Flush everything | |
1da177e4 LT |
1827 | * and wait a while |
1828 | */ | |
1829 | raid5_unplug_device(mddev->queue); | |
1830 | release_stripe(sh); | |
1831 | schedule(); | |
1832 | goto retry; | |
1833 | } | |
1834 | finish_wait(&conf->wait_for_overlap, &w); | |
1835 | raid5_plug_device(conf); | |
1836 | handle_stripe(sh); | |
1837 | release_stripe(sh); | |
1da177e4 LT |
1838 | } else { |
1839 | /* cannot get stripe for read-ahead, just give-up */ | |
1840 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1841 | finish_wait(&conf->wait_for_overlap, &w); | |
1842 | break; | |
1843 | } | |
1844 | ||
1845 | } | |
1846 | spin_lock_irq(&conf->device_lock); | |
f6344757 N |
1847 | remaining = --bi->bi_phys_segments; |
1848 | spin_unlock_irq(&conf->device_lock); | |
1849 | if (remaining == 0) { | |
1da177e4 LT |
1850 | int bytes = bi->bi_size; |
1851 | ||
1852 | if ( bio_data_dir(bi) == WRITE ) | |
1853 | md_write_end(mddev); | |
1854 | bi->bi_size = 0; | |
1855 | bi->bi_end_io(bi, bytes, 0); | |
1856 | } | |
1da177e4 LT |
1857 | return 0; |
1858 | } | |
1859 | ||
1860 | /* FIXME go_faster isn't used */ | |
57afd89f | 1861 | static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
1da177e4 LT |
1862 | { |
1863 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
1864 | struct stripe_head *sh; | |
ccfcc3c1 N |
1865 | int pd_idx; |
1866 | sector_t first_sector, last_sector; | |
1da177e4 LT |
1867 | int raid_disks = conf->raid_disks; |
1868 | int data_disks = raid_disks-1; | |
72626685 N |
1869 | sector_t max_sector = mddev->size << 1; |
1870 | int sync_blocks; | |
1da177e4 | 1871 | |
72626685 | 1872 | if (sector_nr >= max_sector) { |
1da177e4 LT |
1873 | /* just being told to finish up .. nothing much to do */ |
1874 | unplug_slaves(mddev); | |
29269553 N |
1875 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
1876 | end_reshape(conf); | |
1877 | return 0; | |
1878 | } | |
72626685 N |
1879 | |
1880 | if (mddev->curr_resync < max_sector) /* aborted */ | |
1881 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, | |
1882 | &sync_blocks, 1); | |
1883 | else /* compelted sync */ | |
1884 | conf->fullsync = 0; | |
1885 | bitmap_close_sync(mddev->bitmap); | |
1886 | ||
1da177e4 LT |
1887 | return 0; |
1888 | } | |
ccfcc3c1 N |
1889 | |
1890 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { | |
1891 | /* reshaping is quite different to recovery/resync so it is | |
1892 | * handled quite separately ... here. | |
1893 | * | |
1894 | * On each call to sync_request, we gather one chunk worth of | |
1895 | * destination stripes and flag them as expanding. | |
1896 | * Then we find all the source stripes and request reads. | |
1897 | * As the reads complete, handle_stripe will copy the data | |
1898 | * into the destination stripe and release that stripe. | |
1899 | */ | |
1900 | int i; | |
1901 | int dd_idx; | |
b578d55f | 1902 | sector_t writepos, safepos, gap; |
f6705578 N |
1903 | |
1904 | if (sector_nr == 0 && | |
1905 | conf->expand_progress != 0) { | |
1906 | /* restarting in the middle, skip the initial sectors */ | |
1907 | sector_nr = conf->expand_progress; | |
1908 | sector_div(sector_nr, conf->raid_disks-1); | |
1909 | *skipped = 1; | |
1910 | return sector_nr; | |
1911 | } | |
1912 | ||
b578d55f N |
1913 | /* we update the metadata when there is more than 3Meg |
1914 | * in the block range (that is rather arbitrary, should | |
1915 | * probably be time based) or when the data about to be | |
1916 | * copied would over-write the source of the data at | |
1917 | * the front of the range. | |
1918 | * i.e. one new_stripe forward from expand_progress new_maps | |
1919 | * to after where expand_lo old_maps to | |
1920 | */ | |
1921 | writepos = conf->expand_progress + | |
1922 | conf->chunk_size/512*(conf->raid_disks-1); | |
1923 | sector_div(writepos, conf->raid_disks-1); | |
1924 | safepos = conf->expand_lo; | |
1925 | sector_div(safepos, conf->previous_raid_disks-1); | |
1926 | gap = conf->expand_progress - conf->expand_lo; | |
1927 | ||
1928 | if (writepos >= safepos || | |
1929 | gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) { | |
1930 | /* Cannot proceed until we've updated the superblock... */ | |
1931 | wait_event(conf->wait_for_overlap, | |
1932 | atomic_read(&conf->reshape_stripes)==0); | |
1933 | mddev->reshape_position = conf->expand_progress; | |
1934 | mddev->sb_dirty = 1; | |
1935 | md_wakeup_thread(mddev->thread); | |
1936 | wait_event(mddev->sb_wait, mddev->sb_dirty == 0 || | |
1937 | kthread_should_stop()); | |
1938 | spin_lock_irq(&conf->device_lock); | |
1939 | conf->expand_lo = mddev->reshape_position; | |
1940 | spin_unlock_irq(&conf->device_lock); | |
1941 | wake_up(&conf->wait_for_overlap); | |
1942 | } | |
f6705578 | 1943 | |
ccfcc3c1 N |
1944 | for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) { |
1945 | int j; | |
1946 | int skipped = 0; | |
1947 | pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks); | |
1948 | sh = get_active_stripe(conf, sector_nr+i, | |
1949 | conf->raid_disks, pd_idx, 0); | |
1950 | set_bit(STRIPE_EXPANDING, &sh->state); | |
f6705578 | 1951 | atomic_inc(&conf->reshape_stripes); |
ccfcc3c1 N |
1952 | /* If any of this stripe is beyond the end of the old |
1953 | * array, then we need to zero those blocks | |
1954 | */ | |
1955 | for (j=sh->disks; j--;) { | |
1956 | sector_t s; | |
1957 | if (j == sh->pd_idx) | |
1958 | continue; | |
1959 | s = compute_blocknr(sh, j); | |
1960 | if (s < (mddev->array_size<<1)) { | |
1961 | skipped = 1; | |
1962 | continue; | |
1963 | } | |
1964 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); | |
1965 | set_bit(R5_Expanded, &sh->dev[j].flags); | |
1966 | set_bit(R5_UPTODATE, &sh->dev[j].flags); | |
1967 | } | |
1968 | if (!skipped) { | |
1969 | set_bit(STRIPE_EXPAND_READY, &sh->state); | |
1970 | set_bit(STRIPE_HANDLE, &sh->state); | |
1971 | } | |
1972 | release_stripe(sh); | |
1973 | } | |
1974 | spin_lock_irq(&conf->device_lock); | |
1975 | conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1); | |
1976 | spin_unlock_irq(&conf->device_lock); | |
1977 | /* Ok, those stripe are ready. We can start scheduling | |
1978 | * reads on the source stripes. | |
1979 | * The source stripes are determined by mapping the first and last | |
1980 | * block on the destination stripes. | |
1981 | */ | |
1982 | raid_disks = conf->previous_raid_disks; | |
1983 | data_disks = raid_disks - 1; | |
1984 | first_sector = | |
1985 | raid5_compute_sector(sector_nr*(conf->raid_disks-1), | |
1986 | raid_disks, data_disks, | |
1987 | &dd_idx, &pd_idx, conf); | |
1988 | last_sector = | |
1989 | raid5_compute_sector((sector_nr+conf->chunk_size/512) | |
1990 | *(conf->raid_disks-1) -1, | |
1991 | raid_disks, data_disks, | |
1992 | &dd_idx, &pd_idx, conf); | |
1993 | if (last_sector >= (mddev->size<<1)) | |
1994 | last_sector = (mddev->size<<1)-1; | |
1995 | while (first_sector <= last_sector) { | |
1996 | pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks); | |
1997 | sh = get_active_stripe(conf, first_sector, | |
1998 | conf->previous_raid_disks, pd_idx, 0); | |
1999 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); | |
2000 | set_bit(STRIPE_HANDLE, &sh->state); | |
2001 | release_stripe(sh); | |
2002 | first_sector += STRIPE_SECTORS; | |
2003 | } | |
2004 | return conf->chunk_size>>9; | |
2005 | } | |
1da177e4 LT |
2006 | /* if there is 1 or more failed drives and we are trying |
2007 | * to resync, then assert that we are finished, because there is | |
2008 | * nothing we can do. | |
2009 | */ | |
2010 | if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
57afd89f N |
2011 | sector_t rv = (mddev->size << 1) - sector_nr; |
2012 | *skipped = 1; | |
1da177e4 LT |
2013 | return rv; |
2014 | } | |
72626685 | 2015 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
3855ad9f | 2016 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
72626685 N |
2017 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
2018 | /* we can skip this block, and probably more */ | |
2019 | sync_blocks /= STRIPE_SECTORS; | |
2020 | *skipped = 1; | |
2021 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ | |
2022 | } | |
1da177e4 | 2023 | |
ccfcc3c1 | 2024 | pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks); |
7ecaa1e6 | 2025 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1); |
1da177e4 | 2026 | if (sh == NULL) { |
7ecaa1e6 | 2027 | sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0); |
1da177e4 LT |
2028 | /* make sure we don't swamp the stripe cache if someone else |
2029 | * is trying to get access | |
2030 | */ | |
66c006a5 | 2031 | schedule_timeout_uninterruptible(1); |
1da177e4 | 2032 | } |
72626685 | 2033 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0); |
1da177e4 LT |
2034 | spin_lock(&sh->lock); |
2035 | set_bit(STRIPE_SYNCING, &sh->state); | |
2036 | clear_bit(STRIPE_INSYNC, &sh->state); | |
2037 | spin_unlock(&sh->lock); | |
2038 | ||
2039 | handle_stripe(sh); | |
2040 | release_stripe(sh); | |
2041 | ||
2042 | return STRIPE_SECTORS; | |
2043 | } | |
2044 | ||
2045 | /* | |
2046 | * This is our raid5 kernel thread. | |
2047 | * | |
2048 | * We scan the hash table for stripes which can be handled now. | |
2049 | * During the scan, completed stripes are saved for us by the interrupt | |
2050 | * handler, so that they will not have to wait for our next wakeup. | |
2051 | */ | |
2052 | static void raid5d (mddev_t *mddev) | |
2053 | { | |
2054 | struct stripe_head *sh; | |
2055 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2056 | int handled; | |
2057 | ||
2058 | PRINTK("+++ raid5d active\n"); | |
2059 | ||
2060 | md_check_recovery(mddev); | |
1da177e4 LT |
2061 | |
2062 | handled = 0; | |
2063 | spin_lock_irq(&conf->device_lock); | |
2064 | while (1) { | |
2065 | struct list_head *first; | |
2066 | ||
72626685 N |
2067 | if (conf->seq_flush - conf->seq_write > 0) { |
2068 | int seq = conf->seq_flush; | |
700e432d | 2069 | spin_unlock_irq(&conf->device_lock); |
72626685 | 2070 | bitmap_unplug(mddev->bitmap); |
700e432d | 2071 | spin_lock_irq(&conf->device_lock); |
72626685 N |
2072 | conf->seq_write = seq; |
2073 | activate_bit_delay(conf); | |
2074 | } | |
2075 | ||
1da177e4 LT |
2076 | if (list_empty(&conf->handle_list) && |
2077 | atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD && | |
2078 | !blk_queue_plugged(mddev->queue) && | |
2079 | !list_empty(&conf->delayed_list)) | |
2080 | raid5_activate_delayed(conf); | |
2081 | ||
2082 | if (list_empty(&conf->handle_list)) | |
2083 | break; | |
2084 | ||
2085 | first = conf->handle_list.next; | |
2086 | sh = list_entry(first, struct stripe_head, lru); | |
2087 | ||
2088 | list_del_init(first); | |
2089 | atomic_inc(&sh->count); | |
78bafebd | 2090 | BUG_ON(atomic_read(&sh->count)!= 1); |
1da177e4 LT |
2091 | spin_unlock_irq(&conf->device_lock); |
2092 | ||
2093 | handled++; | |
2094 | handle_stripe(sh); | |
2095 | release_stripe(sh); | |
2096 | ||
2097 | spin_lock_irq(&conf->device_lock); | |
2098 | } | |
2099 | PRINTK("%d stripes handled\n", handled); | |
2100 | ||
2101 | spin_unlock_irq(&conf->device_lock); | |
2102 | ||
2103 | unplug_slaves(mddev); | |
2104 | ||
2105 | PRINTK("--- raid5d inactive\n"); | |
2106 | } | |
2107 | ||
3f294f4f | 2108 | static ssize_t |
007583c9 | 2109 | raid5_show_stripe_cache_size(mddev_t *mddev, char *page) |
3f294f4f | 2110 | { |
007583c9 | 2111 | raid5_conf_t *conf = mddev_to_conf(mddev); |
96de1e66 N |
2112 | if (conf) |
2113 | return sprintf(page, "%d\n", conf->max_nr_stripes); | |
2114 | else | |
2115 | return 0; | |
3f294f4f N |
2116 | } |
2117 | ||
2118 | static ssize_t | |
007583c9 | 2119 | raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len) |
3f294f4f | 2120 | { |
007583c9 | 2121 | raid5_conf_t *conf = mddev_to_conf(mddev); |
3f294f4f N |
2122 | char *end; |
2123 | int new; | |
2124 | if (len >= PAGE_SIZE) | |
2125 | return -EINVAL; | |
96de1e66 N |
2126 | if (!conf) |
2127 | return -ENODEV; | |
3f294f4f N |
2128 | |
2129 | new = simple_strtoul(page, &end, 10); | |
2130 | if (!*page || (*end && *end != '\n') ) | |
2131 | return -EINVAL; | |
2132 | if (new <= 16 || new > 32768) | |
2133 | return -EINVAL; | |
2134 | while (new < conf->max_nr_stripes) { | |
2135 | if (drop_one_stripe(conf)) | |
2136 | conf->max_nr_stripes--; | |
2137 | else | |
2138 | break; | |
2139 | } | |
2140 | while (new > conf->max_nr_stripes) { | |
2141 | if (grow_one_stripe(conf)) | |
2142 | conf->max_nr_stripes++; | |
2143 | else break; | |
2144 | } | |
2145 | return len; | |
2146 | } | |
007583c9 | 2147 | |
96de1e66 N |
2148 | static struct md_sysfs_entry |
2149 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, | |
2150 | raid5_show_stripe_cache_size, | |
2151 | raid5_store_stripe_cache_size); | |
3f294f4f N |
2152 | |
2153 | static ssize_t | |
96de1e66 | 2154 | stripe_cache_active_show(mddev_t *mddev, char *page) |
3f294f4f | 2155 | { |
007583c9 | 2156 | raid5_conf_t *conf = mddev_to_conf(mddev); |
96de1e66 N |
2157 | if (conf) |
2158 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); | |
2159 | else | |
2160 | return 0; | |
3f294f4f N |
2161 | } |
2162 | ||
96de1e66 N |
2163 | static struct md_sysfs_entry |
2164 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); | |
3f294f4f | 2165 | |
007583c9 | 2166 | static struct attribute *raid5_attrs[] = { |
3f294f4f N |
2167 | &raid5_stripecache_size.attr, |
2168 | &raid5_stripecache_active.attr, | |
2169 | NULL, | |
2170 | }; | |
007583c9 N |
2171 | static struct attribute_group raid5_attrs_group = { |
2172 | .name = NULL, | |
2173 | .attrs = raid5_attrs, | |
3f294f4f N |
2174 | }; |
2175 | ||
72626685 | 2176 | static int run(mddev_t *mddev) |
1da177e4 LT |
2177 | { |
2178 | raid5_conf_t *conf; | |
2179 | int raid_disk, memory; | |
2180 | mdk_rdev_t *rdev; | |
2181 | struct disk_info *disk; | |
2182 | struct list_head *tmp; | |
2183 | ||
2184 | if (mddev->level != 5 && mddev->level != 4) { | |
14f8d26b N |
2185 | printk(KERN_ERR "raid5: %s: raid level not set to 4/5 (%d)\n", |
2186 | mdname(mddev), mddev->level); | |
1da177e4 LT |
2187 | return -EIO; |
2188 | } | |
2189 | ||
f6705578 N |
2190 | if (mddev->reshape_position != MaxSector) { |
2191 | /* Check that we can continue the reshape. | |
2192 | * Currently only disks can change, it must | |
2193 | * increase, and we must be past the point where | |
2194 | * a stripe over-writes itself | |
2195 | */ | |
2196 | sector_t here_new, here_old; | |
2197 | int old_disks; | |
2198 | ||
2199 | if (mddev->new_level != mddev->level || | |
2200 | mddev->new_layout != mddev->layout || | |
2201 | mddev->new_chunk != mddev->chunk_size) { | |
2202 | printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n", | |
2203 | mdname(mddev)); | |
2204 | return -EINVAL; | |
2205 | } | |
2206 | if (mddev->delta_disks <= 0) { | |
2207 | printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n", | |
2208 | mdname(mddev)); | |
2209 | return -EINVAL; | |
2210 | } | |
2211 | old_disks = mddev->raid_disks - mddev->delta_disks; | |
2212 | /* reshape_position must be on a new-stripe boundary, and one | |
2213 | * further up in new geometry must map after here in old geometry. | |
2214 | */ | |
2215 | here_new = mddev->reshape_position; | |
2216 | if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) { | |
2217 | printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n"); | |
2218 | return -EINVAL; | |
2219 | } | |
2220 | /* here_new is the stripe we will write to */ | |
2221 | here_old = mddev->reshape_position; | |
2222 | sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1)); | |
2223 | /* here_old is the first stripe that we might need to read from */ | |
2224 | if (here_new >= here_old) { | |
2225 | /* Reading from the same stripe as writing to - bad */ | |
2226 | printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n"); | |
2227 | return -EINVAL; | |
2228 | } | |
2229 | printk(KERN_INFO "raid5: reshape will continue\n"); | |
2230 | /* OK, we should be able to continue; */ | |
2231 | } | |
2232 | ||
2233 | ||
b55e6bfc | 2234 | mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL); |
1da177e4 LT |
2235 | if ((conf = mddev->private) == NULL) |
2236 | goto abort; | |
f6705578 N |
2237 | if (mddev->reshape_position == MaxSector) { |
2238 | conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks; | |
2239 | } else { | |
2240 | conf->raid_disks = mddev->raid_disks; | |
2241 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; | |
2242 | } | |
2243 | ||
2244 | conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info), | |
b55e6bfc N |
2245 | GFP_KERNEL); |
2246 | if (!conf->disks) | |
2247 | goto abort; | |
9ffae0cf | 2248 | |
1da177e4 LT |
2249 | conf->mddev = mddev; |
2250 | ||
fccddba0 | 2251 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
1da177e4 | 2252 | goto abort; |
1da177e4 LT |
2253 | |
2254 | spin_lock_init(&conf->device_lock); | |
2255 | init_waitqueue_head(&conf->wait_for_stripe); | |
2256 | init_waitqueue_head(&conf->wait_for_overlap); | |
2257 | INIT_LIST_HEAD(&conf->handle_list); | |
2258 | INIT_LIST_HEAD(&conf->delayed_list); | |
72626685 | 2259 | INIT_LIST_HEAD(&conf->bitmap_list); |
1da177e4 LT |
2260 | INIT_LIST_HEAD(&conf->inactive_list); |
2261 | atomic_set(&conf->active_stripes, 0); | |
2262 | atomic_set(&conf->preread_active_stripes, 0); | |
2263 | ||
1da177e4 LT |
2264 | PRINTK("raid5: run(%s) called.\n", mdname(mddev)); |
2265 | ||
2266 | ITERATE_RDEV(mddev,rdev,tmp) { | |
2267 | raid_disk = rdev->raid_disk; | |
f6705578 | 2268 | if (raid_disk >= conf->raid_disks |
1da177e4 LT |
2269 | || raid_disk < 0) |
2270 | continue; | |
2271 | disk = conf->disks + raid_disk; | |
2272 | ||
2273 | disk->rdev = rdev; | |
2274 | ||
b2d444d7 | 2275 | if (test_bit(In_sync, &rdev->flags)) { |
1da177e4 LT |
2276 | char b[BDEVNAME_SIZE]; |
2277 | printk(KERN_INFO "raid5: device %s operational as raid" | |
2278 | " disk %d\n", bdevname(rdev->bdev,b), | |
2279 | raid_disk); | |
2280 | conf->working_disks++; | |
2281 | } | |
2282 | } | |
2283 | ||
1da177e4 LT |
2284 | /* |
2285 | * 0 for a fully functional array, 1 for a degraded array. | |
2286 | */ | |
2287 | mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks; | |
2288 | conf->mddev = mddev; | |
2289 | conf->chunk_size = mddev->chunk_size; | |
2290 | conf->level = mddev->level; | |
2291 | conf->algorithm = mddev->layout; | |
2292 | conf->max_nr_stripes = NR_STRIPES; | |
f6705578 | 2293 | conf->expand_progress = mddev->reshape_position; |
1da177e4 LT |
2294 | |
2295 | /* device size must be a multiple of chunk size */ | |
2296 | mddev->size &= ~(mddev->chunk_size/1024 -1); | |
b1581566 | 2297 | mddev->resync_max_sectors = mddev->size << 1; |
1da177e4 LT |
2298 | |
2299 | if (!conf->chunk_size || conf->chunk_size % 4) { | |
2300 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", | |
2301 | conf->chunk_size, mdname(mddev)); | |
2302 | goto abort; | |
2303 | } | |
2304 | if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) { | |
2305 | printk(KERN_ERR | |
2306 | "raid5: unsupported parity algorithm %d for %s\n", | |
2307 | conf->algorithm, mdname(mddev)); | |
2308 | goto abort; | |
2309 | } | |
2310 | if (mddev->degraded > 1) { | |
2311 | printk(KERN_ERR "raid5: not enough operational devices for %s" | |
2312 | " (%d/%d failed)\n", | |
2313 | mdname(mddev), conf->failed_disks, conf->raid_disks); | |
2314 | goto abort; | |
2315 | } | |
2316 | ||
2317 | if (mddev->degraded == 1 && | |
2318 | mddev->recovery_cp != MaxSector) { | |
6ff8d8ec N |
2319 | if (mddev->ok_start_degraded) |
2320 | printk(KERN_WARNING | |
2321 | "raid5: starting dirty degraded array: %s" | |
2322 | "- data corruption possible.\n", | |
2323 | mdname(mddev)); | |
2324 | else { | |
2325 | printk(KERN_ERR | |
2326 | "raid5: cannot start dirty degraded array for %s\n", | |
2327 | mdname(mddev)); | |
2328 | goto abort; | |
2329 | } | |
1da177e4 LT |
2330 | } |
2331 | ||
2332 | { | |
2333 | mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5"); | |
2334 | if (!mddev->thread) { | |
2335 | printk(KERN_ERR | |
2336 | "raid5: couldn't allocate thread for %s\n", | |
2337 | mdname(mddev)); | |
2338 | goto abort; | |
2339 | } | |
2340 | } | |
5036805b | 2341 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
1da177e4 LT |
2342 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
2343 | if (grow_stripes(conf, conf->max_nr_stripes)) { | |
2344 | printk(KERN_ERR | |
2345 | "raid5: couldn't allocate %dkB for buffers\n", memory); | |
2346 | shrink_stripes(conf); | |
2347 | md_unregister_thread(mddev->thread); | |
2348 | goto abort; | |
2349 | } else | |
2350 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", | |
2351 | memory, mdname(mddev)); | |
2352 | ||
2353 | if (mddev->degraded == 0) | |
2354 | printk("raid5: raid level %d set %s active with %d out of %d" | |
2355 | " devices, algorithm %d\n", conf->level, mdname(mddev), | |
2356 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, | |
2357 | conf->algorithm); | |
2358 | else | |
2359 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" | |
2360 | " out of %d devices, algorithm %d\n", conf->level, | |
2361 | mdname(mddev), mddev->raid_disks - mddev->degraded, | |
2362 | mddev->raid_disks, conf->algorithm); | |
2363 | ||
2364 | print_raid5_conf(conf); | |
2365 | ||
f6705578 N |
2366 | if (conf->expand_progress != MaxSector) { |
2367 | printk("...ok start reshape thread\n"); | |
b578d55f | 2368 | conf->expand_lo = conf->expand_progress; |
f6705578 N |
2369 | atomic_set(&conf->reshape_stripes, 0); |
2370 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); | |
2371 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
2372 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
2373 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
2374 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
2375 | "%s_reshape"); | |
2376 | /* FIXME if md_register_thread fails?? */ | |
2377 | md_wakeup_thread(mddev->sync_thread); | |
2378 | ||
2379 | } | |
2380 | ||
1da177e4 LT |
2381 | /* read-ahead size must cover two whole stripes, which is |
2382 | * 2 * (n-1) * chunksize where 'n' is the number of raid devices | |
2383 | */ | |
2384 | { | |
2385 | int stripe = (mddev->raid_disks-1) * mddev->chunk_size | |
2d1f3b5d | 2386 | / PAGE_SIZE; |
1da177e4 LT |
2387 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
2388 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
2389 | } | |
2390 | ||
2391 | /* Ok, everything is just fine now */ | |
007583c9 | 2392 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group); |
7a5febe9 N |
2393 | |
2394 | mddev->queue->unplug_fn = raid5_unplug_device; | |
2395 | mddev->queue->issue_flush_fn = raid5_issue_flush; | |
f6705578 | 2396 | mddev->array_size = mddev->size * (conf->previous_raid_disks - 1); |
7a5febe9 | 2397 | |
1da177e4 LT |
2398 | return 0; |
2399 | abort: | |
2400 | if (conf) { | |
2401 | print_raid5_conf(conf); | |
b55e6bfc | 2402 | kfree(conf->disks); |
fccddba0 | 2403 | kfree(conf->stripe_hashtbl); |
1da177e4 LT |
2404 | kfree(conf); |
2405 | } | |
2406 | mddev->private = NULL; | |
2407 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); | |
2408 | return -EIO; | |
2409 | } | |
2410 | ||
2411 | ||
2412 | ||
3f294f4f | 2413 | static int stop(mddev_t *mddev) |
1da177e4 LT |
2414 | { |
2415 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
2416 | ||
2417 | md_unregister_thread(mddev->thread); | |
2418 | mddev->thread = NULL; | |
2419 | shrink_stripes(conf); | |
fccddba0 | 2420 | kfree(conf->stripe_hashtbl); |
1da177e4 | 2421 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
007583c9 | 2422 | sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); |
b55e6bfc | 2423 | kfree(conf->disks); |
96de1e66 | 2424 | kfree(conf); |
1da177e4 LT |
2425 | mddev->private = NULL; |
2426 | return 0; | |
2427 | } | |
2428 | ||
2429 | #if RAID5_DEBUG | |
2430 | static void print_sh (struct stripe_head *sh) | |
2431 | { | |
2432 | int i; | |
2433 | ||
2434 | printk("sh %llu, pd_idx %d, state %ld.\n", | |
2435 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); | |
2436 | printk("sh %llu, count %d.\n", | |
2437 | (unsigned long long)sh->sector, atomic_read(&sh->count)); | |
2438 | printk("sh %llu, ", (unsigned long long)sh->sector); | |
7ecaa1e6 | 2439 | for (i = 0; i < sh->disks; i++) { |
1da177e4 LT |
2440 | printk("(cache%d: %p %ld) ", |
2441 | i, sh->dev[i].page, sh->dev[i].flags); | |
2442 | } | |
2443 | printk("\n"); | |
2444 | } | |
2445 | ||
2446 | static void printall (raid5_conf_t *conf) | |
2447 | { | |
2448 | struct stripe_head *sh; | |
fccddba0 | 2449 | struct hlist_node *hn; |
1da177e4 LT |
2450 | int i; |
2451 | ||
2452 | spin_lock_irq(&conf->device_lock); | |
2453 | for (i = 0; i < NR_HASH; i++) { | |
fccddba0 | 2454 | hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) { |
1da177e4 LT |
2455 | if (sh->raid_conf != conf) |
2456 | continue; | |
2457 | print_sh(sh); | |
2458 | } | |
2459 | } | |
2460 | spin_unlock_irq(&conf->device_lock); | |
2461 | } | |
2462 | #endif | |
2463 | ||
2464 | static void status (struct seq_file *seq, mddev_t *mddev) | |
2465 | { | |
2466 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
2467 | int i; | |
2468 | ||
2469 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); | |
2470 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks); | |
2471 | for (i = 0; i < conf->raid_disks; i++) | |
2472 | seq_printf (seq, "%s", | |
2473 | conf->disks[i].rdev && | |
b2d444d7 | 2474 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
1da177e4 LT |
2475 | seq_printf (seq, "]"); |
2476 | #if RAID5_DEBUG | |
2477 | #define D(x) \ | |
2478 | seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x)) | |
2479 | printall(conf); | |
2480 | #endif | |
2481 | } | |
2482 | ||
2483 | static void print_raid5_conf (raid5_conf_t *conf) | |
2484 | { | |
2485 | int i; | |
2486 | struct disk_info *tmp; | |
2487 | ||
2488 | printk("RAID5 conf printout:\n"); | |
2489 | if (!conf) { | |
2490 | printk("(conf==NULL)\n"); | |
2491 | return; | |
2492 | } | |
2493 | printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks, | |
2494 | conf->working_disks, conf->failed_disks); | |
2495 | ||
2496 | for (i = 0; i < conf->raid_disks; i++) { | |
2497 | char b[BDEVNAME_SIZE]; | |
2498 | tmp = conf->disks + i; | |
2499 | if (tmp->rdev) | |
2500 | printk(" disk %d, o:%d, dev:%s\n", | |
b2d444d7 | 2501 | i, !test_bit(Faulty, &tmp->rdev->flags), |
1da177e4 LT |
2502 | bdevname(tmp->rdev->bdev,b)); |
2503 | } | |
2504 | } | |
2505 | ||
2506 | static int raid5_spare_active(mddev_t *mddev) | |
2507 | { | |
2508 | int i; | |
2509 | raid5_conf_t *conf = mddev->private; | |
2510 | struct disk_info *tmp; | |
2511 | ||
2512 | for (i = 0; i < conf->raid_disks; i++) { | |
2513 | tmp = conf->disks + i; | |
2514 | if (tmp->rdev | |
b2d444d7 N |
2515 | && !test_bit(Faulty, &tmp->rdev->flags) |
2516 | && !test_bit(In_sync, &tmp->rdev->flags)) { | |
1da177e4 LT |
2517 | mddev->degraded--; |
2518 | conf->failed_disks--; | |
2519 | conf->working_disks++; | |
b2d444d7 | 2520 | set_bit(In_sync, &tmp->rdev->flags); |
1da177e4 LT |
2521 | } |
2522 | } | |
2523 | print_raid5_conf(conf); | |
2524 | return 0; | |
2525 | } | |
2526 | ||
2527 | static int raid5_remove_disk(mddev_t *mddev, int number) | |
2528 | { | |
2529 | raid5_conf_t *conf = mddev->private; | |
2530 | int err = 0; | |
2531 | mdk_rdev_t *rdev; | |
2532 | struct disk_info *p = conf->disks + number; | |
2533 | ||
2534 | print_raid5_conf(conf); | |
2535 | rdev = p->rdev; | |
2536 | if (rdev) { | |
b2d444d7 | 2537 | if (test_bit(In_sync, &rdev->flags) || |
1da177e4 LT |
2538 | atomic_read(&rdev->nr_pending)) { |
2539 | err = -EBUSY; | |
2540 | goto abort; | |
2541 | } | |
2542 | p->rdev = NULL; | |
fbd568a3 | 2543 | synchronize_rcu(); |
1da177e4 LT |
2544 | if (atomic_read(&rdev->nr_pending)) { |
2545 | /* lost the race, try later */ | |
2546 | err = -EBUSY; | |
2547 | p->rdev = rdev; | |
2548 | } | |
2549 | } | |
2550 | abort: | |
2551 | ||
2552 | print_raid5_conf(conf); | |
2553 | return err; | |
2554 | } | |
2555 | ||
2556 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) | |
2557 | { | |
2558 | raid5_conf_t *conf = mddev->private; | |
2559 | int found = 0; | |
2560 | int disk; | |
2561 | struct disk_info *p; | |
2562 | ||
2563 | if (mddev->degraded > 1) | |
2564 | /* no point adding a device */ | |
2565 | return 0; | |
2566 | ||
2567 | /* | |
2568 | * find the disk ... | |
2569 | */ | |
f6705578 | 2570 | for (disk=0; disk < conf->raid_disks; disk++) |
1da177e4 | 2571 | if ((p=conf->disks + disk)->rdev == NULL) { |
b2d444d7 | 2572 | clear_bit(In_sync, &rdev->flags); |
1da177e4 LT |
2573 | rdev->raid_disk = disk; |
2574 | found = 1; | |
72626685 N |
2575 | if (rdev->saved_raid_disk != disk) |
2576 | conf->fullsync = 1; | |
d6065f7b | 2577 | rcu_assign_pointer(p->rdev, rdev); |
1da177e4 LT |
2578 | break; |
2579 | } | |
2580 | print_raid5_conf(conf); | |
2581 | return found; | |
2582 | } | |
2583 | ||
2584 | static int raid5_resize(mddev_t *mddev, sector_t sectors) | |
2585 | { | |
2586 | /* no resync is happening, and there is enough space | |
2587 | * on all devices, so we can resize. | |
2588 | * We need to make sure resync covers any new space. | |
2589 | * If the array is shrinking we should possibly wait until | |
2590 | * any io in the removed space completes, but it hardly seems | |
2591 | * worth it. | |
2592 | */ | |
2593 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); | |
2594 | mddev->array_size = (sectors * (mddev->raid_disks-1))>>1; | |
2595 | set_capacity(mddev->gendisk, mddev->array_size << 1); | |
2596 | mddev->changed = 1; | |
2597 | if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) { | |
2598 | mddev->recovery_cp = mddev->size << 1; | |
2599 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
2600 | } | |
2601 | mddev->size = sectors /2; | |
4b5c7ae8 | 2602 | mddev->resync_max_sectors = sectors; |
1da177e4 LT |
2603 | return 0; |
2604 | } | |
2605 | ||
29269553 | 2606 | #ifdef CONFIG_MD_RAID5_RESHAPE |
63c70c4f | 2607 | static int raid5_check_reshape(mddev_t *mddev) |
29269553 N |
2608 | { |
2609 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2610 | int err; | |
29269553 | 2611 | |
63c70c4f N |
2612 | if (mddev->delta_disks < 0 || |
2613 | mddev->new_level != mddev->level) | |
2614 | return -EINVAL; /* Cannot shrink array or change level yet */ | |
2615 | if (mddev->delta_disks == 0) | |
29269553 N |
2616 | return 0; /* nothing to do */ |
2617 | ||
2618 | /* Can only proceed if there are plenty of stripe_heads. | |
2619 | * We need a minimum of one full stripe,, and for sensible progress | |
2620 | * it is best to have about 4 times that. | |
2621 | * If we require 4 times, then the default 256 4K stripe_heads will | |
2622 | * allow for chunk sizes up to 256K, which is probably OK. | |
2623 | * If the chunk size is greater, user-space should request more | |
2624 | * stripe_heads first. | |
2625 | */ | |
63c70c4f N |
2626 | if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes || |
2627 | (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) { | |
29269553 N |
2628 | printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n", |
2629 | (mddev->chunk_size / STRIPE_SIZE)*4); | |
2630 | return -ENOSPC; | |
2631 | } | |
2632 | ||
63c70c4f N |
2633 | err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks); |
2634 | if (err) | |
2635 | return err; | |
2636 | ||
2637 | /* looks like we might be able to manage this */ | |
2638 | return 0; | |
2639 | } | |
2640 | ||
2641 | static int raid5_start_reshape(mddev_t *mddev) | |
2642 | { | |
2643 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2644 | mdk_rdev_t *rdev; | |
2645 | struct list_head *rtmp; | |
2646 | int spares = 0; | |
2647 | int added_devices = 0; | |
2648 | ||
2649 | if (mddev->degraded || | |
2650 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) | |
2651 | return -EBUSY; | |
2652 | ||
29269553 N |
2653 | ITERATE_RDEV(mddev, rdev, rtmp) |
2654 | if (rdev->raid_disk < 0 && | |
2655 | !test_bit(Faulty, &rdev->flags)) | |
2656 | spares++; | |
63c70c4f N |
2657 | |
2658 | if (spares < mddev->delta_disks-1) | |
29269553 N |
2659 | /* Not enough devices even to make a degraded array |
2660 | * of that size | |
2661 | */ | |
2662 | return -EINVAL; | |
2663 | ||
f6705578 | 2664 | atomic_set(&conf->reshape_stripes, 0); |
29269553 N |
2665 | spin_lock_irq(&conf->device_lock); |
2666 | conf->previous_raid_disks = conf->raid_disks; | |
63c70c4f | 2667 | conf->raid_disks += mddev->delta_disks; |
29269553 | 2668 | conf->expand_progress = 0; |
b578d55f | 2669 | conf->expand_lo = 0; |
29269553 N |
2670 | spin_unlock_irq(&conf->device_lock); |
2671 | ||
2672 | /* Add some new drives, as many as will fit. | |
2673 | * We know there are enough to make the newly sized array work. | |
2674 | */ | |
2675 | ITERATE_RDEV(mddev, rdev, rtmp) | |
2676 | if (rdev->raid_disk < 0 && | |
2677 | !test_bit(Faulty, &rdev->flags)) { | |
2678 | if (raid5_add_disk(mddev, rdev)) { | |
2679 | char nm[20]; | |
2680 | set_bit(In_sync, &rdev->flags); | |
2681 | conf->working_disks++; | |
2682 | added_devices++; | |
2683 | sprintf(nm, "rd%d", rdev->raid_disk); | |
2684 | sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); | |
2685 | } else | |
2686 | break; | |
2687 | } | |
2688 | ||
63c70c4f N |
2689 | mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices; |
2690 | mddev->raid_disks = conf->raid_disks; | |
f6705578 N |
2691 | mddev->reshape_position = 0; |
2692 | mddev->sb_dirty = 1; | |
2693 | ||
29269553 N |
2694 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
2695 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); | |
2696 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); | |
2697 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); | |
2698 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, | |
2699 | "%s_reshape"); | |
2700 | if (!mddev->sync_thread) { | |
2701 | mddev->recovery = 0; | |
2702 | spin_lock_irq(&conf->device_lock); | |
2703 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; | |
2704 | conf->expand_progress = MaxSector; | |
2705 | spin_unlock_irq(&conf->device_lock); | |
2706 | return -EAGAIN; | |
2707 | } | |
2708 | md_wakeup_thread(mddev->sync_thread); | |
2709 | md_new_event(mddev); | |
2710 | return 0; | |
2711 | } | |
2712 | #endif | |
2713 | ||
2714 | static void end_reshape(raid5_conf_t *conf) | |
2715 | { | |
2716 | struct block_device *bdev; | |
2717 | ||
f6705578 N |
2718 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
2719 | conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1); | |
2720 | set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1); | |
2721 | conf->mddev->changed = 1; | |
2722 | ||
2723 | bdev = bdget_disk(conf->mddev->gendisk, 0); | |
2724 | if (bdev) { | |
2725 | mutex_lock(&bdev->bd_inode->i_mutex); | |
2726 | i_size_write(bdev->bd_inode, conf->mddev->array_size << 10); | |
2727 | mutex_unlock(&bdev->bd_inode->i_mutex); | |
2728 | bdput(bdev); | |
2729 | } | |
2730 | spin_lock_irq(&conf->device_lock); | |
2731 | conf->expand_progress = MaxSector; | |
2732 | spin_unlock_irq(&conf->device_lock); | |
2733 | conf->mddev->reshape_position = MaxSector; | |
29269553 | 2734 | } |
29269553 N |
2735 | } |
2736 | ||
72626685 N |
2737 | static void raid5_quiesce(mddev_t *mddev, int state) |
2738 | { | |
2739 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
2740 | ||
2741 | switch(state) { | |
e464eafd N |
2742 | case 2: /* resume for a suspend */ |
2743 | wake_up(&conf->wait_for_overlap); | |
2744 | break; | |
2745 | ||
72626685 N |
2746 | case 1: /* stop all writes */ |
2747 | spin_lock_irq(&conf->device_lock); | |
2748 | conf->quiesce = 1; | |
2749 | wait_event_lock_irq(conf->wait_for_stripe, | |
2750 | atomic_read(&conf->active_stripes) == 0, | |
2751 | conf->device_lock, /* nothing */); | |
2752 | spin_unlock_irq(&conf->device_lock); | |
2753 | break; | |
2754 | ||
2755 | case 0: /* re-enable writes */ | |
2756 | spin_lock_irq(&conf->device_lock); | |
2757 | conf->quiesce = 0; | |
2758 | wake_up(&conf->wait_for_stripe); | |
e464eafd | 2759 | wake_up(&conf->wait_for_overlap); |
72626685 N |
2760 | spin_unlock_irq(&conf->device_lock); |
2761 | break; | |
2762 | } | |
72626685 | 2763 | } |
b15c2e57 | 2764 | |
2604b703 | 2765 | static struct mdk_personality raid5_personality = |
1da177e4 LT |
2766 | { |
2767 | .name = "raid5", | |
2604b703 | 2768 | .level = 5, |
1da177e4 LT |
2769 | .owner = THIS_MODULE, |
2770 | .make_request = make_request, | |
2771 | .run = run, | |
2772 | .stop = stop, | |
2773 | .status = status, | |
2774 | .error_handler = error, | |
2775 | .hot_add_disk = raid5_add_disk, | |
2776 | .hot_remove_disk= raid5_remove_disk, | |
2777 | .spare_active = raid5_spare_active, | |
2778 | .sync_request = sync_request, | |
2779 | .resize = raid5_resize, | |
29269553 | 2780 | #ifdef CONFIG_MD_RAID5_RESHAPE |
63c70c4f N |
2781 | .check_reshape = raid5_check_reshape, |
2782 | .start_reshape = raid5_start_reshape, | |
29269553 | 2783 | #endif |
72626685 | 2784 | .quiesce = raid5_quiesce, |
1da177e4 LT |
2785 | }; |
2786 | ||
2604b703 | 2787 | static struct mdk_personality raid4_personality = |
1da177e4 | 2788 | { |
2604b703 N |
2789 | .name = "raid4", |
2790 | .level = 4, | |
2791 | .owner = THIS_MODULE, | |
2792 | .make_request = make_request, | |
2793 | .run = run, | |
2794 | .stop = stop, | |
2795 | .status = status, | |
2796 | .error_handler = error, | |
2797 | .hot_add_disk = raid5_add_disk, | |
2798 | .hot_remove_disk= raid5_remove_disk, | |
2799 | .spare_active = raid5_spare_active, | |
2800 | .sync_request = sync_request, | |
2801 | .resize = raid5_resize, | |
2802 | .quiesce = raid5_quiesce, | |
2803 | }; | |
2804 | ||
2805 | static int __init raid5_init(void) | |
2806 | { | |
2807 | register_md_personality(&raid5_personality); | |
2808 | register_md_personality(&raid4_personality); | |
2809 | return 0; | |
1da177e4 LT |
2810 | } |
2811 | ||
2604b703 | 2812 | static void raid5_exit(void) |
1da177e4 | 2813 | { |
2604b703 N |
2814 | unregister_md_personality(&raid5_personality); |
2815 | unregister_md_personality(&raid4_personality); | |
1da177e4 LT |
2816 | } |
2817 | ||
2818 | module_init(raid5_init); | |
2819 | module_exit(raid5_exit); | |
2820 | MODULE_LICENSE("GPL"); | |
2821 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ | |
d9d166c2 N |
2822 | MODULE_ALIAS("md-raid5"); |
2823 | MODULE_ALIAS("md-raid4"); | |
2604b703 N |
2824 | MODULE_ALIAS("md-level-5"); |
2825 | MODULE_ALIAS("md-level-4"); |