blob: d4ce51b4d41b7b5f289a5c1737a2422cc9898b9c [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 Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Dan Williams07a3b412009-08-29 19:13:13 -070050#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070052#include <linux/cpu.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110053#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110054#include "raid5.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Stripe cache
59 */
60
61#define NR_STRIPES 256
62#define STRIPE_SIZE PAGE_SIZE
63#define STRIPE_SHIFT (PAGE_SHIFT - 9)
64#define STRIPE_SECTORS (STRIPE_SIZE>>9)
65#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070066#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080067#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define HASH_MASK (NR_HASH - 1)
69
NeilBrownfccddba2006-01-06 00:20:33 -080070#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* bio's attached to a stripe+device for I/O are linked together in bi_sector
73 * order without overlap. There may be several bio's per stripe+device, and
74 * a bio could span several devices.
75 * When walking this list for a particular stripe+device, we must never proceed
76 * beyond a bio that extends past this device, as the next bio might no longer
77 * be valid.
78 * This macro is used to determine the 'next' bio in the list, given the sector
79 * of the current stripe+device
80 */
81#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
82/*
83 * The following can be used to debug the driver
84 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define RAID5_PARANOIA 1
86#if RAID5_PARANOIA && defined(CONFIG_SMP)
87# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
88#else
89# define CHECK_DEVLOCK()
90#endif
91
Dan Williams45b42332007-07-09 11:56:43 -070092#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define inline
94#define __inline__
95#endif
96
Bernd Schubert6be9d492008-05-23 13:04:34 -070097#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
98
Jens Axboe960e7392008-08-15 10:41:18 +020099/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200100 * We maintain a biased count of active stripes in the bottom 16 bits of
101 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200102 */
103static inline int raid5_bi_phys_segments(struct bio *bio)
104{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200105 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
108static inline int raid5_bi_hw_segments(struct bio *bio)
109{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200110 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200111}
112
113static inline int raid5_dec_bi_phys_segments(struct bio *bio)
114{
115 --bio->bi_phys_segments;
116 return raid5_bi_phys_segments(bio);
117}
118
119static inline int raid5_dec_bi_hw_segments(struct bio *bio)
120{
121 unsigned short val = raid5_bi_hw_segments(bio);
122
123 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200124 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200125 return val;
126}
127
128static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
129{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200130 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200131}
132
NeilBrownd0dabf72009-03-31 14:39:38 +1100133/* Find first data disk in a raid6 stripe */
134static inline int raid6_d0(struct stripe_head *sh)
135{
NeilBrown67cc2b82009-03-31 14:39:38 +1100136 if (sh->ddf_layout)
137 /* ddf always start from first device */
138 return 0;
139 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100140 if (sh->qd_idx == sh->disks - 1)
141 return 0;
142 else
143 return sh->qd_idx + 1;
144}
NeilBrown16a53ec2006-06-26 00:27:38 -0700145static inline int raid6_next_disk(int disk, int raid_disks)
146{
147 disk++;
148 return (disk < raid_disks) ? disk : 0;
149}
Dan Williamsa4456852007-07-09 11:56:43 -0700150
NeilBrownd0dabf72009-03-31 14:39:38 +1100151/* When walking through the disks in a raid5, starting at raid6_d0,
152 * We need to map each disk to a 'slot', where the data disks are slot
153 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
154 * is raid_disks-1. This help does that mapping.
155 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100156static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
157 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100158{
159 int slot;
NeilBrown67cc2b82009-03-31 14:39:38 +1100160
NeilBrowne4424fe2009-10-16 16:27:34 +1100161 if (sh->ddf_layout)
162 slot = (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100163 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100164 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100165 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100166 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100167 if (!sh->ddf_layout)
168 slot = (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100169 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
NeilBrown784052e2009-03-31 15:19:07 +1100307static void raid5_build_block(struct stripe_head *sh, int i, int previous);
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
NeilBrown86b42c72009-03-31 15:19:03 +1100326 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100327 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100329 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 sh->state = 0;
331
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800332
333 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct r5dev *dev = &sh->dev[i];
335
Dan Williamsd84e0f12007-01-02 13:52:30 -0700336 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700338 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700340 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 test_bit(R5_LOCKED, &dev->flags));
342 BUG();
343 }
344 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100345 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 insert_hash(conf, sh);
348}
349
NeilBrown86b42c72009-03-31 15:19:03 +1100350static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
351 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800354 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700357 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800358 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100359 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700361 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return NULL;
363}
364
365static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200366static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
NeilBrownb5663ba2009-03-31 14:39:38 +1100368static struct stripe_head *
369get_active_stripe(raid5_conf_t *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000370 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct stripe_head *sh;
373
Dan Williams45b42332007-07-09 11:56:43 -0700374 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 spin_lock_irq(&conf->device_lock);
377
378 do {
NeilBrown72626682005-09-09 16:23:54 -0700379 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000380 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700381 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100382 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!sh) {
384 if (!conf->inactive_blocked)
385 sh = get_free_stripe(conf);
386 if (noblock && sh == NULL)
387 break;
388 if (!sh) {
389 conf->inactive_blocked = 1;
390 wait_event_lock_irq(conf->wait_for_stripe,
391 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800392 (atomic_read(&conf->active_stripes)
393 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 || !conf->inactive_blocked),
395 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700396 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 );
398 conf->inactive_blocked = 0;
399 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100400 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 } else {
402 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100403 BUG_ON(!list_empty(&sh->lru)
404 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 } else {
406 if (!test_bit(STRIPE_HANDLE, &sh->state))
407 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700408 if (list_empty(&sh->lru) &&
409 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700410 BUG();
411 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
414 } while (sh == NULL);
415
416 if (sh)
417 atomic_inc(&sh->count);
418
419 spin_unlock_irq(&conf->device_lock);
420 return sh;
421}
422
NeilBrown6712ecf2007-09-27 12:47:43 +0200423static void
424raid5_end_read_request(struct bio *bi, int error);
425static void
426raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700427
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000428static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700429{
430 raid5_conf_t *conf = sh->raid_conf;
431 int i, disks = sh->disks;
432
433 might_sleep();
434
435 for (i = disks; i--; ) {
436 int rw;
437 struct bio *bi;
438 mdk_rdev_t *rdev;
439 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
440 rw = WRITE;
441 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
442 rw = READ;
443 else
444 continue;
445
446 bi = &sh->dev[i].req;
447
448 bi->bi_rw = rw;
449 if (rw == WRITE)
450 bi->bi_end_io = raid5_end_write_request;
451 else
452 bi->bi_end_io = raid5_end_read_request;
453
454 rcu_read_lock();
455 rdev = rcu_dereference(conf->disks[i].rdev);
456 if (rdev && test_bit(Faulty, &rdev->flags))
457 rdev = NULL;
458 if (rdev)
459 atomic_inc(&rdev->nr_pending);
460 rcu_read_unlock();
461
462 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000463 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700464 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
465
Dan Williams2b7497f2008-06-28 08:31:52 +1000466 set_bit(STRIPE_IO_STARTED, &sh->state);
467
Dan Williams91c00922007-01-02 13:52:30 -0700468 bi->bi_bdev = rdev->bdev;
469 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700470 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700471 bi->bi_rw, i);
472 atomic_inc(&sh->count);
473 bi->bi_sector = sh->sector + rdev->data_offset;
474 bi->bi_flags = 1 << BIO_UPTODATE;
475 bi->bi_vcnt = 1;
476 bi->bi_max_vecs = 1;
477 bi->bi_idx = 0;
478 bi->bi_io_vec = &sh->dev[i].vec;
479 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
480 bi->bi_io_vec[0].bv_offset = 0;
481 bi->bi_size = STRIPE_SIZE;
482 bi->bi_next = NULL;
483 if (rw == WRITE &&
484 test_bit(R5_ReWrite, &sh->dev[i].flags))
485 atomic_add(STRIPE_SECTORS,
486 &rdev->corrected_errors);
487 generic_make_request(bi);
488 } else {
489 if (rw == WRITE)
490 set_bit(STRIPE_DEGRADED, &sh->state);
491 pr_debug("skip op %ld on disc %d for sector %llu\n",
492 bi->bi_rw, i, (unsigned long long)sh->sector);
493 clear_bit(R5_LOCKED, &sh->dev[i].flags);
494 set_bit(STRIPE_HANDLE, &sh->state);
495 }
496 }
497}
498
499static struct dma_async_tx_descriptor *
500async_copy_data(int frombio, struct bio *bio, struct page *page,
501 sector_t sector, struct dma_async_tx_descriptor *tx)
502{
503 struct bio_vec *bvl;
504 struct page *bio_page;
505 int i;
506 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700507 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700508 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700509
510 if (bio->bi_sector >= sector)
511 page_offset = (signed)(bio->bi_sector - sector) * 512;
512 else
513 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700514
Dan Williams0403e382009-09-08 17:42:50 -0700515 if (frombio)
516 flags |= ASYNC_TX_FENCE;
517 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
518
Dan Williams91c00922007-01-02 13:52:30 -0700519 bio_for_each_segment(bvl, bio, i) {
520 int len = bio_iovec_idx(bio, i)->bv_len;
521 int clen;
522 int b_offset = 0;
523
524 if (page_offset < 0) {
525 b_offset = -page_offset;
526 page_offset += b_offset;
527 len -= b_offset;
528 }
529
530 if (len > 0 && page_offset + len > STRIPE_SIZE)
531 clen = STRIPE_SIZE - page_offset;
532 else
533 clen = len;
534
535 if (clen > 0) {
536 b_offset += bio_iovec_idx(bio, i)->bv_offset;
537 bio_page = bio_iovec_idx(bio, i)->bv_page;
538 if (frombio)
539 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700540 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700541 else
542 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700543 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700544 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700545 /* chain the operations */
546 submit.depend_tx = tx;
547
Dan Williams91c00922007-01-02 13:52:30 -0700548 if (clen < len) /* hit end of page */
549 break;
550 page_offset += len;
551 }
552
553 return tx;
554}
555
556static void ops_complete_biofill(void *stripe_head_ref)
557{
558 struct stripe_head *sh = stripe_head_ref;
559 struct bio *return_bi = NULL;
560 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700561 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700562
Harvey Harrisone46b2722008-04-28 02:15:50 -0700563 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700564 (unsigned long long)sh->sector);
565
566 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000567 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700568 for (i = sh->disks; i--; ) {
569 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700570
571 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700572 /* and check if we need to reply to a read request,
573 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000574 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700575 */
576 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700577 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700578
Dan Williams91c00922007-01-02 13:52:30 -0700579 BUG_ON(!dev->read);
580 rbi = dev->read;
581 dev->read = NULL;
582 while (rbi && rbi->bi_sector <
583 dev->sector + STRIPE_SECTORS) {
584 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200585 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700586 rbi->bi_next = return_bi;
587 return_bi = rbi;
588 }
Dan Williams91c00922007-01-02 13:52:30 -0700589 rbi = rbi2;
590 }
591 }
592 }
Dan Williams83de75c2008-06-28 08:31:58 +1000593 spin_unlock_irq(&conf->device_lock);
594 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700595
596 return_io(return_bi);
597
Dan Williamse4d84902007-09-24 10:06:13 -0700598 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700599 release_stripe(sh);
600}
601
602static void ops_run_biofill(struct stripe_head *sh)
603{
604 struct dma_async_tx_descriptor *tx = NULL;
605 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700606 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700607 int i;
608
Harvey Harrisone46b2722008-04-28 02:15:50 -0700609 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700610 (unsigned long long)sh->sector);
611
612 for (i = sh->disks; i--; ) {
613 struct r5dev *dev = &sh->dev[i];
614 if (test_bit(R5_Wantfill, &dev->flags)) {
615 struct bio *rbi;
616 spin_lock_irq(&conf->device_lock);
617 dev->read = rbi = dev->toread;
618 dev->toread = NULL;
619 spin_unlock_irq(&conf->device_lock);
620 while (rbi && rbi->bi_sector <
621 dev->sector + STRIPE_SECTORS) {
622 tx = async_copy_data(0, rbi, dev->page,
623 dev->sector, tx);
624 rbi = r5_next_bio(rbi, dev->sector);
625 }
626 }
627 }
628
629 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700630 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
631 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700632}
633
Dan Williams4e7d2c02009-08-29 19:13:11 -0700634static void mark_target_uptodate(struct stripe_head *sh, int target)
635{
636 struct r5dev *tgt;
637
638 if (target < 0)
639 return;
640
641 tgt = &sh->dev[target];
642 set_bit(R5_UPTODATE, &tgt->flags);
643 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
644 clear_bit(R5_Wantcompute, &tgt->flags);
645}
646
Dan Williamsac6b53b2009-07-14 13:40:19 -0700647static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700648{
649 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700650
Harvey Harrisone46b2722008-04-28 02:15:50 -0700651 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700652 (unsigned long long)sh->sector);
653
Dan Williamsac6b53b2009-07-14 13:40:19 -0700654 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700655 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700656 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700657
Dan Williamsecc65c92008-06-28 08:31:57 +1000658 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
659 if (sh->check_state == check_state_compute_run)
660 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700661 set_bit(STRIPE_HANDLE, &sh->state);
662 release_stripe(sh);
663}
664
Dan Williamsd6f38f32009-07-14 11:50:52 -0700665/* return a pointer to the address conversion region of the scribble buffer */
666static addr_conv_t *to_addr_conv(struct stripe_head *sh,
667 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700668{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700669 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
670}
671
672static struct dma_async_tx_descriptor *
673ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
674{
Dan Williams91c00922007-01-02 13:52:30 -0700675 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700676 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700677 int target = sh->ops.target;
678 struct r5dev *tgt = &sh->dev[target];
679 struct page *xor_dest = tgt->page;
680 int count = 0;
681 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700682 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700683 int i;
684
685 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700686 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700687 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
688
689 for (i = disks; i--; )
690 if (i != target)
691 xor_srcs[count++] = sh->dev[i].page;
692
693 atomic_inc(&sh->count);
694
Dan Williams0403e382009-09-08 17:42:50 -0700695 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700696 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700697 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700698 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700699 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700700 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700701
Dan Williams91c00922007-01-02 13:52:30 -0700702 return tx;
703}
704
Dan Williamsac6b53b2009-07-14 13:40:19 -0700705/* set_syndrome_sources - populate source buffers for gen_syndrome
706 * @srcs - (struct page *) array of size sh->disks
707 * @sh - stripe_head to parse
708 *
709 * Populates srcs in proper layout order for the stripe and returns the
710 * 'count' of sources to be used in a call to async_gen_syndrome. The P
711 * destination buffer is recorded in srcs[count] and the Q destination
712 * is recorded in srcs[count+1]].
713 */
714static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
715{
716 int disks = sh->disks;
717 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
718 int d0_idx = raid6_d0(sh);
719 int count;
720 int i;
721
722 for (i = 0; i < disks; i++)
723 srcs[i] = (void *)raid6_empty_zero_page;
724
725 count = 0;
726 i = d0_idx;
727 do {
728 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
729
730 srcs[slot] = sh->dev[i].page;
731 i = raid6_next_disk(i, disks);
732 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700733
NeilBrowne4424fe2009-10-16 16:27:34 +1100734 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700735}
736
737static struct dma_async_tx_descriptor *
738ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
739{
740 int disks = sh->disks;
741 struct page **blocks = percpu->scribble;
742 int target;
743 int qd_idx = sh->qd_idx;
744 struct dma_async_tx_descriptor *tx;
745 struct async_submit_ctl submit;
746 struct r5dev *tgt;
747 struct page *dest;
748 int i;
749 int count;
750
751 if (sh->ops.target < 0)
752 target = sh->ops.target2;
753 else if (sh->ops.target2 < 0)
754 target = sh->ops.target;
755 else
756 /* we should only have one valid target */
757 BUG();
758 BUG_ON(target < 0);
759 pr_debug("%s: stripe %llu block: %d\n",
760 __func__, (unsigned long long)sh->sector, target);
761
762 tgt = &sh->dev[target];
763 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
764 dest = tgt->page;
765
766 atomic_inc(&sh->count);
767
768 if (target == qd_idx) {
769 count = set_syndrome_sources(blocks, sh);
770 blocks[count] = NULL; /* regenerating p is not necessary */
771 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700772 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
773 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700774 to_addr_conv(sh, percpu));
775 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
776 } else {
777 /* Compute any data- or p-drive using XOR */
778 count = 0;
779 for (i = disks; i-- ; ) {
780 if (i == target || i == qd_idx)
781 continue;
782 blocks[count++] = sh->dev[i].page;
783 }
784
Dan Williams0403e382009-09-08 17:42:50 -0700785 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
786 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700787 to_addr_conv(sh, percpu));
788 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
789 }
790
791 return tx;
792}
793
794static struct dma_async_tx_descriptor *
795ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
796{
797 int i, count, disks = sh->disks;
798 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
799 int d0_idx = raid6_d0(sh);
800 int faila = -1, failb = -1;
801 int target = sh->ops.target;
802 int target2 = sh->ops.target2;
803 struct r5dev *tgt = &sh->dev[target];
804 struct r5dev *tgt2 = &sh->dev[target2];
805 struct dma_async_tx_descriptor *tx;
806 struct page **blocks = percpu->scribble;
807 struct async_submit_ctl submit;
808
809 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
810 __func__, (unsigned long long)sh->sector, target, target2);
811 BUG_ON(target < 0 || target2 < 0);
812 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
813 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
814
Dan Williams6c910a72009-09-16 12:24:54 -0700815 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700816 * slot number conversion for 'faila' and 'failb'
817 */
818 for (i = 0; i < disks ; i++)
819 blocks[i] = (void *)raid6_empty_zero_page;
820 count = 0;
821 i = d0_idx;
822 do {
823 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
824
825 blocks[slot] = sh->dev[i].page;
826
827 if (i == target)
828 faila = slot;
829 if (i == target2)
830 failb = slot;
831 i = raid6_next_disk(i, disks);
832 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700833
834 BUG_ON(faila == failb);
835 if (failb < faila)
836 swap(faila, failb);
837 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
838 __func__, (unsigned long long)sh->sector, faila, failb);
839
840 atomic_inc(&sh->count);
841
842 if (failb == syndrome_disks+1) {
843 /* Q disk is one of the missing disks */
844 if (faila == syndrome_disks) {
845 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700846 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
847 ops_complete_compute, sh,
848 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +1100849 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700850 STRIPE_SIZE, &submit);
851 } else {
852 struct page *dest;
853 int data_target;
854 int qd_idx = sh->qd_idx;
855
856 /* Missing D+Q: recompute D from P, then recompute Q */
857 if (target == qd_idx)
858 data_target = target2;
859 else
860 data_target = target;
861
862 count = 0;
863 for (i = disks; i-- ; ) {
864 if (i == data_target || i == qd_idx)
865 continue;
866 blocks[count++] = sh->dev[i].page;
867 }
868 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700869 init_async_submit(&submit,
870 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
871 NULL, NULL, NULL,
872 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700873 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
874 &submit);
875
876 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700877 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
878 ops_complete_compute, sh,
879 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700880 return async_gen_syndrome(blocks, 0, count+2,
881 STRIPE_SIZE, &submit);
882 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700883 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700884 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
885 ops_complete_compute, sh,
886 to_addr_conv(sh, percpu));
887 if (failb == syndrome_disks) {
888 /* We're missing D+P. */
889 return async_raid6_datap_recov(syndrome_disks+2,
890 STRIPE_SIZE, faila,
891 blocks, &submit);
892 } else {
893 /* We're missing D+D. */
894 return async_raid6_2data_recov(syndrome_disks+2,
895 STRIPE_SIZE, faila, failb,
896 blocks, &submit);
897 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700898 }
899}
900
901
Dan Williams91c00922007-01-02 13:52:30 -0700902static void ops_complete_prexor(void *stripe_head_ref)
903{
904 struct stripe_head *sh = stripe_head_ref;
905
Harvey Harrisone46b2722008-04-28 02:15:50 -0700906 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700907 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700908}
909
910static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700911ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
912 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700913{
Dan Williams91c00922007-01-02 13:52:30 -0700914 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700915 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700916 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700917 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700918
919 /* existing parity data subtracted */
920 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
921
Harvey Harrisone46b2722008-04-28 02:15:50 -0700922 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700923 (unsigned long long)sh->sector);
924
925 for (i = disks; i--; ) {
926 struct r5dev *dev = &sh->dev[i];
927 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000928 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700929 xor_srcs[count++] = dev->page;
930 }
931
Dan Williams0403e382009-09-08 17:42:50 -0700932 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700933 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700934 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700935
936 return tx;
937}
938
939static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000940ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700941{
942 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000943 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700944
Harvey Harrisone46b2722008-04-28 02:15:50 -0700945 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700946 (unsigned long long)sh->sector);
947
948 for (i = disks; i--; ) {
949 struct r5dev *dev = &sh->dev[i];
950 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700951
Dan Williamsd8ee0722008-06-28 08:32:06 +1000952 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700953 struct bio *wbi;
954
955 spin_lock(&sh->lock);
956 chosen = dev->towrite;
957 dev->towrite = NULL;
958 BUG_ON(dev->written);
959 wbi = dev->written = chosen;
960 spin_unlock(&sh->lock);
961
962 while (wbi && wbi->bi_sector <
963 dev->sector + STRIPE_SECTORS) {
964 tx = async_copy_data(1, wbi, dev->page,
965 dev->sector, tx);
966 wbi = r5_next_bio(wbi, dev->sector);
967 }
968 }
969 }
970
971 return tx;
972}
973
Dan Williamsac6b53b2009-07-14 13:40:19 -0700974static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700975{
976 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700977 int disks = sh->disks;
978 int pd_idx = sh->pd_idx;
979 int qd_idx = sh->qd_idx;
980 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700981
Harvey Harrisone46b2722008-04-28 02:15:50 -0700982 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700983 (unsigned long long)sh->sector);
984
985 for (i = disks; i--; ) {
986 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -0700987
988 if (dev->written || i == pd_idx || i == qd_idx)
Dan Williams91c00922007-01-02 13:52:30 -0700989 set_bit(R5_UPTODATE, &dev->flags);
990 }
991
Dan Williamsd8ee0722008-06-28 08:32:06 +1000992 if (sh->reconstruct_state == reconstruct_state_drain_run)
993 sh->reconstruct_state = reconstruct_state_drain_result;
994 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
995 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
996 else {
997 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
998 sh->reconstruct_state = reconstruct_state_result;
999 }
Dan Williams91c00922007-01-02 13:52:30 -07001000
1001 set_bit(STRIPE_HANDLE, &sh->state);
1002 release_stripe(sh);
1003}
1004
1005static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001006ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1007 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001008{
Dan Williams91c00922007-01-02 13:52:30 -07001009 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001010 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001011 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001012 int count = 0, pd_idx = sh->pd_idx, i;
1013 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001014 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001015 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001016
Harvey Harrisone46b2722008-04-28 02:15:50 -07001017 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001018 (unsigned long long)sh->sector);
1019
1020 /* check if prexor is active which means only process blocks
1021 * that are part of a read-modify-write (written)
1022 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001023 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1024 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001025 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1026 for (i = disks; i--; ) {
1027 struct r5dev *dev = &sh->dev[i];
1028 if (dev->written)
1029 xor_srcs[count++] = dev->page;
1030 }
1031 } else {
1032 xor_dest = sh->dev[pd_idx].page;
1033 for (i = disks; i--; ) {
1034 struct r5dev *dev = &sh->dev[i];
1035 if (i != pd_idx)
1036 xor_srcs[count++] = dev->page;
1037 }
1038 }
1039
Dan Williams91c00922007-01-02 13:52:30 -07001040 /* 1/ if we prexor'd then the dest is reused as a source
1041 * 2/ if we did not prexor then we are redoing the parity
1042 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1043 * for the synchronous xor case
1044 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001045 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001046 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1047
1048 atomic_inc(&sh->count);
1049
Dan Williamsac6b53b2009-07-14 13:40:19 -07001050 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001051 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001052 if (unlikely(count == 1))
1053 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1054 else
1055 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001056}
1057
Dan Williamsac6b53b2009-07-14 13:40:19 -07001058static void
1059ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1060 struct dma_async_tx_descriptor *tx)
1061{
1062 struct async_submit_ctl submit;
1063 struct page **blocks = percpu->scribble;
1064 int count;
1065
1066 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1067
1068 count = set_syndrome_sources(blocks, sh);
1069
1070 atomic_inc(&sh->count);
1071
1072 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1073 sh, to_addr_conv(sh, percpu));
1074 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001075}
1076
1077static void ops_complete_check(void *stripe_head_ref)
1078{
1079 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001080
Harvey Harrisone46b2722008-04-28 02:15:50 -07001081 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001082 (unsigned long long)sh->sector);
1083
Dan Williamsecc65c92008-06-28 08:31:57 +10001084 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001085 set_bit(STRIPE_HANDLE, &sh->state);
1086 release_stripe(sh);
1087}
1088
Dan Williamsac6b53b2009-07-14 13:40:19 -07001089static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001090{
Dan Williams91c00922007-01-02 13:52:30 -07001091 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001092 int pd_idx = sh->pd_idx;
1093 int qd_idx = sh->qd_idx;
1094 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001095 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001096 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001097 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001098 int count;
1099 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001100
Harvey Harrisone46b2722008-04-28 02:15:50 -07001101 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001102 (unsigned long long)sh->sector);
1103
Dan Williamsac6b53b2009-07-14 13:40:19 -07001104 count = 0;
1105 xor_dest = sh->dev[pd_idx].page;
1106 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001107 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001108 if (i == pd_idx || i == qd_idx)
1109 continue;
1110 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001111 }
1112
Dan Williamsd6f38f32009-07-14 11:50:52 -07001113 init_async_submit(&submit, 0, NULL, NULL, NULL,
1114 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001115 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001116 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001117
Dan Williams91c00922007-01-02 13:52:30 -07001118 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001119 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1120 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001121}
1122
Dan Williamsac6b53b2009-07-14 13:40:19 -07001123static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1124{
1125 struct page **srcs = percpu->scribble;
1126 struct async_submit_ctl submit;
1127 int count;
1128
1129 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1130 (unsigned long long)sh->sector, checkp);
1131
1132 count = set_syndrome_sources(srcs, sh);
1133 if (!checkp)
1134 srcs[count] = NULL;
1135
1136 atomic_inc(&sh->count);
1137 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1138 sh, to_addr_conv(sh, percpu));
1139 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1140 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1141}
1142
Dan Williams417b8d42009-10-16 16:25:22 +11001143static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001144{
1145 int overlap_clear = 0, i, disks = sh->disks;
1146 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001147 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001148 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001149 struct raid5_percpu *percpu;
1150 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001151
Dan Williamsd6f38f32009-07-14 11:50:52 -07001152 cpu = get_cpu();
1153 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001154 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001155 ops_run_biofill(sh);
1156 overlap_clear++;
1157 }
1158
Dan Williams7b3a8712008-06-28 08:32:09 +10001159 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001160 if (level < 6)
1161 tx = ops_run_compute5(sh, percpu);
1162 else {
1163 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1164 tx = ops_run_compute6_1(sh, percpu);
1165 else
1166 tx = ops_run_compute6_2(sh, percpu);
1167 }
1168 /* terminate the chain if reconstruct is not set to be run */
1169 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001170 async_tx_ack(tx);
1171 }
Dan Williams91c00922007-01-02 13:52:30 -07001172
Dan Williams600aa102008-06-28 08:32:05 +10001173 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001174 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001175
Dan Williams600aa102008-06-28 08:32:05 +10001176 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001177 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001178 overlap_clear++;
1179 }
1180
Dan Williamsac6b53b2009-07-14 13:40:19 -07001181 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1182 if (level < 6)
1183 ops_run_reconstruct5(sh, percpu, tx);
1184 else
1185 ops_run_reconstruct6(sh, percpu, tx);
1186 }
Dan Williams91c00922007-01-02 13:52:30 -07001187
Dan Williamsac6b53b2009-07-14 13:40:19 -07001188 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1189 if (sh->check_state == check_state_run)
1190 ops_run_check_p(sh, percpu);
1191 else if (sh->check_state == check_state_run_q)
1192 ops_run_check_pq(sh, percpu, 0);
1193 else if (sh->check_state == check_state_run_pq)
1194 ops_run_check_pq(sh, percpu, 1);
1195 else
1196 BUG();
1197 }
Dan Williams91c00922007-01-02 13:52:30 -07001198
Dan Williams91c00922007-01-02 13:52:30 -07001199 if (overlap_clear)
1200 for (i = disks; i--; ) {
1201 struct r5dev *dev = &sh->dev[i];
1202 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1203 wake_up(&sh->raid_conf->wait_for_overlap);
1204 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001205 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001206}
1207
Dan Williams417b8d42009-10-16 16:25:22 +11001208#ifdef CONFIG_MULTICORE_RAID456
1209static void async_run_ops(void *param, async_cookie_t cookie)
1210{
1211 struct stripe_head *sh = param;
1212 unsigned long ops_request = sh->ops.request;
1213
1214 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1215 wake_up(&sh->ops.wait_for_ops);
1216
1217 __raid_run_ops(sh, ops_request);
1218 release_stripe(sh);
1219}
1220
1221static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1222{
1223 /* since handle_stripe can be called outside of raid5d context
1224 * we need to ensure sh->ops.request is de-staged before another
1225 * request arrives
1226 */
1227 wait_event(sh->ops.wait_for_ops,
1228 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1229 sh->ops.request = ops_request;
1230
1231 atomic_inc(&sh->count);
1232 async_schedule(async_run_ops, sh);
1233}
1234#else
1235#define raid_run_ops __raid_run_ops
1236#endif
1237
NeilBrown3f294f42005-11-08 21:39:25 -08001238static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -08001241 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1242 if (!sh)
1243 return 0;
1244 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
1245 sh->raid_conf = conf;
1246 spin_lock_init(&sh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001247 #ifdef CONFIG_MULTICORE_RAID456
1248 init_waitqueue_head(&sh->ops.wait_for_ops);
1249 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001250
1251 if (grow_buffers(sh, conf->raid_disks)) {
1252 shrink_buffers(sh, conf->raid_disks);
1253 kmem_cache_free(conf->slab_cache, sh);
1254 return 0;
1255 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001256 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -08001257 /* we just created an active stripe so... */
1258 atomic_set(&sh->count, 1);
1259 atomic_inc(&conf->active_stripes);
1260 INIT_LIST_HEAD(&sh->lru);
1261 release_stripe(sh);
1262 return 1;
1263}
1264
1265static int grow_stripes(raid5_conf_t *conf, int num)
1266{
Christoph Lametere18b8902006-12-06 20:33:20 -08001267 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int devs = conf->raid_disks;
1269
NeilBrown245f46c2009-03-31 14:39:39 +11001270 sprintf(conf->cache_name[0],
1271 "raid%d-%s", conf->level, mdname(conf->mddev));
1272 sprintf(conf->cache_name[1],
1273 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -08001274 conf->active_name = 0;
1275 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001277 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (!sc)
1279 return 1;
1280 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001281 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001282 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001283 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return 0;
1286}
NeilBrown29269552006-03-27 01:18:10 -08001287
Dan Williamsd6f38f32009-07-14 11:50:52 -07001288/**
1289 * scribble_len - return the required size of the scribble region
1290 * @num - total number of disks in the array
1291 *
1292 * The size must be enough to contain:
1293 * 1/ a struct page pointer for each device in the array +2
1294 * 2/ room to convert each entry in (1) to its corresponding dma
1295 * (dma_map_page()) or page (page_address()) address.
1296 *
1297 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1298 * calculate over all devices (not just the data blocks), using zeros in place
1299 * of the P and Q blocks.
1300 */
1301static size_t scribble_len(int num)
1302{
1303 size_t len;
1304
1305 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1306
1307 return len;
1308}
1309
NeilBrownad01c9e2006-03-27 01:18:07 -08001310static int resize_stripes(raid5_conf_t *conf, int newsize)
1311{
1312 /* Make all the stripes able to hold 'newsize' devices.
1313 * New slots in each stripe get 'page' set to a new page.
1314 *
1315 * This happens in stages:
1316 * 1/ create a new kmem_cache and allocate the required number of
1317 * stripe_heads.
1318 * 2/ gather all the old stripe_heads and tranfer the pages across
1319 * to the new stripe_heads. This will have the side effect of
1320 * freezing the array as once all stripe_heads have been collected,
1321 * no IO will be possible. Old stripe heads are freed once their
1322 * pages have been transferred over, and the old kmem_cache is
1323 * freed when all stripes are done.
1324 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1325 * we simple return a failre status - no need to clean anything up.
1326 * 4/ allocate new pages for the new slots in the new stripe_heads.
1327 * If this fails, we don't bother trying the shrink the
1328 * stripe_heads down again, we just leave them as they are.
1329 * As each stripe_head is processed the new one is released into
1330 * active service.
1331 *
1332 * Once step2 is started, we cannot afford to wait for a write,
1333 * so we use GFP_NOIO allocations.
1334 */
1335 struct stripe_head *osh, *nsh;
1336 LIST_HEAD(newstripes);
1337 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001338 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001339 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001340 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001341 int i;
1342
1343 if (newsize <= conf->pool_size)
1344 return 0; /* never bother to shrink */
1345
Dan Williamsb5470dc2008-06-27 21:44:04 -07001346 err = md_allow_write(conf->mddev);
1347 if (err)
1348 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001349
NeilBrownad01c9e2006-03-27 01:18:07 -08001350 /* Step 1 */
1351 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1352 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001353 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001354 if (!sc)
1355 return -ENOMEM;
1356
1357 for (i = conf->max_nr_stripes; i; i--) {
1358 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1359 if (!nsh)
1360 break;
1361
1362 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1363
1364 nsh->raid_conf = conf;
1365 spin_lock_init(&nsh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001366 #ifdef CONFIG_MULTICORE_RAID456
1367 init_waitqueue_head(&nsh->ops.wait_for_ops);
1368 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001369
1370 list_add(&nsh->lru, &newstripes);
1371 }
1372 if (i) {
1373 /* didn't get enough, give up */
1374 while (!list_empty(&newstripes)) {
1375 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1376 list_del(&nsh->lru);
1377 kmem_cache_free(sc, nsh);
1378 }
1379 kmem_cache_destroy(sc);
1380 return -ENOMEM;
1381 }
1382 /* Step 2 - Must use GFP_NOIO now.
1383 * OK, we have enough stripes, start collecting inactive
1384 * stripes and copying them over
1385 */
1386 list_for_each_entry(nsh, &newstripes, lru) {
1387 spin_lock_irq(&conf->device_lock);
1388 wait_event_lock_irq(conf->wait_for_stripe,
1389 !list_empty(&conf->inactive_list),
1390 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001391 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001392 );
1393 osh = get_free_stripe(conf);
1394 spin_unlock_irq(&conf->device_lock);
1395 atomic_set(&nsh->count, 1);
1396 for(i=0; i<conf->pool_size; i++)
1397 nsh->dev[i].page = osh->dev[i].page;
1398 for( ; i<newsize; i++)
1399 nsh->dev[i].page = NULL;
1400 kmem_cache_free(conf->slab_cache, osh);
1401 }
1402 kmem_cache_destroy(conf->slab_cache);
1403
1404 /* Step 3.
1405 * At this point, we are holding all the stripes so the array
1406 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001407 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001408 */
1409 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1410 if (ndisks) {
1411 for (i=0; i<conf->raid_disks; i++)
1412 ndisks[i] = conf->disks[i];
1413 kfree(conf->disks);
1414 conf->disks = ndisks;
1415 } else
1416 err = -ENOMEM;
1417
Dan Williamsd6f38f32009-07-14 11:50:52 -07001418 get_online_cpus();
1419 conf->scribble_len = scribble_len(newsize);
1420 for_each_present_cpu(cpu) {
1421 struct raid5_percpu *percpu;
1422 void *scribble;
1423
1424 percpu = per_cpu_ptr(conf->percpu, cpu);
1425 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1426
1427 if (scribble) {
1428 kfree(percpu->scribble);
1429 percpu->scribble = scribble;
1430 } else {
1431 err = -ENOMEM;
1432 break;
1433 }
1434 }
1435 put_online_cpus();
1436
NeilBrownad01c9e2006-03-27 01:18:07 -08001437 /* Step 4, return new stripes to service */
1438 while(!list_empty(&newstripes)) {
1439 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1440 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001441
NeilBrownad01c9e2006-03-27 01:18:07 -08001442 for (i=conf->raid_disks; i < newsize; i++)
1443 if (nsh->dev[i].page == NULL) {
1444 struct page *p = alloc_page(GFP_NOIO);
1445 nsh->dev[i].page = p;
1446 if (!p)
1447 err = -ENOMEM;
1448 }
1449 release_stripe(nsh);
1450 }
1451 /* critical section pass, GFP_NOIO no longer needed */
1452
1453 conf->slab_cache = sc;
1454 conf->active_name = 1-conf->active_name;
1455 conf->pool_size = newsize;
1456 return err;
1457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
NeilBrown3f294f42005-11-08 21:39:25 -08001459static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct stripe_head *sh;
1462
NeilBrown3f294f42005-11-08 21:39:25 -08001463 spin_lock_irq(&conf->device_lock);
1464 sh = get_free_stripe(conf);
1465 spin_unlock_irq(&conf->device_lock);
1466 if (!sh)
1467 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001468 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001469 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001470 kmem_cache_free(conf->slab_cache, sh);
1471 atomic_dec(&conf->active_stripes);
1472 return 1;
1473}
1474
1475static void shrink_stripes(raid5_conf_t *conf)
1476{
1477 while (drop_one_stripe(conf))
1478 ;
1479
NeilBrown29fc7e32006-02-03 03:03:41 -08001480 if (conf->slab_cache)
1481 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 conf->slab_cache = NULL;
1483}
1484
NeilBrown6712ecf2007-09-27 12:47:43 +02001485static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
NeilBrown99c0fb52009-03-31 14:39:38 +11001487 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001489 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001491 char b[BDEVNAME_SIZE];
1492 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 for (i=0 ; i<disks; i++)
1496 if (bi == &sh->dev[i].req)
1497 break;
1498
Dan Williams45b42332007-07-09 11:56:43 -07001499 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1500 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 uptodate);
1502 if (i == disks) {
1503 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001504 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506
1507 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001509 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001510 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001511 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1512 " (%lu sectors at %llu on %s)\n",
1513 mdname(conf->mddev), STRIPE_SECTORS,
1514 (unsigned long long)(sh->sector
1515 + rdev->data_offset),
1516 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001517 clear_bit(R5_ReadError, &sh->dev[i].flags);
1518 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1519 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001520 if (atomic_read(&conf->disks[i].rdev->read_errors))
1521 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001523 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001524 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001525 rdev = conf->disks[i].rdev;
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001528 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001529 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001530 printk_rl(KERN_WARNING
1531 "raid5:%s: read error not correctable "
1532 "(sector %llu on %s).\n",
1533 mdname(conf->mddev),
1534 (unsigned long long)(sh->sector
1535 + rdev->data_offset),
1536 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001537 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001538 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001539 printk_rl(KERN_WARNING
1540 "raid5:%s: read error NOT corrected!! "
1541 "(sector %llu on %s).\n",
1542 mdname(conf->mddev),
1543 (unsigned long long)(sh->sector
1544 + rdev->data_offset),
1545 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001546 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001547 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001548 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001549 "raid5:%s: Too many read errors, failing device %s.\n",
1550 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001551 else
1552 retry = 1;
1553 if (retry)
1554 set_bit(R5_ReadError, &sh->dev[i].flags);
1555 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001556 clear_bit(R5_ReadError, &sh->dev[i].flags);
1557 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001558 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1563 set_bit(STRIPE_HANDLE, &sh->state);
1564 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
NeilBrownd710e132008-10-13 11:55:12 +11001567static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
NeilBrown99c0fb52009-03-31 14:39:38 +11001569 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001571 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 for (i=0 ; i<disks; i++)
1575 if (bi == &sh->dev[i].req)
1576 break;
1577
Dan Williams45b42332007-07-09 11:56:43 -07001578 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1580 uptodate);
1581 if (i == disks) {
1582 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (!uptodate)
1587 md_error(conf->mddev, conf->disks[i].rdev);
1588
1589 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1590
1591 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1592 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001593 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596
NeilBrown784052e2009-03-31 15:19:07 +11001597static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
NeilBrown784052e2009-03-31 15:19:07 +11001599static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct r5dev *dev = &sh->dev[i];
1602
1603 bio_init(&dev->req);
1604 dev->req.bi_io_vec = &dev->vec;
1605 dev->req.bi_vcnt++;
1606 dev->req.bi_max_vecs++;
1607 dev->vec.bv_page = dev->page;
1608 dev->vec.bv_len = STRIPE_SIZE;
1609 dev->vec.bv_offset = 0;
1610
1611 dev->req.bi_sector = sh->sector;
1612 dev->req.bi_private = sh;
1613
1614 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001615 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1619{
1620 char b[BDEVNAME_SIZE];
1621 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001622 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
NeilBrownb2d444d2005-11-08 21:39:31 -08001624 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001625 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001626 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1627 unsigned long flags;
1628 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001630 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * if recovery was running, make sure it aborts.
1633 */
NeilBrowndfc70642008-05-23 13:04:39 -07001634 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001636 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001637 printk(KERN_ALERT
1638 "raid5: Disk failure on %s, disabling device.\n"
1639 "raid5: Operation continuing on %d devices.\n",
1640 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644/*
1645 * Input: a 'big' sector number,
1646 * Output: index of the data and parity disk, and the sector # in them.
1647 */
NeilBrown112bf892009-03-31 14:39:38 +11001648static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001649 int previous, int *dd_idx,
1650 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 long stripe;
1653 unsigned long chunk_number;
1654 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001655 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001656 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001658 int algorithm = previous ? conf->prev_algo
1659 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001660 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1661 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001662 int raid_disks = previous ? conf->previous_raid_disks
1663 : conf->raid_disks;
1664 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /* First compute the information on this sector */
1667
1668 /*
1669 * Compute the chunk number and the sector offset inside the chunk
1670 */
1671 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1672 chunk_number = r_sector;
1673 BUG_ON(r_sector != chunk_number);
1674
1675 /*
1676 * Compute the stripe number
1677 */
1678 stripe = chunk_number / data_disks;
1679
1680 /*
1681 * Compute the data disk and parity disk indexes inside the stripe
1682 */
1683 *dd_idx = chunk_number % data_disks;
1684
1685 /*
1686 * Select the parity disk based on the user selected algorithm.
1687 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001688 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001689 switch(conf->level) {
1690 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001691 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001692 break;
1693 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001694 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001696 pd_idx = data_disks - stripe % raid_disks;
1697 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 (*dd_idx)++;
1699 break;
1700 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001701 pd_idx = stripe % raid_disks;
1702 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 (*dd_idx)++;
1704 break;
1705 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001706 pd_idx = data_disks - stripe % raid_disks;
1707 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001710 pd_idx = stripe % raid_disks;
1711 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001713 case ALGORITHM_PARITY_0:
1714 pd_idx = 0;
1715 (*dd_idx)++;
1716 break;
1717 case ALGORITHM_PARITY_N:
1718 pd_idx = data_disks;
1719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001721 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001722 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001723 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001724 }
1725 break;
1726 case 6:
1727
NeilBrowne183eae2009-03-31 15:20:22 +11001728 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001729 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001730 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1731 qd_idx = pd_idx + 1;
1732 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001733 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001734 qd_idx = 0;
1735 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001736 (*dd_idx) += 2; /* D D P Q D */
1737 break;
1738 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001739 pd_idx = stripe % raid_disks;
1740 qd_idx = pd_idx + 1;
1741 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001742 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001743 qd_idx = 0;
1744 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001745 (*dd_idx) += 2; /* D D P Q D */
1746 break;
1747 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001748 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1749 qd_idx = (pd_idx + 1) % raid_disks;
1750 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001751 break;
1752 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001753 pd_idx = stripe % raid_disks;
1754 qd_idx = (pd_idx + 1) % raid_disks;
1755 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001756 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001757
1758 case ALGORITHM_PARITY_0:
1759 pd_idx = 0;
1760 qd_idx = 1;
1761 (*dd_idx) += 2;
1762 break;
1763 case ALGORITHM_PARITY_N:
1764 pd_idx = data_disks;
1765 qd_idx = data_disks + 1;
1766 break;
1767
1768 case ALGORITHM_ROTATING_ZERO_RESTART:
1769 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1770 * of blocks for computing Q is different.
1771 */
1772 pd_idx = stripe % raid_disks;
1773 qd_idx = pd_idx + 1;
1774 if (pd_idx == raid_disks-1) {
1775 (*dd_idx)++; /* Q D D D P */
1776 qd_idx = 0;
1777 } else if (*dd_idx >= pd_idx)
1778 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001779 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001780 break;
1781
1782 case ALGORITHM_ROTATING_N_RESTART:
1783 /* Same a left_asymmetric, by first stripe is
1784 * D D D P Q rather than
1785 * Q D D D P
1786 */
1787 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1788 qd_idx = pd_idx + 1;
1789 if (pd_idx == raid_disks-1) {
1790 (*dd_idx)++; /* Q D D D P */
1791 qd_idx = 0;
1792 } else if (*dd_idx >= pd_idx)
1793 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001794 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001795 break;
1796
1797 case ALGORITHM_ROTATING_N_CONTINUE:
1798 /* Same as left_symmetric but Q is before P */
1799 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1800 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1801 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001802 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001803 break;
1804
1805 case ALGORITHM_LEFT_ASYMMETRIC_6:
1806 /* RAID5 left_asymmetric, with Q on last device */
1807 pd_idx = data_disks - stripe % (raid_disks-1);
1808 if (*dd_idx >= pd_idx)
1809 (*dd_idx)++;
1810 qd_idx = raid_disks - 1;
1811 break;
1812
1813 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1814 pd_idx = stripe % (raid_disks-1);
1815 if (*dd_idx >= pd_idx)
1816 (*dd_idx)++;
1817 qd_idx = raid_disks - 1;
1818 break;
1819
1820 case ALGORITHM_LEFT_SYMMETRIC_6:
1821 pd_idx = data_disks - stripe % (raid_disks-1);
1822 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1823 qd_idx = raid_disks - 1;
1824 break;
1825
1826 case ALGORITHM_RIGHT_SYMMETRIC_6:
1827 pd_idx = stripe % (raid_disks-1);
1828 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1829 qd_idx = raid_disks - 1;
1830 break;
1831
1832 case ALGORITHM_PARITY_0_6:
1833 pd_idx = 0;
1834 (*dd_idx)++;
1835 qd_idx = raid_disks - 1;
1836 break;
1837
1838
NeilBrown16a53ec2006-06-26 00:27:38 -07001839 default:
NeilBrownd710e132008-10-13 11:55:12 +11001840 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001841 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001842 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001843 }
1844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846
NeilBrown911d4ee2009-03-31 14:39:38 +11001847 if (sh) {
1848 sh->pd_idx = pd_idx;
1849 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001850 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /*
1853 * Finally, compute the new sector number
1854 */
1855 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1856 return new_sector;
1857}
1858
1859
NeilBrown784052e2009-03-31 15:19:07 +11001860static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001863 int raid_disks = sh->disks;
1864 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001866 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1867 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001868 int algorithm = previous ? conf->prev_algo
1869 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 sector_t stripe;
1871 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001872 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001874 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
NeilBrown16a53ec2006-06-26 00:27:38 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1878 stripe = new_sector;
1879 BUG_ON(new_sector != stripe);
1880
NeilBrown16a53ec2006-06-26 00:27:38 -07001881 if (i == sh->pd_idx)
1882 return 0;
1883 switch(conf->level) {
1884 case 4: break;
1885 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001886 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 case ALGORITHM_LEFT_ASYMMETRIC:
1888 case ALGORITHM_RIGHT_ASYMMETRIC:
1889 if (i > sh->pd_idx)
1890 i--;
1891 break;
1892 case ALGORITHM_LEFT_SYMMETRIC:
1893 case ALGORITHM_RIGHT_SYMMETRIC:
1894 if (i < sh->pd_idx)
1895 i += raid_disks;
1896 i -= (sh->pd_idx + 1);
1897 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001898 case ALGORITHM_PARITY_0:
1899 i -= 1;
1900 break;
1901 case ALGORITHM_PARITY_N:
1902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001904 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001905 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001906 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001907 }
1908 break;
1909 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001910 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001911 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001912 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001913 case ALGORITHM_LEFT_ASYMMETRIC:
1914 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001915 case ALGORITHM_ROTATING_ZERO_RESTART:
1916 case ALGORITHM_ROTATING_N_RESTART:
1917 if (sh->pd_idx == raid_disks-1)
1918 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001919 else if (i > sh->pd_idx)
1920 i -= 2; /* D D P Q D */
1921 break;
1922 case ALGORITHM_LEFT_SYMMETRIC:
1923 case ALGORITHM_RIGHT_SYMMETRIC:
1924 if (sh->pd_idx == raid_disks-1)
1925 i--; /* Q D D D P */
1926 else {
1927 /* D D P Q D */
1928 if (i < sh->pd_idx)
1929 i += raid_disks;
1930 i -= (sh->pd_idx + 2);
1931 }
1932 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001933 case ALGORITHM_PARITY_0:
1934 i -= 2;
1935 break;
1936 case ALGORITHM_PARITY_N:
1937 break;
1938 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11001939 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11001940 if (sh->pd_idx == 0)
1941 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11001942 else {
1943 /* D D Q P D */
1944 if (i < sh->pd_idx)
1945 i += raid_disks;
1946 i -= (sh->pd_idx + 1);
1947 }
NeilBrown99c0fb52009-03-31 14:39:38 +11001948 break;
1949 case ALGORITHM_LEFT_ASYMMETRIC_6:
1950 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1951 if (i > sh->pd_idx)
1952 i--;
1953 break;
1954 case ALGORITHM_LEFT_SYMMETRIC_6:
1955 case ALGORITHM_RIGHT_SYMMETRIC_6:
1956 if (i < sh->pd_idx)
1957 i += data_disks + 1;
1958 i -= (sh->pd_idx + 1);
1959 break;
1960 case ALGORITHM_PARITY_0_6:
1961 i -= 1;
1962 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001963 default:
NeilBrownd710e132008-10-13 11:55:12 +11001964 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001965 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001966 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001967 }
1968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970
1971 chunk_number = stripe * data_disks + i;
1972 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1973
NeilBrown112bf892009-03-31 14:39:38 +11001974 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001975 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001976 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1977 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001978 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return 0;
1980 }
1981 return r_sector;
1982}
1983
1984
Dan Williams600aa102008-06-28 08:32:05 +10001985static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001986schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001987 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001988{
1989 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001990 raid5_conf_t *conf = sh->raid_conf;
1991 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07001992
Dan Williamse33129d2007-01-02 13:52:30 -07001993 if (rcw) {
1994 /* if we are not expanding this is a proper write request, and
1995 * there will be bios with new data to be drained into the
1996 * stripe cache
1997 */
1998 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001999 sh->reconstruct_state = reconstruct_state_drain_run;
2000 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2001 } else
2002 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002003
Dan Williamsac6b53b2009-07-14 13:40:19 -07002004 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002005
2006 for (i = disks; i--; ) {
2007 struct r5dev *dev = &sh->dev[i];
2008
2009 if (dev->towrite) {
2010 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002011 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002012 if (!expand)
2013 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002014 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002015 }
2016 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002017 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002018 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002019 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002020 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002021 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002022 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2023 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2024
Dan Williamsd8ee0722008-06-28 08:32:06 +10002025 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002026 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2027 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002028 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002029
2030 for (i = disks; i--; ) {
2031 struct r5dev *dev = &sh->dev[i];
2032 if (i == pd_idx)
2033 continue;
2034
Dan Williamse33129d2007-01-02 13:52:30 -07002035 if (dev->towrite &&
2036 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002037 test_bit(R5_Wantcompute, &dev->flags))) {
2038 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002039 set_bit(R5_LOCKED, &dev->flags);
2040 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002041 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002042 }
2043 }
2044 }
2045
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002046 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002047 * are in flight
2048 */
2049 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2050 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002051 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002052
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002053 if (level == 6) {
2054 int qd_idx = sh->qd_idx;
2055 struct r5dev *dev = &sh->dev[qd_idx];
2056
2057 set_bit(R5_LOCKED, &dev->flags);
2058 clear_bit(R5_UPTODATE, &dev->flags);
2059 s->locked++;
2060 }
2061
Dan Williams600aa102008-06-28 08:32:05 +10002062 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002063 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002064 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002065}
NeilBrown16a53ec2006-06-26 00:27:38 -07002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067/*
2068 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002069 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 * The bi_next chain must be in order.
2071 */
2072static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2073{
2074 struct bio **bip;
2075 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002076 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Dan Williams45b42332007-07-09 11:56:43 -07002078 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 (unsigned long long)bi->bi_sector,
2080 (unsigned long long)sh->sector);
2081
2082
2083 spin_lock(&sh->lock);
2084 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002085 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002087 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2088 firstwrite = 1;
2089 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 bip = &sh->dev[dd_idx].toread;
2091 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2092 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2093 goto overlap;
2094 bip = & (*bip)->bi_next;
2095 }
2096 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2097 goto overlap;
2098
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002099 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (*bip)
2101 bi->bi_next = *bip;
2102 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002103 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 spin_unlock_irq(&conf->device_lock);
2105 spin_unlock(&sh->lock);
2106
Dan Williams45b42332007-07-09 11:56:43 -07002107 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 (unsigned long long)bi->bi_sector,
2109 (unsigned long long)sh->sector, dd_idx);
2110
NeilBrown72626682005-09-09 16:23:54 -07002111 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002112 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2113 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002114 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002115 set_bit(STRIPE_BIT_DELAY, &sh->state);
2116 }
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (forwrite) {
2119 /* check if page is covered */
2120 sector_t sector = sh->dev[dd_idx].sector;
2121 for (bi=sh->dev[dd_idx].towrite;
2122 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2123 bi && bi->bi_sector <= sector;
2124 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2125 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2126 sector = bi->bi_sector + (bi->bi_size>>9);
2127 }
2128 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2129 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2130 }
2131 return 1;
2132
2133 overlap:
2134 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2135 spin_unlock_irq(&conf->device_lock);
2136 spin_unlock(&sh->lock);
2137 return 0;
2138}
2139
NeilBrown29269552006-03-27 01:18:10 -08002140static void end_reshape(raid5_conf_t *conf);
2141
NeilBrown911d4ee2009-03-31 14:39:38 +11002142static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2143 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002144{
NeilBrown784052e2009-03-31 15:19:07 +11002145 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002146 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002147 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002148 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002149 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002150
NeilBrown112bf892009-03-31 14:39:38 +11002151 raid5_compute_sector(conf,
2152 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002153 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002154 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002155 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002156}
2157
Dan Williamsa4456852007-07-09 11:56:43 -07002158static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002159handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002160 struct stripe_head_state *s, int disks,
2161 struct bio **return_bi)
2162{
2163 int i;
2164 for (i = disks; i--; ) {
2165 struct bio *bi;
2166 int bitmap_end = 0;
2167
2168 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2169 mdk_rdev_t *rdev;
2170 rcu_read_lock();
2171 rdev = rcu_dereference(conf->disks[i].rdev);
2172 if (rdev && test_bit(In_sync, &rdev->flags))
2173 /* multiple read failures in one stripe */
2174 md_error(conf->mddev, rdev);
2175 rcu_read_unlock();
2176 }
2177 spin_lock_irq(&conf->device_lock);
2178 /* fail all writes first */
2179 bi = sh->dev[i].towrite;
2180 sh->dev[i].towrite = NULL;
2181 if (bi) {
2182 s->to_write--;
2183 bitmap_end = 1;
2184 }
2185
2186 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2187 wake_up(&conf->wait_for_overlap);
2188
2189 while (bi && bi->bi_sector <
2190 sh->dev[i].sector + STRIPE_SECTORS) {
2191 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2192 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002193 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002194 md_write_end(conf->mddev);
2195 bi->bi_next = *return_bi;
2196 *return_bi = bi;
2197 }
2198 bi = nextbi;
2199 }
2200 /* and fail all 'written' */
2201 bi = sh->dev[i].written;
2202 sh->dev[i].written = NULL;
2203 if (bi) bitmap_end = 1;
2204 while (bi && bi->bi_sector <
2205 sh->dev[i].sector + STRIPE_SECTORS) {
2206 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2207 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002208 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002209 md_write_end(conf->mddev);
2210 bi->bi_next = *return_bi;
2211 *return_bi = bi;
2212 }
2213 bi = bi2;
2214 }
2215
Dan Williamsb5e98d62007-01-02 13:52:31 -07002216 /* fail any reads if this device is non-operational and
2217 * the data has not reached the cache yet.
2218 */
2219 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2220 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2221 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002222 bi = sh->dev[i].toread;
2223 sh->dev[i].toread = NULL;
2224 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2225 wake_up(&conf->wait_for_overlap);
2226 if (bi) s->to_read--;
2227 while (bi && bi->bi_sector <
2228 sh->dev[i].sector + STRIPE_SECTORS) {
2229 struct bio *nextbi =
2230 r5_next_bio(bi, sh->dev[i].sector);
2231 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002232 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002233 bi->bi_next = *return_bi;
2234 *return_bi = bi;
2235 }
2236 bi = nextbi;
2237 }
2238 }
2239 spin_unlock_irq(&conf->device_lock);
2240 if (bitmap_end)
2241 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2242 STRIPE_SECTORS, 0, 0);
2243 }
2244
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002245 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2246 if (atomic_dec_and_test(&conf->pending_full_writes))
2247 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002248}
2249
Dan Williams1fe797e2008-06-28 09:16:30 +10002250/* fetch_block5 - checks the given member device to see if its data needs
2251 * to be read or computed to satisfy a request.
2252 *
2253 * Returns 1 when no more member devices need to be checked, otherwise returns
2254 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002255 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002256static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2257 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002258{
2259 struct r5dev *dev = &sh->dev[disk_idx];
2260 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2261
Dan Williamsf38e1212007-01-02 13:52:30 -07002262 /* is the data in this block needed, and can we get it? */
2263 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002264 !test_bit(R5_UPTODATE, &dev->flags) &&
2265 (dev->toread ||
2266 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2267 s->syncing || s->expanding ||
2268 (s->failed &&
2269 (failed_dev->toread ||
2270 (failed_dev->towrite &&
2271 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002272 /* We would like to get this block, possibly by computing it,
2273 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002274 */
2275 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002276 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002277 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2278 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002279 set_bit(R5_Wantcompute, &dev->flags);
2280 sh->ops.target = disk_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002281 sh->ops.target2 = -1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002282 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002283 /* Careful: from this point on 'uptodate' is in the eye
Dan Williamsac6b53b2009-07-14 13:40:19 -07002284 * of raid_run_ops which services 'compute' operations
Dan Williamsf38e1212007-01-02 13:52:30 -07002285 * before writes. R5_Wantcompute flags a block that will
2286 * be R5_UPTODATE by the time it is needed for a
2287 * subsequent operation.
2288 */
2289 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002290 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002291 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002292 set_bit(R5_LOCKED, &dev->flags);
2293 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002294 s->locked++;
2295 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2296 s->syncing);
2297 }
2298 }
2299
Dan Williams1fe797e2008-06-28 09:16:30 +10002300 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002301}
2302
Dan Williams1fe797e2008-06-28 09:16:30 +10002303/**
2304 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2305 */
2306static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002307 struct stripe_head_state *s, int disks)
2308{
2309 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002310
Dan Williamsf38e1212007-01-02 13:52:30 -07002311 /* look for blocks to read/compute, skip this if a compute
2312 * is already in flight, or if the stripe contents are in the
2313 * midst of changing due to a write
2314 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002315 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002316 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002317 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002318 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002319 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002320 set_bit(STRIPE_HANDLE, &sh->state);
2321}
2322
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002323/* fetch_block6 - checks the given member device to see if its data needs
2324 * to be read or computed to satisfy a request.
2325 *
2326 * Returns 1 when no more member devices need to be checked, otherwise returns
2327 * 0 to tell the loop in handle_stripe_fill6 to continue
2328 */
2329static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2330 struct r6_state *r6s, int disk_idx, int disks)
2331{
2332 struct r5dev *dev = &sh->dev[disk_idx];
2333 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2334 &sh->dev[r6s->failed_num[1]] };
2335
2336 if (!test_bit(R5_LOCKED, &dev->flags) &&
2337 !test_bit(R5_UPTODATE, &dev->flags) &&
2338 (dev->toread ||
2339 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2340 s->syncing || s->expanding ||
2341 (s->failed >= 1 &&
2342 (fdev[0]->toread || s->to_write)) ||
2343 (s->failed >= 2 &&
2344 (fdev[1]->toread || s->to_write)))) {
2345 /* we would like to get this block, possibly by computing it,
2346 * otherwise read it if the backing disk is insync
2347 */
2348 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2349 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2350 if ((s->uptodate == disks - 1) &&
2351 (s->failed && (disk_idx == r6s->failed_num[0] ||
2352 disk_idx == r6s->failed_num[1]))) {
2353 /* have disk failed, and we're requested to fetch it;
2354 * do compute it
2355 */
2356 pr_debug("Computing stripe %llu block %d\n",
2357 (unsigned long long)sh->sector, disk_idx);
2358 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2359 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2360 set_bit(R5_Wantcompute, &dev->flags);
2361 sh->ops.target = disk_idx;
2362 sh->ops.target2 = -1; /* no 2nd target */
2363 s->req_compute = 1;
2364 s->uptodate++;
2365 return 1;
2366 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2367 /* Computing 2-failure is *very* expensive; only
2368 * do it if failed >= 2
2369 */
2370 int other;
2371 for (other = disks; other--; ) {
2372 if (other == disk_idx)
2373 continue;
2374 if (!test_bit(R5_UPTODATE,
2375 &sh->dev[other].flags))
2376 break;
2377 }
2378 BUG_ON(other < 0);
2379 pr_debug("Computing stripe %llu blocks %d,%d\n",
2380 (unsigned long long)sh->sector,
2381 disk_idx, other);
2382 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2383 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2384 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2385 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2386 sh->ops.target = disk_idx;
2387 sh->ops.target2 = other;
2388 s->uptodate += 2;
2389 s->req_compute = 1;
2390 return 1;
2391 } else if (test_bit(R5_Insync, &dev->flags)) {
2392 set_bit(R5_LOCKED, &dev->flags);
2393 set_bit(R5_Wantread, &dev->flags);
2394 s->locked++;
2395 pr_debug("Reading block %d (sync=%d)\n",
2396 disk_idx, s->syncing);
2397 }
2398 }
2399
2400 return 0;
2401}
2402
2403/**
2404 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2405 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002406static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002407 struct stripe_head_state *s, struct r6_state *r6s,
2408 int disks)
2409{
2410 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002411
2412 /* look for blocks to read/compute, skip this if a compute
2413 * is already in flight, or if the stripe contents are in the
2414 * midst of changing due to a write
2415 */
2416 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2417 !sh->reconstruct_state)
2418 for (i = disks; i--; )
2419 if (fetch_block6(sh, s, r6s, i, disks))
2420 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002421 set_bit(STRIPE_HANDLE, &sh->state);
2422}
2423
2424
Dan Williams1fe797e2008-06-28 09:16:30 +10002425/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002426 * any written block on an uptodate or failed drive can be returned.
2427 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2428 * never LOCKED, so we don't need to test 'failed' directly.
2429 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002430static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002431 struct stripe_head *sh, int disks, struct bio **return_bi)
2432{
2433 int i;
2434 struct r5dev *dev;
2435
2436 for (i = disks; i--; )
2437 if (sh->dev[i].written) {
2438 dev = &sh->dev[i];
2439 if (!test_bit(R5_LOCKED, &dev->flags) &&
2440 test_bit(R5_UPTODATE, &dev->flags)) {
2441 /* We can return any write requests */
2442 struct bio *wbi, *wbi2;
2443 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002444 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002445 spin_lock_irq(&conf->device_lock);
2446 wbi = dev->written;
2447 dev->written = NULL;
2448 while (wbi && wbi->bi_sector <
2449 dev->sector + STRIPE_SECTORS) {
2450 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002451 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002452 md_write_end(conf->mddev);
2453 wbi->bi_next = *return_bi;
2454 *return_bi = wbi;
2455 }
2456 wbi = wbi2;
2457 }
2458 if (dev->towrite == NULL)
2459 bitmap_end = 1;
2460 spin_unlock_irq(&conf->device_lock);
2461 if (bitmap_end)
2462 bitmap_endwrite(conf->mddev->bitmap,
2463 sh->sector,
2464 STRIPE_SECTORS,
2465 !test_bit(STRIPE_DEGRADED, &sh->state),
2466 0);
2467 }
2468 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002469
2470 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2471 if (atomic_dec_and_test(&conf->pending_full_writes))
2472 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002473}
2474
Dan Williams1fe797e2008-06-28 09:16:30 +10002475static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002476 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2477{
2478 int rmw = 0, rcw = 0, i;
2479 for (i = disks; i--; ) {
2480 /* would I have to read this buffer for read_modify_write */
2481 struct r5dev *dev = &sh->dev[i];
2482 if ((dev->towrite || i == sh->pd_idx) &&
2483 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002484 !(test_bit(R5_UPTODATE, &dev->flags) ||
2485 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002486 if (test_bit(R5_Insync, &dev->flags))
2487 rmw++;
2488 else
2489 rmw += 2*disks; /* cannot read it */
2490 }
2491 /* Would I have to read this buffer for reconstruct_write */
2492 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2493 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002494 !(test_bit(R5_UPTODATE, &dev->flags) ||
2495 test_bit(R5_Wantcompute, &dev->flags))) {
2496 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002497 else
2498 rcw += 2*disks;
2499 }
2500 }
Dan Williams45b42332007-07-09 11:56:43 -07002501 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002502 (unsigned long long)sh->sector, rmw, rcw);
2503 set_bit(STRIPE_HANDLE, &sh->state);
2504 if (rmw < rcw && rmw > 0)
2505 /* prefer read-modify-write, but need to get some data */
2506 for (i = disks; i--; ) {
2507 struct r5dev *dev = &sh->dev[i];
2508 if ((dev->towrite || i == sh->pd_idx) &&
2509 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002510 !(test_bit(R5_UPTODATE, &dev->flags) ||
2511 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002512 test_bit(R5_Insync, &dev->flags)) {
2513 if (
2514 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002515 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002516 "%d for r-m-w\n", i);
2517 set_bit(R5_LOCKED, &dev->flags);
2518 set_bit(R5_Wantread, &dev->flags);
2519 s->locked++;
2520 } else {
2521 set_bit(STRIPE_DELAYED, &sh->state);
2522 set_bit(STRIPE_HANDLE, &sh->state);
2523 }
2524 }
2525 }
2526 if (rcw <= rmw && rcw > 0)
2527 /* want reconstruct write, but need to get some data */
2528 for (i = disks; i--; ) {
2529 struct r5dev *dev = &sh->dev[i];
2530 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2531 i != sh->pd_idx &&
2532 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002533 !(test_bit(R5_UPTODATE, &dev->flags) ||
2534 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002535 test_bit(R5_Insync, &dev->flags)) {
2536 if (
2537 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002538 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002539 "%d for Reconstruct\n", i);
2540 set_bit(R5_LOCKED, &dev->flags);
2541 set_bit(R5_Wantread, &dev->flags);
2542 s->locked++;
2543 } else {
2544 set_bit(STRIPE_DELAYED, &sh->state);
2545 set_bit(STRIPE_HANDLE, &sh->state);
2546 }
2547 }
2548 }
2549 /* now if nothing is locked, and if we have enough data,
2550 * we can start a write request
2551 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002552 /* since handle_stripe can be called at any time we need to handle the
2553 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002554 * subsequent call wants to start a write request. raid_run_ops only
2555 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002556 * simultaneously. If this is not the case then new writes need to be
2557 * held off until the compute completes.
2558 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002559 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2560 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2561 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002562 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002563}
2564
Dan Williams1fe797e2008-06-28 09:16:30 +10002565static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002566 struct stripe_head *sh, struct stripe_head_state *s,
2567 struct r6_state *r6s, int disks)
2568{
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002569 int rcw = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002570 int qd_idx = sh->qd_idx;
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002571
2572 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002573 for (i = disks; i--; ) {
2574 struct r5dev *dev = &sh->dev[i];
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002575 /* check if we haven't enough data */
2576 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2577 i != pd_idx && i != qd_idx &&
2578 !test_bit(R5_LOCKED, &dev->flags) &&
2579 !(test_bit(R5_UPTODATE, &dev->flags) ||
2580 test_bit(R5_Wantcompute, &dev->flags))) {
2581 rcw++;
2582 if (!test_bit(R5_Insync, &dev->flags))
2583 continue; /* it's a failed drive */
2584
2585 if (
2586 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2587 pr_debug("Read_old stripe %llu "
2588 "block %d for Reconstruct\n",
2589 (unsigned long long)sh->sector, i);
2590 set_bit(R5_LOCKED, &dev->flags);
2591 set_bit(R5_Wantread, &dev->flags);
2592 s->locked++;
2593 } else {
2594 pr_debug("Request delayed stripe %llu "
2595 "block %d for Reconstruct\n",
2596 (unsigned long long)sh->sector, i);
2597 set_bit(STRIPE_DELAYED, &sh->state);
2598 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002599 }
2600 }
2601 }
Dan Williamsa4456852007-07-09 11:56:43 -07002602 /* now if nothing is locked, and if we have enough data, we can start a
2603 * write request
2604 */
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002605 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2606 s->locked == 0 && rcw == 0 &&
Dan Williamsa4456852007-07-09 11:56:43 -07002607 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002608 schedule_reconstruction(sh, s, 1, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002609 }
2610}
2611
2612static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2613 struct stripe_head_state *s, int disks)
2614{
Dan Williamsecc65c92008-06-28 08:31:57 +10002615 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002616
Dan Williamsbd2ab672008-04-10 21:29:27 -07002617 set_bit(STRIPE_HANDLE, &sh->state);
2618
Dan Williamsecc65c92008-06-28 08:31:57 +10002619 switch (sh->check_state) {
2620 case check_state_idle:
2621 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002622 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002623 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002624 sh->check_state = check_state_run;
2625 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002626 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002627 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002628 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002629 }
Dan Williamsa4456852007-07-09 11:56:43 -07002630 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002631 /* fall through */
2632 case check_state_compute_result:
2633 sh->check_state = check_state_idle;
2634 if (!dev)
2635 dev = &sh->dev[sh->pd_idx];
2636
2637 /* check that a write has not made the stripe insync */
2638 if (test_bit(STRIPE_INSYNC, &sh->state))
2639 break;
2640
2641 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002642 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2643 BUG_ON(s->uptodate != disks);
2644
2645 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002646 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002647 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002648
Dan Williamsa4456852007-07-09 11:56:43 -07002649 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002650 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002651 break;
2652 case check_state_run:
2653 break; /* we will be called again upon completion */
2654 case check_state_check_result:
2655 sh->check_state = check_state_idle;
2656
2657 /* if a failure occurred during the check operation, leave
2658 * STRIPE_INSYNC not set and let the stripe be handled again
2659 */
2660 if (s->failed)
2661 break;
2662
2663 /* handle a successful check operation, if parity is correct
2664 * we are done. Otherwise update the mismatch count and repair
2665 * parity if !MD_RECOVERY_CHECK
2666 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002667 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002668 /* parity is correct (on disc,
2669 * not in buffer any more)
2670 */
2671 set_bit(STRIPE_INSYNC, &sh->state);
2672 else {
2673 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2674 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2675 /* don't try to repair!! */
2676 set_bit(STRIPE_INSYNC, &sh->state);
2677 else {
2678 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002679 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002680 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2681 set_bit(R5_Wantcompute,
2682 &sh->dev[sh->pd_idx].flags);
2683 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002684 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002685 s->uptodate++;
2686 }
2687 }
2688 break;
2689 case check_state_compute_run:
2690 break;
2691 default:
2692 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2693 __func__, sh->check_state,
2694 (unsigned long long) sh->sector);
2695 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002696 }
2697}
2698
2699
2700static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002701 struct stripe_head_state *s,
2702 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002703{
Dan Williamsa4456852007-07-09 11:56:43 -07002704 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002705 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002706 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002707
2708 set_bit(STRIPE_HANDLE, &sh->state);
2709
2710 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002711
Dan Williamsa4456852007-07-09 11:56:43 -07002712 /* Want to check and possibly repair P and Q.
2713 * However there could be one 'failed' device, in which
2714 * case we can only check one of them, possibly using the
2715 * other to generate missing data
2716 */
2717
Dan Williamsd82dfee2009-07-14 13:40:57 -07002718 switch (sh->check_state) {
2719 case check_state_idle:
2720 /* start a new check operation if there are < 2 failures */
Dan Williamsa4456852007-07-09 11:56:43 -07002721 if (s->failed == r6s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002722 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002723 * makes sense to check P (If anything else were failed,
2724 * we would have used P to recreate it).
2725 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002726 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002727 }
2728 if (!r6s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002729 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002730 * anything, so it makes sense to check it
2731 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002732 if (sh->check_state == check_state_run)
2733 sh->check_state = check_state_run_pq;
2734 else
2735 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002736 }
Dan Williams36d1c642009-07-14 11:48:22 -07002737
Dan Williamsd82dfee2009-07-14 13:40:57 -07002738 /* discard potentially stale zero_sum_result */
2739 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002740
Dan Williamsd82dfee2009-07-14 13:40:57 -07002741 if (sh->check_state == check_state_run) {
2742 /* async_xor_zero_sum destroys the contents of P */
2743 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2744 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002745 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002746 if (sh->check_state >= check_state_run &&
2747 sh->check_state <= check_state_run_pq) {
2748 /* async_syndrome_zero_sum preserves P and Q, so
2749 * no need to mark them !uptodate here
2750 */
2751 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2752 break;
2753 }
Dan Williams36d1c642009-07-14 11:48:22 -07002754
Dan Williamsd82dfee2009-07-14 13:40:57 -07002755 /* we have 2-disk failure */
2756 BUG_ON(s->failed != 2);
2757 /* fall through */
2758 case check_state_compute_result:
2759 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002760
Dan Williamsd82dfee2009-07-14 13:40:57 -07002761 /* check that a write has not made the stripe insync */
2762 if (test_bit(STRIPE_INSYNC, &sh->state))
2763 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002764
2765 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002766 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002767 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002768 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002769 if (s->failed == 2) {
2770 dev = &sh->dev[r6s->failed_num[1]];
2771 s->locked++;
2772 set_bit(R5_LOCKED, &dev->flags);
2773 set_bit(R5_Wantwrite, &dev->flags);
2774 }
2775 if (s->failed >= 1) {
2776 dev = &sh->dev[r6s->failed_num[0]];
2777 s->locked++;
2778 set_bit(R5_LOCKED, &dev->flags);
2779 set_bit(R5_Wantwrite, &dev->flags);
2780 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002781 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002782 dev = &sh->dev[pd_idx];
2783 s->locked++;
2784 set_bit(R5_LOCKED, &dev->flags);
2785 set_bit(R5_Wantwrite, &dev->flags);
2786 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002787 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002788 dev = &sh->dev[qd_idx];
2789 s->locked++;
2790 set_bit(R5_LOCKED, &dev->flags);
2791 set_bit(R5_Wantwrite, &dev->flags);
2792 }
2793 clear_bit(STRIPE_DEGRADED, &sh->state);
2794
2795 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002796 break;
2797 case check_state_run:
2798 case check_state_run_q:
2799 case check_state_run_pq:
2800 break; /* we will be called again upon completion */
2801 case check_state_check_result:
2802 sh->check_state = check_state_idle;
2803
2804 /* handle a successful check operation, if parity is correct
2805 * we are done. Otherwise update the mismatch count and repair
2806 * parity if !MD_RECOVERY_CHECK
2807 */
2808 if (sh->ops.zero_sum_result == 0) {
2809 /* both parities are correct */
2810 if (!s->failed)
2811 set_bit(STRIPE_INSYNC, &sh->state);
2812 else {
2813 /* in contrast to the raid5 case we can validate
2814 * parity, but still have a failure to write
2815 * back
2816 */
2817 sh->check_state = check_state_compute_result;
2818 /* Returning at this point means that we may go
2819 * off and bring p and/or q uptodate again so
2820 * we make sure to check zero_sum_result again
2821 * to verify if p or q need writeback
2822 */
2823 }
2824 } else {
2825 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2826 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2827 /* don't try to repair!! */
2828 set_bit(STRIPE_INSYNC, &sh->state);
2829 else {
2830 int *target = &sh->ops.target;
2831
2832 sh->ops.target = -1;
2833 sh->ops.target2 = -1;
2834 sh->check_state = check_state_compute_run;
2835 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2836 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2837 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2838 set_bit(R5_Wantcompute,
2839 &sh->dev[pd_idx].flags);
2840 *target = pd_idx;
2841 target = &sh->ops.target2;
2842 s->uptodate++;
2843 }
2844 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2845 set_bit(R5_Wantcompute,
2846 &sh->dev[qd_idx].flags);
2847 *target = qd_idx;
2848 s->uptodate++;
2849 }
2850 }
2851 }
2852 break;
2853 case check_state_compute_run:
2854 break;
2855 default:
2856 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2857 __func__, sh->check_state,
2858 (unsigned long long) sh->sector);
2859 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002860 }
2861}
2862
2863static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2864 struct r6_state *r6s)
2865{
2866 int i;
2867
2868 /* We have read all the blocks in this stripe and now we need to
2869 * copy some of them into a target stripe for expand.
2870 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002871 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002872 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2873 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002874 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002875 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002876 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002877 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002878
NeilBrown784052e2009-03-31 15:19:07 +11002879 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002880 sector_t s = raid5_compute_sector(conf, bn, 0,
2881 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002882 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002883 if (sh2 == NULL)
2884 /* so far only the early blocks of this stripe
2885 * have been requested. When later blocks
2886 * get requested, we will try again
2887 */
2888 continue;
2889 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2890 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2891 /* must have already done this block */
2892 release_stripe(sh2);
2893 continue;
2894 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002895
2896 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002897 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002898 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002899 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002900 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002901
Dan Williamsa4456852007-07-09 11:56:43 -07002902 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2903 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2904 for (j = 0; j < conf->raid_disks; j++)
2905 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002906 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002907 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2908 break;
2909 if (j == conf->raid_disks) {
2910 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2911 set_bit(STRIPE_HANDLE, &sh2->state);
2912 }
2913 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002914
Dan Williamsa4456852007-07-09 11:56:43 -07002915 }
NeilBrowna2e08552007-09-11 15:23:36 -07002916 /* done submitting copies, wait for them to complete */
2917 if (tx) {
2918 async_tx_ack(tx);
2919 dma_wait_for_async_tx(tx);
2920 }
Dan Williamsa4456852007-07-09 11:56:43 -07002921}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Dan Williams6bfe0b42008-04-30 00:52:32 -07002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924/*
2925 * handle_stripe - do things to a stripe.
2926 *
2927 * We lock the stripe and then examine the state of various bits
2928 * to see what needs to be done.
2929 * Possible results:
2930 * return some read request which now have data
2931 * return some write requests which are safely on disc
2932 * schedule a read on some buffers
2933 * schedule a write of some buffers
2934 * return confirmation of parity correctness
2935 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 * buffers are taken off read_list or write_list, and bh_cache buffers
2937 * get BH_Lock set before the stripe lock is released.
2938 *
2939 */
Dan Williamsa4456852007-07-09 11:56:43 -07002940
NeilBrown14425772009-10-16 15:55:25 +11002941static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002944 int disks = sh->disks, i;
2945 struct bio *return_bi = NULL;
2946 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002948 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002949 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Dan Williamsa4456852007-07-09 11:56:43 -07002951 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002952 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2953 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2954 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2955 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 spin_lock(&sh->lock);
2958 clear_bit(STRIPE_HANDLE, &sh->state);
2959 clear_bit(STRIPE_DELAYED, &sh->state);
2960
Dan Williamsa4456852007-07-09 11:56:43 -07002961 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2962 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2963 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002966 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 for (i=disks; i--; ) {
2968 mdk_rdev_t *rdev;
NeilBrowna9f326e2009-09-23 18:06:41 +10002969
2970 dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Dan Williamsb5e98d62007-01-02 13:52:31 -07002973 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2974 "written %p\n", i, dev->flags, dev->toread, dev->read,
2975 dev->towrite, dev->written);
2976
2977 /* maybe we can request a biofill operation
2978 *
2979 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002980 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002981 */
2982 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002983 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002984 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002987 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2988 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002989 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Dan Williamsb5e98d62007-01-02 13:52:31 -07002991 if (test_bit(R5_Wantfill, &dev->flags))
2992 s.to_fill++;
2993 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002994 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002996 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002998 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
Dan Williamsa4456852007-07-09 11:56:43 -07003000 if (dev->written)
3001 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08003002 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003003 if (blocked_rdev == NULL &&
3004 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003005 blocked_rdev = rdev;
3006 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003007 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003008 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08003009 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08003010 clear_bit(R5_ReadError, &dev->flags);
3011 clear_bit(R5_ReWrite, &dev->flags);
3012 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003013 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003014 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003015 s.failed++;
3016 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 } else
3018 set_bit(R5_Insync, &dev->flags);
3019 }
NeilBrown9910f162006-01-06 00:20:24 -08003020 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07003021
Dan Williams6bfe0b42008-04-30 00:52:32 -07003022 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003023 if (s.syncing || s.expanding || s.expanded ||
3024 s.to_write || s.written) {
3025 set_bit(STRIPE_HANDLE, &sh->state);
3026 goto unlock;
3027 }
3028 /* There is nothing for the blocked_rdev to block */
3029 rdev_dec_pending(blocked_rdev, conf->mddev);
3030 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003031 }
3032
Dan Williams83de75c2008-06-28 08:31:58 +10003033 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3034 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3035 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3036 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07003037
Dan Williams45b42332007-07-09 11:56:43 -07003038 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003040 s.locked, s.uptodate, s.to_read, s.to_write,
3041 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* check if the array has lost two devices and, if so, some requests might
3043 * need to be failed
3044 */
Dan Williamsa4456852007-07-09 11:56:43 -07003045 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003046 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003047 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3049 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003050 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
3052
3053 /* might be able to return some write requests if the parity block
3054 * is safe, or on a failed drive
3055 */
3056 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003057 if ( s.written &&
3058 ((test_bit(R5_Insync, &dev->flags) &&
3059 !test_bit(R5_LOCKED, &dev->flags) &&
3060 test_bit(R5_UPTODATE, &dev->flags)) ||
3061 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10003062 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 /* Now we might consider reading some blocks, either to check/generate
3065 * parity, or to satisfy requests
3066 * or to load a block that is being partially written.
3067 */
Dan Williamsa4456852007-07-09 11:56:43 -07003068 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10003069 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003070 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Dan Williamse33129d2007-01-02 13:52:30 -07003072 /* Now we check to see if any write operations have recently
3073 * completed
3074 */
Dan Williamse0a115e2008-06-05 22:45:52 -07003075 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003076 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07003077 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003078 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3079 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10003080 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07003081
3082 /* All the 'written' buffers and the parity block are ready to
3083 * be written back to disk
3084 */
3085 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3086 for (i = disks; i--; ) {
3087 dev = &sh->dev[i];
3088 if (test_bit(R5_LOCKED, &dev->flags) &&
3089 (i == sh->pd_idx || dev->written)) {
3090 pr_debug("Writing block %d\n", i);
3091 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07003092 if (prexor)
3093 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07003094 if (!test_bit(R5_Insync, &dev->flags) ||
3095 (i == sh->pd_idx && s.failed == 0))
3096 set_bit(STRIPE_INSYNC, &sh->state);
3097 }
3098 }
3099 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3100 atomic_dec(&conf->preread_active_stripes);
3101 if (atomic_read(&conf->preread_active_stripes) <
3102 IO_THRESHOLD)
3103 md_wakeup_thread(conf->mddev->thread);
3104 }
3105 }
3106
3107 /* Now to consider new write requests and what else, if anything
3108 * should be read. We do not handle new writes when:
3109 * 1/ A 'write' operation (copy+xor) is already in flight.
3110 * 2/ A 'check' operation is in flight, as it may clobber the parity
3111 * block.
3112 */
Dan Williams600aa102008-06-28 08:32:05 +10003113 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003114 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07003117 * Any reads will already have been scheduled, so we just see if enough
3118 * data is available. The parity check is held off while parity
3119 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 */
Dan Williamsecc65c92008-06-28 08:31:57 +10003121 if (sh->check_state ||
3122 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003123 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10003124 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07003125 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07003126
Dan Williamsa4456852007-07-09 11:56:43 -07003127 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3129 clear_bit(STRIPE_SYNCING, &sh->state);
3130 }
NeilBrown4e5314b2005-11-08 21:39:22 -08003131
3132 /* If the failed drive is just a ReadError, then we might need to progress
3133 * the repair/check process
3134 */
Dan Williamsa4456852007-07-09 11:56:43 -07003135 if (s.failed == 1 && !conf->mddev->ro &&
3136 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3137 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3138 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003139 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003140 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003141 if (!test_bit(R5_ReWrite, &dev->flags)) {
3142 set_bit(R5_Wantwrite, &dev->flags);
3143 set_bit(R5_ReWrite, &dev->flags);
3144 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003145 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003146 } else {
3147 /* let's read it back */
3148 set_bit(R5_Wantread, &dev->flags);
3149 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003150 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003151 }
3152 }
3153
Dan Williams600aa102008-06-28 08:32:05 +10003154 /* Finish reconstruct operations initiated by the expansion process */
3155 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003156 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003157 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003158 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3159 /* sh cannot be written until sh2 has been read.
3160 * so arrange for sh to be delayed a little
3161 */
3162 set_bit(STRIPE_DELAYED, &sh->state);
3163 set_bit(STRIPE_HANDLE, &sh->state);
3164 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3165 &sh2->state))
3166 atomic_inc(&conf->preread_active_stripes);
3167 release_stripe(sh2);
3168 goto unlock;
3169 }
3170 if (sh2)
3171 release_stripe(sh2);
3172
Dan Williams600aa102008-06-28 08:32:05 +10003173 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003174 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003175 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003176 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003177 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003178 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003179 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003180 }
3181
3182 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003183 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003184 /* Need to write out all blocks after computing parity */
3185 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003186 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003187 schedule_reconstruction(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003188 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003189 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003190 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003191 wake_up(&conf->wait_for_overlap);
3192 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3193 }
3194
Dan Williams0f94e872008-01-08 15:32:53 -08003195 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003196 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003197 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003198
Dan Williams6bfe0b42008-04-30 00:52:32 -07003199 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 spin_unlock(&sh->lock);
3201
Dan Williams6bfe0b42008-04-30 00:52:32 -07003202 /* wait for this device to become unblocked */
3203 if (unlikely(blocked_rdev))
3204 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3205
Dan Williams600aa102008-06-28 08:32:05 +10003206 if (s.ops_request)
Dan Williamsac6b53b2009-07-14 13:40:19 -07003207 raid_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003208
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003209 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Dan Williamsa4456852007-07-09 11:56:43 -07003211 return_io(return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
NeilBrown14425772009-10-16 15:55:25 +11003214static void handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003215{
NeilBrownbff61972009-03-31 14:33:13 +11003216 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003217 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003218 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003219 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003220 struct stripe_head_state s;
3221 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003222 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003223 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003224
Dan Williams45b42332007-07-09 11:56:43 -07003225 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003226 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003227 (unsigned long long)sh->sector, sh->state,
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003228 atomic_read(&sh->count), pd_idx, qd_idx,
3229 sh->check_state, sh->reconstruct_state);
Dan Williamsa4456852007-07-09 11:56:43 -07003230 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003231
3232 spin_lock(&sh->lock);
3233 clear_bit(STRIPE_HANDLE, &sh->state);
3234 clear_bit(STRIPE_DELAYED, &sh->state);
3235
Dan Williamsa4456852007-07-09 11:56:43 -07003236 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3237 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3238 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003239 /* Now to look around and see what can be done */
3240
3241 rcu_read_lock();
3242 for (i=disks; i--; ) {
3243 mdk_rdev_t *rdev;
3244 dev = &sh->dev[i];
3245 clear_bit(R5_Insync, &dev->flags);
3246
Dan Williams45b42332007-07-09 11:56:43 -07003247 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003248 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003249 /* maybe we can reply to a read
3250 *
3251 * new wantfill requests are only permitted while
3252 * ops_complete_biofill is guaranteed to be inactive
3253 */
3254 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3255 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3256 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003257
3258 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003259 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3260 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003261 if (test_bit(R5_Wantcompute, &dev->flags)) {
3262 s.compute++;
3263 BUG_ON(s.compute > 2);
3264 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003265
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003266 if (test_bit(R5_Wantfill, &dev->flags)) {
3267 s.to_fill++;
3268 } else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003269 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003270 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003271 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003272 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003273 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003274 }
Dan Williamsa4456852007-07-09 11:56:43 -07003275 if (dev->written)
3276 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003277 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003278 if (blocked_rdev == NULL &&
3279 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003280 blocked_rdev = rdev;
3281 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003282 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003283 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3284 /* The ReadError flag will just be confusing now */
3285 clear_bit(R5_ReadError, &dev->flags);
3286 clear_bit(R5_ReWrite, &dev->flags);
3287 }
3288 if (!rdev || !test_bit(In_sync, &rdev->flags)
3289 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003290 if (s.failed < 2)
3291 r6s.failed_num[s.failed] = i;
3292 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003293 } else
3294 set_bit(R5_Insync, &dev->flags);
3295 }
3296 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003297
3298 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003299 if (s.syncing || s.expanding || s.expanded ||
3300 s.to_write || s.written) {
3301 set_bit(STRIPE_HANDLE, &sh->state);
3302 goto unlock;
3303 }
3304 /* There is nothing for the blocked_rdev to block */
3305 rdev_dec_pending(blocked_rdev, conf->mddev);
3306 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003307 }
NeilBrownac4090d2008-08-05 15:54:13 +10003308
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003309 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3310 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3311 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3312 }
3313
Dan Williams45b42332007-07-09 11:56:43 -07003314 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003315 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003316 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3317 r6s.failed_num[0], r6s.failed_num[1]);
3318 /* check if the array has lost >2 devices and, if so, some requests
3319 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003320 */
Dan Williamsa4456852007-07-09 11:56:43 -07003321 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003322 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003323 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003324 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3325 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003326 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003327 }
3328
3329 /*
3330 * might be able to return some write requests if the parity blocks
3331 * are safe, or on a failed drive
3332 */
3333 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003334 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3335 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003336 qdev = &sh->dev[qd_idx];
3337 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3338 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003339
Dan Williamsa4456852007-07-09 11:56:43 -07003340 if ( s.written &&
3341 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003342 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003343 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3344 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003345 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003346 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003347 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003348
3349 /* Now we might consider reading some blocks, either to check/generate
3350 * parity, or to satisfy requests
3351 * or to load a block that is being partially written.
3352 */
Dan Williamsa4456852007-07-09 11:56:43 -07003353 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003354 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003355 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003356
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003357 /* Now we check to see if any write operations have recently
3358 * completed
3359 */
3360 if (sh->reconstruct_state == reconstruct_state_drain_result) {
3361 int qd_idx = sh->qd_idx;
3362
3363 sh->reconstruct_state = reconstruct_state_idle;
3364 /* All the 'written' buffers and the parity blocks are ready to
3365 * be written back to disk
3366 */
3367 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3368 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
3369 for (i = disks; i--; ) {
3370 dev = &sh->dev[i];
3371 if (test_bit(R5_LOCKED, &dev->flags) &&
3372 (i == sh->pd_idx || i == qd_idx ||
3373 dev->written)) {
3374 pr_debug("Writing block %d\n", i);
3375 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3376 set_bit(R5_Wantwrite, &dev->flags);
3377 if (!test_bit(R5_Insync, &dev->flags) ||
3378 ((i == sh->pd_idx || i == qd_idx) &&
3379 s.failed == 0))
3380 set_bit(STRIPE_INSYNC, &sh->state);
3381 }
3382 }
3383 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3384 atomic_dec(&conf->preread_active_stripes);
3385 if (atomic_read(&conf->preread_active_stripes) <
3386 IO_THRESHOLD)
3387 md_wakeup_thread(conf->mddev->thread);
3388 }
3389 }
3390
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07003391 /* Now to consider new write requests and what else, if anything
3392 * should be read. We do not handle new writes when:
3393 * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
3394 * 2/ A 'check' operation is in flight, as it may clobber the parity
3395 * block.
3396 */
3397 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003398 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003399
3400 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003401 * Any reads will already have been scheduled, so we just see if enough
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003402 * data is available. The parity check is held off while parity
3403 * dependent operations are in flight.
NeilBrown16a53ec2006-06-26 00:27:38 -07003404 */
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003405 if (sh->check_state ||
3406 (s.syncing && s.locked == 0 &&
3407 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3408 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williams36d1c642009-07-14 11:48:22 -07003409 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003410
Dan Williamsa4456852007-07-09 11:56:43 -07003411 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003412 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3413 clear_bit(STRIPE_SYNCING, &sh->state);
3414 }
3415
3416 /* If the failed drives are just a ReadError, then we might need
3417 * to progress the repair/check process
3418 */
Dan Williamsa4456852007-07-09 11:56:43 -07003419 if (s.failed <= 2 && !conf->mddev->ro)
3420 for (i = 0; i < s.failed; i++) {
3421 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003422 if (test_bit(R5_ReadError, &dev->flags)
3423 && !test_bit(R5_LOCKED, &dev->flags)
3424 && test_bit(R5_UPTODATE, &dev->flags)
3425 ) {
3426 if (!test_bit(R5_ReWrite, &dev->flags)) {
3427 set_bit(R5_Wantwrite, &dev->flags);
3428 set_bit(R5_ReWrite, &dev->flags);
3429 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003430 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003431 } else {
3432 /* let's read it back */
3433 set_bit(R5_Wantread, &dev->flags);
3434 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003435 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003436 }
3437 }
3438 }
NeilBrownf4168852007-02-28 20:11:53 -08003439
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003440 /* Finish reconstruct operations initiated by the expansion process */
3441 if (sh->reconstruct_state == reconstruct_state_result) {
3442 sh->reconstruct_state = reconstruct_state_idle;
3443 clear_bit(STRIPE_EXPANDING, &sh->state);
3444 for (i = conf->raid_disks; i--; ) {
3445 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3446 set_bit(R5_LOCKED, &sh->dev[i].flags);
3447 s.locked++;
3448 }
3449 }
3450
3451 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3452 !sh->reconstruct_state) {
NeilBrownab69ae12009-03-31 15:26:47 +11003453 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003454 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003455 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3456 /* sh cannot be written until sh2 has been read.
3457 * so arrange for sh to be delayed a little
3458 */
3459 set_bit(STRIPE_DELAYED, &sh->state);
3460 set_bit(STRIPE_HANDLE, &sh->state);
3461 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3462 &sh2->state))
3463 atomic_inc(&conf->preread_active_stripes);
3464 release_stripe(sh2);
3465 goto unlock;
3466 }
3467 if (sh2)
3468 release_stripe(sh2);
3469
NeilBrownf4168852007-02-28 20:11:53 -08003470 /* Need to write out all blocks after computing P&Q */
3471 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003472 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003473 schedule_reconstruction(sh, &s, 1, 1);
3474 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003475 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3476 atomic_dec(&conf->reshape_stripes);
3477 wake_up(&conf->wait_for_overlap);
3478 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3479 }
3480
Dan Williams0f94e872008-01-08 15:32:53 -08003481 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003482 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003483 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003484
Dan Williams6bfe0b42008-04-30 00:52:32 -07003485 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003486 spin_unlock(&sh->lock);
3487
Dan Williams6bfe0b42008-04-30 00:52:32 -07003488 /* wait for this device to become unblocked */
3489 if (unlikely(blocked_rdev))
3490 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3491
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003492 if (s.ops_request)
3493 raid_run_ops(sh, s.ops_request);
3494
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003495 ops_run_io(sh, &s);
3496
Dan Williamsa4456852007-07-09 11:56:43 -07003497 return_io(return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003498}
3499
NeilBrown14425772009-10-16 15:55:25 +11003500static void handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003501{
3502 if (sh->raid_conf->level == 6)
NeilBrown14425772009-10-16 15:55:25 +11003503 handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003504 else
NeilBrown14425772009-10-16 15:55:25 +11003505 handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003506}
3507
Arjan van de Ven858119e2006-01-14 13:20:43 -08003508static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
3510 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3511 while (!list_empty(&conf->delayed_list)) {
3512 struct list_head *l = conf->delayed_list.next;
3513 struct stripe_head *sh;
3514 sh = list_entry(l, struct stripe_head, lru);
3515 list_del_init(l);
3516 clear_bit(STRIPE_DELAYED, &sh->state);
3517 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3518 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003519 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
NeilBrown6ed30032008-02-06 01:40:00 -08003521 } else
3522 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Arjan van de Ven858119e2006-01-14 13:20:43 -08003525static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003526{
3527 /* device_lock is held */
3528 struct list_head head;
3529 list_add(&head, &conf->bitmap_list);
3530 list_del_init(&conf->bitmap_list);
3531 while (!list_empty(&head)) {
3532 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3533 list_del_init(&sh->lru);
3534 atomic_inc(&sh->count);
3535 __release_stripe(conf, sh);
3536 }
3537}
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539static void unplug_slaves(mddev_t *mddev)
3540{
NeilBrown070ec552009-06-16 16:54:21 +10003541 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 int i;
3543
3544 rcu_read_lock();
NeilBrownf001a702009-06-09 14:30:31 +10003545 for (i = 0; i < conf->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003546 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003547 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003548 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
3550 atomic_inc(&rdev->nr_pending);
3551 rcu_read_unlock();
3552
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003553 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555 rdev_dec_pending(rdev, mddev);
3556 rcu_read_lock();
3557 }
3558 }
3559 rcu_read_unlock();
3560}
3561
Jens Axboe165125e2007-07-24 09:28:11 +02003562static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
3564 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003565 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 unsigned long flags;
3567
3568 spin_lock_irqsave(&conf->device_lock, flags);
3569
NeilBrown72626682005-09-09 16:23:54 -07003570 if (blk_remove_plug(q)) {
3571 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 md_wakeup_thread(mddev->thread);
3575
3576 spin_unlock_irqrestore(&conf->device_lock, flags);
3577
3578 unplug_slaves(mddev);
3579}
3580
NeilBrownf022b2f2006-10-03 01:15:56 -07003581static int raid5_congested(void *data, int bits)
3582{
3583 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +10003584 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003585
3586 /* No difference between reads and writes. Just check
3587 * how busy the stripe_cache is
3588 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003589
3590 if (mddev_congested(mddev, bits))
3591 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07003592 if (conf->inactive_blocked)
3593 return 1;
3594 if (conf->quiesce)
3595 return 1;
3596 if (list_empty_careful(&conf->inactive_list))
3597 return 1;
3598
3599 return 0;
3600}
3601
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003602/* We want read requests to align with chunks where possible,
3603 * but write requests don't need to.
3604 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003605static int raid5_mergeable_bvec(struct request_queue *q,
3606 struct bvec_merge_data *bvm,
3607 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003608{
3609 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003610 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003611 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003612 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003613 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003614
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003615 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003616 return biovec->bv_len; /* always allow writes to be mergeable */
3617
Andre Noll664e7c42009-06-18 08:45:27 +10003618 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3619 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003620 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3621 if (max < 0) max = 0;
3622 if (max <= biovec->bv_len && bio_sectors == 0)
3623 return biovec->bv_len;
3624 else
3625 return max;
3626}
3627
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003628
3629static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3630{
3631 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003632 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003633 unsigned int bio_sectors = bio->bi_size >> 9;
3634
Andre Noll664e7c42009-06-18 08:45:27 +10003635 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3636 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003637 return chunk_sectors >=
3638 ((sector & (chunk_sectors - 1)) + bio_sectors);
3639}
3640
3641/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003642 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3643 * later sampled by raid5d.
3644 */
3645static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3646{
3647 unsigned long flags;
3648
3649 spin_lock_irqsave(&conf->device_lock, flags);
3650
3651 bi->bi_next = conf->retry_read_aligned_list;
3652 conf->retry_read_aligned_list = bi;
3653
3654 spin_unlock_irqrestore(&conf->device_lock, flags);
3655 md_wakeup_thread(conf->mddev->thread);
3656}
3657
3658
3659static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3660{
3661 struct bio *bi;
3662
3663 bi = conf->retry_read_aligned;
3664 if (bi) {
3665 conf->retry_read_aligned = NULL;
3666 return bi;
3667 }
3668 bi = conf->retry_read_aligned_list;
3669 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003670 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003671 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003672 /*
3673 * this sets the active strip count to 1 and the processed
3674 * strip count to zero (upper 8 bits)
3675 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003676 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003677 }
3678
3679 return bi;
3680}
3681
3682
3683/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003684 * The "raid5_align_endio" should check if the read succeeded and if it
3685 * did, call bio_endio on the original bio (having bio_put the new bio
3686 * first).
3687 * If the read failed..
3688 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003689static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003690{
3691 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003692 mddev_t *mddev;
3693 raid5_conf_t *conf;
3694 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3695 mdk_rdev_t *rdev;
3696
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003697 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003698
3699 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003700 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003701 rdev = (void*)raid_bi->bi_next;
3702 raid_bi->bi_next = NULL;
3703
3704 rdev_dec_pending(rdev, conf->mddev);
3705
3706 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003707 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003708 if (atomic_dec_and_test(&conf->active_aligned_reads))
3709 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003710 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003711 }
3712
3713
Dan Williams45b42332007-07-09 11:56:43 -07003714 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003715
3716 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003717}
3718
Neil Brown387bb172007-02-08 14:20:29 -08003719static int bio_fits_rdev(struct bio *bi)
3720{
Jens Axboe165125e2007-07-24 09:28:11 +02003721 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003722
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003723 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003724 return 0;
3725 blk_recount_segments(q, bi);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003726 if (bi->bi_phys_segments > queue_max_phys_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003727 return 0;
3728
3729 if (q->merge_bvec_fn)
3730 /* it's too hard to apply the merge_bvec_fn at this stage,
3731 * just just give up
3732 */
3733 return 0;
3734
3735 return 1;
3736}
3737
3738
Jens Axboe165125e2007-07-24 09:28:11 +02003739static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003740{
3741 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003742 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003743 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003744 struct bio* align_bi;
3745 mdk_rdev_t *rdev;
3746
3747 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003748 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003749 return 0;
3750 }
3751 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003752 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003753 */
3754 align_bi = bio_clone(raid_bio, GFP_NOIO);
3755 if (!align_bi)
3756 return 0;
3757 /*
3758 * set bi_end_io to a new function, and set bi_private to the
3759 * original bio.
3760 */
3761 align_bi->bi_end_io = raid5_align_endio;
3762 align_bi->bi_private = raid_bio;
3763 /*
3764 * compute position
3765 */
NeilBrown112bf892009-03-31 14:39:38 +11003766 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3767 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003768 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003769
3770 rcu_read_lock();
3771 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3772 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003773 atomic_inc(&rdev->nr_pending);
3774 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003775 raid_bio->bi_next = (void*)rdev;
3776 align_bi->bi_bdev = rdev->bdev;
3777 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3778 align_bi->bi_sector += rdev->data_offset;
3779
Neil Brown387bb172007-02-08 14:20:29 -08003780 if (!bio_fits_rdev(align_bi)) {
3781 /* too big in some way */
3782 bio_put(align_bi);
3783 rdev_dec_pending(rdev, mddev);
3784 return 0;
3785 }
3786
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003787 spin_lock_irq(&conf->device_lock);
3788 wait_event_lock_irq(conf->wait_for_stripe,
3789 conf->quiesce == 0,
3790 conf->device_lock, /* nothing */);
3791 atomic_inc(&conf->active_aligned_reads);
3792 spin_unlock_irq(&conf->device_lock);
3793
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003794 generic_make_request(align_bi);
3795 return 1;
3796 } else {
3797 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003798 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003799 return 0;
3800 }
3801}
3802
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003803/* __get_priority_stripe - get the next stripe to process
3804 *
3805 * Full stripe writes are allowed to pass preread active stripes up until
3806 * the bypass_threshold is exceeded. In general the bypass_count
3807 * increments when the handle_list is handled before the hold_list; however, it
3808 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3809 * stripe with in flight i/o. The bypass_count will be reset when the
3810 * head of the hold_list has changed, i.e. the head was promoted to the
3811 * handle_list.
3812 */
3813static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3814{
3815 struct stripe_head *sh;
3816
3817 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3818 __func__,
3819 list_empty(&conf->handle_list) ? "empty" : "busy",
3820 list_empty(&conf->hold_list) ? "empty" : "busy",
3821 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3822
3823 if (!list_empty(&conf->handle_list)) {
3824 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3825
3826 if (list_empty(&conf->hold_list))
3827 conf->bypass_count = 0;
3828 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3829 if (conf->hold_list.next == conf->last_hold)
3830 conf->bypass_count++;
3831 else {
3832 conf->last_hold = conf->hold_list.next;
3833 conf->bypass_count -= conf->bypass_threshold;
3834 if (conf->bypass_count < 0)
3835 conf->bypass_count = 0;
3836 }
3837 }
3838 } else if (!list_empty(&conf->hold_list) &&
3839 ((conf->bypass_threshold &&
3840 conf->bypass_count > conf->bypass_threshold) ||
3841 atomic_read(&conf->pending_full_writes) == 0)) {
3842 sh = list_entry(conf->hold_list.next,
3843 typeof(*sh), lru);
3844 conf->bypass_count -= conf->bypass_threshold;
3845 if (conf->bypass_count < 0)
3846 conf->bypass_count = 0;
3847 } else
3848 return NULL;
3849
3850 list_del_init(&sh->lru);
3851 atomic_inc(&sh->count);
3852 BUG_ON(atomic_read(&sh->count) != 1);
3853 return sh;
3854}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003855
Jens Axboe165125e2007-07-24 09:28:11 +02003856static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
3858 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003859 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003860 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 sector_t new_sector;
3862 sector_t logical_sector, last_sector;
3863 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003864 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003865 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Jens Axboe1f98a132009-09-11 14:32:04 +02003867 if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003868 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003869 return 0;
3870 }
3871
NeilBrown3d310eb2005-06-21 17:17:26 -07003872 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003873
Tejun Heo074a7ac2008-08-25 19:56:14 +09003874 cpu = part_stat_lock();
3875 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3876 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3877 bio_sectors(bi));
3878 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
NeilBrown802ba062006-12-13 00:34:13 -08003880 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003881 mddev->reshape_position == MaxSector &&
3882 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003883 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3886 last_sector = bi->bi_sector + (bi->bi_size>>9);
3887 bi->bi_next = NULL;
3888 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3891 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003892 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003893 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003894
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003895 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003896 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003897 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003898 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003899 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003900 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003901 * 64bit on a 32bit platform, and so it might be
3902 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003903 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003904 * the lock is dropped, so once we get a reference
3905 * to the stripe that we think it is, we will have
3906 * to check again.
3907 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003908 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003909 if (mddev->delta_disks < 0
3910 ? logical_sector < conf->reshape_progress
3911 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003912 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003913 previous = 1;
3914 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003915 if (mddev->delta_disks < 0
3916 ? logical_sector < conf->reshape_safe
3917 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003918 spin_unlock_irq(&conf->device_lock);
3919 schedule();
3920 goto retry;
3921 }
3922 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003923 spin_unlock_irq(&conf->device_lock);
3924 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003925 data_disks = disks - conf->max_degraded;
3926
NeilBrown112bf892009-03-31 14:39:38 +11003927 new_sector = raid5_compute_sector(conf, logical_sector,
3928 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003929 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003930 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 (unsigned long long)new_sector,
3932 (unsigned long long)logical_sector);
3933
NeilBrownb5663ba2009-03-31 14:39:38 +11003934 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003935 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003937 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003938 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003939 * stripe, so we must do the range check again.
3940 * Expansion could still move past after this
3941 * test, but as we are holding a reference to
3942 * 'sh', we know that if that happens,
3943 * STRIPE_EXPANDING will get set and the expansion
3944 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003945 */
3946 int must_retry = 0;
3947 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003948 if (mddev->delta_disks < 0
3949 ? logical_sector >= conf->reshape_progress
3950 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003951 /* mismatch, need to try again */
3952 must_retry = 1;
3953 spin_unlock_irq(&conf->device_lock);
3954 if (must_retry) {
3955 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003956 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003957 goto retry;
3958 }
3959 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003960
NeilBrowna5c308d2009-07-01 13:15:35 +10003961 if (bio_data_dir(bi) == WRITE &&
3962 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003963 logical_sector < mddev->suspend_hi) {
3964 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003965 /* As the suspend_* range is controlled by
3966 * userspace, we want an interruptible
3967 * wait.
3968 */
3969 flush_signals(current);
3970 prepare_to_wait(&conf->wait_for_overlap,
3971 &w, TASK_INTERRUPTIBLE);
3972 if (logical_sector >= mddev->suspend_lo &&
3973 logical_sector < mddev->suspend_hi)
3974 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003975 goto retry;
3976 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003977
3978 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3979 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3980 /* Stripe is busy expanding or
3981 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 * and wait a while
3983 */
3984 raid5_unplug_device(mddev->queue);
3985 release_stripe(sh);
3986 schedule();
3987 goto retry;
3988 }
3989 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003990 set_bit(STRIPE_HANDLE, &sh->state);
3991 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 } else {
3994 /* cannot get stripe for read-ahead, just give-up */
3995 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3996 finish_wait(&conf->wait_for_overlap, &w);
3997 break;
3998 }
3999
4000 }
4001 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004002 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004003 spin_unlock_irq(&conf->device_lock);
4004 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
NeilBrown16a53ec2006-06-26 00:27:38 -07004006 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004008
Neil Brown0e13fe232008-06-28 08:31:20 +10004009 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 return 0;
4012}
4013
Dan Williamsb522adc2009-03-31 15:00:31 +11004014static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
4015
NeilBrown52c03292006-06-26 00:27:43 -07004016static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
NeilBrown52c03292006-06-26 00:27:43 -07004018 /* reshaping is quite different to recovery/resync so it is
4019 * handled quite separately ... here.
4020 *
4021 * On each call to sync_request, we gather one chunk worth of
4022 * destination stripes and flag them as expanding.
4023 * Then we find all the source stripes and request reads.
4024 * As the reads complete, handle_stripe will copy the data
4025 * into the destination stripe and release that stripe.
4026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4028 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004029 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004030 int raid_disks = conf->previous_raid_disks;
4031 int data_disks = raid_disks - conf->max_degraded;
4032 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004033 int i;
4034 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004035 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004036 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004037 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004038 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004039
NeilBrownfef9c612009-03-31 15:16:46 +11004040 if (sector_nr == 0) {
4041 /* If restarting in the middle, skip the initial sectors */
4042 if (mddev->delta_disks < 0 &&
4043 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4044 sector_nr = raid5_size(mddev, 0, 0)
4045 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004046 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004047 conf->reshape_progress > 0)
4048 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004049 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004050 if (sector_nr) {
4051 *skipped = 1;
4052 return sector_nr;
4053 }
NeilBrown52c03292006-06-26 00:27:43 -07004054 }
4055
NeilBrown7a661382009-03-31 15:21:40 +11004056 /* We need to process a full chunk at a time.
4057 * If old and new chunk sizes differ, we need to process the
4058 * largest of these
4059 */
Andre Noll664e7c42009-06-18 08:45:27 +10004060 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4061 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004062 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004063 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004064
NeilBrown52c03292006-06-26 00:27:43 -07004065 /* we update the metadata when there is more than 3Meg
4066 * in the block range (that is rather arbitrary, should
4067 * probably be time based) or when the data about to be
4068 * copied would over-write the source of the data at
4069 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004070 * i.e. one new_stripe along from reshape_progress new_maps
4071 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004072 */
NeilBrownfef9c612009-03-31 15:16:46 +11004073 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004074 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004075 readpos = conf->reshape_progress;
4076 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004077 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004078 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004079 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004080 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004081 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004082 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004083 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004084 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004085 readpos -= min_t(sector_t, reshape_sectors, readpos);
4086 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004087 }
NeilBrown52c03292006-06-26 00:27:43 -07004088
NeilBrownc8f517c2009-03-31 15:28:40 +11004089 /* 'writepos' is the most advanced device address we might write.
4090 * 'readpos' is the least advanced device address we might read.
4091 * 'safepos' is the least address recorded in the metadata as having
4092 * been reshaped.
4093 * If 'readpos' is behind 'writepos', then there is no way that we can
4094 * ensure safety in the face of a crash - that must be done by userspace
4095 * making a backup of the data. So in that case there is no particular
4096 * rush to update metadata.
4097 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4098 * update the metadata to advance 'safepos' to match 'readpos' so that
4099 * we can be safe in the event of a crash.
4100 * So we insist on updating metadata if safepos is behind writepos and
4101 * readpos is beyond writepos.
4102 * In any case, update the metadata every 10 seconds.
4103 * Maybe that number should be configurable, but I'm not sure it is
4104 * worth it.... maybe it could be a multiple of safemode_delay???
4105 */
NeilBrownfef9c612009-03-31 15:16:46 +11004106 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004107 ? (safepos > writepos && readpos < writepos)
4108 : (safepos < writepos && readpos > writepos)) ||
4109 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004110 /* Cannot proceed until we've updated the superblock... */
4111 wait_event(conf->wait_for_overlap,
4112 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004113 mddev->reshape_position = conf->reshape_progress;
NeilBrownacb180b2009-04-14 16:28:34 +10004114 mddev->curr_resync_completed = mddev->curr_resync;
NeilBrownc8f517c2009-03-31 15:28:40 +11004115 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004116 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004117 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004118 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004119 kthread_should_stop());
4120 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004121 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004122 spin_unlock_irq(&conf->device_lock);
4123 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004124 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004125 }
4126
NeilBrownec32a2b2009-03-31 15:17:38 +11004127 if (mddev->delta_disks < 0) {
4128 BUG_ON(conf->reshape_progress == 0);
4129 stripe_addr = writepos;
4130 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004131 ~((sector_t)reshape_sectors - 1))
4132 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004133 != sector_nr);
4134 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004135 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004136 stripe_addr = sector_nr;
4137 }
NeilBrownab69ae12009-03-31 15:26:47 +11004138 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004139 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004140 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004141 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004142 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004143 set_bit(STRIPE_EXPANDING, &sh->state);
4144 atomic_inc(&conf->reshape_stripes);
4145 /* If any of this stripe is beyond the end of the old
4146 * array, then we need to zero those blocks
4147 */
4148 for (j=sh->disks; j--;) {
4149 sector_t s;
4150 if (j == sh->pd_idx)
4151 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004152 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004153 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004154 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004155 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004156 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004157 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004158 continue;
4159 }
4160 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4161 set_bit(R5_Expanded, &sh->dev[j].flags);
4162 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4163 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004164 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004165 set_bit(STRIPE_EXPAND_READY, &sh->state);
4166 set_bit(STRIPE_HANDLE, &sh->state);
4167 }
NeilBrownab69ae12009-03-31 15:26:47 +11004168 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004169 }
4170 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004171 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004172 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004173 else
NeilBrown7a661382009-03-31 15:21:40 +11004174 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004175 spin_unlock_irq(&conf->device_lock);
4176 /* Ok, those stripe are ready. We can start scheduling
4177 * reads on the source stripes.
4178 * The source stripes are determined by mapping the first and last
4179 * block on the destination stripes.
4180 */
NeilBrown52c03292006-06-26 00:27:43 -07004181 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004182 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004183 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004184 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004185 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004186 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004187 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004188 if (last_sector >= mddev->dev_sectors)
4189 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004190 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004191 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004192 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4193 set_bit(STRIPE_HANDLE, &sh->state);
4194 release_stripe(sh);
4195 first_sector += STRIPE_SECTORS;
4196 }
NeilBrownab69ae12009-03-31 15:26:47 +11004197 /* Now that the sources are clearly marked, we can release
4198 * the destination stripes
4199 */
4200 while (!list_empty(&stripes)) {
4201 sh = list_entry(stripes.next, struct stripe_head, lru);
4202 list_del_init(&sh->lru);
4203 release_stripe(sh);
4204 }
NeilBrownc6207272008-02-06 01:39:52 -08004205 /* If this takes us to the resync_max point where we have to pause,
4206 * then we need to write out the superblock.
4207 */
NeilBrown7a661382009-03-31 15:21:40 +11004208 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004209 if ((sector_nr - mddev->curr_resync_completed) * 2
4210 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004211 /* Cannot proceed until we've updated the superblock... */
4212 wait_event(conf->wait_for_overlap,
4213 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004214 mddev->reshape_position = conf->reshape_progress;
NeilBrown48606a92009-06-18 09:14:12 +10004215 mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11004216 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004217 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4218 md_wakeup_thread(mddev->thread);
4219 wait_event(mddev->sb_wait,
4220 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4221 || kthread_should_stop());
4222 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004223 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004224 spin_unlock_irq(&conf->device_lock);
4225 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004226 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004227 }
NeilBrown7a661382009-03-31 15:21:40 +11004228 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004229}
4230
4231/* FIXME go_faster isn't used */
4232static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4233{
4234 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4235 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004236 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07004237 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004238 int still_degraded = 0;
4239 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
NeilBrown72626682005-09-09 16:23:54 -07004241 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 /* just being told to finish up .. nothing much to do */
4243 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004244
NeilBrown29269552006-03-27 01:18:10 -08004245 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4246 end_reshape(conf);
4247 return 0;
4248 }
NeilBrown72626682005-09-09 16:23:54 -07004249
4250 if (mddev->curr_resync < max_sector) /* aborted */
4251 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4252 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004253 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004254 conf->fullsync = 0;
4255 bitmap_close_sync(mddev->bitmap);
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 return 0;
4258 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004259
NeilBrown64bd6602009-08-03 10:59:58 +10004260 /* Allow raid5_quiesce to complete */
4261 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4262
NeilBrown52c03292006-06-26 00:27:43 -07004263 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4264 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004265
NeilBrownc6207272008-02-06 01:39:52 -08004266 /* No need to check resync_max as we never do more than one
4267 * stripe, and as resync_max will always be on a chunk boundary,
4268 * if the check in md_do_sync didn't fire, there is no chance
4269 * of overstepping resync_max here
4270 */
4271
NeilBrown16a53ec2006-06-26 00:27:38 -07004272 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 * to resync, then assert that we are finished, because there is
4274 * nothing we can do.
4275 */
NeilBrown3285edf2006-06-26 00:27:55 -07004276 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004277 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004278 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004279 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return rv;
4281 }
NeilBrown72626682005-09-09 16:23:54 -07004282 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004283 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004284 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4285 /* we can skip this block, and probably more */
4286 sync_blocks /= STRIPE_SECTORS;
4287 *skipped = 1;
4288 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
NeilBrownb47490c2008-02-06 01:39:50 -08004291
4292 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4293
NeilBrowna8c906c2009-06-09 14:39:59 +10004294 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004296 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004298 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004300 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004302 /* Need to check if array will still be degraded after recovery/resync
4303 * We don't need to check the 'failed' flag as when that gets set,
4304 * recovery aborts.
4305 */
NeilBrownf001a702009-06-09 14:30:31 +10004306 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004307 if (conf->disks[i].rdev == NULL)
4308 still_degraded = 1;
4309
4310 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4311
4312 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 set_bit(STRIPE_SYNCING, &sh->state);
4314 clear_bit(STRIPE_INSYNC, &sh->state);
4315 spin_unlock(&sh->lock);
4316
NeilBrown14425772009-10-16 15:55:25 +11004317 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 release_stripe(sh);
4319
4320 return STRIPE_SECTORS;
4321}
4322
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004323static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4324{
4325 /* We may not be able to submit a whole bio at once as there
4326 * may not be enough stripe_heads available.
4327 * We cannot pre-allocate enough stripe_heads as we may need
4328 * more than exist in the cache (if we allow ever large chunks).
4329 * So we do one stripe head at a time and record in
4330 * ->bi_hw_segments how many have been done.
4331 *
4332 * We *know* that this entire raid_bio is in one chunk, so
4333 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4334 */
4335 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004336 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004337 sector_t sector, logical_sector, last_sector;
4338 int scnt = 0;
4339 int remaining;
4340 int handled = 0;
4341
4342 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004343 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004344 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004345 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4346
4347 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004348 logical_sector += STRIPE_SECTORS,
4349 sector += STRIPE_SECTORS,
4350 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004351
Jens Axboe960e7392008-08-15 10:41:18 +02004352 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004353 /* already done this stripe */
4354 continue;
4355
NeilBrowna8c906c2009-06-09 14:39:59 +10004356 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004357
4358 if (!sh) {
4359 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004360 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004361 conf->retry_read_aligned = raid_bio;
4362 return handled;
4363 }
4364
4365 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004366 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4367 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004368 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004369 conf->retry_read_aligned = raid_bio;
4370 return handled;
4371 }
4372
Dan Williams36d1c642009-07-14 11:48:22 -07004373 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004374 release_stripe(sh);
4375 handled++;
4376 }
4377 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004378 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004379 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004380 if (remaining == 0)
4381 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004382 if (atomic_dec_and_test(&conf->active_aligned_reads))
4383 wake_up(&conf->wait_for_stripe);
4384 return handled;
4385}
4386
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388/*
4389 * This is our raid5 kernel thread.
4390 *
4391 * We scan the hash table for stripes which can be handled now.
4392 * During the scan, completed stripes are saved for us by the interrupt
4393 * handler, so that they will not have to wait for our next wakeup.
4394 */
NeilBrown6ed30032008-02-06 01:40:00 -08004395static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
4397 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004398 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 int handled;
4400
Dan Williams45b42332007-07-09 11:56:43 -07004401 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
4403 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 handled = 0;
4406 spin_lock_irq(&conf->device_lock);
4407 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004408 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
NeilBrownae3c20c2006-07-10 04:44:17 -07004410 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004411 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004412 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004413 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004414 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004415 conf->seq_write = seq;
4416 activate_bit_delay(conf);
4417 }
4418
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004419 while ((bio = remove_bio_from_retry(conf))) {
4420 int ok;
4421 spin_unlock_irq(&conf->device_lock);
4422 ok = retry_aligned_read(conf, bio);
4423 spin_lock_irq(&conf->device_lock);
4424 if (!ok)
4425 break;
4426 handled++;
4427 }
4428
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004429 sh = __get_priority_stripe(conf);
4430
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004431 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 spin_unlock_irq(&conf->device_lock);
4434
4435 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004436 handle_stripe(sh);
4437 release_stripe(sh);
4438 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440 spin_lock_irq(&conf->device_lock);
4441 }
Dan Williams45b42332007-07-09 11:56:43 -07004442 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 spin_unlock_irq(&conf->device_lock);
4445
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004446 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 unplug_slaves(mddev);
4448
Dan Williams45b42332007-07-09 11:56:43 -07004449 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
4451
NeilBrown3f294f42005-11-08 21:39:25 -08004452static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004453raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004454{
NeilBrown070ec552009-06-16 16:54:21 +10004455 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004456 if (conf)
4457 return sprintf(page, "%d\n", conf->max_nr_stripes);
4458 else
4459 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004460}
4461
4462static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004463raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004464{
NeilBrown070ec552009-06-16 16:54:21 +10004465 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004466 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004467 int err;
4468
NeilBrown3f294f42005-11-08 21:39:25 -08004469 if (len >= PAGE_SIZE)
4470 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004471 if (!conf)
4472 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004473
Dan Williams4ef197d82008-04-28 02:15:54 -07004474 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004475 return -EINVAL;
4476 if (new <= 16 || new > 32768)
4477 return -EINVAL;
4478 while (new < conf->max_nr_stripes) {
4479 if (drop_one_stripe(conf))
4480 conf->max_nr_stripes--;
4481 else
4482 break;
4483 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004484 err = md_allow_write(mddev);
4485 if (err)
4486 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004487 while (new > conf->max_nr_stripes) {
4488 if (grow_one_stripe(conf))
4489 conf->max_nr_stripes++;
4490 else break;
4491 }
4492 return len;
4493}
NeilBrown007583c2005-11-08 21:39:30 -08004494
NeilBrown96de1e62005-11-08 21:39:39 -08004495static struct md_sysfs_entry
4496raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4497 raid5_show_stripe_cache_size,
4498 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004499
4500static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004501raid5_show_preread_threshold(mddev_t *mddev, char *page)
4502{
NeilBrown070ec552009-06-16 16:54:21 +10004503 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004504 if (conf)
4505 return sprintf(page, "%d\n", conf->bypass_threshold);
4506 else
4507 return 0;
4508}
4509
4510static ssize_t
4511raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4512{
NeilBrown070ec552009-06-16 16:54:21 +10004513 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004514 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004515 if (len >= PAGE_SIZE)
4516 return -EINVAL;
4517 if (!conf)
4518 return -ENODEV;
4519
Dan Williams4ef197d82008-04-28 02:15:54 -07004520 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004521 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004522 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004523 return -EINVAL;
4524 conf->bypass_threshold = new;
4525 return len;
4526}
4527
4528static struct md_sysfs_entry
4529raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4530 S_IRUGO | S_IWUSR,
4531 raid5_show_preread_threshold,
4532 raid5_store_preread_threshold);
4533
4534static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004535stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004536{
NeilBrown070ec552009-06-16 16:54:21 +10004537 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004538 if (conf)
4539 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4540 else
4541 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004542}
4543
NeilBrown96de1e62005-11-08 21:39:39 -08004544static struct md_sysfs_entry
4545raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004546
NeilBrown007583c2005-11-08 21:39:30 -08004547static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004548 &raid5_stripecache_size.attr,
4549 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004550 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004551 NULL,
4552};
NeilBrown007583c2005-11-08 21:39:30 -08004553static struct attribute_group raid5_attrs_group = {
4554 .name = NULL,
4555 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004556};
4557
Dan Williams80c3a6c2009-03-17 18:10:40 -07004558static sector_t
4559raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4560{
NeilBrown070ec552009-06-16 16:54:21 +10004561 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004562
4563 if (!sectors)
4564 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004565 if (!raid_disks) {
4566 /* size is defined by the smallest of previous and new size */
4567 if (conf->raid_disks < conf->previous_raid_disks)
4568 raid_disks = conf->raid_disks;
4569 else
4570 raid_disks = conf->previous_raid_disks;
4571 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004572
Andre Noll9d8f0362009-06-18 08:45:01 +10004573 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004574 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004575 return sectors * (raid_disks - conf->max_degraded);
4576}
4577
Dan Williams36d1c642009-07-14 11:48:22 -07004578static void raid5_free_percpu(raid5_conf_t *conf)
4579{
4580 struct raid5_percpu *percpu;
4581 unsigned long cpu;
4582
4583 if (!conf->percpu)
4584 return;
4585
4586 get_online_cpus();
4587 for_each_possible_cpu(cpu) {
4588 percpu = per_cpu_ptr(conf->percpu, cpu);
4589 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004590 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004591 }
4592#ifdef CONFIG_HOTPLUG_CPU
4593 unregister_cpu_notifier(&conf->cpu_notify);
4594#endif
4595 put_online_cpus();
4596
4597 free_percpu(conf->percpu);
4598}
4599
Dan Williams95fc17a2009-07-31 12:39:15 +10004600static void free_conf(raid5_conf_t *conf)
4601{
4602 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004603 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004604 kfree(conf->disks);
4605 kfree(conf->stripe_hashtbl);
4606 kfree(conf);
4607}
4608
Dan Williams36d1c642009-07-14 11:48:22 -07004609#ifdef CONFIG_HOTPLUG_CPU
4610static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4611 void *hcpu)
4612{
4613 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4614 long cpu = (long)hcpu;
4615 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4616
4617 switch (action) {
4618 case CPU_UP_PREPARE:
4619 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004620 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004621 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004622 if (!percpu->scribble)
4623 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4624
4625 if (!percpu->scribble ||
4626 (conf->level == 6 && !percpu->spare_page)) {
4627 safe_put_page(percpu->spare_page);
4628 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004629 pr_err("%s: failed memory allocation for cpu%ld\n",
4630 __func__, cpu);
4631 return NOTIFY_BAD;
4632 }
4633 break;
4634 case CPU_DEAD:
4635 case CPU_DEAD_FROZEN:
4636 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004637 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004638 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004639 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004640 break;
4641 default:
4642 break;
4643 }
4644 return NOTIFY_OK;
4645}
4646#endif
4647
4648static int raid5_alloc_percpu(raid5_conf_t *conf)
4649{
4650 unsigned long cpu;
4651 struct page *spare_page;
4652 struct raid5_percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004653 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004654 int err;
4655
Dan Williams36d1c642009-07-14 11:48:22 -07004656 allcpus = alloc_percpu(struct raid5_percpu);
4657 if (!allcpus)
4658 return -ENOMEM;
4659 conf->percpu = allcpus;
4660
4661 get_online_cpus();
4662 err = 0;
4663 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004664 if (conf->level == 6) {
4665 spare_page = alloc_page(GFP_KERNEL);
4666 if (!spare_page) {
4667 err = -ENOMEM;
4668 break;
4669 }
4670 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4671 }
4672 scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL);
4673 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004674 err = -ENOMEM;
4675 break;
4676 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004677 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004678 }
4679#ifdef CONFIG_HOTPLUG_CPU
4680 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4681 conf->cpu_notify.priority = 0;
4682 if (err == 0)
4683 err = register_cpu_notifier(&conf->cpu_notify);
4684#endif
4685 put_online_cpus();
4686
4687 return err;
4688}
4689
NeilBrown91adb562009-03-31 14:39:39 +11004690static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691{
4692 raid5_conf_t *conf;
4693 int raid_disk, memory;
4694 mdk_rdev_t *rdev;
4695 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
NeilBrown91adb562009-03-31 14:39:39 +11004697 if (mddev->new_level != 5
4698 && mddev->new_level != 4
4699 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004700 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004701 mdname(mddev), mddev->new_level);
4702 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
NeilBrown91adb562009-03-31 14:39:39 +11004704 if ((mddev->new_level == 5
4705 && !algorithm_valid_raid5(mddev->new_layout)) ||
4706 (mddev->new_level == 6
4707 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004708 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004709 mdname(mddev), mddev->new_layout);
4710 return ERR_PTR(-EIO);
4711 }
4712 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4713 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4714 mdname(mddev), mddev->raid_disks);
4715 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Andre Noll664e7c42009-06-18 08:45:27 +10004718 if (!mddev->new_chunk_sectors ||
4719 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4720 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown91adb562009-03-31 14:39:39 +11004721 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
Andre Noll664e7c42009-06-18 08:45:27 +10004722 mddev->new_chunk_sectors << 9, mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004723 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004724 }
4725
NeilBrown91adb562009-03-31 14:39:39 +11004726 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4727 if (conf == NULL)
4728 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004729 spin_lock_init(&conf->device_lock);
4730 init_waitqueue_head(&conf->wait_for_stripe);
4731 init_waitqueue_head(&conf->wait_for_overlap);
4732 INIT_LIST_HEAD(&conf->handle_list);
4733 INIT_LIST_HEAD(&conf->hold_list);
4734 INIT_LIST_HEAD(&conf->delayed_list);
4735 INIT_LIST_HEAD(&conf->bitmap_list);
4736 INIT_LIST_HEAD(&conf->inactive_list);
4737 atomic_set(&conf->active_stripes, 0);
4738 atomic_set(&conf->preread_active_stripes, 0);
4739 atomic_set(&conf->active_aligned_reads, 0);
4740 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrown91adb562009-03-31 14:39:39 +11004741
4742 conf->raid_disks = mddev->raid_disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004743 conf->scribble_len = scribble_len(conf->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004744 if (mddev->reshape_position == MaxSector)
4745 conf->previous_raid_disks = mddev->raid_disks;
4746 else
4747 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4748
4749 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4750 GFP_KERNEL);
4751 if (!conf->disks)
4752 goto abort;
4753
4754 conf->mddev = mddev;
4755
4756 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4757 goto abort;
4758
Dan Williams36d1c642009-07-14 11:48:22 -07004759 conf->level = mddev->new_level;
4760 if (raid5_alloc_percpu(conf) != 0)
4761 goto abort;
4762
NeilBrown91adb562009-03-31 14:39:39 +11004763 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4764
4765 list_for_each_entry(rdev, &mddev->disks, same_set) {
4766 raid_disk = rdev->raid_disk;
4767 if (raid_disk >= conf->raid_disks
4768 || raid_disk < 0)
4769 continue;
4770 disk = conf->disks + raid_disk;
4771
4772 disk->rdev = rdev;
4773
4774 if (test_bit(In_sync, &rdev->flags)) {
4775 char b[BDEVNAME_SIZE];
4776 printk(KERN_INFO "raid5: device %s operational as raid"
4777 " disk %d\n", bdevname(rdev->bdev,b),
4778 raid_disk);
4779 } else
4780 /* Cannot rely on bitmap to complete recovery */
4781 conf->fullsync = 1;
4782 }
4783
Andre Noll09c9e5f2009-06-18 08:45:55 +10004784 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004785 conf->level = mddev->new_level;
4786 if (conf->level == 6)
4787 conf->max_degraded = 2;
4788 else
4789 conf->max_degraded = 1;
4790 conf->algorithm = mddev->new_layout;
4791 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004792 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004793 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004794 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004795 conf->prev_algo = mddev->layout;
4796 }
NeilBrown91adb562009-03-31 14:39:39 +11004797
4798 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4799 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4800 if (grow_stripes(conf, conf->max_nr_stripes)) {
4801 printk(KERN_ERR
4802 "raid5: couldn't allocate %dkB for buffers\n", memory);
4803 goto abort;
4804 } else
4805 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4806 memory, mdname(mddev));
4807
NeilBrown0da3c612009-09-23 18:09:45 +10004808 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004809 if (!conf->thread) {
4810 printk(KERN_ERR
4811 "raid5: couldn't allocate thread for %s\n",
4812 mdname(mddev));
4813 goto abort;
4814 }
4815
4816 return conf;
4817
4818 abort:
4819 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004820 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004821 return ERR_PTR(-EIO);
4822 } else
4823 return ERR_PTR(-ENOMEM);
4824}
4825
4826static int run(mddev_t *mddev)
4827{
4828 raid5_conf_t *conf;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004829 int working_disks = 0, chunk_size;
NeilBrown91adb562009-03-31 14:39:39 +11004830 mdk_rdev_t *rdev;
4831
Andre Noll8c6ac862009-06-18 08:48:06 +10004832 if (mddev->recovery_cp != MaxSector)
4833 printk(KERN_NOTICE "raid5: %s is not clean"
4834 " -- starting background reconstruction\n",
4835 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004836 if (mddev->reshape_position != MaxSector) {
4837 /* Check that we can continue the reshape.
4838 * Currently only disks can change, it must
4839 * increase, and we must be past the point where
4840 * a stripe over-writes itself
4841 */
4842 sector_t here_new, here_old;
4843 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004844 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004845
NeilBrown88ce4932009-03-31 15:24:23 +11004846 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004847 printk(KERN_ERR "raid5: %s: unsupported reshape "
4848 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004849 mdname(mddev));
4850 return -EINVAL;
4851 }
NeilBrownf6705572006-03-27 01:18:11 -08004852 old_disks = mddev->raid_disks - mddev->delta_disks;
4853 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004854 * further up in new geometry must map after here in old
4855 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004856 */
4857 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004858 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004859 (mddev->raid_disks - max_degraded))) {
4860 printk(KERN_ERR "raid5: reshape_position not "
4861 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004862 return -EINVAL;
4863 }
4864 /* here_new is the stripe we will write to */
4865 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004866 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004867 (old_disks-max_degraded));
4868 /* here_old is the first stripe that we might need to read
4869 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004870 if (mddev->delta_disks == 0) {
4871 /* We cannot be sure it is safe to start an in-place
4872 * reshape. It is only safe if user-space if monitoring
4873 * and taking constant backups.
4874 * mdadm always starts a situation like this in
4875 * readonly mode so it can take control before
4876 * allowing any writes. So just check for that.
4877 */
4878 if ((here_new * mddev->new_chunk_sectors !=
4879 here_old * mddev->chunk_sectors) ||
4880 mddev->ro == 0) {
4881 printk(KERN_ERR "raid5: in-place reshape must be started"
4882 " in read-only mode - aborting\n");
4883 return -EINVAL;
4884 }
4885 } else if (mddev->delta_disks < 0
4886 ? (here_new * mddev->new_chunk_sectors <=
4887 here_old * mddev->chunk_sectors)
4888 : (here_new * mddev->new_chunk_sectors >=
4889 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004890 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004891 printk(KERN_ERR "raid5: reshape_position too early for "
4892 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004893 return -EINVAL;
4894 }
4895 printk(KERN_INFO "raid5: reshape will continue\n");
4896 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004897 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004898 BUG_ON(mddev->level != mddev->new_level);
4899 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004900 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004901 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004902 }
4903
NeilBrown245f46c2009-03-31 14:39:39 +11004904 if (mddev->private == NULL)
4905 conf = setup_conf(mddev);
4906 else
4907 conf = mddev->private;
4908
NeilBrown91adb562009-03-31 14:39:39 +11004909 if (IS_ERR(conf))
4910 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004911
NeilBrown91adb562009-03-31 14:39:39 +11004912 mddev->thread = conf->thread;
4913 conf->thread = NULL;
4914 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004917 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 */
NeilBrown91adb562009-03-31 14:39:39 +11004919 list_for_each_entry(rdev, &mddev->disks, same_set)
4920 if (rdev->raid_disk >= 0 &&
4921 test_bit(In_sync, &rdev->flags))
4922 working_disks++;
4923
NeilBrown02c2de82006-10-03 01:15:47 -07004924 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
NeilBrown16a53ec2006-06-26 00:27:38 -07004926 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 printk(KERN_ERR "raid5: not enough operational devices for %s"
4928 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004929 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 goto abort;
4931 }
4932
NeilBrown91adb562009-03-31 14:39:39 +11004933 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004934 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004935 mddev->resync_max_sectors = mddev->dev_sectors;
4936
NeilBrown16a53ec2006-06-26 00:27:38 -07004937 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004939 if (mddev->ok_start_degraded)
4940 printk(KERN_WARNING
4941 "raid5: starting dirty degraded array: %s"
4942 "- data corruption possible.\n",
4943 mdname(mddev));
4944 else {
4945 printk(KERN_ERR
4946 "raid5: cannot start dirty degraded array for %s\n",
4947 mdname(mddev));
4948 goto abort;
4949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 }
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 if (mddev->degraded == 0)
4953 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11004954 " devices, algorithm %d\n", conf->level, mdname(mddev),
4955 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4956 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 else
4958 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4959 " out of %d devices, algorithm %d\n", conf->level,
4960 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11004961 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 print_raid5_conf(conf);
4964
NeilBrownfef9c612009-03-31 15:16:46 +11004965 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08004966 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11004967 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004968 atomic_set(&conf->reshape_stripes, 0);
4969 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4970 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4971 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4972 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4973 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004974 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004975 }
4976
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004978 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 */
4980 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004981 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4982 int stripe = data_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10004983 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4985 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4986 }
4987
4988 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004989 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4990 printk(KERN_WARNING
4991 "raid5: failed to create sysfs attributes for %s\n",
4992 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004993
NeilBrown91adb562009-03-31 14:39:39 +11004994 mddev->queue->queue_lock = &conf->device_lock;
4995
NeilBrown7a5febe2005-05-16 21:53:16 -07004996 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004997 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004998 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004999
Dan Williams1f403622009-03-31 14:59:03 +11005000 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07005001
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005002 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10005003 chunk_size = mddev->chunk_sectors << 9;
5004 blk_queue_io_min(mddev->queue, chunk_size);
5005 blk_queue_io_opt(mddev->queue, chunk_size *
5006 (conf->raid_disks - conf->max_degraded));
5007
5008 list_for_each_entry(rdev, &mddev->disks, same_set)
5009 disk_stack_limits(mddev->gendisk, rdev->bdev,
5010 rdev->data_offset << 9);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 return 0;
5013abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11005014 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11005015 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 if (conf) {
5017 print_raid5_conf(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005018 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 }
5020 mddev->private = NULL;
5021 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
5022 return -EIO;
5023}
5024
5025
5026
NeilBrown3f294f42005-11-08 21:39:25 -08005027static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
5029 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5030
5031 md_unregister_thread(mddev->thread);
5032 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08005033 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08005035 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
Dan Williams95fc17a2009-07-31 12:39:15 +10005036 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 mddev->private = NULL;
5038 return 0;
5039}
5040
Dan Williams45b42332007-07-09 11:56:43 -07005041#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11005042static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043{
5044 int i;
5045
NeilBrown16a53ec2006-06-26 00:27:38 -07005046 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
5047 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
5048 seq_printf(seq, "sh %llu, count %d.\n",
5049 (unsigned long long)sh->sector, atomic_read(&sh->count));
5050 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005051 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07005052 seq_printf(seq, "(cache%d: %p %ld) ",
5053 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005055 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056}
5057
NeilBrownd710e132008-10-13 11:55:12 +11005058static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059{
5060 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08005061 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 int i;
5063
5064 spin_lock_irq(&conf->device_lock);
5065 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005066 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 if (sh->raid_conf != conf)
5068 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005069 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
5071 }
5072 spin_unlock_irq(&conf->device_lock);
5073}
5074#endif
5075
NeilBrownd710e132008-10-13 11:55:12 +11005076static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
5078 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5079 int i;
5080
Andre Noll9d8f0362009-06-18 08:45:01 +10005081 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5082 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005083 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 for (i = 0; i < conf->raid_disks; i++)
5085 seq_printf (seq, "%s",
5086 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005087 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005089#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005090 seq_printf (seq, "\n");
5091 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092#endif
5093}
5094
5095static void print_raid5_conf (raid5_conf_t *conf)
5096{
5097 int i;
5098 struct disk_info *tmp;
5099
5100 printk("RAID5 conf printout:\n");
5101 if (!conf) {
5102 printk("(conf==NULL)\n");
5103 return;
5104 }
NeilBrown02c2de82006-10-03 01:15:47 -07005105 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
5106 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
5108 for (i = 0; i < conf->raid_disks; i++) {
5109 char b[BDEVNAME_SIZE];
5110 tmp = conf->disks + i;
5111 if (tmp->rdev)
5112 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08005113 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 bdevname(tmp->rdev->bdev,b));
5115 }
5116}
5117
5118static int raid5_spare_active(mddev_t *mddev)
5119{
5120 int i;
5121 raid5_conf_t *conf = mddev->private;
5122 struct disk_info *tmp;
5123
5124 for (i = 0; i < conf->raid_disks; i++) {
5125 tmp = conf->disks + i;
5126 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08005127 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005128 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5129 unsigned long flags;
5130 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07005132 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 }
5134 }
5135 print_raid5_conf(conf);
5136 return 0;
5137}
5138
5139static int raid5_remove_disk(mddev_t *mddev, int number)
5140{
5141 raid5_conf_t *conf = mddev->private;
5142 int err = 0;
5143 mdk_rdev_t *rdev;
5144 struct disk_info *p = conf->disks + number;
5145
5146 print_raid5_conf(conf);
5147 rdev = p->rdev;
5148 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005149 if (number >= conf->raid_disks &&
5150 conf->reshape_progress == MaxSector)
5151 clear_bit(In_sync, &rdev->flags);
5152
NeilBrownb2d444d2005-11-08 21:39:31 -08005153 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 atomic_read(&rdev->nr_pending)) {
5155 err = -EBUSY;
5156 goto abort;
5157 }
NeilBrowndfc70642008-05-23 13:04:39 -07005158 /* Only remove non-faulty devices if recovery
5159 * isn't possible.
5160 */
5161 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005162 mddev->degraded <= conf->max_degraded &&
5163 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005164 err = -EBUSY;
5165 goto abort;
5166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005168 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 if (atomic_read(&rdev->nr_pending)) {
5170 /* lost the race, try later */
5171 err = -EBUSY;
5172 p->rdev = rdev;
5173 }
5174 }
5175abort:
5176
5177 print_raid5_conf(conf);
5178 return err;
5179}
5180
5181static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5182{
5183 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005184 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 int disk;
5186 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005187 int first = 0;
5188 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
NeilBrown16a53ec2006-06-26 00:27:38 -07005190 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005192 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
Neil Brown6c2fce22008-06-28 08:31:31 +10005194 if (rdev->raid_disk >= 0)
5195 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
5197 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005198 * find the disk ... but prefer rdev->saved_raid_disk
5199 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005201 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005202 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005203 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5204 disk = rdev->saved_raid_disk;
5205 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005206 disk = first;
5207 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005209 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005211 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005212 if (rdev->saved_raid_disk != disk)
5213 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005214 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 break;
5216 }
5217 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005218 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219}
5220
5221static int raid5_resize(mddev_t *mddev, sector_t sectors)
5222{
5223 /* no resync is happening, and there is enough space
5224 * on all devices, so we can resize.
5225 * We need to make sure resync covers any new space.
5226 * If the array is shrinking we should possibly wait until
5227 * any io in the removed space completes, but it hardly seems
5228 * worth it.
5229 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005230 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005231 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5232 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005233 if (mddev->array_sectors >
5234 raid5_size(mddev, sectors, mddev->raid_disks))
5235 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005236 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce6292007-05-09 18:51:36 -07005237 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005238 revalidate_disk(mddev->gendisk);
Andre Noll58c0fed2009-03-31 14:33:13 +11005239 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5240 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5242 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005243 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005244 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 return 0;
5246}
5247
NeilBrown01ee22b2009-06-18 08:47:20 +10005248static int check_stripe_cache(mddev_t *mddev)
5249{
5250 /* Can only proceed if there are plenty of stripe_heads.
5251 * We need a minimum of one full stripe,, and for sensible progress
5252 * it is best to have about 4 times that.
5253 * If we require 4 times, then the default 256 4K stripe_heads will
5254 * allow for chunk sizes up to 256K, which is probably OK.
5255 * If the chunk size is greater, user-space should request more
5256 * stripe_heads first.
5257 */
5258 raid5_conf_t *conf = mddev->private;
5259 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5260 > conf->max_nr_stripes ||
5261 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5262 > conf->max_nr_stripes) {
5263 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
5264 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5265 / STRIPE_SIZE)*4);
5266 return 0;
5267 }
5268 return 1;
5269}
5270
NeilBrown50ac1682009-06-18 08:47:55 +10005271static int check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005272{
NeilBrown070ec552009-06-16 16:54:21 +10005273 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005274
NeilBrown88ce4932009-03-31 15:24:23 +11005275 if (mddev->delta_disks == 0 &&
5276 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005277 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005278 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005279 if (mddev->bitmap)
5280 /* Cannot grow a bitmap yet */
5281 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11005282 if (mddev->degraded > conf->max_degraded)
5283 return -EINVAL;
5284 if (mddev->delta_disks < 0) {
5285 /* We might be able to shrink, but the devices must
5286 * be made bigger first.
5287 * For raid6, 4 is the minimum size.
5288 * Otherwise 2 is the minimum
5289 */
5290 int min = 2;
5291 if (mddev->level == 6)
5292 min = 4;
5293 if (mddev->raid_disks + mddev->delta_disks < min)
5294 return -EINVAL;
5295 }
NeilBrown29269552006-03-27 01:18:10 -08005296
NeilBrown01ee22b2009-06-18 08:47:20 +10005297 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005298 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005299
NeilBrownec32a2b2009-03-31 15:17:38 +11005300 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005301}
5302
5303static int raid5_start_reshape(mddev_t *mddev)
5304{
NeilBrown070ec552009-06-16 16:54:21 +10005305 raid5_conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08005306 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005307 int spares = 0;
5308 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005309 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005310
NeilBrownf4168852007-02-28 20:11:53 -08005311 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005312 return -EBUSY;
5313
NeilBrown01ee22b2009-06-18 08:47:20 +10005314 if (!check_stripe_cache(mddev))
5315 return -ENOSPC;
5316
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005317 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005318 if (rdev->raid_disk < 0 &&
5319 !test_bit(Faulty, &rdev->flags))
5320 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005321
NeilBrownf4168852007-02-28 20:11:53 -08005322 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005323 /* Not enough devices even to make a degraded array
5324 * of that size
5325 */
5326 return -EINVAL;
5327
NeilBrownec32a2b2009-03-31 15:17:38 +11005328 /* Refuse to reduce size of the array. Any reductions in
5329 * array size must be through explicit setting of array_size
5330 * attribute.
5331 */
5332 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5333 < mddev->array_sectors) {
5334 printk(KERN_ERR "md: %s: array size must be reduced "
5335 "before number of disks\n", mdname(mddev));
5336 return -EINVAL;
5337 }
5338
NeilBrownf6705572006-03-27 01:18:11 -08005339 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005340 spin_lock_irq(&conf->device_lock);
5341 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005342 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005343 conf->prev_chunk_sectors = conf->chunk_sectors;
5344 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005345 conf->prev_algo = conf->algorithm;
5346 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005347 if (mddev->delta_disks < 0)
5348 conf->reshape_progress = raid5_size(mddev, 0, 0);
5349 else
5350 conf->reshape_progress = 0;
5351 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005352 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005353 spin_unlock_irq(&conf->device_lock);
5354
5355 /* Add some new drives, as many as will fit.
5356 * We know there are enough to make the newly sized array work.
5357 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005358 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005359 if (rdev->raid_disk < 0 &&
5360 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005361 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005362 char nm[20];
5363 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08005364 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005365 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005366 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005367 if (sysfs_create_link(&mddev->kobj,
5368 &rdev->kobj, nm))
5369 printk(KERN_WARNING
5370 "raid5: failed to create "
5371 " link %s for %s\n",
5372 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005373 } else
5374 break;
5375 }
5376
NeilBrownec32a2b2009-03-31 15:17:38 +11005377 if (mddev->delta_disks > 0) {
5378 spin_lock_irqsave(&conf->device_lock, flags);
5379 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
5380 - added_devices;
5381 spin_unlock_irqrestore(&conf->device_lock, flags);
5382 }
NeilBrown63c70c42006-03-27 01:18:13 -08005383 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005384 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005385 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005386
NeilBrown29269552006-03-27 01:18:10 -08005387 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5388 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5389 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5390 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5391 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005392 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005393 if (!mddev->sync_thread) {
5394 mddev->recovery = 0;
5395 spin_lock_irq(&conf->device_lock);
5396 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005397 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005398 spin_unlock_irq(&conf->device_lock);
5399 return -EAGAIN;
5400 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005401 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005402 md_wakeup_thread(mddev->sync_thread);
5403 md_new_event(mddev);
5404 return 0;
5405}
NeilBrown29269552006-03-27 01:18:10 -08005406
NeilBrownec32a2b2009-03-31 15:17:38 +11005407/* This is called from the reshape thread and should make any
5408 * changes needed in 'conf'
5409 */
NeilBrown29269552006-03-27 01:18:10 -08005410static void end_reshape(raid5_conf_t *conf)
5411{
NeilBrown29269552006-03-27 01:18:10 -08005412
NeilBrownf6705572006-03-27 01:18:11 -08005413 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005414
NeilBrownf6705572006-03-27 01:18:11 -08005415 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005416 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005417 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005418 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005419 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005420
5421 /* read-ahead size must cover two whole stripes, which is
5422 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5423 */
5424 {
NeilBrowncea9c222009-03-31 15:15:05 +11005425 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005426 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005427 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005428 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5429 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5430 }
NeilBrown29269552006-03-27 01:18:10 -08005431 }
NeilBrown29269552006-03-27 01:18:10 -08005432}
5433
NeilBrownec32a2b2009-03-31 15:17:38 +11005434/* This is called from the raid5d thread with mddev_lock held.
5435 * It makes config changes to the device.
5436 */
NeilBrowncea9c222009-03-31 15:15:05 +11005437static void raid5_finish_reshape(mddev_t *mddev)
5438{
NeilBrown070ec552009-06-16 16:54:21 +10005439 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005440
5441 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5442
NeilBrownec32a2b2009-03-31 15:17:38 +11005443 if (mddev->delta_disks > 0) {
5444 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5445 set_capacity(mddev->gendisk, mddev->array_sectors);
5446 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005447 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005448 } else {
5449 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005450 mddev->degraded = conf->raid_disks;
5451 for (d = 0; d < conf->raid_disks ; d++)
5452 if (conf->disks[d].rdev &&
5453 test_bit(In_sync,
5454 &conf->disks[d].rdev->flags))
5455 mddev->degraded--;
5456 for (d = conf->raid_disks ;
5457 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005458 d++) {
5459 mdk_rdev_t *rdev = conf->disks[d].rdev;
5460 if (rdev && raid5_remove_disk(mddev, d) == 0) {
5461 char nm[20];
5462 sprintf(nm, "rd%d", rdev->raid_disk);
5463 sysfs_remove_link(&mddev->kobj, nm);
5464 rdev->raid_disk = -1;
5465 }
5466 }
NeilBrowncea9c222009-03-31 15:15:05 +11005467 }
NeilBrown88ce4932009-03-31 15:24:23 +11005468 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005469 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005470 mddev->reshape_position = MaxSector;
5471 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005472 }
5473}
5474
NeilBrown72626682005-09-09 16:23:54 -07005475static void raid5_quiesce(mddev_t *mddev, int state)
5476{
NeilBrown070ec552009-06-16 16:54:21 +10005477 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005478
5479 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005480 case 2: /* resume for a suspend */
5481 wake_up(&conf->wait_for_overlap);
5482 break;
5483
NeilBrown72626682005-09-09 16:23:54 -07005484 case 1: /* stop all writes */
5485 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005486 /* '2' tells resync/reshape to pause so that all
5487 * active stripes can drain
5488 */
5489 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005490 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005491 atomic_read(&conf->active_stripes) == 0 &&
5492 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005493 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005494 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005495 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005496 /* allow reshape to continue */
5497 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005498 break;
5499
5500 case 0: /* re-enable writes */
5501 spin_lock_irq(&conf->device_lock);
5502 conf->quiesce = 0;
5503 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005504 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005505 spin_unlock_irq(&conf->device_lock);
5506 break;
5507 }
NeilBrown72626682005-09-09 16:23:54 -07005508}
NeilBrownb15c2e52006-01-06 00:20:16 -08005509
NeilBrownd562b0c2009-03-31 14:39:39 +11005510
5511static void *raid5_takeover_raid1(mddev_t *mddev)
5512{
5513 int chunksect;
5514
5515 if (mddev->raid_disks != 2 ||
5516 mddev->degraded > 1)
5517 return ERR_PTR(-EINVAL);
5518
5519 /* Should check if there are write-behind devices? */
5520
5521 chunksect = 64*2; /* 64K by default */
5522
5523 /* The array must be an exact multiple of chunksize */
5524 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5525 chunksect >>= 1;
5526
5527 if ((chunksect<<9) < STRIPE_SIZE)
5528 /* array size does not allow a suitable chunk size */
5529 return ERR_PTR(-EINVAL);
5530
5531 mddev->new_level = 5;
5532 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005533 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005534
5535 return setup_conf(mddev);
5536}
5537
NeilBrownfc9739c2009-03-31 14:57:20 +11005538static void *raid5_takeover_raid6(mddev_t *mddev)
5539{
5540 int new_layout;
5541
5542 switch (mddev->layout) {
5543 case ALGORITHM_LEFT_ASYMMETRIC_6:
5544 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5545 break;
5546 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5547 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5548 break;
5549 case ALGORITHM_LEFT_SYMMETRIC_6:
5550 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5551 break;
5552 case ALGORITHM_RIGHT_SYMMETRIC_6:
5553 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5554 break;
5555 case ALGORITHM_PARITY_0_6:
5556 new_layout = ALGORITHM_PARITY_0;
5557 break;
5558 case ALGORITHM_PARITY_N:
5559 new_layout = ALGORITHM_PARITY_N;
5560 break;
5561 default:
5562 return ERR_PTR(-EINVAL);
5563 }
5564 mddev->new_level = 5;
5565 mddev->new_layout = new_layout;
5566 mddev->delta_disks = -1;
5567 mddev->raid_disks -= 1;
5568 return setup_conf(mddev);
5569}
5570
NeilBrownd562b0c2009-03-31 14:39:39 +11005571
NeilBrown50ac1682009-06-18 08:47:55 +10005572static int raid5_check_reshape(mddev_t *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005573{
NeilBrown88ce4932009-03-31 15:24:23 +11005574 /* For a 2-drive array, the layout and chunk size can be changed
5575 * immediately as not restriping is needed.
5576 * For larger arrays we record the new value - after validation
5577 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005578 */
NeilBrown070ec552009-06-16 16:54:21 +10005579 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005580 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005581
NeilBrown597a7112009-06-18 08:47:42 +10005582 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005583 return -EINVAL;
5584 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005585 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005586 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005587 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005588 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005589 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005590 /* not factor of array size */
5591 return -EINVAL;
5592 }
5593
5594 /* They look valid */
5595
NeilBrown88ce4932009-03-31 15:24:23 +11005596 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005597 /* can make the change immediately */
5598 if (mddev->new_layout >= 0) {
5599 conf->algorithm = mddev->new_layout;
5600 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005601 }
5602 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005603 conf->chunk_sectors = new_chunk ;
5604 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005605 }
5606 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5607 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005608 }
NeilBrown50ac1682009-06-18 08:47:55 +10005609 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005610}
5611
NeilBrown50ac1682009-06-18 08:47:55 +10005612static int raid6_check_reshape(mddev_t *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005613{
NeilBrown597a7112009-06-18 08:47:42 +10005614 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005615
NeilBrown597a7112009-06-18 08:47:42 +10005616 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005617 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005618 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005619 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005620 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005621 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005622 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005623 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005624 /* not factor of array size */
5625 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005626 }
NeilBrown88ce4932009-03-31 15:24:23 +11005627
5628 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005629 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005630}
5631
NeilBrownd562b0c2009-03-31 14:39:39 +11005632static void *raid5_takeover(mddev_t *mddev)
5633{
5634 /* raid5 can take over:
5635 * raid0 - if all devices are the same - make it a raid4 layout
5636 * raid1 - if there are two drives. We need to know the chunk size
5637 * raid4 - trivial - just use a raid4 layout.
5638 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005639 */
5640
5641 if (mddev->level == 1)
5642 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005643 if (mddev->level == 4) {
5644 mddev->new_layout = ALGORITHM_PARITY_N;
5645 mddev->new_level = 5;
5646 return setup_conf(mddev);
5647 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005648 if (mddev->level == 6)
5649 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005650
5651 return ERR_PTR(-EINVAL);
5652}
5653
5654
NeilBrown245f46c2009-03-31 14:39:39 +11005655static struct mdk_personality raid5_personality;
5656
5657static void *raid6_takeover(mddev_t *mddev)
5658{
5659 /* Currently can only take over a raid5. We map the
5660 * personality to an equivalent raid6 personality
5661 * with the Q block at the end.
5662 */
5663 int new_layout;
5664
5665 if (mddev->pers != &raid5_personality)
5666 return ERR_PTR(-EINVAL);
5667 if (mddev->degraded > 1)
5668 return ERR_PTR(-EINVAL);
5669 if (mddev->raid_disks > 253)
5670 return ERR_PTR(-EINVAL);
5671 if (mddev->raid_disks < 3)
5672 return ERR_PTR(-EINVAL);
5673
5674 switch (mddev->layout) {
5675 case ALGORITHM_LEFT_ASYMMETRIC:
5676 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5677 break;
5678 case ALGORITHM_RIGHT_ASYMMETRIC:
5679 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5680 break;
5681 case ALGORITHM_LEFT_SYMMETRIC:
5682 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5683 break;
5684 case ALGORITHM_RIGHT_SYMMETRIC:
5685 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5686 break;
5687 case ALGORITHM_PARITY_0:
5688 new_layout = ALGORITHM_PARITY_0_6;
5689 break;
5690 case ALGORITHM_PARITY_N:
5691 new_layout = ALGORITHM_PARITY_N;
5692 break;
5693 default:
5694 return ERR_PTR(-EINVAL);
5695 }
5696 mddev->new_level = 6;
5697 mddev->new_layout = new_layout;
5698 mddev->delta_disks = 1;
5699 mddev->raid_disks += 1;
5700 return setup_conf(mddev);
5701}
5702
5703
NeilBrown16a53ec2006-06-26 00:27:38 -07005704static struct mdk_personality raid6_personality =
5705{
5706 .name = "raid6",
5707 .level = 6,
5708 .owner = THIS_MODULE,
5709 .make_request = make_request,
5710 .run = run,
5711 .stop = stop,
5712 .status = status,
5713 .error_handler = error,
5714 .hot_add_disk = raid5_add_disk,
5715 .hot_remove_disk= raid5_remove_disk,
5716 .spare_active = raid5_spare_active,
5717 .sync_request = sync_request,
5718 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005719 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005720 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005721 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005722 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005723 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005724 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005725};
NeilBrown2604b702006-01-06 00:20:36 -08005726static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727{
5728 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005729 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 .owner = THIS_MODULE,
5731 .make_request = make_request,
5732 .run = run,
5733 .stop = stop,
5734 .status = status,
5735 .error_handler = error,
5736 .hot_add_disk = raid5_add_disk,
5737 .hot_remove_disk= raid5_remove_disk,
5738 .spare_active = raid5_spare_active,
5739 .sync_request = sync_request,
5740 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005741 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005742 .check_reshape = raid5_check_reshape,
5743 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005744 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005745 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005746 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747};
5748
NeilBrown2604b702006-01-06 00:20:36 -08005749static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750{
NeilBrown2604b702006-01-06 00:20:36 -08005751 .name = "raid4",
5752 .level = 4,
5753 .owner = THIS_MODULE,
5754 .make_request = make_request,
5755 .run = run,
5756 .stop = stop,
5757 .status = status,
5758 .error_handler = error,
5759 .hot_add_disk = raid5_add_disk,
5760 .hot_remove_disk= raid5_remove_disk,
5761 .spare_active = raid5_spare_active,
5762 .sync_request = sync_request,
5763 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005764 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005765 .check_reshape = raid5_check_reshape,
5766 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005767 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005768 .quiesce = raid5_quiesce,
5769};
5770
5771static int __init raid5_init(void)
5772{
NeilBrown16a53ec2006-06-26 00:27:38 -07005773 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005774 register_md_personality(&raid5_personality);
5775 register_md_personality(&raid4_personality);
5776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
5778
NeilBrown2604b702006-01-06 00:20:36 -08005779static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780{
NeilBrown16a53ec2006-06-26 00:27:38 -07005781 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005782 unregister_md_personality(&raid5_personality);
5783 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784}
5785
5786module_init(raid5_init);
5787module_exit(raid5_exit);
5788MODULE_LICENSE("GPL");
5789MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005790MODULE_ALIAS("md-raid5");
5791MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005792MODULE_ALIAS("md-level-5");
5793MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005794MODULE_ALIAS("md-personality-8"); /* RAID6 */
5795MODULE_ALIAS("md-raid6");
5796MODULE_ALIAS("md-level-6");
5797
5798/* This used to be two separate modules, they were: */
5799MODULE_ALIAS("raid5");
5800MODULE_ALIAS("raid6");