blob: 4f51dfa8e4871ddd2dde6398e3c2f90bf1a347a6 [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
NeilBrown42b9beb2007-05-09 02:35:37 -0700356 sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
357 sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -0800358 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
NeilBrown2a2275d2007-01-26 00:57:11 -0800408 md_allow_write(conf->mddev);
409
NeilBrownad01c9e2006-03-27 01:18:07 -0800410 /* Step 1 */
411 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
412 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
413 0, 0, NULL, NULL);
414 if (!sc)
415 return -ENOMEM;
416
417 for (i = conf->max_nr_stripes; i; i--) {
418 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
419 if (!nsh)
420 break;
421
422 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
423
424 nsh->raid_conf = conf;
425 spin_lock_init(&nsh->lock);
426
427 list_add(&nsh->lru, &newstripes);
428 }
429 if (i) {
430 /* didn't get enough, give up */
431 while (!list_empty(&newstripes)) {
432 nsh = list_entry(newstripes.next, struct stripe_head, lru);
433 list_del(&nsh->lru);
434 kmem_cache_free(sc, nsh);
435 }
436 kmem_cache_destroy(sc);
437 return -ENOMEM;
438 }
439 /* Step 2 - Must use GFP_NOIO now.
440 * OK, we have enough stripes, start collecting inactive
441 * stripes and copying them over
442 */
443 list_for_each_entry(nsh, &newstripes, lru) {
444 spin_lock_irq(&conf->device_lock);
445 wait_event_lock_irq(conf->wait_for_stripe,
446 !list_empty(&conf->inactive_list),
447 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -0800448 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -0800449 );
450 osh = get_free_stripe(conf);
451 spin_unlock_irq(&conf->device_lock);
452 atomic_set(&nsh->count, 1);
453 for(i=0; i<conf->pool_size; i++)
454 nsh->dev[i].page = osh->dev[i].page;
455 for( ; i<newsize; i++)
456 nsh->dev[i].page = NULL;
457 kmem_cache_free(conf->slab_cache, osh);
458 }
459 kmem_cache_destroy(conf->slab_cache);
460
461 /* Step 3.
462 * At this point, we are holding all the stripes so the array
463 * is completely stalled, so now is a good time to resize
464 * conf->disks.
465 */
466 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
467 if (ndisks) {
468 for (i=0; i<conf->raid_disks; i++)
469 ndisks[i] = conf->disks[i];
470 kfree(conf->disks);
471 conf->disks = ndisks;
472 } else
473 err = -ENOMEM;
474
475 /* Step 4, return new stripes to service */
476 while(!list_empty(&newstripes)) {
477 nsh = list_entry(newstripes.next, struct stripe_head, lru);
478 list_del_init(&nsh->lru);
479 for (i=conf->raid_disks; i < newsize; i++)
480 if (nsh->dev[i].page == NULL) {
481 struct page *p = alloc_page(GFP_NOIO);
482 nsh->dev[i].page = p;
483 if (!p)
484 err = -ENOMEM;
485 }
486 release_stripe(nsh);
487 }
488 /* critical section pass, GFP_NOIO no longer needed */
489
490 conf->slab_cache = sc;
491 conf->active_name = 1-conf->active_name;
492 conf->pool_size = newsize;
493 return err;
494}
NeilBrown29269552006-03-27 01:18:10 -0800495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
NeilBrown3f294f42005-11-08 21:39:25 -0800497static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct stripe_head *sh;
500
NeilBrown3f294f42005-11-08 21:39:25 -0800501 spin_lock_irq(&conf->device_lock);
502 sh = get_free_stripe(conf);
503 spin_unlock_irq(&conf->device_lock);
504 if (!sh)
505 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200506 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -0800507 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -0800508 kmem_cache_free(conf->slab_cache, sh);
509 atomic_dec(&conf->active_stripes);
510 return 1;
511}
512
513static void shrink_stripes(raid5_conf_t *conf)
514{
515 while (drop_one_stripe(conf))
516 ;
517
NeilBrown29fc7e32006-02-03 03:03:41 -0800518 if (conf->slab_cache)
519 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 conf->slab_cache = NULL;
521}
522
NeilBrown4e5314b2005-11-08 21:39:22 -0800523static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int error)
525{
526 struct stripe_head *sh = bi->bi_private;
527 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800528 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -0700530 char b[BDEVNAME_SIZE];
531 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (bi->bi_size)
534 return 1;
535
536 for (i=0 ; i<disks; i++)
537 if (bi == &sh->dev[i].req)
538 break;
539
540 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
541 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
542 uptodate);
543 if (i == disks) {
544 BUG();
545 return 0;
546 }
547
548 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -0800550 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -0700551 rdev = conf->disks[i].rdev;
552 printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
553 mdname(conf->mddev), STRIPE_SECTORS,
554 (unsigned long long)sh->sector + rdev->data_offset,
555 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -0800556 clear_bit(R5_ReadError, &sh->dev[i].flags);
557 clear_bit(R5_ReWrite, &sh->dev[i].flags);
558 }
NeilBrownba22dcb2005-11-08 21:39:31 -0800559 if (atomic_read(&conf->disks[i].rdev->read_errors))
560 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else {
NeilBrownd6950432006-07-10 04:44:20 -0700562 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -0800563 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -0700564 rdev = conf->disks[i].rdev;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700567 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -0800568 if (conf->mddev->degraded)
NeilBrownd6950432006-07-10 04:44:20 -0700569 printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
570 mdname(conf->mddev),
571 (unsigned long long)sh->sector + rdev->data_offset,
572 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800573 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -0800574 /* Oh, no!!! */
NeilBrownd6950432006-07-10 04:44:20 -0700575 printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
576 mdname(conf->mddev),
577 (unsigned long long)sh->sector + rdev->data_offset,
578 bdn);
579 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -0800580 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -0800581 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -0700582 "raid5:%s: Too many read errors, failing device %s.\n",
583 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -0800584 else
585 retry = 1;
586 if (retry)
587 set_bit(R5_ReadError, &sh->dev[i].flags);
588 else {
NeilBrown4e5314b2005-11-08 21:39:22 -0800589 clear_bit(R5_ReadError, &sh->dev[i].flags);
590 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -0700591 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -0800592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 clear_bit(R5_LOCKED, &sh->dev[i].flags);
596 set_bit(STRIPE_HANDLE, &sh->state);
597 release_stripe(sh);
598 return 0;
599}
600
601static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
602 int error)
603{
604 struct stripe_head *sh = bi->bi_private;
605 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800606 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
608
609 if (bi->bi_size)
610 return 1;
611
612 for (i=0 ; i<disks; i++)
613 if (bi == &sh->dev[i].req)
614 break;
615
616 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
617 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
618 uptodate);
619 if (i == disks) {
620 BUG();
621 return 0;
622 }
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!uptodate)
625 md_error(conf->mddev, conf->disks[i].rdev);
626
627 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
628
629 clear_bit(R5_LOCKED, &sh->dev[i].flags);
630 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -0700631 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
633}
634
635
636static sector_t compute_blocknr(struct stripe_head *sh, int i);
637
638static void raid5_build_block (struct stripe_head *sh, int i)
639{
640 struct r5dev *dev = &sh->dev[i];
641
642 bio_init(&dev->req);
643 dev->req.bi_io_vec = &dev->vec;
644 dev->req.bi_vcnt++;
645 dev->req.bi_max_vecs++;
646 dev->vec.bv_page = dev->page;
647 dev->vec.bv_len = STRIPE_SIZE;
648 dev->vec.bv_offset = 0;
649
650 dev->req.bi_sector = sh->sector;
651 dev->req.bi_private = sh;
652
653 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -0700654 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static void error(mddev_t *mddev, mdk_rdev_t *rdev)
658{
659 char b[BDEVNAME_SIZE];
660 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
661 PRINTK("raid5: error called\n");
662
NeilBrownb2d444d2005-11-08 21:39:31 -0800663 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -0700664 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -0700665 if (test_and_clear_bit(In_sync, &rdev->flags)) {
666 unsigned long flags;
667 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -0700669 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * if recovery was running, make sure it aborts.
672 */
673 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
674 }
NeilBrownb2d444d2005-11-08 21:39:31 -0800675 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 printk (KERN_ALERT
677 "raid5: Disk failure on %s, disabling device."
678 " Operation continuing on %d devices\n",
NeilBrown02c2de82006-10-03 01:15:47 -0700679 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
NeilBrown16a53ec2006-06-26 00:27:38 -0700681}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683/*
684 * Input: a 'big' sector number,
685 * Output: index of the data and parity disk, and the sector # in them.
686 */
687static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
688 unsigned int data_disks, unsigned int * dd_idx,
689 unsigned int * pd_idx, raid5_conf_t *conf)
690{
691 long stripe;
692 unsigned long chunk_number;
693 unsigned int chunk_offset;
694 sector_t new_sector;
695 int sectors_per_chunk = conf->chunk_size >> 9;
696
697 /* First compute the information on this sector */
698
699 /*
700 * Compute the chunk number and the sector offset inside the chunk
701 */
702 chunk_offset = sector_div(r_sector, sectors_per_chunk);
703 chunk_number = r_sector;
704 BUG_ON(r_sector != chunk_number);
705
706 /*
707 * Compute the stripe number
708 */
709 stripe = chunk_number / data_disks;
710
711 /*
712 * Compute the data disk and parity disk indexes inside the stripe
713 */
714 *dd_idx = chunk_number % data_disks;
715
716 /*
717 * Select the parity disk based on the user selected algorithm.
718 */
NeilBrown16a53ec2006-06-26 00:27:38 -0700719 switch(conf->level) {
720 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 *pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -0700722 break;
723 case 5:
724 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 case ALGORITHM_LEFT_ASYMMETRIC:
726 *pd_idx = data_disks - stripe % raid_disks;
727 if (*dd_idx >= *pd_idx)
728 (*dd_idx)++;
729 break;
730 case ALGORITHM_RIGHT_ASYMMETRIC:
731 *pd_idx = stripe % raid_disks;
732 if (*dd_idx >= *pd_idx)
733 (*dd_idx)++;
734 break;
735 case ALGORITHM_LEFT_SYMMETRIC:
736 *pd_idx = data_disks - stripe % raid_disks;
737 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
738 break;
739 case ALGORITHM_RIGHT_SYMMETRIC:
740 *pd_idx = stripe % raid_disks;
741 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
742 break;
743 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800744 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700746 }
747 break;
748 case 6:
749
750 /**** FIX THIS ****/
751 switch (conf->algorithm) {
752 case ALGORITHM_LEFT_ASYMMETRIC:
753 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
754 if (*pd_idx == raid_disks-1)
755 (*dd_idx)++; /* Q D D D P */
756 else if (*dd_idx >= *pd_idx)
757 (*dd_idx) += 2; /* D D P Q D */
758 break;
759 case ALGORITHM_RIGHT_ASYMMETRIC:
760 *pd_idx = stripe % raid_disks;
761 if (*pd_idx == raid_disks-1)
762 (*dd_idx)++; /* Q D D D P */
763 else if (*dd_idx >= *pd_idx)
764 (*dd_idx) += 2; /* D D P Q D */
765 break;
766 case ALGORITHM_LEFT_SYMMETRIC:
767 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
768 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
769 break;
770 case ALGORITHM_RIGHT_SYMMETRIC:
771 *pd_idx = stripe % raid_disks;
772 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
773 break;
774 default:
775 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
776 conf->algorithm);
777 }
778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 /*
782 * Finally, compute the new sector number
783 */
784 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
785 return new_sector;
786}
787
788
789static sector_t compute_blocknr(struct stripe_head *sh, int i)
790{
791 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -0800792 int raid_disks = sh->disks;
793 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 sector_t new_sector = sh->sector, check;
795 int sectors_per_chunk = conf->chunk_size >> 9;
796 sector_t stripe;
797 int chunk_offset;
798 int chunk_number, dummy1, dummy2, dd_idx = i;
799 sector_t r_sector;
800
NeilBrown16a53ec2006-06-26 00:27:38 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 chunk_offset = sector_div(new_sector, sectors_per_chunk);
803 stripe = new_sector;
804 BUG_ON(new_sector != stripe);
805
NeilBrown16a53ec2006-06-26 00:27:38 -0700806 if (i == sh->pd_idx)
807 return 0;
808 switch(conf->level) {
809 case 4: break;
810 case 5:
811 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 case ALGORITHM_LEFT_ASYMMETRIC:
813 case ALGORITHM_RIGHT_ASYMMETRIC:
814 if (i > sh->pd_idx)
815 i--;
816 break;
817 case ALGORITHM_LEFT_SYMMETRIC:
818 case ALGORITHM_RIGHT_SYMMETRIC:
819 if (i < sh->pd_idx)
820 i += raid_disks;
821 i -= (sh->pd_idx + 1);
822 break;
823 default:
NeilBrown14f8d262006-01-06 00:20:14 -0800824 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -0700825 conf->algorithm);
826 }
827 break;
828 case 6:
NeilBrown16a53ec2006-06-26 00:27:38 -0700829 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
830 return 0; /* It is the Q disk */
831 switch (conf->algorithm) {
832 case ALGORITHM_LEFT_ASYMMETRIC:
833 case ALGORITHM_RIGHT_ASYMMETRIC:
834 if (sh->pd_idx == raid_disks-1)
835 i--; /* Q D D D P */
836 else if (i > sh->pd_idx)
837 i -= 2; /* D D P Q D */
838 break;
839 case ALGORITHM_LEFT_SYMMETRIC:
840 case ALGORITHM_RIGHT_SYMMETRIC:
841 if (sh->pd_idx == raid_disks-1)
842 i--; /* Q D D D P */
843 else {
844 /* D D P Q D */
845 if (i < sh->pd_idx)
846 i += raid_disks;
847 i -= (sh->pd_idx + 2);
848 }
849 break;
850 default:
851 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 conf->algorithm);
NeilBrown16a53ec2006-06-26 00:27:38 -0700853 }
854 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 chunk_number = stripe * data_disks + i;
858 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
859
860 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
861 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -0800862 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
864 }
865 return r_sector;
866}
867
868
869
870/*
NeilBrown16a53ec2006-06-26 00:27:38 -0700871 * Copy data between a page in the stripe cache, and one or more bion
872 * The page could align with the middle of the bio, or there could be
873 * several bion, each with several bio_vecs, which cover part of the page
874 * Multiple bion are linked together on bi_next. There may be extras
875 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
877static void copy_data(int frombio, struct bio *bio,
878 struct page *page,
879 sector_t sector)
880{
881 char *pa = page_address(page);
882 struct bio_vec *bvl;
883 int i;
884 int page_offset;
885
886 if (bio->bi_sector >= sector)
887 page_offset = (signed)(bio->bi_sector - sector) * 512;
888 else
889 page_offset = (signed)(sector - bio->bi_sector) * -512;
890 bio_for_each_segment(bvl, bio, i) {
891 int len = bio_iovec_idx(bio,i)->bv_len;
892 int clen;
893 int b_offset = 0;
894
895 if (page_offset < 0) {
896 b_offset = -page_offset;
897 page_offset += b_offset;
898 len -= b_offset;
899 }
900
901 if (len > 0 && page_offset + len > STRIPE_SIZE)
902 clen = STRIPE_SIZE - page_offset;
903 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (clen > 0) {
906 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
907 if (frombio)
908 memcpy(pa+page_offset, ba+b_offset, clen);
909 else
910 memcpy(ba+b_offset, pa+page_offset, clen);
911 __bio_kunmap_atomic(ba, KM_USER0);
912 }
913 if (clen < len) /* hit end of page */
914 break;
915 page_offset += len;
916 }
917}
918
Dan Williams9bc89cd2007-01-02 11:10:44 -0700919#define check_xor() do { \
920 if (count == MAX_XOR_BLOCKS) { \
921 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
922 count = 0; \
923 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 } while(0)
925
926
927static void compute_block(struct stripe_head *sh, int dd_idx)
928{
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800929 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -0700930 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 PRINTK("compute_block, stripe %llu, idx %d\n",
933 (unsigned long long)sh->sector, dd_idx);
934
Dan Williams9bc89cd2007-01-02 11:10:44 -0700935 dest = page_address(sh->dev[dd_idx].page);
936 memset(dest, 0, STRIPE_SIZE);
937 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 for (i = disks ; i--; ) {
939 if (i == dd_idx)
940 continue;
941 p = page_address(sh->dev[i].page);
942 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
943 ptr[count++] = p;
944 else
NeilBrown14f8d262006-01-06 00:20:14 -0800945 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 " not present\n", dd_idx,
947 (unsigned long long)sh->sector, i);
948
949 check_xor();
950 }
Dan Williams9bc89cd2007-01-02 11:10:44 -0700951 if (count)
952 xor_blocks(count, STRIPE_SIZE, dest, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
954}
955
NeilBrown16a53ec2006-06-26 00:27:38 -0700956static void compute_parity5(struct stripe_head *sh, int method)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800959 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
Dan Williams9bc89cd2007-01-02 11:10:44 -0700960 void *ptr[MAX_XOR_BLOCKS], *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct bio *chosen;
962
NeilBrown16a53ec2006-06-26 00:27:38 -0700963 PRINTK("compute_parity5, stripe %llu, method %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 (unsigned long long)sh->sector, method);
965
Dan Williams9bc89cd2007-01-02 11:10:44 -0700966 count = 0;
967 dest = page_address(sh->dev[pd_idx].page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 switch(method) {
969 case READ_MODIFY_WRITE:
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200970 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 for (i=disks ; i-- ;) {
972 if (i==pd_idx)
973 continue;
974 if (sh->dev[i].towrite &&
975 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
976 ptr[count++] = page_address(sh->dev[i].page);
977 chosen = sh->dev[i].towrite;
978 sh->dev[i].towrite = NULL;
979
980 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
981 wake_up(&conf->wait_for_overlap);
982
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200983 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 sh->dev[i].written = chosen;
985 check_xor();
986 }
987 }
988 break;
989 case RECONSTRUCT_WRITE:
Dan Williams9bc89cd2007-01-02 11:10:44 -0700990 memset(dest, 0, STRIPE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 for (i= disks; i-- ;)
992 if (i!=pd_idx && sh->dev[i].towrite) {
993 chosen = sh->dev[i].towrite;
994 sh->dev[i].towrite = NULL;
995
996 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
997 wake_up(&conf->wait_for_overlap);
998
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200999 BUG_ON(sh->dev[i].written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 sh->dev[i].written = chosen;
1001 }
1002 break;
1003 case CHECK_PARITY:
1004 break;
1005 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001006 if (count) {
1007 xor_blocks(count, STRIPE_SIZE, dest, ptr);
1008 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 for (i = disks; i--;)
1012 if (sh->dev[i].written) {
1013 sector_t sector = sh->dev[i].sector;
1014 struct bio *wbi = sh->dev[i].written;
1015 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1016 copy_data(1, wbi, sh->dev[i].page, sector);
1017 wbi = r5_next_bio(wbi, sector);
1018 }
1019
1020 set_bit(R5_LOCKED, &sh->dev[i].flags);
1021 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1022 }
1023
1024 switch(method) {
1025 case RECONSTRUCT_WRITE:
1026 case CHECK_PARITY:
1027 for (i=disks; i--;)
1028 if (i != pd_idx) {
1029 ptr[count++] = page_address(sh->dev[i].page);
1030 check_xor();
1031 }
1032 break;
1033 case READ_MODIFY_WRITE:
1034 for (i = disks; i--;)
1035 if (sh->dev[i].written) {
1036 ptr[count++] = page_address(sh->dev[i].page);
1037 check_xor();
1038 }
1039 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001040 if (count)
1041 xor_blocks(count, STRIPE_SIZE, dest, ptr);
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (method != CHECK_PARITY) {
1044 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1045 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1046 } else
1047 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1048}
1049
NeilBrown16a53ec2006-06-26 00:27:38 -07001050static void compute_parity6(struct stripe_head *sh, int method)
1051{
1052 raid6_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08001053 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown16a53ec2006-06-26 00:27:38 -07001054 struct bio *chosen;
1055 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1056 void *ptrs[disks];
1057
1058 qd_idx = raid6_next_disk(pd_idx, disks);
1059 d0_idx = raid6_next_disk(qd_idx, disks);
1060
1061 PRINTK("compute_parity, stripe %llu, method %d\n",
1062 (unsigned long long)sh->sector, method);
1063
1064 switch(method) {
1065 case READ_MODIFY_WRITE:
1066 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1067 case RECONSTRUCT_WRITE:
1068 for (i= disks; i-- ;)
1069 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1070 chosen = sh->dev[i].towrite;
1071 sh->dev[i].towrite = NULL;
1072
1073 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1074 wake_up(&conf->wait_for_overlap);
1075
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001076 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001077 sh->dev[i].written = chosen;
1078 }
1079 break;
1080 case CHECK_PARITY:
1081 BUG(); /* Not implemented yet */
1082 }
1083
1084 for (i = disks; i--;)
1085 if (sh->dev[i].written) {
1086 sector_t sector = sh->dev[i].sector;
1087 struct bio *wbi = sh->dev[i].written;
1088 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1089 copy_data(1, wbi, sh->dev[i].page, sector);
1090 wbi = r5_next_bio(wbi, sector);
1091 }
1092
1093 set_bit(R5_LOCKED, &sh->dev[i].flags);
1094 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1095 }
1096
1097// switch(method) {
1098// case RECONSTRUCT_WRITE:
1099// case CHECK_PARITY:
1100// case UPDATE_PARITY:
1101 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1102 /* FIX: Is this ordering of drives even remotely optimal? */
1103 count = 0;
1104 i = d0_idx;
1105 do {
1106 ptrs[count++] = page_address(sh->dev[i].page);
1107 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1108 printk("block %d/%d not uptodate on parity calc\n", i,count);
1109 i = raid6_next_disk(i, disks);
1110 } while ( i != d0_idx );
1111// break;
1112// }
1113
1114 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1115
1116 switch(method) {
1117 case RECONSTRUCT_WRITE:
1118 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1119 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1120 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1121 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1122 break;
1123 case UPDATE_PARITY:
1124 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1125 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1126 break;
1127 }
1128}
1129
1130
1131/* Compute one missing block */
1132static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1133{
NeilBrownf4168852007-02-28 20:11:53 -08001134 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07001135 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrown16a53ec2006-06-26 00:27:38 -07001136 int pd_idx = sh->pd_idx;
1137 int qd_idx = raid6_next_disk(pd_idx, disks);
1138
1139 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1140 (unsigned long long)sh->sector, dd_idx);
1141
1142 if ( dd_idx == qd_idx ) {
1143 /* We're actually computing the Q drive */
1144 compute_parity6(sh, UPDATE_PARITY);
1145 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07001146 dest = page_address(sh->dev[dd_idx].page);
1147 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1148 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001149 for (i = disks ; i--; ) {
1150 if (i == dd_idx || i == qd_idx)
1151 continue;
1152 p = page_address(sh->dev[i].page);
1153 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1154 ptr[count++] = p;
1155 else
1156 printk("compute_block() %d, stripe %llu, %d"
1157 " not present\n", dd_idx,
1158 (unsigned long long)sh->sector, i);
1159
1160 check_xor();
1161 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001162 if (count)
1163 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07001164 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1165 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1166 }
1167}
1168
1169/* Compute two missing blocks */
1170static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1171{
NeilBrownf4168852007-02-28 20:11:53 -08001172 int i, count, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001173 int pd_idx = sh->pd_idx;
1174 int qd_idx = raid6_next_disk(pd_idx, disks);
1175 int d0_idx = raid6_next_disk(qd_idx, disks);
1176 int faila, failb;
1177
1178 /* faila and failb are disk numbers relative to d0_idx */
1179 /* pd_idx become disks-2 and qd_idx become disks-1 */
1180 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1181 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1182
1183 BUG_ON(faila == failb);
1184 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1185
1186 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1187 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1188
1189 if ( failb == disks-1 ) {
1190 /* Q disk is one of the missing disks */
1191 if ( faila == disks-2 ) {
1192 /* Missing P+Q, just recompute */
1193 compute_parity6(sh, UPDATE_PARITY);
1194 return;
1195 } else {
1196 /* We're missing D+Q; recompute D from P */
1197 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1198 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1199 return;
1200 }
1201 }
1202
1203 /* We're missing D+P or D+D; build pointer table */
1204 {
1205 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1206 void *ptrs[disks];
1207
1208 count = 0;
1209 i = d0_idx;
1210 do {
1211 ptrs[count++] = page_address(sh->dev[i].page);
1212 i = raid6_next_disk(i, disks);
1213 if (i != dd_idx1 && i != dd_idx2 &&
1214 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1215 printk("compute_2 with missing block %d/%d\n", count, i);
1216 } while ( i != d0_idx );
1217
1218 if ( failb == disks-2 ) {
1219 /* We're missing D+P. */
1220 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1221 } else {
1222 /* We're missing D+D. */
1223 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1224 }
1225
1226 /* Both the above update both missing blocks */
1227 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1228 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1229 }
1230}
1231
1232
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/*
1235 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001236 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * The bi_next chain must be in order.
1238 */
1239static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1240{
1241 struct bio **bip;
1242 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001243 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1246 (unsigned long long)bi->bi_sector,
1247 (unsigned long long)sh->sector);
1248
1249
1250 spin_lock(&sh->lock);
1251 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001252 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001254 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1255 firstwrite = 1;
1256 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 bip = &sh->dev[dd_idx].toread;
1258 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1259 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1260 goto overlap;
1261 bip = & (*bip)->bi_next;
1262 }
1263 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1264 goto overlap;
1265
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001266 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (*bip)
1268 bi->bi_next = *bip;
1269 *bip = bi;
1270 bi->bi_phys_segments ++;
1271 spin_unlock_irq(&conf->device_lock);
1272 spin_unlock(&sh->lock);
1273
1274 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1275 (unsigned long long)bi->bi_sector,
1276 (unsigned long long)sh->sector, dd_idx);
1277
NeilBrown72626682005-09-09 16:23:54 -07001278 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001279 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1280 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001281 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001282 set_bit(STRIPE_BIT_DELAY, &sh->state);
1283 }
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (forwrite) {
1286 /* check if page is covered */
1287 sector_t sector = sh->dev[dd_idx].sector;
1288 for (bi=sh->dev[dd_idx].towrite;
1289 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1290 bi && bi->bi_sector <= sector;
1291 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1292 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1293 sector = bi->bi_sector + (bi->bi_size>>9);
1294 }
1295 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1296 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1297 }
1298 return 1;
1299
1300 overlap:
1301 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1302 spin_unlock_irq(&conf->device_lock);
1303 spin_unlock(&sh->lock);
1304 return 0;
1305}
1306
NeilBrown29269552006-03-27 01:18:10 -08001307static void end_reshape(raid5_conf_t *conf);
1308
NeilBrown16a53ec2006-06-26 00:27:38 -07001309static int page_is_zero(struct page *p)
1310{
1311 char *a = page_address(p);
1312 return ((*(u32*)a) == 0 &&
1313 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1314}
1315
NeilBrownccfcc3c2006-03-27 01:18:09 -08001316static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1317{
1318 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001319 int pd_idx, dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001320 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1321
NeilBrownb875e532006-12-10 02:20:49 -08001322 raid5_compute_sector(stripe * (disks - conf->max_degraded)
1323 *sectors_per_chunk + chunk_offset,
1324 disks, disks - conf->max_degraded,
1325 &dd_idx, &pd_idx, conf);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001326 return pd_idx;
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330/*
1331 * handle_stripe - do things to a stripe.
1332 *
1333 * We lock the stripe and then examine the state of various bits
1334 * to see what needs to be done.
1335 * Possible results:
1336 * return some read request which now have data
1337 * return some write requests which are safely on disc
1338 * schedule a read on some buffers
1339 * schedule a write of some buffers
1340 * return confirmation of parity correctness
1341 *
1342 * Parity calculations are done inside the stripe lock
1343 * buffers are taken off read_list or write_list, and bh_cache buffers
1344 * get BH_Lock set before the stripe lock is released.
1345 *
1346 */
1347
NeilBrown16a53ec2006-06-26 00:27:38 -07001348static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001351 int disks = sh->disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct bio *return_bi= NULL;
1353 struct bio *bi;
1354 int i;
NeilBrownccfcc3c2006-03-27 01:18:09 -08001355 int syncing, expanding, expanded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1357 int non_overwrite = 0;
1358 int failed_num=0;
1359 struct r5dev *dev;
1360
1361 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1362 (unsigned long long)sh->sector, atomic_read(&sh->count),
1363 sh->pd_idx);
1364
1365 spin_lock(&sh->lock);
1366 clear_bit(STRIPE_HANDLE, &sh->state);
1367 clear_bit(STRIPE_DELAYED, &sh->state);
1368
1369 syncing = test_bit(STRIPE_SYNCING, &sh->state);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001370 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1371 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /* Now to look around and see what can be done */
1373
NeilBrown9910f162006-01-06 00:20:24 -08001374 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 for (i=disks; i--; ) {
1376 mdk_rdev_t *rdev;
1377 dev = &sh->dev[i];
1378 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1381 i, dev->flags, dev->toread, dev->towrite, dev->written);
1382 /* maybe we can reply to a read */
1383 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1384 struct bio *rbi, *rbi2;
1385 PRINTK("Return read for disc %d\n", i);
1386 spin_lock_irq(&conf->device_lock);
1387 rbi = dev->toread;
1388 dev->toread = NULL;
1389 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1390 wake_up(&conf->wait_for_overlap);
1391 spin_unlock_irq(&conf->device_lock);
1392 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1393 copy_data(0, rbi, dev->page, dev->sector);
1394 rbi2 = r5_next_bio(rbi, dev->sector);
1395 spin_lock_irq(&conf->device_lock);
1396 if (--rbi->bi_phys_segments == 0) {
1397 rbi->bi_next = return_bi;
1398 return_bi = rbi;
1399 }
1400 spin_unlock_irq(&conf->device_lock);
1401 rbi = rbi2;
1402 }
1403 }
1404
1405 /* now count some things */
1406 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1407 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1408
1409
1410 if (dev->toread) to_read++;
1411 if (dev->towrite) {
1412 to_write++;
1413 if (!test_bit(R5_OVERWRITE, &dev->flags))
1414 non_overwrite++;
1415 }
1416 if (dev->written) written++;
NeilBrown9910f162006-01-06 00:20:24 -08001417 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001418 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001419 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08001420 clear_bit(R5_ReadError, &dev->flags);
1421 clear_bit(R5_ReWrite, &dev->flags);
1422 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001423 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001424 || test_bit(R5_ReadError, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 failed++;
1426 failed_num = i;
1427 } else
1428 set_bit(R5_Insync, &dev->flags);
1429 }
NeilBrown9910f162006-01-06 00:20:24 -08001430 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 PRINTK("locked=%d uptodate=%d to_read=%d"
1432 " to_write=%d failed=%d failed_num=%d\n",
1433 locked, uptodate, to_read, to_write, failed, failed_num);
1434 /* check if the array has lost two devices and, if so, some requests might
1435 * need to be failed
1436 */
1437 if (failed > 1 && to_read+to_write+written) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 for (i=disks; i--; ) {
NeilBrown72626682005-09-09 16:23:54 -07001439 int bitmap_end = 0;
NeilBrown4e5314b2005-11-08 21:39:22 -08001440
1441 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown9910f162006-01-06 00:20:24 -08001442 mdk_rdev_t *rdev;
1443 rcu_read_lock();
1444 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001445 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001446 /* multiple read failures in one stripe */
1447 md_error(conf->mddev, rdev);
NeilBrown9910f162006-01-06 00:20:24 -08001448 rcu_read_unlock();
NeilBrown4e5314b2005-11-08 21:39:22 -08001449 }
1450
NeilBrown72626682005-09-09 16:23:54 -07001451 spin_lock_irq(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 /* fail all writes first */
1453 bi = sh->dev[i].towrite;
1454 sh->dev[i].towrite = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001455 if (bi) { to_write--; bitmap_end = 1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1458 wake_up(&conf->wait_for_overlap);
1459
1460 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1461 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1462 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1463 if (--bi->bi_phys_segments == 0) {
1464 md_write_end(conf->mddev);
1465 bi->bi_next = return_bi;
1466 return_bi = bi;
1467 }
1468 bi = nextbi;
1469 }
1470 /* and fail all 'written' */
1471 bi = sh->dev[i].written;
1472 sh->dev[i].written = NULL;
NeilBrown72626682005-09-09 16:23:54 -07001473 if (bi) bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1475 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1476 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1477 if (--bi->bi_phys_segments == 0) {
1478 md_write_end(conf->mddev);
1479 bi->bi_next = return_bi;
1480 return_bi = bi;
1481 }
1482 bi = bi2;
1483 }
1484
1485 /* fail any reads if this device is non-operational */
NeilBrown4e5314b2005-11-08 21:39:22 -08001486 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1487 test_bit(R5_ReadError, &sh->dev[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 bi = sh->dev[i].toread;
1489 sh->dev[i].toread = NULL;
1490 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1491 wake_up(&conf->wait_for_overlap);
1492 if (bi) to_read--;
1493 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1494 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1495 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1496 if (--bi->bi_phys_segments == 0) {
1497 bi->bi_next = return_bi;
1498 return_bi = bi;
1499 }
1500 bi = nextbi;
1501 }
1502 }
NeilBrown72626682005-09-09 16:23:54 -07001503 spin_unlock_irq(&conf->device_lock);
1504 if (bitmap_end)
1505 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1506 STRIPE_SECTORS, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 if (failed > 1 && syncing) {
1510 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1511 clear_bit(STRIPE_SYNCING, &sh->state);
1512 syncing = 0;
1513 }
1514
1515 /* might be able to return some write requests if the parity block
1516 * is safe, or on a failed drive
1517 */
1518 dev = &sh->dev[sh->pd_idx];
1519 if ( written &&
1520 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1521 test_bit(R5_UPTODATE, &dev->flags))
1522 || (failed == 1 && failed_num == sh->pd_idx))
1523 ) {
1524 /* any written block on an uptodate or failed drive can be returned.
1525 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1526 * never LOCKED, so we don't need to test 'failed' directly.
1527 */
1528 for (i=disks; i--; )
1529 if (sh->dev[i].written) {
1530 dev = &sh->dev[i];
1531 if (!test_bit(R5_LOCKED, &dev->flags) &&
1532 test_bit(R5_UPTODATE, &dev->flags) ) {
1533 /* We can return any write requests */
1534 struct bio *wbi, *wbi2;
NeilBrown72626682005-09-09 16:23:54 -07001535 int bitmap_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 PRINTK("Return write for disc %d\n", i);
1537 spin_lock_irq(&conf->device_lock);
1538 wbi = dev->written;
1539 dev->written = NULL;
1540 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1541 wbi2 = r5_next_bio(wbi, dev->sector);
1542 if (--wbi->bi_phys_segments == 0) {
1543 md_write_end(conf->mddev);
1544 wbi->bi_next = return_bi;
1545 return_bi = wbi;
1546 }
1547 wbi = wbi2;
1548 }
NeilBrown72626682005-09-09 16:23:54 -07001549 if (dev->towrite == NULL)
1550 bitmap_end = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001552 if (bitmap_end)
1553 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1554 STRIPE_SECTORS,
1555 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557 }
1558 }
1559
1560 /* Now we might consider reading some blocks, either to check/generate
1561 * parity, or to satisfy requests
1562 * or to load a block that is being partially written.
1563 */
NeilBrownccfcc3c2006-03-27 01:18:09 -08001564 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 for (i=disks; i--;) {
1566 dev = &sh->dev[i];
1567 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1568 (dev->toread ||
1569 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1570 syncing ||
NeilBrownccfcc3c2006-03-27 01:18:09 -08001571 expanding ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 (failed && (sh->dev[failed_num].toread ||
1573 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1574 )
1575 ) {
1576 /* we would like to get this block, possibly
1577 * by computing it, but we might not be able to
1578 */
1579 if (uptodate == disks-1) {
1580 PRINTK("Computing block %d\n", i);
1581 compute_block(sh, i);
1582 uptodate++;
1583 } else if (test_bit(R5_Insync, &dev->flags)) {
1584 set_bit(R5_LOCKED, &dev->flags);
1585 set_bit(R5_Wantread, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 locked++;
1587 PRINTK("Reading block %d (sync=%d)\n",
1588 i, syncing);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 }
1591 }
1592 set_bit(STRIPE_HANDLE, &sh->state);
1593 }
1594
1595 /* now to consider writing and what else, if anything should be read */
1596 if (to_write) {
1597 int rmw=0, rcw=0;
1598 for (i=disks ; i--;) {
1599 /* would I have to read this buffer for read_modify_write */
1600 dev = &sh->dev[i];
1601 if ((dev->towrite || i == sh->pd_idx) &&
1602 (!test_bit(R5_LOCKED, &dev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 ) &&
1604 !test_bit(R5_UPTODATE, &dev->flags)) {
1605 if (test_bit(R5_Insync, &dev->flags)
1606/* && !(!mddev->insync && i == sh->pd_idx) */
1607 )
1608 rmw++;
1609 else rmw += 2*disks; /* cannot read it */
1610 }
1611 /* Would I have to read this buffer for reconstruct_write */
1612 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1613 (!test_bit(R5_LOCKED, &dev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 ) &&
1615 !test_bit(R5_UPTODATE, &dev->flags)) {
1616 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1617 else rcw += 2*disks;
1618 }
1619 }
1620 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1621 (unsigned long long)sh->sector, rmw, rcw);
1622 set_bit(STRIPE_HANDLE, &sh->state);
1623 if (rmw < rcw && rmw > 0)
1624 /* prefer read-modify-write, but need to get some data */
1625 for (i=disks; i--;) {
1626 dev = &sh->dev[i];
1627 if ((dev->towrite || i == sh->pd_idx) &&
1628 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1629 test_bit(R5_Insync, &dev->flags)) {
1630 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1631 {
1632 PRINTK("Read_old block %d for r-m-w\n", i);
1633 set_bit(R5_LOCKED, &dev->flags);
1634 set_bit(R5_Wantread, &dev->flags);
1635 locked++;
1636 } else {
1637 set_bit(STRIPE_DELAYED, &sh->state);
1638 set_bit(STRIPE_HANDLE, &sh->state);
1639 }
1640 }
1641 }
1642 if (rcw <= rmw && rcw > 0)
1643 /* want reconstruct write, but need to get some data */
1644 for (i=disks; i--;) {
1645 dev = &sh->dev[i];
1646 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1647 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1648 test_bit(R5_Insync, &dev->flags)) {
1649 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1650 {
1651 PRINTK("Read_old block %d for Reconstruct\n", i);
1652 set_bit(R5_LOCKED, &dev->flags);
1653 set_bit(R5_Wantread, &dev->flags);
1654 locked++;
1655 } else {
1656 set_bit(STRIPE_DELAYED, &sh->state);
1657 set_bit(STRIPE_HANDLE, &sh->state);
1658 }
1659 }
1660 }
1661 /* now if nothing is locked, and if we have enough data, we can start a write request */
NeilBrown72626682005-09-09 16:23:54 -07001662 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1663 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 PRINTK("Computing parity...\n");
NeilBrown16a53ec2006-06-26 00:27:38 -07001665 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /* now every locked buffer is ready to be written */
1667 for (i=disks; i--;)
1668 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1669 PRINTK("Writing block %d\n", i);
1670 locked++;
1671 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1672 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1673 || (i==sh->pd_idx && failed == 0))
1674 set_bit(STRIPE_INSYNC, &sh->state);
1675 }
1676 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1677 atomic_dec(&conf->preread_active_stripes);
1678 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1679 md_wakeup_thread(conf->mddev->thread);
1680 }
1681 }
1682 }
1683
1684 /* maybe we need to check and possibly fix the parity for this stripe
1685 * Any reads will already have been scheduled, so we just see if enough data
1686 * is available
1687 */
1688 if (syncing && locked == 0 &&
NeilBrown14f8d262006-01-06 00:20:14 -08001689 !test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 set_bit(STRIPE_HANDLE, &sh->state);
1691 if (failed == 0) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001692 BUG_ON(uptodate != disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001693 compute_parity5(sh, CHECK_PARITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 uptodate--;
NeilBrown16a53ec2006-06-26 00:27:38 -07001695 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /* parity is correct (on disc, not in buffer any more) */
1697 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown9d888832005-11-08 21:39:26 -08001698 } else {
1699 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1700 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1701 /* don't try to repair!! */
1702 set_bit(STRIPE_INSYNC, &sh->state);
NeilBrown14f8d262006-01-06 00:20:14 -08001703 else {
1704 compute_block(sh, sh->pd_idx);
1705 uptodate++;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708 }
1709 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown14f8d262006-01-06 00:20:14 -08001710 /* either failed parity check, or recovery is happening */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (failed==0)
1712 failed_num = sh->pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 dev = &sh->dev[failed_num];
NeilBrown14f8d262006-01-06 00:20:14 -08001714 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1715 BUG_ON(uptodate != disks);
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 set_bit(R5_LOCKED, &dev->flags);
1718 set_bit(R5_Wantwrite, &dev->flags);
NeilBrown72626682005-09-09 16:23:54 -07001719 clear_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 locked++;
1721 set_bit(STRIPE_INSYNC, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 }
1724 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1725 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1726 clear_bit(STRIPE_SYNCING, &sh->state);
1727 }
NeilBrown4e5314b2005-11-08 21:39:22 -08001728
1729 /* If the failed drive is just a ReadError, then we might need to progress
1730 * the repair/check process
1731 */
NeilBrownba22dcb2005-11-08 21:39:31 -08001732 if (failed == 1 && ! conf->mddev->ro &&
1733 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08001734 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1735 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1736 ) {
1737 dev = &sh->dev[failed_num];
1738 if (!test_bit(R5_ReWrite, &dev->flags)) {
1739 set_bit(R5_Wantwrite, &dev->flags);
1740 set_bit(R5_ReWrite, &dev->flags);
1741 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001742 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001743 } else {
1744 /* let's read it back */
1745 set_bit(R5_Wantread, &dev->flags);
1746 set_bit(R5_LOCKED, &dev->flags);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001747 locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08001748 }
1749 }
1750
NeilBrownccfcc3c2006-03-27 01:18:09 -08001751 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1752 /* Need to write out all blocks after computing parity */
1753 sh->disks = conf->raid_disks;
1754 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001755 compute_parity5(sh, RECONSTRUCT_WRITE);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001756 for (i= conf->raid_disks; i--;) {
1757 set_bit(R5_LOCKED, &sh->dev[i].flags);
1758 locked++;
1759 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1760 }
1761 clear_bit(STRIPE_EXPANDING, &sh->state);
1762 } else if (expanded) {
1763 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08001764 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001765 wake_up(&conf->wait_for_overlap);
1766 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1767 }
1768
1769 if (expanding && locked == 0) {
1770 /* We have read all the blocks in this stripe and now we need to
1771 * copy some of them into a target stripe for expand.
1772 */
1773 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1774 for (i=0; i< sh->disks; i++)
1775 if (i != sh->pd_idx) {
1776 int dd_idx, pd_idx, j;
1777 struct stripe_head *sh2;
1778
1779 sector_t bn = compute_blocknr(sh, i);
1780 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1781 conf->raid_disks-1,
1782 &dd_idx, &pd_idx, conf);
1783 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1784 if (sh2 == NULL)
1785 /* so far only the early blocks of this stripe
1786 * have been requested. When later blocks
1787 * get requested, we will try again
1788 */
1789 continue;
1790 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1791 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1792 /* must have already done this block */
1793 release_stripe(sh2);
1794 continue;
1795 }
1796 memcpy(page_address(sh2->dev[dd_idx].page),
1797 page_address(sh->dev[i].page),
1798 STRIPE_SIZE);
1799 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1800 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1801 for (j=0; j<conf->raid_disks; j++)
1802 if (j != sh2->pd_idx &&
1803 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1804 break;
1805 if (j == conf->raid_disks) {
1806 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1807 set_bit(STRIPE_HANDLE, &sh2->state);
1808 }
1809 release_stripe(sh2);
1810 }
1811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 spin_unlock(&sh->lock);
1814
1815 while ((bi=return_bi)) {
1816 int bytes = bi->bi_size;
1817
1818 return_bi = bi->bi_next;
1819 bi->bi_next = NULL;
1820 bi->bi_size = 0;
NeilBrownc2b00852006-12-10 02:20:51 -08001821 bi->bi_end_io(bi, bytes,
1822 test_bit(BIO_UPTODATE, &bi->bi_flags)
1823 ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825 for (i=disks; i-- ;) {
1826 int rw;
1827 struct bio *bi;
1828 mdk_rdev_t *rdev;
1829 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
NeilBrown802ba062006-12-13 00:34:13 -08001830 rw = WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
NeilBrown802ba062006-12-13 00:34:13 -08001832 rw = READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 else
1834 continue;
1835
1836 bi = &sh->dev[i].req;
1837
1838 bi->bi_rw = rw;
NeilBrown802ba062006-12-13 00:34:13 -08001839 if (rw == WRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 bi->bi_end_io = raid5_end_write_request;
1841 else
1842 bi->bi_end_io = raid5_end_read_request;
1843
1844 rcu_read_lock();
Suzanne Woodd6065f72005-11-08 21:39:27 -08001845 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001846 if (rdev && test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 rdev = NULL;
1848 if (rdev)
1849 atomic_inc(&rdev->nr_pending);
1850 rcu_read_unlock();
1851
1852 if (rdev) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08001853 if (syncing || expanding || expanded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1855
1856 bi->bi_bdev = rdev->bdev;
1857 PRINTK("for %llu schedule op %ld on disc %d\n",
1858 (unsigned long long)sh->sector, bi->bi_rw, i);
1859 atomic_inc(&sh->count);
1860 bi->bi_sector = sh->sector + rdev->data_offset;
1861 bi->bi_flags = 1 << BIO_UPTODATE;
1862 bi->bi_vcnt = 1;
1863 bi->bi_max_vecs = 1;
1864 bi->bi_idx = 0;
1865 bi->bi_io_vec = &sh->dev[i].vec;
1866 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1867 bi->bi_io_vec[0].bv_offset = 0;
1868 bi->bi_size = STRIPE_SIZE;
1869 bi->bi_next = NULL;
NeilBrown4dbcdc72006-01-06 00:20:52 -08001870 if (rw == WRITE &&
1871 test_bit(R5_ReWrite, &sh->dev[i].flags))
1872 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 generic_make_request(bi);
1874 } else {
NeilBrown802ba062006-12-13 00:34:13 -08001875 if (rw == WRITE)
NeilBrown72626682005-09-09 16:23:54 -07001876 set_bit(STRIPE_DEGRADED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 PRINTK("skip op %ld on disc %d for sector %llu\n",
1878 bi->bi_rw, i, (unsigned long long)sh->sector);
1879 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1880 set_bit(STRIPE_HANDLE, &sh->state);
1881 }
1882 }
1883}
1884
NeilBrown16a53ec2006-06-26 00:27:38 -07001885static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1886{
1887 raid6_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08001888 int disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001889 struct bio *return_bi= NULL;
1890 struct bio *bi;
1891 int i;
NeilBrownf4168852007-02-28 20:11:53 -08001892 int syncing, expanding, expanded;
NeilBrown16a53ec2006-06-26 00:27:38 -07001893 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1894 int non_overwrite = 0;
1895 int failed_num[2] = {0, 0};
1896 struct r5dev *dev, *pdev, *qdev;
1897 int pd_idx = sh->pd_idx;
1898 int qd_idx = raid6_next_disk(pd_idx, disks);
1899 int p_failed, q_failed;
1900
1901 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1902 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1903 pd_idx, qd_idx);
1904
1905 spin_lock(&sh->lock);
1906 clear_bit(STRIPE_HANDLE, &sh->state);
1907 clear_bit(STRIPE_DELAYED, &sh->state);
1908
1909 syncing = test_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf4168852007-02-28 20:11:53 -08001910 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1911 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07001912 /* Now to look around and see what can be done */
1913
1914 rcu_read_lock();
1915 for (i=disks; i--; ) {
1916 mdk_rdev_t *rdev;
1917 dev = &sh->dev[i];
1918 clear_bit(R5_Insync, &dev->flags);
1919
1920 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1921 i, dev->flags, dev->toread, dev->towrite, dev->written);
1922 /* maybe we can reply to a read */
1923 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1924 struct bio *rbi, *rbi2;
1925 PRINTK("Return read for disc %d\n", i);
1926 spin_lock_irq(&conf->device_lock);
1927 rbi = dev->toread;
1928 dev->toread = NULL;
1929 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1930 wake_up(&conf->wait_for_overlap);
1931 spin_unlock_irq(&conf->device_lock);
1932 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1933 copy_data(0, rbi, dev->page, dev->sector);
1934 rbi2 = r5_next_bio(rbi, dev->sector);
1935 spin_lock_irq(&conf->device_lock);
1936 if (--rbi->bi_phys_segments == 0) {
1937 rbi->bi_next = return_bi;
1938 return_bi = rbi;
1939 }
1940 spin_unlock_irq(&conf->device_lock);
1941 rbi = rbi2;
1942 }
1943 }
1944
1945 /* now count some things */
1946 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1947 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1948
1949
1950 if (dev->toread) to_read++;
1951 if (dev->towrite) {
1952 to_write++;
1953 if (!test_bit(R5_OVERWRITE, &dev->flags))
1954 non_overwrite++;
1955 }
1956 if (dev->written) written++;
1957 rdev = rcu_dereference(conf->disks[i].rdev);
1958 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1959 /* The ReadError flag will just be confusing now */
1960 clear_bit(R5_ReadError, &dev->flags);
1961 clear_bit(R5_ReWrite, &dev->flags);
1962 }
1963 if (!rdev || !test_bit(In_sync, &rdev->flags)
1964 || test_bit(R5_ReadError, &dev->flags)) {
1965 if ( failed < 2 )
1966 failed_num[failed] = i;
1967 failed++;
1968 } else
1969 set_bit(R5_Insync, &dev->flags);
1970 }
1971 rcu_read_unlock();
1972 PRINTK("locked=%d uptodate=%d to_read=%d"
1973 " to_write=%d failed=%d failed_num=%d,%d\n",
1974 locked, uptodate, to_read, to_write, failed,
1975 failed_num[0], failed_num[1]);
1976 /* check if the array has lost >2 devices and, if so, some requests might
1977 * need to be failed
1978 */
1979 if (failed > 2 && to_read+to_write+written) {
1980 for (i=disks; i--; ) {
1981 int bitmap_end = 0;
1982
1983 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1984 mdk_rdev_t *rdev;
1985 rcu_read_lock();
1986 rdev = rcu_dereference(conf->disks[i].rdev);
1987 if (rdev && test_bit(In_sync, &rdev->flags))
1988 /* multiple read failures in one stripe */
1989 md_error(conf->mddev, rdev);
1990 rcu_read_unlock();
1991 }
1992
1993 spin_lock_irq(&conf->device_lock);
1994 /* fail all writes first */
1995 bi = sh->dev[i].towrite;
1996 sh->dev[i].towrite = NULL;
1997 if (bi) { to_write--; bitmap_end = 1; }
1998
1999 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2000 wake_up(&conf->wait_for_overlap);
2001
2002 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2003 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2004 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2005 if (--bi->bi_phys_segments == 0) {
2006 md_write_end(conf->mddev);
2007 bi->bi_next = return_bi;
2008 return_bi = bi;
2009 }
2010 bi = nextbi;
2011 }
2012 /* and fail all 'written' */
2013 bi = sh->dev[i].written;
2014 sh->dev[i].written = NULL;
2015 if (bi) bitmap_end = 1;
2016 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2017 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2018 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2019 if (--bi->bi_phys_segments == 0) {
2020 md_write_end(conf->mddev);
2021 bi->bi_next = return_bi;
2022 return_bi = bi;
2023 }
2024 bi = bi2;
2025 }
2026
2027 /* fail any reads if this device is non-operational */
2028 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2029 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2030 bi = sh->dev[i].toread;
2031 sh->dev[i].toread = NULL;
2032 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2033 wake_up(&conf->wait_for_overlap);
2034 if (bi) to_read--;
2035 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2036 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2037 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2038 if (--bi->bi_phys_segments == 0) {
2039 bi->bi_next = return_bi;
2040 return_bi = bi;
2041 }
2042 bi = nextbi;
2043 }
2044 }
2045 spin_unlock_irq(&conf->device_lock);
2046 if (bitmap_end)
2047 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2048 STRIPE_SECTORS, 0, 0);
2049 }
2050 }
2051 if (failed > 2 && syncing) {
2052 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2053 clear_bit(STRIPE_SYNCING, &sh->state);
2054 syncing = 0;
2055 }
2056
2057 /*
2058 * might be able to return some write requests if the parity blocks
2059 * are safe, or on a failed drive
2060 */
2061 pdev = &sh->dev[pd_idx];
2062 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2063 || (failed >= 2 && failed_num[1] == pd_idx);
2064 qdev = &sh->dev[qd_idx];
2065 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2066 || (failed >= 2 && failed_num[1] == qd_idx);
2067
2068 if ( written &&
2069 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2070 && !test_bit(R5_LOCKED, &pdev->flags)
2071 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2072 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2073 && !test_bit(R5_LOCKED, &qdev->flags)
2074 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2075 /* any written block on an uptodate or failed drive can be
2076 * returned. Note that if we 'wrote' to a failed drive,
2077 * it will be UPTODATE, but never LOCKED, so we don't need
2078 * to test 'failed' directly.
2079 */
2080 for (i=disks; i--; )
2081 if (sh->dev[i].written) {
2082 dev = &sh->dev[i];
2083 if (!test_bit(R5_LOCKED, &dev->flags) &&
2084 test_bit(R5_UPTODATE, &dev->flags) ) {
2085 /* We can return any write requests */
2086 int bitmap_end = 0;
2087 struct bio *wbi, *wbi2;
2088 PRINTK("Return write for stripe %llu disc %d\n",
2089 (unsigned long long)sh->sector, i);
2090 spin_lock_irq(&conf->device_lock);
2091 wbi = dev->written;
2092 dev->written = NULL;
2093 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2094 wbi2 = r5_next_bio(wbi, dev->sector);
2095 if (--wbi->bi_phys_segments == 0) {
2096 md_write_end(conf->mddev);
2097 wbi->bi_next = return_bi;
2098 return_bi = wbi;
2099 }
2100 wbi = wbi2;
2101 }
2102 if (dev->towrite == NULL)
2103 bitmap_end = 1;
2104 spin_unlock_irq(&conf->device_lock);
2105 if (bitmap_end)
2106 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2107 STRIPE_SECTORS,
2108 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2109 }
2110 }
2111 }
2112
2113 /* Now we might consider reading some blocks, either to check/generate
2114 * parity, or to satisfy requests
2115 * or to load a block that is being partially written.
2116 */
NeilBrownf4168852007-02-28 20:11:53 -08002117 if (to_read || non_overwrite || (to_write && failed) ||
2118 (syncing && (uptodate < disks)) || expanding) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002119 for (i=disks; i--;) {
2120 dev = &sh->dev[i];
2121 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2122 (dev->toread ||
2123 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2124 syncing ||
NeilBrownf4168852007-02-28 20:11:53 -08002125 expanding ||
NeilBrown16a53ec2006-06-26 00:27:38 -07002126 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2127 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2128 )
2129 ) {
2130 /* we would like to get this block, possibly
2131 * by computing it, but we might not be able to
2132 */
2133 if (uptodate == disks-1) {
2134 PRINTK("Computing stripe %llu block %d\n",
2135 (unsigned long long)sh->sector, i);
2136 compute_block_1(sh, i, 0);
2137 uptodate++;
2138 } else if ( uptodate == disks-2 && failed >= 2 ) {
2139 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2140 int other;
2141 for (other=disks; other--;) {
2142 if ( other == i )
2143 continue;
2144 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2145 break;
2146 }
2147 BUG_ON(other < 0);
2148 PRINTK("Computing stripe %llu blocks %d,%d\n",
2149 (unsigned long long)sh->sector, i, other);
2150 compute_block_2(sh, i, other);
2151 uptodate += 2;
2152 } else if (test_bit(R5_Insync, &dev->flags)) {
2153 set_bit(R5_LOCKED, &dev->flags);
2154 set_bit(R5_Wantread, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07002155 locked++;
2156 PRINTK("Reading block %d (sync=%d)\n",
2157 i, syncing);
2158 }
2159 }
2160 }
2161 set_bit(STRIPE_HANDLE, &sh->state);
2162 }
2163
2164 /* now to consider writing and what else, if anything should be read */
2165 if (to_write) {
2166 int rcw=0, must_compute=0;
2167 for (i=disks ; i--;) {
2168 dev = &sh->dev[i];
2169 /* Would I have to read this buffer for reconstruct_write */
2170 if (!test_bit(R5_OVERWRITE, &dev->flags)
2171 && i != pd_idx && i != qd_idx
2172 && (!test_bit(R5_LOCKED, &dev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07002173 ) &&
2174 !test_bit(R5_UPTODATE, &dev->flags)) {
2175 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2176 else {
2177 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2178 must_compute++;
2179 }
2180 }
2181 }
2182 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2183 (unsigned long long)sh->sector, rcw, must_compute);
2184 set_bit(STRIPE_HANDLE, &sh->state);
2185
2186 if (rcw > 0)
2187 /* want reconstruct write, but need to get some data */
2188 for (i=disks; i--;) {
2189 dev = &sh->dev[i];
2190 if (!test_bit(R5_OVERWRITE, &dev->flags)
2191 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2192 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2193 test_bit(R5_Insync, &dev->flags)) {
2194 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2195 {
2196 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2197 (unsigned long long)sh->sector, i);
2198 set_bit(R5_LOCKED, &dev->flags);
2199 set_bit(R5_Wantread, &dev->flags);
2200 locked++;
2201 } else {
2202 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2203 (unsigned long long)sh->sector, i);
2204 set_bit(STRIPE_DELAYED, &sh->state);
2205 set_bit(STRIPE_HANDLE, &sh->state);
2206 }
2207 }
2208 }
2209 /* now if nothing is locked, and if we have enough data, we can start a write request */
2210 if (locked == 0 && rcw == 0 &&
2211 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2212 if ( must_compute > 0 ) {
2213 /* We have failed blocks and need to compute them */
2214 switch ( failed ) {
2215 case 0: BUG();
2216 case 1: compute_block_1(sh, failed_num[0], 0); break;
2217 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2218 default: BUG(); /* This request should have been failed? */
2219 }
2220 }
2221
2222 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2223 compute_parity6(sh, RECONSTRUCT_WRITE);
2224 /* now every locked buffer is ready to be written */
2225 for (i=disks; i--;)
2226 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2227 PRINTK("Writing stripe %llu block %d\n",
2228 (unsigned long long)sh->sector, i);
2229 locked++;
2230 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2231 }
2232 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2233 set_bit(STRIPE_INSYNC, &sh->state);
2234
2235 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2236 atomic_dec(&conf->preread_active_stripes);
2237 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2238 md_wakeup_thread(conf->mddev->thread);
2239 }
2240 }
2241 }
2242
2243 /* maybe we need to check and possibly fix the parity for this stripe
2244 * Any reads will already have been scheduled, so we just see if enough data
2245 * is available
2246 */
2247 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2248 int update_p = 0, update_q = 0;
2249 struct r5dev *dev;
2250
2251 set_bit(STRIPE_HANDLE, &sh->state);
2252
2253 BUG_ON(failed>2);
2254 BUG_ON(uptodate < disks);
2255 /* Want to check and possibly repair P and Q.
2256 * However there could be one 'failed' device, in which
2257 * case we can only check one of them, possibly using the
2258 * other to generate missing data
2259 */
2260
2261 /* If !tmp_page, we cannot do the calculations,
2262 * but as we have set STRIPE_HANDLE, we will soon be called
2263 * by stripe_handle with a tmp_page - just wait until then.
2264 */
2265 if (tmp_page) {
2266 if (failed == q_failed) {
2267 /* The only possible failed device holds 'Q', so it makes
2268 * sense to check P (If anything else were failed, we would
2269 * have used P to recreate it).
2270 */
2271 compute_block_1(sh, pd_idx, 1);
2272 if (!page_is_zero(sh->dev[pd_idx].page)) {
2273 compute_block_1(sh,pd_idx,0);
2274 update_p = 1;
2275 }
2276 }
2277 if (!q_failed && failed < 2) {
2278 /* q is not failed, and we didn't use it to generate
2279 * anything, so it makes sense to check it
2280 */
2281 memcpy(page_address(tmp_page),
2282 page_address(sh->dev[qd_idx].page),
2283 STRIPE_SIZE);
2284 compute_parity6(sh, UPDATE_PARITY);
2285 if (memcmp(page_address(tmp_page),
2286 page_address(sh->dev[qd_idx].page),
2287 STRIPE_SIZE)!= 0) {
2288 clear_bit(STRIPE_INSYNC, &sh->state);
2289 update_q = 1;
2290 }
2291 }
2292 if (update_p || update_q) {
2293 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2294 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2295 /* don't try to repair!! */
2296 update_p = update_q = 0;
2297 }
2298
2299 /* now write out any block on a failed drive,
2300 * or P or Q if they need it
2301 */
2302
2303 if (failed == 2) {
2304 dev = &sh->dev[failed_num[1]];
2305 locked++;
2306 set_bit(R5_LOCKED, &dev->flags);
2307 set_bit(R5_Wantwrite, &dev->flags);
2308 }
2309 if (failed >= 1) {
2310 dev = &sh->dev[failed_num[0]];
2311 locked++;
2312 set_bit(R5_LOCKED, &dev->flags);
2313 set_bit(R5_Wantwrite, &dev->flags);
2314 }
2315
2316 if (update_p) {
2317 dev = &sh->dev[pd_idx];
2318 locked ++;
2319 set_bit(R5_LOCKED, &dev->flags);
2320 set_bit(R5_Wantwrite, &dev->flags);
2321 }
2322 if (update_q) {
2323 dev = &sh->dev[qd_idx];
2324 locked++;
2325 set_bit(R5_LOCKED, &dev->flags);
2326 set_bit(R5_Wantwrite, &dev->flags);
2327 }
2328 clear_bit(STRIPE_DEGRADED, &sh->state);
2329
2330 set_bit(STRIPE_INSYNC, &sh->state);
2331 }
2332 }
2333
2334 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2335 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2336 clear_bit(STRIPE_SYNCING, &sh->state);
2337 }
2338
2339 /* If the failed drives are just a ReadError, then we might need
2340 * to progress the repair/check process
2341 */
2342 if (failed <= 2 && ! conf->mddev->ro)
2343 for (i=0; i<failed;i++) {
2344 dev = &sh->dev[failed_num[i]];
2345 if (test_bit(R5_ReadError, &dev->flags)
2346 && !test_bit(R5_LOCKED, &dev->flags)
2347 && test_bit(R5_UPTODATE, &dev->flags)
2348 ) {
2349 if (!test_bit(R5_ReWrite, &dev->flags)) {
2350 set_bit(R5_Wantwrite, &dev->flags);
2351 set_bit(R5_ReWrite, &dev->flags);
2352 set_bit(R5_LOCKED, &dev->flags);
2353 } else {
2354 /* let's read it back */
2355 set_bit(R5_Wantread, &dev->flags);
2356 set_bit(R5_LOCKED, &dev->flags);
2357 }
2358 }
2359 }
NeilBrownf4168852007-02-28 20:11:53 -08002360
2361 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
2362 /* Need to write out all blocks after computing P&Q */
2363 sh->disks = conf->raid_disks;
2364 sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
2365 conf->raid_disks);
2366 compute_parity6(sh, RECONSTRUCT_WRITE);
2367 for (i = conf->raid_disks ; i-- ; ) {
2368 set_bit(R5_LOCKED, &sh->dev[i].flags);
2369 locked++;
2370 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2371 }
2372 clear_bit(STRIPE_EXPANDING, &sh->state);
2373 } else if (expanded) {
2374 clear_bit(STRIPE_EXPAND_READY, &sh->state);
2375 atomic_dec(&conf->reshape_stripes);
2376 wake_up(&conf->wait_for_overlap);
2377 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2378 }
2379
2380 if (expanding && locked == 0) {
2381 /* We have read all the blocks in this stripe and now we need to
2382 * copy some of them into a target stripe for expand.
2383 */
2384 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2385 for (i = 0; i < sh->disks ; i++)
2386 if (i != pd_idx && i != qd_idx) {
2387 int dd_idx2, pd_idx2, j;
2388 struct stripe_head *sh2;
2389
2390 sector_t bn = compute_blocknr(sh, i);
2391 sector_t s = raid5_compute_sector(
2392 bn, conf->raid_disks,
2393 conf->raid_disks - conf->max_degraded,
2394 &dd_idx2, &pd_idx2, conf);
2395 sh2 = get_active_stripe(conf, s,
2396 conf->raid_disks,
2397 pd_idx2, 1);
2398 if (sh2 == NULL)
2399 /* so for only the early blocks of
2400 * this stripe have been requests.
2401 * When later blocks get requests, we
2402 * will try again
2403 */
2404 continue;
2405 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2406 test_bit(R5_Expanded,
2407 &sh2->dev[dd_idx2].flags)) {
2408 /* must have already done this block */
2409 release_stripe(sh2);
2410 continue;
2411 }
2412 memcpy(page_address(sh2->dev[dd_idx2].page),
2413 page_address(sh->dev[i].page),
2414 STRIPE_SIZE);
2415 set_bit(R5_Expanded, &sh2->dev[dd_idx2].flags);
2416 set_bit(R5_UPTODATE, &sh2->dev[dd_idx2].flags);
2417 for (j = 0 ; j < conf->raid_disks ; j++)
2418 if (j != sh2->pd_idx &&
2419 j != raid6_next_disk(sh2->pd_idx,
2420 sh2->disks) &&
2421 !test_bit(R5_Expanded,
2422 &sh2->dev[j].flags))
2423 break;
2424 if (j == conf->raid_disks) {
2425 set_bit(STRIPE_EXPAND_READY,
2426 &sh2->state);
2427 set_bit(STRIPE_HANDLE, &sh2->state);
2428 }
2429 release_stripe(sh2);
2430 }
2431 }
2432
NeilBrown16a53ec2006-06-26 00:27:38 -07002433 spin_unlock(&sh->lock);
2434
2435 while ((bi=return_bi)) {
2436 int bytes = bi->bi_size;
2437
2438 return_bi = bi->bi_next;
2439 bi->bi_next = NULL;
2440 bi->bi_size = 0;
NeilBrownc2b00852006-12-10 02:20:51 -08002441 bi->bi_end_io(bi, bytes,
2442 test_bit(BIO_UPTODATE, &bi->bi_flags)
2443 ? 0 : -EIO);
NeilBrown16a53ec2006-06-26 00:27:38 -07002444 }
2445 for (i=disks; i-- ;) {
2446 int rw;
2447 struct bio *bi;
2448 mdk_rdev_t *rdev;
2449 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
NeilBrown802ba062006-12-13 00:34:13 -08002450 rw = WRITE;
NeilBrown16a53ec2006-06-26 00:27:38 -07002451 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
NeilBrown802ba062006-12-13 00:34:13 -08002452 rw = READ;
NeilBrown16a53ec2006-06-26 00:27:38 -07002453 else
2454 continue;
2455
2456 bi = &sh->dev[i].req;
2457
2458 bi->bi_rw = rw;
NeilBrown802ba062006-12-13 00:34:13 -08002459 if (rw == WRITE)
NeilBrown16a53ec2006-06-26 00:27:38 -07002460 bi->bi_end_io = raid5_end_write_request;
2461 else
2462 bi->bi_end_io = raid5_end_read_request;
2463
2464 rcu_read_lock();
2465 rdev = rcu_dereference(conf->disks[i].rdev);
2466 if (rdev && test_bit(Faulty, &rdev->flags))
2467 rdev = NULL;
2468 if (rdev)
2469 atomic_inc(&rdev->nr_pending);
2470 rcu_read_unlock();
2471
2472 if (rdev) {
NeilBrownf4168852007-02-28 20:11:53 -08002473 if (syncing || expanding || expanded)
NeilBrown16a53ec2006-06-26 00:27:38 -07002474 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2475
2476 bi->bi_bdev = rdev->bdev;
2477 PRINTK("for %llu schedule op %ld on disc %d\n",
2478 (unsigned long long)sh->sector, bi->bi_rw, i);
2479 atomic_inc(&sh->count);
2480 bi->bi_sector = sh->sector + rdev->data_offset;
2481 bi->bi_flags = 1 << BIO_UPTODATE;
2482 bi->bi_vcnt = 1;
2483 bi->bi_max_vecs = 1;
2484 bi->bi_idx = 0;
2485 bi->bi_io_vec = &sh->dev[i].vec;
2486 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2487 bi->bi_io_vec[0].bv_offset = 0;
2488 bi->bi_size = STRIPE_SIZE;
2489 bi->bi_next = NULL;
2490 if (rw == WRITE &&
2491 test_bit(R5_ReWrite, &sh->dev[i].flags))
2492 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2493 generic_make_request(bi);
2494 } else {
NeilBrown802ba062006-12-13 00:34:13 -08002495 if (rw == WRITE)
NeilBrown16a53ec2006-06-26 00:27:38 -07002496 set_bit(STRIPE_DEGRADED, &sh->state);
2497 PRINTK("skip op %ld on disc %d for sector %llu\n",
2498 bi->bi_rw, i, (unsigned long long)sh->sector);
2499 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2500 set_bit(STRIPE_HANDLE, &sh->state);
2501 }
2502 }
2503}
2504
2505static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2506{
2507 if (sh->raid_conf->level == 6)
2508 handle_stripe6(sh, tmp_page);
2509 else
2510 handle_stripe5(sh);
2511}
2512
2513
2514
Arjan van de Ven858119e2006-01-14 13:20:43 -08002515static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
2517 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2518 while (!list_empty(&conf->delayed_list)) {
2519 struct list_head *l = conf->delayed_list.next;
2520 struct stripe_head *sh;
2521 sh = list_entry(l, struct stripe_head, lru);
2522 list_del_init(l);
2523 clear_bit(STRIPE_DELAYED, &sh->state);
2524 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2525 atomic_inc(&conf->preread_active_stripes);
2526 list_add_tail(&sh->lru, &conf->handle_list);
2527 }
2528 }
2529}
2530
Arjan van de Ven858119e2006-01-14 13:20:43 -08002531static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07002532{
2533 /* device_lock is held */
2534 struct list_head head;
2535 list_add(&head, &conf->bitmap_list);
2536 list_del_init(&conf->bitmap_list);
2537 while (!list_empty(&head)) {
2538 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2539 list_del_init(&sh->lru);
2540 atomic_inc(&sh->count);
2541 __release_stripe(conf, sh);
2542 }
2543}
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545static void unplug_slaves(mddev_t *mddev)
2546{
2547 raid5_conf_t *conf = mddev_to_conf(mddev);
2548 int i;
2549
2550 rcu_read_lock();
2551 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002552 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002553 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2555
2556 atomic_inc(&rdev->nr_pending);
2557 rcu_read_unlock();
2558
2559 if (r_queue->unplug_fn)
2560 r_queue->unplug_fn(r_queue);
2561
2562 rdev_dec_pending(rdev, mddev);
2563 rcu_read_lock();
2564 }
2565 }
2566 rcu_read_unlock();
2567}
2568
2569static void raid5_unplug_device(request_queue_t *q)
2570{
2571 mddev_t *mddev = q->queuedata;
2572 raid5_conf_t *conf = mddev_to_conf(mddev);
2573 unsigned long flags;
2574
2575 spin_lock_irqsave(&conf->device_lock, flags);
2576
NeilBrown72626682005-09-09 16:23:54 -07002577 if (blk_remove_plug(q)) {
2578 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07002580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 md_wakeup_thread(mddev->thread);
2582
2583 spin_unlock_irqrestore(&conf->device_lock, flags);
2584
2585 unplug_slaves(mddev);
2586}
2587
2588static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2589 sector_t *error_sector)
2590{
2591 mddev_t *mddev = q->queuedata;
2592 raid5_conf_t *conf = mddev_to_conf(mddev);
2593 int i, ret = 0;
2594
2595 rcu_read_lock();
2596 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08002597 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08002598 if (rdev && !test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 struct block_device *bdev = rdev->bdev;
2600 request_queue_t *r_queue = bdev_get_queue(bdev);
2601
2602 if (!r_queue->issue_flush_fn)
2603 ret = -EOPNOTSUPP;
2604 else {
2605 atomic_inc(&rdev->nr_pending);
2606 rcu_read_unlock();
2607 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2608 error_sector);
2609 rdev_dec_pending(rdev, mddev);
2610 rcu_read_lock();
2611 }
2612 }
2613 }
2614 rcu_read_unlock();
2615 return ret;
2616}
2617
NeilBrownf022b2f2006-10-03 01:15:56 -07002618static int raid5_congested(void *data, int bits)
2619{
2620 mddev_t *mddev = data;
2621 raid5_conf_t *conf = mddev_to_conf(mddev);
2622
2623 /* No difference between reads and writes. Just check
2624 * how busy the stripe_cache is
2625 */
2626 if (conf->inactive_blocked)
2627 return 1;
2628 if (conf->quiesce)
2629 return 1;
2630 if (list_empty_careful(&conf->inactive_list))
2631 return 1;
2632
2633 return 0;
2634}
2635
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08002636/* We want read requests to align with chunks where possible,
2637 * but write requests don't need to.
2638 */
2639static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
2640{
2641 mddev_t *mddev = q->queuedata;
2642 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2643 int max;
2644 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2645 unsigned int bio_sectors = bio->bi_size >> 9;
2646
NeilBrown802ba062006-12-13 00:34:13 -08002647 if (bio_data_dir(bio) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08002648 return biovec->bv_len; /* always allow writes to be mergeable */
2649
2650 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
2651 if (max < 0) max = 0;
2652 if (max <= biovec->bv_len && bio_sectors == 0)
2653 return biovec->bv_len;
2654 else
2655 return max;
2656}
2657
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002658
2659static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
2660{
2661 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2662 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2663 unsigned int bio_sectors = bio->bi_size >> 9;
2664
2665 return chunk_sectors >=
2666 ((sector & (chunk_sectors - 1)) + bio_sectors);
2667}
2668
2669/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002670 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
2671 * later sampled by raid5d.
2672 */
2673static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
2674{
2675 unsigned long flags;
2676
2677 spin_lock_irqsave(&conf->device_lock, flags);
2678
2679 bi->bi_next = conf->retry_read_aligned_list;
2680 conf->retry_read_aligned_list = bi;
2681
2682 spin_unlock_irqrestore(&conf->device_lock, flags);
2683 md_wakeup_thread(conf->mddev->thread);
2684}
2685
2686
2687static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
2688{
2689 struct bio *bi;
2690
2691 bi = conf->retry_read_aligned;
2692 if (bi) {
2693 conf->retry_read_aligned = NULL;
2694 return bi;
2695 }
2696 bi = conf->retry_read_aligned_list;
2697 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08002698 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002699 bi->bi_next = NULL;
2700 bi->bi_phys_segments = 1; /* biased count of active stripes */
2701 bi->bi_hw_segments = 0; /* count of processed stripes */
2702 }
2703
2704 return bi;
2705}
2706
2707
2708/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002709 * The "raid5_align_endio" should check if the read succeeded and if it
2710 * did, call bio_endio on the original bio (having bio_put the new bio
2711 * first).
2712 * If the read failed..
2713 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002714static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002715{
2716 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002717 mddev_t *mddev;
2718 raid5_conf_t *conf;
2719 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
2720 mdk_rdev_t *rdev;
2721
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002722 if (bi->bi_size)
2723 return 1;
2724 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002725
2726 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
2727 conf = mddev_to_conf(mddev);
2728 rdev = (void*)raid_bi->bi_next;
2729 raid_bi->bi_next = NULL;
2730
2731 rdev_dec_pending(rdev, conf->mddev);
2732
2733 if (!error && uptodate) {
2734 bio_endio(raid_bi, bytes, 0);
2735 if (atomic_dec_and_test(&conf->active_aligned_reads))
2736 wake_up(&conf->wait_for_stripe);
2737 return 0;
2738 }
2739
2740
2741 PRINTK("raid5_align_endio : io error...handing IO for a retry\n");
2742
2743 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002744 return 0;
2745}
2746
Neil Brown387bb172007-02-08 14:20:29 -08002747static int bio_fits_rdev(struct bio *bi)
2748{
2749 request_queue_t *q = bdev_get_queue(bi->bi_bdev);
2750
2751 if ((bi->bi_size>>9) > q->max_sectors)
2752 return 0;
2753 blk_recount_segments(q, bi);
2754 if (bi->bi_phys_segments > q->max_phys_segments ||
2755 bi->bi_hw_segments > q->max_hw_segments)
2756 return 0;
2757
2758 if (q->merge_bvec_fn)
2759 /* it's too hard to apply the merge_bvec_fn at this stage,
2760 * just just give up
2761 */
2762 return 0;
2763
2764 return 1;
2765}
2766
2767
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002768static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
2769{
2770 mddev_t *mddev = q->queuedata;
2771 raid5_conf_t *conf = mddev_to_conf(mddev);
2772 const unsigned int raid_disks = conf->raid_disks;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002773 const unsigned int data_disks = raid_disks - conf->max_degraded;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002774 unsigned int dd_idx, pd_idx;
2775 struct bio* align_bi;
2776 mdk_rdev_t *rdev;
2777
2778 if (!in_chunk_boundary(mddev, raid_bio)) {
NeilBrownc20086d2007-01-26 00:57:14 -08002779 PRINTK("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002780 return 0;
2781 }
2782 /*
2783 * use bio_clone to make a copy of the bio
2784 */
2785 align_bi = bio_clone(raid_bio, GFP_NOIO);
2786 if (!align_bi)
2787 return 0;
2788 /*
2789 * set bi_end_io to a new function, and set bi_private to the
2790 * original bio.
2791 */
2792 align_bi->bi_end_io = raid5_align_endio;
2793 align_bi->bi_private = raid_bio;
2794 /*
2795 * compute position
2796 */
2797 align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
2798 raid_disks,
2799 data_disks,
2800 &dd_idx,
2801 &pd_idx,
2802 conf);
2803
2804 rcu_read_lock();
2805 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
2806 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002807 atomic_inc(&rdev->nr_pending);
2808 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002809 raid_bio->bi_next = (void*)rdev;
2810 align_bi->bi_bdev = rdev->bdev;
2811 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
2812 align_bi->bi_sector += rdev->data_offset;
2813
Neil Brown387bb172007-02-08 14:20:29 -08002814 if (!bio_fits_rdev(align_bi)) {
2815 /* too big in some way */
2816 bio_put(align_bi);
2817 rdev_dec_pending(rdev, mddev);
2818 return 0;
2819 }
2820
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002821 spin_lock_irq(&conf->device_lock);
2822 wait_event_lock_irq(conf->wait_for_stripe,
2823 conf->quiesce == 0,
2824 conf->device_lock, /* nothing */);
2825 atomic_inc(&conf->active_aligned_reads);
2826 spin_unlock_irq(&conf->device_lock);
2827
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002828 generic_make_request(align_bi);
2829 return 1;
2830 } else {
2831 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08002832 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08002833 return 0;
2834 }
2835}
2836
2837
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002838static int make_request(request_queue_t *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 mddev_t *mddev = q->queuedata;
2841 raid5_conf_t *conf = mddev_to_conf(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 unsigned int dd_idx, pd_idx;
2843 sector_t new_sector;
2844 sector_t logical_sector, last_sector;
2845 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01002846 const int rw = bio_data_dir(bi);
NeilBrownf6344752006-03-27 01:18:17 -08002847 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
NeilBrowne5dcdd82005-09-09 16:23:41 -07002849 if (unlikely(bio_barrier(bi))) {
2850 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2851 return 0;
2852 }
2853
NeilBrown3d310eb2005-06-21 17:17:26 -07002854 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07002855
Jens Axboea3623572005-11-01 09:26:16 +01002856 disk_stat_inc(mddev->gendisk, ios[rw]);
2857 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
NeilBrown802ba062006-12-13 00:34:13 -08002859 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08002860 mddev->reshape_position == MaxSector &&
2861 chunk_aligned_read(q,bi))
2862 return 0;
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2865 last_sector = bi->bi_sector + (bi->bi_size>>9);
2866 bi->bi_next = NULL;
2867 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07002868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2870 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002871 int disks, data_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002872
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002873 retry:
NeilBrownb578d552006-03-27 01:18:12 -08002874 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002875 if (likely(conf->expand_progress == MaxSector))
2876 disks = conf->raid_disks;
2877 else {
NeilBrowndf8e7f72006-03-27 01:18:15 -08002878 /* spinlock is needed as expand_progress may be
2879 * 64bit on a 32bit platform, and so it might be
2880 * possible to see a half-updated value
2881 * Ofcourse expand_progress could change after
2882 * the lock is dropped, so once we get a reference
2883 * to the stripe that we think it is, we will have
2884 * to check again.
2885 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002886 spin_lock_irq(&conf->device_lock);
2887 disks = conf->raid_disks;
2888 if (logical_sector >= conf->expand_progress)
2889 disks = conf->previous_raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08002890 else {
2891 if (logical_sector >= conf->expand_lo) {
2892 spin_unlock_irq(&conf->device_lock);
2893 schedule();
2894 goto retry;
2895 }
2896 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002897 spin_unlock_irq(&conf->device_lock);
2898 }
NeilBrown16a53ec2006-06-26 00:27:38 -07002899 data_disks = disks - conf->max_degraded;
2900
2901 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002902 &dd_idx, &pd_idx, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2904 (unsigned long long)new_sector,
2905 (unsigned long long)logical_sector);
2906
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002907 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002909 if (unlikely(conf->expand_progress != MaxSector)) {
2910 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08002911 * stripe, so we must do the range check again.
2912 * Expansion could still move past after this
2913 * test, but as we are holding a reference to
2914 * 'sh', we know that if that happens,
2915 * STRIPE_EXPANDING will get set and the expansion
2916 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002917 */
2918 int must_retry = 0;
2919 spin_lock_irq(&conf->device_lock);
2920 if (logical_sector < conf->expand_progress &&
2921 disks == conf->previous_raid_disks)
2922 /* mismatch, need to try again */
2923 must_retry = 1;
2924 spin_unlock_irq(&conf->device_lock);
2925 if (must_retry) {
2926 release_stripe(sh);
2927 goto retry;
2928 }
2929 }
NeilBrowne464eaf2006-03-27 01:18:14 -08002930 /* FIXME what if we get a false positive because these
2931 * are being updated.
2932 */
2933 if (logical_sector >= mddev->suspend_lo &&
2934 logical_sector < mddev->suspend_hi) {
2935 release_stripe(sh);
2936 schedule();
2937 goto retry;
2938 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002939
2940 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2941 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2942 /* Stripe is busy expanding or
2943 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 * and wait a while
2945 */
2946 raid5_unplug_device(mddev->queue);
2947 release_stripe(sh);
2948 schedule();
2949 goto retry;
2950 }
2951 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown16a53ec2006-06-26 00:27:38 -07002952 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 } else {
2955 /* cannot get stripe for read-ahead, just give-up */
2956 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2957 finish_wait(&conf->wait_for_overlap, &w);
2958 break;
2959 }
2960
2961 }
2962 spin_lock_irq(&conf->device_lock);
NeilBrownf6344752006-03-27 01:18:17 -08002963 remaining = --bi->bi_phys_segments;
2964 spin_unlock_irq(&conf->device_lock);
2965 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 int bytes = bi->bi_size;
2967
NeilBrown16a53ec2006-06-26 00:27:38 -07002968 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 md_write_end(mddev);
2970 bi->bi_size = 0;
NeilBrownc2b00852006-12-10 02:20:51 -08002971 bi->bi_end_io(bi, bytes,
2972 test_bit(BIO_UPTODATE, &bi->bi_flags)
2973 ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return 0;
2976}
2977
NeilBrown52c03292006-06-26 00:27:43 -07002978static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
NeilBrown52c03292006-06-26 00:27:43 -07002980 /* reshaping is quite different to recovery/resync so it is
2981 * handled quite separately ... here.
2982 *
2983 * On each call to sync_request, we gather one chunk worth of
2984 * destination stripes and flag them as expanding.
2985 * Then we find all the source stripes and request reads.
2986 * As the reads complete, handle_stripe will copy the data
2987 * into the destination stripe and release that stripe.
2988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2990 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08002991 int pd_idx;
2992 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08002993 int raid_disks = conf->previous_raid_disks;
2994 int data_disks = raid_disks - conf->max_degraded;
2995 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07002996 int i;
2997 int dd_idx;
2998 sector_t writepos, safepos, gap;
2999
3000 if (sector_nr == 0 &&
3001 conf->expand_progress != 0) {
3002 /* restarting in the middle, skip the initial sectors */
3003 sector_nr = conf->expand_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003004 sector_div(sector_nr, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003005 *skipped = 1;
3006 return sector_nr;
3007 }
3008
3009 /* we update the metadata when there is more than 3Meg
3010 * in the block range (that is rather arbitrary, should
3011 * probably be time based) or when the data about to be
3012 * copied would over-write the source of the data at
3013 * the front of the range.
3014 * i.e. one new_stripe forward from expand_progress new_maps
3015 * to after where expand_lo old_maps to
3016 */
3017 writepos = conf->expand_progress +
NeilBrownf4168852007-02-28 20:11:53 -08003018 conf->chunk_size/512*(new_data_disks);
3019 sector_div(writepos, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003020 safepos = conf->expand_lo;
NeilBrownf4168852007-02-28 20:11:53 -08003021 sector_div(safepos, data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003022 gap = conf->expand_progress - conf->expand_lo;
3023
3024 if (writepos >= safepos ||
NeilBrownf4168852007-02-28 20:11:53 -08003025 gap > (new_data_disks)*3000*2 /*3Meg*/) {
NeilBrown52c03292006-06-26 00:27:43 -07003026 /* Cannot proceed until we've updated the superblock... */
3027 wait_event(conf->wait_for_overlap,
3028 atomic_read(&conf->reshape_stripes)==0);
3029 mddev->reshape_position = conf->expand_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07003030 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003031 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003032 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003033 kthread_should_stop());
3034 spin_lock_irq(&conf->device_lock);
3035 conf->expand_lo = mddev->reshape_position;
3036 spin_unlock_irq(&conf->device_lock);
3037 wake_up(&conf->wait_for_overlap);
3038 }
3039
3040 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
3041 int j;
3042 int skipped = 0;
3043 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
3044 sh = get_active_stripe(conf, sector_nr+i,
3045 conf->raid_disks, pd_idx, 0);
3046 set_bit(STRIPE_EXPANDING, &sh->state);
3047 atomic_inc(&conf->reshape_stripes);
3048 /* If any of this stripe is beyond the end of the old
3049 * array, then we need to zero those blocks
3050 */
3051 for (j=sh->disks; j--;) {
3052 sector_t s;
3053 if (j == sh->pd_idx)
3054 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003055 if (conf->level == 6 &&
3056 j == raid6_next_disk(sh->pd_idx, sh->disks))
3057 continue;
NeilBrown52c03292006-06-26 00:27:43 -07003058 s = compute_blocknr(sh, j);
3059 if (s < (mddev->array_size<<1)) {
3060 skipped = 1;
3061 continue;
3062 }
3063 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3064 set_bit(R5_Expanded, &sh->dev[j].flags);
3065 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3066 }
3067 if (!skipped) {
3068 set_bit(STRIPE_EXPAND_READY, &sh->state);
3069 set_bit(STRIPE_HANDLE, &sh->state);
3070 }
3071 release_stripe(sh);
3072 }
3073 spin_lock_irq(&conf->device_lock);
NeilBrown6d3baf22007-03-05 00:30:44 -08003074 conf->expand_progress = (sector_nr + i) * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07003075 spin_unlock_irq(&conf->device_lock);
3076 /* Ok, those stripe are ready. We can start scheduling
3077 * reads on the source stripes.
3078 * The source stripes are determined by mapping the first and last
3079 * block on the destination stripes.
3080 */
NeilBrown52c03292006-06-26 00:27:43 -07003081 first_sector =
NeilBrownf4168852007-02-28 20:11:53 -08003082 raid5_compute_sector(sector_nr*(new_data_disks),
NeilBrown52c03292006-06-26 00:27:43 -07003083 raid_disks, data_disks,
3084 &dd_idx, &pd_idx, conf);
3085 last_sector =
3086 raid5_compute_sector((sector_nr+conf->chunk_size/512)
NeilBrownf4168852007-02-28 20:11:53 -08003087 *(new_data_disks) -1,
NeilBrown52c03292006-06-26 00:27:43 -07003088 raid_disks, data_disks,
3089 &dd_idx, &pd_idx, conf);
3090 if (last_sector >= (mddev->size<<1))
3091 last_sector = (mddev->size<<1)-1;
3092 while (first_sector <= last_sector) {
NeilBrownf4168852007-02-28 20:11:53 -08003093 pd_idx = stripe_to_pdidx(first_sector, conf,
3094 conf->previous_raid_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003095 sh = get_active_stripe(conf, first_sector,
3096 conf->previous_raid_disks, pd_idx, 0);
3097 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3098 set_bit(STRIPE_HANDLE, &sh->state);
3099 release_stripe(sh);
3100 first_sector += STRIPE_SECTORS;
3101 }
3102 return conf->chunk_size>>9;
3103}
3104
3105/* FIXME go_faster isn't used */
3106static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3107{
3108 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3109 struct stripe_head *sh;
3110 int pd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 int raid_disks = conf->raid_disks;
NeilBrown72626682005-09-09 16:23:54 -07003112 sector_t max_sector = mddev->size << 1;
3113 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003114 int still_degraded = 0;
3115 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
NeilBrown72626682005-09-09 16:23:54 -07003117 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 /* just being told to finish up .. nothing much to do */
3119 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08003120 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3121 end_reshape(conf);
3122 return 0;
3123 }
NeilBrown72626682005-09-09 16:23:54 -07003124
3125 if (mddev->curr_resync < max_sector) /* aborted */
3126 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3127 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003128 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07003129 conf->fullsync = 0;
3130 bitmap_close_sync(mddev->bitmap);
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return 0;
3133 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08003134
NeilBrown52c03292006-06-26 00:27:43 -07003135 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3136 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08003137
NeilBrown16a53ec2006-06-26 00:27:38 -07003138 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 * to resync, then assert that we are finished, because there is
3140 * nothing we can do.
3141 */
NeilBrown3285edf2006-06-26 00:27:55 -07003142 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07003143 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown57afd892005-06-21 17:17:13 -07003144 sector_t rv = (mddev->size << 1) - sector_nr;
3145 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return rv;
3147 }
NeilBrown72626682005-09-09 16:23:54 -07003148 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08003149 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07003150 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3151 /* we can skip this block, and probably more */
3152 sync_blocks /= STRIPE_SECTORS;
3153 *skipped = 1;
3154 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
NeilBrownccfcc3c2006-03-27 01:18:09 -08003157 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003158 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 if (sh == NULL) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003160 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07003162 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08003164 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003166 /* Need to check if array will still be degraded after recovery/resync
3167 * We don't need to check the 'failed' flag as when that gets set,
3168 * recovery aborts.
3169 */
3170 for (i=0; i<mddev->raid_disks; i++)
3171 if (conf->disks[i].rdev == NULL)
3172 still_degraded = 1;
3173
3174 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3175
3176 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 set_bit(STRIPE_SYNCING, &sh->state);
3178 clear_bit(STRIPE_INSYNC, &sh->state);
3179 spin_unlock(&sh->lock);
3180
NeilBrown16a53ec2006-06-26 00:27:38 -07003181 handle_stripe(sh, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 release_stripe(sh);
3183
3184 return STRIPE_SECTORS;
3185}
3186
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003187static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3188{
3189 /* We may not be able to submit a whole bio at once as there
3190 * may not be enough stripe_heads available.
3191 * We cannot pre-allocate enough stripe_heads as we may need
3192 * more than exist in the cache (if we allow ever large chunks).
3193 * So we do one stripe head at a time and record in
3194 * ->bi_hw_segments how many have been done.
3195 *
3196 * We *know* that this entire raid_bio is in one chunk, so
3197 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3198 */
3199 struct stripe_head *sh;
3200 int dd_idx, pd_idx;
3201 sector_t sector, logical_sector, last_sector;
3202 int scnt = 0;
3203 int remaining;
3204 int handled = 0;
3205
3206 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3207 sector = raid5_compute_sector( logical_sector,
3208 conf->raid_disks,
3209 conf->raid_disks - conf->max_degraded,
3210 &dd_idx,
3211 &pd_idx,
3212 conf);
3213 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3214
3215 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08003216 logical_sector += STRIPE_SECTORS,
3217 sector += STRIPE_SECTORS,
3218 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003219
3220 if (scnt < raid_bio->bi_hw_segments)
3221 /* already done this stripe */
3222 continue;
3223
3224 sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
3225
3226 if (!sh) {
3227 /* failed to get a stripe - must wait */
3228 raid_bio->bi_hw_segments = scnt;
3229 conf->retry_read_aligned = raid_bio;
3230 return handled;
3231 }
3232
3233 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08003234 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
3235 release_stripe(sh);
3236 raid_bio->bi_hw_segments = scnt;
3237 conf->retry_read_aligned = raid_bio;
3238 return handled;
3239 }
3240
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003241 handle_stripe(sh, NULL);
3242 release_stripe(sh);
3243 handled++;
3244 }
3245 spin_lock_irq(&conf->device_lock);
3246 remaining = --raid_bio->bi_phys_segments;
3247 spin_unlock_irq(&conf->device_lock);
3248 if (remaining == 0) {
3249 int bytes = raid_bio->bi_size;
3250
3251 raid_bio->bi_size = 0;
NeilBrownc2b00852006-12-10 02:20:51 -08003252 raid_bio->bi_end_io(raid_bio, bytes,
3253 test_bit(BIO_UPTODATE, &raid_bio->bi_flags)
3254 ? 0 : -EIO);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003255 }
3256 if (atomic_dec_and_test(&conf->active_aligned_reads))
3257 wake_up(&conf->wait_for_stripe);
3258 return handled;
3259}
3260
3261
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263/*
3264 * This is our raid5 kernel thread.
3265 *
3266 * We scan the hash table for stripes which can be handled now.
3267 * During the scan, completed stripes are saved for us by the interrupt
3268 * handler, so that they will not have to wait for our next wakeup.
3269 */
3270static void raid5d (mddev_t *mddev)
3271{
3272 struct stripe_head *sh;
3273 raid5_conf_t *conf = mddev_to_conf(mddev);
3274 int handled;
3275
3276 PRINTK("+++ raid5d active\n");
3277
3278 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 handled = 0;
3281 spin_lock_irq(&conf->device_lock);
3282 while (1) {
3283 struct list_head *first;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003284 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
NeilBrownae3c20c2006-07-10 04:44:17 -07003286 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07003287 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08003288 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003289 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08003290 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07003291 conf->seq_write = seq;
3292 activate_bit_delay(conf);
3293 }
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (list_empty(&conf->handle_list) &&
3296 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
3297 !blk_queue_plugged(mddev->queue) &&
3298 !list_empty(&conf->delayed_list))
3299 raid5_activate_delayed(conf);
3300
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003301 while ((bio = remove_bio_from_retry(conf))) {
3302 int ok;
3303 spin_unlock_irq(&conf->device_lock);
3304 ok = retry_aligned_read(conf, bio);
3305 spin_lock_irq(&conf->device_lock);
3306 if (!ok)
3307 break;
3308 handled++;
3309 }
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 if (list_empty(&conf->handle_list))
3312 break;
3313
3314 first = conf->handle_list.next;
3315 sh = list_entry(first, struct stripe_head, lru);
3316
3317 list_del_init(first);
3318 atomic_inc(&sh->count);
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003319 BUG_ON(atomic_read(&sh->count)!= 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 spin_unlock_irq(&conf->device_lock);
3321
3322 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003323 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 release_stripe(sh);
3325
3326 spin_lock_irq(&conf->device_lock);
3327 }
3328 PRINTK("%d stripes handled\n", handled);
3329
3330 spin_unlock_irq(&conf->device_lock);
3331
3332 unplug_slaves(mddev);
3333
3334 PRINTK("--- raid5d inactive\n");
3335}
3336
NeilBrown3f294f42005-11-08 21:39:25 -08003337static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003338raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003339{
NeilBrown007583c2005-11-08 21:39:30 -08003340 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003341 if (conf)
3342 return sprintf(page, "%d\n", conf->max_nr_stripes);
3343 else
3344 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003345}
3346
3347static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08003348raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08003349{
NeilBrown007583c2005-11-08 21:39:30 -08003350 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08003351 char *end;
3352 int new;
3353 if (len >= PAGE_SIZE)
3354 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08003355 if (!conf)
3356 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08003357
3358 new = simple_strtoul(page, &end, 10);
3359 if (!*page || (*end && *end != '\n') )
3360 return -EINVAL;
3361 if (new <= 16 || new > 32768)
3362 return -EINVAL;
3363 while (new < conf->max_nr_stripes) {
3364 if (drop_one_stripe(conf))
3365 conf->max_nr_stripes--;
3366 else
3367 break;
3368 }
NeilBrown2a2275d2007-01-26 00:57:11 -08003369 md_allow_write(mddev);
NeilBrown3f294f42005-11-08 21:39:25 -08003370 while (new > conf->max_nr_stripes) {
3371 if (grow_one_stripe(conf))
3372 conf->max_nr_stripes++;
3373 else break;
3374 }
3375 return len;
3376}
NeilBrown007583c2005-11-08 21:39:30 -08003377
NeilBrown96de1e62005-11-08 21:39:39 -08003378static struct md_sysfs_entry
3379raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3380 raid5_show_stripe_cache_size,
3381 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08003382
3383static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003384stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08003385{
NeilBrown007583c2005-11-08 21:39:30 -08003386 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08003387 if (conf)
3388 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3389 else
3390 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08003391}
3392
NeilBrown96de1e62005-11-08 21:39:39 -08003393static struct md_sysfs_entry
3394raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08003395
NeilBrown007583c2005-11-08 21:39:30 -08003396static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08003397 &raid5_stripecache_size.attr,
3398 &raid5_stripecache_active.attr,
3399 NULL,
3400};
NeilBrown007583c2005-11-08 21:39:30 -08003401static struct attribute_group raid5_attrs_group = {
3402 .name = NULL,
3403 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08003404};
3405
NeilBrown72626682005-09-09 16:23:54 -07003406static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 raid5_conf_t *conf;
3409 int raid_disk, memory;
3410 mdk_rdev_t *rdev;
3411 struct disk_info *disk;
3412 struct list_head *tmp;
NeilBrown02c2de82006-10-03 01:15:47 -07003413 int working_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
NeilBrown16a53ec2006-06-26 00:27:38 -07003415 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3416 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown14f8d262006-01-06 00:20:14 -08003417 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return -EIO;
3419 }
3420
NeilBrownf6705572006-03-27 01:18:11 -08003421 if (mddev->reshape_position != MaxSector) {
3422 /* Check that we can continue the reshape.
3423 * Currently only disks can change, it must
3424 * increase, and we must be past the point where
3425 * a stripe over-writes itself
3426 */
3427 sector_t here_new, here_old;
3428 int old_disks;
NeilBrownf4168852007-02-28 20:11:53 -08003429 int max_degraded = (mddev->level == 5 ? 1 : 2);
NeilBrownf6705572006-03-27 01:18:11 -08003430
3431 if (mddev->new_level != mddev->level ||
3432 mddev->new_layout != mddev->layout ||
3433 mddev->new_chunk != mddev->chunk_size) {
NeilBrownf4168852007-02-28 20:11:53 -08003434 printk(KERN_ERR "raid5: %s: unsupported reshape "
3435 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08003436 mdname(mddev));
3437 return -EINVAL;
3438 }
3439 if (mddev->delta_disks <= 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003440 printk(KERN_ERR "raid5: %s: unsupported reshape "
3441 "(reduce disks) required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08003442 mdname(mddev));
3443 return -EINVAL;
3444 }
3445 old_disks = mddev->raid_disks - mddev->delta_disks;
3446 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08003447 * further up in new geometry must map after here in old
3448 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08003449 */
3450 here_new = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08003451 if (sector_div(here_new, (mddev->chunk_size>>9)*
3452 (mddev->raid_disks - max_degraded))) {
3453 printk(KERN_ERR "raid5: reshape_position not "
3454 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08003455 return -EINVAL;
3456 }
3457 /* here_new is the stripe we will write to */
3458 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08003459 sector_div(here_old, (mddev->chunk_size>>9)*
3460 (old_disks-max_degraded));
3461 /* here_old is the first stripe that we might need to read
3462 * from */
NeilBrownf6705572006-03-27 01:18:11 -08003463 if (here_new >= here_old) {
3464 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08003465 printk(KERN_ERR "raid5: reshape_position too early for "
3466 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08003467 return -EINVAL;
3468 }
3469 printk(KERN_INFO "raid5: reshape will continue\n");
3470 /* OK, we should be able to continue; */
3471 }
3472
3473
NeilBrownb55e6bf2006-03-27 01:18:06 -08003474 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 if ((conf = mddev->private) == NULL)
3476 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08003477 if (mddev->reshape_position == MaxSector) {
3478 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3479 } else {
3480 conf->raid_disks = mddev->raid_disks;
3481 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3482 }
3483
3484 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
NeilBrownb55e6bf2006-03-27 01:18:06 -08003485 GFP_KERNEL);
3486 if (!conf->disks)
3487 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08003488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 conf->mddev = mddev;
3490
NeilBrownfccddba2006-01-06 00:20:33 -08003491 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
NeilBrown16a53ec2006-06-26 00:27:38 -07003494 if (mddev->level == 6) {
3495 conf->spare_page = alloc_page(GFP_KERNEL);
3496 if (!conf->spare_page)
3497 goto abort;
3498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 spin_lock_init(&conf->device_lock);
3500 init_waitqueue_head(&conf->wait_for_stripe);
3501 init_waitqueue_head(&conf->wait_for_overlap);
3502 INIT_LIST_HEAD(&conf->handle_list);
3503 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07003504 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 INIT_LIST_HEAD(&conf->inactive_list);
3506 atomic_set(&conf->active_stripes, 0);
3507 atomic_set(&conf->preread_active_stripes, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003508 atomic_set(&conf->active_aligned_reads, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3511
3512 ITERATE_RDEV(mddev,rdev,tmp) {
3513 raid_disk = rdev->raid_disk;
NeilBrownf6705572006-03-27 01:18:11 -08003514 if (raid_disk >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 || raid_disk < 0)
3516 continue;
3517 disk = conf->disks + raid_disk;
3518
3519 disk->rdev = rdev;
3520
NeilBrownb2d444d2005-11-08 21:39:31 -08003521 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 char b[BDEVNAME_SIZE];
3523 printk(KERN_INFO "raid5: device %s operational as raid"
3524 " disk %d\n", bdevname(rdev->bdev,b),
3525 raid_disk);
NeilBrown02c2de82006-10-03 01:15:47 -07003526 working_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
3528 }
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003531 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 */
NeilBrown02c2de82006-10-03 01:15:47 -07003533 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 conf->mddev = mddev;
3535 conf->chunk_size = mddev->chunk_size;
3536 conf->level = mddev->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003537 if (conf->level == 6)
3538 conf->max_degraded = 2;
3539 else
3540 conf->max_degraded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 conf->algorithm = mddev->layout;
3542 conf->max_nr_stripes = NR_STRIPES;
NeilBrownf6705572006-03-27 01:18:11 -08003543 conf->expand_progress = mddev->reshape_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 /* device size must be a multiple of chunk size */
3546 mddev->size &= ~(mddev->chunk_size/1024 -1);
NeilBrownb1581562005-07-31 22:34:50 -07003547 mddev->resync_max_sectors = mddev->size << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
NeilBrown16a53ec2006-06-26 00:27:38 -07003549 if (conf->level == 6 && conf->raid_disks < 4) {
3550 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3551 mdname(mddev), conf->raid_disks);
3552 goto abort;
3553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 if (!conf->chunk_size || conf->chunk_size % 4) {
3555 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3556 conf->chunk_size, mdname(mddev));
3557 goto abort;
3558 }
3559 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3560 printk(KERN_ERR
3561 "raid5: unsupported parity algorithm %d for %s\n",
3562 conf->algorithm, mdname(mddev));
3563 goto abort;
3564 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003565 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 printk(KERN_ERR "raid5: not enough operational devices for %s"
3567 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07003568 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 goto abort;
3570 }
3571
NeilBrown16a53ec2006-06-26 00:27:38 -07003572 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003574 if (mddev->ok_start_degraded)
3575 printk(KERN_WARNING
3576 "raid5: starting dirty degraded array: %s"
3577 "- data corruption possible.\n",
3578 mdname(mddev));
3579 else {
3580 printk(KERN_ERR
3581 "raid5: cannot start dirty degraded array for %s\n",
3582 mdname(mddev));
3583 goto abort;
3584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586
3587 {
3588 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3589 if (!mddev->thread) {
3590 printk(KERN_ERR
3591 "raid5: couldn't allocate thread for %s\n",
3592 mdname(mddev));
3593 goto abort;
3594 }
3595 }
NeilBrown50368052005-12-12 02:39:17 -08003596 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3598 if (grow_stripes(conf, conf->max_nr_stripes)) {
3599 printk(KERN_ERR
3600 "raid5: couldn't allocate %dkB for buffers\n", memory);
3601 shrink_stripes(conf);
3602 md_unregister_thread(mddev->thread);
3603 goto abort;
3604 } else
3605 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3606 memory, mdname(mddev));
3607
3608 if (mddev->degraded == 0)
3609 printk("raid5: raid level %d set %s active with %d out of %d"
3610 " devices, algorithm %d\n", conf->level, mdname(mddev),
3611 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3612 conf->algorithm);
3613 else
3614 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3615 " out of %d devices, algorithm %d\n", conf->level,
3616 mdname(mddev), mddev->raid_disks - mddev->degraded,
3617 mddev->raid_disks, conf->algorithm);
3618
3619 print_raid5_conf(conf);
3620
NeilBrownf6705572006-03-27 01:18:11 -08003621 if (conf->expand_progress != MaxSector) {
3622 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08003623 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08003624 atomic_set(&conf->reshape_stripes, 0);
3625 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3626 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3627 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3628 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3629 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3630 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08003631 }
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07003634 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 */
3636 {
NeilBrown16a53ec2006-06-26 00:27:38 -07003637 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3638 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07003639 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3641 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3642 }
3643
3644 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08003645 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
3646 printk(KERN_WARNING
3647 "raid5: failed to create sysfs attributes for %s\n",
3648 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07003649
3650 mddev->queue->unplug_fn = raid5_unplug_device;
3651 mddev->queue->issue_flush_fn = raid5_issue_flush;
NeilBrownf022b2f2006-10-03 01:15:56 -07003652 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08003653 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07003654
NeilBrown16a53ec2006-06-26 00:27:38 -07003655 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3656 conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07003657
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003658 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
3659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return 0;
3661abort:
3662 if (conf) {
3663 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07003664 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003665 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08003666 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 kfree(conf);
3668 }
3669 mddev->private = NULL;
3670 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3671 return -EIO;
3672}
3673
3674
3675
NeilBrown3f294f42005-11-08 21:39:25 -08003676static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
3678 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3679
3680 md_unregister_thread(mddev->thread);
3681 mddev->thread = NULL;
3682 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08003683 kfree(conf->stripe_hashtbl);
NeilBrown041ae522007-03-26 21:32:14 -08003684 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08003686 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08003687 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08003688 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 mddev->private = NULL;
3690 return 0;
3691}
3692
3693#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003694static void print_sh (struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
3696 int i;
3697
NeilBrown16a53ec2006-06-26 00:27:38 -07003698 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3699 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3700 seq_printf(seq, "sh %llu, count %d.\n",
3701 (unsigned long long)sh->sector, atomic_read(&sh->count));
3702 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003703 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003704 seq_printf(seq, "(cache%d: %p %ld) ",
3705 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003707 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
NeilBrown16a53ec2006-06-26 00:27:38 -07003710static void printall (struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08003713 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 int i;
3715
3716 spin_lock_irq(&conf->device_lock);
3717 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08003718 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 if (sh->raid_conf != conf)
3720 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07003721 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 }
3723 }
3724 spin_unlock_irq(&conf->device_lock);
3725}
3726#endif
3727
3728static void status (struct seq_file *seq, mddev_t *mddev)
3729{
3730 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3731 int i;
3732
3733 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07003734 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 for (i = 0; i < conf->raid_disks; i++)
3736 seq_printf (seq, "%s",
3737 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08003738 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 seq_printf (seq, "]");
3740#if RAID5_DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07003741 seq_printf (seq, "\n");
3742 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743#endif
3744}
3745
3746static void print_raid5_conf (raid5_conf_t *conf)
3747{
3748 int i;
3749 struct disk_info *tmp;
3750
3751 printk("RAID5 conf printout:\n");
3752 if (!conf) {
3753 printk("(conf==NULL)\n");
3754 return;
3755 }
NeilBrown02c2de82006-10-03 01:15:47 -07003756 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
3757 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
3759 for (i = 0; i < conf->raid_disks; i++) {
3760 char b[BDEVNAME_SIZE];
3761 tmp = conf->disks + i;
3762 if (tmp->rdev)
3763 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08003764 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 bdevname(tmp->rdev->bdev,b));
3766 }
3767}
3768
3769static int raid5_spare_active(mddev_t *mddev)
3770{
3771 int i;
3772 raid5_conf_t *conf = mddev->private;
3773 struct disk_info *tmp;
3774
3775 for (i = 0; i < conf->raid_disks; i++) {
3776 tmp = conf->disks + i;
3777 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08003778 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07003779 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
3780 unsigned long flags;
3781 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07003783 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785 }
3786 print_raid5_conf(conf);
3787 return 0;
3788}
3789
3790static int raid5_remove_disk(mddev_t *mddev, int number)
3791{
3792 raid5_conf_t *conf = mddev->private;
3793 int err = 0;
3794 mdk_rdev_t *rdev;
3795 struct disk_info *p = conf->disks + number;
3796
3797 print_raid5_conf(conf);
3798 rdev = p->rdev;
3799 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003800 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 atomic_read(&rdev->nr_pending)) {
3802 err = -EBUSY;
3803 goto abort;
3804 }
3805 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003806 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 if (atomic_read(&rdev->nr_pending)) {
3808 /* lost the race, try later */
3809 err = -EBUSY;
3810 p->rdev = rdev;
3811 }
3812 }
3813abort:
3814
3815 print_raid5_conf(conf);
3816 return err;
3817}
3818
3819static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3820{
3821 raid5_conf_t *conf = mddev->private;
3822 int found = 0;
3823 int disk;
3824 struct disk_info *p;
3825
NeilBrown16a53ec2006-06-26 00:27:38 -07003826 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 /* no point adding a device */
3828 return 0;
3829
3830 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07003831 * find the disk ... but prefer rdev->saved_raid_disk
3832 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003834 if (rdev->saved_raid_disk >= 0 &&
3835 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3836 disk = rdev->saved_raid_disk;
3837 else
3838 disk = 0;
3839 for ( ; disk < conf->raid_disks; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003841 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 rdev->raid_disk = disk;
3843 found = 1;
NeilBrown72626682005-09-09 16:23:54 -07003844 if (rdev->saved_raid_disk != disk)
3845 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08003846 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 break;
3848 }
3849 print_raid5_conf(conf);
3850 return found;
3851}
3852
3853static int raid5_resize(mddev_t *mddev, sector_t sectors)
3854{
3855 /* no resync is happening, and there is enough space
3856 * on all devices, so we can resize.
3857 * We need to make sure resync covers any new space.
3858 * If the array is shrinking we should possibly wait until
3859 * any io in the removed space completes, but it hardly seems
3860 * worth it.
3861 */
NeilBrown16a53ec2006-06-26 00:27:38 -07003862 raid5_conf_t *conf = mddev_to_conf(mddev);
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003865 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 set_capacity(mddev->gendisk, mddev->array_size << 1);
Linus Torvalds44ce6292007-05-09 18:51:36 -07003867 mddev->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3869 mddev->recovery_cp = mddev->size << 1;
3870 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3871 }
3872 mddev->size = sectors /2;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07003873 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 return 0;
3875}
3876
NeilBrown29269552006-03-27 01:18:10 -08003877#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08003878static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08003879{
3880 raid5_conf_t *conf = mddev_to_conf(mddev);
3881 int err;
NeilBrown29269552006-03-27 01:18:10 -08003882
NeilBrown63c70c42006-03-27 01:18:13 -08003883 if (mddev->delta_disks < 0 ||
3884 mddev->new_level != mddev->level)
3885 return -EINVAL; /* Cannot shrink array or change level yet */
3886 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08003887 return 0; /* nothing to do */
3888
3889 /* Can only proceed if there are plenty of stripe_heads.
3890 * We need a minimum of one full stripe,, and for sensible progress
3891 * it is best to have about 4 times that.
3892 * If we require 4 times, then the default 256 4K stripe_heads will
3893 * allow for chunk sizes up to 256K, which is probably OK.
3894 * If the chunk size is greater, user-space should request more
3895 * stripe_heads first.
3896 */
NeilBrown63c70c42006-03-27 01:18:13 -08003897 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3898 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08003899 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3900 (mddev->chunk_size / STRIPE_SIZE)*4);
3901 return -ENOSPC;
3902 }
3903
NeilBrown63c70c42006-03-27 01:18:13 -08003904 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3905 if (err)
3906 return err;
3907
NeilBrownb4c4c7b2007-02-28 20:11:48 -08003908 if (mddev->degraded > conf->max_degraded)
3909 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08003910 /* looks like we might be able to manage this */
3911 return 0;
3912}
3913
3914static int raid5_start_reshape(mddev_t *mddev)
3915{
3916 raid5_conf_t *conf = mddev_to_conf(mddev);
3917 mdk_rdev_t *rdev;
3918 struct list_head *rtmp;
3919 int spares = 0;
3920 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07003921 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08003922
NeilBrownf4168852007-02-28 20:11:53 -08003923 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08003924 return -EBUSY;
3925
NeilBrown29269552006-03-27 01:18:10 -08003926 ITERATE_RDEV(mddev, rdev, rtmp)
3927 if (rdev->raid_disk < 0 &&
3928 !test_bit(Faulty, &rdev->flags))
3929 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08003930
NeilBrownf4168852007-02-28 20:11:53 -08003931 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08003932 /* Not enough devices even to make a degraded array
3933 * of that size
3934 */
3935 return -EINVAL;
3936
NeilBrownf6705572006-03-27 01:18:11 -08003937 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08003938 spin_lock_irq(&conf->device_lock);
3939 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08003940 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08003941 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003942 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08003943 spin_unlock_irq(&conf->device_lock);
3944
3945 /* Add some new drives, as many as will fit.
3946 * We know there are enough to make the newly sized array work.
3947 */
3948 ITERATE_RDEV(mddev, rdev, rtmp)
3949 if (rdev->raid_disk < 0 &&
3950 !test_bit(Faulty, &rdev->flags)) {
3951 if (raid5_add_disk(mddev, rdev)) {
3952 char nm[20];
3953 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08003954 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003955 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08003956 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003957 if (sysfs_create_link(&mddev->kobj,
3958 &rdev->kobj, nm))
3959 printk(KERN_WARNING
3960 "raid5: failed to create "
3961 " link %s for %s\n",
3962 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08003963 } else
3964 break;
3965 }
3966
NeilBrownc04be0a2006-10-03 01:15:53 -07003967 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003968 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
NeilBrownc04be0a2006-10-03 01:15:53 -07003969 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08003970 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08003971 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07003972 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08003973
NeilBrown29269552006-03-27 01:18:10 -08003974 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3975 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3976 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3977 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3978 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3979 "%s_reshape");
3980 if (!mddev->sync_thread) {
3981 mddev->recovery = 0;
3982 spin_lock_irq(&conf->device_lock);
3983 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3984 conf->expand_progress = MaxSector;
3985 spin_unlock_irq(&conf->device_lock);
3986 return -EAGAIN;
3987 }
3988 md_wakeup_thread(mddev->sync_thread);
3989 md_new_event(mddev);
3990 return 0;
3991}
3992#endif
3993
3994static void end_reshape(raid5_conf_t *conf)
3995{
3996 struct block_device *bdev;
3997
NeilBrownf6705572006-03-27 01:18:11 -08003998 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrownf4168852007-02-28 20:11:53 -08003999 conf->mddev->array_size = conf->mddev->size *
4000 (conf->raid_disks - conf->max_degraded);
NeilBrownf6705572006-03-27 01:18:11 -08004001 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004002 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08004003
NeilBrownf6705572006-03-27 01:18:11 -08004004 bdev = bdget_disk(conf->mddev->gendisk, 0);
4005 if (bdev) {
4006 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown0692c6b2006-11-08 17:44:48 -08004007 i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
NeilBrownf6705572006-03-27 01:18:11 -08004008 mutex_unlock(&bdev->bd_inode->i_mutex);
4009 bdput(bdev);
4010 }
4011 spin_lock_irq(&conf->device_lock);
4012 conf->expand_progress = MaxSector;
4013 spin_unlock_irq(&conf->device_lock);
4014 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07004015
4016 /* read-ahead size must cover two whole stripes, which is
4017 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4018 */
4019 {
4020 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4021 int stripe = data_disks *
4022 (conf->mddev->chunk_size / PAGE_SIZE);
4023 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4024 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4025 }
NeilBrown29269552006-03-27 01:18:10 -08004026 }
NeilBrown29269552006-03-27 01:18:10 -08004027}
4028
NeilBrown72626682005-09-09 16:23:54 -07004029static void raid5_quiesce(mddev_t *mddev, int state)
4030{
4031 raid5_conf_t *conf = mddev_to_conf(mddev);
4032
4033 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08004034 case 2: /* resume for a suspend */
4035 wake_up(&conf->wait_for_overlap);
4036 break;
4037
NeilBrown72626682005-09-09 16:23:54 -07004038 case 1: /* stop all writes */
4039 spin_lock_irq(&conf->device_lock);
4040 conf->quiesce = 1;
4041 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004042 atomic_read(&conf->active_stripes) == 0 &&
4043 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07004044 conf->device_lock, /* nothing */);
4045 spin_unlock_irq(&conf->device_lock);
4046 break;
4047
4048 case 0: /* re-enable writes */
4049 spin_lock_irq(&conf->device_lock);
4050 conf->quiesce = 0;
4051 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08004052 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07004053 spin_unlock_irq(&conf->device_lock);
4054 break;
4055 }
NeilBrown72626682005-09-09 16:23:54 -07004056}
NeilBrownb15c2e52006-01-06 00:20:16 -08004057
NeilBrown16a53ec2006-06-26 00:27:38 -07004058static struct mdk_personality raid6_personality =
4059{
4060 .name = "raid6",
4061 .level = 6,
4062 .owner = THIS_MODULE,
4063 .make_request = make_request,
4064 .run = run,
4065 .stop = stop,
4066 .status = status,
4067 .error_handler = error,
4068 .hot_add_disk = raid5_add_disk,
4069 .hot_remove_disk= raid5_remove_disk,
4070 .spare_active = raid5_spare_active,
4071 .sync_request = sync_request,
4072 .resize = raid5_resize,
NeilBrownf4168852007-02-28 20:11:53 -08004073#ifdef CONFIG_MD_RAID5_RESHAPE
4074 .check_reshape = raid5_check_reshape,
4075 .start_reshape = raid5_start_reshape,
4076#endif
NeilBrown16a53ec2006-06-26 00:27:38 -07004077 .quiesce = raid5_quiesce,
4078};
NeilBrown2604b702006-01-06 00:20:36 -08004079static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
4081 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08004082 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 .owner = THIS_MODULE,
4084 .make_request = make_request,
4085 .run = run,
4086 .stop = stop,
4087 .status = status,
4088 .error_handler = error,
4089 .hot_add_disk = raid5_add_disk,
4090 .hot_remove_disk= raid5_remove_disk,
4091 .spare_active = raid5_spare_active,
4092 .sync_request = sync_request,
4093 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08004094#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004095 .check_reshape = raid5_check_reshape,
4096 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08004097#endif
NeilBrown72626682005-09-09 16:23:54 -07004098 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099};
4100
NeilBrown2604b702006-01-06 00:20:36 -08004101static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
NeilBrown2604b702006-01-06 00:20:36 -08004103 .name = "raid4",
4104 .level = 4,
4105 .owner = THIS_MODULE,
4106 .make_request = make_request,
4107 .run = run,
4108 .stop = stop,
4109 .status = status,
4110 .error_handler = error,
4111 .hot_add_disk = raid5_add_disk,
4112 .hot_remove_disk= raid5_remove_disk,
4113 .spare_active = raid5_spare_active,
4114 .sync_request = sync_request,
4115 .resize = raid5_resize,
NeilBrown3d378902007-03-26 21:32:13 -08004116#ifdef CONFIG_MD_RAID5_RESHAPE
4117 .check_reshape = raid5_check_reshape,
4118 .start_reshape = raid5_start_reshape,
4119#endif
NeilBrown2604b702006-01-06 00:20:36 -08004120 .quiesce = raid5_quiesce,
4121};
4122
4123static int __init raid5_init(void)
4124{
NeilBrown16a53ec2006-06-26 00:27:38 -07004125 int e;
4126
4127 e = raid6_select_algo();
4128 if ( e )
4129 return e;
4130 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004131 register_md_personality(&raid5_personality);
4132 register_md_personality(&raid4_personality);
4133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
NeilBrown2604b702006-01-06 00:20:36 -08004136static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
NeilBrown16a53ec2006-06-26 00:27:38 -07004138 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004139 unregister_md_personality(&raid5_personality);
4140 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
4143module_init(raid5_init);
4144module_exit(raid5_exit);
4145MODULE_LICENSE("GPL");
4146MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004147MODULE_ALIAS("md-raid5");
4148MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08004149MODULE_ALIAS("md-level-5");
4150MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07004151MODULE_ALIAS("md-personality-8"); /* RAID6 */
4152MODULE_ALIAS("md-raid6");
4153MODULE_ALIAS("md-level-6");
4154
4155/* This used to be two separate modules, they were: */
4156MODULE_ALIAS("raid5");
4157MODULE_ALIAS("raid6");