diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/md/raid5.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/md/raid5.c')
-rw-r--r-- | drivers/md/raid5.c | 1965 |
1 files changed, 1965 insertions, 0 deletions
diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c new file mode 100644 index 000000000000..52c3a81c4aa7 --- /dev/null +++ b/drivers/md/raid5.c | |||
@@ -0,0 +1,1965 @@ | |||
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 | |||
1414 | if (bio_data_dir(bi)==WRITE) { | ||
1415 | disk_stat_inc(mddev->gendisk, writes); | ||
1416 | disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi)); | ||
1417 | } else { | ||
1418 | disk_stat_inc(mddev->gendisk, reads); | ||
1419 | disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi)); | ||
1420 | } | ||
1421 | |||
1422 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); | ||
1423 | last_sector = bi->bi_sector + (bi->bi_size>>9); | ||
1424 | bi->bi_next = NULL; | ||
1425 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ | ||
1426 | if ( bio_data_dir(bi) == WRITE ) | ||
1427 | md_write_start(mddev); | ||
1428 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { | ||
1429 | DEFINE_WAIT(w); | ||
1430 | |||
1431 | new_sector = raid5_compute_sector(logical_sector, | ||
1432 | raid_disks, data_disks, &dd_idx, &pd_idx, conf); | ||
1433 | |||
1434 | PRINTK("raid5: make_request, sector %llu logical %llu\n", | ||
1435 | (unsigned long long)new_sector, | ||
1436 | (unsigned long long)logical_sector); | ||
1437 | |||
1438 | retry: | ||
1439 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); | ||
1440 | sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK)); | ||
1441 | if (sh) { | ||
1442 | if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { | ||
1443 | /* Add failed due to overlap. Flush everything | ||
1444 | * and wait a while | ||
1445 | */ | ||
1446 | raid5_unplug_device(mddev->queue); | ||
1447 | release_stripe(sh); | ||
1448 | schedule(); | ||
1449 | goto retry; | ||
1450 | } | ||
1451 | finish_wait(&conf->wait_for_overlap, &w); | ||
1452 | raid5_plug_device(conf); | ||
1453 | handle_stripe(sh); | ||
1454 | release_stripe(sh); | ||
1455 | |||
1456 | } else { | ||
1457 | /* cannot get stripe for read-ahead, just give-up */ | ||
1458 | clear_bit(BIO_UPTODATE, &bi->bi_flags); | ||
1459 | finish_wait(&conf->wait_for_overlap, &w); | ||
1460 | break; | ||
1461 | } | ||
1462 | |||
1463 | } | ||
1464 | spin_lock_irq(&conf->device_lock); | ||
1465 | if (--bi->bi_phys_segments == 0) { | ||
1466 | int bytes = bi->bi_size; | ||
1467 | |||
1468 | if ( bio_data_dir(bi) == WRITE ) | ||
1469 | md_write_end(mddev); | ||
1470 | bi->bi_size = 0; | ||
1471 | bi->bi_end_io(bi, bytes, 0); | ||
1472 | } | ||
1473 | spin_unlock_irq(&conf->device_lock); | ||
1474 | return 0; | ||
1475 | } | ||
1476 | |||
1477 | /* FIXME go_faster isn't used */ | ||
1478 | static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster) | ||
1479 | { | ||
1480 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | ||
1481 | struct stripe_head *sh; | ||
1482 | int sectors_per_chunk = conf->chunk_size >> 9; | ||
1483 | sector_t x; | ||
1484 | unsigned long stripe; | ||
1485 | int chunk_offset; | ||
1486 | int dd_idx, pd_idx; | ||
1487 | sector_t first_sector; | ||
1488 | int raid_disks = conf->raid_disks; | ||
1489 | int data_disks = raid_disks-1; | ||
1490 | |||
1491 | if (sector_nr >= mddev->size <<1) { | ||
1492 | /* just being told to finish up .. nothing much to do */ | ||
1493 | unplug_slaves(mddev); | ||
1494 | return 0; | ||
1495 | } | ||
1496 | /* if there is 1 or more failed drives and we are trying | ||
1497 | * to resync, then assert that we are finished, because there is | ||
1498 | * nothing we can do. | ||
1499 | */ | ||
1500 | if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { | ||
1501 | int rv = (mddev->size << 1) - sector_nr; | ||
1502 | md_done_sync(mddev, rv, 1); | ||
1503 | return rv; | ||
1504 | } | ||
1505 | |||
1506 | x = sector_nr; | ||
1507 | chunk_offset = sector_div(x, sectors_per_chunk); | ||
1508 | stripe = x; | ||
1509 | BUG_ON(x != stripe); | ||
1510 | |||
1511 | first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk | ||
1512 | + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf); | ||
1513 | sh = get_active_stripe(conf, sector_nr, pd_idx, 1); | ||
1514 | if (sh == NULL) { | ||
1515 | sh = get_active_stripe(conf, sector_nr, pd_idx, 0); | ||
1516 | /* make sure we don't swamp the stripe cache if someone else | ||
1517 | * is trying to get access | ||
1518 | */ | ||
1519 | set_current_state(TASK_UNINTERRUPTIBLE); | ||
1520 | schedule_timeout(1); | ||
1521 | } | ||
1522 | spin_lock(&sh->lock); | ||
1523 | set_bit(STRIPE_SYNCING, &sh->state); | ||
1524 | clear_bit(STRIPE_INSYNC, &sh->state); | ||
1525 | spin_unlock(&sh->lock); | ||
1526 | |||
1527 | handle_stripe(sh); | ||
1528 | release_stripe(sh); | ||
1529 | |||
1530 | return STRIPE_SECTORS; | ||
1531 | } | ||
1532 | |||
1533 | /* | ||
1534 | * This is our raid5 kernel thread. | ||
1535 | * | ||
1536 | * We scan the hash table for stripes which can be handled now. | ||
1537 | * During the scan, completed stripes are saved for us by the interrupt | ||
1538 | * handler, so that they will not have to wait for our next wakeup. | ||
1539 | */ | ||
1540 | static void raid5d (mddev_t *mddev) | ||
1541 | { | ||
1542 | struct stripe_head *sh; | ||
1543 | raid5_conf_t *conf = mddev_to_conf(mddev); | ||
1544 | int handled; | ||
1545 | |||
1546 | PRINTK("+++ raid5d active\n"); | ||
1547 | |||
1548 | md_check_recovery(mddev); | ||
1549 | md_handle_safemode(mddev); | ||
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 | |||
1623 | mddev->queue->unplug_fn = raid5_unplug_device; | ||
1624 | mddev->queue->issue_flush_fn = raid5_issue_flush; | ||
1625 | |||
1626 | PRINTK("raid5: run(%s) called.\n", mdname(mddev)); | ||
1627 | |||
1628 | ITERATE_RDEV(mddev,rdev,tmp) { | ||
1629 | raid_disk = rdev->raid_disk; | ||
1630 | if (raid_disk >= mddev->raid_disks | ||
1631 | || raid_disk < 0) | ||
1632 | continue; | ||
1633 | disk = conf->disks + raid_disk; | ||
1634 | |||
1635 | disk->rdev = rdev; | ||
1636 | |||
1637 | if (rdev->in_sync) { | ||
1638 | char b[BDEVNAME_SIZE]; | ||
1639 | printk(KERN_INFO "raid5: device %s operational as raid" | ||
1640 | " disk %d\n", bdevname(rdev->bdev,b), | ||
1641 | raid_disk); | ||
1642 | conf->working_disks++; | ||
1643 | } | ||
1644 | } | ||
1645 | |||
1646 | conf->raid_disks = mddev->raid_disks; | ||
1647 | /* | ||
1648 | * 0 for a fully functional array, 1 for a degraded array. | ||
1649 | */ | ||
1650 | mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks; | ||
1651 | conf->mddev = mddev; | ||
1652 | conf->chunk_size = mddev->chunk_size; | ||
1653 | conf->level = mddev->level; | ||
1654 | conf->algorithm = mddev->layout; | ||
1655 | conf->max_nr_stripes = NR_STRIPES; | ||
1656 | |||
1657 | /* device size must be a multiple of chunk size */ | ||
1658 | mddev->size &= ~(mddev->chunk_size/1024 -1); | ||
1659 | |||
1660 | if (!conf->chunk_size || conf->chunk_size % 4) { | ||
1661 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", | ||
1662 | conf->chunk_size, mdname(mddev)); | ||
1663 | goto abort; | ||
1664 | } | ||
1665 | if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) { | ||
1666 | printk(KERN_ERR | ||
1667 | "raid5: unsupported parity algorithm %d for %s\n", | ||
1668 | conf->algorithm, mdname(mddev)); | ||
1669 | goto abort; | ||
1670 | } | ||
1671 | if (mddev->degraded > 1) { | ||
1672 | printk(KERN_ERR "raid5: not enough operational devices for %s" | ||
1673 | " (%d/%d failed)\n", | ||
1674 | mdname(mddev), conf->failed_disks, conf->raid_disks); | ||
1675 | goto abort; | ||
1676 | } | ||
1677 | |||
1678 | if (mddev->degraded == 1 && | ||
1679 | mddev->recovery_cp != MaxSector) { | ||
1680 | printk(KERN_ERR | ||
1681 | "raid5: cannot start dirty degraded array for %s\n", | ||
1682 | mdname(mddev)); | ||
1683 | goto abort; | ||
1684 | } | ||
1685 | |||
1686 | { | ||
1687 | mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5"); | ||
1688 | if (!mddev->thread) { | ||
1689 | printk(KERN_ERR | ||
1690 | "raid5: couldn't allocate thread for %s\n", | ||
1691 | mdname(mddev)); | ||
1692 | goto abort; | ||
1693 | } | ||
1694 | } | ||
1695 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + | ||
1696 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; | ||
1697 | if (grow_stripes(conf, conf->max_nr_stripes)) { | ||
1698 | printk(KERN_ERR | ||
1699 | "raid5: couldn't allocate %dkB for buffers\n", memory); | ||
1700 | shrink_stripes(conf); | ||
1701 | md_unregister_thread(mddev->thread); | ||
1702 | goto abort; | ||
1703 | } else | ||
1704 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", | ||
1705 | memory, mdname(mddev)); | ||
1706 | |||
1707 | if (mddev->degraded == 0) | ||
1708 | printk("raid5: raid level %d set %s active with %d out of %d" | ||
1709 | " devices, algorithm %d\n", conf->level, mdname(mddev), | ||
1710 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, | ||
1711 | conf->algorithm); | ||
1712 | else | ||
1713 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" | ||
1714 | " out of %d devices, algorithm %d\n", conf->level, | ||
1715 | mdname(mddev), mddev->raid_disks - mddev->degraded, | ||
1716 | mddev->raid_disks, conf->algorithm); | ||
1717 | |||
1718 | print_raid5_conf(conf); | ||
1719 | |||
1720 | /* read-ahead size must cover two whole stripes, which is | ||
1721 | * 2 * (n-1) * chunksize where 'n' is the number of raid devices | ||
1722 | */ | ||
1723 | { | ||
1724 | int stripe = (mddev->raid_disks-1) * mddev->chunk_size | ||
1725 | / PAGE_CACHE_SIZE; | ||
1726 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) | ||
1727 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; | ||
1728 | } | ||
1729 | |||
1730 | /* Ok, everything is just fine now */ | ||
1731 | mddev->array_size = mddev->size * (mddev->raid_disks - 1); | ||
1732 | return 0; | ||
1733 | abort: | ||
1734 | if (conf) { | ||
1735 | print_raid5_conf(conf); | ||
1736 | if (conf->stripe_hashtbl) | ||
1737 | free_pages((unsigned long) conf->stripe_hashtbl, | ||
1738 | HASH_PAGES_ORDER); | ||
1739 | kfree(conf); | ||
1740 | } | ||
1741 | mddev->private = NULL; | ||
1742 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); | ||
1743 | return -EIO; | ||
1744 | } | ||
1745 | |||
1746 | |||
1747 | |||
1748 | static int stop (mddev_t *mddev) | ||
1749 | { | ||
1750 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | ||
1751 | |||
1752 | md_unregister_thread(mddev->thread); | ||
1753 | mddev->thread = NULL; | ||
1754 | shrink_stripes(conf); | ||
1755 | free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER); | ||
1756 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ | ||
1757 | kfree(conf); | ||
1758 | mddev->private = NULL; | ||
1759 | return 0; | ||
1760 | } | ||
1761 | |||
1762 | #if RAID5_DEBUG | ||
1763 | static void print_sh (struct stripe_head *sh) | ||
1764 | { | ||
1765 | int i; | ||
1766 | |||
1767 | printk("sh %llu, pd_idx %d, state %ld.\n", | ||
1768 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); | ||
1769 | printk("sh %llu, count %d.\n", | ||
1770 | (unsigned long long)sh->sector, atomic_read(&sh->count)); | ||
1771 | printk("sh %llu, ", (unsigned long long)sh->sector); | ||
1772 | for (i = 0; i < sh->raid_conf->raid_disks; i++) { | ||
1773 | printk("(cache%d: %p %ld) ", | ||
1774 | i, sh->dev[i].page, sh->dev[i].flags); | ||
1775 | } | ||
1776 | printk("\n"); | ||
1777 | } | ||
1778 | |||
1779 | static void printall (raid5_conf_t *conf) | ||
1780 | { | ||
1781 | struct stripe_head *sh; | ||
1782 | int i; | ||
1783 | |||
1784 | spin_lock_irq(&conf->device_lock); | ||
1785 | for (i = 0; i < NR_HASH; i++) { | ||
1786 | sh = conf->stripe_hashtbl[i]; | ||
1787 | for (; sh; sh = sh->hash_next) { | ||
1788 | if (sh->raid_conf != conf) | ||
1789 | continue; | ||
1790 | print_sh(sh); | ||
1791 | } | ||
1792 | } | ||
1793 | spin_unlock_irq(&conf->device_lock); | ||
1794 | } | ||
1795 | #endif | ||
1796 | |||
1797 | static void status (struct seq_file *seq, mddev_t *mddev) | ||
1798 | { | ||
1799 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; | ||
1800 | int i; | ||
1801 | |||
1802 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); | ||
1803 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks); | ||
1804 | for (i = 0; i < conf->raid_disks; i++) | ||
1805 | seq_printf (seq, "%s", | ||
1806 | conf->disks[i].rdev && | ||
1807 | conf->disks[i].rdev->in_sync ? "U" : "_"); | ||
1808 | seq_printf (seq, "]"); | ||
1809 | #if RAID5_DEBUG | ||
1810 | #define D(x) \ | ||
1811 | seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x)) | ||
1812 | printall(conf); | ||
1813 | #endif | ||
1814 | } | ||
1815 | |||
1816 | static void print_raid5_conf (raid5_conf_t *conf) | ||
1817 | { | ||
1818 | int i; | ||
1819 | struct disk_info *tmp; | ||
1820 | |||
1821 | printk("RAID5 conf printout:\n"); | ||
1822 | if (!conf) { | ||
1823 | printk("(conf==NULL)\n"); | ||
1824 | return; | ||
1825 | } | ||
1826 | printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks, | ||
1827 | conf->working_disks, conf->failed_disks); | ||
1828 | |||
1829 | for (i = 0; i < conf->raid_disks; i++) { | ||
1830 | char b[BDEVNAME_SIZE]; | ||
1831 | tmp = conf->disks + i; | ||
1832 | if (tmp->rdev) | ||
1833 | printk(" disk %d, o:%d, dev:%s\n", | ||
1834 | i, !tmp->rdev->faulty, | ||
1835 | bdevname(tmp->rdev->bdev,b)); | ||
1836 | } | ||
1837 | } | ||
1838 | |||
1839 | static int raid5_spare_active(mddev_t *mddev) | ||
1840 | { | ||
1841 | int i; | ||
1842 | raid5_conf_t *conf = mddev->private; | ||
1843 | struct disk_info *tmp; | ||
1844 | |||
1845 | for (i = 0; i < conf->raid_disks; i++) { | ||
1846 | tmp = conf->disks + i; | ||
1847 | if (tmp->rdev | ||
1848 | && !tmp->rdev->faulty | ||
1849 | && !tmp->rdev->in_sync) { | ||
1850 | mddev->degraded--; | ||
1851 | conf->failed_disks--; | ||
1852 | conf->working_disks++; | ||
1853 | tmp->rdev->in_sync = 1; | ||
1854 | } | ||
1855 | } | ||
1856 | print_raid5_conf(conf); | ||
1857 | return 0; | ||
1858 | } | ||
1859 | |||
1860 | static int raid5_remove_disk(mddev_t *mddev, int number) | ||
1861 | { | ||
1862 | raid5_conf_t *conf = mddev->private; | ||
1863 | int err = 0; | ||
1864 | mdk_rdev_t *rdev; | ||
1865 | struct disk_info *p = conf->disks + number; | ||
1866 | |||
1867 | print_raid5_conf(conf); | ||
1868 | rdev = p->rdev; | ||
1869 | if (rdev) { | ||
1870 | if (rdev->in_sync || | ||
1871 | atomic_read(&rdev->nr_pending)) { | ||
1872 | err = -EBUSY; | ||
1873 | goto abort; | ||
1874 | } | ||
1875 | p->rdev = NULL; | ||
1876 | synchronize_kernel(); | ||
1877 | if (atomic_read(&rdev->nr_pending)) { | ||
1878 | /* lost the race, try later */ | ||
1879 | err = -EBUSY; | ||
1880 | p->rdev = rdev; | ||
1881 | } | ||
1882 | } | ||
1883 | abort: | ||
1884 | |||
1885 | print_raid5_conf(conf); | ||
1886 | return err; | ||
1887 | } | ||
1888 | |||
1889 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) | ||
1890 | { | ||
1891 | raid5_conf_t *conf = mddev->private; | ||
1892 | int found = 0; | ||
1893 | int disk; | ||
1894 | struct disk_info *p; | ||
1895 | |||
1896 | if (mddev->degraded > 1) | ||
1897 | /* no point adding a device */ | ||
1898 | return 0; | ||
1899 | |||
1900 | /* | ||
1901 | * find the disk ... | ||
1902 | */ | ||
1903 | for (disk=0; disk < mddev->raid_disks; disk++) | ||
1904 | if ((p=conf->disks + disk)->rdev == NULL) { | ||
1905 | rdev->in_sync = 0; | ||
1906 | rdev->raid_disk = disk; | ||
1907 | found = 1; | ||
1908 | p->rdev = rdev; | ||
1909 | break; | ||
1910 | } | ||
1911 | print_raid5_conf(conf); | ||
1912 | return found; | ||
1913 | } | ||
1914 | |||
1915 | static int raid5_resize(mddev_t *mddev, sector_t sectors) | ||
1916 | { | ||
1917 | /* no resync is happening, and there is enough space | ||
1918 | * on all devices, so we can resize. | ||
1919 | * We need to make sure resync covers any new space. | ||
1920 | * If the array is shrinking we should possibly wait until | ||
1921 | * any io in the removed space completes, but it hardly seems | ||
1922 | * worth it. | ||
1923 | */ | ||
1924 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); | ||
1925 | mddev->array_size = (sectors * (mddev->raid_disks-1))>>1; | ||
1926 | set_capacity(mddev->gendisk, mddev->array_size << 1); | ||
1927 | mddev->changed = 1; | ||
1928 | if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) { | ||
1929 | mddev->recovery_cp = mddev->size << 1; | ||
1930 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); | ||
1931 | } | ||
1932 | mddev->size = sectors /2; | ||
1933 | return 0; | ||
1934 | } | ||
1935 | |||
1936 | static mdk_personality_t raid5_personality= | ||
1937 | { | ||
1938 | .name = "raid5", | ||
1939 | .owner = THIS_MODULE, | ||
1940 | .make_request = make_request, | ||
1941 | .run = run, | ||
1942 | .stop = stop, | ||
1943 | .status = status, | ||
1944 | .error_handler = error, | ||
1945 | .hot_add_disk = raid5_add_disk, | ||
1946 | .hot_remove_disk= raid5_remove_disk, | ||
1947 | .spare_active = raid5_spare_active, | ||
1948 | .sync_request = sync_request, | ||
1949 | .resize = raid5_resize, | ||
1950 | }; | ||
1951 | |||
1952 | static int __init raid5_init (void) | ||
1953 | { | ||
1954 | return register_md_personality (RAID5, &raid5_personality); | ||
1955 | } | ||
1956 | |||
1957 | static void raid5_exit (void) | ||
1958 | { | ||
1959 | unregister_md_personality (RAID5); | ||
1960 | } | ||
1961 | |||
1962 | module_init(raid5_init); | ||
1963 | module_exit(raid5_exit); | ||
1964 | MODULE_LICENSE("GPL"); | ||
1965 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ | ||