blob: 7a6df515b00820497777dd188b621a560dc80d3b [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
NeilBrownfccddba2006-01-06 00:20:33 -080038#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define HASH_MASK (NR_HASH - 1)
40
NeilBrownfccddba2006-01-06 00:20:33 -080041#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
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
Arjan van de Ven858119e2006-01-14 13:20:43 -080072static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
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);
NeilBrown72626682005-09-09 16:23:54 -070082 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -070088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 md_wakeup_thread(conf->mddev->thread);
90 } else {
91 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
92 atomic_dec(&conf->preread_active_stripes);
93 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
94 md_wakeup_thread(conf->mddev->thread);
95 }
96 list_add_tail(&sh->lru, &conf->inactive_list);
97 atomic_dec(&conf->active_stripes);
98 if (!conf->inactive_blocked ||
NeilBrown50368052005-12-12 02:39:17 -080099 atomic_read(&conf->active_stripes) < (conf->max_nr_stripes*3/4))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 wake_up(&conf->wait_for_stripe);
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
NeilBrownfccddba2006-01-06 00:20:33 -0800114static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
117
NeilBrownfccddba2006-01-06 00:20:33 -0800118 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Arjan van de Ven858119e2006-01-14 13:20:43 -0800121static void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
NeilBrownfccddba2006-01-06 00:20:33 -0800123 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
126
127 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800128 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
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;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800160 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
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
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800181static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800184 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
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
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800201 sh->disks = disks;
202
203 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 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
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800220static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800223 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 CHECK_DEVLOCK();
226 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800227 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800228 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 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
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800237static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
238 int pd_idx, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
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 {
NeilBrown72626682005-09-09 16:23:54 -0700247 wait_event_lock_irq(conf->wait_for_stripe,
248 conf->quiesce == 0,
249 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800250 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 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) &&
NeilBrown50368052005-12-12 02:39:17 -0800260 (atomic_read(&conf->active_stripes)
261 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 || !conf->inactive_blocked),
263 conf->device_lock,
264 unplug_slaves(conf->mddev);
265 );
266 conf->inactive_blocked = 0;
267 } else
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800268 init_stripe(sh, sector, pd_idx, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 } else {
270 if (atomic_read(&sh->count)) {
271 if (!list_empty(&sh->lru))
272 BUG();
273 } else {
274 if (!test_bit(STRIPE_HANDLE, &sh->state))
275 atomic_inc(&conf->active_stripes);
276 if (list_empty(&sh->lru))
277 BUG();
278 list_del_init(&sh->lru);
279 }
280 }
281 } while (sh == NULL);
282
283 if (sh)
284 atomic_inc(&sh->count);
285
286 spin_unlock_irq(&conf->device_lock);
287 return sh;
288}
289
NeilBrown3f294f42005-11-08 21:39:25 -0800290static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800293 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
294 if (!sh)
295 return 0;
296 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
297 sh->raid_conf = conf;
298 spin_lock_init(&sh->lock);
299
300 if (grow_buffers(sh, conf->raid_disks)) {
301 shrink_buffers(sh, conf->raid_disks);
302 kmem_cache_free(conf->slab_cache, sh);
303 return 0;
304 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800305 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800306 /* we just created an active stripe so... */
307 atomic_set(&sh->count, 1);
308 atomic_inc(&conf->active_stripes);
309 INIT_LIST_HEAD(&sh->lru);
310 release_stripe(sh);
311 return 1;
312}
313
314static int grow_stripes(raid5_conf_t *conf, int num)
315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 kmem_cache_t *sc;
317 int devs = conf->raid_disks;
318
NeilBrownad01c9e2006-03-27 01:18:07 -0800319 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
320 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
321 conf->active_name = 0;
322 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
324 0, 0, NULL, NULL);
325 if (!sc)
326 return 1;
327 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800328 conf->pool_size = devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 while (num--) {
NeilBrown3f294f42005-11-08 21:39:25 -0800330 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 return 0;
334}
NeilBrownad01c9e2006-03-27 01:18:07 -0800335static int resize_stripes(raid5_conf_t *conf, int newsize)
336{
337 /* Make all the stripes able to hold 'newsize' devices.
338 * New slots in each stripe get 'page' set to a new page.
339 *
340 * This happens in stages:
341 * 1/ create a new kmem_cache and allocate the required number of
342 * stripe_heads.
343 * 2/ gather all the old stripe_heads and tranfer the pages across
344 * to the new stripe_heads. This will have the side effect of
345 * freezing the array as once all stripe_heads have been collected,
346 * no IO will be possible. Old stripe heads are freed once their
347 * pages have been transferred over, and the old kmem_cache is
348 * freed when all stripes are done.
349 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
350 * we simple return a failre status - no need to clean anything up.
351 * 4/ allocate new pages for the new slots in the new stripe_heads.
352 * If this fails, we don't bother trying the shrink the
353 * stripe_heads down again, we just leave them as they are.
354 * As each stripe_head is processed the new one is released into
355 * active service.
356 *
357 * Once step2 is started, we cannot afford to wait for a write,
358 * so we use GFP_NOIO allocations.
359 */
360 struct stripe_head *osh, *nsh;
361 LIST_HEAD(newstripes);
362 struct disk_info *ndisks;
363 int err = 0;
364 kmem_cache_t *sc;
365 int i;
366
367 if (newsize <= conf->pool_size)
368 return 0; /* never bother to shrink */
369
370 /* Step 1 */
371 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
372 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
373 0, 0, NULL, NULL);
374 if (!sc)
375 return -ENOMEM;
376
377 for (i = conf->max_nr_stripes; i; i--) {
378 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
379 if (!nsh)
380 break;
381
382 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
383
384 nsh->raid_conf = conf;
385 spin_lock_init(&nsh->lock);
386
387 list_add(&nsh->lru, &newstripes);
388 }
389 if (i) {
390 /* didn't get enough, give up */
391 while (!list_empty(&newstripes)) {
392 nsh = list_entry(newstripes.next, struct stripe_head, lru);
393 list_del(&nsh->lru);
394 kmem_cache_free(sc, nsh);
395 }
396 kmem_cache_destroy(sc);
397 return -ENOMEM;
398 }
399 /* Step 2 - Must use GFP_NOIO now.
400 * OK, we have enough stripes, start collecting inactive
401 * stripes and copying them over
402 */
403 list_for_each_entry(nsh, &newstripes, lru) {
404 spin_lock_irq(&conf->device_lock);
405 wait_event_lock_irq(conf->wait_for_stripe,
406 !list_empty(&conf->inactive_list),
407 conf->device_lock,
408 unplug_slaves(conf->mddev);
409 );
410 osh = get_free_stripe(conf);
411 spin_unlock_irq(&conf->device_lock);
412 atomic_set(&nsh->count, 1);
413 for(i=0; i<conf->pool_size; i++)
414 nsh->dev[i].page = osh->dev[i].page;
415 for( ; i<newsize; i++)
416 nsh->dev[i].page = NULL;
417 kmem_cache_free(conf->slab_cache, osh);
418 }
419 kmem_cache_destroy(conf->slab_cache);
420
421 /* Step 3.
422 * At this point, we are holding all the stripes so the array
423 * is completely stalled, so now is a good time to resize
424 * conf->disks.
425 */
426 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
427 if (ndisks) {
428 for (i=0; i<conf->raid_disks; i++)
429 ndisks[i] = conf->disks[i];
430 kfree(conf->disks);
431 conf->disks = ndisks;
432 } else
433 err = -ENOMEM;
434
435 /* Step 4, return new stripes to service */
436 while(!list_empty(&newstripes)) {
437 nsh = list_entry(newstripes.next, struct stripe_head, lru);
438 list_del_init(&nsh->lru);
439 for (i=conf->raid_disks; i < newsize; i++)
440 if (nsh->dev[i].page == NULL) {
441 struct page *p = alloc_page(GFP_NOIO);
442 nsh->dev[i].page = p;
443 if (!p)
444 err = -ENOMEM;
445 }
446 release_stripe(nsh);
447 }
448 /* critical section pass, GFP_NOIO no longer needed */
449
450 conf->slab_cache = sc;
451 conf->active_name = 1-conf->active_name;
452 conf->pool_size = newsize;
453 return err;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
NeilBrown3f294f42005-11-08 21:39:25 -0800457static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct stripe_head *sh;
460
NeilBrown3f294f42005-11-08 21:39:25 -0800461 spin_lock_irq(&conf->device_lock);
462 sh = get_free_stripe(conf);
463 spin_unlock_irq(&conf->device_lock);
464 if (!sh)
465 return 0;
466 if (atomic_read(&sh->count))
467 BUG();
NeilBrownad01c9e2006-03-27 01:18:07 -0800468 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -0800469 kmem_cache_free(conf->slab_cache, sh);
470 atomic_dec(&conf->active_stripes);
471 return 1;
472}
473
474static void shrink_stripes(raid5_conf_t *conf)
475{
476 while (drop_one_stripe(conf))
477 ;
478
NeilBrown29fc7e32006-02-03 03:03:41 -0800479 if (conf->slab_cache)
480 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 conf->slab_cache = NULL;
482}
483
NeilBrown4e5314b2005-11-08 21:39:22 -0800484static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int error)
486{
487 struct stripe_head *sh = bi->bi_private;
488 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800489 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
491
492 if (bi->bi_size)
493 return 1;
494
495 for (i=0 ; i<disks; i++)
496 if (bi == &sh->dev[i].req)
497 break;
498
499 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
500 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
501 uptodate);
502 if (i == disks) {
503 BUG();
504 return 0;
505 }
506
507 if (uptodate) {
508#if 0
509 struct bio *bio;
510 unsigned long flags;
511 spin_lock_irqsave(&conf->device_lock, flags);
512 /* we can return a buffer if we bypassed the cache or
513 * if the top buffer is not in highmem. If there are
514 * multiple buffers, leave the extra work to
515 * handle_stripe
516 */
517 buffer = sh->bh_read[i];
518 if (buffer &&
519 (!PageHighMem(buffer->b_page)
520 || buffer->b_page == bh->b_page )
521 ) {
522 sh->bh_read[i] = buffer->b_reqnext;
523 buffer->b_reqnext = NULL;
524 } else
525 buffer = NULL;
526 spin_unlock_irqrestore(&conf->device_lock, flags);
527 if (sh->bh_page[i]==bh->b_page)
528 set_buffer_uptodate(bh);
529 if (buffer) {
530 if (buffer->b_page != bh->b_page)
531 memcpy(buffer->b_data, bh->b_data, bh->b_size);
532 buffer->b_end_io(buffer, 1);
533 }
534#else
535 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -0800536#endif
537 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -0800538 printk(KERN_INFO "raid5: read error corrected!!\n");
NeilBrown4e5314b2005-11-08 21:39:22 -0800539 clear_bit(R5_ReadError, &sh->dev[i].flags);
540 clear_bit(R5_ReWrite, &sh->dev[i].flags);
541 }
NeilBrownba22dcb2005-11-08 21:39:31 -0800542 if (atomic_read(&conf->disks[i].rdev->read_errors))
543 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else {
NeilBrownba22dcb2005-11-08 21:39:31 -0800545 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -0800547 atomic_inc(&conf->disks[i].rdev->read_errors);
548 if (conf->mddev->degraded)
NeilBrown14f8d262006-01-06 00:20:14 -0800549 printk(KERN_WARNING "raid5: read error not correctable.\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800550 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -0800551 /* Oh, no!!! */
NeilBrown14f8d262006-01-06 00:20:14 -0800552 printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800553 else if (atomic_read(&conf->disks[i].rdev->read_errors)
554 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -0800555 printk(KERN_WARNING
556 "raid5: Too many read errors, failing device.\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800557 else
558 retry = 1;
559 if (retry)
560 set_bit(R5_ReadError, &sh->dev[i].flags);
561 else {
NeilBrown4e5314b2005-11-08 21:39:22 -0800562 clear_bit(R5_ReadError, &sh->dev[i].flags);
563 clear_bit(R5_ReWrite, &sh->dev[i].flags);
564 md_error(conf->mddev, conf->disks[i].rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -0800565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
568#if 0
569 /* must restore b_page before unlocking buffer... */
570 if (sh->bh_page[i] != bh->b_page) {
571 bh->b_page = sh->bh_page[i];
572 bh->b_data = page_address(bh->b_page);
573 clear_buffer_uptodate(bh);
574 }
575#endif
576 clear_bit(R5_LOCKED, &sh->dev[i].flags);
577 set_bit(STRIPE_HANDLE, &sh->state);
578 release_stripe(sh);
579 return 0;
580}
581
582static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
583 int error)
584{
585 struct stripe_head *sh = bi->bi_private;
586 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800587 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long flags;
589 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
590
591 if (bi->bi_size)
592 return 1;
593
594 for (i=0 ; i<disks; i++)
595 if (bi == &sh->dev[i].req)
596 break;
597
598 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
599 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
600 uptodate);
601 if (i == disks) {
602 BUG();
603 return 0;
604 }
605
606 spin_lock_irqsave(&conf->device_lock, flags);
607 if (!uptodate)
608 md_error(conf->mddev, conf->disks[i].rdev);
609
610 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
611
612 clear_bit(R5_LOCKED, &sh->dev[i].flags);
613 set_bit(STRIPE_HANDLE, &sh->state);
614 __release_stripe(conf, sh);
615 spin_unlock_irqrestore(&conf->device_lock, flags);
616 return 0;
617}
618
619
620static sector_t compute_blocknr(struct stripe_head *sh, int i);
621
622static void raid5_build_block (struct stripe_head *sh, int i)
623{
624 struct r5dev *dev = &sh->dev[i];
625
626 bio_init(&dev->req);
627 dev->req.bi_io_vec = &dev->vec;
628 dev->req.bi_vcnt++;
629 dev->req.bi_max_vecs++;
630 dev->vec.bv_page = dev->page;
631 dev->vec.bv_len = STRIPE_SIZE;
632 dev->vec.bv_offset = 0;
633
634 dev->req.bi_sector = sh->sector;
635 dev->req.bi_private = sh;
636
637 dev->flags = 0;
638 if (i != sh->pd_idx)
639 dev->sector = compute_blocknr(sh, i);
640}
641
642static void error(mddev_t *mddev, mdk_rdev_t *rdev)
643{
644 char b[BDEVNAME_SIZE];
645 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
646 PRINTK("raid5: error called\n");
647
NeilBrownb2d444d2005-11-08 21:39:31 -0800648 if (!test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 mddev->sb_dirty = 1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800650 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 conf->working_disks--;
652 mddev->degraded++;
653 conf->failed_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800654 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /*
656 * if recovery was running, make sure it aborts.
657 */
658 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
659 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800660 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 printk (KERN_ALERT
662 "raid5: Disk failure on %s, disabling device."
663 " Operation continuing on %d devices\n",
664 bdevname(rdev->bdev,b), conf->working_disks);
665 }
666}
667
668/*
669 * Input: a 'big' sector number,
670 * Output: index of the data and parity disk, and the sector # in them.
671 */
672static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
673 unsigned int data_disks, unsigned int * dd_idx,
674 unsigned int * pd_idx, raid5_conf_t *conf)
675{
676 long stripe;
677 unsigned long chunk_number;
678 unsigned int chunk_offset;
679 sector_t new_sector;
680 int sectors_per_chunk = conf->chunk_size >> 9;
681
682 /* First compute the information on this sector */
683
684 /*
685 * Compute the chunk number and the sector offset inside the chunk
686 */
687 chunk_offset = sector_div(r_sector, sectors_per_chunk);
688 chunk_number = r_sector;
689 BUG_ON(r_sector != chunk_number);
690
691 /*
692 * Compute the stripe number
693 */
694 stripe = chunk_number / data_disks;
695
696 /*
697 * Compute the data disk and parity disk indexes inside the stripe
698 */
699 *dd_idx = chunk_number % data_disks;
700
701 /*
702 * Select the parity disk based on the user selected algorithm.
703 */
704 if (conf->level == 4)
705 *pd_idx = data_disks;
706 else switch (conf->algorithm) {
707 case ALGORITHM_LEFT_ASYMMETRIC:
708 *pd_idx = data_disks - stripe % raid_disks;
709 if (*dd_idx >= *pd_idx)
710 (*dd_idx)++;
711 break;
712 case ALGORITHM_RIGHT_ASYMMETRIC:
713 *pd_idx = stripe % raid_disks;
714 if (*dd_idx >= *pd_idx)
715 (*dd_idx)++;
716 break;
717 case ALGORITHM_LEFT_SYMMETRIC:
718 *pd_idx = data_disks - stripe % raid_disks;
719 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
720 break;
721 case ALGORITHM_RIGHT_SYMMETRIC:
722 *pd_idx = stripe % raid_disks;
723 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
724 break;
725 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800726 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 conf->algorithm);
728 }
729
730 /*
731 * Finally, compute the new sector number
732 */
733 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
734 return new_sector;
735}
736
737
738static sector_t compute_blocknr(struct stripe_head *sh, int i)
739{
740 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800741 int raid_disks = sh->disks, data_disks = raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 sector_t new_sector = sh->sector, check;
743 int sectors_per_chunk = conf->chunk_size >> 9;
744 sector_t stripe;
745 int chunk_offset;
746 int chunk_number, dummy1, dummy2, dd_idx = i;
747 sector_t r_sector;
748
749 chunk_offset = sector_div(new_sector, sectors_per_chunk);
750 stripe = new_sector;
751 BUG_ON(new_sector != stripe);
752
753
754 switch (conf->algorithm) {
755 case ALGORITHM_LEFT_ASYMMETRIC:
756 case ALGORITHM_RIGHT_ASYMMETRIC:
757 if (i > sh->pd_idx)
758 i--;
759 break;
760 case ALGORITHM_LEFT_SYMMETRIC:
761 case ALGORITHM_RIGHT_SYMMETRIC:
762 if (i < sh->pd_idx)
763 i += raid_disks;
764 i -= (sh->pd_idx + 1);
765 break;
766 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800767 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 conf->algorithm);
769 }
770
771 chunk_number = stripe * data_disks + i;
772 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
773
774 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
775 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -0800776 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778 }
779 return r_sector;
780}
781
782
783
784/*
785 * Copy data between a page in the stripe cache, and a bio.
786 * There are no alignment or size guarantees between the page or the
787 * bio except that there is some overlap.
788 * All iovecs in the bio must be considered.
789 */
790static void copy_data(int frombio, struct bio *bio,
791 struct page *page,
792 sector_t sector)
793{
794 char *pa = page_address(page);
795 struct bio_vec *bvl;
796 int i;
797 int page_offset;
798
799 if (bio->bi_sector >= sector)
800 page_offset = (signed)(bio->bi_sector - sector) * 512;
801 else
802 page_offset = (signed)(sector - bio->bi_sector) * -512;
803 bio_for_each_segment(bvl, bio, i) {
804 int len = bio_iovec_idx(bio,i)->bv_len;
805 int clen;
806 int b_offset = 0;
807
808 if (page_offset < 0) {
809 b_offset = -page_offset;
810 page_offset += b_offset;
811 len -= b_offset;
812 }
813
814 if (len > 0 && page_offset + len > STRIPE_SIZE)
815 clen = STRIPE_SIZE - page_offset;
816 else clen = len;
817
818 if (clen > 0) {
819 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
820 if (frombio)
821 memcpy(pa+page_offset, ba+b_offset, clen);
822 else
823 memcpy(ba+b_offset, pa+page_offset, clen);
824 __bio_kunmap_atomic(ba, KM_USER0);
825 }
826 if (clen < len) /* hit end of page */
827 break;
828 page_offset += len;
829 }
830}
831
832#define check_xor() do { \
833 if (count == MAX_XOR_BLOCKS) { \
834 xor_block(count, STRIPE_SIZE, ptr); \
835 count = 1; \
836 } \
837 } while(0)
838
839
840static void compute_block(struct stripe_head *sh, int dd_idx)
841{
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800842 int i, count, disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 void *ptr[MAX_XOR_BLOCKS], *p;
844
845 PRINTK("compute_block, stripe %llu, idx %d\n",
846 (unsigned long long)sh->sector, dd_idx);
847
848 ptr[0] = page_address(sh->dev[dd_idx].page);
849 memset(ptr[0], 0, STRIPE_SIZE);
850 count = 1;
851 for (i = disks ; i--; ) {
852 if (i == dd_idx)
853 continue;
854 p = page_address(sh->dev[i].page);
855 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
856 ptr[count++] = p;
857 else
NeilBrown14f8d262006-01-06 00:20:14 -0800858 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 " not present\n", dd_idx,
860 (unsigned long long)sh->sector, i);
861
862 check_xor();
863 }
864 if (count != 1)
865 xor_block(count, STRIPE_SIZE, ptr);
866 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
867}
868
869static void compute_parity(struct stripe_head *sh, int method)
870{
871 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800872 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 void *ptr[MAX_XOR_BLOCKS];
874 struct bio *chosen;
875
876 PRINTK("compute_parity, stripe %llu, method %d\n",
877 (unsigned long long)sh->sector, method);
878
879 count = 1;
880 ptr[0] = page_address(sh->dev[pd_idx].page);
881 switch(method) {
882 case READ_MODIFY_WRITE:
883 if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
884 BUG();
885 for (i=disks ; i-- ;) {
886 if (i==pd_idx)
887 continue;
888 if (sh->dev[i].towrite &&
889 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
890 ptr[count++] = page_address(sh->dev[i].page);
891 chosen = sh->dev[i].towrite;
892 sh->dev[i].towrite = NULL;
893
894 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
895 wake_up(&conf->wait_for_overlap);
896
897 if (sh->dev[i].written) BUG();
898 sh->dev[i].written = chosen;
899 check_xor();
900 }
901 }
902 break;
903 case RECONSTRUCT_WRITE:
904 memset(ptr[0], 0, STRIPE_SIZE);
905 for (i= disks; i-- ;)
906 if (i!=pd_idx && sh->dev[i].towrite) {
907 chosen = sh->dev[i].towrite;
908 sh->dev[i].towrite = NULL;
909
910 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
911 wake_up(&conf->wait_for_overlap);
912
913 if (sh->dev[i].written) BUG();
914 sh->dev[i].written = chosen;
915 }
916 break;
917 case CHECK_PARITY:
918 break;
919 }
920 if (count>1) {
921 xor_block(count, STRIPE_SIZE, ptr);
922 count = 1;
923 }
924
925 for (i = disks; i--;)
926 if (sh->dev[i].written) {
927 sector_t sector = sh->dev[i].sector;
928 struct bio *wbi = sh->dev[i].written;
929 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
930 copy_data(1, wbi, sh->dev[i].page, sector);
931 wbi = r5_next_bio(wbi, sector);
932 }
933
934 set_bit(R5_LOCKED, &sh->dev[i].flags);
935 set_bit(R5_UPTODATE, &sh->dev[i].flags);
936 }
937
938 switch(method) {
939 case RECONSTRUCT_WRITE:
940 case CHECK_PARITY:
941 for (i=disks; i--;)
942 if (i != pd_idx) {
943 ptr[count++] = page_address(sh->dev[i].page);
944 check_xor();
945 }
946 break;
947 case READ_MODIFY_WRITE:
948 for (i = disks; i--;)
949 if (sh->dev[i].written) {
950 ptr[count++] = page_address(sh->dev[i].page);
951 check_xor();
952 }
953 }
954 if (count != 1)
955 xor_block(count, STRIPE_SIZE, ptr);
956
957 if (method != CHECK_PARITY) {
958 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
959 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
960 } else
961 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
962}
963
964/*
965 * Each stripe/dev can have one or more bion attached.
966 * toread/towrite point to the first in a chain.
967 * The bi_next chain must be in order.
968 */
969static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
970{
971 struct bio **bip;
972 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -0700973 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 PRINTK("adding bh b#%llu to stripe s#%llu\n",
976 (unsigned long long)bi->bi_sector,
977 (unsigned long long)sh->sector);
978
979
980 spin_lock(&sh->lock);
981 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -0700982 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -0700984 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
985 firstwrite = 1;
986 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 bip = &sh->dev[dd_idx].toread;
988 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
989 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
990 goto overlap;
991 bip = & (*bip)->bi_next;
992 }
993 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
994 goto overlap;
995
996 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
997 BUG();
998 if (*bip)
999 bi->bi_next = *bip;
1000 *bip = bi;
1001 bi->bi_phys_segments ++;
1002 spin_unlock_irq(&conf->device_lock);
1003 spin_unlock(&sh->lock);
1004
1005 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1006 (unsigned long long)bi->bi_sector,
1007 (unsigned long long)sh->sector, dd_idx);
1008
NeilBrown72626682005-09-09 16:23:54 -07001009 if (conf->mddev->bitmap && firstwrite) {
1010 sh->bm_seq = conf->seq_write;
1011 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1012 STRIPE_SECTORS, 0);
1013 set_bit(STRIPE_BIT_DELAY, &sh->state);
1014 }
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (forwrite) {
1017 /* check if page is covered */
1018 sector_t sector = sh->dev[dd_idx].sector;
1019 for (bi=sh->dev[dd_idx].towrite;
1020 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1021 bi && bi->bi_sector <= sector;
1022 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1023 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1024 sector = bi->bi_sector + (bi->bi_size>>9);
1025 }
1026 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1027 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1028 }
1029 return 1;
1030
1031 overlap:
1032 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1033 spin_unlock_irq(&conf->device_lock);
1034 spin_unlock(&sh->lock);
1035 return 0;
1036}
1037
1038
1039/*
1040 * handle_stripe - do things to a stripe.
1041 *
1042 * We lock the stripe and then examine the state of various bits
1043 * to see what needs to be done.
1044 * Possible results:
1045 * return some read request which now have data
1046 * return some write requests which are safely on disc
1047 * schedule a read on some buffers
1048 * schedule a write of some buffers
1049 * return confirmation of parity correctness
1050 *
1051 * Parity calculations are done inside the stripe lock
1052 * buffers are taken off read_list or write_list, and bh_cache buffers
1053 * get BH_Lock set before the stripe lock is released.
1054 *
1055 */
1056
1057static void handle_stripe(struct stripe_head *sh)
1058{
1059 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001060 int disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct bio *return_bi= NULL;
1062 struct bio *bi;
1063 int i;
1064 int syncing;
1065 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1066 int non_overwrite = 0;
1067 int failed_num=0;
1068 struct r5dev *dev;
1069
1070 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1071 (unsigned long long)sh->sector, atomic_read(&sh->count),
1072 sh->pd_idx);
1073
1074 spin_lock(&sh->lock);
1075 clear_bit(STRIPE_HANDLE, &sh->state);
1076 clear_bit(STRIPE_DELAYED, &sh->state);
1077
1078 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1079 /* Now to look around and see what can be done */
1080
NeilBrown9910f162006-01-06 00:20:24 -08001081 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 for (i=disks; i--; ) {
1083 mdk_rdev_t *rdev;
1084 dev = &sh->dev[i];
1085 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1088 i, dev->flags, dev->toread, dev->towrite, dev->written);
1089 /* maybe we can reply to a read */
1090 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1091 struct bio *rbi, *rbi2;
1092 PRINTK("Return read for disc %d\n", i);
1093 spin_lock_irq(&conf->device_lock);
1094 rbi = dev->toread;
1095 dev->toread = NULL;
1096 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1097 wake_up(&conf->wait_for_overlap);
1098 spin_unlock_irq(&conf->device_lock);
1099 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1100 copy_data(0, rbi, dev->page, dev->sector);
1101 rbi2 = r5_next_bio(rbi, dev->sector);
1102 spin_lock_irq(&conf->device_lock);
1103 if (--rbi->bi_phys_segments == 0) {
1104 rbi->bi_next = return_bi;
1105 return_bi = rbi;
1106 }
1107 spin_unlock_irq(&conf->device_lock);
1108 rbi = rbi2;
1109 }
1110 }
1111
1112 /* now count some things */
1113 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1114 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1115
1116
1117 if (dev->toread) to_read++;
1118 if (dev->towrite) {
1119 to_write++;
1120 if (!test_bit(R5_OVERWRITE, &dev->flags))
1121 non_overwrite++;
1122 }
1123 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001124 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001125 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001126 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08001127 clear_bit(R5_ReadError, &dev->flags);
1128 clear_bit(R5_ReWrite, &dev->flags);
1129 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001130 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001131 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 failed++;
1133 failed_num = i;
1134 } else
1135 set_bit(R5_Insync, &dev->flags);
1136 }
NeilBrown9910f162006-01-06 00:20:24 -08001137 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 PRINTK("locked=%d uptodate=%d to_read=%d"
1139 " to_write=%d failed=%d failed_num=%d\n",
1140 locked, uptodate, to_read, to_write, failed, failed_num);
1141 /* check if the array has lost two devices and, if so, some requests might
1142 * need to be failed
1143 */
1144 if (failed > 1 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 for (i=disks; i--; ) {
NeilBrown72626682005-09-09 16:23:54 -07001146 int bitmap_end = 0;
NeilBrown4e5314b2005-11-08 21:39:22 -08001147
1148 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001149 mdk_rdev_t *rdev;
1150 rcu_read_lock();
1151 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001152 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001153 /* multiple read failures in one stripe */
1154 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001155 rcu_read_unlock();
NeilBrown4e5314b2005-11-08 21:39:22 -08001156 }
1157
NeilBrown72626682005-09-09 16:23:54 -07001158 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* fail all writes first */
1160 bi = sh->dev[i].towrite;
1161 sh->dev[i].towrite = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001162 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1165 wake_up(&conf->wait_for_overlap);
1166
1167 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1168 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1169 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1170 if (--bi->bi_phys_segments == 0) {
1171 md_write_end(conf->mddev);
1172 bi->bi_next = return_bi;
1173 return_bi = bi;
1174 }
1175 bi = nextbi;
1176 }
1177 /* and fail all 'written' */
1178 bi = sh->dev[i].written;
1179 sh->dev[i].written = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001180 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1182 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1183 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1184 if (--bi->bi_phys_segments == 0) {
1185 md_write_end(conf->mddev);
1186 bi->bi_next = return_bi;
1187 return_bi = bi;
1188 }
1189 bi = bi2;
1190 }
1191
1192 /* fail any reads if this device is non-operational */
NeilBrown4e5314b2005-11-08 21:39:22 -08001193 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1194 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 bi = sh->dev[i].toread;
1196 sh->dev[i].toread = NULL;
1197 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1198 wake_up(&conf->wait_for_overlap);
1199 if (bi) to_read--;
1200 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1201 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1202 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1203 if (--bi->bi_phys_segments == 0) {
1204 bi->bi_next = return_bi;
1205 return_bi = bi;
1206 }
1207 bi = nextbi;
1208 }
1209 }
NeilBrown72626682005-09-09 16:23:54 -07001210 spin_unlock_irq(&conf->device_lock);
1211 if (bitmap_end)
1212 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1213 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 if (failed > 1 && syncing) {
1217 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1218 clear_bit(STRIPE_SYNCING, &sh->state);
1219 syncing = 0;
1220 }
1221
1222 /* might be able to return some write requests if the parity block
1223 * is safe, or on a failed drive
1224 */
1225 dev = &sh->dev[sh->pd_idx];
1226 if ( written &&
1227 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1228 test_bit(R5_UPTODATE, &dev->flags))
1229 || (failed == 1 && failed_num == sh->pd_idx))
1230 ) {
1231 /* any written block on an uptodate or failed drive can be returned.
1232 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1233 * never LOCKED, so we don't need to test 'failed' directly.
1234 */
1235 for (i=disks; i--; )
1236 if (sh->dev[i].written) {
1237 dev = &sh->dev[i];
1238 if (!test_bit(R5_LOCKED, &dev->flags) &&
1239 test_bit(R5_UPTODATE, &dev->flags) ) {
1240 /* We can return any write requests */
1241 struct bio *wbi, *wbi2;
NeilBrown72626682005-09-09 16:23:54 -07001242 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 PRINTK("Return write for disc %d\n", i);
1244 spin_lock_irq(&conf->device_lock);
1245 wbi = dev->written;
1246 dev->written = NULL;
1247 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1248 wbi2 = r5_next_bio(wbi, dev->sector);
1249 if (--wbi->bi_phys_segments == 0) {
1250 md_write_end(conf->mddev);
1251 wbi->bi_next = return_bi;
1252 return_bi = wbi;
1253 }
1254 wbi = wbi2;
1255 }
NeilBrown72626682005-09-09 16:23:54 -07001256 if (dev->towrite == NULL)
1257 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001259 if (bitmap_end)
1260 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1261 STRIPE_SECTORS,
1262 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264 }
1265 }
1266
1267 /* Now we might consider reading some blocks, either to check/generate
1268 * parity, or to satisfy requests
1269 * or to load a block that is being partially written.
1270 */
1271 if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
1272 for (i=disks; i--;) {
1273 dev = &sh->dev[i];
1274 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1275 (dev->toread ||
1276 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1277 syncing ||
1278 (failed && (sh->dev[failed_num].toread ||
1279 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1280 )
1281 ) {
1282 /* we would like to get this block, possibly
1283 * by computing it, but we might not be able to
1284 */
1285 if (uptodate == disks-1) {
1286 PRINTK("Computing block %d\n", i);
1287 compute_block(sh, i);
1288 uptodate++;
1289 } else if (test_bit(R5_Insync, &dev->flags)) {
1290 set_bit(R5_LOCKED, &dev->flags);
1291 set_bit(R5_Wantread, &dev->flags);
1292#if 0
1293 /* if I am just reading this block and we don't have
1294 a failed drive, or any pending writes then sidestep the cache */
1295 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1296 ! syncing && !failed && !to_write) {
1297 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1298 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1299 }
1300#endif
1301 locked++;
1302 PRINTK("Reading block %d (sync=%d)\n",
1303 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 }
1306 }
1307 set_bit(STRIPE_HANDLE, &sh->state);
1308 }
1309
1310 /* now to consider writing and what else, if anything should be read */
1311 if (to_write) {
1312 int rmw=0, rcw=0;
1313 for (i=disks ; i--;) {
1314 /* would I have to read this buffer for read_modify_write */
1315 dev = &sh->dev[i];
1316 if ((dev->towrite || i == sh->pd_idx) &&
1317 (!test_bit(R5_LOCKED, &dev->flags)
1318#if 0
1319|| sh->bh_page[i]!=bh->b_page
1320#endif
1321 ) &&
1322 !test_bit(R5_UPTODATE, &dev->flags)) {
1323 if (test_bit(R5_Insync, &dev->flags)
1324/* && !(!mddev->insync && i == sh->pd_idx) */
1325 )
1326 rmw++;
1327 else rmw += 2*disks; /* cannot read it */
1328 }
1329 /* Would I have to read this buffer for reconstruct_write */
1330 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1331 (!test_bit(R5_LOCKED, &dev->flags)
1332#if 0
1333|| sh->bh_page[i] != bh->b_page
1334#endif
1335 ) &&
1336 !test_bit(R5_UPTODATE, &dev->flags)) {
1337 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1338 else rcw += 2*disks;
1339 }
1340 }
1341 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1342 (unsigned long long)sh->sector, rmw, rcw);
1343 set_bit(STRIPE_HANDLE, &sh->state);
1344 if (rmw < rcw && rmw > 0)
1345 /* prefer read-modify-write, but need to get some data */
1346 for (i=disks; i--;) {
1347 dev = &sh->dev[i];
1348 if ((dev->towrite || i == sh->pd_idx) &&
1349 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1350 test_bit(R5_Insync, &dev->flags)) {
1351 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1352 {
1353 PRINTK("Read_old block %d for r-m-w\n", i);
1354 set_bit(R5_LOCKED, &dev->flags);
1355 set_bit(R5_Wantread, &dev->flags);
1356 locked++;
1357 } else {
1358 set_bit(STRIPE_DELAYED, &sh->state);
1359 set_bit(STRIPE_HANDLE, &sh->state);
1360 }
1361 }
1362 }
1363 if (rcw <= rmw && rcw > 0)
1364 /* want reconstruct write, but need to get some data */
1365 for (i=disks; i--;) {
1366 dev = &sh->dev[i];
1367 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1368 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1369 test_bit(R5_Insync, &dev->flags)) {
1370 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1371 {
1372 PRINTK("Read_old block %d for Reconstruct\n", i);
1373 set_bit(R5_LOCKED, &dev->flags);
1374 set_bit(R5_Wantread, &dev->flags);
1375 locked++;
1376 } else {
1377 set_bit(STRIPE_DELAYED, &sh->state);
1378 set_bit(STRIPE_HANDLE, &sh->state);
1379 }
1380 }
1381 }
1382 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown72626682005-09-09 16:23:54 -07001383 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1384 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 PRINTK("Computing parity...\n");
1386 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1387 /* now every locked buffer is ready to be written */
1388 for (i=disks; i--;)
1389 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1390 PRINTK("Writing block %d\n", i);
1391 locked++;
1392 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1393 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1394 || (i==sh->pd_idx && failed == 0))
1395 set_bit(STRIPE_INSYNC, &sh->state);
1396 }
1397 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1398 atomic_dec(&conf->preread_active_stripes);
1399 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1400 md_wakeup_thread(conf->mddev->thread);
1401 }
1402 }
1403 }
1404
1405 /* maybe we need to check and possibly fix the parity for this stripe
1406 * Any reads will already have been scheduled, so we just see if enough data
1407 * is available
1408 */
1409 if (syncing && locked == 0 &&
NeilBrown14f8d262006-01-06 00:20:14 -08001410 !test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 set_bit(STRIPE_HANDLE, &sh->state);
1412 if (failed == 0) {
1413 char *pagea;
1414 if (uptodate != disks)
1415 BUG();
1416 compute_parity(sh, CHECK_PARITY);
1417 uptodate--;
1418 pagea = page_address(sh->dev[sh->pd_idx].page);
1419 if ((*(u32*)pagea) == 0 &&
1420 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1421 /* parity is correct (on disc, not in buffer any more) */
1422 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown9d888832005-11-08 21:39:26 -08001423 } else {
1424 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1425 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1426 /* don't try to repair!! */
1427 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown14f8d262006-01-06 00:20:14 -08001428 else {
1429 compute_block(sh, sh->pd_idx);
1430 uptodate++;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 }
1434 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001435 /* either failed parity check, or recovery is happening */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (failed==0)
1437 failed_num = sh->pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 dev = &sh->dev[failed_num];
NeilBrown14f8d262006-01-06 00:20:14 -08001439 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1440 BUG_ON(uptodate != disks);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 set_bit(R5_LOCKED, &dev->flags);
1443 set_bit(R5_Wantwrite, &dev->flags);
NeilBrown72626682005-09-09 16:23:54 -07001444 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 locked++;
1446 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448 }
1449 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1450 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1451 clear_bit(STRIPE_SYNCING, &sh->state);
1452 }
NeilBrown4e5314b2005-11-08 21:39:22 -08001453
1454 /* If the failed drive is just a ReadError, then we might need to progress
1455 * the repair/check process
1456 */
NeilBrownba22dcb2005-11-08 21:39:31 -08001457 if (failed == 1 && ! conf->mddev->ro &&
1458 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001459 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1460 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1461 ) {
1462 dev = &sh->dev[failed_num];
1463 if (!test_bit(R5_ReWrite, &dev->flags)) {
1464 set_bit(R5_Wantwrite, &dev->flags);
1465 set_bit(R5_ReWrite, &dev->flags);
1466 set_bit(R5_LOCKED, &dev->flags);
1467 } else {
1468 /* let's read it back */
1469 set_bit(R5_Wantread, &dev->flags);
1470 set_bit(R5_LOCKED, &dev->flags);
1471 }
1472 }
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 spin_unlock(&sh->lock);
1475
1476 while ((bi=return_bi)) {
1477 int bytes = bi->bi_size;
1478
1479 return_bi = bi->bi_next;
1480 bi->bi_next = NULL;
1481 bi->bi_size = 0;
1482 bi->bi_end_io(bi, bytes, 0);
1483 }
1484 for (i=disks; i-- ;) {
1485 int rw;
1486 struct bio *bi;
1487 mdk_rdev_t *rdev;
1488 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1489 rw = 1;
1490 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1491 rw = 0;
1492 else
1493 continue;
1494
1495 bi = &sh->dev[i].req;
1496
1497 bi->bi_rw = rw;
1498 if (rw)
1499 bi->bi_end_io = raid5_end_write_request;
1500 else
1501 bi->bi_end_io = raid5_end_read_request;
1502
1503 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001504 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001505 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 rdev = NULL;
1507 if (rdev)
1508 atomic_inc(&rdev->nr_pending);
1509 rcu_read_unlock();
1510
1511 if (rdev) {
NeilBrown9910f162006-01-06 00:20:24 -08001512 if (syncing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1514
1515 bi->bi_bdev = rdev->bdev;
1516 PRINTK("for %llu schedule op %ld on disc %d\n",
1517 (unsigned long long)sh->sector, bi->bi_rw, i);
1518 atomic_inc(&sh->count);
1519 bi->bi_sector = sh->sector + rdev->data_offset;
1520 bi->bi_flags = 1 << BIO_UPTODATE;
1521 bi->bi_vcnt = 1;
1522 bi->bi_max_vecs = 1;
1523 bi->bi_idx = 0;
1524 bi->bi_io_vec = &sh->dev[i].vec;
1525 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1526 bi->bi_io_vec[0].bv_offset = 0;
1527 bi->bi_size = STRIPE_SIZE;
1528 bi->bi_next = NULL;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001529 if (rw == WRITE &&
1530 test_bit(R5_ReWrite, &sh->dev[i].flags))
1531 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 generic_make_request(bi);
1533 } else {
NeilBrown72626682005-09-09 16:23:54 -07001534 if (rw == 1)
1535 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 PRINTK("skip op %ld on disc %d for sector %llu\n",
1537 bi->bi_rw, i, (unsigned long long)sh->sector);
1538 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1539 set_bit(STRIPE_HANDLE, &sh->state);
1540 }
1541 }
1542}
1543
Arjan van de Ven858119e2006-01-14 13:20:43 -08001544static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1547 while (!list_empty(&conf->delayed_list)) {
1548 struct list_head *l = conf->delayed_list.next;
1549 struct stripe_head *sh;
1550 sh = list_entry(l, struct stripe_head, lru);
1551 list_del_init(l);
1552 clear_bit(STRIPE_DELAYED, &sh->state);
1553 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1554 atomic_inc(&conf->preread_active_stripes);
1555 list_add_tail(&sh->lru, &conf->handle_list);
1556 }
1557 }
1558}
1559
Arjan van de Ven858119e2006-01-14 13:20:43 -08001560static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07001561{
1562 /* device_lock is held */
1563 struct list_head head;
1564 list_add(&head, &conf->bitmap_list);
1565 list_del_init(&conf->bitmap_list);
1566 while (!list_empty(&head)) {
1567 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1568 list_del_init(&sh->lru);
1569 atomic_inc(&sh->count);
1570 __release_stripe(conf, sh);
1571 }
1572}
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574static void unplug_slaves(mddev_t *mddev)
1575{
1576 raid5_conf_t *conf = mddev_to_conf(mddev);
1577 int i;
1578
1579 rcu_read_lock();
1580 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08001581 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001582 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1584
1585 atomic_inc(&rdev->nr_pending);
1586 rcu_read_unlock();
1587
1588 if (r_queue->unplug_fn)
1589 r_queue->unplug_fn(r_queue);
1590
1591 rdev_dec_pending(rdev, mddev);
1592 rcu_read_lock();
1593 }
1594 }
1595 rcu_read_unlock();
1596}
1597
1598static void raid5_unplug_device(request_queue_t *q)
1599{
1600 mddev_t *mddev = q->queuedata;
1601 raid5_conf_t *conf = mddev_to_conf(mddev);
1602 unsigned long flags;
1603
1604 spin_lock_irqsave(&conf->device_lock, flags);
1605
NeilBrown72626682005-09-09 16:23:54 -07001606 if (blk_remove_plug(q)) {
1607 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07001609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 md_wakeup_thread(mddev->thread);
1611
1612 spin_unlock_irqrestore(&conf->device_lock, flags);
1613
1614 unplug_slaves(mddev);
1615}
1616
1617static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1618 sector_t *error_sector)
1619{
1620 mddev_t *mddev = q->queuedata;
1621 raid5_conf_t *conf = mddev_to_conf(mddev);
1622 int i, ret = 0;
1623
1624 rcu_read_lock();
1625 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08001626 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001627 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct block_device *bdev = rdev->bdev;
1629 request_queue_t *r_queue = bdev_get_queue(bdev);
1630
1631 if (!r_queue->issue_flush_fn)
1632 ret = -EOPNOTSUPP;
1633 else {
1634 atomic_inc(&rdev->nr_pending);
1635 rcu_read_unlock();
1636 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1637 error_sector);
1638 rdev_dec_pending(rdev, mddev);
1639 rcu_read_lock();
1640 }
1641 }
1642 }
1643 rcu_read_unlock();
1644 return ret;
1645}
1646
1647static inline void raid5_plug_device(raid5_conf_t *conf)
1648{
1649 spin_lock_irq(&conf->device_lock);
1650 blk_plug_device(conf->mddev->queue);
1651 spin_unlock_irq(&conf->device_lock);
1652}
1653
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001654static int make_request(request_queue_t *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 mddev_t *mddev = q->queuedata;
1657 raid5_conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 unsigned int dd_idx, pd_idx;
1659 sector_t new_sector;
1660 sector_t logical_sector, last_sector;
1661 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01001662 const int rw = bio_data_dir(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
NeilBrowne5dcdd82005-09-09 16:23:41 -07001664 if (unlikely(bio_barrier(bi))) {
1665 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1666 return 0;
1667 }
1668
NeilBrown3d310eb2005-06-21 17:17:26 -07001669 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07001670
Jens Axboea3623572005-11-01 09:26:16 +01001671 disk_stat_inc(mddev->gendisk, ios[rw]);
1672 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1675 last_sector = bi->bi_sector + (bi->bi_size>>9);
1676 bi->bi_next = NULL;
1677 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1680 DEFINE_WAIT(w);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001681 int disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001683 retry:
1684 if (likely(conf->expand_progress == MaxSector))
1685 disks = conf->raid_disks;
1686 else {
1687 spin_lock_irq(&conf->device_lock);
1688 disks = conf->raid_disks;
1689 if (logical_sector >= conf->expand_progress)
1690 disks = conf->previous_raid_disks;
1691 spin_unlock_irq(&conf->device_lock);
1692 }
1693 new_sector = raid5_compute_sector(logical_sector, disks, disks - 1,
1694 &dd_idx, &pd_idx, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1696 (unsigned long long)new_sector,
1697 (unsigned long long)logical_sector);
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001700 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001702 if (unlikely(conf->expand_progress != MaxSector)) {
1703 /* expansion might have moved on while waiting for a
1704 * stripe, so we much do the range check again.
1705 */
1706 int must_retry = 0;
1707 spin_lock_irq(&conf->device_lock);
1708 if (logical_sector < conf->expand_progress &&
1709 disks == conf->previous_raid_disks)
1710 /* mismatch, need to try again */
1711 must_retry = 1;
1712 spin_unlock_irq(&conf->device_lock);
1713 if (must_retry) {
1714 release_stripe(sh);
1715 goto retry;
1716 }
1717 }
1718
1719 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
1720 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1721 /* Stripe is busy expanding or
1722 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 * and wait a while
1724 */
1725 raid5_unplug_device(mddev->queue);
1726 release_stripe(sh);
1727 schedule();
1728 goto retry;
1729 }
1730 finish_wait(&conf->wait_for_overlap, &w);
1731 raid5_plug_device(conf);
1732 handle_stripe(sh);
1733 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 } else {
1735 /* cannot get stripe for read-ahead, just give-up */
1736 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1737 finish_wait(&conf->wait_for_overlap, &w);
1738 break;
1739 }
1740
1741 }
1742 spin_lock_irq(&conf->device_lock);
1743 if (--bi->bi_phys_segments == 0) {
1744 int bytes = bi->bi_size;
1745
1746 if ( bio_data_dir(bi) == WRITE )
1747 md_write_end(mddev);
1748 bi->bi_size = 0;
1749 bi->bi_end_io(bi, bytes, 0);
1750 }
1751 spin_unlock_irq(&conf->device_lock);
1752 return 0;
1753}
1754
1755/* FIXME go_faster isn't used */
NeilBrown57afd892005-06-21 17:17:13 -07001756static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1759 struct stripe_head *sh;
1760 int sectors_per_chunk = conf->chunk_size >> 9;
1761 sector_t x;
1762 unsigned long stripe;
1763 int chunk_offset;
1764 int dd_idx, pd_idx;
1765 sector_t first_sector;
1766 int raid_disks = conf->raid_disks;
1767 int data_disks = raid_disks-1;
NeilBrown72626682005-09-09 16:23:54 -07001768 sector_t max_sector = mddev->size << 1;
1769 int sync_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
NeilBrown72626682005-09-09 16:23:54 -07001771 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* just being told to finish up .. nothing much to do */
1773 unplug_slaves(mddev);
NeilBrown72626682005-09-09 16:23:54 -07001774
1775 if (mddev->curr_resync < max_sector) /* aborted */
1776 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1777 &sync_blocks, 1);
1778 else /* compelted sync */
1779 conf->fullsync = 0;
1780 bitmap_close_sync(mddev->bitmap);
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return 0;
1783 }
1784 /* if there is 1 or more failed drives and we are trying
1785 * to resync, then assert that we are finished, because there is
1786 * nothing we can do.
1787 */
1788 if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07001789 sector_t rv = (mddev->size << 1) - sector_nr;
1790 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return rv;
1792 }
NeilBrown72626682005-09-09 16:23:54 -07001793 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08001794 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07001795 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1796 /* we can skip this block, and probably more */
1797 sync_blocks /= STRIPE_SECTORS;
1798 *skipped = 1;
1799 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 x = sector_nr;
1803 chunk_offset = sector_div(x, sectors_per_chunk);
1804 stripe = x;
1805 BUG_ON(x != stripe);
1806
1807 first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1808 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001809 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (sh == NULL) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001811 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /* make sure we don't swamp the stripe cache if someone else
1813 * is trying to get access
1814 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08001815 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
NeilBrown72626682005-09-09 16:23:54 -07001817 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 spin_lock(&sh->lock);
1819 set_bit(STRIPE_SYNCING, &sh->state);
1820 clear_bit(STRIPE_INSYNC, &sh->state);
1821 spin_unlock(&sh->lock);
1822
1823 handle_stripe(sh);
1824 release_stripe(sh);
1825
1826 return STRIPE_SECTORS;
1827}
1828
1829/*
1830 * This is our raid5 kernel thread.
1831 *
1832 * We scan the hash table for stripes which can be handled now.
1833 * During the scan, completed stripes are saved for us by the interrupt
1834 * handler, so that they will not have to wait for our next wakeup.
1835 */
1836static void raid5d (mddev_t *mddev)
1837{
1838 struct stripe_head *sh;
1839 raid5_conf_t *conf = mddev_to_conf(mddev);
1840 int handled;
1841
1842 PRINTK("+++ raid5d active\n");
1843
1844 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 handled = 0;
1847 spin_lock_irq(&conf->device_lock);
1848 while (1) {
1849 struct list_head *first;
1850
NeilBrown72626682005-09-09 16:23:54 -07001851 if (conf->seq_flush - conf->seq_write > 0) {
1852 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08001853 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001854 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08001855 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001856 conf->seq_write = seq;
1857 activate_bit_delay(conf);
1858 }
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (list_empty(&conf->handle_list) &&
1861 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1862 !blk_queue_plugged(mddev->queue) &&
1863 !list_empty(&conf->delayed_list))
1864 raid5_activate_delayed(conf);
1865
1866 if (list_empty(&conf->handle_list))
1867 break;
1868
1869 first = conf->handle_list.next;
1870 sh = list_entry(first, struct stripe_head, lru);
1871
1872 list_del_init(first);
1873 atomic_inc(&sh->count);
1874 if (atomic_read(&sh->count)!= 1)
1875 BUG();
1876 spin_unlock_irq(&conf->device_lock);
1877
1878 handled++;
1879 handle_stripe(sh);
1880 release_stripe(sh);
1881
1882 spin_lock_irq(&conf->device_lock);
1883 }
1884 PRINTK("%d stripes handled\n", handled);
1885
1886 spin_unlock_irq(&conf->device_lock);
1887
1888 unplug_slaves(mddev);
1889
1890 PRINTK("--- raid5d inactive\n");
1891}
1892
NeilBrown3f294f42005-11-08 21:39:25 -08001893static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08001894raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08001895{
NeilBrown007583c2005-11-08 21:39:30 -08001896 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08001897 if (conf)
1898 return sprintf(page, "%d\n", conf->max_nr_stripes);
1899 else
1900 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08001901}
1902
1903static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08001904raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08001905{
NeilBrown007583c2005-11-08 21:39:30 -08001906 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08001907 char *end;
1908 int new;
1909 if (len >= PAGE_SIZE)
1910 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08001911 if (!conf)
1912 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08001913
1914 new = simple_strtoul(page, &end, 10);
1915 if (!*page || (*end && *end != '\n') )
1916 return -EINVAL;
1917 if (new <= 16 || new > 32768)
1918 return -EINVAL;
1919 while (new < conf->max_nr_stripes) {
1920 if (drop_one_stripe(conf))
1921 conf->max_nr_stripes--;
1922 else
1923 break;
1924 }
1925 while (new > conf->max_nr_stripes) {
1926 if (grow_one_stripe(conf))
1927 conf->max_nr_stripes++;
1928 else break;
1929 }
1930 return len;
1931}
NeilBrown007583c2005-11-08 21:39:30 -08001932
NeilBrown96de1e62005-11-08 21:39:39 -08001933static struct md_sysfs_entry
1934raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
1935 raid5_show_stripe_cache_size,
1936 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001937
1938static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001939stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08001940{
NeilBrown007583c2005-11-08 21:39:30 -08001941 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08001942 if (conf)
1943 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
1944 else
1945 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08001946}
1947
NeilBrown96de1e62005-11-08 21:39:39 -08001948static struct md_sysfs_entry
1949raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08001950
NeilBrown007583c2005-11-08 21:39:30 -08001951static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08001952 &raid5_stripecache_size.attr,
1953 &raid5_stripecache_active.attr,
1954 NULL,
1955};
NeilBrown007583c2005-11-08 21:39:30 -08001956static struct attribute_group raid5_attrs_group = {
1957 .name = NULL,
1958 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08001959};
1960
NeilBrown72626682005-09-09 16:23:54 -07001961static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 raid5_conf_t *conf;
1964 int raid_disk, memory;
1965 mdk_rdev_t *rdev;
1966 struct disk_info *disk;
1967 struct list_head *tmp;
1968
1969 if (mddev->level != 5 && mddev->level != 4) {
NeilBrown14f8d262006-01-06 00:20:14 -08001970 printk(KERN_ERR "raid5: %s: raid level not set to 4/5 (%d)\n",
1971 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -EIO;
1973 }
1974
NeilBrownb55e6bf2006-03-27 01:18:06 -08001975 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if ((conf = mddev->private) == NULL)
1977 goto abort;
NeilBrownb55e6bf2006-03-27 01:18:06 -08001978 conf->disks = kzalloc(mddev->raid_disks * sizeof(struct disk_info),
1979 GFP_KERNEL);
1980 if (!conf->disks)
1981 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 conf->mddev = mddev;
1984
NeilBrownfccddba2006-01-06 00:20:33 -08001985 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 spin_lock_init(&conf->device_lock);
1989 init_waitqueue_head(&conf->wait_for_stripe);
1990 init_waitqueue_head(&conf->wait_for_overlap);
1991 INIT_LIST_HEAD(&conf->handle_list);
1992 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07001993 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 INIT_LIST_HEAD(&conf->inactive_list);
1995 atomic_set(&conf->active_stripes, 0);
1996 atomic_set(&conf->preread_active_stripes, 0);
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
1999
2000 ITERATE_RDEV(mddev,rdev,tmp) {
2001 raid_disk = rdev->raid_disk;
2002 if (raid_disk >= mddev->raid_disks
2003 || raid_disk < 0)
2004 continue;
2005 disk = conf->disks + raid_disk;
2006
2007 disk->rdev = rdev;
2008
NeilBrownb2d444d2005-11-08 21:39:31 -08002009 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 char b[BDEVNAME_SIZE];
2011 printk(KERN_INFO "raid5: device %s operational as raid"
2012 " disk %d\n", bdevname(rdev->bdev,b),
2013 raid_disk);
2014 conf->working_disks++;
2015 }
2016 }
2017
2018 conf->raid_disks = mddev->raid_disks;
2019 /*
2020 * 0 for a fully functional array, 1 for a degraded array.
2021 */
2022 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
2023 conf->mddev = mddev;
2024 conf->chunk_size = mddev->chunk_size;
2025 conf->level = mddev->level;
2026 conf->algorithm = mddev->layout;
2027 conf->max_nr_stripes = NR_STRIPES;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002028 conf->expand_progress = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 /* device size must be a multiple of chunk size */
2031 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07002032 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 if (!conf->chunk_size || conf->chunk_size % 4) {
2035 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
2036 conf->chunk_size, mdname(mddev));
2037 goto abort;
2038 }
2039 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
2040 printk(KERN_ERR
2041 "raid5: unsupported parity algorithm %d for %s\n",
2042 conf->algorithm, mdname(mddev));
2043 goto abort;
2044 }
2045 if (mddev->degraded > 1) {
2046 printk(KERN_ERR "raid5: not enough operational devices for %s"
2047 " (%d/%d failed)\n",
2048 mdname(mddev), conf->failed_disks, conf->raid_disks);
2049 goto abort;
2050 }
2051
2052 if (mddev->degraded == 1 &&
2053 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002054 if (mddev->ok_start_degraded)
2055 printk(KERN_WARNING
2056 "raid5: starting dirty degraded array: %s"
2057 "- data corruption possible.\n",
2058 mdname(mddev));
2059 else {
2060 printk(KERN_ERR
2061 "raid5: cannot start dirty degraded array for %s\n",
2062 mdname(mddev));
2063 goto abort;
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
2067 {
2068 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
2069 if (!mddev->thread) {
2070 printk(KERN_ERR
2071 "raid5: couldn't allocate thread for %s\n",
2072 mdname(mddev));
2073 goto abort;
2074 }
2075 }
NeilBrown50368052005-12-12 02:39:17 -08002076 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
2078 if (grow_stripes(conf, conf->max_nr_stripes)) {
2079 printk(KERN_ERR
2080 "raid5: couldn't allocate %dkB for buffers\n", memory);
2081 shrink_stripes(conf);
2082 md_unregister_thread(mddev->thread);
2083 goto abort;
2084 } else
2085 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
2086 memory, mdname(mddev));
2087
2088 if (mddev->degraded == 0)
2089 printk("raid5: raid level %d set %s active with %d out of %d"
2090 " devices, algorithm %d\n", conf->level, mdname(mddev),
2091 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
2092 conf->algorithm);
2093 else
2094 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
2095 " out of %d devices, algorithm %d\n", conf->level,
2096 mdname(mddev), mddev->raid_disks - mddev->degraded,
2097 mddev->raid_disks, conf->algorithm);
2098
2099 print_raid5_conf(conf);
2100
2101 /* read-ahead size must cover two whole stripes, which is
2102 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
2103 */
2104 {
2105 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
NeilBrown2d1f3b52006-01-06 00:20:31 -08002106 / PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
2108 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
2109 }
2110
2111 /* Ok, everything is just fine now */
NeilBrown007583c2005-11-08 21:39:30 -08002112 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
NeilBrown7a5febe2005-05-16 21:53:16 -07002113
2114 mddev->queue->unplug_fn = raid5_unplug_device;
2115 mddev->queue->issue_flush_fn = raid5_issue_flush;
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 mddev->array_size = mddev->size * (mddev->raid_disks - 1);
2118 return 0;
2119abort:
2120 if (conf) {
2121 print_raid5_conf(conf);
NeilBrownb55e6bf2006-03-27 01:18:06 -08002122 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08002123 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 kfree(conf);
2125 }
2126 mddev->private = NULL;
2127 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
2128 return -EIO;
2129}
2130
2131
2132
NeilBrown3f294f42005-11-08 21:39:25 -08002133static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2136
2137 md_unregister_thread(mddev->thread);
2138 mddev->thread = NULL;
2139 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08002140 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08002142 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08002143 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08002144 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 mddev->private = NULL;
2146 return 0;
2147}
2148
2149#if RAID5_DEBUG
2150static void print_sh (struct stripe_head *sh)
2151{
2152 int i;
2153
2154 printk("sh %llu, pd_idx %d, state %ld.\n",
2155 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2156 printk("sh %llu, count %d.\n",
2157 (unsigned long long)sh->sector, atomic_read(&sh->count));
2158 printk("sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002159 for (i = 0; i < sh->disks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 printk("(cache%d: %p %ld) ",
2161 i, sh->dev[i].page, sh->dev[i].flags);
2162 }
2163 printk("\n");
2164}
2165
2166static void printall (raid5_conf_t *conf)
2167{
2168 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08002169 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 int i;
2171
2172 spin_lock_irq(&conf->device_lock);
2173 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08002174 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (sh->raid_conf != conf)
2176 continue;
2177 print_sh(sh);
2178 }
2179 }
2180 spin_unlock_irq(&conf->device_lock);
2181}
2182#endif
2183
2184static void status (struct seq_file *seq, mddev_t *mddev)
2185{
2186 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2187 int i;
2188
2189 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2190 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2191 for (i = 0; i < conf->raid_disks; i++)
2192 seq_printf (seq, "%s",
2193 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08002194 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 seq_printf (seq, "]");
2196#if RAID5_DEBUG
2197#define D(x) \
2198 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2199 printall(conf);
2200#endif
2201}
2202
2203static void print_raid5_conf (raid5_conf_t *conf)
2204{
2205 int i;
2206 struct disk_info *tmp;
2207
2208 printk("RAID5 conf printout:\n");
2209 if (!conf) {
2210 printk("(conf==NULL)\n");
2211 return;
2212 }
2213 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2214 conf->working_disks, conf->failed_disks);
2215
2216 for (i = 0; i < conf->raid_disks; i++) {
2217 char b[BDEVNAME_SIZE];
2218 tmp = conf->disks + i;
2219 if (tmp->rdev)
2220 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08002221 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 bdevname(tmp->rdev->bdev,b));
2223 }
2224}
2225
2226static int raid5_spare_active(mddev_t *mddev)
2227{
2228 int i;
2229 raid5_conf_t *conf = mddev->private;
2230 struct disk_info *tmp;
2231
2232 for (i = 0; i < conf->raid_disks; i++) {
2233 tmp = conf->disks + i;
2234 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08002235 && !test_bit(Faulty, &tmp->rdev->flags)
2236 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 mddev->degraded--;
2238 conf->failed_disks--;
2239 conf->working_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -08002240 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 }
2243 print_raid5_conf(conf);
2244 return 0;
2245}
2246
2247static int raid5_remove_disk(mddev_t *mddev, int number)
2248{
2249 raid5_conf_t *conf = mddev->private;
2250 int err = 0;
2251 mdk_rdev_t *rdev;
2252 struct disk_info *p = conf->disks + number;
2253
2254 print_raid5_conf(conf);
2255 rdev = p->rdev;
2256 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002257 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 atomic_read(&rdev->nr_pending)) {
2259 err = -EBUSY;
2260 goto abort;
2261 }
2262 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002263 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (atomic_read(&rdev->nr_pending)) {
2265 /* lost the race, try later */
2266 err = -EBUSY;
2267 p->rdev = rdev;
2268 }
2269 }
2270abort:
2271
2272 print_raid5_conf(conf);
2273 return err;
2274}
2275
2276static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2277{
2278 raid5_conf_t *conf = mddev->private;
2279 int found = 0;
2280 int disk;
2281 struct disk_info *p;
2282
2283 if (mddev->degraded > 1)
2284 /* no point adding a device */
2285 return 0;
2286
2287 /*
2288 * find the disk ...
2289 */
2290 for (disk=0; disk < mddev->raid_disks; disk++)
2291 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002292 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 rdev->raid_disk = disk;
2294 found = 1;
NeilBrown72626682005-09-09 16:23:54 -07002295 if (rdev->saved_raid_disk != disk)
2296 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08002297 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 break;
2299 }
2300 print_raid5_conf(conf);
2301 return found;
2302}
2303
2304static int raid5_resize(mddev_t *mddev, sector_t sectors)
2305{
2306 /* no resync is happening, and there is enough space
2307 * on all devices, so we can resize.
2308 * We need to make sure resync covers any new space.
2309 * If the array is shrinking we should possibly wait until
2310 * any io in the removed space completes, but it hardly seems
2311 * worth it.
2312 */
2313 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2314 mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
2315 set_capacity(mddev->gendisk, mddev->array_size << 1);
2316 mddev->changed = 1;
2317 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2318 mddev->recovery_cp = mddev->size << 1;
2319 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2320 }
2321 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07002322 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324}
2325
NeilBrown72626682005-09-09 16:23:54 -07002326static void raid5_quiesce(mddev_t *mddev, int state)
2327{
2328 raid5_conf_t *conf = mddev_to_conf(mddev);
2329
2330 switch(state) {
2331 case 1: /* stop all writes */
2332 spin_lock_irq(&conf->device_lock);
2333 conf->quiesce = 1;
2334 wait_event_lock_irq(conf->wait_for_stripe,
2335 atomic_read(&conf->active_stripes) == 0,
2336 conf->device_lock, /* nothing */);
2337 spin_unlock_irq(&conf->device_lock);
2338 break;
2339
2340 case 0: /* re-enable writes */
2341 spin_lock_irq(&conf->device_lock);
2342 conf->quiesce = 0;
2343 wake_up(&conf->wait_for_stripe);
2344 spin_unlock_irq(&conf->device_lock);
2345 break;
2346 }
NeilBrown72626682005-09-09 16:23:54 -07002347}
NeilBrownb15c2e52006-01-06 00:20:16 -08002348
NeilBrown2604b702006-01-06 00:20:36 -08002349static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08002352 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 .owner = THIS_MODULE,
2354 .make_request = make_request,
2355 .run = run,
2356 .stop = stop,
2357 .status = status,
2358 .error_handler = error,
2359 .hot_add_disk = raid5_add_disk,
2360 .hot_remove_disk= raid5_remove_disk,
2361 .spare_active = raid5_spare_active,
2362 .sync_request = sync_request,
2363 .resize = raid5_resize,
NeilBrown72626682005-09-09 16:23:54 -07002364 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365};
2366
NeilBrown2604b702006-01-06 00:20:36 -08002367static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
NeilBrown2604b702006-01-06 00:20:36 -08002369 .name = "raid4",
2370 .level = 4,
2371 .owner = THIS_MODULE,
2372 .make_request = make_request,
2373 .run = run,
2374 .stop = stop,
2375 .status = status,
2376 .error_handler = error,
2377 .hot_add_disk = raid5_add_disk,
2378 .hot_remove_disk= raid5_remove_disk,
2379 .spare_active = raid5_spare_active,
2380 .sync_request = sync_request,
2381 .resize = raid5_resize,
2382 .quiesce = raid5_quiesce,
2383};
2384
2385static int __init raid5_init(void)
2386{
2387 register_md_personality(&raid5_personality);
2388 register_md_personality(&raid4_personality);
2389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
NeilBrown2604b702006-01-06 00:20:36 -08002392static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
NeilBrown2604b702006-01-06 00:20:36 -08002394 unregister_md_personality(&raid5_personality);
2395 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
2398module_init(raid5_init);
2399module_exit(raid5_exit);
2400MODULE_LICENSE("GPL");
2401MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08002402MODULE_ALIAS("md-raid5");
2403MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08002404MODULE_ALIAS("md-level-5");
2405MODULE_ALIAS("md-level-4");