blob: c1d94ed9718bb73043702ab070ff0bbd852eb5c7 [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
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williams91c00922007-01-02 13:52:30 -070048#include <linux/async_tx.h>
NeilBrownbff61972009-03-31 14:33:13 +110049#include <linux/seq_file.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110051#include "raid5.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110052#include "raid6.h"
53#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * Stripe cache
57 */
58
59#define NR_STRIPES 256
60#define STRIPE_SIZE PAGE_SIZE
61#define STRIPE_SHIFT (PAGE_SHIFT - 9)
62#define STRIPE_SECTORS (STRIPE_SIZE>>9)
63#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070064#define BYPASS_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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define RAID5_PARANOIA 1
84#if RAID5_PARANOIA && defined(CONFIG_SMP)
85# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
86#else
87# define CHECK_DEVLOCK()
88#endif
89
Dan Williams45b42332007-07-09 11:56:43 -070090#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define inline
92#define __inline__
93#endif
94
Bernd Schubert6be9d492008-05-23 13:04:34 -070095#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
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
Jens Axboe960e7392008-08-15 10:41:18 +0200102/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200103 * We maintain a biased count of active stripes in the bottom 16 bits of
104 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200105 */
106static inline int raid5_bi_phys_segments(struct bio *bio)
107{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200108 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200109}
110
111static inline int raid5_bi_hw_segments(struct bio *bio)
112{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200113 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200114}
115
116static inline int raid5_dec_bi_phys_segments(struct bio *bio)
117{
118 --bio->bi_phys_segments;
119 return raid5_bi_phys_segments(bio);
120}
121
122static inline int raid5_dec_bi_hw_segments(struct bio *bio)
123{
124 unsigned short val = raid5_bi_hw_segments(bio);
125
126 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200127 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200128 return val;
129}
130
131static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
132{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200133 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200134}
135
NeilBrownd0dabf72009-03-31 14:39:38 +1100136/* Find first data disk in a raid6 stripe */
137static inline int raid6_d0(struct stripe_head *sh)
138{
139 if (sh->qd_idx == sh->disks - 1)
140 return 0;
141 else
142 return sh->qd_idx + 1;
143}
NeilBrown16a53ec2006-06-26 00:27:38 -0700144static inline int raid6_next_disk(int disk, int raid_disks)
145{
146 disk++;
147 return (disk < raid_disks) ? disk : 0;
148}
Dan Williamsa4456852007-07-09 11:56:43 -0700149
NeilBrownd0dabf72009-03-31 14:39:38 +1100150/* When walking through the disks in a raid5, starting at raid6_d0,
151 * We need to map each disk to a 'slot', where the data disks are slot
152 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
153 * is raid_disks-1. This help does that mapping.
154 */
155static int raid6_idx_to_slot(int idx, struct stripe_head *sh, int *count)
156{
157 int slot;
158 if (idx == sh->pd_idx)
159 return sh->disks - 2;
160 if (idx == sh->qd_idx)
161 return sh->disks - 1;
162 slot = (*count)++;
163 return slot;
164}
165
Dan Williamsa4456852007-07-09 11:56:43 -0700166static void return_io(struct bio *return_bi)
167{
168 struct bio *bi = return_bi;
169 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700170
171 return_bi = bi->bi_next;
172 bi->bi_next = NULL;
173 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000174 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700175 bi = return_bi;
176 }
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static void print_raid5_conf (raid5_conf_t *conf);
180
Dan Williams600aa102008-06-28 08:32:05 +1000181static int stripe_operations_active(struct stripe_head *sh)
182{
183 return sh->check_state || sh->reconstruct_state ||
184 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
185 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
186}
187
Arjan van de Ven858119e2006-01-14 13:20:43 -0800188static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200191 BUG_ON(!list_empty(&sh->lru));
192 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700194 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700196 blk_plug_device(conf->mddev->queue);
197 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700198 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700199 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700200 blk_plug_device(conf->mddev->queue);
201 } else {
NeilBrown72626682005-09-09 16:23:54 -0700202 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 md_wakeup_thread(conf->mddev->thread);
206 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000207 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
209 atomic_dec(&conf->preread_active_stripes);
210 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
211 md_wakeup_thread(conf->mddev->thread);
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800214 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
215 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800217 if (conf->retry_read_aligned)
218 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221 }
222}
NeilBrownd0dabf72009-03-31 14:39:38 +1100223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224static void release_stripe(struct stripe_head *sh)
225{
226 raid5_conf_t *conf = sh->raid_conf;
227 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 spin_lock_irqsave(&conf->device_lock, flags);
230 __release_stripe(conf, sh);
231 spin_unlock_irqrestore(&conf->device_lock, flags);
232}
233
NeilBrownfccddba2006-01-06 00:20:33 -0800234static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Dan Williams45b42332007-07-09 11:56:43 -0700236 pr_debug("remove_hash(), stripe %llu\n",
237 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
NeilBrownfccddba2006-01-06 00:20:33 -0800239 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
NeilBrown16a53ec2006-06-26 00:27:38 -0700242static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
NeilBrownfccddba2006-01-06 00:20:33 -0800244 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Dan Williams45b42332007-07-09 11:56:43 -0700246 pr_debug("insert_hash(), stripe %llu\n",
247 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800250 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253
254/* find an idle stripe, make sure it is unhashed, and return it. */
255static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
256{
257 struct stripe_head *sh = NULL;
258 struct list_head *first;
259
260 CHECK_DEVLOCK();
261 if (list_empty(&conf->inactive_list))
262 goto out;
263 first = conf->inactive_list.next;
264 sh = list_entry(first, struct stripe_head, lru);
265 list_del_init(first);
266 remove_hash(sh);
267 atomic_inc(&conf->active_stripes);
268out:
269 return sh;
270}
271
272static void shrink_buffers(struct stripe_head *sh, int num)
273{
274 struct page *p;
275 int i;
276
277 for (i=0; i<num ; i++) {
278 p = sh->dev[i].page;
279 if (!p)
280 continue;
281 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800282 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284}
285
286static int grow_buffers(struct stripe_head *sh, int num)
287{
288 int i;
289
290 for (i=0; i<num; i++) {
291 struct page *page;
292
293 if (!(page = alloc_page(GFP_KERNEL))) {
294 return 1;
295 }
296 sh->dev[i].page = page;
297 }
298 return 0;
299}
300
NeilBrownd710e132008-10-13 11:55:12 +1100301static void raid5_build_block(struct stripe_head *sh, int i);
NeilBrown911d4ee2009-03-31 14:39:38 +1100302static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
303 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrownb5663ba2009-03-31 14:39:38 +1100305static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800308 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200310 BUG_ON(atomic_read(&sh->count) != 0);
311 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000312 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700315 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 (unsigned long long)sh->sector);
317
318 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700319
NeilBrownb5663ba2009-03-31 14:39:38 +1100320 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100322 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sh->state = 0;
324
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800325
326 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct r5dev *dev = &sh->dev[i];
328
Dan Williamsd84e0f12007-01-02 13:52:30 -0700329 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700331 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 test_bit(R5_LOCKED, &dev->flags));
335 BUG();
336 }
337 dev->flags = 0;
338 raid5_build_block(sh, i);
339 }
340 insert_hash(conf, sh);
341}
342
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800343static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800346 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700349 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800350 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800351 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700353 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return NULL;
355}
356
357static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200358static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
NeilBrownb5663ba2009-03-31 14:39:38 +1100360static struct stripe_head *
361get_active_stripe(raid5_conf_t *conf, sector_t sector,
362 int previous, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct stripe_head *sh;
NeilBrownb5663ba2009-03-31 14:39:38 +1100365 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Dan Williams45b42332007-07-09 11:56:43 -0700367 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 spin_lock_irq(&conf->device_lock);
370
371 do {
NeilBrown72626682005-09-09 16:23:54 -0700372 wait_event_lock_irq(conf->wait_for_stripe,
373 conf->quiesce == 0,
374 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800375 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (!sh) {
377 if (!conf->inactive_blocked)
378 sh = get_free_stripe(conf);
379 if (noblock && sh == NULL)
380 break;
381 if (!sh) {
382 conf->inactive_blocked = 1;
383 wait_event_lock_irq(conf->wait_for_stripe,
384 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800385 (atomic_read(&conf->active_stripes)
386 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 || !conf->inactive_blocked),
388 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700389 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 );
391 conf->inactive_blocked = 0;
392 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100393 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 } else {
395 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200396 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } else {
398 if (!test_bit(STRIPE_HANDLE, &sh->state))
399 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700400 if (list_empty(&sh->lru) &&
401 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700402 BUG();
403 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 }
406 } while (sh == NULL);
407
408 if (sh)
409 atomic_inc(&sh->count);
410
411 spin_unlock_irq(&conf->device_lock);
412 return sh;
413}
414
NeilBrown6712ecf2007-09-27 12:47:43 +0200415static void
416raid5_end_read_request(struct bio *bi, int error);
417static void
418raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700419
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000420static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700421{
422 raid5_conf_t *conf = sh->raid_conf;
423 int i, disks = sh->disks;
424
425 might_sleep();
426
427 for (i = disks; i--; ) {
428 int rw;
429 struct bio *bi;
430 mdk_rdev_t *rdev;
431 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
432 rw = WRITE;
433 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
434 rw = READ;
435 else
436 continue;
437
438 bi = &sh->dev[i].req;
439
440 bi->bi_rw = rw;
441 if (rw == WRITE)
442 bi->bi_end_io = raid5_end_write_request;
443 else
444 bi->bi_end_io = raid5_end_read_request;
445
446 rcu_read_lock();
447 rdev = rcu_dereference(conf->disks[i].rdev);
448 if (rdev && test_bit(Faulty, &rdev->flags))
449 rdev = NULL;
450 if (rdev)
451 atomic_inc(&rdev->nr_pending);
452 rcu_read_unlock();
453
454 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000455 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700456 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
457
Dan Williams2b7497f2008-06-28 08:31:52 +1000458 set_bit(STRIPE_IO_STARTED, &sh->state);
459
Dan Williams91c00922007-01-02 13:52:30 -0700460 bi->bi_bdev = rdev->bdev;
461 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700462 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700463 bi->bi_rw, i);
464 atomic_inc(&sh->count);
465 bi->bi_sector = sh->sector + rdev->data_offset;
466 bi->bi_flags = 1 << BIO_UPTODATE;
467 bi->bi_vcnt = 1;
468 bi->bi_max_vecs = 1;
469 bi->bi_idx = 0;
470 bi->bi_io_vec = &sh->dev[i].vec;
471 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
472 bi->bi_io_vec[0].bv_offset = 0;
473 bi->bi_size = STRIPE_SIZE;
474 bi->bi_next = NULL;
475 if (rw == WRITE &&
476 test_bit(R5_ReWrite, &sh->dev[i].flags))
477 atomic_add(STRIPE_SECTORS,
478 &rdev->corrected_errors);
479 generic_make_request(bi);
480 } else {
481 if (rw == WRITE)
482 set_bit(STRIPE_DEGRADED, &sh->state);
483 pr_debug("skip op %ld on disc %d for sector %llu\n",
484 bi->bi_rw, i, (unsigned long long)sh->sector);
485 clear_bit(R5_LOCKED, &sh->dev[i].flags);
486 set_bit(STRIPE_HANDLE, &sh->state);
487 }
488 }
489}
490
491static struct dma_async_tx_descriptor *
492async_copy_data(int frombio, struct bio *bio, struct page *page,
493 sector_t sector, struct dma_async_tx_descriptor *tx)
494{
495 struct bio_vec *bvl;
496 struct page *bio_page;
497 int i;
498 int page_offset;
499
500 if (bio->bi_sector >= sector)
501 page_offset = (signed)(bio->bi_sector - sector) * 512;
502 else
503 page_offset = (signed)(sector - bio->bi_sector) * -512;
504 bio_for_each_segment(bvl, bio, i) {
505 int len = bio_iovec_idx(bio, i)->bv_len;
506 int clen;
507 int b_offset = 0;
508
509 if (page_offset < 0) {
510 b_offset = -page_offset;
511 page_offset += b_offset;
512 len -= b_offset;
513 }
514
515 if (len > 0 && page_offset + len > STRIPE_SIZE)
516 clen = STRIPE_SIZE - page_offset;
517 else
518 clen = len;
519
520 if (clen > 0) {
521 b_offset += bio_iovec_idx(bio, i)->bv_offset;
522 bio_page = bio_iovec_idx(bio, i)->bv_page;
523 if (frombio)
524 tx = async_memcpy(page, bio_page, page_offset,
525 b_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700526 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700527 tx, NULL, NULL);
528 else
529 tx = async_memcpy(bio_page, page, b_offset,
530 page_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700531 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700532 tx, NULL, NULL);
533 }
534 if (clen < len) /* hit end of page */
535 break;
536 page_offset += len;
537 }
538
539 return tx;
540}
541
542static void ops_complete_biofill(void *stripe_head_ref)
543{
544 struct stripe_head *sh = stripe_head_ref;
545 struct bio *return_bi = NULL;
546 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700547 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700548
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700549 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700550 (unsigned long long)sh->sector);
551
552 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000553 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700554 for (i = sh->disks; i--; ) {
555 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700556
557 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700558 /* and check if we need to reply to a read request,
559 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000560 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700561 */
562 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700563 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700564
Dan Williams91c00922007-01-02 13:52:30 -0700565 BUG_ON(!dev->read);
566 rbi = dev->read;
567 dev->read = NULL;
568 while (rbi && rbi->bi_sector <
569 dev->sector + STRIPE_SECTORS) {
570 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200571 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700572 rbi->bi_next = return_bi;
573 return_bi = rbi;
574 }
Dan Williams91c00922007-01-02 13:52:30 -0700575 rbi = rbi2;
576 }
577 }
578 }
Dan Williams83de75c2008-06-28 08:31:58 +1000579 spin_unlock_irq(&conf->device_lock);
580 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700581
582 return_io(return_bi);
583
Dan Williamse4d84902007-09-24 10:06:13 -0700584 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700585 release_stripe(sh);
586}
587
588static void ops_run_biofill(struct stripe_head *sh)
589{
590 struct dma_async_tx_descriptor *tx = NULL;
591 raid5_conf_t *conf = sh->raid_conf;
592 int i;
593
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700594 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700595 (unsigned long long)sh->sector);
596
597 for (i = sh->disks; i--; ) {
598 struct r5dev *dev = &sh->dev[i];
599 if (test_bit(R5_Wantfill, &dev->flags)) {
600 struct bio *rbi;
601 spin_lock_irq(&conf->device_lock);
602 dev->read = rbi = dev->toread;
603 dev->toread = NULL;
604 spin_unlock_irq(&conf->device_lock);
605 while (rbi && rbi->bi_sector <
606 dev->sector + STRIPE_SECTORS) {
607 tx = async_copy_data(0, rbi, dev->page,
608 dev->sector, tx);
609 rbi = r5_next_bio(rbi, dev->sector);
610 }
611 }
612 }
613
614 atomic_inc(&sh->count);
615 async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
616 ops_complete_biofill, sh);
617}
618
619static void ops_complete_compute5(void *stripe_head_ref)
620{
621 struct stripe_head *sh = stripe_head_ref;
622 int target = sh->ops.target;
623 struct r5dev *tgt = &sh->dev[target];
624
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700625 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700626 (unsigned long long)sh->sector);
627
628 set_bit(R5_UPTODATE, &tgt->flags);
629 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
630 clear_bit(R5_Wantcompute, &tgt->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +1000631 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
632 if (sh->check_state == check_state_compute_run)
633 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700634 set_bit(STRIPE_HANDLE, &sh->state);
635 release_stripe(sh);
636}
637
Dan Williams7b3a8712008-06-28 08:32:09 +1000638static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -0700639{
640 /* kernel stack size limits the total number of disks */
641 int disks = sh->disks;
642 struct page *xor_srcs[disks];
643 int target = sh->ops.target;
644 struct r5dev *tgt = &sh->dev[target];
645 struct page *xor_dest = tgt->page;
646 int count = 0;
647 struct dma_async_tx_descriptor *tx;
648 int i;
649
650 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700651 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700652 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
653
654 for (i = disks; i--; )
655 if (i != target)
656 xor_srcs[count++] = sh->dev[i].page;
657
658 atomic_inc(&sh->count);
659
660 if (unlikely(count == 1))
661 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
662 0, NULL, ops_complete_compute5, sh);
663 else
664 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
665 ASYNC_TX_XOR_ZERO_DST, NULL,
666 ops_complete_compute5, sh);
667
Dan Williams91c00922007-01-02 13:52:30 -0700668 return tx;
669}
670
671static void ops_complete_prexor(void *stripe_head_ref)
672{
673 struct stripe_head *sh = stripe_head_ref;
674
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700675 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700676 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700677}
678
679static struct dma_async_tx_descriptor *
680ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
681{
682 /* kernel stack size limits the total number of disks */
683 int disks = sh->disks;
684 struct page *xor_srcs[disks];
685 int count = 0, pd_idx = sh->pd_idx, i;
686
687 /* existing parity data subtracted */
688 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
689
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700690 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700691 (unsigned long long)sh->sector);
692
693 for (i = disks; i--; ) {
694 struct r5dev *dev = &sh->dev[i];
695 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000696 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700697 xor_srcs[count++] = dev->page;
698 }
699
700 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
701 ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
702 ops_complete_prexor, sh);
703
704 return tx;
705}
706
707static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000708ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700709{
710 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000711 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700712
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700713 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700714 (unsigned long long)sh->sector);
715
716 for (i = disks; i--; ) {
717 struct r5dev *dev = &sh->dev[i];
718 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700719
Dan Williamsd8ee0722008-06-28 08:32:06 +1000720 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700721 struct bio *wbi;
722
723 spin_lock(&sh->lock);
724 chosen = dev->towrite;
725 dev->towrite = NULL;
726 BUG_ON(dev->written);
727 wbi = dev->written = chosen;
728 spin_unlock(&sh->lock);
729
730 while (wbi && wbi->bi_sector <
731 dev->sector + STRIPE_SECTORS) {
732 tx = async_copy_data(1, wbi, dev->page,
733 dev->sector, tx);
734 wbi = r5_next_bio(wbi, dev->sector);
735 }
736 }
737 }
738
739 return tx;
740}
741
742static void ops_complete_postxor(void *stripe_head_ref)
743{
744 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700745 int disks = sh->disks, i, pd_idx = sh->pd_idx;
746
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700747 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700748 (unsigned long long)sh->sector);
749
750 for (i = disks; i--; ) {
751 struct r5dev *dev = &sh->dev[i];
752 if (dev->written || i == pd_idx)
753 set_bit(R5_UPTODATE, &dev->flags);
754 }
755
Dan Williamsd8ee0722008-06-28 08:32:06 +1000756 if (sh->reconstruct_state == reconstruct_state_drain_run)
757 sh->reconstruct_state = reconstruct_state_drain_result;
758 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
759 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
760 else {
761 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
762 sh->reconstruct_state = reconstruct_state_result;
763 }
Dan Williams91c00922007-01-02 13:52:30 -0700764
765 set_bit(STRIPE_HANDLE, &sh->state);
766 release_stripe(sh);
767}
768
769static void
Dan Williamsd8ee0722008-06-28 08:32:06 +1000770ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700771{
772 /* kernel stack size limits the total number of disks */
773 int disks = sh->disks;
774 struct page *xor_srcs[disks];
775
776 int count = 0, pd_idx = sh->pd_idx, i;
777 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000778 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700779 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -0700780
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700781 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700782 (unsigned long long)sh->sector);
783
784 /* check if prexor is active which means only process blocks
785 * that are part of a read-modify-write (written)
786 */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000787 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
788 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700789 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
790 for (i = disks; i--; ) {
791 struct r5dev *dev = &sh->dev[i];
792 if (dev->written)
793 xor_srcs[count++] = dev->page;
794 }
795 } else {
796 xor_dest = sh->dev[pd_idx].page;
797 for (i = disks; i--; ) {
798 struct r5dev *dev = &sh->dev[i];
799 if (i != pd_idx)
800 xor_srcs[count++] = dev->page;
801 }
802 }
803
Dan Williams91c00922007-01-02 13:52:30 -0700804 /* 1/ if we prexor'd then the dest is reused as a source
805 * 2/ if we did not prexor then we are redoing the parity
806 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
807 * for the synchronous xor case
808 */
809 flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
810 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
811
812 atomic_inc(&sh->count);
813
814 if (unlikely(count == 1)) {
815 flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
816 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000817 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700818 } else
819 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000820 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700821}
822
823static void ops_complete_check(void *stripe_head_ref)
824{
825 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700826
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700827 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700828 (unsigned long long)sh->sector);
829
Dan Williamsecc65c92008-06-28 08:31:57 +1000830 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -0700831 set_bit(STRIPE_HANDLE, &sh->state);
832 release_stripe(sh);
833}
834
835static void ops_run_check(struct stripe_head *sh)
836{
837 /* kernel stack size limits the total number of disks */
838 int disks = sh->disks;
839 struct page *xor_srcs[disks];
840 struct dma_async_tx_descriptor *tx;
841
842 int count = 0, pd_idx = sh->pd_idx, i;
843 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
844
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700845 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700846 (unsigned long long)sh->sector);
847
848 for (i = disks; i--; ) {
849 struct r5dev *dev = &sh->dev[i];
850 if (i != pd_idx)
851 xor_srcs[count++] = dev->page;
852 }
853
854 tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
855 &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
856
Dan Williams91c00922007-01-02 13:52:30 -0700857 atomic_inc(&sh->count);
858 tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
859 ops_complete_check, sh);
860}
861
Dan Williams600aa102008-06-28 08:32:05 +1000862static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -0700863{
864 int overlap_clear = 0, i, disks = sh->disks;
865 struct dma_async_tx_descriptor *tx = NULL;
866
Dan Williams83de75c2008-06-28 08:31:58 +1000867 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -0700868 ops_run_biofill(sh);
869 overlap_clear++;
870 }
871
Dan Williams7b3a8712008-06-28 08:32:09 +1000872 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
873 tx = ops_run_compute5(sh);
874 /* terminate the chain if postxor is not set to be run */
875 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
876 async_tx_ack(tx);
877 }
Dan Williams91c00922007-01-02 13:52:30 -0700878
Dan Williams600aa102008-06-28 08:32:05 +1000879 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700880 tx = ops_run_prexor(sh, tx);
881
Dan Williams600aa102008-06-28 08:32:05 +1000882 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +1000883 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700884 overlap_clear++;
885 }
886
Dan Williams600aa102008-06-28 08:32:05 +1000887 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
Dan Williamsd8ee0722008-06-28 08:32:06 +1000888 ops_run_postxor(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700889
Dan Williamsecc65c92008-06-28 08:31:57 +1000890 if (test_bit(STRIPE_OP_CHECK, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700891 ops_run_check(sh);
892
Dan Williams91c00922007-01-02 13:52:30 -0700893 if (overlap_clear)
894 for (i = disks; i--; ) {
895 struct r5dev *dev = &sh->dev[i];
896 if (test_and_clear_bit(R5_Overlap, &dev->flags))
897 wake_up(&sh->raid_conf->wait_for_overlap);
898 }
899}
900
NeilBrown3f294f42005-11-08 21:39:25 -0800901static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800904 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
905 if (!sh)
906 return 0;
907 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
908 sh->raid_conf = conf;
909 spin_lock_init(&sh->lock);
910
911 if (grow_buffers(sh, conf->raid_disks)) {
912 shrink_buffers(sh, conf->raid_disks);
913 kmem_cache_free(conf->slab_cache, sh);
914 return 0;
915 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800916 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800917 /* we just created an active stripe so... */
918 atomic_set(&sh->count, 1);
919 atomic_inc(&conf->active_stripes);
920 INIT_LIST_HEAD(&sh->lru);
921 release_stripe(sh);
922 return 1;
923}
924
925static int grow_stripes(raid5_conf_t *conf, int num)
926{
Christoph Lametere18b8902006-12-06 20:33:20 -0800927 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int devs = conf->raid_disks;
929
NeilBrown42b9beb2007-05-09 02:35:37 -0700930 sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
931 sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -0800932 conf->active_name = 0;
933 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900935 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (!sc)
937 return 1;
938 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800939 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700940 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800941 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944}
NeilBrown29269552006-03-27 01:18:10 -0800945
946#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800947static int resize_stripes(raid5_conf_t *conf, int newsize)
948{
949 /* Make all the stripes able to hold 'newsize' devices.
950 * New slots in each stripe get 'page' set to a new page.
951 *
952 * This happens in stages:
953 * 1/ create a new kmem_cache and allocate the required number of
954 * stripe_heads.
955 * 2/ gather all the old stripe_heads and tranfer the pages across
956 * to the new stripe_heads. This will have the side effect of
957 * freezing the array as once all stripe_heads have been collected,
958 * no IO will be possible. Old stripe heads are freed once their
959 * pages have been transferred over, and the old kmem_cache is
960 * freed when all stripes are done.
961 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
962 * we simple return a failre status - no need to clean anything up.
963 * 4/ allocate new pages for the new slots in the new stripe_heads.
964 * If this fails, we don't bother trying the shrink the
965 * stripe_heads down again, we just leave them as they are.
966 * As each stripe_head is processed the new one is released into
967 * active service.
968 *
969 * Once step2 is started, we cannot afford to wait for a write,
970 * so we use GFP_NOIO allocations.
971 */
972 struct stripe_head *osh, *nsh;
973 LIST_HEAD(newstripes);
974 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -0700975 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -0800976 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800977 int i;
978
979 if (newsize <= conf->pool_size)
980 return 0; /* never bother to shrink */
981
Dan Williamsb5470dc2008-06-27 21:44:04 -0700982 err = md_allow_write(conf->mddev);
983 if (err)
984 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -0800985
NeilBrownad01c9e2006-03-27 01:18:07 -0800986 /* Step 1 */
987 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
988 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900989 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -0800990 if (!sc)
991 return -ENOMEM;
992
993 for (i = conf->max_nr_stripes; i; i--) {
994 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
995 if (!nsh)
996 break;
997
998 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
999
1000 nsh->raid_conf = conf;
1001 spin_lock_init(&nsh->lock);
1002
1003 list_add(&nsh->lru, &newstripes);
1004 }
1005 if (i) {
1006 /* didn't get enough, give up */
1007 while (!list_empty(&newstripes)) {
1008 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1009 list_del(&nsh->lru);
1010 kmem_cache_free(sc, nsh);
1011 }
1012 kmem_cache_destroy(sc);
1013 return -ENOMEM;
1014 }
1015 /* Step 2 - Must use GFP_NOIO now.
1016 * OK, we have enough stripes, start collecting inactive
1017 * stripes and copying them over
1018 */
1019 list_for_each_entry(nsh, &newstripes, lru) {
1020 spin_lock_irq(&conf->device_lock);
1021 wait_event_lock_irq(conf->wait_for_stripe,
1022 !list_empty(&conf->inactive_list),
1023 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001024 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001025 );
1026 osh = get_free_stripe(conf);
1027 spin_unlock_irq(&conf->device_lock);
1028 atomic_set(&nsh->count, 1);
1029 for(i=0; i<conf->pool_size; i++)
1030 nsh->dev[i].page = osh->dev[i].page;
1031 for( ; i<newsize; i++)
1032 nsh->dev[i].page = NULL;
1033 kmem_cache_free(conf->slab_cache, osh);
1034 }
1035 kmem_cache_destroy(conf->slab_cache);
1036
1037 /* Step 3.
1038 * At this point, we are holding all the stripes so the array
1039 * is completely stalled, so now is a good time to resize
1040 * conf->disks.
1041 */
1042 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1043 if (ndisks) {
1044 for (i=0; i<conf->raid_disks; i++)
1045 ndisks[i] = conf->disks[i];
1046 kfree(conf->disks);
1047 conf->disks = ndisks;
1048 } else
1049 err = -ENOMEM;
1050
1051 /* Step 4, return new stripes to service */
1052 while(!list_empty(&newstripes)) {
1053 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1054 list_del_init(&nsh->lru);
1055 for (i=conf->raid_disks; i < newsize; i++)
1056 if (nsh->dev[i].page == NULL) {
1057 struct page *p = alloc_page(GFP_NOIO);
1058 nsh->dev[i].page = p;
1059 if (!p)
1060 err = -ENOMEM;
1061 }
1062 release_stripe(nsh);
1063 }
1064 /* critical section pass, GFP_NOIO no longer needed */
1065
1066 conf->slab_cache = sc;
1067 conf->active_name = 1-conf->active_name;
1068 conf->pool_size = newsize;
1069 return err;
1070}
NeilBrown29269552006-03-27 01:18:10 -08001071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
NeilBrown3f294f42005-11-08 21:39:25 -08001073static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct stripe_head *sh;
1076
NeilBrown3f294f42005-11-08 21:39:25 -08001077 spin_lock_irq(&conf->device_lock);
1078 sh = get_free_stripe(conf);
1079 spin_unlock_irq(&conf->device_lock);
1080 if (!sh)
1081 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001082 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001083 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001084 kmem_cache_free(conf->slab_cache, sh);
1085 atomic_dec(&conf->active_stripes);
1086 return 1;
1087}
1088
1089static void shrink_stripes(raid5_conf_t *conf)
1090{
1091 while (drop_one_stripe(conf))
1092 ;
1093
NeilBrown29fc7e32006-02-03 03:03:41 -08001094 if (conf->slab_cache)
1095 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 conf->slab_cache = NULL;
1097}
1098
NeilBrown6712ecf2007-09-27 12:47:43 +02001099static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
NeilBrown99c0fb52009-03-31 14:39:38 +11001101 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001103 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001105 char b[BDEVNAME_SIZE];
1106 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 for (i=0 ; i<disks; i++)
1110 if (bi == &sh->dev[i].req)
1111 break;
1112
Dan Williams45b42332007-07-09 11:56:43 -07001113 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1114 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 uptodate);
1116 if (i == disks) {
1117 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
1121 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001123 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001124 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001125 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1126 " (%lu sectors at %llu on %s)\n",
1127 mdname(conf->mddev), STRIPE_SECTORS,
1128 (unsigned long long)(sh->sector
1129 + rdev->data_offset),
1130 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001131 clear_bit(R5_ReadError, &sh->dev[i].flags);
1132 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1133 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001134 if (atomic_read(&conf->disks[i].rdev->read_errors))
1135 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001137 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001138 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001139 rdev = conf->disks[i].rdev;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001142 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001143 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001144 printk_rl(KERN_WARNING
1145 "raid5:%s: read error not correctable "
1146 "(sector %llu on %s).\n",
1147 mdname(conf->mddev),
1148 (unsigned long long)(sh->sector
1149 + rdev->data_offset),
1150 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001151 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001152 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001153 printk_rl(KERN_WARNING
1154 "raid5:%s: read error NOT corrected!! "
1155 "(sector %llu on %s).\n",
1156 mdname(conf->mddev),
1157 (unsigned long long)(sh->sector
1158 + rdev->data_offset),
1159 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001160 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001161 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001162 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001163 "raid5:%s: Too many read errors, failing device %s.\n",
1164 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001165 else
1166 retry = 1;
1167 if (retry)
1168 set_bit(R5_ReadError, &sh->dev[i].flags);
1169 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001170 clear_bit(R5_ReadError, &sh->dev[i].flags);
1171 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001172 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1177 set_bit(STRIPE_HANDLE, &sh->state);
1178 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
NeilBrownd710e132008-10-13 11:55:12 +11001181static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
NeilBrown99c0fb52009-03-31 14:39:38 +11001183 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001185 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 for (i=0 ; i<disks; i++)
1189 if (bi == &sh->dev[i].req)
1190 break;
1191
Dan Williams45b42332007-07-09 11:56:43 -07001192 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1194 uptodate);
1195 if (i == disks) {
1196 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!uptodate)
1201 md_error(conf->mddev, conf->disks[i].rdev);
1202
1203 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1204
1205 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1206 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001207 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210
1211static sector_t compute_blocknr(struct stripe_head *sh, int i);
1212
NeilBrownd710e132008-10-13 11:55:12 +11001213static void raid5_build_block(struct stripe_head *sh, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct r5dev *dev = &sh->dev[i];
1216
1217 bio_init(&dev->req);
1218 dev->req.bi_io_vec = &dev->vec;
1219 dev->req.bi_vcnt++;
1220 dev->req.bi_max_vecs++;
1221 dev->vec.bv_page = dev->page;
1222 dev->vec.bv_len = STRIPE_SIZE;
1223 dev->vec.bv_offset = 0;
1224
1225 dev->req.bi_sector = sh->sector;
1226 dev->req.bi_private = sh;
1227
1228 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001229 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1233{
1234 char b[BDEVNAME_SIZE];
1235 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001236 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
NeilBrownb2d444d2005-11-08 21:39:31 -08001238 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b422006-10-03 01:15:46 -07001239 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001240 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1241 unsigned long flags;
1242 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001244 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
1246 * if recovery was running, make sure it aborts.
1247 */
NeilBrowndfc70642008-05-23 13:04:39 -07001248 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001250 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001251 printk(KERN_ALERT
1252 "raid5: Disk failure on %s, disabling device.\n"
1253 "raid5: Operation continuing on %d devices.\n",
1254 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258/*
1259 * Input: a 'big' sector number,
1260 * Output: index of the data and parity disk, and the sector # in them.
1261 */
NeilBrown112bf892009-03-31 14:39:38 +11001262static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001263 int previous, int *dd_idx,
1264 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
1266 long stripe;
1267 unsigned long chunk_number;
1268 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001269 int pd_idx, qd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sector_t new_sector;
1271 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrown112bf892009-03-31 14:39:38 +11001272 int raid_disks = previous ? conf->previous_raid_disks
1273 : conf->raid_disks;
1274 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* First compute the information on this sector */
1277
1278 /*
1279 * Compute the chunk number and the sector offset inside the chunk
1280 */
1281 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1282 chunk_number = r_sector;
1283 BUG_ON(r_sector != chunk_number);
1284
1285 /*
1286 * Compute the stripe number
1287 */
1288 stripe = chunk_number / data_disks;
1289
1290 /*
1291 * Compute the data disk and parity disk indexes inside the stripe
1292 */
1293 *dd_idx = chunk_number % data_disks;
1294
1295 /*
1296 * Select the parity disk based on the user selected algorithm.
1297 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001298 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001299 switch(conf->level) {
1300 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001301 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001302 break;
1303 case 5:
1304 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001306 pd_idx = data_disks - stripe % raid_disks;
1307 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 (*dd_idx)++;
1309 break;
1310 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001311 pd_idx = stripe % raid_disks;
1312 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 (*dd_idx)++;
1314 break;
1315 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001316 pd_idx = data_disks - stripe % raid_disks;
1317 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001320 pd_idx = stripe % raid_disks;
1321 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001323 case ALGORITHM_PARITY_0:
1324 pd_idx = 0;
1325 (*dd_idx)++;
1326 break;
1327 case ALGORITHM_PARITY_N:
1328 pd_idx = data_disks;
1329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001331 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001333 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001334 }
1335 break;
1336 case 6:
1337
NeilBrown16a53ec2006-06-26 00:27:38 -07001338 switch (conf->algorithm) {
1339 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001340 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1341 qd_idx = pd_idx + 1;
1342 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001343 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001344 qd_idx = 0;
1345 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001346 (*dd_idx) += 2; /* D D P Q D */
1347 break;
1348 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001349 pd_idx = stripe % raid_disks;
1350 qd_idx = pd_idx + 1;
1351 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001352 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001353 qd_idx = 0;
1354 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001355 (*dd_idx) += 2; /* D D P Q D */
1356 break;
1357 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001358 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1359 qd_idx = (pd_idx + 1) % raid_disks;
1360 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001361 break;
1362 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001363 pd_idx = stripe % raid_disks;
1364 qd_idx = (pd_idx + 1) % raid_disks;
1365 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001366 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001367
1368 case ALGORITHM_PARITY_0:
1369 pd_idx = 0;
1370 qd_idx = 1;
1371 (*dd_idx) += 2;
1372 break;
1373 case ALGORITHM_PARITY_N:
1374 pd_idx = data_disks;
1375 qd_idx = data_disks + 1;
1376 break;
1377
1378 case ALGORITHM_ROTATING_ZERO_RESTART:
1379 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1380 * of blocks for computing Q is different.
1381 */
1382 pd_idx = stripe % raid_disks;
1383 qd_idx = pd_idx + 1;
1384 if (pd_idx == raid_disks-1) {
1385 (*dd_idx)++; /* Q D D D P */
1386 qd_idx = 0;
1387 } else if (*dd_idx >= pd_idx)
1388 (*dd_idx) += 2; /* D D P Q D */
1389 break;
1390
1391 case ALGORITHM_ROTATING_N_RESTART:
1392 /* Same a left_asymmetric, by first stripe is
1393 * D D D P Q rather than
1394 * Q D D D P
1395 */
1396 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1397 qd_idx = pd_idx + 1;
1398 if (pd_idx == raid_disks-1) {
1399 (*dd_idx)++; /* Q D D D P */
1400 qd_idx = 0;
1401 } else if (*dd_idx >= pd_idx)
1402 (*dd_idx) += 2; /* D D P Q D */
1403 break;
1404
1405 case ALGORITHM_ROTATING_N_CONTINUE:
1406 /* Same as left_symmetric but Q is before P */
1407 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1408 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1409 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1410 break;
1411
1412 case ALGORITHM_LEFT_ASYMMETRIC_6:
1413 /* RAID5 left_asymmetric, with Q on last device */
1414 pd_idx = data_disks - stripe % (raid_disks-1);
1415 if (*dd_idx >= pd_idx)
1416 (*dd_idx)++;
1417 qd_idx = raid_disks - 1;
1418 break;
1419
1420 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1421 pd_idx = stripe % (raid_disks-1);
1422 if (*dd_idx >= pd_idx)
1423 (*dd_idx)++;
1424 qd_idx = raid_disks - 1;
1425 break;
1426
1427 case ALGORITHM_LEFT_SYMMETRIC_6:
1428 pd_idx = data_disks - stripe % (raid_disks-1);
1429 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1430 qd_idx = raid_disks - 1;
1431 break;
1432
1433 case ALGORITHM_RIGHT_SYMMETRIC_6:
1434 pd_idx = stripe % (raid_disks-1);
1435 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1436 qd_idx = raid_disks - 1;
1437 break;
1438
1439 case ALGORITHM_PARITY_0_6:
1440 pd_idx = 0;
1441 (*dd_idx)++;
1442 qd_idx = raid_disks - 1;
1443 break;
1444
1445
NeilBrown16a53ec2006-06-26 00:27:38 -07001446 default:
NeilBrownd710e132008-10-13 11:55:12 +11001447 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1448 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001449 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001450 }
1451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
NeilBrown911d4ee2009-03-31 14:39:38 +11001454 if (sh) {
1455 sh->pd_idx = pd_idx;
1456 sh->qd_idx = qd_idx;
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /*
1459 * Finally, compute the new sector number
1460 */
1461 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1462 return new_sector;
1463}
1464
1465
1466static sector_t compute_blocknr(struct stripe_head *sh, int i)
1467{
1468 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001469 int raid_disks = sh->disks;
1470 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 sector_t new_sector = sh->sector, check;
1472 int sectors_per_chunk = conf->chunk_size >> 9;
1473 sector_t stripe;
1474 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001475 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001477 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
NeilBrown16a53ec2006-06-26 00:27:38 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1481 stripe = new_sector;
1482 BUG_ON(new_sector != stripe);
1483
NeilBrown16a53ec2006-06-26 00:27:38 -07001484 if (i == sh->pd_idx)
1485 return 0;
1486 switch(conf->level) {
1487 case 4: break;
1488 case 5:
1489 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 case ALGORITHM_LEFT_ASYMMETRIC:
1491 case ALGORITHM_RIGHT_ASYMMETRIC:
1492 if (i > sh->pd_idx)
1493 i--;
1494 break;
1495 case ALGORITHM_LEFT_SYMMETRIC:
1496 case ALGORITHM_RIGHT_SYMMETRIC:
1497 if (i < sh->pd_idx)
1498 i += raid_disks;
1499 i -= (sh->pd_idx + 1);
1500 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001501 case ALGORITHM_PARITY_0:
1502 i -= 1;
1503 break;
1504 case ALGORITHM_PARITY_N:
1505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001507 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001508 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001509 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001510 }
1511 break;
1512 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001513 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001514 return 0; /* It is the Q disk */
1515 switch (conf->algorithm) {
1516 case ALGORITHM_LEFT_ASYMMETRIC:
1517 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001518 case ALGORITHM_ROTATING_ZERO_RESTART:
1519 case ALGORITHM_ROTATING_N_RESTART:
1520 if (sh->pd_idx == raid_disks-1)
1521 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001522 else if (i > sh->pd_idx)
1523 i -= 2; /* D D P Q D */
1524 break;
1525 case ALGORITHM_LEFT_SYMMETRIC:
1526 case ALGORITHM_RIGHT_SYMMETRIC:
1527 if (sh->pd_idx == raid_disks-1)
1528 i--; /* Q D D D P */
1529 else {
1530 /* D D P Q D */
1531 if (i < sh->pd_idx)
1532 i += raid_disks;
1533 i -= (sh->pd_idx + 2);
1534 }
1535 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001536 case ALGORITHM_PARITY_0:
1537 i -= 2;
1538 break;
1539 case ALGORITHM_PARITY_N:
1540 break;
1541 case ALGORITHM_ROTATING_N_CONTINUE:
1542 if (sh->pd_idx == 0)
1543 i--; /* P D D D Q */
1544 else if (i > sh->pd_idx)
1545 i -= 2; /* D D Q P D */
1546 break;
1547 case ALGORITHM_LEFT_ASYMMETRIC_6:
1548 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1549 if (i > sh->pd_idx)
1550 i--;
1551 break;
1552 case ALGORITHM_LEFT_SYMMETRIC_6:
1553 case ALGORITHM_RIGHT_SYMMETRIC_6:
1554 if (i < sh->pd_idx)
1555 i += data_disks + 1;
1556 i -= (sh->pd_idx + 1);
1557 break;
1558 case ALGORITHM_PARITY_0_6:
1559 i -= 1;
1560 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001561 default:
NeilBrownd710e132008-10-13 11:55:12 +11001562 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1563 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001564 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001565 }
1566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
1569 chunk_number = stripe * data_disks + i;
1570 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1571
NeilBrown112bf892009-03-31 14:39:38 +11001572 check = raid5_compute_sector(conf, r_sector,
1573 (raid_disks != conf->raid_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11001574 &dummy1, &sh2);
1575 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1576 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001577 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579 }
1580 return r_sector;
1581}
1582
1583
1584
1585/*
NeilBrown16a53ec2006-06-26 00:27:38 -07001586 * Copy data between a page in the stripe cache, and one or more bion
1587 * The page could align with the middle of the bio, or there could be
1588 * several bion, each with several bio_vecs, which cover part of the page
1589 * Multiple bion are linked together on bi_next. There may be extras
1590 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 */
1592static void copy_data(int frombio, struct bio *bio,
1593 struct page *page,
1594 sector_t sector)
1595{
1596 char *pa = page_address(page);
1597 struct bio_vec *bvl;
1598 int i;
1599 int page_offset;
1600
1601 if (bio->bi_sector >= sector)
1602 page_offset = (signed)(bio->bi_sector - sector) * 512;
1603 else
1604 page_offset = (signed)(sector - bio->bi_sector) * -512;
1605 bio_for_each_segment(bvl, bio, i) {
1606 int len = bio_iovec_idx(bio,i)->bv_len;
1607 int clen;
1608 int b_offset = 0;
1609
1610 if (page_offset < 0) {
1611 b_offset = -page_offset;
1612 page_offset += b_offset;
1613 len -= b_offset;
1614 }
1615
1616 if (len > 0 && page_offset + len > STRIPE_SIZE)
1617 clen = STRIPE_SIZE - page_offset;
1618 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -07001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (clen > 0) {
1621 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1622 if (frombio)
1623 memcpy(pa+page_offset, ba+b_offset, clen);
1624 else
1625 memcpy(ba+b_offset, pa+page_offset, clen);
1626 __bio_kunmap_atomic(ba, KM_USER0);
1627 }
1628 if (clen < len) /* hit end of page */
1629 break;
1630 page_offset += len;
1631 }
1632}
1633
Dan Williams9bc89cd2007-01-02 11:10:44 -07001634#define check_xor() do { \
1635 if (count == MAX_XOR_BLOCKS) { \
1636 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1637 count = 0; \
1638 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 } while(0)
1640
NeilBrown16a53ec2006-06-26 00:27:38 -07001641static void compute_parity6(struct stripe_head *sh, int method)
1642{
NeilBrownbff61972009-03-31 14:33:13 +11001643 raid5_conf_t *conf = sh->raid_conf;
NeilBrownd0dabf72009-03-31 14:39:38 +11001644 int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown16a53ec2006-06-26 00:27:38 -07001645 struct bio *chosen;
1646 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1647 void *ptrs[disks];
1648
NeilBrownd0dabf72009-03-31 14:39:38 +11001649 pd_idx = sh->pd_idx;
1650 qd_idx = sh->qd_idx;
1651 d0_idx = raid6_d0(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07001652
Dan Williams45b42332007-07-09 11:56:43 -07001653 pr_debug("compute_parity, stripe %llu, method %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001654 (unsigned long long)sh->sector, method);
1655
1656 switch(method) {
1657 case READ_MODIFY_WRITE:
1658 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1659 case RECONSTRUCT_WRITE:
1660 for (i= disks; i-- ;)
1661 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1662 chosen = sh->dev[i].towrite;
1663 sh->dev[i].towrite = NULL;
1664
1665 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1666 wake_up(&conf->wait_for_overlap);
1667
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001668 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001669 sh->dev[i].written = chosen;
1670 }
1671 break;
1672 case CHECK_PARITY:
1673 BUG(); /* Not implemented yet */
1674 }
1675
1676 for (i = disks; i--;)
1677 if (sh->dev[i].written) {
1678 sector_t sector = sh->dev[i].sector;
1679 struct bio *wbi = sh->dev[i].written;
1680 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1681 copy_data(1, wbi, sh->dev[i].page, sector);
1682 wbi = r5_next_bio(wbi, sector);
1683 }
1684
1685 set_bit(R5_LOCKED, &sh->dev[i].flags);
1686 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1687 }
1688
NeilBrownd0dabf72009-03-31 14:39:38 +11001689 /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
1690 /* FIX: Is this ordering of drives even remotely optimal? */
1691 count = 0;
1692 i = d0_idx;
1693 do {
1694 int slot = raid6_idx_to_slot(i, sh, &count);
1695 ptrs[slot] = page_address(sh->dev[i].page);
1696 if (slot < sh->disks - 2 &&
1697 !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1698 printk(KERN_ERR "block %d/%d not uptodate "
1699 "on parity calc\n", i, count);
1700 BUG();
1701 }
1702 i = raid6_next_disk(i, disks);
1703 } while (i != d0_idx);
1704 BUG_ON(count+2 != disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001705
1706 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1707
1708 switch(method) {
1709 case RECONSTRUCT_WRITE:
1710 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1711 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1712 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1713 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1714 break;
1715 case UPDATE_PARITY:
1716 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1717 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1718 break;
1719 }
1720}
1721
1722
1723/* Compute one missing block */
1724static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1725{
NeilBrownf4168852007-02-28 20:11:53 -08001726 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07001727 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrownd0dabf72009-03-31 14:39:38 +11001728 int qd_idx = sh->qd_idx;
NeilBrown16a53ec2006-06-26 00:27:38 -07001729
Dan Williams45b42332007-07-09 11:56:43 -07001730 pr_debug("compute_block_1, stripe %llu, idx %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001731 (unsigned long long)sh->sector, dd_idx);
1732
1733 if ( dd_idx == qd_idx ) {
1734 /* We're actually computing the Q drive */
1735 compute_parity6(sh, UPDATE_PARITY);
1736 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07001737 dest = page_address(sh->dev[dd_idx].page);
1738 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1739 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001740 for (i = disks ; i--; ) {
1741 if (i == dd_idx || i == qd_idx)
1742 continue;
1743 p = page_address(sh->dev[i].page);
1744 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1745 ptr[count++] = p;
1746 else
1747 printk("compute_block() %d, stripe %llu, %d"
1748 " not present\n", dd_idx,
1749 (unsigned long long)sh->sector, i);
1750
1751 check_xor();
1752 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001753 if (count)
1754 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07001755 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1756 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1757 }
1758}
1759
1760/* Compute two missing blocks */
1761static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1762{
NeilBrownf4168852007-02-28 20:11:53 -08001763 int i, count, disks = sh->disks;
NeilBrownd0dabf72009-03-31 14:39:38 +11001764 int d0_idx = raid6_d0(sh);
1765 int faila = -1, failb = -1;
1766 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1767 void *ptrs[disks];
NeilBrown16a53ec2006-06-26 00:27:38 -07001768
NeilBrownd0dabf72009-03-31 14:39:38 +11001769 count = 0;
1770 i = d0_idx;
1771 do {
1772 int slot;
1773 slot = raid6_idx_to_slot(i, sh, &count);
1774 ptrs[slot] = page_address(sh->dev[i].page);
1775 if (i == dd_idx1)
1776 faila = slot;
1777 if (i == dd_idx2)
1778 failb = slot;
1779 i = raid6_next_disk(i, disks);
1780 } while (i != d0_idx);
1781 BUG_ON(count+2 != disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001782
1783 BUG_ON(faila == failb);
1784 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1785
Dan Williams45b42332007-07-09 11:56:43 -07001786 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
NeilBrownd0dabf72009-03-31 14:39:38 +11001787 (unsigned long long)sh->sector, dd_idx1, dd_idx2,
1788 faila, failb);
NeilBrown16a53ec2006-06-26 00:27:38 -07001789
1790 if ( failb == disks-1 ) {
1791 /* Q disk is one of the missing disks */
1792 if ( faila == disks-2 ) {
1793 /* Missing P+Q, just recompute */
1794 compute_parity6(sh, UPDATE_PARITY);
1795 return;
1796 } else {
1797 /* We're missing D+Q; recompute D from P */
NeilBrownd0dabf72009-03-31 14:39:38 +11001798 compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
1799 dd_idx2 : dd_idx1),
1800 0);
NeilBrown16a53ec2006-06-26 00:27:38 -07001801 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1802 return;
1803 }
1804 }
1805
NeilBrownd0dabf72009-03-31 14:39:38 +11001806 /* We're missing D+P or D+D; */
1807 if (failb == disks-2) {
1808 /* We're missing D+P. */
1809 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1810 } else {
1811 /* We're missing D+D. */
1812 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001813 }
NeilBrownd0dabf72009-03-31 14:39:38 +11001814
1815 /* Both the above update both missing blocks */
1816 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1817 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07001818}
1819
Dan Williams600aa102008-06-28 08:32:05 +10001820static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001821schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001822 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001823{
1824 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001825
Dan Williamse33129d2007-01-02 13:52:30 -07001826 if (rcw) {
1827 /* if we are not expanding this is a proper write request, and
1828 * there will be bios with new data to be drained into the
1829 * stripe cache
1830 */
1831 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001832 sh->reconstruct_state = reconstruct_state_drain_run;
1833 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1834 } else
1835 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001836
Dan Williams600aa102008-06-28 08:32:05 +10001837 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001838
1839 for (i = disks; i--; ) {
1840 struct r5dev *dev = &sh->dev[i];
1841
1842 if (dev->towrite) {
1843 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001844 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001845 if (!expand)
1846 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001847 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001848 }
1849 }
Dan Williams600aa102008-06-28 08:32:05 +10001850 if (s->locked + 1 == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001851 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1852 atomic_inc(&sh->raid_conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001853 } else {
1854 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1855 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1856
Dan Williamsd8ee0722008-06-28 08:32:06 +10001857 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001858 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1859 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1860 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001861
1862 for (i = disks; i--; ) {
1863 struct r5dev *dev = &sh->dev[i];
1864 if (i == pd_idx)
1865 continue;
1866
Dan Williamse33129d2007-01-02 13:52:30 -07001867 if (dev->towrite &&
1868 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001869 test_bit(R5_Wantcompute, &dev->flags))) {
1870 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001871 set_bit(R5_LOCKED, &dev->flags);
1872 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001873 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001874 }
1875 }
1876 }
1877
1878 /* keep the parity disk locked while asynchronous operations
1879 * are in flight
1880 */
1881 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1882 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10001883 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001884
Dan Williams600aa102008-06-28 08:32:05 +10001885 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001886 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10001887 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001888}
NeilBrown16a53ec2006-06-26 00:27:38 -07001889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/*
1891 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001892 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * The bi_next chain must be in order.
1894 */
1895static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1896{
1897 struct bio **bip;
1898 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001899 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Dan Williams45b42332007-07-09 11:56:43 -07001901 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 (unsigned long long)bi->bi_sector,
1903 (unsigned long long)sh->sector);
1904
1905
1906 spin_lock(&sh->lock);
1907 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001908 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001910 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1911 firstwrite = 1;
1912 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 bip = &sh->dev[dd_idx].toread;
1914 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1915 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1916 goto overlap;
1917 bip = & (*bip)->bi_next;
1918 }
1919 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1920 goto overlap;
1921
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001922 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (*bip)
1924 bi->bi_next = *bip;
1925 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02001926 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 spin_unlock_irq(&conf->device_lock);
1928 spin_unlock(&sh->lock);
1929
Dan Williams45b42332007-07-09 11:56:43 -07001930 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 (unsigned long long)bi->bi_sector,
1932 (unsigned long long)sh->sector, dd_idx);
1933
NeilBrown72626682005-09-09 16:23:54 -07001934 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001935 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1936 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001937 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001938 set_bit(STRIPE_BIT_DELAY, &sh->state);
1939 }
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (forwrite) {
1942 /* check if page is covered */
1943 sector_t sector = sh->dev[dd_idx].sector;
1944 for (bi=sh->dev[dd_idx].towrite;
1945 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1946 bi && bi->bi_sector <= sector;
1947 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1948 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1949 sector = bi->bi_sector + (bi->bi_size>>9);
1950 }
1951 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1952 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1953 }
1954 return 1;
1955
1956 overlap:
1957 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1958 spin_unlock_irq(&conf->device_lock);
1959 spin_unlock(&sh->lock);
1960 return 0;
1961}
1962
NeilBrown29269552006-03-27 01:18:10 -08001963static void end_reshape(raid5_conf_t *conf);
1964
NeilBrown16a53ec2006-06-26 00:27:38 -07001965static int page_is_zero(struct page *p)
1966{
1967 char *a = page_address(p);
1968 return ((*(u32*)a) == 0 &&
1969 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1970}
1971
NeilBrown911d4ee2009-03-31 14:39:38 +11001972static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
1973 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08001974{
1975 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrown911d4ee2009-03-31 14:39:38 +11001976 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001977 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11001978 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001979
NeilBrown112bf892009-03-31 14:39:38 +11001980 raid5_compute_sector(conf,
1981 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08001982 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11001983 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11001984 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08001985}
1986
Dan Williamsa4456852007-07-09 11:56:43 -07001987static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001988handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07001989 struct stripe_head_state *s, int disks,
1990 struct bio **return_bi)
1991{
1992 int i;
1993 for (i = disks; i--; ) {
1994 struct bio *bi;
1995 int bitmap_end = 0;
1996
1997 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1998 mdk_rdev_t *rdev;
1999 rcu_read_lock();
2000 rdev = rcu_dereference(conf->disks[i].rdev);
2001 if (rdev && test_bit(In_sync, &rdev->flags))
2002 /* multiple read failures in one stripe */
2003 md_error(conf->mddev, rdev);
2004 rcu_read_unlock();
2005 }
2006 spin_lock_irq(&conf->device_lock);
2007 /* fail all writes first */
2008 bi = sh->dev[i].towrite;
2009 sh->dev[i].towrite = NULL;
2010 if (bi) {
2011 s->to_write--;
2012 bitmap_end = 1;
2013 }
2014
2015 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2016 wake_up(&conf->wait_for_overlap);
2017
2018 while (bi && bi->bi_sector <
2019 sh->dev[i].sector + STRIPE_SECTORS) {
2020 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2021 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002022 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002023 md_write_end(conf->mddev);
2024 bi->bi_next = *return_bi;
2025 *return_bi = bi;
2026 }
2027 bi = nextbi;
2028 }
2029 /* and fail all 'written' */
2030 bi = sh->dev[i].written;
2031 sh->dev[i].written = NULL;
2032 if (bi) bitmap_end = 1;
2033 while (bi && bi->bi_sector <
2034 sh->dev[i].sector + STRIPE_SECTORS) {
2035 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2036 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002037 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002038 md_write_end(conf->mddev);
2039 bi->bi_next = *return_bi;
2040 *return_bi = bi;
2041 }
2042 bi = bi2;
2043 }
2044
Dan Williamsb5e98d62007-01-02 13:52:31 -07002045 /* fail any reads if this device is non-operational and
2046 * the data has not reached the cache yet.
2047 */
2048 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2049 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2050 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002051 bi = sh->dev[i].toread;
2052 sh->dev[i].toread = NULL;
2053 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2054 wake_up(&conf->wait_for_overlap);
2055 if (bi) s->to_read--;
2056 while (bi && bi->bi_sector <
2057 sh->dev[i].sector + STRIPE_SECTORS) {
2058 struct bio *nextbi =
2059 r5_next_bio(bi, sh->dev[i].sector);
2060 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002061 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002062 bi->bi_next = *return_bi;
2063 *return_bi = bi;
2064 }
2065 bi = nextbi;
2066 }
2067 }
2068 spin_unlock_irq(&conf->device_lock);
2069 if (bitmap_end)
2070 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2071 STRIPE_SECTORS, 0, 0);
2072 }
2073
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002074 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2075 if (atomic_dec_and_test(&conf->pending_full_writes))
2076 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002077}
2078
Dan Williams1fe797e2008-06-28 09:16:30 +10002079/* fetch_block5 - checks the given member device to see if its data needs
2080 * to be read or computed to satisfy a request.
2081 *
2082 * Returns 1 when no more member devices need to be checked, otherwise returns
2083 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002084 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002085static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2086 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002087{
2088 struct r5dev *dev = &sh->dev[disk_idx];
2089 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2090
Dan Williamsf38e1212007-01-02 13:52:30 -07002091 /* is the data in this block needed, and can we get it? */
2092 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002093 !test_bit(R5_UPTODATE, &dev->flags) &&
2094 (dev->toread ||
2095 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2096 s->syncing || s->expanding ||
2097 (s->failed &&
2098 (failed_dev->toread ||
2099 (failed_dev->towrite &&
2100 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002101 /* We would like to get this block, possibly by computing it,
2102 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002103 */
2104 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002105 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002106 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2107 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002108 set_bit(R5_Wantcompute, &dev->flags);
2109 sh->ops.target = disk_idx;
2110 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002111 /* Careful: from this point on 'uptodate' is in the eye
2112 * of raid5_run_ops which services 'compute' operations
2113 * before writes. R5_Wantcompute flags a block that will
2114 * be R5_UPTODATE by the time it is needed for a
2115 * subsequent operation.
2116 */
2117 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002118 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002119 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002120 set_bit(R5_LOCKED, &dev->flags);
2121 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002122 s->locked++;
2123 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2124 s->syncing);
2125 }
2126 }
2127
Dan Williams1fe797e2008-06-28 09:16:30 +10002128 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002129}
2130
Dan Williams1fe797e2008-06-28 09:16:30 +10002131/**
2132 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2133 */
2134static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002135 struct stripe_head_state *s, int disks)
2136{
2137 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002138
Dan Williamsf38e1212007-01-02 13:52:30 -07002139 /* look for blocks to read/compute, skip this if a compute
2140 * is already in flight, or if the stripe contents are in the
2141 * midst of changing due to a write
2142 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002143 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002144 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002145 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002146 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002147 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002148 set_bit(STRIPE_HANDLE, &sh->state);
2149}
2150
Dan Williams1fe797e2008-06-28 09:16:30 +10002151static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002152 struct stripe_head_state *s, struct r6_state *r6s,
2153 int disks)
2154{
2155 int i;
2156 for (i = disks; i--; ) {
2157 struct r5dev *dev = &sh->dev[i];
2158 if (!test_bit(R5_LOCKED, &dev->flags) &&
2159 !test_bit(R5_UPTODATE, &dev->flags) &&
2160 (dev->toread || (dev->towrite &&
2161 !test_bit(R5_OVERWRITE, &dev->flags)) ||
2162 s->syncing || s->expanding ||
2163 (s->failed >= 1 &&
2164 (sh->dev[r6s->failed_num[0]].toread ||
2165 s->to_write)) ||
2166 (s->failed >= 2 &&
2167 (sh->dev[r6s->failed_num[1]].toread ||
2168 s->to_write)))) {
2169 /* we would like to get this block, possibly
2170 * by computing it, but we might not be able to
2171 */
Dan Williamsc3378692008-06-05 22:45:54 -07002172 if ((s->uptodate == disks - 1) &&
2173 (s->failed && (i == r6s->failed_num[0] ||
2174 i == r6s->failed_num[1]))) {
Dan Williams45b42332007-07-09 11:56:43 -07002175 pr_debug("Computing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002176 (unsigned long long)sh->sector, i);
2177 compute_block_1(sh, i, 0);
2178 s->uptodate++;
2179 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
2180 /* Computing 2-failure is *very* expensive; only
2181 * do it if failed >= 2
2182 */
2183 int other;
2184 for (other = disks; other--; ) {
2185 if (other == i)
2186 continue;
2187 if (!test_bit(R5_UPTODATE,
2188 &sh->dev[other].flags))
2189 break;
2190 }
2191 BUG_ON(other < 0);
Dan Williams45b42332007-07-09 11:56:43 -07002192 pr_debug("Computing stripe %llu blocks %d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002193 (unsigned long long)sh->sector,
2194 i, other);
2195 compute_block_2(sh, i, other);
2196 s->uptodate += 2;
2197 } else if (test_bit(R5_Insync, &dev->flags)) {
2198 set_bit(R5_LOCKED, &dev->flags);
2199 set_bit(R5_Wantread, &dev->flags);
2200 s->locked++;
Dan Williams45b42332007-07-09 11:56:43 -07002201 pr_debug("Reading block %d (sync=%d)\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002202 i, s->syncing);
2203 }
2204 }
2205 }
2206 set_bit(STRIPE_HANDLE, &sh->state);
2207}
2208
2209
Dan Williams1fe797e2008-06-28 09:16:30 +10002210/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002211 * any written block on an uptodate or failed drive can be returned.
2212 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2213 * never LOCKED, so we don't need to test 'failed' directly.
2214 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002215static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002216 struct stripe_head *sh, int disks, struct bio **return_bi)
2217{
2218 int i;
2219 struct r5dev *dev;
2220
2221 for (i = disks; i--; )
2222 if (sh->dev[i].written) {
2223 dev = &sh->dev[i];
2224 if (!test_bit(R5_LOCKED, &dev->flags) &&
2225 test_bit(R5_UPTODATE, &dev->flags)) {
2226 /* We can return any write requests */
2227 struct bio *wbi, *wbi2;
2228 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002229 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002230 spin_lock_irq(&conf->device_lock);
2231 wbi = dev->written;
2232 dev->written = NULL;
2233 while (wbi && wbi->bi_sector <
2234 dev->sector + STRIPE_SECTORS) {
2235 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002236 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002237 md_write_end(conf->mddev);
2238 wbi->bi_next = *return_bi;
2239 *return_bi = wbi;
2240 }
2241 wbi = wbi2;
2242 }
2243 if (dev->towrite == NULL)
2244 bitmap_end = 1;
2245 spin_unlock_irq(&conf->device_lock);
2246 if (bitmap_end)
2247 bitmap_endwrite(conf->mddev->bitmap,
2248 sh->sector,
2249 STRIPE_SECTORS,
2250 !test_bit(STRIPE_DEGRADED, &sh->state),
2251 0);
2252 }
2253 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002254
2255 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2256 if (atomic_dec_and_test(&conf->pending_full_writes))
2257 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002258}
2259
Dan Williams1fe797e2008-06-28 09:16:30 +10002260static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002261 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2262{
2263 int rmw = 0, rcw = 0, i;
2264 for (i = disks; i--; ) {
2265 /* would I have to read this buffer for read_modify_write */
2266 struct r5dev *dev = &sh->dev[i];
2267 if ((dev->towrite || i == sh->pd_idx) &&
2268 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002269 !(test_bit(R5_UPTODATE, &dev->flags) ||
2270 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002271 if (test_bit(R5_Insync, &dev->flags))
2272 rmw++;
2273 else
2274 rmw += 2*disks; /* cannot read it */
2275 }
2276 /* Would I have to read this buffer for reconstruct_write */
2277 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2278 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002279 !(test_bit(R5_UPTODATE, &dev->flags) ||
2280 test_bit(R5_Wantcompute, &dev->flags))) {
2281 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002282 else
2283 rcw += 2*disks;
2284 }
2285 }
Dan Williams45b42332007-07-09 11:56:43 -07002286 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002287 (unsigned long long)sh->sector, rmw, rcw);
2288 set_bit(STRIPE_HANDLE, &sh->state);
2289 if (rmw < rcw && rmw > 0)
2290 /* prefer read-modify-write, but need to get some data */
2291 for (i = disks; i--; ) {
2292 struct r5dev *dev = &sh->dev[i];
2293 if ((dev->towrite || i == sh->pd_idx) &&
2294 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002295 !(test_bit(R5_UPTODATE, &dev->flags) ||
2296 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002297 test_bit(R5_Insync, &dev->flags)) {
2298 if (
2299 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002300 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002301 "%d for r-m-w\n", i);
2302 set_bit(R5_LOCKED, &dev->flags);
2303 set_bit(R5_Wantread, &dev->flags);
2304 s->locked++;
2305 } else {
2306 set_bit(STRIPE_DELAYED, &sh->state);
2307 set_bit(STRIPE_HANDLE, &sh->state);
2308 }
2309 }
2310 }
2311 if (rcw <= rmw && rcw > 0)
2312 /* want reconstruct write, but need to get some data */
2313 for (i = disks; i--; ) {
2314 struct r5dev *dev = &sh->dev[i];
2315 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2316 i != sh->pd_idx &&
2317 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002318 !(test_bit(R5_UPTODATE, &dev->flags) ||
2319 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002320 test_bit(R5_Insync, &dev->flags)) {
2321 if (
2322 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002323 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002324 "%d for Reconstruct\n", i);
2325 set_bit(R5_LOCKED, &dev->flags);
2326 set_bit(R5_Wantread, &dev->flags);
2327 s->locked++;
2328 } else {
2329 set_bit(STRIPE_DELAYED, &sh->state);
2330 set_bit(STRIPE_HANDLE, &sh->state);
2331 }
2332 }
2333 }
2334 /* now if nothing is locked, and if we have enough data,
2335 * we can start a write request
2336 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002337 /* since handle_stripe can be called at any time we need to handle the
2338 * case where a compute block operation has been submitted and then a
2339 * subsequent call wants to start a write request. raid5_run_ops only
2340 * handles the case where compute block and postxor are requested
2341 * simultaneously. If this is not the case then new writes need to be
2342 * held off until the compute completes.
2343 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002344 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2345 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2346 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002347 schedule_reconstruction5(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002348}
2349
Dan Williams1fe797e2008-06-28 09:16:30 +10002350static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002351 struct stripe_head *sh, struct stripe_head_state *s,
2352 struct r6_state *r6s, int disks)
2353{
2354 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
2355 int qd_idx = r6s->qd_idx;
2356 for (i = disks; i--; ) {
2357 struct r5dev *dev = &sh->dev[i];
2358 /* Would I have to read this buffer for reconstruct_write */
2359 if (!test_bit(R5_OVERWRITE, &dev->flags)
2360 && i != pd_idx && i != qd_idx
2361 && (!test_bit(R5_LOCKED, &dev->flags)
2362 ) &&
2363 !test_bit(R5_UPTODATE, &dev->flags)) {
2364 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2365 else {
Dan Williams45b42332007-07-09 11:56:43 -07002366 pr_debug("raid6: must_compute: "
Dan Williamsa4456852007-07-09 11:56:43 -07002367 "disk %d flags=%#lx\n", i, dev->flags);
2368 must_compute++;
2369 }
2370 }
2371 }
Dan Williams45b42332007-07-09 11:56:43 -07002372 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002373 (unsigned long long)sh->sector, rcw, must_compute);
2374 set_bit(STRIPE_HANDLE, &sh->state);
2375
2376 if (rcw > 0)
2377 /* want reconstruct write, but need to get some data */
2378 for (i = disks; i--; ) {
2379 struct r5dev *dev = &sh->dev[i];
2380 if (!test_bit(R5_OVERWRITE, &dev->flags)
2381 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2382 && !test_bit(R5_LOCKED, &dev->flags) &&
2383 !test_bit(R5_UPTODATE, &dev->flags) &&
2384 test_bit(R5_Insync, &dev->flags)) {
2385 if (
2386 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002387 pr_debug("Read_old stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002388 "block %d for Reconstruct\n",
2389 (unsigned long long)sh->sector, i);
2390 set_bit(R5_LOCKED, &dev->flags);
2391 set_bit(R5_Wantread, &dev->flags);
2392 s->locked++;
2393 } else {
Dan Williams45b42332007-07-09 11:56:43 -07002394 pr_debug("Request delayed stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002395 "block %d for Reconstruct\n",
2396 (unsigned long long)sh->sector, i);
2397 set_bit(STRIPE_DELAYED, &sh->state);
2398 set_bit(STRIPE_HANDLE, &sh->state);
2399 }
2400 }
2401 }
2402 /* now if nothing is locked, and if we have enough data, we can start a
2403 * write request
2404 */
2405 if (s->locked == 0 && rcw == 0 &&
2406 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2407 if (must_compute > 0) {
2408 /* We have failed blocks and need to compute them */
2409 switch (s->failed) {
2410 case 0:
2411 BUG();
2412 case 1:
2413 compute_block_1(sh, r6s->failed_num[0], 0);
2414 break;
2415 case 2:
2416 compute_block_2(sh, r6s->failed_num[0],
2417 r6s->failed_num[1]);
2418 break;
2419 default: /* This request should have been failed? */
2420 BUG();
2421 }
2422 }
2423
Dan Williams45b42332007-07-09 11:56:43 -07002424 pr_debug("Computing parity for stripe %llu\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002425 (unsigned long long)sh->sector);
2426 compute_parity6(sh, RECONSTRUCT_WRITE);
2427 /* now every locked buffer is ready to be written */
2428 for (i = disks; i--; )
2429 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
Dan Williams45b42332007-07-09 11:56:43 -07002430 pr_debug("Writing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002431 (unsigned long long)sh->sector, i);
2432 s->locked++;
2433 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2434 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002435 if (s->locked == disks)
2436 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2437 atomic_inc(&conf->pending_full_writes);
Dan Williamsa4456852007-07-09 11:56:43 -07002438 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2439 set_bit(STRIPE_INSYNC, &sh->state);
2440
2441 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2442 atomic_dec(&conf->preread_active_stripes);
2443 if (atomic_read(&conf->preread_active_stripes) <
2444 IO_THRESHOLD)
2445 md_wakeup_thread(conf->mddev->thread);
2446 }
2447 }
2448}
2449
2450static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2451 struct stripe_head_state *s, int disks)
2452{
Dan Williamsecc65c92008-06-28 08:31:57 +10002453 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002454
Dan Williamsbd2ab672008-04-10 21:29:27 -07002455 set_bit(STRIPE_HANDLE, &sh->state);
2456
Dan Williamsecc65c92008-06-28 08:31:57 +10002457 switch (sh->check_state) {
2458 case check_state_idle:
2459 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002460 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002461 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002462 sh->check_state = check_state_run;
2463 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002464 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002465 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002466 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002467 }
Dan Williamsa4456852007-07-09 11:56:43 -07002468 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002469 /* fall through */
2470 case check_state_compute_result:
2471 sh->check_state = check_state_idle;
2472 if (!dev)
2473 dev = &sh->dev[sh->pd_idx];
2474
2475 /* check that a write has not made the stripe insync */
2476 if (test_bit(STRIPE_INSYNC, &sh->state))
2477 break;
2478
2479 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002480 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2481 BUG_ON(s->uptodate != disks);
2482
2483 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002484 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002485 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002486
Dan Williamsa4456852007-07-09 11:56:43 -07002487 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002488 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002489 break;
2490 case check_state_run:
2491 break; /* we will be called again upon completion */
2492 case check_state_check_result:
2493 sh->check_state = check_state_idle;
2494
2495 /* if a failure occurred during the check operation, leave
2496 * STRIPE_INSYNC not set and let the stripe be handled again
2497 */
2498 if (s->failed)
2499 break;
2500
2501 /* handle a successful check operation, if parity is correct
2502 * we are done. Otherwise update the mismatch count and repair
2503 * parity if !MD_RECOVERY_CHECK
2504 */
2505 if (sh->ops.zero_sum_result == 0)
2506 /* parity is correct (on disc,
2507 * not in buffer any more)
2508 */
2509 set_bit(STRIPE_INSYNC, &sh->state);
2510 else {
2511 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2512 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2513 /* don't try to repair!! */
2514 set_bit(STRIPE_INSYNC, &sh->state);
2515 else {
2516 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002517 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002518 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2519 set_bit(R5_Wantcompute,
2520 &sh->dev[sh->pd_idx].flags);
2521 sh->ops.target = sh->pd_idx;
2522 s->uptodate++;
2523 }
2524 }
2525 break;
2526 case check_state_compute_run:
2527 break;
2528 default:
2529 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2530 __func__, sh->check_state,
2531 (unsigned long long) sh->sector);
2532 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002533 }
2534}
2535
2536
2537static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2538 struct stripe_head_state *s,
2539 struct r6_state *r6s, struct page *tmp_page,
2540 int disks)
2541{
2542 int update_p = 0, update_q = 0;
2543 struct r5dev *dev;
2544 int pd_idx = sh->pd_idx;
2545 int qd_idx = r6s->qd_idx;
2546
2547 set_bit(STRIPE_HANDLE, &sh->state);
2548
2549 BUG_ON(s->failed > 2);
2550 BUG_ON(s->uptodate < disks);
2551 /* Want to check and possibly repair P and Q.
2552 * However there could be one 'failed' device, in which
2553 * case we can only check one of them, possibly using the
2554 * other to generate missing data
2555 */
2556
2557 /* If !tmp_page, we cannot do the calculations,
2558 * but as we have set STRIPE_HANDLE, we will soon be called
2559 * by stripe_handle with a tmp_page - just wait until then.
2560 */
2561 if (tmp_page) {
2562 if (s->failed == r6s->q_failed) {
2563 /* The only possible failed device holds 'Q', so it
2564 * makes sense to check P (If anything else were failed,
2565 * we would have used P to recreate it).
2566 */
2567 compute_block_1(sh, pd_idx, 1);
2568 if (!page_is_zero(sh->dev[pd_idx].page)) {
2569 compute_block_1(sh, pd_idx, 0);
2570 update_p = 1;
2571 }
2572 }
2573 if (!r6s->q_failed && s->failed < 2) {
2574 /* q is not failed, and we didn't use it to generate
2575 * anything, so it makes sense to check it
2576 */
2577 memcpy(page_address(tmp_page),
2578 page_address(sh->dev[qd_idx].page),
2579 STRIPE_SIZE);
2580 compute_parity6(sh, UPDATE_PARITY);
2581 if (memcmp(page_address(tmp_page),
2582 page_address(sh->dev[qd_idx].page),
2583 STRIPE_SIZE) != 0) {
2584 clear_bit(STRIPE_INSYNC, &sh->state);
2585 update_q = 1;
2586 }
2587 }
2588 if (update_p || update_q) {
2589 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2590 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2591 /* don't try to repair!! */
2592 update_p = update_q = 0;
2593 }
2594
2595 /* now write out any block on a failed drive,
2596 * or P or Q if they need it
2597 */
2598
2599 if (s->failed == 2) {
2600 dev = &sh->dev[r6s->failed_num[1]];
2601 s->locked++;
2602 set_bit(R5_LOCKED, &dev->flags);
2603 set_bit(R5_Wantwrite, &dev->flags);
2604 }
2605 if (s->failed >= 1) {
2606 dev = &sh->dev[r6s->failed_num[0]];
2607 s->locked++;
2608 set_bit(R5_LOCKED, &dev->flags);
2609 set_bit(R5_Wantwrite, &dev->flags);
2610 }
2611
2612 if (update_p) {
2613 dev = &sh->dev[pd_idx];
2614 s->locked++;
2615 set_bit(R5_LOCKED, &dev->flags);
2616 set_bit(R5_Wantwrite, &dev->flags);
2617 }
2618 if (update_q) {
2619 dev = &sh->dev[qd_idx];
2620 s->locked++;
2621 set_bit(R5_LOCKED, &dev->flags);
2622 set_bit(R5_Wantwrite, &dev->flags);
2623 }
2624 clear_bit(STRIPE_DEGRADED, &sh->state);
2625
2626 set_bit(STRIPE_INSYNC, &sh->state);
2627 }
2628}
2629
2630static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2631 struct r6_state *r6s)
2632{
2633 int i;
2634
2635 /* We have read all the blocks in this stripe and now we need to
2636 * copy some of them into a target stripe for expand.
2637 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002638 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002639 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2640 for (i = 0; i < sh->disks; i++)
NeilBrowna2e08552007-09-11 15:23:36 -07002641 if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002642 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002643 struct stripe_head *sh2;
2644
2645 sector_t bn = compute_blocknr(sh, i);
NeilBrown911d4ee2009-03-31 14:39:38 +11002646 sector_t s = raid5_compute_sector(conf, bn, 0,
2647 &dd_idx, NULL);
NeilBrownb5663ba2009-03-31 14:39:38 +11002648 sh2 = get_active_stripe(conf, s, 0, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002649 if (sh2 == NULL)
2650 /* so far only the early blocks of this stripe
2651 * have been requested. When later blocks
2652 * get requested, we will try again
2653 */
2654 continue;
2655 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2656 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2657 /* must have already done this block */
2658 release_stripe(sh2);
2659 continue;
2660 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002661
2662 /* place all the copies on one channel */
2663 tx = async_memcpy(sh2->dev[dd_idx].page,
2664 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2665 ASYNC_TX_DEP_ACK, tx, NULL, NULL);
2666
Dan Williamsa4456852007-07-09 11:56:43 -07002667 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2668 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2669 for (j = 0; j < conf->raid_disks; j++)
2670 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002671 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002672 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2673 break;
2674 if (j == conf->raid_disks) {
2675 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2676 set_bit(STRIPE_HANDLE, &sh2->state);
2677 }
2678 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002679
Dan Williamsa4456852007-07-09 11:56:43 -07002680 }
NeilBrowna2e08552007-09-11 15:23:36 -07002681 /* done submitting copies, wait for them to complete */
2682 if (tx) {
2683 async_tx_ack(tx);
2684 dma_wait_for_async_tx(tx);
2685 }
Dan Williamsa4456852007-07-09 11:56:43 -07002686}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Dan Williams6bfe0b42008-04-30 00:52:32 -07002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689/*
2690 * handle_stripe - do things to a stripe.
2691 *
2692 * We lock the stripe and then examine the state of various bits
2693 * to see what needs to be done.
2694 * Possible results:
2695 * return some read request which now have data
2696 * return some write requests which are safely on disc
2697 * schedule a read on some buffers
2698 * schedule a write of some buffers
2699 * return confirmation of parity correctness
2700 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * buffers are taken off read_list or write_list, and bh_cache buffers
2702 * get BH_Lock set before the stripe lock is released.
2703 *
2704 */
Dan Williamsa4456852007-07-09 11:56:43 -07002705
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002706static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
2708 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002709 int disks = sh->disks, i;
2710 struct bio *return_bi = NULL;
2711 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002713 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002714 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Dan Williamsa4456852007-07-09 11:56:43 -07002716 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002717 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2718 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2719 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2720 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 spin_lock(&sh->lock);
2723 clear_bit(STRIPE_HANDLE, &sh->state);
2724 clear_bit(STRIPE_DELAYED, &sh->state);
2725
Dan Williamsa4456852007-07-09 11:56:43 -07002726 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2727 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2728 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002731 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 for (i=disks; i--; ) {
2733 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002734 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Dan Williamsb5e98d62007-01-02 13:52:31 -07002737 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2738 "written %p\n", i, dev->flags, dev->toread, dev->read,
2739 dev->towrite, dev->written);
2740
2741 /* maybe we can request a biofill operation
2742 *
2743 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002744 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002745 */
2746 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002747 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002748 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002751 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2752 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002753 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Dan Williamsb5e98d62007-01-02 13:52:31 -07002755 if (test_bit(R5_Wantfill, &dev->flags))
2756 s.to_fill++;
2757 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002758 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002760 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002762 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
Dan Williamsa4456852007-07-09 11:56:43 -07002764 if (dev->written)
2765 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002766 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002767 if (blocked_rdev == NULL &&
2768 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002769 blocked_rdev = rdev;
2770 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002771 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002772 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002773 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002774 clear_bit(R5_ReadError, &dev->flags);
2775 clear_bit(R5_ReWrite, &dev->flags);
2776 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002777 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002778 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002779 s.failed++;
2780 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 } else
2782 set_bit(R5_Insync, &dev->flags);
2783 }
NeilBrown9910f162006-01-06 00:20:24 -08002784 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002785
Dan Williams6bfe0b42008-04-30 00:52:32 -07002786 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10002787 if (s.syncing || s.expanding || s.expanded ||
2788 s.to_write || s.written) {
2789 set_bit(STRIPE_HANDLE, &sh->state);
2790 goto unlock;
2791 }
2792 /* There is nothing for the blocked_rdev to block */
2793 rdev_dec_pending(blocked_rdev, conf->mddev);
2794 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002795 }
2796
Dan Williams83de75c2008-06-28 08:31:58 +10002797 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2798 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2799 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2800 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002801
Dan Williams45b42332007-07-09 11:56:43 -07002802 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002804 s.locked, s.uptodate, s.to_read, s.to_write,
2805 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 /* check if the array has lost two devices and, if so, some requests might
2807 * need to be failed
2808 */
Dan Williamsa4456852007-07-09 11:56:43 -07002809 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10002810 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07002811 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2813 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002814 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
2816
2817 /* might be able to return some write requests if the parity block
2818 * is safe, or on a failed drive
2819 */
2820 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07002821 if ( s.written &&
2822 ((test_bit(R5_Insync, &dev->flags) &&
2823 !test_bit(R5_LOCKED, &dev->flags) &&
2824 test_bit(R5_UPTODATE, &dev->flags)) ||
2825 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002826 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 /* Now we might consider reading some blocks, either to check/generate
2829 * parity, or to satisfy requests
2830 * or to load a block that is being partially written.
2831 */
Dan Williamsa4456852007-07-09 11:56:43 -07002832 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10002833 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10002834 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Dan Williamse33129d2007-01-02 13:52:30 -07002836 /* Now we check to see if any write operations have recently
2837 * completed
2838 */
Dan Williamse0a115e2008-06-05 22:45:52 -07002839 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002840 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07002841 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002842 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2843 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10002844 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07002845
2846 /* All the 'written' buffers and the parity block are ready to
2847 * be written back to disk
2848 */
2849 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2850 for (i = disks; i--; ) {
2851 dev = &sh->dev[i];
2852 if (test_bit(R5_LOCKED, &dev->flags) &&
2853 (i == sh->pd_idx || dev->written)) {
2854 pr_debug("Writing block %d\n", i);
2855 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07002856 if (prexor)
2857 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07002858 if (!test_bit(R5_Insync, &dev->flags) ||
2859 (i == sh->pd_idx && s.failed == 0))
2860 set_bit(STRIPE_INSYNC, &sh->state);
2861 }
2862 }
2863 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2864 atomic_dec(&conf->preread_active_stripes);
2865 if (atomic_read(&conf->preread_active_stripes) <
2866 IO_THRESHOLD)
2867 md_wakeup_thread(conf->mddev->thread);
2868 }
2869 }
2870
2871 /* Now to consider new write requests and what else, if anything
2872 * should be read. We do not handle new writes when:
2873 * 1/ A 'write' operation (copy+xor) is already in flight.
2874 * 2/ A 'check' operation is in flight, as it may clobber the parity
2875 * block.
2876 */
Dan Williams600aa102008-06-28 08:32:05 +10002877 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10002878 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07002881 * Any reads will already have been scheduled, so we just see if enough
2882 * data is available. The parity check is held off while parity
2883 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 */
Dan Williamsecc65c92008-06-28 08:31:57 +10002885 if (sh->check_state ||
2886 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002887 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002888 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07002889 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07002890
Dan Williamsa4456852007-07-09 11:56:43 -07002891 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2893 clear_bit(STRIPE_SYNCING, &sh->state);
2894 }
NeilBrown4e5314b2005-11-08 21:39:22 -08002895
2896 /* If the failed drive is just a ReadError, then we might need to progress
2897 * the repair/check process
2898 */
Dan Williamsa4456852007-07-09 11:56:43 -07002899 if (s.failed == 1 && !conf->mddev->ro &&
2900 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
2901 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
2902 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002903 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002904 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08002905 if (!test_bit(R5_ReWrite, &dev->flags)) {
2906 set_bit(R5_Wantwrite, &dev->flags);
2907 set_bit(R5_ReWrite, &dev->flags);
2908 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002909 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002910 } else {
2911 /* let's read it back */
2912 set_bit(R5_Wantread, &dev->flags);
2913 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002914 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002915 }
2916 }
2917
Dan Williams600aa102008-06-28 08:32:05 +10002918 /* Finish reconstruct operations initiated by the expansion process */
2919 if (sh->reconstruct_state == reconstruct_state_result) {
2920 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07002921 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07002922 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07002923 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07002924 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10002925 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07002926 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002927 }
2928
2929 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10002930 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002931 /* Need to write out all blocks after computing parity */
2932 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11002933 stripe_set_idx(sh->sector, conf, 0, sh);
Dan Williams1fe797e2008-06-28 09:16:30 +10002934 schedule_reconstruction5(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10002935 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002936 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08002937 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002938 wake_up(&conf->wait_for_overlap);
2939 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2940 }
2941
Dan Williams0f94e87c2008-01-08 15:32:53 -08002942 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002943 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07002944 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002945
Dan Williams6bfe0b42008-04-30 00:52:32 -07002946 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 spin_unlock(&sh->lock);
2948
Dan Williams6bfe0b42008-04-30 00:52:32 -07002949 /* wait for this device to become unblocked */
2950 if (unlikely(blocked_rdev))
2951 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
2952
Dan Williams600aa102008-06-28 08:32:05 +10002953 if (s.ops_request)
2954 raid5_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07002955
Dan Williamsc4e5ac02008-06-28 08:31:53 +10002956 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Dan Williamsa4456852007-07-09 11:56:43 -07002958 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002959
2960 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002963static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
NeilBrown16a53ec2006-06-26 00:27:38 -07002964{
NeilBrownbff61972009-03-31 14:33:13 +11002965 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002966 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07002967 struct bio *return_bi = NULL;
2968 int i, pd_idx = sh->pd_idx;
2969 struct stripe_head_state s;
2970 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07002971 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002972 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07002973
NeilBrownd0dabf72009-03-31 14:39:38 +11002974 r6s.qd_idx = sh->qd_idx;
Dan Williams45b42332007-07-09 11:56:43 -07002975 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Dan Williamsa4456852007-07-09 11:56:43 -07002976 "pd_idx=%d, qd_idx=%d\n",
2977 (unsigned long long)sh->sector, sh->state,
2978 atomic_read(&sh->count), pd_idx, r6s.qd_idx);
2979 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07002980
2981 spin_lock(&sh->lock);
2982 clear_bit(STRIPE_HANDLE, &sh->state);
2983 clear_bit(STRIPE_DELAYED, &sh->state);
2984
Dan Williamsa4456852007-07-09 11:56:43 -07002985 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2986 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2987 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07002988 /* Now to look around and see what can be done */
2989
2990 rcu_read_lock();
2991 for (i=disks; i--; ) {
2992 mdk_rdev_t *rdev;
2993 dev = &sh->dev[i];
2994 clear_bit(R5_Insync, &dev->flags);
2995
Dan Williams45b42332007-07-09 11:56:43 -07002996 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07002997 i, dev->flags, dev->toread, dev->towrite, dev->written);
2998 /* maybe we can reply to a read */
2999 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
3000 struct bio *rbi, *rbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003001 pr_debug("Return read for disc %d\n", i);
NeilBrown16a53ec2006-06-26 00:27:38 -07003002 spin_lock_irq(&conf->device_lock);
3003 rbi = dev->toread;
3004 dev->toread = NULL;
3005 if (test_and_clear_bit(R5_Overlap, &dev->flags))
3006 wake_up(&conf->wait_for_overlap);
3007 spin_unlock_irq(&conf->device_lock);
3008 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
3009 copy_data(0, rbi, dev->page, dev->sector);
3010 rbi2 = r5_next_bio(rbi, dev->sector);
3011 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003012 if (!raid5_dec_bi_phys_segments(rbi)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003013 rbi->bi_next = return_bi;
3014 return_bi = rbi;
3015 }
3016 spin_unlock_irq(&conf->device_lock);
3017 rbi = rbi2;
3018 }
3019 }
3020
3021 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003022 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3023 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003024
3025
Dan Williamsa4456852007-07-09 11:56:43 -07003026 if (dev->toread)
3027 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003028 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003029 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003030 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003031 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003032 }
Dan Williamsa4456852007-07-09 11:56:43 -07003033 if (dev->written)
3034 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003035 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003036 if (blocked_rdev == NULL &&
3037 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003038 blocked_rdev = rdev;
3039 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003040 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003041 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3042 /* The ReadError flag will just be confusing now */
3043 clear_bit(R5_ReadError, &dev->flags);
3044 clear_bit(R5_ReWrite, &dev->flags);
3045 }
3046 if (!rdev || !test_bit(In_sync, &rdev->flags)
3047 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003048 if (s.failed < 2)
3049 r6s.failed_num[s.failed] = i;
3050 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003051 } else
3052 set_bit(R5_Insync, &dev->flags);
3053 }
3054 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003055
3056 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003057 if (s.syncing || s.expanding || s.expanded ||
3058 s.to_write || s.written) {
3059 set_bit(STRIPE_HANDLE, &sh->state);
3060 goto unlock;
3061 }
3062 /* There is nothing for the blocked_rdev to block */
3063 rdev_dec_pending(blocked_rdev, conf->mddev);
3064 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003065 }
NeilBrownac4090d2008-08-05 15:54:13 +10003066
Dan Williams45b42332007-07-09 11:56:43 -07003067 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003068 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003069 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3070 r6s.failed_num[0], r6s.failed_num[1]);
3071 /* check if the array has lost >2 devices and, if so, some requests
3072 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003073 */
Dan Williamsa4456852007-07-09 11:56:43 -07003074 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003075 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003076 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003077 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3078 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003079 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003080 }
3081
3082 /*
3083 * might be able to return some write requests if the parity blocks
3084 * are safe, or on a failed drive
3085 */
3086 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003087 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3088 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
3089 qdev = &sh->dev[r6s.qd_idx];
3090 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
3091 || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003092
Dan Williamsa4456852007-07-09 11:56:43 -07003093 if ( s.written &&
3094 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003095 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003096 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3097 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003098 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003099 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003100 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003101
3102 /* Now we might consider reading some blocks, either to check/generate
3103 * parity, or to satisfy requests
3104 * or to load a block that is being partially written.
3105 */
Dan Williamsa4456852007-07-09 11:56:43 -07003106 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
3107 (s.syncing && (s.uptodate < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003108 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003109
3110 /* now to consider writing and what else, if anything should be read */
Dan Williamsa4456852007-07-09 11:56:43 -07003111 if (s.to_write)
Dan Williams1fe797e2008-06-28 09:16:30 +10003112 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003113
3114 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003115 * Any reads will already have been scheduled, so we just see if enough
3116 * data is available
NeilBrown16a53ec2006-06-26 00:27:38 -07003117 */
Dan Williamsa4456852007-07-09 11:56:43 -07003118 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
3119 handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003120
Dan Williamsa4456852007-07-09 11:56:43 -07003121 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003122 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3123 clear_bit(STRIPE_SYNCING, &sh->state);
3124 }
3125
3126 /* If the failed drives are just a ReadError, then we might need
3127 * to progress the repair/check process
3128 */
Dan Williamsa4456852007-07-09 11:56:43 -07003129 if (s.failed <= 2 && !conf->mddev->ro)
3130 for (i = 0; i < s.failed; i++) {
3131 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003132 if (test_bit(R5_ReadError, &dev->flags)
3133 && !test_bit(R5_LOCKED, &dev->flags)
3134 && test_bit(R5_UPTODATE, &dev->flags)
3135 ) {
3136 if (!test_bit(R5_ReWrite, &dev->flags)) {
3137 set_bit(R5_Wantwrite, &dev->flags);
3138 set_bit(R5_ReWrite, &dev->flags);
3139 set_bit(R5_LOCKED, &dev->flags);
3140 } else {
3141 /* let's read it back */
3142 set_bit(R5_Wantread, &dev->flags);
3143 set_bit(R5_LOCKED, &dev->flags);
3144 }
3145 }
3146 }
NeilBrownf4168852007-02-28 20:11:53 -08003147
Dan Williamsa4456852007-07-09 11:56:43 -07003148 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
NeilBrownf4168852007-02-28 20:11:53 -08003149 /* Need to write out all blocks after computing P&Q */
3150 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003151 stripe_set_idx(sh->sector, conf, 0, sh);
NeilBrownf4168852007-02-28 20:11:53 -08003152 compute_parity6(sh, RECONSTRUCT_WRITE);
3153 for (i = conf->raid_disks ; i-- ; ) {
3154 set_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003155 s.locked++;
NeilBrownf4168852007-02-28 20:11:53 -08003156 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3157 }
3158 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003159 } else if (s.expanded) {
NeilBrownf4168852007-02-28 20:11:53 -08003160 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3161 atomic_dec(&conf->reshape_stripes);
3162 wake_up(&conf->wait_for_overlap);
3163 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3164 }
3165
Dan Williams0f94e87c2008-01-08 15:32:53 -08003166 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003167 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003168 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003169
Dan Williams6bfe0b42008-04-30 00:52:32 -07003170 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003171 spin_unlock(&sh->lock);
3172
Dan Williams6bfe0b42008-04-30 00:52:32 -07003173 /* wait for this device to become unblocked */
3174 if (unlikely(blocked_rdev))
3175 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3176
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003177 ops_run_io(sh, &s);
3178
Dan Williamsa4456852007-07-09 11:56:43 -07003179 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003180
3181 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003182}
3183
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003184/* returns true if the stripe was handled */
3185static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
NeilBrown16a53ec2006-06-26 00:27:38 -07003186{
3187 if (sh->raid_conf->level == 6)
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003188 return handle_stripe6(sh, tmp_page);
NeilBrown16a53ec2006-06-26 00:27:38 -07003189 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003190 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003191}
3192
3193
3194
Arjan van de Ven858119e2006-01-14 13:20:43 -08003195static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3198 while (!list_empty(&conf->delayed_list)) {
3199 struct list_head *l = conf->delayed_list.next;
3200 struct stripe_head *sh;
3201 sh = list_entry(l, struct stripe_head, lru);
3202 list_del_init(l);
3203 clear_bit(STRIPE_DELAYED, &sh->state);
3204 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3205 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003206 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
NeilBrown6ed30032008-02-06 01:40:00 -08003208 } else
3209 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
Arjan van de Ven858119e2006-01-14 13:20:43 -08003212static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003213{
3214 /* device_lock is held */
3215 struct list_head head;
3216 list_add(&head, &conf->bitmap_list);
3217 list_del_init(&conf->bitmap_list);
3218 while (!list_empty(&head)) {
3219 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3220 list_del_init(&sh->lru);
3221 atomic_inc(&sh->count);
3222 __release_stripe(conf, sh);
3223 }
3224}
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226static void unplug_slaves(mddev_t *mddev)
3227{
3228 raid5_conf_t *conf = mddev_to_conf(mddev);
3229 int i;
3230
3231 rcu_read_lock();
3232 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003233 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003234 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003235 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 atomic_inc(&rdev->nr_pending);
3238 rcu_read_unlock();
3239
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003240 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 rdev_dec_pending(rdev, mddev);
3243 rcu_read_lock();
3244 }
3245 }
3246 rcu_read_unlock();
3247}
3248
Jens Axboe165125e2007-07-24 09:28:11 +02003249static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 mddev_t *mddev = q->queuedata;
3252 raid5_conf_t *conf = mddev_to_conf(mddev);
3253 unsigned long flags;
3254
3255 spin_lock_irqsave(&conf->device_lock, flags);
3256
NeilBrown72626682005-09-09 16:23:54 -07003257 if (blk_remove_plug(q)) {
3258 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 md_wakeup_thread(mddev->thread);
3262
3263 spin_unlock_irqrestore(&conf->device_lock, flags);
3264
3265 unplug_slaves(mddev);
3266}
3267
NeilBrownf022b2f2006-10-03 01:15:56 -07003268static int raid5_congested(void *data, int bits)
3269{
3270 mddev_t *mddev = data;
3271 raid5_conf_t *conf = mddev_to_conf(mddev);
3272
3273 /* No difference between reads and writes. Just check
3274 * how busy the stripe_cache is
3275 */
3276 if (conf->inactive_blocked)
3277 return 1;
3278 if (conf->quiesce)
3279 return 1;
3280 if (list_empty_careful(&conf->inactive_list))
3281 return 1;
3282
3283 return 0;
3284}
3285
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003286/* We want read requests to align with chunks where possible,
3287 * but write requests don't need to.
3288 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003289static int raid5_mergeable_bvec(struct request_queue *q,
3290 struct bvec_merge_data *bvm,
3291 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003292{
3293 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003294 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003295 int max;
3296 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003297 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003298
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003299 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003300 return biovec->bv_len; /* always allow writes to be mergeable */
3301
3302 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3303 if (max < 0) max = 0;
3304 if (max <= biovec->bv_len && bio_sectors == 0)
3305 return biovec->bv_len;
3306 else
3307 return max;
3308}
3309
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003310
3311static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3312{
3313 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3314 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3315 unsigned int bio_sectors = bio->bi_size >> 9;
3316
3317 return chunk_sectors >=
3318 ((sector & (chunk_sectors - 1)) + bio_sectors);
3319}
3320
3321/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003322 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3323 * later sampled by raid5d.
3324 */
3325static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3326{
3327 unsigned long flags;
3328
3329 spin_lock_irqsave(&conf->device_lock, flags);
3330
3331 bi->bi_next = conf->retry_read_aligned_list;
3332 conf->retry_read_aligned_list = bi;
3333
3334 spin_unlock_irqrestore(&conf->device_lock, flags);
3335 md_wakeup_thread(conf->mddev->thread);
3336}
3337
3338
3339static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3340{
3341 struct bio *bi;
3342
3343 bi = conf->retry_read_aligned;
3344 if (bi) {
3345 conf->retry_read_aligned = NULL;
3346 return bi;
3347 }
3348 bi = conf->retry_read_aligned_list;
3349 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003350 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003351 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003352 /*
3353 * this sets the active strip count to 1 and the processed
3354 * strip count to zero (upper 8 bits)
3355 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003356 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003357 }
3358
3359 return bi;
3360}
3361
3362
3363/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003364 * The "raid5_align_endio" should check if the read succeeded and if it
3365 * did, call bio_endio on the original bio (having bio_put the new bio
3366 * first).
3367 * If the read failed..
3368 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003369static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003370{
3371 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003372 mddev_t *mddev;
3373 raid5_conf_t *conf;
3374 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3375 mdk_rdev_t *rdev;
3376
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003377 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003378
3379 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3380 conf = mddev_to_conf(mddev);
3381 rdev = (void*)raid_bi->bi_next;
3382 raid_bi->bi_next = NULL;
3383
3384 rdev_dec_pending(rdev, conf->mddev);
3385
3386 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003387 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003388 if (atomic_dec_and_test(&conf->active_aligned_reads))
3389 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003390 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003391 }
3392
3393
Dan Williams45b42332007-07-09 11:56:43 -07003394 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003395
3396 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003397}
3398
Neil Brown387bb172007-02-08 14:20:29 -08003399static int bio_fits_rdev(struct bio *bi)
3400{
Jens Axboe165125e2007-07-24 09:28:11 +02003401 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003402
3403 if ((bi->bi_size>>9) > q->max_sectors)
3404 return 0;
3405 blk_recount_segments(q, bi);
Jens Axboe960e7392008-08-15 10:41:18 +02003406 if (bi->bi_phys_segments > q->max_phys_segments)
Neil Brown387bb172007-02-08 14:20:29 -08003407 return 0;
3408
3409 if (q->merge_bvec_fn)
3410 /* it's too hard to apply the merge_bvec_fn at this stage,
3411 * just just give up
3412 */
3413 return 0;
3414
3415 return 1;
3416}
3417
3418
Jens Axboe165125e2007-07-24 09:28:11 +02003419static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003420{
3421 mddev_t *mddev = q->queuedata;
3422 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003423 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003424 struct bio* align_bi;
3425 mdk_rdev_t *rdev;
3426
3427 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003428 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003429 return 0;
3430 }
3431 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003432 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003433 */
3434 align_bi = bio_clone(raid_bio, GFP_NOIO);
3435 if (!align_bi)
3436 return 0;
3437 /*
3438 * set bi_end_io to a new function, and set bi_private to the
3439 * original bio.
3440 */
3441 align_bi->bi_end_io = raid5_align_endio;
3442 align_bi->bi_private = raid_bio;
3443 /*
3444 * compute position
3445 */
NeilBrown112bf892009-03-31 14:39:38 +11003446 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3447 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003448 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003449
3450 rcu_read_lock();
3451 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3452 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003453 atomic_inc(&rdev->nr_pending);
3454 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003455 raid_bio->bi_next = (void*)rdev;
3456 align_bi->bi_bdev = rdev->bdev;
3457 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3458 align_bi->bi_sector += rdev->data_offset;
3459
Neil Brown387bb172007-02-08 14:20:29 -08003460 if (!bio_fits_rdev(align_bi)) {
3461 /* too big in some way */
3462 bio_put(align_bi);
3463 rdev_dec_pending(rdev, mddev);
3464 return 0;
3465 }
3466
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003467 spin_lock_irq(&conf->device_lock);
3468 wait_event_lock_irq(conf->wait_for_stripe,
3469 conf->quiesce == 0,
3470 conf->device_lock, /* nothing */);
3471 atomic_inc(&conf->active_aligned_reads);
3472 spin_unlock_irq(&conf->device_lock);
3473
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003474 generic_make_request(align_bi);
3475 return 1;
3476 } else {
3477 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003478 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003479 return 0;
3480 }
3481}
3482
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003483/* __get_priority_stripe - get the next stripe to process
3484 *
3485 * Full stripe writes are allowed to pass preread active stripes up until
3486 * the bypass_threshold is exceeded. In general the bypass_count
3487 * increments when the handle_list is handled before the hold_list; however, it
3488 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3489 * stripe with in flight i/o. The bypass_count will be reset when the
3490 * head of the hold_list has changed, i.e. the head was promoted to the
3491 * handle_list.
3492 */
3493static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3494{
3495 struct stripe_head *sh;
3496
3497 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3498 __func__,
3499 list_empty(&conf->handle_list) ? "empty" : "busy",
3500 list_empty(&conf->hold_list) ? "empty" : "busy",
3501 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3502
3503 if (!list_empty(&conf->handle_list)) {
3504 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3505
3506 if (list_empty(&conf->hold_list))
3507 conf->bypass_count = 0;
3508 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3509 if (conf->hold_list.next == conf->last_hold)
3510 conf->bypass_count++;
3511 else {
3512 conf->last_hold = conf->hold_list.next;
3513 conf->bypass_count -= conf->bypass_threshold;
3514 if (conf->bypass_count < 0)
3515 conf->bypass_count = 0;
3516 }
3517 }
3518 } else if (!list_empty(&conf->hold_list) &&
3519 ((conf->bypass_threshold &&
3520 conf->bypass_count > conf->bypass_threshold) ||
3521 atomic_read(&conf->pending_full_writes) == 0)) {
3522 sh = list_entry(conf->hold_list.next,
3523 typeof(*sh), lru);
3524 conf->bypass_count -= conf->bypass_threshold;
3525 if (conf->bypass_count < 0)
3526 conf->bypass_count = 0;
3527 } else
3528 return NULL;
3529
3530 list_del_init(&sh->lru);
3531 atomic_inc(&sh->count);
3532 BUG_ON(atomic_read(&sh->count) != 1);
3533 return sh;
3534}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003535
Jens Axboe165125e2007-07-24 09:28:11 +02003536static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 mddev_t *mddev = q->queuedata;
3539 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003540 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 sector_t new_sector;
3542 sector_t logical_sector, last_sector;
3543 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003544 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003545 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
NeilBrowne5dcdd82005-09-09 16:23:41 -07003547 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003548 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003549 return 0;
3550 }
3551
NeilBrown3d310eb2005-06-21 17:17:26 -07003552 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003553
Tejun Heo074a7ac2008-08-25 19:56:14 +09003554 cpu = part_stat_lock();
3555 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3556 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3557 bio_sectors(bi));
3558 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
NeilBrown802ba062006-12-13 00:34:13 -08003560 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003561 mddev->reshape_position == MaxSector &&
3562 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003563 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3566 last_sector = bi->bi_sector + (bi->bi_size>>9);
3567 bi->bi_next = NULL;
3568 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3571 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003572 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003573 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003574
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003575 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003576 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003577 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003578 if (likely(conf->expand_progress == MaxSector))
3579 disks = conf->raid_disks;
3580 else {
NeilBrowndf8e7f762006-03-27 01:18:15 -08003581 /* spinlock is needed as expand_progress may be
3582 * 64bit on a 32bit platform, and so it might be
3583 * possible to see a half-updated value
3584 * Ofcourse expand_progress could change after
3585 * the lock is dropped, so once we get a reference
3586 * to the stripe that we think it is, we will have
3587 * to check again.
3588 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003589 spin_lock_irq(&conf->device_lock);
3590 disks = conf->raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003591 if (logical_sector >= conf->expand_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003592 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003593 previous = 1;
3594 } else {
NeilBrownb578d552006-03-27 01:18:12 -08003595 if (logical_sector >= conf->expand_lo) {
3596 spin_unlock_irq(&conf->device_lock);
3597 schedule();
3598 goto retry;
3599 }
3600 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003601 spin_unlock_irq(&conf->device_lock);
3602 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003603 data_disks = disks - conf->max_degraded;
3604
NeilBrown112bf892009-03-31 14:39:38 +11003605 new_sector = raid5_compute_sector(conf, logical_sector,
3606 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003607 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003608 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 (unsigned long long)new_sector,
3610 (unsigned long long)logical_sector);
3611
NeilBrownb5663ba2009-03-31 14:39:38 +11003612 sh = get_active_stripe(conf, new_sector, previous,
3613 (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003615 if (unlikely(conf->expand_progress != MaxSector)) {
3616 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08003617 * stripe, so we must do the range check again.
3618 * Expansion could still move past after this
3619 * test, but as we are holding a reference to
3620 * 'sh', we know that if that happens,
3621 * STRIPE_EXPANDING will get set and the expansion
3622 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003623 */
3624 int must_retry = 0;
3625 spin_lock_irq(&conf->device_lock);
3626 if (logical_sector < conf->expand_progress &&
3627 disks == conf->previous_raid_disks)
3628 /* mismatch, need to try again */
3629 must_retry = 1;
3630 spin_unlock_irq(&conf->device_lock);
3631 if (must_retry) {
3632 release_stripe(sh);
3633 goto retry;
3634 }
3635 }
NeilBrowne464eaf2006-03-27 01:18:14 -08003636 /* FIXME what if we get a false positive because these
3637 * are being updated.
3638 */
3639 if (logical_sector >= mddev->suspend_lo &&
3640 logical_sector < mddev->suspend_hi) {
3641 release_stripe(sh);
3642 schedule();
3643 goto retry;
3644 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003645
3646 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3647 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3648 /* Stripe is busy expanding or
3649 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 * and wait a while
3651 */
3652 raid5_unplug_device(mddev->queue);
3653 release_stripe(sh);
3654 schedule();
3655 goto retry;
3656 }
3657 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003658 set_bit(STRIPE_HANDLE, &sh->state);
3659 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 } else {
3662 /* cannot get stripe for read-ahead, just give-up */
3663 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3664 finish_wait(&conf->wait_for_overlap, &w);
3665 break;
3666 }
3667
3668 }
3669 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003670 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003671 spin_unlock_irq(&conf->device_lock);
3672 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
NeilBrown16a53ec2006-06-26 00:27:38 -07003674 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003676
Neil Brown0e13fe232008-06-28 08:31:20 +10003677 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return 0;
3680}
3681
NeilBrown52c03292006-06-26 00:27:43 -07003682static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
NeilBrown52c03292006-06-26 00:27:43 -07003684 /* reshaping is quite different to recovery/resync so it is
3685 * handled quite separately ... here.
3686 *
3687 * On each call to sync_request, we gather one chunk worth of
3688 * destination stripes and flag them as expanding.
3689 * Then we find all the source stripes and request reads.
3690 * As the reads complete, handle_stripe will copy the data
3691 * into the destination stripe and release that stripe.
3692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3694 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003695 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003696 int raid_disks = conf->previous_raid_disks;
3697 int data_disks = raid_disks - conf->max_degraded;
3698 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003699 int i;
3700 int dd_idx;
3701 sector_t writepos, safepos, gap;
3702
3703 if (sector_nr == 0 &&
3704 conf->expand_progress != 0) {
3705 /* restarting in the middle, skip the initial sectors */
3706 sector_nr = conf->expand_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003707 sector_div(sector_nr, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003708 *skipped = 1;
3709 return sector_nr;
3710 }
3711
3712 /* we update the metadata when there is more than 3Meg
3713 * in the block range (that is rather arbitrary, should
3714 * probably be time based) or when the data about to be
3715 * copied would over-write the source of the data at
3716 * the front of the range.
3717 * i.e. one new_stripe forward from expand_progress new_maps
3718 * to after where expand_lo old_maps to
3719 */
3720 writepos = conf->expand_progress +
NeilBrownf4168852007-02-28 20:11:53 -08003721 conf->chunk_size/512*(new_data_disks);
3722 sector_div(writepos, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003723 safepos = conf->expand_lo;
NeilBrownf4168852007-02-28 20:11:53 -08003724 sector_div(safepos, data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003725 gap = conf->expand_progress - conf->expand_lo;
3726
3727 if (writepos >= safepos ||
NeilBrownf4168852007-02-28 20:11:53 -08003728 gap > (new_data_disks)*3000*2 /*3Meg*/) {
NeilBrown52c03292006-06-26 00:27:43 -07003729 /* Cannot proceed until we've updated the superblock... */
3730 wait_event(conf->wait_for_overlap,
3731 atomic_read(&conf->reshape_stripes)==0);
3732 mddev->reshape_position = conf->expand_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07003733 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003734 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07003735 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003736 kthread_should_stop());
3737 spin_lock_irq(&conf->device_lock);
3738 conf->expand_lo = mddev->reshape_position;
3739 spin_unlock_irq(&conf->device_lock);
3740 wake_up(&conf->wait_for_overlap);
3741 }
3742
3743 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
3744 int j;
3745 int skipped = 0;
NeilBrownb5663ba2009-03-31 14:39:38 +11003746 sh = get_active_stripe(conf, sector_nr+i, 0, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003747 set_bit(STRIPE_EXPANDING, &sh->state);
3748 atomic_inc(&conf->reshape_stripes);
3749 /* If any of this stripe is beyond the end of the old
3750 * array, then we need to zero those blocks
3751 */
3752 for (j=sh->disks; j--;) {
3753 sector_t s;
3754 if (j == sh->pd_idx)
3755 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003756 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11003757 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08003758 continue;
NeilBrown52c03292006-06-26 00:27:43 -07003759 s = compute_blocknr(sh, j);
Andre Nollf233ea52008-07-21 17:05:22 +10003760 if (s < mddev->array_sectors) {
NeilBrown52c03292006-06-26 00:27:43 -07003761 skipped = 1;
3762 continue;
3763 }
3764 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3765 set_bit(R5_Expanded, &sh->dev[j].flags);
3766 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3767 }
3768 if (!skipped) {
3769 set_bit(STRIPE_EXPAND_READY, &sh->state);
3770 set_bit(STRIPE_HANDLE, &sh->state);
3771 }
3772 release_stripe(sh);
3773 }
3774 spin_lock_irq(&conf->device_lock);
NeilBrown6d3baf22007-03-05 00:30:44 -08003775 conf->expand_progress = (sector_nr + i) * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07003776 spin_unlock_irq(&conf->device_lock);
3777 /* Ok, those stripe are ready. We can start scheduling
3778 * reads on the source stripes.
3779 * The source stripes are determined by mapping the first and last
3780 * block on the destination stripes.
3781 */
NeilBrown52c03292006-06-26 00:27:43 -07003782 first_sector =
NeilBrown112bf892009-03-31 14:39:38 +11003783 raid5_compute_sector(conf, sector_nr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11003784 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07003785 last_sector =
NeilBrown112bf892009-03-31 14:39:38 +11003786 raid5_compute_sector(conf, ((sector_nr+conf->chunk_size/512)
3787 *(new_data_disks) - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11003788 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11003789 if (last_sector >= mddev->dev_sectors)
3790 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07003791 while (first_sector <= last_sector) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003792 sh = get_active_stripe(conf, first_sector, 1, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003793 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3794 set_bit(STRIPE_HANDLE, &sh->state);
3795 release_stripe(sh);
3796 first_sector += STRIPE_SECTORS;
3797 }
NeilBrownc6207272008-02-06 01:39:52 -08003798 /* If this takes us to the resync_max point where we have to pause,
3799 * then we need to write out the superblock.
3800 */
3801 sector_nr += conf->chunk_size>>9;
3802 if (sector_nr >= mddev->resync_max) {
3803 /* Cannot proceed until we've updated the superblock... */
3804 wait_event(conf->wait_for_overlap,
3805 atomic_read(&conf->reshape_stripes) == 0);
3806 mddev->reshape_position = conf->expand_progress;
3807 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3808 md_wakeup_thread(mddev->thread);
3809 wait_event(mddev->sb_wait,
3810 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
3811 || kthread_should_stop());
3812 spin_lock_irq(&conf->device_lock);
3813 conf->expand_lo = mddev->reshape_position;
3814 spin_unlock_irq(&conf->device_lock);
3815 wake_up(&conf->wait_for_overlap);
3816 }
NeilBrown52c03292006-06-26 00:27:43 -07003817 return conf->chunk_size>>9;
3818}
3819
3820/* FIXME go_faster isn't used */
3821static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3822{
3823 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3824 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11003825 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07003826 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003827 int still_degraded = 0;
3828 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
NeilBrown72626682005-09-09 16:23:54 -07003830 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 /* just being told to finish up .. nothing much to do */
3832 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08003833 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3834 end_reshape(conf);
3835 return 0;
3836 }
NeilBrown72626682005-09-09 16:23:54 -07003837
3838 if (mddev->curr_resync < max_sector) /* aborted */
3839 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3840 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003841 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07003842 conf->fullsync = 0;
3843 bitmap_close_sync(mddev->bitmap);
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return 0;
3846 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08003847
NeilBrown52c03292006-06-26 00:27:43 -07003848 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3849 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08003850
NeilBrownc6207272008-02-06 01:39:52 -08003851 /* No need to check resync_max as we never do more than one
3852 * stripe, and as resync_max will always be on a chunk boundary,
3853 * if the check in md_do_sync didn't fire, there is no chance
3854 * of overstepping resync_max here
3855 */
3856
NeilBrown16a53ec2006-06-26 00:27:38 -07003857 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 * to resync, then assert that we are finished, because there is
3859 * nothing we can do.
3860 */
NeilBrown3285edf2006-06-26 00:27:55 -07003861 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07003862 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003863 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07003864 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return rv;
3866 }
NeilBrown72626682005-09-09 16:23:54 -07003867 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08003868 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07003869 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3870 /* we can skip this block, and probably more */
3871 sync_blocks /= STRIPE_SECTORS;
3872 *skipped = 1;
3873 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
NeilBrownb47490c2008-02-06 01:39:50 -08003876
3877 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3878
NeilBrownb5663ba2009-03-31 14:39:38 +11003879 sh = get_active_stripe(conf, sector_nr, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 if (sh == NULL) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003881 sh = get_active_stripe(conf, sector_nr, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07003883 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08003885 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003887 /* Need to check if array will still be degraded after recovery/resync
3888 * We don't need to check the 'failed' flag as when that gets set,
3889 * recovery aborts.
3890 */
3891 for (i=0; i<mddev->raid_disks; i++)
3892 if (conf->disks[i].rdev == NULL)
3893 still_degraded = 1;
3894
3895 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3896
3897 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 set_bit(STRIPE_SYNCING, &sh->state);
3899 clear_bit(STRIPE_INSYNC, &sh->state);
3900 spin_unlock(&sh->lock);
3901
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003902 /* wait for any blocked device to be handled */
3903 while(unlikely(!handle_stripe(sh, NULL)))
3904 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 release_stripe(sh);
3906
3907 return STRIPE_SECTORS;
3908}
3909
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003910static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3911{
3912 /* We may not be able to submit a whole bio at once as there
3913 * may not be enough stripe_heads available.
3914 * We cannot pre-allocate enough stripe_heads as we may need
3915 * more than exist in the cache (if we allow ever large chunks).
3916 * So we do one stripe head at a time and record in
3917 * ->bi_hw_segments how many have been done.
3918 *
3919 * We *know* that this entire raid_bio is in one chunk, so
3920 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3921 */
3922 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11003923 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003924 sector_t sector, logical_sector, last_sector;
3925 int scnt = 0;
3926 int remaining;
3927 int handled = 0;
3928
3929 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11003930 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11003931 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003932 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3933
3934 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08003935 logical_sector += STRIPE_SECTORS,
3936 sector += STRIPE_SECTORS,
3937 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003938
Jens Axboe960e7392008-08-15 10:41:18 +02003939 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003940 /* already done this stripe */
3941 continue;
3942
NeilBrownb5663ba2009-03-31 14:39:38 +11003943 sh = get_active_stripe(conf, sector, 0, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003944
3945 if (!sh) {
3946 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02003947 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003948 conf->retry_read_aligned = raid_bio;
3949 return handled;
3950 }
3951
3952 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08003953 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
3954 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02003955 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08003956 conf->retry_read_aligned = raid_bio;
3957 return handled;
3958 }
3959
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003960 handle_stripe(sh, NULL);
3961 release_stripe(sh);
3962 handled++;
3963 }
3964 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003965 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003966 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10003967 if (remaining == 0)
3968 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003969 if (atomic_dec_and_test(&conf->active_aligned_reads))
3970 wake_up(&conf->wait_for_stripe);
3971 return handled;
3972}
3973
3974
3975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976/*
3977 * This is our raid5 kernel thread.
3978 *
3979 * We scan the hash table for stripes which can be handled now.
3980 * During the scan, completed stripes are saved for us by the interrupt
3981 * handler, so that they will not have to wait for our next wakeup.
3982 */
NeilBrown6ed30032008-02-06 01:40:00 -08003983static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
3985 struct stripe_head *sh;
3986 raid5_conf_t *conf = mddev_to_conf(mddev);
3987 int handled;
3988
Dan Williams45b42332007-07-09 11:56:43 -07003989 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
3991 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 handled = 0;
3994 spin_lock_irq(&conf->device_lock);
3995 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003996 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
NeilBrownae3c20c2006-07-10 04:44:17 -07003998 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07003999 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004000 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004001 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004002 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004003 conf->seq_write = seq;
4004 activate_bit_delay(conf);
4005 }
4006
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004007 while ((bio = remove_bio_from_retry(conf))) {
4008 int ok;
4009 spin_unlock_irq(&conf->device_lock);
4010 ok = retry_aligned_read(conf, bio);
4011 spin_lock_irq(&conf->device_lock);
4012 if (!ok)
4013 break;
4014 handled++;
4015 }
4016
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004017 sh = __get_priority_stripe(conf);
4018
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004019 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 spin_unlock_irq(&conf->device_lock);
4022
4023 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004024 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 release_stripe(sh);
4026
4027 spin_lock_irq(&conf->device_lock);
4028 }
Dan Williams45b42332007-07-09 11:56:43 -07004029 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
4031 spin_unlock_irq(&conf->device_lock);
4032
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004033 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 unplug_slaves(mddev);
4035
Dan Williams45b42332007-07-09 11:56:43 -07004036 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037}
4038
NeilBrown3f294f42005-11-08 21:39:25 -08004039static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004040raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004041{
NeilBrown007583c2005-11-08 21:39:30 -08004042 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004043 if (conf)
4044 return sprintf(page, "%d\n", conf->max_nr_stripes);
4045 else
4046 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004047}
4048
4049static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004050raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004051{
NeilBrown007583c2005-11-08 21:39:30 -08004052 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004053 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004054 int err;
4055
NeilBrown3f294f42005-11-08 21:39:25 -08004056 if (len >= PAGE_SIZE)
4057 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004058 if (!conf)
4059 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004060
Dan Williams4ef197d82008-04-28 02:15:54 -07004061 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004062 return -EINVAL;
4063 if (new <= 16 || new > 32768)
4064 return -EINVAL;
4065 while (new < conf->max_nr_stripes) {
4066 if (drop_one_stripe(conf))
4067 conf->max_nr_stripes--;
4068 else
4069 break;
4070 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004071 err = md_allow_write(mddev);
4072 if (err)
4073 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004074 while (new > conf->max_nr_stripes) {
4075 if (grow_one_stripe(conf))
4076 conf->max_nr_stripes++;
4077 else break;
4078 }
4079 return len;
4080}
NeilBrown007583c2005-11-08 21:39:30 -08004081
NeilBrown96de1e62005-11-08 21:39:39 -08004082static struct md_sysfs_entry
4083raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4084 raid5_show_stripe_cache_size,
4085 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004086
4087static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004088raid5_show_preread_threshold(mddev_t *mddev, char *page)
4089{
4090 raid5_conf_t *conf = mddev_to_conf(mddev);
4091 if (conf)
4092 return sprintf(page, "%d\n", conf->bypass_threshold);
4093 else
4094 return 0;
4095}
4096
4097static ssize_t
4098raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4099{
4100 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004101 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004102 if (len >= PAGE_SIZE)
4103 return -EINVAL;
4104 if (!conf)
4105 return -ENODEV;
4106
Dan Williams4ef197d82008-04-28 02:15:54 -07004107 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004108 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004109 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004110 return -EINVAL;
4111 conf->bypass_threshold = new;
4112 return len;
4113}
4114
4115static struct md_sysfs_entry
4116raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4117 S_IRUGO | S_IWUSR,
4118 raid5_show_preread_threshold,
4119 raid5_store_preread_threshold);
4120
4121static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004122stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004123{
NeilBrown007583c2005-11-08 21:39:30 -08004124 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004125 if (conf)
4126 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4127 else
4128 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004129}
4130
NeilBrown96de1e62005-11-08 21:39:39 -08004131static struct md_sysfs_entry
4132raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004133
NeilBrown007583c2005-11-08 21:39:30 -08004134static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004135 &raid5_stripecache_size.attr,
4136 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004137 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004138 NULL,
4139};
NeilBrown007583c2005-11-08 21:39:30 -08004140static struct attribute_group raid5_attrs_group = {
4141 .name = NULL,
4142 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004143};
4144
NeilBrown72626682005-09-09 16:23:54 -07004145static int run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
4147 raid5_conf_t *conf;
4148 int raid_disk, memory;
4149 mdk_rdev_t *rdev;
4150 struct disk_info *disk;
NeilBrown02c2de82006-10-03 01:15:47 -07004151 int working_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
NeilBrown16a53ec2006-06-26 00:27:38 -07004153 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
4154 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown14f8d262006-01-06 00:20:14 -08004155 mdname(mddev), mddev->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return -EIO;
4157 }
NeilBrown99c0fb52009-03-31 14:39:38 +11004158 if ((mddev->level == 5 && !algorithm_valid_raid5(mddev->layout)) ||
4159 (mddev->level == 6 && !algorithm_valid_raid6(mddev->layout))) {
4160 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
4161 mdname(mddev), mddev->layout);
4162 return -EIO;
4163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
NeilBrown4bbf3772008-10-13 11:55:12 +11004165 if (mddev->chunk_size < PAGE_SIZE) {
4166 printk(KERN_ERR "md/raid5: chunk_size must be at least "
4167 "PAGE_SIZE but %d < %ld\n",
4168 mddev->chunk_size, PAGE_SIZE);
4169 return -EINVAL;
4170 }
4171
NeilBrownf6705572006-03-27 01:18:11 -08004172 if (mddev->reshape_position != MaxSector) {
4173 /* Check that we can continue the reshape.
4174 * Currently only disks can change, it must
4175 * increase, and we must be past the point where
4176 * a stripe over-writes itself
4177 */
4178 sector_t here_new, here_old;
4179 int old_disks;
NeilBrownf4168852007-02-28 20:11:53 -08004180 int max_degraded = (mddev->level == 5 ? 1 : 2);
NeilBrownf6705572006-03-27 01:18:11 -08004181
4182 if (mddev->new_level != mddev->level ||
4183 mddev->new_layout != mddev->layout ||
4184 mddev->new_chunk != mddev->chunk_size) {
NeilBrownf4168852007-02-28 20:11:53 -08004185 printk(KERN_ERR "raid5: %s: unsupported reshape "
4186 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004187 mdname(mddev));
4188 return -EINVAL;
4189 }
4190 if (mddev->delta_disks <= 0) {
NeilBrownf4168852007-02-28 20:11:53 -08004191 printk(KERN_ERR "raid5: %s: unsupported reshape "
4192 "(reduce disks) required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004193 mdname(mddev));
4194 return -EINVAL;
4195 }
4196 old_disks = mddev->raid_disks - mddev->delta_disks;
4197 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004198 * further up in new geometry must map after here in old
4199 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004200 */
4201 here_new = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004202 if (sector_div(here_new, (mddev->chunk_size>>9)*
4203 (mddev->raid_disks - max_degraded))) {
4204 printk(KERN_ERR "raid5: reshape_position not "
4205 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004206 return -EINVAL;
4207 }
4208 /* here_new is the stripe we will write to */
4209 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004210 sector_div(here_old, (mddev->chunk_size>>9)*
4211 (old_disks-max_degraded));
4212 /* here_old is the first stripe that we might need to read
4213 * from */
NeilBrownf6705572006-03-27 01:18:11 -08004214 if (here_new >= here_old) {
4215 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004216 printk(KERN_ERR "raid5: reshape_position too early for "
4217 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004218 return -EINVAL;
4219 }
4220 printk(KERN_INFO "raid5: reshape will continue\n");
4221 /* OK, we should be able to continue; */
4222 }
4223
4224
NeilBrownb55e6bf2006-03-27 01:18:06 -08004225 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 if ((conf = mddev->private) == NULL)
4227 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08004228 if (mddev->reshape_position == MaxSector) {
4229 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
4230 } else {
4231 conf->raid_disks = mddev->raid_disks;
4232 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4233 }
4234
4235 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
NeilBrownb55e6bf2006-03-27 01:18:06 -08004236 GFP_KERNEL);
4237 if (!conf->disks)
4238 goto abort;
NeilBrown9ffae0c2006-01-06 00:20:32 -08004239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 conf->mddev = mddev;
4241
NeilBrownfccddba2006-01-06 00:20:33 -08004242 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
NeilBrown16a53ec2006-06-26 00:27:38 -07004245 if (mddev->level == 6) {
4246 conf->spare_page = alloc_page(GFP_KERNEL);
4247 if (!conf->spare_page)
4248 goto abort;
4249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 spin_lock_init(&conf->device_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -07004251 mddev->queue->queue_lock = &conf->device_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 init_waitqueue_head(&conf->wait_for_stripe);
4253 init_waitqueue_head(&conf->wait_for_overlap);
4254 INIT_LIST_HEAD(&conf->handle_list);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004255 INIT_LIST_HEAD(&conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 INIT_LIST_HEAD(&conf->delayed_list);
NeilBrown72626682005-09-09 16:23:54 -07004257 INIT_LIST_HEAD(&conf->bitmap_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 INIT_LIST_HEAD(&conf->inactive_list);
4259 atomic_set(&conf->active_stripes, 0);
4260 atomic_set(&conf->preread_active_stripes, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004261 atomic_set(&conf->active_aligned_reads, 0);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004262 conf->bypass_threshold = BYPASS_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Dan Williams45b42332007-07-09 11:56:43 -07004264 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004266 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 raid_disk = rdev->raid_disk;
NeilBrownf6705572006-03-27 01:18:11 -08004268 if (raid_disk >= conf->raid_disks
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 || raid_disk < 0)
4270 continue;
4271 disk = conf->disks + raid_disk;
4272
4273 disk->rdev = rdev;
4274
NeilBrownb2d444d2005-11-08 21:39:31 -08004275 if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 char b[BDEVNAME_SIZE];
4277 printk(KERN_INFO "raid5: device %s operational as raid"
4278 " disk %d\n", bdevname(rdev->bdev,b),
4279 raid_disk);
NeilBrown02c2de82006-10-03 01:15:47 -07004280 working_disks++;
Neil Brown8c2e8702008-06-28 08:30:52 +10004281 } else
4282 /* Cannot rely on bitmap to complete recovery */
4283 conf->fullsync = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 }
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004287 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 */
NeilBrown02c2de82006-10-03 01:15:47 -07004289 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 conf->mddev = mddev;
4291 conf->chunk_size = mddev->chunk_size;
4292 conf->level = mddev->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07004293 if (conf->level == 6)
4294 conf->max_degraded = 2;
4295 else
4296 conf->max_degraded = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 conf->algorithm = mddev->layout;
4298 conf->max_nr_stripes = NR_STRIPES;
NeilBrownf6705572006-03-27 01:18:11 -08004299 conf->expand_progress = mddev->reshape_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 /* device size must be a multiple of chunk size */
Andre Noll58c0fed2009-03-31 14:33:13 +11004302 mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
4303 mddev->resync_max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
NeilBrown16a53ec2006-06-26 00:27:38 -07004305 if (conf->level == 6 && conf->raid_disks < 4) {
4306 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4307 mdname(mddev), conf->raid_disks);
4308 goto abort;
4309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 if (!conf->chunk_size || conf->chunk_size % 4) {
4311 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4312 conf->chunk_size, mdname(mddev));
4313 goto abort;
4314 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004315 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 printk(KERN_ERR "raid5: not enough operational devices for %s"
4317 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004318 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 goto abort;
4320 }
4321
NeilBrown16a53ec2006-06-26 00:27:38 -07004322 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004324 if (mddev->ok_start_degraded)
4325 printk(KERN_WARNING
4326 "raid5: starting dirty degraded array: %s"
4327 "- data corruption possible.\n",
4328 mdname(mddev));
4329 else {
4330 printk(KERN_ERR
4331 "raid5: cannot start dirty degraded array for %s\n",
4332 mdname(mddev));
4333 goto abort;
4334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
4336
4337 {
4338 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4339 if (!mddev->thread) {
4340 printk(KERN_ERR
4341 "raid5: couldn't allocate thread for %s\n",
4342 mdname(mddev));
4343 goto abort;
4344 }
4345 }
NeilBrown50368052005-12-12 02:39:17 -08004346 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4348 if (grow_stripes(conf, conf->max_nr_stripes)) {
4349 printk(KERN_ERR
4350 "raid5: couldn't allocate %dkB for buffers\n", memory);
4351 shrink_stripes(conf);
4352 md_unregister_thread(mddev->thread);
4353 goto abort;
4354 } else
4355 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4356 memory, mdname(mddev));
4357
4358 if (mddev->degraded == 0)
4359 printk("raid5: raid level %d set %s active with %d out of %d"
4360 " devices, algorithm %d\n", conf->level, mdname(mddev),
4361 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4362 conf->algorithm);
4363 else
4364 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4365 " out of %d devices, algorithm %d\n", conf->level,
4366 mdname(mddev), mddev->raid_disks - mddev->degraded,
4367 mddev->raid_disks, conf->algorithm);
4368
4369 print_raid5_conf(conf);
4370
NeilBrownf6705572006-03-27 01:18:11 -08004371 if (conf->expand_progress != MaxSector) {
4372 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08004373 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004374 atomic_set(&conf->reshape_stripes, 0);
4375 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4376 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4377 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4378 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4379 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4380 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004381 }
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004384 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 */
4386 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004387 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4388 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07004389 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4391 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4392 }
4393
4394 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004395 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4396 printk(KERN_WARNING
4397 "raid5: failed to create sysfs attributes for %s\n",
4398 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004399
4400 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004401 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004402 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004403
Andre Noll58c0fed2009-03-31 14:33:13 +11004404 mddev->array_sectors = mddev->dev_sectors *
4405 (conf->previous_raid_disks - conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07004406
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004407 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 return 0;
4410abort:
4411 if (conf) {
4412 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07004413 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004414 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08004415 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 kfree(conf);
4417 }
4418 mddev->private = NULL;
4419 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
4420 return -EIO;
4421}
4422
4423
4424
NeilBrown3f294f42005-11-08 21:39:25 -08004425static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
4427 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4428
4429 md_unregister_thread(mddev->thread);
4430 mddev->thread = NULL;
4431 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08004432 kfree(conf->stripe_hashtbl);
NeilBrown041ae522007-03-26 21:32:14 -08004433 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08004435 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004436 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08004437 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 mddev->private = NULL;
4439 return 0;
4440}
4441
Dan Williams45b42332007-07-09 11:56:43 -07004442#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11004443static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444{
4445 int i;
4446
NeilBrown16a53ec2006-06-26 00:27:38 -07004447 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4448 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4449 seq_printf(seq, "sh %llu, count %d.\n",
4450 (unsigned long long)sh->sector, atomic_read(&sh->count));
4451 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004452 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004453 seq_printf(seq, "(cache%d: %p %ld) ",
4454 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004456 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
4458
NeilBrownd710e132008-10-13 11:55:12 +11004459static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
4461 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08004462 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int i;
4464
4465 spin_lock_irq(&conf->device_lock);
4466 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08004467 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 if (sh->raid_conf != conf)
4469 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07004470 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 }
4472 }
4473 spin_unlock_irq(&conf->device_lock);
4474}
4475#endif
4476
NeilBrownd710e132008-10-13 11:55:12 +11004477static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
4479 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4480 int i;
4481
4482 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004483 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 for (i = 0; i < conf->raid_disks; i++)
4485 seq_printf (seq, "%s",
4486 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004487 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07004489#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07004490 seq_printf (seq, "\n");
4491 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492#endif
4493}
4494
4495static void print_raid5_conf (raid5_conf_t *conf)
4496{
4497 int i;
4498 struct disk_info *tmp;
4499
4500 printk("RAID5 conf printout:\n");
4501 if (!conf) {
4502 printk("(conf==NULL)\n");
4503 return;
4504 }
NeilBrown02c2de82006-10-03 01:15:47 -07004505 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
4506 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 for (i = 0; i < conf->raid_disks; i++) {
4509 char b[BDEVNAME_SIZE];
4510 tmp = conf->disks + i;
4511 if (tmp->rdev)
4512 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08004513 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 bdevname(tmp->rdev->bdev,b));
4515 }
4516}
4517
4518static int raid5_spare_active(mddev_t *mddev)
4519{
4520 int i;
4521 raid5_conf_t *conf = mddev->private;
4522 struct disk_info *tmp;
4523
4524 for (i = 0; i < conf->raid_disks; i++) {
4525 tmp = conf->disks + i;
4526 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08004527 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07004528 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
4529 unsigned long flags;
4530 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07004532 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
4534 }
4535 print_raid5_conf(conf);
4536 return 0;
4537}
4538
4539static int raid5_remove_disk(mddev_t *mddev, int number)
4540{
4541 raid5_conf_t *conf = mddev->private;
4542 int err = 0;
4543 mdk_rdev_t *rdev;
4544 struct disk_info *p = conf->disks + number;
4545
4546 print_raid5_conf(conf);
4547 rdev = p->rdev;
4548 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004549 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 atomic_read(&rdev->nr_pending)) {
4551 err = -EBUSY;
4552 goto abort;
4553 }
NeilBrowndfc70642008-05-23 13:04:39 -07004554 /* Only remove non-faulty devices if recovery
4555 * isn't possible.
4556 */
4557 if (!test_bit(Faulty, &rdev->flags) &&
4558 mddev->degraded <= conf->max_degraded) {
4559 err = -EBUSY;
4560 goto abort;
4561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004563 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (atomic_read(&rdev->nr_pending)) {
4565 /* lost the race, try later */
4566 err = -EBUSY;
4567 p->rdev = rdev;
4568 }
4569 }
4570abort:
4571
4572 print_raid5_conf(conf);
4573 return err;
4574}
4575
4576static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
4577{
4578 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10004579 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 int disk;
4581 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10004582 int first = 0;
4583 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
NeilBrown16a53ec2006-06-26 00:27:38 -07004585 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10004587 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Neil Brown6c2fce22008-06-28 08:31:31 +10004589 if (rdev->raid_disk >= 0)
4590 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004593 * find the disk ... but prefer rdev->saved_raid_disk
4594 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004596 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10004597 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004598 conf->disks[rdev->saved_raid_disk].rdev == NULL)
4599 disk = rdev->saved_raid_disk;
4600 else
Neil Brown6c2fce22008-06-28 08:31:31 +10004601 disk = first;
4602 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004604 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10004606 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07004607 if (rdev->saved_raid_disk != disk)
4608 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08004609 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 break;
4611 }
4612 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10004613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614}
4615
4616static int raid5_resize(mddev_t *mddev, sector_t sectors)
4617{
4618 /* no resync is happening, and there is enough space
4619 * on all devices, so we can resize.
4620 * We need to make sure resync covers any new space.
4621 * If the array is shrinking we should possibly wait until
4622 * any io in the removed space completes, but it hardly seems
4623 * worth it.
4624 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004625 raid5_conf_t *conf = mddev_to_conf(mddev);
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
Andre Nollf233ea52008-07-21 17:05:22 +10004628 mddev->array_sectors = sectors * (mddev->raid_disks
4629 - conf->max_degraded);
4630 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004631 mddev->changed = 1;
Andre Noll58c0fed2009-03-31 14:33:13 +11004632 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
4633 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4635 }
Andre Noll58c0fed2009-03-31 14:33:13 +11004636 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07004637 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 return 0;
4639}
4640
NeilBrown29269552006-03-27 01:18:10 -08004641#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004642static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08004643{
4644 raid5_conf_t *conf = mddev_to_conf(mddev);
4645 int err;
NeilBrown29269552006-03-27 01:18:10 -08004646
NeilBrown63c70c42006-03-27 01:18:13 -08004647 if (mddev->delta_disks < 0 ||
4648 mddev->new_level != mddev->level)
4649 return -EINVAL; /* Cannot shrink array or change level yet */
4650 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08004651 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10004652 if (mddev->bitmap)
4653 /* Cannot grow a bitmap yet */
4654 return -EBUSY;
NeilBrown29269552006-03-27 01:18:10 -08004655
4656 /* Can only proceed if there are plenty of stripe_heads.
4657 * We need a minimum of one full stripe,, and for sensible progress
4658 * it is best to have about 4 times that.
4659 * If we require 4 times, then the default 256 4K stripe_heads will
4660 * allow for chunk sizes up to 256K, which is probably OK.
4661 * If the chunk size is greater, user-space should request more
4662 * stripe_heads first.
4663 */
NeilBrown63c70c42006-03-27 01:18:13 -08004664 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
4665 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08004666 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
4667 (mddev->chunk_size / STRIPE_SIZE)*4);
4668 return -ENOSPC;
4669 }
4670
NeilBrown63c70c42006-03-27 01:18:13 -08004671 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
4672 if (err)
4673 return err;
4674
NeilBrownb4c4c7b2007-02-28 20:11:48 -08004675 if (mddev->degraded > conf->max_degraded)
4676 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004677 /* looks like we might be able to manage this */
4678 return 0;
4679}
4680
4681static int raid5_start_reshape(mddev_t *mddev)
4682{
4683 raid5_conf_t *conf = mddev_to_conf(mddev);
4684 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08004685 int spares = 0;
4686 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07004687 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08004688
NeilBrownf4168852007-02-28 20:11:53 -08004689 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08004690 return -EBUSY;
4691
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004692 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08004693 if (rdev->raid_disk < 0 &&
4694 !test_bit(Faulty, &rdev->flags))
4695 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08004696
NeilBrownf4168852007-02-28 20:11:53 -08004697 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08004698 /* Not enough devices even to make a degraded array
4699 * of that size
4700 */
4701 return -EINVAL;
4702
NeilBrownf6705572006-03-27 01:18:11 -08004703 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08004704 spin_lock_irq(&conf->device_lock);
4705 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08004706 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08004707 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004708 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08004709 spin_unlock_irq(&conf->device_lock);
4710
4711 /* Add some new drives, as many as will fit.
4712 * We know there are enough to make the newly sized array work.
4713 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004714 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08004715 if (rdev->raid_disk < 0 &&
4716 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10004717 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08004718 char nm[20];
4719 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08004720 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004721 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08004722 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004723 if (sysfs_create_link(&mddev->kobj,
4724 &rdev->kobj, nm))
4725 printk(KERN_WARNING
4726 "raid5: failed to create "
4727 " link %s for %s\n",
4728 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08004729 } else
4730 break;
4731 }
4732
NeilBrownc04be0a2006-10-03 01:15:53 -07004733 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08004734 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
NeilBrownc04be0a2006-10-03 01:15:53 -07004735 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08004736 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08004737 mddev->reshape_position = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07004738 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08004739
NeilBrown29269552006-03-27 01:18:10 -08004740 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4741 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4742 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4743 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4744 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4745 "%s_reshape");
4746 if (!mddev->sync_thread) {
4747 mddev->recovery = 0;
4748 spin_lock_irq(&conf->device_lock);
4749 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
4750 conf->expand_progress = MaxSector;
4751 spin_unlock_irq(&conf->device_lock);
4752 return -EAGAIN;
4753 }
4754 md_wakeup_thread(mddev->sync_thread);
4755 md_new_event(mddev);
4756 return 0;
4757}
4758#endif
4759
4760static void end_reshape(raid5_conf_t *conf)
4761{
4762 struct block_device *bdev;
4763
NeilBrownf6705572006-03-27 01:18:11 -08004764 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004765 conf->mddev->array_sectors = conf->mddev->dev_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004766 (conf->raid_disks - conf->max_degraded);
Andre Nollf233ea52008-07-21 17:05:22 +10004767 set_capacity(conf->mddev->gendisk, conf->mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004768 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08004769
NeilBrownf6705572006-03-27 01:18:11 -08004770 bdev = bdget_disk(conf->mddev->gendisk, 0);
4771 if (bdev) {
4772 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004773 i_size_write(bdev->bd_inode,
4774 (loff_t)conf->mddev->array_sectors << 9);
NeilBrownf6705572006-03-27 01:18:11 -08004775 mutex_unlock(&bdev->bd_inode->i_mutex);
4776 bdput(bdev);
4777 }
4778 spin_lock_irq(&conf->device_lock);
4779 conf->expand_progress = MaxSector;
4780 spin_unlock_irq(&conf->device_lock);
4781 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07004782
4783 /* read-ahead size must cover two whole stripes, which is
4784 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4785 */
4786 {
4787 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4788 int stripe = data_disks *
4789 (conf->mddev->chunk_size / PAGE_SIZE);
4790 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4791 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4792 }
NeilBrown29269552006-03-27 01:18:10 -08004793 }
NeilBrown29269552006-03-27 01:18:10 -08004794}
4795
NeilBrown72626682005-09-09 16:23:54 -07004796static void raid5_quiesce(mddev_t *mddev, int state)
4797{
4798 raid5_conf_t *conf = mddev_to_conf(mddev);
4799
4800 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08004801 case 2: /* resume for a suspend */
4802 wake_up(&conf->wait_for_overlap);
4803 break;
4804
NeilBrown72626682005-09-09 16:23:54 -07004805 case 1: /* stop all writes */
4806 spin_lock_irq(&conf->device_lock);
4807 conf->quiesce = 1;
4808 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004809 atomic_read(&conf->active_stripes) == 0 &&
4810 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07004811 conf->device_lock, /* nothing */);
4812 spin_unlock_irq(&conf->device_lock);
4813 break;
4814
4815 case 0: /* re-enable writes */
4816 spin_lock_irq(&conf->device_lock);
4817 conf->quiesce = 0;
4818 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08004819 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07004820 spin_unlock_irq(&conf->device_lock);
4821 break;
4822 }
NeilBrown72626682005-09-09 16:23:54 -07004823}
NeilBrownb15c2e52006-01-06 00:20:16 -08004824
NeilBrown16a53ec2006-06-26 00:27:38 -07004825static struct mdk_personality raid6_personality =
4826{
4827 .name = "raid6",
4828 .level = 6,
4829 .owner = THIS_MODULE,
4830 .make_request = make_request,
4831 .run = run,
4832 .stop = stop,
4833 .status = status,
4834 .error_handler = error,
4835 .hot_add_disk = raid5_add_disk,
4836 .hot_remove_disk= raid5_remove_disk,
4837 .spare_active = raid5_spare_active,
4838 .sync_request = sync_request,
4839 .resize = raid5_resize,
NeilBrownf4168852007-02-28 20:11:53 -08004840#ifdef CONFIG_MD_RAID5_RESHAPE
4841 .check_reshape = raid5_check_reshape,
4842 .start_reshape = raid5_start_reshape,
4843#endif
NeilBrown16a53ec2006-06-26 00:27:38 -07004844 .quiesce = raid5_quiesce,
4845};
NeilBrown2604b702006-01-06 00:20:36 -08004846static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
4848 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08004849 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 .owner = THIS_MODULE,
4851 .make_request = make_request,
4852 .run = run,
4853 .stop = stop,
4854 .status = status,
4855 .error_handler = error,
4856 .hot_add_disk = raid5_add_disk,
4857 .hot_remove_disk= raid5_remove_disk,
4858 .spare_active = raid5_spare_active,
4859 .sync_request = sync_request,
4860 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08004861#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004862 .check_reshape = raid5_check_reshape,
4863 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08004864#endif
NeilBrown72626682005-09-09 16:23:54 -07004865 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866};
4867
NeilBrown2604b702006-01-06 00:20:36 -08004868static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
NeilBrown2604b702006-01-06 00:20:36 -08004870 .name = "raid4",
4871 .level = 4,
4872 .owner = THIS_MODULE,
4873 .make_request = make_request,
4874 .run = run,
4875 .stop = stop,
4876 .status = status,
4877 .error_handler = error,
4878 .hot_add_disk = raid5_add_disk,
4879 .hot_remove_disk= raid5_remove_disk,
4880 .spare_active = raid5_spare_active,
4881 .sync_request = sync_request,
4882 .resize = raid5_resize,
NeilBrown3d378902007-03-26 21:32:13 -08004883#ifdef CONFIG_MD_RAID5_RESHAPE
4884 .check_reshape = raid5_check_reshape,
4885 .start_reshape = raid5_start_reshape,
4886#endif
NeilBrown2604b702006-01-06 00:20:36 -08004887 .quiesce = raid5_quiesce,
4888};
4889
4890static int __init raid5_init(void)
4891{
NeilBrown16a53ec2006-06-26 00:27:38 -07004892 int e;
4893
4894 e = raid6_select_algo();
4895 if ( e )
4896 return e;
4897 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004898 register_md_personality(&raid5_personality);
4899 register_md_personality(&raid4_personality);
4900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
4902
NeilBrown2604b702006-01-06 00:20:36 -08004903static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
NeilBrown16a53ec2006-06-26 00:27:38 -07004905 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004906 unregister_md_personality(&raid5_personality);
4907 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
4910module_init(raid5_init);
4911module_exit(raid5_exit);
4912MODULE_LICENSE("GPL");
4913MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004914MODULE_ALIAS("md-raid5");
4915MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08004916MODULE_ALIAS("md-level-5");
4917MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07004918MODULE_ALIAS("md-personality-8"); /* RAID6 */
4919MODULE_ALIAS("md-raid6");
4920MODULE_ALIAS("md-level-6");
4921
4922/* This used to be two separate modules, they were: */
4923MODULE_ALIAS("raid5");
4924MODULE_ALIAS("raid6");