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 | ||
27 | /* | |
28 | * Stripe cache | |
29 | */ | |
30 | ||
31 | #define NR_STRIPES 256 | |
32 | #define STRIPE_SIZE PAGE_SIZE | |
33 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) | |
34 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) | |
35 | #define IO_THRESHOLD 1 | |
36 | #define HASH_PAGES 1 | |
37 | #define HASH_PAGES_ORDER 0 | |
38 | #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *)) | |
39 | #define HASH_MASK (NR_HASH - 1) | |
40 | ||
41 | #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]) | |
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 | ||
72 | static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) | |
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); | |
82 | else | |
83 | list_add_tail(&sh->lru, &conf->handle_list); | |
84 | md_wakeup_thread(conf->mddev->thread); | |
85 | } else { | |
86 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
87 | atomic_dec(&conf->preread_active_stripes); | |
88 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) | |
89 | md_wakeup_thread(conf->mddev->thread); | |
90 | } | |
91 | list_add_tail(&sh->lru, &conf->inactive_list); | |
92 | atomic_dec(&conf->active_stripes); | |
93 | if (!conf->inactive_blocked || | |
94 | atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4)) | |
95 | wake_up(&conf->wait_for_stripe); | |
96 | } | |
97 | } | |
98 | } | |
99 | static void release_stripe(struct stripe_head *sh) | |
100 | { | |
101 | raid5_conf_t *conf = sh->raid_conf; | |
102 | unsigned long flags; | |
103 | ||
104 | spin_lock_irqsave(&conf->device_lock, flags); | |
105 | __release_stripe(conf, sh); | |
106 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
107 | } | |
108 | ||
109 | static void remove_hash(struct stripe_head *sh) | |
110 | { | |
111 | PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector); | |
112 | ||
113 | if (sh->hash_pprev) { | |
114 | if (sh->hash_next) | |
115 | sh->hash_next->hash_pprev = sh->hash_pprev; | |
116 | *sh->hash_pprev = sh->hash_next; | |
117 | sh->hash_pprev = NULL; | |
118 | } | |
119 | } | |
120 | ||
121 | static __inline__ void insert_hash(raid5_conf_t *conf, struct stripe_head *sh) | |
122 | { | |
123 | struct stripe_head **shp = &stripe_hash(conf, sh->sector); | |
124 | ||
125 | PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector); | |
126 | ||
127 | CHECK_DEVLOCK(); | |
128 | if ((sh->hash_next = *shp) != NULL) | |
129 | (*shp)->hash_pprev = &sh->hash_next; | |
130 | *shp = sh; | |
131 | sh->hash_pprev = shp; | |
132 | } | |
133 | ||
134 | ||
135 | /* find an idle stripe, make sure it is unhashed, and return it. */ | |
136 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) | |
137 | { | |
138 | struct stripe_head *sh = NULL; | |
139 | struct list_head *first; | |
140 | ||
141 | CHECK_DEVLOCK(); | |
142 | if (list_empty(&conf->inactive_list)) | |
143 | goto out; | |
144 | first = conf->inactive_list.next; | |
145 | sh = list_entry(first, struct stripe_head, lru); | |
146 | list_del_init(first); | |
147 | remove_hash(sh); | |
148 | atomic_inc(&conf->active_stripes); | |
149 | out: | |
150 | return sh; | |
151 | } | |
152 | ||
153 | static void shrink_buffers(struct stripe_head *sh, int num) | |
154 | { | |
155 | struct page *p; | |
156 | int i; | |
157 | ||
158 | for (i=0; i<num ; i++) { | |
159 | p = sh->dev[i].page; | |
160 | if (!p) | |
161 | continue; | |
162 | sh->dev[i].page = NULL; | |
163 | page_cache_release(p); | |
164 | } | |
165 | } | |
166 | ||
167 | static int grow_buffers(struct stripe_head *sh, int num) | |
168 | { | |
169 | int i; | |
170 | ||
171 | for (i=0; i<num; i++) { | |
172 | struct page *page; | |
173 | ||
174 | if (!(page = alloc_page(GFP_KERNEL))) { | |
175 | return 1; | |
176 | } | |
177 | sh->dev[i].page = page; | |
178 | } | |
179 | return 0; | |
180 | } | |
181 | ||
182 | static void raid5_build_block (struct stripe_head *sh, int i); | |
183 | ||
184 | static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx) | |
185 | { | |
186 | raid5_conf_t *conf = sh->raid_conf; | |
187 | int disks = conf->raid_disks, i; | |
188 | ||
189 | if (atomic_read(&sh->count) != 0) | |
190 | BUG(); | |
191 | if (test_bit(STRIPE_HANDLE, &sh->state)) | |
192 | BUG(); | |
193 | ||
194 | CHECK_DEVLOCK(); | |
195 | PRINTK("init_stripe called, stripe %llu\n", | |
196 | (unsigned long long)sh->sector); | |
197 | ||
198 | remove_hash(sh); | |
199 | ||
200 | sh->sector = sector; | |
201 | sh->pd_idx = pd_idx; | |
202 | sh->state = 0; | |
203 | ||
204 | for (i=disks; i--; ) { | |
205 | struct r5dev *dev = &sh->dev[i]; | |
206 | ||
207 | if (dev->toread || dev->towrite || dev->written || | |
208 | test_bit(R5_LOCKED, &dev->flags)) { | |
209 | printk("sector=%llx i=%d %p %p %p %d\n", | |
210 | (unsigned long long)sh->sector, i, dev->toread, | |
211 | dev->towrite, dev->written, | |
212 | test_bit(R5_LOCKED, &dev->flags)); | |
213 | BUG(); | |
214 | } | |
215 | dev->flags = 0; | |
216 | raid5_build_block(sh, i); | |
217 | } | |
218 | insert_hash(conf, sh); | |
219 | } | |
220 | ||
221 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector) | |
222 | { | |
223 | struct stripe_head *sh; | |
224 | ||
225 | CHECK_DEVLOCK(); | |
226 | PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector); | |
227 | for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next) | |
228 | if (sh->sector == sector) | |
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 | ||
237 | static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, | |
238 | int pd_idx, int noblock) | |
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 { | |
247 | sh = __find_stripe(conf, sector); | |
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) && | |
257 | (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4) | |
258 | || !conf->inactive_blocked), | |
259 | conf->device_lock, | |
260 | unplug_slaves(conf->mddev); | |
261 | ); | |
262 | conf->inactive_blocked = 0; | |
263 | } else | |
264 | init_stripe(sh, sector, pd_idx); | |
265 | } else { | |
266 | if (atomic_read(&sh->count)) { | |
267 | if (!list_empty(&sh->lru)) | |
268 | BUG(); | |
269 | } else { | |
270 | if (!test_bit(STRIPE_HANDLE, &sh->state)) | |
271 | atomic_inc(&conf->active_stripes); | |
272 | if (list_empty(&sh->lru)) | |
273 | BUG(); | |
274 | list_del_init(&sh->lru); | |
275 | } | |
276 | } | |
277 | } while (sh == NULL); | |
278 | ||
279 | if (sh) | |
280 | atomic_inc(&sh->count); | |
281 | ||
282 | spin_unlock_irq(&conf->device_lock); | |
283 | return sh; | |
284 | } | |
285 | ||
286 | static int grow_stripes(raid5_conf_t *conf, int num) | |
287 | { | |
288 | struct stripe_head *sh; | |
289 | kmem_cache_t *sc; | |
290 | int devs = conf->raid_disks; | |
291 | ||
292 | sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev)); | |
293 | ||
294 | sc = kmem_cache_create(conf->cache_name, | |
295 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), | |
296 | 0, 0, NULL, NULL); | |
297 | if (!sc) | |
298 | return 1; | |
299 | conf->slab_cache = sc; | |
300 | while (num--) { | |
301 | sh = kmem_cache_alloc(sc, GFP_KERNEL); | |
302 | if (!sh) | |
303 | return 1; | |
304 | memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev)); | |
305 | sh->raid_conf = conf; | |
306 | spin_lock_init(&sh->lock); | |
307 | ||
308 | if (grow_buffers(sh, conf->raid_disks)) { | |
309 | shrink_buffers(sh, conf->raid_disks); | |
310 | kmem_cache_free(sc, sh); | |
311 | return 1; | |
312 | } | |
313 | /* we just created an active stripe so... */ | |
314 | atomic_set(&sh->count, 1); | |
315 | atomic_inc(&conf->active_stripes); | |
316 | INIT_LIST_HEAD(&sh->lru); | |
317 | release_stripe(sh); | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
322 | static void shrink_stripes(raid5_conf_t *conf) | |
323 | { | |
324 | struct stripe_head *sh; | |
325 | ||
326 | while (1) { | |
327 | spin_lock_irq(&conf->device_lock); | |
328 | sh = get_free_stripe(conf); | |
329 | spin_unlock_irq(&conf->device_lock); | |
330 | if (!sh) | |
331 | break; | |
332 | if (atomic_read(&sh->count)) | |
333 | BUG(); | |
334 | shrink_buffers(sh, conf->raid_disks); | |
335 | kmem_cache_free(conf->slab_cache, sh); | |
336 | atomic_dec(&conf->active_stripes); | |
337 | } | |
338 | kmem_cache_destroy(conf->slab_cache); | |
339 | conf->slab_cache = NULL; | |
340 | } | |
341 | ||
342 | static int raid5_end_read_request (struct bio * bi, unsigned int bytes_done, | |
343 | int error) | |
344 | { | |
345 | struct stripe_head *sh = bi->bi_private; | |
346 | raid5_conf_t *conf = sh->raid_conf; | |
347 | int disks = conf->raid_disks, i; | |
348 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); | |
349 | ||
350 | if (bi->bi_size) | |
351 | return 1; | |
352 | ||
353 | for (i=0 ; i<disks; i++) | |
354 | if (bi == &sh->dev[i].req) | |
355 | break; | |
356 | ||
357 | PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n", | |
358 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), | |
359 | uptodate); | |
360 | if (i == disks) { | |
361 | BUG(); | |
362 | return 0; | |
363 | } | |
364 | ||
365 | if (uptodate) { | |
366 | #if 0 | |
367 | struct bio *bio; | |
368 | unsigned long flags; | |
369 | spin_lock_irqsave(&conf->device_lock, flags); | |
370 | /* we can return a buffer if we bypassed the cache or | |
371 | * if the top buffer is not in highmem. If there are | |
372 | * multiple buffers, leave the extra work to | |
373 | * handle_stripe | |
374 | */ | |
375 | buffer = sh->bh_read[i]; | |
376 | if (buffer && | |
377 | (!PageHighMem(buffer->b_page) | |
378 | || buffer->b_page == bh->b_page ) | |
379 | ) { | |
380 | sh->bh_read[i] = buffer->b_reqnext; | |
381 | buffer->b_reqnext = NULL; | |
382 | } else | |
383 | buffer = NULL; | |
384 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
385 | if (sh->bh_page[i]==bh->b_page) | |
386 | set_buffer_uptodate(bh); | |
387 | if (buffer) { | |
388 | if (buffer->b_page != bh->b_page) | |
389 | memcpy(buffer->b_data, bh->b_data, bh->b_size); | |
390 | buffer->b_end_io(buffer, 1); | |
391 | } | |
392 | #else | |
393 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
394 | #endif | |
395 | } else { | |
396 | md_error(conf->mddev, conf->disks[i].rdev); | |
397 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); | |
398 | } | |
399 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); | |
400 | #if 0 | |
401 | /* must restore b_page before unlocking buffer... */ | |
402 | if (sh->bh_page[i] != bh->b_page) { | |
403 | bh->b_page = sh->bh_page[i]; | |
404 | bh->b_data = page_address(bh->b_page); | |
405 | clear_buffer_uptodate(bh); | |
406 | } | |
407 | #endif | |
408 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
409 | set_bit(STRIPE_HANDLE, &sh->state); | |
410 | release_stripe(sh); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done, | |
415 | int error) | |
416 | { | |
417 | struct stripe_head *sh = bi->bi_private; | |
418 | raid5_conf_t *conf = sh->raid_conf; | |
419 | int disks = conf->raid_disks, i; | |
420 | unsigned long flags; | |
421 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); | |
422 | ||
423 | if (bi->bi_size) | |
424 | return 1; | |
425 | ||
426 | for (i=0 ; i<disks; i++) | |
427 | if (bi == &sh->dev[i].req) | |
428 | break; | |
429 | ||
430 | PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n", | |
431 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), | |
432 | uptodate); | |
433 | if (i == disks) { | |
434 | BUG(); | |
435 | return 0; | |
436 | } | |
437 | ||
438 | spin_lock_irqsave(&conf->device_lock, flags); | |
439 | if (!uptodate) | |
440 | md_error(conf->mddev, conf->disks[i].rdev); | |
441 | ||
442 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); | |
443 | ||
444 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
445 | set_bit(STRIPE_HANDLE, &sh->state); | |
446 | __release_stripe(conf, sh); | |
447 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
448 | return 0; | |
449 | } | |
450 | ||
451 | ||
452 | static sector_t compute_blocknr(struct stripe_head *sh, int i); | |
453 | ||
454 | static void raid5_build_block (struct stripe_head *sh, int i) | |
455 | { | |
456 | struct r5dev *dev = &sh->dev[i]; | |
457 | ||
458 | bio_init(&dev->req); | |
459 | dev->req.bi_io_vec = &dev->vec; | |
460 | dev->req.bi_vcnt++; | |
461 | dev->req.bi_max_vecs++; | |
462 | dev->vec.bv_page = dev->page; | |
463 | dev->vec.bv_len = STRIPE_SIZE; | |
464 | dev->vec.bv_offset = 0; | |
465 | ||
466 | dev->req.bi_sector = sh->sector; | |
467 | dev->req.bi_private = sh; | |
468 | ||
469 | dev->flags = 0; | |
470 | if (i != sh->pd_idx) | |
471 | dev->sector = compute_blocknr(sh, i); | |
472 | } | |
473 | ||
474 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) | |
475 | { | |
476 | char b[BDEVNAME_SIZE]; | |
477 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
478 | PRINTK("raid5: error called\n"); | |
479 | ||
480 | if (!rdev->faulty) { | |
481 | mddev->sb_dirty = 1; | |
482 | if (rdev->in_sync) { | |
483 | conf->working_disks--; | |
484 | mddev->degraded++; | |
485 | conf->failed_disks++; | |
486 | rdev->in_sync = 0; | |
487 | /* | |
488 | * if recovery was running, make sure it aborts. | |
489 | */ | |
490 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); | |
491 | } | |
492 | rdev->faulty = 1; | |
493 | printk (KERN_ALERT | |
494 | "raid5: Disk failure on %s, disabling device." | |
495 | " Operation continuing on %d devices\n", | |
496 | bdevname(rdev->bdev,b), conf->working_disks); | |
497 | } | |
498 | } | |
499 | ||
500 | /* | |
501 | * Input: a 'big' sector number, | |
502 | * Output: index of the data and parity disk, and the sector # in them. | |
503 | */ | |
504 | static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks, | |
505 | unsigned int data_disks, unsigned int * dd_idx, | |
506 | unsigned int * pd_idx, raid5_conf_t *conf) | |
507 | { | |
508 | long stripe; | |
509 | unsigned long chunk_number; | |
510 | unsigned int chunk_offset; | |
511 | sector_t new_sector; | |
512 | int sectors_per_chunk = conf->chunk_size >> 9; | |
513 | ||
514 | /* First compute the information on this sector */ | |
515 | ||
516 | /* | |
517 | * Compute the chunk number and the sector offset inside the chunk | |
518 | */ | |
519 | chunk_offset = sector_div(r_sector, sectors_per_chunk); | |
520 | chunk_number = r_sector; | |
521 | BUG_ON(r_sector != chunk_number); | |
522 | ||
523 | /* | |
524 | * Compute the stripe number | |
525 | */ | |
526 | stripe = chunk_number / data_disks; | |
527 | ||
528 | /* | |
529 | * Compute the data disk and parity disk indexes inside the stripe | |
530 | */ | |
531 | *dd_idx = chunk_number % data_disks; | |
532 | ||
533 | /* | |
534 | * Select the parity disk based on the user selected algorithm. | |
535 | */ | |
536 | if (conf->level == 4) | |
537 | *pd_idx = data_disks; | |
538 | else switch (conf->algorithm) { | |
539 | case ALGORITHM_LEFT_ASYMMETRIC: | |
540 | *pd_idx = data_disks - stripe % raid_disks; | |
541 | if (*dd_idx >= *pd_idx) | |
542 | (*dd_idx)++; | |
543 | break; | |
544 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
545 | *pd_idx = stripe % raid_disks; | |
546 | if (*dd_idx >= *pd_idx) | |
547 | (*dd_idx)++; | |
548 | break; | |
549 | case ALGORITHM_LEFT_SYMMETRIC: | |
550 | *pd_idx = data_disks - stripe % raid_disks; | |
551 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; | |
552 | break; | |
553 | case ALGORITHM_RIGHT_SYMMETRIC: | |
554 | *pd_idx = stripe % raid_disks; | |
555 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; | |
556 | break; | |
557 | default: | |
558 | printk("raid5: unsupported algorithm %d\n", | |
559 | conf->algorithm); | |
560 | } | |
561 | ||
562 | /* | |
563 | * Finally, compute the new sector number | |
564 | */ | |
565 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; | |
566 | return new_sector; | |
567 | } | |
568 | ||
569 | ||
570 | static sector_t compute_blocknr(struct stripe_head *sh, int i) | |
571 | { | |
572 | raid5_conf_t *conf = sh->raid_conf; | |
573 | int raid_disks = conf->raid_disks, data_disks = raid_disks - 1; | |
574 | sector_t new_sector = sh->sector, check; | |
575 | int sectors_per_chunk = conf->chunk_size >> 9; | |
576 | sector_t stripe; | |
577 | int chunk_offset; | |
578 | int chunk_number, dummy1, dummy2, dd_idx = i; | |
579 | sector_t r_sector; | |
580 | ||
581 | chunk_offset = sector_div(new_sector, sectors_per_chunk); | |
582 | stripe = new_sector; | |
583 | BUG_ON(new_sector != stripe); | |
584 | ||
585 | ||
586 | switch (conf->algorithm) { | |
587 | case ALGORITHM_LEFT_ASYMMETRIC: | |
588 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
589 | if (i > sh->pd_idx) | |
590 | i--; | |
591 | break; | |
592 | case ALGORITHM_LEFT_SYMMETRIC: | |
593 | case ALGORITHM_RIGHT_SYMMETRIC: | |
594 | if (i < sh->pd_idx) | |
595 | i += raid_disks; | |
596 | i -= (sh->pd_idx + 1); | |
597 | break; | |
598 | default: | |
599 | printk("raid5: unsupported algorithm %d\n", | |
600 | conf->algorithm); | |
601 | } | |
602 | ||
603 | chunk_number = stripe * data_disks + i; | |
604 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; | |
605 | ||
606 | check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf); | |
607 | if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) { | |
608 | printk("compute_blocknr: map not correct\n"); | |
609 | return 0; | |
610 | } | |
611 | return r_sector; | |
612 | } | |
613 | ||
614 | ||
615 | ||
616 | /* | |
617 | * Copy data between a page in the stripe cache, and a bio. | |
618 | * There are no alignment or size guarantees between the page or the | |
619 | * bio except that there is some overlap. | |
620 | * All iovecs in the bio must be considered. | |
621 | */ | |
622 | static void copy_data(int frombio, struct bio *bio, | |
623 | struct page *page, | |
624 | sector_t sector) | |
625 | { | |
626 | char *pa = page_address(page); | |
627 | struct bio_vec *bvl; | |
628 | int i; | |
629 | int page_offset; | |
630 | ||
631 | if (bio->bi_sector >= sector) | |
632 | page_offset = (signed)(bio->bi_sector - sector) * 512; | |
633 | else | |
634 | page_offset = (signed)(sector - bio->bi_sector) * -512; | |
635 | bio_for_each_segment(bvl, bio, i) { | |
636 | int len = bio_iovec_idx(bio,i)->bv_len; | |
637 | int clen; | |
638 | int b_offset = 0; | |
639 | ||
640 | if (page_offset < 0) { | |
641 | b_offset = -page_offset; | |
642 | page_offset += b_offset; | |
643 | len -= b_offset; | |
644 | } | |
645 | ||
646 | if (len > 0 && page_offset + len > STRIPE_SIZE) | |
647 | clen = STRIPE_SIZE - page_offset; | |
648 | else clen = len; | |
649 | ||
650 | if (clen > 0) { | |
651 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); | |
652 | if (frombio) | |
653 | memcpy(pa+page_offset, ba+b_offset, clen); | |
654 | else | |
655 | memcpy(ba+b_offset, pa+page_offset, clen); | |
656 | __bio_kunmap_atomic(ba, KM_USER0); | |
657 | } | |
658 | if (clen < len) /* hit end of page */ | |
659 | break; | |
660 | page_offset += len; | |
661 | } | |
662 | } | |
663 | ||
664 | #define check_xor() do { \ | |
665 | if (count == MAX_XOR_BLOCKS) { \ | |
666 | xor_block(count, STRIPE_SIZE, ptr); \ | |
667 | count = 1; \ | |
668 | } \ | |
669 | } while(0) | |
670 | ||
671 | ||
672 | static void compute_block(struct stripe_head *sh, int dd_idx) | |
673 | { | |
674 | raid5_conf_t *conf = sh->raid_conf; | |
675 | int i, count, disks = conf->raid_disks; | |
676 | void *ptr[MAX_XOR_BLOCKS], *p; | |
677 | ||
678 | PRINTK("compute_block, stripe %llu, idx %d\n", | |
679 | (unsigned long long)sh->sector, dd_idx); | |
680 | ||
681 | ptr[0] = page_address(sh->dev[dd_idx].page); | |
682 | memset(ptr[0], 0, STRIPE_SIZE); | |
683 | count = 1; | |
684 | for (i = disks ; i--; ) { | |
685 | if (i == dd_idx) | |
686 | continue; | |
687 | p = page_address(sh->dev[i].page); | |
688 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) | |
689 | ptr[count++] = p; | |
690 | else | |
691 | printk("compute_block() %d, stripe %llu, %d" | |
692 | " not present\n", dd_idx, | |
693 | (unsigned long long)sh->sector, i); | |
694 | ||
695 | check_xor(); | |
696 | } | |
697 | if (count != 1) | |
698 | xor_block(count, STRIPE_SIZE, ptr); | |
699 | set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); | |
700 | } | |
701 | ||
702 | static void compute_parity(struct stripe_head *sh, int method) | |
703 | { | |
704 | raid5_conf_t *conf = sh->raid_conf; | |
705 | int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count; | |
706 | void *ptr[MAX_XOR_BLOCKS]; | |
707 | struct bio *chosen; | |
708 | ||
709 | PRINTK("compute_parity, stripe %llu, method %d\n", | |
710 | (unsigned long long)sh->sector, method); | |
711 | ||
712 | count = 1; | |
713 | ptr[0] = page_address(sh->dev[pd_idx].page); | |
714 | switch(method) { | |
715 | case READ_MODIFY_WRITE: | |
716 | if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags)) | |
717 | BUG(); | |
718 | for (i=disks ; i-- ;) { | |
719 | if (i==pd_idx) | |
720 | continue; | |
721 | if (sh->dev[i].towrite && | |
722 | test_bit(R5_UPTODATE, &sh->dev[i].flags)) { | |
723 | ptr[count++] = page_address(sh->dev[i].page); | |
724 | chosen = sh->dev[i].towrite; | |
725 | sh->dev[i].towrite = NULL; | |
726 | ||
727 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
728 | wake_up(&conf->wait_for_overlap); | |
729 | ||
730 | if (sh->dev[i].written) BUG(); | |
731 | sh->dev[i].written = chosen; | |
732 | check_xor(); | |
733 | } | |
734 | } | |
735 | break; | |
736 | case RECONSTRUCT_WRITE: | |
737 | memset(ptr[0], 0, STRIPE_SIZE); | |
738 | for (i= disks; i-- ;) | |
739 | if (i!=pd_idx && sh->dev[i].towrite) { | |
740 | chosen = sh->dev[i].towrite; | |
741 | sh->dev[i].towrite = NULL; | |
742 | ||
743 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
744 | wake_up(&conf->wait_for_overlap); | |
745 | ||
746 | if (sh->dev[i].written) BUG(); | |
747 | sh->dev[i].written = chosen; | |
748 | } | |
749 | break; | |
750 | case CHECK_PARITY: | |
751 | break; | |
752 | } | |
753 | if (count>1) { | |
754 | xor_block(count, STRIPE_SIZE, ptr); | |
755 | count = 1; | |
756 | } | |
757 | ||
758 | for (i = disks; i--;) | |
759 | if (sh->dev[i].written) { | |
760 | sector_t sector = sh->dev[i].sector; | |
761 | struct bio *wbi = sh->dev[i].written; | |
762 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { | |
763 | copy_data(1, wbi, sh->dev[i].page, sector); | |
764 | wbi = r5_next_bio(wbi, sector); | |
765 | } | |
766 | ||
767 | set_bit(R5_LOCKED, &sh->dev[i].flags); | |
768 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
769 | } | |
770 | ||
771 | switch(method) { | |
772 | case RECONSTRUCT_WRITE: | |
773 | case CHECK_PARITY: | |
774 | for (i=disks; i--;) | |
775 | if (i != pd_idx) { | |
776 | ptr[count++] = page_address(sh->dev[i].page); | |
777 | check_xor(); | |
778 | } | |
779 | break; | |
780 | case READ_MODIFY_WRITE: | |
781 | for (i = disks; i--;) | |
782 | if (sh->dev[i].written) { | |
783 | ptr[count++] = page_address(sh->dev[i].page); | |
784 | check_xor(); | |
785 | } | |
786 | } | |
787 | if (count != 1) | |
788 | xor_block(count, STRIPE_SIZE, ptr); | |
789 | ||
790 | if (method != CHECK_PARITY) { | |
791 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
792 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); | |
793 | } else | |
794 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); | |
795 | } | |
796 | ||
797 | /* | |
798 | * Each stripe/dev can have one or more bion attached. | |
799 | * toread/towrite point to the first in a chain. | |
800 | * The bi_next chain must be in order. | |
801 | */ | |
802 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) | |
803 | { | |
804 | struct bio **bip; | |
805 | raid5_conf_t *conf = sh->raid_conf; | |
806 | ||
807 | PRINTK("adding bh b#%llu to stripe s#%llu\n", | |
808 | (unsigned long long)bi->bi_sector, | |
809 | (unsigned long long)sh->sector); | |
810 | ||
811 | ||
812 | spin_lock(&sh->lock); | |
813 | spin_lock_irq(&conf->device_lock); | |
814 | if (forwrite) | |
815 | bip = &sh->dev[dd_idx].towrite; | |
816 | else | |
817 | bip = &sh->dev[dd_idx].toread; | |
818 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { | |
819 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) | |
820 | goto overlap; | |
821 | bip = & (*bip)->bi_next; | |
822 | } | |
823 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) | |
824 | goto overlap; | |
825 | ||
826 | if (*bip && bi->bi_next && (*bip) != bi->bi_next) | |
827 | BUG(); | |
828 | if (*bip) | |
829 | bi->bi_next = *bip; | |
830 | *bip = bi; | |
831 | bi->bi_phys_segments ++; | |
832 | spin_unlock_irq(&conf->device_lock); | |
833 | spin_unlock(&sh->lock); | |
834 | ||
835 | PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n", | |
836 | (unsigned long long)bi->bi_sector, | |
837 | (unsigned long long)sh->sector, dd_idx); | |
838 | ||
839 | if (forwrite) { | |
840 | /* check if page is covered */ | |
841 | sector_t sector = sh->dev[dd_idx].sector; | |
842 | for (bi=sh->dev[dd_idx].towrite; | |
843 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && | |
844 | bi && bi->bi_sector <= sector; | |
845 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { | |
846 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) | |
847 | sector = bi->bi_sector + (bi->bi_size>>9); | |
848 | } | |
849 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) | |
850 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); | |
851 | } | |
852 | return 1; | |
853 | ||
854 | overlap: | |
855 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); | |
856 | spin_unlock_irq(&conf->device_lock); | |
857 | spin_unlock(&sh->lock); | |
858 | return 0; | |
859 | } | |
860 | ||
861 | ||
862 | /* | |
863 | * handle_stripe - do things to a stripe. | |
864 | * | |
865 | * We lock the stripe and then examine the state of various bits | |
866 | * to see what needs to be done. | |
867 | * Possible results: | |
868 | * return some read request which now have data | |
869 | * return some write requests which are safely on disc | |
870 | * schedule a read on some buffers | |
871 | * schedule a write of some buffers | |
872 | * return confirmation of parity correctness | |
873 | * | |
874 | * Parity calculations are done inside the stripe lock | |
875 | * buffers are taken off read_list or write_list, and bh_cache buffers | |
876 | * get BH_Lock set before the stripe lock is released. | |
877 | * | |
878 | */ | |
879 | ||
880 | static void handle_stripe(struct stripe_head *sh) | |
881 | { | |
882 | raid5_conf_t *conf = sh->raid_conf; | |
883 | int disks = conf->raid_disks; | |
884 | struct bio *return_bi= NULL; | |
885 | struct bio *bi; | |
886 | int i; | |
887 | int syncing; | |
888 | int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0; | |
889 | int non_overwrite = 0; | |
890 | int failed_num=0; | |
891 | struct r5dev *dev; | |
892 | ||
893 | PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n", | |
894 | (unsigned long long)sh->sector, atomic_read(&sh->count), | |
895 | sh->pd_idx); | |
896 | ||
897 | spin_lock(&sh->lock); | |
898 | clear_bit(STRIPE_HANDLE, &sh->state); | |
899 | clear_bit(STRIPE_DELAYED, &sh->state); | |
900 | ||
901 | syncing = test_bit(STRIPE_SYNCING, &sh->state); | |
902 | /* Now to look around and see what can be done */ | |
903 | ||
904 | for (i=disks; i--; ) { | |
905 | mdk_rdev_t *rdev; | |
906 | dev = &sh->dev[i]; | |
907 | clear_bit(R5_Insync, &dev->flags); | |
908 | clear_bit(R5_Syncio, &dev->flags); | |
909 | ||
910 | PRINTK("check %d: state 0x%lx read %p write %p written %p\n", | |
911 | i, dev->flags, dev->toread, dev->towrite, dev->written); | |
912 | /* maybe we can reply to a read */ | |
913 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { | |
914 | struct bio *rbi, *rbi2; | |
915 | PRINTK("Return read for disc %d\n", i); | |
916 | spin_lock_irq(&conf->device_lock); | |
917 | rbi = dev->toread; | |
918 | dev->toread = NULL; | |
919 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) | |
920 | wake_up(&conf->wait_for_overlap); | |
921 | spin_unlock_irq(&conf->device_lock); | |
922 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
923 | copy_data(0, rbi, dev->page, dev->sector); | |
924 | rbi2 = r5_next_bio(rbi, dev->sector); | |
925 | spin_lock_irq(&conf->device_lock); | |
926 | if (--rbi->bi_phys_segments == 0) { | |
927 | rbi->bi_next = return_bi; | |
928 | return_bi = rbi; | |
929 | } | |
930 | spin_unlock_irq(&conf->device_lock); | |
931 | rbi = rbi2; | |
932 | } | |
933 | } | |
934 | ||
935 | /* now count some things */ | |
936 | if (test_bit(R5_LOCKED, &dev->flags)) locked++; | |
937 | if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++; | |
938 | ||
939 | ||
940 | if (dev->toread) to_read++; | |
941 | if (dev->towrite) { | |
942 | to_write++; | |
943 | if (!test_bit(R5_OVERWRITE, &dev->flags)) | |
944 | non_overwrite++; | |
945 | } | |
946 | if (dev->written) written++; | |
947 | rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */ | |
948 | if (!rdev || !rdev->in_sync) { | |
949 | failed++; | |
950 | failed_num = i; | |
951 | } else | |
952 | set_bit(R5_Insync, &dev->flags); | |
953 | } | |
954 | PRINTK("locked=%d uptodate=%d to_read=%d" | |
955 | " to_write=%d failed=%d failed_num=%d\n", | |
956 | locked, uptodate, to_read, to_write, failed, failed_num); | |
957 | /* check if the array has lost two devices and, if so, some requests might | |
958 | * need to be failed | |
959 | */ | |
960 | if (failed > 1 && to_read+to_write+written) { | |
961 | spin_lock_irq(&conf->device_lock); | |
962 | for (i=disks; i--; ) { | |
963 | /* fail all writes first */ | |
964 | bi = sh->dev[i].towrite; | |
965 | sh->dev[i].towrite = NULL; | |
966 | if (bi) to_write--; | |
967 | ||
968 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
969 | wake_up(&conf->wait_for_overlap); | |
970 | ||
971 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
972 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
973 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
974 | if (--bi->bi_phys_segments == 0) { | |
975 | md_write_end(conf->mddev); | |
976 | bi->bi_next = return_bi; | |
977 | return_bi = bi; | |
978 | } | |
979 | bi = nextbi; | |
980 | } | |
981 | /* and fail all 'written' */ | |
982 | bi = sh->dev[i].written; | |
983 | sh->dev[i].written = NULL; | |
984 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { | |
985 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); | |
986 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
987 | if (--bi->bi_phys_segments == 0) { | |
988 | md_write_end(conf->mddev); | |
989 | bi->bi_next = return_bi; | |
990 | return_bi = bi; | |
991 | } | |
992 | bi = bi2; | |
993 | } | |
994 | ||
995 | /* fail any reads if this device is non-operational */ | |
996 | if (!test_bit(R5_Insync, &sh->dev[i].flags)) { | |
997 | bi = sh->dev[i].toread; | |
998 | sh->dev[i].toread = NULL; | |
999 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
1000 | wake_up(&conf->wait_for_overlap); | |
1001 | if (bi) to_read--; | |
1002 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ | |
1003 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); | |
1004 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1005 | if (--bi->bi_phys_segments == 0) { | |
1006 | bi->bi_next = return_bi; | |
1007 | return_bi = bi; | |
1008 | } | |
1009 | bi = nextbi; | |
1010 | } | |
1011 | } | |
1012 | } | |
1013 | spin_unlock_irq(&conf->device_lock); | |
1014 | } | |
1015 | if (failed > 1 && syncing) { | |
1016 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); | |
1017 | clear_bit(STRIPE_SYNCING, &sh->state); | |
1018 | syncing = 0; | |
1019 | } | |
1020 | ||
1021 | /* might be able to return some write requests if the parity block | |
1022 | * is safe, or on a failed drive | |
1023 | */ | |
1024 | dev = &sh->dev[sh->pd_idx]; | |
1025 | if ( written && | |
1026 | ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) && | |
1027 | test_bit(R5_UPTODATE, &dev->flags)) | |
1028 | || (failed == 1 && failed_num == sh->pd_idx)) | |
1029 | ) { | |
1030 | /* any written block on an uptodate or failed drive can be returned. | |
1031 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but | |
1032 | * never LOCKED, so we don't need to test 'failed' directly. | |
1033 | */ | |
1034 | for (i=disks; i--; ) | |
1035 | if (sh->dev[i].written) { | |
1036 | dev = &sh->dev[i]; | |
1037 | if (!test_bit(R5_LOCKED, &dev->flags) && | |
1038 | test_bit(R5_UPTODATE, &dev->flags) ) { | |
1039 | /* We can return any write requests */ | |
1040 | struct bio *wbi, *wbi2; | |
1041 | PRINTK("Return write for disc %d\n", i); | |
1042 | spin_lock_irq(&conf->device_lock); | |
1043 | wbi = dev->written; | |
1044 | dev->written = NULL; | |
1045 | while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) { | |
1046 | wbi2 = r5_next_bio(wbi, dev->sector); | |
1047 | if (--wbi->bi_phys_segments == 0) { | |
1048 | md_write_end(conf->mddev); | |
1049 | wbi->bi_next = return_bi; | |
1050 | return_bi = wbi; | |
1051 | } | |
1052 | wbi = wbi2; | |
1053 | } | |
1054 | spin_unlock_irq(&conf->device_lock); | |
1055 | } | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | /* Now we might consider reading some blocks, either to check/generate | |
1060 | * parity, or to satisfy requests | |
1061 | * or to load a block that is being partially written. | |
1062 | */ | |
1063 | if (to_read || non_overwrite || (syncing && (uptodate < disks))) { | |
1064 | for (i=disks; i--;) { | |
1065 | dev = &sh->dev[i]; | |
1066 | if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1067 | (dev->toread || | |
1068 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || | |
1069 | syncing || | |
1070 | (failed && (sh->dev[failed_num].toread || | |
1071 | (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags)))) | |
1072 | ) | |
1073 | ) { | |
1074 | /* we would like to get this block, possibly | |
1075 | * by computing it, but we might not be able to | |
1076 | */ | |
1077 | if (uptodate == disks-1) { | |
1078 | PRINTK("Computing block %d\n", i); | |
1079 | compute_block(sh, i); | |
1080 | uptodate++; | |
1081 | } else if (test_bit(R5_Insync, &dev->flags)) { | |
1082 | set_bit(R5_LOCKED, &dev->flags); | |
1083 | set_bit(R5_Wantread, &dev->flags); | |
1084 | #if 0 | |
1085 | /* if I am just reading this block and we don't have | |
1086 | a failed drive, or any pending writes then sidestep the cache */ | |
1087 | if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext && | |
1088 | ! syncing && !failed && !to_write) { | |
1089 | sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page; | |
1090 | sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data; | |
1091 | } | |
1092 | #endif | |
1093 | locked++; | |
1094 | PRINTK("Reading block %d (sync=%d)\n", | |
1095 | i, syncing); | |
1096 | if (syncing) | |
1097 | md_sync_acct(conf->disks[i].rdev->bdev, | |
1098 | STRIPE_SECTORS); | |
1099 | } | |
1100 | } | |
1101 | } | |
1102 | set_bit(STRIPE_HANDLE, &sh->state); | |
1103 | } | |
1104 | ||
1105 | /* now to consider writing and what else, if anything should be read */ | |
1106 | if (to_write) { | |
1107 | int rmw=0, rcw=0; | |
1108 | for (i=disks ; i--;) { | |
1109 | /* would I have to read this buffer for read_modify_write */ | |
1110 | dev = &sh->dev[i]; | |
1111 | if ((dev->towrite || i == sh->pd_idx) && | |
1112 | (!test_bit(R5_LOCKED, &dev->flags) | |
1113 | #if 0 | |
1114 | || sh->bh_page[i]!=bh->b_page | |
1115 | #endif | |
1116 | ) && | |
1117 | !test_bit(R5_UPTODATE, &dev->flags)) { | |
1118 | if (test_bit(R5_Insync, &dev->flags) | |
1119 | /* && !(!mddev->insync && i == sh->pd_idx) */ | |
1120 | ) | |
1121 | rmw++; | |
1122 | else rmw += 2*disks; /* cannot read it */ | |
1123 | } | |
1124 | /* Would I have to read this buffer for reconstruct_write */ | |
1125 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && | |
1126 | (!test_bit(R5_LOCKED, &dev->flags) | |
1127 | #if 0 | |
1128 | || sh->bh_page[i] != bh->b_page | |
1129 | #endif | |
1130 | ) && | |
1131 | !test_bit(R5_UPTODATE, &dev->flags)) { | |
1132 | if (test_bit(R5_Insync, &dev->flags)) rcw++; | |
1133 | else rcw += 2*disks; | |
1134 | } | |
1135 | } | |
1136 | PRINTK("for sector %llu, rmw=%d rcw=%d\n", | |
1137 | (unsigned long long)sh->sector, rmw, rcw); | |
1138 | set_bit(STRIPE_HANDLE, &sh->state); | |
1139 | if (rmw < rcw && rmw > 0) | |
1140 | /* prefer read-modify-write, but need to get some data */ | |
1141 | for (i=disks; i--;) { | |
1142 | dev = &sh->dev[i]; | |
1143 | if ((dev->towrite || i == sh->pd_idx) && | |
1144 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1145 | test_bit(R5_Insync, &dev->flags)) { | |
1146 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1147 | { | |
1148 | PRINTK("Read_old block %d for r-m-w\n", i); | |
1149 | set_bit(R5_LOCKED, &dev->flags); | |
1150 | set_bit(R5_Wantread, &dev->flags); | |
1151 | locked++; | |
1152 | } else { | |
1153 | set_bit(STRIPE_DELAYED, &sh->state); | |
1154 | set_bit(STRIPE_HANDLE, &sh->state); | |
1155 | } | |
1156 | } | |
1157 | } | |
1158 | if (rcw <= rmw && rcw > 0) | |
1159 | /* want reconstruct write, but need to get some data */ | |
1160 | for (i=disks; i--;) { | |
1161 | dev = &sh->dev[i]; | |
1162 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && | |
1163 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && | |
1164 | test_bit(R5_Insync, &dev->flags)) { | |
1165 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1166 | { | |
1167 | PRINTK("Read_old block %d for Reconstruct\n", i); | |
1168 | set_bit(R5_LOCKED, &dev->flags); | |
1169 | set_bit(R5_Wantread, &dev->flags); | |
1170 | locked++; | |
1171 | } else { | |
1172 | set_bit(STRIPE_DELAYED, &sh->state); | |
1173 | set_bit(STRIPE_HANDLE, &sh->state); | |
1174 | } | |
1175 | } | |
1176 | } | |
1177 | /* now if nothing is locked, and if we have enough data, we can start a write request */ | |
1178 | if (locked == 0 && (rcw == 0 ||rmw == 0)) { | |
1179 | PRINTK("Computing parity...\n"); | |
1180 | compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE); | |
1181 | /* now every locked buffer is ready to be written */ | |
1182 | for (i=disks; i--;) | |
1183 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { | |
1184 | PRINTK("Writing block %d\n", i); | |
1185 | locked++; | |
1186 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
1187 | if (!test_bit(R5_Insync, &sh->dev[i].flags) | |
1188 | || (i==sh->pd_idx && failed == 0)) | |
1189 | set_bit(STRIPE_INSYNC, &sh->state); | |
1190 | } | |
1191 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { | |
1192 | atomic_dec(&conf->preread_active_stripes); | |
1193 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) | |
1194 | md_wakeup_thread(conf->mddev->thread); | |
1195 | } | |
1196 | } | |
1197 | } | |
1198 | ||
1199 | /* maybe we need to check and possibly fix the parity for this stripe | |
1200 | * Any reads will already have been scheduled, so we just see if enough data | |
1201 | * is available | |
1202 | */ | |
1203 | if (syncing && locked == 0 && | |
1204 | !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) { | |
1205 | set_bit(STRIPE_HANDLE, &sh->state); | |
1206 | if (failed == 0) { | |
1207 | char *pagea; | |
1208 | if (uptodate != disks) | |
1209 | BUG(); | |
1210 | compute_parity(sh, CHECK_PARITY); | |
1211 | uptodate--; | |
1212 | pagea = page_address(sh->dev[sh->pd_idx].page); | |
1213 | if ((*(u32*)pagea) == 0 && | |
1214 | !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) { | |
1215 | /* parity is correct (on disc, not in buffer any more) */ | |
1216 | set_bit(STRIPE_INSYNC, &sh->state); | |
1217 | } | |
1218 | } | |
1219 | if (!test_bit(STRIPE_INSYNC, &sh->state)) { | |
1220 | if (failed==0) | |
1221 | failed_num = sh->pd_idx; | |
1222 | /* should be able to compute the missing block and write it to spare */ | |
1223 | if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) { | |
1224 | if (uptodate+1 != disks) | |
1225 | BUG(); | |
1226 | compute_block(sh, failed_num); | |
1227 | uptodate++; | |
1228 | } | |
1229 | if (uptodate != disks) | |
1230 | BUG(); | |
1231 | dev = &sh->dev[failed_num]; | |
1232 | set_bit(R5_LOCKED, &dev->flags); | |
1233 | set_bit(R5_Wantwrite, &dev->flags); | |
1234 | locked++; | |
1235 | set_bit(STRIPE_INSYNC, &sh->state); | |
1236 | set_bit(R5_Syncio, &dev->flags); | |
1237 | } | |
1238 | } | |
1239 | if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { | |
1240 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); | |
1241 | clear_bit(STRIPE_SYNCING, &sh->state); | |
1242 | } | |
1243 | ||
1244 | spin_unlock(&sh->lock); | |
1245 | ||
1246 | while ((bi=return_bi)) { | |
1247 | int bytes = bi->bi_size; | |
1248 | ||
1249 | return_bi = bi->bi_next; | |
1250 | bi->bi_next = NULL; | |
1251 | bi->bi_size = 0; | |
1252 | bi->bi_end_io(bi, bytes, 0); | |
1253 | } | |
1254 | for (i=disks; i-- ;) { | |
1255 | int rw; | |
1256 | struct bio *bi; | |
1257 | mdk_rdev_t *rdev; | |
1258 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) | |
1259 | rw = 1; | |
1260 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) | |
1261 | rw = 0; | |
1262 | else | |
1263 | continue; | |
1264 | ||
1265 | bi = &sh->dev[i].req; | |
1266 | ||
1267 | bi->bi_rw = rw; | |
1268 | if (rw) | |
1269 | bi->bi_end_io = raid5_end_write_request; | |
1270 | else | |
1271 | bi->bi_end_io = raid5_end_read_request; | |
1272 | ||
1273 | rcu_read_lock(); | |
1274 | rdev = conf->disks[i].rdev; | |
1275 | if (rdev && rdev->faulty) | |
1276 | rdev = NULL; | |
1277 | if (rdev) | |
1278 | atomic_inc(&rdev->nr_pending); | |
1279 | rcu_read_unlock(); | |
1280 | ||
1281 | if (rdev) { | |
1282 | if (test_bit(R5_Syncio, &sh->dev[i].flags)) | |
1283 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); | |
1284 | ||
1285 | bi->bi_bdev = rdev->bdev; | |
1286 | PRINTK("for %llu schedule op %ld on disc %d\n", | |
1287 | (unsigned long long)sh->sector, bi->bi_rw, i); | |
1288 | atomic_inc(&sh->count); | |
1289 | bi->bi_sector = sh->sector + rdev->data_offset; | |
1290 | bi->bi_flags = 1 << BIO_UPTODATE; | |
1291 | bi->bi_vcnt = 1; | |
1292 | bi->bi_max_vecs = 1; | |
1293 | bi->bi_idx = 0; | |
1294 | bi->bi_io_vec = &sh->dev[i].vec; | |
1295 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; | |
1296 | bi->bi_io_vec[0].bv_offset = 0; | |
1297 | bi->bi_size = STRIPE_SIZE; | |
1298 | bi->bi_next = NULL; | |
1299 | generic_make_request(bi); | |
1300 | } else { | |
1301 | PRINTK("skip op %ld on disc %d for sector %llu\n", | |
1302 | bi->bi_rw, i, (unsigned long long)sh->sector); | |
1303 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
1304 | set_bit(STRIPE_HANDLE, &sh->state); | |
1305 | } | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | static inline void raid5_activate_delayed(raid5_conf_t *conf) | |
1310 | { | |
1311 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { | |
1312 | while (!list_empty(&conf->delayed_list)) { | |
1313 | struct list_head *l = conf->delayed_list.next; | |
1314 | struct stripe_head *sh; | |
1315 | sh = list_entry(l, struct stripe_head, lru); | |
1316 | list_del_init(l); | |
1317 | clear_bit(STRIPE_DELAYED, &sh->state); | |
1318 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
1319 | atomic_inc(&conf->preread_active_stripes); | |
1320 | list_add_tail(&sh->lru, &conf->handle_list); | |
1321 | } | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | static void unplug_slaves(mddev_t *mddev) | |
1326 | { | |
1327 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1328 | int i; | |
1329 | ||
1330 | rcu_read_lock(); | |
1331 | for (i=0; i<mddev->raid_disks; i++) { | |
1332 | mdk_rdev_t *rdev = conf->disks[i].rdev; | |
1333 | if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) { | |
1334 | request_queue_t *r_queue = bdev_get_queue(rdev->bdev); | |
1335 | ||
1336 | atomic_inc(&rdev->nr_pending); | |
1337 | rcu_read_unlock(); | |
1338 | ||
1339 | if (r_queue->unplug_fn) | |
1340 | r_queue->unplug_fn(r_queue); | |
1341 | ||
1342 | rdev_dec_pending(rdev, mddev); | |
1343 | rcu_read_lock(); | |
1344 | } | |
1345 | } | |
1346 | rcu_read_unlock(); | |
1347 | } | |
1348 | ||
1349 | static void raid5_unplug_device(request_queue_t *q) | |
1350 | { | |
1351 | mddev_t *mddev = q->queuedata; | |
1352 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1353 | unsigned long flags; | |
1354 | ||
1355 | spin_lock_irqsave(&conf->device_lock, flags); | |
1356 | ||
1357 | if (blk_remove_plug(q)) | |
1358 | raid5_activate_delayed(conf); | |
1359 | md_wakeup_thread(mddev->thread); | |
1360 | ||
1361 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1362 | ||
1363 | unplug_slaves(mddev); | |
1364 | } | |
1365 | ||
1366 | static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk, | |
1367 | sector_t *error_sector) | |
1368 | { | |
1369 | mddev_t *mddev = q->queuedata; | |
1370 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1371 | int i, ret = 0; | |
1372 | ||
1373 | rcu_read_lock(); | |
1374 | for (i=0; i<mddev->raid_disks && ret == 0; i++) { | |
1375 | mdk_rdev_t *rdev = conf->disks[i].rdev; | |
1376 | if (rdev && !rdev->faulty) { | |
1377 | struct block_device *bdev = rdev->bdev; | |
1378 | request_queue_t *r_queue = bdev_get_queue(bdev); | |
1379 | ||
1380 | if (!r_queue->issue_flush_fn) | |
1381 | ret = -EOPNOTSUPP; | |
1382 | else { | |
1383 | atomic_inc(&rdev->nr_pending); | |
1384 | rcu_read_unlock(); | |
1385 | ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, | |
1386 | error_sector); | |
1387 | rdev_dec_pending(rdev, mddev); | |
1388 | rcu_read_lock(); | |
1389 | } | |
1390 | } | |
1391 | } | |
1392 | rcu_read_unlock(); | |
1393 | return ret; | |
1394 | } | |
1395 | ||
1396 | static inline void raid5_plug_device(raid5_conf_t *conf) | |
1397 | { | |
1398 | spin_lock_irq(&conf->device_lock); | |
1399 | blk_plug_device(conf->mddev->queue); | |
1400 | spin_unlock_irq(&conf->device_lock); | |
1401 | } | |
1402 | ||
1403 | static int make_request (request_queue_t *q, struct bio * bi) | |
1404 | { | |
1405 | mddev_t *mddev = q->queuedata; | |
1406 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1407 | const unsigned int raid_disks = conf->raid_disks; | |
1408 | const unsigned int data_disks = raid_disks - 1; | |
1409 | unsigned int dd_idx, pd_idx; | |
1410 | sector_t new_sector; | |
1411 | sector_t logical_sector, last_sector; | |
1412 | struct stripe_head *sh; | |
1413 | ||
3d310eb7 | 1414 | md_write_start(mddev, bi); |
06d91a5f | 1415 | |
1da177e4 LT |
1416 | if (bio_data_dir(bi)==WRITE) { |
1417 | disk_stat_inc(mddev->gendisk, writes); | |
1418 | disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi)); | |
1419 | } else { | |
1420 | disk_stat_inc(mddev->gendisk, reads); | |
1421 | disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi)); | |
1422 | } | |
1423 | ||
1424 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); | |
1425 | last_sector = bi->bi_sector + (bi->bi_size>>9); | |
1426 | bi->bi_next = NULL; | |
1427 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ | |
06d91a5f | 1428 | |
1da177e4 LT |
1429 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
1430 | DEFINE_WAIT(w); | |
1431 | ||
1432 | new_sector = raid5_compute_sector(logical_sector, | |
1433 | raid_disks, data_disks, &dd_idx, &pd_idx, conf); | |
1434 | ||
1435 | PRINTK("raid5: make_request, sector %llu logical %llu\n", | |
1436 | (unsigned long long)new_sector, | |
1437 | (unsigned long long)logical_sector); | |
1438 | ||
1439 | retry: | |
1440 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); | |
1441 | sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK)); | |
1442 | if (sh) { | |
1443 | if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { | |
1444 | /* Add failed due to overlap. Flush everything | |
1445 | * and wait a while | |
1446 | */ | |
1447 | raid5_unplug_device(mddev->queue); | |
1448 | release_stripe(sh); | |
1449 | schedule(); | |
1450 | goto retry; | |
1451 | } | |
1452 | finish_wait(&conf->wait_for_overlap, &w); | |
1453 | raid5_plug_device(conf); | |
1454 | handle_stripe(sh); | |
1455 | release_stripe(sh); | |
1456 | ||
1457 | } else { | |
1458 | /* cannot get stripe for read-ahead, just give-up */ | |
1459 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | |
1460 | finish_wait(&conf->wait_for_overlap, &w); | |
1461 | break; | |
1462 | } | |
1463 | ||
1464 | } | |
1465 | spin_lock_irq(&conf->device_lock); | |
1466 | if (--bi->bi_phys_segments == 0) { | |
1467 | int bytes = bi->bi_size; | |
1468 | ||
1469 | if ( bio_data_dir(bi) == WRITE ) | |
1470 | md_write_end(mddev); | |
1471 | bi->bi_size = 0; | |
1472 | bi->bi_end_io(bi, bytes, 0); | |
1473 | } | |
1474 | spin_unlock_irq(&conf->device_lock); | |
1475 | return 0; | |
1476 | } | |
1477 | ||
1478 | /* FIXME go_faster isn't used */ | |
57afd89f | 1479 | static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster) |
1da177e4 LT |
1480 | { |
1481 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
1482 | struct stripe_head *sh; | |
1483 | int sectors_per_chunk = conf->chunk_size >> 9; | |
1484 | sector_t x; | |
1485 | unsigned long stripe; | |
1486 | int chunk_offset; | |
1487 | int dd_idx, pd_idx; | |
1488 | sector_t first_sector; | |
1489 | int raid_disks = conf->raid_disks; | |
1490 | int data_disks = raid_disks-1; | |
1491 | ||
1492 | if (sector_nr >= mddev->size <<1) { | |
1493 | /* just being told to finish up .. nothing much to do */ | |
1494 | unplug_slaves(mddev); | |
1495 | return 0; | |
1496 | } | |
1497 | /* if there is 1 or more failed drives and we are trying | |
1498 | * to resync, then assert that we are finished, because there is | |
1499 | * nothing we can do. | |
1500 | */ | |
1501 | if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | |
57afd89f N |
1502 | sector_t rv = (mddev->size << 1) - sector_nr; |
1503 | *skipped = 1; | |
1da177e4 LT |
1504 | return rv; |
1505 | } | |
1506 | ||
1507 | x = sector_nr; | |
1508 | chunk_offset = sector_div(x, sectors_per_chunk); | |
1509 | stripe = x; | |
1510 | BUG_ON(x != stripe); | |
1511 | ||
1512 | first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk | |
1513 | + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf); | |
1514 | sh = get_active_stripe(conf, sector_nr, pd_idx, 1); | |
1515 | if (sh == NULL) { | |
1516 | sh = get_active_stripe(conf, sector_nr, pd_idx, 0); | |
1517 | /* make sure we don't swamp the stripe cache if someone else | |
1518 | * is trying to get access | |
1519 | */ | |
1520 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1521 | schedule_timeout(1); | |
1522 | } | |
1523 | spin_lock(&sh->lock); | |
1524 | set_bit(STRIPE_SYNCING, &sh->state); | |
1525 | clear_bit(STRIPE_INSYNC, &sh->state); | |
1526 | spin_unlock(&sh->lock); | |
1527 | ||
1528 | handle_stripe(sh); | |
1529 | release_stripe(sh); | |
1530 | ||
1531 | return STRIPE_SECTORS; | |
1532 | } | |
1533 | ||
1534 | /* | |
1535 | * This is our raid5 kernel thread. | |
1536 | * | |
1537 | * We scan the hash table for stripes which can be handled now. | |
1538 | * During the scan, completed stripes are saved for us by the interrupt | |
1539 | * handler, so that they will not have to wait for our next wakeup. | |
1540 | */ | |
1541 | static void raid5d (mddev_t *mddev) | |
1542 | { | |
1543 | struct stripe_head *sh; | |
1544 | raid5_conf_t *conf = mddev_to_conf(mddev); | |
1545 | int handled; | |
1546 | ||
1547 | PRINTK("+++ raid5d active\n"); | |
1548 | ||
1549 | md_check_recovery(mddev); | |
1da177e4 LT |
1550 | |
1551 | handled = 0; | |
1552 | spin_lock_irq(&conf->device_lock); | |
1553 | while (1) { | |
1554 | struct list_head *first; | |
1555 | ||
1556 | if (list_empty(&conf->handle_list) && | |
1557 | atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD && | |
1558 | !blk_queue_plugged(mddev->queue) && | |
1559 | !list_empty(&conf->delayed_list)) | |
1560 | raid5_activate_delayed(conf); | |
1561 | ||
1562 | if (list_empty(&conf->handle_list)) | |
1563 | break; | |
1564 | ||
1565 | first = conf->handle_list.next; | |
1566 | sh = list_entry(first, struct stripe_head, lru); | |
1567 | ||
1568 | list_del_init(first); | |
1569 | atomic_inc(&sh->count); | |
1570 | if (atomic_read(&sh->count)!= 1) | |
1571 | BUG(); | |
1572 | spin_unlock_irq(&conf->device_lock); | |
1573 | ||
1574 | handled++; | |
1575 | handle_stripe(sh); | |
1576 | release_stripe(sh); | |
1577 | ||
1578 | spin_lock_irq(&conf->device_lock); | |
1579 | } | |
1580 | PRINTK("%d stripes handled\n", handled); | |
1581 | ||
1582 | spin_unlock_irq(&conf->device_lock); | |
1583 | ||
1584 | unplug_slaves(mddev); | |
1585 | ||
1586 | PRINTK("--- raid5d inactive\n"); | |
1587 | } | |
1588 | ||
1589 | static int run (mddev_t *mddev) | |
1590 | { | |
1591 | raid5_conf_t *conf; | |
1592 | int raid_disk, memory; | |
1593 | mdk_rdev_t *rdev; | |
1594 | struct disk_info *disk; | |
1595 | struct list_head *tmp; | |
1596 | ||
1597 | if (mddev->level != 5 && mddev->level != 4) { | |
1598 | printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level); | |
1599 | return -EIO; | |
1600 | } | |
1601 | ||
1602 | mddev->private = kmalloc (sizeof (raid5_conf_t) | |
1603 | + mddev->raid_disks * sizeof(struct disk_info), | |
1604 | GFP_KERNEL); | |
1605 | if ((conf = mddev->private) == NULL) | |
1606 | goto abort; | |
1607 | memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) ); | |
1608 | conf->mddev = mddev; | |
1609 | ||
1610 | if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL) | |
1611 | goto abort; | |
1612 | memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE); | |
1613 | ||
1614 | spin_lock_init(&conf->device_lock); | |
1615 | init_waitqueue_head(&conf->wait_for_stripe); | |
1616 | init_waitqueue_head(&conf->wait_for_overlap); | |
1617 | INIT_LIST_HEAD(&conf->handle_list); | |
1618 | INIT_LIST_HEAD(&conf->delayed_list); | |
1619 | INIT_LIST_HEAD(&conf->inactive_list); | |
1620 | atomic_set(&conf->active_stripes, 0); | |
1621 | atomic_set(&conf->preread_active_stripes, 0); | |
1622 | ||
1da177e4 LT |
1623 | PRINTK("raid5: run(%s) called.\n", mdname(mddev)); |
1624 | ||
1625 | ITERATE_RDEV(mddev,rdev,tmp) { | |
1626 | raid_disk = rdev->raid_disk; | |
1627 | if (raid_disk >= mddev->raid_disks | |
1628 | || raid_disk < 0) | |
1629 | continue; | |
1630 | disk = conf->disks + raid_disk; | |
1631 | ||
1632 | disk->rdev = rdev; | |
1633 | ||
1634 | if (rdev->in_sync) { | |
1635 | char b[BDEVNAME_SIZE]; | |
1636 | printk(KERN_INFO "raid5: device %s operational as raid" | |
1637 | " disk %d\n", bdevname(rdev->bdev,b), | |
1638 | raid_disk); | |
1639 | conf->working_disks++; | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | conf->raid_disks = mddev->raid_disks; | |
1644 | /* | |
1645 | * 0 for a fully functional array, 1 for a degraded array. | |
1646 | */ | |
1647 | mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks; | |
1648 | conf->mddev = mddev; | |
1649 | conf->chunk_size = mddev->chunk_size; | |
1650 | conf->level = mddev->level; | |
1651 | conf->algorithm = mddev->layout; | |
1652 | conf->max_nr_stripes = NR_STRIPES; | |
1653 | ||
1654 | /* device size must be a multiple of chunk size */ | |
1655 | mddev->size &= ~(mddev->chunk_size/1024 -1); | |
b1581566 | 1656 | mddev->resync_max_sectors = mddev->size << 1; |
1da177e4 LT |
1657 | |
1658 | if (!conf->chunk_size || conf->chunk_size % 4) { | |
1659 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", | |
1660 | conf->chunk_size, mdname(mddev)); | |
1661 | goto abort; | |
1662 | } | |
1663 | if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) { | |
1664 | printk(KERN_ERR | |
1665 | "raid5: unsupported parity algorithm %d for %s\n", | |
1666 | conf->algorithm, mdname(mddev)); | |
1667 | goto abort; | |
1668 | } | |
1669 | if (mddev->degraded > 1) { | |
1670 | printk(KERN_ERR "raid5: not enough operational devices for %s" | |
1671 | " (%d/%d failed)\n", | |
1672 | mdname(mddev), conf->failed_disks, conf->raid_disks); | |
1673 | goto abort; | |
1674 | } | |
1675 | ||
1676 | if (mddev->degraded == 1 && | |
1677 | mddev->recovery_cp != MaxSector) { | |
1678 | printk(KERN_ERR | |
1679 | "raid5: cannot start dirty degraded array for %s\n", | |
1680 | mdname(mddev)); | |
1681 | goto abort; | |
1682 | } | |
1683 | ||
1684 | { | |
1685 | mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5"); | |
1686 | if (!mddev->thread) { | |
1687 | printk(KERN_ERR | |
1688 | "raid5: couldn't allocate thread for %s\n", | |
1689 | mdname(mddev)); | |
1690 | goto abort; | |
1691 | } | |
1692 | } | |
1693 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + | |
1694 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; | |
1695 | if (grow_stripes(conf, conf->max_nr_stripes)) { | |
1696 | printk(KERN_ERR | |
1697 | "raid5: couldn't allocate %dkB for buffers\n", memory); | |
1698 | shrink_stripes(conf); | |
1699 | md_unregister_thread(mddev->thread); | |
1700 | goto abort; | |
1701 | } else | |
1702 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", | |
1703 | memory, mdname(mddev)); | |
1704 | ||
1705 | if (mddev->degraded == 0) | |
1706 | printk("raid5: raid level %d set %s active with %d out of %d" | |
1707 | " devices, algorithm %d\n", conf->level, mdname(mddev), | |
1708 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, | |
1709 | conf->algorithm); | |
1710 | else | |
1711 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" | |
1712 | " out of %d devices, algorithm %d\n", conf->level, | |
1713 | mdname(mddev), mddev->raid_disks - mddev->degraded, | |
1714 | mddev->raid_disks, conf->algorithm); | |
1715 | ||
1716 | print_raid5_conf(conf); | |
1717 | ||
1718 | /* read-ahead size must cover two whole stripes, which is | |
1719 | * 2 * (n-1) * chunksize where 'n' is the number of raid devices | |
1720 | */ | |
1721 | { | |
1722 | int stripe = (mddev->raid_disks-1) * mddev->chunk_size | |
1723 | / PAGE_CACHE_SIZE; | |
1724 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) | |
1725 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | |
1726 | } | |
1727 | ||
1728 | /* Ok, everything is just fine now */ | |
7a5febe9 N |
1729 | |
1730 | mddev->queue->unplug_fn = raid5_unplug_device; | |
1731 | mddev->queue->issue_flush_fn = raid5_issue_flush; | |
1732 | ||
1da177e4 LT |
1733 | mddev->array_size = mddev->size * (mddev->raid_disks - 1); |
1734 | return 0; | |
1735 | abort: | |
1736 | if (conf) { | |
1737 | print_raid5_conf(conf); | |
1738 | if (conf->stripe_hashtbl) | |
1739 | free_pages((unsigned long) conf->stripe_hashtbl, | |
1740 | HASH_PAGES_ORDER); | |
1741 | kfree(conf); | |
1742 | } | |
1743 | mddev->private = NULL; | |
1744 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); | |
1745 | return -EIO; | |
1746 | } | |
1747 | ||
1748 | ||
1749 | ||
1750 | static int stop (mddev_t *mddev) | |
1751 | { | |
1752 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
1753 | ||
1754 | md_unregister_thread(mddev->thread); | |
1755 | mddev->thread = NULL; | |
1756 | shrink_stripes(conf); | |
1757 | free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER); | |
1758 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ | |
1759 | kfree(conf); | |
1760 | mddev->private = NULL; | |
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | #if RAID5_DEBUG | |
1765 | static void print_sh (struct stripe_head *sh) | |
1766 | { | |
1767 | int i; | |
1768 | ||
1769 | printk("sh %llu, pd_idx %d, state %ld.\n", | |
1770 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); | |
1771 | printk("sh %llu, count %d.\n", | |
1772 | (unsigned long long)sh->sector, atomic_read(&sh->count)); | |
1773 | printk("sh %llu, ", (unsigned long long)sh->sector); | |
1774 | for (i = 0; i < sh->raid_conf->raid_disks; i++) { | |
1775 | printk("(cache%d: %p %ld) ", | |
1776 | i, sh->dev[i].page, sh->dev[i].flags); | |
1777 | } | |
1778 | printk("\n"); | |
1779 | } | |
1780 | ||
1781 | static void printall (raid5_conf_t *conf) | |
1782 | { | |
1783 | struct stripe_head *sh; | |
1784 | int i; | |
1785 | ||
1786 | spin_lock_irq(&conf->device_lock); | |
1787 | for (i = 0; i < NR_HASH; i++) { | |
1788 | sh = conf->stripe_hashtbl[i]; | |
1789 | for (; sh; sh = sh->hash_next) { | |
1790 | if (sh->raid_conf != conf) | |
1791 | continue; | |
1792 | print_sh(sh); | |
1793 | } | |
1794 | } | |
1795 | spin_unlock_irq(&conf->device_lock); | |
1796 | } | |
1797 | #endif | |
1798 | ||
1799 | static void status (struct seq_file *seq, mddev_t *mddev) | |
1800 | { | |
1801 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | |
1802 | int i; | |
1803 | ||
1804 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); | |
1805 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks); | |
1806 | for (i = 0; i < conf->raid_disks; i++) | |
1807 | seq_printf (seq, "%s", | |
1808 | conf->disks[i].rdev && | |
1809 | conf->disks[i].rdev->in_sync ? "U" : "_"); | |
1810 | seq_printf (seq, "]"); | |
1811 | #if RAID5_DEBUG | |
1812 | #define D(x) \ | |
1813 | seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x)) | |
1814 | printall(conf); | |
1815 | #endif | |
1816 | } | |
1817 | ||
1818 | static void print_raid5_conf (raid5_conf_t *conf) | |
1819 | { | |
1820 | int i; | |
1821 | struct disk_info *tmp; | |
1822 | ||
1823 | printk("RAID5 conf printout:\n"); | |
1824 | if (!conf) { | |
1825 | printk("(conf==NULL)\n"); | |
1826 | return; | |
1827 | } | |
1828 | printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks, | |
1829 | conf->working_disks, conf->failed_disks); | |
1830 | ||
1831 | for (i = 0; i < conf->raid_disks; i++) { | |
1832 | char b[BDEVNAME_SIZE]; | |
1833 | tmp = conf->disks + i; | |
1834 | if (tmp->rdev) | |
1835 | printk(" disk %d, o:%d, dev:%s\n", | |
1836 | i, !tmp->rdev->faulty, | |
1837 | bdevname(tmp->rdev->bdev,b)); | |
1838 | } | |
1839 | } | |
1840 | ||
1841 | static int raid5_spare_active(mddev_t *mddev) | |
1842 | { | |
1843 | int i; | |
1844 | raid5_conf_t *conf = mddev->private; | |
1845 | struct disk_info *tmp; | |
1846 | ||
1847 | for (i = 0; i < conf->raid_disks; i++) { | |
1848 | tmp = conf->disks + i; | |
1849 | if (tmp->rdev | |
1850 | && !tmp->rdev->faulty | |
1851 | && !tmp->rdev->in_sync) { | |
1852 | mddev->degraded--; | |
1853 | conf->failed_disks--; | |
1854 | conf->working_disks++; | |
1855 | tmp->rdev->in_sync = 1; | |
1856 | } | |
1857 | } | |
1858 | print_raid5_conf(conf); | |
1859 | return 0; | |
1860 | } | |
1861 | ||
1862 | static int raid5_remove_disk(mddev_t *mddev, int number) | |
1863 | { | |
1864 | raid5_conf_t *conf = mddev->private; | |
1865 | int err = 0; | |
1866 | mdk_rdev_t *rdev; | |
1867 | struct disk_info *p = conf->disks + number; | |
1868 | ||
1869 | print_raid5_conf(conf); | |
1870 | rdev = p->rdev; | |
1871 | if (rdev) { | |
1872 | if (rdev->in_sync || | |
1873 | atomic_read(&rdev->nr_pending)) { | |
1874 | err = -EBUSY; | |
1875 | goto abort; | |
1876 | } | |
1877 | p->rdev = NULL; | |
fbd568a3 | 1878 | synchronize_rcu(); |
1da177e4 LT |
1879 | if (atomic_read(&rdev->nr_pending)) { |
1880 | /* lost the race, try later */ | |
1881 | err = -EBUSY; | |
1882 | p->rdev = rdev; | |
1883 | } | |
1884 | } | |
1885 | abort: | |
1886 | ||
1887 | print_raid5_conf(conf); | |
1888 | return err; | |
1889 | } | |
1890 | ||
1891 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) | |
1892 | { | |
1893 | raid5_conf_t *conf = mddev->private; | |
1894 | int found = 0; | |
1895 | int disk; | |
1896 | struct disk_info *p; | |
1897 | ||
1898 | if (mddev->degraded > 1) | |
1899 | /* no point adding a device */ | |
1900 | return 0; | |
1901 | ||
1902 | /* | |
1903 | * find the disk ... | |
1904 | */ | |
1905 | for (disk=0; disk < mddev->raid_disks; disk++) | |
1906 | if ((p=conf->disks + disk)->rdev == NULL) { | |
1907 | rdev->in_sync = 0; | |
1908 | rdev->raid_disk = disk; | |
1909 | found = 1; | |
1910 | p->rdev = rdev; | |
1911 | break; | |
1912 | } | |
1913 | print_raid5_conf(conf); | |
1914 | return found; | |
1915 | } | |
1916 | ||
1917 | static int raid5_resize(mddev_t *mddev, sector_t sectors) | |
1918 | { | |
1919 | /* no resync is happening, and there is enough space | |
1920 | * on all devices, so we can resize. | |
1921 | * We need to make sure resync covers any new space. | |
1922 | * If the array is shrinking we should possibly wait until | |
1923 | * any io in the removed space completes, but it hardly seems | |
1924 | * worth it. | |
1925 | */ | |
1926 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); | |
1927 | mddev->array_size = (sectors * (mddev->raid_disks-1))>>1; | |
1928 | set_capacity(mddev->gendisk, mddev->array_size << 1); | |
1929 | mddev->changed = 1; | |
1930 | if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) { | |
1931 | mddev->recovery_cp = mddev->size << 1; | |
1932 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | |
1933 | } | |
1934 | mddev->size = sectors /2; | |
4b5c7ae8 | 1935 | mddev->resync_max_sectors = sectors; |
1da177e4 LT |
1936 | return 0; |
1937 | } | |
1938 | ||
1939 | static mdk_personality_t raid5_personality= | |
1940 | { | |
1941 | .name = "raid5", | |
1942 | .owner = THIS_MODULE, | |
1943 | .make_request = make_request, | |
1944 | .run = run, | |
1945 | .stop = stop, | |
1946 | .status = status, | |
1947 | .error_handler = error, | |
1948 | .hot_add_disk = raid5_add_disk, | |
1949 | .hot_remove_disk= raid5_remove_disk, | |
1950 | .spare_active = raid5_spare_active, | |
1951 | .sync_request = sync_request, | |
1952 | .resize = raid5_resize, | |
1953 | }; | |
1954 | ||
1955 | static int __init raid5_init (void) | |
1956 | { | |
1957 | return register_md_personality (RAID5, &raid5_personality); | |
1958 | } | |
1959 | ||
1960 | static void raid5_exit (void) | |
1961 | { | |
1962 | unregister_md_personality (RAID5); | |
1963 | } | |
1964 | ||
1965 | module_init(raid5_init); | |
1966 | module_exit(raid5_exit); | |
1967 | MODULE_LICENSE("GPL"); | |
1968 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |