blob: 69c3e201fa3bf0ee1ea51378fd172d4d4461e612 [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
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->bm_write is the number of the last batch successfully written.
31 * conf->bm_flush is the number of the last batch that was closed to
32 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is bm_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/module.h>
47#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/highmem.h>
49#include <linux/bitops.h>
NeilBrownf6705572006-03-27 01:18:11 -080050#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/atomic.h>
NeilBrown16a53ec2006-06-26 00:27:38 -070052#include "raid6.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
NeilBrown72626682005-09-09 16:23:54 -070054#include <linux/raid/bitmap.h>
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * Stripe cache
58 */
59
60#define NR_STRIPES 256
61#define STRIPE_SIZE PAGE_SIZE
62#define STRIPE_SHIFT (PAGE_SHIFT - 9)
63#define STRIPE_SECTORS (STRIPE_SIZE>>9)
64#define IO_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080065#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define HASH_MASK (NR_HASH - 1)
67
NeilBrownfccddba2006-01-06 00:20:33 -080068#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* bio's attached to a stripe+device for I/O are linked together in bi_sector
71 * order without overlap. There may be several bio's per stripe+device, and
72 * a bio could span several devices.
73 * When walking this list for a particular stripe+device, we must never proceed
74 * beyond a bio that extends past this device, as the next bio might no longer
75 * be valid.
76 * This macro is used to determine the 'next' bio in the list, given the sector
77 * of the current stripe+device
78 */
79#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
80/*
81 * The following can be used to debug the driver
82 */
83#define RAID5_DEBUG 0
84#define RAID5_PARANOIA 1
85#if RAID5_PARANOIA && defined(CONFIG_SMP)
86# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
87#else
88# define CHECK_DEVLOCK()
89#endif
90
91#define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
92#if RAID5_DEBUG
93#define inline
94#define __inline__
95#endif
96
NeilBrown16a53ec2006-06-26 00:27:38 -070097#if !RAID6_USE_EMPTY_ZERO_PAGE
98/* In .bss so it's zeroed */
99const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
100#endif
101
102static inline int raid6_next_disk(int disk, int raid_disks)
103{
104 disk++;
105 return (disk < raid_disks) ? disk : 0;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static void print_raid5_conf (raid5_conf_t *conf);
108
Arjan van de Ven858119e2006-01-14 13:20:43 -0800109static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200112 BUG_ON(!list_empty(&sh->lru));
113 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700115 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700117 blk_plug_device(conf->mddev->queue);
118 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700119 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700120 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700121 blk_plug_device(conf->mddev->queue);
122 } else {
NeilBrown72626682005-09-09 16:23:54 -0700123 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 md_wakeup_thread(conf->mddev->thread);
127 } else {
128 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
129 atomic_dec(&conf->preread_active_stripes);
130 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
131 md_wakeup_thread(conf->mddev->thread);
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800134 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
135 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 wake_up(&conf->wait_for_stripe);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
140}
141static void release_stripe(struct stripe_head *sh)
142{
143 raid5_conf_t *conf = sh->raid_conf;
144 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 spin_lock_irqsave(&conf->device_lock, flags);
147 __release_stripe(conf, sh);
148 spin_unlock_irqrestore(&conf->device_lock, flags);
149}
150
NeilBrownfccddba2006-01-06 00:20:33 -0800151static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
154
NeilBrownfccddba2006-01-06 00:20:33 -0800155 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
NeilBrown16a53ec2006-06-26 00:27:38 -0700158static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
NeilBrownfccddba2006-01-06 00:20:33 -0800160 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
163
164 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800165 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168
169/* find an idle stripe, make sure it is unhashed, and return it. */
170static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
171{
172 struct stripe_head *sh = NULL;
173 struct list_head *first;
174
175 CHECK_DEVLOCK();
176 if (list_empty(&conf->inactive_list))
177 goto out;
178 first = conf->inactive_list.next;
179 sh = list_entry(first, struct stripe_head, lru);
180 list_del_init(first);
181 remove_hash(sh);
182 atomic_inc(&conf->active_stripes);
183out:
184 return sh;
185}
186
187static void shrink_buffers(struct stripe_head *sh, int num)
188{
189 struct page *p;
190 int i;
191
192 for (i=0; i<num ; i++) {
193 p = sh->dev[i].page;
194 if (!p)
195 continue;
196 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800197 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199}
200
201static int grow_buffers(struct stripe_head *sh, int num)
202{
203 int i;
204
205 for (i=0; i<num; i++) {
206 struct page *page;
207
208 if (!(page = alloc_page(GFP_KERNEL))) {
209 return 1;
210 }
211 sh->dev[i].page = page;
212 }
213 return 0;
214}
215
216static void raid5_build_block (struct stripe_head *sh, int i);
217
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800218static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200223 BUG_ON(atomic_read(&sh->count) != 0);
224 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 CHECK_DEVLOCK();
227 PRINTK("init_stripe called, stripe %llu\n",
228 (unsigned long long)sh->sector);
229
230 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sh->sector = sector;
233 sh->pd_idx = pd_idx;
234 sh->state = 0;
235
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800236 sh->disks = disks;
237
238 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct r5dev *dev = &sh->dev[i];
240
241 if (dev->toread || dev->towrite || dev->written ||
242 test_bit(R5_LOCKED, &dev->flags)) {
243 printk("sector=%llx i=%d %p %p %p %d\n",
244 (unsigned long long)sh->sector, i, dev->toread,
245 dev->towrite, dev->written,
246 test_bit(R5_LOCKED, &dev->flags));
247 BUG();
248 }
249 dev->flags = 0;
250 raid5_build_block(sh, i);
251 }
252 insert_hash(conf, sh);
253}
254
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800255static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800258 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 CHECK_DEVLOCK();
261 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800262 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800263 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return sh;
265 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
266 return NULL;
267}
268
269static void unplug_slaves(mddev_t *mddev);
270static void raid5_unplug_device(request_queue_t *q);
271
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800272static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
273 int pd_idx, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct stripe_head *sh;
276
277 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
278
279 spin_lock_irq(&conf->device_lock);
280
281 do {
NeilBrown72626682005-09-09 16:23:54 -0700282 wait_event_lock_irq(conf->wait_for_stripe,
283 conf->quiesce == 0,
284 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800285 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!sh) {
287 if (!conf->inactive_blocked)
288 sh = get_free_stripe(conf);
289 if (noblock && sh == NULL)
290 break;
291 if (!sh) {
292 conf->inactive_blocked = 1;
293 wait_event_lock_irq(conf->wait_for_stripe,
294 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800295 (atomic_read(&conf->active_stripes)
296 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 || !conf->inactive_blocked),
298 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700299 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 );
301 conf->inactive_blocked = 0;
302 } else
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800303 init_stripe(sh, sector, pd_idx, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 } else {
305 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200306 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 } else {
308 if (!test_bit(STRIPE_HANDLE, &sh->state))
309 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700310 if (list_empty(&sh->lru) &&
311 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700312 BUG();
313 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 }
316 } while (sh == NULL);
317
318 if (sh)
319 atomic_inc(&sh->count);
320
321 spin_unlock_irq(&conf->device_lock);
322 return sh;
323}
324
NeilBrown3f294f42005-11-08 21:39:25 -0800325static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800328 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
329 if (!sh)
330 return 0;
331 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
332 sh->raid_conf = conf;
333 spin_lock_init(&sh->lock);
334
335 if (grow_buffers(sh, conf->raid_disks)) {
336 shrink_buffers(sh, conf->raid_disks);
337 kmem_cache_free(conf->slab_cache, sh);
338 return 0;
339 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800340 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800341 /* we just created an active stripe so... */
342 atomic_set(&sh->count, 1);
343 atomic_inc(&conf->active_stripes);
344 INIT_LIST_HEAD(&sh->lru);
345 release_stripe(sh);
346 return 1;
347}
348
349static int grow_stripes(raid5_conf_t *conf, int num)
350{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 kmem_cache_t *sc;
352 int devs = conf->raid_disks;
353
NeilBrownad01c9e2006-03-27 01:18:07 -0800354 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
355 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
356 conf->active_name = 0;
357 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
359 0, 0, NULL, NULL);
360 if (!sc)
361 return 1;
362 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800363 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700364 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800365 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
NeilBrown29269552006-03-27 01:18:10 -0800369
370#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800371static int resize_stripes(raid5_conf_t *conf, int newsize)
372{
373 /* Make all the stripes able to hold 'newsize' devices.
374 * New slots in each stripe get 'page' set to a new page.
375 *
376 * This happens in stages:
377 * 1/ create a new kmem_cache and allocate the required number of
378 * stripe_heads.
379 * 2/ gather all the old stripe_heads and tranfer the pages across
380 * to the new stripe_heads. This will have the side effect of
381 * freezing the array as once all stripe_heads have been collected,
382 * no IO will be possible. Old stripe heads are freed once their
383 * pages have been transferred over, and the old kmem_cache is
384 * freed when all stripes are done.
385 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
386 * we simple return a failre status - no need to clean anything up.
387 * 4/ allocate new pages for the new slots in the new stripe_heads.
388 * If this fails, we don't bother trying the shrink the
389 * stripe_heads down again, we just leave them as they are.
390 * As each stripe_head is processed the new one is released into
391 * active service.
392 *
393 * Once step2 is started, we cannot afford to wait for a write,
394 * so we use GFP_NOIO allocations.
395 */
396 struct stripe_head *osh, *nsh;
397 LIST_HEAD(newstripes);
398 struct disk_info *ndisks;
399 int err = 0;
400 kmem_cache_t *sc;
401 int i;
402
403 if (newsize <= conf->pool_size)
404 return 0; /* never bother to shrink */
405
406 /* Step 1 */
407 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
408 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
409 0, 0, NULL, NULL);
410 if (!sc)
411 return -ENOMEM;
412
413 for (i = conf->max_nr_stripes; i; i--) {
414 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
415 if (!nsh)
416 break;
417
418 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
419
420 nsh->raid_conf = conf;
421 spin_lock_init(&nsh->lock);
422
423 list_add(&nsh->lru, &newstripes);
424 }
425 if (i) {
426 /* didn't get enough, give up */
427 while (!list_empty(&newstripes)) {
428 nsh = list_entry(newstripes.next, struct stripe_head, lru);
429 list_del(&nsh->lru);
430 kmem_cache_free(sc, nsh);
431 }
432 kmem_cache_destroy(sc);
433 return -ENOMEM;
434 }
435 /* Step 2 - Must use GFP_NOIO now.
436 * OK, we have enough stripes, start collecting inactive
437 * stripes and copying them over
438 */
439 list_for_each_entry(nsh, &newstripes, lru) {
440 spin_lock_irq(&conf->device_lock);
441 wait_event_lock_irq(conf->wait_for_stripe,
442 !list_empty(&conf->inactive_list),
443 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -0800444 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -0800445 );
446 osh = get_free_stripe(conf);
447 spin_unlock_irq(&conf->device_lock);
448 atomic_set(&nsh->count, 1);
449 for(i=0; i<conf->pool_size; i++)
450 nsh->dev[i].page = osh->dev[i].page;
451 for( ; i<newsize; i++)
452 nsh->dev[i].page = NULL;
453 kmem_cache_free(conf->slab_cache, osh);
454 }
455 kmem_cache_destroy(conf->slab_cache);
456
457 /* Step 3.
458 * At this point, we are holding all the stripes so the array
459 * is completely stalled, so now is a good time to resize
460 * conf->disks.
461 */
462 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
463 if (ndisks) {
464 for (i=0; i<conf->raid_disks; i++)
465 ndisks[i] = conf->disks[i];
466 kfree(conf->disks);
467 conf->disks = ndisks;
468 } else
469 err = -ENOMEM;
470
471 /* Step 4, return new stripes to service */
472 while(!list_empty(&newstripes)) {
473 nsh = list_entry(newstripes.next, struct stripe_head, lru);
474 list_del_init(&nsh->lru);
475 for (i=conf->raid_disks; i < newsize; i++)
476 if (nsh->dev[i].page == NULL) {
477 struct page *p = alloc_page(GFP_NOIO);
478 nsh->dev[i].page = p;
479 if (!p)
480 err = -ENOMEM;
481 }
482 release_stripe(nsh);
483 }
484 /* critical section pass, GFP_NOIO no longer needed */
485
486 conf->slab_cache = sc;
487 conf->active_name = 1-conf->active_name;
488 conf->pool_size = newsize;
489 return err;
490}
NeilBrown29269552006-03-27 01:18:10 -0800491#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
NeilBrown3f294f42005-11-08 21:39:25 -0800493static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct stripe_head *sh;
496
NeilBrown3f294f42005-11-08 21:39:25 -0800497 spin_lock_irq(&conf->device_lock);
498 sh = get_free_stripe(conf);
499 spin_unlock_irq(&conf->device_lock);
500 if (!sh)
501 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200502 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -0800503 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -0800504 kmem_cache_free(conf->slab_cache, sh);
505 atomic_dec(&conf->active_stripes);
506 return 1;
507}
508
509static void shrink_stripes(raid5_conf_t *conf)
510{
511 while (drop_one_stripe(conf))
512 ;
513
NeilBrown29fc7e32006-02-03 03:03:41 -0800514 if (conf->slab_cache)
515 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 conf->slab_cache = NULL;
517}
518
NeilBrown4e5314b2005-11-08 21:39:22 -0800519static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int error)
521{
522 struct stripe_head *sh = bi->bi_private;
523 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800524 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -0700526 char b[BDEVNAME_SIZE];
527 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 if (bi->bi_size)
530 return 1;
531
532 for (i=0 ; i<disks; i++)
533 if (bi == &sh->dev[i].req)
534 break;
535
536 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
537 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
538 uptodate);
539 if (i == disks) {
540 BUG();
541 return 0;
542 }
543
544 if (uptodate) {
545#if 0
546 struct bio *bio;
547 unsigned long flags;
548 spin_lock_irqsave(&conf->device_lock, flags);
549 /* we can return a buffer if we bypassed the cache or
550 * if the top buffer is not in highmem. If there are
551 * multiple buffers, leave the extra work to
552 * handle_stripe
553 */
554 buffer = sh->bh_read[i];
555 if (buffer &&
556 (!PageHighMem(buffer->b_page)
557 || buffer->b_page == bh->b_page )
558 ) {
559 sh->bh_read[i] = buffer->b_reqnext;
560 buffer->b_reqnext = NULL;
561 } else
562 buffer = NULL;
563 spin_unlock_irqrestore(&conf->device_lock, flags);
564 if (sh->bh_page[i]==bh->b_page)
565 set_buffer_uptodate(bh);
566 if (buffer) {
567 if (buffer->b_page != bh->b_page)
568 memcpy(buffer->b_data, bh->b_data, bh->b_size);
569 buffer->b_end_io(buffer, 1);
570 }
571#else
572 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -0800573#endif
574 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -0700575 rdev = conf->disks[i].rdev;
576 printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
577 mdname(conf->mddev), STRIPE_SECTORS,
578 (unsigned long long)sh->sector + rdev->data_offset,
579 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -0800580 clear_bit(R5_ReadError, &sh->dev[i].flags);
581 clear_bit(R5_ReWrite, &sh->dev[i].flags);
582 }
NeilBrownba22dcb2005-11-08 21:39:31 -0800583 if (atomic_read(&conf->disks[i].rdev->read_errors))
584 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 } else {
NeilBrownd6950432006-07-10 04:44:20 -0700586 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -0800587 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -0700588 rdev = conf->disks[i].rdev;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700591 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -0800592 if (conf->mddev->degraded)
NeilBrownd6950432006-07-10 04:44:20 -0700593 printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
594 mdname(conf->mddev),
595 (unsigned long long)sh->sector + rdev->data_offset,
596 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800597 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -0800598 /* Oh, no!!! */
NeilBrownd6950432006-07-10 04:44:20 -0700599 printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
600 mdname(conf->mddev),
601 (unsigned long long)sh->sector + rdev->data_offset,
602 bdn);
603 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -0800604 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -0800605 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -0700606 "raid5:%s: Too many read errors, failing device %s.\n",
607 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800608 else
609 retry = 1;
610 if (retry)
611 set_bit(R5_ReadError, &sh->dev[i].flags);
612 else {
NeilBrown4e5314b2005-11-08 21:39:22 -0800613 clear_bit(R5_ReadError, &sh->dev[i].flags);
614 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700615 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -0800616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
619#if 0
620 /* must restore b_page before unlocking buffer... */
621 if (sh->bh_page[i] != bh->b_page) {
622 bh->b_page = sh->bh_page[i];
623 bh->b_data = page_address(bh->b_page);
624 clear_buffer_uptodate(bh);
625 }
626#endif
627 clear_bit(R5_LOCKED, &sh->dev[i].flags);
628 set_bit(STRIPE_HANDLE, &sh->state);
629 release_stripe(sh);
630 return 0;
631}
632
633static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
634 int error)
635{
636 struct stripe_head *sh = bi->bi_private;
637 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800638 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
640
641 if (bi->bi_size)
642 return 1;
643
644 for (i=0 ; i<disks; i++)
645 if (bi == &sh->dev[i].req)
646 break;
647
648 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
649 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
650 uptodate);
651 if (i == disks) {
652 BUG();
653 return 0;
654 }
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!uptodate)
657 md_error(conf->mddev, conf->disks[i].rdev);
658
659 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
660
661 clear_bit(R5_LOCKED, &sh->dev[i].flags);
662 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -0700663 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665}
666
667
668static sector_t compute_blocknr(struct stripe_head *sh, int i);
669
670static void raid5_build_block (struct stripe_head *sh, int i)
671{
672 struct r5dev *dev = &sh->dev[i];
673
674 bio_init(&dev->req);
675 dev->req.bi_io_vec = &dev->vec;
676 dev->req.bi_vcnt++;
677 dev->req.bi_max_vecs++;
678 dev->vec.bv_page = dev->page;
679 dev->vec.bv_len = STRIPE_SIZE;
680 dev->vec.bv_offset = 0;
681
682 dev->req.bi_sector = sh->sector;
683 dev->req.bi_private = sh;
684
685 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -0700686 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static void error(mddev_t *mddev, mdk_rdev_t *rdev)
690{
691 char b[BDEVNAME_SIZE];
692 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
693 PRINTK("raid5: error called\n");
694
NeilBrownb2d444d2005-11-08 21:39:31 -0800695 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -0700696 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -0700697 if (test_and_clear_bit(In_sync, &rdev->flags)) {
698 unsigned long flags;
699 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700701 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
703 * if recovery was running, make sure it aborts.
704 */
705 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
706 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800707 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 printk (KERN_ALERT
709 "raid5: Disk failure on %s, disabling device."
710 " Operation continuing on %d devices\n",
NeilBrown02c2de82006-10-03 01:15:47 -0700711 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
NeilBrown16a53ec2006-06-26 00:27:38 -0700713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715/*
716 * Input: a 'big' sector number,
717 * Output: index of the data and parity disk, and the sector # in them.
718 */
719static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
720 unsigned int data_disks, unsigned int * dd_idx,
721 unsigned int * pd_idx, raid5_conf_t *conf)
722{
723 long stripe;
724 unsigned long chunk_number;
725 unsigned int chunk_offset;
726 sector_t new_sector;
727 int sectors_per_chunk = conf->chunk_size >> 9;
728
729 /* First compute the information on this sector */
730
731 /*
732 * Compute the chunk number and the sector offset inside the chunk
733 */
734 chunk_offset = sector_div(r_sector, sectors_per_chunk);
735 chunk_number = r_sector;
736 BUG_ON(r_sector != chunk_number);
737
738 /*
739 * Compute the stripe number
740 */
741 stripe = chunk_number / data_disks;
742
743 /*
744 * Compute the data disk and parity disk indexes inside the stripe
745 */
746 *dd_idx = chunk_number % data_disks;
747
748 /*
749 * Select the parity disk based on the user selected algorithm.
750 */
NeilBrown16a53ec2006-06-26 00:27:38 -0700751 switch(conf->level) {
752 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -0700754 break;
755 case 5:
756 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case ALGORITHM_LEFT_ASYMMETRIC:
758 *pd_idx = data_disks - stripe % raid_disks;
759 if (*dd_idx >= *pd_idx)
760 (*dd_idx)++;
761 break;
762 case ALGORITHM_RIGHT_ASYMMETRIC:
763 *pd_idx = stripe % raid_disks;
764 if (*dd_idx >= *pd_idx)
765 (*dd_idx)++;
766 break;
767 case ALGORITHM_LEFT_SYMMETRIC:
768 *pd_idx = data_disks - stripe % raid_disks;
769 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
770 break;
771 case ALGORITHM_RIGHT_SYMMETRIC:
772 *pd_idx = stripe % raid_disks;
773 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
774 break;
775 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800776 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700778 }
779 break;
780 case 6:
781
782 /**** FIX THIS ****/
783 switch (conf->algorithm) {
784 case ALGORITHM_LEFT_ASYMMETRIC:
785 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
786 if (*pd_idx == raid_disks-1)
787 (*dd_idx)++; /* Q D D D P */
788 else if (*dd_idx >= *pd_idx)
789 (*dd_idx) += 2; /* D D P Q D */
790 break;
791 case ALGORITHM_RIGHT_ASYMMETRIC:
792 *pd_idx = stripe % raid_disks;
793 if (*pd_idx == raid_disks-1)
794 (*dd_idx)++; /* Q D D D P */
795 else if (*dd_idx >= *pd_idx)
796 (*dd_idx) += 2; /* D D P Q D */
797 break;
798 case ALGORITHM_LEFT_SYMMETRIC:
799 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
800 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
801 break;
802 case ALGORITHM_RIGHT_SYMMETRIC:
803 *pd_idx = stripe % raid_disks;
804 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
805 break;
806 default:
807 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
808 conf->algorithm);
809 }
810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
813 /*
814 * Finally, compute the new sector number
815 */
816 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
817 return new_sector;
818}
819
820
821static sector_t compute_blocknr(struct stripe_head *sh, int i)
822{
823 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800824 int raid_disks = sh->disks, data_disks = raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 sector_t new_sector = sh->sector, check;
826 int sectors_per_chunk = conf->chunk_size >> 9;
827 sector_t stripe;
828 int chunk_offset;
829 int chunk_number, dummy1, dummy2, dd_idx = i;
830 sector_t r_sector;
831
NeilBrown16a53ec2006-06-26 00:27:38 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 chunk_offset = sector_div(new_sector, sectors_per_chunk);
834 stripe = new_sector;
835 BUG_ON(new_sector != stripe);
836
NeilBrown16a53ec2006-06-26 00:27:38 -0700837 if (i == sh->pd_idx)
838 return 0;
839 switch(conf->level) {
840 case 4: break;
841 case 5:
842 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 case ALGORITHM_LEFT_ASYMMETRIC:
844 case ALGORITHM_RIGHT_ASYMMETRIC:
845 if (i > sh->pd_idx)
846 i--;
847 break;
848 case ALGORITHM_LEFT_SYMMETRIC:
849 case ALGORITHM_RIGHT_SYMMETRIC:
850 if (i < sh->pd_idx)
851 i += raid_disks;
852 i -= (sh->pd_idx + 1);
853 break;
854 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800855 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -0700856 conf->algorithm);
857 }
858 break;
859 case 6:
860 data_disks = raid_disks - 2;
861 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
862 return 0; /* It is the Q disk */
863 switch (conf->algorithm) {
864 case ALGORITHM_LEFT_ASYMMETRIC:
865 case ALGORITHM_RIGHT_ASYMMETRIC:
866 if (sh->pd_idx == raid_disks-1)
867 i--; /* Q D D D P */
868 else if (i > sh->pd_idx)
869 i -= 2; /* D D P Q D */
870 break;
871 case ALGORITHM_LEFT_SYMMETRIC:
872 case ALGORITHM_RIGHT_SYMMETRIC:
873 if (sh->pd_idx == raid_disks-1)
874 i--; /* Q D D D P */
875 else {
876 /* D D P Q D */
877 if (i < sh->pd_idx)
878 i += raid_disks;
879 i -= (sh->pd_idx + 2);
880 }
881 break;
882 default:
883 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700885 }
886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
889 chunk_number = stripe * data_disks + i;
890 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
891
892 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
893 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -0800894 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return 0;
896 }
897 return r_sector;
898}
899
900
901
902/*
NeilBrown16a53ec2006-06-26 00:27:38 -0700903 * Copy data between a page in the stripe cache, and one or more bion
904 * The page could align with the middle of the bio, or there could be
905 * several bion, each with several bio_vecs, which cover part of the page
906 * Multiple bion are linked together on bi_next. There may be extras
907 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
909static void copy_data(int frombio, struct bio *bio,
910 struct page *page,
911 sector_t sector)
912{
913 char *pa = page_address(page);
914 struct bio_vec *bvl;
915 int i;
916 int page_offset;
917
918 if (bio->bi_sector >= sector)
919 page_offset = (signed)(bio->bi_sector - sector) * 512;
920 else
921 page_offset = (signed)(sector - bio->bi_sector) * -512;
922 bio_for_each_segment(bvl, bio, i) {
923 int len = bio_iovec_idx(bio,i)->bv_len;
924 int clen;
925 int b_offset = 0;
926
927 if (page_offset < 0) {
928 b_offset = -page_offset;
929 page_offset += b_offset;
930 len -= b_offset;
931 }
932
933 if (len > 0 && page_offset + len > STRIPE_SIZE)
934 clen = STRIPE_SIZE - page_offset;
935 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (clen > 0) {
938 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
939 if (frombio)
940 memcpy(pa+page_offset, ba+b_offset, clen);
941 else
942 memcpy(ba+b_offset, pa+page_offset, clen);
943 __bio_kunmap_atomic(ba, KM_USER0);
944 }
945 if (clen < len) /* hit end of page */
946 break;
947 page_offset += len;
948 }
949}
950
951#define check_xor() do { \
952 if (count == MAX_XOR_BLOCKS) { \
953 xor_block(count, STRIPE_SIZE, ptr); \
954 count = 1; \
955 } \
956 } while(0)
957
958
959static void compute_block(struct stripe_head *sh, int dd_idx)
960{
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800961 int i, count, disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 void *ptr[MAX_XOR_BLOCKS], *p;
963
964 PRINTK("compute_block, stripe %llu, idx %d\n",
965 (unsigned long long)sh->sector, dd_idx);
966
967 ptr[0] = page_address(sh->dev[dd_idx].page);
968 memset(ptr[0], 0, STRIPE_SIZE);
969 count = 1;
970 for (i = disks ; i--; ) {
971 if (i == dd_idx)
972 continue;
973 p = page_address(sh->dev[i].page);
974 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
975 ptr[count++] = p;
976 else
NeilBrown14f8d262006-01-06 00:20:14 -0800977 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 " not present\n", dd_idx,
979 (unsigned long long)sh->sector, i);
980
981 check_xor();
982 }
983 if (count != 1)
984 xor_block(count, STRIPE_SIZE, ptr);
985 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
986}
987
NeilBrown16a53ec2006-06-26 00:27:38 -0700988static void compute_parity5(struct stripe_head *sh, int method)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800991 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 void *ptr[MAX_XOR_BLOCKS];
993 struct bio *chosen;
994
NeilBrown16a53ec2006-06-26 00:27:38 -0700995 PRINTK("compute_parity5, stripe %llu, method %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 (unsigned long long)sh->sector, method);
997
998 count = 1;
999 ptr[0] = page_address(sh->dev[pd_idx].page);
1000 switch(method) {
1001 case READ_MODIFY_WRITE:
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001002 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 for (i=disks ; i-- ;) {
1004 if (i==pd_idx)
1005 continue;
1006 if (sh->dev[i].towrite &&
1007 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1008 ptr[count++] = page_address(sh->dev[i].page);
1009 chosen = sh->dev[i].towrite;
1010 sh->dev[i].towrite = NULL;
1011
1012 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1013 wake_up(&conf->wait_for_overlap);
1014
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001015 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 sh->dev[i].written = chosen;
1017 check_xor();
1018 }
1019 }
1020 break;
1021 case RECONSTRUCT_WRITE:
1022 memset(ptr[0], 0, STRIPE_SIZE);
1023 for (i= disks; i-- ;)
1024 if (i!=pd_idx && sh->dev[i].towrite) {
1025 chosen = sh->dev[i].towrite;
1026 sh->dev[i].towrite = NULL;
1027
1028 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1029 wake_up(&conf->wait_for_overlap);
1030
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001031 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sh->dev[i].written = chosen;
1033 }
1034 break;
1035 case CHECK_PARITY:
1036 break;
1037 }
1038 if (count>1) {
1039 xor_block(count, STRIPE_SIZE, ptr);
1040 count = 1;
1041 }
1042
1043 for (i = disks; i--;)
1044 if (sh->dev[i].written) {
1045 sector_t sector = sh->dev[i].sector;
1046 struct bio *wbi = sh->dev[i].written;
1047 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1048 copy_data(1, wbi, sh->dev[i].page, sector);
1049 wbi = r5_next_bio(wbi, sector);
1050 }
1051
1052 set_bit(R5_LOCKED, &sh->dev[i].flags);
1053 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1054 }
1055
1056 switch(method) {
1057 case RECONSTRUCT_WRITE:
1058 case CHECK_PARITY:
1059 for (i=disks; i--;)
1060 if (i != pd_idx) {
1061 ptr[count++] = page_address(sh->dev[i].page);
1062 check_xor();
1063 }
1064 break;
1065 case READ_MODIFY_WRITE:
1066 for (i = disks; i--;)
1067 if (sh->dev[i].written) {
1068 ptr[count++] = page_address(sh->dev[i].page);
1069 check_xor();
1070 }
1071 }
1072 if (count != 1)
1073 xor_block(count, STRIPE_SIZE, ptr);
1074
1075 if (method != CHECK_PARITY) {
1076 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1077 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1078 } else
1079 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1080}
1081
NeilBrown16a53ec2006-06-26 00:27:38 -07001082static void compute_parity6(struct stripe_head *sh, int method)
1083{
1084 raid6_conf_t *conf = sh->raid_conf;
1085 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
1086 struct bio *chosen;
1087 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1088 void *ptrs[disks];
1089
1090 qd_idx = raid6_next_disk(pd_idx, disks);
1091 d0_idx = raid6_next_disk(qd_idx, disks);
1092
1093 PRINTK("compute_parity, stripe %llu, method %d\n",
1094 (unsigned long long)sh->sector, method);
1095
1096 switch(method) {
1097 case READ_MODIFY_WRITE:
1098 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1099 case RECONSTRUCT_WRITE:
1100 for (i= disks; i-- ;)
1101 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1102 chosen = sh->dev[i].towrite;
1103 sh->dev[i].towrite = NULL;
1104
1105 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1106 wake_up(&conf->wait_for_overlap);
1107
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001108 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001109 sh->dev[i].written = chosen;
1110 }
1111 break;
1112 case CHECK_PARITY:
1113 BUG(); /* Not implemented yet */
1114 }
1115
1116 for (i = disks; i--;)
1117 if (sh->dev[i].written) {
1118 sector_t sector = sh->dev[i].sector;
1119 struct bio *wbi = sh->dev[i].written;
1120 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1121 copy_data(1, wbi, sh->dev[i].page, sector);
1122 wbi = r5_next_bio(wbi, sector);
1123 }
1124
1125 set_bit(R5_LOCKED, &sh->dev[i].flags);
1126 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1127 }
1128
1129// switch(method) {
1130// case RECONSTRUCT_WRITE:
1131// case CHECK_PARITY:
1132// case UPDATE_PARITY:
1133 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1134 /* FIX: Is this ordering of drives even remotely optimal? */
1135 count = 0;
1136 i = d0_idx;
1137 do {
1138 ptrs[count++] = page_address(sh->dev[i].page);
1139 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1140 printk("block %d/%d not uptodate on parity calc\n", i,count);
1141 i = raid6_next_disk(i, disks);
1142 } while ( i != d0_idx );
1143// break;
1144// }
1145
1146 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1147
1148 switch(method) {
1149 case RECONSTRUCT_WRITE:
1150 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1151 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1152 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1153 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1154 break;
1155 case UPDATE_PARITY:
1156 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1157 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1158 break;
1159 }
1160}
1161
1162
1163/* Compute one missing block */
1164static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1165{
1166 raid6_conf_t *conf = sh->raid_conf;
1167 int i, count, disks = conf->raid_disks;
1168 void *ptr[MAX_XOR_BLOCKS], *p;
1169 int pd_idx = sh->pd_idx;
1170 int qd_idx = raid6_next_disk(pd_idx, disks);
1171
1172 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1173 (unsigned long long)sh->sector, dd_idx);
1174
1175 if ( dd_idx == qd_idx ) {
1176 /* We're actually computing the Q drive */
1177 compute_parity6(sh, UPDATE_PARITY);
1178 } else {
1179 ptr[0] = page_address(sh->dev[dd_idx].page);
1180 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1181 count = 1;
1182 for (i = disks ; i--; ) {
1183 if (i == dd_idx || i == qd_idx)
1184 continue;
1185 p = page_address(sh->dev[i].page);
1186 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1187 ptr[count++] = p;
1188 else
1189 printk("compute_block() %d, stripe %llu, %d"
1190 " not present\n", dd_idx,
1191 (unsigned long long)sh->sector, i);
1192
1193 check_xor();
1194 }
1195 if (count != 1)
1196 xor_block(count, STRIPE_SIZE, ptr);
1197 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1198 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1199 }
1200}
1201
1202/* Compute two missing blocks */
1203static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1204{
1205 raid6_conf_t *conf = sh->raid_conf;
1206 int i, count, disks = conf->raid_disks;
1207 int pd_idx = sh->pd_idx;
1208 int qd_idx = raid6_next_disk(pd_idx, disks);
1209 int d0_idx = raid6_next_disk(qd_idx, disks);
1210 int faila, failb;
1211
1212 /* faila and failb are disk numbers relative to d0_idx */
1213 /* pd_idx become disks-2 and qd_idx become disks-1 */
1214 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1215 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1216
1217 BUG_ON(faila == failb);
1218 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1219
1220 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1221 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1222
1223 if ( failb == disks-1 ) {
1224 /* Q disk is one of the missing disks */
1225 if ( faila == disks-2 ) {
1226 /* Missing P+Q, just recompute */
1227 compute_parity6(sh, UPDATE_PARITY);
1228 return;
1229 } else {
1230 /* We're missing D+Q; recompute D from P */
1231 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1232 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1233 return;
1234 }
1235 }
1236
1237 /* We're missing D+P or D+D; build pointer table */
1238 {
1239 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1240 void *ptrs[disks];
1241
1242 count = 0;
1243 i = d0_idx;
1244 do {
1245 ptrs[count++] = page_address(sh->dev[i].page);
1246 i = raid6_next_disk(i, disks);
1247 if (i != dd_idx1 && i != dd_idx2 &&
1248 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1249 printk("compute_2 with missing block %d/%d\n", count, i);
1250 } while ( i != d0_idx );
1251
1252 if ( failb == disks-2 ) {
1253 /* We're missing D+P. */
1254 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1255 } else {
1256 /* We're missing D+D. */
1257 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1258 }
1259
1260 /* Both the above update both missing blocks */
1261 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1262 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1263 }
1264}
1265
1266
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/*
1269 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001270 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * The bi_next chain must be in order.
1272 */
1273static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1274{
1275 struct bio **bip;
1276 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001277 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1280 (unsigned long long)bi->bi_sector,
1281 (unsigned long long)sh->sector);
1282
1283
1284 spin_lock(&sh->lock);
1285 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001286 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001288 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1289 firstwrite = 1;
1290 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 bip = &sh->dev[dd_idx].toread;
1292 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1293 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1294 goto overlap;
1295 bip = & (*bip)->bi_next;
1296 }
1297 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1298 goto overlap;
1299
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001300 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (*bip)
1302 bi->bi_next = *bip;
1303 *bip = bi;
1304 bi->bi_phys_segments ++;
1305 spin_unlock_irq(&conf->device_lock);
1306 spin_unlock(&sh->lock);
1307
1308 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1309 (unsigned long long)bi->bi_sector,
1310 (unsigned long long)sh->sector, dd_idx);
1311
NeilBrown72626682005-09-09 16:23:54 -07001312 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001313 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1314 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001315 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001316 set_bit(STRIPE_BIT_DELAY, &sh->state);
1317 }
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (forwrite) {
1320 /* check if page is covered */
1321 sector_t sector = sh->dev[dd_idx].sector;
1322 for (bi=sh->dev[dd_idx].towrite;
1323 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1324 bi && bi->bi_sector <= sector;
1325 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1326 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1327 sector = bi->bi_sector + (bi->bi_size>>9);
1328 }
1329 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1330 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1331 }
1332 return 1;
1333
1334 overlap:
1335 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1336 spin_unlock_irq(&conf->device_lock);
1337 spin_unlock(&sh->lock);
1338 return 0;
1339}
1340
NeilBrown29269552006-03-27 01:18:10 -08001341static void end_reshape(raid5_conf_t *conf);
1342
NeilBrown16a53ec2006-06-26 00:27:38 -07001343static int page_is_zero(struct page *p)
1344{
1345 char *a = page_address(p);
1346 return ((*(u32*)a) == 0 &&
1347 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1348}
1349
NeilBrownccfcc3c2006-03-27 01:18:09 -08001350static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1351{
1352 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001353 int pd_idx, dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001354 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1355
NeilBrownccfcc3c2006-03-27 01:18:09 -08001356 raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
1357 + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
1358 return pd_idx;
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362/*
1363 * handle_stripe - do things to a stripe.
1364 *
1365 * We lock the stripe and then examine the state of various bits
1366 * to see what needs to be done.
1367 * Possible results:
1368 * return some read request which now have data
1369 * return some write requests which are safely on disc
1370 * schedule a read on some buffers
1371 * schedule a write of some buffers
1372 * return confirmation of parity correctness
1373 *
1374 * Parity calculations are done inside the stripe lock
1375 * buffers are taken off read_list or write_list, and bh_cache buffers
1376 * get BH_Lock set before the stripe lock is released.
1377 *
1378 */
1379
NeilBrown16a53ec2006-06-26 00:27:38 -07001380static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001383 int disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct bio *return_bi= NULL;
1385 struct bio *bi;
1386 int i;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001387 int syncing, expanding, expanded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1389 int non_overwrite = 0;
1390 int failed_num=0;
1391 struct r5dev *dev;
1392
1393 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1394 (unsigned long long)sh->sector, atomic_read(&sh->count),
1395 sh->pd_idx);
1396
1397 spin_lock(&sh->lock);
1398 clear_bit(STRIPE_HANDLE, &sh->state);
1399 clear_bit(STRIPE_DELAYED, &sh->state);
1400
1401 syncing = test_bit(STRIPE_SYNCING, &sh->state);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001402 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1403 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /* Now to look around and see what can be done */
1405
NeilBrown9910f162006-01-06 00:20:24 -08001406 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 for (i=disks; i--; ) {
1408 mdk_rdev_t *rdev;
1409 dev = &sh->dev[i];
1410 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1413 i, dev->flags, dev->toread, dev->towrite, dev->written);
1414 /* maybe we can reply to a read */
1415 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1416 struct bio *rbi, *rbi2;
1417 PRINTK("Return read for disc %d\n", i);
1418 spin_lock_irq(&conf->device_lock);
1419 rbi = dev->toread;
1420 dev->toread = NULL;
1421 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1422 wake_up(&conf->wait_for_overlap);
1423 spin_unlock_irq(&conf->device_lock);
1424 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1425 copy_data(0, rbi, dev->page, dev->sector);
1426 rbi2 = r5_next_bio(rbi, dev->sector);
1427 spin_lock_irq(&conf->device_lock);
1428 if (--rbi->bi_phys_segments == 0) {
1429 rbi->bi_next = return_bi;
1430 return_bi = rbi;
1431 }
1432 spin_unlock_irq(&conf->device_lock);
1433 rbi = rbi2;
1434 }
1435 }
1436
1437 /* now count some things */
1438 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1439 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1440
1441
1442 if (dev->toread) to_read++;
1443 if (dev->towrite) {
1444 to_write++;
1445 if (!test_bit(R5_OVERWRITE, &dev->flags))
1446 non_overwrite++;
1447 }
1448 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001449 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001450 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001451 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08001452 clear_bit(R5_ReadError, &dev->flags);
1453 clear_bit(R5_ReWrite, &dev->flags);
1454 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001455 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001456 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 failed++;
1458 failed_num = i;
1459 } else
1460 set_bit(R5_Insync, &dev->flags);
1461 }
NeilBrown9910f162006-01-06 00:20:24 -08001462 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 PRINTK("locked=%d uptodate=%d to_read=%d"
1464 " to_write=%d failed=%d failed_num=%d\n",
1465 locked, uptodate, to_read, to_write, failed, failed_num);
1466 /* check if the array has lost two devices and, if so, some requests might
1467 * need to be failed
1468 */
1469 if (failed > 1 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 for (i=disks; i--; ) {
NeilBrown72626682005-09-09 16:23:54 -07001471 int bitmap_end = 0;
NeilBrown4e5314b2005-11-08 21:39:22 -08001472
1473 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001474 mdk_rdev_t *rdev;
1475 rcu_read_lock();
1476 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001477 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001478 /* multiple read failures in one stripe */
1479 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001480 rcu_read_unlock();
NeilBrown4e5314b2005-11-08 21:39:22 -08001481 }
1482
NeilBrown72626682005-09-09 16:23:54 -07001483 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* fail all writes first */
1485 bi = sh->dev[i].towrite;
1486 sh->dev[i].towrite = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001487 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1490 wake_up(&conf->wait_for_overlap);
1491
1492 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1493 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1494 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1495 if (--bi->bi_phys_segments == 0) {
1496 md_write_end(conf->mddev);
1497 bi->bi_next = return_bi;
1498 return_bi = bi;
1499 }
1500 bi = nextbi;
1501 }
1502 /* and fail all 'written' */
1503 bi = sh->dev[i].written;
1504 sh->dev[i].written = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001505 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1507 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1508 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1509 if (--bi->bi_phys_segments == 0) {
1510 md_write_end(conf->mddev);
1511 bi->bi_next = return_bi;
1512 return_bi = bi;
1513 }
1514 bi = bi2;
1515 }
1516
1517 /* fail any reads if this device is non-operational */
NeilBrown4e5314b2005-11-08 21:39:22 -08001518 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1519 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 bi = sh->dev[i].toread;
1521 sh->dev[i].toread = NULL;
1522 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1523 wake_up(&conf->wait_for_overlap);
1524 if (bi) to_read--;
1525 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1526 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1527 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1528 if (--bi->bi_phys_segments == 0) {
1529 bi->bi_next = return_bi;
1530 return_bi = bi;
1531 }
1532 bi = nextbi;
1533 }
1534 }
NeilBrown72626682005-09-09 16:23:54 -07001535 spin_unlock_irq(&conf->device_lock);
1536 if (bitmap_end)
1537 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1538 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541 if (failed > 1 && syncing) {
1542 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1543 clear_bit(STRIPE_SYNCING, &sh->state);
1544 syncing = 0;
1545 }
1546
1547 /* might be able to return some write requests if the parity block
1548 * is safe, or on a failed drive
1549 */
1550 dev = &sh->dev[sh->pd_idx];
1551 if ( written &&
1552 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1553 test_bit(R5_UPTODATE, &dev->flags))
1554 || (failed == 1 && failed_num == sh->pd_idx))
1555 ) {
1556 /* any written block on an uptodate or failed drive can be returned.
1557 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1558 * never LOCKED, so we don't need to test 'failed' directly.
1559 */
1560 for (i=disks; i--; )
1561 if (sh->dev[i].written) {
1562 dev = &sh->dev[i];
1563 if (!test_bit(R5_LOCKED, &dev->flags) &&
1564 test_bit(R5_UPTODATE, &dev->flags) ) {
1565 /* We can return any write requests */
1566 struct bio *wbi, *wbi2;
NeilBrown72626682005-09-09 16:23:54 -07001567 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 PRINTK("Return write for disc %d\n", i);
1569 spin_lock_irq(&conf->device_lock);
1570 wbi = dev->written;
1571 dev->written = NULL;
1572 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1573 wbi2 = r5_next_bio(wbi, dev->sector);
1574 if (--wbi->bi_phys_segments == 0) {
1575 md_write_end(conf->mddev);
1576 wbi->bi_next = return_bi;
1577 return_bi = wbi;
1578 }
1579 wbi = wbi2;
1580 }
NeilBrown72626682005-09-09 16:23:54 -07001581 if (dev->towrite == NULL)
1582 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001584 if (bitmap_end)
1585 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1586 STRIPE_SECTORS,
1587 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 }
1590 }
1591
1592 /* Now we might consider reading some blocks, either to check/generate
1593 * parity, or to satisfy requests
1594 * or to load a block that is being partially written.
1595 */
NeilBrownccfcc3c2006-03-27 01:18:09 -08001596 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 for (i=disks; i--;) {
1598 dev = &sh->dev[i];
1599 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1600 (dev->toread ||
1601 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1602 syncing ||
NeilBrownccfcc3c2006-03-27 01:18:09 -08001603 expanding ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 (failed && (sh->dev[failed_num].toread ||
1605 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1606 )
1607 ) {
1608 /* we would like to get this block, possibly
1609 * by computing it, but we might not be able to
1610 */
1611 if (uptodate == disks-1) {
1612 PRINTK("Computing block %d\n", i);
1613 compute_block(sh, i);
1614 uptodate++;
1615 } else if (test_bit(R5_Insync, &dev->flags)) {
1616 set_bit(R5_LOCKED, &dev->flags);
1617 set_bit(R5_Wantread, &dev->flags);
1618#if 0
1619 /* if I am just reading this block and we don't have
1620 a failed drive, or any pending writes then sidestep the cache */
1621 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1622 ! syncing && !failed && !to_write) {
1623 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1624 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1625 }
1626#endif
1627 locked++;
1628 PRINTK("Reading block %d (sync=%d)\n",
1629 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 }
1632 }
1633 set_bit(STRIPE_HANDLE, &sh->state);
1634 }
1635
1636 /* now to consider writing and what else, if anything should be read */
1637 if (to_write) {
1638 int rmw=0, rcw=0;
1639 for (i=disks ; i--;) {
1640 /* would I have to read this buffer for read_modify_write */
1641 dev = &sh->dev[i];
1642 if ((dev->towrite || i == sh->pd_idx) &&
1643 (!test_bit(R5_LOCKED, &dev->flags)
1644#if 0
1645|| sh->bh_page[i]!=bh->b_page
1646#endif
1647 ) &&
1648 !test_bit(R5_UPTODATE, &dev->flags)) {
1649 if (test_bit(R5_Insync, &dev->flags)
1650/* && !(!mddev->insync && i == sh->pd_idx) */
1651 )
1652 rmw++;
1653 else rmw += 2*disks; /* cannot read it */
1654 }
1655 /* Would I have to read this buffer for reconstruct_write */
1656 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1657 (!test_bit(R5_LOCKED, &dev->flags)
1658#if 0
1659|| sh->bh_page[i] != bh->b_page
1660#endif
1661 ) &&
1662 !test_bit(R5_UPTODATE, &dev->flags)) {
1663 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1664 else rcw += 2*disks;
1665 }
1666 }
1667 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1668 (unsigned long long)sh->sector, rmw, rcw);
1669 set_bit(STRIPE_HANDLE, &sh->state);
1670 if (rmw < rcw && rmw > 0)
1671 /* prefer read-modify-write, but need to get some data */
1672 for (i=disks; i--;) {
1673 dev = &sh->dev[i];
1674 if ((dev->towrite || i == sh->pd_idx) &&
1675 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1676 test_bit(R5_Insync, &dev->flags)) {
1677 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1678 {
1679 PRINTK("Read_old block %d for r-m-w\n", i);
1680 set_bit(R5_LOCKED, &dev->flags);
1681 set_bit(R5_Wantread, &dev->flags);
1682 locked++;
1683 } else {
1684 set_bit(STRIPE_DELAYED, &sh->state);
1685 set_bit(STRIPE_HANDLE, &sh->state);
1686 }
1687 }
1688 }
1689 if (rcw <= rmw && rcw > 0)
1690 /* want reconstruct write, but need to get some data */
1691 for (i=disks; i--;) {
1692 dev = &sh->dev[i];
1693 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1694 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1695 test_bit(R5_Insync, &dev->flags)) {
1696 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1697 {
1698 PRINTK("Read_old block %d for Reconstruct\n", i);
1699 set_bit(R5_LOCKED, &dev->flags);
1700 set_bit(R5_Wantread, &dev->flags);
1701 locked++;
1702 } else {
1703 set_bit(STRIPE_DELAYED, &sh->state);
1704 set_bit(STRIPE_HANDLE, &sh->state);
1705 }
1706 }
1707 }
1708 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown72626682005-09-09 16:23:54 -07001709 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1710 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 PRINTK("Computing parity...\n");
NeilBrown16a53ec2006-06-26 00:27:38 -07001712 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /* now every locked buffer is ready to be written */
1714 for (i=disks; i--;)
1715 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1716 PRINTK("Writing block %d\n", i);
1717 locked++;
1718 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1719 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1720 || (i==sh->pd_idx && failed == 0))
1721 set_bit(STRIPE_INSYNC, &sh->state);
1722 }
1723 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1724 atomic_dec(&conf->preread_active_stripes);
1725 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1726 md_wakeup_thread(conf->mddev->thread);
1727 }
1728 }
1729 }
1730
1731 /* maybe we need to check and possibly fix the parity for this stripe
1732 * Any reads will already have been scheduled, so we just see if enough data
1733 * is available
1734 */
1735 if (syncing && locked == 0 &&
NeilBrown14f8d262006-01-06 00:20:14 -08001736 !test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 set_bit(STRIPE_HANDLE, &sh->state);
1738 if (failed == 0) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001739 BUG_ON(uptodate != disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001740 compute_parity5(sh, CHECK_PARITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 uptodate--;
NeilBrown16a53ec2006-06-26 00:27:38 -07001742 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* parity is correct (on disc, not in buffer any more) */
1744 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown9d888832005-11-08 21:39:26 -08001745 } else {
1746 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1747 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1748 /* don't try to repair!! */
1749 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown14f8d262006-01-06 00:20:14 -08001750 else {
1751 compute_block(sh, sh->pd_idx);
1752 uptodate++;
1753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 }
1756 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001757 /* either failed parity check, or recovery is happening */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (failed==0)
1759 failed_num = sh->pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 dev = &sh->dev[failed_num];
NeilBrown14f8d262006-01-06 00:20:14 -08001761 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1762 BUG_ON(uptodate != disks);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 set_bit(R5_LOCKED, &dev->flags);
1765 set_bit(R5_Wantwrite, &dev->flags);
NeilBrown72626682005-09-09 16:23:54 -07001766 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 locked++;
1768 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770 }
1771 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1772 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1773 clear_bit(STRIPE_SYNCING, &sh->state);
1774 }
NeilBrown4e5314b2005-11-08 21:39:22 -08001775
1776 /* If the failed drive is just a ReadError, then we might need to progress
1777 * the repair/check process
1778 */
NeilBrownba22dcb2005-11-08 21:39:31 -08001779 if (failed == 1 && ! conf->mddev->ro &&
1780 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001781 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1782 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1783 ) {
1784 dev = &sh->dev[failed_num];
1785 if (!test_bit(R5_ReWrite, &dev->flags)) {
1786 set_bit(R5_Wantwrite, &dev->flags);
1787 set_bit(R5_ReWrite, &dev->flags);
1788 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001789 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001790 } else {
1791 /* let's read it back */
1792 set_bit(R5_Wantread, &dev->flags);
1793 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001794 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001795 }
1796 }
1797
NeilBrownccfcc3c2006-03-27 01:18:09 -08001798 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1799 /* Need to write out all blocks after computing parity */
1800 sh->disks = conf->raid_disks;
1801 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001802 compute_parity5(sh, RECONSTRUCT_WRITE);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001803 for (i= conf->raid_disks; i--;) {
1804 set_bit(R5_LOCKED, &sh->dev[i].flags);
1805 locked++;
1806 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1807 }
1808 clear_bit(STRIPE_EXPANDING, &sh->state);
1809 } else if (expanded) {
1810 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08001811 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001812 wake_up(&conf->wait_for_overlap);
1813 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1814 }
1815
1816 if (expanding && locked == 0) {
1817 /* We have read all the blocks in this stripe and now we need to
1818 * copy some of them into a target stripe for expand.
1819 */
1820 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1821 for (i=0; i< sh->disks; i++)
1822 if (i != sh->pd_idx) {
1823 int dd_idx, pd_idx, j;
1824 struct stripe_head *sh2;
1825
1826 sector_t bn = compute_blocknr(sh, i);
1827 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1828 conf->raid_disks-1,
1829 &dd_idx, &pd_idx, conf);
1830 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1831 if (sh2 == NULL)
1832 /* so far only the early blocks of this stripe
1833 * have been requested. When later blocks
1834 * get requested, we will try again
1835 */
1836 continue;
1837 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1838 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1839 /* must have already done this block */
1840 release_stripe(sh2);
1841 continue;
1842 }
1843 memcpy(page_address(sh2->dev[dd_idx].page),
1844 page_address(sh->dev[i].page),
1845 STRIPE_SIZE);
1846 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1847 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1848 for (j=0; j<conf->raid_disks; j++)
1849 if (j != sh2->pd_idx &&
1850 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1851 break;
1852 if (j == conf->raid_disks) {
1853 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1854 set_bit(STRIPE_HANDLE, &sh2->state);
1855 }
1856 release_stripe(sh2);
1857 }
1858 }
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 spin_unlock(&sh->lock);
1861
1862 while ((bi=return_bi)) {
1863 int bytes = bi->bi_size;
1864
1865 return_bi = bi->bi_next;
1866 bi->bi_next = NULL;
1867 bi->bi_size = 0;
1868 bi->bi_end_io(bi, bytes, 0);
1869 }
1870 for (i=disks; i-- ;) {
1871 int rw;
1872 struct bio *bi;
1873 mdk_rdev_t *rdev;
1874 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1875 rw = 1;
1876 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1877 rw = 0;
1878 else
1879 continue;
1880
1881 bi = &sh->dev[i].req;
1882
1883 bi->bi_rw = rw;
1884 if (rw)
1885 bi->bi_end_io = raid5_end_write_request;
1886 else
1887 bi->bi_end_io = raid5_end_read_request;
1888
1889 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001890 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001891 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 rdev = NULL;
1893 if (rdev)
1894 atomic_inc(&rdev->nr_pending);
1895 rcu_read_unlock();
1896
1897 if (rdev) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08001898 if (syncing || expanding || expanded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1900
1901 bi->bi_bdev = rdev->bdev;
1902 PRINTK("for %llu schedule op %ld on disc %d\n",
1903 (unsigned long long)sh->sector, bi->bi_rw, i);
1904 atomic_inc(&sh->count);
1905 bi->bi_sector = sh->sector + rdev->data_offset;
1906 bi->bi_flags = 1 << BIO_UPTODATE;
1907 bi->bi_vcnt = 1;
1908 bi->bi_max_vecs = 1;
1909 bi->bi_idx = 0;
1910 bi->bi_io_vec = &sh->dev[i].vec;
1911 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1912 bi->bi_io_vec[0].bv_offset = 0;
1913 bi->bi_size = STRIPE_SIZE;
1914 bi->bi_next = NULL;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001915 if (rw == WRITE &&
1916 test_bit(R5_ReWrite, &sh->dev[i].flags))
1917 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 generic_make_request(bi);
1919 } else {
NeilBrown72626682005-09-09 16:23:54 -07001920 if (rw == 1)
1921 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 PRINTK("skip op %ld on disc %d for sector %llu\n",
1923 bi->bi_rw, i, (unsigned long long)sh->sector);
1924 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1925 set_bit(STRIPE_HANDLE, &sh->state);
1926 }
1927 }
1928}
1929
NeilBrown16a53ec2006-06-26 00:27:38 -07001930static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1931{
1932 raid6_conf_t *conf = sh->raid_conf;
1933 int disks = conf->raid_disks;
1934 struct bio *return_bi= NULL;
1935 struct bio *bi;
1936 int i;
1937 int syncing;
1938 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1939 int non_overwrite = 0;
1940 int failed_num[2] = {0, 0};
1941 struct r5dev *dev, *pdev, *qdev;
1942 int pd_idx = sh->pd_idx;
1943 int qd_idx = raid6_next_disk(pd_idx, disks);
1944 int p_failed, q_failed;
1945
1946 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1947 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1948 pd_idx, qd_idx);
1949
1950 spin_lock(&sh->lock);
1951 clear_bit(STRIPE_HANDLE, &sh->state);
1952 clear_bit(STRIPE_DELAYED, &sh->state);
1953
1954 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1955 /* Now to look around and see what can be done */
1956
1957 rcu_read_lock();
1958 for (i=disks; i--; ) {
1959 mdk_rdev_t *rdev;
1960 dev = &sh->dev[i];
1961 clear_bit(R5_Insync, &dev->flags);
1962
1963 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1964 i, dev->flags, dev->toread, dev->towrite, dev->written);
1965 /* maybe we can reply to a read */
1966 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1967 struct bio *rbi, *rbi2;
1968 PRINTK("Return read for disc %d\n", i);
1969 spin_lock_irq(&conf->device_lock);
1970 rbi = dev->toread;
1971 dev->toread = NULL;
1972 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1973 wake_up(&conf->wait_for_overlap);
1974 spin_unlock_irq(&conf->device_lock);
1975 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1976 copy_data(0, rbi, dev->page, dev->sector);
1977 rbi2 = r5_next_bio(rbi, dev->sector);
1978 spin_lock_irq(&conf->device_lock);
1979 if (--rbi->bi_phys_segments == 0) {
1980 rbi->bi_next = return_bi;
1981 return_bi = rbi;
1982 }
1983 spin_unlock_irq(&conf->device_lock);
1984 rbi = rbi2;
1985 }
1986 }
1987
1988 /* now count some things */
1989 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1990 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1991
1992
1993 if (dev->toread) to_read++;
1994 if (dev->towrite) {
1995 to_write++;
1996 if (!test_bit(R5_OVERWRITE, &dev->flags))
1997 non_overwrite++;
1998 }
1999 if (dev->written) written++;
2000 rdev = rcu_dereference(conf->disks[i].rdev);
2001 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2002 /* The ReadError flag will just be confusing now */
2003 clear_bit(R5_ReadError, &dev->flags);
2004 clear_bit(R5_ReWrite, &dev->flags);
2005 }
2006 if (!rdev || !test_bit(In_sync, &rdev->flags)
2007 || test_bit(R5_ReadError, &dev->flags)) {
2008 if ( failed < 2 )
2009 failed_num[failed] = i;
2010 failed++;
2011 } else
2012 set_bit(R5_Insync, &dev->flags);
2013 }
2014 rcu_read_unlock();
2015 PRINTK("locked=%d uptodate=%d to_read=%d"
2016 " to_write=%d failed=%d failed_num=%d,%d\n",
2017 locked, uptodate, to_read, to_write, failed,
2018 failed_num[0], failed_num[1]);
2019 /* check if the array has lost >2 devices and, if so, some requests might
2020 * need to be failed
2021 */
2022 if (failed > 2 && to_read+to_write+written) {
2023 for (i=disks; i--; ) {
2024 int bitmap_end = 0;
2025
2026 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2027 mdk_rdev_t *rdev;
2028 rcu_read_lock();
2029 rdev = rcu_dereference(conf->disks[i].rdev);
2030 if (rdev && test_bit(In_sync, &rdev->flags))
2031 /* multiple read failures in one stripe */
2032 md_error(conf->mddev, rdev);
2033 rcu_read_unlock();
2034 }
2035
2036 spin_lock_irq(&conf->device_lock);
2037 /* fail all writes first */
2038 bi = sh->dev[i].towrite;
2039 sh->dev[i].towrite = NULL;
2040 if (bi) { to_write--; bitmap_end = 1; }
2041
2042 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2043 wake_up(&conf->wait_for_overlap);
2044
2045 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2046 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2047 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2048 if (--bi->bi_phys_segments == 0) {
2049 md_write_end(conf->mddev);
2050 bi->bi_next = return_bi;
2051 return_bi = bi;
2052 }
2053 bi = nextbi;
2054 }
2055 /* and fail all 'written' */
2056 bi = sh->dev[i].written;
2057 sh->dev[i].written = NULL;
2058 if (bi) bitmap_end = 1;
2059 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2060 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2061 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2062 if (--bi->bi_phys_segments == 0) {
2063 md_write_end(conf->mddev);
2064 bi->bi_next = return_bi;
2065 return_bi = bi;
2066 }
2067 bi = bi2;
2068 }
2069
2070 /* fail any reads if this device is non-operational */
2071 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2072 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2073 bi = sh->dev[i].toread;
2074 sh->dev[i].toread = NULL;
2075 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2076 wake_up(&conf->wait_for_overlap);
2077 if (bi) to_read--;
2078 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2079 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2080 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2081 if (--bi->bi_phys_segments == 0) {
2082 bi->bi_next = return_bi;
2083 return_bi = bi;
2084 }
2085 bi = nextbi;
2086 }
2087 }
2088 spin_unlock_irq(&conf->device_lock);
2089 if (bitmap_end)
2090 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2091 STRIPE_SECTORS, 0, 0);
2092 }
2093 }
2094 if (failed > 2 && syncing) {
2095 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2096 clear_bit(STRIPE_SYNCING, &sh->state);
2097 syncing = 0;
2098 }
2099
2100 /*
2101 * might be able to return some write requests if the parity blocks
2102 * are safe, or on a failed drive
2103 */
2104 pdev = &sh->dev[pd_idx];
2105 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2106 || (failed >= 2 && failed_num[1] == pd_idx);
2107 qdev = &sh->dev[qd_idx];
2108 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2109 || (failed >= 2 && failed_num[1] == qd_idx);
2110
2111 if ( written &&
2112 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2113 && !test_bit(R5_LOCKED, &pdev->flags)
2114 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2115 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2116 && !test_bit(R5_LOCKED, &qdev->flags)
2117 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2118 /* any written block on an uptodate or failed drive can be
2119 * returned. Note that if we 'wrote' to a failed drive,
2120 * it will be UPTODATE, but never LOCKED, so we don't need
2121 * to test 'failed' directly.
2122 */
2123 for (i=disks; i--; )
2124 if (sh->dev[i].written) {
2125 dev = &sh->dev[i];
2126 if (!test_bit(R5_LOCKED, &dev->flags) &&
2127 test_bit(R5_UPTODATE, &dev->flags) ) {
2128 /* We can return any write requests */
2129 int bitmap_end = 0;
2130 struct bio *wbi, *wbi2;
2131 PRINTK("Return write for stripe %llu disc %d\n",
2132 (unsigned long long)sh->sector, i);
2133 spin_lock_irq(&conf->device_lock);
2134 wbi = dev->written;
2135 dev->written = NULL;
2136 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2137 wbi2 = r5_next_bio(wbi, dev->sector);
2138 if (--wbi->bi_phys_segments == 0) {
2139 md_write_end(conf->mddev);
2140 wbi->bi_next = return_bi;
2141 return_bi = wbi;
2142 }
2143 wbi = wbi2;
2144 }
2145 if (dev->towrite == NULL)
2146 bitmap_end = 1;
2147 spin_unlock_irq(&conf->device_lock);
2148 if (bitmap_end)
2149 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2150 STRIPE_SECTORS,
2151 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2152 }
2153 }
2154 }
2155
2156 /* Now we might consider reading some blocks, either to check/generate
2157 * parity, or to satisfy requests
2158 * or to load a block that is being partially written.
2159 */
2160 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
2161 for (i=disks; i--;) {
2162 dev = &sh->dev[i];
2163 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2164 (dev->toread ||
2165 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2166 syncing ||
2167 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2168 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2169 )
2170 ) {
2171 /* we would like to get this block, possibly
2172 * by computing it, but we might not be able to
2173 */
2174 if (uptodate == disks-1) {
2175 PRINTK("Computing stripe %llu block %d\n",
2176 (unsigned long long)sh->sector, i);
2177 compute_block_1(sh, i, 0);
2178 uptodate++;
2179 } else if ( uptodate == disks-2 && failed >= 2 ) {
2180 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2181 int other;
2182 for (other=disks; other--;) {
2183 if ( other == i )
2184 continue;
2185 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2186 break;
2187 }
2188 BUG_ON(other < 0);
2189 PRINTK("Computing stripe %llu blocks %d,%d\n",
2190 (unsigned long long)sh->sector, i, other);
2191 compute_block_2(sh, i, other);
2192 uptodate += 2;
2193 } else if (test_bit(R5_Insync, &dev->flags)) {
2194 set_bit(R5_LOCKED, &dev->flags);
2195 set_bit(R5_Wantread, &dev->flags);
2196#if 0
2197 /* if I am just reading this block and we don't have
2198 a failed drive, or any pending writes then sidestep the cache */
2199 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
2200 ! syncing && !failed && !to_write) {
2201 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
2202 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
2203 }
2204#endif
2205 locked++;
2206 PRINTK("Reading block %d (sync=%d)\n",
2207 i, syncing);
2208 }
2209 }
2210 }
2211 set_bit(STRIPE_HANDLE, &sh->state);
2212 }
2213
2214 /* now to consider writing and what else, if anything should be read */
2215 if (to_write) {
2216 int rcw=0, must_compute=0;
2217 for (i=disks ; i--;) {
2218 dev = &sh->dev[i];
2219 /* Would I have to read this buffer for reconstruct_write */
2220 if (!test_bit(R5_OVERWRITE, &dev->flags)
2221 && i != pd_idx && i != qd_idx
2222 && (!test_bit(R5_LOCKED, &dev->flags)
2223#if 0
2224 || sh->bh_page[i] != bh->b_page
2225#endif
2226 ) &&
2227 !test_bit(R5_UPTODATE, &dev->flags)) {
2228 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2229 else {
2230 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2231 must_compute++;
2232 }
2233 }
2234 }
2235 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2236 (unsigned long long)sh->sector, rcw, must_compute);
2237 set_bit(STRIPE_HANDLE, &sh->state);
2238
2239 if (rcw > 0)
2240 /* want reconstruct write, but need to get some data */
2241 for (i=disks; i--;) {
2242 dev = &sh->dev[i];
2243 if (!test_bit(R5_OVERWRITE, &dev->flags)
2244 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2245 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2246 test_bit(R5_Insync, &dev->flags)) {
2247 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2248 {
2249 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2250 (unsigned long long)sh->sector, i);
2251 set_bit(R5_LOCKED, &dev->flags);
2252 set_bit(R5_Wantread, &dev->flags);
2253 locked++;
2254 } else {
2255 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2256 (unsigned long long)sh->sector, i);
2257 set_bit(STRIPE_DELAYED, &sh->state);
2258 set_bit(STRIPE_HANDLE, &sh->state);
2259 }
2260 }
2261 }
2262 /* now if nothing is locked, and if we have enough data, we can start a write request */
2263 if (locked == 0 && rcw == 0 &&
2264 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2265 if ( must_compute > 0 ) {
2266 /* We have failed blocks and need to compute them */
2267 switch ( failed ) {
2268 case 0: BUG();
2269 case 1: compute_block_1(sh, failed_num[0], 0); break;
2270 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2271 default: BUG(); /* This request should have been failed? */
2272 }
2273 }
2274
2275 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2276 compute_parity6(sh, RECONSTRUCT_WRITE);
2277 /* now every locked buffer is ready to be written */
2278 for (i=disks; i--;)
2279 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2280 PRINTK("Writing stripe %llu block %d\n",
2281 (unsigned long long)sh->sector, i);
2282 locked++;
2283 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2284 }
2285 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2286 set_bit(STRIPE_INSYNC, &sh->state);
2287
2288 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2289 atomic_dec(&conf->preread_active_stripes);
2290 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2291 md_wakeup_thread(conf->mddev->thread);
2292 }
2293 }
2294 }
2295
2296 /* maybe we need to check and possibly fix the parity for this stripe
2297 * Any reads will already have been scheduled, so we just see if enough data
2298 * is available
2299 */
2300 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2301 int update_p = 0, update_q = 0;
2302 struct r5dev *dev;
2303
2304 set_bit(STRIPE_HANDLE, &sh->state);
2305
2306 BUG_ON(failed>2);
2307 BUG_ON(uptodate < disks);
2308 /* Want to check and possibly repair P and Q.
2309 * However there could be one 'failed' device, in which
2310 * case we can only check one of them, possibly using the
2311 * other to generate missing data
2312 */
2313
2314 /* If !tmp_page, we cannot do the calculations,
2315 * but as we have set STRIPE_HANDLE, we will soon be called
2316 * by stripe_handle with a tmp_page - just wait until then.
2317 */
2318 if (tmp_page) {
2319 if (failed == q_failed) {
2320 /* The only possible failed device holds 'Q', so it makes
2321 * sense to check P (If anything else were failed, we would
2322 * have used P to recreate it).
2323 */
2324 compute_block_1(sh, pd_idx, 1);
2325 if (!page_is_zero(sh->dev[pd_idx].page)) {
2326 compute_block_1(sh,pd_idx,0);
2327 update_p = 1;
2328 }
2329 }
2330 if (!q_failed && failed < 2) {
2331 /* q is not failed, and we didn't use it to generate
2332 * anything, so it makes sense to check it
2333 */
2334 memcpy(page_address(tmp_page),
2335 page_address(sh->dev[qd_idx].page),
2336 STRIPE_SIZE);
2337 compute_parity6(sh, UPDATE_PARITY);
2338 if (memcmp(page_address(tmp_page),
2339 page_address(sh->dev[qd_idx].page),
2340 STRIPE_SIZE)!= 0) {
2341 clear_bit(STRIPE_INSYNC, &sh->state);
2342 update_q = 1;
2343 }
2344 }
2345 if (update_p || update_q) {
2346 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2347 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2348 /* don't try to repair!! */
2349 update_p = update_q = 0;
2350 }
2351
2352 /* now write out any block on a failed drive,
2353 * or P or Q if they need it
2354 */
2355
2356 if (failed == 2) {
2357 dev = &sh->dev[failed_num[1]];
2358 locked++;
2359 set_bit(R5_LOCKED, &dev->flags);
2360 set_bit(R5_Wantwrite, &dev->flags);
2361 }
2362 if (failed >= 1) {
2363 dev = &sh->dev[failed_num[0]];
2364 locked++;
2365 set_bit(R5_LOCKED, &dev->flags);
2366 set_bit(R5_Wantwrite, &dev->flags);
2367 }
2368
2369 if (update_p) {
2370 dev = &sh->dev[pd_idx];
2371 locked ++;
2372 set_bit(R5_LOCKED, &dev->flags);
2373 set_bit(R5_Wantwrite, &dev->flags);
2374 }
2375 if (update_q) {
2376 dev = &sh->dev[qd_idx];
2377 locked++;
2378 set_bit(R5_LOCKED, &dev->flags);
2379 set_bit(R5_Wantwrite, &dev->flags);
2380 }
2381 clear_bit(STRIPE_DEGRADED, &sh->state);
2382
2383 set_bit(STRIPE_INSYNC, &sh->state);
2384 }
2385 }
2386
2387 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2388 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2389 clear_bit(STRIPE_SYNCING, &sh->state);
2390 }
2391
2392 /* If the failed drives are just a ReadError, then we might need
2393 * to progress the repair/check process
2394 */
2395 if (failed <= 2 && ! conf->mddev->ro)
2396 for (i=0; i<failed;i++) {
2397 dev = &sh->dev[failed_num[i]];
2398 if (test_bit(R5_ReadError, &dev->flags)
2399 && !test_bit(R5_LOCKED, &dev->flags)
2400 && test_bit(R5_UPTODATE, &dev->flags)
2401 ) {
2402 if (!test_bit(R5_ReWrite, &dev->flags)) {
2403 set_bit(R5_Wantwrite, &dev->flags);
2404 set_bit(R5_ReWrite, &dev->flags);
2405 set_bit(R5_LOCKED, &dev->flags);
2406 } else {
2407 /* let's read it back */
2408 set_bit(R5_Wantread, &dev->flags);
2409 set_bit(R5_LOCKED, &dev->flags);
2410 }
2411 }
2412 }
2413 spin_unlock(&sh->lock);
2414
2415 while ((bi=return_bi)) {
2416 int bytes = bi->bi_size;
2417
2418 return_bi = bi->bi_next;
2419 bi->bi_next = NULL;
2420 bi->bi_size = 0;
2421 bi->bi_end_io(bi, bytes, 0);
2422 }
2423 for (i=disks; i-- ;) {
2424 int rw;
2425 struct bio *bi;
2426 mdk_rdev_t *rdev;
2427 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
2428 rw = 1;
2429 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
2430 rw = 0;
2431 else
2432 continue;
2433
2434 bi = &sh->dev[i].req;
2435
2436 bi->bi_rw = rw;
2437 if (rw)
2438 bi->bi_end_io = raid5_end_write_request;
2439 else
2440 bi->bi_end_io = raid5_end_read_request;
2441
2442 rcu_read_lock();
2443 rdev = rcu_dereference(conf->disks[i].rdev);
2444 if (rdev && test_bit(Faulty, &rdev->flags))
2445 rdev = NULL;
2446 if (rdev)
2447 atomic_inc(&rdev->nr_pending);
2448 rcu_read_unlock();
2449
2450 if (rdev) {
2451 if (syncing)
2452 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2453
2454 bi->bi_bdev = rdev->bdev;
2455 PRINTK("for %llu schedule op %ld on disc %d\n",
2456 (unsigned long long)sh->sector, bi->bi_rw, i);
2457 atomic_inc(&sh->count);
2458 bi->bi_sector = sh->sector + rdev->data_offset;
2459 bi->bi_flags = 1 << BIO_UPTODATE;
2460 bi->bi_vcnt = 1;
2461 bi->bi_max_vecs = 1;
2462 bi->bi_idx = 0;
2463 bi->bi_io_vec = &sh->dev[i].vec;
2464 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2465 bi->bi_io_vec[0].bv_offset = 0;
2466 bi->bi_size = STRIPE_SIZE;
2467 bi->bi_next = NULL;
2468 if (rw == WRITE &&
2469 test_bit(R5_ReWrite, &sh->dev[i].flags))
2470 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2471 generic_make_request(bi);
2472 } else {
2473 if (rw == 1)
2474 set_bit(STRIPE_DEGRADED, &sh->state);
2475 PRINTK("skip op %ld on disc %d for sector %llu\n",
2476 bi->bi_rw, i, (unsigned long long)sh->sector);
2477 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2478 set_bit(STRIPE_HANDLE, &sh->state);
2479 }
2480 }
2481}
2482
2483static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2484{
2485 if (sh->raid_conf->level == 6)
2486 handle_stripe6(sh, tmp_page);
2487 else
2488 handle_stripe5(sh);
2489}
2490
2491
2492
Arjan van de Ven858119e2006-01-14 13:20:43 -08002493static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
2495 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2496 while (!list_empty(&conf->delayed_list)) {
2497 struct list_head *l = conf->delayed_list.next;
2498 struct stripe_head *sh;
2499 sh = list_entry(l, struct stripe_head, lru);
2500 list_del_init(l);
2501 clear_bit(STRIPE_DELAYED, &sh->state);
2502 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2503 atomic_inc(&conf->preread_active_stripes);
2504 list_add_tail(&sh->lru, &conf->handle_list);
2505 }
2506 }
2507}
2508
Arjan van de Ven858119e2006-01-14 13:20:43 -08002509static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07002510{
2511 /* device_lock is held */
2512 struct list_head head;
2513 list_add(&head, &conf->bitmap_list);
2514 list_del_init(&conf->bitmap_list);
2515 while (!list_empty(&head)) {
2516 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2517 list_del_init(&sh->lru);
2518 atomic_inc(&sh->count);
2519 __release_stripe(conf, sh);
2520 }
2521}
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523static void unplug_slaves(mddev_t *mddev)
2524{
2525 raid5_conf_t *conf = mddev_to_conf(mddev);
2526 int i;
2527
2528 rcu_read_lock();
2529 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002530 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002531 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2533
2534 atomic_inc(&rdev->nr_pending);
2535 rcu_read_unlock();
2536
2537 if (r_queue->unplug_fn)
2538 r_queue->unplug_fn(r_queue);
2539
2540 rdev_dec_pending(rdev, mddev);
2541 rcu_read_lock();
2542 }
2543 }
2544 rcu_read_unlock();
2545}
2546
2547static void raid5_unplug_device(request_queue_t *q)
2548{
2549 mddev_t *mddev = q->queuedata;
2550 raid5_conf_t *conf = mddev_to_conf(mddev);
2551 unsigned long flags;
2552
2553 spin_lock_irqsave(&conf->device_lock, flags);
2554
NeilBrown72626682005-09-09 16:23:54 -07002555 if (blk_remove_plug(q)) {
2556 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07002558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 md_wakeup_thread(mddev->thread);
2560
2561 spin_unlock_irqrestore(&conf->device_lock, flags);
2562
2563 unplug_slaves(mddev);
2564}
2565
2566static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2567 sector_t *error_sector)
2568{
2569 mddev_t *mddev = q->queuedata;
2570 raid5_conf_t *conf = mddev_to_conf(mddev);
2571 int i, ret = 0;
2572
2573 rcu_read_lock();
2574 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002575 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002576 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct block_device *bdev = rdev->bdev;
2578 request_queue_t *r_queue = bdev_get_queue(bdev);
2579
2580 if (!r_queue->issue_flush_fn)
2581 ret = -EOPNOTSUPP;
2582 else {
2583 atomic_inc(&rdev->nr_pending);
2584 rcu_read_unlock();
2585 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2586 error_sector);
2587 rdev_dec_pending(rdev, mddev);
2588 rcu_read_lock();
2589 }
2590 }
2591 }
2592 rcu_read_unlock();
2593 return ret;
2594}
2595
NeilBrownf022b2f2006-10-03 01:15:56 -07002596static int raid5_congested(void *data, int bits)
2597{
2598 mddev_t *mddev = data;
2599 raid5_conf_t *conf = mddev_to_conf(mddev);
2600
2601 /* No difference between reads and writes. Just check
2602 * how busy the stripe_cache is
2603 */
2604 if (conf->inactive_blocked)
2605 return 1;
2606 if (conf->quiesce)
2607 return 1;
2608 if (list_empty_careful(&conf->inactive_list))
2609 return 1;
2610
2611 return 0;
2612}
2613
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002614static int make_request(request_queue_t *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 mddev_t *mddev = q->queuedata;
2617 raid5_conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 unsigned int dd_idx, pd_idx;
2619 sector_t new_sector;
2620 sector_t logical_sector, last_sector;
2621 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01002622 const int rw = bio_data_dir(bi);
NeilBrownf6344752006-03-27 01:18:17 -08002623 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
NeilBrowne5dcdd82005-09-09 16:23:41 -07002625 if (unlikely(bio_barrier(bi))) {
2626 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2627 return 0;
2628 }
2629
NeilBrown3d310eb2005-06-21 17:17:26 -07002630 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07002631
Jens Axboea3623572005-11-01 09:26:16 +01002632 disk_stat_inc(mddev->gendisk, ios[rw]);
2633 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2636 last_sector = bi->bi_sector + (bi->bi_size>>9);
2637 bi->bi_next = NULL;
2638 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2641 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002642 int disks, data_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002643
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002644 retry:
NeilBrownb578d552006-03-27 01:18:12 -08002645 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002646 if (likely(conf->expand_progress == MaxSector))
2647 disks = conf->raid_disks;
2648 else {
NeilBrowndf8e7f72006-03-27 01:18:15 -08002649 /* spinlock is needed as expand_progress may be
2650 * 64bit on a 32bit platform, and so it might be
2651 * possible to see a half-updated value
2652 * Ofcourse expand_progress could change after
2653 * the lock is dropped, so once we get a reference
2654 * to the stripe that we think it is, we will have
2655 * to check again.
2656 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002657 spin_lock_irq(&conf->device_lock);
2658 disks = conf->raid_disks;
2659 if (logical_sector >= conf->expand_progress)
2660 disks = conf->previous_raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002661 else {
2662 if (logical_sector >= conf->expand_lo) {
2663 spin_unlock_irq(&conf->device_lock);
2664 schedule();
2665 goto retry;
2666 }
2667 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002668 spin_unlock_irq(&conf->device_lock);
2669 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002670 data_disks = disks - conf->max_degraded;
2671
2672 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002673 &dd_idx, &pd_idx, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2675 (unsigned long long)new_sector,
2676 (unsigned long long)logical_sector);
2677
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002678 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002680 if (unlikely(conf->expand_progress != MaxSector)) {
2681 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08002682 * stripe, so we must do the range check again.
2683 * Expansion could still move past after this
2684 * test, but as we are holding a reference to
2685 * 'sh', we know that if that happens,
2686 * STRIPE_EXPANDING will get set and the expansion
2687 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002688 */
2689 int must_retry = 0;
2690 spin_lock_irq(&conf->device_lock);
2691 if (logical_sector < conf->expand_progress &&
2692 disks == conf->previous_raid_disks)
2693 /* mismatch, need to try again */
2694 must_retry = 1;
2695 spin_unlock_irq(&conf->device_lock);
2696 if (must_retry) {
2697 release_stripe(sh);
2698 goto retry;
2699 }
2700 }
NeilBrowne464eaf2006-03-27 01:18:14 -08002701 /* FIXME what if we get a false positive because these
2702 * are being updated.
2703 */
2704 if (logical_sector >= mddev->suspend_lo &&
2705 logical_sector < mddev->suspend_hi) {
2706 release_stripe(sh);
2707 schedule();
2708 goto retry;
2709 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002710
2711 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2712 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2713 /* Stripe is busy expanding or
2714 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * and wait a while
2716 */
2717 raid5_unplug_device(mddev->queue);
2718 release_stripe(sh);
2719 schedule();
2720 goto retry;
2721 }
2722 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002723 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 } else {
2726 /* cannot get stripe for read-ahead, just give-up */
2727 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2728 finish_wait(&conf->wait_for_overlap, &w);
2729 break;
2730 }
2731
2732 }
2733 spin_lock_irq(&conf->device_lock);
NeilBrownf6344752006-03-27 01:18:17 -08002734 remaining = --bi->bi_phys_segments;
2735 spin_unlock_irq(&conf->device_lock);
2736 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 int bytes = bi->bi_size;
2738
NeilBrown16a53ec2006-06-26 00:27:38 -07002739 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 md_write_end(mddev);
2741 bi->bi_size = 0;
2742 bi->bi_end_io(bi, bytes, 0);
2743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return 0;
2745}
2746
NeilBrown52c03292006-06-26 00:27:43 -07002747static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
NeilBrown52c03292006-06-26 00:27:43 -07002749 /* reshaping is quite different to recovery/resync so it is
2750 * handled quite separately ... here.
2751 *
2752 * On each call to sync_request, we gather one chunk worth of
2753 * destination stripes and flag them as expanding.
2754 * Then we find all the source stripes and request reads.
2755 * As the reads complete, handle_stripe will copy the data
2756 * into the destination stripe and release that stripe.
2757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2759 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08002760 int pd_idx;
2761 sector_t first_sector, last_sector;
NeilBrown52c03292006-06-26 00:27:43 -07002762 int raid_disks;
2763 int data_disks;
2764 int i;
2765 int dd_idx;
2766 sector_t writepos, safepos, gap;
2767
2768 if (sector_nr == 0 &&
2769 conf->expand_progress != 0) {
2770 /* restarting in the middle, skip the initial sectors */
2771 sector_nr = conf->expand_progress;
2772 sector_div(sector_nr, conf->raid_disks-1);
2773 *skipped = 1;
2774 return sector_nr;
2775 }
2776
2777 /* we update the metadata when there is more than 3Meg
2778 * in the block range (that is rather arbitrary, should
2779 * probably be time based) or when the data about to be
2780 * copied would over-write the source of the data at
2781 * the front of the range.
2782 * i.e. one new_stripe forward from expand_progress new_maps
2783 * to after where expand_lo old_maps to
2784 */
2785 writepos = conf->expand_progress +
2786 conf->chunk_size/512*(conf->raid_disks-1);
2787 sector_div(writepos, conf->raid_disks-1);
2788 safepos = conf->expand_lo;
2789 sector_div(safepos, conf->previous_raid_disks-1);
2790 gap = conf->expand_progress - conf->expand_lo;
2791
2792 if (writepos >= safepos ||
2793 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
2794 /* Cannot proceed until we've updated the superblock... */
2795 wait_event(conf->wait_for_overlap,
2796 atomic_read(&conf->reshape_stripes)==0);
2797 mddev->reshape_position = conf->expand_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07002798 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07002799 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07002800 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07002801 kthread_should_stop());
2802 spin_lock_irq(&conf->device_lock);
2803 conf->expand_lo = mddev->reshape_position;
2804 spin_unlock_irq(&conf->device_lock);
2805 wake_up(&conf->wait_for_overlap);
2806 }
2807
2808 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
2809 int j;
2810 int skipped = 0;
2811 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
2812 sh = get_active_stripe(conf, sector_nr+i,
2813 conf->raid_disks, pd_idx, 0);
2814 set_bit(STRIPE_EXPANDING, &sh->state);
2815 atomic_inc(&conf->reshape_stripes);
2816 /* If any of this stripe is beyond the end of the old
2817 * array, then we need to zero those blocks
2818 */
2819 for (j=sh->disks; j--;) {
2820 sector_t s;
2821 if (j == sh->pd_idx)
2822 continue;
2823 s = compute_blocknr(sh, j);
2824 if (s < (mddev->array_size<<1)) {
2825 skipped = 1;
2826 continue;
2827 }
2828 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
2829 set_bit(R5_Expanded, &sh->dev[j].flags);
2830 set_bit(R5_UPTODATE, &sh->dev[j].flags);
2831 }
2832 if (!skipped) {
2833 set_bit(STRIPE_EXPAND_READY, &sh->state);
2834 set_bit(STRIPE_HANDLE, &sh->state);
2835 }
2836 release_stripe(sh);
2837 }
2838 spin_lock_irq(&conf->device_lock);
2839 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
2840 spin_unlock_irq(&conf->device_lock);
2841 /* Ok, those stripe are ready. We can start scheduling
2842 * reads on the source stripes.
2843 * The source stripes are determined by mapping the first and last
2844 * block on the destination stripes.
2845 */
2846 raid_disks = conf->previous_raid_disks;
2847 data_disks = raid_disks - 1;
2848 first_sector =
2849 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
2850 raid_disks, data_disks,
2851 &dd_idx, &pd_idx, conf);
2852 last_sector =
2853 raid5_compute_sector((sector_nr+conf->chunk_size/512)
2854 *(conf->raid_disks-1) -1,
2855 raid_disks, data_disks,
2856 &dd_idx, &pd_idx, conf);
2857 if (last_sector >= (mddev->size<<1))
2858 last_sector = (mddev->size<<1)-1;
2859 while (first_sector <= last_sector) {
2860 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
2861 sh = get_active_stripe(conf, first_sector,
2862 conf->previous_raid_disks, pd_idx, 0);
2863 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2864 set_bit(STRIPE_HANDLE, &sh->state);
2865 release_stripe(sh);
2866 first_sector += STRIPE_SECTORS;
2867 }
2868 return conf->chunk_size>>9;
2869}
2870
2871/* FIXME go_faster isn't used */
2872static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
2873{
2874 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2875 struct stripe_head *sh;
2876 int pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 int raid_disks = conf->raid_disks;
NeilBrown72626682005-09-09 16:23:54 -07002878 sector_t max_sector = mddev->size << 1;
2879 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002880 int still_degraded = 0;
2881 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
NeilBrown72626682005-09-09 16:23:54 -07002883 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /* just being told to finish up .. nothing much to do */
2885 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08002886 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2887 end_reshape(conf);
2888 return 0;
2889 }
NeilBrown72626682005-09-09 16:23:54 -07002890
2891 if (mddev->curr_resync < max_sector) /* aborted */
2892 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2893 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07002894 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07002895 conf->fullsync = 0;
2896 bitmap_close_sync(mddev->bitmap);
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return 0;
2899 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08002900
NeilBrown52c03292006-06-26 00:27:43 -07002901 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2902 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08002903
NeilBrown16a53ec2006-06-26 00:27:38 -07002904 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 * to resync, then assert that we are finished, because there is
2906 * nothing we can do.
2907 */
NeilBrown3285edf2006-06-26 00:27:55 -07002908 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07002909 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07002910 sector_t rv = (mddev->size << 1) - sector_nr;
2911 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return rv;
2913 }
NeilBrown72626682005-09-09 16:23:54 -07002914 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08002915 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07002916 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
2917 /* we can skip this block, and probably more */
2918 sync_blocks /= STRIPE_SECTORS;
2919 *skipped = 1;
2920 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
2921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
NeilBrownccfcc3c2006-03-27 01:18:09 -08002923 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002924 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 if (sh == NULL) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002926 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07002928 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08002930 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002932 /* Need to check if array will still be degraded after recovery/resync
2933 * We don't need to check the 'failed' flag as when that gets set,
2934 * recovery aborts.
2935 */
2936 for (i=0; i<mddev->raid_disks; i++)
2937 if (conf->disks[i].rdev == NULL)
2938 still_degraded = 1;
2939
2940 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
2941
2942 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 set_bit(STRIPE_SYNCING, &sh->state);
2944 clear_bit(STRIPE_INSYNC, &sh->state);
2945 spin_unlock(&sh->lock);
2946
NeilBrown16a53ec2006-06-26 00:27:38 -07002947 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 release_stripe(sh);
2949
2950 return STRIPE_SECTORS;
2951}
2952
2953/*
2954 * This is our raid5 kernel thread.
2955 *
2956 * We scan the hash table for stripes which can be handled now.
2957 * During the scan, completed stripes are saved for us by the interrupt
2958 * handler, so that they will not have to wait for our next wakeup.
2959 */
2960static void raid5d (mddev_t *mddev)
2961{
2962 struct stripe_head *sh;
2963 raid5_conf_t *conf = mddev_to_conf(mddev);
2964 int handled;
2965
2966 PRINTK("+++ raid5d active\n");
2967
2968 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 handled = 0;
2971 spin_lock_irq(&conf->device_lock);
2972 while (1) {
2973 struct list_head *first;
2974
NeilBrownae3c20c2006-07-10 04:44:17 -07002975 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07002976 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08002977 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002978 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08002979 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002980 conf->seq_write = seq;
2981 activate_bit_delay(conf);
2982 }
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (list_empty(&conf->handle_list) &&
2985 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
2986 !blk_queue_plugged(mddev->queue) &&
2987 !list_empty(&conf->delayed_list))
2988 raid5_activate_delayed(conf);
2989
2990 if (list_empty(&conf->handle_list))
2991 break;
2992
2993 first = conf->handle_list.next;
2994 sh = list_entry(first, struct stripe_head, lru);
2995
2996 list_del_init(first);
2997 atomic_inc(&sh->count);
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002998 BUG_ON(atomic_read(&sh->count)!= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 spin_unlock_irq(&conf->device_lock);
3000
3001 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003002 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 release_stripe(sh);
3004
3005 spin_lock_irq(&conf->device_lock);
3006 }
3007 PRINTK("%d stripes handled\n", handled);
3008
3009 spin_unlock_irq(&conf->device_lock);
3010
3011 unplug_slaves(mddev);
3012
3013 PRINTK("--- raid5d inactive\n");
3014}
3015
NeilBrown3f294f42005-11-08 21:39:25 -08003016static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003017raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003018{
NeilBrown007583c2005-11-08 21:39:30 -08003019 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003020 if (conf)
3021 return sprintf(page, "%d\n", conf->max_nr_stripes);
3022 else
3023 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003024}
3025
3026static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003027raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08003028{
NeilBrown007583c2005-11-08 21:39:30 -08003029 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08003030 char *end;
3031 int new;
3032 if (len >= PAGE_SIZE)
3033 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08003034 if (!conf)
3035 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08003036
3037 new = simple_strtoul(page, &end, 10);
3038 if (!*page || (*end && *end != '\n') )
3039 return -EINVAL;
3040 if (new <= 16 || new > 32768)
3041 return -EINVAL;
3042 while (new < conf->max_nr_stripes) {
3043 if (drop_one_stripe(conf))
3044 conf->max_nr_stripes--;
3045 else
3046 break;
3047 }
3048 while (new > conf->max_nr_stripes) {
3049 if (grow_one_stripe(conf))
3050 conf->max_nr_stripes++;
3051 else break;
3052 }
3053 return len;
3054}
NeilBrown007583c2005-11-08 21:39:30 -08003055
NeilBrown96de1e62005-11-08 21:39:39 -08003056static struct md_sysfs_entry
3057raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3058 raid5_show_stripe_cache_size,
3059 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08003060
3061static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003062stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003063{
NeilBrown007583c2005-11-08 21:39:30 -08003064 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003065 if (conf)
3066 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3067 else
3068 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003069}
3070
NeilBrown96de1e62005-11-08 21:39:39 -08003071static struct md_sysfs_entry
3072raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08003073
NeilBrown007583c2005-11-08 21:39:30 -08003074static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08003075 &raid5_stripecache_size.attr,
3076 &raid5_stripecache_active.attr,
3077 NULL,
3078};
NeilBrown007583c2005-11-08 21:39:30 -08003079static struct attribute_group raid5_attrs_group = {
3080 .name = NULL,
3081 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08003082};
3083
NeilBrown72626682005-09-09 16:23:54 -07003084static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
3086 raid5_conf_t *conf;
3087 int raid_disk, memory;
3088 mdk_rdev_t *rdev;
3089 struct disk_info *disk;
3090 struct list_head *tmp;
NeilBrown02c2de82006-10-03 01:15:47 -07003091 int working_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
NeilBrown16a53ec2006-06-26 00:27:38 -07003093 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3094 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown14f8d262006-01-06 00:20:14 -08003095 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return -EIO;
3097 }
3098
NeilBrownf6705572006-03-27 01:18:11 -08003099 if (mddev->reshape_position != MaxSector) {
3100 /* Check that we can continue the reshape.
3101 * Currently only disks can change, it must
3102 * increase, and we must be past the point where
3103 * a stripe over-writes itself
3104 */
3105 sector_t here_new, here_old;
3106 int old_disks;
3107
3108 if (mddev->new_level != mddev->level ||
3109 mddev->new_layout != mddev->layout ||
3110 mddev->new_chunk != mddev->chunk_size) {
3111 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
3112 mdname(mddev));
3113 return -EINVAL;
3114 }
3115 if (mddev->delta_disks <= 0) {
3116 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3117 mdname(mddev));
3118 return -EINVAL;
3119 }
3120 old_disks = mddev->raid_disks - mddev->delta_disks;
3121 /* reshape_position must be on a new-stripe boundary, and one
3122 * further up in new geometry must map after here in old geometry.
3123 */
3124 here_new = mddev->reshape_position;
3125 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
3126 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
3127 return -EINVAL;
3128 }
3129 /* here_new is the stripe we will write to */
3130 here_old = mddev->reshape_position;
3131 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
3132 /* here_old is the first stripe that we might need to read from */
3133 if (here_new >= here_old) {
3134 /* Reading from the same stripe as writing to - bad */
3135 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
3136 return -EINVAL;
3137 }
3138 printk(KERN_INFO "raid5: reshape will continue\n");
3139 /* OK, we should be able to continue; */
3140 }
3141
3142
NeilBrownb55e6bf2006-03-27 01:18:06 -08003143 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if ((conf = mddev->private) == NULL)
3145 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08003146 if (mddev->reshape_position == MaxSector) {
3147 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3148 } else {
3149 conf->raid_disks = mddev->raid_disks;
3150 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3151 }
3152
3153 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
NeilBrownb55e6bf2006-03-27 01:18:06 -08003154 GFP_KERNEL);
3155 if (!conf->disks)
3156 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08003157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 conf->mddev = mddev;
3159
NeilBrownfccddba2006-01-06 00:20:33 -08003160 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
NeilBrown16a53ec2006-06-26 00:27:38 -07003163 if (mddev->level == 6) {
3164 conf->spare_page = alloc_page(GFP_KERNEL);
3165 if (!conf->spare_page)
3166 goto abort;
3167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 spin_lock_init(&conf->device_lock);
3169 init_waitqueue_head(&conf->wait_for_stripe);
3170 init_waitqueue_head(&conf->wait_for_overlap);
3171 INIT_LIST_HEAD(&conf->handle_list);
3172 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07003173 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 INIT_LIST_HEAD(&conf->inactive_list);
3175 atomic_set(&conf->active_stripes, 0);
3176 atomic_set(&conf->preread_active_stripes, 0);
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3179
3180 ITERATE_RDEV(mddev,rdev,tmp) {
3181 raid_disk = rdev->raid_disk;
NeilBrownf6705572006-03-27 01:18:11 -08003182 if (raid_disk >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 || raid_disk < 0)
3184 continue;
3185 disk = conf->disks + raid_disk;
3186
3187 disk->rdev = rdev;
3188
NeilBrownb2d444d2005-11-08 21:39:31 -08003189 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 char b[BDEVNAME_SIZE];
3191 printk(KERN_INFO "raid5: device %s operational as raid"
3192 " disk %d\n", bdevname(rdev->bdev,b),
3193 raid_disk);
NeilBrown02c2de82006-10-03 01:15:47 -07003194 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196 }
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003199 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 */
NeilBrown02c2de82006-10-03 01:15:47 -07003201 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 conf->mddev = mddev;
3203 conf->chunk_size = mddev->chunk_size;
3204 conf->level = mddev->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003205 if (conf->level == 6)
3206 conf->max_degraded = 2;
3207 else
3208 conf->max_degraded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 conf->algorithm = mddev->layout;
3210 conf->max_nr_stripes = NR_STRIPES;
NeilBrownf6705572006-03-27 01:18:11 -08003211 conf->expand_progress = mddev->reshape_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213 /* device size must be a multiple of chunk size */
3214 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07003215 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
NeilBrown16a53ec2006-06-26 00:27:38 -07003217 if (conf->level == 6 && conf->raid_disks < 4) {
3218 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3219 mdname(mddev), conf->raid_disks);
3220 goto abort;
3221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 if (!conf->chunk_size || conf->chunk_size % 4) {
3223 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3224 conf->chunk_size, mdname(mddev));
3225 goto abort;
3226 }
3227 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3228 printk(KERN_ERR
3229 "raid5: unsupported parity algorithm %d for %s\n",
3230 conf->algorithm, mdname(mddev));
3231 goto abort;
3232 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003233 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 printk(KERN_ERR "raid5: not enough operational devices for %s"
3235 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07003236 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 goto abort;
3238 }
3239
NeilBrown16a53ec2006-06-26 00:27:38 -07003240 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003242 if (mddev->ok_start_degraded)
3243 printk(KERN_WARNING
3244 "raid5: starting dirty degraded array: %s"
3245 "- data corruption possible.\n",
3246 mdname(mddev));
3247 else {
3248 printk(KERN_ERR
3249 "raid5: cannot start dirty degraded array for %s\n",
3250 mdname(mddev));
3251 goto abort;
3252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
3254
3255 {
3256 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3257 if (!mddev->thread) {
3258 printk(KERN_ERR
3259 "raid5: couldn't allocate thread for %s\n",
3260 mdname(mddev));
3261 goto abort;
3262 }
3263 }
NeilBrown50368052005-12-12 02:39:17 -08003264 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3266 if (grow_stripes(conf, conf->max_nr_stripes)) {
3267 printk(KERN_ERR
3268 "raid5: couldn't allocate %dkB for buffers\n", memory);
3269 shrink_stripes(conf);
3270 md_unregister_thread(mddev->thread);
3271 goto abort;
3272 } else
3273 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3274 memory, mdname(mddev));
3275
3276 if (mddev->degraded == 0)
3277 printk("raid5: raid level %d set %s active with %d out of %d"
3278 " devices, algorithm %d\n", conf->level, mdname(mddev),
3279 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3280 conf->algorithm);
3281 else
3282 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3283 " out of %d devices, algorithm %d\n", conf->level,
3284 mdname(mddev), mddev->raid_disks - mddev->degraded,
3285 mddev->raid_disks, conf->algorithm);
3286
3287 print_raid5_conf(conf);
3288
NeilBrownf6705572006-03-27 01:18:11 -08003289 if (conf->expand_progress != MaxSector) {
3290 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08003291 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08003292 atomic_set(&conf->reshape_stripes, 0);
3293 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3294 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3295 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3296 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3297 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3298 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08003299 }
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07003302 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 */
3304 {
NeilBrown16a53ec2006-06-26 00:27:38 -07003305 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3306 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07003307 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3309 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3310 }
3311
3312 /* Ok, everything is just fine now */
NeilBrown007583c2005-11-08 21:39:30 -08003313 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
NeilBrown7a5febe2005-05-16 21:53:16 -07003314
3315 mddev->queue->unplug_fn = raid5_unplug_device;
3316 mddev->queue->issue_flush_fn = raid5_issue_flush;
NeilBrownf022b2f2006-10-03 01:15:56 -07003317 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
3318 mddev->queue->backing_dev_info.congested_data = mddev;
3319
NeilBrown16a53ec2006-06-26 00:27:38 -07003320 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3321 conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return 0;
3324abort:
3325 if (conf) {
3326 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07003327 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003328 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08003329 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 kfree(conf);
3331 }
3332 mddev->private = NULL;
3333 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3334 return -EIO;
3335}
3336
3337
3338
NeilBrown3f294f42005-11-08 21:39:25 -08003339static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
3341 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3342
3343 md_unregister_thread(mddev->thread);
3344 mddev->thread = NULL;
3345 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08003346 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08003348 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003349 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08003350 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 mddev->private = NULL;
3352 return 0;
3353}
3354
3355#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003356static void print_sh (struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
3358 int i;
3359
NeilBrown16a53ec2006-06-26 00:27:38 -07003360 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3361 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3362 seq_printf(seq, "sh %llu, count %d.\n",
3363 (unsigned long long)sh->sector, atomic_read(&sh->count));
3364 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003365 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003366 seq_printf(seq, "(cache%d: %p %ld) ",
3367 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003369 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
NeilBrown16a53ec2006-06-26 00:27:38 -07003372static void printall (struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
3374 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08003375 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 int i;
3377
3378 spin_lock_irq(&conf->device_lock);
3379 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08003380 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (sh->raid_conf != conf)
3382 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07003383 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
3385 }
3386 spin_unlock_irq(&conf->device_lock);
3387}
3388#endif
3389
3390static void status (struct seq_file *seq, mddev_t *mddev)
3391{
3392 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3393 int i;
3394
3395 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07003396 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 for (i = 0; i < conf->raid_disks; i++)
3398 seq_printf (seq, "%s",
3399 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08003400 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 seq_printf (seq, "]");
3402#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003403 seq_printf (seq, "\n");
3404 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405#endif
3406}
3407
3408static void print_raid5_conf (raid5_conf_t *conf)
3409{
3410 int i;
3411 struct disk_info *tmp;
3412
3413 printk("RAID5 conf printout:\n");
3414 if (!conf) {
3415 printk("(conf==NULL)\n");
3416 return;
3417 }
NeilBrown02c2de82006-10-03 01:15:47 -07003418 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
3419 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
3421 for (i = 0; i < conf->raid_disks; i++) {
3422 char b[BDEVNAME_SIZE];
3423 tmp = conf->disks + i;
3424 if (tmp->rdev)
3425 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08003426 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 bdevname(tmp->rdev->bdev,b));
3428 }
3429}
3430
3431static int raid5_spare_active(mddev_t *mddev)
3432{
3433 int i;
3434 raid5_conf_t *conf = mddev->private;
3435 struct disk_info *tmp;
3436
3437 for (i = 0; i < conf->raid_disks; i++) {
3438 tmp = conf->disks + i;
3439 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08003440 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07003441 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
3442 unsigned long flags;
3443 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07003445 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
3447 }
3448 print_raid5_conf(conf);
3449 return 0;
3450}
3451
3452static int raid5_remove_disk(mddev_t *mddev, int number)
3453{
3454 raid5_conf_t *conf = mddev->private;
3455 int err = 0;
3456 mdk_rdev_t *rdev;
3457 struct disk_info *p = conf->disks + number;
3458
3459 print_raid5_conf(conf);
3460 rdev = p->rdev;
3461 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003462 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 atomic_read(&rdev->nr_pending)) {
3464 err = -EBUSY;
3465 goto abort;
3466 }
3467 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003468 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 if (atomic_read(&rdev->nr_pending)) {
3470 /* lost the race, try later */
3471 err = -EBUSY;
3472 p->rdev = rdev;
3473 }
3474 }
3475abort:
3476
3477 print_raid5_conf(conf);
3478 return err;
3479}
3480
3481static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3482{
3483 raid5_conf_t *conf = mddev->private;
3484 int found = 0;
3485 int disk;
3486 struct disk_info *p;
3487
NeilBrown16a53ec2006-06-26 00:27:38 -07003488 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 /* no point adding a device */
3490 return 0;
3491
3492 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003493 * find the disk ... but prefer rdev->saved_raid_disk
3494 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003496 if (rdev->saved_raid_disk >= 0 &&
3497 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3498 disk = rdev->saved_raid_disk;
3499 else
3500 disk = 0;
3501 for ( ; disk < conf->raid_disks; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003503 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 rdev->raid_disk = disk;
3505 found = 1;
NeilBrown72626682005-09-09 16:23:54 -07003506 if (rdev->saved_raid_disk != disk)
3507 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08003508 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 break;
3510 }
3511 print_raid5_conf(conf);
3512 return found;
3513}
3514
3515static int raid5_resize(mddev_t *mddev, sector_t sectors)
3516{
3517 /* no resync is happening, and there is enough space
3518 * on all devices, so we can resize.
3519 * We need to make sure resync covers any new space.
3520 * If the array is shrinking we should possibly wait until
3521 * any io in the removed space completes, but it hardly seems
3522 * worth it.
3523 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003524 raid5_conf_t *conf = mddev_to_conf(mddev);
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003527 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 set_capacity(mddev->gendisk, mddev->array_size << 1);
3529 mddev->changed = 1;
3530 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3531 mddev->recovery_cp = mddev->size << 1;
3532 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3533 }
3534 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003535 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return 0;
3537}
3538
NeilBrown29269552006-03-27 01:18:10 -08003539#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003540static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08003541{
3542 raid5_conf_t *conf = mddev_to_conf(mddev);
3543 int err;
NeilBrown29269552006-03-27 01:18:10 -08003544
NeilBrown63c70c42006-03-27 01:18:13 -08003545 if (mddev->delta_disks < 0 ||
3546 mddev->new_level != mddev->level)
3547 return -EINVAL; /* Cannot shrink array or change level yet */
3548 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08003549 return 0; /* nothing to do */
3550
3551 /* Can only proceed if there are plenty of stripe_heads.
3552 * We need a minimum of one full stripe,, and for sensible progress
3553 * it is best to have about 4 times that.
3554 * If we require 4 times, then the default 256 4K stripe_heads will
3555 * allow for chunk sizes up to 256K, which is probably OK.
3556 * If the chunk size is greater, user-space should request more
3557 * stripe_heads first.
3558 */
NeilBrown63c70c42006-03-27 01:18:13 -08003559 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3560 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08003561 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3562 (mddev->chunk_size / STRIPE_SIZE)*4);
3563 return -ENOSPC;
3564 }
3565
NeilBrown63c70c42006-03-27 01:18:13 -08003566 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3567 if (err)
3568 return err;
3569
3570 /* looks like we might be able to manage this */
3571 return 0;
3572}
3573
3574static int raid5_start_reshape(mddev_t *mddev)
3575{
3576 raid5_conf_t *conf = mddev_to_conf(mddev);
3577 mdk_rdev_t *rdev;
3578 struct list_head *rtmp;
3579 int spares = 0;
3580 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07003581 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08003582
3583 if (mddev->degraded ||
3584 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3585 return -EBUSY;
3586
NeilBrown29269552006-03-27 01:18:10 -08003587 ITERATE_RDEV(mddev, rdev, rtmp)
3588 if (rdev->raid_disk < 0 &&
3589 !test_bit(Faulty, &rdev->flags))
3590 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08003591
3592 if (spares < mddev->delta_disks-1)
NeilBrown29269552006-03-27 01:18:10 -08003593 /* Not enough devices even to make a degraded array
3594 * of that size
3595 */
3596 return -EINVAL;
3597
NeilBrownf6705572006-03-27 01:18:11 -08003598 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08003599 spin_lock_irq(&conf->device_lock);
3600 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003601 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08003602 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003603 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08003604 spin_unlock_irq(&conf->device_lock);
3605
3606 /* Add some new drives, as many as will fit.
3607 * We know there are enough to make the newly sized array work.
3608 */
3609 ITERATE_RDEV(mddev, rdev, rtmp)
3610 if (rdev->raid_disk < 0 &&
3611 !test_bit(Faulty, &rdev->flags)) {
3612 if (raid5_add_disk(mddev, rdev)) {
3613 char nm[20];
3614 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08003615 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003616 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08003617 sprintf(nm, "rd%d", rdev->raid_disk);
3618 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3619 } else
3620 break;
3621 }
3622
NeilBrownc04be0a2006-10-03 01:15:53 -07003623 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003624 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
NeilBrownc04be0a2006-10-03 01:15:53 -07003625 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003626 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08003627 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07003628 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08003629
NeilBrown29269552006-03-27 01:18:10 -08003630 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3631 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3632 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3633 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3634 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3635 "%s_reshape");
3636 if (!mddev->sync_thread) {
3637 mddev->recovery = 0;
3638 spin_lock_irq(&conf->device_lock);
3639 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3640 conf->expand_progress = MaxSector;
3641 spin_unlock_irq(&conf->device_lock);
3642 return -EAGAIN;
3643 }
3644 md_wakeup_thread(mddev->sync_thread);
3645 md_new_event(mddev);
3646 return 0;
3647}
3648#endif
3649
3650static void end_reshape(raid5_conf_t *conf)
3651{
3652 struct block_device *bdev;
3653
NeilBrownf6705572006-03-27 01:18:11 -08003654 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
3655 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
3656 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
3657 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08003658
NeilBrownf6705572006-03-27 01:18:11 -08003659 bdev = bdget_disk(conf->mddev->gendisk, 0);
3660 if (bdev) {
3661 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown0692c6b2006-11-08 17:44:48 -08003662 i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
NeilBrownf6705572006-03-27 01:18:11 -08003663 mutex_unlock(&bdev->bd_inode->i_mutex);
3664 bdput(bdev);
3665 }
3666 spin_lock_irq(&conf->device_lock);
3667 conf->expand_progress = MaxSector;
3668 spin_unlock_irq(&conf->device_lock);
3669 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07003670
3671 /* read-ahead size must cover two whole stripes, which is
3672 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3673 */
3674 {
3675 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3676 int stripe = data_disks *
3677 (conf->mddev->chunk_size / PAGE_SIZE);
3678 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3679 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3680 }
NeilBrown29269552006-03-27 01:18:10 -08003681 }
NeilBrown29269552006-03-27 01:18:10 -08003682}
3683
NeilBrown72626682005-09-09 16:23:54 -07003684static void raid5_quiesce(mddev_t *mddev, int state)
3685{
3686 raid5_conf_t *conf = mddev_to_conf(mddev);
3687
3688 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08003689 case 2: /* resume for a suspend */
3690 wake_up(&conf->wait_for_overlap);
3691 break;
3692
NeilBrown72626682005-09-09 16:23:54 -07003693 case 1: /* stop all writes */
3694 spin_lock_irq(&conf->device_lock);
3695 conf->quiesce = 1;
3696 wait_event_lock_irq(conf->wait_for_stripe,
3697 atomic_read(&conf->active_stripes) == 0,
3698 conf->device_lock, /* nothing */);
3699 spin_unlock_irq(&conf->device_lock);
3700 break;
3701
3702 case 0: /* re-enable writes */
3703 spin_lock_irq(&conf->device_lock);
3704 conf->quiesce = 0;
3705 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08003706 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07003707 spin_unlock_irq(&conf->device_lock);
3708 break;
3709 }
NeilBrown72626682005-09-09 16:23:54 -07003710}
NeilBrownb15c2e52006-01-06 00:20:16 -08003711
NeilBrown16a53ec2006-06-26 00:27:38 -07003712static struct mdk_personality raid6_personality =
3713{
3714 .name = "raid6",
3715 .level = 6,
3716 .owner = THIS_MODULE,
3717 .make_request = make_request,
3718 .run = run,
3719 .stop = stop,
3720 .status = status,
3721 .error_handler = error,
3722 .hot_add_disk = raid5_add_disk,
3723 .hot_remove_disk= raid5_remove_disk,
3724 .spare_active = raid5_spare_active,
3725 .sync_request = sync_request,
3726 .resize = raid5_resize,
3727 .quiesce = raid5_quiesce,
3728};
NeilBrown2604b702006-01-06 00:20:36 -08003729static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08003732 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 .owner = THIS_MODULE,
3734 .make_request = make_request,
3735 .run = run,
3736 .stop = stop,
3737 .status = status,
3738 .error_handler = error,
3739 .hot_add_disk = raid5_add_disk,
3740 .hot_remove_disk= raid5_remove_disk,
3741 .spare_active = raid5_spare_active,
3742 .sync_request = sync_request,
3743 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08003744#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003745 .check_reshape = raid5_check_reshape,
3746 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08003747#endif
NeilBrown72626682005-09-09 16:23:54 -07003748 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749};
3750
NeilBrown2604b702006-01-06 00:20:36 -08003751static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
NeilBrown2604b702006-01-06 00:20:36 -08003753 .name = "raid4",
3754 .level = 4,
3755 .owner = THIS_MODULE,
3756 .make_request = make_request,
3757 .run = run,
3758 .stop = stop,
3759 .status = status,
3760 .error_handler = error,
3761 .hot_add_disk = raid5_add_disk,
3762 .hot_remove_disk= raid5_remove_disk,
3763 .spare_active = raid5_spare_active,
3764 .sync_request = sync_request,
3765 .resize = raid5_resize,
3766 .quiesce = raid5_quiesce,
3767};
3768
3769static int __init raid5_init(void)
3770{
NeilBrown16a53ec2006-06-26 00:27:38 -07003771 int e;
3772
3773 e = raid6_select_algo();
3774 if ( e )
3775 return e;
3776 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08003777 register_md_personality(&raid5_personality);
3778 register_md_personality(&raid4_personality);
3779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
3781
NeilBrown2604b702006-01-06 00:20:36 -08003782static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
NeilBrown16a53ec2006-06-26 00:27:38 -07003784 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08003785 unregister_md_personality(&raid5_personality);
3786 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
3789module_init(raid5_init);
3790module_exit(raid5_exit);
3791MODULE_LICENSE("GPL");
3792MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08003793MODULE_ALIAS("md-raid5");
3794MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08003795MODULE_ALIAS("md-level-5");
3796MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07003797MODULE_ALIAS("md-personality-8"); /* RAID6 */
3798MODULE_ALIAS("md-raid6");
3799MODULE_ALIAS("md-level-6");
3800
3801/* This used to be two separate modules, they were: */
3802MODULE_ALIAS("raid5");
3803MODULE_ALIAS("raid6");