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