blob: 0a5f03d93aef34fa066a71000152932962a9a6cb [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 Harrisone46b2722008-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 Harrisone46b2722008-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 Harrisone46b2722008-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 Harrisone46b2722008-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 Harrisone46b2722008-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
813 /* we need to open-code set_syndrome_sources to handle to the
814 * 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 }
882 }
883
Dan Williams0403e382009-09-08 17:42:50 -0700884 init_async_submit(&submit, ASYNC_TX_FENCE, NULL, ops_complete_compute,
885 sh, to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700886 if (failb == syndrome_disks) {
887 /* We're missing D+P. */
888 return async_raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE,
889 faila, blocks, &submit);
890 } else {
891 /* We're missing D+D. */
892 return async_raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE,
893 faila, failb, blocks, &submit);
894 }
895}
896
897
Dan Williams91c00922007-01-02 13:52:30 -0700898static void ops_complete_prexor(void *stripe_head_ref)
899{
900 struct stripe_head *sh = stripe_head_ref;
901
Harvey Harrisone46b2722008-04-28 02:15:50 -0700902 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700903 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700904}
905
906static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700907ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
908 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700909{
Dan Williams91c00922007-01-02 13:52:30 -0700910 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700911 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700912 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700913 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700914
915 /* existing parity data subtracted */
916 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
917
Harvey Harrisone46b2722008-04-28 02:15:50 -0700918 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700919 (unsigned long long)sh->sector);
920
921 for (i = disks; i--; ) {
922 struct r5dev *dev = &sh->dev[i];
923 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000924 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700925 xor_srcs[count++] = dev->page;
926 }
927
Dan Williams0403e382009-09-08 17:42:50 -0700928 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700929 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700930 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700931
932 return tx;
933}
934
935static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000936ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700937{
938 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000939 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700940
Harvey Harrisone46b2722008-04-28 02:15:50 -0700941 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700942 (unsigned long long)sh->sector);
943
944 for (i = disks; i--; ) {
945 struct r5dev *dev = &sh->dev[i];
946 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700947
Dan Williamsd8ee0722008-06-28 08:32:06 +1000948 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700949 struct bio *wbi;
950
951 spin_lock(&sh->lock);
952 chosen = dev->towrite;
953 dev->towrite = NULL;
954 BUG_ON(dev->written);
955 wbi = dev->written = chosen;
956 spin_unlock(&sh->lock);
957
958 while (wbi && wbi->bi_sector <
959 dev->sector + STRIPE_SECTORS) {
960 tx = async_copy_data(1, wbi, dev->page,
961 dev->sector, tx);
962 wbi = r5_next_bio(wbi, dev->sector);
963 }
964 }
965 }
966
967 return tx;
968}
969
Dan Williamsac6b53b2009-07-14 13:40:19 -0700970static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700971{
972 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700973 int disks = sh->disks;
974 int pd_idx = sh->pd_idx;
975 int qd_idx = sh->qd_idx;
976 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700977
Harvey Harrisone46b2722008-04-28 02:15:50 -0700978 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700979 (unsigned long long)sh->sector);
980
981 for (i = disks; i--; ) {
982 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -0700983
984 if (dev->written || i == pd_idx || i == qd_idx)
Dan Williams91c00922007-01-02 13:52:30 -0700985 set_bit(R5_UPTODATE, &dev->flags);
986 }
987
Dan Williamsd8ee0722008-06-28 08:32:06 +1000988 if (sh->reconstruct_state == reconstruct_state_drain_run)
989 sh->reconstruct_state = reconstruct_state_drain_result;
990 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
991 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
992 else {
993 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
994 sh->reconstruct_state = reconstruct_state_result;
995 }
Dan Williams91c00922007-01-02 13:52:30 -0700996
997 set_bit(STRIPE_HANDLE, &sh->state);
998 release_stripe(sh);
999}
1000
1001static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001002ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1003 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001004{
Dan Williams91c00922007-01-02 13:52:30 -07001005 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001006 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001007 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001008 int count = 0, pd_idx = sh->pd_idx, i;
1009 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001010 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001011 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001012
Harvey Harrisone46b2722008-04-28 02:15:50 -07001013 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001014 (unsigned long long)sh->sector);
1015
1016 /* check if prexor is active which means only process blocks
1017 * that are part of a read-modify-write (written)
1018 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001019 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1020 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001021 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1022 for (i = disks; i--; ) {
1023 struct r5dev *dev = &sh->dev[i];
1024 if (dev->written)
1025 xor_srcs[count++] = dev->page;
1026 }
1027 } else {
1028 xor_dest = sh->dev[pd_idx].page;
1029 for (i = disks; i--; ) {
1030 struct r5dev *dev = &sh->dev[i];
1031 if (i != pd_idx)
1032 xor_srcs[count++] = dev->page;
1033 }
1034 }
1035
Dan Williams91c00922007-01-02 13:52:30 -07001036 /* 1/ if we prexor'd then the dest is reused as a source
1037 * 2/ if we did not prexor then we are redoing the parity
1038 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1039 * for the synchronous xor case
1040 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001041 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001042 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1043
1044 atomic_inc(&sh->count);
1045
Dan Williamsac6b53b2009-07-14 13:40:19 -07001046 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001047 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001048 if (unlikely(count == 1))
1049 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1050 else
1051 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001052}
1053
Dan Williamsac6b53b2009-07-14 13:40:19 -07001054static void
1055ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1056 struct dma_async_tx_descriptor *tx)
1057{
1058 struct async_submit_ctl submit;
1059 struct page **blocks = percpu->scribble;
1060 int count;
1061
1062 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1063
1064 count = set_syndrome_sources(blocks, sh);
1065
1066 atomic_inc(&sh->count);
1067
1068 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1069 sh, to_addr_conv(sh, percpu));
1070 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001071}
1072
1073static void ops_complete_check(void *stripe_head_ref)
1074{
1075 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001076
Harvey Harrisone46b2722008-04-28 02:15:50 -07001077 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001078 (unsigned long long)sh->sector);
1079
Dan Williamsecc65c92008-06-28 08:31:57 +10001080 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001081 set_bit(STRIPE_HANDLE, &sh->state);
1082 release_stripe(sh);
1083}
1084
Dan Williamsac6b53b2009-07-14 13:40:19 -07001085static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001086{
Dan Williams91c00922007-01-02 13:52:30 -07001087 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088 int pd_idx = sh->pd_idx;
1089 int qd_idx = sh->qd_idx;
1090 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001091 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001092 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001093 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001094 int count;
1095 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001096
Harvey Harrisone46b2722008-04-28 02:15:50 -07001097 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001098 (unsigned long long)sh->sector);
1099
Dan Williamsac6b53b2009-07-14 13:40:19 -07001100 count = 0;
1101 xor_dest = sh->dev[pd_idx].page;
1102 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001103 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001104 if (i == pd_idx || i == qd_idx)
1105 continue;
1106 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001107 }
1108
Dan Williamsd6f38f32009-07-14 11:50:52 -07001109 init_async_submit(&submit, 0, NULL, NULL, NULL,
1110 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001111 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001112 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001113
Dan Williams91c00922007-01-02 13:52:30 -07001114 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001115 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1116 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001117}
1118
Dan Williamsac6b53b2009-07-14 13:40:19 -07001119static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1120{
1121 struct page **srcs = percpu->scribble;
1122 struct async_submit_ctl submit;
1123 int count;
1124
1125 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1126 (unsigned long long)sh->sector, checkp);
1127
1128 count = set_syndrome_sources(srcs, sh);
1129 if (!checkp)
1130 srcs[count] = NULL;
1131
1132 atomic_inc(&sh->count);
1133 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1134 sh, to_addr_conv(sh, percpu));
1135 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1136 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1137}
1138
1139static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001140{
1141 int overlap_clear = 0, i, disks = sh->disks;
1142 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001143 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001144 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001145 struct raid5_percpu *percpu;
1146 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001147
Dan Williamsd6f38f32009-07-14 11:50:52 -07001148 cpu = get_cpu();
1149 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001150 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001151 ops_run_biofill(sh);
1152 overlap_clear++;
1153 }
1154
Dan Williams7b3a8712008-06-28 08:32:09 +10001155 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001156 if (level < 6)
1157 tx = ops_run_compute5(sh, percpu);
1158 else {
1159 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1160 tx = ops_run_compute6_1(sh, percpu);
1161 else
1162 tx = ops_run_compute6_2(sh, percpu);
1163 }
1164 /* terminate the chain if reconstruct is not set to be run */
1165 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001166 async_tx_ack(tx);
1167 }
Dan Williams91c00922007-01-02 13:52:30 -07001168
Dan Williams600aa102008-06-28 08:32:05 +10001169 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001170 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001171
Dan Williams600aa102008-06-28 08:32:05 +10001172 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001173 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001174 overlap_clear++;
1175 }
1176
Dan Williamsac6b53b2009-07-14 13:40:19 -07001177 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1178 if (level < 6)
1179 ops_run_reconstruct5(sh, percpu, tx);
1180 else
1181 ops_run_reconstruct6(sh, percpu, tx);
1182 }
Dan Williams91c00922007-01-02 13:52:30 -07001183
Dan Williamsac6b53b2009-07-14 13:40:19 -07001184 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1185 if (sh->check_state == check_state_run)
1186 ops_run_check_p(sh, percpu);
1187 else if (sh->check_state == check_state_run_q)
1188 ops_run_check_pq(sh, percpu, 0);
1189 else if (sh->check_state == check_state_run_pq)
1190 ops_run_check_pq(sh, percpu, 1);
1191 else
1192 BUG();
1193 }
Dan Williams91c00922007-01-02 13:52:30 -07001194
Dan Williams91c00922007-01-02 13:52:30 -07001195 if (overlap_clear)
1196 for (i = disks; i--; ) {
1197 struct r5dev *dev = &sh->dev[i];
1198 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1199 wake_up(&sh->raid_conf->wait_for_overlap);
1200 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001201 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001202}
1203
NeilBrown3f294f42005-11-08 21:39:25 -08001204static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -08001207 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1208 if (!sh)
1209 return 0;
1210 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
1211 sh->raid_conf = conf;
1212 spin_lock_init(&sh->lock);
1213
1214 if (grow_buffers(sh, conf->raid_disks)) {
1215 shrink_buffers(sh, conf->raid_disks);
1216 kmem_cache_free(conf->slab_cache, sh);
1217 return 0;
1218 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001219 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -08001220 /* we just created an active stripe so... */
1221 atomic_set(&sh->count, 1);
1222 atomic_inc(&conf->active_stripes);
1223 INIT_LIST_HEAD(&sh->lru);
1224 release_stripe(sh);
1225 return 1;
1226}
1227
1228static int grow_stripes(raid5_conf_t *conf, int num)
1229{
Christoph Lametere18b8902006-12-06 20:33:20 -08001230 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int devs = conf->raid_disks;
1232
NeilBrown245f46c2009-03-31 14:39:39 +11001233 sprintf(conf->cache_name[0],
1234 "raid%d-%s", conf->level, mdname(conf->mddev));
1235 sprintf(conf->cache_name[1],
1236 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -08001237 conf->active_name = 0;
1238 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001240 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (!sc)
1242 return 1;
1243 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001244 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001245 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001246 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return 0;
1249}
NeilBrown29269552006-03-27 01:18:10 -08001250
Dan Williamsd6f38f32009-07-14 11:50:52 -07001251/**
1252 * scribble_len - return the required size of the scribble region
1253 * @num - total number of disks in the array
1254 *
1255 * The size must be enough to contain:
1256 * 1/ a struct page pointer for each device in the array +2
1257 * 2/ room to convert each entry in (1) to its corresponding dma
1258 * (dma_map_page()) or page (page_address()) address.
1259 *
1260 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1261 * calculate over all devices (not just the data blocks), using zeros in place
1262 * of the P and Q blocks.
1263 */
1264static size_t scribble_len(int num)
1265{
1266 size_t len;
1267
1268 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1269
1270 return len;
1271}
1272
NeilBrownad01c9e2006-03-27 01:18:07 -08001273static int resize_stripes(raid5_conf_t *conf, int newsize)
1274{
1275 /* Make all the stripes able to hold 'newsize' devices.
1276 * New slots in each stripe get 'page' set to a new page.
1277 *
1278 * This happens in stages:
1279 * 1/ create a new kmem_cache and allocate the required number of
1280 * stripe_heads.
1281 * 2/ gather all the old stripe_heads and tranfer the pages across
1282 * to the new stripe_heads. This will have the side effect of
1283 * freezing the array as once all stripe_heads have been collected,
1284 * no IO will be possible. Old stripe heads are freed once their
1285 * pages have been transferred over, and the old kmem_cache is
1286 * freed when all stripes are done.
1287 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1288 * we simple return a failre status - no need to clean anything up.
1289 * 4/ allocate new pages for the new slots in the new stripe_heads.
1290 * If this fails, we don't bother trying the shrink the
1291 * stripe_heads down again, we just leave them as they are.
1292 * As each stripe_head is processed the new one is released into
1293 * active service.
1294 *
1295 * Once step2 is started, we cannot afford to wait for a write,
1296 * so we use GFP_NOIO allocations.
1297 */
1298 struct stripe_head *osh, *nsh;
1299 LIST_HEAD(newstripes);
1300 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001301 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001302 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001303 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001304 int i;
1305
1306 if (newsize <= conf->pool_size)
1307 return 0; /* never bother to shrink */
1308
Dan Williamsb5470dc2008-06-27 21:44:04 -07001309 err = md_allow_write(conf->mddev);
1310 if (err)
1311 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001312
NeilBrownad01c9e2006-03-27 01:18:07 -08001313 /* Step 1 */
1314 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1315 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001316 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001317 if (!sc)
1318 return -ENOMEM;
1319
1320 for (i = conf->max_nr_stripes; i; i--) {
1321 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1322 if (!nsh)
1323 break;
1324
1325 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1326
1327 nsh->raid_conf = conf;
1328 spin_lock_init(&nsh->lock);
1329
1330 list_add(&nsh->lru, &newstripes);
1331 }
1332 if (i) {
1333 /* didn't get enough, give up */
1334 while (!list_empty(&newstripes)) {
1335 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1336 list_del(&nsh->lru);
1337 kmem_cache_free(sc, nsh);
1338 }
1339 kmem_cache_destroy(sc);
1340 return -ENOMEM;
1341 }
1342 /* Step 2 - Must use GFP_NOIO now.
1343 * OK, we have enough stripes, start collecting inactive
1344 * stripes and copying them over
1345 */
1346 list_for_each_entry(nsh, &newstripes, lru) {
1347 spin_lock_irq(&conf->device_lock);
1348 wait_event_lock_irq(conf->wait_for_stripe,
1349 !list_empty(&conf->inactive_list),
1350 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001351 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001352 );
1353 osh = get_free_stripe(conf);
1354 spin_unlock_irq(&conf->device_lock);
1355 atomic_set(&nsh->count, 1);
1356 for(i=0; i<conf->pool_size; i++)
1357 nsh->dev[i].page = osh->dev[i].page;
1358 for( ; i<newsize; i++)
1359 nsh->dev[i].page = NULL;
1360 kmem_cache_free(conf->slab_cache, osh);
1361 }
1362 kmem_cache_destroy(conf->slab_cache);
1363
1364 /* Step 3.
1365 * At this point, we are holding all the stripes so the array
1366 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001367 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001368 */
1369 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1370 if (ndisks) {
1371 for (i=0; i<conf->raid_disks; i++)
1372 ndisks[i] = conf->disks[i];
1373 kfree(conf->disks);
1374 conf->disks = ndisks;
1375 } else
1376 err = -ENOMEM;
1377
Dan Williamsd6f38f32009-07-14 11:50:52 -07001378 get_online_cpus();
1379 conf->scribble_len = scribble_len(newsize);
1380 for_each_present_cpu(cpu) {
1381 struct raid5_percpu *percpu;
1382 void *scribble;
1383
1384 percpu = per_cpu_ptr(conf->percpu, cpu);
1385 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1386
1387 if (scribble) {
1388 kfree(percpu->scribble);
1389 percpu->scribble = scribble;
1390 } else {
1391 err = -ENOMEM;
1392 break;
1393 }
1394 }
1395 put_online_cpus();
1396
NeilBrownad01c9e2006-03-27 01:18:07 -08001397 /* Step 4, return new stripes to service */
1398 while(!list_empty(&newstripes)) {
1399 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1400 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001401
NeilBrownad01c9e2006-03-27 01:18:07 -08001402 for (i=conf->raid_disks; i < newsize; i++)
1403 if (nsh->dev[i].page == NULL) {
1404 struct page *p = alloc_page(GFP_NOIO);
1405 nsh->dev[i].page = p;
1406 if (!p)
1407 err = -ENOMEM;
1408 }
1409 release_stripe(nsh);
1410 }
1411 /* critical section pass, GFP_NOIO no longer needed */
1412
1413 conf->slab_cache = sc;
1414 conf->active_name = 1-conf->active_name;
1415 conf->pool_size = newsize;
1416 return err;
1417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
NeilBrown3f294f42005-11-08 21:39:25 -08001419static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 struct stripe_head *sh;
1422
NeilBrown3f294f42005-11-08 21:39:25 -08001423 spin_lock_irq(&conf->device_lock);
1424 sh = get_free_stripe(conf);
1425 spin_unlock_irq(&conf->device_lock);
1426 if (!sh)
1427 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001428 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001429 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001430 kmem_cache_free(conf->slab_cache, sh);
1431 atomic_dec(&conf->active_stripes);
1432 return 1;
1433}
1434
1435static void shrink_stripes(raid5_conf_t *conf)
1436{
1437 while (drop_one_stripe(conf))
1438 ;
1439
NeilBrown29fc7e32006-02-03 03:03:41 -08001440 if (conf->slab_cache)
1441 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 conf->slab_cache = NULL;
1443}
1444
NeilBrown6712ecf2007-09-27 12:47:43 +02001445static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
NeilBrown99c0fb52009-03-31 14:39:38 +11001447 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001449 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001451 char b[BDEVNAME_SIZE];
1452 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 for (i=0 ; i<disks; i++)
1456 if (bi == &sh->dev[i].req)
1457 break;
1458
Dan Williams45b42332007-07-09 11:56:43 -07001459 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1460 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 uptodate);
1462 if (i == disks) {
1463 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001464 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466
1467 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001469 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001470 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001471 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1472 " (%lu sectors at %llu on %s)\n",
1473 mdname(conf->mddev), STRIPE_SECTORS,
1474 (unsigned long long)(sh->sector
1475 + rdev->data_offset),
1476 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001477 clear_bit(R5_ReadError, &sh->dev[i].flags);
1478 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1479 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001480 if (atomic_read(&conf->disks[i].rdev->read_errors))
1481 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001483 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001484 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001485 rdev = conf->disks[i].rdev;
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001488 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001489 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001490 printk_rl(KERN_WARNING
1491 "raid5:%s: read error not correctable "
1492 "(sector %llu on %s).\n",
1493 mdname(conf->mddev),
1494 (unsigned long long)(sh->sector
1495 + rdev->data_offset),
1496 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001497 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001498 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001499 printk_rl(KERN_WARNING
1500 "raid5:%s: read error NOT corrected!! "
1501 "(sector %llu on %s).\n",
1502 mdname(conf->mddev),
1503 (unsigned long long)(sh->sector
1504 + rdev->data_offset),
1505 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001506 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001507 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001508 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001509 "raid5:%s: Too many read errors, failing device %s.\n",
1510 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001511 else
1512 retry = 1;
1513 if (retry)
1514 set_bit(R5_ReadError, &sh->dev[i].flags);
1515 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001516 clear_bit(R5_ReadError, &sh->dev[i].flags);
1517 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001518 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1523 set_bit(STRIPE_HANDLE, &sh->state);
1524 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
NeilBrownd710e132008-10-13 11:55:12 +11001527static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
NeilBrown99c0fb52009-03-31 14:39:38 +11001529 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001531 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 for (i=0 ; i<disks; i++)
1535 if (bi == &sh->dev[i].req)
1536 break;
1537
Dan Williams45b42332007-07-09 11:56:43 -07001538 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1540 uptodate);
1541 if (i == disks) {
1542 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (!uptodate)
1547 md_error(conf->mddev, conf->disks[i].rdev);
1548
1549 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1550
1551 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1552 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001553 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556
NeilBrown784052e2009-03-31 15:19:07 +11001557static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
NeilBrown784052e2009-03-31 15:19:07 +11001559static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 struct r5dev *dev = &sh->dev[i];
1562
1563 bio_init(&dev->req);
1564 dev->req.bi_io_vec = &dev->vec;
1565 dev->req.bi_vcnt++;
1566 dev->req.bi_max_vecs++;
1567 dev->vec.bv_page = dev->page;
1568 dev->vec.bv_len = STRIPE_SIZE;
1569 dev->vec.bv_offset = 0;
1570
1571 dev->req.bi_sector = sh->sector;
1572 dev->req.bi_private = sh;
1573
1574 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001575 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1579{
1580 char b[BDEVNAME_SIZE];
1581 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001582 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
NeilBrownb2d444d2005-11-08 21:39:31 -08001584 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b42006-10-03 01:15:46 -07001585 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001586 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1587 unsigned long flags;
1588 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001590 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * if recovery was running, make sure it aborts.
1593 */
NeilBrowndfc70642008-05-23 13:04:39 -07001594 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001596 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001597 printk(KERN_ALERT
1598 "raid5: Disk failure on %s, disabling device.\n"
1599 "raid5: Operation continuing on %d devices.\n",
1600 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604/*
1605 * Input: a 'big' sector number,
1606 * Output: index of the data and parity disk, and the sector # in them.
1607 */
NeilBrown112bf892009-03-31 14:39:38 +11001608static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001609 int previous, int *dd_idx,
1610 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 long stripe;
1613 unsigned long chunk_number;
1614 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001615 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001616 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001618 int algorithm = previous ? conf->prev_algo
1619 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001620 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1621 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001622 int raid_disks = previous ? conf->previous_raid_disks
1623 : conf->raid_disks;
1624 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /* First compute the information on this sector */
1627
1628 /*
1629 * Compute the chunk number and the sector offset inside the chunk
1630 */
1631 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1632 chunk_number = r_sector;
1633 BUG_ON(r_sector != chunk_number);
1634
1635 /*
1636 * Compute the stripe number
1637 */
1638 stripe = chunk_number / data_disks;
1639
1640 /*
1641 * Compute the data disk and parity disk indexes inside the stripe
1642 */
1643 *dd_idx = chunk_number % data_disks;
1644
1645 /*
1646 * Select the parity disk based on the user selected algorithm.
1647 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001648 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001649 switch(conf->level) {
1650 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001651 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001652 break;
1653 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001654 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001656 pd_idx = data_disks - stripe % raid_disks;
1657 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 (*dd_idx)++;
1659 break;
1660 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001661 pd_idx = stripe % raid_disks;
1662 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 (*dd_idx)++;
1664 break;
1665 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001666 pd_idx = data_disks - stripe % raid_disks;
1667 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
1669 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001670 pd_idx = stripe % raid_disks;
1671 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001673 case ALGORITHM_PARITY_0:
1674 pd_idx = 0;
1675 (*dd_idx)++;
1676 break;
1677 case ALGORITHM_PARITY_N:
1678 pd_idx = data_disks;
1679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001681 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001682 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001683 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001684 }
1685 break;
1686 case 6:
1687
NeilBrowne183eae2009-03-31 15:20:22 +11001688 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001689 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001690 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1691 qd_idx = pd_idx + 1;
1692 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001693 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001694 qd_idx = 0;
1695 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001696 (*dd_idx) += 2; /* D D P Q D */
1697 break;
1698 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001699 pd_idx = stripe % raid_disks;
1700 qd_idx = pd_idx + 1;
1701 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001702 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001703 qd_idx = 0;
1704 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001705 (*dd_idx) += 2; /* D D P Q D */
1706 break;
1707 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001708 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1709 qd_idx = (pd_idx + 1) % raid_disks;
1710 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001711 break;
1712 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001713 pd_idx = stripe % raid_disks;
1714 qd_idx = (pd_idx + 1) % raid_disks;
1715 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001716 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001717
1718 case ALGORITHM_PARITY_0:
1719 pd_idx = 0;
1720 qd_idx = 1;
1721 (*dd_idx) += 2;
1722 break;
1723 case ALGORITHM_PARITY_N:
1724 pd_idx = data_disks;
1725 qd_idx = data_disks + 1;
1726 break;
1727
1728 case ALGORITHM_ROTATING_ZERO_RESTART:
1729 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1730 * of blocks for computing Q is different.
1731 */
1732 pd_idx = stripe % raid_disks;
1733 qd_idx = pd_idx + 1;
1734 if (pd_idx == raid_disks-1) {
1735 (*dd_idx)++; /* Q D D D P */
1736 qd_idx = 0;
1737 } else if (*dd_idx >= pd_idx)
1738 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001739 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001740 break;
1741
1742 case ALGORITHM_ROTATING_N_RESTART:
1743 /* Same a left_asymmetric, by first stripe is
1744 * D D D P Q rather than
1745 * Q D D D P
1746 */
1747 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1748 qd_idx = pd_idx + 1;
1749 if (pd_idx == raid_disks-1) {
1750 (*dd_idx)++; /* Q D D D P */
1751 qd_idx = 0;
1752 } else if (*dd_idx >= pd_idx)
1753 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001754 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001755 break;
1756
1757 case ALGORITHM_ROTATING_N_CONTINUE:
1758 /* Same as left_symmetric but Q is before P */
1759 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1760 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1761 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001762 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001763 break;
1764
1765 case ALGORITHM_LEFT_ASYMMETRIC_6:
1766 /* RAID5 left_asymmetric, with Q on last device */
1767 pd_idx = data_disks - stripe % (raid_disks-1);
1768 if (*dd_idx >= pd_idx)
1769 (*dd_idx)++;
1770 qd_idx = raid_disks - 1;
1771 break;
1772
1773 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1774 pd_idx = stripe % (raid_disks-1);
1775 if (*dd_idx >= pd_idx)
1776 (*dd_idx)++;
1777 qd_idx = raid_disks - 1;
1778 break;
1779
1780 case ALGORITHM_LEFT_SYMMETRIC_6:
1781 pd_idx = data_disks - stripe % (raid_disks-1);
1782 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1783 qd_idx = raid_disks - 1;
1784 break;
1785
1786 case ALGORITHM_RIGHT_SYMMETRIC_6:
1787 pd_idx = stripe % (raid_disks-1);
1788 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1789 qd_idx = raid_disks - 1;
1790 break;
1791
1792 case ALGORITHM_PARITY_0_6:
1793 pd_idx = 0;
1794 (*dd_idx)++;
1795 qd_idx = raid_disks - 1;
1796 break;
1797
1798
NeilBrown16a53ec2006-06-26 00:27:38 -07001799 default:
NeilBrownd710e132008-10-13 11:55:12 +11001800 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001801 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001802 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001803 }
1804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
NeilBrown911d4ee2009-03-31 14:39:38 +11001807 if (sh) {
1808 sh->pd_idx = pd_idx;
1809 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001810 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * Finally, compute the new sector number
1814 */
1815 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1816 return new_sector;
1817}
1818
1819
NeilBrown784052e2009-03-31 15:19:07 +11001820static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
1822 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001823 int raid_disks = sh->disks;
1824 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001826 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1827 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001828 int algorithm = previous ? conf->prev_algo
1829 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 sector_t stripe;
1831 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001832 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001834 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
NeilBrown16a53ec2006-06-26 00:27:38 -07001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1838 stripe = new_sector;
1839 BUG_ON(new_sector != stripe);
1840
NeilBrown16a53ec2006-06-26 00:27:38 -07001841 if (i == sh->pd_idx)
1842 return 0;
1843 switch(conf->level) {
1844 case 4: break;
1845 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001846 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 case ALGORITHM_LEFT_ASYMMETRIC:
1848 case ALGORITHM_RIGHT_ASYMMETRIC:
1849 if (i > sh->pd_idx)
1850 i--;
1851 break;
1852 case ALGORITHM_LEFT_SYMMETRIC:
1853 case ALGORITHM_RIGHT_SYMMETRIC:
1854 if (i < sh->pd_idx)
1855 i += raid_disks;
1856 i -= (sh->pd_idx + 1);
1857 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001858 case ALGORITHM_PARITY_0:
1859 i -= 1;
1860 break;
1861 case ALGORITHM_PARITY_N:
1862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001864 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001865 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001866 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001867 }
1868 break;
1869 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001870 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001871 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001872 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001873 case ALGORITHM_LEFT_ASYMMETRIC:
1874 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001875 case ALGORITHM_ROTATING_ZERO_RESTART:
1876 case ALGORITHM_ROTATING_N_RESTART:
1877 if (sh->pd_idx == raid_disks-1)
1878 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001879 else if (i > sh->pd_idx)
1880 i -= 2; /* D D P Q D */
1881 break;
1882 case ALGORITHM_LEFT_SYMMETRIC:
1883 case ALGORITHM_RIGHT_SYMMETRIC:
1884 if (sh->pd_idx == raid_disks-1)
1885 i--; /* Q D D D P */
1886 else {
1887 /* D D P Q D */
1888 if (i < sh->pd_idx)
1889 i += raid_disks;
1890 i -= (sh->pd_idx + 2);
1891 }
1892 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001893 case ALGORITHM_PARITY_0:
1894 i -= 2;
1895 break;
1896 case ALGORITHM_PARITY_N:
1897 break;
1898 case ALGORITHM_ROTATING_N_CONTINUE:
1899 if (sh->pd_idx == 0)
1900 i--; /* P D D D Q */
1901 else if (i > sh->pd_idx)
1902 i -= 2; /* D D Q P D */
1903 break;
1904 case ALGORITHM_LEFT_ASYMMETRIC_6:
1905 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1906 if (i > sh->pd_idx)
1907 i--;
1908 break;
1909 case ALGORITHM_LEFT_SYMMETRIC_6:
1910 case ALGORITHM_RIGHT_SYMMETRIC_6:
1911 if (i < sh->pd_idx)
1912 i += data_disks + 1;
1913 i -= (sh->pd_idx + 1);
1914 break;
1915 case ALGORITHM_PARITY_0_6:
1916 i -= 1;
1917 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001918 default:
NeilBrownd710e132008-10-13 11:55:12 +11001919 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001920 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001921 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001922 }
1923 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
1926 chunk_number = stripe * data_disks + i;
1927 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1928
NeilBrown112bf892009-03-31 14:39:38 +11001929 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001930 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001931 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1932 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001933 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return 0;
1935 }
1936 return r_sector;
1937}
1938
1939
Dan Williams600aa102008-06-28 08:32:05 +10001940static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001941schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001942 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001943{
1944 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001945 raid5_conf_t *conf = sh->raid_conf;
1946 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07001947
Dan Williamse33129d2007-01-02 13:52:30 -07001948 if (rcw) {
1949 /* if we are not expanding this is a proper write request, and
1950 * there will be bios with new data to be drained into the
1951 * stripe cache
1952 */
1953 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001954 sh->reconstruct_state = reconstruct_state_drain_run;
1955 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1956 } else
1957 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001958
Dan Williamsac6b53b2009-07-14 13:40:19 -07001959 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001960
1961 for (i = disks; i--; ) {
1962 struct r5dev *dev = &sh->dev[i];
1963
1964 if (dev->towrite) {
1965 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001966 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001967 if (!expand)
1968 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001969 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001970 }
1971 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001972 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001973 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001974 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001975 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001976 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07001977 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1978 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1979
Dan Williamsd8ee0722008-06-28 08:32:06 +10001980 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001981 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1982 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001983 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001984
1985 for (i = disks; i--; ) {
1986 struct r5dev *dev = &sh->dev[i];
1987 if (i == pd_idx)
1988 continue;
1989
Dan Williamse33129d2007-01-02 13:52:30 -07001990 if (dev->towrite &&
1991 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001992 test_bit(R5_Wantcompute, &dev->flags))) {
1993 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001994 set_bit(R5_LOCKED, &dev->flags);
1995 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001996 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001997 }
1998 }
1999 }
2000
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002001 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002002 * are in flight
2003 */
2004 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2005 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002006 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002007
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002008 if (level == 6) {
2009 int qd_idx = sh->qd_idx;
2010 struct r5dev *dev = &sh->dev[qd_idx];
2011
2012 set_bit(R5_LOCKED, &dev->flags);
2013 clear_bit(R5_UPTODATE, &dev->flags);
2014 s->locked++;
2015 }
2016
Dan Williams600aa102008-06-28 08:32:05 +10002017 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002018 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002019 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002020}
NeilBrown16a53ec2006-06-26 00:27:38 -07002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022/*
2023 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002024 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 * The bi_next chain must be in order.
2026 */
2027static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2028{
2029 struct bio **bip;
2030 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002031 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Dan Williams45b42332007-07-09 11:56:43 -07002033 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 (unsigned long long)bi->bi_sector,
2035 (unsigned long long)sh->sector);
2036
2037
2038 spin_lock(&sh->lock);
2039 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002040 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002042 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2043 firstwrite = 1;
2044 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 bip = &sh->dev[dd_idx].toread;
2046 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2047 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2048 goto overlap;
2049 bip = & (*bip)->bi_next;
2050 }
2051 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2052 goto overlap;
2053
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002054 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (*bip)
2056 bi->bi_next = *bip;
2057 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002058 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 spin_unlock_irq(&conf->device_lock);
2060 spin_unlock(&sh->lock);
2061
Dan Williams45b42332007-07-09 11:56:43 -07002062 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 (unsigned long long)bi->bi_sector,
2064 (unsigned long long)sh->sector, dd_idx);
2065
NeilBrown72626682005-09-09 16:23:54 -07002066 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002067 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2068 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002069 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002070 set_bit(STRIPE_BIT_DELAY, &sh->state);
2071 }
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (forwrite) {
2074 /* check if page is covered */
2075 sector_t sector = sh->dev[dd_idx].sector;
2076 for (bi=sh->dev[dd_idx].towrite;
2077 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2078 bi && bi->bi_sector <= sector;
2079 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2080 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2081 sector = bi->bi_sector + (bi->bi_size>>9);
2082 }
2083 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2084 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2085 }
2086 return 1;
2087
2088 overlap:
2089 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2090 spin_unlock_irq(&conf->device_lock);
2091 spin_unlock(&sh->lock);
2092 return 0;
2093}
2094
NeilBrown29269552006-03-27 01:18:10 -08002095static void end_reshape(raid5_conf_t *conf);
2096
NeilBrown911d4ee2009-03-31 14:39:38 +11002097static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2098 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002099{
NeilBrown784052e2009-03-31 15:19:07 +11002100 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002101 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002102 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002103 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002104 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002105
NeilBrown112bf892009-03-31 14:39:38 +11002106 raid5_compute_sector(conf,
2107 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002108 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002109 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002110 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002111}
2112
Dan Williamsa4456852007-07-09 11:56:43 -07002113static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002114handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002115 struct stripe_head_state *s, int disks,
2116 struct bio **return_bi)
2117{
2118 int i;
2119 for (i = disks; i--; ) {
2120 struct bio *bi;
2121 int bitmap_end = 0;
2122
2123 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2124 mdk_rdev_t *rdev;
2125 rcu_read_lock();
2126 rdev = rcu_dereference(conf->disks[i].rdev);
2127 if (rdev && test_bit(In_sync, &rdev->flags))
2128 /* multiple read failures in one stripe */
2129 md_error(conf->mddev, rdev);
2130 rcu_read_unlock();
2131 }
2132 spin_lock_irq(&conf->device_lock);
2133 /* fail all writes first */
2134 bi = sh->dev[i].towrite;
2135 sh->dev[i].towrite = NULL;
2136 if (bi) {
2137 s->to_write--;
2138 bitmap_end = 1;
2139 }
2140
2141 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2142 wake_up(&conf->wait_for_overlap);
2143
2144 while (bi && bi->bi_sector <
2145 sh->dev[i].sector + STRIPE_SECTORS) {
2146 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2147 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002148 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002149 md_write_end(conf->mddev);
2150 bi->bi_next = *return_bi;
2151 *return_bi = bi;
2152 }
2153 bi = nextbi;
2154 }
2155 /* and fail all 'written' */
2156 bi = sh->dev[i].written;
2157 sh->dev[i].written = NULL;
2158 if (bi) bitmap_end = 1;
2159 while (bi && bi->bi_sector <
2160 sh->dev[i].sector + STRIPE_SECTORS) {
2161 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2162 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002163 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002164 md_write_end(conf->mddev);
2165 bi->bi_next = *return_bi;
2166 *return_bi = bi;
2167 }
2168 bi = bi2;
2169 }
2170
Dan Williamsb5e98d62007-01-02 13:52:31 -07002171 /* fail any reads if this device is non-operational and
2172 * the data has not reached the cache yet.
2173 */
2174 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2175 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2176 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002177 bi = sh->dev[i].toread;
2178 sh->dev[i].toread = NULL;
2179 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2180 wake_up(&conf->wait_for_overlap);
2181 if (bi) s->to_read--;
2182 while (bi && bi->bi_sector <
2183 sh->dev[i].sector + STRIPE_SECTORS) {
2184 struct bio *nextbi =
2185 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 bi->bi_next = *return_bi;
2189 *return_bi = bi;
2190 }
2191 bi = nextbi;
2192 }
2193 }
2194 spin_unlock_irq(&conf->device_lock);
2195 if (bitmap_end)
2196 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2197 STRIPE_SECTORS, 0, 0);
2198 }
2199
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002200 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2201 if (atomic_dec_and_test(&conf->pending_full_writes))
2202 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002203}
2204
Dan Williams1fe797e2008-06-28 09:16:30 +10002205/* fetch_block5 - checks the given member device to see if its data needs
2206 * to be read or computed to satisfy a request.
2207 *
2208 * Returns 1 when no more member devices need to be checked, otherwise returns
2209 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002210 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002211static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2212 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002213{
2214 struct r5dev *dev = &sh->dev[disk_idx];
2215 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2216
Dan Williamsf38e1212007-01-02 13:52:30 -07002217 /* is the data in this block needed, and can we get it? */
2218 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002219 !test_bit(R5_UPTODATE, &dev->flags) &&
2220 (dev->toread ||
2221 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2222 s->syncing || s->expanding ||
2223 (s->failed &&
2224 (failed_dev->toread ||
2225 (failed_dev->towrite &&
2226 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002227 /* We would like to get this block, possibly by computing it,
2228 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002229 */
2230 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002231 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002232 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2233 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002234 set_bit(R5_Wantcompute, &dev->flags);
2235 sh->ops.target = disk_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002236 sh->ops.target2 = -1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002237 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002238 /* Careful: from this point on 'uptodate' is in the eye
Dan Williamsac6b53b2009-07-14 13:40:19 -07002239 * of raid_run_ops which services 'compute' operations
Dan Williamsf38e1212007-01-02 13:52:30 -07002240 * before writes. R5_Wantcompute flags a block that will
2241 * be R5_UPTODATE by the time it is needed for a
2242 * subsequent operation.
2243 */
2244 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002245 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002246 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002247 set_bit(R5_LOCKED, &dev->flags);
2248 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002249 s->locked++;
2250 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2251 s->syncing);
2252 }
2253 }
2254
Dan Williams1fe797e2008-06-28 09:16:30 +10002255 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002256}
2257
Dan Williams1fe797e2008-06-28 09:16:30 +10002258/**
2259 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2260 */
2261static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002262 struct stripe_head_state *s, int disks)
2263{
2264 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002265
Dan Williamsf38e1212007-01-02 13:52:30 -07002266 /* look for blocks to read/compute, skip this if a compute
2267 * is already in flight, or if the stripe contents are in the
2268 * midst of changing due to a write
2269 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002270 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002271 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002272 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002273 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002274 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002275 set_bit(STRIPE_HANDLE, &sh->state);
2276}
2277
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002278/* fetch_block6 - checks the given member device to see if its data needs
2279 * to be read or computed to satisfy a request.
2280 *
2281 * Returns 1 when no more member devices need to be checked, otherwise returns
2282 * 0 to tell the loop in handle_stripe_fill6 to continue
2283 */
2284static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2285 struct r6_state *r6s, int disk_idx, int disks)
2286{
2287 struct r5dev *dev = &sh->dev[disk_idx];
2288 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2289 &sh->dev[r6s->failed_num[1]] };
2290
2291 if (!test_bit(R5_LOCKED, &dev->flags) &&
2292 !test_bit(R5_UPTODATE, &dev->flags) &&
2293 (dev->toread ||
2294 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2295 s->syncing || s->expanding ||
2296 (s->failed >= 1 &&
2297 (fdev[0]->toread || s->to_write)) ||
2298 (s->failed >= 2 &&
2299 (fdev[1]->toread || s->to_write)))) {
2300 /* we would like to get this block, possibly by computing it,
2301 * otherwise read it if the backing disk is insync
2302 */
2303 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2304 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2305 if ((s->uptodate == disks - 1) &&
2306 (s->failed && (disk_idx == r6s->failed_num[0] ||
2307 disk_idx == r6s->failed_num[1]))) {
2308 /* have disk failed, and we're requested to fetch it;
2309 * do compute it
2310 */
2311 pr_debug("Computing stripe %llu block %d\n",
2312 (unsigned long long)sh->sector, disk_idx);
2313 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2314 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2315 set_bit(R5_Wantcompute, &dev->flags);
2316 sh->ops.target = disk_idx;
2317 sh->ops.target2 = -1; /* no 2nd target */
2318 s->req_compute = 1;
2319 s->uptodate++;
2320 return 1;
2321 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2322 /* Computing 2-failure is *very* expensive; only
2323 * do it if failed >= 2
2324 */
2325 int other;
2326 for (other = disks; other--; ) {
2327 if (other == disk_idx)
2328 continue;
2329 if (!test_bit(R5_UPTODATE,
2330 &sh->dev[other].flags))
2331 break;
2332 }
2333 BUG_ON(other < 0);
2334 pr_debug("Computing stripe %llu blocks %d,%d\n",
2335 (unsigned long long)sh->sector,
2336 disk_idx, other);
2337 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2338 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2339 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2340 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2341 sh->ops.target = disk_idx;
2342 sh->ops.target2 = other;
2343 s->uptodate += 2;
2344 s->req_compute = 1;
2345 return 1;
2346 } else if (test_bit(R5_Insync, &dev->flags)) {
2347 set_bit(R5_LOCKED, &dev->flags);
2348 set_bit(R5_Wantread, &dev->flags);
2349 s->locked++;
2350 pr_debug("Reading block %d (sync=%d)\n",
2351 disk_idx, s->syncing);
2352 }
2353 }
2354
2355 return 0;
2356}
2357
2358/**
2359 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2360 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002361static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002362 struct stripe_head_state *s, struct r6_state *r6s,
2363 int disks)
2364{
2365 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002366
2367 /* look for blocks to read/compute, skip this if a compute
2368 * is already in flight, or if the stripe contents are in the
2369 * midst of changing due to a write
2370 */
2371 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2372 !sh->reconstruct_state)
2373 for (i = disks; i--; )
2374 if (fetch_block6(sh, s, r6s, i, disks))
2375 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002376 set_bit(STRIPE_HANDLE, &sh->state);
2377}
2378
2379
Dan Williams1fe797e2008-06-28 09:16:30 +10002380/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002381 * any written block on an uptodate or failed drive can be returned.
2382 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2383 * never LOCKED, so we don't need to test 'failed' directly.
2384 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002385static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002386 struct stripe_head *sh, int disks, struct bio **return_bi)
2387{
2388 int i;
2389 struct r5dev *dev;
2390
2391 for (i = disks; i--; )
2392 if (sh->dev[i].written) {
2393 dev = &sh->dev[i];
2394 if (!test_bit(R5_LOCKED, &dev->flags) &&
2395 test_bit(R5_UPTODATE, &dev->flags)) {
2396 /* We can return any write requests */
2397 struct bio *wbi, *wbi2;
2398 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002399 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002400 spin_lock_irq(&conf->device_lock);
2401 wbi = dev->written;
2402 dev->written = NULL;
2403 while (wbi && wbi->bi_sector <
2404 dev->sector + STRIPE_SECTORS) {
2405 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002406 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002407 md_write_end(conf->mddev);
2408 wbi->bi_next = *return_bi;
2409 *return_bi = wbi;
2410 }
2411 wbi = wbi2;
2412 }
2413 if (dev->towrite == NULL)
2414 bitmap_end = 1;
2415 spin_unlock_irq(&conf->device_lock);
2416 if (bitmap_end)
2417 bitmap_endwrite(conf->mddev->bitmap,
2418 sh->sector,
2419 STRIPE_SECTORS,
2420 !test_bit(STRIPE_DEGRADED, &sh->state),
2421 0);
2422 }
2423 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002424
2425 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2426 if (atomic_dec_and_test(&conf->pending_full_writes))
2427 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002428}
2429
Dan Williams1fe797e2008-06-28 09:16:30 +10002430static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002431 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2432{
2433 int rmw = 0, rcw = 0, i;
2434 for (i = disks; i--; ) {
2435 /* would I have to read this buffer for read_modify_write */
2436 struct r5dev *dev = &sh->dev[i];
2437 if ((dev->towrite || i == sh->pd_idx) &&
2438 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002439 !(test_bit(R5_UPTODATE, &dev->flags) ||
2440 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002441 if (test_bit(R5_Insync, &dev->flags))
2442 rmw++;
2443 else
2444 rmw += 2*disks; /* cannot read it */
2445 }
2446 /* Would I have to read this buffer for reconstruct_write */
2447 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2448 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002449 !(test_bit(R5_UPTODATE, &dev->flags) ||
2450 test_bit(R5_Wantcompute, &dev->flags))) {
2451 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002452 else
2453 rcw += 2*disks;
2454 }
2455 }
Dan Williams45b42332007-07-09 11:56:43 -07002456 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002457 (unsigned long long)sh->sector, rmw, rcw);
2458 set_bit(STRIPE_HANDLE, &sh->state);
2459 if (rmw < rcw && rmw > 0)
2460 /* prefer read-modify-write, but need to get some data */
2461 for (i = disks; i--; ) {
2462 struct r5dev *dev = &sh->dev[i];
2463 if ((dev->towrite || i == sh->pd_idx) &&
2464 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002465 !(test_bit(R5_UPTODATE, &dev->flags) ||
2466 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002467 test_bit(R5_Insync, &dev->flags)) {
2468 if (
2469 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002470 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002471 "%d for r-m-w\n", i);
2472 set_bit(R5_LOCKED, &dev->flags);
2473 set_bit(R5_Wantread, &dev->flags);
2474 s->locked++;
2475 } else {
2476 set_bit(STRIPE_DELAYED, &sh->state);
2477 set_bit(STRIPE_HANDLE, &sh->state);
2478 }
2479 }
2480 }
2481 if (rcw <= rmw && rcw > 0)
2482 /* want reconstruct write, but need to get some data */
2483 for (i = disks; i--; ) {
2484 struct r5dev *dev = &sh->dev[i];
2485 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2486 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)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002490 test_bit(R5_Insync, &dev->flags)) {
2491 if (
2492 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002493 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002494 "%d for Reconstruct\n", i);
2495 set_bit(R5_LOCKED, &dev->flags);
2496 set_bit(R5_Wantread, &dev->flags);
2497 s->locked++;
2498 } else {
2499 set_bit(STRIPE_DELAYED, &sh->state);
2500 set_bit(STRIPE_HANDLE, &sh->state);
2501 }
2502 }
2503 }
2504 /* now if nothing is locked, and if we have enough data,
2505 * we can start a write request
2506 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002507 /* since handle_stripe can be called at any time we need to handle the
2508 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002509 * subsequent call wants to start a write request. raid_run_ops only
2510 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002511 * simultaneously. If this is not the case then new writes need to be
2512 * held off until the compute completes.
2513 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002514 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2515 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2516 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002517 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002518}
2519
Dan Williams1fe797e2008-06-28 09:16:30 +10002520static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002521 struct stripe_head *sh, struct stripe_head_state *s,
2522 struct r6_state *r6s, int disks)
2523{
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002524 int rcw = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002525 int qd_idx = sh->qd_idx;
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002526
2527 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002528 for (i = disks; i--; ) {
2529 struct r5dev *dev = &sh->dev[i];
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002530 /* check if we haven't enough data */
2531 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2532 i != pd_idx && i != qd_idx &&
2533 !test_bit(R5_LOCKED, &dev->flags) &&
2534 !(test_bit(R5_UPTODATE, &dev->flags) ||
2535 test_bit(R5_Wantcompute, &dev->flags))) {
2536 rcw++;
2537 if (!test_bit(R5_Insync, &dev->flags))
2538 continue; /* it's a failed drive */
2539
2540 if (
2541 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2542 pr_debug("Read_old stripe %llu "
2543 "block %d for Reconstruct\n",
2544 (unsigned long long)sh->sector, i);
2545 set_bit(R5_LOCKED, &dev->flags);
2546 set_bit(R5_Wantread, &dev->flags);
2547 s->locked++;
2548 } else {
2549 pr_debug("Request delayed stripe %llu "
2550 "block %d for Reconstruct\n",
2551 (unsigned long long)sh->sector, i);
2552 set_bit(STRIPE_DELAYED, &sh->state);
2553 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002554 }
2555 }
2556 }
Dan Williamsa4456852007-07-09 11:56:43 -07002557 /* now if nothing is locked, and if we have enough data, we can start a
2558 * write request
2559 */
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002560 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2561 s->locked == 0 && rcw == 0 &&
Dan Williamsa4456852007-07-09 11:56:43 -07002562 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002563 schedule_reconstruction(sh, s, 1, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002564 }
2565}
2566
2567static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2568 struct stripe_head_state *s, int disks)
2569{
Dan Williamsecc65c92008-06-28 08:31:57 +10002570 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002571
Dan Williamsbd2ab672008-04-10 21:29:27 -07002572 set_bit(STRIPE_HANDLE, &sh->state);
2573
Dan Williamsecc65c92008-06-28 08:31:57 +10002574 switch (sh->check_state) {
2575 case check_state_idle:
2576 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002577 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002578 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002579 sh->check_state = check_state_run;
2580 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002581 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002582 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002583 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002584 }
Dan Williamsa4456852007-07-09 11:56:43 -07002585 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002586 /* fall through */
2587 case check_state_compute_result:
2588 sh->check_state = check_state_idle;
2589 if (!dev)
2590 dev = &sh->dev[sh->pd_idx];
2591
2592 /* check that a write has not made the stripe insync */
2593 if (test_bit(STRIPE_INSYNC, &sh->state))
2594 break;
2595
2596 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002597 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2598 BUG_ON(s->uptodate != disks);
2599
2600 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002601 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002602 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002603
Dan Williamsa4456852007-07-09 11:56:43 -07002604 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002605 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002606 break;
2607 case check_state_run:
2608 break; /* we will be called again upon completion */
2609 case check_state_check_result:
2610 sh->check_state = check_state_idle;
2611
2612 /* if a failure occurred during the check operation, leave
2613 * STRIPE_INSYNC not set and let the stripe be handled again
2614 */
2615 if (s->failed)
2616 break;
2617
2618 /* handle a successful check operation, if parity is correct
2619 * we are done. Otherwise update the mismatch count and repair
2620 * parity if !MD_RECOVERY_CHECK
2621 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002622 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002623 /* parity is correct (on disc,
2624 * not in buffer any more)
2625 */
2626 set_bit(STRIPE_INSYNC, &sh->state);
2627 else {
2628 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2629 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2630 /* don't try to repair!! */
2631 set_bit(STRIPE_INSYNC, &sh->state);
2632 else {
2633 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002634 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002635 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2636 set_bit(R5_Wantcompute,
2637 &sh->dev[sh->pd_idx].flags);
2638 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002639 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002640 s->uptodate++;
2641 }
2642 }
2643 break;
2644 case check_state_compute_run:
2645 break;
2646 default:
2647 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2648 __func__, sh->check_state,
2649 (unsigned long long) sh->sector);
2650 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002651 }
2652}
2653
2654
2655static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002656 struct stripe_head_state *s,
2657 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002658{
Dan Williamsa4456852007-07-09 11:56:43 -07002659 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002660 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002661 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002662
2663 set_bit(STRIPE_HANDLE, &sh->state);
2664
2665 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002666
Dan Williamsa4456852007-07-09 11:56:43 -07002667 /* Want to check and possibly repair P and Q.
2668 * However there could be one 'failed' device, in which
2669 * case we can only check one of them, possibly using the
2670 * other to generate missing data
2671 */
2672
Dan Williamsd82dfee2009-07-14 13:40:57 -07002673 switch (sh->check_state) {
2674 case check_state_idle:
2675 /* start a new check operation if there are < 2 failures */
Dan Williamsa4456852007-07-09 11:56:43 -07002676 if (s->failed == r6s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002677 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002678 * makes sense to check P (If anything else were failed,
2679 * we would have used P to recreate it).
2680 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002681 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002682 }
2683 if (!r6s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002684 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002685 * anything, so it makes sense to check it
2686 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002687 if (sh->check_state == check_state_run)
2688 sh->check_state = check_state_run_pq;
2689 else
2690 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002691 }
Dan Williams36d1c642009-07-14 11:48:22 -07002692
Dan Williamsd82dfee2009-07-14 13:40:57 -07002693 /* discard potentially stale zero_sum_result */
2694 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002695
Dan Williamsd82dfee2009-07-14 13:40:57 -07002696 if (sh->check_state == check_state_run) {
2697 /* async_xor_zero_sum destroys the contents of P */
2698 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2699 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002700 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002701 if (sh->check_state >= check_state_run &&
2702 sh->check_state <= check_state_run_pq) {
2703 /* async_syndrome_zero_sum preserves P and Q, so
2704 * no need to mark them !uptodate here
2705 */
2706 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2707 break;
2708 }
Dan Williams36d1c642009-07-14 11:48:22 -07002709
Dan Williamsd82dfee2009-07-14 13:40:57 -07002710 /* we have 2-disk failure */
2711 BUG_ON(s->failed != 2);
2712 /* fall through */
2713 case check_state_compute_result:
2714 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002715
Dan Williamsd82dfee2009-07-14 13:40:57 -07002716 /* check that a write has not made the stripe insync */
2717 if (test_bit(STRIPE_INSYNC, &sh->state))
2718 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002719
2720 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002721 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002722 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002723 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002724 if (s->failed == 2) {
2725 dev = &sh->dev[r6s->failed_num[1]];
2726 s->locked++;
2727 set_bit(R5_LOCKED, &dev->flags);
2728 set_bit(R5_Wantwrite, &dev->flags);
2729 }
2730 if (s->failed >= 1) {
2731 dev = &sh->dev[r6s->failed_num[0]];
2732 s->locked++;
2733 set_bit(R5_LOCKED, &dev->flags);
2734 set_bit(R5_Wantwrite, &dev->flags);
2735 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002736 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002737 dev = &sh->dev[pd_idx];
2738 s->locked++;
2739 set_bit(R5_LOCKED, &dev->flags);
2740 set_bit(R5_Wantwrite, &dev->flags);
2741 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002742 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002743 dev = &sh->dev[qd_idx];
2744 s->locked++;
2745 set_bit(R5_LOCKED, &dev->flags);
2746 set_bit(R5_Wantwrite, &dev->flags);
2747 }
2748 clear_bit(STRIPE_DEGRADED, &sh->state);
2749
2750 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002751 break;
2752 case check_state_run:
2753 case check_state_run_q:
2754 case check_state_run_pq:
2755 break; /* we will be called again upon completion */
2756 case check_state_check_result:
2757 sh->check_state = check_state_idle;
2758
2759 /* handle a successful check operation, if parity is correct
2760 * we are done. Otherwise update the mismatch count and repair
2761 * parity if !MD_RECOVERY_CHECK
2762 */
2763 if (sh->ops.zero_sum_result == 0) {
2764 /* both parities are correct */
2765 if (!s->failed)
2766 set_bit(STRIPE_INSYNC, &sh->state);
2767 else {
2768 /* in contrast to the raid5 case we can validate
2769 * parity, but still have a failure to write
2770 * back
2771 */
2772 sh->check_state = check_state_compute_result;
2773 /* Returning at this point means that we may go
2774 * off and bring p and/or q uptodate again so
2775 * we make sure to check zero_sum_result again
2776 * to verify if p or q need writeback
2777 */
2778 }
2779 } else {
2780 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2781 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2782 /* don't try to repair!! */
2783 set_bit(STRIPE_INSYNC, &sh->state);
2784 else {
2785 int *target = &sh->ops.target;
2786
2787 sh->ops.target = -1;
2788 sh->ops.target2 = -1;
2789 sh->check_state = check_state_compute_run;
2790 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2791 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2792 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2793 set_bit(R5_Wantcompute,
2794 &sh->dev[pd_idx].flags);
2795 *target = pd_idx;
2796 target = &sh->ops.target2;
2797 s->uptodate++;
2798 }
2799 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2800 set_bit(R5_Wantcompute,
2801 &sh->dev[qd_idx].flags);
2802 *target = qd_idx;
2803 s->uptodate++;
2804 }
2805 }
2806 }
2807 break;
2808 case check_state_compute_run:
2809 break;
2810 default:
2811 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2812 __func__, sh->check_state,
2813 (unsigned long long) sh->sector);
2814 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002815 }
2816}
2817
2818static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2819 struct r6_state *r6s)
2820{
2821 int i;
2822
2823 /* We have read all the blocks in this stripe and now we need to
2824 * copy some of them into a target stripe for expand.
2825 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002826 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002827 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2828 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002829 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002830 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002831 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002832 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002833
NeilBrown784052e2009-03-31 15:19:07 +11002834 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002835 sector_t s = raid5_compute_sector(conf, bn, 0,
2836 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002837 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002838 if (sh2 == NULL)
2839 /* so far only the early blocks of this stripe
2840 * have been requested. When later blocks
2841 * get requested, we will try again
2842 */
2843 continue;
2844 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2845 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2846 /* must have already done this block */
2847 release_stripe(sh2);
2848 continue;
2849 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002850
2851 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002852 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002853 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002854 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002855 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002856
Dan Williamsa4456852007-07-09 11:56:43 -07002857 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2858 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2859 for (j = 0; j < conf->raid_disks; j++)
2860 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002861 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002862 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2863 break;
2864 if (j == conf->raid_disks) {
2865 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2866 set_bit(STRIPE_HANDLE, &sh2->state);
2867 }
2868 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002869
Dan Williamsa4456852007-07-09 11:56:43 -07002870 }
NeilBrowna2e08552007-09-11 15:23:36 -07002871 /* done submitting copies, wait for them to complete */
2872 if (tx) {
2873 async_tx_ack(tx);
2874 dma_wait_for_async_tx(tx);
2875 }
Dan Williamsa4456852007-07-09 11:56:43 -07002876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Dan Williams6bfe0b42008-04-30 00:52:32 -07002878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879/*
2880 * handle_stripe - do things to a stripe.
2881 *
2882 * We lock the stripe and then examine the state of various bits
2883 * to see what needs to be done.
2884 * Possible results:
2885 * return some read request which now have data
2886 * return some write requests which are safely on disc
2887 * schedule a read on some buffers
2888 * schedule a write of some buffers
2889 * return confirmation of parity correctness
2890 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 * buffers are taken off read_list or write_list, and bh_cache buffers
2892 * get BH_Lock set before the stripe lock is released.
2893 *
2894 */
Dan Williamsa4456852007-07-09 11:56:43 -07002895
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002896static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
2898 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002899 int disks = sh->disks, i;
2900 struct bio *return_bi = NULL;
2901 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002903 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002904 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Dan Williamsa4456852007-07-09 11:56:43 -07002906 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002907 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2908 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2909 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2910 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 spin_lock(&sh->lock);
2913 clear_bit(STRIPE_HANDLE, &sh->state);
2914 clear_bit(STRIPE_DELAYED, &sh->state);
2915
Dan Williamsa4456852007-07-09 11:56:43 -07002916 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2917 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2918 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002921 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 for (i=disks; i--; ) {
2923 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002924 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Dan Williamsb5e98d62007-01-02 13:52:31 -07002927 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2928 "written %p\n", i, dev->flags, dev->toread, dev->read,
2929 dev->towrite, dev->written);
2930
2931 /* maybe we can request a biofill operation
2932 *
2933 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002934 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002935 */
2936 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002937 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002938 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002941 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2942 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002943 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Dan Williamsb5e98d62007-01-02 13:52:31 -07002945 if (test_bit(R5_Wantfill, &dev->flags))
2946 s.to_fill++;
2947 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002948 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002950 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002952 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
Dan Williamsa4456852007-07-09 11:56:43 -07002954 if (dev->written)
2955 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002956 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002957 if (blocked_rdev == NULL &&
2958 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002959 blocked_rdev = rdev;
2960 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002961 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002962 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002963 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002964 clear_bit(R5_ReadError, &dev->flags);
2965 clear_bit(R5_ReWrite, &dev->flags);
2966 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002967 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002968 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002969 s.failed++;
2970 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 } else
2972 set_bit(R5_Insync, &dev->flags);
2973 }
NeilBrown9910f162006-01-06 00:20:24 -08002974 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002975
Dan Williams6bfe0b42008-04-30 00:52:32 -07002976 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10002977 if (s.syncing || s.expanding || s.expanded ||
2978 s.to_write || s.written) {
2979 set_bit(STRIPE_HANDLE, &sh->state);
2980 goto unlock;
2981 }
2982 /* There is nothing for the blocked_rdev to block */
2983 rdev_dec_pending(blocked_rdev, conf->mddev);
2984 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002985 }
2986
Dan Williams83de75c2008-06-28 08:31:58 +10002987 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2988 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2989 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2990 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002991
Dan Williams45b42332007-07-09 11:56:43 -07002992 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002994 s.locked, s.uptodate, s.to_read, s.to_write,
2995 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 /* check if the array has lost two devices and, if so, some requests might
2997 * need to be failed
2998 */
Dan Williamsa4456852007-07-09 11:56:43 -07002999 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003000 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003001 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3003 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003004 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006
3007 /* might be able to return some write requests if the parity block
3008 * is safe, or on a failed drive
3009 */
3010 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003011 if ( s.written &&
3012 ((test_bit(R5_Insync, &dev->flags) &&
3013 !test_bit(R5_LOCKED, &dev->flags) &&
3014 test_bit(R5_UPTODATE, &dev->flags)) ||
3015 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10003016 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 /* Now we might consider reading some blocks, either to check/generate
3019 * parity, or to satisfy requests
3020 * or to load a block that is being partially written.
3021 */
Dan Williamsa4456852007-07-09 11:56:43 -07003022 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10003023 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003024 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Dan Williamse33129d2007-01-02 13:52:30 -07003026 /* Now we check to see if any write operations have recently
3027 * completed
3028 */
Dan Williamse0a115e2008-06-05 22:45:52 -07003029 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003030 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07003031 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003032 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3033 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10003034 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07003035
3036 /* All the 'written' buffers and the parity block are ready to
3037 * be written back to disk
3038 */
3039 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3040 for (i = disks; i--; ) {
3041 dev = &sh->dev[i];
3042 if (test_bit(R5_LOCKED, &dev->flags) &&
3043 (i == sh->pd_idx || dev->written)) {
3044 pr_debug("Writing block %d\n", i);
3045 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07003046 if (prexor)
3047 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07003048 if (!test_bit(R5_Insync, &dev->flags) ||
3049 (i == sh->pd_idx && s.failed == 0))
3050 set_bit(STRIPE_INSYNC, &sh->state);
3051 }
3052 }
3053 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3054 atomic_dec(&conf->preread_active_stripes);
3055 if (atomic_read(&conf->preread_active_stripes) <
3056 IO_THRESHOLD)
3057 md_wakeup_thread(conf->mddev->thread);
3058 }
3059 }
3060
3061 /* Now to consider new write requests and what else, if anything
3062 * should be read. We do not handle new writes when:
3063 * 1/ A 'write' operation (copy+xor) is already in flight.
3064 * 2/ A 'check' operation is in flight, as it may clobber the parity
3065 * block.
3066 */
Dan Williams600aa102008-06-28 08:32:05 +10003067 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003068 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07003071 * Any reads will already have been scheduled, so we just see if enough
3072 * data is available. The parity check is held off while parity
3073 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 */
Dan Williamsecc65c92008-06-28 08:31:57 +10003075 if (sh->check_state ||
3076 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003077 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10003078 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07003079 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07003080
Dan Williamsa4456852007-07-09 11:56:43 -07003081 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3083 clear_bit(STRIPE_SYNCING, &sh->state);
3084 }
NeilBrown4e5314b2005-11-08 21:39:22 -08003085
3086 /* If the failed drive is just a ReadError, then we might need to progress
3087 * the repair/check process
3088 */
Dan Williamsa4456852007-07-09 11:56:43 -07003089 if (s.failed == 1 && !conf->mddev->ro &&
3090 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3091 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3092 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003093 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003094 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003095 if (!test_bit(R5_ReWrite, &dev->flags)) {
3096 set_bit(R5_Wantwrite, &dev->flags);
3097 set_bit(R5_ReWrite, &dev->flags);
3098 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003099 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003100 } else {
3101 /* let's read it back */
3102 set_bit(R5_Wantread, &dev->flags);
3103 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003104 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003105 }
3106 }
3107
Dan Williams600aa102008-06-28 08:32:05 +10003108 /* Finish reconstruct operations initiated by the expansion process */
3109 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003110 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003111 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003112 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3113 /* sh cannot be written until sh2 has been read.
3114 * so arrange for sh to be delayed a little
3115 */
3116 set_bit(STRIPE_DELAYED, &sh->state);
3117 set_bit(STRIPE_HANDLE, &sh->state);
3118 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3119 &sh2->state))
3120 atomic_inc(&conf->preread_active_stripes);
3121 release_stripe(sh2);
3122 goto unlock;
3123 }
3124 if (sh2)
3125 release_stripe(sh2);
3126
Dan Williams600aa102008-06-28 08:32:05 +10003127 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003128 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003129 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003130 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003131 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003132 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003133 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003134 }
3135
3136 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003137 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003138 /* Need to write out all blocks after computing parity */
3139 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003140 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003141 schedule_reconstruction(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003142 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003143 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003144 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003145 wake_up(&conf->wait_for_overlap);
3146 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3147 }
3148
Dan Williams0f94e872008-01-08 15:32:53 -08003149 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003150 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003151 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003152
Dan Williams6bfe0b42008-04-30 00:52:32 -07003153 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 spin_unlock(&sh->lock);
3155
Dan Williams6bfe0b42008-04-30 00:52:32 -07003156 /* wait for this device to become unblocked */
3157 if (unlikely(blocked_rdev))
3158 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3159
Dan Williams600aa102008-06-28 08:32:05 +10003160 if (s.ops_request)
Dan Williamsac6b53b2009-07-14 13:40:19 -07003161 raid_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003162
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003163 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Dan Williamsa4456852007-07-09 11:56:43 -07003165 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003166
3167 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Dan Williams36d1c642009-07-14 11:48:22 -07003170static bool handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003171{
NeilBrownbff61972009-03-31 14:33:13 +11003172 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003173 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003174 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003175 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003176 struct stripe_head_state s;
3177 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003178 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003179 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003180
Dan Williams45b42332007-07-09 11:56:43 -07003181 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003182 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003183 (unsigned long long)sh->sector, sh->state,
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003184 atomic_read(&sh->count), pd_idx, qd_idx,
3185 sh->check_state, sh->reconstruct_state);
Dan Williamsa4456852007-07-09 11:56:43 -07003186 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003187
3188 spin_lock(&sh->lock);
3189 clear_bit(STRIPE_HANDLE, &sh->state);
3190 clear_bit(STRIPE_DELAYED, &sh->state);
3191
Dan Williamsa4456852007-07-09 11:56:43 -07003192 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3193 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3194 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003195 /* Now to look around and see what can be done */
3196
3197 rcu_read_lock();
3198 for (i=disks; i--; ) {
3199 mdk_rdev_t *rdev;
3200 dev = &sh->dev[i];
3201 clear_bit(R5_Insync, &dev->flags);
3202
Dan Williams45b42332007-07-09 11:56:43 -07003203 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003204 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003205 /* maybe we can reply to a read
3206 *
3207 * new wantfill requests are only permitted while
3208 * ops_complete_biofill is guaranteed to be inactive
3209 */
3210 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3211 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3212 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003213
3214 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003215 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3216 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003217 if (test_bit(R5_Wantcompute, &dev->flags)) {
3218 s.compute++;
3219 BUG_ON(s.compute > 2);
3220 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003221
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003222 if (test_bit(R5_Wantfill, &dev->flags)) {
3223 s.to_fill++;
3224 } else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003225 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003226 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003227 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003228 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003229 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003230 }
Dan Williamsa4456852007-07-09 11:56:43 -07003231 if (dev->written)
3232 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003233 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003234 if (blocked_rdev == NULL &&
3235 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003236 blocked_rdev = rdev;
3237 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003238 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003239 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3240 /* The ReadError flag will just be confusing now */
3241 clear_bit(R5_ReadError, &dev->flags);
3242 clear_bit(R5_ReWrite, &dev->flags);
3243 }
3244 if (!rdev || !test_bit(In_sync, &rdev->flags)
3245 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003246 if (s.failed < 2)
3247 r6s.failed_num[s.failed] = i;
3248 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003249 } else
3250 set_bit(R5_Insync, &dev->flags);
3251 }
3252 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003253
3254 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003255 if (s.syncing || s.expanding || s.expanded ||
3256 s.to_write || s.written) {
3257 set_bit(STRIPE_HANDLE, &sh->state);
3258 goto unlock;
3259 }
3260 /* There is nothing for the blocked_rdev to block */
3261 rdev_dec_pending(blocked_rdev, conf->mddev);
3262 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003263 }
NeilBrownac4090d2008-08-05 15:54:13 +10003264
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003265 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3266 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3267 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3268 }
3269
Dan Williams45b42332007-07-09 11:56:43 -07003270 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003271 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003272 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3273 r6s.failed_num[0], r6s.failed_num[1]);
3274 /* check if the array has lost >2 devices and, if so, some requests
3275 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003276 */
Dan Williamsa4456852007-07-09 11:56:43 -07003277 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003278 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003279 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003280 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3281 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003282 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003283 }
3284
3285 /*
3286 * might be able to return some write requests if the parity blocks
3287 * are safe, or on a failed drive
3288 */
3289 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003290 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3291 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003292 qdev = &sh->dev[qd_idx];
3293 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3294 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003295
Dan Williamsa4456852007-07-09 11:56:43 -07003296 if ( s.written &&
3297 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003298 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003299 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3300 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003301 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003302 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003303 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003304
3305 /* Now we might consider reading some blocks, either to check/generate
3306 * parity, or to satisfy requests
3307 * or to load a block that is being partially written.
3308 */
Dan Williamsa4456852007-07-09 11:56:43 -07003309 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003310 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003311 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003312
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003313 /* Now we check to see if any write operations have recently
3314 * completed
3315 */
3316 if (sh->reconstruct_state == reconstruct_state_drain_result) {
3317 int qd_idx = sh->qd_idx;
3318
3319 sh->reconstruct_state = reconstruct_state_idle;
3320 /* All the 'written' buffers and the parity blocks are ready to
3321 * be written back to disk
3322 */
3323 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3324 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
3325 for (i = disks; i--; ) {
3326 dev = &sh->dev[i];
3327 if (test_bit(R5_LOCKED, &dev->flags) &&
3328 (i == sh->pd_idx || i == qd_idx ||
3329 dev->written)) {
3330 pr_debug("Writing block %d\n", i);
3331 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3332 set_bit(R5_Wantwrite, &dev->flags);
3333 if (!test_bit(R5_Insync, &dev->flags) ||
3334 ((i == sh->pd_idx || i == qd_idx) &&
3335 s.failed == 0))
3336 set_bit(STRIPE_INSYNC, &sh->state);
3337 }
3338 }
3339 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3340 atomic_dec(&conf->preread_active_stripes);
3341 if (atomic_read(&conf->preread_active_stripes) <
3342 IO_THRESHOLD)
3343 md_wakeup_thread(conf->mddev->thread);
3344 }
3345 }
3346
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07003347 /* Now to consider new write requests and what else, if anything
3348 * should be read. We do not handle new writes when:
3349 * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
3350 * 2/ A 'check' operation is in flight, as it may clobber the parity
3351 * block.
3352 */
3353 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003354 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003355
3356 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003357 * Any reads will already have been scheduled, so we just see if enough
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003358 * data is available. The parity check is held off while parity
3359 * dependent operations are in flight.
NeilBrown16a53ec2006-06-26 00:27:38 -07003360 */
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003361 if (sh->check_state ||
3362 (s.syncing && s.locked == 0 &&
3363 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3364 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williams36d1c642009-07-14 11:48:22 -07003365 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003366
Dan Williamsa4456852007-07-09 11:56:43 -07003367 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003368 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3369 clear_bit(STRIPE_SYNCING, &sh->state);
3370 }
3371
3372 /* If the failed drives are just a ReadError, then we might need
3373 * to progress the repair/check process
3374 */
Dan Williamsa4456852007-07-09 11:56:43 -07003375 if (s.failed <= 2 && !conf->mddev->ro)
3376 for (i = 0; i < s.failed; i++) {
3377 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003378 if (test_bit(R5_ReadError, &dev->flags)
3379 && !test_bit(R5_LOCKED, &dev->flags)
3380 && test_bit(R5_UPTODATE, &dev->flags)
3381 ) {
3382 if (!test_bit(R5_ReWrite, &dev->flags)) {
3383 set_bit(R5_Wantwrite, &dev->flags);
3384 set_bit(R5_ReWrite, &dev->flags);
3385 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003386 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003387 } else {
3388 /* let's read it back */
3389 set_bit(R5_Wantread, &dev->flags);
3390 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003391 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003392 }
3393 }
3394 }
NeilBrownf4168852007-02-28 20:11:53 -08003395
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003396 /* Finish reconstruct operations initiated by the expansion process */
3397 if (sh->reconstruct_state == reconstruct_state_result) {
3398 sh->reconstruct_state = reconstruct_state_idle;
3399 clear_bit(STRIPE_EXPANDING, &sh->state);
3400 for (i = conf->raid_disks; i--; ) {
3401 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3402 set_bit(R5_LOCKED, &sh->dev[i].flags);
3403 s.locked++;
3404 }
3405 }
3406
3407 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3408 !sh->reconstruct_state) {
NeilBrownab69ae12009-03-31 15:26:47 +11003409 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003410 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003411 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3412 /* sh cannot be written until sh2 has been read.
3413 * so arrange for sh to be delayed a little
3414 */
3415 set_bit(STRIPE_DELAYED, &sh->state);
3416 set_bit(STRIPE_HANDLE, &sh->state);
3417 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3418 &sh2->state))
3419 atomic_inc(&conf->preread_active_stripes);
3420 release_stripe(sh2);
3421 goto unlock;
3422 }
3423 if (sh2)
3424 release_stripe(sh2);
3425
NeilBrownf4168852007-02-28 20:11:53 -08003426 /* Need to write out all blocks after computing P&Q */
3427 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003428 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003429 schedule_reconstruction(sh, &s, 1, 1);
3430 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003431 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3432 atomic_dec(&conf->reshape_stripes);
3433 wake_up(&conf->wait_for_overlap);
3434 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3435 }
3436
Dan Williams0f94e872008-01-08 15:32:53 -08003437 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003438 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003439 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003440
Dan Williams6bfe0b42008-04-30 00:52:32 -07003441 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003442 spin_unlock(&sh->lock);
3443
Dan Williams6bfe0b42008-04-30 00:52:32 -07003444 /* wait for this device to become unblocked */
3445 if (unlikely(blocked_rdev))
3446 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3447
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003448 if (s.ops_request)
3449 raid_run_ops(sh, s.ops_request);
3450
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003451 ops_run_io(sh, &s);
3452
Dan Williamsa4456852007-07-09 11:56:43 -07003453 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003454
3455 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003456}
3457
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003458/* returns true if the stripe was handled */
Dan Williams36d1c642009-07-14 11:48:22 -07003459static bool handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003460{
3461 if (sh->raid_conf->level == 6)
Dan Williams36d1c642009-07-14 11:48:22 -07003462 return handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003463 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003464 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003465}
3466
Arjan van de Ven858119e2006-01-14 13:20:43 -08003467static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
3469 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3470 while (!list_empty(&conf->delayed_list)) {
3471 struct list_head *l = conf->delayed_list.next;
3472 struct stripe_head *sh;
3473 sh = list_entry(l, struct stripe_head, lru);
3474 list_del_init(l);
3475 clear_bit(STRIPE_DELAYED, &sh->state);
3476 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3477 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003478 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 }
NeilBrown6ed30032008-02-06 01:40:00 -08003480 } else
3481 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
Arjan van de Ven858119e2006-01-14 13:20:43 -08003484static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003485{
3486 /* device_lock is held */
3487 struct list_head head;
3488 list_add(&head, &conf->bitmap_list);
3489 list_del_init(&conf->bitmap_list);
3490 while (!list_empty(&head)) {
3491 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3492 list_del_init(&sh->lru);
3493 atomic_inc(&sh->count);
3494 __release_stripe(conf, sh);
3495 }
3496}
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498static void unplug_slaves(mddev_t *mddev)
3499{
NeilBrown070ec552009-06-16 16:54:21 +10003500 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 int i;
3502
3503 rcu_read_lock();
NeilBrownf001a702009-06-09 14:30:31 +10003504 for (i = 0; i < conf->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003505 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003506 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003507 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509 atomic_inc(&rdev->nr_pending);
3510 rcu_read_unlock();
3511
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003512 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 rdev_dec_pending(rdev, mddev);
3515 rcu_read_lock();
3516 }
3517 }
3518 rcu_read_unlock();
3519}
3520
Jens Axboe165125e2007-07-24 09:28:11 +02003521static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
3523 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003524 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 unsigned long flags;
3526
3527 spin_lock_irqsave(&conf->device_lock, flags);
3528
NeilBrown72626682005-09-09 16:23:54 -07003529 if (blk_remove_plug(q)) {
3530 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 md_wakeup_thread(mddev->thread);
3534
3535 spin_unlock_irqrestore(&conf->device_lock, flags);
3536
3537 unplug_slaves(mddev);
3538}
3539
NeilBrownf022b2f2006-10-03 01:15:56 -07003540static int raid5_congested(void *data, int bits)
3541{
3542 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +10003543 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003544
3545 /* No difference between reads and writes. Just check
3546 * how busy the stripe_cache is
3547 */
3548 if (conf->inactive_blocked)
3549 return 1;
3550 if (conf->quiesce)
3551 return 1;
3552 if (list_empty_careful(&conf->inactive_list))
3553 return 1;
3554
3555 return 0;
3556}
3557
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003558/* We want read requests to align with chunks where possible,
3559 * but write requests don't need to.
3560 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003561static int raid5_mergeable_bvec(struct request_queue *q,
3562 struct bvec_merge_data *bvm,
3563 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003564{
3565 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003566 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003567 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003568 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003569 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003570
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003571 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003572 return biovec->bv_len; /* always allow writes to be mergeable */
3573
Andre Noll664e7c42009-06-18 08:45:27 +10003574 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3575 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003576 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3577 if (max < 0) max = 0;
3578 if (max <= biovec->bv_len && bio_sectors == 0)
3579 return biovec->bv_len;
3580 else
3581 return max;
3582}
3583
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003584
3585static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3586{
3587 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003588 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003589 unsigned int bio_sectors = bio->bi_size >> 9;
3590
Andre Noll664e7c42009-06-18 08:45:27 +10003591 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3592 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003593 return chunk_sectors >=
3594 ((sector & (chunk_sectors - 1)) + bio_sectors);
3595}
3596
3597/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003598 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3599 * later sampled by raid5d.
3600 */
3601static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3602{
3603 unsigned long flags;
3604
3605 spin_lock_irqsave(&conf->device_lock, flags);
3606
3607 bi->bi_next = conf->retry_read_aligned_list;
3608 conf->retry_read_aligned_list = bi;
3609
3610 spin_unlock_irqrestore(&conf->device_lock, flags);
3611 md_wakeup_thread(conf->mddev->thread);
3612}
3613
3614
3615static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3616{
3617 struct bio *bi;
3618
3619 bi = conf->retry_read_aligned;
3620 if (bi) {
3621 conf->retry_read_aligned = NULL;
3622 return bi;
3623 }
3624 bi = conf->retry_read_aligned_list;
3625 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003626 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003627 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003628 /*
3629 * this sets the active strip count to 1 and the processed
3630 * strip count to zero (upper 8 bits)
3631 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003632 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003633 }
3634
3635 return bi;
3636}
3637
3638
3639/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003640 * The "raid5_align_endio" should check if the read succeeded and if it
3641 * did, call bio_endio on the original bio (having bio_put the new bio
3642 * first).
3643 * If the read failed..
3644 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003645static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003646{
3647 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003648 mddev_t *mddev;
3649 raid5_conf_t *conf;
3650 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3651 mdk_rdev_t *rdev;
3652
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003653 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003654
3655 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003656 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003657 rdev = (void*)raid_bi->bi_next;
3658 raid_bi->bi_next = NULL;
3659
3660 rdev_dec_pending(rdev, conf->mddev);
3661
3662 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003663 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003664 if (atomic_dec_and_test(&conf->active_aligned_reads))
3665 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003666 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003667 }
3668
3669
Dan Williams45b42332007-07-09 11:56:43 -07003670 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003671
3672 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003673}
3674
Neil Brown387bb172007-02-08 14:20:29 -08003675static int bio_fits_rdev(struct bio *bi)
3676{
Jens Axboe165125e2007-07-24 09:28:11 +02003677 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003678
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003679 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003680 return 0;
3681 blk_recount_segments(q, bi);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003682 if (bi->bi_phys_segments > queue_max_phys_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003683 return 0;
3684
3685 if (q->merge_bvec_fn)
3686 /* it's too hard to apply the merge_bvec_fn at this stage,
3687 * just just give up
3688 */
3689 return 0;
3690
3691 return 1;
3692}
3693
3694
Jens Axboe165125e2007-07-24 09:28:11 +02003695static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003696{
3697 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003698 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003699 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003700 struct bio* align_bi;
3701 mdk_rdev_t *rdev;
3702
3703 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003704 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003705 return 0;
3706 }
3707 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003708 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003709 */
3710 align_bi = bio_clone(raid_bio, GFP_NOIO);
3711 if (!align_bi)
3712 return 0;
3713 /*
3714 * set bi_end_io to a new function, and set bi_private to the
3715 * original bio.
3716 */
3717 align_bi->bi_end_io = raid5_align_endio;
3718 align_bi->bi_private = raid_bio;
3719 /*
3720 * compute position
3721 */
NeilBrown112bf892009-03-31 14:39:38 +11003722 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3723 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003724 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003725
3726 rcu_read_lock();
3727 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3728 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003729 atomic_inc(&rdev->nr_pending);
3730 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003731 raid_bio->bi_next = (void*)rdev;
3732 align_bi->bi_bdev = rdev->bdev;
3733 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3734 align_bi->bi_sector += rdev->data_offset;
3735
Neil Brown387bb172007-02-08 14:20:29 -08003736 if (!bio_fits_rdev(align_bi)) {
3737 /* too big in some way */
3738 bio_put(align_bi);
3739 rdev_dec_pending(rdev, mddev);
3740 return 0;
3741 }
3742
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003743 spin_lock_irq(&conf->device_lock);
3744 wait_event_lock_irq(conf->wait_for_stripe,
3745 conf->quiesce == 0,
3746 conf->device_lock, /* nothing */);
3747 atomic_inc(&conf->active_aligned_reads);
3748 spin_unlock_irq(&conf->device_lock);
3749
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003750 generic_make_request(align_bi);
3751 return 1;
3752 } else {
3753 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003754 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003755 return 0;
3756 }
3757}
3758
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003759/* __get_priority_stripe - get the next stripe to process
3760 *
3761 * Full stripe writes are allowed to pass preread active stripes up until
3762 * the bypass_threshold is exceeded. In general the bypass_count
3763 * increments when the handle_list is handled before the hold_list; however, it
3764 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3765 * stripe with in flight i/o. The bypass_count will be reset when the
3766 * head of the hold_list has changed, i.e. the head was promoted to the
3767 * handle_list.
3768 */
3769static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3770{
3771 struct stripe_head *sh;
3772
3773 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3774 __func__,
3775 list_empty(&conf->handle_list) ? "empty" : "busy",
3776 list_empty(&conf->hold_list) ? "empty" : "busy",
3777 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3778
3779 if (!list_empty(&conf->handle_list)) {
3780 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3781
3782 if (list_empty(&conf->hold_list))
3783 conf->bypass_count = 0;
3784 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3785 if (conf->hold_list.next == conf->last_hold)
3786 conf->bypass_count++;
3787 else {
3788 conf->last_hold = conf->hold_list.next;
3789 conf->bypass_count -= conf->bypass_threshold;
3790 if (conf->bypass_count < 0)
3791 conf->bypass_count = 0;
3792 }
3793 }
3794 } else if (!list_empty(&conf->hold_list) &&
3795 ((conf->bypass_threshold &&
3796 conf->bypass_count > conf->bypass_threshold) ||
3797 atomic_read(&conf->pending_full_writes) == 0)) {
3798 sh = list_entry(conf->hold_list.next,
3799 typeof(*sh), lru);
3800 conf->bypass_count -= conf->bypass_threshold;
3801 if (conf->bypass_count < 0)
3802 conf->bypass_count = 0;
3803 } else
3804 return NULL;
3805
3806 list_del_init(&sh->lru);
3807 atomic_inc(&sh->count);
3808 BUG_ON(atomic_read(&sh->count) != 1);
3809 return sh;
3810}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003811
Jens Axboe165125e2007-07-24 09:28:11 +02003812static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
3814 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003815 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003816 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 sector_t new_sector;
3818 sector_t logical_sector, last_sector;
3819 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003820 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003821 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
NeilBrowne5dcdd82005-09-09 16:23:41 -07003823 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003824 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003825 return 0;
3826 }
3827
NeilBrown3d310eb2005-06-21 17:17:26 -07003828 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003829
Tejun Heo074a7ac2008-08-25 19:56:14 +09003830 cpu = part_stat_lock();
3831 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3832 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3833 bio_sectors(bi));
3834 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
NeilBrown802ba062006-12-13 00:34:13 -08003836 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003837 mddev->reshape_position == MaxSector &&
3838 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003839 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3842 last_sector = bi->bi_sector + (bi->bi_size>>9);
3843 bi->bi_next = NULL;
3844 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003845
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3847 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003848 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003849 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003850
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003851 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003852 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003853 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003854 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003855 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003856 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08003857 * 64bit on a 32bit platform, and so it might be
3858 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003859 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08003860 * the lock is dropped, so once we get a reference
3861 * to the stripe that we think it is, we will have
3862 * to check again.
3863 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003864 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003865 if (mddev->delta_disks < 0
3866 ? logical_sector < conf->reshape_progress
3867 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003868 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003869 previous = 1;
3870 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003871 if (mddev->delta_disks < 0
3872 ? logical_sector < conf->reshape_safe
3873 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003874 spin_unlock_irq(&conf->device_lock);
3875 schedule();
3876 goto retry;
3877 }
3878 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003879 spin_unlock_irq(&conf->device_lock);
3880 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003881 data_disks = disks - conf->max_degraded;
3882
NeilBrown112bf892009-03-31 14:39:38 +11003883 new_sector = raid5_compute_sector(conf, logical_sector,
3884 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003885 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003886 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 (unsigned long long)new_sector,
3888 (unsigned long long)logical_sector);
3889
NeilBrownb5663ba2009-03-31 14:39:38 +11003890 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003891 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003893 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003894 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08003895 * stripe, so we must do the range check again.
3896 * Expansion could still move past after this
3897 * test, but as we are holding a reference to
3898 * 'sh', we know that if that happens,
3899 * STRIPE_EXPANDING will get set and the expansion
3900 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003901 */
3902 int must_retry = 0;
3903 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003904 if (mddev->delta_disks < 0
3905 ? logical_sector >= conf->reshape_progress
3906 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003907 /* mismatch, need to try again */
3908 must_retry = 1;
3909 spin_unlock_irq(&conf->device_lock);
3910 if (must_retry) {
3911 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003912 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003913 goto retry;
3914 }
3915 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003916
NeilBrowna5c308d2009-07-01 13:15:35 +10003917 if (bio_data_dir(bi) == WRITE &&
3918 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003919 logical_sector < mddev->suspend_hi) {
3920 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003921 /* As the suspend_* range is controlled by
3922 * userspace, we want an interruptible
3923 * wait.
3924 */
3925 flush_signals(current);
3926 prepare_to_wait(&conf->wait_for_overlap,
3927 &w, TASK_INTERRUPTIBLE);
3928 if (logical_sector >= mddev->suspend_lo &&
3929 logical_sector < mddev->suspend_hi)
3930 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003931 goto retry;
3932 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003933
3934 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3935 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3936 /* Stripe is busy expanding or
3937 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 * and wait a while
3939 */
3940 raid5_unplug_device(mddev->queue);
3941 release_stripe(sh);
3942 schedule();
3943 goto retry;
3944 }
3945 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003946 set_bit(STRIPE_HANDLE, &sh->state);
3947 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 } else {
3950 /* cannot get stripe for read-ahead, just give-up */
3951 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3952 finish_wait(&conf->wait_for_overlap, &w);
3953 break;
3954 }
3955
3956 }
3957 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003958 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003959 spin_unlock_irq(&conf->device_lock);
3960 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
NeilBrown16a53ec2006-06-26 00:27:38 -07003962 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003964
Neil Brown0e13fe232008-06-28 08:31:20 +10003965 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return 0;
3968}
3969
Dan Williamsb522adc2009-03-31 15:00:31 +11003970static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
3971
NeilBrown52c03292006-06-26 00:27:43 -07003972static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
NeilBrown52c03292006-06-26 00:27:43 -07003974 /* reshaping is quite different to recovery/resync so it is
3975 * handled quite separately ... here.
3976 *
3977 * On each call to sync_request, we gather one chunk worth of
3978 * destination stripes and flag them as expanding.
3979 * Then we find all the source stripes and request reads.
3980 * As the reads complete, handle_stripe will copy the data
3981 * into the destination stripe and release that stripe.
3982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3984 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003985 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003986 int raid_disks = conf->previous_raid_disks;
3987 int data_disks = raid_disks - conf->max_degraded;
3988 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003989 int i;
3990 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003991 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003992 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003993 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003994 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003995
NeilBrownfef9c612009-03-31 15:16:46 +11003996 if (sector_nr == 0) {
3997 /* If restarting in the middle, skip the initial sectors */
3998 if (mddev->delta_disks < 0 &&
3999 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4000 sector_nr = raid5_size(mddev, 0, 0)
4001 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004002 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004003 conf->reshape_progress > 0)
4004 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004005 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004006 if (sector_nr) {
4007 *skipped = 1;
4008 return sector_nr;
4009 }
NeilBrown52c03292006-06-26 00:27:43 -07004010 }
4011
NeilBrown7a661382009-03-31 15:21:40 +11004012 /* We need to process a full chunk at a time.
4013 * If old and new chunk sizes differ, we need to process the
4014 * largest of these
4015 */
Andre Noll664e7c42009-06-18 08:45:27 +10004016 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4017 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004018 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004019 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004020
NeilBrown52c03292006-06-26 00:27:43 -07004021 /* we update the metadata when there is more than 3Meg
4022 * in the block range (that is rather arbitrary, should
4023 * probably be time based) or when the data about to be
4024 * copied would over-write the source of the data at
4025 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004026 * i.e. one new_stripe along from reshape_progress new_maps
4027 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004028 */
NeilBrownfef9c612009-03-31 15:16:46 +11004029 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004030 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004031 readpos = conf->reshape_progress;
4032 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004033 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004034 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004035 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004036 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004037 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004038 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004039 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004040 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004041 readpos -= min_t(sector_t, reshape_sectors, readpos);
4042 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004043 }
NeilBrown52c03292006-06-26 00:27:43 -07004044
NeilBrownc8f517c2009-03-31 15:28:40 +11004045 /* 'writepos' is the most advanced device address we might write.
4046 * 'readpos' is the least advanced device address we might read.
4047 * 'safepos' is the least address recorded in the metadata as having
4048 * been reshaped.
4049 * If 'readpos' is behind 'writepos', then there is no way that we can
4050 * ensure safety in the face of a crash - that must be done by userspace
4051 * making a backup of the data. So in that case there is no particular
4052 * rush to update metadata.
4053 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4054 * update the metadata to advance 'safepos' to match 'readpos' so that
4055 * we can be safe in the event of a crash.
4056 * So we insist on updating metadata if safepos is behind writepos and
4057 * readpos is beyond writepos.
4058 * In any case, update the metadata every 10 seconds.
4059 * Maybe that number should be configurable, but I'm not sure it is
4060 * worth it.... maybe it could be a multiple of safemode_delay???
4061 */
NeilBrownfef9c612009-03-31 15:16:46 +11004062 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004063 ? (safepos > writepos && readpos < writepos)
4064 : (safepos < writepos && readpos > writepos)) ||
4065 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004066 /* Cannot proceed until we've updated the superblock... */
4067 wait_event(conf->wait_for_overlap,
4068 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004069 mddev->reshape_position = conf->reshape_progress;
NeilBrownacb180b2009-04-14 16:28:34 +10004070 mddev->curr_resync_completed = mddev->curr_resync;
NeilBrownc8f517c2009-03-31 15:28:40 +11004071 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004072 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004073 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004074 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004075 kthread_should_stop());
4076 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004077 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004078 spin_unlock_irq(&conf->device_lock);
4079 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004080 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004081 }
4082
NeilBrownec32a2b2009-03-31 15:17:38 +11004083 if (mddev->delta_disks < 0) {
4084 BUG_ON(conf->reshape_progress == 0);
4085 stripe_addr = writepos;
4086 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004087 ~((sector_t)reshape_sectors - 1))
4088 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004089 != sector_nr);
4090 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004091 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004092 stripe_addr = sector_nr;
4093 }
NeilBrownab69ae12009-03-31 15:26:47 +11004094 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004095 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004096 int j;
4097 int skipped = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004098 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004099 set_bit(STRIPE_EXPANDING, &sh->state);
4100 atomic_inc(&conf->reshape_stripes);
4101 /* If any of this stripe is beyond the end of the old
4102 * array, then we need to zero those blocks
4103 */
4104 for (j=sh->disks; j--;) {
4105 sector_t s;
4106 if (j == sh->pd_idx)
4107 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004108 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004109 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004110 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004111 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004112 if (s < raid5_size(mddev, 0, 0)) {
NeilBrown52c03292006-06-26 00:27:43 -07004113 skipped = 1;
4114 continue;
4115 }
4116 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4117 set_bit(R5_Expanded, &sh->dev[j].flags);
4118 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4119 }
4120 if (!skipped) {
4121 set_bit(STRIPE_EXPAND_READY, &sh->state);
4122 set_bit(STRIPE_HANDLE, &sh->state);
4123 }
NeilBrownab69ae12009-03-31 15:26:47 +11004124 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004125 }
4126 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004127 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004128 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004129 else
NeilBrown7a661382009-03-31 15:21:40 +11004130 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004131 spin_unlock_irq(&conf->device_lock);
4132 /* Ok, those stripe are ready. We can start scheduling
4133 * reads on the source stripes.
4134 * The source stripes are determined by mapping the first and last
4135 * block on the destination stripes.
4136 */
NeilBrown52c03292006-06-26 00:27:43 -07004137 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004138 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004139 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004140 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004141 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004142 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004143 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004144 if (last_sector >= mddev->dev_sectors)
4145 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004146 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004147 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004148 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4149 set_bit(STRIPE_HANDLE, &sh->state);
4150 release_stripe(sh);
4151 first_sector += STRIPE_SECTORS;
4152 }
NeilBrownab69ae12009-03-31 15:26:47 +11004153 /* Now that the sources are clearly marked, we can release
4154 * the destination stripes
4155 */
4156 while (!list_empty(&stripes)) {
4157 sh = list_entry(stripes.next, struct stripe_head, lru);
4158 list_del_init(&sh->lru);
4159 release_stripe(sh);
4160 }
NeilBrownc6207272008-02-06 01:39:52 -08004161 /* If this takes us to the resync_max point where we have to pause,
4162 * then we need to write out the superblock.
4163 */
NeilBrown7a661382009-03-31 15:21:40 +11004164 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004165 if ((sector_nr - mddev->curr_resync_completed) * 2
4166 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004167 /* Cannot proceed until we've updated the superblock... */
4168 wait_event(conf->wait_for_overlap,
4169 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004170 mddev->reshape_position = conf->reshape_progress;
NeilBrown48606a92009-06-18 09:14:12 +10004171 mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11004172 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004173 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4174 md_wakeup_thread(mddev->thread);
4175 wait_event(mddev->sb_wait,
4176 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4177 || kthread_should_stop());
4178 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004179 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004180 spin_unlock_irq(&conf->device_lock);
4181 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004182 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004183 }
NeilBrown7a661382009-03-31 15:21:40 +11004184 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004185}
4186
4187/* FIXME go_faster isn't used */
4188static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4189{
4190 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4191 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004192 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07004193 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004194 int still_degraded = 0;
4195 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
NeilBrown72626682005-09-09 16:23:54 -07004197 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 /* just being told to finish up .. nothing much to do */
4199 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004200
NeilBrown29269552006-03-27 01:18:10 -08004201 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4202 end_reshape(conf);
4203 return 0;
4204 }
NeilBrown72626682005-09-09 16:23:54 -07004205
4206 if (mddev->curr_resync < max_sector) /* aborted */
4207 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4208 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004209 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004210 conf->fullsync = 0;
4211 bitmap_close_sync(mddev->bitmap);
4212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 return 0;
4214 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004215
NeilBrown64bd6602009-08-03 10:59:58 +10004216 /* Allow raid5_quiesce to complete */
4217 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4218
NeilBrown52c03292006-06-26 00:27:43 -07004219 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4220 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004221
NeilBrownc6207272008-02-06 01:39:52 -08004222 /* No need to check resync_max as we never do more than one
4223 * stripe, and as resync_max will always be on a chunk boundary,
4224 * if the check in md_do_sync didn't fire, there is no chance
4225 * of overstepping resync_max here
4226 */
4227
NeilBrown16a53ec2006-06-26 00:27:38 -07004228 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 * to resync, then assert that we are finished, because there is
4230 * nothing we can do.
4231 */
NeilBrown3285edf2006-06-26 00:27:55 -07004232 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004233 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004234 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004235 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return rv;
4237 }
NeilBrown72626682005-09-09 16:23:54 -07004238 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004239 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004240 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4241 /* we can skip this block, and probably more */
4242 sync_blocks /= STRIPE_SECTORS;
4243 *skipped = 1;
4244 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
NeilBrownb47490c2008-02-06 01:39:50 -08004247
4248 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4249
NeilBrowna8c906c2009-06-09 14:39:59 +10004250 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004252 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004254 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004256 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004258 /* Need to check if array will still be degraded after recovery/resync
4259 * We don't need to check the 'failed' flag as when that gets set,
4260 * recovery aborts.
4261 */
NeilBrownf001a702009-06-09 14:30:31 +10004262 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004263 if (conf->disks[i].rdev == NULL)
4264 still_degraded = 1;
4265
4266 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4267
4268 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 set_bit(STRIPE_SYNCING, &sh->state);
4270 clear_bit(STRIPE_INSYNC, &sh->state);
4271 spin_unlock(&sh->lock);
4272
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004273 /* wait for any blocked device to be handled */
Dan Williams36d1c642009-07-14 11:48:22 -07004274 while (unlikely(!handle_stripe(sh)))
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004275 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 release_stripe(sh);
4277
4278 return STRIPE_SECTORS;
4279}
4280
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004281static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4282{
4283 /* We may not be able to submit a whole bio at once as there
4284 * may not be enough stripe_heads available.
4285 * We cannot pre-allocate enough stripe_heads as we may need
4286 * more than exist in the cache (if we allow ever large chunks).
4287 * So we do one stripe head at a time and record in
4288 * ->bi_hw_segments how many have been done.
4289 *
4290 * We *know* that this entire raid_bio is in one chunk, so
4291 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4292 */
4293 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004294 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004295 sector_t sector, logical_sector, last_sector;
4296 int scnt = 0;
4297 int remaining;
4298 int handled = 0;
4299
4300 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004301 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004302 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004303 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4304
4305 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004306 logical_sector += STRIPE_SECTORS,
4307 sector += STRIPE_SECTORS,
4308 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004309
Jens Axboe960e7392008-08-15 10:41:18 +02004310 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004311 /* already done this stripe */
4312 continue;
4313
NeilBrowna8c906c2009-06-09 14:39:59 +10004314 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004315
4316 if (!sh) {
4317 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004318 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004319 conf->retry_read_aligned = raid_bio;
4320 return handled;
4321 }
4322
4323 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004324 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4325 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004326 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004327 conf->retry_read_aligned = raid_bio;
4328 return handled;
4329 }
4330
Dan Williams36d1c642009-07-14 11:48:22 -07004331 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004332 release_stripe(sh);
4333 handled++;
4334 }
4335 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004336 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004337 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004338 if (remaining == 0)
4339 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004340 if (atomic_dec_and_test(&conf->active_aligned_reads))
4341 wake_up(&conf->wait_for_stripe);
4342 return handled;
4343}
4344
Dan Williams07a3b412009-08-29 19:13:13 -07004345#ifdef CONFIG_MULTICORE_RAID456
4346static void __process_stripe(void *param, async_cookie_t cookie)
4347{
4348 struct stripe_head *sh = param;
4349
4350 handle_stripe(sh);
4351 release_stripe(sh);
4352}
4353
4354static void process_stripe(struct stripe_head *sh, struct list_head *domain)
4355{
4356 async_schedule_domain(__process_stripe, sh, domain);
4357}
4358
4359static void synchronize_stripe_processing(struct list_head *domain)
4360{
4361 async_synchronize_full_domain(domain);
4362}
4363#else
4364static void process_stripe(struct stripe_head *sh, struct list_head *domain)
4365{
4366 handle_stripe(sh);
4367 release_stripe(sh);
4368 cond_resched();
4369}
4370
4371static void synchronize_stripe_processing(struct list_head *domain)
4372{
4373}
4374#endif
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004375
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377/*
4378 * This is our raid5 kernel thread.
4379 *
4380 * We scan the hash table for stripes which can be handled now.
4381 * During the scan, completed stripes are saved for us by the interrupt
4382 * handler, so that they will not have to wait for our next wakeup.
4383 */
NeilBrown6ed30032008-02-06 01:40:00 -08004384static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
4386 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004387 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 int handled;
Dan Williams07a3b412009-08-29 19:13:13 -07004389 LIST_HEAD(raid_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Dan Williams45b42332007-07-09 11:56:43 -07004391 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
4393 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
4395 handled = 0;
4396 spin_lock_irq(&conf->device_lock);
4397 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004398 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
NeilBrownae3c20c2006-07-10 04:44:17 -07004400 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004401 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004402 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004403 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004404 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004405 conf->seq_write = seq;
4406 activate_bit_delay(conf);
4407 }
4408
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004409 while ((bio = remove_bio_from_retry(conf))) {
4410 int ok;
4411 spin_unlock_irq(&conf->device_lock);
4412 ok = retry_aligned_read(conf, bio);
4413 spin_lock_irq(&conf->device_lock);
4414 if (!ok)
4415 break;
4416 handled++;
4417 }
4418
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004419 sh = __get_priority_stripe(conf);
4420
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004421 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 spin_unlock_irq(&conf->device_lock);
4424
4425 handled++;
Dan Williams07a3b412009-08-29 19:13:13 -07004426 process_stripe(sh, &raid_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428 spin_lock_irq(&conf->device_lock);
4429 }
Dan Williams45b42332007-07-09 11:56:43 -07004430 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 spin_unlock_irq(&conf->device_lock);
4433
Dan Williams07a3b412009-08-29 19:13:13 -07004434 synchronize_stripe_processing(&raid_domain);
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004435 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 unplug_slaves(mddev);
4437
Dan Williams45b42332007-07-09 11:56:43 -07004438 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
4440
NeilBrown3f294f42005-11-08 21:39:25 -08004441static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004442raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004443{
NeilBrown070ec552009-06-16 16:54:21 +10004444 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004445 if (conf)
4446 return sprintf(page, "%d\n", conf->max_nr_stripes);
4447 else
4448 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004449}
4450
4451static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004452raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004453{
NeilBrown070ec552009-06-16 16:54:21 +10004454 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004455 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004456 int err;
4457
NeilBrown3f294f42005-11-08 21:39:25 -08004458 if (len >= PAGE_SIZE)
4459 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004460 if (!conf)
4461 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004462
Dan Williams4ef197d82008-04-28 02:15:54 -07004463 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004464 return -EINVAL;
4465 if (new <= 16 || new > 32768)
4466 return -EINVAL;
4467 while (new < conf->max_nr_stripes) {
4468 if (drop_one_stripe(conf))
4469 conf->max_nr_stripes--;
4470 else
4471 break;
4472 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004473 err = md_allow_write(mddev);
4474 if (err)
4475 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004476 while (new > conf->max_nr_stripes) {
4477 if (grow_one_stripe(conf))
4478 conf->max_nr_stripes++;
4479 else break;
4480 }
4481 return len;
4482}
NeilBrown007583c2005-11-08 21:39:30 -08004483
NeilBrown96de1e62005-11-08 21:39:39 -08004484static struct md_sysfs_entry
4485raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4486 raid5_show_stripe_cache_size,
4487 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004488
4489static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004490raid5_show_preread_threshold(mddev_t *mddev, char *page)
4491{
NeilBrown070ec552009-06-16 16:54:21 +10004492 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004493 if (conf)
4494 return sprintf(page, "%d\n", conf->bypass_threshold);
4495 else
4496 return 0;
4497}
4498
4499static ssize_t
4500raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4501{
NeilBrown070ec552009-06-16 16:54:21 +10004502 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004503 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004504 if (len >= PAGE_SIZE)
4505 return -EINVAL;
4506 if (!conf)
4507 return -ENODEV;
4508
Dan Williams4ef197d82008-04-28 02:15:54 -07004509 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004510 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004511 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004512 return -EINVAL;
4513 conf->bypass_threshold = new;
4514 return len;
4515}
4516
4517static struct md_sysfs_entry
4518raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4519 S_IRUGO | S_IWUSR,
4520 raid5_show_preread_threshold,
4521 raid5_store_preread_threshold);
4522
4523static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004524stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004525{
NeilBrown070ec552009-06-16 16:54:21 +10004526 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004527 if (conf)
4528 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4529 else
4530 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004531}
4532
NeilBrown96de1e62005-11-08 21:39:39 -08004533static struct md_sysfs_entry
4534raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004535
NeilBrown007583c2005-11-08 21:39:30 -08004536static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004537 &raid5_stripecache_size.attr,
4538 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004539 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004540 NULL,
4541};
NeilBrown007583c2005-11-08 21:39:30 -08004542static struct attribute_group raid5_attrs_group = {
4543 .name = NULL,
4544 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004545};
4546
Dan Williams80c3a6c2009-03-17 18:10:40 -07004547static sector_t
4548raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4549{
NeilBrown070ec552009-06-16 16:54:21 +10004550 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004551
4552 if (!sectors)
4553 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004554 if (!raid_disks) {
4555 /* size is defined by the smallest of previous and new size */
4556 if (conf->raid_disks < conf->previous_raid_disks)
4557 raid_disks = conf->raid_disks;
4558 else
4559 raid_disks = conf->previous_raid_disks;
4560 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004561
Andre Noll9d8f0362009-06-18 08:45:01 +10004562 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004563 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004564 return sectors * (raid_disks - conf->max_degraded);
4565}
4566
Dan Williams36d1c642009-07-14 11:48:22 -07004567static void raid5_free_percpu(raid5_conf_t *conf)
4568{
4569 struct raid5_percpu *percpu;
4570 unsigned long cpu;
4571
4572 if (!conf->percpu)
4573 return;
4574
4575 get_online_cpus();
4576 for_each_possible_cpu(cpu) {
4577 percpu = per_cpu_ptr(conf->percpu, cpu);
4578 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004579 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004580 }
4581#ifdef CONFIG_HOTPLUG_CPU
4582 unregister_cpu_notifier(&conf->cpu_notify);
4583#endif
4584 put_online_cpus();
4585
4586 free_percpu(conf->percpu);
4587}
4588
Dan Williamsa11034b2009-07-14 11:48:16 -07004589static void free_conf(raid5_conf_t *conf)
4590{
4591 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004592 raid5_free_percpu(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07004593 kfree(conf->disks);
4594 kfree(conf->stripe_hashtbl);
4595 kfree(conf);
4596}
4597
Dan Williams36d1c642009-07-14 11:48:22 -07004598#ifdef CONFIG_HOTPLUG_CPU
4599static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4600 void *hcpu)
4601{
4602 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4603 long cpu = (long)hcpu;
4604 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4605
4606 switch (action) {
4607 case CPU_UP_PREPARE:
4608 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004609 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004610 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004611 if (!percpu->scribble)
4612 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4613
4614 if (!percpu->scribble ||
4615 (conf->level == 6 && !percpu->spare_page)) {
4616 safe_put_page(percpu->spare_page);
4617 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004618 pr_err("%s: failed memory allocation for cpu%ld\n",
4619 __func__, cpu);
4620 return NOTIFY_BAD;
4621 }
4622 break;
4623 case CPU_DEAD:
4624 case CPU_DEAD_FROZEN:
4625 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004626 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004627 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004628 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004629 break;
4630 default:
4631 break;
4632 }
4633 return NOTIFY_OK;
4634}
4635#endif
4636
4637static int raid5_alloc_percpu(raid5_conf_t *conf)
4638{
4639 unsigned long cpu;
4640 struct page *spare_page;
4641 struct raid5_percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004642 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004643 int err;
4644
Dan Williams36d1c642009-07-14 11:48:22 -07004645 allcpus = alloc_percpu(struct raid5_percpu);
4646 if (!allcpus)
4647 return -ENOMEM;
4648 conf->percpu = allcpus;
4649
4650 get_online_cpus();
4651 err = 0;
4652 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004653 if (conf->level == 6) {
4654 spare_page = alloc_page(GFP_KERNEL);
4655 if (!spare_page) {
4656 err = -ENOMEM;
4657 break;
4658 }
4659 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4660 }
4661 scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL);
4662 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004663 err = -ENOMEM;
4664 break;
4665 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004666 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004667 }
4668#ifdef CONFIG_HOTPLUG_CPU
4669 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4670 conf->cpu_notify.priority = 0;
4671 if (err == 0)
4672 err = register_cpu_notifier(&conf->cpu_notify);
4673#endif
4674 put_online_cpus();
4675
4676 return err;
4677}
4678
NeilBrown91adb562009-03-31 14:39:39 +11004679static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
4681 raid5_conf_t *conf;
4682 int raid_disk, memory;
4683 mdk_rdev_t *rdev;
4684 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
NeilBrown91adb562009-03-31 14:39:39 +11004686 if (mddev->new_level != 5
4687 && mddev->new_level != 4
4688 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004689 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004690 mdname(mddev), mddev->new_level);
4691 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 }
NeilBrown91adb562009-03-31 14:39:39 +11004693 if ((mddev->new_level == 5
4694 && !algorithm_valid_raid5(mddev->new_layout)) ||
4695 (mddev->new_level == 6
4696 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004697 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004698 mdname(mddev), mddev->new_layout);
4699 return ERR_PTR(-EIO);
4700 }
4701 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4702 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4703 mdname(mddev), mddev->raid_disks);
4704 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Andre Noll664e7c42009-06-18 08:45:27 +10004707 if (!mddev->new_chunk_sectors ||
4708 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4709 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown91adb562009-03-31 14:39:39 +11004710 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
Andre Noll664e7c42009-06-18 08:45:27 +10004711 mddev->new_chunk_sectors << 9, mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004712 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004713 }
4714
NeilBrown91adb562009-03-31 14:39:39 +11004715 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4716 if (conf == NULL)
4717 goto abort;
4718
4719 conf->raid_disks = mddev->raid_disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004720 conf->scribble_len = scribble_len(conf->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004721 if (mddev->reshape_position == MaxSector)
4722 conf->previous_raid_disks = mddev->raid_disks;
4723 else
4724 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4725
4726 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4727 GFP_KERNEL);
4728 if (!conf->disks)
4729 goto abort;
4730
4731 conf->mddev = mddev;
4732
4733 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4734 goto abort;
4735
Dan Williams36d1c642009-07-14 11:48:22 -07004736 conf->level = mddev->new_level;
4737 if (raid5_alloc_percpu(conf) != 0)
4738 goto abort;
4739
NeilBrown91adb562009-03-31 14:39:39 +11004740 spin_lock_init(&conf->device_lock);
4741 init_waitqueue_head(&conf->wait_for_stripe);
4742 init_waitqueue_head(&conf->wait_for_overlap);
4743 INIT_LIST_HEAD(&conf->handle_list);
4744 INIT_LIST_HEAD(&conf->hold_list);
4745 INIT_LIST_HEAD(&conf->delayed_list);
4746 INIT_LIST_HEAD(&conf->bitmap_list);
4747 INIT_LIST_HEAD(&conf->inactive_list);
4748 atomic_set(&conf->active_stripes, 0);
4749 atomic_set(&conf->preread_active_stripes, 0);
4750 atomic_set(&conf->active_aligned_reads, 0);
4751 conf->bypass_threshold = BYPASS_THRESHOLD;
4752
4753 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4754
4755 list_for_each_entry(rdev, &mddev->disks, same_set) {
4756 raid_disk = rdev->raid_disk;
4757 if (raid_disk >= conf->raid_disks
4758 || raid_disk < 0)
4759 continue;
4760 disk = conf->disks + raid_disk;
4761
4762 disk->rdev = rdev;
4763
4764 if (test_bit(In_sync, &rdev->flags)) {
4765 char b[BDEVNAME_SIZE];
4766 printk(KERN_INFO "raid5: device %s operational as raid"
4767 " disk %d\n", bdevname(rdev->bdev,b),
4768 raid_disk);
4769 } else
4770 /* Cannot rely on bitmap to complete recovery */
4771 conf->fullsync = 1;
4772 }
4773
Andre Noll09c9e5f2009-06-18 08:45:55 +10004774 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004775 conf->level = mddev->new_level;
4776 if (conf->level == 6)
4777 conf->max_degraded = 2;
4778 else
4779 conf->max_degraded = 1;
4780 conf->algorithm = mddev->new_layout;
4781 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004782 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004783 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004784 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004785 conf->prev_algo = mddev->layout;
4786 }
NeilBrown91adb562009-03-31 14:39:39 +11004787
4788 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4789 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4790 if (grow_stripes(conf, conf->max_nr_stripes)) {
4791 printk(KERN_ERR
4792 "raid5: couldn't allocate %dkB for buffers\n", memory);
4793 goto abort;
4794 } else
4795 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4796 memory, mdname(mddev));
4797
4798 conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4799 if (!conf->thread) {
4800 printk(KERN_ERR
4801 "raid5: couldn't allocate thread for %s\n",
4802 mdname(mddev));
4803 goto abort;
4804 }
4805
4806 return conf;
4807
4808 abort:
4809 if (conf) {
Dan Williamsa11034b2009-07-14 11:48:16 -07004810 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004811 return ERR_PTR(-EIO);
4812 } else
4813 return ERR_PTR(-ENOMEM);
4814}
4815
4816static int run(mddev_t *mddev)
4817{
4818 raid5_conf_t *conf;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004819 int working_disks = 0, chunk_size;
NeilBrown91adb562009-03-31 14:39:39 +11004820 mdk_rdev_t *rdev;
4821
Andre Noll8c6ac862009-06-18 08:48:06 +10004822 if (mddev->recovery_cp != MaxSector)
4823 printk(KERN_NOTICE "raid5: %s is not clean"
4824 " -- starting background reconstruction\n",
4825 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004826 if (mddev->reshape_position != MaxSector) {
4827 /* Check that we can continue the reshape.
4828 * Currently only disks can change, it must
4829 * increase, and we must be past the point where
4830 * a stripe over-writes itself
4831 */
4832 sector_t here_new, here_old;
4833 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004834 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004835
NeilBrown88ce4932009-03-31 15:24:23 +11004836 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004837 printk(KERN_ERR "raid5: %s: unsupported reshape "
4838 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004839 mdname(mddev));
4840 return -EINVAL;
4841 }
NeilBrownf6705572006-03-27 01:18:11 -08004842 old_disks = mddev->raid_disks - mddev->delta_disks;
4843 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004844 * further up in new geometry must map after here in old
4845 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004846 */
4847 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004848 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004849 (mddev->raid_disks - max_degraded))) {
4850 printk(KERN_ERR "raid5: reshape_position not "
4851 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004852 return -EINVAL;
4853 }
4854 /* here_new is the stripe we will write to */
4855 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004856 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004857 (old_disks-max_degraded));
4858 /* here_old is the first stripe that we might need to read
4859 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004860 if (mddev->delta_disks == 0) {
4861 /* We cannot be sure it is safe to start an in-place
4862 * reshape. It is only safe if user-space if monitoring
4863 * and taking constant backups.
4864 * mdadm always starts a situation like this in
4865 * readonly mode so it can take control before
4866 * allowing any writes. So just check for that.
4867 */
4868 if ((here_new * mddev->new_chunk_sectors !=
4869 here_old * mddev->chunk_sectors) ||
4870 mddev->ro == 0) {
4871 printk(KERN_ERR "raid5: in-place reshape must be started"
4872 " in read-only mode - aborting\n");
4873 return -EINVAL;
4874 }
4875 } else if (mddev->delta_disks < 0
4876 ? (here_new * mddev->new_chunk_sectors <=
4877 here_old * mddev->chunk_sectors)
4878 : (here_new * mddev->new_chunk_sectors >=
4879 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004880 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004881 printk(KERN_ERR "raid5: reshape_position too early for "
4882 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004883 return -EINVAL;
4884 }
4885 printk(KERN_INFO "raid5: reshape will continue\n");
4886 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004887 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004888 BUG_ON(mddev->level != mddev->new_level);
4889 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004890 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004891 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004892 }
4893
NeilBrown245f46c2009-03-31 14:39:39 +11004894 if (mddev->private == NULL)
4895 conf = setup_conf(mddev);
4896 else
4897 conf = mddev->private;
4898
NeilBrown91adb562009-03-31 14:39:39 +11004899 if (IS_ERR(conf))
4900 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004901
NeilBrown91adb562009-03-31 14:39:39 +11004902 mddev->thread = conf->thread;
4903 conf->thread = NULL;
4904 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004907 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 */
NeilBrown91adb562009-03-31 14:39:39 +11004909 list_for_each_entry(rdev, &mddev->disks, same_set)
4910 if (rdev->raid_disk >= 0 &&
4911 test_bit(In_sync, &rdev->flags))
4912 working_disks++;
4913
NeilBrown02c2de82006-10-03 01:15:47 -07004914 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
NeilBrown16a53ec2006-06-26 00:27:38 -07004916 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 printk(KERN_ERR "raid5: not enough operational devices for %s"
4918 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004919 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 goto abort;
4921 }
4922
NeilBrown91adb562009-03-31 14:39:39 +11004923 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004924 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004925 mddev->resync_max_sectors = mddev->dev_sectors;
4926
NeilBrown16a53ec2006-06-26 00:27:38 -07004927 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004929 if (mddev->ok_start_degraded)
4930 printk(KERN_WARNING
4931 "raid5: starting dirty degraded array: %s"
4932 "- data corruption possible.\n",
4933 mdname(mddev));
4934 else {
4935 printk(KERN_ERR
4936 "raid5: cannot start dirty degraded array for %s\n",
4937 mdname(mddev));
4938 goto abort;
4939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 }
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if (mddev->degraded == 0)
4943 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11004944 " devices, algorithm %d\n", conf->level, mdname(mddev),
4945 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4946 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 else
4948 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4949 " out of %d devices, algorithm %d\n", conf->level,
4950 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11004951 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 print_raid5_conf(conf);
4954
NeilBrownfef9c612009-03-31 15:16:46 +11004955 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08004956 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11004957 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004958 atomic_set(&conf->reshape_stripes, 0);
4959 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4960 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4961 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4962 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4963 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4964 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004965 }
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004968 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 */
4970 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004971 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4972 int stripe = data_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10004973 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4975 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4976 }
4977
4978 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004979 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4980 printk(KERN_WARNING
4981 "raid5: failed to create sysfs attributes for %s\n",
4982 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004983
NeilBrown91adb562009-03-31 14:39:39 +11004984 mddev->queue->queue_lock = &conf->device_lock;
4985
NeilBrown7a5febe2005-05-16 21:53:16 -07004986 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004987 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004988 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004989
Dan Williams1f403622009-03-31 14:59:03 +11004990 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07004991
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004992 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004993 chunk_size = mddev->chunk_sectors << 9;
4994 blk_queue_io_min(mddev->queue, chunk_size);
4995 blk_queue_io_opt(mddev->queue, chunk_size *
4996 (conf->raid_disks - conf->max_degraded));
4997
4998 list_for_each_entry(rdev, &mddev->disks, same_set)
4999 disk_stack_limits(mddev->gendisk, rdev->bdev,
5000 rdev->data_offset << 9);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 return 0;
5003abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11005004 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11005005 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 if (conf) {
5007 print_raid5_conf(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07005008 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
5010 mddev->private = NULL;
5011 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
5012 return -EIO;
5013}
5014
5015
5016
NeilBrown3f294f42005-11-08 21:39:25 -08005017static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018{
5019 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5020
5021 md_unregister_thread(mddev->thread);
5022 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08005023 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08005025 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
Dan Williamsa11034b2009-07-14 11:48:16 -07005026 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 mddev->private = NULL;
5028 return 0;
5029}
5030
Dan Williams45b42332007-07-09 11:56:43 -07005031#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11005032static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033{
5034 int i;
5035
NeilBrown16a53ec2006-06-26 00:27:38 -07005036 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
5037 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
5038 seq_printf(seq, "sh %llu, count %d.\n",
5039 (unsigned long long)sh->sector, atomic_read(&sh->count));
5040 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005041 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07005042 seq_printf(seq, "(cache%d: %p %ld) ",
5043 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005045 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046}
5047
NeilBrownd710e132008-10-13 11:55:12 +11005048static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049{
5050 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08005051 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 int i;
5053
5054 spin_lock_irq(&conf->device_lock);
5055 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005056 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 if (sh->raid_conf != conf)
5058 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005059 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 }
5061 }
5062 spin_unlock_irq(&conf->device_lock);
5063}
5064#endif
5065
NeilBrownd710e132008-10-13 11:55:12 +11005066static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067{
5068 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5069 int i;
5070
Andre Noll9d8f0362009-06-18 08:45:01 +10005071 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5072 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005073 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 for (i = 0; i < conf->raid_disks; i++)
5075 seq_printf (seq, "%s",
5076 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005077 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005079#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005080 seq_printf (seq, "\n");
5081 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082#endif
5083}
5084
5085static void print_raid5_conf (raid5_conf_t *conf)
5086{
5087 int i;
5088 struct disk_info *tmp;
5089
5090 printk("RAID5 conf printout:\n");
5091 if (!conf) {
5092 printk("(conf==NULL)\n");
5093 return;
5094 }
NeilBrown02c2de82006-10-03 01:15:47 -07005095 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
5096 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 for (i = 0; i < conf->raid_disks; i++) {
5099 char b[BDEVNAME_SIZE];
5100 tmp = conf->disks + i;
5101 if (tmp->rdev)
5102 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08005103 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 bdevname(tmp->rdev->bdev,b));
5105 }
5106}
5107
5108static int raid5_spare_active(mddev_t *mddev)
5109{
5110 int i;
5111 raid5_conf_t *conf = mddev->private;
5112 struct disk_info *tmp;
5113
5114 for (i = 0; i < conf->raid_disks; i++) {
5115 tmp = conf->disks + i;
5116 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08005117 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005118 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5119 unsigned long flags;
5120 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07005122 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
5124 }
5125 print_raid5_conf(conf);
5126 return 0;
5127}
5128
5129static int raid5_remove_disk(mddev_t *mddev, int number)
5130{
5131 raid5_conf_t *conf = mddev->private;
5132 int err = 0;
5133 mdk_rdev_t *rdev;
5134 struct disk_info *p = conf->disks + number;
5135
5136 print_raid5_conf(conf);
5137 rdev = p->rdev;
5138 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005139 if (number >= conf->raid_disks &&
5140 conf->reshape_progress == MaxSector)
5141 clear_bit(In_sync, &rdev->flags);
5142
NeilBrownb2d444d2005-11-08 21:39:31 -08005143 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 atomic_read(&rdev->nr_pending)) {
5145 err = -EBUSY;
5146 goto abort;
5147 }
NeilBrowndfc70642008-05-23 13:04:39 -07005148 /* Only remove non-faulty devices if recovery
5149 * isn't possible.
5150 */
5151 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005152 mddev->degraded <= conf->max_degraded &&
5153 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005154 err = -EBUSY;
5155 goto abort;
5156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005158 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 if (atomic_read(&rdev->nr_pending)) {
5160 /* lost the race, try later */
5161 err = -EBUSY;
5162 p->rdev = rdev;
5163 }
5164 }
5165abort:
5166
5167 print_raid5_conf(conf);
5168 return err;
5169}
5170
5171static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5172{
5173 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005174 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 int disk;
5176 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005177 int first = 0;
5178 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
NeilBrown16a53ec2006-06-26 00:27:38 -07005180 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Neil Brown6c2fce22008-06-28 08:31:31 +10005184 if (rdev->raid_disk >= 0)
5185 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
5187 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005188 * find the disk ... but prefer rdev->saved_raid_disk
5189 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005191 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005192 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005193 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5194 disk = rdev->saved_raid_disk;
5195 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005196 disk = first;
5197 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005199 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005201 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005202 if (rdev->saved_raid_disk != disk)
5203 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005204 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 break;
5206 }
5207 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209}
5210
5211static int raid5_resize(mddev_t *mddev, sector_t sectors)
5212{
5213 /* no resync is happening, and there is enough space
5214 * on all devices, so we can resize.
5215 * We need to make sure resync covers any new space.
5216 * If the array is shrinking we should possibly wait until
5217 * any io in the removed space completes, but it hardly seems
5218 * worth it.
5219 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005220 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005221 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5222 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005223 if (mddev->array_sectors >
5224 raid5_size(mddev, sectors, mddev->raid_disks))
5225 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005226 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce6292007-05-09 18:51:36 -07005227 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005228 revalidate_disk(mddev->gendisk);
Andre Noll58c0fed2009-03-31 14:33:13 +11005229 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5230 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5232 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005233 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005234 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 return 0;
5236}
5237
NeilBrown01ee22b2009-06-18 08:47:20 +10005238static int check_stripe_cache(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005239{
NeilBrown01ee22b2009-06-18 08:47:20 +10005240 /* Can only proceed if there are plenty of stripe_heads.
5241 * We need a minimum of one full stripe,, and for sensible progress
5242 * it is best to have about 4 times that.
5243 * If we require 4 times, then the default 256 4K stripe_heads will
5244 * allow for chunk sizes up to 256K, which is probably OK.
5245 * If the chunk size is greater, user-space should request more
5246 * stripe_heads first.
5247 */
5248 raid5_conf_t *conf = mddev->private;
5249 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5250 > conf->max_nr_stripes ||
5251 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5252 > conf->max_nr_stripes) {
5253 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
5254 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5255 / STRIPE_SIZE)*4);
5256 return 0;
5257 }
5258 return 1;
5259}
5260
NeilBrown50ac1682009-06-18 08:47:55 +10005261static int check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005262{
NeilBrown070ec552009-06-16 16:54:21 +10005263 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005264
NeilBrown88ce4932009-03-31 15:24:23 +11005265 if (mddev->delta_disks == 0 &&
5266 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005267 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005268 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005269 if (mddev->bitmap)
5270 /* Cannot grow a bitmap yet */
5271 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11005272 if (mddev->degraded > conf->max_degraded)
5273 return -EINVAL;
5274 if (mddev->delta_disks < 0) {
5275 /* We might be able to shrink, but the devices must
5276 * be made bigger first.
5277 * For raid6, 4 is the minimum size.
5278 * Otherwise 2 is the minimum
5279 */
5280 int min = 2;
5281 if (mddev->level == 6)
5282 min = 4;
5283 if (mddev->raid_disks + mddev->delta_disks < min)
5284 return -EINVAL;
5285 }
NeilBrown29269552006-03-27 01:18:10 -08005286
NeilBrown01ee22b2009-06-18 08:47:20 +10005287 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005288 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005289
NeilBrownec32a2b2009-03-31 15:17:38 +11005290 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005291}
5292
5293static int raid5_start_reshape(mddev_t *mddev)
5294{
NeilBrown070ec552009-06-16 16:54:21 +10005295 raid5_conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08005296 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005297 int spares = 0;
5298 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005299 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005300
NeilBrownf4168852007-02-28 20:11:53 -08005301 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005302 return -EBUSY;
5303
NeilBrown01ee22b2009-06-18 08:47:20 +10005304 if (!check_stripe_cache(mddev))
5305 return -ENOSPC;
5306
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005307 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005308 if (rdev->raid_disk < 0 &&
5309 !test_bit(Faulty, &rdev->flags))
5310 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005311
NeilBrownf4168852007-02-28 20:11:53 -08005312 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005313 /* Not enough devices even to make a degraded array
5314 * of that size
5315 */
5316 return -EINVAL;
5317
NeilBrownec32a2b2009-03-31 15:17:38 +11005318 /* Refuse to reduce size of the array. Any reductions in
5319 * array size must be through explicit setting of array_size
5320 * attribute.
5321 */
5322 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5323 < mddev->array_sectors) {
5324 printk(KERN_ERR "md: %s: array size must be reduced "
5325 "before number of disks\n", mdname(mddev));
5326 return -EINVAL;
5327 }
5328
NeilBrownf6705572006-03-27 01:18:11 -08005329 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005330 spin_lock_irq(&conf->device_lock);
5331 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005332 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005333 conf->prev_chunk_sectors = conf->chunk_sectors;
5334 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005335 conf->prev_algo = conf->algorithm;
5336 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005337 if (mddev->delta_disks < 0)
5338 conf->reshape_progress = raid5_size(mddev, 0, 0);
5339 else
5340 conf->reshape_progress = 0;
5341 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005342 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005343 spin_unlock_irq(&conf->device_lock);
5344
5345 /* Add some new drives, as many as will fit.
5346 * We know there are enough to make the newly sized array work.
5347 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005348 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005349 if (rdev->raid_disk < 0 &&
5350 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005351 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005352 char nm[20];
5353 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08005354 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005355 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005356 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005357 if (sysfs_create_link(&mddev->kobj,
5358 &rdev->kobj, nm))
5359 printk(KERN_WARNING
5360 "raid5: failed to create "
5361 " link %s for %s\n",
5362 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005363 } else
5364 break;
5365 }
5366
NeilBrownec32a2b2009-03-31 15:17:38 +11005367 if (mddev->delta_disks > 0) {
5368 spin_lock_irqsave(&conf->device_lock, flags);
5369 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
5370 - added_devices;
5371 spin_unlock_irqrestore(&conf->device_lock, flags);
5372 }
NeilBrown63c70c42006-03-27 01:18:13 -08005373 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005374 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005375 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005376
NeilBrown29269552006-03-27 01:18:10 -08005377 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5378 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5379 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5380 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5381 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5382 "%s_reshape");
5383 if (!mddev->sync_thread) {
5384 mddev->recovery = 0;
5385 spin_lock_irq(&conf->device_lock);
5386 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005387 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005388 spin_unlock_irq(&conf->device_lock);
5389 return -EAGAIN;
5390 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005391 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005392 md_wakeup_thread(mddev->sync_thread);
5393 md_new_event(mddev);
5394 return 0;
5395}
NeilBrown29269552006-03-27 01:18:10 -08005396
NeilBrownec32a2b2009-03-31 15:17:38 +11005397/* This is called from the reshape thread and should make any
5398 * changes needed in 'conf'
5399 */
NeilBrown29269552006-03-27 01:18:10 -08005400static void end_reshape(raid5_conf_t *conf)
5401{
NeilBrown29269552006-03-27 01:18:10 -08005402
NeilBrownf6705572006-03-27 01:18:11 -08005403 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005404
NeilBrownf6705572006-03-27 01:18:11 -08005405 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005406 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005407 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005408 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005409 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005410
5411 /* read-ahead size must cover two whole stripes, which is
5412 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5413 */
5414 {
NeilBrowncea9c222009-03-31 15:15:05 +11005415 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005416 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005417 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005418 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5419 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5420 }
NeilBrown29269552006-03-27 01:18:10 -08005421 }
NeilBrown29269552006-03-27 01:18:10 -08005422}
5423
NeilBrownec32a2b2009-03-31 15:17:38 +11005424/* This is called from the raid5d thread with mddev_lock held.
5425 * It makes config changes to the device.
5426 */
NeilBrowncea9c222009-03-31 15:15:05 +11005427static void raid5_finish_reshape(mddev_t *mddev)
5428{
NeilBrown070ec552009-06-16 16:54:21 +10005429 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005430
5431 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5432
NeilBrownec32a2b2009-03-31 15:17:38 +11005433 if (mddev->delta_disks > 0) {
5434 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5435 set_capacity(mddev->gendisk, mddev->array_sectors);
5436 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005437 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005438 } else {
5439 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005440 mddev->degraded = conf->raid_disks;
5441 for (d = 0; d < conf->raid_disks ; d++)
5442 if (conf->disks[d].rdev &&
5443 test_bit(In_sync,
5444 &conf->disks[d].rdev->flags))
5445 mddev->degraded--;
5446 for (d = conf->raid_disks ;
5447 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005448 d++) {
5449 mdk_rdev_t *rdev = conf->disks[d].rdev;
5450 if (rdev && raid5_remove_disk(mddev, d) == 0) {
5451 char nm[20];
5452 sprintf(nm, "rd%d", rdev->raid_disk);
5453 sysfs_remove_link(&mddev->kobj, nm);
5454 rdev->raid_disk = -1;
5455 }
5456 }
NeilBrowncea9c222009-03-31 15:15:05 +11005457 }
NeilBrown88ce4932009-03-31 15:24:23 +11005458 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005459 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005460 mddev->reshape_position = MaxSector;
5461 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005462 }
5463}
5464
NeilBrown72626682005-09-09 16:23:54 -07005465static void raid5_quiesce(mddev_t *mddev, int state)
5466{
NeilBrown070ec552009-06-16 16:54:21 +10005467 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005468
5469 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005470 case 2: /* resume for a suspend */
5471 wake_up(&conf->wait_for_overlap);
5472 break;
5473
NeilBrown72626682005-09-09 16:23:54 -07005474 case 1: /* stop all writes */
5475 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005476 /* '2' tells resync/reshape to pause so that all
5477 * active stripes can drain
5478 */
5479 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005480 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005481 atomic_read(&conf->active_stripes) == 0 &&
5482 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005483 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005484 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005485 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005486 /* allow reshape to continue */
5487 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005488 break;
5489
5490 case 0: /* re-enable writes */
5491 spin_lock_irq(&conf->device_lock);
5492 conf->quiesce = 0;
5493 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005494 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005495 spin_unlock_irq(&conf->device_lock);
5496 break;
5497 }
NeilBrown72626682005-09-09 16:23:54 -07005498}
NeilBrownb15c2e52006-01-06 00:20:16 -08005499
NeilBrownd562b0c2009-03-31 14:39:39 +11005500
5501static void *raid5_takeover_raid1(mddev_t *mddev)
5502{
5503 int chunksect;
5504
5505 if (mddev->raid_disks != 2 ||
5506 mddev->degraded > 1)
5507 return ERR_PTR(-EINVAL);
5508
5509 /* Should check if there are write-behind devices? */
5510
5511 chunksect = 64*2; /* 64K by default */
5512
5513 /* The array must be an exact multiple of chunksize */
5514 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5515 chunksect >>= 1;
5516
5517 if ((chunksect<<9) < STRIPE_SIZE)
5518 /* array size does not allow a suitable chunk size */
5519 return ERR_PTR(-EINVAL);
5520
5521 mddev->new_level = 5;
5522 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005523 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005524
5525 return setup_conf(mddev);
5526}
5527
NeilBrownfc9739c2009-03-31 14:57:20 +11005528static void *raid5_takeover_raid6(mddev_t *mddev)
5529{
5530 int new_layout;
5531
5532 switch (mddev->layout) {
5533 case ALGORITHM_LEFT_ASYMMETRIC_6:
5534 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5535 break;
5536 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5537 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5538 break;
5539 case ALGORITHM_LEFT_SYMMETRIC_6:
5540 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5541 break;
5542 case ALGORITHM_RIGHT_SYMMETRIC_6:
5543 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5544 break;
5545 case ALGORITHM_PARITY_0_6:
5546 new_layout = ALGORITHM_PARITY_0;
5547 break;
5548 case ALGORITHM_PARITY_N:
5549 new_layout = ALGORITHM_PARITY_N;
5550 break;
5551 default:
5552 return ERR_PTR(-EINVAL);
5553 }
5554 mddev->new_level = 5;
5555 mddev->new_layout = new_layout;
5556 mddev->delta_disks = -1;
5557 mddev->raid_disks -= 1;
5558 return setup_conf(mddev);
5559}
5560
NeilBrownd562b0c2009-03-31 14:39:39 +11005561
NeilBrown50ac1682009-06-18 08:47:55 +10005562static int raid5_check_reshape(mddev_t *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005563{
NeilBrown88ce4932009-03-31 15:24:23 +11005564 /* For a 2-drive array, the layout and chunk size can be changed
5565 * immediately as not restriping is needed.
5566 * For larger arrays we record the new value - after validation
5567 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005568 */
NeilBrown070ec552009-06-16 16:54:21 +10005569 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005570 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005571
NeilBrown597a7112009-06-18 08:47:42 +10005572 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005573 return -EINVAL;
5574 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005575 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005576 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005577 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005578 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005579 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005580 /* not factor of array size */
5581 return -EINVAL;
5582 }
5583
5584 /* They look valid */
5585
NeilBrown88ce4932009-03-31 15:24:23 +11005586 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005587 /* can make the change immediately */
5588 if (mddev->new_layout >= 0) {
5589 conf->algorithm = mddev->new_layout;
5590 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005591 }
5592 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005593 conf->chunk_sectors = new_chunk ;
5594 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005595 }
5596 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5597 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005598 }
NeilBrown50ac1682009-06-18 08:47:55 +10005599 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005600}
5601
NeilBrown50ac1682009-06-18 08:47:55 +10005602static int raid6_check_reshape(mddev_t *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005603{
NeilBrown597a7112009-06-18 08:47:42 +10005604 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005605
NeilBrown597a7112009-06-18 08:47:42 +10005606 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005607 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005608 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005609 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005610 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005611 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005612 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005613 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005614 /* not factor of array size */
5615 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005616 }
NeilBrown88ce4932009-03-31 15:24:23 +11005617
5618 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005619 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005620}
5621
NeilBrownd562b0c2009-03-31 14:39:39 +11005622static void *raid5_takeover(mddev_t *mddev)
5623{
5624 /* raid5 can take over:
5625 * raid0 - if all devices are the same - make it a raid4 layout
5626 * raid1 - if there are two drives. We need to know the chunk size
5627 * raid4 - trivial - just use a raid4 layout.
5628 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005629 */
5630
5631 if (mddev->level == 1)
5632 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005633 if (mddev->level == 4) {
5634 mddev->new_layout = ALGORITHM_PARITY_N;
5635 mddev->new_level = 5;
5636 return setup_conf(mddev);
5637 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005638 if (mddev->level == 6)
5639 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005640
5641 return ERR_PTR(-EINVAL);
5642}
5643
5644
NeilBrown245f46c2009-03-31 14:39:39 +11005645static struct mdk_personality raid5_personality;
5646
5647static void *raid6_takeover(mddev_t *mddev)
5648{
5649 /* Currently can only take over a raid5. We map the
5650 * personality to an equivalent raid6 personality
5651 * with the Q block at the end.
5652 */
5653 int new_layout;
5654
5655 if (mddev->pers != &raid5_personality)
5656 return ERR_PTR(-EINVAL);
5657 if (mddev->degraded > 1)
5658 return ERR_PTR(-EINVAL);
5659 if (mddev->raid_disks > 253)
5660 return ERR_PTR(-EINVAL);
5661 if (mddev->raid_disks < 3)
5662 return ERR_PTR(-EINVAL);
5663
5664 switch (mddev->layout) {
5665 case ALGORITHM_LEFT_ASYMMETRIC:
5666 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5667 break;
5668 case ALGORITHM_RIGHT_ASYMMETRIC:
5669 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5670 break;
5671 case ALGORITHM_LEFT_SYMMETRIC:
5672 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5673 break;
5674 case ALGORITHM_RIGHT_SYMMETRIC:
5675 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5676 break;
5677 case ALGORITHM_PARITY_0:
5678 new_layout = ALGORITHM_PARITY_0_6;
5679 break;
5680 case ALGORITHM_PARITY_N:
5681 new_layout = ALGORITHM_PARITY_N;
5682 break;
5683 default:
5684 return ERR_PTR(-EINVAL);
5685 }
5686 mddev->new_level = 6;
5687 mddev->new_layout = new_layout;
5688 mddev->delta_disks = 1;
5689 mddev->raid_disks += 1;
5690 return setup_conf(mddev);
5691}
5692
5693
NeilBrown16a53ec2006-06-26 00:27:38 -07005694static struct mdk_personality raid6_personality =
5695{
5696 .name = "raid6",
5697 .level = 6,
5698 .owner = THIS_MODULE,
5699 .make_request = make_request,
5700 .run = run,
5701 .stop = stop,
5702 .status = status,
5703 .error_handler = error,
5704 .hot_add_disk = raid5_add_disk,
5705 .hot_remove_disk= raid5_remove_disk,
5706 .spare_active = raid5_spare_active,
5707 .sync_request = sync_request,
5708 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005709 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005710 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005711 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005712 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005713 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005714 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005715};
NeilBrown2604b702006-01-06 00:20:36 -08005716static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717{
5718 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005719 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 .owner = THIS_MODULE,
5721 .make_request = make_request,
5722 .run = run,
5723 .stop = stop,
5724 .status = status,
5725 .error_handler = error,
5726 .hot_add_disk = raid5_add_disk,
5727 .hot_remove_disk= raid5_remove_disk,
5728 .spare_active = raid5_spare_active,
5729 .sync_request = sync_request,
5730 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005731 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005732 .check_reshape = raid5_check_reshape,
5733 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005734 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005735 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005736 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737};
5738
NeilBrown2604b702006-01-06 00:20:36 -08005739static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740{
NeilBrown2604b702006-01-06 00:20:36 -08005741 .name = "raid4",
5742 .level = 4,
5743 .owner = THIS_MODULE,
5744 .make_request = make_request,
5745 .run = run,
5746 .stop = stop,
5747 .status = status,
5748 .error_handler = error,
5749 .hot_add_disk = raid5_add_disk,
5750 .hot_remove_disk= raid5_remove_disk,
5751 .spare_active = raid5_spare_active,
5752 .sync_request = sync_request,
5753 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005754 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005755 .check_reshape = raid5_check_reshape,
5756 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005757 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005758 .quiesce = raid5_quiesce,
5759};
5760
5761static int __init raid5_init(void)
5762{
NeilBrown16a53ec2006-06-26 00:27:38 -07005763 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005764 register_md_personality(&raid5_personality);
5765 register_md_personality(&raid4_personality);
5766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767}
5768
NeilBrown2604b702006-01-06 00:20:36 -08005769static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770{
NeilBrown16a53ec2006-06-26 00:27:38 -07005771 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005772 unregister_md_personality(&raid5_personality);
5773 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774}
5775
5776module_init(raid5_init);
5777module_exit(raid5_exit);
5778MODULE_LICENSE("GPL");
5779MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005780MODULE_ALIAS("md-raid5");
5781MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005782MODULE_ALIAS("md-level-5");
5783MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005784MODULE_ALIAS("md-personality-8"); /* RAID6 */
5785MODULE_ALIAS("md-raid6");
5786MODULE_ALIAS("md-level-6");
5787
5788/* This used to be two separate modules, they were: */
5789MODULE_ALIAS("raid5");
5790MODULE_ALIAS("raid6");