blob: dd0d00108a31a1eb8c502795839eda3108c6988e [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
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/highmem.h>
25#include <linux/bitops.h>
NeilBrownf6705572006-03-27 01:18:11 -080026#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/atomic.h>
NeilBrown16a53ec2006-06-26 00:27:38 -070028#include "raid6.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
NeilBrown72626682005-09-09 16:23:54 -070030#include <linux/raid/bitmap.h>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * Stripe cache
34 */
35
36#define NR_STRIPES 256
37#define STRIPE_SIZE PAGE_SIZE
38#define STRIPE_SHIFT (PAGE_SHIFT - 9)
39#define STRIPE_SECTORS (STRIPE_SIZE>>9)
40#define IO_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080041#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define HASH_MASK (NR_HASH - 1)
43
NeilBrownfccddba2006-01-06 00:20:33 -080044#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* bio's attached to a stripe+device for I/O are linked together in bi_sector
47 * order without overlap. There may be several bio's per stripe+device, and
48 * a bio could span several devices.
49 * When walking this list for a particular stripe+device, we must never proceed
50 * beyond a bio that extends past this device, as the next bio might no longer
51 * be valid.
52 * This macro is used to determine the 'next' bio in the list, given the sector
53 * of the current stripe+device
54 */
55#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
56/*
57 * The following can be used to debug the driver
58 */
59#define RAID5_DEBUG 0
60#define RAID5_PARANOIA 1
61#if RAID5_PARANOIA && defined(CONFIG_SMP)
62# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
63#else
64# define CHECK_DEVLOCK()
65#endif
66
67#define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
68#if RAID5_DEBUG
69#define inline
70#define __inline__
71#endif
72
NeilBrown16a53ec2006-06-26 00:27:38 -070073#if !RAID6_USE_EMPTY_ZERO_PAGE
74/* In .bss so it's zeroed */
75const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
76#endif
77
78static inline int raid6_next_disk(int disk, int raid_disks)
79{
80 disk++;
81 return (disk < raid_disks) ? disk : 0;
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static void print_raid5_conf (raid5_conf_t *conf);
84
Arjan van de Ven858119e2006-01-14 13:20:43 -080085static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +020088 BUG_ON(!list_empty(&sh->lru));
89 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (test_bit(STRIPE_HANDLE, &sh->state)) {
91 if (test_bit(STRIPE_DELAYED, &sh->state))
92 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -070093 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
94 conf->seq_write == sh->bm_seq)
95 list_add_tail(&sh->lru, &conf->bitmap_list);
96 else {
97 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -070099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 md_wakeup_thread(conf->mddev->thread);
101 } else {
102 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
103 atomic_dec(&conf->preread_active_stripes);
104 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
105 md_wakeup_thread(conf->mddev->thread);
106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800108 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
109 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 wake_up(&conf->wait_for_stripe);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 }
114}
115static void release_stripe(struct stripe_head *sh)
116{
117 raid5_conf_t *conf = sh->raid_conf;
118 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 spin_lock_irqsave(&conf->device_lock, flags);
121 __release_stripe(conf, sh);
122 spin_unlock_irqrestore(&conf->device_lock, flags);
123}
124
NeilBrownfccddba2006-01-06 00:20:33 -0800125static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
128
NeilBrownfccddba2006-01-06 00:20:33 -0800129 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
NeilBrown16a53ec2006-06-26 00:27:38 -0700132static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
NeilBrownfccddba2006-01-06 00:20:33 -0800134 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
137
138 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800139 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142
143/* find an idle stripe, make sure it is unhashed, and return it. */
144static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
145{
146 struct stripe_head *sh = NULL;
147 struct list_head *first;
148
149 CHECK_DEVLOCK();
150 if (list_empty(&conf->inactive_list))
151 goto out;
152 first = conf->inactive_list.next;
153 sh = list_entry(first, struct stripe_head, lru);
154 list_del_init(first);
155 remove_hash(sh);
156 atomic_inc(&conf->active_stripes);
157out:
158 return sh;
159}
160
161static void shrink_buffers(struct stripe_head *sh, int num)
162{
163 struct page *p;
164 int i;
165
166 for (i=0; i<num ; i++) {
167 p = sh->dev[i].page;
168 if (!p)
169 continue;
170 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800171 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173}
174
175static int grow_buffers(struct stripe_head *sh, int num)
176{
177 int i;
178
179 for (i=0; i<num; i++) {
180 struct page *page;
181
182 if (!(page = alloc_page(GFP_KERNEL))) {
183 return 1;
184 }
185 sh->dev[i].page = page;
186 }
187 return 0;
188}
189
190static void raid5_build_block (struct stripe_head *sh, int i);
191
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800192static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800195 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200197 BUG_ON(atomic_read(&sh->count) != 0);
198 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 CHECK_DEVLOCK();
201 PRINTK("init_stripe called, stripe %llu\n",
202 (unsigned long long)sh->sector);
203
204 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 sh->sector = sector;
207 sh->pd_idx = pd_idx;
208 sh->state = 0;
209
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800210 sh->disks = disks;
211
212 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct r5dev *dev = &sh->dev[i];
214
215 if (dev->toread || dev->towrite || dev->written ||
216 test_bit(R5_LOCKED, &dev->flags)) {
217 printk("sector=%llx i=%d %p %p %p %d\n",
218 (unsigned long long)sh->sector, i, dev->toread,
219 dev->towrite, dev->written,
220 test_bit(R5_LOCKED, &dev->flags));
221 BUG();
222 }
223 dev->flags = 0;
224 raid5_build_block(sh, i);
225 }
226 insert_hash(conf, sh);
227}
228
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800229static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800232 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 CHECK_DEVLOCK();
235 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800236 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800237 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return sh;
239 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
240 return NULL;
241}
242
243static void unplug_slaves(mddev_t *mddev);
244static void raid5_unplug_device(request_queue_t *q);
245
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800246static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
247 int pd_idx, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct stripe_head *sh;
250
251 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
252
253 spin_lock_irq(&conf->device_lock);
254
255 do {
NeilBrown72626682005-09-09 16:23:54 -0700256 wait_event_lock_irq(conf->wait_for_stripe,
257 conf->quiesce == 0,
258 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800259 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!sh) {
261 if (!conf->inactive_blocked)
262 sh = get_free_stripe(conf);
263 if (noblock && sh == NULL)
264 break;
265 if (!sh) {
266 conf->inactive_blocked = 1;
267 wait_event_lock_irq(conf->wait_for_stripe,
268 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800269 (atomic_read(&conf->active_stripes)
270 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 || !conf->inactive_blocked),
272 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700273 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 );
275 conf->inactive_blocked = 0;
276 } else
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800277 init_stripe(sh, sector, pd_idx, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 } else {
279 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200280 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 } else {
282 if (!test_bit(STRIPE_HANDLE, &sh->state))
283 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700284 if (list_empty(&sh->lru) &&
285 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700286 BUG();
287 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 }
290 } while (sh == NULL);
291
292 if (sh)
293 atomic_inc(&sh->count);
294
295 spin_unlock_irq(&conf->device_lock);
296 return sh;
297}
298
NeilBrown3f294f42005-11-08 21:39:25 -0800299static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800302 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
303 if (!sh)
304 return 0;
305 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
306 sh->raid_conf = conf;
307 spin_lock_init(&sh->lock);
308
309 if (grow_buffers(sh, conf->raid_disks)) {
310 shrink_buffers(sh, conf->raid_disks);
311 kmem_cache_free(conf->slab_cache, sh);
312 return 0;
313 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800314 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800315 /* we just created an active stripe so... */
316 atomic_set(&sh->count, 1);
317 atomic_inc(&conf->active_stripes);
318 INIT_LIST_HEAD(&sh->lru);
319 release_stripe(sh);
320 return 1;
321}
322
323static int grow_stripes(raid5_conf_t *conf, int num)
324{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 kmem_cache_t *sc;
326 int devs = conf->raid_disks;
327
NeilBrownad01c9e2006-03-27 01:18:07 -0800328 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
329 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
330 conf->active_name = 0;
331 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
333 0, 0, NULL, NULL);
334 if (!sc)
335 return 1;
336 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800337 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700338 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800339 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342}
NeilBrown29269552006-03-27 01:18:10 -0800343
344#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800345static int resize_stripes(raid5_conf_t *conf, int newsize)
346{
347 /* Make all the stripes able to hold 'newsize' devices.
348 * New slots in each stripe get 'page' set to a new page.
349 *
350 * This happens in stages:
351 * 1/ create a new kmem_cache and allocate the required number of
352 * stripe_heads.
353 * 2/ gather all the old stripe_heads and tranfer the pages across
354 * to the new stripe_heads. This will have the side effect of
355 * freezing the array as once all stripe_heads have been collected,
356 * no IO will be possible. Old stripe heads are freed once their
357 * pages have been transferred over, and the old kmem_cache is
358 * freed when all stripes are done.
359 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
360 * we simple return a failre status - no need to clean anything up.
361 * 4/ allocate new pages for the new slots in the new stripe_heads.
362 * If this fails, we don't bother trying the shrink the
363 * stripe_heads down again, we just leave them as they are.
364 * As each stripe_head is processed the new one is released into
365 * active service.
366 *
367 * Once step2 is started, we cannot afford to wait for a write,
368 * so we use GFP_NOIO allocations.
369 */
370 struct stripe_head *osh, *nsh;
371 LIST_HEAD(newstripes);
372 struct disk_info *ndisks;
373 int err = 0;
374 kmem_cache_t *sc;
375 int i;
376
377 if (newsize <= conf->pool_size)
378 return 0; /* never bother to shrink */
379
380 /* Step 1 */
381 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
382 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
383 0, 0, NULL, NULL);
384 if (!sc)
385 return -ENOMEM;
386
387 for (i = conf->max_nr_stripes; i; i--) {
388 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
389 if (!nsh)
390 break;
391
392 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
393
394 nsh->raid_conf = conf;
395 spin_lock_init(&nsh->lock);
396
397 list_add(&nsh->lru, &newstripes);
398 }
399 if (i) {
400 /* didn't get enough, give up */
401 while (!list_empty(&newstripes)) {
402 nsh = list_entry(newstripes.next, struct stripe_head, lru);
403 list_del(&nsh->lru);
404 kmem_cache_free(sc, nsh);
405 }
406 kmem_cache_destroy(sc);
407 return -ENOMEM;
408 }
409 /* Step 2 - Must use GFP_NOIO now.
410 * OK, we have enough stripes, start collecting inactive
411 * stripes and copying them over
412 */
413 list_for_each_entry(nsh, &newstripes, lru) {
414 spin_lock_irq(&conf->device_lock);
415 wait_event_lock_irq(conf->wait_for_stripe,
416 !list_empty(&conf->inactive_list),
417 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -0800418 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -0800419 );
420 osh = get_free_stripe(conf);
421 spin_unlock_irq(&conf->device_lock);
422 atomic_set(&nsh->count, 1);
423 for(i=0; i<conf->pool_size; i++)
424 nsh->dev[i].page = osh->dev[i].page;
425 for( ; i<newsize; i++)
426 nsh->dev[i].page = NULL;
427 kmem_cache_free(conf->slab_cache, osh);
428 }
429 kmem_cache_destroy(conf->slab_cache);
430
431 /* Step 3.
432 * At this point, we are holding all the stripes so the array
433 * is completely stalled, so now is a good time to resize
434 * conf->disks.
435 */
436 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
437 if (ndisks) {
438 for (i=0; i<conf->raid_disks; i++)
439 ndisks[i] = conf->disks[i];
440 kfree(conf->disks);
441 conf->disks = ndisks;
442 } else
443 err = -ENOMEM;
444
445 /* Step 4, return new stripes to service */
446 while(!list_empty(&newstripes)) {
447 nsh = list_entry(newstripes.next, struct stripe_head, lru);
448 list_del_init(&nsh->lru);
449 for (i=conf->raid_disks; i < newsize; i++)
450 if (nsh->dev[i].page == NULL) {
451 struct page *p = alloc_page(GFP_NOIO);
452 nsh->dev[i].page = p;
453 if (!p)
454 err = -ENOMEM;
455 }
456 release_stripe(nsh);
457 }
458 /* critical section pass, GFP_NOIO no longer needed */
459
460 conf->slab_cache = sc;
461 conf->active_name = 1-conf->active_name;
462 conf->pool_size = newsize;
463 return err;
464}
NeilBrown29269552006-03-27 01:18:10 -0800465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
NeilBrown3f294f42005-11-08 21:39:25 -0800467static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct stripe_head *sh;
470
NeilBrown3f294f42005-11-08 21:39:25 -0800471 spin_lock_irq(&conf->device_lock);
472 sh = get_free_stripe(conf);
473 spin_unlock_irq(&conf->device_lock);
474 if (!sh)
475 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200476 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -0800477 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -0800478 kmem_cache_free(conf->slab_cache, sh);
479 atomic_dec(&conf->active_stripes);
480 return 1;
481}
482
483static void shrink_stripes(raid5_conf_t *conf)
484{
485 while (drop_one_stripe(conf))
486 ;
487
NeilBrown29fc7e32006-02-03 03:03:41 -0800488 if (conf->slab_cache)
489 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 conf->slab_cache = NULL;
491}
492
NeilBrown4e5314b2005-11-08 21:39:22 -0800493static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int error)
495{
496 struct stripe_head *sh = bi->bi_private;
497 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800498 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
500
501 if (bi->bi_size)
502 return 1;
503
504 for (i=0 ; i<disks; i++)
505 if (bi == &sh->dev[i].req)
506 break;
507
508 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
509 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
510 uptodate);
511 if (i == disks) {
512 BUG();
513 return 0;
514 }
515
516 if (uptodate) {
517#if 0
518 struct bio *bio;
519 unsigned long flags;
520 spin_lock_irqsave(&conf->device_lock, flags);
521 /* we can return a buffer if we bypassed the cache or
522 * if the top buffer is not in highmem. If there are
523 * multiple buffers, leave the extra work to
524 * handle_stripe
525 */
526 buffer = sh->bh_read[i];
527 if (buffer &&
528 (!PageHighMem(buffer->b_page)
529 || buffer->b_page == bh->b_page )
530 ) {
531 sh->bh_read[i] = buffer->b_reqnext;
532 buffer->b_reqnext = NULL;
533 } else
534 buffer = NULL;
535 spin_unlock_irqrestore(&conf->device_lock, flags);
536 if (sh->bh_page[i]==bh->b_page)
537 set_buffer_uptodate(bh);
538 if (buffer) {
539 if (buffer->b_page != bh->b_page)
540 memcpy(buffer->b_data, bh->b_data, bh->b_size);
541 buffer->b_end_io(buffer, 1);
542 }
543#else
544 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -0800545#endif
546 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -0800547 printk(KERN_INFO "raid5: read error corrected!!\n");
NeilBrown4e5314b2005-11-08 21:39:22 -0800548 clear_bit(R5_ReadError, &sh->dev[i].flags);
549 clear_bit(R5_ReWrite, &sh->dev[i].flags);
550 }
NeilBrownba22dcb2005-11-08 21:39:31 -0800551 if (atomic_read(&conf->disks[i].rdev->read_errors))
552 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 } else {
NeilBrownba22dcb2005-11-08 21:39:31 -0800554 int retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -0800556 atomic_inc(&conf->disks[i].rdev->read_errors);
557 if (conf->mddev->degraded)
NeilBrown14f8d262006-01-06 00:20:14 -0800558 printk(KERN_WARNING "raid5: read error not correctable.\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800559 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -0800560 /* Oh, no!!! */
NeilBrown14f8d262006-01-06 00:20:14 -0800561 printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800562 else if (atomic_read(&conf->disks[i].rdev->read_errors)
563 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -0800564 printk(KERN_WARNING
565 "raid5: Too many read errors, failing device.\n");
NeilBrownba22dcb2005-11-08 21:39:31 -0800566 else
567 retry = 1;
568 if (retry)
569 set_bit(R5_ReadError, &sh->dev[i].flags);
570 else {
NeilBrown4e5314b2005-11-08 21:39:22 -0800571 clear_bit(R5_ReadError, &sh->dev[i].flags);
572 clear_bit(R5_ReWrite, &sh->dev[i].flags);
573 md_error(conf->mddev, conf->disks[i].rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -0800574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
577#if 0
578 /* must restore b_page before unlocking buffer... */
579 if (sh->bh_page[i] != bh->b_page) {
580 bh->b_page = sh->bh_page[i];
581 bh->b_data = page_address(bh->b_page);
582 clear_buffer_uptodate(bh);
583 }
584#endif
585 clear_bit(R5_LOCKED, &sh->dev[i].flags);
586 set_bit(STRIPE_HANDLE, &sh->state);
587 release_stripe(sh);
588 return 0;
589}
590
591static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
592 int error)
593{
594 struct stripe_head *sh = bi->bi_private;
595 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800596 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned long flags;
598 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
599
600 if (bi->bi_size)
601 return 1;
602
603 for (i=0 ; i<disks; i++)
604 if (bi == &sh->dev[i].req)
605 break;
606
607 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
608 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
609 uptodate);
610 if (i == disks) {
611 BUG();
612 return 0;
613 }
614
615 spin_lock_irqsave(&conf->device_lock, flags);
616 if (!uptodate)
617 md_error(conf->mddev, conf->disks[i].rdev);
618
619 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
620
621 clear_bit(R5_LOCKED, &sh->dev[i].flags);
622 set_bit(STRIPE_HANDLE, &sh->state);
623 __release_stripe(conf, sh);
624 spin_unlock_irqrestore(&conf->device_lock, flags);
625 return 0;
626}
627
628
629static sector_t compute_blocknr(struct stripe_head *sh, int i);
630
631static void raid5_build_block (struct stripe_head *sh, int i)
632{
633 struct r5dev *dev = &sh->dev[i];
634
635 bio_init(&dev->req);
636 dev->req.bi_io_vec = &dev->vec;
637 dev->req.bi_vcnt++;
638 dev->req.bi_max_vecs++;
639 dev->vec.bv_page = dev->page;
640 dev->vec.bv_len = STRIPE_SIZE;
641 dev->vec.bv_offset = 0;
642
643 dev->req.bi_sector = sh->sector;
644 dev->req.bi_private = sh;
645
646 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -0700647 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650static void error(mddev_t *mddev, mdk_rdev_t *rdev)
651{
652 char b[BDEVNAME_SIZE];
653 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
654 PRINTK("raid5: error called\n");
655
NeilBrownb2d444d2005-11-08 21:39:31 -0800656 if (!test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 mddev->sb_dirty = 1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800658 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 conf->working_disks--;
660 mddev->degraded++;
661 conf->failed_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -0800662 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * if recovery was running, make sure it aborts.
665 */
666 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
667 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800668 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 printk (KERN_ALERT
670 "raid5: Disk failure on %s, disabling device."
671 " Operation continuing on %d devices\n",
672 bdevname(rdev->bdev,b), conf->working_disks);
673 }
NeilBrown16a53ec2006-06-26 00:27:38 -0700674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676/*
677 * Input: a 'big' sector number,
678 * Output: index of the data and parity disk, and the sector # in them.
679 */
680static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
681 unsigned int data_disks, unsigned int * dd_idx,
682 unsigned int * pd_idx, raid5_conf_t *conf)
683{
684 long stripe;
685 unsigned long chunk_number;
686 unsigned int chunk_offset;
687 sector_t new_sector;
688 int sectors_per_chunk = conf->chunk_size >> 9;
689
690 /* First compute the information on this sector */
691
692 /*
693 * Compute the chunk number and the sector offset inside the chunk
694 */
695 chunk_offset = sector_div(r_sector, sectors_per_chunk);
696 chunk_number = r_sector;
697 BUG_ON(r_sector != chunk_number);
698
699 /*
700 * Compute the stripe number
701 */
702 stripe = chunk_number / data_disks;
703
704 /*
705 * Compute the data disk and parity disk indexes inside the stripe
706 */
707 *dd_idx = chunk_number % data_disks;
708
709 /*
710 * Select the parity disk based on the user selected algorithm.
711 */
NeilBrown16a53ec2006-06-26 00:27:38 -0700712 switch(conf->level) {
713 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 *pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -0700715 break;
716 case 5:
717 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 case ALGORITHM_LEFT_ASYMMETRIC:
719 *pd_idx = data_disks - stripe % raid_disks;
720 if (*dd_idx >= *pd_idx)
721 (*dd_idx)++;
722 break;
723 case ALGORITHM_RIGHT_ASYMMETRIC:
724 *pd_idx = stripe % raid_disks;
725 if (*dd_idx >= *pd_idx)
726 (*dd_idx)++;
727 break;
728 case ALGORITHM_LEFT_SYMMETRIC:
729 *pd_idx = data_disks - stripe % raid_disks;
730 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
731 break;
732 case ALGORITHM_RIGHT_SYMMETRIC:
733 *pd_idx = stripe % raid_disks;
734 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
735 break;
736 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800737 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700739 }
740 break;
741 case 6:
742
743 /**** FIX THIS ****/
744 switch (conf->algorithm) {
745 case ALGORITHM_LEFT_ASYMMETRIC:
746 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
747 if (*pd_idx == raid_disks-1)
748 (*dd_idx)++; /* Q D D D P */
749 else if (*dd_idx >= *pd_idx)
750 (*dd_idx) += 2; /* D D P Q D */
751 break;
752 case ALGORITHM_RIGHT_ASYMMETRIC:
753 *pd_idx = stripe % raid_disks;
754 if (*pd_idx == raid_disks-1)
755 (*dd_idx)++; /* Q D D D P */
756 else if (*dd_idx >= *pd_idx)
757 (*dd_idx) += 2; /* D D P Q D */
758 break;
759 case ALGORITHM_LEFT_SYMMETRIC:
760 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
761 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
762 break;
763 case ALGORITHM_RIGHT_SYMMETRIC:
764 *pd_idx = stripe % raid_disks;
765 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
766 break;
767 default:
768 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
769 conf->algorithm);
770 }
771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774 /*
775 * Finally, compute the new sector number
776 */
777 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
778 return new_sector;
779}
780
781
782static sector_t compute_blocknr(struct stripe_head *sh, int i)
783{
784 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800785 int raid_disks = sh->disks, data_disks = raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 sector_t new_sector = sh->sector, check;
787 int sectors_per_chunk = conf->chunk_size >> 9;
788 sector_t stripe;
789 int chunk_offset;
790 int chunk_number, dummy1, dummy2, dd_idx = i;
791 sector_t r_sector;
792
NeilBrown16a53ec2006-06-26 00:27:38 -0700793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 chunk_offset = sector_div(new_sector, sectors_per_chunk);
795 stripe = new_sector;
796 BUG_ON(new_sector != stripe);
797
NeilBrown16a53ec2006-06-26 00:27:38 -0700798 if (i == sh->pd_idx)
799 return 0;
800 switch(conf->level) {
801 case 4: break;
802 case 5:
803 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 case ALGORITHM_LEFT_ASYMMETRIC:
805 case ALGORITHM_RIGHT_ASYMMETRIC:
806 if (i > sh->pd_idx)
807 i--;
808 break;
809 case ALGORITHM_LEFT_SYMMETRIC:
810 case ALGORITHM_RIGHT_SYMMETRIC:
811 if (i < sh->pd_idx)
812 i += raid_disks;
813 i -= (sh->pd_idx + 1);
814 break;
815 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800816 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -0700817 conf->algorithm);
818 }
819 break;
820 case 6:
821 data_disks = raid_disks - 2;
822 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
823 return 0; /* It is the Q disk */
824 switch (conf->algorithm) {
825 case ALGORITHM_LEFT_ASYMMETRIC:
826 case ALGORITHM_RIGHT_ASYMMETRIC:
827 if (sh->pd_idx == raid_disks-1)
828 i--; /* Q D D D P */
829 else if (i > sh->pd_idx)
830 i -= 2; /* D D P Q D */
831 break;
832 case ALGORITHM_LEFT_SYMMETRIC:
833 case ALGORITHM_RIGHT_SYMMETRIC:
834 if (sh->pd_idx == raid_disks-1)
835 i--; /* Q D D D P */
836 else {
837 /* D D P Q D */
838 if (i < sh->pd_idx)
839 i += raid_disks;
840 i -= (sh->pd_idx + 2);
841 }
842 break;
843 default:
844 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700846 }
847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
850 chunk_number = stripe * data_disks + i;
851 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
852
853 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
854 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -0800855 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return 0;
857 }
858 return r_sector;
859}
860
861
862
863/*
NeilBrown16a53ec2006-06-26 00:27:38 -0700864 * Copy data between a page in the stripe cache, and one or more bion
865 * The page could align with the middle of the bio, or there could be
866 * several bion, each with several bio_vecs, which cover part of the page
867 * Multiple bion are linked together on bi_next. There may be extras
868 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
870static void copy_data(int frombio, struct bio *bio,
871 struct page *page,
872 sector_t sector)
873{
874 char *pa = page_address(page);
875 struct bio_vec *bvl;
876 int i;
877 int page_offset;
878
879 if (bio->bi_sector >= sector)
880 page_offset = (signed)(bio->bi_sector - sector) * 512;
881 else
882 page_offset = (signed)(sector - bio->bi_sector) * -512;
883 bio_for_each_segment(bvl, bio, i) {
884 int len = bio_iovec_idx(bio,i)->bv_len;
885 int clen;
886 int b_offset = 0;
887
888 if (page_offset < 0) {
889 b_offset = -page_offset;
890 page_offset += b_offset;
891 len -= b_offset;
892 }
893
894 if (len > 0 && page_offset + len > STRIPE_SIZE)
895 clen = STRIPE_SIZE - page_offset;
896 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (clen > 0) {
899 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
900 if (frombio)
901 memcpy(pa+page_offset, ba+b_offset, clen);
902 else
903 memcpy(ba+b_offset, pa+page_offset, clen);
904 __bio_kunmap_atomic(ba, KM_USER0);
905 }
906 if (clen < len) /* hit end of page */
907 break;
908 page_offset += len;
909 }
910}
911
912#define check_xor() do { \
913 if (count == MAX_XOR_BLOCKS) { \
914 xor_block(count, STRIPE_SIZE, ptr); \
915 count = 1; \
916 } \
917 } while(0)
918
919
920static void compute_block(struct stripe_head *sh, int dd_idx)
921{
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800922 int i, count, disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 void *ptr[MAX_XOR_BLOCKS], *p;
924
925 PRINTK("compute_block, stripe %llu, idx %d\n",
926 (unsigned long long)sh->sector, dd_idx);
927
928 ptr[0] = page_address(sh->dev[dd_idx].page);
929 memset(ptr[0], 0, STRIPE_SIZE);
930 count = 1;
931 for (i = disks ; i--; ) {
932 if (i == dd_idx)
933 continue;
934 p = page_address(sh->dev[i].page);
935 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
936 ptr[count++] = p;
937 else
NeilBrown14f8d262006-01-06 00:20:14 -0800938 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 " not present\n", dd_idx,
940 (unsigned long long)sh->sector, i);
941
942 check_xor();
943 }
944 if (count != 1)
945 xor_block(count, STRIPE_SIZE, ptr);
946 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
947}
948
NeilBrown16a53ec2006-06-26 00:27:38 -0700949static void compute_parity5(struct stripe_head *sh, int method)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800952 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 void *ptr[MAX_XOR_BLOCKS];
954 struct bio *chosen;
955
NeilBrown16a53ec2006-06-26 00:27:38 -0700956 PRINTK("compute_parity5, stripe %llu, method %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 (unsigned long long)sh->sector, method);
958
959 count = 1;
960 ptr[0] = page_address(sh->dev[pd_idx].page);
961 switch(method) {
962 case READ_MODIFY_WRITE:
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200963 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 for (i=disks ; i-- ;) {
965 if (i==pd_idx)
966 continue;
967 if (sh->dev[i].towrite &&
968 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
969 ptr[count++] = page_address(sh->dev[i].page);
970 chosen = sh->dev[i].towrite;
971 sh->dev[i].towrite = NULL;
972
973 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
974 wake_up(&conf->wait_for_overlap);
975
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200976 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 sh->dev[i].written = chosen;
978 check_xor();
979 }
980 }
981 break;
982 case RECONSTRUCT_WRITE:
983 memset(ptr[0], 0, STRIPE_SIZE);
984 for (i= disks; i-- ;)
985 if (i!=pd_idx && sh->dev[i].towrite) {
986 chosen = sh->dev[i].towrite;
987 sh->dev[i].towrite = NULL;
988
989 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
990 wake_up(&conf->wait_for_overlap);
991
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200992 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sh->dev[i].written = chosen;
994 }
995 break;
996 case CHECK_PARITY:
997 break;
998 }
999 if (count>1) {
1000 xor_block(count, STRIPE_SIZE, ptr);
1001 count = 1;
1002 }
1003
1004 for (i = disks; i--;)
1005 if (sh->dev[i].written) {
1006 sector_t sector = sh->dev[i].sector;
1007 struct bio *wbi = sh->dev[i].written;
1008 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1009 copy_data(1, wbi, sh->dev[i].page, sector);
1010 wbi = r5_next_bio(wbi, sector);
1011 }
1012
1013 set_bit(R5_LOCKED, &sh->dev[i].flags);
1014 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1015 }
1016
1017 switch(method) {
1018 case RECONSTRUCT_WRITE:
1019 case CHECK_PARITY:
1020 for (i=disks; i--;)
1021 if (i != pd_idx) {
1022 ptr[count++] = page_address(sh->dev[i].page);
1023 check_xor();
1024 }
1025 break;
1026 case READ_MODIFY_WRITE:
1027 for (i = disks; i--;)
1028 if (sh->dev[i].written) {
1029 ptr[count++] = page_address(sh->dev[i].page);
1030 check_xor();
1031 }
1032 }
1033 if (count != 1)
1034 xor_block(count, STRIPE_SIZE, ptr);
1035
1036 if (method != CHECK_PARITY) {
1037 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1038 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1039 } else
1040 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1041}
1042
NeilBrown16a53ec2006-06-26 00:27:38 -07001043static void compute_parity6(struct stripe_head *sh, int method)
1044{
1045 raid6_conf_t *conf = sh->raid_conf;
1046 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
1047 struct bio *chosen;
1048 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1049 void *ptrs[disks];
1050
1051 qd_idx = raid6_next_disk(pd_idx, disks);
1052 d0_idx = raid6_next_disk(qd_idx, disks);
1053
1054 PRINTK("compute_parity, stripe %llu, method %d\n",
1055 (unsigned long long)sh->sector, method);
1056
1057 switch(method) {
1058 case READ_MODIFY_WRITE:
1059 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1060 case RECONSTRUCT_WRITE:
1061 for (i= disks; i-- ;)
1062 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1063 chosen = sh->dev[i].towrite;
1064 sh->dev[i].towrite = NULL;
1065
1066 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1067 wake_up(&conf->wait_for_overlap);
1068
1069 if (sh->dev[i].written) BUG();
1070 sh->dev[i].written = chosen;
1071 }
1072 break;
1073 case CHECK_PARITY:
1074 BUG(); /* Not implemented yet */
1075 }
1076
1077 for (i = disks; i--;)
1078 if (sh->dev[i].written) {
1079 sector_t sector = sh->dev[i].sector;
1080 struct bio *wbi = sh->dev[i].written;
1081 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1082 copy_data(1, wbi, sh->dev[i].page, sector);
1083 wbi = r5_next_bio(wbi, sector);
1084 }
1085
1086 set_bit(R5_LOCKED, &sh->dev[i].flags);
1087 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1088 }
1089
1090// switch(method) {
1091// case RECONSTRUCT_WRITE:
1092// case CHECK_PARITY:
1093// case UPDATE_PARITY:
1094 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1095 /* FIX: Is this ordering of drives even remotely optimal? */
1096 count = 0;
1097 i = d0_idx;
1098 do {
1099 ptrs[count++] = page_address(sh->dev[i].page);
1100 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1101 printk("block %d/%d not uptodate on parity calc\n", i,count);
1102 i = raid6_next_disk(i, disks);
1103 } while ( i != d0_idx );
1104// break;
1105// }
1106
1107 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1108
1109 switch(method) {
1110 case RECONSTRUCT_WRITE:
1111 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1112 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1113 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1114 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1115 break;
1116 case UPDATE_PARITY:
1117 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1118 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1119 break;
1120 }
1121}
1122
1123
1124/* Compute one missing block */
1125static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1126{
1127 raid6_conf_t *conf = sh->raid_conf;
1128 int i, count, disks = conf->raid_disks;
1129 void *ptr[MAX_XOR_BLOCKS], *p;
1130 int pd_idx = sh->pd_idx;
1131 int qd_idx = raid6_next_disk(pd_idx, disks);
1132
1133 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1134 (unsigned long long)sh->sector, dd_idx);
1135
1136 if ( dd_idx == qd_idx ) {
1137 /* We're actually computing the Q drive */
1138 compute_parity6(sh, UPDATE_PARITY);
1139 } else {
1140 ptr[0] = page_address(sh->dev[dd_idx].page);
1141 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1142 count = 1;
1143 for (i = disks ; i--; ) {
1144 if (i == dd_idx || i == qd_idx)
1145 continue;
1146 p = page_address(sh->dev[i].page);
1147 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1148 ptr[count++] = p;
1149 else
1150 printk("compute_block() %d, stripe %llu, %d"
1151 " not present\n", dd_idx,
1152 (unsigned long long)sh->sector, i);
1153
1154 check_xor();
1155 }
1156 if (count != 1)
1157 xor_block(count, STRIPE_SIZE, ptr);
1158 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1159 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1160 }
1161}
1162
1163/* Compute two missing blocks */
1164static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1165{
1166 raid6_conf_t *conf = sh->raid_conf;
1167 int i, count, disks = conf->raid_disks;
1168 int pd_idx = sh->pd_idx;
1169 int qd_idx = raid6_next_disk(pd_idx, disks);
1170 int d0_idx = raid6_next_disk(qd_idx, disks);
1171 int faila, failb;
1172
1173 /* faila and failb are disk numbers relative to d0_idx */
1174 /* pd_idx become disks-2 and qd_idx become disks-1 */
1175 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1176 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1177
1178 BUG_ON(faila == failb);
1179 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1180
1181 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1182 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1183
1184 if ( failb == disks-1 ) {
1185 /* Q disk is one of the missing disks */
1186 if ( faila == disks-2 ) {
1187 /* Missing P+Q, just recompute */
1188 compute_parity6(sh, UPDATE_PARITY);
1189 return;
1190 } else {
1191 /* We're missing D+Q; recompute D from P */
1192 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1193 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1194 return;
1195 }
1196 }
1197
1198 /* We're missing D+P or D+D; build pointer table */
1199 {
1200 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1201 void *ptrs[disks];
1202
1203 count = 0;
1204 i = d0_idx;
1205 do {
1206 ptrs[count++] = page_address(sh->dev[i].page);
1207 i = raid6_next_disk(i, disks);
1208 if (i != dd_idx1 && i != dd_idx2 &&
1209 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1210 printk("compute_2 with missing block %d/%d\n", count, i);
1211 } while ( i != d0_idx );
1212
1213 if ( failb == disks-2 ) {
1214 /* We're missing D+P. */
1215 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1216 } else {
1217 /* We're missing D+D. */
1218 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1219 }
1220
1221 /* Both the above update both missing blocks */
1222 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1223 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1224 }
1225}
1226
1227
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
1230 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001231 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * The bi_next chain must be in order.
1233 */
1234static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1235{
1236 struct bio **bip;
1237 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001238 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1241 (unsigned long long)bi->bi_sector,
1242 (unsigned long long)sh->sector);
1243
1244
1245 spin_lock(&sh->lock);
1246 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001247 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001249 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1250 firstwrite = 1;
1251 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 bip = &sh->dev[dd_idx].toread;
1253 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1254 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1255 goto overlap;
1256 bip = & (*bip)->bi_next;
1257 }
1258 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1259 goto overlap;
1260
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001261 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (*bip)
1263 bi->bi_next = *bip;
1264 *bip = bi;
1265 bi->bi_phys_segments ++;
1266 spin_unlock_irq(&conf->device_lock);
1267 spin_unlock(&sh->lock);
1268
1269 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1270 (unsigned long long)bi->bi_sector,
1271 (unsigned long long)sh->sector, dd_idx);
1272
NeilBrown72626682005-09-09 16:23:54 -07001273 if (conf->mddev->bitmap && firstwrite) {
1274 sh->bm_seq = conf->seq_write;
1275 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1276 STRIPE_SECTORS, 0);
1277 set_bit(STRIPE_BIT_DELAY, &sh->state);
1278 }
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (forwrite) {
1281 /* check if page is covered */
1282 sector_t sector = sh->dev[dd_idx].sector;
1283 for (bi=sh->dev[dd_idx].towrite;
1284 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1285 bi && bi->bi_sector <= sector;
1286 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1287 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1288 sector = bi->bi_sector + (bi->bi_size>>9);
1289 }
1290 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1291 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1292 }
1293 return 1;
1294
1295 overlap:
1296 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1297 spin_unlock_irq(&conf->device_lock);
1298 spin_unlock(&sh->lock);
1299 return 0;
1300}
1301
NeilBrown29269552006-03-27 01:18:10 -08001302static void end_reshape(raid5_conf_t *conf);
1303
NeilBrown16a53ec2006-06-26 00:27:38 -07001304static int page_is_zero(struct page *p)
1305{
1306 char *a = page_address(p);
1307 return ((*(u32*)a) == 0 &&
1308 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1309}
1310
NeilBrownccfcc3c2006-03-27 01:18:09 -08001311static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1312{
1313 int sectors_per_chunk = conf->chunk_size >> 9;
1314 sector_t x = stripe;
1315 int pd_idx, dd_idx;
1316 int chunk_offset = sector_div(x, sectors_per_chunk);
1317 stripe = x;
1318 raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
1319 + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
1320 return pd_idx;
1321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324/*
1325 * handle_stripe - do things to a stripe.
1326 *
1327 * We lock the stripe and then examine the state of various bits
1328 * to see what needs to be done.
1329 * Possible results:
1330 * return some read request which now have data
1331 * return some write requests which are safely on disc
1332 * schedule a read on some buffers
1333 * schedule a write of some buffers
1334 * return confirmation of parity correctness
1335 *
1336 * Parity calculations are done inside the stripe lock
1337 * buffers are taken off read_list or write_list, and bh_cache buffers
1338 * get BH_Lock set before the stripe lock is released.
1339 *
1340 */
1341
NeilBrown16a53ec2006-06-26 00:27:38 -07001342static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001345 int disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct bio *return_bi= NULL;
1347 struct bio *bi;
1348 int i;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001349 int syncing, expanding, expanded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1351 int non_overwrite = 0;
1352 int failed_num=0;
1353 struct r5dev *dev;
1354
1355 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1356 (unsigned long long)sh->sector, atomic_read(&sh->count),
1357 sh->pd_idx);
1358
1359 spin_lock(&sh->lock);
1360 clear_bit(STRIPE_HANDLE, &sh->state);
1361 clear_bit(STRIPE_DELAYED, &sh->state);
1362
1363 syncing = test_bit(STRIPE_SYNCING, &sh->state);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001364 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1365 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* Now to look around and see what can be done */
1367
NeilBrown9910f162006-01-06 00:20:24 -08001368 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 for (i=disks; i--; ) {
1370 mdk_rdev_t *rdev;
1371 dev = &sh->dev[i];
1372 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1375 i, dev->flags, dev->toread, dev->towrite, dev->written);
1376 /* maybe we can reply to a read */
1377 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1378 struct bio *rbi, *rbi2;
1379 PRINTK("Return read for disc %d\n", i);
1380 spin_lock_irq(&conf->device_lock);
1381 rbi = dev->toread;
1382 dev->toread = NULL;
1383 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1384 wake_up(&conf->wait_for_overlap);
1385 spin_unlock_irq(&conf->device_lock);
1386 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1387 copy_data(0, rbi, dev->page, dev->sector);
1388 rbi2 = r5_next_bio(rbi, dev->sector);
1389 spin_lock_irq(&conf->device_lock);
1390 if (--rbi->bi_phys_segments == 0) {
1391 rbi->bi_next = return_bi;
1392 return_bi = rbi;
1393 }
1394 spin_unlock_irq(&conf->device_lock);
1395 rbi = rbi2;
1396 }
1397 }
1398
1399 /* now count some things */
1400 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1401 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1402
1403
1404 if (dev->toread) to_read++;
1405 if (dev->towrite) {
1406 to_write++;
1407 if (!test_bit(R5_OVERWRITE, &dev->flags))
1408 non_overwrite++;
1409 }
1410 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001411 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001412 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001413 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08001414 clear_bit(R5_ReadError, &dev->flags);
1415 clear_bit(R5_ReWrite, &dev->flags);
1416 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001417 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001418 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 failed++;
1420 failed_num = i;
1421 } else
1422 set_bit(R5_Insync, &dev->flags);
1423 }
NeilBrown9910f162006-01-06 00:20:24 -08001424 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 PRINTK("locked=%d uptodate=%d to_read=%d"
1426 " to_write=%d failed=%d failed_num=%d\n",
1427 locked, uptodate, to_read, to_write, failed, failed_num);
1428 /* check if the array has lost two devices and, if so, some requests might
1429 * need to be failed
1430 */
1431 if (failed > 1 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 for (i=disks; i--; ) {
NeilBrown72626682005-09-09 16:23:54 -07001433 int bitmap_end = 0;
NeilBrown4e5314b2005-11-08 21:39:22 -08001434
1435 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001436 mdk_rdev_t *rdev;
1437 rcu_read_lock();
1438 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001439 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001440 /* multiple read failures in one stripe */
1441 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001442 rcu_read_unlock();
NeilBrown4e5314b2005-11-08 21:39:22 -08001443 }
1444
NeilBrown72626682005-09-09 16:23:54 -07001445 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* fail all writes first */
1447 bi = sh->dev[i].towrite;
1448 sh->dev[i].towrite = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001449 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1452 wake_up(&conf->wait_for_overlap);
1453
1454 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1455 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1456 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1457 if (--bi->bi_phys_segments == 0) {
1458 md_write_end(conf->mddev);
1459 bi->bi_next = return_bi;
1460 return_bi = bi;
1461 }
1462 bi = nextbi;
1463 }
1464 /* and fail all 'written' */
1465 bi = sh->dev[i].written;
1466 sh->dev[i].written = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001467 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1469 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1470 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1471 if (--bi->bi_phys_segments == 0) {
1472 md_write_end(conf->mddev);
1473 bi->bi_next = return_bi;
1474 return_bi = bi;
1475 }
1476 bi = bi2;
1477 }
1478
1479 /* fail any reads if this device is non-operational */
NeilBrown4e5314b2005-11-08 21:39:22 -08001480 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1481 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 bi = sh->dev[i].toread;
1483 sh->dev[i].toread = NULL;
1484 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1485 wake_up(&conf->wait_for_overlap);
1486 if (bi) to_read--;
1487 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1488 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1489 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1490 if (--bi->bi_phys_segments == 0) {
1491 bi->bi_next = return_bi;
1492 return_bi = bi;
1493 }
1494 bi = nextbi;
1495 }
1496 }
NeilBrown72626682005-09-09 16:23:54 -07001497 spin_unlock_irq(&conf->device_lock);
1498 if (bitmap_end)
1499 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1500 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 if (failed > 1 && syncing) {
1504 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1505 clear_bit(STRIPE_SYNCING, &sh->state);
1506 syncing = 0;
1507 }
1508
1509 /* might be able to return some write requests if the parity block
1510 * is safe, or on a failed drive
1511 */
1512 dev = &sh->dev[sh->pd_idx];
1513 if ( written &&
1514 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1515 test_bit(R5_UPTODATE, &dev->flags))
1516 || (failed == 1 && failed_num == sh->pd_idx))
1517 ) {
1518 /* any written block on an uptodate or failed drive can be returned.
1519 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1520 * never LOCKED, so we don't need to test 'failed' directly.
1521 */
1522 for (i=disks; i--; )
1523 if (sh->dev[i].written) {
1524 dev = &sh->dev[i];
1525 if (!test_bit(R5_LOCKED, &dev->flags) &&
1526 test_bit(R5_UPTODATE, &dev->flags) ) {
1527 /* We can return any write requests */
1528 struct bio *wbi, *wbi2;
NeilBrown72626682005-09-09 16:23:54 -07001529 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 PRINTK("Return write for disc %d\n", i);
1531 spin_lock_irq(&conf->device_lock);
1532 wbi = dev->written;
1533 dev->written = NULL;
1534 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1535 wbi2 = r5_next_bio(wbi, dev->sector);
1536 if (--wbi->bi_phys_segments == 0) {
1537 md_write_end(conf->mddev);
1538 wbi->bi_next = return_bi;
1539 return_bi = wbi;
1540 }
1541 wbi = wbi2;
1542 }
NeilBrown72626682005-09-09 16:23:54 -07001543 if (dev->towrite == NULL)
1544 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001546 if (bitmap_end)
1547 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1548 STRIPE_SECTORS,
1549 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551 }
1552 }
1553
1554 /* Now we might consider reading some blocks, either to check/generate
1555 * parity, or to satisfy requests
1556 * or to load a block that is being partially written.
1557 */
NeilBrownccfcc3c2006-03-27 01:18:09 -08001558 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 for (i=disks; i--;) {
1560 dev = &sh->dev[i];
1561 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1562 (dev->toread ||
1563 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1564 syncing ||
NeilBrownccfcc3c2006-03-27 01:18:09 -08001565 expanding ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 (failed && (sh->dev[failed_num].toread ||
1567 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1568 )
1569 ) {
1570 /* we would like to get this block, possibly
1571 * by computing it, but we might not be able to
1572 */
1573 if (uptodate == disks-1) {
1574 PRINTK("Computing block %d\n", i);
1575 compute_block(sh, i);
1576 uptodate++;
1577 } else if (test_bit(R5_Insync, &dev->flags)) {
1578 set_bit(R5_LOCKED, &dev->flags);
1579 set_bit(R5_Wantread, &dev->flags);
1580#if 0
1581 /* if I am just reading this block and we don't have
1582 a failed drive, or any pending writes then sidestep the cache */
1583 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1584 ! syncing && !failed && !to_write) {
1585 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1586 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1587 }
1588#endif
1589 locked++;
1590 PRINTK("Reading block %d (sync=%d)\n",
1591 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 }
1594 }
1595 set_bit(STRIPE_HANDLE, &sh->state);
1596 }
1597
1598 /* now to consider writing and what else, if anything should be read */
1599 if (to_write) {
1600 int rmw=0, rcw=0;
1601 for (i=disks ; i--;) {
1602 /* would I have to read this buffer for read_modify_write */
1603 dev = &sh->dev[i];
1604 if ((dev->towrite || i == sh->pd_idx) &&
1605 (!test_bit(R5_LOCKED, &dev->flags)
1606#if 0
1607|| sh->bh_page[i]!=bh->b_page
1608#endif
1609 ) &&
1610 !test_bit(R5_UPTODATE, &dev->flags)) {
1611 if (test_bit(R5_Insync, &dev->flags)
1612/* && !(!mddev->insync && i == sh->pd_idx) */
1613 )
1614 rmw++;
1615 else rmw += 2*disks; /* cannot read it */
1616 }
1617 /* Would I have to read this buffer for reconstruct_write */
1618 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1619 (!test_bit(R5_LOCKED, &dev->flags)
1620#if 0
1621|| sh->bh_page[i] != bh->b_page
1622#endif
1623 ) &&
1624 !test_bit(R5_UPTODATE, &dev->flags)) {
1625 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1626 else rcw += 2*disks;
1627 }
1628 }
1629 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1630 (unsigned long long)sh->sector, rmw, rcw);
1631 set_bit(STRIPE_HANDLE, &sh->state);
1632 if (rmw < rcw && rmw > 0)
1633 /* prefer read-modify-write, but need to get some data */
1634 for (i=disks; i--;) {
1635 dev = &sh->dev[i];
1636 if ((dev->towrite || i == sh->pd_idx) &&
1637 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1638 test_bit(R5_Insync, &dev->flags)) {
1639 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1640 {
1641 PRINTK("Read_old block %d for r-m-w\n", i);
1642 set_bit(R5_LOCKED, &dev->flags);
1643 set_bit(R5_Wantread, &dev->flags);
1644 locked++;
1645 } else {
1646 set_bit(STRIPE_DELAYED, &sh->state);
1647 set_bit(STRIPE_HANDLE, &sh->state);
1648 }
1649 }
1650 }
1651 if (rcw <= rmw && rcw > 0)
1652 /* want reconstruct write, but need to get some data */
1653 for (i=disks; i--;) {
1654 dev = &sh->dev[i];
1655 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1656 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1657 test_bit(R5_Insync, &dev->flags)) {
1658 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1659 {
1660 PRINTK("Read_old block %d for Reconstruct\n", i);
1661 set_bit(R5_LOCKED, &dev->flags);
1662 set_bit(R5_Wantread, &dev->flags);
1663 locked++;
1664 } else {
1665 set_bit(STRIPE_DELAYED, &sh->state);
1666 set_bit(STRIPE_HANDLE, &sh->state);
1667 }
1668 }
1669 }
1670 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown72626682005-09-09 16:23:54 -07001671 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1672 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 PRINTK("Computing parity...\n");
NeilBrown16a53ec2006-06-26 00:27:38 -07001674 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /* now every locked buffer is ready to be written */
1676 for (i=disks; i--;)
1677 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1678 PRINTK("Writing block %d\n", i);
1679 locked++;
1680 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1681 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1682 || (i==sh->pd_idx && failed == 0))
1683 set_bit(STRIPE_INSYNC, &sh->state);
1684 }
1685 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1686 atomic_dec(&conf->preread_active_stripes);
1687 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1688 md_wakeup_thread(conf->mddev->thread);
1689 }
1690 }
1691 }
1692
1693 /* maybe we need to check and possibly fix the parity for this stripe
1694 * Any reads will already have been scheduled, so we just see if enough data
1695 * is available
1696 */
1697 if (syncing && locked == 0 &&
NeilBrown14f8d262006-01-06 00:20:14 -08001698 !test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 set_bit(STRIPE_HANDLE, &sh->state);
1700 if (failed == 0) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001701 BUG_ON(uptodate != disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001702 compute_parity5(sh, CHECK_PARITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 uptodate--;
NeilBrown16a53ec2006-06-26 00:27:38 -07001704 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /* parity is correct (on disc, not in buffer any more) */
1706 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown9d888832005-11-08 21:39:26 -08001707 } else {
1708 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1709 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1710 /* don't try to repair!! */
1711 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown14f8d262006-01-06 00:20:14 -08001712 else {
1713 compute_block(sh, sh->pd_idx);
1714 uptodate++;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 }
1718 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001719 /* either failed parity check, or recovery is happening */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (failed==0)
1721 failed_num = sh->pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 dev = &sh->dev[failed_num];
NeilBrown14f8d262006-01-06 00:20:14 -08001723 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1724 BUG_ON(uptodate != disks);
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 set_bit(R5_LOCKED, &dev->flags);
1727 set_bit(R5_Wantwrite, &dev->flags);
NeilBrown72626682005-09-09 16:23:54 -07001728 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 locked++;
1730 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732 }
1733 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1734 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1735 clear_bit(STRIPE_SYNCING, &sh->state);
1736 }
NeilBrown4e5314b2005-11-08 21:39:22 -08001737
1738 /* If the failed drive is just a ReadError, then we might need to progress
1739 * the repair/check process
1740 */
NeilBrownba22dcb2005-11-08 21:39:31 -08001741 if (failed == 1 && ! conf->mddev->ro &&
1742 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001743 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1744 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1745 ) {
1746 dev = &sh->dev[failed_num];
1747 if (!test_bit(R5_ReWrite, &dev->flags)) {
1748 set_bit(R5_Wantwrite, &dev->flags);
1749 set_bit(R5_ReWrite, &dev->flags);
1750 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001751 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001752 } else {
1753 /* let's read it back */
1754 set_bit(R5_Wantread, &dev->flags);
1755 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001756 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001757 }
1758 }
1759
NeilBrownccfcc3c2006-03-27 01:18:09 -08001760 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1761 /* Need to write out all blocks after computing parity */
1762 sh->disks = conf->raid_disks;
1763 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001764 compute_parity5(sh, RECONSTRUCT_WRITE);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001765 for (i= conf->raid_disks; i--;) {
1766 set_bit(R5_LOCKED, &sh->dev[i].flags);
1767 locked++;
1768 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1769 }
1770 clear_bit(STRIPE_EXPANDING, &sh->state);
1771 } else if (expanded) {
1772 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08001773 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001774 wake_up(&conf->wait_for_overlap);
1775 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1776 }
1777
1778 if (expanding && locked == 0) {
1779 /* We have read all the blocks in this stripe and now we need to
1780 * copy some of them into a target stripe for expand.
1781 */
1782 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1783 for (i=0; i< sh->disks; i++)
1784 if (i != sh->pd_idx) {
1785 int dd_idx, pd_idx, j;
1786 struct stripe_head *sh2;
1787
1788 sector_t bn = compute_blocknr(sh, i);
1789 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1790 conf->raid_disks-1,
1791 &dd_idx, &pd_idx, conf);
1792 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1793 if (sh2 == NULL)
1794 /* so far only the early blocks of this stripe
1795 * have been requested. When later blocks
1796 * get requested, we will try again
1797 */
1798 continue;
1799 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1800 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1801 /* must have already done this block */
1802 release_stripe(sh2);
1803 continue;
1804 }
1805 memcpy(page_address(sh2->dev[dd_idx].page),
1806 page_address(sh->dev[i].page),
1807 STRIPE_SIZE);
1808 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1809 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1810 for (j=0; j<conf->raid_disks; j++)
1811 if (j != sh2->pd_idx &&
1812 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1813 break;
1814 if (j == conf->raid_disks) {
1815 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1816 set_bit(STRIPE_HANDLE, &sh2->state);
1817 }
1818 release_stripe(sh2);
1819 }
1820 }
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 spin_unlock(&sh->lock);
1823
1824 while ((bi=return_bi)) {
1825 int bytes = bi->bi_size;
1826
1827 return_bi = bi->bi_next;
1828 bi->bi_next = NULL;
1829 bi->bi_size = 0;
1830 bi->bi_end_io(bi, bytes, 0);
1831 }
1832 for (i=disks; i-- ;) {
1833 int rw;
1834 struct bio *bi;
1835 mdk_rdev_t *rdev;
1836 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1837 rw = 1;
1838 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1839 rw = 0;
1840 else
1841 continue;
1842
1843 bi = &sh->dev[i].req;
1844
1845 bi->bi_rw = rw;
1846 if (rw)
1847 bi->bi_end_io = raid5_end_write_request;
1848 else
1849 bi->bi_end_io = raid5_end_read_request;
1850
1851 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001852 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001853 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 rdev = NULL;
1855 if (rdev)
1856 atomic_inc(&rdev->nr_pending);
1857 rcu_read_unlock();
1858
1859 if (rdev) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08001860 if (syncing || expanding || expanded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1862
1863 bi->bi_bdev = rdev->bdev;
1864 PRINTK("for %llu schedule op %ld on disc %d\n",
1865 (unsigned long long)sh->sector, bi->bi_rw, i);
1866 atomic_inc(&sh->count);
1867 bi->bi_sector = sh->sector + rdev->data_offset;
1868 bi->bi_flags = 1 << BIO_UPTODATE;
1869 bi->bi_vcnt = 1;
1870 bi->bi_max_vecs = 1;
1871 bi->bi_idx = 0;
1872 bi->bi_io_vec = &sh->dev[i].vec;
1873 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1874 bi->bi_io_vec[0].bv_offset = 0;
1875 bi->bi_size = STRIPE_SIZE;
1876 bi->bi_next = NULL;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001877 if (rw == WRITE &&
1878 test_bit(R5_ReWrite, &sh->dev[i].flags))
1879 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 generic_make_request(bi);
1881 } else {
NeilBrown72626682005-09-09 16:23:54 -07001882 if (rw == 1)
1883 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 PRINTK("skip op %ld on disc %d for sector %llu\n",
1885 bi->bi_rw, i, (unsigned long long)sh->sector);
1886 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1887 set_bit(STRIPE_HANDLE, &sh->state);
1888 }
1889 }
1890}
1891
NeilBrown16a53ec2006-06-26 00:27:38 -07001892static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1893{
1894 raid6_conf_t *conf = sh->raid_conf;
1895 int disks = conf->raid_disks;
1896 struct bio *return_bi= NULL;
1897 struct bio *bi;
1898 int i;
1899 int syncing;
1900 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1901 int non_overwrite = 0;
1902 int failed_num[2] = {0, 0};
1903 struct r5dev *dev, *pdev, *qdev;
1904 int pd_idx = sh->pd_idx;
1905 int qd_idx = raid6_next_disk(pd_idx, disks);
1906 int p_failed, q_failed;
1907
1908 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1909 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1910 pd_idx, qd_idx);
1911
1912 spin_lock(&sh->lock);
1913 clear_bit(STRIPE_HANDLE, &sh->state);
1914 clear_bit(STRIPE_DELAYED, &sh->state);
1915
1916 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1917 /* Now to look around and see what can be done */
1918
1919 rcu_read_lock();
1920 for (i=disks; i--; ) {
1921 mdk_rdev_t *rdev;
1922 dev = &sh->dev[i];
1923 clear_bit(R5_Insync, &dev->flags);
1924
1925 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1926 i, dev->flags, dev->toread, dev->towrite, dev->written);
1927 /* maybe we can reply to a read */
1928 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1929 struct bio *rbi, *rbi2;
1930 PRINTK("Return read for disc %d\n", i);
1931 spin_lock_irq(&conf->device_lock);
1932 rbi = dev->toread;
1933 dev->toread = NULL;
1934 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1935 wake_up(&conf->wait_for_overlap);
1936 spin_unlock_irq(&conf->device_lock);
1937 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1938 copy_data(0, rbi, dev->page, dev->sector);
1939 rbi2 = r5_next_bio(rbi, dev->sector);
1940 spin_lock_irq(&conf->device_lock);
1941 if (--rbi->bi_phys_segments == 0) {
1942 rbi->bi_next = return_bi;
1943 return_bi = rbi;
1944 }
1945 spin_unlock_irq(&conf->device_lock);
1946 rbi = rbi2;
1947 }
1948 }
1949
1950 /* now count some things */
1951 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1952 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1953
1954
1955 if (dev->toread) to_read++;
1956 if (dev->towrite) {
1957 to_write++;
1958 if (!test_bit(R5_OVERWRITE, &dev->flags))
1959 non_overwrite++;
1960 }
1961 if (dev->written) written++;
1962 rdev = rcu_dereference(conf->disks[i].rdev);
1963 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1964 /* The ReadError flag will just be confusing now */
1965 clear_bit(R5_ReadError, &dev->flags);
1966 clear_bit(R5_ReWrite, &dev->flags);
1967 }
1968 if (!rdev || !test_bit(In_sync, &rdev->flags)
1969 || test_bit(R5_ReadError, &dev->flags)) {
1970 if ( failed < 2 )
1971 failed_num[failed] = i;
1972 failed++;
1973 } else
1974 set_bit(R5_Insync, &dev->flags);
1975 }
1976 rcu_read_unlock();
1977 PRINTK("locked=%d uptodate=%d to_read=%d"
1978 " to_write=%d failed=%d failed_num=%d,%d\n",
1979 locked, uptodate, to_read, to_write, failed,
1980 failed_num[0], failed_num[1]);
1981 /* check if the array has lost >2 devices and, if so, some requests might
1982 * need to be failed
1983 */
1984 if (failed > 2 && to_read+to_write+written) {
1985 for (i=disks; i--; ) {
1986 int bitmap_end = 0;
1987
1988 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1989 mdk_rdev_t *rdev;
1990 rcu_read_lock();
1991 rdev = rcu_dereference(conf->disks[i].rdev);
1992 if (rdev && test_bit(In_sync, &rdev->flags))
1993 /* multiple read failures in one stripe */
1994 md_error(conf->mddev, rdev);
1995 rcu_read_unlock();
1996 }
1997
1998 spin_lock_irq(&conf->device_lock);
1999 /* fail all writes first */
2000 bi = sh->dev[i].towrite;
2001 sh->dev[i].towrite = NULL;
2002 if (bi) { to_write--; bitmap_end = 1; }
2003
2004 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2005 wake_up(&conf->wait_for_overlap);
2006
2007 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2008 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2009 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2010 if (--bi->bi_phys_segments == 0) {
2011 md_write_end(conf->mddev);
2012 bi->bi_next = return_bi;
2013 return_bi = bi;
2014 }
2015 bi = nextbi;
2016 }
2017 /* and fail all 'written' */
2018 bi = sh->dev[i].written;
2019 sh->dev[i].written = NULL;
2020 if (bi) bitmap_end = 1;
2021 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2022 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2023 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2024 if (--bi->bi_phys_segments == 0) {
2025 md_write_end(conf->mddev);
2026 bi->bi_next = return_bi;
2027 return_bi = bi;
2028 }
2029 bi = bi2;
2030 }
2031
2032 /* fail any reads if this device is non-operational */
2033 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2034 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2035 bi = sh->dev[i].toread;
2036 sh->dev[i].toread = NULL;
2037 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2038 wake_up(&conf->wait_for_overlap);
2039 if (bi) to_read--;
2040 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2041 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2042 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2043 if (--bi->bi_phys_segments == 0) {
2044 bi->bi_next = return_bi;
2045 return_bi = bi;
2046 }
2047 bi = nextbi;
2048 }
2049 }
2050 spin_unlock_irq(&conf->device_lock);
2051 if (bitmap_end)
2052 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2053 STRIPE_SECTORS, 0, 0);
2054 }
2055 }
2056 if (failed > 2 && syncing) {
2057 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2058 clear_bit(STRIPE_SYNCING, &sh->state);
2059 syncing = 0;
2060 }
2061
2062 /*
2063 * might be able to return some write requests if the parity blocks
2064 * are safe, or on a failed drive
2065 */
2066 pdev = &sh->dev[pd_idx];
2067 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2068 || (failed >= 2 && failed_num[1] == pd_idx);
2069 qdev = &sh->dev[qd_idx];
2070 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2071 || (failed >= 2 && failed_num[1] == qd_idx);
2072
2073 if ( written &&
2074 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2075 && !test_bit(R5_LOCKED, &pdev->flags)
2076 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2077 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2078 && !test_bit(R5_LOCKED, &qdev->flags)
2079 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2080 /* any written block on an uptodate or failed drive can be
2081 * returned. Note that if we 'wrote' to a failed drive,
2082 * it will be UPTODATE, but never LOCKED, so we don't need
2083 * to test 'failed' directly.
2084 */
2085 for (i=disks; i--; )
2086 if (sh->dev[i].written) {
2087 dev = &sh->dev[i];
2088 if (!test_bit(R5_LOCKED, &dev->flags) &&
2089 test_bit(R5_UPTODATE, &dev->flags) ) {
2090 /* We can return any write requests */
2091 int bitmap_end = 0;
2092 struct bio *wbi, *wbi2;
2093 PRINTK("Return write for stripe %llu disc %d\n",
2094 (unsigned long long)sh->sector, i);
2095 spin_lock_irq(&conf->device_lock);
2096 wbi = dev->written;
2097 dev->written = NULL;
2098 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2099 wbi2 = r5_next_bio(wbi, dev->sector);
2100 if (--wbi->bi_phys_segments == 0) {
2101 md_write_end(conf->mddev);
2102 wbi->bi_next = return_bi;
2103 return_bi = wbi;
2104 }
2105 wbi = wbi2;
2106 }
2107 if (dev->towrite == NULL)
2108 bitmap_end = 1;
2109 spin_unlock_irq(&conf->device_lock);
2110 if (bitmap_end)
2111 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2112 STRIPE_SECTORS,
2113 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2114 }
2115 }
2116 }
2117
2118 /* Now we might consider reading some blocks, either to check/generate
2119 * parity, or to satisfy requests
2120 * or to load a block that is being partially written.
2121 */
2122 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
2123 for (i=disks; i--;) {
2124 dev = &sh->dev[i];
2125 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2126 (dev->toread ||
2127 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2128 syncing ||
2129 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2130 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2131 )
2132 ) {
2133 /* we would like to get this block, possibly
2134 * by computing it, but we might not be able to
2135 */
2136 if (uptodate == disks-1) {
2137 PRINTK("Computing stripe %llu block %d\n",
2138 (unsigned long long)sh->sector, i);
2139 compute_block_1(sh, i, 0);
2140 uptodate++;
2141 } else if ( uptodate == disks-2 && failed >= 2 ) {
2142 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2143 int other;
2144 for (other=disks; other--;) {
2145 if ( other == i )
2146 continue;
2147 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2148 break;
2149 }
2150 BUG_ON(other < 0);
2151 PRINTK("Computing stripe %llu blocks %d,%d\n",
2152 (unsigned long long)sh->sector, i, other);
2153 compute_block_2(sh, i, other);
2154 uptodate += 2;
2155 } else if (test_bit(R5_Insync, &dev->flags)) {
2156 set_bit(R5_LOCKED, &dev->flags);
2157 set_bit(R5_Wantread, &dev->flags);
2158#if 0
2159 /* if I am just reading this block and we don't have
2160 a failed drive, or any pending writes then sidestep the cache */
2161 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
2162 ! syncing && !failed && !to_write) {
2163 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
2164 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
2165 }
2166#endif
2167 locked++;
2168 PRINTK("Reading block %d (sync=%d)\n",
2169 i, syncing);
2170 }
2171 }
2172 }
2173 set_bit(STRIPE_HANDLE, &sh->state);
2174 }
2175
2176 /* now to consider writing and what else, if anything should be read */
2177 if (to_write) {
2178 int rcw=0, must_compute=0;
2179 for (i=disks ; i--;) {
2180 dev = &sh->dev[i];
2181 /* Would I have to read this buffer for reconstruct_write */
2182 if (!test_bit(R5_OVERWRITE, &dev->flags)
2183 && i != pd_idx && i != qd_idx
2184 && (!test_bit(R5_LOCKED, &dev->flags)
2185#if 0
2186 || sh->bh_page[i] != bh->b_page
2187#endif
2188 ) &&
2189 !test_bit(R5_UPTODATE, &dev->flags)) {
2190 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2191 else {
2192 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2193 must_compute++;
2194 }
2195 }
2196 }
2197 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2198 (unsigned long long)sh->sector, rcw, must_compute);
2199 set_bit(STRIPE_HANDLE, &sh->state);
2200
2201 if (rcw > 0)
2202 /* want reconstruct write, but need to get some data */
2203 for (i=disks; i--;) {
2204 dev = &sh->dev[i];
2205 if (!test_bit(R5_OVERWRITE, &dev->flags)
2206 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2207 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2208 test_bit(R5_Insync, &dev->flags)) {
2209 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2210 {
2211 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2212 (unsigned long long)sh->sector, i);
2213 set_bit(R5_LOCKED, &dev->flags);
2214 set_bit(R5_Wantread, &dev->flags);
2215 locked++;
2216 } else {
2217 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2218 (unsigned long long)sh->sector, i);
2219 set_bit(STRIPE_DELAYED, &sh->state);
2220 set_bit(STRIPE_HANDLE, &sh->state);
2221 }
2222 }
2223 }
2224 /* now if nothing is locked, and if we have enough data, we can start a write request */
2225 if (locked == 0 && rcw == 0 &&
2226 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2227 if ( must_compute > 0 ) {
2228 /* We have failed blocks and need to compute them */
2229 switch ( failed ) {
2230 case 0: BUG();
2231 case 1: compute_block_1(sh, failed_num[0], 0); break;
2232 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2233 default: BUG(); /* This request should have been failed? */
2234 }
2235 }
2236
2237 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2238 compute_parity6(sh, RECONSTRUCT_WRITE);
2239 /* now every locked buffer is ready to be written */
2240 for (i=disks; i--;)
2241 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2242 PRINTK("Writing stripe %llu block %d\n",
2243 (unsigned long long)sh->sector, i);
2244 locked++;
2245 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2246 }
2247 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2248 set_bit(STRIPE_INSYNC, &sh->state);
2249
2250 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2251 atomic_dec(&conf->preread_active_stripes);
2252 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2253 md_wakeup_thread(conf->mddev->thread);
2254 }
2255 }
2256 }
2257
2258 /* maybe we need to check and possibly fix the parity for this stripe
2259 * Any reads will already have been scheduled, so we just see if enough data
2260 * is available
2261 */
2262 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2263 int update_p = 0, update_q = 0;
2264 struct r5dev *dev;
2265
2266 set_bit(STRIPE_HANDLE, &sh->state);
2267
2268 BUG_ON(failed>2);
2269 BUG_ON(uptodate < disks);
2270 /* Want to check and possibly repair P and Q.
2271 * However there could be one 'failed' device, in which
2272 * case we can only check one of them, possibly using the
2273 * other to generate missing data
2274 */
2275
2276 /* If !tmp_page, we cannot do the calculations,
2277 * but as we have set STRIPE_HANDLE, we will soon be called
2278 * by stripe_handle with a tmp_page - just wait until then.
2279 */
2280 if (tmp_page) {
2281 if (failed == q_failed) {
2282 /* The only possible failed device holds 'Q', so it makes
2283 * sense to check P (If anything else were failed, we would
2284 * have used P to recreate it).
2285 */
2286 compute_block_1(sh, pd_idx, 1);
2287 if (!page_is_zero(sh->dev[pd_idx].page)) {
2288 compute_block_1(sh,pd_idx,0);
2289 update_p = 1;
2290 }
2291 }
2292 if (!q_failed && failed < 2) {
2293 /* q is not failed, and we didn't use it to generate
2294 * anything, so it makes sense to check it
2295 */
2296 memcpy(page_address(tmp_page),
2297 page_address(sh->dev[qd_idx].page),
2298 STRIPE_SIZE);
2299 compute_parity6(sh, UPDATE_PARITY);
2300 if (memcmp(page_address(tmp_page),
2301 page_address(sh->dev[qd_idx].page),
2302 STRIPE_SIZE)!= 0) {
2303 clear_bit(STRIPE_INSYNC, &sh->state);
2304 update_q = 1;
2305 }
2306 }
2307 if (update_p || update_q) {
2308 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2309 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2310 /* don't try to repair!! */
2311 update_p = update_q = 0;
2312 }
2313
2314 /* now write out any block on a failed drive,
2315 * or P or Q if they need it
2316 */
2317
2318 if (failed == 2) {
2319 dev = &sh->dev[failed_num[1]];
2320 locked++;
2321 set_bit(R5_LOCKED, &dev->flags);
2322 set_bit(R5_Wantwrite, &dev->flags);
2323 }
2324 if (failed >= 1) {
2325 dev = &sh->dev[failed_num[0]];
2326 locked++;
2327 set_bit(R5_LOCKED, &dev->flags);
2328 set_bit(R5_Wantwrite, &dev->flags);
2329 }
2330
2331 if (update_p) {
2332 dev = &sh->dev[pd_idx];
2333 locked ++;
2334 set_bit(R5_LOCKED, &dev->flags);
2335 set_bit(R5_Wantwrite, &dev->flags);
2336 }
2337 if (update_q) {
2338 dev = &sh->dev[qd_idx];
2339 locked++;
2340 set_bit(R5_LOCKED, &dev->flags);
2341 set_bit(R5_Wantwrite, &dev->flags);
2342 }
2343 clear_bit(STRIPE_DEGRADED, &sh->state);
2344
2345 set_bit(STRIPE_INSYNC, &sh->state);
2346 }
2347 }
2348
2349 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2350 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2351 clear_bit(STRIPE_SYNCING, &sh->state);
2352 }
2353
2354 /* If the failed drives are just a ReadError, then we might need
2355 * to progress the repair/check process
2356 */
2357 if (failed <= 2 && ! conf->mddev->ro)
2358 for (i=0; i<failed;i++) {
2359 dev = &sh->dev[failed_num[i]];
2360 if (test_bit(R5_ReadError, &dev->flags)
2361 && !test_bit(R5_LOCKED, &dev->flags)
2362 && test_bit(R5_UPTODATE, &dev->flags)
2363 ) {
2364 if (!test_bit(R5_ReWrite, &dev->flags)) {
2365 set_bit(R5_Wantwrite, &dev->flags);
2366 set_bit(R5_ReWrite, &dev->flags);
2367 set_bit(R5_LOCKED, &dev->flags);
2368 } else {
2369 /* let's read it back */
2370 set_bit(R5_Wantread, &dev->flags);
2371 set_bit(R5_LOCKED, &dev->flags);
2372 }
2373 }
2374 }
2375 spin_unlock(&sh->lock);
2376
2377 while ((bi=return_bi)) {
2378 int bytes = bi->bi_size;
2379
2380 return_bi = bi->bi_next;
2381 bi->bi_next = NULL;
2382 bi->bi_size = 0;
2383 bi->bi_end_io(bi, bytes, 0);
2384 }
2385 for (i=disks; i-- ;) {
2386 int rw;
2387 struct bio *bi;
2388 mdk_rdev_t *rdev;
2389 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
2390 rw = 1;
2391 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
2392 rw = 0;
2393 else
2394 continue;
2395
2396 bi = &sh->dev[i].req;
2397
2398 bi->bi_rw = rw;
2399 if (rw)
2400 bi->bi_end_io = raid5_end_write_request;
2401 else
2402 bi->bi_end_io = raid5_end_read_request;
2403
2404 rcu_read_lock();
2405 rdev = rcu_dereference(conf->disks[i].rdev);
2406 if (rdev && test_bit(Faulty, &rdev->flags))
2407 rdev = NULL;
2408 if (rdev)
2409 atomic_inc(&rdev->nr_pending);
2410 rcu_read_unlock();
2411
2412 if (rdev) {
2413 if (syncing)
2414 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2415
2416 bi->bi_bdev = rdev->bdev;
2417 PRINTK("for %llu schedule op %ld on disc %d\n",
2418 (unsigned long long)sh->sector, bi->bi_rw, i);
2419 atomic_inc(&sh->count);
2420 bi->bi_sector = sh->sector + rdev->data_offset;
2421 bi->bi_flags = 1 << BIO_UPTODATE;
2422 bi->bi_vcnt = 1;
2423 bi->bi_max_vecs = 1;
2424 bi->bi_idx = 0;
2425 bi->bi_io_vec = &sh->dev[i].vec;
2426 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2427 bi->bi_io_vec[0].bv_offset = 0;
2428 bi->bi_size = STRIPE_SIZE;
2429 bi->bi_next = NULL;
2430 if (rw == WRITE &&
2431 test_bit(R5_ReWrite, &sh->dev[i].flags))
2432 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2433 generic_make_request(bi);
2434 } else {
2435 if (rw == 1)
2436 set_bit(STRIPE_DEGRADED, &sh->state);
2437 PRINTK("skip op %ld on disc %d for sector %llu\n",
2438 bi->bi_rw, i, (unsigned long long)sh->sector);
2439 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2440 set_bit(STRIPE_HANDLE, &sh->state);
2441 }
2442 }
2443}
2444
2445static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2446{
2447 if (sh->raid_conf->level == 6)
2448 handle_stripe6(sh, tmp_page);
2449 else
2450 handle_stripe5(sh);
2451}
2452
2453
2454
Arjan van de Ven858119e2006-01-14 13:20:43 -08002455static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2458 while (!list_empty(&conf->delayed_list)) {
2459 struct list_head *l = conf->delayed_list.next;
2460 struct stripe_head *sh;
2461 sh = list_entry(l, struct stripe_head, lru);
2462 list_del_init(l);
2463 clear_bit(STRIPE_DELAYED, &sh->state);
2464 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2465 atomic_inc(&conf->preread_active_stripes);
2466 list_add_tail(&sh->lru, &conf->handle_list);
2467 }
2468 }
2469}
2470
Arjan van de Ven858119e2006-01-14 13:20:43 -08002471static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07002472{
2473 /* device_lock is held */
2474 struct list_head head;
2475 list_add(&head, &conf->bitmap_list);
2476 list_del_init(&conf->bitmap_list);
2477 while (!list_empty(&head)) {
2478 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2479 list_del_init(&sh->lru);
2480 atomic_inc(&sh->count);
2481 __release_stripe(conf, sh);
2482 }
2483}
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485static void unplug_slaves(mddev_t *mddev)
2486{
2487 raid5_conf_t *conf = mddev_to_conf(mddev);
2488 int i;
2489
2490 rcu_read_lock();
2491 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002492 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002493 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2495
2496 atomic_inc(&rdev->nr_pending);
2497 rcu_read_unlock();
2498
2499 if (r_queue->unplug_fn)
2500 r_queue->unplug_fn(r_queue);
2501
2502 rdev_dec_pending(rdev, mddev);
2503 rcu_read_lock();
2504 }
2505 }
2506 rcu_read_unlock();
2507}
2508
2509static void raid5_unplug_device(request_queue_t *q)
2510{
2511 mddev_t *mddev = q->queuedata;
2512 raid5_conf_t *conf = mddev_to_conf(mddev);
2513 unsigned long flags;
2514
2515 spin_lock_irqsave(&conf->device_lock, flags);
2516
NeilBrown72626682005-09-09 16:23:54 -07002517 if (blk_remove_plug(q)) {
2518 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07002520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 md_wakeup_thread(mddev->thread);
2522
2523 spin_unlock_irqrestore(&conf->device_lock, flags);
2524
2525 unplug_slaves(mddev);
2526}
2527
2528static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2529 sector_t *error_sector)
2530{
2531 mddev_t *mddev = q->queuedata;
2532 raid5_conf_t *conf = mddev_to_conf(mddev);
2533 int i, ret = 0;
2534
2535 rcu_read_lock();
2536 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002537 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002538 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 struct block_device *bdev = rdev->bdev;
2540 request_queue_t *r_queue = bdev_get_queue(bdev);
2541
2542 if (!r_queue->issue_flush_fn)
2543 ret = -EOPNOTSUPP;
2544 else {
2545 atomic_inc(&rdev->nr_pending);
2546 rcu_read_unlock();
2547 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2548 error_sector);
2549 rdev_dec_pending(rdev, mddev);
2550 rcu_read_lock();
2551 }
2552 }
2553 }
2554 rcu_read_unlock();
2555 return ret;
2556}
2557
2558static inline void raid5_plug_device(raid5_conf_t *conf)
2559{
2560 spin_lock_irq(&conf->device_lock);
2561 blk_plug_device(conf->mddev->queue);
2562 spin_unlock_irq(&conf->device_lock);
2563}
2564
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002565static int make_request(request_queue_t *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
2567 mddev_t *mddev = q->queuedata;
2568 raid5_conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 unsigned int dd_idx, pd_idx;
2570 sector_t new_sector;
2571 sector_t logical_sector, last_sector;
2572 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01002573 const int rw = bio_data_dir(bi);
NeilBrownf6344752006-03-27 01:18:17 -08002574 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
NeilBrowne5dcdd82005-09-09 16:23:41 -07002576 if (unlikely(bio_barrier(bi))) {
2577 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2578 return 0;
2579 }
2580
NeilBrown3d310eb2005-06-21 17:17:26 -07002581 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07002582
Jens Axboea3623572005-11-01 09:26:16 +01002583 disk_stat_inc(mddev->gendisk, ios[rw]);
2584 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2587 last_sector = bi->bi_sector + (bi->bi_size>>9);
2588 bi->bi_next = NULL;
2589 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2592 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002593 int disks, data_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002594
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002595 retry:
NeilBrownb578d552006-03-27 01:18:12 -08002596 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002597 if (likely(conf->expand_progress == MaxSector))
2598 disks = conf->raid_disks;
2599 else {
NeilBrowndf8e7f72006-03-27 01:18:15 -08002600 /* spinlock is needed as expand_progress may be
2601 * 64bit on a 32bit platform, and so it might be
2602 * possible to see a half-updated value
2603 * Ofcourse expand_progress could change after
2604 * the lock is dropped, so once we get a reference
2605 * to the stripe that we think it is, we will have
2606 * to check again.
2607 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002608 spin_lock_irq(&conf->device_lock);
2609 disks = conf->raid_disks;
2610 if (logical_sector >= conf->expand_progress)
2611 disks = conf->previous_raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002612 else {
2613 if (logical_sector >= conf->expand_lo) {
2614 spin_unlock_irq(&conf->device_lock);
2615 schedule();
2616 goto retry;
2617 }
2618 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002619 spin_unlock_irq(&conf->device_lock);
2620 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002621 data_disks = disks - conf->max_degraded;
2622
2623 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002624 &dd_idx, &pd_idx, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2626 (unsigned long long)new_sector,
2627 (unsigned long long)logical_sector);
2628
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002629 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002631 if (unlikely(conf->expand_progress != MaxSector)) {
2632 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08002633 * stripe, so we must do the range check again.
2634 * Expansion could still move past after this
2635 * test, but as we are holding a reference to
2636 * 'sh', we know that if that happens,
2637 * STRIPE_EXPANDING will get set and the expansion
2638 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002639 */
2640 int must_retry = 0;
2641 spin_lock_irq(&conf->device_lock);
2642 if (logical_sector < conf->expand_progress &&
2643 disks == conf->previous_raid_disks)
2644 /* mismatch, need to try again */
2645 must_retry = 1;
2646 spin_unlock_irq(&conf->device_lock);
2647 if (must_retry) {
2648 release_stripe(sh);
2649 goto retry;
2650 }
2651 }
NeilBrowne464eaf2006-03-27 01:18:14 -08002652 /* FIXME what if we get a false positive because these
2653 * are being updated.
2654 */
2655 if (logical_sector >= mddev->suspend_lo &&
2656 logical_sector < mddev->suspend_hi) {
2657 release_stripe(sh);
2658 schedule();
2659 goto retry;
2660 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002661
2662 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2663 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2664 /* Stripe is busy expanding or
2665 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 * and wait a while
2667 */
2668 raid5_unplug_device(mddev->queue);
2669 release_stripe(sh);
2670 schedule();
2671 goto retry;
2672 }
2673 finish_wait(&conf->wait_for_overlap, &w);
2674 raid5_plug_device(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07002675 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 } else {
2678 /* cannot get stripe for read-ahead, just give-up */
2679 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2680 finish_wait(&conf->wait_for_overlap, &w);
2681 break;
2682 }
2683
2684 }
2685 spin_lock_irq(&conf->device_lock);
NeilBrownf6344752006-03-27 01:18:17 -08002686 remaining = --bi->bi_phys_segments;
2687 spin_unlock_irq(&conf->device_lock);
2688 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 int bytes = bi->bi_size;
2690
NeilBrown16a53ec2006-06-26 00:27:38 -07002691 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 md_write_end(mddev);
2693 bi->bi_size = 0;
2694 bi->bi_end_io(bi, bytes, 0);
2695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return 0;
2697}
2698
NeilBrown52c03292006-06-26 00:27:43 -07002699static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
NeilBrown52c03292006-06-26 00:27:43 -07002701 /* reshaping is quite different to recovery/resync so it is
2702 * handled quite separately ... here.
2703 *
2704 * On each call to sync_request, we gather one chunk worth of
2705 * destination stripes and flag them as expanding.
2706 * Then we find all the source stripes and request reads.
2707 * As the reads complete, handle_stripe will copy the data
2708 * into the destination stripe and release that stripe.
2709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2711 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08002712 int pd_idx;
2713 sector_t first_sector, last_sector;
NeilBrown52c03292006-06-26 00:27:43 -07002714 int raid_disks;
2715 int data_disks;
2716 int i;
2717 int dd_idx;
2718 sector_t writepos, safepos, gap;
2719
2720 if (sector_nr == 0 &&
2721 conf->expand_progress != 0) {
2722 /* restarting in the middle, skip the initial sectors */
2723 sector_nr = conf->expand_progress;
2724 sector_div(sector_nr, conf->raid_disks-1);
2725 *skipped = 1;
2726 return sector_nr;
2727 }
2728
2729 /* we update the metadata when there is more than 3Meg
2730 * in the block range (that is rather arbitrary, should
2731 * probably be time based) or when the data about to be
2732 * copied would over-write the source of the data at
2733 * the front of the range.
2734 * i.e. one new_stripe forward from expand_progress new_maps
2735 * to after where expand_lo old_maps to
2736 */
2737 writepos = conf->expand_progress +
2738 conf->chunk_size/512*(conf->raid_disks-1);
2739 sector_div(writepos, conf->raid_disks-1);
2740 safepos = conf->expand_lo;
2741 sector_div(safepos, conf->previous_raid_disks-1);
2742 gap = conf->expand_progress - conf->expand_lo;
2743
2744 if (writepos >= safepos ||
2745 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
2746 /* Cannot proceed until we've updated the superblock... */
2747 wait_event(conf->wait_for_overlap,
2748 atomic_read(&conf->reshape_stripes)==0);
2749 mddev->reshape_position = conf->expand_progress;
2750 mddev->sb_dirty = 1;
2751 md_wakeup_thread(mddev->thread);
2752 wait_event(mddev->sb_wait, mddev->sb_dirty == 0 ||
2753 kthread_should_stop());
2754 spin_lock_irq(&conf->device_lock);
2755 conf->expand_lo = mddev->reshape_position;
2756 spin_unlock_irq(&conf->device_lock);
2757 wake_up(&conf->wait_for_overlap);
2758 }
2759
2760 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
2761 int j;
2762 int skipped = 0;
2763 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
2764 sh = get_active_stripe(conf, sector_nr+i,
2765 conf->raid_disks, pd_idx, 0);
2766 set_bit(STRIPE_EXPANDING, &sh->state);
2767 atomic_inc(&conf->reshape_stripes);
2768 /* If any of this stripe is beyond the end of the old
2769 * array, then we need to zero those blocks
2770 */
2771 for (j=sh->disks; j--;) {
2772 sector_t s;
2773 if (j == sh->pd_idx)
2774 continue;
2775 s = compute_blocknr(sh, j);
2776 if (s < (mddev->array_size<<1)) {
2777 skipped = 1;
2778 continue;
2779 }
2780 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
2781 set_bit(R5_Expanded, &sh->dev[j].flags);
2782 set_bit(R5_UPTODATE, &sh->dev[j].flags);
2783 }
2784 if (!skipped) {
2785 set_bit(STRIPE_EXPAND_READY, &sh->state);
2786 set_bit(STRIPE_HANDLE, &sh->state);
2787 }
2788 release_stripe(sh);
2789 }
2790 spin_lock_irq(&conf->device_lock);
2791 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
2792 spin_unlock_irq(&conf->device_lock);
2793 /* Ok, those stripe are ready. We can start scheduling
2794 * reads on the source stripes.
2795 * The source stripes are determined by mapping the first and last
2796 * block on the destination stripes.
2797 */
2798 raid_disks = conf->previous_raid_disks;
2799 data_disks = raid_disks - 1;
2800 first_sector =
2801 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
2802 raid_disks, data_disks,
2803 &dd_idx, &pd_idx, conf);
2804 last_sector =
2805 raid5_compute_sector((sector_nr+conf->chunk_size/512)
2806 *(conf->raid_disks-1) -1,
2807 raid_disks, data_disks,
2808 &dd_idx, &pd_idx, conf);
2809 if (last_sector >= (mddev->size<<1))
2810 last_sector = (mddev->size<<1)-1;
2811 while (first_sector <= last_sector) {
2812 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
2813 sh = get_active_stripe(conf, first_sector,
2814 conf->previous_raid_disks, pd_idx, 0);
2815 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2816 set_bit(STRIPE_HANDLE, &sh->state);
2817 release_stripe(sh);
2818 first_sector += STRIPE_SECTORS;
2819 }
2820 return conf->chunk_size>>9;
2821}
2822
2823/* FIXME go_faster isn't used */
2824static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
2825{
2826 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2827 struct stripe_head *sh;
2828 int pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 int raid_disks = conf->raid_disks;
NeilBrown72626682005-09-09 16:23:54 -07002830 sector_t max_sector = mddev->size << 1;
2831 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002832 int still_degraded = 0;
2833 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
NeilBrown72626682005-09-09 16:23:54 -07002835 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 /* just being told to finish up .. nothing much to do */
2837 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08002838 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2839 end_reshape(conf);
2840 return 0;
2841 }
NeilBrown72626682005-09-09 16:23:54 -07002842
2843 if (mddev->curr_resync < max_sector) /* aborted */
2844 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2845 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07002846 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07002847 conf->fullsync = 0;
2848 bitmap_close_sync(mddev->bitmap);
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return 0;
2851 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08002852
NeilBrown52c03292006-06-26 00:27:43 -07002853 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2854 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08002855
NeilBrown16a53ec2006-06-26 00:27:38 -07002856 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 * to resync, then assert that we are finished, because there is
2858 * nothing we can do.
2859 */
NeilBrown3285edf2006-06-26 00:27:55 -07002860 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07002861 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07002862 sector_t rv = (mddev->size << 1) - sector_nr;
2863 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return rv;
2865 }
NeilBrown72626682005-09-09 16:23:54 -07002866 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08002867 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07002868 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
2869 /* we can skip this block, and probably more */
2870 sync_blocks /= STRIPE_SECTORS;
2871 *skipped = 1;
2872 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
2873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
NeilBrownccfcc3c2006-03-27 01:18:09 -08002875 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002876 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (sh == NULL) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002878 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07002880 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08002882 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002884 /* Need to check if array will still be degraded after recovery/resync
2885 * We don't need to check the 'failed' flag as when that gets set,
2886 * recovery aborts.
2887 */
2888 for (i=0; i<mddev->raid_disks; i++)
2889 if (conf->disks[i].rdev == NULL)
2890 still_degraded = 1;
2891
2892 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
2893
2894 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 set_bit(STRIPE_SYNCING, &sh->state);
2896 clear_bit(STRIPE_INSYNC, &sh->state);
2897 spin_unlock(&sh->lock);
2898
NeilBrown16a53ec2006-06-26 00:27:38 -07002899 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 release_stripe(sh);
2901
2902 return STRIPE_SECTORS;
2903}
2904
2905/*
2906 * This is our raid5 kernel thread.
2907 *
2908 * We scan the hash table for stripes which can be handled now.
2909 * During the scan, completed stripes are saved for us by the interrupt
2910 * handler, so that they will not have to wait for our next wakeup.
2911 */
2912static void raid5d (mddev_t *mddev)
2913{
2914 struct stripe_head *sh;
2915 raid5_conf_t *conf = mddev_to_conf(mddev);
2916 int handled;
2917
2918 PRINTK("+++ raid5d active\n");
2919
2920 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922 handled = 0;
2923 spin_lock_irq(&conf->device_lock);
2924 while (1) {
2925 struct list_head *first;
2926
NeilBrown72626682005-09-09 16:23:54 -07002927 if (conf->seq_flush - conf->seq_write > 0) {
2928 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08002929 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002930 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08002931 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002932 conf->seq_write = seq;
2933 activate_bit_delay(conf);
2934 }
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (list_empty(&conf->handle_list) &&
2937 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
2938 !blk_queue_plugged(mddev->queue) &&
2939 !list_empty(&conf->delayed_list))
2940 raid5_activate_delayed(conf);
2941
2942 if (list_empty(&conf->handle_list))
2943 break;
2944
2945 first = conf->handle_list.next;
2946 sh = list_entry(first, struct stripe_head, lru);
2947
2948 list_del_init(first);
2949 atomic_inc(&sh->count);
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002950 BUG_ON(atomic_read(&sh->count)!= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 spin_unlock_irq(&conf->device_lock);
2952
2953 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07002954 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 release_stripe(sh);
2956
2957 spin_lock_irq(&conf->device_lock);
2958 }
2959 PRINTK("%d stripes handled\n", handled);
2960
2961 spin_unlock_irq(&conf->device_lock);
2962
2963 unplug_slaves(mddev);
2964
2965 PRINTK("--- raid5d inactive\n");
2966}
2967
NeilBrown3f294f42005-11-08 21:39:25 -08002968static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08002969raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08002970{
NeilBrown007583c2005-11-08 21:39:30 -08002971 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08002972 if (conf)
2973 return sprintf(page, "%d\n", conf->max_nr_stripes);
2974 else
2975 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08002976}
2977
2978static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08002979raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08002980{
NeilBrown007583c2005-11-08 21:39:30 -08002981 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08002982 char *end;
2983 int new;
2984 if (len >= PAGE_SIZE)
2985 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08002986 if (!conf)
2987 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08002988
2989 new = simple_strtoul(page, &end, 10);
2990 if (!*page || (*end && *end != '\n') )
2991 return -EINVAL;
2992 if (new <= 16 || new > 32768)
2993 return -EINVAL;
2994 while (new < conf->max_nr_stripes) {
2995 if (drop_one_stripe(conf))
2996 conf->max_nr_stripes--;
2997 else
2998 break;
2999 }
3000 while (new > conf->max_nr_stripes) {
3001 if (grow_one_stripe(conf))
3002 conf->max_nr_stripes++;
3003 else break;
3004 }
3005 return len;
3006}
NeilBrown007583c2005-11-08 21:39:30 -08003007
NeilBrown96de1e62005-11-08 21:39:39 -08003008static struct md_sysfs_entry
3009raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3010 raid5_show_stripe_cache_size,
3011 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08003012
3013static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003014stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003015{
NeilBrown007583c2005-11-08 21:39:30 -08003016 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003017 if (conf)
3018 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3019 else
3020 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003021}
3022
NeilBrown96de1e62005-11-08 21:39:39 -08003023static struct md_sysfs_entry
3024raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08003025
NeilBrown007583c2005-11-08 21:39:30 -08003026static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08003027 &raid5_stripecache_size.attr,
3028 &raid5_stripecache_active.attr,
3029 NULL,
3030};
NeilBrown007583c2005-11-08 21:39:30 -08003031static struct attribute_group raid5_attrs_group = {
3032 .name = NULL,
3033 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08003034};
3035
NeilBrown72626682005-09-09 16:23:54 -07003036static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
3038 raid5_conf_t *conf;
3039 int raid_disk, memory;
3040 mdk_rdev_t *rdev;
3041 struct disk_info *disk;
3042 struct list_head *tmp;
3043
NeilBrown16a53ec2006-06-26 00:27:38 -07003044 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3045 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown14f8d262006-01-06 00:20:14 -08003046 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return -EIO;
3048 }
3049
NeilBrownf6705572006-03-27 01:18:11 -08003050 if (mddev->reshape_position != MaxSector) {
3051 /* Check that we can continue the reshape.
3052 * Currently only disks can change, it must
3053 * increase, and we must be past the point where
3054 * a stripe over-writes itself
3055 */
3056 sector_t here_new, here_old;
3057 int old_disks;
3058
3059 if (mddev->new_level != mddev->level ||
3060 mddev->new_layout != mddev->layout ||
3061 mddev->new_chunk != mddev->chunk_size) {
3062 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
3063 mdname(mddev));
3064 return -EINVAL;
3065 }
3066 if (mddev->delta_disks <= 0) {
3067 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3068 mdname(mddev));
3069 return -EINVAL;
3070 }
3071 old_disks = mddev->raid_disks - mddev->delta_disks;
3072 /* reshape_position must be on a new-stripe boundary, and one
3073 * further up in new geometry must map after here in old geometry.
3074 */
3075 here_new = mddev->reshape_position;
3076 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
3077 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
3078 return -EINVAL;
3079 }
3080 /* here_new is the stripe we will write to */
3081 here_old = mddev->reshape_position;
3082 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
3083 /* here_old is the first stripe that we might need to read from */
3084 if (here_new >= here_old) {
3085 /* Reading from the same stripe as writing to - bad */
3086 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
3087 return -EINVAL;
3088 }
3089 printk(KERN_INFO "raid5: reshape will continue\n");
3090 /* OK, we should be able to continue; */
3091 }
3092
3093
NeilBrownb55e6bf2006-03-27 01:18:06 -08003094 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if ((conf = mddev->private) == NULL)
3096 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08003097 if (mddev->reshape_position == MaxSector) {
3098 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3099 } else {
3100 conf->raid_disks = mddev->raid_disks;
3101 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3102 }
3103
3104 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
NeilBrownb55e6bf2006-03-27 01:18:06 -08003105 GFP_KERNEL);
3106 if (!conf->disks)
3107 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08003108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 conf->mddev = mddev;
3110
NeilBrownfccddba2006-01-06 00:20:33 -08003111 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
NeilBrown16a53ec2006-06-26 00:27:38 -07003114 if (mddev->level == 6) {
3115 conf->spare_page = alloc_page(GFP_KERNEL);
3116 if (!conf->spare_page)
3117 goto abort;
3118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 spin_lock_init(&conf->device_lock);
3120 init_waitqueue_head(&conf->wait_for_stripe);
3121 init_waitqueue_head(&conf->wait_for_overlap);
3122 INIT_LIST_HEAD(&conf->handle_list);
3123 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07003124 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 INIT_LIST_HEAD(&conf->inactive_list);
3126 atomic_set(&conf->active_stripes, 0);
3127 atomic_set(&conf->preread_active_stripes, 0);
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3130
3131 ITERATE_RDEV(mddev,rdev,tmp) {
3132 raid_disk = rdev->raid_disk;
NeilBrownf6705572006-03-27 01:18:11 -08003133 if (raid_disk >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 || raid_disk < 0)
3135 continue;
3136 disk = conf->disks + raid_disk;
3137
3138 disk->rdev = rdev;
3139
NeilBrownb2d444d2005-11-08 21:39:31 -08003140 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 char b[BDEVNAME_SIZE];
3142 printk(KERN_INFO "raid5: device %s operational as raid"
3143 " disk %d\n", bdevname(rdev->bdev,b),
3144 raid_disk);
3145 conf->working_disks++;
3146 }
3147 }
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003150 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 */
3152 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
3153 conf->mddev = mddev;
3154 conf->chunk_size = mddev->chunk_size;
3155 conf->level = mddev->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003156 if (conf->level == 6)
3157 conf->max_degraded = 2;
3158 else
3159 conf->max_degraded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 conf->algorithm = mddev->layout;
3161 conf->max_nr_stripes = NR_STRIPES;
NeilBrownf6705572006-03-27 01:18:11 -08003162 conf->expand_progress = mddev->reshape_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 /* device size must be a multiple of chunk size */
3165 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07003166 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
NeilBrown16a53ec2006-06-26 00:27:38 -07003168 if (conf->level == 6 && conf->raid_disks < 4) {
3169 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3170 mdname(mddev), conf->raid_disks);
3171 goto abort;
3172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (!conf->chunk_size || conf->chunk_size % 4) {
3174 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3175 conf->chunk_size, mdname(mddev));
3176 goto abort;
3177 }
3178 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3179 printk(KERN_ERR
3180 "raid5: unsupported parity algorithm %d for %s\n",
3181 conf->algorithm, mdname(mddev));
3182 goto abort;
3183 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003184 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 printk(KERN_ERR "raid5: not enough operational devices for %s"
3186 " (%d/%d failed)\n",
3187 mdname(mddev), conf->failed_disks, conf->raid_disks);
3188 goto abort;
3189 }
3190
NeilBrown16a53ec2006-06-26 00:27:38 -07003191 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003193 if (mddev->ok_start_degraded)
3194 printk(KERN_WARNING
3195 "raid5: starting dirty degraded array: %s"
3196 "- data corruption possible.\n",
3197 mdname(mddev));
3198 else {
3199 printk(KERN_ERR
3200 "raid5: cannot start dirty degraded array for %s\n",
3201 mdname(mddev));
3202 goto abort;
3203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205
3206 {
3207 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3208 if (!mddev->thread) {
3209 printk(KERN_ERR
3210 "raid5: couldn't allocate thread for %s\n",
3211 mdname(mddev));
3212 goto abort;
3213 }
3214 }
NeilBrown50368052005-12-12 02:39:17 -08003215 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3217 if (grow_stripes(conf, conf->max_nr_stripes)) {
3218 printk(KERN_ERR
3219 "raid5: couldn't allocate %dkB for buffers\n", memory);
3220 shrink_stripes(conf);
3221 md_unregister_thread(mddev->thread);
3222 goto abort;
3223 } else
3224 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3225 memory, mdname(mddev));
3226
3227 if (mddev->degraded == 0)
3228 printk("raid5: raid level %d set %s active with %d out of %d"
3229 " devices, algorithm %d\n", conf->level, mdname(mddev),
3230 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3231 conf->algorithm);
3232 else
3233 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3234 " out of %d devices, algorithm %d\n", conf->level,
3235 mdname(mddev), mddev->raid_disks - mddev->degraded,
3236 mddev->raid_disks, conf->algorithm);
3237
3238 print_raid5_conf(conf);
3239
NeilBrownf6705572006-03-27 01:18:11 -08003240 if (conf->expand_progress != MaxSector) {
3241 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08003242 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08003243 atomic_set(&conf->reshape_stripes, 0);
3244 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3245 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3246 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3247 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3248 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3249 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08003250 }
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07003253 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 */
3255 {
NeilBrown16a53ec2006-06-26 00:27:38 -07003256 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3257 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07003258 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3260 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3261 }
3262
3263 /* Ok, everything is just fine now */
NeilBrown007583c2005-11-08 21:39:30 -08003264 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
NeilBrown7a5febe2005-05-16 21:53:16 -07003265
3266 mddev->queue->unplug_fn = raid5_unplug_device;
3267 mddev->queue->issue_flush_fn = raid5_issue_flush;
NeilBrown16a53ec2006-06-26 00:27:38 -07003268 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3269 conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07003270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 return 0;
3272abort:
3273 if (conf) {
3274 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07003275 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003276 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08003277 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 kfree(conf);
3279 }
3280 mddev->private = NULL;
3281 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3282 return -EIO;
3283}
3284
3285
3286
NeilBrown3f294f42005-11-08 21:39:25 -08003287static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
3289 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3290
3291 md_unregister_thread(mddev->thread);
3292 mddev->thread = NULL;
3293 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08003294 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08003296 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003297 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08003298 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 mddev->private = NULL;
3300 return 0;
3301}
3302
3303#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003304static void print_sh (struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
3306 int i;
3307
NeilBrown16a53ec2006-06-26 00:27:38 -07003308 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3309 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3310 seq_printf(seq, "sh %llu, count %d.\n",
3311 (unsigned long long)sh->sector, atomic_read(&sh->count));
3312 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003313 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003314 seq_printf(seq, "(cache%d: %p %ld) ",
3315 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003317 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
NeilBrown16a53ec2006-06-26 00:27:38 -07003320static void printall (struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
3322 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08003323 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 int i;
3325
3326 spin_lock_irq(&conf->device_lock);
3327 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08003328 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (sh->raid_conf != conf)
3330 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07003331 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333 }
3334 spin_unlock_irq(&conf->device_lock);
3335}
3336#endif
3337
3338static void status (struct seq_file *seq, mddev_t *mddev)
3339{
3340 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3341 int i;
3342
3343 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
3344 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
3345 for (i = 0; i < conf->raid_disks; i++)
3346 seq_printf (seq, "%s",
3347 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08003348 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 seq_printf (seq, "]");
3350#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003351 seq_printf (seq, "\n");
3352 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353#endif
3354}
3355
3356static void print_raid5_conf (raid5_conf_t *conf)
3357{
3358 int i;
3359 struct disk_info *tmp;
3360
3361 printk("RAID5 conf printout:\n");
3362 if (!conf) {
3363 printk("(conf==NULL)\n");
3364 return;
3365 }
3366 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
3367 conf->working_disks, conf->failed_disks);
3368
3369 for (i = 0; i < conf->raid_disks; i++) {
3370 char b[BDEVNAME_SIZE];
3371 tmp = conf->disks + i;
3372 if (tmp->rdev)
3373 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08003374 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 bdevname(tmp->rdev->bdev,b));
3376 }
3377}
3378
3379static int raid5_spare_active(mddev_t *mddev)
3380{
3381 int i;
3382 raid5_conf_t *conf = mddev->private;
3383 struct disk_info *tmp;
3384
3385 for (i = 0; i < conf->raid_disks; i++) {
3386 tmp = conf->disks + i;
3387 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08003388 && !test_bit(Faulty, &tmp->rdev->flags)
3389 && !test_bit(In_sync, &tmp->rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 mddev->degraded--;
3391 conf->failed_disks--;
3392 conf->working_disks++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003393 set_bit(In_sync, &tmp->rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
3395 }
3396 print_raid5_conf(conf);
3397 return 0;
3398}
3399
3400static int raid5_remove_disk(mddev_t *mddev, int number)
3401{
3402 raid5_conf_t *conf = mddev->private;
3403 int err = 0;
3404 mdk_rdev_t *rdev;
3405 struct disk_info *p = conf->disks + number;
3406
3407 print_raid5_conf(conf);
3408 rdev = p->rdev;
3409 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003410 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 atomic_read(&rdev->nr_pending)) {
3412 err = -EBUSY;
3413 goto abort;
3414 }
3415 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003416 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 if (atomic_read(&rdev->nr_pending)) {
3418 /* lost the race, try later */
3419 err = -EBUSY;
3420 p->rdev = rdev;
3421 }
3422 }
3423abort:
3424
3425 print_raid5_conf(conf);
3426 return err;
3427}
3428
3429static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3430{
3431 raid5_conf_t *conf = mddev->private;
3432 int found = 0;
3433 int disk;
3434 struct disk_info *p;
3435
NeilBrown16a53ec2006-06-26 00:27:38 -07003436 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* no point adding a device */
3438 return 0;
3439
3440 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003441 * find the disk ... but prefer rdev->saved_raid_disk
3442 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003444 if (rdev->saved_raid_disk >= 0 &&
3445 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3446 disk = rdev->saved_raid_disk;
3447 else
3448 disk = 0;
3449 for ( ; disk < conf->raid_disks; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003451 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 rdev->raid_disk = disk;
3453 found = 1;
NeilBrown72626682005-09-09 16:23:54 -07003454 if (rdev->saved_raid_disk != disk)
3455 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08003456 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 break;
3458 }
3459 print_raid5_conf(conf);
3460 return found;
3461}
3462
3463static int raid5_resize(mddev_t *mddev, sector_t sectors)
3464{
3465 /* no resync is happening, and there is enough space
3466 * on all devices, so we can resize.
3467 * We need to make sure resync covers any new space.
3468 * If the array is shrinking we should possibly wait until
3469 * any io in the removed space completes, but it hardly seems
3470 * worth it.
3471 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003472 raid5_conf_t *conf = mddev_to_conf(mddev);
3473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003475 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 set_capacity(mddev->gendisk, mddev->array_size << 1);
3477 mddev->changed = 1;
3478 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3479 mddev->recovery_cp = mddev->size << 1;
3480 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3481 }
3482 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003483 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return 0;
3485}
3486
NeilBrown29269552006-03-27 01:18:10 -08003487#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003488static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08003489{
3490 raid5_conf_t *conf = mddev_to_conf(mddev);
3491 int err;
NeilBrown29269552006-03-27 01:18:10 -08003492
NeilBrown63c70c42006-03-27 01:18:13 -08003493 if (mddev->delta_disks < 0 ||
3494 mddev->new_level != mddev->level)
3495 return -EINVAL; /* Cannot shrink array or change level yet */
3496 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08003497 return 0; /* nothing to do */
3498
3499 /* Can only proceed if there are plenty of stripe_heads.
3500 * We need a minimum of one full stripe,, and for sensible progress
3501 * it is best to have about 4 times that.
3502 * If we require 4 times, then the default 256 4K stripe_heads will
3503 * allow for chunk sizes up to 256K, which is probably OK.
3504 * If the chunk size is greater, user-space should request more
3505 * stripe_heads first.
3506 */
NeilBrown63c70c42006-03-27 01:18:13 -08003507 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3508 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08003509 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3510 (mddev->chunk_size / STRIPE_SIZE)*4);
3511 return -ENOSPC;
3512 }
3513
NeilBrown63c70c42006-03-27 01:18:13 -08003514 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3515 if (err)
3516 return err;
3517
3518 /* looks like we might be able to manage this */
3519 return 0;
3520}
3521
3522static int raid5_start_reshape(mddev_t *mddev)
3523{
3524 raid5_conf_t *conf = mddev_to_conf(mddev);
3525 mdk_rdev_t *rdev;
3526 struct list_head *rtmp;
3527 int spares = 0;
3528 int added_devices = 0;
3529
3530 if (mddev->degraded ||
3531 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3532 return -EBUSY;
3533
NeilBrown29269552006-03-27 01:18:10 -08003534 ITERATE_RDEV(mddev, rdev, rtmp)
3535 if (rdev->raid_disk < 0 &&
3536 !test_bit(Faulty, &rdev->flags))
3537 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08003538
3539 if (spares < mddev->delta_disks-1)
NeilBrown29269552006-03-27 01:18:10 -08003540 /* Not enough devices even to make a degraded array
3541 * of that size
3542 */
3543 return -EINVAL;
3544
NeilBrownf6705572006-03-27 01:18:11 -08003545 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08003546 spin_lock_irq(&conf->device_lock);
3547 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003548 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08003549 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003550 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08003551 spin_unlock_irq(&conf->device_lock);
3552
3553 /* Add some new drives, as many as will fit.
3554 * We know there are enough to make the newly sized array work.
3555 */
3556 ITERATE_RDEV(mddev, rdev, rtmp)
3557 if (rdev->raid_disk < 0 &&
3558 !test_bit(Faulty, &rdev->flags)) {
3559 if (raid5_add_disk(mddev, rdev)) {
3560 char nm[20];
3561 set_bit(In_sync, &rdev->flags);
3562 conf->working_disks++;
3563 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003564 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08003565 sprintf(nm, "rd%d", rdev->raid_disk);
3566 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3567 } else
3568 break;
3569 }
3570
NeilBrown63c70c42006-03-27 01:18:13 -08003571 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
3572 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08003573 mddev->reshape_position = 0;
3574 mddev->sb_dirty = 1;
3575
NeilBrown29269552006-03-27 01:18:10 -08003576 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3577 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3578 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3579 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3580 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3581 "%s_reshape");
3582 if (!mddev->sync_thread) {
3583 mddev->recovery = 0;
3584 spin_lock_irq(&conf->device_lock);
3585 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3586 conf->expand_progress = MaxSector;
3587 spin_unlock_irq(&conf->device_lock);
3588 return -EAGAIN;
3589 }
3590 md_wakeup_thread(mddev->sync_thread);
3591 md_new_event(mddev);
3592 return 0;
3593}
3594#endif
3595
3596static void end_reshape(raid5_conf_t *conf)
3597{
3598 struct block_device *bdev;
3599
NeilBrownf6705572006-03-27 01:18:11 -08003600 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
3601 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
3602 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
3603 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08003604
NeilBrownf6705572006-03-27 01:18:11 -08003605 bdev = bdget_disk(conf->mddev->gendisk, 0);
3606 if (bdev) {
3607 mutex_lock(&bdev->bd_inode->i_mutex);
3608 i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
3609 mutex_unlock(&bdev->bd_inode->i_mutex);
3610 bdput(bdev);
3611 }
3612 spin_lock_irq(&conf->device_lock);
3613 conf->expand_progress = MaxSector;
3614 spin_unlock_irq(&conf->device_lock);
3615 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07003616
3617 /* read-ahead size must cover two whole stripes, which is
3618 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3619 */
3620 {
3621 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3622 int stripe = data_disks *
3623 (conf->mddev->chunk_size / PAGE_SIZE);
3624 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3625 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3626 }
NeilBrown29269552006-03-27 01:18:10 -08003627 }
NeilBrown29269552006-03-27 01:18:10 -08003628}
3629
NeilBrown72626682005-09-09 16:23:54 -07003630static void raid5_quiesce(mddev_t *mddev, int state)
3631{
3632 raid5_conf_t *conf = mddev_to_conf(mddev);
3633
3634 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08003635 case 2: /* resume for a suspend */
3636 wake_up(&conf->wait_for_overlap);
3637 break;
3638
NeilBrown72626682005-09-09 16:23:54 -07003639 case 1: /* stop all writes */
3640 spin_lock_irq(&conf->device_lock);
3641 conf->quiesce = 1;
3642 wait_event_lock_irq(conf->wait_for_stripe,
3643 atomic_read(&conf->active_stripes) == 0,
3644 conf->device_lock, /* nothing */);
3645 spin_unlock_irq(&conf->device_lock);
3646 break;
3647
3648 case 0: /* re-enable writes */
3649 spin_lock_irq(&conf->device_lock);
3650 conf->quiesce = 0;
3651 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08003652 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07003653 spin_unlock_irq(&conf->device_lock);
3654 break;
3655 }
NeilBrown72626682005-09-09 16:23:54 -07003656}
NeilBrownb15c2e52006-01-06 00:20:16 -08003657
NeilBrown16a53ec2006-06-26 00:27:38 -07003658static struct mdk_personality raid6_personality =
3659{
3660 .name = "raid6",
3661 .level = 6,
3662 .owner = THIS_MODULE,
3663 .make_request = make_request,
3664 .run = run,
3665 .stop = stop,
3666 .status = status,
3667 .error_handler = error,
3668 .hot_add_disk = raid5_add_disk,
3669 .hot_remove_disk= raid5_remove_disk,
3670 .spare_active = raid5_spare_active,
3671 .sync_request = sync_request,
3672 .resize = raid5_resize,
3673 .quiesce = raid5_quiesce,
3674};
NeilBrown2604b702006-01-06 00:20:36 -08003675static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
3677 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08003678 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 .owner = THIS_MODULE,
3680 .make_request = make_request,
3681 .run = run,
3682 .stop = stop,
3683 .status = status,
3684 .error_handler = error,
3685 .hot_add_disk = raid5_add_disk,
3686 .hot_remove_disk= raid5_remove_disk,
3687 .spare_active = raid5_spare_active,
3688 .sync_request = sync_request,
3689 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08003690#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003691 .check_reshape = raid5_check_reshape,
3692 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08003693#endif
NeilBrown72626682005-09-09 16:23:54 -07003694 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695};
3696
NeilBrown2604b702006-01-06 00:20:36 -08003697static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
NeilBrown2604b702006-01-06 00:20:36 -08003699 .name = "raid4",
3700 .level = 4,
3701 .owner = THIS_MODULE,
3702 .make_request = make_request,
3703 .run = run,
3704 .stop = stop,
3705 .status = status,
3706 .error_handler = error,
3707 .hot_add_disk = raid5_add_disk,
3708 .hot_remove_disk= raid5_remove_disk,
3709 .spare_active = raid5_spare_active,
3710 .sync_request = sync_request,
3711 .resize = raid5_resize,
3712 .quiesce = raid5_quiesce,
3713};
3714
3715static int __init raid5_init(void)
3716{
NeilBrown16a53ec2006-06-26 00:27:38 -07003717 int e;
3718
3719 e = raid6_select_algo();
3720 if ( e )
3721 return e;
3722 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08003723 register_md_personality(&raid5_personality);
3724 register_md_personality(&raid4_personality);
3725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
NeilBrown2604b702006-01-06 00:20:36 -08003728static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
NeilBrown16a53ec2006-06-26 00:27:38 -07003730 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08003731 unregister_md_personality(&raid5_personality);
3732 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
3735module_init(raid5_init);
3736module_exit(raid5_exit);
3737MODULE_LICENSE("GPL");
3738MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003739MODULE_ALIAS("md-raid5");
3740MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08003741MODULE_ALIAS("md-level-5");
3742MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07003743MODULE_ALIAS("md-personality-8"); /* RAID6 */
3744MODULE_ALIAS("md-raid6");
3745MODULE_ALIAS("md-level-6");
3746
3747/* This used to be two separate modules, they were: */
3748MODULE_ALIAS("raid5");
3749MODULE_ALIAS("raid6");