blob: ec5186fd510aa6e2794cfb0b7ec935cf303dd52a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
NeilBrown72626682005-09-09 16:23:54 -070027#include <linux/raid/bitmap.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
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
38#define HASH_PAGES 1
39#define HASH_PAGES_ORDER 0
40#define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
41#define HASH_MASK (NR_HASH - 1)
42
43#define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
44
45/* bio's attached to a stripe+device for I/O are linked together in bi_sector
46 * order without overlap. There may be several bio's per stripe+device, and
47 * a bio could span several devices.
48 * When walking this list for a particular stripe+device, we must never proceed
49 * beyond a bio that extends past this device, as the next bio might no longer
50 * be valid.
51 * This macro is used to determine the 'next' bio in the list, given the sector
52 * of the current stripe+device
53 */
54#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
55/*
56 * The following can be used to debug the driver
57 */
58#define RAID5_DEBUG 0
59#define RAID5_PARANOIA 1
60#if RAID5_PARANOIA && defined(CONFIG_SMP)
61# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
62#else
63# define CHECK_DEVLOCK()
64#endif
65
66#define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
67#if RAID5_DEBUG
68#define inline
69#define __inline__
70#endif
71
72static void print_raid5_conf (raid5_conf_t *conf);
73
74static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
75{
76 if (atomic_dec_and_test(&sh->count)) {
77 if (!list_empty(&sh->lru))
78 BUG();
79 if (atomic_read(&conf->active_stripes)==0)
80 BUG();
81 if (test_bit(STRIPE_HANDLE, &sh->state)) {
82 if (test_bit(STRIPE_DELAYED, &sh->state))
83 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -070084 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
85 conf->seq_write == sh->bm_seq)
86 list_add_tail(&sh->lru, &conf->bitmap_list);
87 else {
88 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -070090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 md_wakeup_thread(conf->mddev->thread);
92 } else {
93 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
94 atomic_dec(&conf->preread_active_stripes);
95 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
96 md_wakeup_thread(conf->mddev->thread);
97 }
98 list_add_tail(&sh->lru, &conf->inactive_list);
99 atomic_dec(&conf->active_stripes);
100 if (!conf->inactive_blocked ||
NeilBrown50368052005-12-12 02:39:17 -0800101 atomic_read(&conf->active_stripes) < (conf->max_nr_stripes*3/4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 wake_up(&conf->wait_for_stripe);
103 }
104 }
105}
106static void release_stripe(struct stripe_head *sh)
107{
108 raid5_conf_t *conf = sh->raid_conf;
109 unsigned long flags;
110
111 spin_lock_irqsave(&conf->device_lock, flags);
112 __release_stripe(conf, sh);
113 spin_unlock_irqrestore(&conf->device_lock, flags);
114}
115
116static void remove_hash(struct stripe_head *sh)
117{
118 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
119
120 if (sh->hash_pprev) {
121 if (sh->hash_next)
122 sh->hash_next->hash_pprev = sh->hash_pprev;
123 *sh->hash_pprev = sh->hash_next;
124 sh->hash_pprev = NULL;
125 }
126}
127
128static __inline__ void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
129{
130 struct stripe_head **shp = &stripe_hash(conf, sh->sector);
131
132 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
133
134 CHECK_DEVLOCK();
135 if ((sh->hash_next = *shp) != NULL)
136 (*shp)->hash_pprev = &sh->hash_next;
137 *shp = sh;
138 sh->hash_pprev = shp;
139}
140
141
142/* find an idle stripe, make sure it is unhashed, and return it. */
143static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
144{
145 struct stripe_head *sh = NULL;
146 struct list_head *first;
147
148 CHECK_DEVLOCK();
149 if (list_empty(&conf->inactive_list))
150 goto out;
151 first = conf->inactive_list.next;
152 sh = list_entry(first, struct stripe_head, lru);
153 list_del_init(first);
154 remove_hash(sh);
155 atomic_inc(&conf->active_stripes);
156out:
157 return sh;
158}
159
160static void shrink_buffers(struct stripe_head *sh, int num)
161{
162 struct page *p;
163 int i;
164
165 for (i=0; i<num ; i++) {
166 p = sh->dev[i].page;
167 if (!p)
168 continue;
169 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800170 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172}
173
174static int grow_buffers(struct stripe_head *sh, int num)
175{
176 int i;
177
178 for (i=0; i<num; i++) {
179 struct page *page;
180
181 if (!(page = alloc_page(GFP_KERNEL))) {
182 return 1;
183 }
184 sh->dev[i].page = page;
185 }
186 return 0;
187}
188
189static void raid5_build_block (struct stripe_head *sh, int i);
190
191static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
192{
193 raid5_conf_t *conf = sh->raid_conf;
194 int disks = conf->raid_disks, i;
195
196 if (atomic_read(&sh->count) != 0)
197 BUG();
198 if (test_bit(STRIPE_HANDLE, &sh->state))
199 BUG();
200
201 CHECK_DEVLOCK();
202 PRINTK("init_stripe called, stripe %llu\n",
203 (unsigned long long)sh->sector);
204
205 remove_hash(sh);
206
207 sh->sector = sector;
208 sh->pd_idx = pd_idx;
209 sh->state = 0;
210
211 for (i=disks; i--; ) {
212 struct r5dev *dev = &sh->dev[i];
213
214 if (dev->toread || dev->towrite || dev->written ||
215 test_bit(R5_LOCKED, &dev->flags)) {
216 printk("sector=%llx i=%d %p %p %p %d\n",
217 (unsigned long long)sh->sector, i, dev->toread,
218 dev->towrite, dev->written,
219 test_bit(R5_LOCKED, &dev->flags));
220 BUG();
221 }
222 dev->flags = 0;
223 raid5_build_block(sh, i);
224 }
225 insert_hash(conf, sh);
226}
227
228static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector)
229{
230 struct stripe_head *sh;
231
232 CHECK_DEVLOCK();
233 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
234 for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
235 if (sh->sector == sector)
236 return sh;
237 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
238 return NULL;
239}
240
241static void unplug_slaves(mddev_t *mddev);
242static void raid5_unplug_device(request_queue_t *q);
243
244static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector,
245 int pd_idx, int noblock)
246{
247 struct stripe_head *sh;
248
249 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
250
251 spin_lock_irq(&conf->device_lock);
252
253 do {
NeilBrown72626682005-09-09 16:23:54 -0700254 wait_event_lock_irq(conf->wait_for_stripe,
255 conf->quiesce == 0,
256 conf->device_lock, /* nothing */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 sh = __find_stripe(conf, sector);
258 if (!sh) {
259 if (!conf->inactive_blocked)
260 sh = get_free_stripe(conf);
261 if (noblock && sh == NULL)
262 break;
263 if (!sh) {
264 conf->inactive_blocked = 1;
265 wait_event_lock_irq(conf->wait_for_stripe,
266 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800267 (atomic_read(&conf->active_stripes)
268 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 || !conf->inactive_blocked),
270 conf->device_lock,
271 unplug_slaves(conf->mddev);
272 );
273 conf->inactive_blocked = 0;
274 } else
275 init_stripe(sh, sector, pd_idx);
276 } else {
277 if (atomic_read(&sh->count)) {
278 if (!list_empty(&sh->lru))
279 BUG();
280 } else {
281 if (!test_bit(STRIPE_HANDLE, &sh->state))
282 atomic_inc(&conf->active_stripes);
283 if (list_empty(&sh->lru))
284 BUG();
285 list_del_init(&sh->lru);
286 }
287 }
288 } while (sh == NULL);
289
290 if (sh)
291 atomic_inc(&sh->count);
292
293 spin_unlock_irq(&conf->device_lock);
294 return sh;
295}
296
NeilBrown3f294f42005-11-08 21:39:25 -0800297static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800300 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
301 if (!sh)
302 return 0;
303 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
304 sh->raid_conf = conf;
305 spin_lock_init(&sh->lock);
306
307 if (grow_buffers(sh, conf->raid_disks)) {
308 shrink_buffers(sh, conf->raid_disks);
309 kmem_cache_free(conf->slab_cache, sh);
310 return 0;
311 }
312 /* we just created an active stripe so... */
313 atomic_set(&sh->count, 1);
314 atomic_inc(&conf->active_stripes);
315 INIT_LIST_HEAD(&sh->lru);
316 release_stripe(sh);
317 return 1;
318}
319
320static int grow_stripes(raid5_conf_t *conf, int num)
321{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 kmem_cache_t *sc;
323 int devs = conf->raid_disks;
324
325 sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev));
326
327 sc = kmem_cache_create(conf->cache_name,
328 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
329 0, 0, NULL, NULL);
330 if (!sc)
331 return 1;
332 conf->slab_cache = sc;
333 while (num--) {
NeilBrown3f294f42005-11-08 21:39:25 -0800334 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 return 0;
338}
339
NeilBrown3f294f42005-11-08 21:39:25 -0800340static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct stripe_head *sh;
343
NeilBrown3f294f42005-11-08 21:39:25 -0800344 spin_lock_irq(&conf->device_lock);
345 sh = get_free_stripe(conf);
346 spin_unlock_irq(&conf->device_lock);
347 if (!sh)
348 return 0;
349 if (atomic_read(&sh->count))
350 BUG();
351 shrink_buffers(sh, conf->raid_disks);
352 kmem_cache_free(conf->slab_cache, sh);
353 atomic_dec(&conf->active_stripes);
354 return 1;
355}
356
357static void shrink_stripes(raid5_conf_t *conf)
358{
359 while (drop_one_stripe(conf))
360 ;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 kmem_cache_destroy(conf->slab_cache);
363 conf->slab_cache = NULL;
364}
365
NeilBrown4e5314b2005-11-08 21:39:22 -0800366static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int error)
368{
369 struct stripe_head *sh = bi->bi_private;
370 raid5_conf_t *conf = sh->raid_conf;
371 int disks = conf->raid_disks, i;
372 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
373
374 if (bi->bi_size)
375 return 1;
376
377 for (i=0 ; i<disks; i++)
378 if (bi == &sh->dev[i].req)
379 break;
380
381 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
382 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
383 uptodate);
384 if (i == disks) {
385 BUG();
386 return 0;
387 }
388
389 if (uptodate) {
390#if 0
391 struct bio *bio;
392 unsigned long flags;
393 spin_lock_irqsave(&conf->device_lock, flags);
394 /* we can return a buffer if we bypassed the cache or
395 * if the top buffer is not in highmem. If there are
396 * multiple buffers, leave the extra work to
397 * handle_stripe
398 */
399 buffer = sh->bh_read[i];
400 if (buffer &&
401 (!PageHighMem(buffer->b_page)
402 || buffer->b_page == bh->b_page )
403 ) {
404 sh->bh_read[i] = buffer->b_reqnext;
405 buffer->b_reqnext = NULL;
406 } else
407 buffer = NULL;
408 spin_unlock_irqrestore(&conf->device_lock, flags);
409 if (sh->bh_page[i]==bh->b_page)
410 set_buffer_uptodate(bh);
411 if (buffer) {
412 if (buffer->b_page != bh->b_page)
413 memcpy(buffer->b_data, bh->b_data, bh->b_size);
414 buffer->b_end_io(buffer, 1);
415 }
416#else
417 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -0800418#endif
419 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -0800420 printk(KERN_INFO "raid5: read error corrected!!\n");
NeilBrown4e5314b2005-11-08 21:39:22 -0800421 clear_bit(R5_ReadError, &sh->dev[i].flags);
422 clear_bit(R5_ReWrite, &sh->dev[i].flags);
423 }
NeilBrownba22dcb2005-11-08 21:39:31 -0800424 if (atomic_read(&conf->disks[i].rdev->read_errors))
425 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 } else {
NeilBrownba22dcb2005-11-08 21:39:31 -0800427 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -0800429 atomic_inc(&conf->disks[i].rdev->read_errors);
430 if (conf->mddev->degraded)
NeilBrown14f8d262006-01-06 00:20:14 -0800431 printk(KERN_WARNING "raid5: read error not correctable.\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800432 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -0800433 /* Oh, no!!! */
NeilBrown14f8d262006-01-06 00:20:14 -0800434 printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800435 else if (atomic_read(&conf->disks[i].rdev->read_errors)
436 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -0800437 printk(KERN_WARNING
438 "raid5: Too many read errors, failing device.\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800439 else
440 retry = 1;
441 if (retry)
442 set_bit(R5_ReadError, &sh->dev[i].flags);
443 else {
NeilBrown4e5314b2005-11-08 21:39:22 -0800444 clear_bit(R5_ReadError, &sh->dev[i].flags);
445 clear_bit(R5_ReWrite, &sh->dev[i].flags);
446 md_error(conf->mddev, conf->disks[i].rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -0800447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
450#if 0
451 /* must restore b_page before unlocking buffer... */
452 if (sh->bh_page[i] != bh->b_page) {
453 bh->b_page = sh->bh_page[i];
454 bh->b_data = page_address(bh->b_page);
455 clear_buffer_uptodate(bh);
456 }
457#endif
458 clear_bit(R5_LOCKED, &sh->dev[i].flags);
459 set_bit(STRIPE_HANDLE, &sh->state);
460 release_stripe(sh);
461 return 0;
462}
463
464static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
465 int error)
466{
467 struct stripe_head *sh = bi->bi_private;
468 raid5_conf_t *conf = sh->raid_conf;
469 int disks = conf->raid_disks, i;
470 unsigned long flags;
471 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
472
473 if (bi->bi_size)
474 return 1;
475
476 for (i=0 ; i<disks; i++)
477 if (bi == &sh->dev[i].req)
478 break;
479
480 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
481 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
482 uptodate);
483 if (i == disks) {
484 BUG();
485 return 0;
486 }
487
488 spin_lock_irqsave(&conf->device_lock, flags);
489 if (!uptodate)
490 md_error(conf->mddev, conf->disks[i].rdev);
491
492 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
493
494 clear_bit(R5_LOCKED, &sh->dev[i].flags);
495 set_bit(STRIPE_HANDLE, &sh->state);
496 __release_stripe(conf, sh);
497 spin_unlock_irqrestore(&conf->device_lock, flags);
498 return 0;
499}
500
501
502static sector_t compute_blocknr(struct stripe_head *sh, int i);
503
504static void raid5_build_block (struct stripe_head *sh, int i)
505{
506 struct r5dev *dev = &sh->dev[i];
507
508 bio_init(&dev->req);
509 dev->req.bi_io_vec = &dev->vec;
510 dev->req.bi_vcnt++;
511 dev->req.bi_max_vecs++;
512 dev->vec.bv_page = dev->page;
513 dev->vec.bv_len = STRIPE_SIZE;
514 dev->vec.bv_offset = 0;
515
516 dev->req.bi_sector = sh->sector;
517 dev->req.bi_private = sh;
518
519 dev->flags = 0;
520 if (i != sh->pd_idx)
521 dev->sector = compute_blocknr(sh, i);
522}
523
524static void error(mddev_t *mddev, mdk_rdev_t *rdev)
525{
526 char b[BDEVNAME_SIZE];
527 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
528 PRINTK("raid5: error called\n");
529
NeilBrownb2d444d2005-11-08 21:39:31 -0800530 if (!test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 mddev->sb_dirty = 1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800532 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 conf->working_disks--;
534 mddev->degraded++;
535 conf->failed_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800536 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * if recovery was running, make sure it aborts.
539 */
540 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
541 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800542 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 printk (KERN_ALERT
544 "raid5: Disk failure on %s, disabling device."
545 " Operation continuing on %d devices\n",
546 bdevname(rdev->bdev,b), conf->working_disks);
547 }
548}
549
550/*
551 * Input: a 'big' sector number,
552 * Output: index of the data and parity disk, and the sector # in them.
553 */
554static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
555 unsigned int data_disks, unsigned int * dd_idx,
556 unsigned int * pd_idx, raid5_conf_t *conf)
557{
558 long stripe;
559 unsigned long chunk_number;
560 unsigned int chunk_offset;
561 sector_t new_sector;
562 int sectors_per_chunk = conf->chunk_size >> 9;
563
564 /* First compute the information on this sector */
565
566 /*
567 * Compute the chunk number and the sector offset inside the chunk
568 */
569 chunk_offset = sector_div(r_sector, sectors_per_chunk);
570 chunk_number = r_sector;
571 BUG_ON(r_sector != chunk_number);
572
573 /*
574 * Compute the stripe number
575 */
576 stripe = chunk_number / data_disks;
577
578 /*
579 * Compute the data disk and parity disk indexes inside the stripe
580 */
581 *dd_idx = chunk_number % data_disks;
582
583 /*
584 * Select the parity disk based on the user selected algorithm.
585 */
586 if (conf->level == 4)
587 *pd_idx = data_disks;
588 else switch (conf->algorithm) {
589 case ALGORITHM_LEFT_ASYMMETRIC:
590 *pd_idx = data_disks - stripe % raid_disks;
591 if (*dd_idx >= *pd_idx)
592 (*dd_idx)++;
593 break;
594 case ALGORITHM_RIGHT_ASYMMETRIC:
595 *pd_idx = stripe % raid_disks;
596 if (*dd_idx >= *pd_idx)
597 (*dd_idx)++;
598 break;
599 case ALGORITHM_LEFT_SYMMETRIC:
600 *pd_idx = data_disks - stripe % raid_disks;
601 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
602 break;
603 case ALGORITHM_RIGHT_SYMMETRIC:
604 *pd_idx = stripe % raid_disks;
605 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
606 break;
607 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800608 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 conf->algorithm);
610 }
611
612 /*
613 * Finally, compute the new sector number
614 */
615 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
616 return new_sector;
617}
618
619
620static sector_t compute_blocknr(struct stripe_head *sh, int i)
621{
622 raid5_conf_t *conf = sh->raid_conf;
623 int raid_disks = conf->raid_disks, data_disks = raid_disks - 1;
624 sector_t new_sector = sh->sector, check;
625 int sectors_per_chunk = conf->chunk_size >> 9;
626 sector_t stripe;
627 int chunk_offset;
628 int chunk_number, dummy1, dummy2, dd_idx = i;
629 sector_t r_sector;
630
631 chunk_offset = sector_div(new_sector, sectors_per_chunk);
632 stripe = new_sector;
633 BUG_ON(new_sector != stripe);
634
635
636 switch (conf->algorithm) {
637 case ALGORITHM_LEFT_ASYMMETRIC:
638 case ALGORITHM_RIGHT_ASYMMETRIC:
639 if (i > sh->pd_idx)
640 i--;
641 break;
642 case ALGORITHM_LEFT_SYMMETRIC:
643 case ALGORITHM_RIGHT_SYMMETRIC:
644 if (i < sh->pd_idx)
645 i += raid_disks;
646 i -= (sh->pd_idx + 1);
647 break;
648 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800649 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 conf->algorithm);
651 }
652
653 chunk_number = stripe * data_disks + i;
654 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
655
656 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
657 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -0800658 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0;
660 }
661 return r_sector;
662}
663
664
665
666/*
667 * Copy data between a page in the stripe cache, and a bio.
668 * There are no alignment or size guarantees between the page or the
669 * bio except that there is some overlap.
670 * All iovecs in the bio must be considered.
671 */
672static void copy_data(int frombio, struct bio *bio,
673 struct page *page,
674 sector_t sector)
675{
676 char *pa = page_address(page);
677 struct bio_vec *bvl;
678 int i;
679 int page_offset;
680
681 if (bio->bi_sector >= sector)
682 page_offset = (signed)(bio->bi_sector - sector) * 512;
683 else
684 page_offset = (signed)(sector - bio->bi_sector) * -512;
685 bio_for_each_segment(bvl, bio, i) {
686 int len = bio_iovec_idx(bio,i)->bv_len;
687 int clen;
688 int b_offset = 0;
689
690 if (page_offset < 0) {
691 b_offset = -page_offset;
692 page_offset += b_offset;
693 len -= b_offset;
694 }
695
696 if (len > 0 && page_offset + len > STRIPE_SIZE)
697 clen = STRIPE_SIZE - page_offset;
698 else clen = len;
699
700 if (clen > 0) {
701 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
702 if (frombio)
703 memcpy(pa+page_offset, ba+b_offset, clen);
704 else
705 memcpy(ba+b_offset, pa+page_offset, clen);
706 __bio_kunmap_atomic(ba, KM_USER0);
707 }
708 if (clen < len) /* hit end of page */
709 break;
710 page_offset += len;
711 }
712}
713
714#define check_xor() do { \
715 if (count == MAX_XOR_BLOCKS) { \
716 xor_block(count, STRIPE_SIZE, ptr); \
717 count = 1; \
718 } \
719 } while(0)
720
721
722static void compute_block(struct stripe_head *sh, int dd_idx)
723{
724 raid5_conf_t *conf = sh->raid_conf;
725 int i, count, disks = conf->raid_disks;
726 void *ptr[MAX_XOR_BLOCKS], *p;
727
728 PRINTK("compute_block, stripe %llu, idx %d\n",
729 (unsigned long long)sh->sector, dd_idx);
730
731 ptr[0] = page_address(sh->dev[dd_idx].page);
732 memset(ptr[0], 0, STRIPE_SIZE);
733 count = 1;
734 for (i = disks ; i--; ) {
735 if (i == dd_idx)
736 continue;
737 p = page_address(sh->dev[i].page);
738 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
739 ptr[count++] = p;
740 else
NeilBrown14f8d262006-01-06 00:20:14 -0800741 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 " not present\n", dd_idx,
743 (unsigned long long)sh->sector, i);
744
745 check_xor();
746 }
747 if (count != 1)
748 xor_block(count, STRIPE_SIZE, ptr);
749 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
750}
751
752static void compute_parity(struct stripe_head *sh, int method)
753{
754 raid5_conf_t *conf = sh->raid_conf;
755 int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count;
756 void *ptr[MAX_XOR_BLOCKS];
757 struct bio *chosen;
758
759 PRINTK("compute_parity, stripe %llu, method %d\n",
760 (unsigned long long)sh->sector, method);
761
762 count = 1;
763 ptr[0] = page_address(sh->dev[pd_idx].page);
764 switch(method) {
765 case READ_MODIFY_WRITE:
766 if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
767 BUG();
768 for (i=disks ; i-- ;) {
769 if (i==pd_idx)
770 continue;
771 if (sh->dev[i].towrite &&
772 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
773 ptr[count++] = page_address(sh->dev[i].page);
774 chosen = sh->dev[i].towrite;
775 sh->dev[i].towrite = NULL;
776
777 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
778 wake_up(&conf->wait_for_overlap);
779
780 if (sh->dev[i].written) BUG();
781 sh->dev[i].written = chosen;
782 check_xor();
783 }
784 }
785 break;
786 case RECONSTRUCT_WRITE:
787 memset(ptr[0], 0, STRIPE_SIZE);
788 for (i= disks; i-- ;)
789 if (i!=pd_idx && sh->dev[i].towrite) {
790 chosen = sh->dev[i].towrite;
791 sh->dev[i].towrite = NULL;
792
793 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
794 wake_up(&conf->wait_for_overlap);
795
796 if (sh->dev[i].written) BUG();
797 sh->dev[i].written = chosen;
798 }
799 break;
800 case CHECK_PARITY:
801 break;
802 }
803 if (count>1) {
804 xor_block(count, STRIPE_SIZE, ptr);
805 count = 1;
806 }
807
808 for (i = disks; i--;)
809 if (sh->dev[i].written) {
810 sector_t sector = sh->dev[i].sector;
811 struct bio *wbi = sh->dev[i].written;
812 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
813 copy_data(1, wbi, sh->dev[i].page, sector);
814 wbi = r5_next_bio(wbi, sector);
815 }
816
817 set_bit(R5_LOCKED, &sh->dev[i].flags);
818 set_bit(R5_UPTODATE, &sh->dev[i].flags);
819 }
820
821 switch(method) {
822 case RECONSTRUCT_WRITE:
823 case CHECK_PARITY:
824 for (i=disks; i--;)
825 if (i != pd_idx) {
826 ptr[count++] = page_address(sh->dev[i].page);
827 check_xor();
828 }
829 break;
830 case READ_MODIFY_WRITE:
831 for (i = disks; i--;)
832 if (sh->dev[i].written) {
833 ptr[count++] = page_address(sh->dev[i].page);
834 check_xor();
835 }
836 }
837 if (count != 1)
838 xor_block(count, STRIPE_SIZE, ptr);
839
840 if (method != CHECK_PARITY) {
841 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
842 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
843 } else
844 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
845}
846
847/*
848 * Each stripe/dev can have one or more bion attached.
849 * toread/towrite point to the first in a chain.
850 * The bi_next chain must be in order.
851 */
852static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
853{
854 struct bio **bip;
855 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -0700856 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 PRINTK("adding bh b#%llu to stripe s#%llu\n",
859 (unsigned long long)bi->bi_sector,
860 (unsigned long long)sh->sector);
861
862
863 spin_lock(&sh->lock);
864 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -0700865 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -0700867 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
868 firstwrite = 1;
869 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 bip = &sh->dev[dd_idx].toread;
871 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
872 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
873 goto overlap;
874 bip = & (*bip)->bi_next;
875 }
876 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
877 goto overlap;
878
879 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
880 BUG();
881 if (*bip)
882 bi->bi_next = *bip;
883 *bip = bi;
884 bi->bi_phys_segments ++;
885 spin_unlock_irq(&conf->device_lock);
886 spin_unlock(&sh->lock);
887
888 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
889 (unsigned long long)bi->bi_sector,
890 (unsigned long long)sh->sector, dd_idx);
891
NeilBrown72626682005-09-09 16:23:54 -0700892 if (conf->mddev->bitmap && firstwrite) {
893 sh->bm_seq = conf->seq_write;
894 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
895 STRIPE_SECTORS, 0);
896 set_bit(STRIPE_BIT_DELAY, &sh->state);
897 }
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (forwrite) {
900 /* check if page is covered */
901 sector_t sector = sh->dev[dd_idx].sector;
902 for (bi=sh->dev[dd_idx].towrite;
903 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
904 bi && bi->bi_sector <= sector;
905 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
906 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
907 sector = bi->bi_sector + (bi->bi_size>>9);
908 }
909 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
910 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
911 }
912 return 1;
913
914 overlap:
915 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
916 spin_unlock_irq(&conf->device_lock);
917 spin_unlock(&sh->lock);
918 return 0;
919}
920
921
922/*
923 * handle_stripe - do things to a stripe.
924 *
925 * We lock the stripe and then examine the state of various bits
926 * to see what needs to be done.
927 * Possible results:
928 * return some read request which now have data
929 * return some write requests which are safely on disc
930 * schedule a read on some buffers
931 * schedule a write of some buffers
932 * return confirmation of parity correctness
933 *
934 * Parity calculations are done inside the stripe lock
935 * buffers are taken off read_list or write_list, and bh_cache buffers
936 * get BH_Lock set before the stripe lock is released.
937 *
938 */
939
940static void handle_stripe(struct stripe_head *sh)
941{
942 raid5_conf_t *conf = sh->raid_conf;
943 int disks = conf->raid_disks;
944 struct bio *return_bi= NULL;
945 struct bio *bi;
946 int i;
947 int syncing;
948 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
949 int non_overwrite = 0;
950 int failed_num=0;
951 struct r5dev *dev;
952
953 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
954 (unsigned long long)sh->sector, atomic_read(&sh->count),
955 sh->pd_idx);
956
957 spin_lock(&sh->lock);
958 clear_bit(STRIPE_HANDLE, &sh->state);
959 clear_bit(STRIPE_DELAYED, &sh->state);
960
961 syncing = test_bit(STRIPE_SYNCING, &sh->state);
962 /* Now to look around and see what can be done */
963
NeilBrown9910f162006-01-06 00:20:24 -0800964 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 for (i=disks; i--; ) {
966 mdk_rdev_t *rdev;
967 dev = &sh->dev[i];
968 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
971 i, dev->flags, dev->toread, dev->towrite, dev->written);
972 /* maybe we can reply to a read */
973 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
974 struct bio *rbi, *rbi2;
975 PRINTK("Return read for disc %d\n", i);
976 spin_lock_irq(&conf->device_lock);
977 rbi = dev->toread;
978 dev->toread = NULL;
979 if (test_and_clear_bit(R5_Overlap, &dev->flags))
980 wake_up(&conf->wait_for_overlap);
981 spin_unlock_irq(&conf->device_lock);
982 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
983 copy_data(0, rbi, dev->page, dev->sector);
984 rbi2 = r5_next_bio(rbi, dev->sector);
985 spin_lock_irq(&conf->device_lock);
986 if (--rbi->bi_phys_segments == 0) {
987 rbi->bi_next = return_bi;
988 return_bi = rbi;
989 }
990 spin_unlock_irq(&conf->device_lock);
991 rbi = rbi2;
992 }
993 }
994
995 /* now count some things */
996 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
997 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
998
999
1000 if (dev->toread) to_read++;
1001 if (dev->towrite) {
1002 to_write++;
1003 if (!test_bit(R5_OVERWRITE, &dev->flags))
1004 non_overwrite++;
1005 }
1006 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001007 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001008 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001009 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08001010 clear_bit(R5_ReadError, &dev->flags);
1011 clear_bit(R5_ReWrite, &dev->flags);
1012 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001013 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001014 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 failed++;
1016 failed_num = i;
1017 } else
1018 set_bit(R5_Insync, &dev->flags);
1019 }
NeilBrown9910f162006-01-06 00:20:24 -08001020 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 PRINTK("locked=%d uptodate=%d to_read=%d"
1022 " to_write=%d failed=%d failed_num=%d\n",
1023 locked, uptodate, to_read, to_write, failed, failed_num);
1024 /* check if the array has lost two devices and, if so, some requests might
1025 * need to be failed
1026 */
1027 if (failed > 1 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 for (i=disks; i--; ) {
NeilBrown72626682005-09-09 16:23:54 -07001029 int bitmap_end = 0;
NeilBrown4e5314b2005-11-08 21:39:22 -08001030
1031 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001032 mdk_rdev_t *rdev;
1033 rcu_read_lock();
1034 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001035 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001036 /* multiple read failures in one stripe */
1037 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001038 rcu_read_unlock();
NeilBrown4e5314b2005-11-08 21:39:22 -08001039 }
1040
NeilBrown72626682005-09-09 16:23:54 -07001041 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* fail all writes first */
1043 bi = sh->dev[i].towrite;
1044 sh->dev[i].towrite = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001045 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1048 wake_up(&conf->wait_for_overlap);
1049
1050 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1051 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1052 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1053 if (--bi->bi_phys_segments == 0) {
1054 md_write_end(conf->mddev);
1055 bi->bi_next = return_bi;
1056 return_bi = bi;
1057 }
1058 bi = nextbi;
1059 }
1060 /* and fail all 'written' */
1061 bi = sh->dev[i].written;
1062 sh->dev[i].written = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001063 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1065 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1066 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1067 if (--bi->bi_phys_segments == 0) {
1068 md_write_end(conf->mddev);
1069 bi->bi_next = return_bi;
1070 return_bi = bi;
1071 }
1072 bi = bi2;
1073 }
1074
1075 /* fail any reads if this device is non-operational */
NeilBrown4e5314b2005-11-08 21:39:22 -08001076 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1077 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 bi = sh->dev[i].toread;
1079 sh->dev[i].toread = NULL;
1080 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1081 wake_up(&conf->wait_for_overlap);
1082 if (bi) to_read--;
1083 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1084 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1085 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1086 if (--bi->bi_phys_segments == 0) {
1087 bi->bi_next = return_bi;
1088 return_bi = bi;
1089 }
1090 bi = nextbi;
1091 }
1092 }
NeilBrown72626682005-09-09 16:23:54 -07001093 spin_unlock_irq(&conf->device_lock);
1094 if (bitmap_end)
1095 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1096 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 if (failed > 1 && syncing) {
1100 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1101 clear_bit(STRIPE_SYNCING, &sh->state);
1102 syncing = 0;
1103 }
1104
1105 /* might be able to return some write requests if the parity block
1106 * is safe, or on a failed drive
1107 */
1108 dev = &sh->dev[sh->pd_idx];
1109 if ( written &&
1110 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1111 test_bit(R5_UPTODATE, &dev->flags))
1112 || (failed == 1 && failed_num == sh->pd_idx))
1113 ) {
1114 /* any written block on an uptodate or failed drive can be returned.
1115 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1116 * never LOCKED, so we don't need to test 'failed' directly.
1117 */
1118 for (i=disks; i--; )
1119 if (sh->dev[i].written) {
1120 dev = &sh->dev[i];
1121 if (!test_bit(R5_LOCKED, &dev->flags) &&
1122 test_bit(R5_UPTODATE, &dev->flags) ) {
1123 /* We can return any write requests */
1124 struct bio *wbi, *wbi2;
NeilBrown72626682005-09-09 16:23:54 -07001125 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 PRINTK("Return write for disc %d\n", i);
1127 spin_lock_irq(&conf->device_lock);
1128 wbi = dev->written;
1129 dev->written = NULL;
1130 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1131 wbi2 = r5_next_bio(wbi, dev->sector);
1132 if (--wbi->bi_phys_segments == 0) {
1133 md_write_end(conf->mddev);
1134 wbi->bi_next = return_bi;
1135 return_bi = wbi;
1136 }
1137 wbi = wbi2;
1138 }
NeilBrown72626682005-09-09 16:23:54 -07001139 if (dev->towrite == NULL)
1140 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001142 if (bitmap_end)
1143 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1144 STRIPE_SECTORS,
1145 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 }
1148 }
1149
1150 /* Now we might consider reading some blocks, either to check/generate
1151 * parity, or to satisfy requests
1152 * or to load a block that is being partially written.
1153 */
1154 if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
1155 for (i=disks; i--;) {
1156 dev = &sh->dev[i];
1157 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1158 (dev->toread ||
1159 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1160 syncing ||
1161 (failed && (sh->dev[failed_num].toread ||
1162 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1163 )
1164 ) {
1165 /* we would like to get this block, possibly
1166 * by computing it, but we might not be able to
1167 */
1168 if (uptodate == disks-1) {
1169 PRINTK("Computing block %d\n", i);
1170 compute_block(sh, i);
1171 uptodate++;
1172 } else if (test_bit(R5_Insync, &dev->flags)) {
1173 set_bit(R5_LOCKED, &dev->flags);
1174 set_bit(R5_Wantread, &dev->flags);
1175#if 0
1176 /* if I am just reading this block and we don't have
1177 a failed drive, or any pending writes then sidestep the cache */
1178 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1179 ! syncing && !failed && !to_write) {
1180 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1181 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1182 }
1183#endif
1184 locked++;
1185 PRINTK("Reading block %d (sync=%d)\n",
1186 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 }
1189 }
1190 set_bit(STRIPE_HANDLE, &sh->state);
1191 }
1192
1193 /* now to consider writing and what else, if anything should be read */
1194 if (to_write) {
1195 int rmw=0, rcw=0;
1196 for (i=disks ; i--;) {
1197 /* would I have to read this buffer for read_modify_write */
1198 dev = &sh->dev[i];
1199 if ((dev->towrite || i == sh->pd_idx) &&
1200 (!test_bit(R5_LOCKED, &dev->flags)
1201#if 0
1202|| sh->bh_page[i]!=bh->b_page
1203#endif
1204 ) &&
1205 !test_bit(R5_UPTODATE, &dev->flags)) {
1206 if (test_bit(R5_Insync, &dev->flags)
1207/* && !(!mddev->insync && i == sh->pd_idx) */
1208 )
1209 rmw++;
1210 else rmw += 2*disks; /* cannot read it */
1211 }
1212 /* Would I have to read this buffer for reconstruct_write */
1213 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1214 (!test_bit(R5_LOCKED, &dev->flags)
1215#if 0
1216|| sh->bh_page[i] != bh->b_page
1217#endif
1218 ) &&
1219 !test_bit(R5_UPTODATE, &dev->flags)) {
1220 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1221 else rcw += 2*disks;
1222 }
1223 }
1224 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1225 (unsigned long long)sh->sector, rmw, rcw);
1226 set_bit(STRIPE_HANDLE, &sh->state);
1227 if (rmw < rcw && rmw > 0)
1228 /* prefer read-modify-write, but need to get some data */
1229 for (i=disks; i--;) {
1230 dev = &sh->dev[i];
1231 if ((dev->towrite || i == sh->pd_idx) &&
1232 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1233 test_bit(R5_Insync, &dev->flags)) {
1234 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1235 {
1236 PRINTK("Read_old block %d for r-m-w\n", i);
1237 set_bit(R5_LOCKED, &dev->flags);
1238 set_bit(R5_Wantread, &dev->flags);
1239 locked++;
1240 } else {
1241 set_bit(STRIPE_DELAYED, &sh->state);
1242 set_bit(STRIPE_HANDLE, &sh->state);
1243 }
1244 }
1245 }
1246 if (rcw <= rmw && rcw > 0)
1247 /* want reconstruct write, but need to get some data */
1248 for (i=disks; i--;) {
1249 dev = &sh->dev[i];
1250 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1251 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1252 test_bit(R5_Insync, &dev->flags)) {
1253 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1254 {
1255 PRINTK("Read_old block %d for Reconstruct\n", i);
1256 set_bit(R5_LOCKED, &dev->flags);
1257 set_bit(R5_Wantread, &dev->flags);
1258 locked++;
1259 } else {
1260 set_bit(STRIPE_DELAYED, &sh->state);
1261 set_bit(STRIPE_HANDLE, &sh->state);
1262 }
1263 }
1264 }
1265 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown72626682005-09-09 16:23:54 -07001266 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1267 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 PRINTK("Computing parity...\n");
1269 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1270 /* now every locked buffer is ready to be written */
1271 for (i=disks; i--;)
1272 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1273 PRINTK("Writing block %d\n", i);
1274 locked++;
1275 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1276 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1277 || (i==sh->pd_idx && failed == 0))
1278 set_bit(STRIPE_INSYNC, &sh->state);
1279 }
1280 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1281 atomic_dec(&conf->preread_active_stripes);
1282 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1283 md_wakeup_thread(conf->mddev->thread);
1284 }
1285 }
1286 }
1287
1288 /* maybe we need to check and possibly fix the parity for this stripe
1289 * Any reads will already have been scheduled, so we just see if enough data
1290 * is available
1291 */
1292 if (syncing && locked == 0 &&
NeilBrown14f8d262006-01-06 00:20:14 -08001293 !test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 set_bit(STRIPE_HANDLE, &sh->state);
1295 if (failed == 0) {
1296 char *pagea;
1297 if (uptodate != disks)
1298 BUG();
1299 compute_parity(sh, CHECK_PARITY);
1300 uptodate--;
1301 pagea = page_address(sh->dev[sh->pd_idx].page);
1302 if ((*(u32*)pagea) == 0 &&
1303 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1304 /* parity is correct (on disc, not in buffer any more) */
1305 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown9d888832005-11-08 21:39:26 -08001306 } else {
1307 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1308 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1309 /* don't try to repair!! */
1310 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown14f8d262006-01-06 00:20:14 -08001311 else {
1312 compute_block(sh, sh->pd_idx);
1313 uptodate++;
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316 }
1317 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001318 /* either failed parity check, or recovery is happening */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (failed==0)
1320 failed_num = sh->pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 dev = &sh->dev[failed_num];
NeilBrown14f8d262006-01-06 00:20:14 -08001322 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1323 BUG_ON(uptodate != disks);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 set_bit(R5_LOCKED, &dev->flags);
1326 set_bit(R5_Wantwrite, &dev->flags);
NeilBrown72626682005-09-09 16:23:54 -07001327 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 locked++;
1329 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 }
1332 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1333 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1334 clear_bit(STRIPE_SYNCING, &sh->state);
1335 }
NeilBrown4e5314b2005-11-08 21:39:22 -08001336
1337 /* If the failed drive is just a ReadError, then we might need to progress
1338 * the repair/check process
1339 */
NeilBrownba22dcb2005-11-08 21:39:31 -08001340 if (failed == 1 && ! conf->mddev->ro &&
1341 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001342 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1343 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1344 ) {
1345 dev = &sh->dev[failed_num];
1346 if (!test_bit(R5_ReWrite, &dev->flags)) {
1347 set_bit(R5_Wantwrite, &dev->flags);
1348 set_bit(R5_ReWrite, &dev->flags);
1349 set_bit(R5_LOCKED, &dev->flags);
1350 } else {
1351 /* let's read it back */
1352 set_bit(R5_Wantread, &dev->flags);
1353 set_bit(R5_LOCKED, &dev->flags);
1354 }
1355 }
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 spin_unlock(&sh->lock);
1358
1359 while ((bi=return_bi)) {
1360 int bytes = bi->bi_size;
1361
1362 return_bi = bi->bi_next;
1363 bi->bi_next = NULL;
1364 bi->bi_size = 0;
1365 bi->bi_end_io(bi, bytes, 0);
1366 }
1367 for (i=disks; i-- ;) {
1368 int rw;
1369 struct bio *bi;
1370 mdk_rdev_t *rdev;
1371 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1372 rw = 1;
1373 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1374 rw = 0;
1375 else
1376 continue;
1377
1378 bi = &sh->dev[i].req;
1379
1380 bi->bi_rw = rw;
1381 if (rw)
1382 bi->bi_end_io = raid5_end_write_request;
1383 else
1384 bi->bi_end_io = raid5_end_read_request;
1385
1386 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001387 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001388 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 rdev = NULL;
1390 if (rdev)
1391 atomic_inc(&rdev->nr_pending);
1392 rcu_read_unlock();
1393
1394 if (rdev) {
NeilBrown9910f162006-01-06 00:20:24 -08001395 if (syncing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1397
1398 bi->bi_bdev = rdev->bdev;
1399 PRINTK("for %llu schedule op %ld on disc %d\n",
1400 (unsigned long long)sh->sector, bi->bi_rw, i);
1401 atomic_inc(&sh->count);
1402 bi->bi_sector = sh->sector + rdev->data_offset;
1403 bi->bi_flags = 1 << BIO_UPTODATE;
1404 bi->bi_vcnt = 1;
1405 bi->bi_max_vecs = 1;
1406 bi->bi_idx = 0;
1407 bi->bi_io_vec = &sh->dev[i].vec;
1408 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1409 bi->bi_io_vec[0].bv_offset = 0;
1410 bi->bi_size = STRIPE_SIZE;
1411 bi->bi_next = NULL;
1412 generic_make_request(bi);
1413 } else {
NeilBrown72626682005-09-09 16:23:54 -07001414 if (rw == 1)
1415 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 PRINTK("skip op %ld on disc %d for sector %llu\n",
1417 bi->bi_rw, i, (unsigned long long)sh->sector);
1418 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1419 set_bit(STRIPE_HANDLE, &sh->state);
1420 }
1421 }
1422}
1423
1424static inline void raid5_activate_delayed(raid5_conf_t *conf)
1425{
1426 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1427 while (!list_empty(&conf->delayed_list)) {
1428 struct list_head *l = conf->delayed_list.next;
1429 struct stripe_head *sh;
1430 sh = list_entry(l, struct stripe_head, lru);
1431 list_del_init(l);
1432 clear_bit(STRIPE_DELAYED, &sh->state);
1433 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1434 atomic_inc(&conf->preread_active_stripes);
1435 list_add_tail(&sh->lru, &conf->handle_list);
1436 }
1437 }
1438}
1439
NeilBrown72626682005-09-09 16:23:54 -07001440static inline void activate_bit_delay(raid5_conf_t *conf)
1441{
1442 /* device_lock is held */
1443 struct list_head head;
1444 list_add(&head, &conf->bitmap_list);
1445 list_del_init(&conf->bitmap_list);
1446 while (!list_empty(&head)) {
1447 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1448 list_del_init(&sh->lru);
1449 atomic_inc(&sh->count);
1450 __release_stripe(conf, sh);
1451 }
1452}
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454static void unplug_slaves(mddev_t *mddev)
1455{
1456 raid5_conf_t *conf = mddev_to_conf(mddev);
1457 int i;
1458
1459 rcu_read_lock();
1460 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08001461 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001462 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1464
1465 atomic_inc(&rdev->nr_pending);
1466 rcu_read_unlock();
1467
1468 if (r_queue->unplug_fn)
1469 r_queue->unplug_fn(r_queue);
1470
1471 rdev_dec_pending(rdev, mddev);
1472 rcu_read_lock();
1473 }
1474 }
1475 rcu_read_unlock();
1476}
1477
1478static void raid5_unplug_device(request_queue_t *q)
1479{
1480 mddev_t *mddev = q->queuedata;
1481 raid5_conf_t *conf = mddev_to_conf(mddev);
1482 unsigned long flags;
1483
1484 spin_lock_irqsave(&conf->device_lock, flags);
1485
NeilBrown72626682005-09-09 16:23:54 -07001486 if (blk_remove_plug(q)) {
1487 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 md_wakeup_thread(mddev->thread);
1491
1492 spin_unlock_irqrestore(&conf->device_lock, flags);
1493
1494 unplug_slaves(mddev);
1495}
1496
1497static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1498 sector_t *error_sector)
1499{
1500 mddev_t *mddev = q->queuedata;
1501 raid5_conf_t *conf = mddev_to_conf(mddev);
1502 int i, ret = 0;
1503
1504 rcu_read_lock();
1505 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08001506 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001507 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct block_device *bdev = rdev->bdev;
1509 request_queue_t *r_queue = bdev_get_queue(bdev);
1510
1511 if (!r_queue->issue_flush_fn)
1512 ret = -EOPNOTSUPP;
1513 else {
1514 atomic_inc(&rdev->nr_pending);
1515 rcu_read_unlock();
1516 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1517 error_sector);
1518 rdev_dec_pending(rdev, mddev);
1519 rcu_read_lock();
1520 }
1521 }
1522 }
1523 rcu_read_unlock();
1524 return ret;
1525}
1526
1527static inline void raid5_plug_device(raid5_conf_t *conf)
1528{
1529 spin_lock_irq(&conf->device_lock);
1530 blk_plug_device(conf->mddev->queue);
1531 spin_unlock_irq(&conf->device_lock);
1532}
1533
1534static int make_request (request_queue_t *q, struct bio * bi)
1535{
1536 mddev_t *mddev = q->queuedata;
1537 raid5_conf_t *conf = mddev_to_conf(mddev);
1538 const unsigned int raid_disks = conf->raid_disks;
1539 const unsigned int data_disks = raid_disks - 1;
1540 unsigned int dd_idx, pd_idx;
1541 sector_t new_sector;
1542 sector_t logical_sector, last_sector;
1543 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01001544 const int rw = bio_data_dir(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
NeilBrowne5dcdd82005-09-09 16:23:41 -07001546 if (unlikely(bio_barrier(bi))) {
1547 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1548 return 0;
1549 }
1550
NeilBrown3d310eb2005-06-21 17:17:26 -07001551 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07001552
Jens Axboea3623572005-11-01 09:26:16 +01001553 disk_stat_inc(mddev->gendisk, ios[rw]);
1554 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1557 last_sector = bi->bi_sector + (bi->bi_size>>9);
1558 bi->bi_next = NULL;
1559 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1562 DEFINE_WAIT(w);
1563
1564 new_sector = raid5_compute_sector(logical_sector,
1565 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1566
1567 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1568 (unsigned long long)new_sector,
1569 (unsigned long long)logical_sector);
1570
1571 retry:
1572 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1573 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1574 if (sh) {
1575 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1576 /* Add failed due to overlap. Flush everything
1577 * and wait a while
1578 */
1579 raid5_unplug_device(mddev->queue);
1580 release_stripe(sh);
1581 schedule();
1582 goto retry;
1583 }
1584 finish_wait(&conf->wait_for_overlap, &w);
1585 raid5_plug_device(conf);
1586 handle_stripe(sh);
1587 release_stripe(sh);
1588
1589 } else {
1590 /* cannot get stripe for read-ahead, just give-up */
1591 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1592 finish_wait(&conf->wait_for_overlap, &w);
1593 break;
1594 }
1595
1596 }
1597 spin_lock_irq(&conf->device_lock);
1598 if (--bi->bi_phys_segments == 0) {
1599 int bytes = bi->bi_size;
1600
1601 if ( bio_data_dir(bi) == WRITE )
1602 md_write_end(mddev);
1603 bi->bi_size = 0;
1604 bi->bi_end_io(bi, bytes, 0);
1605 }
1606 spin_unlock_irq(&conf->device_lock);
1607 return 0;
1608}
1609
1610/* FIXME go_faster isn't used */
NeilBrown57afd892005-06-21 17:17:13 -07001611static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1614 struct stripe_head *sh;
1615 int sectors_per_chunk = conf->chunk_size >> 9;
1616 sector_t x;
1617 unsigned long stripe;
1618 int chunk_offset;
1619 int dd_idx, pd_idx;
1620 sector_t first_sector;
1621 int raid_disks = conf->raid_disks;
1622 int data_disks = raid_disks-1;
NeilBrown72626682005-09-09 16:23:54 -07001623 sector_t max_sector = mddev->size << 1;
1624 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
NeilBrown72626682005-09-09 16:23:54 -07001626 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /* just being told to finish up .. nothing much to do */
1628 unplug_slaves(mddev);
NeilBrown72626682005-09-09 16:23:54 -07001629
1630 if (mddev->curr_resync < max_sector) /* aborted */
1631 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1632 &sync_blocks, 1);
1633 else /* compelted sync */
1634 conf->fullsync = 0;
1635 bitmap_close_sync(mddev->bitmap);
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return 0;
1638 }
1639 /* if there is 1 or more failed drives and we are trying
1640 * to resync, then assert that we are finished, because there is
1641 * nothing we can do.
1642 */
1643 if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07001644 sector_t rv = (mddev->size << 1) - sector_nr;
1645 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return rv;
1647 }
NeilBrown72626682005-09-09 16:23:54 -07001648 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08001649 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07001650 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1651 /* we can skip this block, and probably more */
1652 sync_blocks /= STRIPE_SECTORS;
1653 *skipped = 1;
1654 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 x = sector_nr;
1658 chunk_offset = sector_div(x, sectors_per_chunk);
1659 stripe = x;
1660 BUG_ON(x != stripe);
1661
1662 first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1663 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1664 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1665 if (sh == NULL) {
1666 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1667 /* make sure we don't swamp the stripe cache if someone else
1668 * is trying to get access
1669 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08001670 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
NeilBrown72626682005-09-09 16:23:54 -07001672 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 spin_lock(&sh->lock);
1674 set_bit(STRIPE_SYNCING, &sh->state);
1675 clear_bit(STRIPE_INSYNC, &sh->state);
1676 spin_unlock(&sh->lock);
1677
1678 handle_stripe(sh);
1679 release_stripe(sh);
1680
1681 return STRIPE_SECTORS;
1682}
1683
1684/*
1685 * This is our raid5 kernel thread.
1686 *
1687 * We scan the hash table for stripes which can be handled now.
1688 * During the scan, completed stripes are saved for us by the interrupt
1689 * handler, so that they will not have to wait for our next wakeup.
1690 */
1691static void raid5d (mddev_t *mddev)
1692{
1693 struct stripe_head *sh;
1694 raid5_conf_t *conf = mddev_to_conf(mddev);
1695 int handled;
1696
1697 PRINTK("+++ raid5d active\n");
1698
1699 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 handled = 0;
1702 spin_lock_irq(&conf->device_lock);
1703 while (1) {
1704 struct list_head *first;
1705
NeilBrown72626682005-09-09 16:23:54 -07001706 if (conf->seq_flush - conf->seq_write > 0) {
1707 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08001708 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001709 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08001710 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001711 conf->seq_write = seq;
1712 activate_bit_delay(conf);
1713 }
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (list_empty(&conf->handle_list) &&
1716 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1717 !blk_queue_plugged(mddev->queue) &&
1718 !list_empty(&conf->delayed_list))
1719 raid5_activate_delayed(conf);
1720
1721 if (list_empty(&conf->handle_list))
1722 break;
1723
1724 first = conf->handle_list.next;
1725 sh = list_entry(first, struct stripe_head, lru);
1726
1727 list_del_init(first);
1728 atomic_inc(&sh->count);
1729 if (atomic_read(&sh->count)!= 1)
1730 BUG();
1731 spin_unlock_irq(&conf->device_lock);
1732
1733 handled++;
1734 handle_stripe(sh);
1735 release_stripe(sh);
1736
1737 spin_lock_irq(&conf->device_lock);
1738 }
1739 PRINTK("%d stripes handled\n", handled);
1740
1741 spin_unlock_irq(&conf->device_lock);
1742
1743 unplug_slaves(mddev);
1744
1745 PRINTK("--- raid5d inactive\n");
1746}
1747
NeilBrown3f294f42005-11-08 21:39:25 -08001748static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08001749raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08001750{
NeilBrown007583c2005-11-08 21:39:30 -08001751 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08001752 if (conf)
1753 return sprintf(page, "%d\n", conf->max_nr_stripes);
1754 else
1755 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08001756}
1757
1758static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08001759raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08001760{
NeilBrown007583c2005-11-08 21:39:30 -08001761 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08001762 char *end;
1763 int new;
1764 if (len >= PAGE_SIZE)
1765 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08001766 if (!conf)
1767 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08001768
1769 new = simple_strtoul(page, &end, 10);
1770 if (!*page || (*end && *end != '\n') )
1771 return -EINVAL;
1772 if (new <= 16 || new > 32768)
1773 return -EINVAL;
1774 while (new < conf->max_nr_stripes) {
1775 if (drop_one_stripe(conf))
1776 conf->max_nr_stripes--;
1777 else
1778 break;
1779 }
1780 while (new > conf->max_nr_stripes) {
1781 if (grow_one_stripe(conf))
1782 conf->max_nr_stripes++;
1783 else break;
1784 }
1785 return len;
1786}
NeilBrown007583c2005-11-08 21:39:30 -08001787
NeilBrown96de1e62005-11-08 21:39:39 -08001788static struct md_sysfs_entry
1789raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
1790 raid5_show_stripe_cache_size,
1791 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001792
1793static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001794stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08001795{
NeilBrown007583c2005-11-08 21:39:30 -08001796 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08001797 if (conf)
1798 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
1799 else
1800 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08001801}
1802
NeilBrown96de1e62005-11-08 21:39:39 -08001803static struct md_sysfs_entry
1804raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08001805
NeilBrown007583c2005-11-08 21:39:30 -08001806static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08001807 &raid5_stripecache_size.attr,
1808 &raid5_stripecache_active.attr,
1809 NULL,
1810};
NeilBrown007583c2005-11-08 21:39:30 -08001811static struct attribute_group raid5_attrs_group = {
1812 .name = NULL,
1813 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08001814};
1815
NeilBrown72626682005-09-09 16:23:54 -07001816static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 raid5_conf_t *conf;
1819 int raid_disk, memory;
1820 mdk_rdev_t *rdev;
1821 struct disk_info *disk;
1822 struct list_head *tmp;
1823
1824 if (mddev->level != 5 && mddev->level != 4) {
NeilBrown14f8d262006-01-06 00:20:14 -08001825 printk(KERN_ERR "raid5: %s: raid level not set to 4/5 (%d)\n",
1826 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return -EIO;
1828 }
1829
1830 mddev->private = kmalloc (sizeof (raid5_conf_t)
1831 + mddev->raid_disks * sizeof(struct disk_info),
1832 GFP_KERNEL);
1833 if ((conf = mddev->private) == NULL)
1834 goto abort;
1835 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1836 conf->mddev = mddev;
1837
1838 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1839 goto abort;
1840 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1841
1842 spin_lock_init(&conf->device_lock);
1843 init_waitqueue_head(&conf->wait_for_stripe);
1844 init_waitqueue_head(&conf->wait_for_overlap);
1845 INIT_LIST_HEAD(&conf->handle_list);
1846 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07001847 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 INIT_LIST_HEAD(&conf->inactive_list);
1849 atomic_set(&conf->active_stripes, 0);
1850 atomic_set(&conf->preread_active_stripes, 0);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
1853
1854 ITERATE_RDEV(mddev,rdev,tmp) {
1855 raid_disk = rdev->raid_disk;
1856 if (raid_disk >= mddev->raid_disks
1857 || raid_disk < 0)
1858 continue;
1859 disk = conf->disks + raid_disk;
1860
1861 disk->rdev = rdev;
1862
NeilBrownb2d444d2005-11-08 21:39:31 -08001863 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 char b[BDEVNAME_SIZE];
1865 printk(KERN_INFO "raid5: device %s operational as raid"
1866 " disk %d\n", bdevname(rdev->bdev,b),
1867 raid_disk);
1868 conf->working_disks++;
1869 }
1870 }
1871
1872 conf->raid_disks = mddev->raid_disks;
1873 /*
1874 * 0 for a fully functional array, 1 for a degraded array.
1875 */
1876 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1877 conf->mddev = mddev;
1878 conf->chunk_size = mddev->chunk_size;
1879 conf->level = mddev->level;
1880 conf->algorithm = mddev->layout;
1881 conf->max_nr_stripes = NR_STRIPES;
1882
1883 /* device size must be a multiple of chunk size */
1884 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07001885 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 if (!conf->chunk_size || conf->chunk_size % 4) {
1888 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
1889 conf->chunk_size, mdname(mddev));
1890 goto abort;
1891 }
1892 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1893 printk(KERN_ERR
1894 "raid5: unsupported parity algorithm %d for %s\n",
1895 conf->algorithm, mdname(mddev));
1896 goto abort;
1897 }
1898 if (mddev->degraded > 1) {
1899 printk(KERN_ERR "raid5: not enough operational devices for %s"
1900 " (%d/%d failed)\n",
1901 mdname(mddev), conf->failed_disks, conf->raid_disks);
1902 goto abort;
1903 }
1904
1905 if (mddev->degraded == 1 &&
1906 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08001907 if (mddev->ok_start_degraded)
1908 printk(KERN_WARNING
1909 "raid5: starting dirty degraded array: %s"
1910 "- data corruption possible.\n",
1911 mdname(mddev));
1912 else {
1913 printk(KERN_ERR
1914 "raid5: cannot start dirty degraded array for %s\n",
1915 mdname(mddev));
1916 goto abort;
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919
1920 {
1921 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
1922 if (!mddev->thread) {
1923 printk(KERN_ERR
1924 "raid5: couldn't allocate thread for %s\n",
1925 mdname(mddev));
1926 goto abort;
1927 }
1928 }
NeilBrown50368052005-12-12 02:39:17 -08001929 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1931 if (grow_stripes(conf, conf->max_nr_stripes)) {
1932 printk(KERN_ERR
1933 "raid5: couldn't allocate %dkB for buffers\n", memory);
1934 shrink_stripes(conf);
1935 md_unregister_thread(mddev->thread);
1936 goto abort;
1937 } else
1938 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
1939 memory, mdname(mddev));
1940
1941 if (mddev->degraded == 0)
1942 printk("raid5: raid level %d set %s active with %d out of %d"
1943 " devices, algorithm %d\n", conf->level, mdname(mddev),
1944 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1945 conf->algorithm);
1946 else
1947 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
1948 " out of %d devices, algorithm %d\n", conf->level,
1949 mdname(mddev), mddev->raid_disks - mddev->degraded,
1950 mddev->raid_disks, conf->algorithm);
1951
1952 print_raid5_conf(conf);
1953
1954 /* read-ahead size must cover two whole stripes, which is
1955 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
1956 */
1957 {
1958 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
NeilBrown2d1f3b52006-01-06 00:20:31 -08001959 / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1961 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1962 }
1963
1964 /* Ok, everything is just fine now */
NeilBrown007583c2005-11-08 21:39:30 -08001965 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
NeilBrown7a5febe2005-05-16 21:53:16 -07001966
1967 mddev->queue->unplug_fn = raid5_unplug_device;
1968 mddev->queue->issue_flush_fn = raid5_issue_flush;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 mddev->array_size = mddev->size * (mddev->raid_disks - 1);
1971 return 0;
1972abort:
1973 if (conf) {
1974 print_raid5_conf(conf);
1975 if (conf->stripe_hashtbl)
1976 free_pages((unsigned long) conf->stripe_hashtbl,
1977 HASH_PAGES_ORDER);
1978 kfree(conf);
1979 }
1980 mddev->private = NULL;
1981 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
1982 return -EIO;
1983}
1984
1985
1986
NeilBrown3f294f42005-11-08 21:39:25 -08001987static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1990
1991 md_unregister_thread(mddev->thread);
1992 mddev->thread = NULL;
1993 shrink_stripes(conf);
1994 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
1995 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08001996 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrown96de1e62005-11-08 21:39:39 -08001997 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 mddev->private = NULL;
1999 return 0;
2000}
2001
2002#if RAID5_DEBUG
2003static void print_sh (struct stripe_head *sh)
2004{
2005 int i;
2006
2007 printk("sh %llu, pd_idx %d, state %ld.\n",
2008 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2009 printk("sh %llu, count %d.\n",
2010 (unsigned long long)sh->sector, atomic_read(&sh->count));
2011 printk("sh %llu, ", (unsigned long long)sh->sector);
2012 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
2013 printk("(cache%d: %p %ld) ",
2014 i, sh->dev[i].page, sh->dev[i].flags);
2015 }
2016 printk("\n");
2017}
2018
2019static void printall (raid5_conf_t *conf)
2020{
2021 struct stripe_head *sh;
2022 int i;
2023
2024 spin_lock_irq(&conf->device_lock);
2025 for (i = 0; i < NR_HASH; i++) {
2026 sh = conf->stripe_hashtbl[i];
2027 for (; sh; sh = sh->hash_next) {
2028 if (sh->raid_conf != conf)
2029 continue;
2030 print_sh(sh);
2031 }
2032 }
2033 spin_unlock_irq(&conf->device_lock);
2034}
2035#endif
2036
2037static void status (struct seq_file *seq, mddev_t *mddev)
2038{
2039 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2040 int i;
2041
2042 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2043 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2044 for (i = 0; i < conf->raid_disks; i++)
2045 seq_printf (seq, "%s",
2046 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08002047 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 seq_printf (seq, "]");
2049#if RAID5_DEBUG
2050#define D(x) \
2051 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2052 printall(conf);
2053#endif
2054}
2055
2056static void print_raid5_conf (raid5_conf_t *conf)
2057{
2058 int i;
2059 struct disk_info *tmp;
2060
2061 printk("RAID5 conf printout:\n");
2062 if (!conf) {
2063 printk("(conf==NULL)\n");
2064 return;
2065 }
2066 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2067 conf->working_disks, conf->failed_disks);
2068
2069 for (i = 0; i < conf->raid_disks; i++) {
2070 char b[BDEVNAME_SIZE];
2071 tmp = conf->disks + i;
2072 if (tmp->rdev)
2073 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08002074 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 bdevname(tmp->rdev->bdev,b));
2076 }
2077}
2078
2079static int raid5_spare_active(mddev_t *mddev)
2080{
2081 int i;
2082 raid5_conf_t *conf = mddev->private;
2083 struct disk_info *tmp;
2084
2085 for (i = 0; i < conf->raid_disks; i++) {
2086 tmp = conf->disks + i;
2087 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08002088 && !test_bit(Faulty, &tmp->rdev->flags)
2089 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 mddev->degraded--;
2091 conf->failed_disks--;
2092 conf->working_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002093 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 }
2096 print_raid5_conf(conf);
2097 return 0;
2098}
2099
2100static int raid5_remove_disk(mddev_t *mddev, int number)
2101{
2102 raid5_conf_t *conf = mddev->private;
2103 int err = 0;
2104 mdk_rdev_t *rdev;
2105 struct disk_info *p = conf->disks + number;
2106
2107 print_raid5_conf(conf);
2108 rdev = p->rdev;
2109 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002110 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 atomic_read(&rdev->nr_pending)) {
2112 err = -EBUSY;
2113 goto abort;
2114 }
2115 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002116 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (atomic_read(&rdev->nr_pending)) {
2118 /* lost the race, try later */
2119 err = -EBUSY;
2120 p->rdev = rdev;
2121 }
2122 }
2123abort:
2124
2125 print_raid5_conf(conf);
2126 return err;
2127}
2128
2129static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2130{
2131 raid5_conf_t *conf = mddev->private;
2132 int found = 0;
2133 int disk;
2134 struct disk_info *p;
2135
2136 if (mddev->degraded > 1)
2137 /* no point adding a device */
2138 return 0;
2139
2140 /*
2141 * find the disk ...
2142 */
2143 for (disk=0; disk < mddev->raid_disks; disk++)
2144 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002145 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 rdev->raid_disk = disk;
2147 found = 1;
NeilBrown72626682005-09-09 16:23:54 -07002148 if (rdev->saved_raid_disk != disk)
2149 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08002150 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 break;
2152 }
2153 print_raid5_conf(conf);
2154 return found;
2155}
2156
2157static int raid5_resize(mddev_t *mddev, sector_t sectors)
2158{
2159 /* no resync is happening, and there is enough space
2160 * on all devices, so we can resize.
2161 * We need to make sure resync covers any new space.
2162 * If the array is shrinking we should possibly wait until
2163 * any io in the removed space completes, but it hardly seems
2164 * worth it.
2165 */
2166 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2167 mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
2168 set_capacity(mddev->gendisk, mddev->array_size << 1);
2169 mddev->changed = 1;
2170 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2171 mddev->recovery_cp = mddev->size << 1;
2172 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2173 }
2174 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07002175 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return 0;
2177}
2178
NeilBrown72626682005-09-09 16:23:54 -07002179static void raid5_quiesce(mddev_t *mddev, int state)
2180{
2181 raid5_conf_t *conf = mddev_to_conf(mddev);
2182
2183 switch(state) {
2184 case 1: /* stop all writes */
2185 spin_lock_irq(&conf->device_lock);
2186 conf->quiesce = 1;
2187 wait_event_lock_irq(conf->wait_for_stripe,
2188 atomic_read(&conf->active_stripes) == 0,
2189 conf->device_lock, /* nothing */);
2190 spin_unlock_irq(&conf->device_lock);
2191 break;
2192
2193 case 0: /* re-enable writes */
2194 spin_lock_irq(&conf->device_lock);
2195 conf->quiesce = 0;
2196 wake_up(&conf->wait_for_stripe);
2197 spin_unlock_irq(&conf->device_lock);
2198 break;
2199 }
NeilBrown72626682005-09-09 16:23:54 -07002200}
NeilBrownb15c2e52006-01-06 00:20:16 -08002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202static mdk_personality_t raid5_personality=
2203{
2204 .name = "raid5",
2205 .owner = THIS_MODULE,
2206 .make_request = make_request,
2207 .run = run,
2208 .stop = stop,
2209 .status = status,
2210 .error_handler = error,
2211 .hot_add_disk = raid5_add_disk,
2212 .hot_remove_disk= raid5_remove_disk,
2213 .spare_active = raid5_spare_active,
2214 .sync_request = sync_request,
2215 .resize = raid5_resize,
NeilBrown72626682005-09-09 16:23:54 -07002216 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217};
2218
2219static int __init raid5_init (void)
2220{
2221 return register_md_personality (RAID5, &raid5_personality);
2222}
2223
2224static void raid5_exit (void)
2225{
2226 unregister_md_personality (RAID5);
2227}
2228
2229module_init(raid5_init);
2230module_exit(raid5_exit);
2231MODULE_LICENSE("GPL");
2232MODULE_ALIAS("md-personality-4"); /* RAID5 */