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