blob: 25c3c29134d15fdefbd77b9bc399d2e1b0c83963 [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
NeilBrownd0dabf72009-03-31 14:39:38 +1100161 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100162 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100163 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100164 return syndrome_disks + 1;
NeilBrownd0dabf72009-03-31 14:39:38 +1100165 slot = (*count)++;
166 return slot;
167}
168
Dan Williamsa4456852007-07-09 11:56:43 -0700169static void return_io(struct bio *return_bi)
170{
171 struct bio *bi = return_bi;
172 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700173
174 return_bi = bi->bi_next;
175 bi->bi_next = NULL;
176 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000177 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700178 bi = return_bi;
179 }
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void print_raid5_conf (raid5_conf_t *conf);
183
Dan Williams600aa102008-06-28 08:32:05 +1000184static int stripe_operations_active(struct stripe_head *sh)
185{
186 return sh->check_state || sh->reconstruct_state ||
187 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
188 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
189}
190
Arjan van de Ven858119e2006-01-14 13:20:43 -0800191static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200194 BUG_ON(!list_empty(&sh->lru));
195 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700197 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700199 blk_plug_device(conf->mddev->queue);
200 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700201 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700202 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700203 blk_plug_device(conf->mddev->queue);
204 } else {
NeilBrown72626682005-09-09 16:23:54 -0700205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(conf->mddev->thread);
209 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000210 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225}
NeilBrownd0dabf72009-03-31 14:39:38 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void release_stripe(struct stripe_head *sh)
228{
229 raid5_conf_t *conf = sh->raid_conf;
230 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
235}
236
NeilBrownfccddba2006-01-06 00:20:33 -0800237static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dan Williams45b42332007-07-09 11:56:43 -0700239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrownfccddba2006-01-06 00:20:33 -0800242 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrown16a53ec2006-06-26 00:27:38 -0700245static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrownfccddba2006-01-06 00:20:33 -0800247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Williams45b42332007-07-09 11:56:43 -0700249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800253 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256
257/* find an idle stripe, make sure it is unhashed, and return it. */
258static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
259{
260 struct stripe_head *sh = NULL;
261 struct list_head *first;
262
263 CHECK_DEVLOCK();
264 if (list_empty(&conf->inactive_list))
265 goto out;
266 first = conf->inactive_list.next;
267 sh = list_entry(first, struct stripe_head, lru);
268 list_del_init(first);
269 remove_hash(sh);
270 atomic_inc(&conf->active_stripes);
271out:
272 return sh;
273}
274
275static void shrink_buffers(struct stripe_head *sh, int num)
276{
277 struct page *p;
278 int i;
279
280 for (i=0; i<num ; i++) {
281 p = sh->dev[i].page;
282 if (!p)
283 continue;
284 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800285 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287}
288
289static int grow_buffers(struct stripe_head *sh, int num)
290{
291 int i;
292
293 for (i=0; i<num; i++) {
294 struct page *page;
295
296 if (!(page = alloc_page(GFP_KERNEL))) {
297 return 1;
298 }
299 sh->dev[i].page = page;
300 }
301 return 0;
302}
303
NeilBrown784052e2009-03-31 15:19:07 +1100304static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrown911d4ee2009-03-31 14:39:38 +1100305static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
306 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownb5663ba2009-03-31 14:39:38 +1100308static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800311 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000315 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700318 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 (unsigned long long)sh->sector);
320
321 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700322
NeilBrown86b42c72009-03-31 15:19:03 +1100323 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100324 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100326 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sh->state = 0;
328
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800329
330 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct r5dev *dev = &sh->dev[i];
332
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700337 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 test_bit(R5_LOCKED, &dev->flags));
339 BUG();
340 }
341 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100342 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 insert_hash(conf, sh);
345}
346
NeilBrown86b42c72009-03-31 15:19:03 +1100347static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
348 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800351 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700354 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800355 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100356 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700358 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return NULL;
360}
361
362static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200363static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
NeilBrownb5663ba2009-03-31 14:39:38 +1100365static struct stripe_head *
366get_active_stripe(raid5_conf_t *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000367 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct stripe_head *sh;
370
Dan Williams45b42332007-07-09 11:56:43 -0700371 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 spin_lock_irq(&conf->device_lock);
374
375 do {
NeilBrown72626682005-09-09 16:23:54 -0700376 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000377 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700378 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100379 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!sh) {
381 if (!conf->inactive_blocked)
382 sh = get_free_stripe(conf);
383 if (noblock && sh == NULL)
384 break;
385 if (!sh) {
386 conf->inactive_blocked = 1;
387 wait_event_lock_irq(conf->wait_for_stripe,
388 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800389 (atomic_read(&conf->active_stripes)
390 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 || !conf->inactive_blocked),
392 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700393 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 );
395 conf->inactive_blocked = 0;
396 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100397 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else {
399 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100400 BUG_ON(!list_empty(&sh->lru)
401 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 } else {
403 if (!test_bit(STRIPE_HANDLE, &sh->state))
404 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700405 if (list_empty(&sh->lru) &&
406 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700407 BUG();
408 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 }
411 } while (sh == NULL);
412
413 if (sh)
414 atomic_inc(&sh->count);
415
416 spin_unlock_irq(&conf->device_lock);
417 return sh;
418}
419
NeilBrown6712ecf2007-09-27 12:47:43 +0200420static void
421raid5_end_read_request(struct bio *bi, int error);
422static void
423raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700424
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000425static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700426{
427 raid5_conf_t *conf = sh->raid_conf;
428 int i, disks = sh->disks;
429
430 might_sleep();
431
432 for (i = disks; i--; ) {
433 int rw;
434 struct bio *bi;
435 mdk_rdev_t *rdev;
436 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
437 rw = WRITE;
438 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
439 rw = READ;
440 else
441 continue;
442
443 bi = &sh->dev[i].req;
444
445 bi->bi_rw = rw;
446 if (rw == WRITE)
447 bi->bi_end_io = raid5_end_write_request;
448 else
449 bi->bi_end_io = raid5_end_read_request;
450
451 rcu_read_lock();
452 rdev = rcu_dereference(conf->disks[i].rdev);
453 if (rdev && test_bit(Faulty, &rdev->flags))
454 rdev = NULL;
455 if (rdev)
456 atomic_inc(&rdev->nr_pending);
457 rcu_read_unlock();
458
459 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000460 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700461 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
462
Dan Williams2b7497f2008-06-28 08:31:52 +1000463 set_bit(STRIPE_IO_STARTED, &sh->state);
464
Dan Williams91c00922007-01-02 13:52:30 -0700465 bi->bi_bdev = rdev->bdev;
466 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700467 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700468 bi->bi_rw, i);
469 atomic_inc(&sh->count);
470 bi->bi_sector = sh->sector + rdev->data_offset;
471 bi->bi_flags = 1 << BIO_UPTODATE;
472 bi->bi_vcnt = 1;
473 bi->bi_max_vecs = 1;
474 bi->bi_idx = 0;
475 bi->bi_io_vec = &sh->dev[i].vec;
476 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
477 bi->bi_io_vec[0].bv_offset = 0;
478 bi->bi_size = STRIPE_SIZE;
479 bi->bi_next = NULL;
480 if (rw == WRITE &&
481 test_bit(R5_ReWrite, &sh->dev[i].flags))
482 atomic_add(STRIPE_SECTORS,
483 &rdev->corrected_errors);
484 generic_make_request(bi);
485 } else {
486 if (rw == WRITE)
487 set_bit(STRIPE_DEGRADED, &sh->state);
488 pr_debug("skip op %ld on disc %d for sector %llu\n",
489 bi->bi_rw, i, (unsigned long long)sh->sector);
490 clear_bit(R5_LOCKED, &sh->dev[i].flags);
491 set_bit(STRIPE_HANDLE, &sh->state);
492 }
493 }
494}
495
496static struct dma_async_tx_descriptor *
497async_copy_data(int frombio, struct bio *bio, struct page *page,
498 sector_t sector, struct dma_async_tx_descriptor *tx)
499{
500 struct bio_vec *bvl;
501 struct page *bio_page;
502 int i;
503 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700504 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700505 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700506
507 if (bio->bi_sector >= sector)
508 page_offset = (signed)(bio->bi_sector - sector) * 512;
509 else
510 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700511
Dan Williams0403e382009-09-08 17:42:50 -0700512 if (frombio)
513 flags |= ASYNC_TX_FENCE;
514 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
515
Dan Williams91c00922007-01-02 13:52:30 -0700516 bio_for_each_segment(bvl, bio, i) {
517 int len = bio_iovec_idx(bio, i)->bv_len;
518 int clen;
519 int b_offset = 0;
520
521 if (page_offset < 0) {
522 b_offset = -page_offset;
523 page_offset += b_offset;
524 len -= b_offset;
525 }
526
527 if (len > 0 && page_offset + len > STRIPE_SIZE)
528 clen = STRIPE_SIZE - page_offset;
529 else
530 clen = len;
531
532 if (clen > 0) {
533 b_offset += bio_iovec_idx(bio, i)->bv_offset;
534 bio_page = bio_iovec_idx(bio, i)->bv_page;
535 if (frombio)
536 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700537 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700538 else
539 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700540 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700541 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700542 /* chain the operations */
543 submit.depend_tx = tx;
544
Dan Williams91c00922007-01-02 13:52:30 -0700545 if (clen < len) /* hit end of page */
546 break;
547 page_offset += len;
548 }
549
550 return tx;
551}
552
553static void ops_complete_biofill(void *stripe_head_ref)
554{
555 struct stripe_head *sh = stripe_head_ref;
556 struct bio *return_bi = NULL;
557 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700558 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700559
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700560 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700561 (unsigned long long)sh->sector);
562
563 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000564 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700565 for (i = sh->disks; i--; ) {
566 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700567
568 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700569 /* and check if we need to reply to a read request,
570 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000571 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700572 */
573 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700574 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700575
Dan Williams91c00922007-01-02 13:52:30 -0700576 BUG_ON(!dev->read);
577 rbi = dev->read;
578 dev->read = NULL;
579 while (rbi && rbi->bi_sector <
580 dev->sector + STRIPE_SECTORS) {
581 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200582 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700583 rbi->bi_next = return_bi;
584 return_bi = rbi;
585 }
Dan Williams91c00922007-01-02 13:52:30 -0700586 rbi = rbi2;
587 }
588 }
589 }
Dan Williams83de75c2008-06-28 08:31:58 +1000590 spin_unlock_irq(&conf->device_lock);
591 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700592
593 return_io(return_bi);
594
Dan Williamse4d84902007-09-24 10:06:13 -0700595 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700596 release_stripe(sh);
597}
598
599static void ops_run_biofill(struct stripe_head *sh)
600{
601 struct dma_async_tx_descriptor *tx = NULL;
602 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700603 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700604 int i;
605
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700606 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700607 (unsigned long long)sh->sector);
608
609 for (i = sh->disks; i--; ) {
610 struct r5dev *dev = &sh->dev[i];
611 if (test_bit(R5_Wantfill, &dev->flags)) {
612 struct bio *rbi;
613 spin_lock_irq(&conf->device_lock);
614 dev->read = rbi = dev->toread;
615 dev->toread = NULL;
616 spin_unlock_irq(&conf->device_lock);
617 while (rbi && rbi->bi_sector <
618 dev->sector + STRIPE_SECTORS) {
619 tx = async_copy_data(0, rbi, dev->page,
620 dev->sector, tx);
621 rbi = r5_next_bio(rbi, dev->sector);
622 }
623 }
624 }
625
626 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700627 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
628 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700629}
630
Dan Williams4e7d2c02009-08-29 19:13:11 -0700631static void mark_target_uptodate(struct stripe_head *sh, int target)
632{
633 struct r5dev *tgt;
634
635 if (target < 0)
636 return;
637
638 tgt = &sh->dev[target];
639 set_bit(R5_UPTODATE, &tgt->flags);
640 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
641 clear_bit(R5_Wantcompute, &tgt->flags);
642}
643
Dan Williamsac6b53b2009-07-14 13:40:19 -0700644static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700645{
646 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700647
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700648 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700649 (unsigned long long)sh->sector);
650
Dan Williamsac6b53b2009-07-14 13:40:19 -0700651 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700652 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700653 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700654
Dan Williamsecc65c92008-06-28 08:31:57 +1000655 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
656 if (sh->check_state == check_state_compute_run)
657 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700658 set_bit(STRIPE_HANDLE, &sh->state);
659 release_stripe(sh);
660}
661
Dan Williamsd6f38f32009-07-14 11:50:52 -0700662/* return a pointer to the address conversion region of the scribble buffer */
663static addr_conv_t *to_addr_conv(struct stripe_head *sh,
664 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700665{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700666 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
667}
668
669static struct dma_async_tx_descriptor *
670ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
671{
Dan Williams91c00922007-01-02 13:52:30 -0700672 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700673 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700674 int target = sh->ops.target;
675 struct r5dev *tgt = &sh->dev[target];
676 struct page *xor_dest = tgt->page;
677 int count = 0;
678 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700679 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700680 int i;
681
682 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700683 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700684 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
685
686 for (i = disks; i--; )
687 if (i != target)
688 xor_srcs[count++] = sh->dev[i].page;
689
690 atomic_inc(&sh->count);
691
Dan Williams0403e382009-09-08 17:42:50 -0700692 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700693 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700694 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700695 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700696 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700697 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700698
Dan Williams91c00922007-01-02 13:52:30 -0700699 return tx;
700}
701
Dan Williamsac6b53b2009-07-14 13:40:19 -0700702/* set_syndrome_sources - populate source buffers for gen_syndrome
703 * @srcs - (struct page *) array of size sh->disks
704 * @sh - stripe_head to parse
705 *
706 * Populates srcs in proper layout order for the stripe and returns the
707 * 'count' of sources to be used in a call to async_gen_syndrome. The P
708 * destination buffer is recorded in srcs[count] and the Q destination
709 * is recorded in srcs[count+1]].
710 */
711static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
712{
713 int disks = sh->disks;
714 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
715 int d0_idx = raid6_d0(sh);
716 int count;
717 int i;
718
719 for (i = 0; i < disks; i++)
720 srcs[i] = (void *)raid6_empty_zero_page;
721
722 count = 0;
723 i = d0_idx;
724 do {
725 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
726
727 srcs[slot] = sh->dev[i].page;
728 i = raid6_next_disk(i, disks);
729 } while (i != d0_idx);
730 BUG_ON(count != syndrome_disks);
731
732 return count;
733}
734
735static struct dma_async_tx_descriptor *
736ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
737{
738 int disks = sh->disks;
739 struct page **blocks = percpu->scribble;
740 int target;
741 int qd_idx = sh->qd_idx;
742 struct dma_async_tx_descriptor *tx;
743 struct async_submit_ctl submit;
744 struct r5dev *tgt;
745 struct page *dest;
746 int i;
747 int count;
748
749 if (sh->ops.target < 0)
750 target = sh->ops.target2;
751 else if (sh->ops.target2 < 0)
752 target = sh->ops.target;
753 else
754 /* we should only have one valid target */
755 BUG();
756 BUG_ON(target < 0);
757 pr_debug("%s: stripe %llu block: %d\n",
758 __func__, (unsigned long long)sh->sector, target);
759
760 tgt = &sh->dev[target];
761 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
762 dest = tgt->page;
763
764 atomic_inc(&sh->count);
765
766 if (target == qd_idx) {
767 count = set_syndrome_sources(blocks, sh);
768 blocks[count] = NULL; /* regenerating p is not necessary */
769 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700770 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
771 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700772 to_addr_conv(sh, percpu));
773 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
774 } else {
775 /* Compute any data- or p-drive using XOR */
776 count = 0;
777 for (i = disks; i-- ; ) {
778 if (i == target || i == qd_idx)
779 continue;
780 blocks[count++] = sh->dev[i].page;
781 }
782
Dan Williams0403e382009-09-08 17:42:50 -0700783 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
784 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700785 to_addr_conv(sh, percpu));
786 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
787 }
788
789 return tx;
790}
791
792static struct dma_async_tx_descriptor *
793ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
794{
795 int i, count, disks = sh->disks;
796 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
797 int d0_idx = raid6_d0(sh);
798 int faila = -1, failb = -1;
799 int target = sh->ops.target;
800 int target2 = sh->ops.target2;
801 struct r5dev *tgt = &sh->dev[target];
802 struct r5dev *tgt2 = &sh->dev[target2];
803 struct dma_async_tx_descriptor *tx;
804 struct page **blocks = percpu->scribble;
805 struct async_submit_ctl submit;
806
807 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
808 __func__, (unsigned long long)sh->sector, target, target2);
809 BUG_ON(target < 0 || target2 < 0);
810 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
811 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
812
Dan Williams6c910a72009-09-16 12:24:54 -0700813 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700814 * slot number conversion for 'faila' and 'failb'
815 */
816 for (i = 0; i < disks ; i++)
817 blocks[i] = (void *)raid6_empty_zero_page;
818 count = 0;
819 i = d0_idx;
820 do {
821 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
822
823 blocks[slot] = sh->dev[i].page;
824
825 if (i == target)
826 faila = slot;
827 if (i == target2)
828 failb = slot;
829 i = raid6_next_disk(i, disks);
830 } while (i != d0_idx);
831 BUG_ON(count != syndrome_disks);
832
833 BUG_ON(faila == failb);
834 if (failb < faila)
835 swap(faila, failb);
836 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
837 __func__, (unsigned long long)sh->sector, faila, failb);
838
839 atomic_inc(&sh->count);
840
841 if (failb == syndrome_disks+1) {
842 /* Q disk is one of the missing disks */
843 if (faila == syndrome_disks) {
844 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700845 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
846 ops_complete_compute, sh,
847 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700848 return async_gen_syndrome(blocks, 0, count+2,
849 STRIPE_SIZE, &submit);
850 } else {
851 struct page *dest;
852 int data_target;
853 int qd_idx = sh->qd_idx;
854
855 /* Missing D+Q: recompute D from P, then recompute Q */
856 if (target == qd_idx)
857 data_target = target2;
858 else
859 data_target = target;
860
861 count = 0;
862 for (i = disks; i-- ; ) {
863 if (i == data_target || i == qd_idx)
864 continue;
865 blocks[count++] = sh->dev[i].page;
866 }
867 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700868 init_async_submit(&submit,
869 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
870 NULL, NULL, NULL,
871 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700872 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
873 &submit);
874
875 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700876 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
877 ops_complete_compute, sh,
878 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700879 return async_gen_syndrome(blocks, 0, count+2,
880 STRIPE_SIZE, &submit);
881 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700882 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700883 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
884 ops_complete_compute, sh,
885 to_addr_conv(sh, percpu));
886 if (failb == syndrome_disks) {
887 /* We're missing D+P. */
888 return async_raid6_datap_recov(syndrome_disks+2,
889 STRIPE_SIZE, faila,
890 blocks, &submit);
891 } else {
892 /* We're missing D+D. */
893 return async_raid6_2data_recov(syndrome_disks+2,
894 STRIPE_SIZE, faila, failb,
895 blocks, &submit);
896 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700897 }
898}
899
900
Dan Williams91c00922007-01-02 13:52:30 -0700901static void ops_complete_prexor(void *stripe_head_ref)
902{
903 struct stripe_head *sh = stripe_head_ref;
904
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700905 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700906 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700907}
908
909static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700910ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
911 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700912{
Dan Williams91c00922007-01-02 13:52:30 -0700913 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700914 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700915 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700916 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700917
918 /* existing parity data subtracted */
919 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
920
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700921 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700922 (unsigned long long)sh->sector);
923
924 for (i = disks; i--; ) {
925 struct r5dev *dev = &sh->dev[i];
926 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000927 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700928 xor_srcs[count++] = dev->page;
929 }
930
Dan Williams0403e382009-09-08 17:42:50 -0700931 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700932 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700933 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700934
935 return tx;
936}
937
938static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000939ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700940{
941 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000942 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700943
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700944 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700945 (unsigned long long)sh->sector);
946
947 for (i = disks; i--; ) {
948 struct r5dev *dev = &sh->dev[i];
949 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700950
Dan Williamsd8ee0722008-06-28 08:32:06 +1000951 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700952 struct bio *wbi;
953
954 spin_lock(&sh->lock);
955 chosen = dev->towrite;
956 dev->towrite = NULL;
957 BUG_ON(dev->written);
958 wbi = dev->written = chosen;
959 spin_unlock(&sh->lock);
960
961 while (wbi && wbi->bi_sector <
962 dev->sector + STRIPE_SECTORS) {
963 tx = async_copy_data(1, wbi, dev->page,
964 dev->sector, tx);
965 wbi = r5_next_bio(wbi, dev->sector);
966 }
967 }
968 }
969
970 return tx;
971}
972
Dan Williamsac6b53b2009-07-14 13:40:19 -0700973static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700974{
975 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700976 int disks = sh->disks;
977 int pd_idx = sh->pd_idx;
978 int qd_idx = sh->qd_idx;
979 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700980
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700981 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700982 (unsigned long long)sh->sector);
983
984 for (i = disks; i--; ) {
985 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -0700986
987 if (dev->written || i == pd_idx || i == qd_idx)
Dan Williams91c00922007-01-02 13:52:30 -0700988 set_bit(R5_UPTODATE, &dev->flags);
989 }
990
Dan Williamsd8ee0722008-06-28 08:32:06 +1000991 if (sh->reconstruct_state == reconstruct_state_drain_run)
992 sh->reconstruct_state = reconstruct_state_drain_result;
993 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
994 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
995 else {
996 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
997 sh->reconstruct_state = reconstruct_state_result;
998 }
Dan Williams91c00922007-01-02 13:52:30 -0700999
1000 set_bit(STRIPE_HANDLE, &sh->state);
1001 release_stripe(sh);
1002}
1003
1004static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001005ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1006 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001007{
Dan Williams91c00922007-01-02 13:52:30 -07001008 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001009 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001010 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001011 int count = 0, pd_idx = sh->pd_idx, i;
1012 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001013 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001014 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001015
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001016 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001017 (unsigned long long)sh->sector);
1018
1019 /* check if prexor is active which means only process blocks
1020 * that are part of a read-modify-write (written)
1021 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001022 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1023 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001024 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1025 for (i = disks; i--; ) {
1026 struct r5dev *dev = &sh->dev[i];
1027 if (dev->written)
1028 xor_srcs[count++] = dev->page;
1029 }
1030 } else {
1031 xor_dest = sh->dev[pd_idx].page;
1032 for (i = disks; i--; ) {
1033 struct r5dev *dev = &sh->dev[i];
1034 if (i != pd_idx)
1035 xor_srcs[count++] = dev->page;
1036 }
1037 }
1038
Dan Williams91c00922007-01-02 13:52:30 -07001039 /* 1/ if we prexor'd then the dest is reused as a source
1040 * 2/ if we did not prexor then we are redoing the parity
1041 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1042 * for the synchronous xor case
1043 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001044 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001045 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1046
1047 atomic_inc(&sh->count);
1048
Dan Williamsac6b53b2009-07-14 13:40:19 -07001049 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001050 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001051 if (unlikely(count == 1))
1052 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1053 else
1054 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001055}
1056
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057static void
1058ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1059 struct dma_async_tx_descriptor *tx)
1060{
1061 struct async_submit_ctl submit;
1062 struct page **blocks = percpu->scribble;
1063 int count;
1064
1065 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1066
1067 count = set_syndrome_sources(blocks, sh);
1068
1069 atomic_inc(&sh->count);
1070
1071 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1072 sh, to_addr_conv(sh, percpu));
1073 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001074}
1075
1076static void ops_complete_check(void *stripe_head_ref)
1077{
1078 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001079
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001080 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001081 (unsigned long long)sh->sector);
1082
Dan Williamsecc65c92008-06-28 08:31:57 +10001083 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001084 set_bit(STRIPE_HANDLE, &sh->state);
1085 release_stripe(sh);
1086}
1087
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001089{
Dan Williams91c00922007-01-02 13:52:30 -07001090 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001091 int pd_idx = sh->pd_idx;
1092 int qd_idx = sh->qd_idx;
1093 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001094 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001095 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001096 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001097 int count;
1098 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001099
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001100 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001101 (unsigned long long)sh->sector);
1102
Dan Williamsac6b53b2009-07-14 13:40:19 -07001103 count = 0;
1104 xor_dest = sh->dev[pd_idx].page;
1105 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001106 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001107 if (i == pd_idx || i == qd_idx)
1108 continue;
1109 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001110 }
1111
Dan Williamsd6f38f32009-07-14 11:50:52 -07001112 init_async_submit(&submit, 0, NULL, NULL, NULL,
1113 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001114 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001115 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001116
Dan Williams91c00922007-01-02 13:52:30 -07001117 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001118 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1119 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001120}
1121
Dan Williamsac6b53b2009-07-14 13:40:19 -07001122static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1123{
1124 struct page **srcs = percpu->scribble;
1125 struct async_submit_ctl submit;
1126 int count;
1127
1128 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1129 (unsigned long long)sh->sector, checkp);
1130
1131 count = set_syndrome_sources(srcs, sh);
1132 if (!checkp)
1133 srcs[count] = NULL;
1134
1135 atomic_inc(&sh->count);
1136 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1137 sh, to_addr_conv(sh, percpu));
1138 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1139 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1140}
1141
Dan Williams417b8d42009-10-16 16:25:22 +11001142static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001143{
1144 int overlap_clear = 0, i, disks = sh->disks;
1145 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001146 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001147 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001148 struct raid5_percpu *percpu;
1149 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001150
Dan Williamsd6f38f32009-07-14 11:50:52 -07001151 cpu = get_cpu();
1152 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001153 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001154 ops_run_biofill(sh);
1155 overlap_clear++;
1156 }
1157
Dan Williams7b3a8712008-06-28 08:32:09 +10001158 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001159 if (level < 6)
1160 tx = ops_run_compute5(sh, percpu);
1161 else {
1162 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1163 tx = ops_run_compute6_1(sh, percpu);
1164 else
1165 tx = ops_run_compute6_2(sh, percpu);
1166 }
1167 /* terminate the chain if reconstruct is not set to be run */
1168 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001169 async_tx_ack(tx);
1170 }
Dan Williams91c00922007-01-02 13:52:30 -07001171
Dan Williams600aa102008-06-28 08:32:05 +10001172 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001173 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001174
Dan Williams600aa102008-06-28 08:32:05 +10001175 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001176 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001177 overlap_clear++;
1178 }
1179
Dan Williamsac6b53b2009-07-14 13:40:19 -07001180 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1181 if (level < 6)
1182 ops_run_reconstruct5(sh, percpu, tx);
1183 else
1184 ops_run_reconstruct6(sh, percpu, tx);
1185 }
Dan Williams91c00922007-01-02 13:52:30 -07001186
Dan Williamsac6b53b2009-07-14 13:40:19 -07001187 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1188 if (sh->check_state == check_state_run)
1189 ops_run_check_p(sh, percpu);
1190 else if (sh->check_state == check_state_run_q)
1191 ops_run_check_pq(sh, percpu, 0);
1192 else if (sh->check_state == check_state_run_pq)
1193 ops_run_check_pq(sh, percpu, 1);
1194 else
1195 BUG();
1196 }
Dan Williams91c00922007-01-02 13:52:30 -07001197
Dan Williams91c00922007-01-02 13:52:30 -07001198 if (overlap_clear)
1199 for (i = disks; i--; ) {
1200 struct r5dev *dev = &sh->dev[i];
1201 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1202 wake_up(&sh->raid_conf->wait_for_overlap);
1203 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001204 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001205}
1206
Dan Williams417b8d42009-10-16 16:25:22 +11001207#ifdef CONFIG_MULTICORE_RAID456
1208static void async_run_ops(void *param, async_cookie_t cookie)
1209{
1210 struct stripe_head *sh = param;
1211 unsigned long ops_request = sh->ops.request;
1212
1213 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1214 wake_up(&sh->ops.wait_for_ops);
1215
1216 __raid_run_ops(sh, ops_request);
1217 release_stripe(sh);
1218}
1219
1220static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1221{
1222 /* since handle_stripe can be called outside of raid5d context
1223 * we need to ensure sh->ops.request is de-staged before another
1224 * request arrives
1225 */
1226 wait_event(sh->ops.wait_for_ops,
1227 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1228 sh->ops.request = ops_request;
1229
1230 atomic_inc(&sh->count);
1231 async_schedule(async_run_ops, sh);
1232}
1233#else
1234#define raid_run_ops __raid_run_ops
1235#endif
1236
NeilBrown3f294f42005-11-08 21:39:25 -08001237static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -08001240 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1241 if (!sh)
1242 return 0;
1243 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
1244 sh->raid_conf = conf;
1245 spin_lock_init(&sh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001246 #ifdef CONFIG_MULTICORE_RAID456
1247 init_waitqueue_head(&sh->ops.wait_for_ops);
1248 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001249
1250 if (grow_buffers(sh, conf->raid_disks)) {
1251 shrink_buffers(sh, conf->raid_disks);
1252 kmem_cache_free(conf->slab_cache, sh);
1253 return 0;
1254 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001255 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -08001256 /* we just created an active stripe so... */
1257 atomic_set(&sh->count, 1);
1258 atomic_inc(&conf->active_stripes);
1259 INIT_LIST_HEAD(&sh->lru);
1260 release_stripe(sh);
1261 return 1;
1262}
1263
1264static int grow_stripes(raid5_conf_t *conf, int num)
1265{
Christoph Lametere18b8902006-12-06 20:33:20 -08001266 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int devs = conf->raid_disks;
1268
NeilBrown245f46c2009-03-31 14:39:39 +11001269 sprintf(conf->cache_name[0],
1270 "raid%d-%s", conf->level, mdname(conf->mddev));
1271 sprintf(conf->cache_name[1],
1272 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -08001273 conf->active_name = 0;
1274 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001276 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (!sc)
1278 return 1;
1279 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001280 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001281 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001282 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
NeilBrown29269552006-03-27 01:18:10 -08001286
Dan Williamsd6f38f32009-07-14 11:50:52 -07001287/**
1288 * scribble_len - return the required size of the scribble region
1289 * @num - total number of disks in the array
1290 *
1291 * The size must be enough to contain:
1292 * 1/ a struct page pointer for each device in the array +2
1293 * 2/ room to convert each entry in (1) to its corresponding dma
1294 * (dma_map_page()) or page (page_address()) address.
1295 *
1296 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1297 * calculate over all devices (not just the data blocks), using zeros in place
1298 * of the P and Q blocks.
1299 */
1300static size_t scribble_len(int num)
1301{
1302 size_t len;
1303
1304 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1305
1306 return len;
1307}
1308
NeilBrownad01c9e2006-03-27 01:18:07 -08001309static int resize_stripes(raid5_conf_t *conf, int newsize)
1310{
1311 /* Make all the stripes able to hold 'newsize' devices.
1312 * New slots in each stripe get 'page' set to a new page.
1313 *
1314 * This happens in stages:
1315 * 1/ create a new kmem_cache and allocate the required number of
1316 * stripe_heads.
1317 * 2/ gather all the old stripe_heads and tranfer the pages across
1318 * to the new stripe_heads. This will have the side effect of
1319 * freezing the array as once all stripe_heads have been collected,
1320 * no IO will be possible. Old stripe heads are freed once their
1321 * pages have been transferred over, and the old kmem_cache is
1322 * freed when all stripes are done.
1323 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1324 * we simple return a failre status - no need to clean anything up.
1325 * 4/ allocate new pages for the new slots in the new stripe_heads.
1326 * If this fails, we don't bother trying the shrink the
1327 * stripe_heads down again, we just leave them as they are.
1328 * As each stripe_head is processed the new one is released into
1329 * active service.
1330 *
1331 * Once step2 is started, we cannot afford to wait for a write,
1332 * so we use GFP_NOIO allocations.
1333 */
1334 struct stripe_head *osh, *nsh;
1335 LIST_HEAD(newstripes);
1336 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001337 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001338 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001339 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001340 int i;
1341
1342 if (newsize <= conf->pool_size)
1343 return 0; /* never bother to shrink */
1344
Dan Williamsb5470dc2008-06-27 21:44:04 -07001345 err = md_allow_write(conf->mddev);
1346 if (err)
1347 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001348
NeilBrownad01c9e2006-03-27 01:18:07 -08001349 /* Step 1 */
1350 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1351 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001352 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001353 if (!sc)
1354 return -ENOMEM;
1355
1356 for (i = conf->max_nr_stripes; i; i--) {
1357 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1358 if (!nsh)
1359 break;
1360
1361 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1362
1363 nsh->raid_conf = conf;
1364 spin_lock_init(&nsh->lock);
Dan Williams417b8d42009-10-16 16:25:22 +11001365 #ifdef CONFIG_MULTICORE_RAID456
1366 init_waitqueue_head(&nsh->ops.wait_for_ops);
1367 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001368
1369 list_add(&nsh->lru, &newstripes);
1370 }
1371 if (i) {
1372 /* didn't get enough, give up */
1373 while (!list_empty(&newstripes)) {
1374 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1375 list_del(&nsh->lru);
1376 kmem_cache_free(sc, nsh);
1377 }
1378 kmem_cache_destroy(sc);
1379 return -ENOMEM;
1380 }
1381 /* Step 2 - Must use GFP_NOIO now.
1382 * OK, we have enough stripes, start collecting inactive
1383 * stripes and copying them over
1384 */
1385 list_for_each_entry(nsh, &newstripes, lru) {
1386 spin_lock_irq(&conf->device_lock);
1387 wait_event_lock_irq(conf->wait_for_stripe,
1388 !list_empty(&conf->inactive_list),
1389 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001390 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001391 );
1392 osh = get_free_stripe(conf);
1393 spin_unlock_irq(&conf->device_lock);
1394 atomic_set(&nsh->count, 1);
1395 for(i=0; i<conf->pool_size; i++)
1396 nsh->dev[i].page = osh->dev[i].page;
1397 for( ; i<newsize; i++)
1398 nsh->dev[i].page = NULL;
1399 kmem_cache_free(conf->slab_cache, osh);
1400 }
1401 kmem_cache_destroy(conf->slab_cache);
1402
1403 /* Step 3.
1404 * At this point, we are holding all the stripes so the array
1405 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001406 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001407 */
1408 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1409 if (ndisks) {
1410 for (i=0; i<conf->raid_disks; i++)
1411 ndisks[i] = conf->disks[i];
1412 kfree(conf->disks);
1413 conf->disks = ndisks;
1414 } else
1415 err = -ENOMEM;
1416
Dan Williamsd6f38f32009-07-14 11:50:52 -07001417 get_online_cpus();
1418 conf->scribble_len = scribble_len(newsize);
1419 for_each_present_cpu(cpu) {
1420 struct raid5_percpu *percpu;
1421 void *scribble;
1422
1423 percpu = per_cpu_ptr(conf->percpu, cpu);
1424 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1425
1426 if (scribble) {
1427 kfree(percpu->scribble);
1428 percpu->scribble = scribble;
1429 } else {
1430 err = -ENOMEM;
1431 break;
1432 }
1433 }
1434 put_online_cpus();
1435
NeilBrownad01c9e2006-03-27 01:18:07 -08001436 /* Step 4, return new stripes to service */
1437 while(!list_empty(&newstripes)) {
1438 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1439 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001440
NeilBrownad01c9e2006-03-27 01:18:07 -08001441 for (i=conf->raid_disks; i < newsize; i++)
1442 if (nsh->dev[i].page == NULL) {
1443 struct page *p = alloc_page(GFP_NOIO);
1444 nsh->dev[i].page = p;
1445 if (!p)
1446 err = -ENOMEM;
1447 }
1448 release_stripe(nsh);
1449 }
1450 /* critical section pass, GFP_NOIO no longer needed */
1451
1452 conf->slab_cache = sc;
1453 conf->active_name = 1-conf->active_name;
1454 conf->pool_size = newsize;
1455 return err;
1456}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
NeilBrown3f294f42005-11-08 21:39:25 -08001458static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct stripe_head *sh;
1461
NeilBrown3f294f42005-11-08 21:39:25 -08001462 spin_lock_irq(&conf->device_lock);
1463 sh = get_free_stripe(conf);
1464 spin_unlock_irq(&conf->device_lock);
1465 if (!sh)
1466 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001467 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001468 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001469 kmem_cache_free(conf->slab_cache, sh);
1470 atomic_dec(&conf->active_stripes);
1471 return 1;
1472}
1473
1474static void shrink_stripes(raid5_conf_t *conf)
1475{
1476 while (drop_one_stripe(conf))
1477 ;
1478
NeilBrown29fc7e32006-02-03 03:03:41 -08001479 if (conf->slab_cache)
1480 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 conf->slab_cache = NULL;
1482}
1483
NeilBrown6712ecf2007-09-27 12:47:43 +02001484static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
NeilBrown99c0fb52009-03-31 14:39:38 +11001486 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001488 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001490 char b[BDEVNAME_SIZE];
1491 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 for (i=0 ; i<disks; i++)
1495 if (bi == &sh->dev[i].req)
1496 break;
1497
Dan Williams45b42332007-07-09 11:56:43 -07001498 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1499 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 uptodate);
1501 if (i == disks) {
1502 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505
1506 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001508 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001509 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001510 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1511 " (%lu sectors at %llu on %s)\n",
1512 mdname(conf->mddev), STRIPE_SECTORS,
1513 (unsigned long long)(sh->sector
1514 + rdev->data_offset),
1515 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001516 clear_bit(R5_ReadError, &sh->dev[i].flags);
1517 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1518 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001519 if (atomic_read(&conf->disks[i].rdev->read_errors))
1520 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001522 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001523 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001524 rdev = conf->disks[i].rdev;
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001527 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001528 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001529 printk_rl(KERN_WARNING
1530 "raid5:%s: read error not correctable "
1531 "(sector %llu on %s).\n",
1532 mdname(conf->mddev),
1533 (unsigned long long)(sh->sector
1534 + rdev->data_offset),
1535 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001536 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001537 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001538 printk_rl(KERN_WARNING
1539 "raid5:%s: read error NOT corrected!! "
1540 "(sector %llu on %s).\n",
1541 mdname(conf->mddev),
1542 (unsigned long long)(sh->sector
1543 + rdev->data_offset),
1544 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001545 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001546 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001547 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001548 "raid5:%s: Too many read errors, failing device %s.\n",
1549 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001550 else
1551 retry = 1;
1552 if (retry)
1553 set_bit(R5_ReadError, &sh->dev[i].flags);
1554 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001555 clear_bit(R5_ReadError, &sh->dev[i].flags);
1556 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001557 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1562 set_bit(STRIPE_HANDLE, &sh->state);
1563 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
NeilBrownd710e132008-10-13 11:55:12 +11001566static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
NeilBrown99c0fb52009-03-31 14:39:38 +11001568 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001570 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 for (i=0 ; i<disks; i++)
1574 if (bi == &sh->dev[i].req)
1575 break;
1576
Dan Williams45b42332007-07-09 11:56:43 -07001577 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1579 uptodate);
1580 if (i == disks) {
1581 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001582 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (!uptodate)
1586 md_error(conf->mddev, conf->disks[i].rdev);
1587
1588 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1589
1590 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1591 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001592 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595
NeilBrown784052e2009-03-31 15:19:07 +11001596static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
NeilBrown784052e2009-03-31 15:19:07 +11001598static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct r5dev *dev = &sh->dev[i];
1601
1602 bio_init(&dev->req);
1603 dev->req.bi_io_vec = &dev->vec;
1604 dev->req.bi_vcnt++;
1605 dev->req.bi_max_vecs++;
1606 dev->vec.bv_page = dev->page;
1607 dev->vec.bv_len = STRIPE_SIZE;
1608 dev->vec.bv_offset = 0;
1609
1610 dev->req.bi_sector = sh->sector;
1611 dev->req.bi_private = sh;
1612
1613 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001614 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1618{
1619 char b[BDEVNAME_SIZE];
1620 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001621 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
NeilBrownb2d444d2005-11-08 21:39:31 -08001623 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b422006-10-03 01:15:46 -07001624 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001625 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1626 unsigned long flags;
1627 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001629 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * if recovery was running, make sure it aborts.
1632 */
NeilBrowndfc70642008-05-23 13:04:39 -07001633 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001635 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001636 printk(KERN_ALERT
1637 "raid5: Disk failure on %s, disabling device.\n"
1638 "raid5: Operation continuing on %d devices.\n",
1639 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643/*
1644 * Input: a 'big' sector number,
1645 * Output: index of the data and parity disk, and the sector # in them.
1646 */
NeilBrown112bf892009-03-31 14:39:38 +11001647static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001648 int previous, int *dd_idx,
1649 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 long stripe;
1652 unsigned long chunk_number;
1653 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001654 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001655 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001657 int algorithm = previous ? conf->prev_algo
1658 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001659 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1660 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001661 int raid_disks = previous ? conf->previous_raid_disks
1662 : conf->raid_disks;
1663 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 /* First compute the information on this sector */
1666
1667 /*
1668 * Compute the chunk number and the sector offset inside the chunk
1669 */
1670 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1671 chunk_number = r_sector;
1672 BUG_ON(r_sector != chunk_number);
1673
1674 /*
1675 * Compute the stripe number
1676 */
1677 stripe = chunk_number / data_disks;
1678
1679 /*
1680 * Compute the data disk and parity disk indexes inside the stripe
1681 */
1682 *dd_idx = chunk_number % data_disks;
1683
1684 /*
1685 * Select the parity disk based on the user selected algorithm.
1686 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001687 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001688 switch(conf->level) {
1689 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001690 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001691 break;
1692 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001693 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001695 pd_idx = data_disks - stripe % raid_disks;
1696 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 (*dd_idx)++;
1698 break;
1699 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001700 pd_idx = stripe % raid_disks;
1701 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 (*dd_idx)++;
1703 break;
1704 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001705 pd_idx = data_disks - stripe % raid_disks;
1706 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001709 pd_idx = stripe % raid_disks;
1710 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001712 case ALGORITHM_PARITY_0:
1713 pd_idx = 0;
1714 (*dd_idx)++;
1715 break;
1716 case ALGORITHM_PARITY_N:
1717 pd_idx = data_disks;
1718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001720 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001721 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001722 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001723 }
1724 break;
1725 case 6:
1726
NeilBrowne183eae2009-03-31 15:20:22 +11001727 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001728 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001729 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1730 qd_idx = pd_idx + 1;
1731 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001732 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001733 qd_idx = 0;
1734 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001735 (*dd_idx) += 2; /* D D P Q D */
1736 break;
1737 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001738 pd_idx = stripe % raid_disks;
1739 qd_idx = pd_idx + 1;
1740 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001741 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001742 qd_idx = 0;
1743 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001744 (*dd_idx) += 2; /* D D P Q D */
1745 break;
1746 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001747 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1748 qd_idx = (pd_idx + 1) % raid_disks;
1749 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001750 break;
1751 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001752 pd_idx = stripe % raid_disks;
1753 qd_idx = (pd_idx + 1) % raid_disks;
1754 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001755 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001756
1757 case ALGORITHM_PARITY_0:
1758 pd_idx = 0;
1759 qd_idx = 1;
1760 (*dd_idx) += 2;
1761 break;
1762 case ALGORITHM_PARITY_N:
1763 pd_idx = data_disks;
1764 qd_idx = data_disks + 1;
1765 break;
1766
1767 case ALGORITHM_ROTATING_ZERO_RESTART:
1768 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1769 * of blocks for computing Q is different.
1770 */
1771 pd_idx = stripe % raid_disks;
1772 qd_idx = pd_idx + 1;
1773 if (pd_idx == raid_disks-1) {
1774 (*dd_idx)++; /* Q D D D P */
1775 qd_idx = 0;
1776 } else if (*dd_idx >= pd_idx)
1777 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001778 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001779 break;
1780
1781 case ALGORITHM_ROTATING_N_RESTART:
1782 /* Same a left_asymmetric, by first stripe is
1783 * D D D P Q rather than
1784 * Q D D D P
1785 */
1786 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1787 qd_idx = pd_idx + 1;
1788 if (pd_idx == raid_disks-1) {
1789 (*dd_idx)++; /* Q D D D P */
1790 qd_idx = 0;
1791 } else if (*dd_idx >= pd_idx)
1792 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001793 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001794 break;
1795
1796 case ALGORITHM_ROTATING_N_CONTINUE:
1797 /* Same as left_symmetric but Q is before P */
1798 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1799 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1800 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001801 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001802 break;
1803
1804 case ALGORITHM_LEFT_ASYMMETRIC_6:
1805 /* RAID5 left_asymmetric, with Q on last device */
1806 pd_idx = data_disks - stripe % (raid_disks-1);
1807 if (*dd_idx >= pd_idx)
1808 (*dd_idx)++;
1809 qd_idx = raid_disks - 1;
1810 break;
1811
1812 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1813 pd_idx = stripe % (raid_disks-1);
1814 if (*dd_idx >= pd_idx)
1815 (*dd_idx)++;
1816 qd_idx = raid_disks - 1;
1817 break;
1818
1819 case ALGORITHM_LEFT_SYMMETRIC_6:
1820 pd_idx = data_disks - stripe % (raid_disks-1);
1821 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1822 qd_idx = raid_disks - 1;
1823 break;
1824
1825 case ALGORITHM_RIGHT_SYMMETRIC_6:
1826 pd_idx = stripe % (raid_disks-1);
1827 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1828 qd_idx = raid_disks - 1;
1829 break;
1830
1831 case ALGORITHM_PARITY_0_6:
1832 pd_idx = 0;
1833 (*dd_idx)++;
1834 qd_idx = raid_disks - 1;
1835 break;
1836
1837
NeilBrown16a53ec2006-06-26 00:27:38 -07001838 default:
NeilBrownd710e132008-10-13 11:55:12 +11001839 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001840 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001841 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001842 }
1843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845
NeilBrown911d4ee2009-03-31 14:39:38 +11001846 if (sh) {
1847 sh->pd_idx = pd_idx;
1848 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001849 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /*
1852 * Finally, compute the new sector number
1853 */
1854 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1855 return new_sector;
1856}
1857
1858
NeilBrown784052e2009-03-31 15:19:07 +11001859static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001862 int raid_disks = sh->disks;
1863 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001865 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1866 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001867 int algorithm = previous ? conf->prev_algo
1868 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 sector_t stripe;
1870 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001871 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001873 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
NeilBrown16a53ec2006-06-26 00:27:38 -07001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1877 stripe = new_sector;
1878 BUG_ON(new_sector != stripe);
1879
NeilBrown16a53ec2006-06-26 00:27:38 -07001880 if (i == sh->pd_idx)
1881 return 0;
1882 switch(conf->level) {
1883 case 4: break;
1884 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001885 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 case ALGORITHM_LEFT_ASYMMETRIC:
1887 case ALGORITHM_RIGHT_ASYMMETRIC:
1888 if (i > sh->pd_idx)
1889 i--;
1890 break;
1891 case ALGORITHM_LEFT_SYMMETRIC:
1892 case ALGORITHM_RIGHT_SYMMETRIC:
1893 if (i < sh->pd_idx)
1894 i += raid_disks;
1895 i -= (sh->pd_idx + 1);
1896 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001897 case ALGORITHM_PARITY_0:
1898 i -= 1;
1899 break;
1900 case ALGORITHM_PARITY_N:
1901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001903 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001904 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001905 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001906 }
1907 break;
1908 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001909 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001910 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001911 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001912 case ALGORITHM_LEFT_ASYMMETRIC:
1913 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001914 case ALGORITHM_ROTATING_ZERO_RESTART:
1915 case ALGORITHM_ROTATING_N_RESTART:
1916 if (sh->pd_idx == raid_disks-1)
1917 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001918 else if (i > sh->pd_idx)
1919 i -= 2; /* D D P Q D */
1920 break;
1921 case ALGORITHM_LEFT_SYMMETRIC:
1922 case ALGORITHM_RIGHT_SYMMETRIC:
1923 if (sh->pd_idx == raid_disks-1)
1924 i--; /* Q D D D P */
1925 else {
1926 /* D D P Q D */
1927 if (i < sh->pd_idx)
1928 i += raid_disks;
1929 i -= (sh->pd_idx + 2);
1930 }
1931 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001932 case ALGORITHM_PARITY_0:
1933 i -= 2;
1934 break;
1935 case ALGORITHM_PARITY_N:
1936 break;
1937 case ALGORITHM_ROTATING_N_CONTINUE:
1938 if (sh->pd_idx == 0)
1939 i--; /* P D D D Q */
1940 else if (i > sh->pd_idx)
1941 i -= 2; /* D D Q P D */
1942 break;
1943 case ALGORITHM_LEFT_ASYMMETRIC_6:
1944 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1945 if (i > sh->pd_idx)
1946 i--;
1947 break;
1948 case ALGORITHM_LEFT_SYMMETRIC_6:
1949 case ALGORITHM_RIGHT_SYMMETRIC_6:
1950 if (i < sh->pd_idx)
1951 i += data_disks + 1;
1952 i -= (sh->pd_idx + 1);
1953 break;
1954 case ALGORITHM_PARITY_0_6:
1955 i -= 1;
1956 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001957 default:
NeilBrownd710e132008-10-13 11:55:12 +11001958 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001959 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001960 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001961 }
1962 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
1965 chunk_number = stripe * data_disks + i;
1966 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1967
NeilBrown112bf892009-03-31 14:39:38 +11001968 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001969 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001970 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1971 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001972 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
1974 }
1975 return r_sector;
1976}
1977
1978
Dan Williams600aa102008-06-28 08:32:05 +10001979static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001980schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001981 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001982{
1983 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001984 raid5_conf_t *conf = sh->raid_conf;
1985 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07001986
Dan Williamse33129d2007-01-02 13:52:30 -07001987 if (rcw) {
1988 /* if we are not expanding this is a proper write request, and
1989 * there will be bios with new data to be drained into the
1990 * stripe cache
1991 */
1992 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001993 sh->reconstruct_state = reconstruct_state_drain_run;
1994 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1995 } else
1996 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001997
Dan Williamsac6b53b2009-07-14 13:40:19 -07001998 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001999
2000 for (i = disks; i--; ) {
2001 struct r5dev *dev = &sh->dev[i];
2002
2003 if (dev->towrite) {
2004 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002005 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002006 if (!expand)
2007 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002008 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002009 }
2010 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002011 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002012 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002013 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002014 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002015 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002016 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2017 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2018
Dan Williamsd8ee0722008-06-28 08:32:06 +10002019 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002020 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2021 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002022 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002023
2024 for (i = disks; i--; ) {
2025 struct r5dev *dev = &sh->dev[i];
2026 if (i == pd_idx)
2027 continue;
2028
Dan Williamse33129d2007-01-02 13:52:30 -07002029 if (dev->towrite &&
2030 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002031 test_bit(R5_Wantcompute, &dev->flags))) {
2032 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002033 set_bit(R5_LOCKED, &dev->flags);
2034 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002035 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002036 }
2037 }
2038 }
2039
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002040 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002041 * are in flight
2042 */
2043 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2044 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002045 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002046
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002047 if (level == 6) {
2048 int qd_idx = sh->qd_idx;
2049 struct r5dev *dev = &sh->dev[qd_idx];
2050
2051 set_bit(R5_LOCKED, &dev->flags);
2052 clear_bit(R5_UPTODATE, &dev->flags);
2053 s->locked++;
2054 }
2055
Dan Williams600aa102008-06-28 08:32:05 +10002056 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002057 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002058 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002059}
NeilBrown16a53ec2006-06-26 00:27:38 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061/*
2062 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002063 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 * The bi_next chain must be in order.
2065 */
2066static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2067{
2068 struct bio **bip;
2069 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002070 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Dan Williams45b42332007-07-09 11:56:43 -07002072 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 (unsigned long long)bi->bi_sector,
2074 (unsigned long long)sh->sector);
2075
2076
2077 spin_lock(&sh->lock);
2078 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002079 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002081 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2082 firstwrite = 1;
2083 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 bip = &sh->dev[dd_idx].toread;
2085 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2086 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2087 goto overlap;
2088 bip = & (*bip)->bi_next;
2089 }
2090 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2091 goto overlap;
2092
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002093 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (*bip)
2095 bi->bi_next = *bip;
2096 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002097 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 spin_unlock_irq(&conf->device_lock);
2099 spin_unlock(&sh->lock);
2100
Dan Williams45b42332007-07-09 11:56:43 -07002101 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 (unsigned long long)bi->bi_sector,
2103 (unsigned long long)sh->sector, dd_idx);
2104
NeilBrown72626682005-09-09 16:23:54 -07002105 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002106 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2107 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002108 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002109 set_bit(STRIPE_BIT_DELAY, &sh->state);
2110 }
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (forwrite) {
2113 /* check if page is covered */
2114 sector_t sector = sh->dev[dd_idx].sector;
2115 for (bi=sh->dev[dd_idx].towrite;
2116 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2117 bi && bi->bi_sector <= sector;
2118 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2119 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2120 sector = bi->bi_sector + (bi->bi_size>>9);
2121 }
2122 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2123 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2124 }
2125 return 1;
2126
2127 overlap:
2128 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2129 spin_unlock_irq(&conf->device_lock);
2130 spin_unlock(&sh->lock);
2131 return 0;
2132}
2133
NeilBrown29269552006-03-27 01:18:10 -08002134static void end_reshape(raid5_conf_t *conf);
2135
NeilBrown911d4ee2009-03-31 14:39:38 +11002136static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2137 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002138{
NeilBrown784052e2009-03-31 15:19:07 +11002139 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002140 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002141 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002142 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002143 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002144
NeilBrown112bf892009-03-31 14:39:38 +11002145 raid5_compute_sector(conf,
2146 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002147 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002148 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002149 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002150}
2151
Dan Williamsa4456852007-07-09 11:56:43 -07002152static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002153handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002154 struct stripe_head_state *s, int disks,
2155 struct bio **return_bi)
2156{
2157 int i;
2158 for (i = disks; i--; ) {
2159 struct bio *bi;
2160 int bitmap_end = 0;
2161
2162 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2163 mdk_rdev_t *rdev;
2164 rcu_read_lock();
2165 rdev = rcu_dereference(conf->disks[i].rdev);
2166 if (rdev && test_bit(In_sync, &rdev->flags))
2167 /* multiple read failures in one stripe */
2168 md_error(conf->mddev, rdev);
2169 rcu_read_unlock();
2170 }
2171 spin_lock_irq(&conf->device_lock);
2172 /* fail all writes first */
2173 bi = sh->dev[i].towrite;
2174 sh->dev[i].towrite = NULL;
2175 if (bi) {
2176 s->to_write--;
2177 bitmap_end = 1;
2178 }
2179
2180 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2181 wake_up(&conf->wait_for_overlap);
2182
2183 while (bi && bi->bi_sector <
2184 sh->dev[i].sector + STRIPE_SECTORS) {
2185 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2186 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002187 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002188 md_write_end(conf->mddev);
2189 bi->bi_next = *return_bi;
2190 *return_bi = bi;
2191 }
2192 bi = nextbi;
2193 }
2194 /* and fail all 'written' */
2195 bi = sh->dev[i].written;
2196 sh->dev[i].written = NULL;
2197 if (bi) bitmap_end = 1;
2198 while (bi && bi->bi_sector <
2199 sh->dev[i].sector + STRIPE_SECTORS) {
2200 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2201 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002202 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002203 md_write_end(conf->mddev);
2204 bi->bi_next = *return_bi;
2205 *return_bi = bi;
2206 }
2207 bi = bi2;
2208 }
2209
Dan Williamsb5e98d62007-01-02 13:52:31 -07002210 /* fail any reads if this device is non-operational and
2211 * the data has not reached the cache yet.
2212 */
2213 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2214 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2215 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002216 bi = sh->dev[i].toread;
2217 sh->dev[i].toread = NULL;
2218 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2219 wake_up(&conf->wait_for_overlap);
2220 if (bi) s->to_read--;
2221 while (bi && bi->bi_sector <
2222 sh->dev[i].sector + STRIPE_SECTORS) {
2223 struct bio *nextbi =
2224 r5_next_bio(bi, sh->dev[i].sector);
2225 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002226 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002227 bi->bi_next = *return_bi;
2228 *return_bi = bi;
2229 }
2230 bi = nextbi;
2231 }
2232 }
2233 spin_unlock_irq(&conf->device_lock);
2234 if (bitmap_end)
2235 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2236 STRIPE_SECTORS, 0, 0);
2237 }
2238
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002239 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2240 if (atomic_dec_and_test(&conf->pending_full_writes))
2241 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002242}
2243
Dan Williams1fe797e2008-06-28 09:16:30 +10002244/* fetch_block5 - checks the given member device to see if its data needs
2245 * to be read or computed to satisfy a request.
2246 *
2247 * Returns 1 when no more member devices need to be checked, otherwise returns
2248 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002249 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002250static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2251 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002252{
2253 struct r5dev *dev = &sh->dev[disk_idx];
2254 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2255
Dan Williamsf38e1212007-01-02 13:52:30 -07002256 /* is the data in this block needed, and can we get it? */
2257 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002258 !test_bit(R5_UPTODATE, &dev->flags) &&
2259 (dev->toread ||
2260 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2261 s->syncing || s->expanding ||
2262 (s->failed &&
2263 (failed_dev->toread ||
2264 (failed_dev->towrite &&
2265 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002266 /* We would like to get this block, possibly by computing it,
2267 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002268 */
2269 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002270 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002271 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2272 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002273 set_bit(R5_Wantcompute, &dev->flags);
2274 sh->ops.target = disk_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002275 sh->ops.target2 = -1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002276 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002277 /* Careful: from this point on 'uptodate' is in the eye
Dan Williamsac6b53b2009-07-14 13:40:19 -07002278 * of raid_run_ops which services 'compute' operations
Dan Williamsf38e1212007-01-02 13:52:30 -07002279 * before writes. R5_Wantcompute flags a block that will
2280 * be R5_UPTODATE by the time it is needed for a
2281 * subsequent operation.
2282 */
2283 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002284 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002285 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002286 set_bit(R5_LOCKED, &dev->flags);
2287 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002288 s->locked++;
2289 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2290 s->syncing);
2291 }
2292 }
2293
Dan Williams1fe797e2008-06-28 09:16:30 +10002294 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002295}
2296
Dan Williams1fe797e2008-06-28 09:16:30 +10002297/**
2298 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2299 */
2300static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002301 struct stripe_head_state *s, int disks)
2302{
2303 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002304
Dan Williamsf38e1212007-01-02 13:52:30 -07002305 /* look for blocks to read/compute, skip this if a compute
2306 * is already in flight, or if the stripe contents are in the
2307 * midst of changing due to a write
2308 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002309 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002310 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002311 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002312 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002313 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002314 set_bit(STRIPE_HANDLE, &sh->state);
2315}
2316
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002317/* fetch_block6 - checks the given member device to see if its data needs
2318 * to be read or computed to satisfy a request.
2319 *
2320 * Returns 1 when no more member devices need to be checked, otherwise returns
2321 * 0 to tell the loop in handle_stripe_fill6 to continue
2322 */
2323static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2324 struct r6_state *r6s, int disk_idx, int disks)
2325{
2326 struct r5dev *dev = &sh->dev[disk_idx];
2327 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2328 &sh->dev[r6s->failed_num[1]] };
2329
2330 if (!test_bit(R5_LOCKED, &dev->flags) &&
2331 !test_bit(R5_UPTODATE, &dev->flags) &&
2332 (dev->toread ||
2333 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2334 s->syncing || s->expanding ||
2335 (s->failed >= 1 &&
2336 (fdev[0]->toread || s->to_write)) ||
2337 (s->failed >= 2 &&
2338 (fdev[1]->toread || s->to_write)))) {
2339 /* we would like to get this block, possibly by computing it,
2340 * otherwise read it if the backing disk is insync
2341 */
2342 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2343 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2344 if ((s->uptodate == disks - 1) &&
2345 (s->failed && (disk_idx == r6s->failed_num[0] ||
2346 disk_idx == r6s->failed_num[1]))) {
2347 /* have disk failed, and we're requested to fetch it;
2348 * do compute it
2349 */
2350 pr_debug("Computing stripe %llu block %d\n",
2351 (unsigned long long)sh->sector, disk_idx);
2352 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2353 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2354 set_bit(R5_Wantcompute, &dev->flags);
2355 sh->ops.target = disk_idx;
2356 sh->ops.target2 = -1; /* no 2nd target */
2357 s->req_compute = 1;
2358 s->uptodate++;
2359 return 1;
2360 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2361 /* Computing 2-failure is *very* expensive; only
2362 * do it if failed >= 2
2363 */
2364 int other;
2365 for (other = disks; other--; ) {
2366 if (other == disk_idx)
2367 continue;
2368 if (!test_bit(R5_UPTODATE,
2369 &sh->dev[other].flags))
2370 break;
2371 }
2372 BUG_ON(other < 0);
2373 pr_debug("Computing stripe %llu blocks %d,%d\n",
2374 (unsigned long long)sh->sector,
2375 disk_idx, other);
2376 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2377 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2378 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2379 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2380 sh->ops.target = disk_idx;
2381 sh->ops.target2 = other;
2382 s->uptodate += 2;
2383 s->req_compute = 1;
2384 return 1;
2385 } else if (test_bit(R5_Insync, &dev->flags)) {
2386 set_bit(R5_LOCKED, &dev->flags);
2387 set_bit(R5_Wantread, &dev->flags);
2388 s->locked++;
2389 pr_debug("Reading block %d (sync=%d)\n",
2390 disk_idx, s->syncing);
2391 }
2392 }
2393
2394 return 0;
2395}
2396
2397/**
2398 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2399 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002400static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002401 struct stripe_head_state *s, struct r6_state *r6s,
2402 int disks)
2403{
2404 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002405
2406 /* look for blocks to read/compute, skip this if a compute
2407 * is already in flight, or if the stripe contents are in the
2408 * midst of changing due to a write
2409 */
2410 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2411 !sh->reconstruct_state)
2412 for (i = disks; i--; )
2413 if (fetch_block6(sh, s, r6s, i, disks))
2414 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002415 set_bit(STRIPE_HANDLE, &sh->state);
2416}
2417
2418
Dan Williams1fe797e2008-06-28 09:16:30 +10002419/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002420 * any written block on an uptodate or failed drive can be returned.
2421 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2422 * never LOCKED, so we don't need to test 'failed' directly.
2423 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002424static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002425 struct stripe_head *sh, int disks, struct bio **return_bi)
2426{
2427 int i;
2428 struct r5dev *dev;
2429
2430 for (i = disks; i--; )
2431 if (sh->dev[i].written) {
2432 dev = &sh->dev[i];
2433 if (!test_bit(R5_LOCKED, &dev->flags) &&
2434 test_bit(R5_UPTODATE, &dev->flags)) {
2435 /* We can return any write requests */
2436 struct bio *wbi, *wbi2;
2437 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002438 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002439 spin_lock_irq(&conf->device_lock);
2440 wbi = dev->written;
2441 dev->written = NULL;
2442 while (wbi && wbi->bi_sector <
2443 dev->sector + STRIPE_SECTORS) {
2444 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002445 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002446 md_write_end(conf->mddev);
2447 wbi->bi_next = *return_bi;
2448 *return_bi = wbi;
2449 }
2450 wbi = wbi2;
2451 }
2452 if (dev->towrite == NULL)
2453 bitmap_end = 1;
2454 spin_unlock_irq(&conf->device_lock);
2455 if (bitmap_end)
2456 bitmap_endwrite(conf->mddev->bitmap,
2457 sh->sector,
2458 STRIPE_SECTORS,
2459 !test_bit(STRIPE_DEGRADED, &sh->state),
2460 0);
2461 }
2462 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002463
2464 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2465 if (atomic_dec_and_test(&conf->pending_full_writes))
2466 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002467}
2468
Dan Williams1fe797e2008-06-28 09:16:30 +10002469static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002470 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2471{
2472 int rmw = 0, rcw = 0, i;
2473 for (i = disks; i--; ) {
2474 /* would I have to read this buffer for read_modify_write */
2475 struct r5dev *dev = &sh->dev[i];
2476 if ((dev->towrite || i == sh->pd_idx) &&
2477 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002478 !(test_bit(R5_UPTODATE, &dev->flags) ||
2479 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002480 if (test_bit(R5_Insync, &dev->flags))
2481 rmw++;
2482 else
2483 rmw += 2*disks; /* cannot read it */
2484 }
2485 /* Would I have to read this buffer for reconstruct_write */
2486 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2487 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002488 !(test_bit(R5_UPTODATE, &dev->flags) ||
2489 test_bit(R5_Wantcompute, &dev->flags))) {
2490 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002491 else
2492 rcw += 2*disks;
2493 }
2494 }
Dan Williams45b42332007-07-09 11:56:43 -07002495 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002496 (unsigned long long)sh->sector, rmw, rcw);
2497 set_bit(STRIPE_HANDLE, &sh->state);
2498 if (rmw < rcw && rmw > 0)
2499 /* prefer read-modify-write, but need to get some data */
2500 for (i = disks; i--; ) {
2501 struct r5dev *dev = &sh->dev[i];
2502 if ((dev->towrite || i == sh->pd_idx) &&
2503 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002504 !(test_bit(R5_UPTODATE, &dev->flags) ||
2505 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002506 test_bit(R5_Insync, &dev->flags)) {
2507 if (
2508 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002509 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002510 "%d for r-m-w\n", i);
2511 set_bit(R5_LOCKED, &dev->flags);
2512 set_bit(R5_Wantread, &dev->flags);
2513 s->locked++;
2514 } else {
2515 set_bit(STRIPE_DELAYED, &sh->state);
2516 set_bit(STRIPE_HANDLE, &sh->state);
2517 }
2518 }
2519 }
2520 if (rcw <= rmw && rcw > 0)
2521 /* want reconstruct write, but need to get some data */
2522 for (i = disks; i--; ) {
2523 struct r5dev *dev = &sh->dev[i];
2524 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2525 i != sh->pd_idx &&
2526 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002527 !(test_bit(R5_UPTODATE, &dev->flags) ||
2528 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002529 test_bit(R5_Insync, &dev->flags)) {
2530 if (
2531 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002532 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002533 "%d for Reconstruct\n", i);
2534 set_bit(R5_LOCKED, &dev->flags);
2535 set_bit(R5_Wantread, &dev->flags);
2536 s->locked++;
2537 } else {
2538 set_bit(STRIPE_DELAYED, &sh->state);
2539 set_bit(STRIPE_HANDLE, &sh->state);
2540 }
2541 }
2542 }
2543 /* now if nothing is locked, and if we have enough data,
2544 * we can start a write request
2545 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002546 /* since handle_stripe can be called at any time we need to handle the
2547 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002548 * subsequent call wants to start a write request. raid_run_ops only
2549 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002550 * simultaneously. If this is not the case then new writes need to be
2551 * held off until the compute completes.
2552 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002553 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2554 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2555 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002556 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002557}
2558
Dan Williams1fe797e2008-06-28 09:16:30 +10002559static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002560 struct stripe_head *sh, struct stripe_head_state *s,
2561 struct r6_state *r6s, int disks)
2562{
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002563 int rcw = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002564 int qd_idx = sh->qd_idx;
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002565
2566 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002567 for (i = disks; i--; ) {
2568 struct r5dev *dev = &sh->dev[i];
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002569 /* check if we haven't enough data */
2570 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2571 i != pd_idx && i != qd_idx &&
2572 !test_bit(R5_LOCKED, &dev->flags) &&
2573 !(test_bit(R5_UPTODATE, &dev->flags) ||
2574 test_bit(R5_Wantcompute, &dev->flags))) {
2575 rcw++;
2576 if (!test_bit(R5_Insync, &dev->flags))
2577 continue; /* it's a failed drive */
2578
2579 if (
2580 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2581 pr_debug("Read_old stripe %llu "
2582 "block %d for Reconstruct\n",
2583 (unsigned long long)sh->sector, i);
2584 set_bit(R5_LOCKED, &dev->flags);
2585 set_bit(R5_Wantread, &dev->flags);
2586 s->locked++;
2587 } else {
2588 pr_debug("Request delayed stripe %llu "
2589 "block %d for Reconstruct\n",
2590 (unsigned long long)sh->sector, i);
2591 set_bit(STRIPE_DELAYED, &sh->state);
2592 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002593 }
2594 }
2595 }
Dan Williamsa4456852007-07-09 11:56:43 -07002596 /* now if nothing is locked, and if we have enough data, we can start a
2597 * write request
2598 */
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002599 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2600 s->locked == 0 && rcw == 0 &&
Dan Williamsa4456852007-07-09 11:56:43 -07002601 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002602 schedule_reconstruction(sh, s, 1, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002603 }
2604}
2605
2606static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2607 struct stripe_head_state *s, int disks)
2608{
Dan Williamsecc65c92008-06-28 08:31:57 +10002609 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002610
Dan Williamsbd2ab672008-04-10 21:29:27 -07002611 set_bit(STRIPE_HANDLE, &sh->state);
2612
Dan Williamsecc65c92008-06-28 08:31:57 +10002613 switch (sh->check_state) {
2614 case check_state_idle:
2615 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002616 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002617 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002618 sh->check_state = check_state_run;
2619 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002620 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002621 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002622 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002623 }
Dan Williamsa4456852007-07-09 11:56:43 -07002624 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002625 /* fall through */
2626 case check_state_compute_result:
2627 sh->check_state = check_state_idle;
2628 if (!dev)
2629 dev = &sh->dev[sh->pd_idx];
2630
2631 /* check that a write has not made the stripe insync */
2632 if (test_bit(STRIPE_INSYNC, &sh->state))
2633 break;
2634
2635 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002636 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2637 BUG_ON(s->uptodate != disks);
2638
2639 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002640 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002641 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002642
Dan Williamsa4456852007-07-09 11:56:43 -07002643 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002644 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002645 break;
2646 case check_state_run:
2647 break; /* we will be called again upon completion */
2648 case check_state_check_result:
2649 sh->check_state = check_state_idle;
2650
2651 /* if a failure occurred during the check operation, leave
2652 * STRIPE_INSYNC not set and let the stripe be handled again
2653 */
2654 if (s->failed)
2655 break;
2656
2657 /* handle a successful check operation, if parity is correct
2658 * we are done. Otherwise update the mismatch count and repair
2659 * parity if !MD_RECOVERY_CHECK
2660 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002661 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002662 /* parity is correct (on disc,
2663 * not in buffer any more)
2664 */
2665 set_bit(STRIPE_INSYNC, &sh->state);
2666 else {
2667 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2668 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2669 /* don't try to repair!! */
2670 set_bit(STRIPE_INSYNC, &sh->state);
2671 else {
2672 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002673 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002674 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2675 set_bit(R5_Wantcompute,
2676 &sh->dev[sh->pd_idx].flags);
2677 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002678 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002679 s->uptodate++;
2680 }
2681 }
2682 break;
2683 case check_state_compute_run:
2684 break;
2685 default:
2686 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2687 __func__, sh->check_state,
2688 (unsigned long long) sh->sector);
2689 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002690 }
2691}
2692
2693
2694static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002695 struct stripe_head_state *s,
2696 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002697{
Dan Williamsa4456852007-07-09 11:56:43 -07002698 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002699 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002700 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002701
2702 set_bit(STRIPE_HANDLE, &sh->state);
2703
2704 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002705
Dan Williamsa4456852007-07-09 11:56:43 -07002706 /* Want to check and possibly repair P and Q.
2707 * However there could be one 'failed' device, in which
2708 * case we can only check one of them, possibly using the
2709 * other to generate missing data
2710 */
2711
Dan Williamsd82dfee2009-07-14 13:40:57 -07002712 switch (sh->check_state) {
2713 case check_state_idle:
2714 /* start a new check operation if there are < 2 failures */
Dan Williamsa4456852007-07-09 11:56:43 -07002715 if (s->failed == r6s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002716 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002717 * makes sense to check P (If anything else were failed,
2718 * we would have used P to recreate it).
2719 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002720 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002721 }
2722 if (!r6s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002723 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002724 * anything, so it makes sense to check it
2725 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002726 if (sh->check_state == check_state_run)
2727 sh->check_state = check_state_run_pq;
2728 else
2729 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002730 }
Dan Williams36d1c642009-07-14 11:48:22 -07002731
Dan Williamsd82dfee2009-07-14 13:40:57 -07002732 /* discard potentially stale zero_sum_result */
2733 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002734
Dan Williamsd82dfee2009-07-14 13:40:57 -07002735 if (sh->check_state == check_state_run) {
2736 /* async_xor_zero_sum destroys the contents of P */
2737 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2738 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002739 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002740 if (sh->check_state >= check_state_run &&
2741 sh->check_state <= check_state_run_pq) {
2742 /* async_syndrome_zero_sum preserves P and Q, so
2743 * no need to mark them !uptodate here
2744 */
2745 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2746 break;
2747 }
Dan Williams36d1c642009-07-14 11:48:22 -07002748
Dan Williamsd82dfee2009-07-14 13:40:57 -07002749 /* we have 2-disk failure */
2750 BUG_ON(s->failed != 2);
2751 /* fall through */
2752 case check_state_compute_result:
2753 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002754
Dan Williamsd82dfee2009-07-14 13:40:57 -07002755 /* check that a write has not made the stripe insync */
2756 if (test_bit(STRIPE_INSYNC, &sh->state))
2757 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002758
2759 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002760 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002761 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002762 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002763 if (s->failed == 2) {
2764 dev = &sh->dev[r6s->failed_num[1]];
2765 s->locked++;
2766 set_bit(R5_LOCKED, &dev->flags);
2767 set_bit(R5_Wantwrite, &dev->flags);
2768 }
2769 if (s->failed >= 1) {
2770 dev = &sh->dev[r6s->failed_num[0]];
2771 s->locked++;
2772 set_bit(R5_LOCKED, &dev->flags);
2773 set_bit(R5_Wantwrite, &dev->flags);
2774 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002775 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002776 dev = &sh->dev[pd_idx];
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_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002782 dev = &sh->dev[qd_idx];
2783 s->locked++;
2784 set_bit(R5_LOCKED, &dev->flags);
2785 set_bit(R5_Wantwrite, &dev->flags);
2786 }
2787 clear_bit(STRIPE_DEGRADED, &sh->state);
2788
2789 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002790 break;
2791 case check_state_run:
2792 case check_state_run_q:
2793 case check_state_run_pq:
2794 break; /* we will be called again upon completion */
2795 case check_state_check_result:
2796 sh->check_state = check_state_idle;
2797
2798 /* handle a successful check operation, if parity is correct
2799 * we are done. Otherwise update the mismatch count and repair
2800 * parity if !MD_RECOVERY_CHECK
2801 */
2802 if (sh->ops.zero_sum_result == 0) {
2803 /* both parities are correct */
2804 if (!s->failed)
2805 set_bit(STRIPE_INSYNC, &sh->state);
2806 else {
2807 /* in contrast to the raid5 case we can validate
2808 * parity, but still have a failure to write
2809 * back
2810 */
2811 sh->check_state = check_state_compute_result;
2812 /* Returning at this point means that we may go
2813 * off and bring p and/or q uptodate again so
2814 * we make sure to check zero_sum_result again
2815 * to verify if p or q need writeback
2816 */
2817 }
2818 } else {
2819 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2820 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2821 /* don't try to repair!! */
2822 set_bit(STRIPE_INSYNC, &sh->state);
2823 else {
2824 int *target = &sh->ops.target;
2825
2826 sh->ops.target = -1;
2827 sh->ops.target2 = -1;
2828 sh->check_state = check_state_compute_run;
2829 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2830 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2831 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2832 set_bit(R5_Wantcompute,
2833 &sh->dev[pd_idx].flags);
2834 *target = pd_idx;
2835 target = &sh->ops.target2;
2836 s->uptodate++;
2837 }
2838 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2839 set_bit(R5_Wantcompute,
2840 &sh->dev[qd_idx].flags);
2841 *target = qd_idx;
2842 s->uptodate++;
2843 }
2844 }
2845 }
2846 break;
2847 case check_state_compute_run:
2848 break;
2849 default:
2850 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2851 __func__, sh->check_state,
2852 (unsigned long long) sh->sector);
2853 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002854 }
2855}
2856
2857static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2858 struct r6_state *r6s)
2859{
2860 int i;
2861
2862 /* We have read all the blocks in this stripe and now we need to
2863 * copy some of them into a target stripe for expand.
2864 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002865 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002866 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2867 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002868 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002869 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002870 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002871 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002872
NeilBrown784052e2009-03-31 15:19:07 +11002873 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002874 sector_t s = raid5_compute_sector(conf, bn, 0,
2875 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002876 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002877 if (sh2 == NULL)
2878 /* so far only the early blocks of this stripe
2879 * have been requested. When later blocks
2880 * get requested, we will try again
2881 */
2882 continue;
2883 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2884 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2885 /* must have already done this block */
2886 release_stripe(sh2);
2887 continue;
2888 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002889
2890 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002891 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002892 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002893 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002894 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002895
Dan Williamsa4456852007-07-09 11:56:43 -07002896 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2897 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2898 for (j = 0; j < conf->raid_disks; j++)
2899 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002900 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002901 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2902 break;
2903 if (j == conf->raid_disks) {
2904 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2905 set_bit(STRIPE_HANDLE, &sh2->state);
2906 }
2907 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002908
Dan Williamsa4456852007-07-09 11:56:43 -07002909 }
NeilBrowna2e08552007-09-11 15:23:36 -07002910 /* done submitting copies, wait for them to complete */
2911 if (tx) {
2912 async_tx_ack(tx);
2913 dma_wait_for_async_tx(tx);
2914 }
Dan Williamsa4456852007-07-09 11:56:43 -07002915}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Dan Williams6bfe0b42008-04-30 00:52:32 -07002917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918/*
2919 * handle_stripe - do things to a stripe.
2920 *
2921 * We lock the stripe and then examine the state of various bits
2922 * to see what needs to be done.
2923 * Possible results:
2924 * return some read request which now have data
2925 * return some write requests which are safely on disc
2926 * schedule a read on some buffers
2927 * schedule a write of some buffers
2928 * return confirmation of parity correctness
2929 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 * buffers are taken off read_list or write_list, and bh_cache buffers
2931 * get BH_Lock set before the stripe lock is released.
2932 *
2933 */
Dan Williamsa4456852007-07-09 11:56:43 -07002934
NeilBrown14425772009-10-16 15:55:25 +11002935static void handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
2937 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002938 int disks = sh->disks, i;
2939 struct bio *return_bi = NULL;
2940 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002942 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002943 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Dan Williamsa4456852007-07-09 11:56:43 -07002945 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002946 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2947 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2948 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2949 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 spin_lock(&sh->lock);
2952 clear_bit(STRIPE_HANDLE, &sh->state);
2953 clear_bit(STRIPE_DELAYED, &sh->state);
2954
Dan Williamsa4456852007-07-09 11:56:43 -07002955 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2956 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2957 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002960 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 for (i=disks; i--; ) {
2962 mdk_rdev_t *rdev;
NeilBrowna9f326e2009-09-23 18:06:41 +10002963
2964 dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Dan Williamsb5e98d62007-01-02 13:52:31 -07002967 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2968 "written %p\n", i, dev->flags, dev->toread, dev->read,
2969 dev->towrite, dev->written);
2970
2971 /* maybe we can request a biofill operation
2972 *
2973 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002974 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002975 */
2976 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002977 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002978 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002981 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2982 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002983 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Dan Williamsb5e98d62007-01-02 13:52:31 -07002985 if (test_bit(R5_Wantfill, &dev->flags))
2986 s.to_fill++;
2987 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002988 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002990 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002992 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
Dan Williamsa4456852007-07-09 11:56:43 -07002994 if (dev->written)
2995 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002996 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002997 if (blocked_rdev == NULL &&
2998 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002999 blocked_rdev = rdev;
3000 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003001 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003002 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08003003 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08003004 clear_bit(R5_ReadError, &dev->flags);
3005 clear_bit(R5_ReWrite, &dev->flags);
3006 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003007 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003008 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003009 s.failed++;
3010 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 } else
3012 set_bit(R5_Insync, &dev->flags);
3013 }
NeilBrown9910f162006-01-06 00:20:24 -08003014 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07003015
Dan Williams6bfe0b42008-04-30 00:52:32 -07003016 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003017 if (s.syncing || s.expanding || s.expanded ||
3018 s.to_write || s.written) {
3019 set_bit(STRIPE_HANDLE, &sh->state);
3020 goto unlock;
3021 }
3022 /* There is nothing for the blocked_rdev to block */
3023 rdev_dec_pending(blocked_rdev, conf->mddev);
3024 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003025 }
3026
Dan Williams83de75c2008-06-28 08:31:58 +10003027 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3028 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3029 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3030 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07003031
Dan Williams45b42332007-07-09 11:56:43 -07003032 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003034 s.locked, s.uptodate, s.to_read, s.to_write,
3035 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 /* check if the array has lost two devices and, if so, some requests might
3037 * need to be failed
3038 */
Dan Williamsa4456852007-07-09 11:56:43 -07003039 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003040 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003041 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3043 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003044 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046
3047 /* might be able to return some write requests if the parity block
3048 * is safe, or on a failed drive
3049 */
3050 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003051 if ( s.written &&
3052 ((test_bit(R5_Insync, &dev->flags) &&
3053 !test_bit(R5_LOCKED, &dev->flags) &&
3054 test_bit(R5_UPTODATE, &dev->flags)) ||
3055 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10003056 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 /* Now we might consider reading some blocks, either to check/generate
3059 * parity, or to satisfy requests
3060 * or to load a block that is being partially written.
3061 */
Dan Williamsa4456852007-07-09 11:56:43 -07003062 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10003063 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003064 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Dan Williamse33129d2007-01-02 13:52:30 -07003066 /* Now we check to see if any write operations have recently
3067 * completed
3068 */
Dan Williamse0a115e2008-06-05 22:45:52 -07003069 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003070 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07003071 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003072 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3073 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10003074 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07003075
3076 /* All the 'written' buffers and the parity block are ready to
3077 * be written back to disk
3078 */
3079 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3080 for (i = disks; i--; ) {
3081 dev = &sh->dev[i];
3082 if (test_bit(R5_LOCKED, &dev->flags) &&
3083 (i == sh->pd_idx || dev->written)) {
3084 pr_debug("Writing block %d\n", i);
3085 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07003086 if (prexor)
3087 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07003088 if (!test_bit(R5_Insync, &dev->flags) ||
3089 (i == sh->pd_idx && s.failed == 0))
3090 set_bit(STRIPE_INSYNC, &sh->state);
3091 }
3092 }
3093 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3094 atomic_dec(&conf->preread_active_stripes);
3095 if (atomic_read(&conf->preread_active_stripes) <
3096 IO_THRESHOLD)
3097 md_wakeup_thread(conf->mddev->thread);
3098 }
3099 }
3100
3101 /* Now to consider new write requests and what else, if anything
3102 * should be read. We do not handle new writes when:
3103 * 1/ A 'write' operation (copy+xor) is already in flight.
3104 * 2/ A 'check' operation is in flight, as it may clobber the parity
3105 * block.
3106 */
Dan Williams600aa102008-06-28 08:32:05 +10003107 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003108 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07003111 * Any reads will already have been scheduled, so we just see if enough
3112 * data is available. The parity check is held off while parity
3113 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 */
Dan Williamsecc65c92008-06-28 08:31:57 +10003115 if (sh->check_state ||
3116 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003117 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10003118 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07003119 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07003120
Dan Williamsa4456852007-07-09 11:56:43 -07003121 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3123 clear_bit(STRIPE_SYNCING, &sh->state);
3124 }
NeilBrown4e5314b2005-11-08 21:39:22 -08003125
3126 /* If the failed drive is just a ReadError, then we might need to progress
3127 * the repair/check process
3128 */
Dan Williamsa4456852007-07-09 11:56:43 -07003129 if (s.failed == 1 && !conf->mddev->ro &&
3130 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3131 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3132 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003133 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003134 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003135 if (!test_bit(R5_ReWrite, &dev->flags)) {
3136 set_bit(R5_Wantwrite, &dev->flags);
3137 set_bit(R5_ReWrite, &dev->flags);
3138 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003139 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003140 } else {
3141 /* let's read it back */
3142 set_bit(R5_Wantread, &dev->flags);
3143 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003144 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003145 }
3146 }
3147
Dan Williams600aa102008-06-28 08:32:05 +10003148 /* Finish reconstruct operations initiated by the expansion process */
3149 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003150 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003151 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003152 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3153 /* sh cannot be written until sh2 has been read.
3154 * so arrange for sh to be delayed a little
3155 */
3156 set_bit(STRIPE_DELAYED, &sh->state);
3157 set_bit(STRIPE_HANDLE, &sh->state);
3158 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3159 &sh2->state))
3160 atomic_inc(&conf->preread_active_stripes);
3161 release_stripe(sh2);
3162 goto unlock;
3163 }
3164 if (sh2)
3165 release_stripe(sh2);
3166
Dan Williams600aa102008-06-28 08:32:05 +10003167 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003168 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003169 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003170 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003171 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003172 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003173 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003174 }
3175
3176 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003177 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003178 /* Need to write out all blocks after computing parity */
3179 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003180 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003181 schedule_reconstruction(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003182 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003183 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003184 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003185 wake_up(&conf->wait_for_overlap);
3186 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3187 }
3188
Dan Williams0f94e87c2008-01-08 15:32:53 -08003189 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003190 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003191 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003192
Dan Williams6bfe0b42008-04-30 00:52:32 -07003193 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 spin_unlock(&sh->lock);
3195
Dan Williams6bfe0b42008-04-30 00:52:32 -07003196 /* wait for this device to become unblocked */
3197 if (unlikely(blocked_rdev))
3198 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3199
Dan Williams600aa102008-06-28 08:32:05 +10003200 if (s.ops_request)
Dan Williamsac6b53b2009-07-14 13:40:19 -07003201 raid_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003202
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003203 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Dan Williamsa4456852007-07-09 11:56:43 -07003205 return_io(return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
NeilBrown14425772009-10-16 15:55:25 +11003208static void handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003209{
NeilBrownbff61972009-03-31 14:33:13 +11003210 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003211 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003212 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003213 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003214 struct stripe_head_state s;
3215 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003216 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003217 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003218
Dan Williams45b42332007-07-09 11:56:43 -07003219 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003220 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003221 (unsigned long long)sh->sector, sh->state,
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003222 atomic_read(&sh->count), pd_idx, qd_idx,
3223 sh->check_state, sh->reconstruct_state);
Dan Williamsa4456852007-07-09 11:56:43 -07003224 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003225
3226 spin_lock(&sh->lock);
3227 clear_bit(STRIPE_HANDLE, &sh->state);
3228 clear_bit(STRIPE_DELAYED, &sh->state);
3229
Dan Williamsa4456852007-07-09 11:56:43 -07003230 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3231 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3232 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003233 /* Now to look around and see what can be done */
3234
3235 rcu_read_lock();
3236 for (i=disks; i--; ) {
3237 mdk_rdev_t *rdev;
3238 dev = &sh->dev[i];
3239 clear_bit(R5_Insync, &dev->flags);
3240
Dan Williams45b42332007-07-09 11:56:43 -07003241 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003242 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003243 /* maybe we can reply to a read
3244 *
3245 * new wantfill requests are only permitted while
3246 * ops_complete_biofill is guaranteed to be inactive
3247 */
3248 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3249 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3250 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003251
3252 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003253 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3254 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003255 if (test_bit(R5_Wantcompute, &dev->flags)) {
3256 s.compute++;
3257 BUG_ON(s.compute > 2);
3258 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003259
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003260 if (test_bit(R5_Wantfill, &dev->flags)) {
3261 s.to_fill++;
3262 } else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003263 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003264 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003265 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003266 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003267 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003268 }
Dan Williamsa4456852007-07-09 11:56:43 -07003269 if (dev->written)
3270 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003271 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003272 if (blocked_rdev == NULL &&
3273 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003274 blocked_rdev = rdev;
3275 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003276 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003277 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3278 /* The ReadError flag will just be confusing now */
3279 clear_bit(R5_ReadError, &dev->flags);
3280 clear_bit(R5_ReWrite, &dev->flags);
3281 }
3282 if (!rdev || !test_bit(In_sync, &rdev->flags)
3283 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003284 if (s.failed < 2)
3285 r6s.failed_num[s.failed] = i;
3286 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003287 } else
3288 set_bit(R5_Insync, &dev->flags);
3289 }
3290 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003291
3292 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003293 if (s.syncing || s.expanding || s.expanded ||
3294 s.to_write || s.written) {
3295 set_bit(STRIPE_HANDLE, &sh->state);
3296 goto unlock;
3297 }
3298 /* There is nothing for the blocked_rdev to block */
3299 rdev_dec_pending(blocked_rdev, conf->mddev);
3300 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003301 }
NeilBrownac4090d2008-08-05 15:54:13 +10003302
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003303 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3304 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3305 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3306 }
3307
Dan Williams45b42332007-07-09 11:56:43 -07003308 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003309 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003310 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3311 r6s.failed_num[0], r6s.failed_num[1]);
3312 /* check if the array has lost >2 devices and, if so, some requests
3313 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003314 */
Dan Williamsa4456852007-07-09 11:56:43 -07003315 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003316 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003317 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003318 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3319 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003320 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003321 }
3322
3323 /*
3324 * might be able to return some write requests if the parity blocks
3325 * are safe, or on a failed drive
3326 */
3327 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003328 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3329 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003330 qdev = &sh->dev[qd_idx];
3331 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3332 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003333
Dan Williamsa4456852007-07-09 11:56:43 -07003334 if ( s.written &&
3335 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003336 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003337 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3338 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003339 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003340 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003341 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003342
3343 /* Now we might consider reading some blocks, either to check/generate
3344 * parity, or to satisfy requests
3345 * or to load a block that is being partially written.
3346 */
Dan Williamsa4456852007-07-09 11:56:43 -07003347 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003348 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003349 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003350
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003351 /* Now we check to see if any write operations have recently
3352 * completed
3353 */
3354 if (sh->reconstruct_state == reconstruct_state_drain_result) {
3355 int qd_idx = sh->qd_idx;
3356
3357 sh->reconstruct_state = reconstruct_state_idle;
3358 /* All the 'written' buffers and the parity blocks are ready to
3359 * be written back to disk
3360 */
3361 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3362 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
3363 for (i = disks; i--; ) {
3364 dev = &sh->dev[i];
3365 if (test_bit(R5_LOCKED, &dev->flags) &&
3366 (i == sh->pd_idx || i == qd_idx ||
3367 dev->written)) {
3368 pr_debug("Writing block %d\n", i);
3369 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3370 set_bit(R5_Wantwrite, &dev->flags);
3371 if (!test_bit(R5_Insync, &dev->flags) ||
3372 ((i == sh->pd_idx || i == qd_idx) &&
3373 s.failed == 0))
3374 set_bit(STRIPE_INSYNC, &sh->state);
3375 }
3376 }
3377 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3378 atomic_dec(&conf->preread_active_stripes);
3379 if (atomic_read(&conf->preread_active_stripes) <
3380 IO_THRESHOLD)
3381 md_wakeup_thread(conf->mddev->thread);
3382 }
3383 }
3384
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07003385 /* Now to consider new write requests and what else, if anything
3386 * should be read. We do not handle new writes when:
3387 * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
3388 * 2/ A 'check' operation is in flight, as it may clobber the parity
3389 * block.
3390 */
3391 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003392 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003393
3394 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003395 * Any reads will already have been scheduled, so we just see if enough
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003396 * data is available. The parity check is held off while parity
3397 * dependent operations are in flight.
NeilBrown16a53ec2006-06-26 00:27:38 -07003398 */
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003399 if (sh->check_state ||
3400 (s.syncing && s.locked == 0 &&
3401 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3402 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williams36d1c642009-07-14 11:48:22 -07003403 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003404
Dan Williamsa4456852007-07-09 11:56:43 -07003405 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003406 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3407 clear_bit(STRIPE_SYNCING, &sh->state);
3408 }
3409
3410 /* If the failed drives are just a ReadError, then we might need
3411 * to progress the repair/check process
3412 */
Dan Williamsa4456852007-07-09 11:56:43 -07003413 if (s.failed <= 2 && !conf->mddev->ro)
3414 for (i = 0; i < s.failed; i++) {
3415 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003416 if (test_bit(R5_ReadError, &dev->flags)
3417 && !test_bit(R5_LOCKED, &dev->flags)
3418 && test_bit(R5_UPTODATE, &dev->flags)
3419 ) {
3420 if (!test_bit(R5_ReWrite, &dev->flags)) {
3421 set_bit(R5_Wantwrite, &dev->flags);
3422 set_bit(R5_ReWrite, &dev->flags);
3423 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003424 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003425 } else {
3426 /* let's read it back */
3427 set_bit(R5_Wantread, &dev->flags);
3428 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003429 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003430 }
3431 }
3432 }
NeilBrownf4168852007-02-28 20:11:53 -08003433
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003434 /* Finish reconstruct operations initiated by the expansion process */
3435 if (sh->reconstruct_state == reconstruct_state_result) {
3436 sh->reconstruct_state = reconstruct_state_idle;
3437 clear_bit(STRIPE_EXPANDING, &sh->state);
3438 for (i = conf->raid_disks; i--; ) {
3439 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3440 set_bit(R5_LOCKED, &sh->dev[i].flags);
3441 s.locked++;
3442 }
3443 }
3444
3445 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3446 !sh->reconstruct_state) {
NeilBrownab69ae12009-03-31 15:26:47 +11003447 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003448 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003449 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3450 /* sh cannot be written until sh2 has been read.
3451 * so arrange for sh to be delayed a little
3452 */
3453 set_bit(STRIPE_DELAYED, &sh->state);
3454 set_bit(STRIPE_HANDLE, &sh->state);
3455 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3456 &sh2->state))
3457 atomic_inc(&conf->preread_active_stripes);
3458 release_stripe(sh2);
3459 goto unlock;
3460 }
3461 if (sh2)
3462 release_stripe(sh2);
3463
NeilBrownf4168852007-02-28 20:11:53 -08003464 /* Need to write out all blocks after computing P&Q */
3465 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003466 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003467 schedule_reconstruction(sh, &s, 1, 1);
3468 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003469 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3470 atomic_dec(&conf->reshape_stripes);
3471 wake_up(&conf->wait_for_overlap);
3472 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3473 }
3474
Dan Williams0f94e87c2008-01-08 15:32:53 -08003475 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003476 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003477 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003478
Dan Williams6bfe0b42008-04-30 00:52:32 -07003479 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003480 spin_unlock(&sh->lock);
3481
Dan Williams6bfe0b42008-04-30 00:52:32 -07003482 /* wait for this device to become unblocked */
3483 if (unlikely(blocked_rdev))
3484 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3485
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003486 if (s.ops_request)
3487 raid_run_ops(sh, s.ops_request);
3488
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003489 ops_run_io(sh, &s);
3490
Dan Williamsa4456852007-07-09 11:56:43 -07003491 return_io(return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003492}
3493
NeilBrown14425772009-10-16 15:55:25 +11003494static void handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003495{
3496 if (sh->raid_conf->level == 6)
NeilBrown14425772009-10-16 15:55:25 +11003497 handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003498 else
NeilBrown14425772009-10-16 15:55:25 +11003499 handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003500}
3501
Arjan van de Ven858119e2006-01-14 13:20:43 -08003502static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503{
3504 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3505 while (!list_empty(&conf->delayed_list)) {
3506 struct list_head *l = conf->delayed_list.next;
3507 struct stripe_head *sh;
3508 sh = list_entry(l, struct stripe_head, lru);
3509 list_del_init(l);
3510 clear_bit(STRIPE_DELAYED, &sh->state);
3511 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3512 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003513 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
NeilBrown6ed30032008-02-06 01:40:00 -08003515 } else
3516 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Arjan van de Ven858119e2006-01-14 13:20:43 -08003519static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003520{
3521 /* device_lock is held */
3522 struct list_head head;
3523 list_add(&head, &conf->bitmap_list);
3524 list_del_init(&conf->bitmap_list);
3525 while (!list_empty(&head)) {
3526 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3527 list_del_init(&sh->lru);
3528 atomic_inc(&sh->count);
3529 __release_stripe(conf, sh);
3530 }
3531}
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533static void unplug_slaves(mddev_t *mddev)
3534{
NeilBrown070ec552009-06-16 16:54:21 +10003535 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 int i;
3537
3538 rcu_read_lock();
NeilBrownf001a702009-06-09 14:30:31 +10003539 for (i = 0; i < conf->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003540 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003541 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003542 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
3544 atomic_inc(&rdev->nr_pending);
3545 rcu_read_unlock();
3546
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003547 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 rdev_dec_pending(rdev, mddev);
3550 rcu_read_lock();
3551 }
3552 }
3553 rcu_read_unlock();
3554}
3555
Jens Axboe165125e2007-07-24 09:28:11 +02003556static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
3558 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003559 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 unsigned long flags;
3561
3562 spin_lock_irqsave(&conf->device_lock, flags);
3563
NeilBrown72626682005-09-09 16:23:54 -07003564 if (blk_remove_plug(q)) {
3565 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 md_wakeup_thread(mddev->thread);
3569
3570 spin_unlock_irqrestore(&conf->device_lock, flags);
3571
3572 unplug_slaves(mddev);
3573}
3574
NeilBrownf022b2f2006-10-03 01:15:56 -07003575static int raid5_congested(void *data, int bits)
3576{
3577 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +10003578 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003579
3580 /* No difference between reads and writes. Just check
3581 * how busy the stripe_cache is
3582 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003583
3584 if (mddev_congested(mddev, bits))
3585 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07003586 if (conf->inactive_blocked)
3587 return 1;
3588 if (conf->quiesce)
3589 return 1;
3590 if (list_empty_careful(&conf->inactive_list))
3591 return 1;
3592
3593 return 0;
3594}
3595
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003596/* We want read requests to align with chunks where possible,
3597 * but write requests don't need to.
3598 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003599static int raid5_mergeable_bvec(struct request_queue *q,
3600 struct bvec_merge_data *bvm,
3601 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003602{
3603 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003604 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003605 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003606 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003607 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003608
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003609 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003610 return biovec->bv_len; /* always allow writes to be mergeable */
3611
Andre Noll664e7c42009-06-18 08:45:27 +10003612 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3613 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003614 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3615 if (max < 0) max = 0;
3616 if (max <= biovec->bv_len && bio_sectors == 0)
3617 return biovec->bv_len;
3618 else
3619 return max;
3620}
3621
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003622
3623static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3624{
3625 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003626 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003627 unsigned int bio_sectors = bio->bi_size >> 9;
3628
Andre Noll664e7c42009-06-18 08:45:27 +10003629 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3630 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003631 return chunk_sectors >=
3632 ((sector & (chunk_sectors - 1)) + bio_sectors);
3633}
3634
3635/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003636 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3637 * later sampled by raid5d.
3638 */
3639static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3640{
3641 unsigned long flags;
3642
3643 spin_lock_irqsave(&conf->device_lock, flags);
3644
3645 bi->bi_next = conf->retry_read_aligned_list;
3646 conf->retry_read_aligned_list = bi;
3647
3648 spin_unlock_irqrestore(&conf->device_lock, flags);
3649 md_wakeup_thread(conf->mddev->thread);
3650}
3651
3652
3653static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3654{
3655 struct bio *bi;
3656
3657 bi = conf->retry_read_aligned;
3658 if (bi) {
3659 conf->retry_read_aligned = NULL;
3660 return bi;
3661 }
3662 bi = conf->retry_read_aligned_list;
3663 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003664 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003665 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003666 /*
3667 * this sets the active strip count to 1 and the processed
3668 * strip count to zero (upper 8 bits)
3669 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003670 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003671 }
3672
3673 return bi;
3674}
3675
3676
3677/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003678 * The "raid5_align_endio" should check if the read succeeded and if it
3679 * did, call bio_endio on the original bio (having bio_put the new bio
3680 * first).
3681 * If the read failed..
3682 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003683static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003684{
3685 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003686 mddev_t *mddev;
3687 raid5_conf_t *conf;
3688 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3689 mdk_rdev_t *rdev;
3690
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003691 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003692
3693 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003694 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003695 rdev = (void*)raid_bi->bi_next;
3696 raid_bi->bi_next = NULL;
3697
3698 rdev_dec_pending(rdev, conf->mddev);
3699
3700 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003701 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003702 if (atomic_dec_and_test(&conf->active_aligned_reads))
3703 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003704 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003705 }
3706
3707
Dan Williams45b42332007-07-09 11:56:43 -07003708 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003709
3710 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003711}
3712
Neil Brown387bb172007-02-08 14:20:29 -08003713static int bio_fits_rdev(struct bio *bi)
3714{
Jens Axboe165125e2007-07-24 09:28:11 +02003715 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003716
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003717 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003718 return 0;
3719 blk_recount_segments(q, bi);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003720 if (bi->bi_phys_segments > queue_max_phys_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003721 return 0;
3722
3723 if (q->merge_bvec_fn)
3724 /* it's too hard to apply the merge_bvec_fn at this stage,
3725 * just just give up
3726 */
3727 return 0;
3728
3729 return 1;
3730}
3731
3732
Jens Axboe165125e2007-07-24 09:28:11 +02003733static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003734{
3735 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003736 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003737 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003738 struct bio* align_bi;
3739 mdk_rdev_t *rdev;
3740
3741 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003742 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003743 return 0;
3744 }
3745 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003746 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003747 */
3748 align_bi = bio_clone(raid_bio, GFP_NOIO);
3749 if (!align_bi)
3750 return 0;
3751 /*
3752 * set bi_end_io to a new function, and set bi_private to the
3753 * original bio.
3754 */
3755 align_bi->bi_end_io = raid5_align_endio;
3756 align_bi->bi_private = raid_bio;
3757 /*
3758 * compute position
3759 */
NeilBrown112bf892009-03-31 14:39:38 +11003760 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3761 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003762 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003763
3764 rcu_read_lock();
3765 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3766 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003767 atomic_inc(&rdev->nr_pending);
3768 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003769 raid_bio->bi_next = (void*)rdev;
3770 align_bi->bi_bdev = rdev->bdev;
3771 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3772 align_bi->bi_sector += rdev->data_offset;
3773
Neil Brown387bb172007-02-08 14:20:29 -08003774 if (!bio_fits_rdev(align_bi)) {
3775 /* too big in some way */
3776 bio_put(align_bi);
3777 rdev_dec_pending(rdev, mddev);
3778 return 0;
3779 }
3780
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003781 spin_lock_irq(&conf->device_lock);
3782 wait_event_lock_irq(conf->wait_for_stripe,
3783 conf->quiesce == 0,
3784 conf->device_lock, /* nothing */);
3785 atomic_inc(&conf->active_aligned_reads);
3786 spin_unlock_irq(&conf->device_lock);
3787
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003788 generic_make_request(align_bi);
3789 return 1;
3790 } else {
3791 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003792 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003793 return 0;
3794 }
3795}
3796
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003797/* __get_priority_stripe - get the next stripe to process
3798 *
3799 * Full stripe writes are allowed to pass preread active stripes up until
3800 * the bypass_threshold is exceeded. In general the bypass_count
3801 * increments when the handle_list is handled before the hold_list; however, it
3802 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3803 * stripe with in flight i/o. The bypass_count will be reset when the
3804 * head of the hold_list has changed, i.e. the head was promoted to the
3805 * handle_list.
3806 */
3807static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3808{
3809 struct stripe_head *sh;
3810
3811 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3812 __func__,
3813 list_empty(&conf->handle_list) ? "empty" : "busy",
3814 list_empty(&conf->hold_list) ? "empty" : "busy",
3815 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3816
3817 if (!list_empty(&conf->handle_list)) {
3818 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3819
3820 if (list_empty(&conf->hold_list))
3821 conf->bypass_count = 0;
3822 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3823 if (conf->hold_list.next == conf->last_hold)
3824 conf->bypass_count++;
3825 else {
3826 conf->last_hold = conf->hold_list.next;
3827 conf->bypass_count -= conf->bypass_threshold;
3828 if (conf->bypass_count < 0)
3829 conf->bypass_count = 0;
3830 }
3831 }
3832 } else if (!list_empty(&conf->hold_list) &&
3833 ((conf->bypass_threshold &&
3834 conf->bypass_count > conf->bypass_threshold) ||
3835 atomic_read(&conf->pending_full_writes) == 0)) {
3836 sh = list_entry(conf->hold_list.next,
3837 typeof(*sh), lru);
3838 conf->bypass_count -= conf->bypass_threshold;
3839 if (conf->bypass_count < 0)
3840 conf->bypass_count = 0;
3841 } else
3842 return NULL;
3843
3844 list_del_init(&sh->lru);
3845 atomic_inc(&sh->count);
3846 BUG_ON(atomic_read(&sh->count) != 1);
3847 return sh;
3848}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003849
Jens Axboe165125e2007-07-24 09:28:11 +02003850static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
3852 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003853 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003854 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 sector_t new_sector;
3856 sector_t logical_sector, last_sector;
3857 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003858 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003859 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Jens Axboe1f98a132009-09-11 14:32:04 +02003861 if (unlikely(bio_rw_flagged(bi, BIO_RW_BARRIER))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003862 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003863 return 0;
3864 }
3865
NeilBrown3d310eb2005-06-21 17:17:26 -07003866 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003867
Tejun Heo074a7ac2008-08-25 19:56:14 +09003868 cpu = part_stat_lock();
3869 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3870 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3871 bio_sectors(bi));
3872 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
NeilBrown802ba062006-12-13 00:34:13 -08003874 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003875 mddev->reshape_position == MaxSector &&
3876 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003877 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3880 last_sector = bi->bi_sector + (bi->bi_size>>9);
3881 bi->bi_next = NULL;
3882 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3885 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003886 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003887 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003888
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003889 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003890 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003891 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003892 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003893 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003894 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08003895 * 64bit on a 32bit platform, and so it might be
3896 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003897 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08003898 * the lock is dropped, so once we get a reference
3899 * to the stripe that we think it is, we will have
3900 * to check again.
3901 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003902 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003903 if (mddev->delta_disks < 0
3904 ? logical_sector < conf->reshape_progress
3905 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003906 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003907 previous = 1;
3908 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003909 if (mddev->delta_disks < 0
3910 ? logical_sector < conf->reshape_safe
3911 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003912 spin_unlock_irq(&conf->device_lock);
3913 schedule();
3914 goto retry;
3915 }
3916 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003917 spin_unlock_irq(&conf->device_lock);
3918 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003919 data_disks = disks - conf->max_degraded;
3920
NeilBrown112bf892009-03-31 14:39:38 +11003921 new_sector = raid5_compute_sector(conf, logical_sector,
3922 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003923 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003924 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 (unsigned long long)new_sector,
3926 (unsigned long long)logical_sector);
3927
NeilBrownb5663ba2009-03-31 14:39:38 +11003928 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003929 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003931 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003932 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08003933 * stripe, so we must do the range check again.
3934 * Expansion could still move past after this
3935 * test, but as we are holding a reference to
3936 * 'sh', we know that if that happens,
3937 * STRIPE_EXPANDING will get set and the expansion
3938 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003939 */
3940 int must_retry = 0;
3941 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003942 if (mddev->delta_disks < 0
3943 ? logical_sector >= conf->reshape_progress
3944 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003945 /* mismatch, need to try again */
3946 must_retry = 1;
3947 spin_unlock_irq(&conf->device_lock);
3948 if (must_retry) {
3949 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003950 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003951 goto retry;
3952 }
3953 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003954
NeilBrowna5c308d2009-07-01 13:15:35 +10003955 if (bio_data_dir(bi) == WRITE &&
3956 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003957 logical_sector < mddev->suspend_hi) {
3958 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003959 /* As the suspend_* range is controlled by
3960 * userspace, we want an interruptible
3961 * wait.
3962 */
3963 flush_signals(current);
3964 prepare_to_wait(&conf->wait_for_overlap,
3965 &w, TASK_INTERRUPTIBLE);
3966 if (logical_sector >= mddev->suspend_lo &&
3967 logical_sector < mddev->suspend_hi)
3968 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003969 goto retry;
3970 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003971
3972 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3973 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3974 /* Stripe is busy expanding or
3975 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 * and wait a while
3977 */
3978 raid5_unplug_device(mddev->queue);
3979 release_stripe(sh);
3980 schedule();
3981 goto retry;
3982 }
3983 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003984 set_bit(STRIPE_HANDLE, &sh->state);
3985 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 } else {
3988 /* cannot get stripe for read-ahead, just give-up */
3989 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3990 finish_wait(&conf->wait_for_overlap, &w);
3991 break;
3992 }
3993
3994 }
3995 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003996 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003997 spin_unlock_irq(&conf->device_lock);
3998 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
NeilBrown16a53ec2006-06-26 00:27:38 -07004000 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004002
Neil Brown0e13fe232008-06-28 08:31:20 +10004003 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 return 0;
4006}
4007
Dan Williamsb522adc2009-03-31 15:00:31 +11004008static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
4009
NeilBrown52c03292006-06-26 00:27:43 -07004010static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
NeilBrown52c03292006-06-26 00:27:43 -07004012 /* reshaping is quite different to recovery/resync so it is
4013 * handled quite separately ... here.
4014 *
4015 * On each call to sync_request, we gather one chunk worth of
4016 * destination stripes and flag them as expanding.
4017 * Then we find all the source stripes and request reads.
4018 * As the reads complete, handle_stripe will copy the data
4019 * into the destination stripe and release that stripe.
4020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4022 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004023 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004024 int raid_disks = conf->previous_raid_disks;
4025 int data_disks = raid_disks - conf->max_degraded;
4026 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004027 int i;
4028 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004029 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004030 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004031 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004032 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004033
NeilBrownfef9c612009-03-31 15:16:46 +11004034 if (sector_nr == 0) {
4035 /* If restarting in the middle, skip the initial sectors */
4036 if (mddev->delta_disks < 0 &&
4037 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4038 sector_nr = raid5_size(mddev, 0, 0)
4039 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004040 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004041 conf->reshape_progress > 0)
4042 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004043 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004044 if (sector_nr) {
4045 *skipped = 1;
4046 return sector_nr;
4047 }
NeilBrown52c03292006-06-26 00:27:43 -07004048 }
4049
NeilBrown7a661382009-03-31 15:21:40 +11004050 /* We need to process a full chunk at a time.
4051 * If old and new chunk sizes differ, we need to process the
4052 * largest of these
4053 */
Andre Noll664e7c42009-06-18 08:45:27 +10004054 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4055 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004056 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004057 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004058
NeilBrown52c03292006-06-26 00:27:43 -07004059 /* we update the metadata when there is more than 3Meg
4060 * in the block range (that is rather arbitrary, should
4061 * probably be time based) or when the data about to be
4062 * copied would over-write the source of the data at
4063 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004064 * i.e. one new_stripe along from reshape_progress new_maps
4065 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004066 */
NeilBrownfef9c612009-03-31 15:16:46 +11004067 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004068 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004069 readpos = conf->reshape_progress;
4070 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004071 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004072 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004073 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004074 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004075 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004076 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004077 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004078 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004079 readpos -= min_t(sector_t, reshape_sectors, readpos);
4080 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004081 }
NeilBrown52c03292006-06-26 00:27:43 -07004082
NeilBrownc8f517c2009-03-31 15:28:40 +11004083 /* 'writepos' is the most advanced device address we might write.
4084 * 'readpos' is the least advanced device address we might read.
4085 * 'safepos' is the least address recorded in the metadata as having
4086 * been reshaped.
4087 * If 'readpos' is behind 'writepos', then there is no way that we can
4088 * ensure safety in the face of a crash - that must be done by userspace
4089 * making a backup of the data. So in that case there is no particular
4090 * rush to update metadata.
4091 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4092 * update the metadata to advance 'safepos' to match 'readpos' so that
4093 * we can be safe in the event of a crash.
4094 * So we insist on updating metadata if safepos is behind writepos and
4095 * readpos is beyond writepos.
4096 * In any case, update the metadata every 10 seconds.
4097 * Maybe that number should be configurable, but I'm not sure it is
4098 * worth it.... maybe it could be a multiple of safemode_delay???
4099 */
NeilBrownfef9c612009-03-31 15:16:46 +11004100 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004101 ? (safepos > writepos && readpos < writepos)
4102 : (safepos < writepos && readpos > writepos)) ||
4103 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004104 /* Cannot proceed until we've updated the superblock... */
4105 wait_event(conf->wait_for_overlap,
4106 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004107 mddev->reshape_position = conf->reshape_progress;
NeilBrownacb180b2009-04-14 16:28:34 +10004108 mddev->curr_resync_completed = mddev->curr_resync;
NeilBrownc8f517c2009-03-31 15:28:40 +11004109 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004110 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004111 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004112 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004113 kthread_should_stop());
4114 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004115 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004116 spin_unlock_irq(&conf->device_lock);
4117 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004118 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004119 }
4120
NeilBrownec32a2b2009-03-31 15:17:38 +11004121 if (mddev->delta_disks < 0) {
4122 BUG_ON(conf->reshape_progress == 0);
4123 stripe_addr = writepos;
4124 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004125 ~((sector_t)reshape_sectors - 1))
4126 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004127 != sector_nr);
4128 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004129 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004130 stripe_addr = sector_nr;
4131 }
NeilBrownab69ae12009-03-31 15:26:47 +11004132 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004133 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004134 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004135 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004136 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004137 set_bit(STRIPE_EXPANDING, &sh->state);
4138 atomic_inc(&conf->reshape_stripes);
4139 /* If any of this stripe is beyond the end of the old
4140 * array, then we need to zero those blocks
4141 */
4142 for (j=sh->disks; j--;) {
4143 sector_t s;
4144 if (j == sh->pd_idx)
4145 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004146 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004147 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004148 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004149 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004150 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004151 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004152 continue;
4153 }
4154 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4155 set_bit(R5_Expanded, &sh->dev[j].flags);
4156 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4157 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004158 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004159 set_bit(STRIPE_EXPAND_READY, &sh->state);
4160 set_bit(STRIPE_HANDLE, &sh->state);
4161 }
NeilBrownab69ae12009-03-31 15:26:47 +11004162 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004163 }
4164 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004165 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004166 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004167 else
NeilBrown7a661382009-03-31 15:21:40 +11004168 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004169 spin_unlock_irq(&conf->device_lock);
4170 /* Ok, those stripe are ready. We can start scheduling
4171 * reads on the source stripes.
4172 * The source stripes are determined by mapping the first and last
4173 * block on the destination stripes.
4174 */
NeilBrown52c03292006-06-26 00:27:43 -07004175 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004176 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004177 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004178 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004179 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004180 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004181 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004182 if (last_sector >= mddev->dev_sectors)
4183 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004184 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004185 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004186 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4187 set_bit(STRIPE_HANDLE, &sh->state);
4188 release_stripe(sh);
4189 first_sector += STRIPE_SECTORS;
4190 }
NeilBrownab69ae12009-03-31 15:26:47 +11004191 /* Now that the sources are clearly marked, we can release
4192 * the destination stripes
4193 */
4194 while (!list_empty(&stripes)) {
4195 sh = list_entry(stripes.next, struct stripe_head, lru);
4196 list_del_init(&sh->lru);
4197 release_stripe(sh);
4198 }
NeilBrownc6207272008-02-06 01:39:52 -08004199 /* If this takes us to the resync_max point where we have to pause,
4200 * then we need to write out the superblock.
4201 */
NeilBrown7a661382009-03-31 15:21:40 +11004202 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004203 if ((sector_nr - mddev->curr_resync_completed) * 2
4204 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004205 /* Cannot proceed until we've updated the superblock... */
4206 wait_event(conf->wait_for_overlap,
4207 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004208 mddev->reshape_position = conf->reshape_progress;
NeilBrown48606a92009-06-18 09:14:12 +10004209 mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11004210 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004211 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4212 md_wakeup_thread(mddev->thread);
4213 wait_event(mddev->sb_wait,
4214 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4215 || kthread_should_stop());
4216 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004217 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004218 spin_unlock_irq(&conf->device_lock);
4219 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004220 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004221 }
NeilBrown7a661382009-03-31 15:21:40 +11004222 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004223}
4224
4225/* FIXME go_faster isn't used */
4226static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4227{
4228 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4229 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004230 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07004231 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004232 int still_degraded = 0;
4233 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
NeilBrown72626682005-09-09 16:23:54 -07004235 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 /* just being told to finish up .. nothing much to do */
4237 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004238
NeilBrown29269552006-03-27 01:18:10 -08004239 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4240 end_reshape(conf);
4241 return 0;
4242 }
NeilBrown72626682005-09-09 16:23:54 -07004243
4244 if (mddev->curr_resync < max_sector) /* aborted */
4245 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4246 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004247 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004248 conf->fullsync = 0;
4249 bitmap_close_sync(mddev->bitmap);
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 return 0;
4252 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004253
NeilBrown64bd6602009-08-03 10:59:58 +10004254 /* Allow raid5_quiesce to complete */
4255 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4256
NeilBrown52c03292006-06-26 00:27:43 -07004257 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4258 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004259
NeilBrownc6207272008-02-06 01:39:52 -08004260 /* No need to check resync_max as we never do more than one
4261 * stripe, and as resync_max will always be on a chunk boundary,
4262 * if the check in md_do_sync didn't fire, there is no chance
4263 * of overstepping resync_max here
4264 */
4265
NeilBrown16a53ec2006-06-26 00:27:38 -07004266 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 * to resync, then assert that we are finished, because there is
4268 * nothing we can do.
4269 */
NeilBrown3285edf2006-06-26 00:27:55 -07004270 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004271 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004272 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004273 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 return rv;
4275 }
NeilBrown72626682005-09-09 16:23:54 -07004276 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004277 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004278 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4279 /* we can skip this block, and probably more */
4280 sync_blocks /= STRIPE_SECTORS;
4281 *skipped = 1;
4282 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
NeilBrownb47490c2008-02-06 01:39:50 -08004285
4286 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4287
NeilBrowna8c906c2009-06-09 14:39:59 +10004288 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004290 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004292 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004294 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004296 /* Need to check if array will still be degraded after recovery/resync
4297 * We don't need to check the 'failed' flag as when that gets set,
4298 * recovery aborts.
4299 */
NeilBrownf001a702009-06-09 14:30:31 +10004300 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004301 if (conf->disks[i].rdev == NULL)
4302 still_degraded = 1;
4303
4304 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4305
4306 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 set_bit(STRIPE_SYNCING, &sh->state);
4308 clear_bit(STRIPE_INSYNC, &sh->state);
4309 spin_unlock(&sh->lock);
4310
NeilBrown14425772009-10-16 15:55:25 +11004311 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 release_stripe(sh);
4313
4314 return STRIPE_SECTORS;
4315}
4316
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004317static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4318{
4319 /* We may not be able to submit a whole bio at once as there
4320 * may not be enough stripe_heads available.
4321 * We cannot pre-allocate enough stripe_heads as we may need
4322 * more than exist in the cache (if we allow ever large chunks).
4323 * So we do one stripe head at a time and record in
4324 * ->bi_hw_segments how many have been done.
4325 *
4326 * We *know* that this entire raid_bio is in one chunk, so
4327 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4328 */
4329 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004330 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004331 sector_t sector, logical_sector, last_sector;
4332 int scnt = 0;
4333 int remaining;
4334 int handled = 0;
4335
4336 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004337 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004338 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004339 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4340
4341 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004342 logical_sector += STRIPE_SECTORS,
4343 sector += STRIPE_SECTORS,
4344 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004345
Jens Axboe960e7392008-08-15 10:41:18 +02004346 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004347 /* already done this stripe */
4348 continue;
4349
NeilBrowna8c906c2009-06-09 14:39:59 +10004350 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004351
4352 if (!sh) {
4353 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004354 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004355 conf->retry_read_aligned = raid_bio;
4356 return handled;
4357 }
4358
4359 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004360 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4361 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004362 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004363 conf->retry_read_aligned = raid_bio;
4364 return handled;
4365 }
4366
Dan Williams36d1c642009-07-14 11:48:22 -07004367 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004368 release_stripe(sh);
4369 handled++;
4370 }
4371 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004372 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004373 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004374 if (remaining == 0)
4375 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004376 if (atomic_dec_and_test(&conf->active_aligned_reads))
4377 wake_up(&conf->wait_for_stripe);
4378 return handled;
4379}
4380
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382/*
4383 * This is our raid5 kernel thread.
4384 *
4385 * We scan the hash table for stripes which can be handled now.
4386 * During the scan, completed stripes are saved for us by the interrupt
4387 * handler, so that they will not have to wait for our next wakeup.
4388 */
NeilBrown6ed30032008-02-06 01:40:00 -08004389static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
4391 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004392 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 int handled;
4394
Dan Williams45b42332007-07-09 11:56:43 -07004395 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
4397 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
4399 handled = 0;
4400 spin_lock_irq(&conf->device_lock);
4401 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004402 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
NeilBrownae3c20c2006-07-10 04:44:17 -07004404 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004405 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004406 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004407 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004408 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004409 conf->seq_write = seq;
4410 activate_bit_delay(conf);
4411 }
4412
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004413 while ((bio = remove_bio_from_retry(conf))) {
4414 int ok;
4415 spin_unlock_irq(&conf->device_lock);
4416 ok = retry_aligned_read(conf, bio);
4417 spin_lock_irq(&conf->device_lock);
4418 if (!ok)
4419 break;
4420 handled++;
4421 }
4422
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004423 sh = __get_priority_stripe(conf);
4424
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004425 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 spin_unlock_irq(&conf->device_lock);
4428
4429 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004430 handle_stripe(sh);
4431 release_stripe(sh);
4432 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
4434 spin_lock_irq(&conf->device_lock);
4435 }
Dan Williams45b42332007-07-09 11:56:43 -07004436 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
4438 spin_unlock_irq(&conf->device_lock);
4439
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004440 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 unplug_slaves(mddev);
4442
Dan Williams45b42332007-07-09 11:56:43 -07004443 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
NeilBrown3f294f42005-11-08 21:39:25 -08004446static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004447raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004448{
NeilBrown070ec552009-06-16 16:54:21 +10004449 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004450 if (conf)
4451 return sprintf(page, "%d\n", conf->max_nr_stripes);
4452 else
4453 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004454}
4455
4456static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004457raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004458{
NeilBrown070ec552009-06-16 16:54:21 +10004459 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004460 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004461 int err;
4462
NeilBrown3f294f42005-11-08 21:39:25 -08004463 if (len >= PAGE_SIZE)
4464 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004465 if (!conf)
4466 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004467
Dan Williams4ef197d82008-04-28 02:15:54 -07004468 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004469 return -EINVAL;
4470 if (new <= 16 || new > 32768)
4471 return -EINVAL;
4472 while (new < conf->max_nr_stripes) {
4473 if (drop_one_stripe(conf))
4474 conf->max_nr_stripes--;
4475 else
4476 break;
4477 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004478 err = md_allow_write(mddev);
4479 if (err)
4480 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004481 while (new > conf->max_nr_stripes) {
4482 if (grow_one_stripe(conf))
4483 conf->max_nr_stripes++;
4484 else break;
4485 }
4486 return len;
4487}
NeilBrown007583c2005-11-08 21:39:30 -08004488
NeilBrown96de1e62005-11-08 21:39:39 -08004489static struct md_sysfs_entry
4490raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4491 raid5_show_stripe_cache_size,
4492 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004493
4494static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004495raid5_show_preread_threshold(mddev_t *mddev, char *page)
4496{
NeilBrown070ec552009-06-16 16:54:21 +10004497 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004498 if (conf)
4499 return sprintf(page, "%d\n", conf->bypass_threshold);
4500 else
4501 return 0;
4502}
4503
4504static ssize_t
4505raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4506{
NeilBrown070ec552009-06-16 16:54:21 +10004507 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004508 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004509 if (len >= PAGE_SIZE)
4510 return -EINVAL;
4511 if (!conf)
4512 return -ENODEV;
4513
Dan Williams4ef197d82008-04-28 02:15:54 -07004514 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004515 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004516 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004517 return -EINVAL;
4518 conf->bypass_threshold = new;
4519 return len;
4520}
4521
4522static struct md_sysfs_entry
4523raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4524 S_IRUGO | S_IWUSR,
4525 raid5_show_preread_threshold,
4526 raid5_store_preread_threshold);
4527
4528static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004529stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004530{
NeilBrown070ec552009-06-16 16:54:21 +10004531 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004532 if (conf)
4533 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4534 else
4535 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004536}
4537
NeilBrown96de1e62005-11-08 21:39:39 -08004538static struct md_sysfs_entry
4539raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004540
NeilBrown007583c2005-11-08 21:39:30 -08004541static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004542 &raid5_stripecache_size.attr,
4543 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004544 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004545 NULL,
4546};
NeilBrown007583c2005-11-08 21:39:30 -08004547static struct attribute_group raid5_attrs_group = {
4548 .name = NULL,
4549 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004550};
4551
Dan Williams80c3a6c2009-03-17 18:10:40 -07004552static sector_t
4553raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4554{
NeilBrown070ec552009-06-16 16:54:21 +10004555 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004556
4557 if (!sectors)
4558 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004559 if (!raid_disks) {
4560 /* size is defined by the smallest of previous and new size */
4561 if (conf->raid_disks < conf->previous_raid_disks)
4562 raid_disks = conf->raid_disks;
4563 else
4564 raid_disks = conf->previous_raid_disks;
4565 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004566
Andre Noll9d8f0362009-06-18 08:45:01 +10004567 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004568 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004569 return sectors * (raid_disks - conf->max_degraded);
4570}
4571
Dan Williams36d1c642009-07-14 11:48:22 -07004572static void raid5_free_percpu(raid5_conf_t *conf)
4573{
4574 struct raid5_percpu *percpu;
4575 unsigned long cpu;
4576
4577 if (!conf->percpu)
4578 return;
4579
4580 get_online_cpus();
4581 for_each_possible_cpu(cpu) {
4582 percpu = per_cpu_ptr(conf->percpu, cpu);
4583 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004584 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004585 }
4586#ifdef CONFIG_HOTPLUG_CPU
4587 unregister_cpu_notifier(&conf->cpu_notify);
4588#endif
4589 put_online_cpus();
4590
4591 free_percpu(conf->percpu);
4592}
4593
Dan Williams95fc17a2009-07-31 12:39:15 +10004594static void free_conf(raid5_conf_t *conf)
4595{
4596 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004597 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004598 kfree(conf->disks);
4599 kfree(conf->stripe_hashtbl);
4600 kfree(conf);
4601}
4602
Dan Williams36d1c642009-07-14 11:48:22 -07004603#ifdef CONFIG_HOTPLUG_CPU
4604static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4605 void *hcpu)
4606{
4607 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4608 long cpu = (long)hcpu;
4609 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4610
4611 switch (action) {
4612 case CPU_UP_PREPARE:
4613 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004614 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004615 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004616 if (!percpu->scribble)
4617 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4618
4619 if (!percpu->scribble ||
4620 (conf->level == 6 && !percpu->spare_page)) {
4621 safe_put_page(percpu->spare_page);
4622 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004623 pr_err("%s: failed memory allocation for cpu%ld\n",
4624 __func__, cpu);
4625 return NOTIFY_BAD;
4626 }
4627 break;
4628 case CPU_DEAD:
4629 case CPU_DEAD_FROZEN:
4630 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004631 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004632 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004633 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004634 break;
4635 default:
4636 break;
4637 }
4638 return NOTIFY_OK;
4639}
4640#endif
4641
4642static int raid5_alloc_percpu(raid5_conf_t *conf)
4643{
4644 unsigned long cpu;
4645 struct page *spare_page;
4646 struct raid5_percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004647 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004648 int err;
4649
Dan Williams36d1c642009-07-14 11:48:22 -07004650 allcpus = alloc_percpu(struct raid5_percpu);
4651 if (!allcpus)
4652 return -ENOMEM;
4653 conf->percpu = allcpus;
4654
4655 get_online_cpus();
4656 err = 0;
4657 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004658 if (conf->level == 6) {
4659 spare_page = alloc_page(GFP_KERNEL);
4660 if (!spare_page) {
4661 err = -ENOMEM;
4662 break;
4663 }
4664 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4665 }
4666 scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL);
4667 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004668 err = -ENOMEM;
4669 break;
4670 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004671 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004672 }
4673#ifdef CONFIG_HOTPLUG_CPU
4674 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4675 conf->cpu_notify.priority = 0;
4676 if (err == 0)
4677 err = register_cpu_notifier(&conf->cpu_notify);
4678#endif
4679 put_online_cpus();
4680
4681 return err;
4682}
4683
NeilBrown91adb562009-03-31 14:39:39 +11004684static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
4686 raid5_conf_t *conf;
4687 int raid_disk, memory;
4688 mdk_rdev_t *rdev;
4689 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
NeilBrown91adb562009-03-31 14:39:39 +11004691 if (mddev->new_level != 5
4692 && mddev->new_level != 4
4693 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004694 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004695 mdname(mddev), mddev->new_level);
4696 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 }
NeilBrown91adb562009-03-31 14:39:39 +11004698 if ((mddev->new_level == 5
4699 && !algorithm_valid_raid5(mddev->new_layout)) ||
4700 (mddev->new_level == 6
4701 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004702 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004703 mdname(mddev), mddev->new_layout);
4704 return ERR_PTR(-EIO);
4705 }
4706 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4707 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4708 mdname(mddev), mddev->raid_disks);
4709 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Andre Noll664e7c42009-06-18 08:45:27 +10004712 if (!mddev->new_chunk_sectors ||
4713 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4714 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown91adb562009-03-31 14:39:39 +11004715 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
Andre Noll664e7c42009-06-18 08:45:27 +10004716 mddev->new_chunk_sectors << 9, mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004717 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004718 }
4719
NeilBrown91adb562009-03-31 14:39:39 +11004720 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4721 if (conf == NULL)
4722 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004723 spin_lock_init(&conf->device_lock);
4724 init_waitqueue_head(&conf->wait_for_stripe);
4725 init_waitqueue_head(&conf->wait_for_overlap);
4726 INIT_LIST_HEAD(&conf->handle_list);
4727 INIT_LIST_HEAD(&conf->hold_list);
4728 INIT_LIST_HEAD(&conf->delayed_list);
4729 INIT_LIST_HEAD(&conf->bitmap_list);
4730 INIT_LIST_HEAD(&conf->inactive_list);
4731 atomic_set(&conf->active_stripes, 0);
4732 atomic_set(&conf->preread_active_stripes, 0);
4733 atomic_set(&conf->active_aligned_reads, 0);
4734 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrown91adb562009-03-31 14:39:39 +11004735
4736 conf->raid_disks = mddev->raid_disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004737 conf->scribble_len = scribble_len(conf->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004738 if (mddev->reshape_position == MaxSector)
4739 conf->previous_raid_disks = mddev->raid_disks;
4740 else
4741 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4742
4743 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4744 GFP_KERNEL);
4745 if (!conf->disks)
4746 goto abort;
4747
4748 conf->mddev = mddev;
4749
4750 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4751 goto abort;
4752
Dan Williams36d1c642009-07-14 11:48:22 -07004753 conf->level = mddev->new_level;
4754 if (raid5_alloc_percpu(conf) != 0)
4755 goto abort;
4756
NeilBrown91adb562009-03-31 14:39:39 +11004757 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4758
4759 list_for_each_entry(rdev, &mddev->disks, same_set) {
4760 raid_disk = rdev->raid_disk;
4761 if (raid_disk >= conf->raid_disks
4762 || raid_disk < 0)
4763 continue;
4764 disk = conf->disks + raid_disk;
4765
4766 disk->rdev = rdev;
4767
4768 if (test_bit(In_sync, &rdev->flags)) {
4769 char b[BDEVNAME_SIZE];
4770 printk(KERN_INFO "raid5: device %s operational as raid"
4771 " disk %d\n", bdevname(rdev->bdev,b),
4772 raid_disk);
4773 } else
4774 /* Cannot rely on bitmap to complete recovery */
4775 conf->fullsync = 1;
4776 }
4777
Andre Noll09c9e5f2009-06-18 08:45:55 +10004778 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004779 conf->level = mddev->new_level;
4780 if (conf->level == 6)
4781 conf->max_degraded = 2;
4782 else
4783 conf->max_degraded = 1;
4784 conf->algorithm = mddev->new_layout;
4785 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004786 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004787 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004788 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004789 conf->prev_algo = mddev->layout;
4790 }
NeilBrown91adb562009-03-31 14:39:39 +11004791
4792 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4793 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4794 if (grow_stripes(conf, conf->max_nr_stripes)) {
4795 printk(KERN_ERR
4796 "raid5: couldn't allocate %dkB for buffers\n", memory);
4797 goto abort;
4798 } else
4799 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4800 memory, mdname(mddev));
4801
NeilBrown0da3c612009-09-23 18:09:45 +10004802 conf->thread = md_register_thread(raid5d, mddev, NULL);
NeilBrown91adb562009-03-31 14:39:39 +11004803 if (!conf->thread) {
4804 printk(KERN_ERR
4805 "raid5: couldn't allocate thread for %s\n",
4806 mdname(mddev));
4807 goto abort;
4808 }
4809
4810 return conf;
4811
4812 abort:
4813 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10004814 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004815 return ERR_PTR(-EIO);
4816 } else
4817 return ERR_PTR(-ENOMEM);
4818}
4819
4820static int run(mddev_t *mddev)
4821{
4822 raid5_conf_t *conf;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004823 int working_disks = 0, chunk_size;
NeilBrown91adb562009-03-31 14:39:39 +11004824 mdk_rdev_t *rdev;
4825
Andre Noll8c6ac8682009-06-18 08:48:06 +10004826 if (mddev->recovery_cp != MaxSector)
4827 printk(KERN_NOTICE "raid5: %s is not clean"
4828 " -- starting background reconstruction\n",
4829 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004830 if (mddev->reshape_position != MaxSector) {
4831 /* Check that we can continue the reshape.
4832 * Currently only disks can change, it must
4833 * increase, and we must be past the point where
4834 * a stripe over-writes itself
4835 */
4836 sector_t here_new, here_old;
4837 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004838 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004839
NeilBrown88ce4932009-03-31 15:24:23 +11004840 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004841 printk(KERN_ERR "raid5: %s: unsupported reshape "
4842 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004843 mdname(mddev));
4844 return -EINVAL;
4845 }
NeilBrownf6705572006-03-27 01:18:11 -08004846 old_disks = mddev->raid_disks - mddev->delta_disks;
4847 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004848 * further up in new geometry must map after here in old
4849 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004850 */
4851 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004852 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004853 (mddev->raid_disks - max_degraded))) {
4854 printk(KERN_ERR "raid5: reshape_position not "
4855 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004856 return -EINVAL;
4857 }
4858 /* here_new is the stripe we will write to */
4859 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004860 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004861 (old_disks-max_degraded));
4862 /* here_old is the first stripe that we might need to read
4863 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004864 if (mddev->delta_disks == 0) {
4865 /* We cannot be sure it is safe to start an in-place
4866 * reshape. It is only safe if user-space if monitoring
4867 * and taking constant backups.
4868 * mdadm always starts a situation like this in
4869 * readonly mode so it can take control before
4870 * allowing any writes. So just check for that.
4871 */
4872 if ((here_new * mddev->new_chunk_sectors !=
4873 here_old * mddev->chunk_sectors) ||
4874 mddev->ro == 0) {
4875 printk(KERN_ERR "raid5: in-place reshape must be started"
4876 " in read-only mode - aborting\n");
4877 return -EINVAL;
4878 }
4879 } else if (mddev->delta_disks < 0
4880 ? (here_new * mddev->new_chunk_sectors <=
4881 here_old * mddev->chunk_sectors)
4882 : (here_new * mddev->new_chunk_sectors >=
4883 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004884 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004885 printk(KERN_ERR "raid5: reshape_position too early for "
4886 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004887 return -EINVAL;
4888 }
4889 printk(KERN_INFO "raid5: reshape will continue\n");
4890 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004891 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004892 BUG_ON(mddev->level != mddev->new_level);
4893 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004894 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004895 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004896 }
4897
NeilBrown245f46c2009-03-31 14:39:39 +11004898 if (mddev->private == NULL)
4899 conf = setup_conf(mddev);
4900 else
4901 conf = mddev->private;
4902
NeilBrown91adb562009-03-31 14:39:39 +11004903 if (IS_ERR(conf))
4904 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004905
NeilBrown91adb562009-03-31 14:39:39 +11004906 mddev->thread = conf->thread;
4907 conf->thread = NULL;
4908 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004911 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 */
NeilBrown91adb562009-03-31 14:39:39 +11004913 list_for_each_entry(rdev, &mddev->disks, same_set)
4914 if (rdev->raid_disk >= 0 &&
4915 test_bit(In_sync, &rdev->flags))
4916 working_disks++;
4917
NeilBrown02c2de82006-10-03 01:15:47 -07004918 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
NeilBrown16a53ec2006-06-26 00:27:38 -07004920 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 printk(KERN_ERR "raid5: not enough operational devices for %s"
4922 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004923 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 goto abort;
4925 }
4926
NeilBrown91adb562009-03-31 14:39:39 +11004927 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004928 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004929 mddev->resync_max_sectors = mddev->dev_sectors;
4930
NeilBrown16a53ec2006-06-26 00:27:38 -07004931 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004933 if (mddev->ok_start_degraded)
4934 printk(KERN_WARNING
4935 "raid5: starting dirty degraded array: %s"
4936 "- data corruption possible.\n",
4937 mdname(mddev));
4938 else {
4939 printk(KERN_ERR
4940 "raid5: cannot start dirty degraded array for %s\n",
4941 mdname(mddev));
4942 goto abort;
4943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 }
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (mddev->degraded == 0)
4947 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11004948 " devices, algorithm %d\n", conf->level, mdname(mddev),
4949 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4950 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 else
4952 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4953 " out of %d devices, algorithm %d\n", conf->level,
4954 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11004955 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
4957 print_raid5_conf(conf);
4958
NeilBrownfef9c612009-03-31 15:16:46 +11004959 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08004960 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11004961 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004962 atomic_set(&conf->reshape_stripes, 0);
4963 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4964 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4965 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4966 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4967 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004968 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004969 }
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004972 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 */
4974 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004975 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4976 int stripe = data_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10004977 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4979 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4980 }
4981
4982 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004983 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4984 printk(KERN_WARNING
4985 "raid5: failed to create sysfs attributes for %s\n",
4986 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004987
NeilBrown91adb562009-03-31 14:39:39 +11004988 mddev->queue->queue_lock = &conf->device_lock;
4989
NeilBrown7a5febe2005-05-16 21:53:16 -07004990 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004991 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004992 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004993
Dan Williams1f403622009-03-31 14:59:03 +11004994 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07004995
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004996 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004997 chunk_size = mddev->chunk_sectors << 9;
4998 blk_queue_io_min(mddev->queue, chunk_size);
4999 blk_queue_io_opt(mddev->queue, chunk_size *
5000 (conf->raid_disks - conf->max_degraded));
5001
5002 list_for_each_entry(rdev, &mddev->disks, same_set)
5003 disk_stack_limits(mddev->gendisk, rdev->bdev,
5004 rdev->data_offset << 9);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 return 0;
5007abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11005008 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11005009 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 if (conf) {
5011 print_raid5_conf(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005012 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 }
5014 mddev->private = NULL;
5015 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
5016 return -EIO;
5017}
5018
5019
5020
NeilBrown3f294f42005-11-08 21:39:25 -08005021static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
5023 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5024
5025 md_unregister_thread(mddev->thread);
5026 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08005027 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08005029 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
Dan Williams95fc17a2009-07-31 12:39:15 +10005030 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 mddev->private = NULL;
5032 return 0;
5033}
5034
Dan Williams45b42332007-07-09 11:56:43 -07005035#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11005036static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
5038 int i;
5039
NeilBrown16a53ec2006-06-26 00:27:38 -07005040 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
5041 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
5042 seq_printf(seq, "sh %llu, count %d.\n",
5043 (unsigned long long)sh->sector, atomic_read(&sh->count));
5044 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005045 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07005046 seq_printf(seq, "(cache%d: %p %ld) ",
5047 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005049 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050}
5051
NeilBrownd710e132008-10-13 11:55:12 +11005052static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053{
5054 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08005055 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 int i;
5057
5058 spin_lock_irq(&conf->device_lock);
5059 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005060 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 if (sh->raid_conf != conf)
5062 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005063 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 }
5065 }
5066 spin_unlock_irq(&conf->device_lock);
5067}
5068#endif
5069
NeilBrownd710e132008-10-13 11:55:12 +11005070static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071{
5072 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5073 int i;
5074
Andre Noll9d8f0362009-06-18 08:45:01 +10005075 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5076 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005077 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 for (i = 0; i < conf->raid_disks; i++)
5079 seq_printf (seq, "%s",
5080 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005081 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005083#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005084 seq_printf (seq, "\n");
5085 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086#endif
5087}
5088
5089static void print_raid5_conf (raid5_conf_t *conf)
5090{
5091 int i;
5092 struct disk_info *tmp;
5093
5094 printk("RAID5 conf printout:\n");
5095 if (!conf) {
5096 printk("(conf==NULL)\n");
5097 return;
5098 }
NeilBrown02c2de82006-10-03 01:15:47 -07005099 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
5100 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 for (i = 0; i < conf->raid_disks; i++) {
5103 char b[BDEVNAME_SIZE];
5104 tmp = conf->disks + i;
5105 if (tmp->rdev)
5106 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08005107 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 bdevname(tmp->rdev->bdev,b));
5109 }
5110}
5111
5112static int raid5_spare_active(mddev_t *mddev)
5113{
5114 int i;
5115 raid5_conf_t *conf = mddev->private;
5116 struct disk_info *tmp;
5117
5118 for (i = 0; i < conf->raid_disks; i++) {
5119 tmp = conf->disks + i;
5120 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08005121 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005122 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5123 unsigned long flags;
5124 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07005126 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 }
5128 }
5129 print_raid5_conf(conf);
5130 return 0;
5131}
5132
5133static int raid5_remove_disk(mddev_t *mddev, int number)
5134{
5135 raid5_conf_t *conf = mddev->private;
5136 int err = 0;
5137 mdk_rdev_t *rdev;
5138 struct disk_info *p = conf->disks + number;
5139
5140 print_raid5_conf(conf);
5141 rdev = p->rdev;
5142 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005143 if (number >= conf->raid_disks &&
5144 conf->reshape_progress == MaxSector)
5145 clear_bit(In_sync, &rdev->flags);
5146
NeilBrownb2d444d2005-11-08 21:39:31 -08005147 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 atomic_read(&rdev->nr_pending)) {
5149 err = -EBUSY;
5150 goto abort;
5151 }
NeilBrowndfc70642008-05-23 13:04:39 -07005152 /* Only remove non-faulty devices if recovery
5153 * isn't possible.
5154 */
5155 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005156 mddev->degraded <= conf->max_degraded &&
5157 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005158 err = -EBUSY;
5159 goto abort;
5160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005162 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 if (atomic_read(&rdev->nr_pending)) {
5164 /* lost the race, try later */
5165 err = -EBUSY;
5166 p->rdev = rdev;
5167 }
5168 }
5169abort:
5170
5171 print_raid5_conf(conf);
5172 return err;
5173}
5174
5175static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5176{
5177 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005178 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 int disk;
5180 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005181 int first = 0;
5182 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
NeilBrown16a53ec2006-06-26 00:27:38 -07005184 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
Neil Brown6c2fce22008-06-28 08:31:31 +10005188 if (rdev->raid_disk >= 0)
5189 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
5191 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005192 * find the disk ... but prefer rdev->saved_raid_disk
5193 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005195 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005196 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005197 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5198 disk = rdev->saved_raid_disk;
5199 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005200 disk = first;
5201 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005203 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005205 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005206 if (rdev->saved_raid_disk != disk)
5207 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005208 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 break;
5210 }
5211 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213}
5214
5215static int raid5_resize(mddev_t *mddev, sector_t sectors)
5216{
5217 /* no resync is happening, and there is enough space
5218 * on all devices, so we can resize.
5219 * We need to make sure resync covers any new space.
5220 * If the array is shrinking we should possibly wait until
5221 * any io in the removed space completes, but it hardly seems
5222 * worth it.
5223 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005224 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005225 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5226 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005227 if (mddev->array_sectors >
5228 raid5_size(mddev, sectors, mddev->raid_disks))
5229 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005230 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07005231 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005232 revalidate_disk(mddev->gendisk);
Andre Noll58c0fed2009-03-31 14:33:13 +11005233 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5234 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5236 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005237 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005238 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 return 0;
5240}
5241
NeilBrown01ee22b2009-06-18 08:47:20 +10005242static int check_stripe_cache(mddev_t *mddev)
5243{
5244 /* Can only proceed if there are plenty of stripe_heads.
5245 * We need a minimum of one full stripe,, and for sensible progress
5246 * it is best to have about 4 times that.
5247 * If we require 4 times, then the default 256 4K stripe_heads will
5248 * allow for chunk sizes up to 256K, which is probably OK.
5249 * If the chunk size is greater, user-space should request more
5250 * stripe_heads first.
5251 */
5252 raid5_conf_t *conf = mddev->private;
5253 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5254 > conf->max_nr_stripes ||
5255 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5256 > conf->max_nr_stripes) {
5257 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
5258 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5259 / STRIPE_SIZE)*4);
5260 return 0;
5261 }
5262 return 1;
5263}
5264
NeilBrown50ac1682009-06-18 08:47:55 +10005265static int check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005266{
NeilBrown070ec552009-06-16 16:54:21 +10005267 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005268
NeilBrown88ce4932009-03-31 15:24:23 +11005269 if (mddev->delta_disks == 0 &&
5270 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005271 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005272 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005273 if (mddev->bitmap)
5274 /* Cannot grow a bitmap yet */
5275 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11005276 if (mddev->degraded > conf->max_degraded)
5277 return -EINVAL;
5278 if (mddev->delta_disks < 0) {
5279 /* We might be able to shrink, but the devices must
5280 * be made bigger first.
5281 * For raid6, 4 is the minimum size.
5282 * Otherwise 2 is the minimum
5283 */
5284 int min = 2;
5285 if (mddev->level == 6)
5286 min = 4;
5287 if (mddev->raid_disks + mddev->delta_disks < min)
5288 return -EINVAL;
5289 }
NeilBrown29269552006-03-27 01:18:10 -08005290
NeilBrown01ee22b2009-06-18 08:47:20 +10005291 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005292 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005293
NeilBrownec32a2b2009-03-31 15:17:38 +11005294 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005295}
5296
5297static int raid5_start_reshape(mddev_t *mddev)
5298{
NeilBrown070ec552009-06-16 16:54:21 +10005299 raid5_conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08005300 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005301 int spares = 0;
5302 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005303 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005304
NeilBrownf4168852007-02-28 20:11:53 -08005305 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005306 return -EBUSY;
5307
NeilBrown01ee22b2009-06-18 08:47:20 +10005308 if (!check_stripe_cache(mddev))
5309 return -ENOSPC;
5310
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005311 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005312 if (rdev->raid_disk < 0 &&
5313 !test_bit(Faulty, &rdev->flags))
5314 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005315
NeilBrownf4168852007-02-28 20:11:53 -08005316 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005317 /* Not enough devices even to make a degraded array
5318 * of that size
5319 */
5320 return -EINVAL;
5321
NeilBrownec32a2b2009-03-31 15:17:38 +11005322 /* Refuse to reduce size of the array. Any reductions in
5323 * array size must be through explicit setting of array_size
5324 * attribute.
5325 */
5326 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5327 < mddev->array_sectors) {
5328 printk(KERN_ERR "md: %s: array size must be reduced "
5329 "before number of disks\n", mdname(mddev));
5330 return -EINVAL;
5331 }
5332
NeilBrownf6705572006-03-27 01:18:11 -08005333 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005334 spin_lock_irq(&conf->device_lock);
5335 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005336 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005337 conf->prev_chunk_sectors = conf->chunk_sectors;
5338 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005339 conf->prev_algo = conf->algorithm;
5340 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005341 if (mddev->delta_disks < 0)
5342 conf->reshape_progress = raid5_size(mddev, 0, 0);
5343 else
5344 conf->reshape_progress = 0;
5345 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005346 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005347 spin_unlock_irq(&conf->device_lock);
5348
5349 /* Add some new drives, as many as will fit.
5350 * We know there are enough to make the newly sized array work.
5351 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005352 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005353 if (rdev->raid_disk < 0 &&
5354 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005355 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005356 char nm[20];
5357 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08005358 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005359 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005360 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005361 if (sysfs_create_link(&mddev->kobj,
5362 &rdev->kobj, nm))
5363 printk(KERN_WARNING
5364 "raid5: failed to create "
5365 " link %s for %s\n",
5366 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005367 } else
5368 break;
5369 }
5370
NeilBrownec32a2b2009-03-31 15:17:38 +11005371 if (mddev->delta_disks > 0) {
5372 spin_lock_irqsave(&conf->device_lock, flags);
5373 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
5374 - added_devices;
5375 spin_unlock_irqrestore(&conf->device_lock, flags);
5376 }
NeilBrown63c70c42006-03-27 01:18:13 -08005377 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005378 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005379 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005380
NeilBrown29269552006-03-27 01:18:10 -08005381 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5382 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5383 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5384 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5385 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005386 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005387 if (!mddev->sync_thread) {
5388 mddev->recovery = 0;
5389 spin_lock_irq(&conf->device_lock);
5390 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005391 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005392 spin_unlock_irq(&conf->device_lock);
5393 return -EAGAIN;
5394 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005395 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005396 md_wakeup_thread(mddev->sync_thread);
5397 md_new_event(mddev);
5398 return 0;
5399}
NeilBrown29269552006-03-27 01:18:10 -08005400
NeilBrownec32a2b2009-03-31 15:17:38 +11005401/* This is called from the reshape thread and should make any
5402 * changes needed in 'conf'
5403 */
NeilBrown29269552006-03-27 01:18:10 -08005404static void end_reshape(raid5_conf_t *conf)
5405{
NeilBrown29269552006-03-27 01:18:10 -08005406
NeilBrownf6705572006-03-27 01:18:11 -08005407 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005408
NeilBrownf6705572006-03-27 01:18:11 -08005409 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005410 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005411 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005412 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005413 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005414
5415 /* read-ahead size must cover two whole stripes, which is
5416 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5417 */
5418 {
NeilBrowncea9c222009-03-31 15:15:05 +11005419 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005420 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005421 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005422 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5423 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5424 }
NeilBrown29269552006-03-27 01:18:10 -08005425 }
NeilBrown29269552006-03-27 01:18:10 -08005426}
5427
NeilBrownec32a2b2009-03-31 15:17:38 +11005428/* This is called from the raid5d thread with mddev_lock held.
5429 * It makes config changes to the device.
5430 */
NeilBrowncea9c222009-03-31 15:15:05 +11005431static void raid5_finish_reshape(mddev_t *mddev)
5432{
NeilBrown070ec552009-06-16 16:54:21 +10005433 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005434
5435 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5436
NeilBrownec32a2b2009-03-31 15:17:38 +11005437 if (mddev->delta_disks > 0) {
5438 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5439 set_capacity(mddev->gendisk, mddev->array_sectors);
5440 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005441 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005442 } else {
5443 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005444 mddev->degraded = conf->raid_disks;
5445 for (d = 0; d < conf->raid_disks ; d++)
5446 if (conf->disks[d].rdev &&
5447 test_bit(In_sync,
5448 &conf->disks[d].rdev->flags))
5449 mddev->degraded--;
5450 for (d = conf->raid_disks ;
5451 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005452 d++) {
5453 mdk_rdev_t *rdev = conf->disks[d].rdev;
5454 if (rdev && raid5_remove_disk(mddev, d) == 0) {
5455 char nm[20];
5456 sprintf(nm, "rd%d", rdev->raid_disk);
5457 sysfs_remove_link(&mddev->kobj, nm);
5458 rdev->raid_disk = -1;
5459 }
5460 }
NeilBrowncea9c222009-03-31 15:15:05 +11005461 }
NeilBrown88ce4932009-03-31 15:24:23 +11005462 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005463 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005464 mddev->reshape_position = MaxSector;
5465 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005466 }
5467}
5468
NeilBrown72626682005-09-09 16:23:54 -07005469static void raid5_quiesce(mddev_t *mddev, int state)
5470{
NeilBrown070ec552009-06-16 16:54:21 +10005471 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005472
5473 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005474 case 2: /* resume for a suspend */
5475 wake_up(&conf->wait_for_overlap);
5476 break;
5477
NeilBrown72626682005-09-09 16:23:54 -07005478 case 1: /* stop all writes */
5479 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005480 /* '2' tells resync/reshape to pause so that all
5481 * active stripes can drain
5482 */
5483 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005484 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005485 atomic_read(&conf->active_stripes) == 0 &&
5486 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005487 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005488 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005489 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005490 /* allow reshape to continue */
5491 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005492 break;
5493
5494 case 0: /* re-enable writes */
5495 spin_lock_irq(&conf->device_lock);
5496 conf->quiesce = 0;
5497 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005498 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005499 spin_unlock_irq(&conf->device_lock);
5500 break;
5501 }
NeilBrown72626682005-09-09 16:23:54 -07005502}
NeilBrownb15c2e52006-01-06 00:20:16 -08005503
NeilBrownd562b0c2009-03-31 14:39:39 +11005504
5505static void *raid5_takeover_raid1(mddev_t *mddev)
5506{
5507 int chunksect;
5508
5509 if (mddev->raid_disks != 2 ||
5510 mddev->degraded > 1)
5511 return ERR_PTR(-EINVAL);
5512
5513 /* Should check if there are write-behind devices? */
5514
5515 chunksect = 64*2; /* 64K by default */
5516
5517 /* The array must be an exact multiple of chunksize */
5518 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5519 chunksect >>= 1;
5520
5521 if ((chunksect<<9) < STRIPE_SIZE)
5522 /* array size does not allow a suitable chunk size */
5523 return ERR_PTR(-EINVAL);
5524
5525 mddev->new_level = 5;
5526 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005527 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005528
5529 return setup_conf(mddev);
5530}
5531
NeilBrownfc9739c2009-03-31 14:57:20 +11005532static void *raid5_takeover_raid6(mddev_t *mddev)
5533{
5534 int new_layout;
5535
5536 switch (mddev->layout) {
5537 case ALGORITHM_LEFT_ASYMMETRIC_6:
5538 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5539 break;
5540 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5541 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5542 break;
5543 case ALGORITHM_LEFT_SYMMETRIC_6:
5544 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5545 break;
5546 case ALGORITHM_RIGHT_SYMMETRIC_6:
5547 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5548 break;
5549 case ALGORITHM_PARITY_0_6:
5550 new_layout = ALGORITHM_PARITY_0;
5551 break;
5552 case ALGORITHM_PARITY_N:
5553 new_layout = ALGORITHM_PARITY_N;
5554 break;
5555 default:
5556 return ERR_PTR(-EINVAL);
5557 }
5558 mddev->new_level = 5;
5559 mddev->new_layout = new_layout;
5560 mddev->delta_disks = -1;
5561 mddev->raid_disks -= 1;
5562 return setup_conf(mddev);
5563}
5564
NeilBrownd562b0c2009-03-31 14:39:39 +11005565
NeilBrown50ac1682009-06-18 08:47:55 +10005566static int raid5_check_reshape(mddev_t *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005567{
NeilBrown88ce4932009-03-31 15:24:23 +11005568 /* For a 2-drive array, the layout and chunk size can be changed
5569 * immediately as not restriping is needed.
5570 * For larger arrays we record the new value - after validation
5571 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005572 */
NeilBrown070ec552009-06-16 16:54:21 +10005573 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005574 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005575
NeilBrown597a7112009-06-18 08:47:42 +10005576 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005577 return -EINVAL;
5578 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005579 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005580 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005581 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005582 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005583 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005584 /* not factor of array size */
5585 return -EINVAL;
5586 }
5587
5588 /* They look valid */
5589
NeilBrown88ce4932009-03-31 15:24:23 +11005590 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005591 /* can make the change immediately */
5592 if (mddev->new_layout >= 0) {
5593 conf->algorithm = mddev->new_layout;
5594 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005595 }
5596 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005597 conf->chunk_sectors = new_chunk ;
5598 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005599 }
5600 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5601 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005602 }
NeilBrown50ac1682009-06-18 08:47:55 +10005603 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005604}
5605
NeilBrown50ac1682009-06-18 08:47:55 +10005606static int raid6_check_reshape(mddev_t *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005607{
NeilBrown597a7112009-06-18 08:47:42 +10005608 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005609
NeilBrown597a7112009-06-18 08:47:42 +10005610 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005611 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005612 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005613 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005614 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005615 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005616 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005617 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005618 /* not factor of array size */
5619 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005620 }
NeilBrown88ce4932009-03-31 15:24:23 +11005621
5622 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005623 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005624}
5625
NeilBrownd562b0c2009-03-31 14:39:39 +11005626static void *raid5_takeover(mddev_t *mddev)
5627{
5628 /* raid5 can take over:
5629 * raid0 - if all devices are the same - make it a raid4 layout
5630 * raid1 - if there are two drives. We need to know the chunk size
5631 * raid4 - trivial - just use a raid4 layout.
5632 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005633 */
5634
5635 if (mddev->level == 1)
5636 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005637 if (mddev->level == 4) {
5638 mddev->new_layout = ALGORITHM_PARITY_N;
5639 mddev->new_level = 5;
5640 return setup_conf(mddev);
5641 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005642 if (mddev->level == 6)
5643 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005644
5645 return ERR_PTR(-EINVAL);
5646}
5647
5648
NeilBrown245f46c2009-03-31 14:39:39 +11005649static struct mdk_personality raid5_personality;
5650
5651static void *raid6_takeover(mddev_t *mddev)
5652{
5653 /* Currently can only take over a raid5. We map the
5654 * personality to an equivalent raid6 personality
5655 * with the Q block at the end.
5656 */
5657 int new_layout;
5658
5659 if (mddev->pers != &raid5_personality)
5660 return ERR_PTR(-EINVAL);
5661 if (mddev->degraded > 1)
5662 return ERR_PTR(-EINVAL);
5663 if (mddev->raid_disks > 253)
5664 return ERR_PTR(-EINVAL);
5665 if (mddev->raid_disks < 3)
5666 return ERR_PTR(-EINVAL);
5667
5668 switch (mddev->layout) {
5669 case ALGORITHM_LEFT_ASYMMETRIC:
5670 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5671 break;
5672 case ALGORITHM_RIGHT_ASYMMETRIC:
5673 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5674 break;
5675 case ALGORITHM_LEFT_SYMMETRIC:
5676 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5677 break;
5678 case ALGORITHM_RIGHT_SYMMETRIC:
5679 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5680 break;
5681 case ALGORITHM_PARITY_0:
5682 new_layout = ALGORITHM_PARITY_0_6;
5683 break;
5684 case ALGORITHM_PARITY_N:
5685 new_layout = ALGORITHM_PARITY_N;
5686 break;
5687 default:
5688 return ERR_PTR(-EINVAL);
5689 }
5690 mddev->new_level = 6;
5691 mddev->new_layout = new_layout;
5692 mddev->delta_disks = 1;
5693 mddev->raid_disks += 1;
5694 return setup_conf(mddev);
5695}
5696
5697
NeilBrown16a53ec2006-06-26 00:27:38 -07005698static struct mdk_personality raid6_personality =
5699{
5700 .name = "raid6",
5701 .level = 6,
5702 .owner = THIS_MODULE,
5703 .make_request = make_request,
5704 .run = run,
5705 .stop = stop,
5706 .status = status,
5707 .error_handler = error,
5708 .hot_add_disk = raid5_add_disk,
5709 .hot_remove_disk= raid5_remove_disk,
5710 .spare_active = raid5_spare_active,
5711 .sync_request = sync_request,
5712 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005713 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005714 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005715 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005716 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005717 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005718 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005719};
NeilBrown2604b702006-01-06 00:20:36 -08005720static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721{
5722 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005723 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 .owner = THIS_MODULE,
5725 .make_request = make_request,
5726 .run = run,
5727 .stop = stop,
5728 .status = status,
5729 .error_handler = error,
5730 .hot_add_disk = raid5_add_disk,
5731 .hot_remove_disk= raid5_remove_disk,
5732 .spare_active = raid5_spare_active,
5733 .sync_request = sync_request,
5734 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005735 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005736 .check_reshape = raid5_check_reshape,
5737 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005738 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005739 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005740 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741};
5742
NeilBrown2604b702006-01-06 00:20:36 -08005743static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744{
NeilBrown2604b702006-01-06 00:20:36 -08005745 .name = "raid4",
5746 .level = 4,
5747 .owner = THIS_MODULE,
5748 .make_request = make_request,
5749 .run = run,
5750 .stop = stop,
5751 .status = status,
5752 .error_handler = error,
5753 .hot_add_disk = raid5_add_disk,
5754 .hot_remove_disk= raid5_remove_disk,
5755 .spare_active = raid5_spare_active,
5756 .sync_request = sync_request,
5757 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005758 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005759 .check_reshape = raid5_check_reshape,
5760 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005761 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005762 .quiesce = raid5_quiesce,
5763};
5764
5765static int __init raid5_init(void)
5766{
NeilBrown16a53ec2006-06-26 00:27:38 -07005767 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005768 register_md_personality(&raid5_personality);
5769 register_md_personality(&raid4_personality);
5770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771}
5772
NeilBrown2604b702006-01-06 00:20:36 -08005773static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774{
NeilBrown16a53ec2006-06-26 00:27:38 -07005775 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005776 unregister_md_personality(&raid5_personality);
5777 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778}
5779
5780module_init(raid5_init);
5781module_exit(raid5_exit);
5782MODULE_LICENSE("GPL");
5783MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005784MODULE_ALIAS("md-raid5");
5785MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005786MODULE_ALIAS("md-level-5");
5787MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005788MODULE_ALIAS("md-personality-8"); /* RAID6 */
5789MODULE_ALIAS("md-raid6");
5790MODULE_ALIAS("md-level-6");
5791
5792/* This used to be two separate modules, they were: */
5793MODULE_ALIAS("raid5");
5794MODULE_ALIAS("raid6");