blob: d855f9f59b022b33fa6d255264f00a31d36b2fbd [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);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800137 if (conf->retry_read_aligned)
138 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 }
142}
143static void release_stripe(struct stripe_head *sh)
144{
145 raid5_conf_t *conf = sh->raid_conf;
146 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 spin_lock_irqsave(&conf->device_lock, flags);
149 __release_stripe(conf, sh);
150 spin_unlock_irqrestore(&conf->device_lock, flags);
151}
152
NeilBrownfccddba2006-01-06 00:20:33 -0800153static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
156
NeilBrownfccddba2006-01-06 00:20:33 -0800157 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
NeilBrown16a53ec2006-06-26 00:27:38 -0700160static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
NeilBrownfccddba2006-01-06 00:20:33 -0800162 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
165
166 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800167 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170
171/* find an idle stripe, make sure it is unhashed, and return it. */
172static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
173{
174 struct stripe_head *sh = NULL;
175 struct list_head *first;
176
177 CHECK_DEVLOCK();
178 if (list_empty(&conf->inactive_list))
179 goto out;
180 first = conf->inactive_list.next;
181 sh = list_entry(first, struct stripe_head, lru);
182 list_del_init(first);
183 remove_hash(sh);
184 atomic_inc(&conf->active_stripes);
185out:
186 return sh;
187}
188
189static void shrink_buffers(struct stripe_head *sh, int num)
190{
191 struct page *p;
192 int i;
193
194 for (i=0; i<num ; i++) {
195 p = sh->dev[i].page;
196 if (!p)
197 continue;
198 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800199 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201}
202
203static int grow_buffers(struct stripe_head *sh, int num)
204{
205 int i;
206
207 for (i=0; i<num; i++) {
208 struct page *page;
209
210 if (!(page = alloc_page(GFP_KERNEL))) {
211 return 1;
212 }
213 sh->dev[i].page = page;
214 }
215 return 0;
216}
217
218static void raid5_build_block (struct stripe_head *sh, int i);
219
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800220static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800223 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200225 BUG_ON(atomic_read(&sh->count) != 0);
226 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 CHECK_DEVLOCK();
229 PRINTK("init_stripe called, stripe %llu\n",
230 (unsigned long long)sh->sector);
231
232 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 sh->sector = sector;
235 sh->pd_idx = pd_idx;
236 sh->state = 0;
237
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800238 sh->disks = disks;
239
240 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct r5dev *dev = &sh->dev[i];
242
243 if (dev->toread || dev->towrite || dev->written ||
244 test_bit(R5_LOCKED, &dev->flags)) {
245 printk("sector=%llx i=%d %p %p %p %d\n",
246 (unsigned long long)sh->sector, i, dev->toread,
247 dev->towrite, dev->written,
248 test_bit(R5_LOCKED, &dev->flags));
249 BUG();
250 }
251 dev->flags = 0;
252 raid5_build_block(sh, i);
253 }
254 insert_hash(conf, sh);
255}
256
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800257static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800260 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 CHECK_DEVLOCK();
263 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800264 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800265 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return sh;
267 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
268 return NULL;
269}
270
271static void unplug_slaves(mddev_t *mddev);
272static void raid5_unplug_device(request_queue_t *q);
273
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800274static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
275 int pd_idx, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct stripe_head *sh;
278
279 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
280
281 spin_lock_irq(&conf->device_lock);
282
283 do {
NeilBrown72626682005-09-09 16:23:54 -0700284 wait_event_lock_irq(conf->wait_for_stripe,
285 conf->quiesce == 0,
286 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800287 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!sh) {
289 if (!conf->inactive_blocked)
290 sh = get_free_stripe(conf);
291 if (noblock && sh == NULL)
292 break;
293 if (!sh) {
294 conf->inactive_blocked = 1;
295 wait_event_lock_irq(conf->wait_for_stripe,
296 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800297 (atomic_read(&conf->active_stripes)
298 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 || !conf->inactive_blocked),
300 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700301 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 );
303 conf->inactive_blocked = 0;
304 } else
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800305 init_stripe(sh, sector, pd_idx, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 } else {
307 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200308 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 } else {
310 if (!test_bit(STRIPE_HANDLE, &sh->state))
311 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700312 if (list_empty(&sh->lru) &&
313 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700314 BUG();
315 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317 }
318 } while (sh == NULL);
319
320 if (sh)
321 atomic_inc(&sh->count);
322
323 spin_unlock_irq(&conf->device_lock);
324 return sh;
325}
326
NeilBrown3f294f42005-11-08 21:39:25 -0800327static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800330 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
331 if (!sh)
332 return 0;
333 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
334 sh->raid_conf = conf;
335 spin_lock_init(&sh->lock);
336
337 if (grow_buffers(sh, conf->raid_disks)) {
338 shrink_buffers(sh, conf->raid_disks);
339 kmem_cache_free(conf->slab_cache, sh);
340 return 0;
341 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800342 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800343 /* we just created an active stripe so... */
344 atomic_set(&sh->count, 1);
345 atomic_inc(&conf->active_stripes);
346 INIT_LIST_HEAD(&sh->lru);
347 release_stripe(sh);
348 return 1;
349}
350
351static int grow_stripes(raid5_conf_t *conf, int num)
352{
Christoph Lametere18b8902006-12-06 20:33:20 -0800353 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int devs = conf->raid_disks;
355
NeilBrownad01c9e2006-03-27 01:18:07 -0800356 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
357 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
358 conf->active_name = 0;
359 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
361 0, 0, NULL, NULL);
362 if (!sc)
363 return 1;
364 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800365 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700366 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800367 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
NeilBrown29269552006-03-27 01:18:10 -0800371
372#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800373static int resize_stripes(raid5_conf_t *conf, int newsize)
374{
375 /* Make all the stripes able to hold 'newsize' devices.
376 * New slots in each stripe get 'page' set to a new page.
377 *
378 * This happens in stages:
379 * 1/ create a new kmem_cache and allocate the required number of
380 * stripe_heads.
381 * 2/ gather all the old stripe_heads and tranfer the pages across
382 * to the new stripe_heads. This will have the side effect of
383 * freezing the array as once all stripe_heads have been collected,
384 * no IO will be possible. Old stripe heads are freed once their
385 * pages have been transferred over, and the old kmem_cache is
386 * freed when all stripes are done.
387 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
388 * we simple return a failre status - no need to clean anything up.
389 * 4/ allocate new pages for the new slots in the new stripe_heads.
390 * If this fails, we don't bother trying the shrink the
391 * stripe_heads down again, we just leave them as they are.
392 * As each stripe_head is processed the new one is released into
393 * active service.
394 *
395 * Once step2 is started, we cannot afford to wait for a write,
396 * so we use GFP_NOIO allocations.
397 */
398 struct stripe_head *osh, *nsh;
399 LIST_HEAD(newstripes);
400 struct disk_info *ndisks;
401 int err = 0;
Christoph Lametere18b8902006-12-06 20:33:20 -0800402 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800403 int i;
404
405 if (newsize <= conf->pool_size)
406 return 0; /* never bother to shrink */
407
408 /* Step 1 */
409 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
410 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
411 0, 0, NULL, NULL);
412 if (!sc)
413 return -ENOMEM;
414
415 for (i = conf->max_nr_stripes; i; i--) {
416 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
417 if (!nsh)
418 break;
419
420 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
421
422 nsh->raid_conf = conf;
423 spin_lock_init(&nsh->lock);
424
425 list_add(&nsh->lru, &newstripes);
426 }
427 if (i) {
428 /* didn't get enough, give up */
429 while (!list_empty(&newstripes)) {
430 nsh = list_entry(newstripes.next, struct stripe_head, lru);
431 list_del(&nsh->lru);
432 kmem_cache_free(sc, nsh);
433 }
434 kmem_cache_destroy(sc);
435 return -ENOMEM;
436 }
437 /* Step 2 - Must use GFP_NOIO now.
438 * OK, we have enough stripes, start collecting inactive
439 * stripes and copying them over
440 */
441 list_for_each_entry(nsh, &newstripes, lru) {
442 spin_lock_irq(&conf->device_lock);
443 wait_event_lock_irq(conf->wait_for_stripe,
444 !list_empty(&conf->inactive_list),
445 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -0800446 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -0800447 );
448 osh = get_free_stripe(conf);
449 spin_unlock_irq(&conf->device_lock);
450 atomic_set(&nsh->count, 1);
451 for(i=0; i<conf->pool_size; i++)
452 nsh->dev[i].page = osh->dev[i].page;
453 for( ; i<newsize; i++)
454 nsh->dev[i].page = NULL;
455 kmem_cache_free(conf->slab_cache, osh);
456 }
457 kmem_cache_destroy(conf->slab_cache);
458
459 /* Step 3.
460 * At this point, we are holding all the stripes so the array
461 * is completely stalled, so now is a good time to resize
462 * conf->disks.
463 */
464 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
465 if (ndisks) {
466 for (i=0; i<conf->raid_disks; i++)
467 ndisks[i] = conf->disks[i];
468 kfree(conf->disks);
469 conf->disks = ndisks;
470 } else
471 err = -ENOMEM;
472
473 /* Step 4, return new stripes to service */
474 while(!list_empty(&newstripes)) {
475 nsh = list_entry(newstripes.next, struct stripe_head, lru);
476 list_del_init(&nsh->lru);
477 for (i=conf->raid_disks; i < newsize; i++)
478 if (nsh->dev[i].page == NULL) {
479 struct page *p = alloc_page(GFP_NOIO);
480 nsh->dev[i].page = p;
481 if (!p)
482 err = -ENOMEM;
483 }
484 release_stripe(nsh);
485 }
486 /* critical section pass, GFP_NOIO no longer needed */
487
488 conf->slab_cache = sc;
489 conf->active_name = 1-conf->active_name;
490 conf->pool_size = newsize;
491 return err;
492}
NeilBrown29269552006-03-27 01:18:10 -0800493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrown3f294f42005-11-08 21:39:25 -0800495static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct stripe_head *sh;
498
NeilBrown3f294f42005-11-08 21:39:25 -0800499 spin_lock_irq(&conf->device_lock);
500 sh = get_free_stripe(conf);
501 spin_unlock_irq(&conf->device_lock);
502 if (!sh)
503 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200504 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -0800505 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -0800506 kmem_cache_free(conf->slab_cache, sh);
507 atomic_dec(&conf->active_stripes);
508 return 1;
509}
510
511static void shrink_stripes(raid5_conf_t *conf)
512{
513 while (drop_one_stripe(conf))
514 ;
515
NeilBrown29fc7e32006-02-03 03:03:41 -0800516 if (conf->slab_cache)
517 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 conf->slab_cache = NULL;
519}
520
NeilBrown4e5314b2005-11-08 21:39:22 -0800521static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int error)
523{
524 struct stripe_head *sh = bi->bi_private;
525 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800526 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -0700528 char b[BDEVNAME_SIZE];
529 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (bi->bi_size)
532 return 1;
533
534 for (i=0 ; i<disks; i++)
535 if (bi == &sh->dev[i].req)
536 break;
537
538 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
539 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
540 uptodate);
541 if (i == disks) {
542 BUG();
543 return 0;
544 }
545
546 if (uptodate) {
547#if 0
548 struct bio *bio;
549 unsigned long flags;
550 spin_lock_irqsave(&conf->device_lock, flags);
551 /* we can return a buffer if we bypassed the cache or
552 * if the top buffer is not in highmem. If there are
553 * multiple buffers, leave the extra work to
554 * handle_stripe
555 */
556 buffer = sh->bh_read[i];
557 if (buffer &&
558 (!PageHighMem(buffer->b_page)
559 || buffer->b_page == bh->b_page )
560 ) {
561 sh->bh_read[i] = buffer->b_reqnext;
562 buffer->b_reqnext = NULL;
563 } else
564 buffer = NULL;
565 spin_unlock_irqrestore(&conf->device_lock, flags);
566 if (sh->bh_page[i]==bh->b_page)
567 set_buffer_uptodate(bh);
568 if (buffer) {
569 if (buffer->b_page != bh->b_page)
570 memcpy(buffer->b_data, bh->b_data, bh->b_size);
571 buffer->b_end_io(buffer, 1);
572 }
573#else
574 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -0800575#endif
576 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -0700577 rdev = conf->disks[i].rdev;
578 printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
579 mdname(conf->mddev), STRIPE_SECTORS,
580 (unsigned long long)sh->sector + rdev->data_offset,
581 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -0800582 clear_bit(R5_ReadError, &sh->dev[i].flags);
583 clear_bit(R5_ReWrite, &sh->dev[i].flags);
584 }
NeilBrownba22dcb2005-11-08 21:39:31 -0800585 if (atomic_read(&conf->disks[i].rdev->read_errors))
586 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } else {
NeilBrownd6950432006-07-10 04:44:20 -0700588 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -0800589 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -0700590 rdev = conf->disks[i].rdev;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700593 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -0800594 if (conf->mddev->degraded)
NeilBrownd6950432006-07-10 04:44:20 -0700595 printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
596 mdname(conf->mddev),
597 (unsigned long long)sh->sector + rdev->data_offset,
598 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800599 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -0800600 /* Oh, no!!! */
NeilBrownd6950432006-07-10 04:44:20 -0700601 printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
602 mdname(conf->mddev),
603 (unsigned long long)sh->sector + rdev->data_offset,
604 bdn);
605 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -0800606 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -0800607 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -0700608 "raid5:%s: Too many read errors, failing device %s.\n",
609 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800610 else
611 retry = 1;
612 if (retry)
613 set_bit(R5_ReadError, &sh->dev[i].flags);
614 else {
NeilBrown4e5314b2005-11-08 21:39:22 -0800615 clear_bit(R5_ReadError, &sh->dev[i].flags);
616 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700617 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -0800618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
621#if 0
622 /* must restore b_page before unlocking buffer... */
623 if (sh->bh_page[i] != bh->b_page) {
624 bh->b_page = sh->bh_page[i];
625 bh->b_data = page_address(bh->b_page);
626 clear_buffer_uptodate(bh);
627 }
628#endif
629 clear_bit(R5_LOCKED, &sh->dev[i].flags);
630 set_bit(STRIPE_HANDLE, &sh->state);
631 release_stripe(sh);
632 return 0;
633}
634
635static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
636 int error)
637{
638 struct stripe_head *sh = bi->bi_private;
639 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800640 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
642
643 if (bi->bi_size)
644 return 1;
645
646 for (i=0 ; i<disks; i++)
647 if (bi == &sh->dev[i].req)
648 break;
649
650 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
651 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
652 uptodate);
653 if (i == disks) {
654 BUG();
655 return 0;
656 }
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!uptodate)
659 md_error(conf->mddev, conf->disks[i].rdev);
660
661 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
662
663 clear_bit(R5_LOCKED, &sh->dev[i].flags);
664 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -0700665 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
669
670static sector_t compute_blocknr(struct stripe_head *sh, int i);
671
672static void raid5_build_block (struct stripe_head *sh, int i)
673{
674 struct r5dev *dev = &sh->dev[i];
675
676 bio_init(&dev->req);
677 dev->req.bi_io_vec = &dev->vec;
678 dev->req.bi_vcnt++;
679 dev->req.bi_max_vecs++;
680 dev->vec.bv_page = dev->page;
681 dev->vec.bv_len = STRIPE_SIZE;
682 dev->vec.bv_offset = 0;
683
684 dev->req.bi_sector = sh->sector;
685 dev->req.bi_private = sh;
686
687 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -0700688 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691static void error(mddev_t *mddev, mdk_rdev_t *rdev)
692{
693 char b[BDEVNAME_SIZE];
694 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
695 PRINTK("raid5: error called\n");
696
NeilBrownb2d444d2005-11-08 21:39:31 -0800697 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -0700698 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -0700699 if (test_and_clear_bit(In_sync, &rdev->flags)) {
700 unsigned long flags;
701 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700703 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
705 * if recovery was running, make sure it aborts.
706 */
707 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
708 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800709 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 printk (KERN_ALERT
711 "raid5: Disk failure on %s, disabling device."
712 " Operation continuing on %d devices\n",
NeilBrown02c2de82006-10-03 01:15:47 -0700713 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
NeilBrown16a53ec2006-06-26 00:27:38 -0700715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717/*
718 * Input: a 'big' sector number,
719 * Output: index of the data and parity disk, and the sector # in them.
720 */
721static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
722 unsigned int data_disks, unsigned int * dd_idx,
723 unsigned int * pd_idx, raid5_conf_t *conf)
724{
725 long stripe;
726 unsigned long chunk_number;
727 unsigned int chunk_offset;
728 sector_t new_sector;
729 int sectors_per_chunk = conf->chunk_size >> 9;
730
731 /* First compute the information on this sector */
732
733 /*
734 * Compute the chunk number and the sector offset inside the chunk
735 */
736 chunk_offset = sector_div(r_sector, sectors_per_chunk);
737 chunk_number = r_sector;
738 BUG_ON(r_sector != chunk_number);
739
740 /*
741 * Compute the stripe number
742 */
743 stripe = chunk_number / data_disks;
744
745 /*
746 * Compute the data disk and parity disk indexes inside the stripe
747 */
748 *dd_idx = chunk_number % data_disks;
749
750 /*
751 * Select the parity disk based on the user selected algorithm.
752 */
NeilBrown16a53ec2006-06-26 00:27:38 -0700753 switch(conf->level) {
754 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -0700756 break;
757 case 5:
758 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 case ALGORITHM_LEFT_ASYMMETRIC:
760 *pd_idx = data_disks - stripe % raid_disks;
761 if (*dd_idx >= *pd_idx)
762 (*dd_idx)++;
763 break;
764 case ALGORITHM_RIGHT_ASYMMETRIC:
765 *pd_idx = stripe % raid_disks;
766 if (*dd_idx >= *pd_idx)
767 (*dd_idx)++;
768 break;
769 case ALGORITHM_LEFT_SYMMETRIC:
770 *pd_idx = data_disks - stripe % raid_disks;
771 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
772 break;
773 case ALGORITHM_RIGHT_SYMMETRIC:
774 *pd_idx = stripe % raid_disks;
775 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
776 break;
777 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800778 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700780 }
781 break;
782 case 6:
783
784 /**** FIX THIS ****/
785 switch (conf->algorithm) {
786 case ALGORITHM_LEFT_ASYMMETRIC:
787 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
788 if (*pd_idx == raid_disks-1)
789 (*dd_idx)++; /* Q D D D P */
790 else if (*dd_idx >= *pd_idx)
791 (*dd_idx) += 2; /* D D P Q D */
792 break;
793 case ALGORITHM_RIGHT_ASYMMETRIC:
794 *pd_idx = stripe % raid_disks;
795 if (*pd_idx == raid_disks-1)
796 (*dd_idx)++; /* Q D D D P */
797 else if (*dd_idx >= *pd_idx)
798 (*dd_idx) += 2; /* D D P Q D */
799 break;
800 case ALGORITHM_LEFT_SYMMETRIC:
801 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
802 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
803 break;
804 case ALGORITHM_RIGHT_SYMMETRIC:
805 *pd_idx = stripe % raid_disks;
806 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
807 break;
808 default:
809 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
810 conf->algorithm);
811 }
812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815 /*
816 * Finally, compute the new sector number
817 */
818 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
819 return new_sector;
820}
821
822
823static sector_t compute_blocknr(struct stripe_head *sh, int i)
824{
825 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -0800826 int raid_disks = sh->disks;
827 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 sector_t new_sector = sh->sector, check;
829 int sectors_per_chunk = conf->chunk_size >> 9;
830 sector_t stripe;
831 int chunk_offset;
832 int chunk_number, dummy1, dummy2, dd_idx = i;
833 sector_t r_sector;
834
NeilBrown16a53ec2006-06-26 00:27:38 -0700835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 chunk_offset = sector_div(new_sector, sectors_per_chunk);
837 stripe = new_sector;
838 BUG_ON(new_sector != stripe);
839
NeilBrown16a53ec2006-06-26 00:27:38 -0700840 if (i == sh->pd_idx)
841 return 0;
842 switch(conf->level) {
843 case 4: break;
844 case 5:
845 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 case ALGORITHM_LEFT_ASYMMETRIC:
847 case ALGORITHM_RIGHT_ASYMMETRIC:
848 if (i > sh->pd_idx)
849 i--;
850 break;
851 case ALGORITHM_LEFT_SYMMETRIC:
852 case ALGORITHM_RIGHT_SYMMETRIC:
853 if (i < sh->pd_idx)
854 i += raid_disks;
855 i -= (sh->pd_idx + 1);
856 break;
857 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800858 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -0700859 conf->algorithm);
860 }
861 break;
862 case 6:
NeilBrown16a53ec2006-06-26 00:27:38 -0700863 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
864 return 0; /* It is the Q disk */
865 switch (conf->algorithm) {
866 case ALGORITHM_LEFT_ASYMMETRIC:
867 case ALGORITHM_RIGHT_ASYMMETRIC:
868 if (sh->pd_idx == raid_disks-1)
869 i--; /* Q D D D P */
870 else if (i > sh->pd_idx)
871 i -= 2; /* D D P Q D */
872 break;
873 case ALGORITHM_LEFT_SYMMETRIC:
874 case ALGORITHM_RIGHT_SYMMETRIC:
875 if (sh->pd_idx == raid_disks-1)
876 i--; /* Q D D D P */
877 else {
878 /* D D P Q D */
879 if (i < sh->pd_idx)
880 i += raid_disks;
881 i -= (sh->pd_idx + 2);
882 }
883 break;
884 default:
885 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700887 }
888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 chunk_number = stripe * data_disks + i;
892 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
893
894 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
895 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -0800896 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898 }
899 return r_sector;
900}
901
902
903
904/*
NeilBrown16a53ec2006-06-26 00:27:38 -0700905 * Copy data between a page in the stripe cache, and one or more bion
906 * The page could align with the middle of the bio, or there could be
907 * several bion, each with several bio_vecs, which cover part of the page
908 * Multiple bion are linked together on bi_next. There may be extras
909 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
911static void copy_data(int frombio, struct bio *bio,
912 struct page *page,
913 sector_t sector)
914{
915 char *pa = page_address(page);
916 struct bio_vec *bvl;
917 int i;
918 int page_offset;
919
920 if (bio->bi_sector >= sector)
921 page_offset = (signed)(bio->bi_sector - sector) * 512;
922 else
923 page_offset = (signed)(sector - bio->bi_sector) * -512;
924 bio_for_each_segment(bvl, bio, i) {
925 int len = bio_iovec_idx(bio,i)->bv_len;
926 int clen;
927 int b_offset = 0;
928
929 if (page_offset < 0) {
930 b_offset = -page_offset;
931 page_offset += b_offset;
932 len -= b_offset;
933 }
934
935 if (len > 0 && page_offset + len > STRIPE_SIZE)
936 clen = STRIPE_SIZE - page_offset;
937 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (clen > 0) {
940 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
941 if (frombio)
942 memcpy(pa+page_offset, ba+b_offset, clen);
943 else
944 memcpy(ba+b_offset, pa+page_offset, clen);
945 __bio_kunmap_atomic(ba, KM_USER0);
946 }
947 if (clen < len) /* hit end of page */
948 break;
949 page_offset += len;
950 }
951}
952
953#define check_xor() do { \
954 if (count == MAX_XOR_BLOCKS) { \
955 xor_block(count, STRIPE_SIZE, ptr); \
956 count = 1; \
957 } \
958 } while(0)
959
960
961static void compute_block(struct stripe_head *sh, int dd_idx)
962{
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800963 int i, count, disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 void *ptr[MAX_XOR_BLOCKS], *p;
965
966 PRINTK("compute_block, stripe %llu, idx %d\n",
967 (unsigned long long)sh->sector, dd_idx);
968
969 ptr[0] = page_address(sh->dev[dd_idx].page);
970 memset(ptr[0], 0, STRIPE_SIZE);
971 count = 1;
972 for (i = disks ; i--; ) {
973 if (i == dd_idx)
974 continue;
975 p = page_address(sh->dev[i].page);
976 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
977 ptr[count++] = p;
978 else
NeilBrown14f8d262006-01-06 00:20:14 -0800979 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 " not present\n", dd_idx,
981 (unsigned long long)sh->sector, i);
982
983 check_xor();
984 }
985 if (count != 1)
986 xor_block(count, STRIPE_SIZE, ptr);
987 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
988}
989
NeilBrown16a53ec2006-06-26 00:27:38 -0700990static void compute_parity5(struct stripe_head *sh, int method)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800993 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 void *ptr[MAX_XOR_BLOCKS];
995 struct bio *chosen;
996
NeilBrown16a53ec2006-06-26 00:27:38 -0700997 PRINTK("compute_parity5, stripe %llu, method %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 (unsigned long long)sh->sector, method);
999
1000 count = 1;
1001 ptr[0] = page_address(sh->dev[pd_idx].page);
1002 switch(method) {
1003 case READ_MODIFY_WRITE:
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001004 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 for (i=disks ; i-- ;) {
1006 if (i==pd_idx)
1007 continue;
1008 if (sh->dev[i].towrite &&
1009 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1010 ptr[count++] = page_address(sh->dev[i].page);
1011 chosen = sh->dev[i].towrite;
1012 sh->dev[i].towrite = NULL;
1013
1014 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1015 wake_up(&conf->wait_for_overlap);
1016
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001017 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sh->dev[i].written = chosen;
1019 check_xor();
1020 }
1021 }
1022 break;
1023 case RECONSTRUCT_WRITE:
1024 memset(ptr[0], 0, STRIPE_SIZE);
1025 for (i= disks; i-- ;)
1026 if (i!=pd_idx && sh->dev[i].towrite) {
1027 chosen = sh->dev[i].towrite;
1028 sh->dev[i].towrite = NULL;
1029
1030 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1031 wake_up(&conf->wait_for_overlap);
1032
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001033 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 sh->dev[i].written = chosen;
1035 }
1036 break;
1037 case CHECK_PARITY:
1038 break;
1039 }
1040 if (count>1) {
1041 xor_block(count, STRIPE_SIZE, ptr);
1042 count = 1;
1043 }
1044
1045 for (i = disks; i--;)
1046 if (sh->dev[i].written) {
1047 sector_t sector = sh->dev[i].sector;
1048 struct bio *wbi = sh->dev[i].written;
1049 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1050 copy_data(1, wbi, sh->dev[i].page, sector);
1051 wbi = r5_next_bio(wbi, sector);
1052 }
1053
1054 set_bit(R5_LOCKED, &sh->dev[i].flags);
1055 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1056 }
1057
1058 switch(method) {
1059 case RECONSTRUCT_WRITE:
1060 case CHECK_PARITY:
1061 for (i=disks; i--;)
1062 if (i != pd_idx) {
1063 ptr[count++] = page_address(sh->dev[i].page);
1064 check_xor();
1065 }
1066 break;
1067 case READ_MODIFY_WRITE:
1068 for (i = disks; i--;)
1069 if (sh->dev[i].written) {
1070 ptr[count++] = page_address(sh->dev[i].page);
1071 check_xor();
1072 }
1073 }
1074 if (count != 1)
1075 xor_block(count, STRIPE_SIZE, ptr);
1076
1077 if (method != CHECK_PARITY) {
1078 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1079 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1080 } else
1081 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1082}
1083
NeilBrown16a53ec2006-06-26 00:27:38 -07001084static void compute_parity6(struct stripe_head *sh, int method)
1085{
1086 raid6_conf_t *conf = sh->raid_conf;
1087 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
1088 struct bio *chosen;
1089 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1090 void *ptrs[disks];
1091
1092 qd_idx = raid6_next_disk(pd_idx, disks);
1093 d0_idx = raid6_next_disk(qd_idx, disks);
1094
1095 PRINTK("compute_parity, stripe %llu, method %d\n",
1096 (unsigned long long)sh->sector, method);
1097
1098 switch(method) {
1099 case READ_MODIFY_WRITE:
1100 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1101 case RECONSTRUCT_WRITE:
1102 for (i= disks; i-- ;)
1103 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1104 chosen = sh->dev[i].towrite;
1105 sh->dev[i].towrite = NULL;
1106
1107 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1108 wake_up(&conf->wait_for_overlap);
1109
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001110 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001111 sh->dev[i].written = chosen;
1112 }
1113 break;
1114 case CHECK_PARITY:
1115 BUG(); /* Not implemented yet */
1116 }
1117
1118 for (i = disks; i--;)
1119 if (sh->dev[i].written) {
1120 sector_t sector = sh->dev[i].sector;
1121 struct bio *wbi = sh->dev[i].written;
1122 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1123 copy_data(1, wbi, sh->dev[i].page, sector);
1124 wbi = r5_next_bio(wbi, sector);
1125 }
1126
1127 set_bit(R5_LOCKED, &sh->dev[i].flags);
1128 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1129 }
1130
1131// switch(method) {
1132// case RECONSTRUCT_WRITE:
1133// case CHECK_PARITY:
1134// case UPDATE_PARITY:
1135 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1136 /* FIX: Is this ordering of drives even remotely optimal? */
1137 count = 0;
1138 i = d0_idx;
1139 do {
1140 ptrs[count++] = page_address(sh->dev[i].page);
1141 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1142 printk("block %d/%d not uptodate on parity calc\n", i,count);
1143 i = raid6_next_disk(i, disks);
1144 } while ( i != d0_idx );
1145// break;
1146// }
1147
1148 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1149
1150 switch(method) {
1151 case RECONSTRUCT_WRITE:
1152 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1153 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1154 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1155 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1156 break;
1157 case UPDATE_PARITY:
1158 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1159 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1160 break;
1161 }
1162}
1163
1164
1165/* Compute one missing block */
1166static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1167{
1168 raid6_conf_t *conf = sh->raid_conf;
1169 int i, count, disks = conf->raid_disks;
1170 void *ptr[MAX_XOR_BLOCKS], *p;
1171 int pd_idx = sh->pd_idx;
1172 int qd_idx = raid6_next_disk(pd_idx, disks);
1173
1174 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1175 (unsigned long long)sh->sector, dd_idx);
1176
1177 if ( dd_idx == qd_idx ) {
1178 /* We're actually computing the Q drive */
1179 compute_parity6(sh, UPDATE_PARITY);
1180 } else {
1181 ptr[0] = page_address(sh->dev[dd_idx].page);
1182 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1183 count = 1;
1184 for (i = disks ; i--; ) {
1185 if (i == dd_idx || i == qd_idx)
1186 continue;
1187 p = page_address(sh->dev[i].page);
1188 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1189 ptr[count++] = p;
1190 else
1191 printk("compute_block() %d, stripe %llu, %d"
1192 " not present\n", dd_idx,
1193 (unsigned long long)sh->sector, i);
1194
1195 check_xor();
1196 }
1197 if (count != 1)
1198 xor_block(count, STRIPE_SIZE, ptr);
1199 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1200 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1201 }
1202}
1203
1204/* Compute two missing blocks */
1205static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1206{
1207 raid6_conf_t *conf = sh->raid_conf;
1208 int i, count, disks = conf->raid_disks;
1209 int pd_idx = sh->pd_idx;
1210 int qd_idx = raid6_next_disk(pd_idx, disks);
1211 int d0_idx = raid6_next_disk(qd_idx, disks);
1212 int faila, failb;
1213
1214 /* faila and failb are disk numbers relative to d0_idx */
1215 /* pd_idx become disks-2 and qd_idx become disks-1 */
1216 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1217 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1218
1219 BUG_ON(faila == failb);
1220 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1221
1222 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1223 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1224
1225 if ( failb == disks-1 ) {
1226 /* Q disk is one of the missing disks */
1227 if ( faila == disks-2 ) {
1228 /* Missing P+Q, just recompute */
1229 compute_parity6(sh, UPDATE_PARITY);
1230 return;
1231 } else {
1232 /* We're missing D+Q; recompute D from P */
1233 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1234 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1235 return;
1236 }
1237 }
1238
1239 /* We're missing D+P or D+D; build pointer table */
1240 {
1241 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1242 void *ptrs[disks];
1243
1244 count = 0;
1245 i = d0_idx;
1246 do {
1247 ptrs[count++] = page_address(sh->dev[i].page);
1248 i = raid6_next_disk(i, disks);
1249 if (i != dd_idx1 && i != dd_idx2 &&
1250 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1251 printk("compute_2 with missing block %d/%d\n", count, i);
1252 } while ( i != d0_idx );
1253
1254 if ( failb == disks-2 ) {
1255 /* We're missing D+P. */
1256 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1257 } else {
1258 /* We're missing D+D. */
1259 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1260 }
1261
1262 /* Both the above update both missing blocks */
1263 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1264 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1265 }
1266}
1267
1268
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
1271 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001272 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * The bi_next chain must be in order.
1274 */
1275static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1276{
1277 struct bio **bip;
1278 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001279 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1282 (unsigned long long)bi->bi_sector,
1283 (unsigned long long)sh->sector);
1284
1285
1286 spin_lock(&sh->lock);
1287 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001288 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001290 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1291 firstwrite = 1;
1292 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 bip = &sh->dev[dd_idx].toread;
1294 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1295 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1296 goto overlap;
1297 bip = & (*bip)->bi_next;
1298 }
1299 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1300 goto overlap;
1301
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001302 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (*bip)
1304 bi->bi_next = *bip;
1305 *bip = bi;
1306 bi->bi_phys_segments ++;
1307 spin_unlock_irq(&conf->device_lock);
1308 spin_unlock(&sh->lock);
1309
1310 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1311 (unsigned long long)bi->bi_sector,
1312 (unsigned long long)sh->sector, dd_idx);
1313
NeilBrown72626682005-09-09 16:23:54 -07001314 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001315 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1316 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001317 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001318 set_bit(STRIPE_BIT_DELAY, &sh->state);
1319 }
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (forwrite) {
1322 /* check if page is covered */
1323 sector_t sector = sh->dev[dd_idx].sector;
1324 for (bi=sh->dev[dd_idx].towrite;
1325 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1326 bi && bi->bi_sector <= sector;
1327 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1328 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1329 sector = bi->bi_sector + (bi->bi_size>>9);
1330 }
1331 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1332 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1333 }
1334 return 1;
1335
1336 overlap:
1337 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1338 spin_unlock_irq(&conf->device_lock);
1339 spin_unlock(&sh->lock);
1340 return 0;
1341}
1342
NeilBrown29269552006-03-27 01:18:10 -08001343static void end_reshape(raid5_conf_t *conf);
1344
NeilBrown16a53ec2006-06-26 00:27:38 -07001345static int page_is_zero(struct page *p)
1346{
1347 char *a = page_address(p);
1348 return ((*(u32*)a) == 0 &&
1349 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1350}
1351
NeilBrownccfcc3c2006-03-27 01:18:09 -08001352static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1353{
1354 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001355 int pd_idx, dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001356 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1357
NeilBrownb875e532006-12-10 02:20:49 -08001358 raid5_compute_sector(stripe * (disks - conf->max_degraded)
1359 *sectors_per_chunk + chunk_offset,
1360 disks, disks - conf->max_degraded,
1361 &dd_idx, &pd_idx, conf);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001362 return pd_idx;
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366/*
1367 * handle_stripe - do things to a stripe.
1368 *
1369 * We lock the stripe and then examine the state of various bits
1370 * to see what needs to be done.
1371 * Possible results:
1372 * return some read request which now have data
1373 * return some write requests which are safely on disc
1374 * schedule a read on some buffers
1375 * schedule a write of some buffers
1376 * return confirmation of parity correctness
1377 *
1378 * Parity calculations are done inside the stripe lock
1379 * buffers are taken off read_list or write_list, and bh_cache buffers
1380 * get BH_Lock set before the stripe lock is released.
1381 *
1382 */
1383
NeilBrown16a53ec2006-06-26 00:27:38 -07001384static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001387 int disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct bio *return_bi= NULL;
1389 struct bio *bi;
1390 int i;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001391 int syncing, expanding, expanded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1393 int non_overwrite = 0;
1394 int failed_num=0;
1395 struct r5dev *dev;
1396
1397 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1398 (unsigned long long)sh->sector, atomic_read(&sh->count),
1399 sh->pd_idx);
1400
1401 spin_lock(&sh->lock);
1402 clear_bit(STRIPE_HANDLE, &sh->state);
1403 clear_bit(STRIPE_DELAYED, &sh->state);
1404
1405 syncing = test_bit(STRIPE_SYNCING, &sh->state);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001406 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1407 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* Now to look around and see what can be done */
1409
NeilBrown9910f162006-01-06 00:20:24 -08001410 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 for (i=disks; i--; ) {
1412 mdk_rdev_t *rdev;
1413 dev = &sh->dev[i];
1414 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1417 i, dev->flags, dev->toread, dev->towrite, dev->written);
1418 /* maybe we can reply to a read */
1419 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1420 struct bio *rbi, *rbi2;
1421 PRINTK("Return read for disc %d\n", i);
1422 spin_lock_irq(&conf->device_lock);
1423 rbi = dev->toread;
1424 dev->toread = NULL;
1425 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1426 wake_up(&conf->wait_for_overlap);
1427 spin_unlock_irq(&conf->device_lock);
1428 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1429 copy_data(0, rbi, dev->page, dev->sector);
1430 rbi2 = r5_next_bio(rbi, dev->sector);
1431 spin_lock_irq(&conf->device_lock);
1432 if (--rbi->bi_phys_segments == 0) {
1433 rbi->bi_next = return_bi;
1434 return_bi = rbi;
1435 }
1436 spin_unlock_irq(&conf->device_lock);
1437 rbi = rbi2;
1438 }
1439 }
1440
1441 /* now count some things */
1442 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1443 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1444
1445
1446 if (dev->toread) to_read++;
1447 if (dev->towrite) {
1448 to_write++;
1449 if (!test_bit(R5_OVERWRITE, &dev->flags))
1450 non_overwrite++;
1451 }
1452 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001453 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001454 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001455 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08001456 clear_bit(R5_ReadError, &dev->flags);
1457 clear_bit(R5_ReWrite, &dev->flags);
1458 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001459 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001460 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 failed++;
1462 failed_num = i;
1463 } else
1464 set_bit(R5_Insync, &dev->flags);
1465 }
NeilBrown9910f162006-01-06 00:20:24 -08001466 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 PRINTK("locked=%d uptodate=%d to_read=%d"
1468 " to_write=%d failed=%d failed_num=%d\n",
1469 locked, uptodate, to_read, to_write, failed, failed_num);
1470 /* check if the array has lost two devices and, if so, some requests might
1471 * need to be failed
1472 */
1473 if (failed > 1 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 for (i=disks; i--; ) {
NeilBrown72626682005-09-09 16:23:54 -07001475 int bitmap_end = 0;
NeilBrown4e5314b2005-11-08 21:39:22 -08001476
1477 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001478 mdk_rdev_t *rdev;
1479 rcu_read_lock();
1480 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001481 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001482 /* multiple read failures in one stripe */
1483 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001484 rcu_read_unlock();
NeilBrown4e5314b2005-11-08 21:39:22 -08001485 }
1486
NeilBrown72626682005-09-09 16:23:54 -07001487 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /* fail all writes first */
1489 bi = sh->dev[i].towrite;
1490 sh->dev[i].towrite = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001491 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1494 wake_up(&conf->wait_for_overlap);
1495
1496 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1497 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1498 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1499 if (--bi->bi_phys_segments == 0) {
1500 md_write_end(conf->mddev);
1501 bi->bi_next = return_bi;
1502 return_bi = bi;
1503 }
1504 bi = nextbi;
1505 }
1506 /* and fail all 'written' */
1507 bi = sh->dev[i].written;
1508 sh->dev[i].written = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001509 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1511 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1512 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1513 if (--bi->bi_phys_segments == 0) {
1514 md_write_end(conf->mddev);
1515 bi->bi_next = return_bi;
1516 return_bi = bi;
1517 }
1518 bi = bi2;
1519 }
1520
1521 /* fail any reads if this device is non-operational */
NeilBrown4e5314b2005-11-08 21:39:22 -08001522 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1523 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 bi = sh->dev[i].toread;
1525 sh->dev[i].toread = NULL;
1526 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1527 wake_up(&conf->wait_for_overlap);
1528 if (bi) to_read--;
1529 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1530 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1531 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1532 if (--bi->bi_phys_segments == 0) {
1533 bi->bi_next = return_bi;
1534 return_bi = bi;
1535 }
1536 bi = nextbi;
1537 }
1538 }
NeilBrown72626682005-09-09 16:23:54 -07001539 spin_unlock_irq(&conf->device_lock);
1540 if (bitmap_end)
1541 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1542 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 if (failed > 1 && syncing) {
1546 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1547 clear_bit(STRIPE_SYNCING, &sh->state);
1548 syncing = 0;
1549 }
1550
1551 /* might be able to return some write requests if the parity block
1552 * is safe, or on a failed drive
1553 */
1554 dev = &sh->dev[sh->pd_idx];
1555 if ( written &&
1556 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1557 test_bit(R5_UPTODATE, &dev->flags))
1558 || (failed == 1 && failed_num == sh->pd_idx))
1559 ) {
1560 /* any written block on an uptodate or failed drive can be returned.
1561 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1562 * never LOCKED, so we don't need to test 'failed' directly.
1563 */
1564 for (i=disks; i--; )
1565 if (sh->dev[i].written) {
1566 dev = &sh->dev[i];
1567 if (!test_bit(R5_LOCKED, &dev->flags) &&
1568 test_bit(R5_UPTODATE, &dev->flags) ) {
1569 /* We can return any write requests */
1570 struct bio *wbi, *wbi2;
NeilBrown72626682005-09-09 16:23:54 -07001571 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 PRINTK("Return write for disc %d\n", i);
1573 spin_lock_irq(&conf->device_lock);
1574 wbi = dev->written;
1575 dev->written = NULL;
1576 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1577 wbi2 = r5_next_bio(wbi, dev->sector);
1578 if (--wbi->bi_phys_segments == 0) {
1579 md_write_end(conf->mddev);
1580 wbi->bi_next = return_bi;
1581 return_bi = wbi;
1582 }
1583 wbi = wbi2;
1584 }
NeilBrown72626682005-09-09 16:23:54 -07001585 if (dev->towrite == NULL)
1586 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001588 if (bitmap_end)
1589 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1590 STRIPE_SECTORS,
1591 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 }
1594 }
1595
1596 /* Now we might consider reading some blocks, either to check/generate
1597 * parity, or to satisfy requests
1598 * or to load a block that is being partially written.
1599 */
NeilBrownccfcc3c2006-03-27 01:18:09 -08001600 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 for (i=disks; i--;) {
1602 dev = &sh->dev[i];
1603 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1604 (dev->toread ||
1605 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1606 syncing ||
NeilBrownccfcc3c2006-03-27 01:18:09 -08001607 expanding ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 (failed && (sh->dev[failed_num].toread ||
1609 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1610 )
1611 ) {
1612 /* we would like to get this block, possibly
1613 * by computing it, but we might not be able to
1614 */
1615 if (uptodate == disks-1) {
1616 PRINTK("Computing block %d\n", i);
1617 compute_block(sh, i);
1618 uptodate++;
1619 } else if (test_bit(R5_Insync, &dev->flags)) {
1620 set_bit(R5_LOCKED, &dev->flags);
1621 set_bit(R5_Wantread, &dev->flags);
1622#if 0
1623 /* if I am just reading this block and we don't have
1624 a failed drive, or any pending writes then sidestep the cache */
1625 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1626 ! syncing && !failed && !to_write) {
1627 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1628 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1629 }
1630#endif
1631 locked++;
1632 PRINTK("Reading block %d (sync=%d)\n",
1633 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635 }
1636 }
1637 set_bit(STRIPE_HANDLE, &sh->state);
1638 }
1639
1640 /* now to consider writing and what else, if anything should be read */
1641 if (to_write) {
1642 int rmw=0, rcw=0;
1643 for (i=disks ; i--;) {
1644 /* would I have to read this buffer for read_modify_write */
1645 dev = &sh->dev[i];
1646 if ((dev->towrite || i == sh->pd_idx) &&
1647 (!test_bit(R5_LOCKED, &dev->flags)
1648#if 0
1649|| sh->bh_page[i]!=bh->b_page
1650#endif
1651 ) &&
1652 !test_bit(R5_UPTODATE, &dev->flags)) {
1653 if (test_bit(R5_Insync, &dev->flags)
1654/* && !(!mddev->insync && i == sh->pd_idx) */
1655 )
1656 rmw++;
1657 else rmw += 2*disks; /* cannot read it */
1658 }
1659 /* Would I have to read this buffer for reconstruct_write */
1660 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1661 (!test_bit(R5_LOCKED, &dev->flags)
1662#if 0
1663|| sh->bh_page[i] != bh->b_page
1664#endif
1665 ) &&
1666 !test_bit(R5_UPTODATE, &dev->flags)) {
1667 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1668 else rcw += 2*disks;
1669 }
1670 }
1671 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1672 (unsigned long long)sh->sector, rmw, rcw);
1673 set_bit(STRIPE_HANDLE, &sh->state);
1674 if (rmw < rcw && rmw > 0)
1675 /* prefer read-modify-write, but need to get some data */
1676 for (i=disks; i--;) {
1677 dev = &sh->dev[i];
1678 if ((dev->towrite || i == sh->pd_idx) &&
1679 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1680 test_bit(R5_Insync, &dev->flags)) {
1681 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1682 {
1683 PRINTK("Read_old block %d for r-m-w\n", i);
1684 set_bit(R5_LOCKED, &dev->flags);
1685 set_bit(R5_Wantread, &dev->flags);
1686 locked++;
1687 } else {
1688 set_bit(STRIPE_DELAYED, &sh->state);
1689 set_bit(STRIPE_HANDLE, &sh->state);
1690 }
1691 }
1692 }
1693 if (rcw <= rmw && rcw > 0)
1694 /* want reconstruct write, but need to get some data */
1695 for (i=disks; i--;) {
1696 dev = &sh->dev[i];
1697 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1698 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1699 test_bit(R5_Insync, &dev->flags)) {
1700 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1701 {
1702 PRINTK("Read_old block %d for Reconstruct\n", i);
1703 set_bit(R5_LOCKED, &dev->flags);
1704 set_bit(R5_Wantread, &dev->flags);
1705 locked++;
1706 } else {
1707 set_bit(STRIPE_DELAYED, &sh->state);
1708 set_bit(STRIPE_HANDLE, &sh->state);
1709 }
1710 }
1711 }
1712 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown72626682005-09-09 16:23:54 -07001713 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1714 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 PRINTK("Computing parity...\n");
NeilBrown16a53ec2006-06-26 00:27:38 -07001716 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /* now every locked buffer is ready to be written */
1718 for (i=disks; i--;)
1719 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1720 PRINTK("Writing block %d\n", i);
1721 locked++;
1722 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1723 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1724 || (i==sh->pd_idx && failed == 0))
1725 set_bit(STRIPE_INSYNC, &sh->state);
1726 }
1727 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1728 atomic_dec(&conf->preread_active_stripes);
1729 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1730 md_wakeup_thread(conf->mddev->thread);
1731 }
1732 }
1733 }
1734
1735 /* maybe we need to check and possibly fix the parity for this stripe
1736 * Any reads will already have been scheduled, so we just see if enough data
1737 * is available
1738 */
1739 if (syncing && locked == 0 &&
NeilBrown14f8d262006-01-06 00:20:14 -08001740 !test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 set_bit(STRIPE_HANDLE, &sh->state);
1742 if (failed == 0) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001743 BUG_ON(uptodate != disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001744 compute_parity5(sh, CHECK_PARITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 uptodate--;
NeilBrown16a53ec2006-06-26 00:27:38 -07001746 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* parity is correct (on disc, not in buffer any more) */
1748 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown9d888832005-11-08 21:39:26 -08001749 } else {
1750 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1751 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1752 /* don't try to repair!! */
1753 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown14f8d262006-01-06 00:20:14 -08001754 else {
1755 compute_block(sh, sh->pd_idx);
1756 uptodate++;
1757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 }
1760 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001761 /* either failed parity check, or recovery is happening */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (failed==0)
1763 failed_num = sh->pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 dev = &sh->dev[failed_num];
NeilBrown14f8d262006-01-06 00:20:14 -08001765 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1766 BUG_ON(uptodate != disks);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 set_bit(R5_LOCKED, &dev->flags);
1769 set_bit(R5_Wantwrite, &dev->flags);
NeilBrown72626682005-09-09 16:23:54 -07001770 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 locked++;
1772 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 }
1775 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1776 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1777 clear_bit(STRIPE_SYNCING, &sh->state);
1778 }
NeilBrown4e5314b2005-11-08 21:39:22 -08001779
1780 /* If the failed drive is just a ReadError, then we might need to progress
1781 * the repair/check process
1782 */
NeilBrownba22dcb2005-11-08 21:39:31 -08001783 if (failed == 1 && ! conf->mddev->ro &&
1784 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001785 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1786 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1787 ) {
1788 dev = &sh->dev[failed_num];
1789 if (!test_bit(R5_ReWrite, &dev->flags)) {
1790 set_bit(R5_Wantwrite, &dev->flags);
1791 set_bit(R5_ReWrite, &dev->flags);
1792 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001793 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001794 } else {
1795 /* let's read it back */
1796 set_bit(R5_Wantread, &dev->flags);
1797 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001798 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001799 }
1800 }
1801
NeilBrownccfcc3c2006-03-27 01:18:09 -08001802 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1803 /* Need to write out all blocks after computing parity */
1804 sh->disks = conf->raid_disks;
1805 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001806 compute_parity5(sh, RECONSTRUCT_WRITE);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001807 for (i= conf->raid_disks; i--;) {
1808 set_bit(R5_LOCKED, &sh->dev[i].flags);
1809 locked++;
1810 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1811 }
1812 clear_bit(STRIPE_EXPANDING, &sh->state);
1813 } else if (expanded) {
1814 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08001815 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001816 wake_up(&conf->wait_for_overlap);
1817 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1818 }
1819
1820 if (expanding && locked == 0) {
1821 /* We have read all the blocks in this stripe and now we need to
1822 * copy some of them into a target stripe for expand.
1823 */
1824 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1825 for (i=0; i< sh->disks; i++)
1826 if (i != sh->pd_idx) {
1827 int dd_idx, pd_idx, j;
1828 struct stripe_head *sh2;
1829
1830 sector_t bn = compute_blocknr(sh, i);
1831 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1832 conf->raid_disks-1,
1833 &dd_idx, &pd_idx, conf);
1834 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1835 if (sh2 == NULL)
1836 /* so far only the early blocks of this stripe
1837 * have been requested. When later blocks
1838 * get requested, we will try again
1839 */
1840 continue;
1841 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1842 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1843 /* must have already done this block */
1844 release_stripe(sh2);
1845 continue;
1846 }
1847 memcpy(page_address(sh2->dev[dd_idx].page),
1848 page_address(sh->dev[i].page),
1849 STRIPE_SIZE);
1850 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1851 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1852 for (j=0; j<conf->raid_disks; j++)
1853 if (j != sh2->pd_idx &&
1854 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1855 break;
1856 if (j == conf->raid_disks) {
1857 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1858 set_bit(STRIPE_HANDLE, &sh2->state);
1859 }
1860 release_stripe(sh2);
1861 }
1862 }
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 spin_unlock(&sh->lock);
1865
1866 while ((bi=return_bi)) {
1867 int bytes = bi->bi_size;
1868
1869 return_bi = bi->bi_next;
1870 bi->bi_next = NULL;
1871 bi->bi_size = 0;
1872 bi->bi_end_io(bi, bytes, 0);
1873 }
1874 for (i=disks; i-- ;) {
1875 int rw;
1876 struct bio *bi;
1877 mdk_rdev_t *rdev;
1878 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1879 rw = 1;
1880 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1881 rw = 0;
1882 else
1883 continue;
1884
1885 bi = &sh->dev[i].req;
1886
1887 bi->bi_rw = rw;
1888 if (rw)
1889 bi->bi_end_io = raid5_end_write_request;
1890 else
1891 bi->bi_end_io = raid5_end_read_request;
1892
1893 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001894 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001895 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 rdev = NULL;
1897 if (rdev)
1898 atomic_inc(&rdev->nr_pending);
1899 rcu_read_unlock();
1900
1901 if (rdev) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08001902 if (syncing || expanding || expanded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1904
1905 bi->bi_bdev = rdev->bdev;
1906 PRINTK("for %llu schedule op %ld on disc %d\n",
1907 (unsigned long long)sh->sector, bi->bi_rw, i);
1908 atomic_inc(&sh->count);
1909 bi->bi_sector = sh->sector + rdev->data_offset;
1910 bi->bi_flags = 1 << BIO_UPTODATE;
1911 bi->bi_vcnt = 1;
1912 bi->bi_max_vecs = 1;
1913 bi->bi_idx = 0;
1914 bi->bi_io_vec = &sh->dev[i].vec;
1915 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1916 bi->bi_io_vec[0].bv_offset = 0;
1917 bi->bi_size = STRIPE_SIZE;
1918 bi->bi_next = NULL;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001919 if (rw == WRITE &&
1920 test_bit(R5_ReWrite, &sh->dev[i].flags))
1921 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 generic_make_request(bi);
1923 } else {
NeilBrown72626682005-09-09 16:23:54 -07001924 if (rw == 1)
1925 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 PRINTK("skip op %ld on disc %d for sector %llu\n",
1927 bi->bi_rw, i, (unsigned long long)sh->sector);
1928 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1929 set_bit(STRIPE_HANDLE, &sh->state);
1930 }
1931 }
1932}
1933
NeilBrown16a53ec2006-06-26 00:27:38 -07001934static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1935{
1936 raid6_conf_t *conf = sh->raid_conf;
1937 int disks = conf->raid_disks;
1938 struct bio *return_bi= NULL;
1939 struct bio *bi;
1940 int i;
1941 int syncing;
1942 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1943 int non_overwrite = 0;
1944 int failed_num[2] = {0, 0};
1945 struct r5dev *dev, *pdev, *qdev;
1946 int pd_idx = sh->pd_idx;
1947 int qd_idx = raid6_next_disk(pd_idx, disks);
1948 int p_failed, q_failed;
1949
1950 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1951 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1952 pd_idx, qd_idx);
1953
1954 spin_lock(&sh->lock);
1955 clear_bit(STRIPE_HANDLE, &sh->state);
1956 clear_bit(STRIPE_DELAYED, &sh->state);
1957
1958 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1959 /* Now to look around and see what can be done */
1960
1961 rcu_read_lock();
1962 for (i=disks; i--; ) {
1963 mdk_rdev_t *rdev;
1964 dev = &sh->dev[i];
1965 clear_bit(R5_Insync, &dev->flags);
1966
1967 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1968 i, dev->flags, dev->toread, dev->towrite, dev->written);
1969 /* maybe we can reply to a read */
1970 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1971 struct bio *rbi, *rbi2;
1972 PRINTK("Return read for disc %d\n", i);
1973 spin_lock_irq(&conf->device_lock);
1974 rbi = dev->toread;
1975 dev->toread = NULL;
1976 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1977 wake_up(&conf->wait_for_overlap);
1978 spin_unlock_irq(&conf->device_lock);
1979 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1980 copy_data(0, rbi, dev->page, dev->sector);
1981 rbi2 = r5_next_bio(rbi, dev->sector);
1982 spin_lock_irq(&conf->device_lock);
1983 if (--rbi->bi_phys_segments == 0) {
1984 rbi->bi_next = return_bi;
1985 return_bi = rbi;
1986 }
1987 spin_unlock_irq(&conf->device_lock);
1988 rbi = rbi2;
1989 }
1990 }
1991
1992 /* now count some things */
1993 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1994 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1995
1996
1997 if (dev->toread) to_read++;
1998 if (dev->towrite) {
1999 to_write++;
2000 if (!test_bit(R5_OVERWRITE, &dev->flags))
2001 non_overwrite++;
2002 }
2003 if (dev->written) written++;
2004 rdev = rcu_dereference(conf->disks[i].rdev);
2005 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
2006 /* The ReadError flag will just be confusing now */
2007 clear_bit(R5_ReadError, &dev->flags);
2008 clear_bit(R5_ReWrite, &dev->flags);
2009 }
2010 if (!rdev || !test_bit(In_sync, &rdev->flags)
2011 || test_bit(R5_ReadError, &dev->flags)) {
2012 if ( failed < 2 )
2013 failed_num[failed] = i;
2014 failed++;
2015 } else
2016 set_bit(R5_Insync, &dev->flags);
2017 }
2018 rcu_read_unlock();
2019 PRINTK("locked=%d uptodate=%d to_read=%d"
2020 " to_write=%d failed=%d failed_num=%d,%d\n",
2021 locked, uptodate, to_read, to_write, failed,
2022 failed_num[0], failed_num[1]);
2023 /* check if the array has lost >2 devices and, if so, some requests might
2024 * need to be failed
2025 */
2026 if (failed > 2 && to_read+to_write+written) {
2027 for (i=disks; i--; ) {
2028 int bitmap_end = 0;
2029
2030 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2031 mdk_rdev_t *rdev;
2032 rcu_read_lock();
2033 rdev = rcu_dereference(conf->disks[i].rdev);
2034 if (rdev && test_bit(In_sync, &rdev->flags))
2035 /* multiple read failures in one stripe */
2036 md_error(conf->mddev, rdev);
2037 rcu_read_unlock();
2038 }
2039
2040 spin_lock_irq(&conf->device_lock);
2041 /* fail all writes first */
2042 bi = sh->dev[i].towrite;
2043 sh->dev[i].towrite = NULL;
2044 if (bi) { to_write--; bitmap_end = 1; }
2045
2046 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2047 wake_up(&conf->wait_for_overlap);
2048
2049 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2050 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2051 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2052 if (--bi->bi_phys_segments == 0) {
2053 md_write_end(conf->mddev);
2054 bi->bi_next = return_bi;
2055 return_bi = bi;
2056 }
2057 bi = nextbi;
2058 }
2059 /* and fail all 'written' */
2060 bi = sh->dev[i].written;
2061 sh->dev[i].written = NULL;
2062 if (bi) bitmap_end = 1;
2063 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2064 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2065 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2066 if (--bi->bi_phys_segments == 0) {
2067 md_write_end(conf->mddev);
2068 bi->bi_next = return_bi;
2069 return_bi = bi;
2070 }
2071 bi = bi2;
2072 }
2073
2074 /* fail any reads if this device is non-operational */
2075 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2076 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2077 bi = sh->dev[i].toread;
2078 sh->dev[i].toread = NULL;
2079 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2080 wake_up(&conf->wait_for_overlap);
2081 if (bi) to_read--;
2082 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2083 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2084 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2085 if (--bi->bi_phys_segments == 0) {
2086 bi->bi_next = return_bi;
2087 return_bi = bi;
2088 }
2089 bi = nextbi;
2090 }
2091 }
2092 spin_unlock_irq(&conf->device_lock);
2093 if (bitmap_end)
2094 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2095 STRIPE_SECTORS, 0, 0);
2096 }
2097 }
2098 if (failed > 2 && syncing) {
2099 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2100 clear_bit(STRIPE_SYNCING, &sh->state);
2101 syncing = 0;
2102 }
2103
2104 /*
2105 * might be able to return some write requests if the parity blocks
2106 * are safe, or on a failed drive
2107 */
2108 pdev = &sh->dev[pd_idx];
2109 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2110 || (failed >= 2 && failed_num[1] == pd_idx);
2111 qdev = &sh->dev[qd_idx];
2112 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2113 || (failed >= 2 && failed_num[1] == qd_idx);
2114
2115 if ( written &&
2116 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2117 && !test_bit(R5_LOCKED, &pdev->flags)
2118 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2119 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2120 && !test_bit(R5_LOCKED, &qdev->flags)
2121 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2122 /* any written block on an uptodate or failed drive can be
2123 * returned. Note that if we 'wrote' to a failed drive,
2124 * it will be UPTODATE, but never LOCKED, so we don't need
2125 * to test 'failed' directly.
2126 */
2127 for (i=disks; i--; )
2128 if (sh->dev[i].written) {
2129 dev = &sh->dev[i];
2130 if (!test_bit(R5_LOCKED, &dev->flags) &&
2131 test_bit(R5_UPTODATE, &dev->flags) ) {
2132 /* We can return any write requests */
2133 int bitmap_end = 0;
2134 struct bio *wbi, *wbi2;
2135 PRINTK("Return write for stripe %llu disc %d\n",
2136 (unsigned long long)sh->sector, i);
2137 spin_lock_irq(&conf->device_lock);
2138 wbi = dev->written;
2139 dev->written = NULL;
2140 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2141 wbi2 = r5_next_bio(wbi, dev->sector);
2142 if (--wbi->bi_phys_segments == 0) {
2143 md_write_end(conf->mddev);
2144 wbi->bi_next = return_bi;
2145 return_bi = wbi;
2146 }
2147 wbi = wbi2;
2148 }
2149 if (dev->towrite == NULL)
2150 bitmap_end = 1;
2151 spin_unlock_irq(&conf->device_lock);
2152 if (bitmap_end)
2153 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2154 STRIPE_SECTORS,
2155 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2156 }
2157 }
2158 }
2159
2160 /* Now we might consider reading some blocks, either to check/generate
2161 * parity, or to satisfy requests
2162 * or to load a block that is being partially written.
2163 */
2164 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
2165 for (i=disks; i--;) {
2166 dev = &sh->dev[i];
2167 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2168 (dev->toread ||
2169 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2170 syncing ||
2171 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2172 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2173 )
2174 ) {
2175 /* we would like to get this block, possibly
2176 * by computing it, but we might not be able to
2177 */
2178 if (uptodate == disks-1) {
2179 PRINTK("Computing stripe %llu block %d\n",
2180 (unsigned long long)sh->sector, i);
2181 compute_block_1(sh, i, 0);
2182 uptodate++;
2183 } else if ( uptodate == disks-2 && failed >= 2 ) {
2184 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2185 int other;
2186 for (other=disks; other--;) {
2187 if ( other == i )
2188 continue;
2189 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2190 break;
2191 }
2192 BUG_ON(other < 0);
2193 PRINTK("Computing stripe %llu blocks %d,%d\n",
2194 (unsigned long long)sh->sector, i, other);
2195 compute_block_2(sh, i, other);
2196 uptodate += 2;
2197 } else if (test_bit(R5_Insync, &dev->flags)) {
2198 set_bit(R5_LOCKED, &dev->flags);
2199 set_bit(R5_Wantread, &dev->flags);
2200#if 0
2201 /* if I am just reading this block and we don't have
2202 a failed drive, or any pending writes then sidestep the cache */
2203 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
2204 ! syncing && !failed && !to_write) {
2205 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
2206 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
2207 }
2208#endif
2209 locked++;
2210 PRINTK("Reading block %d (sync=%d)\n",
2211 i, syncing);
2212 }
2213 }
2214 }
2215 set_bit(STRIPE_HANDLE, &sh->state);
2216 }
2217
2218 /* now to consider writing and what else, if anything should be read */
2219 if (to_write) {
2220 int rcw=0, must_compute=0;
2221 for (i=disks ; i--;) {
2222 dev = &sh->dev[i];
2223 /* Would I have to read this buffer for reconstruct_write */
2224 if (!test_bit(R5_OVERWRITE, &dev->flags)
2225 && i != pd_idx && i != qd_idx
2226 && (!test_bit(R5_LOCKED, &dev->flags)
2227#if 0
2228 || sh->bh_page[i] != bh->b_page
2229#endif
2230 ) &&
2231 !test_bit(R5_UPTODATE, &dev->flags)) {
2232 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2233 else {
2234 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2235 must_compute++;
2236 }
2237 }
2238 }
2239 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2240 (unsigned long long)sh->sector, rcw, must_compute);
2241 set_bit(STRIPE_HANDLE, &sh->state);
2242
2243 if (rcw > 0)
2244 /* want reconstruct write, but need to get some data */
2245 for (i=disks; i--;) {
2246 dev = &sh->dev[i];
2247 if (!test_bit(R5_OVERWRITE, &dev->flags)
2248 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2249 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2250 test_bit(R5_Insync, &dev->flags)) {
2251 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2252 {
2253 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2254 (unsigned long long)sh->sector, i);
2255 set_bit(R5_LOCKED, &dev->flags);
2256 set_bit(R5_Wantread, &dev->flags);
2257 locked++;
2258 } else {
2259 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2260 (unsigned long long)sh->sector, i);
2261 set_bit(STRIPE_DELAYED, &sh->state);
2262 set_bit(STRIPE_HANDLE, &sh->state);
2263 }
2264 }
2265 }
2266 /* now if nothing is locked, and if we have enough data, we can start a write request */
2267 if (locked == 0 && rcw == 0 &&
2268 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2269 if ( must_compute > 0 ) {
2270 /* We have failed blocks and need to compute them */
2271 switch ( failed ) {
2272 case 0: BUG();
2273 case 1: compute_block_1(sh, failed_num[0], 0); break;
2274 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2275 default: BUG(); /* This request should have been failed? */
2276 }
2277 }
2278
2279 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2280 compute_parity6(sh, RECONSTRUCT_WRITE);
2281 /* now every locked buffer is ready to be written */
2282 for (i=disks; i--;)
2283 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2284 PRINTK("Writing stripe %llu block %d\n",
2285 (unsigned long long)sh->sector, i);
2286 locked++;
2287 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2288 }
2289 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2290 set_bit(STRIPE_INSYNC, &sh->state);
2291
2292 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2293 atomic_dec(&conf->preread_active_stripes);
2294 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2295 md_wakeup_thread(conf->mddev->thread);
2296 }
2297 }
2298 }
2299
2300 /* maybe we need to check and possibly fix the parity for this stripe
2301 * Any reads will already have been scheduled, so we just see if enough data
2302 * is available
2303 */
2304 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2305 int update_p = 0, update_q = 0;
2306 struct r5dev *dev;
2307
2308 set_bit(STRIPE_HANDLE, &sh->state);
2309
2310 BUG_ON(failed>2);
2311 BUG_ON(uptodate < disks);
2312 /* Want to check and possibly repair P and Q.
2313 * However there could be one 'failed' device, in which
2314 * case we can only check one of them, possibly using the
2315 * other to generate missing data
2316 */
2317
2318 /* If !tmp_page, we cannot do the calculations,
2319 * but as we have set STRIPE_HANDLE, we will soon be called
2320 * by stripe_handle with a tmp_page - just wait until then.
2321 */
2322 if (tmp_page) {
2323 if (failed == q_failed) {
2324 /* The only possible failed device holds 'Q', so it makes
2325 * sense to check P (If anything else were failed, we would
2326 * have used P to recreate it).
2327 */
2328 compute_block_1(sh, pd_idx, 1);
2329 if (!page_is_zero(sh->dev[pd_idx].page)) {
2330 compute_block_1(sh,pd_idx,0);
2331 update_p = 1;
2332 }
2333 }
2334 if (!q_failed && failed < 2) {
2335 /* q is not failed, and we didn't use it to generate
2336 * anything, so it makes sense to check it
2337 */
2338 memcpy(page_address(tmp_page),
2339 page_address(sh->dev[qd_idx].page),
2340 STRIPE_SIZE);
2341 compute_parity6(sh, UPDATE_PARITY);
2342 if (memcmp(page_address(tmp_page),
2343 page_address(sh->dev[qd_idx].page),
2344 STRIPE_SIZE)!= 0) {
2345 clear_bit(STRIPE_INSYNC, &sh->state);
2346 update_q = 1;
2347 }
2348 }
2349 if (update_p || update_q) {
2350 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2351 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2352 /* don't try to repair!! */
2353 update_p = update_q = 0;
2354 }
2355
2356 /* now write out any block on a failed drive,
2357 * or P or Q if they need it
2358 */
2359
2360 if (failed == 2) {
2361 dev = &sh->dev[failed_num[1]];
2362 locked++;
2363 set_bit(R5_LOCKED, &dev->flags);
2364 set_bit(R5_Wantwrite, &dev->flags);
2365 }
2366 if (failed >= 1) {
2367 dev = &sh->dev[failed_num[0]];
2368 locked++;
2369 set_bit(R5_LOCKED, &dev->flags);
2370 set_bit(R5_Wantwrite, &dev->flags);
2371 }
2372
2373 if (update_p) {
2374 dev = &sh->dev[pd_idx];
2375 locked ++;
2376 set_bit(R5_LOCKED, &dev->flags);
2377 set_bit(R5_Wantwrite, &dev->flags);
2378 }
2379 if (update_q) {
2380 dev = &sh->dev[qd_idx];
2381 locked++;
2382 set_bit(R5_LOCKED, &dev->flags);
2383 set_bit(R5_Wantwrite, &dev->flags);
2384 }
2385 clear_bit(STRIPE_DEGRADED, &sh->state);
2386
2387 set_bit(STRIPE_INSYNC, &sh->state);
2388 }
2389 }
2390
2391 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2392 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2393 clear_bit(STRIPE_SYNCING, &sh->state);
2394 }
2395
2396 /* If the failed drives are just a ReadError, then we might need
2397 * to progress the repair/check process
2398 */
2399 if (failed <= 2 && ! conf->mddev->ro)
2400 for (i=0; i<failed;i++) {
2401 dev = &sh->dev[failed_num[i]];
2402 if (test_bit(R5_ReadError, &dev->flags)
2403 && !test_bit(R5_LOCKED, &dev->flags)
2404 && test_bit(R5_UPTODATE, &dev->flags)
2405 ) {
2406 if (!test_bit(R5_ReWrite, &dev->flags)) {
2407 set_bit(R5_Wantwrite, &dev->flags);
2408 set_bit(R5_ReWrite, &dev->flags);
2409 set_bit(R5_LOCKED, &dev->flags);
2410 } else {
2411 /* let's read it back */
2412 set_bit(R5_Wantread, &dev->flags);
2413 set_bit(R5_LOCKED, &dev->flags);
2414 }
2415 }
2416 }
2417 spin_unlock(&sh->lock);
2418
2419 while ((bi=return_bi)) {
2420 int bytes = bi->bi_size;
2421
2422 return_bi = bi->bi_next;
2423 bi->bi_next = NULL;
2424 bi->bi_size = 0;
2425 bi->bi_end_io(bi, bytes, 0);
2426 }
2427 for (i=disks; i-- ;) {
2428 int rw;
2429 struct bio *bi;
2430 mdk_rdev_t *rdev;
2431 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
2432 rw = 1;
2433 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
2434 rw = 0;
2435 else
2436 continue;
2437
2438 bi = &sh->dev[i].req;
2439
2440 bi->bi_rw = rw;
2441 if (rw)
2442 bi->bi_end_io = raid5_end_write_request;
2443 else
2444 bi->bi_end_io = raid5_end_read_request;
2445
2446 rcu_read_lock();
2447 rdev = rcu_dereference(conf->disks[i].rdev);
2448 if (rdev && test_bit(Faulty, &rdev->flags))
2449 rdev = NULL;
2450 if (rdev)
2451 atomic_inc(&rdev->nr_pending);
2452 rcu_read_unlock();
2453
2454 if (rdev) {
2455 if (syncing)
2456 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2457
2458 bi->bi_bdev = rdev->bdev;
2459 PRINTK("for %llu schedule op %ld on disc %d\n",
2460 (unsigned long long)sh->sector, bi->bi_rw, i);
2461 atomic_inc(&sh->count);
2462 bi->bi_sector = sh->sector + rdev->data_offset;
2463 bi->bi_flags = 1 << BIO_UPTODATE;
2464 bi->bi_vcnt = 1;
2465 bi->bi_max_vecs = 1;
2466 bi->bi_idx = 0;
2467 bi->bi_io_vec = &sh->dev[i].vec;
2468 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2469 bi->bi_io_vec[0].bv_offset = 0;
2470 bi->bi_size = STRIPE_SIZE;
2471 bi->bi_next = NULL;
2472 if (rw == WRITE &&
2473 test_bit(R5_ReWrite, &sh->dev[i].flags))
2474 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2475 generic_make_request(bi);
2476 } else {
2477 if (rw == 1)
2478 set_bit(STRIPE_DEGRADED, &sh->state);
2479 PRINTK("skip op %ld on disc %d for sector %llu\n",
2480 bi->bi_rw, i, (unsigned long long)sh->sector);
2481 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2482 set_bit(STRIPE_HANDLE, &sh->state);
2483 }
2484 }
2485}
2486
2487static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2488{
2489 if (sh->raid_conf->level == 6)
2490 handle_stripe6(sh, tmp_page);
2491 else
2492 handle_stripe5(sh);
2493}
2494
2495
2496
Arjan van de Ven858119e2006-01-14 13:20:43 -08002497static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2500 while (!list_empty(&conf->delayed_list)) {
2501 struct list_head *l = conf->delayed_list.next;
2502 struct stripe_head *sh;
2503 sh = list_entry(l, struct stripe_head, lru);
2504 list_del_init(l);
2505 clear_bit(STRIPE_DELAYED, &sh->state);
2506 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2507 atomic_inc(&conf->preread_active_stripes);
2508 list_add_tail(&sh->lru, &conf->handle_list);
2509 }
2510 }
2511}
2512
Arjan van de Ven858119e2006-01-14 13:20:43 -08002513static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07002514{
2515 /* device_lock is held */
2516 struct list_head head;
2517 list_add(&head, &conf->bitmap_list);
2518 list_del_init(&conf->bitmap_list);
2519 while (!list_empty(&head)) {
2520 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2521 list_del_init(&sh->lru);
2522 atomic_inc(&sh->count);
2523 __release_stripe(conf, sh);
2524 }
2525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527static void unplug_slaves(mddev_t *mddev)
2528{
2529 raid5_conf_t *conf = mddev_to_conf(mddev);
2530 int i;
2531
2532 rcu_read_lock();
2533 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002534 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002535 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2537
2538 atomic_inc(&rdev->nr_pending);
2539 rcu_read_unlock();
2540
2541 if (r_queue->unplug_fn)
2542 r_queue->unplug_fn(r_queue);
2543
2544 rdev_dec_pending(rdev, mddev);
2545 rcu_read_lock();
2546 }
2547 }
2548 rcu_read_unlock();
2549}
2550
2551static void raid5_unplug_device(request_queue_t *q)
2552{
2553 mddev_t *mddev = q->queuedata;
2554 raid5_conf_t *conf = mddev_to_conf(mddev);
2555 unsigned long flags;
2556
2557 spin_lock_irqsave(&conf->device_lock, flags);
2558
NeilBrown72626682005-09-09 16:23:54 -07002559 if (blk_remove_plug(q)) {
2560 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07002562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 md_wakeup_thread(mddev->thread);
2564
2565 spin_unlock_irqrestore(&conf->device_lock, flags);
2566
2567 unplug_slaves(mddev);
2568}
2569
2570static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2571 sector_t *error_sector)
2572{
2573 mddev_t *mddev = q->queuedata;
2574 raid5_conf_t *conf = mddev_to_conf(mddev);
2575 int i, ret = 0;
2576
2577 rcu_read_lock();
2578 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002579 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002580 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 struct block_device *bdev = rdev->bdev;
2582 request_queue_t *r_queue = bdev_get_queue(bdev);
2583
2584 if (!r_queue->issue_flush_fn)
2585 ret = -EOPNOTSUPP;
2586 else {
2587 atomic_inc(&rdev->nr_pending);
2588 rcu_read_unlock();
2589 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2590 error_sector);
2591 rdev_dec_pending(rdev, mddev);
2592 rcu_read_lock();
2593 }
2594 }
2595 }
2596 rcu_read_unlock();
2597 return ret;
2598}
2599
NeilBrownf022b2f2006-10-03 01:15:56 -07002600static int raid5_congested(void *data, int bits)
2601{
2602 mddev_t *mddev = data;
2603 raid5_conf_t *conf = mddev_to_conf(mddev);
2604
2605 /* No difference between reads and writes. Just check
2606 * how busy the stripe_cache is
2607 */
2608 if (conf->inactive_blocked)
2609 return 1;
2610 if (conf->quiesce)
2611 return 1;
2612 if (list_empty_careful(&conf->inactive_list))
2613 return 1;
2614
2615 return 0;
2616}
2617
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08002618/* We want read requests to align with chunks where possible,
2619 * but write requests don't need to.
2620 */
2621static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
2622{
2623 mddev_t *mddev = q->queuedata;
2624 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2625 int max;
2626 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2627 unsigned int bio_sectors = bio->bi_size >> 9;
2628
2629 if (bio_data_dir(bio))
2630 return biovec->bv_len; /* always allow writes to be mergeable */
2631
2632 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
2633 if (max < 0) max = 0;
2634 if (max <= biovec->bv_len && bio_sectors == 0)
2635 return biovec->bv_len;
2636 else
2637 return max;
2638}
2639
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002640
2641static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
2642{
2643 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2644 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2645 unsigned int bio_sectors = bio->bi_size >> 9;
2646
2647 return chunk_sectors >=
2648 ((sector & (chunk_sectors - 1)) + bio_sectors);
2649}
2650
2651/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002652 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
2653 * later sampled by raid5d.
2654 */
2655static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
2656{
2657 unsigned long flags;
2658
2659 spin_lock_irqsave(&conf->device_lock, flags);
2660
2661 bi->bi_next = conf->retry_read_aligned_list;
2662 conf->retry_read_aligned_list = bi;
2663
2664 spin_unlock_irqrestore(&conf->device_lock, flags);
2665 md_wakeup_thread(conf->mddev->thread);
2666}
2667
2668
2669static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
2670{
2671 struct bio *bi;
2672
2673 bi = conf->retry_read_aligned;
2674 if (bi) {
2675 conf->retry_read_aligned = NULL;
2676 return bi;
2677 }
2678 bi = conf->retry_read_aligned_list;
2679 if(bi) {
2680 conf->retry_read_aligned = bi->bi_next;
2681 bi->bi_next = NULL;
2682 bi->bi_phys_segments = 1; /* biased count of active stripes */
2683 bi->bi_hw_segments = 0; /* count of processed stripes */
2684 }
2685
2686 return bi;
2687}
2688
2689
2690/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002691 * The "raid5_align_endio" should check if the read succeeded and if it
2692 * did, call bio_endio on the original bio (having bio_put the new bio
2693 * first).
2694 * If the read failed..
2695 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002696static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002697{
2698 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002699 mddev_t *mddev;
2700 raid5_conf_t *conf;
2701 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
2702 mdk_rdev_t *rdev;
2703
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002704 if (bi->bi_size)
2705 return 1;
2706 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002707
2708 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
2709 conf = mddev_to_conf(mddev);
2710 rdev = (void*)raid_bi->bi_next;
2711 raid_bi->bi_next = NULL;
2712
2713 rdev_dec_pending(rdev, conf->mddev);
2714
2715 if (!error && uptodate) {
2716 bio_endio(raid_bi, bytes, 0);
2717 if (atomic_dec_and_test(&conf->active_aligned_reads))
2718 wake_up(&conf->wait_for_stripe);
2719 return 0;
2720 }
2721
2722
2723 PRINTK("raid5_align_endio : io error...handing IO for a retry\n");
2724
2725 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002726 return 0;
2727}
2728
2729static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
2730{
2731 mddev_t *mddev = q->queuedata;
2732 raid5_conf_t *conf = mddev_to_conf(mddev);
2733 const unsigned int raid_disks = conf->raid_disks;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002734 const unsigned int data_disks = raid_disks - conf->max_degraded;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002735 unsigned int dd_idx, pd_idx;
2736 struct bio* align_bi;
2737 mdk_rdev_t *rdev;
2738
2739 if (!in_chunk_boundary(mddev, raid_bio)) {
2740 printk("chunk_aligned_read : non aligned\n");
2741 return 0;
2742 }
2743 /*
2744 * use bio_clone to make a copy of the bio
2745 */
2746 align_bi = bio_clone(raid_bio, GFP_NOIO);
2747 if (!align_bi)
2748 return 0;
2749 /*
2750 * set bi_end_io to a new function, and set bi_private to the
2751 * original bio.
2752 */
2753 align_bi->bi_end_io = raid5_align_endio;
2754 align_bi->bi_private = raid_bio;
2755 /*
2756 * compute position
2757 */
2758 align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
2759 raid_disks,
2760 data_disks,
2761 &dd_idx,
2762 &pd_idx,
2763 conf);
2764
2765 rcu_read_lock();
2766 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
2767 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002768 atomic_inc(&rdev->nr_pending);
2769 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002770 raid_bio->bi_next = (void*)rdev;
2771 align_bi->bi_bdev = rdev->bdev;
2772 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
2773 align_bi->bi_sector += rdev->data_offset;
2774
2775 spin_lock_irq(&conf->device_lock);
2776 wait_event_lock_irq(conf->wait_for_stripe,
2777 conf->quiesce == 0,
2778 conf->device_lock, /* nothing */);
2779 atomic_inc(&conf->active_aligned_reads);
2780 spin_unlock_irq(&conf->device_lock);
2781
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002782 generic_make_request(align_bi);
2783 return 1;
2784 } else {
2785 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002786 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002787 return 0;
2788 }
2789}
2790
2791
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002792static int make_request(request_queue_t *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
2794 mddev_t *mddev = q->queuedata;
2795 raid5_conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 unsigned int dd_idx, pd_idx;
2797 sector_t new_sector;
2798 sector_t logical_sector, last_sector;
2799 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01002800 const int rw = bio_data_dir(bi);
NeilBrownf6344752006-03-27 01:18:17 -08002801 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
NeilBrowne5dcdd82005-09-09 16:23:41 -07002803 if (unlikely(bio_barrier(bi))) {
2804 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2805 return 0;
2806 }
2807
NeilBrown3d310eb2005-06-21 17:17:26 -07002808 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07002809
Jens Axboea3623572005-11-01 09:26:16 +01002810 disk_stat_inc(mddev->gendisk, ios[rw]);
2811 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08002813 if (bio_data_dir(bi) == READ &&
2814 mddev->reshape_position == MaxSector &&
2815 chunk_aligned_read(q,bi))
2816 return 0;
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2819 last_sector = bi->bi_sector + (bi->bi_size>>9);
2820 bi->bi_next = NULL;
2821 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07002822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2824 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002825 int disks, data_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002826
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002827 retry:
NeilBrownb578d552006-03-27 01:18:12 -08002828 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002829 if (likely(conf->expand_progress == MaxSector))
2830 disks = conf->raid_disks;
2831 else {
NeilBrowndf8e7f72006-03-27 01:18:15 -08002832 /* spinlock is needed as expand_progress may be
2833 * 64bit on a 32bit platform, and so it might be
2834 * possible to see a half-updated value
2835 * Ofcourse expand_progress could change after
2836 * the lock is dropped, so once we get a reference
2837 * to the stripe that we think it is, we will have
2838 * to check again.
2839 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002840 spin_lock_irq(&conf->device_lock);
2841 disks = conf->raid_disks;
2842 if (logical_sector >= conf->expand_progress)
2843 disks = conf->previous_raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002844 else {
2845 if (logical_sector >= conf->expand_lo) {
2846 spin_unlock_irq(&conf->device_lock);
2847 schedule();
2848 goto retry;
2849 }
2850 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002851 spin_unlock_irq(&conf->device_lock);
2852 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002853 data_disks = disks - conf->max_degraded;
2854
2855 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002856 &dd_idx, &pd_idx, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2858 (unsigned long long)new_sector,
2859 (unsigned long long)logical_sector);
2860
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002861 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002863 if (unlikely(conf->expand_progress != MaxSector)) {
2864 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08002865 * stripe, so we must do the range check again.
2866 * Expansion could still move past after this
2867 * test, but as we are holding a reference to
2868 * 'sh', we know that if that happens,
2869 * STRIPE_EXPANDING will get set and the expansion
2870 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002871 */
2872 int must_retry = 0;
2873 spin_lock_irq(&conf->device_lock);
2874 if (logical_sector < conf->expand_progress &&
2875 disks == conf->previous_raid_disks)
2876 /* mismatch, need to try again */
2877 must_retry = 1;
2878 spin_unlock_irq(&conf->device_lock);
2879 if (must_retry) {
2880 release_stripe(sh);
2881 goto retry;
2882 }
2883 }
NeilBrowne464eaf2006-03-27 01:18:14 -08002884 /* FIXME what if we get a false positive because these
2885 * are being updated.
2886 */
2887 if (logical_sector >= mddev->suspend_lo &&
2888 logical_sector < mddev->suspend_hi) {
2889 release_stripe(sh);
2890 schedule();
2891 goto retry;
2892 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002893
2894 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2895 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2896 /* Stripe is busy expanding or
2897 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 * and wait a while
2899 */
2900 raid5_unplug_device(mddev->queue);
2901 release_stripe(sh);
2902 schedule();
2903 goto retry;
2904 }
2905 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002906 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 } else {
2909 /* cannot get stripe for read-ahead, just give-up */
2910 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2911 finish_wait(&conf->wait_for_overlap, &w);
2912 break;
2913 }
2914
2915 }
2916 spin_lock_irq(&conf->device_lock);
NeilBrownf6344752006-03-27 01:18:17 -08002917 remaining = --bi->bi_phys_segments;
2918 spin_unlock_irq(&conf->device_lock);
2919 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 int bytes = bi->bi_size;
2921
NeilBrown16a53ec2006-06-26 00:27:38 -07002922 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 md_write_end(mddev);
2924 bi->bi_size = 0;
2925 bi->bi_end_io(bi, bytes, 0);
2926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return 0;
2928}
2929
NeilBrown52c03292006-06-26 00:27:43 -07002930static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
NeilBrown52c03292006-06-26 00:27:43 -07002932 /* reshaping is quite different to recovery/resync so it is
2933 * handled quite separately ... here.
2934 *
2935 * On each call to sync_request, we gather one chunk worth of
2936 * destination stripes and flag them as expanding.
2937 * Then we find all the source stripes and request reads.
2938 * As the reads complete, handle_stripe will copy the data
2939 * into the destination stripe and release that stripe.
2940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2942 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08002943 int pd_idx;
2944 sector_t first_sector, last_sector;
NeilBrown52c03292006-06-26 00:27:43 -07002945 int raid_disks;
2946 int data_disks;
2947 int i;
2948 int dd_idx;
2949 sector_t writepos, safepos, gap;
2950
2951 if (sector_nr == 0 &&
2952 conf->expand_progress != 0) {
2953 /* restarting in the middle, skip the initial sectors */
2954 sector_nr = conf->expand_progress;
2955 sector_div(sector_nr, conf->raid_disks-1);
2956 *skipped = 1;
2957 return sector_nr;
2958 }
2959
2960 /* we update the metadata when there is more than 3Meg
2961 * in the block range (that is rather arbitrary, should
2962 * probably be time based) or when the data about to be
2963 * copied would over-write the source of the data at
2964 * the front of the range.
2965 * i.e. one new_stripe forward from expand_progress new_maps
2966 * to after where expand_lo old_maps to
2967 */
2968 writepos = conf->expand_progress +
2969 conf->chunk_size/512*(conf->raid_disks-1);
2970 sector_div(writepos, conf->raid_disks-1);
2971 safepos = conf->expand_lo;
2972 sector_div(safepos, conf->previous_raid_disks-1);
2973 gap = conf->expand_progress - conf->expand_lo;
2974
2975 if (writepos >= safepos ||
2976 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
2977 /* Cannot proceed until we've updated the superblock... */
2978 wait_event(conf->wait_for_overlap,
2979 atomic_read(&conf->reshape_stripes)==0);
2980 mddev->reshape_position = conf->expand_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07002981 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07002982 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07002983 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07002984 kthread_should_stop());
2985 spin_lock_irq(&conf->device_lock);
2986 conf->expand_lo = mddev->reshape_position;
2987 spin_unlock_irq(&conf->device_lock);
2988 wake_up(&conf->wait_for_overlap);
2989 }
2990
2991 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
2992 int j;
2993 int skipped = 0;
2994 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
2995 sh = get_active_stripe(conf, sector_nr+i,
2996 conf->raid_disks, pd_idx, 0);
2997 set_bit(STRIPE_EXPANDING, &sh->state);
2998 atomic_inc(&conf->reshape_stripes);
2999 /* If any of this stripe is beyond the end of the old
3000 * array, then we need to zero those blocks
3001 */
3002 for (j=sh->disks; j--;) {
3003 sector_t s;
3004 if (j == sh->pd_idx)
3005 continue;
3006 s = compute_blocknr(sh, j);
3007 if (s < (mddev->array_size<<1)) {
3008 skipped = 1;
3009 continue;
3010 }
3011 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3012 set_bit(R5_Expanded, &sh->dev[j].flags);
3013 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3014 }
3015 if (!skipped) {
3016 set_bit(STRIPE_EXPAND_READY, &sh->state);
3017 set_bit(STRIPE_HANDLE, &sh->state);
3018 }
3019 release_stripe(sh);
3020 }
3021 spin_lock_irq(&conf->device_lock);
3022 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
3023 spin_unlock_irq(&conf->device_lock);
3024 /* Ok, those stripe are ready. We can start scheduling
3025 * reads on the source stripes.
3026 * The source stripes are determined by mapping the first and last
3027 * block on the destination stripes.
3028 */
3029 raid_disks = conf->previous_raid_disks;
3030 data_disks = raid_disks - 1;
3031 first_sector =
3032 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
3033 raid_disks, data_disks,
3034 &dd_idx, &pd_idx, conf);
3035 last_sector =
3036 raid5_compute_sector((sector_nr+conf->chunk_size/512)
3037 *(conf->raid_disks-1) -1,
3038 raid_disks, data_disks,
3039 &dd_idx, &pd_idx, conf);
3040 if (last_sector >= (mddev->size<<1))
3041 last_sector = (mddev->size<<1)-1;
3042 while (first_sector <= last_sector) {
3043 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
3044 sh = get_active_stripe(conf, first_sector,
3045 conf->previous_raid_disks, pd_idx, 0);
3046 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3047 set_bit(STRIPE_HANDLE, &sh->state);
3048 release_stripe(sh);
3049 first_sector += STRIPE_SECTORS;
3050 }
3051 return conf->chunk_size>>9;
3052}
3053
3054/* FIXME go_faster isn't used */
3055static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3056{
3057 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3058 struct stripe_head *sh;
3059 int pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 int raid_disks = conf->raid_disks;
NeilBrown72626682005-09-09 16:23:54 -07003061 sector_t max_sector = mddev->size << 1;
3062 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003063 int still_degraded = 0;
3064 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
NeilBrown72626682005-09-09 16:23:54 -07003066 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 /* just being told to finish up .. nothing much to do */
3068 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08003069 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3070 end_reshape(conf);
3071 return 0;
3072 }
NeilBrown72626682005-09-09 16:23:54 -07003073
3074 if (mddev->curr_resync < max_sector) /* aborted */
3075 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3076 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003077 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07003078 conf->fullsync = 0;
3079 bitmap_close_sync(mddev->bitmap);
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return 0;
3082 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08003083
NeilBrown52c03292006-06-26 00:27:43 -07003084 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3085 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08003086
NeilBrown16a53ec2006-06-26 00:27:38 -07003087 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 * to resync, then assert that we are finished, because there is
3089 * nothing we can do.
3090 */
NeilBrown3285edf2006-06-26 00:27:55 -07003091 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07003092 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07003093 sector_t rv = (mddev->size << 1) - sector_nr;
3094 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return rv;
3096 }
NeilBrown72626682005-09-09 16:23:54 -07003097 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08003098 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07003099 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3100 /* we can skip this block, and probably more */
3101 sync_blocks /= STRIPE_SECTORS;
3102 *skipped = 1;
3103 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
NeilBrownccfcc3c2006-03-27 01:18:09 -08003106 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003107 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (sh == NULL) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003109 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07003111 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08003113 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003115 /* Need to check if array will still be degraded after recovery/resync
3116 * We don't need to check the 'failed' flag as when that gets set,
3117 * recovery aborts.
3118 */
3119 for (i=0; i<mddev->raid_disks; i++)
3120 if (conf->disks[i].rdev == NULL)
3121 still_degraded = 1;
3122
3123 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3124
3125 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 set_bit(STRIPE_SYNCING, &sh->state);
3127 clear_bit(STRIPE_INSYNC, &sh->state);
3128 spin_unlock(&sh->lock);
3129
NeilBrown16a53ec2006-06-26 00:27:38 -07003130 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 release_stripe(sh);
3132
3133 return STRIPE_SECTORS;
3134}
3135
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003136static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3137{
3138 /* We may not be able to submit a whole bio at once as there
3139 * may not be enough stripe_heads available.
3140 * We cannot pre-allocate enough stripe_heads as we may need
3141 * more than exist in the cache (if we allow ever large chunks).
3142 * So we do one stripe head at a time and record in
3143 * ->bi_hw_segments how many have been done.
3144 *
3145 * We *know* that this entire raid_bio is in one chunk, so
3146 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3147 */
3148 struct stripe_head *sh;
3149 int dd_idx, pd_idx;
3150 sector_t sector, logical_sector, last_sector;
3151 int scnt = 0;
3152 int remaining;
3153 int handled = 0;
3154
3155 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3156 sector = raid5_compute_sector( logical_sector,
3157 conf->raid_disks,
3158 conf->raid_disks - conf->max_degraded,
3159 &dd_idx,
3160 &pd_idx,
3161 conf);
3162 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3163
3164 for (; logical_sector < last_sector;
3165 logical_sector += STRIPE_SECTORS, scnt++) {
3166
3167 if (scnt < raid_bio->bi_hw_segments)
3168 /* already done this stripe */
3169 continue;
3170
3171 sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
3172
3173 if (!sh) {
3174 /* failed to get a stripe - must wait */
3175 raid_bio->bi_hw_segments = scnt;
3176 conf->retry_read_aligned = raid_bio;
3177 return handled;
3178 }
3179
3180 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
3181 add_stripe_bio(sh, raid_bio, dd_idx, 0);
3182 handle_stripe(sh, NULL);
3183 release_stripe(sh);
3184 handled++;
3185 }
3186 spin_lock_irq(&conf->device_lock);
3187 remaining = --raid_bio->bi_phys_segments;
3188 spin_unlock_irq(&conf->device_lock);
3189 if (remaining == 0) {
3190 int bytes = raid_bio->bi_size;
3191
3192 raid_bio->bi_size = 0;
3193 raid_bio->bi_end_io(raid_bio, bytes, 0);
3194 }
3195 if (atomic_dec_and_test(&conf->active_aligned_reads))
3196 wake_up(&conf->wait_for_stripe);
3197 return handled;
3198}
3199
3200
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202/*
3203 * This is our raid5 kernel thread.
3204 *
3205 * We scan the hash table for stripes which can be handled now.
3206 * During the scan, completed stripes are saved for us by the interrupt
3207 * handler, so that they will not have to wait for our next wakeup.
3208 */
3209static void raid5d (mddev_t *mddev)
3210{
3211 struct stripe_head *sh;
3212 raid5_conf_t *conf = mddev_to_conf(mddev);
3213 int handled;
3214
3215 PRINTK("+++ raid5d active\n");
3216
3217 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 handled = 0;
3220 spin_lock_irq(&conf->device_lock);
3221 while (1) {
3222 struct list_head *first;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003223 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
NeilBrownae3c20c2006-07-10 04:44:17 -07003225 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07003226 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08003227 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003228 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08003229 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003230 conf->seq_write = seq;
3231 activate_bit_delay(conf);
3232 }
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (list_empty(&conf->handle_list) &&
3235 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
3236 !blk_queue_plugged(mddev->queue) &&
3237 !list_empty(&conf->delayed_list))
3238 raid5_activate_delayed(conf);
3239
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003240 while ((bio = remove_bio_from_retry(conf))) {
3241 int ok;
3242 spin_unlock_irq(&conf->device_lock);
3243 ok = retry_aligned_read(conf, bio);
3244 spin_lock_irq(&conf->device_lock);
3245 if (!ok)
3246 break;
3247 handled++;
3248 }
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (list_empty(&conf->handle_list))
3251 break;
3252
3253 first = conf->handle_list.next;
3254 sh = list_entry(first, struct stripe_head, lru);
3255
3256 list_del_init(first);
3257 atomic_inc(&sh->count);
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003258 BUG_ON(atomic_read(&sh->count)!= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 spin_unlock_irq(&conf->device_lock);
3260
3261 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003262 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 release_stripe(sh);
3264
3265 spin_lock_irq(&conf->device_lock);
3266 }
3267 PRINTK("%d stripes handled\n", handled);
3268
3269 spin_unlock_irq(&conf->device_lock);
3270
3271 unplug_slaves(mddev);
3272
3273 PRINTK("--- raid5d inactive\n");
3274}
3275
NeilBrown3f294f42005-11-08 21:39:25 -08003276static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003277raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003278{
NeilBrown007583c2005-11-08 21:39:30 -08003279 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003280 if (conf)
3281 return sprintf(page, "%d\n", conf->max_nr_stripes);
3282 else
3283 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003284}
3285
3286static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003287raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08003288{
NeilBrown007583c2005-11-08 21:39:30 -08003289 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08003290 char *end;
3291 int new;
3292 if (len >= PAGE_SIZE)
3293 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08003294 if (!conf)
3295 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08003296
3297 new = simple_strtoul(page, &end, 10);
3298 if (!*page || (*end && *end != '\n') )
3299 return -EINVAL;
3300 if (new <= 16 || new > 32768)
3301 return -EINVAL;
3302 while (new < conf->max_nr_stripes) {
3303 if (drop_one_stripe(conf))
3304 conf->max_nr_stripes--;
3305 else
3306 break;
3307 }
3308 while (new > conf->max_nr_stripes) {
3309 if (grow_one_stripe(conf))
3310 conf->max_nr_stripes++;
3311 else break;
3312 }
3313 return len;
3314}
NeilBrown007583c2005-11-08 21:39:30 -08003315
NeilBrown96de1e62005-11-08 21:39:39 -08003316static struct md_sysfs_entry
3317raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3318 raid5_show_stripe_cache_size,
3319 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08003320
3321static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003322stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003323{
NeilBrown007583c2005-11-08 21:39:30 -08003324 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003325 if (conf)
3326 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3327 else
3328 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003329}
3330
NeilBrown96de1e62005-11-08 21:39:39 -08003331static struct md_sysfs_entry
3332raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08003333
NeilBrown007583c2005-11-08 21:39:30 -08003334static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08003335 &raid5_stripecache_size.attr,
3336 &raid5_stripecache_active.attr,
3337 NULL,
3338};
NeilBrown007583c2005-11-08 21:39:30 -08003339static struct attribute_group raid5_attrs_group = {
3340 .name = NULL,
3341 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08003342};
3343
NeilBrown72626682005-09-09 16:23:54 -07003344static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
3346 raid5_conf_t *conf;
3347 int raid_disk, memory;
3348 mdk_rdev_t *rdev;
3349 struct disk_info *disk;
3350 struct list_head *tmp;
NeilBrown02c2de82006-10-03 01:15:47 -07003351 int working_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
NeilBrown16a53ec2006-06-26 00:27:38 -07003353 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3354 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown14f8d262006-01-06 00:20:14 -08003355 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return -EIO;
3357 }
3358
NeilBrownf6705572006-03-27 01:18:11 -08003359 if (mddev->reshape_position != MaxSector) {
3360 /* Check that we can continue the reshape.
3361 * Currently only disks can change, it must
3362 * increase, and we must be past the point where
3363 * a stripe over-writes itself
3364 */
3365 sector_t here_new, here_old;
3366 int old_disks;
3367
3368 if (mddev->new_level != mddev->level ||
3369 mddev->new_layout != mddev->layout ||
3370 mddev->new_chunk != mddev->chunk_size) {
3371 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
3372 mdname(mddev));
3373 return -EINVAL;
3374 }
3375 if (mddev->delta_disks <= 0) {
3376 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3377 mdname(mddev));
3378 return -EINVAL;
3379 }
3380 old_disks = mddev->raid_disks - mddev->delta_disks;
3381 /* reshape_position must be on a new-stripe boundary, and one
3382 * further up in new geometry must map after here in old geometry.
3383 */
3384 here_new = mddev->reshape_position;
3385 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
3386 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
3387 return -EINVAL;
3388 }
3389 /* here_new is the stripe we will write to */
3390 here_old = mddev->reshape_position;
3391 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
3392 /* here_old is the first stripe that we might need to read from */
3393 if (here_new >= here_old) {
3394 /* Reading from the same stripe as writing to - bad */
3395 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
3396 return -EINVAL;
3397 }
3398 printk(KERN_INFO "raid5: reshape will continue\n");
3399 /* OK, we should be able to continue; */
3400 }
3401
3402
NeilBrownb55e6bf2006-03-27 01:18:06 -08003403 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if ((conf = mddev->private) == NULL)
3405 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08003406 if (mddev->reshape_position == MaxSector) {
3407 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3408 } else {
3409 conf->raid_disks = mddev->raid_disks;
3410 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3411 }
3412
3413 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
NeilBrownb55e6bf2006-03-27 01:18:06 -08003414 GFP_KERNEL);
3415 if (!conf->disks)
3416 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 conf->mddev = mddev;
3419
NeilBrownfccddba2006-01-06 00:20:33 -08003420 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
NeilBrown16a53ec2006-06-26 00:27:38 -07003423 if (mddev->level == 6) {
3424 conf->spare_page = alloc_page(GFP_KERNEL);
3425 if (!conf->spare_page)
3426 goto abort;
3427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 spin_lock_init(&conf->device_lock);
3429 init_waitqueue_head(&conf->wait_for_stripe);
3430 init_waitqueue_head(&conf->wait_for_overlap);
3431 INIT_LIST_HEAD(&conf->handle_list);
3432 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07003433 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 INIT_LIST_HEAD(&conf->inactive_list);
3435 atomic_set(&conf->active_stripes, 0);
3436 atomic_set(&conf->preread_active_stripes, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003437 atomic_set(&conf->active_aligned_reads, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3440
3441 ITERATE_RDEV(mddev,rdev,tmp) {
3442 raid_disk = rdev->raid_disk;
NeilBrownf6705572006-03-27 01:18:11 -08003443 if (raid_disk >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 || raid_disk < 0)
3445 continue;
3446 disk = conf->disks + raid_disk;
3447
3448 disk->rdev = rdev;
3449
NeilBrownb2d444d2005-11-08 21:39:31 -08003450 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 char b[BDEVNAME_SIZE];
3452 printk(KERN_INFO "raid5: device %s operational as raid"
3453 " disk %d\n", bdevname(rdev->bdev,b),
3454 raid_disk);
NeilBrown02c2de82006-10-03 01:15:47 -07003455 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457 }
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003460 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 */
NeilBrown02c2de82006-10-03 01:15:47 -07003462 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 conf->mddev = mddev;
3464 conf->chunk_size = mddev->chunk_size;
3465 conf->level = mddev->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003466 if (conf->level == 6)
3467 conf->max_degraded = 2;
3468 else
3469 conf->max_degraded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 conf->algorithm = mddev->layout;
3471 conf->max_nr_stripes = NR_STRIPES;
NeilBrownf6705572006-03-27 01:18:11 -08003472 conf->expand_progress = mddev->reshape_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 /* device size must be a multiple of chunk size */
3475 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07003476 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
NeilBrown16a53ec2006-06-26 00:27:38 -07003478 if (conf->level == 6 && conf->raid_disks < 4) {
3479 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3480 mdname(mddev), conf->raid_disks);
3481 goto abort;
3482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 if (!conf->chunk_size || conf->chunk_size % 4) {
3484 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3485 conf->chunk_size, mdname(mddev));
3486 goto abort;
3487 }
3488 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3489 printk(KERN_ERR
3490 "raid5: unsupported parity algorithm %d for %s\n",
3491 conf->algorithm, mdname(mddev));
3492 goto abort;
3493 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003494 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 printk(KERN_ERR "raid5: not enough operational devices for %s"
3496 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07003497 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 goto abort;
3499 }
3500
NeilBrown16a53ec2006-06-26 00:27:38 -07003501 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003503 if (mddev->ok_start_degraded)
3504 printk(KERN_WARNING
3505 "raid5: starting dirty degraded array: %s"
3506 "- data corruption possible.\n",
3507 mdname(mddev));
3508 else {
3509 printk(KERN_ERR
3510 "raid5: cannot start dirty degraded array for %s\n",
3511 mdname(mddev));
3512 goto abort;
3513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
3515
3516 {
3517 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3518 if (!mddev->thread) {
3519 printk(KERN_ERR
3520 "raid5: couldn't allocate thread for %s\n",
3521 mdname(mddev));
3522 goto abort;
3523 }
3524 }
NeilBrown50368052005-12-12 02:39:17 -08003525 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3527 if (grow_stripes(conf, conf->max_nr_stripes)) {
3528 printk(KERN_ERR
3529 "raid5: couldn't allocate %dkB for buffers\n", memory);
3530 shrink_stripes(conf);
3531 md_unregister_thread(mddev->thread);
3532 goto abort;
3533 } else
3534 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3535 memory, mdname(mddev));
3536
3537 if (mddev->degraded == 0)
3538 printk("raid5: raid level %d set %s active with %d out of %d"
3539 " devices, algorithm %d\n", conf->level, mdname(mddev),
3540 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3541 conf->algorithm);
3542 else
3543 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3544 " out of %d devices, algorithm %d\n", conf->level,
3545 mdname(mddev), mddev->raid_disks - mddev->degraded,
3546 mddev->raid_disks, conf->algorithm);
3547
3548 print_raid5_conf(conf);
3549
NeilBrownf6705572006-03-27 01:18:11 -08003550 if (conf->expand_progress != MaxSector) {
3551 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08003552 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08003553 atomic_set(&conf->reshape_stripes, 0);
3554 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3555 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3556 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3557 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3558 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3559 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08003560 }
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07003563 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 */
3565 {
NeilBrown16a53ec2006-06-26 00:27:38 -07003566 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3567 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07003568 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3570 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3571 }
3572
3573 /* Ok, everything is just fine now */
NeilBrown007583c2005-11-08 21:39:30 -08003574 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
NeilBrown7a5febe2005-05-16 21:53:16 -07003575
3576 mddev->queue->unplug_fn = raid5_unplug_device;
3577 mddev->queue->issue_flush_fn = raid5_issue_flush;
NeilBrownf022b2f2006-10-03 01:15:56 -07003578 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
3579 mddev->queue->backing_dev_info.congested_data = mddev;
3580
NeilBrown16a53ec2006-06-26 00:27:38 -07003581 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3582 conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07003583
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003584 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return 0;
3587abort:
3588 if (conf) {
3589 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07003590 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003591 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08003592 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 kfree(conf);
3594 }
3595 mddev->private = NULL;
3596 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3597 return -EIO;
3598}
3599
3600
3601
NeilBrown3f294f42005-11-08 21:39:25 -08003602static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
3604 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3605
3606 md_unregister_thread(mddev->thread);
3607 mddev->thread = NULL;
3608 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08003609 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08003611 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003612 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08003613 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 mddev->private = NULL;
3615 return 0;
3616}
3617
3618#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003619static void print_sh (struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 int i;
3622
NeilBrown16a53ec2006-06-26 00:27:38 -07003623 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3624 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3625 seq_printf(seq, "sh %llu, count %d.\n",
3626 (unsigned long long)sh->sector, atomic_read(&sh->count));
3627 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003628 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003629 seq_printf(seq, "(cache%d: %p %ld) ",
3630 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003632 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
3634
NeilBrown16a53ec2006-06-26 00:27:38 -07003635static void printall (struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08003638 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 int i;
3640
3641 spin_lock_irq(&conf->device_lock);
3642 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08003643 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 if (sh->raid_conf != conf)
3645 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07003646 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 }
3648 }
3649 spin_unlock_irq(&conf->device_lock);
3650}
3651#endif
3652
3653static void status (struct seq_file *seq, mddev_t *mddev)
3654{
3655 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3656 int i;
3657
3658 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07003659 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 for (i = 0; i < conf->raid_disks; i++)
3661 seq_printf (seq, "%s",
3662 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08003663 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 seq_printf (seq, "]");
3665#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003666 seq_printf (seq, "\n");
3667 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668#endif
3669}
3670
3671static void print_raid5_conf (raid5_conf_t *conf)
3672{
3673 int i;
3674 struct disk_info *tmp;
3675
3676 printk("RAID5 conf printout:\n");
3677 if (!conf) {
3678 printk("(conf==NULL)\n");
3679 return;
3680 }
NeilBrown02c2de82006-10-03 01:15:47 -07003681 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
3682 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 for (i = 0; i < conf->raid_disks; i++) {
3685 char b[BDEVNAME_SIZE];
3686 tmp = conf->disks + i;
3687 if (tmp->rdev)
3688 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08003689 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 bdevname(tmp->rdev->bdev,b));
3691 }
3692}
3693
3694static int raid5_spare_active(mddev_t *mddev)
3695{
3696 int i;
3697 raid5_conf_t *conf = mddev->private;
3698 struct disk_info *tmp;
3699
3700 for (i = 0; i < conf->raid_disks; i++) {
3701 tmp = conf->disks + i;
3702 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08003703 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07003704 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
3705 unsigned long flags;
3706 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07003708 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 }
3710 }
3711 print_raid5_conf(conf);
3712 return 0;
3713}
3714
3715static int raid5_remove_disk(mddev_t *mddev, int number)
3716{
3717 raid5_conf_t *conf = mddev->private;
3718 int err = 0;
3719 mdk_rdev_t *rdev;
3720 struct disk_info *p = conf->disks + number;
3721
3722 print_raid5_conf(conf);
3723 rdev = p->rdev;
3724 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003725 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 atomic_read(&rdev->nr_pending)) {
3727 err = -EBUSY;
3728 goto abort;
3729 }
3730 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003731 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if (atomic_read(&rdev->nr_pending)) {
3733 /* lost the race, try later */
3734 err = -EBUSY;
3735 p->rdev = rdev;
3736 }
3737 }
3738abort:
3739
3740 print_raid5_conf(conf);
3741 return err;
3742}
3743
3744static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3745{
3746 raid5_conf_t *conf = mddev->private;
3747 int found = 0;
3748 int disk;
3749 struct disk_info *p;
3750
NeilBrown16a53ec2006-06-26 00:27:38 -07003751 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 /* no point adding a device */
3753 return 0;
3754
3755 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003756 * find the disk ... but prefer rdev->saved_raid_disk
3757 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003759 if (rdev->saved_raid_disk >= 0 &&
3760 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3761 disk = rdev->saved_raid_disk;
3762 else
3763 disk = 0;
3764 for ( ; disk < conf->raid_disks; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003766 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 rdev->raid_disk = disk;
3768 found = 1;
NeilBrown72626682005-09-09 16:23:54 -07003769 if (rdev->saved_raid_disk != disk)
3770 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08003771 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 break;
3773 }
3774 print_raid5_conf(conf);
3775 return found;
3776}
3777
3778static int raid5_resize(mddev_t *mddev, sector_t sectors)
3779{
3780 /* no resync is happening, and there is enough space
3781 * on all devices, so we can resize.
3782 * We need to make sure resync covers any new space.
3783 * If the array is shrinking we should possibly wait until
3784 * any io in the removed space completes, but it hardly seems
3785 * worth it.
3786 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003787 raid5_conf_t *conf = mddev_to_conf(mddev);
3788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003790 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 set_capacity(mddev->gendisk, mddev->array_size << 1);
3792 mddev->changed = 1;
3793 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3794 mddev->recovery_cp = mddev->size << 1;
3795 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3796 }
3797 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003798 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return 0;
3800}
3801
NeilBrown29269552006-03-27 01:18:10 -08003802#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003803static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08003804{
3805 raid5_conf_t *conf = mddev_to_conf(mddev);
3806 int err;
NeilBrown29269552006-03-27 01:18:10 -08003807
NeilBrown63c70c42006-03-27 01:18:13 -08003808 if (mddev->delta_disks < 0 ||
3809 mddev->new_level != mddev->level)
3810 return -EINVAL; /* Cannot shrink array or change level yet */
3811 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08003812 return 0; /* nothing to do */
3813
3814 /* Can only proceed if there are plenty of stripe_heads.
3815 * We need a minimum of one full stripe,, and for sensible progress
3816 * it is best to have about 4 times that.
3817 * If we require 4 times, then the default 256 4K stripe_heads will
3818 * allow for chunk sizes up to 256K, which is probably OK.
3819 * If the chunk size is greater, user-space should request more
3820 * stripe_heads first.
3821 */
NeilBrown63c70c42006-03-27 01:18:13 -08003822 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3823 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08003824 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3825 (mddev->chunk_size / STRIPE_SIZE)*4);
3826 return -ENOSPC;
3827 }
3828
NeilBrown63c70c42006-03-27 01:18:13 -08003829 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3830 if (err)
3831 return err;
3832
3833 /* looks like we might be able to manage this */
3834 return 0;
3835}
3836
3837static int raid5_start_reshape(mddev_t *mddev)
3838{
3839 raid5_conf_t *conf = mddev_to_conf(mddev);
3840 mdk_rdev_t *rdev;
3841 struct list_head *rtmp;
3842 int spares = 0;
3843 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07003844 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08003845
3846 if (mddev->degraded ||
3847 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3848 return -EBUSY;
3849
NeilBrown29269552006-03-27 01:18:10 -08003850 ITERATE_RDEV(mddev, rdev, rtmp)
3851 if (rdev->raid_disk < 0 &&
3852 !test_bit(Faulty, &rdev->flags))
3853 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08003854
3855 if (spares < mddev->delta_disks-1)
NeilBrown29269552006-03-27 01:18:10 -08003856 /* Not enough devices even to make a degraded array
3857 * of that size
3858 */
3859 return -EINVAL;
3860
NeilBrownf6705572006-03-27 01:18:11 -08003861 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08003862 spin_lock_irq(&conf->device_lock);
3863 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003864 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08003865 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003866 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08003867 spin_unlock_irq(&conf->device_lock);
3868
3869 /* Add some new drives, as many as will fit.
3870 * We know there are enough to make the newly sized array work.
3871 */
3872 ITERATE_RDEV(mddev, rdev, rtmp)
3873 if (rdev->raid_disk < 0 &&
3874 !test_bit(Faulty, &rdev->flags)) {
3875 if (raid5_add_disk(mddev, rdev)) {
3876 char nm[20];
3877 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08003878 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003879 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08003880 sprintf(nm, "rd%d", rdev->raid_disk);
3881 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3882 } else
3883 break;
3884 }
3885
NeilBrownc04be0a2006-10-03 01:15:53 -07003886 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003887 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
NeilBrownc04be0a2006-10-03 01:15:53 -07003888 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003889 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08003890 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07003891 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08003892
NeilBrown29269552006-03-27 01:18:10 -08003893 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3894 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3895 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3896 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3897 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3898 "%s_reshape");
3899 if (!mddev->sync_thread) {
3900 mddev->recovery = 0;
3901 spin_lock_irq(&conf->device_lock);
3902 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3903 conf->expand_progress = MaxSector;
3904 spin_unlock_irq(&conf->device_lock);
3905 return -EAGAIN;
3906 }
3907 md_wakeup_thread(mddev->sync_thread);
3908 md_new_event(mddev);
3909 return 0;
3910}
3911#endif
3912
3913static void end_reshape(raid5_conf_t *conf)
3914{
3915 struct block_device *bdev;
3916
NeilBrownf6705572006-03-27 01:18:11 -08003917 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
3918 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
3919 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
3920 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08003921
NeilBrownf6705572006-03-27 01:18:11 -08003922 bdev = bdget_disk(conf->mddev->gendisk, 0);
3923 if (bdev) {
3924 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown0692c6b2006-11-08 17:44:48 -08003925 i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
NeilBrownf6705572006-03-27 01:18:11 -08003926 mutex_unlock(&bdev->bd_inode->i_mutex);
3927 bdput(bdev);
3928 }
3929 spin_lock_irq(&conf->device_lock);
3930 conf->expand_progress = MaxSector;
3931 spin_unlock_irq(&conf->device_lock);
3932 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07003933
3934 /* read-ahead size must cover two whole stripes, which is
3935 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3936 */
3937 {
3938 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3939 int stripe = data_disks *
3940 (conf->mddev->chunk_size / PAGE_SIZE);
3941 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3942 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3943 }
NeilBrown29269552006-03-27 01:18:10 -08003944 }
NeilBrown29269552006-03-27 01:18:10 -08003945}
3946
NeilBrown72626682005-09-09 16:23:54 -07003947static void raid5_quiesce(mddev_t *mddev, int state)
3948{
3949 raid5_conf_t *conf = mddev_to_conf(mddev);
3950
3951 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08003952 case 2: /* resume for a suspend */
3953 wake_up(&conf->wait_for_overlap);
3954 break;
3955
NeilBrown72626682005-09-09 16:23:54 -07003956 case 1: /* stop all writes */
3957 spin_lock_irq(&conf->device_lock);
3958 conf->quiesce = 1;
3959 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003960 atomic_read(&conf->active_stripes) == 0 &&
3961 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07003962 conf->device_lock, /* nothing */);
3963 spin_unlock_irq(&conf->device_lock);
3964 break;
3965
3966 case 0: /* re-enable writes */
3967 spin_lock_irq(&conf->device_lock);
3968 conf->quiesce = 0;
3969 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08003970 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07003971 spin_unlock_irq(&conf->device_lock);
3972 break;
3973 }
NeilBrown72626682005-09-09 16:23:54 -07003974}
NeilBrownb15c2e52006-01-06 00:20:16 -08003975
NeilBrown16a53ec2006-06-26 00:27:38 -07003976static struct mdk_personality raid6_personality =
3977{
3978 .name = "raid6",
3979 .level = 6,
3980 .owner = THIS_MODULE,
3981 .make_request = make_request,
3982 .run = run,
3983 .stop = stop,
3984 .status = status,
3985 .error_handler = error,
3986 .hot_add_disk = raid5_add_disk,
3987 .hot_remove_disk= raid5_remove_disk,
3988 .spare_active = raid5_spare_active,
3989 .sync_request = sync_request,
3990 .resize = raid5_resize,
3991 .quiesce = raid5_quiesce,
3992};
NeilBrown2604b702006-01-06 00:20:36 -08003993static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
3995 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08003996 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 .owner = THIS_MODULE,
3998 .make_request = make_request,
3999 .run = run,
4000 .stop = stop,
4001 .status = status,
4002 .error_handler = error,
4003 .hot_add_disk = raid5_add_disk,
4004 .hot_remove_disk= raid5_remove_disk,
4005 .spare_active = raid5_spare_active,
4006 .sync_request = sync_request,
4007 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08004008#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004009 .check_reshape = raid5_check_reshape,
4010 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08004011#endif
NeilBrown72626682005-09-09 16:23:54 -07004012 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013};
4014
NeilBrown2604b702006-01-06 00:20:36 -08004015static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
NeilBrown2604b702006-01-06 00:20:36 -08004017 .name = "raid4",
4018 .level = 4,
4019 .owner = THIS_MODULE,
4020 .make_request = make_request,
4021 .run = run,
4022 .stop = stop,
4023 .status = status,
4024 .error_handler = error,
4025 .hot_add_disk = raid5_add_disk,
4026 .hot_remove_disk= raid5_remove_disk,
4027 .spare_active = raid5_spare_active,
4028 .sync_request = sync_request,
4029 .resize = raid5_resize,
4030 .quiesce = raid5_quiesce,
4031};
4032
4033static int __init raid5_init(void)
4034{
NeilBrown16a53ec2006-06-26 00:27:38 -07004035 int e;
4036
4037 e = raid6_select_algo();
4038 if ( e )
4039 return e;
4040 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004041 register_md_personality(&raid5_personality);
4042 register_md_personality(&raid4_personality);
4043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
NeilBrown2604b702006-01-06 00:20:36 -08004046static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
NeilBrown16a53ec2006-06-26 00:27:38 -07004048 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004049 unregister_md_personality(&raid5_personality);
4050 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051}
4052
4053module_init(raid5_init);
4054module_exit(raid5_exit);
4055MODULE_LICENSE("GPL");
4056MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004057MODULE_ALIAS("md-raid5");
4058MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08004059MODULE_ALIAS("md-level-5");
4060MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07004061MODULE_ALIAS("md-personality-8"); /* RAID6 */
4062MODULE_ALIAS("md-raid6");
4063MODULE_ALIAS("md-level-6");
4064
4065/* This used to be two separate modules, they were: */
4066MODULE_ALIAS("raid5");
4067MODULE_ALIAS("raid6");