blob: 81789fa7a023368d83518ea6f80ffed570c83fbd [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{
NeilBrown67cc2b82009-03-31 14:39:38 +1100139 if (sh->ddf_layout)
140 /* ddf always start from first device */
141 return 0;
142 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100143 if (sh->qd_idx == sh->disks - 1)
144 return 0;
145 else
146 return sh->qd_idx + 1;
147}
NeilBrown16a53ec2006-06-26 00:27:38 -0700148static inline int raid6_next_disk(int disk, int raid_disks)
149{
150 disk++;
151 return (disk < raid_disks) ? disk : 0;
152}
Dan Williamsa4456852007-07-09 11:56:43 -0700153
NeilBrownd0dabf72009-03-31 14:39:38 +1100154/* When walking through the disks in a raid5, starting at raid6_d0,
155 * We need to map each disk to a 'slot', where the data disks are slot
156 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
157 * is raid_disks-1. This help does that mapping.
158 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100159static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
160 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100161{
162 int slot;
NeilBrown67cc2b82009-03-31 14:39:38 +1100163
NeilBrownd0dabf72009-03-31 14:39:38 +1100164 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100165 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100166 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100167 return syndrome_disks + 1;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 slot = (*count)++;
169 return slot;
170}
171
Dan Williamsa4456852007-07-09 11:56:43 -0700172static void return_io(struct bio *return_bi)
173{
174 struct bio *bi = return_bi;
175 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700176
177 return_bi = bi->bi_next;
178 bi->bi_next = NULL;
179 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000180 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700181 bi = return_bi;
182 }
183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static void print_raid5_conf (raid5_conf_t *conf);
186
Dan Williams600aa102008-06-28 08:32:05 +1000187static int stripe_operations_active(struct stripe_head *sh)
188{
189 return sh->check_state || sh->reconstruct_state ||
190 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
191 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
192}
193
Arjan van de Ven858119e2006-01-14 13:20:43 -0800194static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200197 BUG_ON(!list_empty(&sh->lru));
198 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700200 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700202 blk_plug_device(conf->mddev->queue);
203 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700204 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700205 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700206 blk_plug_device(conf->mddev->queue);
207 } else {
NeilBrown72626682005-09-09 16:23:54 -0700208 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 md_wakeup_thread(conf->mddev->thread);
212 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000213 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
215 atomic_dec(&conf->preread_active_stripes);
216 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
217 md_wakeup_thread(conf->mddev->thread);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800220 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
221 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800223 if (conf->retry_read_aligned)
224 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 }
228}
NeilBrownd0dabf72009-03-31 14:39:38 +1100229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static void release_stripe(struct stripe_head *sh)
231{
232 raid5_conf_t *conf = sh->raid_conf;
233 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 spin_lock_irqsave(&conf->device_lock, flags);
236 __release_stripe(conf, sh);
237 spin_unlock_irqrestore(&conf->device_lock, flags);
238}
239
NeilBrownfccddba2006-01-06 00:20:33 -0800240static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Dan Williams45b42332007-07-09 11:56:43 -0700242 pr_debug("remove_hash(), stripe %llu\n",
243 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
NeilBrownfccddba2006-01-06 00:20:33 -0800245 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
NeilBrown16a53ec2006-06-26 00:27:38 -0700248static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
NeilBrownfccddba2006-01-06 00:20:33 -0800250 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Dan Williams45b42332007-07-09 11:56:43 -0700252 pr_debug("insert_hash(), stripe %llu\n",
253 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800256 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259
260/* find an idle stripe, make sure it is unhashed, and return it. */
261static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
262{
263 struct stripe_head *sh = NULL;
264 struct list_head *first;
265
266 CHECK_DEVLOCK();
267 if (list_empty(&conf->inactive_list))
268 goto out;
269 first = conf->inactive_list.next;
270 sh = list_entry(first, struct stripe_head, lru);
271 list_del_init(first);
272 remove_hash(sh);
273 atomic_inc(&conf->active_stripes);
274out:
275 return sh;
276}
277
278static void shrink_buffers(struct stripe_head *sh, int num)
279{
280 struct page *p;
281 int i;
282
283 for (i=0; i<num ; i++) {
284 p = sh->dev[i].page;
285 if (!p)
286 continue;
287 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800288 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290}
291
292static int grow_buffers(struct stripe_head *sh, int num)
293{
294 int i;
295
296 for (i=0; i<num; i++) {
297 struct page *page;
298
299 if (!(page = alloc_page(GFP_KERNEL))) {
300 return 1;
301 }
302 sh->dev[i].page = page;
303 }
304 return 0;
305}
306
NeilBrownd710e132008-10-13 11:55:12 +1100307static void raid5_build_block(struct stripe_head *sh, int i);
NeilBrown911d4ee2009-03-31 14:39:38 +1100308static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
309 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
NeilBrownb5663ba2009-03-31 14:39:38 +1100311static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800314 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200316 BUG_ON(atomic_read(&sh->count) != 0);
317 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000318 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700321 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 (unsigned long long)sh->sector);
323
324 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700325
NeilBrownb5663ba2009-03-31 14:39:38 +1100326 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100328 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 sh->state = 0;
330
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800331
332 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct r5dev *dev = &sh->dev[i];
334
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700337 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700339 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 test_bit(R5_LOCKED, &dev->flags));
341 BUG();
342 }
343 dev->flags = 0;
344 raid5_build_block(sh, i);
345 }
346 insert_hash(conf, sh);
347}
348
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800349static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800352 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700355 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800356 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800357 if (sh->sector == sector && sh->disks == disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700359 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return NULL;
361}
362
363static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200364static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
NeilBrownb5663ba2009-03-31 14:39:38 +1100366static struct stripe_head *
367get_active_stripe(raid5_conf_t *conf, sector_t sector,
368 int previous, int noblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct stripe_head *sh;
NeilBrownb5663ba2009-03-31 14:39:38 +1100371 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dan Williams45b42332007-07-09 11:56:43 -0700373 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 spin_lock_irq(&conf->device_lock);
376
377 do {
NeilBrown72626682005-09-09 16:23:54 -0700378 wait_event_lock_irq(conf->wait_for_stripe,
379 conf->quiesce == 0,
380 conf->device_lock, /* nothing */);
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800381 sh = __find_stripe(conf, sector, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!sh) {
383 if (!conf->inactive_blocked)
384 sh = get_free_stripe(conf);
385 if (noblock && sh == NULL)
386 break;
387 if (!sh) {
388 conf->inactive_blocked = 1;
389 wait_event_lock_irq(conf->wait_for_stripe,
390 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800391 (atomic_read(&conf->active_stripes)
392 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 || !conf->inactive_blocked),
394 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700395 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 );
397 conf->inactive_blocked = 0;
398 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100399 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 } else {
401 if (atomic_read(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200402 BUG_ON(!list_empty(&sh->lru));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 } else {
404 if (!test_bit(STRIPE_HANDLE, &sh->state))
405 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700406 if (list_empty(&sh->lru) &&
407 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700408 BUG();
409 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 }
412 } while (sh == NULL);
413
414 if (sh)
415 atomic_inc(&sh->count);
416
417 spin_unlock_irq(&conf->device_lock);
418 return sh;
419}
420
NeilBrown6712ecf2007-09-27 12:47:43 +0200421static void
422raid5_end_read_request(struct bio *bi, int error);
423static void
424raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700425
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000426static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700427{
428 raid5_conf_t *conf = sh->raid_conf;
429 int i, disks = sh->disks;
430
431 might_sleep();
432
433 for (i = disks; i--; ) {
434 int rw;
435 struct bio *bi;
436 mdk_rdev_t *rdev;
437 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
438 rw = WRITE;
439 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
440 rw = READ;
441 else
442 continue;
443
444 bi = &sh->dev[i].req;
445
446 bi->bi_rw = rw;
447 if (rw == WRITE)
448 bi->bi_end_io = raid5_end_write_request;
449 else
450 bi->bi_end_io = raid5_end_read_request;
451
452 rcu_read_lock();
453 rdev = rcu_dereference(conf->disks[i].rdev);
454 if (rdev && test_bit(Faulty, &rdev->flags))
455 rdev = NULL;
456 if (rdev)
457 atomic_inc(&rdev->nr_pending);
458 rcu_read_unlock();
459
460 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000461 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700462 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
463
Dan Williams2b7497f2008-06-28 08:31:52 +1000464 set_bit(STRIPE_IO_STARTED, &sh->state);
465
Dan Williams91c00922007-01-02 13:52:30 -0700466 bi->bi_bdev = rdev->bdev;
467 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700468 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700469 bi->bi_rw, i);
470 atomic_inc(&sh->count);
471 bi->bi_sector = sh->sector + rdev->data_offset;
472 bi->bi_flags = 1 << BIO_UPTODATE;
473 bi->bi_vcnt = 1;
474 bi->bi_max_vecs = 1;
475 bi->bi_idx = 0;
476 bi->bi_io_vec = &sh->dev[i].vec;
477 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
478 bi->bi_io_vec[0].bv_offset = 0;
479 bi->bi_size = STRIPE_SIZE;
480 bi->bi_next = NULL;
481 if (rw == WRITE &&
482 test_bit(R5_ReWrite, &sh->dev[i].flags))
483 atomic_add(STRIPE_SECTORS,
484 &rdev->corrected_errors);
485 generic_make_request(bi);
486 } else {
487 if (rw == WRITE)
488 set_bit(STRIPE_DEGRADED, &sh->state);
489 pr_debug("skip op %ld on disc %d for sector %llu\n",
490 bi->bi_rw, i, (unsigned long long)sh->sector);
491 clear_bit(R5_LOCKED, &sh->dev[i].flags);
492 set_bit(STRIPE_HANDLE, &sh->state);
493 }
494 }
495}
496
497static struct dma_async_tx_descriptor *
498async_copy_data(int frombio, struct bio *bio, struct page *page,
499 sector_t sector, struct dma_async_tx_descriptor *tx)
500{
501 struct bio_vec *bvl;
502 struct page *bio_page;
503 int i;
504 int page_offset;
505
506 if (bio->bi_sector >= sector)
507 page_offset = (signed)(bio->bi_sector - sector) * 512;
508 else
509 page_offset = (signed)(sector - bio->bi_sector) * -512;
510 bio_for_each_segment(bvl, bio, i) {
511 int len = bio_iovec_idx(bio, i)->bv_len;
512 int clen;
513 int b_offset = 0;
514
515 if (page_offset < 0) {
516 b_offset = -page_offset;
517 page_offset += b_offset;
518 len -= b_offset;
519 }
520
521 if (len > 0 && page_offset + len > STRIPE_SIZE)
522 clen = STRIPE_SIZE - page_offset;
523 else
524 clen = len;
525
526 if (clen > 0) {
527 b_offset += bio_iovec_idx(bio, i)->bv_offset;
528 bio_page = bio_iovec_idx(bio, i)->bv_page;
529 if (frombio)
530 tx = async_memcpy(page, bio_page, page_offset,
531 b_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700532 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700533 tx, NULL, NULL);
534 else
535 tx = async_memcpy(bio_page, page, b_offset,
536 page_offset, clen,
Dan Williamseb0645a2007-07-20 00:31:46 -0700537 ASYNC_TX_DEP_ACK,
Dan Williams91c00922007-01-02 13:52:30 -0700538 tx, NULL, NULL);
539 }
540 if (clen < len) /* hit end of page */
541 break;
542 page_offset += len;
543 }
544
545 return tx;
546}
547
548static void ops_complete_biofill(void *stripe_head_ref)
549{
550 struct stripe_head *sh = stripe_head_ref;
551 struct bio *return_bi = NULL;
552 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700553 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700554
Harvey Harrisone46b2722008-04-28 02:15:50 -0700555 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700556 (unsigned long long)sh->sector);
557
558 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000559 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700560 for (i = sh->disks; i--; ) {
561 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700562
563 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700564 /* and check if we need to reply to a read request,
565 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000566 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700567 */
568 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700569 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700570
Dan Williams91c00922007-01-02 13:52:30 -0700571 BUG_ON(!dev->read);
572 rbi = dev->read;
573 dev->read = NULL;
574 while (rbi && rbi->bi_sector <
575 dev->sector + STRIPE_SECTORS) {
576 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200577 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700578 rbi->bi_next = return_bi;
579 return_bi = rbi;
580 }
Dan Williams91c00922007-01-02 13:52:30 -0700581 rbi = rbi2;
582 }
583 }
584 }
Dan Williams83de75c2008-06-28 08:31:58 +1000585 spin_unlock_irq(&conf->device_lock);
586 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700587
588 return_io(return_bi);
589
Dan Williamse4d84902007-09-24 10:06:13 -0700590 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700591 release_stripe(sh);
592}
593
594static void ops_run_biofill(struct stripe_head *sh)
595{
596 struct dma_async_tx_descriptor *tx = NULL;
597 raid5_conf_t *conf = sh->raid_conf;
598 int i;
599
Harvey Harrisone46b2722008-04-28 02:15:50 -0700600 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700601 (unsigned long long)sh->sector);
602
603 for (i = sh->disks; i--; ) {
604 struct r5dev *dev = &sh->dev[i];
605 if (test_bit(R5_Wantfill, &dev->flags)) {
606 struct bio *rbi;
607 spin_lock_irq(&conf->device_lock);
608 dev->read = rbi = dev->toread;
609 dev->toread = NULL;
610 spin_unlock_irq(&conf->device_lock);
611 while (rbi && rbi->bi_sector <
612 dev->sector + STRIPE_SECTORS) {
613 tx = async_copy_data(0, rbi, dev->page,
614 dev->sector, tx);
615 rbi = r5_next_bio(rbi, dev->sector);
616 }
617 }
618 }
619
620 atomic_inc(&sh->count);
621 async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
622 ops_complete_biofill, sh);
623}
624
625static void ops_complete_compute5(void *stripe_head_ref)
626{
627 struct stripe_head *sh = stripe_head_ref;
628 int target = sh->ops.target;
629 struct r5dev *tgt = &sh->dev[target];
630
Harvey Harrisone46b2722008-04-28 02:15:50 -0700631 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700632 (unsigned long long)sh->sector);
633
634 set_bit(R5_UPTODATE, &tgt->flags);
635 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
636 clear_bit(R5_Wantcompute, &tgt->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +1000637 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
638 if (sh->check_state == check_state_compute_run)
639 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700640 set_bit(STRIPE_HANDLE, &sh->state);
641 release_stripe(sh);
642}
643
Dan Williams7b3a8712008-06-28 08:32:09 +1000644static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -0700645{
646 /* kernel stack size limits the total number of disks */
647 int disks = sh->disks;
648 struct page *xor_srcs[disks];
649 int target = sh->ops.target;
650 struct r5dev *tgt = &sh->dev[target];
651 struct page *xor_dest = tgt->page;
652 int count = 0;
653 struct dma_async_tx_descriptor *tx;
654 int i;
655
656 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700657 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700658 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
659
660 for (i = disks; i--; )
661 if (i != target)
662 xor_srcs[count++] = sh->dev[i].page;
663
664 atomic_inc(&sh->count);
665
666 if (unlikely(count == 1))
667 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
668 0, NULL, ops_complete_compute5, sh);
669 else
670 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
671 ASYNC_TX_XOR_ZERO_DST, NULL,
672 ops_complete_compute5, sh);
673
Dan Williams91c00922007-01-02 13:52:30 -0700674 return tx;
675}
676
677static void ops_complete_prexor(void *stripe_head_ref)
678{
679 struct stripe_head *sh = stripe_head_ref;
680
Harvey Harrisone46b2722008-04-28 02:15:50 -0700681 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700682 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700683}
684
685static struct dma_async_tx_descriptor *
686ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
687{
688 /* kernel stack size limits the total number of disks */
689 int disks = sh->disks;
690 struct page *xor_srcs[disks];
691 int count = 0, pd_idx = sh->pd_idx, i;
692
693 /* existing parity data subtracted */
694 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
695
Harvey Harrisone46b2722008-04-28 02:15:50 -0700696 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700697 (unsigned long long)sh->sector);
698
699 for (i = disks; i--; ) {
700 struct r5dev *dev = &sh->dev[i];
701 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000702 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700703 xor_srcs[count++] = dev->page;
704 }
705
706 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
707 ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
708 ops_complete_prexor, sh);
709
710 return tx;
711}
712
713static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000714ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700715{
716 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000717 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700718
Harvey Harrisone46b2722008-04-28 02:15:50 -0700719 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700720 (unsigned long long)sh->sector);
721
722 for (i = disks; i--; ) {
723 struct r5dev *dev = &sh->dev[i];
724 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700725
Dan Williamsd8ee0722008-06-28 08:32:06 +1000726 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700727 struct bio *wbi;
728
729 spin_lock(&sh->lock);
730 chosen = dev->towrite;
731 dev->towrite = NULL;
732 BUG_ON(dev->written);
733 wbi = dev->written = chosen;
734 spin_unlock(&sh->lock);
735
736 while (wbi && wbi->bi_sector <
737 dev->sector + STRIPE_SECTORS) {
738 tx = async_copy_data(1, wbi, dev->page,
739 dev->sector, tx);
740 wbi = r5_next_bio(wbi, dev->sector);
741 }
742 }
743 }
744
745 return tx;
746}
747
748static void ops_complete_postxor(void *stripe_head_ref)
749{
750 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700751 int disks = sh->disks, i, pd_idx = sh->pd_idx;
752
Harvey Harrisone46b2722008-04-28 02:15:50 -0700753 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700754 (unsigned long long)sh->sector);
755
756 for (i = disks; i--; ) {
757 struct r5dev *dev = &sh->dev[i];
758 if (dev->written || i == pd_idx)
759 set_bit(R5_UPTODATE, &dev->flags);
760 }
761
Dan Williamsd8ee0722008-06-28 08:32:06 +1000762 if (sh->reconstruct_state == reconstruct_state_drain_run)
763 sh->reconstruct_state = reconstruct_state_drain_result;
764 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
765 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
766 else {
767 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
768 sh->reconstruct_state = reconstruct_state_result;
769 }
Dan Williams91c00922007-01-02 13:52:30 -0700770
771 set_bit(STRIPE_HANDLE, &sh->state);
772 release_stripe(sh);
773}
774
775static void
Dan Williamsd8ee0722008-06-28 08:32:06 +1000776ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700777{
778 /* kernel stack size limits the total number of disks */
779 int disks = sh->disks;
780 struct page *xor_srcs[disks];
781
782 int count = 0, pd_idx = sh->pd_idx, i;
783 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000784 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700785 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -0700786
Harvey Harrisone46b2722008-04-28 02:15:50 -0700787 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700788 (unsigned long long)sh->sector);
789
790 /* check if prexor is active which means only process blocks
791 * that are part of a read-modify-write (written)
792 */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000793 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
794 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700795 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
796 for (i = disks; i--; ) {
797 struct r5dev *dev = &sh->dev[i];
798 if (dev->written)
799 xor_srcs[count++] = dev->page;
800 }
801 } else {
802 xor_dest = sh->dev[pd_idx].page;
803 for (i = disks; i--; ) {
804 struct r5dev *dev = &sh->dev[i];
805 if (i != pd_idx)
806 xor_srcs[count++] = dev->page;
807 }
808 }
809
Dan Williams91c00922007-01-02 13:52:30 -0700810 /* 1/ if we prexor'd then the dest is reused as a source
811 * 2/ if we did not prexor then we are redoing the parity
812 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
813 * for the synchronous xor case
814 */
815 flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
816 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
817
818 atomic_inc(&sh->count);
819
820 if (unlikely(count == 1)) {
821 flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
822 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000823 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700824 } else
825 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsd8ee0722008-06-28 08:32:06 +1000826 flags, tx, ops_complete_postxor, sh);
Dan Williams91c00922007-01-02 13:52:30 -0700827}
828
829static void ops_complete_check(void *stripe_head_ref)
830{
831 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700832
Harvey Harrisone46b2722008-04-28 02:15:50 -0700833 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700834 (unsigned long long)sh->sector);
835
Dan Williamsecc65c92008-06-28 08:31:57 +1000836 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -0700837 set_bit(STRIPE_HANDLE, &sh->state);
838 release_stripe(sh);
839}
840
841static void ops_run_check(struct stripe_head *sh)
842{
843 /* kernel stack size limits the total number of disks */
844 int disks = sh->disks;
845 struct page *xor_srcs[disks];
846 struct dma_async_tx_descriptor *tx;
847
848 int count = 0, pd_idx = sh->pd_idx, i;
849 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
850
Harvey Harrisone46b2722008-04-28 02:15:50 -0700851 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700852 (unsigned long long)sh->sector);
853
854 for (i = disks; i--; ) {
855 struct r5dev *dev = &sh->dev[i];
856 if (i != pd_idx)
857 xor_srcs[count++] = dev->page;
858 }
859
860 tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
861 &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
862
Dan Williams91c00922007-01-02 13:52:30 -0700863 atomic_inc(&sh->count);
864 tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
865 ops_complete_check, sh);
866}
867
Dan Williams600aa102008-06-28 08:32:05 +1000868static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -0700869{
870 int overlap_clear = 0, i, disks = sh->disks;
871 struct dma_async_tx_descriptor *tx = NULL;
872
Dan Williams83de75c2008-06-28 08:31:58 +1000873 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -0700874 ops_run_biofill(sh);
875 overlap_clear++;
876 }
877
Dan Williams7b3a8712008-06-28 08:32:09 +1000878 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
879 tx = ops_run_compute5(sh);
880 /* terminate the chain if postxor is not set to be run */
881 if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
882 async_tx_ack(tx);
883 }
Dan Williams91c00922007-01-02 13:52:30 -0700884
Dan Williams600aa102008-06-28 08:32:05 +1000885 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700886 tx = ops_run_prexor(sh, tx);
887
Dan Williams600aa102008-06-28 08:32:05 +1000888 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +1000889 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700890 overlap_clear++;
891 }
892
Dan Williams600aa102008-06-28 08:32:05 +1000893 if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
Dan Williamsd8ee0722008-06-28 08:32:06 +1000894 ops_run_postxor(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -0700895
Dan Williamsecc65c92008-06-28 08:31:57 +1000896 if (test_bit(STRIPE_OP_CHECK, &ops_request))
Dan Williams91c00922007-01-02 13:52:30 -0700897 ops_run_check(sh);
898
Dan Williams91c00922007-01-02 13:52:30 -0700899 if (overlap_clear)
900 for (i = disks; i--; ) {
901 struct r5dev *dev = &sh->dev[i];
902 if (test_and_clear_bit(R5_Overlap, &dev->flags))
903 wake_up(&sh->raid_conf->wait_for_overlap);
904 }
905}
906
NeilBrown3f294f42005-11-08 21:39:25 -0800907static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -0800910 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
911 if (!sh)
912 return 0;
913 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
914 sh->raid_conf = conf;
915 spin_lock_init(&sh->lock);
916
917 if (grow_buffers(sh, conf->raid_disks)) {
918 shrink_buffers(sh, conf->raid_disks);
919 kmem_cache_free(conf->slab_cache, sh);
920 return 0;
921 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800922 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -0800923 /* we just created an active stripe so... */
924 atomic_set(&sh->count, 1);
925 atomic_inc(&conf->active_stripes);
926 INIT_LIST_HEAD(&sh->lru);
927 release_stripe(sh);
928 return 1;
929}
930
931static int grow_stripes(raid5_conf_t *conf, int num)
932{
Christoph Lametere18b8902006-12-06 20:33:20 -0800933 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int devs = conf->raid_disks;
935
NeilBrown42b9beb2007-05-09 02:35:37 -0700936 sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
937 sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -0800938 conf->active_name = 0;
939 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900941 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!sc)
943 return 1;
944 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800945 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -0700946 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -0800947 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950}
NeilBrown29269552006-03-27 01:18:10 -0800951
952#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrownad01c9e2006-03-27 01:18:07 -0800953static int resize_stripes(raid5_conf_t *conf, int newsize)
954{
955 /* Make all the stripes able to hold 'newsize' devices.
956 * New slots in each stripe get 'page' set to a new page.
957 *
958 * This happens in stages:
959 * 1/ create a new kmem_cache and allocate the required number of
960 * stripe_heads.
961 * 2/ gather all the old stripe_heads and tranfer the pages across
962 * to the new stripe_heads. This will have the side effect of
963 * freezing the array as once all stripe_heads have been collected,
964 * no IO will be possible. Old stripe heads are freed once their
965 * pages have been transferred over, and the old kmem_cache is
966 * freed when all stripes are done.
967 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
968 * we simple return a failre status - no need to clean anything up.
969 * 4/ allocate new pages for the new slots in the new stripe_heads.
970 * If this fails, we don't bother trying the shrink the
971 * stripe_heads down again, we just leave them as they are.
972 * As each stripe_head is processed the new one is released into
973 * active service.
974 *
975 * Once step2 is started, we cannot afford to wait for a write,
976 * so we use GFP_NOIO allocations.
977 */
978 struct stripe_head *osh, *nsh;
979 LIST_HEAD(newstripes);
980 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -0700981 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -0800982 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -0800983 int i;
984
985 if (newsize <= conf->pool_size)
986 return 0; /* never bother to shrink */
987
Dan Williamsb5470dc2008-06-27 21:44:04 -0700988 err = md_allow_write(conf->mddev);
989 if (err)
990 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -0800991
NeilBrownad01c9e2006-03-27 01:18:07 -0800992 /* Step 1 */
993 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
994 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +0900995 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -0800996 if (!sc)
997 return -ENOMEM;
998
999 for (i = conf->max_nr_stripes; i; i--) {
1000 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1001 if (!nsh)
1002 break;
1003
1004 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1005
1006 nsh->raid_conf = conf;
1007 spin_lock_init(&nsh->lock);
1008
1009 list_add(&nsh->lru, &newstripes);
1010 }
1011 if (i) {
1012 /* didn't get enough, give up */
1013 while (!list_empty(&newstripes)) {
1014 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1015 list_del(&nsh->lru);
1016 kmem_cache_free(sc, nsh);
1017 }
1018 kmem_cache_destroy(sc);
1019 return -ENOMEM;
1020 }
1021 /* Step 2 - Must use GFP_NOIO now.
1022 * OK, we have enough stripes, start collecting inactive
1023 * stripes and copying them over
1024 */
1025 list_for_each_entry(nsh, &newstripes, lru) {
1026 spin_lock_irq(&conf->device_lock);
1027 wait_event_lock_irq(conf->wait_for_stripe,
1028 !list_empty(&conf->inactive_list),
1029 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001030 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001031 );
1032 osh = get_free_stripe(conf);
1033 spin_unlock_irq(&conf->device_lock);
1034 atomic_set(&nsh->count, 1);
1035 for(i=0; i<conf->pool_size; i++)
1036 nsh->dev[i].page = osh->dev[i].page;
1037 for( ; i<newsize; i++)
1038 nsh->dev[i].page = NULL;
1039 kmem_cache_free(conf->slab_cache, osh);
1040 }
1041 kmem_cache_destroy(conf->slab_cache);
1042
1043 /* Step 3.
1044 * At this point, we are holding all the stripes so the array
1045 * is completely stalled, so now is a good time to resize
1046 * conf->disks.
1047 */
1048 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1049 if (ndisks) {
1050 for (i=0; i<conf->raid_disks; i++)
1051 ndisks[i] = conf->disks[i];
1052 kfree(conf->disks);
1053 conf->disks = ndisks;
1054 } else
1055 err = -ENOMEM;
1056
1057 /* Step 4, return new stripes to service */
1058 while(!list_empty(&newstripes)) {
1059 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1060 list_del_init(&nsh->lru);
1061 for (i=conf->raid_disks; i < newsize; i++)
1062 if (nsh->dev[i].page == NULL) {
1063 struct page *p = alloc_page(GFP_NOIO);
1064 nsh->dev[i].page = p;
1065 if (!p)
1066 err = -ENOMEM;
1067 }
1068 release_stripe(nsh);
1069 }
1070 /* critical section pass, GFP_NOIO no longer needed */
1071
1072 conf->slab_cache = sc;
1073 conf->active_name = 1-conf->active_name;
1074 conf->pool_size = newsize;
1075 return err;
1076}
NeilBrown29269552006-03-27 01:18:10 -08001077#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
NeilBrown3f294f42005-11-08 21:39:25 -08001079static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct stripe_head *sh;
1082
NeilBrown3f294f42005-11-08 21:39:25 -08001083 spin_lock_irq(&conf->device_lock);
1084 sh = get_free_stripe(conf);
1085 spin_unlock_irq(&conf->device_lock);
1086 if (!sh)
1087 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001088 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001089 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001090 kmem_cache_free(conf->slab_cache, sh);
1091 atomic_dec(&conf->active_stripes);
1092 return 1;
1093}
1094
1095static void shrink_stripes(raid5_conf_t *conf)
1096{
1097 while (drop_one_stripe(conf))
1098 ;
1099
NeilBrown29fc7e32006-02-03 03:03:41 -08001100 if (conf->slab_cache)
1101 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 conf->slab_cache = NULL;
1103}
1104
NeilBrown6712ecf2007-09-27 12:47:43 +02001105static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
NeilBrown99c0fb52009-03-31 14:39:38 +11001107 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001109 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001111 char b[BDEVNAME_SIZE];
1112 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 for (i=0 ; i<disks; i++)
1116 if (bi == &sh->dev[i].req)
1117 break;
1118
Dan Williams45b42332007-07-09 11:56:43 -07001119 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1120 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 uptodate);
1122 if (i == disks) {
1123 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001129 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001130 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001131 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1132 " (%lu sectors at %llu on %s)\n",
1133 mdname(conf->mddev), STRIPE_SECTORS,
1134 (unsigned long long)(sh->sector
1135 + rdev->data_offset),
1136 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001137 clear_bit(R5_ReadError, &sh->dev[i].flags);
1138 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1139 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001140 if (atomic_read(&conf->disks[i].rdev->read_errors))
1141 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001143 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001144 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001145 rdev = conf->disks[i].rdev;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001148 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001149 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001150 printk_rl(KERN_WARNING
1151 "raid5:%s: read error not correctable "
1152 "(sector %llu on %s).\n",
1153 mdname(conf->mddev),
1154 (unsigned long long)(sh->sector
1155 + rdev->data_offset),
1156 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001157 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001158 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001159 printk_rl(KERN_WARNING
1160 "raid5:%s: read error NOT corrected!! "
1161 "(sector %llu on %s).\n",
1162 mdname(conf->mddev),
1163 (unsigned long long)(sh->sector
1164 + rdev->data_offset),
1165 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001166 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001167 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001168 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001169 "raid5:%s: Too many read errors, failing device %s.\n",
1170 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001171 else
1172 retry = 1;
1173 if (retry)
1174 set_bit(R5_ReadError, &sh->dev[i].flags);
1175 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001176 clear_bit(R5_ReadError, &sh->dev[i].flags);
1177 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001178 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1183 set_bit(STRIPE_HANDLE, &sh->state);
1184 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
NeilBrownd710e132008-10-13 11:55:12 +11001187static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
NeilBrown99c0fb52009-03-31 14:39:38 +11001189 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001191 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 for (i=0 ; i<disks; i++)
1195 if (bi == &sh->dev[i].req)
1196 break;
1197
Dan Williams45b42332007-07-09 11:56:43 -07001198 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1200 uptodate);
1201 if (i == disks) {
1202 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (!uptodate)
1207 md_error(conf->mddev, conf->disks[i].rdev);
1208
1209 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1210
1211 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1212 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001213 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216
1217static sector_t compute_blocknr(struct stripe_head *sh, int i);
1218
NeilBrownd710e132008-10-13 11:55:12 +11001219static void raid5_build_block(struct stripe_head *sh, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct r5dev *dev = &sh->dev[i];
1222
1223 bio_init(&dev->req);
1224 dev->req.bi_io_vec = &dev->vec;
1225 dev->req.bi_vcnt++;
1226 dev->req.bi_max_vecs++;
1227 dev->vec.bv_page = dev->page;
1228 dev->vec.bv_len = STRIPE_SIZE;
1229 dev->vec.bv_offset = 0;
1230
1231 dev->req.bi_sector = sh->sector;
1232 dev->req.bi_private = sh;
1233
1234 dev->flags = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001235 dev->sector = compute_blocknr(sh, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1239{
1240 char b[BDEVNAME_SIZE];
1241 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001242 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
NeilBrownb2d444d2005-11-08 21:39:31 -08001244 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001245 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001246 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1247 unsigned long flags;
1248 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001250 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * if recovery was running, make sure it aborts.
1253 */
NeilBrowndfc70642008-05-23 13:04:39 -07001254 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001256 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001257 printk(KERN_ALERT
1258 "raid5: Disk failure on %s, disabling device.\n"
1259 "raid5: Operation continuing on %d devices.\n",
1260 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264/*
1265 * Input: a 'big' sector number,
1266 * Output: index of the data and parity disk, and the sector # in them.
1267 */
NeilBrown112bf892009-03-31 14:39:38 +11001268static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001269 int previous, int *dd_idx,
1270 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 long stripe;
1273 unsigned long chunk_number;
1274 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001275 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001276 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sector_t new_sector;
1278 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrown112bf892009-03-31 14:39:38 +11001279 int raid_disks = previous ? conf->previous_raid_disks
1280 : conf->raid_disks;
1281 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /* First compute the information on this sector */
1284
1285 /*
1286 * Compute the chunk number and the sector offset inside the chunk
1287 */
1288 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1289 chunk_number = r_sector;
1290 BUG_ON(r_sector != chunk_number);
1291
1292 /*
1293 * Compute the stripe number
1294 */
1295 stripe = chunk_number / data_disks;
1296
1297 /*
1298 * Compute the data disk and parity disk indexes inside the stripe
1299 */
1300 *dd_idx = chunk_number % data_disks;
1301
1302 /*
1303 * Select the parity disk based on the user selected algorithm.
1304 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001305 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001306 switch(conf->level) {
1307 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001308 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001309 break;
1310 case 5:
1311 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001313 pd_idx = data_disks - stripe % raid_disks;
1314 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 (*dd_idx)++;
1316 break;
1317 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001318 pd_idx = stripe % raid_disks;
1319 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 (*dd_idx)++;
1321 break;
1322 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001323 pd_idx = data_disks - stripe % raid_disks;
1324 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 break;
1326 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001327 pd_idx = stripe % raid_disks;
1328 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001330 case ALGORITHM_PARITY_0:
1331 pd_idx = 0;
1332 (*dd_idx)++;
1333 break;
1334 case ALGORITHM_PARITY_N:
1335 pd_idx = data_disks;
1336 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001338 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001340 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001341 }
1342 break;
1343 case 6:
1344
NeilBrown16a53ec2006-06-26 00:27:38 -07001345 switch (conf->algorithm) {
1346 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001347 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1348 qd_idx = pd_idx + 1;
1349 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001350 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001351 qd_idx = 0;
1352 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001353 (*dd_idx) += 2; /* D D P Q D */
1354 break;
1355 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001356 pd_idx = stripe % raid_disks;
1357 qd_idx = pd_idx + 1;
1358 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001359 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001360 qd_idx = 0;
1361 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001362 (*dd_idx) += 2; /* D D P Q D */
1363 break;
1364 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001365 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1366 qd_idx = (pd_idx + 1) % raid_disks;
1367 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001368 break;
1369 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001370 pd_idx = stripe % raid_disks;
1371 qd_idx = (pd_idx + 1) % raid_disks;
1372 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001373 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001374
1375 case ALGORITHM_PARITY_0:
1376 pd_idx = 0;
1377 qd_idx = 1;
1378 (*dd_idx) += 2;
1379 break;
1380 case ALGORITHM_PARITY_N:
1381 pd_idx = data_disks;
1382 qd_idx = data_disks + 1;
1383 break;
1384
1385 case ALGORITHM_ROTATING_ZERO_RESTART:
1386 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1387 * of blocks for computing Q is different.
1388 */
1389 pd_idx = stripe % raid_disks;
1390 qd_idx = pd_idx + 1;
1391 if (pd_idx == raid_disks-1) {
1392 (*dd_idx)++; /* Q D D D P */
1393 qd_idx = 0;
1394 } else if (*dd_idx >= pd_idx)
1395 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001396 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001397 break;
1398
1399 case ALGORITHM_ROTATING_N_RESTART:
1400 /* Same a left_asymmetric, by first stripe is
1401 * D D D P Q rather than
1402 * Q D D D P
1403 */
1404 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1405 qd_idx = pd_idx + 1;
1406 if (pd_idx == raid_disks-1) {
1407 (*dd_idx)++; /* Q D D D P */
1408 qd_idx = 0;
1409 } else if (*dd_idx >= pd_idx)
1410 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001411 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001412 break;
1413
1414 case ALGORITHM_ROTATING_N_CONTINUE:
1415 /* Same as left_symmetric but Q is before P */
1416 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1417 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1418 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001419 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001420 break;
1421
1422 case ALGORITHM_LEFT_ASYMMETRIC_6:
1423 /* RAID5 left_asymmetric, with Q on last device */
1424 pd_idx = data_disks - stripe % (raid_disks-1);
1425 if (*dd_idx >= pd_idx)
1426 (*dd_idx)++;
1427 qd_idx = raid_disks - 1;
1428 break;
1429
1430 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1431 pd_idx = stripe % (raid_disks-1);
1432 if (*dd_idx >= pd_idx)
1433 (*dd_idx)++;
1434 qd_idx = raid_disks - 1;
1435 break;
1436
1437 case ALGORITHM_LEFT_SYMMETRIC_6:
1438 pd_idx = data_disks - stripe % (raid_disks-1);
1439 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1440 qd_idx = raid_disks - 1;
1441 break;
1442
1443 case ALGORITHM_RIGHT_SYMMETRIC_6:
1444 pd_idx = stripe % (raid_disks-1);
1445 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1446 qd_idx = raid_disks - 1;
1447 break;
1448
1449 case ALGORITHM_PARITY_0_6:
1450 pd_idx = 0;
1451 (*dd_idx)++;
1452 qd_idx = raid_disks - 1;
1453 break;
1454
1455
NeilBrown16a53ec2006-06-26 00:27:38 -07001456 default:
NeilBrownd710e132008-10-13 11:55:12 +11001457 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1458 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001459 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001460 }
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
NeilBrown911d4ee2009-03-31 14:39:38 +11001464 if (sh) {
1465 sh->pd_idx = pd_idx;
1466 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001467 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /*
1470 * Finally, compute the new sector number
1471 */
1472 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1473 return new_sector;
1474}
1475
1476
1477static sector_t compute_blocknr(struct stripe_head *sh, int i)
1478{
1479 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001480 int raid_disks = sh->disks;
1481 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 sector_t new_sector = sh->sector, check;
1483 int sectors_per_chunk = conf->chunk_size >> 9;
1484 sector_t stripe;
1485 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001486 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001488 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
NeilBrown16a53ec2006-06-26 00:27:38 -07001490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1492 stripe = new_sector;
1493 BUG_ON(new_sector != stripe);
1494
NeilBrown16a53ec2006-06-26 00:27:38 -07001495 if (i == sh->pd_idx)
1496 return 0;
1497 switch(conf->level) {
1498 case 4: break;
1499 case 5:
1500 switch (conf->algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 case ALGORITHM_LEFT_ASYMMETRIC:
1502 case ALGORITHM_RIGHT_ASYMMETRIC:
1503 if (i > sh->pd_idx)
1504 i--;
1505 break;
1506 case ALGORITHM_LEFT_SYMMETRIC:
1507 case ALGORITHM_RIGHT_SYMMETRIC:
1508 if (i < sh->pd_idx)
1509 i += raid_disks;
1510 i -= (sh->pd_idx + 1);
1511 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001512 case ALGORITHM_PARITY_0:
1513 i -= 1;
1514 break;
1515 case ALGORITHM_PARITY_N:
1516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001518 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001519 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001520 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001521 }
1522 break;
1523 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001524 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001525 return 0; /* It is the Q disk */
1526 switch (conf->algorithm) {
1527 case ALGORITHM_LEFT_ASYMMETRIC:
1528 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001529 case ALGORITHM_ROTATING_ZERO_RESTART:
1530 case ALGORITHM_ROTATING_N_RESTART:
1531 if (sh->pd_idx == raid_disks-1)
1532 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001533 else if (i > sh->pd_idx)
1534 i -= 2; /* D D P Q D */
1535 break;
1536 case ALGORITHM_LEFT_SYMMETRIC:
1537 case ALGORITHM_RIGHT_SYMMETRIC:
1538 if (sh->pd_idx == raid_disks-1)
1539 i--; /* Q D D D P */
1540 else {
1541 /* D D P Q D */
1542 if (i < sh->pd_idx)
1543 i += raid_disks;
1544 i -= (sh->pd_idx + 2);
1545 }
1546 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001547 case ALGORITHM_PARITY_0:
1548 i -= 2;
1549 break;
1550 case ALGORITHM_PARITY_N:
1551 break;
1552 case ALGORITHM_ROTATING_N_CONTINUE:
1553 if (sh->pd_idx == 0)
1554 i--; /* P D D D Q */
1555 else if (i > sh->pd_idx)
1556 i -= 2; /* D D Q P D */
1557 break;
1558 case ALGORITHM_LEFT_ASYMMETRIC_6:
1559 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1560 if (i > sh->pd_idx)
1561 i--;
1562 break;
1563 case ALGORITHM_LEFT_SYMMETRIC_6:
1564 case ALGORITHM_RIGHT_SYMMETRIC_6:
1565 if (i < sh->pd_idx)
1566 i += data_disks + 1;
1567 i -= (sh->pd_idx + 1);
1568 break;
1569 case ALGORITHM_PARITY_0_6:
1570 i -= 1;
1571 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001572 default:
NeilBrownd710e132008-10-13 11:55:12 +11001573 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
1574 conf->algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001575 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001576 }
1577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579
1580 chunk_number = stripe * data_disks + i;
1581 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1582
NeilBrown112bf892009-03-31 14:39:38 +11001583 check = raid5_compute_sector(conf, r_sector,
1584 (raid_disks != conf->raid_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11001585 &dummy1, &sh2);
1586 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1587 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001588 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return 0;
1590 }
1591 return r_sector;
1592}
1593
1594
1595
1596/*
NeilBrown16a53ec2006-06-26 00:27:38 -07001597 * Copy data between a page in the stripe cache, and one or more bion
1598 * The page could align with the middle of the bio, or there could be
1599 * several bion, each with several bio_vecs, which cover part of the page
1600 * Multiple bion are linked together on bi_next. There may be extras
1601 * at the end of this list. We ignore them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
1603static void copy_data(int frombio, struct bio *bio,
1604 struct page *page,
1605 sector_t sector)
1606{
1607 char *pa = page_address(page);
1608 struct bio_vec *bvl;
1609 int i;
1610 int page_offset;
1611
1612 if (bio->bi_sector >= sector)
1613 page_offset = (signed)(bio->bi_sector - sector) * 512;
1614 else
1615 page_offset = (signed)(sector - bio->bi_sector) * -512;
1616 bio_for_each_segment(bvl, bio, i) {
1617 int len = bio_iovec_idx(bio,i)->bv_len;
1618 int clen;
1619 int b_offset = 0;
1620
1621 if (page_offset < 0) {
1622 b_offset = -page_offset;
1623 page_offset += b_offset;
1624 len -= b_offset;
1625 }
1626
1627 if (len > 0 && page_offset + len > STRIPE_SIZE)
1628 clen = STRIPE_SIZE - page_offset;
1629 else clen = len;
NeilBrown16a53ec2006-06-26 00:27:38 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (clen > 0) {
1632 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
1633 if (frombio)
1634 memcpy(pa+page_offset, ba+b_offset, clen);
1635 else
1636 memcpy(ba+b_offset, pa+page_offset, clen);
1637 __bio_kunmap_atomic(ba, KM_USER0);
1638 }
1639 if (clen < len) /* hit end of page */
1640 break;
1641 page_offset += len;
1642 }
1643}
1644
Dan Williams9bc89cd2007-01-02 11:10:44 -07001645#define check_xor() do { \
1646 if (count == MAX_XOR_BLOCKS) { \
1647 xor_blocks(count, STRIPE_SIZE, dest, ptr);\
1648 count = 0; \
1649 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 } while(0)
1651
NeilBrown16a53ec2006-06-26 00:27:38 -07001652static void compute_parity6(struct stripe_head *sh, int method)
1653{
NeilBrownbff61972009-03-31 14:33:13 +11001654 raid5_conf_t *conf = sh->raid_conf;
NeilBrownd0dabf72009-03-31 14:39:38 +11001655 int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
NeilBrown67cc2b82009-03-31 14:39:38 +11001656 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
NeilBrown16a53ec2006-06-26 00:27:38 -07001657 struct bio *chosen;
1658 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001659 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001660
NeilBrownd0dabf72009-03-31 14:39:38 +11001661 pd_idx = sh->pd_idx;
1662 qd_idx = sh->qd_idx;
1663 d0_idx = raid6_d0(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07001664
Dan Williams45b42332007-07-09 11:56:43 -07001665 pr_debug("compute_parity, stripe %llu, method %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001666 (unsigned long long)sh->sector, method);
1667
1668 switch(method) {
1669 case READ_MODIFY_WRITE:
1670 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1671 case RECONSTRUCT_WRITE:
1672 for (i= disks; i-- ;)
1673 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1674 chosen = sh->dev[i].towrite;
1675 sh->dev[i].towrite = NULL;
1676
1677 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1678 wake_up(&conf->wait_for_overlap);
1679
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02001680 BUG_ON(sh->dev[i].written);
NeilBrown16a53ec2006-06-26 00:27:38 -07001681 sh->dev[i].written = chosen;
1682 }
1683 break;
1684 case CHECK_PARITY:
1685 BUG(); /* Not implemented yet */
1686 }
1687
1688 for (i = disks; i--;)
1689 if (sh->dev[i].written) {
1690 sector_t sector = sh->dev[i].sector;
1691 struct bio *wbi = sh->dev[i].written;
1692 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1693 copy_data(1, wbi, sh->dev[i].page, sector);
1694 wbi = r5_next_bio(wbi, sector);
1695 }
1696
1697 set_bit(R5_LOCKED, &sh->dev[i].flags);
1698 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1699 }
1700
NeilBrownd0dabf72009-03-31 14:39:38 +11001701 /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
NeilBrown67cc2b82009-03-31 14:39:38 +11001702
1703 for (i = 0; i < disks; i++)
1704 ptrs[i] = (void *)raid6_empty_zero_page;
1705
NeilBrownd0dabf72009-03-31 14:39:38 +11001706 count = 0;
1707 i = d0_idx;
1708 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001709 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1710
NeilBrownd0dabf72009-03-31 14:39:38 +11001711 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001712 if (slot < syndrome_disks &&
NeilBrownd0dabf72009-03-31 14:39:38 +11001713 !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
1714 printk(KERN_ERR "block %d/%d not uptodate "
1715 "on parity calc\n", i, count);
1716 BUG();
1717 }
NeilBrown67cc2b82009-03-31 14:39:38 +11001718
NeilBrownd0dabf72009-03-31 14:39:38 +11001719 i = raid6_next_disk(i, disks);
1720 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001721 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001722
NeilBrown67cc2b82009-03-31 14:39:38 +11001723 raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001724
1725 switch(method) {
1726 case RECONSTRUCT_WRITE:
1727 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1728 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1729 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1730 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1731 break;
1732 case UPDATE_PARITY:
1733 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1734 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1735 break;
1736 }
1737}
1738
1739
1740/* Compute one missing block */
1741static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1742{
NeilBrownf4168852007-02-28 20:11:53 -08001743 int i, count, disks = sh->disks;
Dan Williams9bc89cd2007-01-02 11:10:44 -07001744 void *ptr[MAX_XOR_BLOCKS], *dest, *p;
NeilBrownd0dabf72009-03-31 14:39:38 +11001745 int qd_idx = sh->qd_idx;
NeilBrown16a53ec2006-06-26 00:27:38 -07001746
Dan Williams45b42332007-07-09 11:56:43 -07001747 pr_debug("compute_block_1, stripe %llu, idx %d\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07001748 (unsigned long long)sh->sector, dd_idx);
1749
1750 if ( dd_idx == qd_idx ) {
1751 /* We're actually computing the Q drive */
1752 compute_parity6(sh, UPDATE_PARITY);
1753 } else {
Dan Williams9bc89cd2007-01-02 11:10:44 -07001754 dest = page_address(sh->dev[dd_idx].page);
1755 if (!nozero) memset(dest, 0, STRIPE_SIZE);
1756 count = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001757 for (i = disks ; i--; ) {
1758 if (i == dd_idx || i == qd_idx)
1759 continue;
1760 p = page_address(sh->dev[i].page);
1761 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1762 ptr[count++] = p;
1763 else
1764 printk("compute_block() %d, stripe %llu, %d"
1765 " not present\n", dd_idx,
1766 (unsigned long long)sh->sector, i);
1767
1768 check_xor();
1769 }
Dan Williams9bc89cd2007-01-02 11:10:44 -07001770 if (count)
1771 xor_blocks(count, STRIPE_SIZE, dest, ptr);
NeilBrown16a53ec2006-06-26 00:27:38 -07001772 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1773 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1774 }
1775}
1776
1777/* Compute two missing blocks */
1778static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1779{
NeilBrownf4168852007-02-28 20:11:53 -08001780 int i, count, disks = sh->disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001781 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
NeilBrownd0dabf72009-03-31 14:39:38 +11001782 int d0_idx = raid6_d0(sh);
1783 int faila = -1, failb = -1;
1784 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
NeilBrown67cc2b82009-03-31 14:39:38 +11001785 void *ptrs[syndrome_disks+2];
NeilBrown16a53ec2006-06-26 00:27:38 -07001786
NeilBrown67cc2b82009-03-31 14:39:38 +11001787 for (i = 0; i < disks ; i++)
1788 ptrs[i] = (void *)raid6_empty_zero_page;
NeilBrownd0dabf72009-03-31 14:39:38 +11001789 count = 0;
1790 i = d0_idx;
1791 do {
NeilBrown67cc2b82009-03-31 14:39:38 +11001792 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1793
NeilBrownd0dabf72009-03-31 14:39:38 +11001794 ptrs[slot] = page_address(sh->dev[i].page);
NeilBrown67cc2b82009-03-31 14:39:38 +11001795
NeilBrownd0dabf72009-03-31 14:39:38 +11001796 if (i == dd_idx1)
1797 faila = slot;
1798 if (i == dd_idx2)
1799 failb = slot;
1800 i = raid6_next_disk(i, disks);
1801 } while (i != d0_idx);
NeilBrown67cc2b82009-03-31 14:39:38 +11001802 BUG_ON(count != syndrome_disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07001803
1804 BUG_ON(faila == failb);
1805 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1806
Dan Williams45b42332007-07-09 11:56:43 -07001807 pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
NeilBrownd0dabf72009-03-31 14:39:38 +11001808 (unsigned long long)sh->sector, dd_idx1, dd_idx2,
1809 faila, failb);
NeilBrown16a53ec2006-06-26 00:27:38 -07001810
NeilBrown67cc2b82009-03-31 14:39:38 +11001811 if (failb == syndrome_disks+1) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001812 /* Q disk is one of the missing disks */
NeilBrown67cc2b82009-03-31 14:39:38 +11001813 if (faila == syndrome_disks) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001814 /* Missing P+Q, just recompute */
1815 compute_parity6(sh, UPDATE_PARITY);
1816 return;
1817 } else {
1818 /* We're missing D+Q; recompute D from P */
NeilBrownd0dabf72009-03-31 14:39:38 +11001819 compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
1820 dd_idx2 : dd_idx1),
1821 0);
NeilBrown16a53ec2006-06-26 00:27:38 -07001822 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1823 return;
1824 }
1825 }
1826
NeilBrownd0dabf72009-03-31 14:39:38 +11001827 /* We're missing D+P or D+D; */
NeilBrown67cc2b82009-03-31 14:39:38 +11001828 if (failb == syndrome_disks) {
NeilBrownd0dabf72009-03-31 14:39:38 +11001829 /* We're missing D+P. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001830 raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
NeilBrownd0dabf72009-03-31 14:39:38 +11001831 } else {
1832 /* We're missing D+D. */
NeilBrown67cc2b82009-03-31 14:39:38 +11001833 raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
1834 ptrs);
NeilBrown16a53ec2006-06-26 00:27:38 -07001835 }
NeilBrownd0dabf72009-03-31 14:39:38 +11001836
1837 /* Both the above update both missing blocks */
1838 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1839 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07001840}
1841
Dan Williams600aa102008-06-28 08:32:05 +10001842static void
Dan Williams1fe797e2008-06-28 09:16:30 +10001843schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001844 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001845{
1846 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001847
Dan Williamse33129d2007-01-02 13:52:30 -07001848 if (rcw) {
1849 /* if we are not expanding this is a proper write request, and
1850 * there will be bios with new data to be drained into the
1851 * stripe cache
1852 */
1853 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001854 sh->reconstruct_state = reconstruct_state_drain_run;
1855 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1856 } else
1857 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001858
Dan Williams600aa102008-06-28 08:32:05 +10001859 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001860
1861 for (i = disks; i--; ) {
1862 struct r5dev *dev = &sh->dev[i];
1863
1864 if (dev->towrite) {
1865 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001866 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001867 if (!expand)
1868 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001869 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001870 }
1871 }
Dan Williams600aa102008-06-28 08:32:05 +10001872 if (s->locked + 1 == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001873 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
1874 atomic_inc(&sh->raid_conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001875 } else {
1876 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1877 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1878
Dan Williamsd8ee0722008-06-28 08:32:06 +10001879 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001880 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1881 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1882 set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001883
1884 for (i = disks; i--; ) {
1885 struct r5dev *dev = &sh->dev[i];
1886 if (i == pd_idx)
1887 continue;
1888
Dan Williamse33129d2007-01-02 13:52:30 -07001889 if (dev->towrite &&
1890 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001891 test_bit(R5_Wantcompute, &dev->flags))) {
1892 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001893 set_bit(R5_LOCKED, &dev->flags);
1894 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001895 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001896 }
1897 }
1898 }
1899
1900 /* keep the parity disk locked while asynchronous operations
1901 * are in flight
1902 */
1903 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1904 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10001905 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001906
Dan Williams600aa102008-06-28 08:32:05 +10001907 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001908 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10001909 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001910}
NeilBrown16a53ec2006-06-26 00:27:38 -07001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/*
1913 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07001914 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * The bi_next chain must be in order.
1916 */
1917static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1918{
1919 struct bio **bip;
1920 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07001921 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Dan Williams45b42332007-07-09 11:56:43 -07001923 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 (unsigned long long)bi->bi_sector,
1925 (unsigned long long)sh->sector);
1926
1927
1928 spin_lock(&sh->lock);
1929 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07001930 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07001932 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1933 firstwrite = 1;
1934 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 bip = &sh->dev[dd_idx].toread;
1936 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1937 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1938 goto overlap;
1939 bip = & (*bip)->bi_next;
1940 }
1941 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1942 goto overlap;
1943
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001944 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (*bip)
1946 bi->bi_next = *bip;
1947 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02001948 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 spin_unlock_irq(&conf->device_lock);
1950 spin_unlock(&sh->lock);
1951
Dan Williams45b42332007-07-09 11:56:43 -07001952 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 (unsigned long long)bi->bi_sector,
1954 (unsigned long long)sh->sector, dd_idx);
1955
NeilBrown72626682005-09-09 16:23:54 -07001956 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07001957 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1958 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07001959 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07001960 set_bit(STRIPE_BIT_DELAY, &sh->state);
1961 }
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (forwrite) {
1964 /* check if page is covered */
1965 sector_t sector = sh->dev[dd_idx].sector;
1966 for (bi=sh->dev[dd_idx].towrite;
1967 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1968 bi && bi->bi_sector <= sector;
1969 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1970 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1971 sector = bi->bi_sector + (bi->bi_size>>9);
1972 }
1973 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1974 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1975 }
1976 return 1;
1977
1978 overlap:
1979 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1980 spin_unlock_irq(&conf->device_lock);
1981 spin_unlock(&sh->lock);
1982 return 0;
1983}
1984
NeilBrown29269552006-03-27 01:18:10 -08001985static void end_reshape(raid5_conf_t *conf);
1986
NeilBrown16a53ec2006-06-26 00:27:38 -07001987static int page_is_zero(struct page *p)
1988{
1989 char *a = page_address(p);
1990 return ((*(u32*)a) == 0 &&
1991 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1992}
1993
NeilBrown911d4ee2009-03-31 14:39:38 +11001994static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
1995 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08001996{
1997 int sectors_per_chunk = conf->chunk_size >> 9;
NeilBrown911d4ee2009-03-31 14:39:38 +11001998 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07001999 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002000 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002001
NeilBrown112bf892009-03-31 14:39:38 +11002002 raid5_compute_sector(conf,
2003 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002004 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002005 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002006 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002007}
2008
Dan Williamsa4456852007-07-09 11:56:43 -07002009static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002010handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002011 struct stripe_head_state *s, int disks,
2012 struct bio **return_bi)
2013{
2014 int i;
2015 for (i = disks; i--; ) {
2016 struct bio *bi;
2017 int bitmap_end = 0;
2018
2019 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2020 mdk_rdev_t *rdev;
2021 rcu_read_lock();
2022 rdev = rcu_dereference(conf->disks[i].rdev);
2023 if (rdev && test_bit(In_sync, &rdev->flags))
2024 /* multiple read failures in one stripe */
2025 md_error(conf->mddev, rdev);
2026 rcu_read_unlock();
2027 }
2028 spin_lock_irq(&conf->device_lock);
2029 /* fail all writes first */
2030 bi = sh->dev[i].towrite;
2031 sh->dev[i].towrite = NULL;
2032 if (bi) {
2033 s->to_write--;
2034 bitmap_end = 1;
2035 }
2036
2037 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2038 wake_up(&conf->wait_for_overlap);
2039
2040 while (bi && bi->bi_sector <
2041 sh->dev[i].sector + STRIPE_SECTORS) {
2042 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2043 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002044 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002045 md_write_end(conf->mddev);
2046 bi->bi_next = *return_bi;
2047 *return_bi = bi;
2048 }
2049 bi = nextbi;
2050 }
2051 /* and fail all 'written' */
2052 bi = sh->dev[i].written;
2053 sh->dev[i].written = NULL;
2054 if (bi) bitmap_end = 1;
2055 while (bi && bi->bi_sector <
2056 sh->dev[i].sector + STRIPE_SECTORS) {
2057 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2058 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002059 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002060 md_write_end(conf->mddev);
2061 bi->bi_next = *return_bi;
2062 *return_bi = bi;
2063 }
2064 bi = bi2;
2065 }
2066
Dan Williamsb5e98d62007-01-02 13:52:31 -07002067 /* fail any reads if this device is non-operational and
2068 * the data has not reached the cache yet.
2069 */
2070 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2071 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2072 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002073 bi = sh->dev[i].toread;
2074 sh->dev[i].toread = NULL;
2075 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2076 wake_up(&conf->wait_for_overlap);
2077 if (bi) s->to_read--;
2078 while (bi && bi->bi_sector <
2079 sh->dev[i].sector + STRIPE_SECTORS) {
2080 struct bio *nextbi =
2081 r5_next_bio(bi, sh->dev[i].sector);
2082 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002083 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002084 bi->bi_next = *return_bi;
2085 *return_bi = bi;
2086 }
2087 bi = nextbi;
2088 }
2089 }
2090 spin_unlock_irq(&conf->device_lock);
2091 if (bitmap_end)
2092 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2093 STRIPE_SECTORS, 0, 0);
2094 }
2095
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002096 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2097 if (atomic_dec_and_test(&conf->pending_full_writes))
2098 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002099}
2100
Dan Williams1fe797e2008-06-28 09:16:30 +10002101/* fetch_block5 - checks the given member device to see if its data needs
2102 * to be read or computed to satisfy a request.
2103 *
2104 * Returns 1 when no more member devices need to be checked, otherwise returns
2105 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002106 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002107static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2108 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002109{
2110 struct r5dev *dev = &sh->dev[disk_idx];
2111 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2112
Dan Williamsf38e1212007-01-02 13:52:30 -07002113 /* is the data in this block needed, and can we get it? */
2114 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002115 !test_bit(R5_UPTODATE, &dev->flags) &&
2116 (dev->toread ||
2117 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2118 s->syncing || s->expanding ||
2119 (s->failed &&
2120 (failed_dev->toread ||
2121 (failed_dev->towrite &&
2122 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002123 /* We would like to get this block, possibly by computing it,
2124 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002125 */
2126 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002127 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002128 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2129 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002130 set_bit(R5_Wantcompute, &dev->flags);
2131 sh->ops.target = disk_idx;
2132 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002133 /* Careful: from this point on 'uptodate' is in the eye
2134 * of raid5_run_ops which services 'compute' operations
2135 * before writes. R5_Wantcompute flags a block that will
2136 * be R5_UPTODATE by the time it is needed for a
2137 * subsequent operation.
2138 */
2139 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002140 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002141 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002142 set_bit(R5_LOCKED, &dev->flags);
2143 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002144 s->locked++;
2145 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2146 s->syncing);
2147 }
2148 }
2149
Dan Williams1fe797e2008-06-28 09:16:30 +10002150 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002151}
2152
Dan Williams1fe797e2008-06-28 09:16:30 +10002153/**
2154 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2155 */
2156static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002157 struct stripe_head_state *s, int disks)
2158{
2159 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002160
Dan Williamsf38e1212007-01-02 13:52:30 -07002161 /* look for blocks to read/compute, skip this if a compute
2162 * is already in flight, or if the stripe contents are in the
2163 * midst of changing due to a write
2164 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002165 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002166 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002167 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002168 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002169 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002170 set_bit(STRIPE_HANDLE, &sh->state);
2171}
2172
Dan Williams1fe797e2008-06-28 09:16:30 +10002173static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002174 struct stripe_head_state *s, struct r6_state *r6s,
2175 int disks)
2176{
2177 int i;
2178 for (i = disks; i--; ) {
2179 struct r5dev *dev = &sh->dev[i];
2180 if (!test_bit(R5_LOCKED, &dev->flags) &&
2181 !test_bit(R5_UPTODATE, &dev->flags) &&
2182 (dev->toread || (dev->towrite &&
2183 !test_bit(R5_OVERWRITE, &dev->flags)) ||
2184 s->syncing || s->expanding ||
2185 (s->failed >= 1 &&
2186 (sh->dev[r6s->failed_num[0]].toread ||
2187 s->to_write)) ||
2188 (s->failed >= 2 &&
2189 (sh->dev[r6s->failed_num[1]].toread ||
2190 s->to_write)))) {
2191 /* we would like to get this block, possibly
2192 * by computing it, but we might not be able to
2193 */
Dan Williamsc3378692008-06-05 22:45:54 -07002194 if ((s->uptodate == disks - 1) &&
2195 (s->failed && (i == r6s->failed_num[0] ||
2196 i == r6s->failed_num[1]))) {
Dan Williams45b42332007-07-09 11:56:43 -07002197 pr_debug("Computing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002198 (unsigned long long)sh->sector, i);
2199 compute_block_1(sh, i, 0);
2200 s->uptodate++;
2201 } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
2202 /* Computing 2-failure is *very* expensive; only
2203 * do it if failed >= 2
2204 */
2205 int other;
2206 for (other = disks; other--; ) {
2207 if (other == i)
2208 continue;
2209 if (!test_bit(R5_UPTODATE,
2210 &sh->dev[other].flags))
2211 break;
2212 }
2213 BUG_ON(other < 0);
Dan Williams45b42332007-07-09 11:56:43 -07002214 pr_debug("Computing stripe %llu blocks %d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002215 (unsigned long long)sh->sector,
2216 i, other);
2217 compute_block_2(sh, i, other);
2218 s->uptodate += 2;
2219 } else if (test_bit(R5_Insync, &dev->flags)) {
2220 set_bit(R5_LOCKED, &dev->flags);
2221 set_bit(R5_Wantread, &dev->flags);
2222 s->locked++;
Dan Williams45b42332007-07-09 11:56:43 -07002223 pr_debug("Reading block %d (sync=%d)\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002224 i, s->syncing);
2225 }
2226 }
2227 }
2228 set_bit(STRIPE_HANDLE, &sh->state);
2229}
2230
2231
Dan Williams1fe797e2008-06-28 09:16:30 +10002232/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002233 * any written block on an uptodate or failed drive can be returned.
2234 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2235 * never LOCKED, so we don't need to test 'failed' directly.
2236 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002237static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002238 struct stripe_head *sh, int disks, struct bio **return_bi)
2239{
2240 int i;
2241 struct r5dev *dev;
2242
2243 for (i = disks; i--; )
2244 if (sh->dev[i].written) {
2245 dev = &sh->dev[i];
2246 if (!test_bit(R5_LOCKED, &dev->flags) &&
2247 test_bit(R5_UPTODATE, &dev->flags)) {
2248 /* We can return any write requests */
2249 struct bio *wbi, *wbi2;
2250 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002251 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002252 spin_lock_irq(&conf->device_lock);
2253 wbi = dev->written;
2254 dev->written = NULL;
2255 while (wbi && wbi->bi_sector <
2256 dev->sector + STRIPE_SECTORS) {
2257 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002258 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002259 md_write_end(conf->mddev);
2260 wbi->bi_next = *return_bi;
2261 *return_bi = wbi;
2262 }
2263 wbi = wbi2;
2264 }
2265 if (dev->towrite == NULL)
2266 bitmap_end = 1;
2267 spin_unlock_irq(&conf->device_lock);
2268 if (bitmap_end)
2269 bitmap_endwrite(conf->mddev->bitmap,
2270 sh->sector,
2271 STRIPE_SECTORS,
2272 !test_bit(STRIPE_DEGRADED, &sh->state),
2273 0);
2274 }
2275 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002276
2277 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2278 if (atomic_dec_and_test(&conf->pending_full_writes))
2279 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002280}
2281
Dan Williams1fe797e2008-06-28 09:16:30 +10002282static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002283 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2284{
2285 int rmw = 0, rcw = 0, i;
2286 for (i = disks; i--; ) {
2287 /* would I have to read this buffer for read_modify_write */
2288 struct r5dev *dev = &sh->dev[i];
2289 if ((dev->towrite || i == sh->pd_idx) &&
2290 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002291 !(test_bit(R5_UPTODATE, &dev->flags) ||
2292 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002293 if (test_bit(R5_Insync, &dev->flags))
2294 rmw++;
2295 else
2296 rmw += 2*disks; /* cannot read it */
2297 }
2298 /* Would I have to read this buffer for reconstruct_write */
2299 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2300 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002301 !(test_bit(R5_UPTODATE, &dev->flags) ||
2302 test_bit(R5_Wantcompute, &dev->flags))) {
2303 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002304 else
2305 rcw += 2*disks;
2306 }
2307 }
Dan Williams45b42332007-07-09 11:56:43 -07002308 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002309 (unsigned long long)sh->sector, rmw, rcw);
2310 set_bit(STRIPE_HANDLE, &sh->state);
2311 if (rmw < rcw && rmw > 0)
2312 /* prefer read-modify-write, but need to get some data */
2313 for (i = disks; i--; ) {
2314 struct r5dev *dev = &sh->dev[i];
2315 if ((dev->towrite || i == sh->pd_idx) &&
2316 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002317 !(test_bit(R5_UPTODATE, &dev->flags) ||
2318 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002319 test_bit(R5_Insync, &dev->flags)) {
2320 if (
2321 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002322 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002323 "%d for r-m-w\n", i);
2324 set_bit(R5_LOCKED, &dev->flags);
2325 set_bit(R5_Wantread, &dev->flags);
2326 s->locked++;
2327 } else {
2328 set_bit(STRIPE_DELAYED, &sh->state);
2329 set_bit(STRIPE_HANDLE, &sh->state);
2330 }
2331 }
2332 }
2333 if (rcw <= rmw && rcw > 0)
2334 /* want reconstruct write, but need to get some data */
2335 for (i = disks; i--; ) {
2336 struct r5dev *dev = &sh->dev[i];
2337 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2338 i != sh->pd_idx &&
2339 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002340 !(test_bit(R5_UPTODATE, &dev->flags) ||
2341 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002342 test_bit(R5_Insync, &dev->flags)) {
2343 if (
2344 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002345 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002346 "%d for Reconstruct\n", i);
2347 set_bit(R5_LOCKED, &dev->flags);
2348 set_bit(R5_Wantread, &dev->flags);
2349 s->locked++;
2350 } else {
2351 set_bit(STRIPE_DELAYED, &sh->state);
2352 set_bit(STRIPE_HANDLE, &sh->state);
2353 }
2354 }
2355 }
2356 /* now if nothing is locked, and if we have enough data,
2357 * we can start a write request
2358 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002359 /* since handle_stripe can be called at any time we need to handle the
2360 * case where a compute block operation has been submitted and then a
2361 * subsequent call wants to start a write request. raid5_run_ops only
2362 * handles the case where compute block and postxor are requested
2363 * simultaneously. If this is not the case then new writes need to be
2364 * held off until the compute completes.
2365 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002366 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2367 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2368 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002369 schedule_reconstruction5(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002370}
2371
Dan Williams1fe797e2008-06-28 09:16:30 +10002372static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002373 struct stripe_head *sh, struct stripe_head_state *s,
2374 struct r6_state *r6s, int disks)
2375{
2376 int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
2377 int qd_idx = r6s->qd_idx;
2378 for (i = disks; i--; ) {
2379 struct r5dev *dev = &sh->dev[i];
2380 /* Would I have to read this buffer for reconstruct_write */
2381 if (!test_bit(R5_OVERWRITE, &dev->flags)
2382 && i != pd_idx && i != qd_idx
2383 && (!test_bit(R5_LOCKED, &dev->flags)
2384 ) &&
2385 !test_bit(R5_UPTODATE, &dev->flags)) {
2386 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2387 else {
Dan Williams45b42332007-07-09 11:56:43 -07002388 pr_debug("raid6: must_compute: "
Dan Williamsa4456852007-07-09 11:56:43 -07002389 "disk %d flags=%#lx\n", i, dev->flags);
2390 must_compute++;
2391 }
2392 }
2393 }
Dan Williams45b42332007-07-09 11:56:43 -07002394 pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002395 (unsigned long long)sh->sector, rcw, must_compute);
2396 set_bit(STRIPE_HANDLE, &sh->state);
2397
2398 if (rcw > 0)
2399 /* want reconstruct write, but need to get some data */
2400 for (i = disks; i--; ) {
2401 struct r5dev *dev = &sh->dev[i];
2402 if (!test_bit(R5_OVERWRITE, &dev->flags)
2403 && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
2404 && !test_bit(R5_LOCKED, &dev->flags) &&
2405 !test_bit(R5_UPTODATE, &dev->flags) &&
2406 test_bit(R5_Insync, &dev->flags)) {
2407 if (
2408 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002409 pr_debug("Read_old stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002410 "block %d for Reconstruct\n",
2411 (unsigned long long)sh->sector, i);
2412 set_bit(R5_LOCKED, &dev->flags);
2413 set_bit(R5_Wantread, &dev->flags);
2414 s->locked++;
2415 } else {
Dan Williams45b42332007-07-09 11:56:43 -07002416 pr_debug("Request delayed stripe %llu "
Dan Williamsa4456852007-07-09 11:56:43 -07002417 "block %d for Reconstruct\n",
2418 (unsigned long long)sh->sector, i);
2419 set_bit(STRIPE_DELAYED, &sh->state);
2420 set_bit(STRIPE_HANDLE, &sh->state);
2421 }
2422 }
2423 }
2424 /* now if nothing is locked, and if we have enough data, we can start a
2425 * write request
2426 */
2427 if (s->locked == 0 && rcw == 0 &&
2428 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2429 if (must_compute > 0) {
2430 /* We have failed blocks and need to compute them */
2431 switch (s->failed) {
2432 case 0:
2433 BUG();
2434 case 1:
2435 compute_block_1(sh, r6s->failed_num[0], 0);
2436 break;
2437 case 2:
2438 compute_block_2(sh, r6s->failed_num[0],
2439 r6s->failed_num[1]);
2440 break;
2441 default: /* This request should have been failed? */
2442 BUG();
2443 }
2444 }
2445
Dan Williams45b42332007-07-09 11:56:43 -07002446 pr_debug("Computing parity for stripe %llu\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002447 (unsigned long long)sh->sector);
2448 compute_parity6(sh, RECONSTRUCT_WRITE);
2449 /* now every locked buffer is ready to be written */
2450 for (i = disks; i--; )
2451 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
Dan Williams45b42332007-07-09 11:56:43 -07002452 pr_debug("Writing stripe %llu block %d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002453 (unsigned long long)sh->sector, i);
2454 s->locked++;
2455 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2456 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002457 if (s->locked == disks)
2458 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2459 atomic_inc(&conf->pending_full_writes);
Dan Williamsa4456852007-07-09 11:56:43 -07002460 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2461 set_bit(STRIPE_INSYNC, &sh->state);
2462
2463 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2464 atomic_dec(&conf->preread_active_stripes);
2465 if (atomic_read(&conf->preread_active_stripes) <
2466 IO_THRESHOLD)
2467 md_wakeup_thread(conf->mddev->thread);
2468 }
2469 }
2470}
2471
2472static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2473 struct stripe_head_state *s, int disks)
2474{
Dan Williamsecc65c92008-06-28 08:31:57 +10002475 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002476
Dan Williamsbd2ab672008-04-10 21:29:27 -07002477 set_bit(STRIPE_HANDLE, &sh->state);
2478
Dan Williamsecc65c92008-06-28 08:31:57 +10002479 switch (sh->check_state) {
2480 case check_state_idle:
2481 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002482 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002483 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002484 sh->check_state = check_state_run;
2485 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002486 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002487 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002488 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002489 }
Dan Williamsa4456852007-07-09 11:56:43 -07002490 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002491 /* fall through */
2492 case check_state_compute_result:
2493 sh->check_state = check_state_idle;
2494 if (!dev)
2495 dev = &sh->dev[sh->pd_idx];
2496
2497 /* check that a write has not made the stripe insync */
2498 if (test_bit(STRIPE_INSYNC, &sh->state))
2499 break;
2500
2501 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002502 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2503 BUG_ON(s->uptodate != disks);
2504
2505 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002506 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002507 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002508
Dan Williamsa4456852007-07-09 11:56:43 -07002509 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002510 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002511 break;
2512 case check_state_run:
2513 break; /* we will be called again upon completion */
2514 case check_state_check_result:
2515 sh->check_state = check_state_idle;
2516
2517 /* if a failure occurred during the check operation, leave
2518 * STRIPE_INSYNC not set and let the stripe be handled again
2519 */
2520 if (s->failed)
2521 break;
2522
2523 /* handle a successful check operation, if parity is correct
2524 * we are done. Otherwise update the mismatch count and repair
2525 * parity if !MD_RECOVERY_CHECK
2526 */
2527 if (sh->ops.zero_sum_result == 0)
2528 /* parity is correct (on disc,
2529 * not in buffer any more)
2530 */
2531 set_bit(STRIPE_INSYNC, &sh->state);
2532 else {
2533 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2534 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2535 /* don't try to repair!! */
2536 set_bit(STRIPE_INSYNC, &sh->state);
2537 else {
2538 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002539 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002540 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2541 set_bit(R5_Wantcompute,
2542 &sh->dev[sh->pd_idx].flags);
2543 sh->ops.target = sh->pd_idx;
2544 s->uptodate++;
2545 }
2546 }
2547 break;
2548 case check_state_compute_run:
2549 break;
2550 default:
2551 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2552 __func__, sh->check_state,
2553 (unsigned long long) sh->sector);
2554 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002555 }
2556}
2557
2558
2559static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2560 struct stripe_head_state *s,
2561 struct r6_state *r6s, struct page *tmp_page,
2562 int disks)
2563{
2564 int update_p = 0, update_q = 0;
2565 struct r5dev *dev;
2566 int pd_idx = sh->pd_idx;
2567 int qd_idx = r6s->qd_idx;
2568
2569 set_bit(STRIPE_HANDLE, &sh->state);
2570
2571 BUG_ON(s->failed > 2);
2572 BUG_ON(s->uptodate < disks);
2573 /* Want to check and possibly repair P and Q.
2574 * However there could be one 'failed' device, in which
2575 * case we can only check one of them, possibly using the
2576 * other to generate missing data
2577 */
2578
2579 /* If !tmp_page, we cannot do the calculations,
2580 * but as we have set STRIPE_HANDLE, we will soon be called
2581 * by stripe_handle with a tmp_page - just wait until then.
2582 */
2583 if (tmp_page) {
2584 if (s->failed == r6s->q_failed) {
2585 /* The only possible failed device holds 'Q', so it
2586 * makes sense to check P (If anything else were failed,
2587 * we would have used P to recreate it).
2588 */
2589 compute_block_1(sh, pd_idx, 1);
2590 if (!page_is_zero(sh->dev[pd_idx].page)) {
2591 compute_block_1(sh, pd_idx, 0);
2592 update_p = 1;
2593 }
2594 }
2595 if (!r6s->q_failed && s->failed < 2) {
2596 /* q is not failed, and we didn't use it to generate
2597 * anything, so it makes sense to check it
2598 */
2599 memcpy(page_address(tmp_page),
2600 page_address(sh->dev[qd_idx].page),
2601 STRIPE_SIZE);
2602 compute_parity6(sh, UPDATE_PARITY);
2603 if (memcmp(page_address(tmp_page),
2604 page_address(sh->dev[qd_idx].page),
2605 STRIPE_SIZE) != 0) {
2606 clear_bit(STRIPE_INSYNC, &sh->state);
2607 update_q = 1;
2608 }
2609 }
2610 if (update_p || update_q) {
2611 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2612 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2613 /* don't try to repair!! */
2614 update_p = update_q = 0;
2615 }
2616
2617 /* now write out any block on a failed drive,
2618 * or P or Q if they need it
2619 */
2620
2621 if (s->failed == 2) {
2622 dev = &sh->dev[r6s->failed_num[1]];
2623 s->locked++;
2624 set_bit(R5_LOCKED, &dev->flags);
2625 set_bit(R5_Wantwrite, &dev->flags);
2626 }
2627 if (s->failed >= 1) {
2628 dev = &sh->dev[r6s->failed_num[0]];
2629 s->locked++;
2630 set_bit(R5_LOCKED, &dev->flags);
2631 set_bit(R5_Wantwrite, &dev->flags);
2632 }
2633
2634 if (update_p) {
2635 dev = &sh->dev[pd_idx];
2636 s->locked++;
2637 set_bit(R5_LOCKED, &dev->flags);
2638 set_bit(R5_Wantwrite, &dev->flags);
2639 }
2640 if (update_q) {
2641 dev = &sh->dev[qd_idx];
2642 s->locked++;
2643 set_bit(R5_LOCKED, &dev->flags);
2644 set_bit(R5_Wantwrite, &dev->flags);
2645 }
2646 clear_bit(STRIPE_DEGRADED, &sh->state);
2647
2648 set_bit(STRIPE_INSYNC, &sh->state);
2649 }
2650}
2651
2652static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2653 struct r6_state *r6s)
2654{
2655 int i;
2656
2657 /* We have read all the blocks in this stripe and now we need to
2658 * copy some of them into a target stripe for expand.
2659 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002660 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002661 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2662 for (i = 0; i < sh->disks; i++)
NeilBrowna2e08552007-09-11 15:23:36 -07002663 if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002664 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002665 struct stripe_head *sh2;
2666
2667 sector_t bn = compute_blocknr(sh, i);
NeilBrown911d4ee2009-03-31 14:39:38 +11002668 sector_t s = raid5_compute_sector(conf, bn, 0,
2669 &dd_idx, NULL);
NeilBrownb5663ba2009-03-31 14:39:38 +11002670 sh2 = get_active_stripe(conf, s, 0, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002671 if (sh2 == NULL)
2672 /* so far only the early blocks of this stripe
2673 * have been requested. When later blocks
2674 * get requested, we will try again
2675 */
2676 continue;
2677 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2678 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2679 /* must have already done this block */
2680 release_stripe(sh2);
2681 continue;
2682 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002683
2684 /* place all the copies on one channel */
2685 tx = async_memcpy(sh2->dev[dd_idx].page,
2686 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2687 ASYNC_TX_DEP_ACK, tx, NULL, NULL);
2688
Dan Williamsa4456852007-07-09 11:56:43 -07002689 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2690 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2691 for (j = 0; j < conf->raid_disks; j++)
2692 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002693 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002694 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2695 break;
2696 if (j == conf->raid_disks) {
2697 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2698 set_bit(STRIPE_HANDLE, &sh2->state);
2699 }
2700 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002701
Dan Williamsa4456852007-07-09 11:56:43 -07002702 }
NeilBrowna2e08552007-09-11 15:23:36 -07002703 /* done submitting copies, wait for them to complete */
2704 if (tx) {
2705 async_tx_ack(tx);
2706 dma_wait_for_async_tx(tx);
2707 }
Dan Williamsa4456852007-07-09 11:56:43 -07002708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Dan Williams6bfe0b42008-04-30 00:52:32 -07002710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711/*
2712 * handle_stripe - do things to a stripe.
2713 *
2714 * We lock the stripe and then examine the state of various bits
2715 * to see what needs to be done.
2716 * Possible results:
2717 * return some read request which now have data
2718 * return some write requests which are safely on disc
2719 * schedule a read on some buffers
2720 * schedule a write of some buffers
2721 * return confirmation of parity correctness
2722 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 * buffers are taken off read_list or write_list, and bh_cache buffers
2724 * get BH_Lock set before the stripe lock is released.
2725 *
2726 */
Dan Williamsa4456852007-07-09 11:56:43 -07002727
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002728static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002731 int disks = sh->disks, i;
2732 struct bio *return_bi = NULL;
2733 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002735 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002736 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Dan Williamsa4456852007-07-09 11:56:43 -07002738 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002739 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2740 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2741 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2742 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 spin_lock(&sh->lock);
2745 clear_bit(STRIPE_HANDLE, &sh->state);
2746 clear_bit(STRIPE_DELAYED, &sh->state);
2747
Dan Williamsa4456852007-07-09 11:56:43 -07002748 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2749 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2750 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002753 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 for (i=disks; i--; ) {
2755 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002756 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Dan Williamsb5e98d62007-01-02 13:52:31 -07002759 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2760 "written %p\n", i, dev->flags, dev->toread, dev->read,
2761 dev->towrite, dev->written);
2762
2763 /* maybe we can request a biofill operation
2764 *
2765 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002766 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002767 */
2768 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002769 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002770 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002773 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2774 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002775 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Dan Williamsb5e98d62007-01-02 13:52:31 -07002777 if (test_bit(R5_Wantfill, &dev->flags))
2778 s.to_fill++;
2779 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002780 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002782 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002784 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Dan Williamsa4456852007-07-09 11:56:43 -07002786 if (dev->written)
2787 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002788 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002789 if (blocked_rdev == NULL &&
2790 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002791 blocked_rdev = rdev;
2792 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002793 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002794 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002795 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002796 clear_bit(R5_ReadError, &dev->flags);
2797 clear_bit(R5_ReWrite, &dev->flags);
2798 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002799 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002800 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002801 s.failed++;
2802 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 } else
2804 set_bit(R5_Insync, &dev->flags);
2805 }
NeilBrown9910f162006-01-06 00:20:24 -08002806 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002807
Dan Williams6bfe0b42008-04-30 00:52:32 -07002808 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10002809 if (s.syncing || s.expanding || s.expanded ||
2810 s.to_write || s.written) {
2811 set_bit(STRIPE_HANDLE, &sh->state);
2812 goto unlock;
2813 }
2814 /* There is nothing for the blocked_rdev to block */
2815 rdev_dec_pending(blocked_rdev, conf->mddev);
2816 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002817 }
2818
Dan Williams83de75c2008-06-28 08:31:58 +10002819 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2820 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2821 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2822 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002823
Dan Williams45b42332007-07-09 11:56:43 -07002824 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002826 s.locked, s.uptodate, s.to_read, s.to_write,
2827 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 /* check if the array has lost two devices and, if so, some requests might
2829 * need to be failed
2830 */
Dan Williamsa4456852007-07-09 11:56:43 -07002831 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10002832 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07002833 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2835 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002836 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838
2839 /* might be able to return some write requests if the parity block
2840 * is safe, or on a failed drive
2841 */
2842 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07002843 if ( s.written &&
2844 ((test_bit(R5_Insync, &dev->flags) &&
2845 !test_bit(R5_LOCKED, &dev->flags) &&
2846 test_bit(R5_UPTODATE, &dev->flags)) ||
2847 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10002848 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* Now we might consider reading some blocks, either to check/generate
2851 * parity, or to satisfy requests
2852 * or to load a block that is being partially written.
2853 */
Dan Williamsa4456852007-07-09 11:56:43 -07002854 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10002855 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10002856 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Dan Williamse33129d2007-01-02 13:52:30 -07002858 /* Now we check to see if any write operations have recently
2859 * completed
2860 */
Dan Williamse0a115e2008-06-05 22:45:52 -07002861 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002862 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07002863 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10002864 if (sh->reconstruct_state == reconstruct_state_drain_result ||
2865 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10002866 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07002867
2868 /* All the 'written' buffers and the parity block are ready to
2869 * be written back to disk
2870 */
2871 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
2872 for (i = disks; i--; ) {
2873 dev = &sh->dev[i];
2874 if (test_bit(R5_LOCKED, &dev->flags) &&
2875 (i == sh->pd_idx || dev->written)) {
2876 pr_debug("Writing block %d\n", i);
2877 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07002878 if (prexor)
2879 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07002880 if (!test_bit(R5_Insync, &dev->flags) ||
2881 (i == sh->pd_idx && s.failed == 0))
2882 set_bit(STRIPE_INSYNC, &sh->state);
2883 }
2884 }
2885 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2886 atomic_dec(&conf->preread_active_stripes);
2887 if (atomic_read(&conf->preread_active_stripes) <
2888 IO_THRESHOLD)
2889 md_wakeup_thread(conf->mddev->thread);
2890 }
2891 }
2892
2893 /* Now to consider new write requests and what else, if anything
2894 * should be read. We do not handle new writes when:
2895 * 1/ A 'write' operation (copy+xor) is already in flight.
2896 * 2/ A 'check' operation is in flight, as it may clobber the parity
2897 * block.
2898 */
Dan Williams600aa102008-06-28 08:32:05 +10002899 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10002900 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07002903 * Any reads will already have been scheduled, so we just see if enough
2904 * data is available. The parity check is held off while parity
2905 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 */
Dan Williamsecc65c92008-06-28 08:31:57 +10002907 if (sh->check_state ||
2908 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002909 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002910 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07002911 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07002912
Dan Williamsa4456852007-07-09 11:56:43 -07002913 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2915 clear_bit(STRIPE_SYNCING, &sh->state);
2916 }
NeilBrown4e5314b2005-11-08 21:39:22 -08002917
2918 /* If the failed drive is just a ReadError, then we might need to progress
2919 * the repair/check process
2920 */
Dan Williamsa4456852007-07-09 11:56:43 -07002921 if (s.failed == 1 && !conf->mddev->ro &&
2922 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
2923 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
2924 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002925 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002926 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08002927 if (!test_bit(R5_ReWrite, &dev->flags)) {
2928 set_bit(R5_Wantwrite, &dev->flags);
2929 set_bit(R5_ReWrite, &dev->flags);
2930 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002931 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002932 } else {
2933 /* let's read it back */
2934 set_bit(R5_Wantread, &dev->flags);
2935 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002936 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08002937 }
2938 }
2939
Dan Williams600aa102008-06-28 08:32:05 +10002940 /* Finish reconstruct operations initiated by the expansion process */
2941 if (sh->reconstruct_state == reconstruct_state_result) {
2942 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07002943 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07002944 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07002945 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07002946 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10002947 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07002948 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002949 }
2950
2951 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10002952 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002953 /* Need to write out all blocks after computing parity */
2954 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11002955 stripe_set_idx(sh->sector, conf, 0, sh);
Dan Williams1fe797e2008-06-28 09:16:30 +10002956 schedule_reconstruction5(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10002957 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002958 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08002959 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002960 wake_up(&conf->wait_for_overlap);
2961 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
2962 }
2963
Dan Williams0f94e872008-01-08 15:32:53 -08002964 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10002965 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07002966 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002967
Dan Williams6bfe0b42008-04-30 00:52:32 -07002968 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 spin_unlock(&sh->lock);
2970
Dan Williams6bfe0b42008-04-30 00:52:32 -07002971 /* wait for this device to become unblocked */
2972 if (unlikely(blocked_rdev))
2973 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
2974
Dan Williams600aa102008-06-28 08:32:05 +10002975 if (s.ops_request)
2976 raid5_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07002977
Dan Williamsc4e5ac02008-06-28 08:31:53 +10002978 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Dan Williamsa4456852007-07-09 11:56:43 -07002980 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002981
2982 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002985static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
NeilBrown16a53ec2006-06-26 00:27:38 -07002986{
NeilBrownbff61972009-03-31 14:33:13 +11002987 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08002988 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07002989 struct bio *return_bi = NULL;
2990 int i, pd_idx = sh->pd_idx;
2991 struct stripe_head_state s;
2992 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07002993 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002994 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07002995
NeilBrownd0dabf72009-03-31 14:39:38 +11002996 r6s.qd_idx = sh->qd_idx;
Dan Williams45b42332007-07-09 11:56:43 -07002997 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Dan Williamsa4456852007-07-09 11:56:43 -07002998 "pd_idx=%d, qd_idx=%d\n",
2999 (unsigned long long)sh->sector, sh->state,
3000 atomic_read(&sh->count), pd_idx, r6s.qd_idx);
3001 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003002
3003 spin_lock(&sh->lock);
3004 clear_bit(STRIPE_HANDLE, &sh->state);
3005 clear_bit(STRIPE_DELAYED, &sh->state);
3006
Dan Williamsa4456852007-07-09 11:56:43 -07003007 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3008 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3009 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003010 /* Now to look around and see what can be done */
3011
3012 rcu_read_lock();
3013 for (i=disks; i--; ) {
3014 mdk_rdev_t *rdev;
3015 dev = &sh->dev[i];
3016 clear_bit(R5_Insync, &dev->flags);
3017
Dan Williams45b42332007-07-09 11:56:43 -07003018 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003019 i, dev->flags, dev->toread, dev->towrite, dev->written);
3020 /* maybe we can reply to a read */
3021 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
3022 struct bio *rbi, *rbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003023 pr_debug("Return read for disc %d\n", i);
NeilBrown16a53ec2006-06-26 00:27:38 -07003024 spin_lock_irq(&conf->device_lock);
3025 rbi = dev->toread;
3026 dev->toread = NULL;
3027 if (test_and_clear_bit(R5_Overlap, &dev->flags))
3028 wake_up(&conf->wait_for_overlap);
3029 spin_unlock_irq(&conf->device_lock);
3030 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
3031 copy_data(0, rbi, dev->page, dev->sector);
3032 rbi2 = r5_next_bio(rbi, dev->sector);
3033 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003034 if (!raid5_dec_bi_phys_segments(rbi)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003035 rbi->bi_next = return_bi;
3036 return_bi = rbi;
3037 }
3038 spin_unlock_irq(&conf->device_lock);
3039 rbi = rbi2;
3040 }
3041 }
3042
3043 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003044 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3045 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003046
3047
Dan Williamsa4456852007-07-09 11:56:43 -07003048 if (dev->toread)
3049 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003050 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003051 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003052 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003053 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003054 }
Dan Williamsa4456852007-07-09 11:56:43 -07003055 if (dev->written)
3056 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003057 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003058 if (blocked_rdev == NULL &&
3059 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003060 blocked_rdev = rdev;
3061 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003062 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003063 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3064 /* The ReadError flag will just be confusing now */
3065 clear_bit(R5_ReadError, &dev->flags);
3066 clear_bit(R5_ReWrite, &dev->flags);
3067 }
3068 if (!rdev || !test_bit(In_sync, &rdev->flags)
3069 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003070 if (s.failed < 2)
3071 r6s.failed_num[s.failed] = i;
3072 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003073 } else
3074 set_bit(R5_Insync, &dev->flags);
3075 }
3076 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003077
3078 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003079 if (s.syncing || s.expanding || s.expanded ||
3080 s.to_write || s.written) {
3081 set_bit(STRIPE_HANDLE, &sh->state);
3082 goto unlock;
3083 }
3084 /* There is nothing for the blocked_rdev to block */
3085 rdev_dec_pending(blocked_rdev, conf->mddev);
3086 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003087 }
NeilBrownac4090d2008-08-05 15:54:13 +10003088
Dan Williams45b42332007-07-09 11:56:43 -07003089 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003090 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003091 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3092 r6s.failed_num[0], r6s.failed_num[1]);
3093 /* check if the array has lost >2 devices and, if so, some requests
3094 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003095 */
Dan Williamsa4456852007-07-09 11:56:43 -07003096 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003097 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003098 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003099 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3100 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003101 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003102 }
3103
3104 /*
3105 * might be able to return some write requests if the parity blocks
3106 * are safe, or on a failed drive
3107 */
3108 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003109 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3110 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
3111 qdev = &sh->dev[r6s.qd_idx];
3112 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
3113 || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003114
Dan Williamsa4456852007-07-09 11:56:43 -07003115 if ( s.written &&
3116 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003117 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003118 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3119 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003120 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003121 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003122 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003123
3124 /* Now we might consider reading some blocks, either to check/generate
3125 * parity, or to satisfy requests
3126 * or to load a block that is being partially written.
3127 */
Dan Williamsa4456852007-07-09 11:56:43 -07003128 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
3129 (s.syncing && (s.uptodate < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003130 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003131
3132 /* now to consider writing and what else, if anything should be read */
Dan Williamsa4456852007-07-09 11:56:43 -07003133 if (s.to_write)
Dan Williams1fe797e2008-06-28 09:16:30 +10003134 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003135
3136 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003137 * Any reads will already have been scheduled, so we just see if enough
3138 * data is available
NeilBrown16a53ec2006-06-26 00:27:38 -07003139 */
Dan Williamsa4456852007-07-09 11:56:43 -07003140 if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
3141 handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003142
Dan Williamsa4456852007-07-09 11:56:43 -07003143 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003144 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3145 clear_bit(STRIPE_SYNCING, &sh->state);
3146 }
3147
3148 /* If the failed drives are just a ReadError, then we might need
3149 * to progress the repair/check process
3150 */
Dan Williamsa4456852007-07-09 11:56:43 -07003151 if (s.failed <= 2 && !conf->mddev->ro)
3152 for (i = 0; i < s.failed; i++) {
3153 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003154 if (test_bit(R5_ReadError, &dev->flags)
3155 && !test_bit(R5_LOCKED, &dev->flags)
3156 && test_bit(R5_UPTODATE, &dev->flags)
3157 ) {
3158 if (!test_bit(R5_ReWrite, &dev->flags)) {
3159 set_bit(R5_Wantwrite, &dev->flags);
3160 set_bit(R5_ReWrite, &dev->flags);
3161 set_bit(R5_LOCKED, &dev->flags);
3162 } else {
3163 /* let's read it back */
3164 set_bit(R5_Wantread, &dev->flags);
3165 set_bit(R5_LOCKED, &dev->flags);
3166 }
3167 }
3168 }
NeilBrownf4168852007-02-28 20:11:53 -08003169
Dan Williamsa4456852007-07-09 11:56:43 -07003170 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
NeilBrownf4168852007-02-28 20:11:53 -08003171 /* Need to write out all blocks after computing P&Q */
3172 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003173 stripe_set_idx(sh->sector, conf, 0, sh);
NeilBrownf4168852007-02-28 20:11:53 -08003174 compute_parity6(sh, RECONSTRUCT_WRITE);
3175 for (i = conf->raid_disks ; i-- ; ) {
3176 set_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003177 s.locked++;
NeilBrownf4168852007-02-28 20:11:53 -08003178 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3179 }
3180 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003181 } else if (s.expanded) {
NeilBrownf4168852007-02-28 20:11:53 -08003182 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3183 atomic_dec(&conf->reshape_stripes);
3184 wake_up(&conf->wait_for_overlap);
3185 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3186 }
3187
Dan Williams0f94e872008-01-08 15:32:53 -08003188 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003189 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003190 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003191
Dan Williams6bfe0b42008-04-30 00:52:32 -07003192 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003193 spin_unlock(&sh->lock);
3194
Dan Williams6bfe0b42008-04-30 00:52:32 -07003195 /* wait for this device to become unblocked */
3196 if (unlikely(blocked_rdev))
3197 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3198
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003199 ops_run_io(sh, &s);
3200
Dan Williamsa4456852007-07-09 11:56:43 -07003201 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003202
3203 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003204}
3205
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003206/* returns true if the stripe was handled */
3207static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
NeilBrown16a53ec2006-06-26 00:27:38 -07003208{
3209 if (sh->raid_conf->level == 6)
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003210 return handle_stripe6(sh, tmp_page);
NeilBrown16a53ec2006-06-26 00:27:38 -07003211 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003212 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003213}
3214
3215
3216
Arjan van de Ven858119e2006-01-14 13:20:43 -08003217static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
3219 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3220 while (!list_empty(&conf->delayed_list)) {
3221 struct list_head *l = conf->delayed_list.next;
3222 struct stripe_head *sh;
3223 sh = list_entry(l, struct stripe_head, lru);
3224 list_del_init(l);
3225 clear_bit(STRIPE_DELAYED, &sh->state);
3226 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3227 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003228 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 }
NeilBrown6ed30032008-02-06 01:40:00 -08003230 } else
3231 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
Arjan van de Ven858119e2006-01-14 13:20:43 -08003234static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003235{
3236 /* device_lock is held */
3237 struct list_head head;
3238 list_add(&head, &conf->bitmap_list);
3239 list_del_init(&conf->bitmap_list);
3240 while (!list_empty(&head)) {
3241 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3242 list_del_init(&sh->lru);
3243 atomic_inc(&sh->count);
3244 __release_stripe(conf, sh);
3245 }
3246}
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248static void unplug_slaves(mddev_t *mddev)
3249{
3250 raid5_conf_t *conf = mddev_to_conf(mddev);
3251 int i;
3252
3253 rcu_read_lock();
3254 for (i=0; i<mddev->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003255 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003256 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003257 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 atomic_inc(&rdev->nr_pending);
3260 rcu_read_unlock();
3261
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003262 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 rdev_dec_pending(rdev, mddev);
3265 rcu_read_lock();
3266 }
3267 }
3268 rcu_read_unlock();
3269}
3270
Jens Axboe165125e2007-07-24 09:28:11 +02003271static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
3273 mddev_t *mddev = q->queuedata;
3274 raid5_conf_t *conf = mddev_to_conf(mddev);
3275 unsigned long flags;
3276
3277 spin_lock_irqsave(&conf->device_lock, flags);
3278
NeilBrown72626682005-09-09 16:23:54 -07003279 if (blk_remove_plug(q)) {
3280 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 md_wakeup_thread(mddev->thread);
3284
3285 spin_unlock_irqrestore(&conf->device_lock, flags);
3286
3287 unplug_slaves(mddev);
3288}
3289
NeilBrownf022b2f2006-10-03 01:15:56 -07003290static int raid5_congested(void *data, int bits)
3291{
3292 mddev_t *mddev = data;
3293 raid5_conf_t *conf = mddev_to_conf(mddev);
3294
3295 /* No difference between reads and writes. Just check
3296 * how busy the stripe_cache is
3297 */
3298 if (conf->inactive_blocked)
3299 return 1;
3300 if (conf->quiesce)
3301 return 1;
3302 if (list_empty_careful(&conf->inactive_list))
3303 return 1;
3304
3305 return 0;
3306}
3307
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003308/* We want read requests to align with chunks where possible,
3309 * but write requests don't need to.
3310 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003311static int raid5_mergeable_bvec(struct request_queue *q,
3312 struct bvec_merge_data *bvm,
3313 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003314{
3315 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003316 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003317 int max;
3318 unsigned int chunk_sectors = mddev->chunk_size >> 9;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003319 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003320
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003321 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003322 return biovec->bv_len; /* always allow writes to be mergeable */
3323
3324 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3325 if (max < 0) max = 0;
3326 if (max <= biovec->bv_len && bio_sectors == 0)
3327 return biovec->bv_len;
3328 else
3329 return max;
3330}
3331
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003332
3333static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3334{
3335 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3336 unsigned int chunk_sectors = mddev->chunk_size >> 9;
3337 unsigned int bio_sectors = bio->bi_size >> 9;
3338
3339 return chunk_sectors >=
3340 ((sector & (chunk_sectors - 1)) + bio_sectors);
3341}
3342
3343/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003344 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3345 * later sampled by raid5d.
3346 */
3347static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3348{
3349 unsigned long flags;
3350
3351 spin_lock_irqsave(&conf->device_lock, flags);
3352
3353 bi->bi_next = conf->retry_read_aligned_list;
3354 conf->retry_read_aligned_list = bi;
3355
3356 spin_unlock_irqrestore(&conf->device_lock, flags);
3357 md_wakeup_thread(conf->mddev->thread);
3358}
3359
3360
3361static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3362{
3363 struct bio *bi;
3364
3365 bi = conf->retry_read_aligned;
3366 if (bi) {
3367 conf->retry_read_aligned = NULL;
3368 return bi;
3369 }
3370 bi = conf->retry_read_aligned_list;
3371 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003372 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003373 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003374 /*
3375 * this sets the active strip count to 1 and the processed
3376 * strip count to zero (upper 8 bits)
3377 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003378 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003379 }
3380
3381 return bi;
3382}
3383
3384
3385/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003386 * The "raid5_align_endio" should check if the read succeeded and if it
3387 * did, call bio_endio on the original bio (having bio_put the new bio
3388 * first).
3389 * If the read failed..
3390 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003391static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003392{
3393 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003394 mddev_t *mddev;
3395 raid5_conf_t *conf;
3396 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3397 mdk_rdev_t *rdev;
3398
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003399 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003400
3401 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
3402 conf = mddev_to_conf(mddev);
3403 rdev = (void*)raid_bi->bi_next;
3404 raid_bi->bi_next = NULL;
3405
3406 rdev_dec_pending(rdev, conf->mddev);
3407
3408 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003409 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003410 if (atomic_dec_and_test(&conf->active_aligned_reads))
3411 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003412 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003413 }
3414
3415
Dan Williams45b42332007-07-09 11:56:43 -07003416 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003417
3418 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003419}
3420
Neil Brown387bb172007-02-08 14:20:29 -08003421static int bio_fits_rdev(struct bio *bi)
3422{
Jens Axboe165125e2007-07-24 09:28:11 +02003423 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003424
3425 if ((bi->bi_size>>9) > q->max_sectors)
3426 return 0;
3427 blk_recount_segments(q, bi);
Jens Axboe960e7392008-08-15 10:41:18 +02003428 if (bi->bi_phys_segments > q->max_phys_segments)
Neil Brown387bb172007-02-08 14:20:29 -08003429 return 0;
3430
3431 if (q->merge_bvec_fn)
3432 /* it's too hard to apply the merge_bvec_fn at this stage,
3433 * just just give up
3434 */
3435 return 0;
3436
3437 return 1;
3438}
3439
3440
Jens Axboe165125e2007-07-24 09:28:11 +02003441static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003442{
3443 mddev_t *mddev = q->queuedata;
3444 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003445 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003446 struct bio* align_bi;
3447 mdk_rdev_t *rdev;
3448
3449 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003450 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003451 return 0;
3452 }
3453 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003454 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003455 */
3456 align_bi = bio_clone(raid_bio, GFP_NOIO);
3457 if (!align_bi)
3458 return 0;
3459 /*
3460 * set bi_end_io to a new function, and set bi_private to the
3461 * original bio.
3462 */
3463 align_bi->bi_end_io = raid5_align_endio;
3464 align_bi->bi_private = raid_bio;
3465 /*
3466 * compute position
3467 */
NeilBrown112bf892009-03-31 14:39:38 +11003468 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3469 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003470 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003471
3472 rcu_read_lock();
3473 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3474 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003475 atomic_inc(&rdev->nr_pending);
3476 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003477 raid_bio->bi_next = (void*)rdev;
3478 align_bi->bi_bdev = rdev->bdev;
3479 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3480 align_bi->bi_sector += rdev->data_offset;
3481
Neil Brown387bb172007-02-08 14:20:29 -08003482 if (!bio_fits_rdev(align_bi)) {
3483 /* too big in some way */
3484 bio_put(align_bi);
3485 rdev_dec_pending(rdev, mddev);
3486 return 0;
3487 }
3488
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003489 spin_lock_irq(&conf->device_lock);
3490 wait_event_lock_irq(conf->wait_for_stripe,
3491 conf->quiesce == 0,
3492 conf->device_lock, /* nothing */);
3493 atomic_inc(&conf->active_aligned_reads);
3494 spin_unlock_irq(&conf->device_lock);
3495
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003496 generic_make_request(align_bi);
3497 return 1;
3498 } else {
3499 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003500 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003501 return 0;
3502 }
3503}
3504
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003505/* __get_priority_stripe - get the next stripe to process
3506 *
3507 * Full stripe writes are allowed to pass preread active stripes up until
3508 * the bypass_threshold is exceeded. In general the bypass_count
3509 * increments when the handle_list is handled before the hold_list; however, it
3510 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3511 * stripe with in flight i/o. The bypass_count will be reset when the
3512 * head of the hold_list has changed, i.e. the head was promoted to the
3513 * handle_list.
3514 */
3515static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3516{
3517 struct stripe_head *sh;
3518
3519 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3520 __func__,
3521 list_empty(&conf->handle_list) ? "empty" : "busy",
3522 list_empty(&conf->hold_list) ? "empty" : "busy",
3523 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3524
3525 if (!list_empty(&conf->handle_list)) {
3526 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3527
3528 if (list_empty(&conf->hold_list))
3529 conf->bypass_count = 0;
3530 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3531 if (conf->hold_list.next == conf->last_hold)
3532 conf->bypass_count++;
3533 else {
3534 conf->last_hold = conf->hold_list.next;
3535 conf->bypass_count -= conf->bypass_threshold;
3536 if (conf->bypass_count < 0)
3537 conf->bypass_count = 0;
3538 }
3539 }
3540 } else if (!list_empty(&conf->hold_list) &&
3541 ((conf->bypass_threshold &&
3542 conf->bypass_count > conf->bypass_threshold) ||
3543 atomic_read(&conf->pending_full_writes) == 0)) {
3544 sh = list_entry(conf->hold_list.next,
3545 typeof(*sh), lru);
3546 conf->bypass_count -= conf->bypass_threshold;
3547 if (conf->bypass_count < 0)
3548 conf->bypass_count = 0;
3549 } else
3550 return NULL;
3551
3552 list_del_init(&sh->lru);
3553 atomic_inc(&sh->count);
3554 BUG_ON(atomic_read(&sh->count) != 1);
3555 return sh;
3556}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003557
Jens Axboe165125e2007-07-24 09:28:11 +02003558static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
3560 mddev_t *mddev = q->queuedata;
3561 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown911d4ee2009-03-31 14:39:38 +11003562 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 sector_t new_sector;
3564 sector_t logical_sector, last_sector;
3565 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003566 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003567 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
NeilBrowne5dcdd82005-09-09 16:23:41 -07003569 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003570 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003571 return 0;
3572 }
3573
NeilBrown3d310eb2005-06-21 17:17:26 -07003574 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003575
Tejun Heo074a7ac2008-08-25 19:56:14 +09003576 cpu = part_stat_lock();
3577 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3578 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3579 bio_sectors(bi));
3580 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
NeilBrown802ba062006-12-13 00:34:13 -08003582 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003583 mddev->reshape_position == MaxSector &&
3584 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003585 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3588 last_sector = bi->bi_sector + (bi->bi_size>>9);
3589 bi->bi_next = NULL;
3590 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3593 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003594 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003595 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003596
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003597 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003598 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08003599 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003600 if (likely(conf->expand_progress == MaxSector))
3601 disks = conf->raid_disks;
3602 else {
NeilBrowndf8e7f72006-03-27 01:18:15 -08003603 /* spinlock is needed as expand_progress may be
3604 * 64bit on a 32bit platform, and so it might be
3605 * possible to see a half-updated value
3606 * Ofcourse expand_progress could change after
3607 * the lock is dropped, so once we get a reference
3608 * to the stripe that we think it is, we will have
3609 * to check again.
3610 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003611 spin_lock_irq(&conf->device_lock);
3612 disks = conf->raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003613 if (logical_sector >= conf->expand_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003614 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003615 previous = 1;
3616 } else {
NeilBrownb578d552006-03-27 01:18:12 -08003617 if (logical_sector >= conf->expand_lo) {
3618 spin_unlock_irq(&conf->device_lock);
3619 schedule();
3620 goto retry;
3621 }
3622 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003623 spin_unlock_irq(&conf->device_lock);
3624 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003625 data_disks = disks - conf->max_degraded;
3626
NeilBrown112bf892009-03-31 14:39:38 +11003627 new_sector = raid5_compute_sector(conf, logical_sector,
3628 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003629 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003630 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 (unsigned long long)new_sector,
3632 (unsigned long long)logical_sector);
3633
NeilBrownb5663ba2009-03-31 14:39:38 +11003634 sh = get_active_stripe(conf, new_sector, previous,
3635 (bi->bi_rw&RWA_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 if (sh) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003637 if (unlikely(conf->expand_progress != MaxSector)) {
3638 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003639 * stripe, so we must do the range check again.
3640 * Expansion could still move past after this
3641 * test, but as we are holding a reference to
3642 * 'sh', we know that if that happens,
3643 * STRIPE_EXPANDING will get set and the expansion
3644 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003645 */
3646 int must_retry = 0;
3647 spin_lock_irq(&conf->device_lock);
3648 if (logical_sector < conf->expand_progress &&
3649 disks == conf->previous_raid_disks)
3650 /* mismatch, need to try again */
3651 must_retry = 1;
3652 spin_unlock_irq(&conf->device_lock);
3653 if (must_retry) {
3654 release_stripe(sh);
3655 goto retry;
3656 }
3657 }
NeilBrowne464eaf2006-03-27 01:18:14 -08003658 /* FIXME what if we get a false positive because these
3659 * are being updated.
3660 */
3661 if (logical_sector >= mddev->suspend_lo &&
3662 logical_sector < mddev->suspend_hi) {
3663 release_stripe(sh);
3664 schedule();
3665 goto retry;
3666 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003667
3668 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3669 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3670 /* Stripe is busy expanding or
3671 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 * and wait a while
3673 */
3674 raid5_unplug_device(mddev->queue);
3675 release_stripe(sh);
3676 schedule();
3677 goto retry;
3678 }
3679 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003680 set_bit(STRIPE_HANDLE, &sh->state);
3681 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 } else {
3684 /* cannot get stripe for read-ahead, just give-up */
3685 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3686 finish_wait(&conf->wait_for_overlap, &w);
3687 break;
3688 }
3689
3690 }
3691 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003692 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003693 spin_unlock_irq(&conf->device_lock);
3694 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
NeilBrown16a53ec2006-06-26 00:27:38 -07003696 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003698
Neil Brown0e13fe232008-06-28 08:31:20 +10003699 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 return 0;
3702}
3703
NeilBrown52c03292006-06-26 00:27:43 -07003704static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
NeilBrown52c03292006-06-26 00:27:43 -07003706 /* reshaping is quite different to recovery/resync so it is
3707 * handled quite separately ... here.
3708 *
3709 * On each call to sync_request, we gather one chunk worth of
3710 * destination stripes and flag them as expanding.
3711 * Then we find all the source stripes and request reads.
3712 * As the reads complete, handle_stripe will copy the data
3713 * into the destination stripe and release that stripe.
3714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3716 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003717 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003718 int raid_disks = conf->previous_raid_disks;
3719 int data_disks = raid_disks - conf->max_degraded;
3720 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003721 int i;
3722 int dd_idx;
3723 sector_t writepos, safepos, gap;
3724
3725 if (sector_nr == 0 &&
3726 conf->expand_progress != 0) {
3727 /* restarting in the middle, skip the initial sectors */
3728 sector_nr = conf->expand_progress;
NeilBrownf4168852007-02-28 20:11:53 -08003729 sector_div(sector_nr, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003730 *skipped = 1;
3731 return sector_nr;
3732 }
3733
3734 /* we update the metadata when there is more than 3Meg
3735 * in the block range (that is rather arbitrary, should
3736 * probably be time based) or when the data about to be
3737 * copied would over-write the source of the data at
3738 * the front of the range.
3739 * i.e. one new_stripe forward from expand_progress new_maps
3740 * to after where expand_lo old_maps to
3741 */
3742 writepos = conf->expand_progress +
NeilBrownf4168852007-02-28 20:11:53 -08003743 conf->chunk_size/512*(new_data_disks);
3744 sector_div(writepos, new_data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003745 safepos = conf->expand_lo;
NeilBrownf4168852007-02-28 20:11:53 -08003746 sector_div(safepos, data_disks);
NeilBrown52c03292006-06-26 00:27:43 -07003747 gap = conf->expand_progress - conf->expand_lo;
3748
3749 if (writepos >= safepos ||
NeilBrownf4168852007-02-28 20:11:53 -08003750 gap > (new_data_disks)*3000*2 /*3Meg*/) {
NeilBrown52c03292006-06-26 00:27:43 -07003751 /* Cannot proceed until we've updated the superblock... */
3752 wait_event(conf->wait_for_overlap,
3753 atomic_read(&conf->reshape_stripes)==0);
3754 mddev->reshape_position = conf->expand_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07003755 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07003756 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07003757 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07003758 kthread_should_stop());
3759 spin_lock_irq(&conf->device_lock);
3760 conf->expand_lo = mddev->reshape_position;
3761 spin_unlock_irq(&conf->device_lock);
3762 wake_up(&conf->wait_for_overlap);
3763 }
3764
3765 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
3766 int j;
3767 int skipped = 0;
NeilBrownb5663ba2009-03-31 14:39:38 +11003768 sh = get_active_stripe(conf, sector_nr+i, 0, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003769 set_bit(STRIPE_EXPANDING, &sh->state);
3770 atomic_inc(&conf->reshape_stripes);
3771 /* If any of this stripe is beyond the end of the old
3772 * array, then we need to zero those blocks
3773 */
3774 for (j=sh->disks; j--;) {
3775 sector_t s;
3776 if (j == sh->pd_idx)
3777 continue;
NeilBrownf4168852007-02-28 20:11:53 -08003778 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11003779 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08003780 continue;
NeilBrown52c03292006-06-26 00:27:43 -07003781 s = compute_blocknr(sh, j);
Andre Nollf233ea52008-07-21 17:05:22 +10003782 if (s < mddev->array_sectors) {
NeilBrown52c03292006-06-26 00:27:43 -07003783 skipped = 1;
3784 continue;
3785 }
3786 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
3787 set_bit(R5_Expanded, &sh->dev[j].flags);
3788 set_bit(R5_UPTODATE, &sh->dev[j].flags);
3789 }
3790 if (!skipped) {
3791 set_bit(STRIPE_EXPAND_READY, &sh->state);
3792 set_bit(STRIPE_HANDLE, &sh->state);
3793 }
3794 release_stripe(sh);
3795 }
3796 spin_lock_irq(&conf->device_lock);
NeilBrown6d3baf22007-03-05 00:30:44 -08003797 conf->expand_progress = (sector_nr + i) * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07003798 spin_unlock_irq(&conf->device_lock);
3799 /* Ok, those stripe are ready. We can start scheduling
3800 * reads on the source stripes.
3801 * The source stripes are determined by mapping the first and last
3802 * block on the destination stripes.
3803 */
NeilBrown52c03292006-06-26 00:27:43 -07003804 first_sector =
NeilBrown112bf892009-03-31 14:39:38 +11003805 raid5_compute_sector(conf, sector_nr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11003806 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07003807 last_sector =
NeilBrown112bf892009-03-31 14:39:38 +11003808 raid5_compute_sector(conf, ((sector_nr+conf->chunk_size/512)
3809 *(new_data_disks) - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11003810 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11003811 if (last_sector >= mddev->dev_sectors)
3812 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07003813 while (first_sector <= last_sector) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003814 sh = get_active_stripe(conf, first_sector, 1, 0);
NeilBrown52c03292006-06-26 00:27:43 -07003815 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3816 set_bit(STRIPE_HANDLE, &sh->state);
3817 release_stripe(sh);
3818 first_sector += STRIPE_SECTORS;
3819 }
NeilBrownc6207272008-02-06 01:39:52 -08003820 /* If this takes us to the resync_max point where we have to pause,
3821 * then we need to write out the superblock.
3822 */
3823 sector_nr += conf->chunk_size>>9;
3824 if (sector_nr >= mddev->resync_max) {
3825 /* Cannot proceed until we've updated the superblock... */
3826 wait_event(conf->wait_for_overlap,
3827 atomic_read(&conf->reshape_stripes) == 0);
3828 mddev->reshape_position = conf->expand_progress;
3829 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3830 md_wakeup_thread(mddev->thread);
3831 wait_event(mddev->sb_wait,
3832 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
3833 || kthread_should_stop());
3834 spin_lock_irq(&conf->device_lock);
3835 conf->expand_lo = mddev->reshape_position;
3836 spin_unlock_irq(&conf->device_lock);
3837 wake_up(&conf->wait_for_overlap);
3838 }
NeilBrown52c03292006-06-26 00:27:43 -07003839 return conf->chunk_size>>9;
3840}
3841
3842/* FIXME go_faster isn't used */
3843static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3844{
3845 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3846 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11003847 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07003848 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003849 int still_degraded = 0;
3850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
NeilBrown72626682005-09-09 16:23:54 -07003852 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 /* just being told to finish up .. nothing much to do */
3854 unplug_slaves(mddev);
NeilBrown29269552006-03-27 01:18:10 -08003855 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3856 end_reshape(conf);
3857 return 0;
3858 }
NeilBrown72626682005-09-09 16:23:54 -07003859
3860 if (mddev->curr_resync < max_sector) /* aborted */
3861 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3862 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07003863 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07003864 conf->fullsync = 0;
3865 bitmap_close_sync(mddev->bitmap);
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 return 0;
3868 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08003869
NeilBrown52c03292006-06-26 00:27:43 -07003870 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3871 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08003872
NeilBrownc6207272008-02-06 01:39:52 -08003873 /* No need to check resync_max as we never do more than one
3874 * stripe, and as resync_max will always be on a chunk boundary,
3875 * if the check in md_do_sync didn't fire, there is no chance
3876 * of overstepping resync_max here
3877 */
3878
NeilBrown16a53ec2006-06-26 00:27:38 -07003879 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 * to resync, then assert that we are finished, because there is
3881 * nothing we can do.
3882 */
NeilBrown3285edf2006-06-26 00:27:55 -07003883 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07003884 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003885 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07003886 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 return rv;
3888 }
NeilBrown72626682005-09-09 16:23:54 -07003889 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08003890 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07003891 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3892 /* we can skip this block, and probably more */
3893 sync_blocks /= STRIPE_SECTORS;
3894 *skipped = 1;
3895 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
NeilBrownb47490c2008-02-06 01:39:50 -08003898
3899 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
3900
NeilBrownb5663ba2009-03-31 14:39:38 +11003901 sh = get_active_stripe(conf, sector_nr, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 if (sh == NULL) {
NeilBrownb5663ba2009-03-31 14:39:38 +11003903 sh = get_active_stripe(conf, sector_nr, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07003905 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08003907 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003909 /* Need to check if array will still be degraded after recovery/resync
3910 * We don't need to check the 'failed' flag as when that gets set,
3911 * recovery aborts.
3912 */
3913 for (i=0; i<mddev->raid_disks; i++)
3914 if (conf->disks[i].rdev == NULL)
3915 still_degraded = 1;
3916
3917 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3918
3919 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 set_bit(STRIPE_SYNCING, &sh->state);
3921 clear_bit(STRIPE_INSYNC, &sh->state);
3922 spin_unlock(&sh->lock);
3923
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003924 /* wait for any blocked device to be handled */
3925 while(unlikely(!handle_stripe(sh, NULL)))
3926 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 release_stripe(sh);
3928
3929 return STRIPE_SECTORS;
3930}
3931
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003932static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3933{
3934 /* We may not be able to submit a whole bio at once as there
3935 * may not be enough stripe_heads available.
3936 * We cannot pre-allocate enough stripe_heads as we may need
3937 * more than exist in the cache (if we allow ever large chunks).
3938 * So we do one stripe head at a time and record in
3939 * ->bi_hw_segments how many have been done.
3940 *
3941 * We *know* that this entire raid_bio is in one chunk, so
3942 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3943 */
3944 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11003945 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003946 sector_t sector, logical_sector, last_sector;
3947 int scnt = 0;
3948 int remaining;
3949 int handled = 0;
3950
3951 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11003952 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11003953 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003954 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3955
3956 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08003957 logical_sector += STRIPE_SECTORS,
3958 sector += STRIPE_SECTORS,
3959 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003960
Jens Axboe960e7392008-08-15 10:41:18 +02003961 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003962 /* already done this stripe */
3963 continue;
3964
NeilBrownb5663ba2009-03-31 14:39:38 +11003965 sh = get_active_stripe(conf, sector, 0, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003966
3967 if (!sh) {
3968 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02003969 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003970 conf->retry_read_aligned = raid_bio;
3971 return handled;
3972 }
3973
3974 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08003975 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
3976 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02003977 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08003978 conf->retry_read_aligned = raid_bio;
3979 return handled;
3980 }
3981
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003982 handle_stripe(sh, NULL);
3983 release_stripe(sh);
3984 handled++;
3985 }
3986 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003987 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003988 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10003989 if (remaining == 0)
3990 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003991 if (atomic_dec_and_test(&conf->active_aligned_reads))
3992 wake_up(&conf->wait_for_stripe);
3993 return handled;
3994}
3995
3996
3997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998/*
3999 * This is our raid5 kernel thread.
4000 *
4001 * We scan the hash table for stripes which can be handled now.
4002 * During the scan, completed stripes are saved for us by the interrupt
4003 * handler, so that they will not have to wait for our next wakeup.
4004 */
NeilBrown6ed30032008-02-06 01:40:00 -08004005static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
4007 struct stripe_head *sh;
4008 raid5_conf_t *conf = mddev_to_conf(mddev);
4009 int handled;
4010
Dan Williams45b42332007-07-09 11:56:43 -07004011 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015 handled = 0;
4016 spin_lock_irq(&conf->device_lock);
4017 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004018 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
NeilBrownae3c20c2006-07-10 04:44:17 -07004020 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004021 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004022 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004023 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004024 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004025 conf->seq_write = seq;
4026 activate_bit_delay(conf);
4027 }
4028
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004029 while ((bio = remove_bio_from_retry(conf))) {
4030 int ok;
4031 spin_unlock_irq(&conf->device_lock);
4032 ok = retry_aligned_read(conf, bio);
4033 spin_lock_irq(&conf->device_lock);
4034 if (!ok)
4035 break;
4036 handled++;
4037 }
4038
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004039 sh = __get_priority_stripe(conf);
4040
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004041 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 spin_unlock_irq(&conf->device_lock);
4044
4045 handled++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004046 handle_stripe(sh, conf->spare_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 release_stripe(sh);
4048
4049 spin_lock_irq(&conf->device_lock);
4050 }
Dan Williams45b42332007-07-09 11:56:43 -07004051 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053 spin_unlock_irq(&conf->device_lock);
4054
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004055 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 unplug_slaves(mddev);
4057
Dan Williams45b42332007-07-09 11:56:43 -07004058 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
NeilBrown3f294f42005-11-08 21:39:25 -08004061static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004062raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004063{
NeilBrown007583c2005-11-08 21:39:30 -08004064 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004065 if (conf)
4066 return sprintf(page, "%d\n", conf->max_nr_stripes);
4067 else
4068 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004069}
4070
4071static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004072raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004073{
NeilBrown007583c2005-11-08 21:39:30 -08004074 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004075 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004076 int err;
4077
NeilBrown3f294f42005-11-08 21:39:25 -08004078 if (len >= PAGE_SIZE)
4079 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004080 if (!conf)
4081 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004082
Dan Williams4ef197d82008-04-28 02:15:54 -07004083 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004084 return -EINVAL;
4085 if (new <= 16 || new > 32768)
4086 return -EINVAL;
4087 while (new < conf->max_nr_stripes) {
4088 if (drop_one_stripe(conf))
4089 conf->max_nr_stripes--;
4090 else
4091 break;
4092 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004093 err = md_allow_write(mddev);
4094 if (err)
4095 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004096 while (new > conf->max_nr_stripes) {
4097 if (grow_one_stripe(conf))
4098 conf->max_nr_stripes++;
4099 else break;
4100 }
4101 return len;
4102}
NeilBrown007583c2005-11-08 21:39:30 -08004103
NeilBrown96de1e62005-11-08 21:39:39 -08004104static struct md_sysfs_entry
4105raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4106 raid5_show_stripe_cache_size,
4107 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004108
4109static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004110raid5_show_preread_threshold(mddev_t *mddev, char *page)
4111{
4112 raid5_conf_t *conf = mddev_to_conf(mddev);
4113 if (conf)
4114 return sprintf(page, "%d\n", conf->bypass_threshold);
4115 else
4116 return 0;
4117}
4118
4119static ssize_t
4120raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4121{
4122 raid5_conf_t *conf = mddev_to_conf(mddev);
Dan Williams4ef197d82008-04-28 02:15:54 -07004123 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004124 if (len >= PAGE_SIZE)
4125 return -EINVAL;
4126 if (!conf)
4127 return -ENODEV;
4128
Dan Williams4ef197d82008-04-28 02:15:54 -07004129 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004130 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004131 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004132 return -EINVAL;
4133 conf->bypass_threshold = new;
4134 return len;
4135}
4136
4137static struct md_sysfs_entry
4138raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4139 S_IRUGO | S_IWUSR,
4140 raid5_show_preread_threshold,
4141 raid5_store_preread_threshold);
4142
4143static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004144stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004145{
NeilBrown007583c2005-11-08 21:39:30 -08004146 raid5_conf_t *conf = mddev_to_conf(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08004147 if (conf)
4148 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4149 else
4150 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004151}
4152
NeilBrown96de1e62005-11-08 21:39:39 -08004153static struct md_sysfs_entry
4154raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004155
NeilBrown007583c2005-11-08 21:39:30 -08004156static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004157 &raid5_stripecache_size.attr,
4158 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004159 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004160 NULL,
4161};
NeilBrown007583c2005-11-08 21:39:30 -08004162static struct attribute_group raid5_attrs_group = {
4163 .name = NULL,
4164 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004165};
4166
NeilBrown91adb562009-03-31 14:39:39 +11004167static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
4169 raid5_conf_t *conf;
4170 int raid_disk, memory;
4171 mdk_rdev_t *rdev;
4172 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
NeilBrown91adb562009-03-31 14:39:39 +11004174 if (mddev->new_level != 5
4175 && mddev->new_level != 4
4176 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004177 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004178 mdname(mddev), mddev->new_level);
4179 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 }
NeilBrown91adb562009-03-31 14:39:39 +11004181 if ((mddev->new_level == 5
4182 && !algorithm_valid_raid5(mddev->new_layout)) ||
4183 (mddev->new_level == 6
4184 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004185 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004186 mdname(mddev), mddev->new_layout);
4187 return ERR_PTR(-EIO);
4188 }
4189 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4190 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4191 mdname(mddev), mddev->raid_disks);
4192 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
NeilBrown91adb562009-03-31 14:39:39 +11004195 if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) {
4196 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
4197 mddev->new_chunk, mdname(mddev));
4198 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004199 }
4200
NeilBrown91adb562009-03-31 14:39:39 +11004201 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4202 if (conf == NULL)
4203 goto abort;
4204
4205 conf->raid_disks = mddev->raid_disks;
4206 if (mddev->reshape_position == MaxSector)
4207 conf->previous_raid_disks = mddev->raid_disks;
4208 else
4209 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4210
4211 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4212 GFP_KERNEL);
4213 if (!conf->disks)
4214 goto abort;
4215
4216 conf->mddev = mddev;
4217
4218 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4219 goto abort;
4220
4221 if (mddev->new_level == 6) {
4222 conf->spare_page = alloc_page(GFP_KERNEL);
4223 if (!conf->spare_page)
4224 goto abort;
4225 }
4226 spin_lock_init(&conf->device_lock);
4227 init_waitqueue_head(&conf->wait_for_stripe);
4228 init_waitqueue_head(&conf->wait_for_overlap);
4229 INIT_LIST_HEAD(&conf->handle_list);
4230 INIT_LIST_HEAD(&conf->hold_list);
4231 INIT_LIST_HEAD(&conf->delayed_list);
4232 INIT_LIST_HEAD(&conf->bitmap_list);
4233 INIT_LIST_HEAD(&conf->inactive_list);
4234 atomic_set(&conf->active_stripes, 0);
4235 atomic_set(&conf->preread_active_stripes, 0);
4236 atomic_set(&conf->active_aligned_reads, 0);
4237 conf->bypass_threshold = BYPASS_THRESHOLD;
4238
4239 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4240
4241 list_for_each_entry(rdev, &mddev->disks, same_set) {
4242 raid_disk = rdev->raid_disk;
4243 if (raid_disk >= conf->raid_disks
4244 || raid_disk < 0)
4245 continue;
4246 disk = conf->disks + raid_disk;
4247
4248 disk->rdev = rdev;
4249
4250 if (test_bit(In_sync, &rdev->flags)) {
4251 char b[BDEVNAME_SIZE];
4252 printk(KERN_INFO "raid5: device %s operational as raid"
4253 " disk %d\n", bdevname(rdev->bdev,b),
4254 raid_disk);
4255 } else
4256 /* Cannot rely on bitmap to complete recovery */
4257 conf->fullsync = 1;
4258 }
4259
4260 conf->chunk_size = mddev->new_chunk;
4261 conf->level = mddev->new_level;
4262 if (conf->level == 6)
4263 conf->max_degraded = 2;
4264 else
4265 conf->max_degraded = 1;
4266 conf->algorithm = mddev->new_layout;
4267 conf->max_nr_stripes = NR_STRIPES;
4268 conf->expand_progress = mddev->reshape_position;
4269
4270 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4271 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4272 if (grow_stripes(conf, conf->max_nr_stripes)) {
4273 printk(KERN_ERR
4274 "raid5: couldn't allocate %dkB for buffers\n", memory);
4275 goto abort;
4276 } else
4277 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4278 memory, mdname(mddev));
4279
4280 conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4281 if (!conf->thread) {
4282 printk(KERN_ERR
4283 "raid5: couldn't allocate thread for %s\n",
4284 mdname(mddev));
4285 goto abort;
4286 }
4287
4288 return conf;
4289
4290 abort:
4291 if (conf) {
4292 shrink_stripes(conf);
4293 safe_put_page(conf->spare_page);
4294 kfree(conf->disks);
4295 kfree(conf->stripe_hashtbl);
4296 kfree(conf);
4297 return ERR_PTR(-EIO);
4298 } else
4299 return ERR_PTR(-ENOMEM);
4300}
4301
4302static int run(mddev_t *mddev)
4303{
4304 raid5_conf_t *conf;
4305 int working_disks = 0;
4306 mdk_rdev_t *rdev;
4307
NeilBrownf6705572006-03-27 01:18:11 -08004308 if (mddev->reshape_position != MaxSector) {
4309 /* Check that we can continue the reshape.
4310 * Currently only disks can change, it must
4311 * increase, and we must be past the point where
4312 * a stripe over-writes itself
4313 */
4314 sector_t here_new, here_old;
4315 int old_disks;
NeilBrownf4168852007-02-28 20:11:53 -08004316 int max_degraded = (mddev->level == 5 ? 1 : 2);
NeilBrownf6705572006-03-27 01:18:11 -08004317
4318 if (mddev->new_level != mddev->level ||
4319 mddev->new_layout != mddev->layout ||
4320 mddev->new_chunk != mddev->chunk_size) {
NeilBrownf4168852007-02-28 20:11:53 -08004321 printk(KERN_ERR "raid5: %s: unsupported reshape "
4322 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004323 mdname(mddev));
4324 return -EINVAL;
4325 }
4326 if (mddev->delta_disks <= 0) {
NeilBrownf4168852007-02-28 20:11:53 -08004327 printk(KERN_ERR "raid5: %s: unsupported reshape "
4328 "(reduce disks) required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004329 mdname(mddev));
4330 return -EINVAL;
4331 }
4332 old_disks = mddev->raid_disks - mddev->delta_disks;
4333 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004334 * further up in new geometry must map after here in old
4335 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004336 */
4337 here_new = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004338 if (sector_div(here_new, (mddev->chunk_size>>9)*
4339 (mddev->raid_disks - max_degraded))) {
4340 printk(KERN_ERR "raid5: reshape_position not "
4341 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004342 return -EINVAL;
4343 }
4344 /* here_new is the stripe we will write to */
4345 here_old = mddev->reshape_position;
NeilBrownf4168852007-02-28 20:11:53 -08004346 sector_div(here_old, (mddev->chunk_size>>9)*
4347 (old_disks-max_degraded));
4348 /* here_old is the first stripe that we might need to read
4349 * from */
NeilBrownf6705572006-03-27 01:18:11 -08004350 if (here_new >= here_old) {
4351 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004352 printk(KERN_ERR "raid5: reshape_position too early for "
4353 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004354 return -EINVAL;
4355 }
4356 printk(KERN_INFO "raid5: reshape will continue\n");
4357 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004358 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004359 BUG_ON(mddev->level != mddev->new_level);
4360 BUG_ON(mddev->layout != mddev->new_layout);
4361 BUG_ON(mddev->chunk_size != mddev->new_chunk);
4362 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004363 }
NeilBrown91adb562009-03-31 14:39:39 +11004364 conf = setup_conf(mddev);
NeilBrownf6705572006-03-27 01:18:11 -08004365
NeilBrown91adb562009-03-31 14:39:39 +11004366 if (conf == NULL)
4367 return -EIO;
4368 if (IS_ERR(conf))
4369 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004370
NeilBrown91adb562009-03-31 14:39:39 +11004371 mddev->thread = conf->thread;
4372 conf->thread = NULL;
4373 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004376 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 */
NeilBrown91adb562009-03-31 14:39:39 +11004378 list_for_each_entry(rdev, &mddev->disks, same_set)
4379 if (rdev->raid_disk >= 0 &&
4380 test_bit(In_sync, &rdev->flags))
4381 working_disks++;
4382
NeilBrown02c2de82006-10-03 01:15:47 -07004383 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
NeilBrown16a53ec2006-06-26 00:27:38 -07004385 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 printk(KERN_ERR "raid5: not enough operational devices for %s"
4387 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004388 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 goto abort;
4390 }
4391
NeilBrown91adb562009-03-31 14:39:39 +11004392 /* device size must be a multiple of chunk size */
4393 mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
4394 mddev->resync_max_sectors = mddev->dev_sectors;
4395
NeilBrown16a53ec2006-06-26 00:27:38 -07004396 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004398 if (mddev->ok_start_degraded)
4399 printk(KERN_WARNING
4400 "raid5: starting dirty degraded array: %s"
4401 "- data corruption possible.\n",
4402 mdname(mddev));
4403 else {
4404 printk(KERN_ERR
4405 "raid5: cannot start dirty degraded array for %s\n",
4406 mdname(mddev));
4407 goto abort;
4408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 if (mddev->degraded == 0)
4412 printk("raid5: raid level %d set %s active with %d out of %d"
4413 " devices, algorithm %d\n", conf->level, mdname(mddev),
4414 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4415 conf->algorithm);
4416 else
4417 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4418 " out of %d devices, algorithm %d\n", conf->level,
4419 mdname(mddev), mddev->raid_disks - mddev->degraded,
4420 mddev->raid_disks, conf->algorithm);
4421
4422 print_raid5_conf(conf);
4423
NeilBrownf6705572006-03-27 01:18:11 -08004424 if (conf->expand_progress != MaxSector) {
4425 printk("...ok start reshape thread\n");
NeilBrownb578d552006-03-27 01:18:12 -08004426 conf->expand_lo = conf->expand_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004427 atomic_set(&conf->reshape_stripes, 0);
4428 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4429 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4430 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4431 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4432 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4433 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004434 }
4435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004437 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 */
4439 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004440 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4441 int stripe = data_disks *
NeilBrown8932c2e2006-06-26 00:27:36 -07004442 (mddev->chunk_size / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4444 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4445 }
4446
4447 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004448 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4449 printk(KERN_WARNING
4450 "raid5: failed to create sysfs attributes for %s\n",
4451 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004452
NeilBrown91adb562009-03-31 14:39:39 +11004453 mddev->queue->queue_lock = &conf->device_lock;
4454
NeilBrown7a5febe2005-05-16 21:53:16 -07004455 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004456 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004457 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004458
Andre Noll58c0fed2009-03-31 14:33:13 +11004459 mddev->array_sectors = mddev->dev_sectors *
4460 (conf->previous_raid_disks - conf->max_degraded);
NeilBrown7a5febe2005-05-16 21:53:16 -07004461
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004462 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return 0;
4465abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11004466 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11004467 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 if (conf) {
NeilBrown91adb562009-03-31 14:39:39 +11004469 shrink_stripes(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 print_raid5_conf(conf);
NeilBrown16a53ec2006-06-26 00:27:38 -07004471 safe_put_page(conf->spare_page);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004472 kfree(conf->disks);
NeilBrownfccddba2006-01-06 00:20:33 -08004473 kfree(conf->stripe_hashtbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 kfree(conf);
4475 }
4476 mddev->private = NULL;
4477 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
4478 return -EIO;
4479}
4480
4481
4482
NeilBrown3f294f42005-11-08 21:39:25 -08004483static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484{
4485 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4486
4487 md_unregister_thread(mddev->thread);
4488 mddev->thread = NULL;
4489 shrink_stripes(conf);
NeilBrownfccddba2006-01-06 00:20:33 -08004490 kfree(conf->stripe_hashtbl);
NeilBrown041ae522007-03-26 21:32:14 -08004491 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08004493 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
NeilBrownb55e6bf2006-03-27 01:18:06 -08004494 kfree(conf->disks);
NeilBrown96de1e62005-11-08 21:39:39 -08004495 kfree(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 mddev->private = NULL;
4497 return 0;
4498}
4499
Dan Williams45b42332007-07-09 11:56:43 -07004500#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11004501static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
4503 int i;
4504
NeilBrown16a53ec2006-06-26 00:27:38 -07004505 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4506 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4507 seq_printf(seq, "sh %llu, count %d.\n",
4508 (unsigned long long)sh->sector, atomic_read(&sh->count));
4509 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004510 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004511 seq_printf(seq, "(cache%d: %p %ld) ",
4512 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004514 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
NeilBrownd710e132008-10-13 11:55:12 +11004517static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
4519 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08004520 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 int i;
4522
4523 spin_lock_irq(&conf->device_lock);
4524 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08004525 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 if (sh->raid_conf != conf)
4527 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07004528 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 }
4530 }
4531 spin_unlock_irq(&conf->device_lock);
4532}
4533#endif
4534
NeilBrownd710e132008-10-13 11:55:12 +11004535static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
4537 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4538 int i;
4539
4540 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07004541 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 for (i = 0; i < conf->raid_disks; i++)
4543 seq_printf (seq, "%s",
4544 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004545 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07004547#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07004548 seq_printf (seq, "\n");
4549 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550#endif
4551}
4552
4553static void print_raid5_conf (raid5_conf_t *conf)
4554{
4555 int i;
4556 struct disk_info *tmp;
4557
4558 printk("RAID5 conf printout:\n");
4559 if (!conf) {
4560 printk("(conf==NULL)\n");
4561 return;
4562 }
NeilBrown02c2de82006-10-03 01:15:47 -07004563 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
4564 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 for (i = 0; i < conf->raid_disks; i++) {
4567 char b[BDEVNAME_SIZE];
4568 tmp = conf->disks + i;
4569 if (tmp->rdev)
4570 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08004571 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 bdevname(tmp->rdev->bdev,b));
4573 }
4574}
4575
4576static int raid5_spare_active(mddev_t *mddev)
4577{
4578 int i;
4579 raid5_conf_t *conf = mddev->private;
4580 struct disk_info *tmp;
4581
4582 for (i = 0; i < conf->raid_disks; i++) {
4583 tmp = conf->disks + i;
4584 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08004585 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07004586 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
4587 unsigned long flags;
4588 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07004590 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 }
4592 }
4593 print_raid5_conf(conf);
4594 return 0;
4595}
4596
4597static int raid5_remove_disk(mddev_t *mddev, int number)
4598{
4599 raid5_conf_t *conf = mddev->private;
4600 int err = 0;
4601 mdk_rdev_t *rdev;
4602 struct disk_info *p = conf->disks + number;
4603
4604 print_raid5_conf(conf);
4605 rdev = p->rdev;
4606 if (rdev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004607 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 atomic_read(&rdev->nr_pending)) {
4609 err = -EBUSY;
4610 goto abort;
4611 }
NeilBrowndfc70642008-05-23 13:04:39 -07004612 /* Only remove non-faulty devices if recovery
4613 * isn't possible.
4614 */
4615 if (!test_bit(Faulty, &rdev->flags) &&
4616 mddev->degraded <= conf->max_degraded) {
4617 err = -EBUSY;
4618 goto abort;
4619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004621 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (atomic_read(&rdev->nr_pending)) {
4623 /* lost the race, try later */
4624 err = -EBUSY;
4625 p->rdev = rdev;
4626 }
4627 }
4628abort:
4629
4630 print_raid5_conf(conf);
4631 return err;
4632}
4633
4634static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
4635{
4636 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10004637 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 int disk;
4639 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10004640 int first = 0;
4641 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
NeilBrown16a53ec2006-06-26 00:27:38 -07004643 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10004645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Neil Brown6c2fce22008-06-28 08:31:31 +10004647 if (rdev->raid_disk >= 0)
4648 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004651 * find the disk ... but prefer rdev->saved_raid_disk
4652 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004654 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10004655 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004656 conf->disks[rdev->saved_raid_disk].rdev == NULL)
4657 disk = rdev->saved_raid_disk;
4658 else
Neil Brown6c2fce22008-06-28 08:31:31 +10004659 disk = first;
4660 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004662 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10004664 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07004665 if (rdev->saved_raid_disk != disk)
4666 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08004667 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 break;
4669 }
4670 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10004671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
4673
4674static int raid5_resize(mddev_t *mddev, sector_t sectors)
4675{
4676 /* no resync is happening, and there is enough space
4677 * on all devices, so we can resize.
4678 * We need to make sure resync covers any new space.
4679 * If the array is shrinking we should possibly wait until
4680 * any io in the removed space completes, but it hardly seems
4681 * worth it.
4682 */
NeilBrown16a53ec2006-06-26 00:27:38 -07004683 raid5_conf_t *conf = mddev_to_conf(mddev);
4684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
Andre Nollf233ea52008-07-21 17:05:22 +10004686 mddev->array_sectors = sectors * (mddev->raid_disks
4687 - conf->max_degraded);
4688 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004689 mddev->changed = 1;
Andre Noll58c0fed2009-03-31 14:33:13 +11004690 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
4691 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4693 }
Andre Noll58c0fed2009-03-31 14:33:13 +11004694 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07004695 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return 0;
4697}
4698
NeilBrown29269552006-03-27 01:18:10 -08004699#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004700static int raid5_check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08004701{
4702 raid5_conf_t *conf = mddev_to_conf(mddev);
4703 int err;
NeilBrown29269552006-03-27 01:18:10 -08004704
NeilBrown63c70c42006-03-27 01:18:13 -08004705 if (mddev->delta_disks < 0 ||
4706 mddev->new_level != mddev->level)
4707 return -EINVAL; /* Cannot shrink array or change level yet */
4708 if (mddev->delta_disks == 0)
NeilBrown29269552006-03-27 01:18:10 -08004709 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10004710 if (mddev->bitmap)
4711 /* Cannot grow a bitmap yet */
4712 return -EBUSY;
NeilBrown29269552006-03-27 01:18:10 -08004713
4714 /* Can only proceed if there are plenty of stripe_heads.
4715 * We need a minimum of one full stripe,, and for sensible progress
4716 * it is best to have about 4 times that.
4717 * If we require 4 times, then the default 256 4K stripe_heads will
4718 * allow for chunk sizes up to 256K, which is probably OK.
4719 * If the chunk size is greater, user-space should request more
4720 * stripe_heads first.
4721 */
NeilBrown63c70c42006-03-27 01:18:13 -08004722 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
4723 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
NeilBrown29269552006-03-27 01:18:10 -08004724 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
4725 (mddev->chunk_size / STRIPE_SIZE)*4);
4726 return -ENOSPC;
4727 }
4728
NeilBrown63c70c42006-03-27 01:18:13 -08004729 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
4730 if (err)
4731 return err;
4732
NeilBrownb4c4c7b2007-02-28 20:11:48 -08004733 if (mddev->degraded > conf->max_degraded)
4734 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004735 /* looks like we might be able to manage this */
4736 return 0;
4737}
4738
4739static int raid5_start_reshape(mddev_t *mddev)
4740{
4741 raid5_conf_t *conf = mddev_to_conf(mddev);
4742 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08004743 int spares = 0;
4744 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07004745 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08004746
NeilBrownf4168852007-02-28 20:11:53 -08004747 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08004748 return -EBUSY;
4749
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004750 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08004751 if (rdev->raid_disk < 0 &&
4752 !test_bit(Faulty, &rdev->flags))
4753 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08004754
NeilBrownf4168852007-02-28 20:11:53 -08004755 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08004756 /* Not enough devices even to make a degraded array
4757 * of that size
4758 */
4759 return -EINVAL;
4760
NeilBrownf6705572006-03-27 01:18:11 -08004761 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08004762 spin_lock_irq(&conf->device_lock);
4763 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08004764 conf->raid_disks += mddev->delta_disks;
NeilBrown29269552006-03-27 01:18:10 -08004765 conf->expand_progress = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004766 conf->expand_lo = 0;
NeilBrown29269552006-03-27 01:18:10 -08004767 spin_unlock_irq(&conf->device_lock);
4768
4769 /* Add some new drives, as many as will fit.
4770 * We know there are enough to make the newly sized array work.
4771 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004772 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08004773 if (rdev->raid_disk < 0 &&
4774 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10004775 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08004776 char nm[20];
4777 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08004778 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004779 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08004780 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004781 if (sysfs_create_link(&mddev->kobj,
4782 &rdev->kobj, nm))
4783 printk(KERN_WARNING
4784 "raid5: failed to create "
4785 " link %s for %s\n",
4786 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08004787 } else
4788 break;
4789 }
4790
NeilBrownc04be0a2006-10-03 01:15:53 -07004791 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08004792 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
NeilBrownc04be0a2006-10-03 01:15:53 -07004793 spin_unlock_irqrestore(&conf->device_lock, flags);
NeilBrown63c70c42006-03-27 01:18:13 -08004794 mddev->raid_disks = conf->raid_disks;
NeilBrownf6705572006-03-27 01:18:11 -08004795 mddev->reshape_position = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004796 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08004797
NeilBrown29269552006-03-27 01:18:10 -08004798 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4799 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4800 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4801 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4802 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4803 "%s_reshape");
4804 if (!mddev->sync_thread) {
4805 mddev->recovery = 0;
4806 spin_lock_irq(&conf->device_lock);
4807 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
4808 conf->expand_progress = MaxSector;
4809 spin_unlock_irq(&conf->device_lock);
4810 return -EAGAIN;
4811 }
4812 md_wakeup_thread(mddev->sync_thread);
4813 md_new_event(mddev);
4814 return 0;
4815}
4816#endif
4817
4818static void end_reshape(raid5_conf_t *conf)
4819{
4820 struct block_device *bdev;
4821
NeilBrownf6705572006-03-27 01:18:11 -08004822 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004823 conf->mddev->array_sectors = conf->mddev->dev_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004824 (conf->raid_disks - conf->max_degraded);
Andre Nollf233ea52008-07-21 17:05:22 +10004825 set_capacity(conf->mddev->gendisk, conf->mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004826 conf->mddev->changed = 1;
NeilBrown29269552006-03-27 01:18:10 -08004827
NeilBrownf6705572006-03-27 01:18:11 -08004828 bdev = bdget_disk(conf->mddev->gendisk, 0);
4829 if (bdev) {
4830 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004831 i_size_write(bdev->bd_inode,
4832 (loff_t)conf->mddev->array_sectors << 9);
NeilBrownf6705572006-03-27 01:18:11 -08004833 mutex_unlock(&bdev->bd_inode->i_mutex);
4834 bdput(bdev);
4835 }
4836 spin_lock_irq(&conf->device_lock);
4837 conf->expand_progress = MaxSector;
4838 spin_unlock_irq(&conf->device_lock);
4839 conf->mddev->reshape_position = MaxSector;
NeilBrown16a53ec2006-06-26 00:27:38 -07004840
4841 /* read-ahead size must cover two whole stripes, which is
4842 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4843 */
4844 {
4845 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4846 int stripe = data_disks *
4847 (conf->mddev->chunk_size / PAGE_SIZE);
4848 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4849 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4850 }
NeilBrown29269552006-03-27 01:18:10 -08004851 }
NeilBrown29269552006-03-27 01:18:10 -08004852}
4853
NeilBrown72626682005-09-09 16:23:54 -07004854static void raid5_quiesce(mddev_t *mddev, int state)
4855{
4856 raid5_conf_t *conf = mddev_to_conf(mddev);
4857
4858 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08004859 case 2: /* resume for a suspend */
4860 wake_up(&conf->wait_for_overlap);
4861 break;
4862
NeilBrown72626682005-09-09 16:23:54 -07004863 case 1: /* stop all writes */
4864 spin_lock_irq(&conf->device_lock);
4865 conf->quiesce = 1;
4866 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004867 atomic_read(&conf->active_stripes) == 0 &&
4868 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07004869 conf->device_lock, /* nothing */);
4870 spin_unlock_irq(&conf->device_lock);
4871 break;
4872
4873 case 0: /* re-enable writes */
4874 spin_lock_irq(&conf->device_lock);
4875 conf->quiesce = 0;
4876 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08004877 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07004878 spin_unlock_irq(&conf->device_lock);
4879 break;
4880 }
NeilBrown72626682005-09-09 16:23:54 -07004881}
NeilBrownb15c2e52006-01-06 00:20:16 -08004882
NeilBrown16a53ec2006-06-26 00:27:38 -07004883static struct mdk_personality raid6_personality =
4884{
4885 .name = "raid6",
4886 .level = 6,
4887 .owner = THIS_MODULE,
4888 .make_request = make_request,
4889 .run = run,
4890 .stop = stop,
4891 .status = status,
4892 .error_handler = error,
4893 .hot_add_disk = raid5_add_disk,
4894 .hot_remove_disk= raid5_remove_disk,
4895 .spare_active = raid5_spare_active,
4896 .sync_request = sync_request,
4897 .resize = raid5_resize,
NeilBrownf4168852007-02-28 20:11:53 -08004898#ifdef CONFIG_MD_RAID5_RESHAPE
4899 .check_reshape = raid5_check_reshape,
4900 .start_reshape = raid5_start_reshape,
4901#endif
NeilBrown16a53ec2006-06-26 00:27:38 -07004902 .quiesce = raid5_quiesce,
4903};
NeilBrown2604b702006-01-06 00:20:36 -08004904static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
4906 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08004907 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 .owner = THIS_MODULE,
4909 .make_request = make_request,
4910 .run = run,
4911 .stop = stop,
4912 .status = status,
4913 .error_handler = error,
4914 .hot_add_disk = raid5_add_disk,
4915 .hot_remove_disk= raid5_remove_disk,
4916 .spare_active = raid5_spare_active,
4917 .sync_request = sync_request,
4918 .resize = raid5_resize,
NeilBrown29269552006-03-27 01:18:10 -08004919#ifdef CONFIG_MD_RAID5_RESHAPE
NeilBrown63c70c42006-03-27 01:18:13 -08004920 .check_reshape = raid5_check_reshape,
4921 .start_reshape = raid5_start_reshape,
NeilBrown29269552006-03-27 01:18:10 -08004922#endif
NeilBrown72626682005-09-09 16:23:54 -07004923 .quiesce = raid5_quiesce,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924};
4925
NeilBrown2604b702006-01-06 00:20:36 -08004926static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
NeilBrown2604b702006-01-06 00:20:36 -08004928 .name = "raid4",
4929 .level = 4,
4930 .owner = THIS_MODULE,
4931 .make_request = make_request,
4932 .run = run,
4933 .stop = stop,
4934 .status = status,
4935 .error_handler = error,
4936 .hot_add_disk = raid5_add_disk,
4937 .hot_remove_disk= raid5_remove_disk,
4938 .spare_active = raid5_spare_active,
4939 .sync_request = sync_request,
4940 .resize = raid5_resize,
NeilBrown3d378902007-03-26 21:32:13 -08004941#ifdef CONFIG_MD_RAID5_RESHAPE
4942 .check_reshape = raid5_check_reshape,
4943 .start_reshape = raid5_start_reshape,
4944#endif
NeilBrown2604b702006-01-06 00:20:36 -08004945 .quiesce = raid5_quiesce,
4946};
4947
4948static int __init raid5_init(void)
4949{
NeilBrown16a53ec2006-06-26 00:27:38 -07004950 int e;
4951
4952 e = raid6_select_algo();
4953 if ( e )
4954 return e;
4955 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004956 register_md_personality(&raid5_personality);
4957 register_md_personality(&raid4_personality);
4958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960
NeilBrown2604b702006-01-06 00:20:36 -08004961static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962{
NeilBrown16a53ec2006-06-26 00:27:38 -07004963 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08004964 unregister_md_personality(&raid5_personality);
4965 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
4967
4968module_init(raid5_init);
4969module_exit(raid5_exit);
4970MODULE_LICENSE("GPL");
4971MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08004972MODULE_ALIAS("md-raid5");
4973MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08004974MODULE_ALIAS("md-level-5");
4975MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07004976MODULE_ALIAS("md-personality-8"); /* RAID6 */
4977MODULE_ALIAS("md-raid6");
4978MODULE_ALIAS("md-level-6");
4979
4980/* This used to be two separate modules, they were: */
4981MODULE_ALIAS("raid5");
4982MODULE_ALIAS("raid6");