blob: 1898eda60722594e4f52e7995f8e1b22553de5dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->bm_write is the number of the last batch successfully written.
31 * conf->bm_flush is the number of the last batch that was closed to
32 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is bm_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Dan Williams07a3b412009-08-29 19:13:13 -070050#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070052#include <linux/cpu.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110053#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110054#include "raid5.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Stripe cache
59 */
60
61#define NR_STRIPES 256
62#define STRIPE_SIZE PAGE_SIZE
63#define STRIPE_SHIFT (PAGE_SHIFT - 9)
64#define STRIPE_SECTORS (STRIPE_SIZE>>9)
65#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070066#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080067#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define HASH_MASK (NR_HASH - 1)
69
NeilBrownfccddba2006-01-06 00:20:33 -080070#define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/* bio's attached to a stripe+device for I/O are linked together in bi_sector
73 * order without overlap. There may be several bio's per stripe+device, and
74 * a bio could span several devices.
75 * When walking this list for a particular stripe+device, we must never proceed
76 * beyond a bio that extends past this device, as the next bio might no longer
77 * be valid.
78 * This macro is used to determine the 'next' bio in the list, given the sector
79 * of the current stripe+device
80 */
81#define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
82/*
83 * The following can be used to debug the driver
84 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define RAID5_PARANOIA 1
86#if RAID5_PARANOIA && defined(CONFIG_SMP)
87# define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
88#else
89# define CHECK_DEVLOCK()
90#endif
91
Dan Williams45b42332007-07-09 11:56:43 -070092#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#define inline
94#define __inline__
95#endif
96
Bernd Schubert6be9d492008-05-23 13:04:34 -070097#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
98
Jens Axboe960e7392008-08-15 10:41:18 +020099/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200100 * We maintain a biased count of active stripes in the bottom 16 bits of
101 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200102 */
103static inline int raid5_bi_phys_segments(struct bio *bio)
104{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200105 return bio->bi_phys_segments & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
108static inline int raid5_bi_hw_segments(struct bio *bio)
109{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200110 return (bio->bi_phys_segments >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200111}
112
113static inline int raid5_dec_bi_phys_segments(struct bio *bio)
114{
115 --bio->bi_phys_segments;
116 return raid5_bi_phys_segments(bio);
117}
118
119static inline int raid5_dec_bi_hw_segments(struct bio *bio)
120{
121 unsigned short val = raid5_bi_hw_segments(bio);
122
123 --val;
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200124 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
Jens Axboe960e7392008-08-15 10:41:18 +0200125 return val;
126}
127
128static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
129{
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200130 bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
Jens Axboe960e7392008-08-15 10:41:18 +0200131}
132
NeilBrownd0dabf72009-03-31 14:39:38 +1100133/* Find first data disk in a raid6 stripe */
134static inline int raid6_d0(struct stripe_head *sh)
135{
NeilBrown67cc2b82009-03-31 14:39:38 +1100136 if (sh->ddf_layout)
137 /* ddf always start from first device */
138 return 0;
139 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100140 if (sh->qd_idx == sh->disks - 1)
141 return 0;
142 else
143 return sh->qd_idx + 1;
144}
NeilBrown16a53ec2006-06-26 00:27:38 -0700145static inline int raid6_next_disk(int disk, int raid_disks)
146{
147 disk++;
148 return (disk < raid_disks) ? disk : 0;
149}
Dan Williamsa4456852007-07-09 11:56:43 -0700150
NeilBrownd0dabf72009-03-31 14:39:38 +1100151/* When walking through the disks in a raid5, starting at raid6_d0,
152 * We need to map each disk to a 'slot', where the data disks are slot
153 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
154 * is raid_disks-1. This help does that mapping.
155 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100156static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
157 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100158{
159 int slot;
NeilBrown67cc2b82009-03-31 14:39:38 +1100160
NeilBrownd0dabf72009-03-31 14:39:38 +1100161 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100162 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100163 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100164 return syndrome_disks + 1;
NeilBrownd0dabf72009-03-31 14:39:38 +1100165 slot = (*count)++;
166 return slot;
167}
168
Dan Williamsa4456852007-07-09 11:56:43 -0700169static void return_io(struct bio *return_bi)
170{
171 struct bio *bi = return_bi;
172 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700173
174 return_bi = bi->bi_next;
175 bi->bi_next = NULL;
176 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000177 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700178 bi = return_bi;
179 }
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void print_raid5_conf (raid5_conf_t *conf);
183
Dan Williams600aa102008-06-28 08:32:05 +1000184static int stripe_operations_active(struct stripe_head *sh)
185{
186 return sh->check_state || sh->reconstruct_state ||
187 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
188 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
189}
190
Arjan van de Ven858119e2006-01-14 13:20:43 -0800191static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 if (atomic_dec_and_test(&sh->count)) {
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200194 BUG_ON(!list_empty(&sh->lru));
195 BUG_ON(atomic_read(&conf->active_stripes)==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (test_bit(STRIPE_HANDLE, &sh->state)) {
NeilBrown7c785b72006-07-10 04:44:16 -0700197 if (test_bit(STRIPE_DELAYED, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700199 blk_plug_device(conf->mddev->queue);
200 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
NeilBrownae3c20c2006-07-10 04:44:17 -0700201 sh->bm_seq - conf->seq_write > 0) {
NeilBrown72626682005-09-09 16:23:54 -0700202 list_add_tail(&sh->lru, &conf->bitmap_list);
NeilBrown7c785b72006-07-10 04:44:16 -0700203 blk_plug_device(conf->mddev->queue);
204 } else {
NeilBrown72626682005-09-09 16:23:54 -0700205 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 list_add_tail(&sh->lru, &conf->handle_list);
NeilBrown72626682005-09-09 16:23:54 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 md_wakeup_thread(conf->mddev->thread);
209 } else {
Dan Williams600aa102008-06-28 08:32:05 +1000210 BUG_ON(stripe_operations_active(sh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
212 atomic_dec(&conf->preread_active_stripes);
213 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
214 md_wakeup_thread(conf->mddev->thread);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 atomic_dec(&conf->active_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800217 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
218 list_add_tail(&sh->lru, &conf->inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 wake_up(&conf->wait_for_stripe);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -0800220 if (conf->retry_read_aligned)
221 md_wakeup_thread(conf->mddev->thread);
NeilBrownccfcc3c2006-03-27 01:18:09 -0800222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
225}
NeilBrownd0dabf72009-03-31 14:39:38 +1100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void release_stripe(struct stripe_head *sh)
228{
229 raid5_conf_t *conf = sh->raid_conf;
230 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_lock_irqsave(&conf->device_lock, flags);
233 __release_stripe(conf, sh);
234 spin_unlock_irqrestore(&conf->device_lock, flags);
235}
236
NeilBrownfccddba2006-01-06 00:20:33 -0800237static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Dan Williams45b42332007-07-09 11:56:43 -0700239 pr_debug("remove_hash(), stripe %llu\n",
240 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
NeilBrownfccddba2006-01-06 00:20:33 -0800242 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
NeilBrown16a53ec2006-06-26 00:27:38 -0700245static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
NeilBrownfccddba2006-01-06 00:20:33 -0800247 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dan Williams45b42332007-07-09 11:56:43 -0700249 pr_debug("insert_hash(), stripe %llu\n",
250 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 CHECK_DEVLOCK();
NeilBrownfccddba2006-01-06 00:20:33 -0800253 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256
257/* find an idle stripe, make sure it is unhashed, and return it. */
258static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
259{
260 struct stripe_head *sh = NULL;
261 struct list_head *first;
262
263 CHECK_DEVLOCK();
264 if (list_empty(&conf->inactive_list))
265 goto out;
266 first = conf->inactive_list.next;
267 sh = list_entry(first, struct stripe_head, lru);
268 list_del_init(first);
269 remove_hash(sh);
270 atomic_inc(&conf->active_stripes);
271out:
272 return sh;
273}
274
275static void shrink_buffers(struct stripe_head *sh, int num)
276{
277 struct page *p;
278 int i;
279
280 for (i=0; i<num ; i++) {
281 p = sh->dev[i].page;
282 if (!p)
283 continue;
284 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800285 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287}
288
289static int grow_buffers(struct stripe_head *sh, int num)
290{
291 int i;
292
293 for (i=0; i<num; i++) {
294 struct page *page;
295
296 if (!(page = alloc_page(GFP_KERNEL))) {
297 return 1;
298 }
299 sh->dev[i].page = page;
300 }
301 return 0;
302}
303
NeilBrown784052e2009-03-31 15:19:07 +1100304static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrown911d4ee2009-03-31 14:39:38 +1100305static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
306 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownb5663ba2009-03-31 14:39:38 +1100308static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800311 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200313 BUG_ON(atomic_read(&sh->count) != 0);
314 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000315 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700318 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 (unsigned long long)sh->sector);
320
321 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700322
NeilBrown86b42c72009-03-31 15:19:03 +1100323 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100324 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100326 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 sh->state = 0;
328
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800329
330 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 struct r5dev *dev = &sh->dev[i];
332
Dan Williamsd84e0f12007-01-02 13:52:30 -0700333 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700335 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700337 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 test_bit(R5_LOCKED, &dev->flags));
339 BUG();
340 }
341 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100342 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344 insert_hash(conf, sh);
345}
346
NeilBrown86b42c72009-03-31 15:19:03 +1100347static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
348 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800351 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 CHECK_DEVLOCK();
Dan Williams45b42332007-07-09 11:56:43 -0700354 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800355 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100356 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700358 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return NULL;
360}
361
362static void unplug_slaves(mddev_t *mddev);
Jens Axboe165125e2007-07-24 09:28:11 +0200363static void raid5_unplug_device(struct request_queue *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
NeilBrownb5663ba2009-03-31 14:39:38 +1100365static struct stripe_head *
366get_active_stripe(raid5_conf_t *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000367 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct stripe_head *sh;
370
Dan Williams45b42332007-07-09 11:56:43 -0700371 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 spin_lock_irq(&conf->device_lock);
374
375 do {
NeilBrown72626682005-09-09 16:23:54 -0700376 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000377 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700378 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100379 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!sh) {
381 if (!conf->inactive_blocked)
382 sh = get_free_stripe(conf);
383 if (noblock && sh == NULL)
384 break;
385 if (!sh) {
386 conf->inactive_blocked = 1;
387 wait_event_lock_irq(conf->wait_for_stripe,
388 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800389 (atomic_read(&conf->active_stripes)
390 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 || !conf->inactive_blocked),
392 conf->device_lock,
NeilBrownf4370782006-07-10 04:44:14 -0700393 raid5_unplug_device(conf->mddev->queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 );
395 conf->inactive_blocked = 0;
396 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100397 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else {
399 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100400 BUG_ON(!list_empty(&sh->lru)
401 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 } else {
403 if (!test_bit(STRIPE_HANDLE, &sh->state))
404 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700405 if (list_empty(&sh->lru) &&
406 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700407 BUG();
408 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 }
411 } while (sh == NULL);
412
413 if (sh)
414 atomic_inc(&sh->count);
415
416 spin_unlock_irq(&conf->device_lock);
417 return sh;
418}
419
NeilBrown6712ecf2007-09-27 12:47:43 +0200420static void
421raid5_end_read_request(struct bio *bi, int error);
422static void
423raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700424
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000425static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700426{
427 raid5_conf_t *conf = sh->raid_conf;
428 int i, disks = sh->disks;
429
430 might_sleep();
431
432 for (i = disks; i--; ) {
433 int rw;
434 struct bio *bi;
435 mdk_rdev_t *rdev;
436 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
437 rw = WRITE;
438 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
439 rw = READ;
440 else
441 continue;
442
443 bi = &sh->dev[i].req;
444
445 bi->bi_rw = rw;
446 if (rw == WRITE)
447 bi->bi_end_io = raid5_end_write_request;
448 else
449 bi->bi_end_io = raid5_end_read_request;
450
451 rcu_read_lock();
452 rdev = rcu_dereference(conf->disks[i].rdev);
453 if (rdev && test_bit(Faulty, &rdev->flags))
454 rdev = NULL;
455 if (rdev)
456 atomic_inc(&rdev->nr_pending);
457 rcu_read_unlock();
458
459 if (rdev) {
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000460 if (s->syncing || s->expanding || s->expanded)
Dan Williams91c00922007-01-02 13:52:30 -0700461 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
462
Dan Williams2b7497f2008-06-28 08:31:52 +1000463 set_bit(STRIPE_IO_STARTED, &sh->state);
464
Dan Williams91c00922007-01-02 13:52:30 -0700465 bi->bi_bdev = rdev->bdev;
466 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700467 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700468 bi->bi_rw, i);
469 atomic_inc(&sh->count);
470 bi->bi_sector = sh->sector + rdev->data_offset;
471 bi->bi_flags = 1 << BIO_UPTODATE;
472 bi->bi_vcnt = 1;
473 bi->bi_max_vecs = 1;
474 bi->bi_idx = 0;
475 bi->bi_io_vec = &sh->dev[i].vec;
476 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
477 bi->bi_io_vec[0].bv_offset = 0;
478 bi->bi_size = STRIPE_SIZE;
479 bi->bi_next = NULL;
480 if (rw == WRITE &&
481 test_bit(R5_ReWrite, &sh->dev[i].flags))
482 atomic_add(STRIPE_SECTORS,
483 &rdev->corrected_errors);
484 generic_make_request(bi);
485 } else {
486 if (rw == WRITE)
487 set_bit(STRIPE_DEGRADED, &sh->state);
488 pr_debug("skip op %ld on disc %d for sector %llu\n",
489 bi->bi_rw, i, (unsigned long long)sh->sector);
490 clear_bit(R5_LOCKED, &sh->dev[i].flags);
491 set_bit(STRIPE_HANDLE, &sh->state);
492 }
493 }
494}
495
496static struct dma_async_tx_descriptor *
497async_copy_data(int frombio, struct bio *bio, struct page *page,
498 sector_t sector, struct dma_async_tx_descriptor *tx)
499{
500 struct bio_vec *bvl;
501 struct page *bio_page;
502 int i;
503 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700504 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700505 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700506
507 if (bio->bi_sector >= sector)
508 page_offset = (signed)(bio->bi_sector - sector) * 512;
509 else
510 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700511
Dan Williams0403e382009-09-08 17:42:50 -0700512 if (frombio)
513 flags |= ASYNC_TX_FENCE;
514 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
515
Dan Williams91c00922007-01-02 13:52:30 -0700516 bio_for_each_segment(bvl, bio, i) {
517 int len = bio_iovec_idx(bio, i)->bv_len;
518 int clen;
519 int b_offset = 0;
520
521 if (page_offset < 0) {
522 b_offset = -page_offset;
523 page_offset += b_offset;
524 len -= b_offset;
525 }
526
527 if (len > 0 && page_offset + len > STRIPE_SIZE)
528 clen = STRIPE_SIZE - page_offset;
529 else
530 clen = len;
531
532 if (clen > 0) {
533 b_offset += bio_iovec_idx(bio, i)->bv_offset;
534 bio_page = bio_iovec_idx(bio, i)->bv_page;
535 if (frombio)
536 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700537 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700538 else
539 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700540 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700541 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700542 /* chain the operations */
543 submit.depend_tx = tx;
544
Dan Williams91c00922007-01-02 13:52:30 -0700545 if (clen < len) /* hit end of page */
546 break;
547 page_offset += len;
548 }
549
550 return tx;
551}
552
553static void ops_complete_biofill(void *stripe_head_ref)
554{
555 struct stripe_head *sh = stripe_head_ref;
556 struct bio *return_bi = NULL;
557 raid5_conf_t *conf = sh->raid_conf;
Dan Williamse4d84902007-09-24 10:06:13 -0700558 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700559
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700560 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700561 (unsigned long long)sh->sector);
562
563 /* clear completed biofills */
Dan Williams83de75c2008-06-28 08:31:58 +1000564 spin_lock_irq(&conf->device_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700565 for (i = sh->disks; i--; ) {
566 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700567
568 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700569 /* and check if we need to reply to a read request,
570 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000571 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700572 */
573 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700574 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700575
Dan Williams91c00922007-01-02 13:52:30 -0700576 BUG_ON(!dev->read);
577 rbi = dev->read;
578 dev->read = NULL;
579 while (rbi && rbi->bi_sector <
580 dev->sector + STRIPE_SECTORS) {
581 rbi2 = r5_next_bio(rbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +0200582 if (!raid5_dec_bi_phys_segments(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700583 rbi->bi_next = return_bi;
584 return_bi = rbi;
585 }
Dan Williams91c00922007-01-02 13:52:30 -0700586 rbi = rbi2;
587 }
588 }
589 }
Dan Williams83de75c2008-06-28 08:31:58 +1000590 spin_unlock_irq(&conf->device_lock);
591 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700592
593 return_io(return_bi);
594
Dan Williamse4d84902007-09-24 10:06:13 -0700595 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700596 release_stripe(sh);
597}
598
599static void ops_run_biofill(struct stripe_head *sh)
600{
601 struct dma_async_tx_descriptor *tx = NULL;
602 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa08abd82009-06-03 11:43:59 -0700603 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700604 int i;
605
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700606 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700607 (unsigned long long)sh->sector);
608
609 for (i = sh->disks; i--; ) {
610 struct r5dev *dev = &sh->dev[i];
611 if (test_bit(R5_Wantfill, &dev->flags)) {
612 struct bio *rbi;
613 spin_lock_irq(&conf->device_lock);
614 dev->read = rbi = dev->toread;
615 dev->toread = NULL;
616 spin_unlock_irq(&conf->device_lock);
617 while (rbi && rbi->bi_sector <
618 dev->sector + STRIPE_SECTORS) {
619 tx = async_copy_data(0, rbi, dev->page,
620 dev->sector, tx);
621 rbi = r5_next_bio(rbi, dev->sector);
622 }
623 }
624 }
625
626 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700627 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
628 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700629}
630
Dan Williams4e7d2c02009-08-29 19:13:11 -0700631static void mark_target_uptodate(struct stripe_head *sh, int target)
632{
633 struct r5dev *tgt;
634
635 if (target < 0)
636 return;
637
638 tgt = &sh->dev[target];
639 set_bit(R5_UPTODATE, &tgt->flags);
640 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
641 clear_bit(R5_Wantcompute, &tgt->flags);
642}
643
Dan Williamsac6b53b2009-07-14 13:40:19 -0700644static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700645{
646 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700647
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700648 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700649 (unsigned long long)sh->sector);
650
Dan Williamsac6b53b2009-07-14 13:40:19 -0700651 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700652 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700653 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700654
Dan Williamsecc65c92008-06-28 08:31:57 +1000655 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
656 if (sh->check_state == check_state_compute_run)
657 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700658 set_bit(STRIPE_HANDLE, &sh->state);
659 release_stripe(sh);
660}
661
Dan Williamsd6f38f32009-07-14 11:50:52 -0700662/* return a pointer to the address conversion region of the scribble buffer */
663static addr_conv_t *to_addr_conv(struct stripe_head *sh,
664 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700665{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700666 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
667}
668
669static struct dma_async_tx_descriptor *
670ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
671{
Dan Williams91c00922007-01-02 13:52:30 -0700672 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700673 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700674 int target = sh->ops.target;
675 struct r5dev *tgt = &sh->dev[target];
676 struct page *xor_dest = tgt->page;
677 int count = 0;
678 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700679 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700680 int i;
681
682 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700683 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700684 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
685
686 for (i = disks; i--; )
687 if (i != target)
688 xor_srcs[count++] = sh->dev[i].page;
689
690 atomic_inc(&sh->count);
691
Dan Williams0403e382009-09-08 17:42:50 -0700692 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700693 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700694 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700695 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700696 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700697 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700698
Dan Williams91c00922007-01-02 13:52:30 -0700699 return tx;
700}
701
Dan Williamsac6b53b2009-07-14 13:40:19 -0700702/* set_syndrome_sources - populate source buffers for gen_syndrome
703 * @srcs - (struct page *) array of size sh->disks
704 * @sh - stripe_head to parse
705 *
706 * Populates srcs in proper layout order for the stripe and returns the
707 * 'count' of sources to be used in a call to async_gen_syndrome. The P
708 * destination buffer is recorded in srcs[count] and the Q destination
709 * is recorded in srcs[count+1]].
710 */
711static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
712{
713 int disks = sh->disks;
714 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
715 int d0_idx = raid6_d0(sh);
716 int count;
717 int i;
718
719 for (i = 0; i < disks; i++)
720 srcs[i] = (void *)raid6_empty_zero_page;
721
722 count = 0;
723 i = d0_idx;
724 do {
725 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
726
727 srcs[slot] = sh->dev[i].page;
728 i = raid6_next_disk(i, disks);
729 } while (i != d0_idx);
730 BUG_ON(count != syndrome_disks);
731
732 return count;
733}
734
735static struct dma_async_tx_descriptor *
736ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
737{
738 int disks = sh->disks;
739 struct page **blocks = percpu->scribble;
740 int target;
741 int qd_idx = sh->qd_idx;
742 struct dma_async_tx_descriptor *tx;
743 struct async_submit_ctl submit;
744 struct r5dev *tgt;
745 struct page *dest;
746 int i;
747 int count;
748
749 if (sh->ops.target < 0)
750 target = sh->ops.target2;
751 else if (sh->ops.target2 < 0)
752 target = sh->ops.target;
753 else
754 /* we should only have one valid target */
755 BUG();
756 BUG_ON(target < 0);
757 pr_debug("%s: stripe %llu block: %d\n",
758 __func__, (unsigned long long)sh->sector, target);
759
760 tgt = &sh->dev[target];
761 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
762 dest = tgt->page;
763
764 atomic_inc(&sh->count);
765
766 if (target == qd_idx) {
767 count = set_syndrome_sources(blocks, sh);
768 blocks[count] = NULL; /* regenerating p is not necessary */
769 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700770 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
771 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700772 to_addr_conv(sh, percpu));
773 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
774 } else {
775 /* Compute any data- or p-drive using XOR */
776 count = 0;
777 for (i = disks; i-- ; ) {
778 if (i == target || i == qd_idx)
779 continue;
780 blocks[count++] = sh->dev[i].page;
781 }
782
Dan Williams0403e382009-09-08 17:42:50 -0700783 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
784 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700785 to_addr_conv(sh, percpu));
786 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
787 }
788
789 return tx;
790}
791
792static struct dma_async_tx_descriptor *
793ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
794{
795 int i, count, disks = sh->disks;
796 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
797 int d0_idx = raid6_d0(sh);
798 int faila = -1, failb = -1;
799 int target = sh->ops.target;
800 int target2 = sh->ops.target2;
801 struct r5dev *tgt = &sh->dev[target];
802 struct r5dev *tgt2 = &sh->dev[target2];
803 struct dma_async_tx_descriptor *tx;
804 struct page **blocks = percpu->scribble;
805 struct async_submit_ctl submit;
806
807 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
808 __func__, (unsigned long long)sh->sector, target, target2);
809 BUG_ON(target < 0 || target2 < 0);
810 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
811 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
812
Dan Williams6c910a72009-09-16 12:24:54 -0700813 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -0700814 * slot number conversion for 'faila' and 'failb'
815 */
816 for (i = 0; i < disks ; i++)
817 blocks[i] = (void *)raid6_empty_zero_page;
818 count = 0;
819 i = d0_idx;
820 do {
821 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
822
823 blocks[slot] = sh->dev[i].page;
824
825 if (i == target)
826 faila = slot;
827 if (i == target2)
828 failb = slot;
829 i = raid6_next_disk(i, disks);
830 } while (i != d0_idx);
831 BUG_ON(count != syndrome_disks);
832
833 BUG_ON(faila == failb);
834 if (failb < faila)
835 swap(faila, failb);
836 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
837 __func__, (unsigned long long)sh->sector, faila, failb);
838
839 atomic_inc(&sh->count);
840
841 if (failb == syndrome_disks+1) {
842 /* Q disk is one of the missing disks */
843 if (faila == syndrome_disks) {
844 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -0700845 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
846 ops_complete_compute, sh,
847 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700848 return async_gen_syndrome(blocks, 0, count+2,
849 STRIPE_SIZE, &submit);
850 } else {
851 struct page *dest;
852 int data_target;
853 int qd_idx = sh->qd_idx;
854
855 /* Missing D+Q: recompute D from P, then recompute Q */
856 if (target == qd_idx)
857 data_target = target2;
858 else
859 data_target = target;
860
861 count = 0;
862 for (i = disks; i-- ; ) {
863 if (i == data_target || i == qd_idx)
864 continue;
865 blocks[count++] = sh->dev[i].page;
866 }
867 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -0700868 init_async_submit(&submit,
869 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
870 NULL, NULL, NULL,
871 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700872 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
873 &submit);
874
875 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -0700876 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
877 ops_complete_compute, sh,
878 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -0700879 return async_gen_syndrome(blocks, 0, count+2,
880 STRIPE_SIZE, &submit);
881 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700882 } else {
Dan Williams6c910a72009-09-16 12:24:54 -0700883 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
884 ops_complete_compute, sh,
885 to_addr_conv(sh, percpu));
886 if (failb == syndrome_disks) {
887 /* We're missing D+P. */
888 return async_raid6_datap_recov(syndrome_disks+2,
889 STRIPE_SIZE, faila,
890 blocks, &submit);
891 } else {
892 /* We're missing D+D. */
893 return async_raid6_2data_recov(syndrome_disks+2,
894 STRIPE_SIZE, faila, failb,
895 blocks, &submit);
896 }
Dan Williamsac6b53b2009-07-14 13:40:19 -0700897 }
898}
899
900
Dan Williams91c00922007-01-02 13:52:30 -0700901static void ops_complete_prexor(void *stripe_head_ref)
902{
903 struct stripe_head *sh = stripe_head_ref;
904
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700905 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700906 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -0700907}
908
909static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -0700910ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
911 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700912{
Dan Williams91c00922007-01-02 13:52:30 -0700913 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700914 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700915 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -0700916 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700917
918 /* existing parity data subtracted */
919 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
920
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700921 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700922 (unsigned long long)sh->sector);
923
924 for (i = disks; i--; ) {
925 struct r5dev *dev = &sh->dev[i];
926 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +1000927 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -0700928 xor_srcs[count++] = dev->page;
929 }
930
Dan Williams0403e382009-09-08 17:42:50 -0700931 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -0700932 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -0700933 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700934
935 return tx;
936}
937
938static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +1000939ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -0700940{
941 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +1000942 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700943
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700944 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700945 (unsigned long long)sh->sector);
946
947 for (i = disks; i--; ) {
948 struct r5dev *dev = &sh->dev[i];
949 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -0700950
Dan Williamsd8ee0722008-06-28 08:32:06 +1000951 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700952 struct bio *wbi;
953
954 spin_lock(&sh->lock);
955 chosen = dev->towrite;
956 dev->towrite = NULL;
957 BUG_ON(dev->written);
958 wbi = dev->written = chosen;
959 spin_unlock(&sh->lock);
960
961 while (wbi && wbi->bi_sector <
962 dev->sector + STRIPE_SECTORS) {
963 tx = async_copy_data(1, wbi, dev->page,
964 dev->sector, tx);
965 wbi = r5_next_bio(wbi, dev->sector);
966 }
967 }
968 }
969
970 return tx;
971}
972
Dan Williamsac6b53b2009-07-14 13:40:19 -0700973static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700974{
975 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700976 int disks = sh->disks;
977 int pd_idx = sh->pd_idx;
978 int qd_idx = sh->qd_idx;
979 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700980
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700981 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700982 (unsigned long long)sh->sector);
983
984 for (i = disks; i--; ) {
985 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -0700986
987 if (dev->written || i == pd_idx || i == qd_idx)
Dan Williams91c00922007-01-02 13:52:30 -0700988 set_bit(R5_UPTODATE, &dev->flags);
989 }
990
Dan Williamsd8ee0722008-06-28 08:32:06 +1000991 if (sh->reconstruct_state == reconstruct_state_drain_run)
992 sh->reconstruct_state = reconstruct_state_drain_result;
993 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
994 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
995 else {
996 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
997 sh->reconstruct_state = reconstruct_state_result;
998 }
Dan Williams91c00922007-01-02 13:52:30 -0700999
1000 set_bit(STRIPE_HANDLE, &sh->state);
1001 release_stripe(sh);
1002}
1003
1004static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001005ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1006 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001007{
Dan Williams91c00922007-01-02 13:52:30 -07001008 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001009 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001010 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001011 int count = 0, pd_idx = sh->pd_idx, i;
1012 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001013 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001014 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001015
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001016 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001017 (unsigned long long)sh->sector);
1018
1019 /* check if prexor is active which means only process blocks
1020 * that are part of a read-modify-write (written)
1021 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001022 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1023 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001024 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1025 for (i = disks; i--; ) {
1026 struct r5dev *dev = &sh->dev[i];
1027 if (dev->written)
1028 xor_srcs[count++] = dev->page;
1029 }
1030 } else {
1031 xor_dest = sh->dev[pd_idx].page;
1032 for (i = disks; i--; ) {
1033 struct r5dev *dev = &sh->dev[i];
1034 if (i != pd_idx)
1035 xor_srcs[count++] = dev->page;
1036 }
1037 }
1038
Dan Williams91c00922007-01-02 13:52:30 -07001039 /* 1/ if we prexor'd then the dest is reused as a source
1040 * 2/ if we did not prexor then we are redoing the parity
1041 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1042 * for the synchronous xor case
1043 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001044 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001045 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1046
1047 atomic_inc(&sh->count);
1048
Dan Williamsac6b53b2009-07-14 13:40:19 -07001049 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001050 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001051 if (unlikely(count == 1))
1052 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1053 else
1054 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001055}
1056
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057static void
1058ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1059 struct dma_async_tx_descriptor *tx)
1060{
1061 struct async_submit_ctl submit;
1062 struct page **blocks = percpu->scribble;
1063 int count;
1064
1065 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1066
1067 count = set_syndrome_sources(blocks, sh);
1068
1069 atomic_inc(&sh->count);
1070
1071 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1072 sh, to_addr_conv(sh, percpu));
1073 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001074}
1075
1076static void ops_complete_check(void *stripe_head_ref)
1077{
1078 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001079
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001080 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001081 (unsigned long long)sh->sector);
1082
Dan Williamsecc65c92008-06-28 08:31:57 +10001083 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001084 set_bit(STRIPE_HANDLE, &sh->state);
1085 release_stripe(sh);
1086}
1087
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001089{
Dan Williams91c00922007-01-02 13:52:30 -07001090 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001091 int pd_idx = sh->pd_idx;
1092 int qd_idx = sh->qd_idx;
1093 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001094 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001095 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001096 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001097 int count;
1098 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001099
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001100 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001101 (unsigned long long)sh->sector);
1102
Dan Williamsac6b53b2009-07-14 13:40:19 -07001103 count = 0;
1104 xor_dest = sh->dev[pd_idx].page;
1105 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001106 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001107 if (i == pd_idx || i == qd_idx)
1108 continue;
1109 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001110 }
1111
Dan Williamsd6f38f32009-07-14 11:50:52 -07001112 init_async_submit(&submit, 0, NULL, NULL, NULL,
1113 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001114 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001115 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001116
Dan Williams91c00922007-01-02 13:52:30 -07001117 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001118 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1119 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001120}
1121
Dan Williamsac6b53b2009-07-14 13:40:19 -07001122static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1123{
1124 struct page **srcs = percpu->scribble;
1125 struct async_submit_ctl submit;
1126 int count;
1127
1128 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1129 (unsigned long long)sh->sector, checkp);
1130
1131 count = set_syndrome_sources(srcs, sh);
1132 if (!checkp)
1133 srcs[count] = NULL;
1134
1135 atomic_inc(&sh->count);
1136 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1137 sh, to_addr_conv(sh, percpu));
1138 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1139 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1140}
1141
1142static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001143{
1144 int overlap_clear = 0, i, disks = sh->disks;
1145 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001146 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001147 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001148 struct raid5_percpu *percpu;
1149 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001150
Dan Williamsd6f38f32009-07-14 11:50:52 -07001151 cpu = get_cpu();
1152 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001153 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001154 ops_run_biofill(sh);
1155 overlap_clear++;
1156 }
1157
Dan Williams7b3a8712008-06-28 08:32:09 +10001158 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001159 if (level < 6)
1160 tx = ops_run_compute5(sh, percpu);
1161 else {
1162 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1163 tx = ops_run_compute6_1(sh, percpu);
1164 else
1165 tx = ops_run_compute6_2(sh, percpu);
1166 }
1167 /* terminate the chain if reconstruct is not set to be run */
1168 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001169 async_tx_ack(tx);
1170 }
Dan Williams91c00922007-01-02 13:52:30 -07001171
Dan Williams600aa102008-06-28 08:32:05 +10001172 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001173 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001174
Dan Williams600aa102008-06-28 08:32:05 +10001175 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001176 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001177 overlap_clear++;
1178 }
1179
Dan Williamsac6b53b2009-07-14 13:40:19 -07001180 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1181 if (level < 6)
1182 ops_run_reconstruct5(sh, percpu, tx);
1183 else
1184 ops_run_reconstruct6(sh, percpu, tx);
1185 }
Dan Williams91c00922007-01-02 13:52:30 -07001186
Dan Williamsac6b53b2009-07-14 13:40:19 -07001187 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1188 if (sh->check_state == check_state_run)
1189 ops_run_check_p(sh, percpu);
1190 else if (sh->check_state == check_state_run_q)
1191 ops_run_check_pq(sh, percpu, 0);
1192 else if (sh->check_state == check_state_run_pq)
1193 ops_run_check_pq(sh, percpu, 1);
1194 else
1195 BUG();
1196 }
Dan Williams91c00922007-01-02 13:52:30 -07001197
Dan Williams91c00922007-01-02 13:52:30 -07001198 if (overlap_clear)
1199 for (i = disks; i--; ) {
1200 struct r5dev *dev = &sh->dev[i];
1201 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1202 wake_up(&sh->raid_conf->wait_for_overlap);
1203 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001204 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001205}
1206
NeilBrown3f294f42005-11-08 21:39:25 -08001207static int grow_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct stripe_head *sh;
NeilBrown3f294f42005-11-08 21:39:25 -08001210 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
1211 if (!sh)
1212 return 0;
1213 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
1214 sh->raid_conf = conf;
1215 spin_lock_init(&sh->lock);
1216
1217 if (grow_buffers(sh, conf->raid_disks)) {
1218 shrink_buffers(sh, conf->raid_disks);
1219 kmem_cache_free(conf->slab_cache, sh);
1220 return 0;
1221 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001222 sh->disks = conf->raid_disks;
NeilBrown3f294f42005-11-08 21:39:25 -08001223 /* we just created an active stripe so... */
1224 atomic_set(&sh->count, 1);
1225 atomic_inc(&conf->active_stripes);
1226 INIT_LIST_HEAD(&sh->lru);
1227 release_stripe(sh);
1228 return 1;
1229}
1230
1231static int grow_stripes(raid5_conf_t *conf, int num)
1232{
Christoph Lametere18b8902006-12-06 20:33:20 -08001233 struct kmem_cache *sc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int devs = conf->raid_disks;
1235
NeilBrown245f46c2009-03-31 14:39:39 +11001236 sprintf(conf->cache_name[0],
1237 "raid%d-%s", conf->level, mdname(conf->mddev));
1238 sprintf(conf->cache_name[1],
1239 "raid%d-%s-alt", conf->level, mdname(conf->mddev));
NeilBrownad01c9e2006-03-27 01:18:07 -08001240 conf->active_name = 0;
1241 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001243 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!sc)
1245 return 1;
1246 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001247 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001248 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001249 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return 0;
1252}
NeilBrown29269552006-03-27 01:18:10 -08001253
Dan Williamsd6f38f32009-07-14 11:50:52 -07001254/**
1255 * scribble_len - return the required size of the scribble region
1256 * @num - total number of disks in the array
1257 *
1258 * The size must be enough to contain:
1259 * 1/ a struct page pointer for each device in the array +2
1260 * 2/ room to convert each entry in (1) to its corresponding dma
1261 * (dma_map_page()) or page (page_address()) address.
1262 *
1263 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1264 * calculate over all devices (not just the data blocks), using zeros in place
1265 * of the P and Q blocks.
1266 */
1267static size_t scribble_len(int num)
1268{
1269 size_t len;
1270
1271 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1272
1273 return len;
1274}
1275
NeilBrownad01c9e2006-03-27 01:18:07 -08001276static int resize_stripes(raid5_conf_t *conf, int newsize)
1277{
1278 /* Make all the stripes able to hold 'newsize' devices.
1279 * New slots in each stripe get 'page' set to a new page.
1280 *
1281 * This happens in stages:
1282 * 1/ create a new kmem_cache and allocate the required number of
1283 * stripe_heads.
1284 * 2/ gather all the old stripe_heads and tranfer the pages across
1285 * to the new stripe_heads. This will have the side effect of
1286 * freezing the array as once all stripe_heads have been collected,
1287 * no IO will be possible. Old stripe heads are freed once their
1288 * pages have been transferred over, and the old kmem_cache is
1289 * freed when all stripes are done.
1290 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1291 * we simple return a failre status - no need to clean anything up.
1292 * 4/ allocate new pages for the new slots in the new stripe_heads.
1293 * If this fails, we don't bother trying the shrink the
1294 * stripe_heads down again, we just leave them as they are.
1295 * As each stripe_head is processed the new one is released into
1296 * active service.
1297 *
1298 * Once step2 is started, we cannot afford to wait for a write,
1299 * so we use GFP_NOIO allocations.
1300 */
1301 struct stripe_head *osh, *nsh;
1302 LIST_HEAD(newstripes);
1303 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001304 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001305 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001306 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001307 int i;
1308
1309 if (newsize <= conf->pool_size)
1310 return 0; /* never bother to shrink */
1311
Dan Williamsb5470dc2008-06-27 21:44:04 -07001312 err = md_allow_write(conf->mddev);
1313 if (err)
1314 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001315
NeilBrownad01c9e2006-03-27 01:18:07 -08001316 /* Step 1 */
1317 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1318 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001319 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001320 if (!sc)
1321 return -ENOMEM;
1322
1323 for (i = conf->max_nr_stripes; i; i--) {
1324 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
1325 if (!nsh)
1326 break;
1327
1328 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
1329
1330 nsh->raid_conf = conf;
1331 spin_lock_init(&nsh->lock);
1332
1333 list_add(&nsh->lru, &newstripes);
1334 }
1335 if (i) {
1336 /* didn't get enough, give up */
1337 while (!list_empty(&newstripes)) {
1338 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1339 list_del(&nsh->lru);
1340 kmem_cache_free(sc, nsh);
1341 }
1342 kmem_cache_destroy(sc);
1343 return -ENOMEM;
1344 }
1345 /* Step 2 - Must use GFP_NOIO now.
1346 * OK, we have enough stripes, start collecting inactive
1347 * stripes and copying them over
1348 */
1349 list_for_each_entry(nsh, &newstripes, lru) {
1350 spin_lock_irq(&conf->device_lock);
1351 wait_event_lock_irq(conf->wait_for_stripe,
1352 !list_empty(&conf->inactive_list),
1353 conf->device_lock,
NeilBrownb3b46be2006-03-27 01:18:16 -08001354 unplug_slaves(conf->mddev)
NeilBrownad01c9e2006-03-27 01:18:07 -08001355 );
1356 osh = get_free_stripe(conf);
1357 spin_unlock_irq(&conf->device_lock);
1358 atomic_set(&nsh->count, 1);
1359 for(i=0; i<conf->pool_size; i++)
1360 nsh->dev[i].page = osh->dev[i].page;
1361 for( ; i<newsize; i++)
1362 nsh->dev[i].page = NULL;
1363 kmem_cache_free(conf->slab_cache, osh);
1364 }
1365 kmem_cache_destroy(conf->slab_cache);
1366
1367 /* Step 3.
1368 * At this point, we are holding all the stripes so the array
1369 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001370 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001371 */
1372 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1373 if (ndisks) {
1374 for (i=0; i<conf->raid_disks; i++)
1375 ndisks[i] = conf->disks[i];
1376 kfree(conf->disks);
1377 conf->disks = ndisks;
1378 } else
1379 err = -ENOMEM;
1380
Dan Williamsd6f38f32009-07-14 11:50:52 -07001381 get_online_cpus();
1382 conf->scribble_len = scribble_len(newsize);
1383 for_each_present_cpu(cpu) {
1384 struct raid5_percpu *percpu;
1385 void *scribble;
1386
1387 percpu = per_cpu_ptr(conf->percpu, cpu);
1388 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1389
1390 if (scribble) {
1391 kfree(percpu->scribble);
1392 percpu->scribble = scribble;
1393 } else {
1394 err = -ENOMEM;
1395 break;
1396 }
1397 }
1398 put_online_cpus();
1399
NeilBrownad01c9e2006-03-27 01:18:07 -08001400 /* Step 4, return new stripes to service */
1401 while(!list_empty(&newstripes)) {
1402 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1403 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001404
NeilBrownad01c9e2006-03-27 01:18:07 -08001405 for (i=conf->raid_disks; i < newsize; i++)
1406 if (nsh->dev[i].page == NULL) {
1407 struct page *p = alloc_page(GFP_NOIO);
1408 nsh->dev[i].page = p;
1409 if (!p)
1410 err = -ENOMEM;
1411 }
1412 release_stripe(nsh);
1413 }
1414 /* critical section pass, GFP_NOIO no longer needed */
1415
1416 conf->slab_cache = sc;
1417 conf->active_name = 1-conf->active_name;
1418 conf->pool_size = newsize;
1419 return err;
1420}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
NeilBrown3f294f42005-11-08 21:39:25 -08001422static int drop_one_stripe(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct stripe_head *sh;
1425
NeilBrown3f294f42005-11-08 21:39:25 -08001426 spin_lock_irq(&conf->device_lock);
1427 sh = get_free_stripe(conf);
1428 spin_unlock_irq(&conf->device_lock);
1429 if (!sh)
1430 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001431 BUG_ON(atomic_read(&sh->count));
NeilBrownad01c9e2006-03-27 01:18:07 -08001432 shrink_buffers(sh, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08001433 kmem_cache_free(conf->slab_cache, sh);
1434 atomic_dec(&conf->active_stripes);
1435 return 1;
1436}
1437
1438static void shrink_stripes(raid5_conf_t *conf)
1439{
1440 while (drop_one_stripe(conf))
1441 ;
1442
NeilBrown29fc7e32006-02-03 03:03:41 -08001443 if (conf->slab_cache)
1444 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 conf->slab_cache = NULL;
1446}
1447
NeilBrown6712ecf2007-09-27 12:47:43 +02001448static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
NeilBrown99c0fb52009-03-31 14:39:38 +11001450 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001452 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001454 char b[BDEVNAME_SIZE];
1455 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 for (i=0 ; i<disks; i++)
1459 if (bi == &sh->dev[i].req)
1460 break;
1461
Dan Williams45b42332007-07-09 11:56:43 -07001462 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1463 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 uptodate);
1465 if (i == disks) {
1466 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
1470 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001472 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrownd6950432006-07-10 04:44:20 -07001473 rdev = conf->disks[i].rdev;
Bernd Schubert6be9d492008-05-23 13:04:34 -07001474 printk_rl(KERN_INFO "raid5:%s: read error corrected"
1475 " (%lu sectors at %llu on %s)\n",
1476 mdname(conf->mddev), STRIPE_SECTORS,
1477 (unsigned long long)(sh->sector
1478 + rdev->data_offset),
1479 bdevname(rdev->bdev, b));
NeilBrown4e5314b2005-11-08 21:39:22 -08001480 clear_bit(R5_ReadError, &sh->dev[i].flags);
1481 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1482 }
NeilBrownba22dcb2005-11-08 21:39:31 -08001483 if (atomic_read(&conf->disks[i].rdev->read_errors))
1484 atomic_set(&conf->disks[i].rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 } else {
NeilBrownd6950432006-07-10 04:44:20 -07001486 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001487 int retry = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001488 rdev = conf->disks[i].rdev;
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001491 atomic_inc(&rdev->read_errors);
NeilBrownba22dcb2005-11-08 21:39:31 -08001492 if (conf->mddev->degraded)
Bernd Schubert6be9d492008-05-23 13:04:34 -07001493 printk_rl(KERN_WARNING
1494 "raid5:%s: read error not correctable "
1495 "(sector %llu on %s).\n",
1496 mdname(conf->mddev),
1497 (unsigned long long)(sh->sector
1498 + rdev->data_offset),
1499 bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001500 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
NeilBrown4e5314b2005-11-08 21:39:22 -08001501 /* Oh, no!!! */
Bernd Schubert6be9d492008-05-23 13:04:34 -07001502 printk_rl(KERN_WARNING
1503 "raid5:%s: read error NOT corrected!! "
1504 "(sector %llu on %s).\n",
1505 mdname(conf->mddev),
1506 (unsigned long long)(sh->sector
1507 + rdev->data_offset),
1508 bdn);
NeilBrownd6950432006-07-10 04:44:20 -07001509 else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001510 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001511 printk(KERN_WARNING
NeilBrownd6950432006-07-10 04:44:20 -07001512 "raid5:%s: Too many read errors, failing device %s.\n",
1513 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001514 else
1515 retry = 1;
1516 if (retry)
1517 set_bit(R5_ReadError, &sh->dev[i].flags);
1518 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001519 clear_bit(R5_ReadError, &sh->dev[i].flags);
1520 clear_bit(R5_ReWrite, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001521 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1526 set_bit(STRIPE_HANDLE, &sh->state);
1527 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
NeilBrownd710e132008-10-13 11:55:12 +11001530static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
NeilBrown99c0fb52009-03-31 14:39:38 +11001532 struct stripe_head *sh = bi->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 raid5_conf_t *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001534 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 for (i=0 ; i<disks; i++)
1538 if (bi == &sh->dev[i].req)
1539 break;
1540
Dan Williams45b42332007-07-09 11:56:43 -07001541 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1543 uptodate);
1544 if (i == disks) {
1545 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (!uptodate)
1550 md_error(conf->mddev, conf->disks[i].rdev);
1551
1552 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1553
1554 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1555 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001556 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559
NeilBrown784052e2009-03-31 15:19:07 +11001560static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
NeilBrown784052e2009-03-31 15:19:07 +11001562static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 struct r5dev *dev = &sh->dev[i];
1565
1566 bio_init(&dev->req);
1567 dev->req.bi_io_vec = &dev->vec;
1568 dev->req.bi_vcnt++;
1569 dev->req.bi_max_vecs++;
1570 dev->vec.bv_page = dev->page;
1571 dev->vec.bv_len = STRIPE_SIZE;
1572 dev->vec.bv_offset = 0;
1573
1574 dev->req.bi_sector = sh->sector;
1575 dev->req.bi_private = sh;
1576
1577 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001578 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1582{
1583 char b[BDEVNAME_SIZE];
1584 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
Dan Williams45b42332007-07-09 11:56:43 -07001585 pr_debug("raid5: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
NeilBrownb2d444d2005-11-08 21:39:31 -08001587 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown850b2b422006-10-03 01:15:46 -07001588 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc04be0a2006-10-03 01:15:53 -07001589 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1590 unsigned long flags;
1591 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 mddev->degraded++;
NeilBrownc04be0a2006-10-03 01:15:53 -07001593 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /*
1595 * if recovery was running, make sure it aborts.
1596 */
NeilBrowndfc70642008-05-23 13:04:39 -07001597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001599 set_bit(Faulty, &rdev->flags);
NeilBrownd710e132008-10-13 11:55:12 +11001600 printk(KERN_ALERT
1601 "raid5: Disk failure on %s, disabling device.\n"
1602 "raid5: Operation continuing on %d devices.\n",
1603 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
NeilBrown16a53ec2006-06-26 00:27:38 -07001605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607/*
1608 * Input: a 'big' sector number,
1609 * Output: index of the data and parity disk, and the sector # in them.
1610 */
NeilBrown112bf892009-03-31 14:39:38 +11001611static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001612 int previous, int *dd_idx,
1613 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 long stripe;
1616 unsigned long chunk_number;
1617 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001618 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001619 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001621 int algorithm = previous ? conf->prev_algo
1622 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001623 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1624 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001625 int raid_disks = previous ? conf->previous_raid_disks
1626 : conf->raid_disks;
1627 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 /* First compute the information on this sector */
1630
1631 /*
1632 * Compute the chunk number and the sector offset inside the chunk
1633 */
1634 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1635 chunk_number = r_sector;
1636 BUG_ON(r_sector != chunk_number);
1637
1638 /*
1639 * Compute the stripe number
1640 */
1641 stripe = chunk_number / data_disks;
1642
1643 /*
1644 * Compute the data disk and parity disk indexes inside the stripe
1645 */
1646 *dd_idx = chunk_number % data_disks;
1647
1648 /*
1649 * Select the parity disk based on the user selected algorithm.
1650 */
NeilBrown911d4ee2009-03-31 14:39:38 +11001651 pd_idx = qd_idx = ~0;
NeilBrown16a53ec2006-06-26 00:27:38 -07001652 switch(conf->level) {
1653 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001654 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001655 break;
1656 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001657 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001659 pd_idx = data_disks - stripe % raid_disks;
1660 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 (*dd_idx)++;
1662 break;
1663 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001664 pd_idx = stripe % raid_disks;
1665 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 (*dd_idx)++;
1667 break;
1668 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001669 pd_idx = data_disks - stripe % raid_disks;
1670 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001673 pd_idx = stripe % raid_disks;
1674 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001676 case ALGORITHM_PARITY_0:
1677 pd_idx = 0;
1678 (*dd_idx)++;
1679 break;
1680 case ALGORITHM_PARITY_N:
1681 pd_idx = data_disks;
1682 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001684 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001685 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001686 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001687 }
1688 break;
1689 case 6:
1690
NeilBrowne183eae2009-03-31 15:20:22 +11001691 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001692 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001693 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1694 qd_idx = pd_idx + 1;
1695 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001696 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001697 qd_idx = 0;
1698 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001699 (*dd_idx) += 2; /* D D P Q D */
1700 break;
1701 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001702 pd_idx = stripe % raid_disks;
1703 qd_idx = pd_idx + 1;
1704 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11001705 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11001706 qd_idx = 0;
1707 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001708 (*dd_idx) += 2; /* D D P Q D */
1709 break;
1710 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001711 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1712 qd_idx = (pd_idx + 1) % raid_disks;
1713 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001714 break;
1715 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown911d4ee2009-03-31 14:39:38 +11001716 pd_idx = stripe % raid_disks;
1717 qd_idx = (pd_idx + 1) % raid_disks;
1718 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001719 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001720
1721 case ALGORITHM_PARITY_0:
1722 pd_idx = 0;
1723 qd_idx = 1;
1724 (*dd_idx) += 2;
1725 break;
1726 case ALGORITHM_PARITY_N:
1727 pd_idx = data_disks;
1728 qd_idx = data_disks + 1;
1729 break;
1730
1731 case ALGORITHM_ROTATING_ZERO_RESTART:
1732 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1733 * of blocks for computing Q is different.
1734 */
1735 pd_idx = stripe % raid_disks;
1736 qd_idx = pd_idx + 1;
1737 if (pd_idx == raid_disks-1) {
1738 (*dd_idx)++; /* Q D D D P */
1739 qd_idx = 0;
1740 } else if (*dd_idx >= pd_idx)
1741 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001742 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001743 break;
1744
1745 case ALGORITHM_ROTATING_N_RESTART:
1746 /* Same a left_asymmetric, by first stripe is
1747 * D D D P Q rather than
1748 * Q D D D P
1749 */
1750 pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
1751 qd_idx = pd_idx + 1;
1752 if (pd_idx == raid_disks-1) {
1753 (*dd_idx)++; /* Q D D D P */
1754 qd_idx = 0;
1755 } else if (*dd_idx >= pd_idx)
1756 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11001757 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001758 break;
1759
1760 case ALGORITHM_ROTATING_N_CONTINUE:
1761 /* Same as left_symmetric but Q is before P */
1762 pd_idx = raid_disks - 1 - (stripe % raid_disks);
1763 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1764 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11001765 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11001766 break;
1767
1768 case ALGORITHM_LEFT_ASYMMETRIC_6:
1769 /* RAID5 left_asymmetric, with Q on last device */
1770 pd_idx = data_disks - stripe % (raid_disks-1);
1771 if (*dd_idx >= pd_idx)
1772 (*dd_idx)++;
1773 qd_idx = raid_disks - 1;
1774 break;
1775
1776 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1777 pd_idx = stripe % (raid_disks-1);
1778 if (*dd_idx >= pd_idx)
1779 (*dd_idx)++;
1780 qd_idx = raid_disks - 1;
1781 break;
1782
1783 case ALGORITHM_LEFT_SYMMETRIC_6:
1784 pd_idx = data_disks - stripe % (raid_disks-1);
1785 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1786 qd_idx = raid_disks - 1;
1787 break;
1788
1789 case ALGORITHM_RIGHT_SYMMETRIC_6:
1790 pd_idx = stripe % (raid_disks-1);
1791 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1792 qd_idx = raid_disks - 1;
1793 break;
1794
1795 case ALGORITHM_PARITY_0_6:
1796 pd_idx = 0;
1797 (*dd_idx)++;
1798 qd_idx = raid_disks - 1;
1799 break;
1800
1801
NeilBrown16a53ec2006-06-26 00:27:38 -07001802 default:
NeilBrownd710e132008-10-13 11:55:12 +11001803 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001804 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001805 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001806 }
1807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809
NeilBrown911d4ee2009-03-31 14:39:38 +11001810 if (sh) {
1811 sh->pd_idx = pd_idx;
1812 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001813 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11001814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * Finally, compute the new sector number
1817 */
1818 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1819 return new_sector;
1820}
1821
1822
NeilBrown784052e2009-03-31 15:19:07 +11001823static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 raid5_conf_t *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08001826 int raid_disks = sh->disks;
1827 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001829 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1830 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11001831 int algorithm = previous ? conf->prev_algo
1832 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 sector_t stripe;
1834 int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001835 int chunk_number, dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11001837 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
NeilBrown16a53ec2006-06-26 00:27:38 -07001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1841 stripe = new_sector;
1842 BUG_ON(new_sector != stripe);
1843
NeilBrown16a53ec2006-06-26 00:27:38 -07001844 if (i == sh->pd_idx)
1845 return 0;
1846 switch(conf->level) {
1847 case 4: break;
1848 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001849 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 case ALGORITHM_LEFT_ASYMMETRIC:
1851 case ALGORITHM_RIGHT_ASYMMETRIC:
1852 if (i > sh->pd_idx)
1853 i--;
1854 break;
1855 case ALGORITHM_LEFT_SYMMETRIC:
1856 case ALGORITHM_RIGHT_SYMMETRIC:
1857 if (i < sh->pd_idx)
1858 i += raid_disks;
1859 i -= (sh->pd_idx + 1);
1860 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001861 case ALGORITHM_PARITY_0:
1862 i -= 1;
1863 break;
1864 case ALGORITHM_PARITY_N:
1865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 default:
NeilBrown14f8d262006-01-06 00:20:14 -08001867 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001868 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001869 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001870 }
1871 break;
1872 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11001873 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07001874 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11001875 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07001876 case ALGORITHM_LEFT_ASYMMETRIC:
1877 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11001878 case ALGORITHM_ROTATING_ZERO_RESTART:
1879 case ALGORITHM_ROTATING_N_RESTART:
1880 if (sh->pd_idx == raid_disks-1)
1881 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07001882 else if (i > sh->pd_idx)
1883 i -= 2; /* D D P Q D */
1884 break;
1885 case ALGORITHM_LEFT_SYMMETRIC:
1886 case ALGORITHM_RIGHT_SYMMETRIC:
1887 if (sh->pd_idx == raid_disks-1)
1888 i--; /* Q D D D P */
1889 else {
1890 /* D D P Q D */
1891 if (i < sh->pd_idx)
1892 i += raid_disks;
1893 i -= (sh->pd_idx + 2);
1894 }
1895 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11001896 case ALGORITHM_PARITY_0:
1897 i -= 2;
1898 break;
1899 case ALGORITHM_PARITY_N:
1900 break;
1901 case ALGORITHM_ROTATING_N_CONTINUE:
1902 if (sh->pd_idx == 0)
1903 i--; /* P D D D Q */
1904 else if (i > sh->pd_idx)
1905 i -= 2; /* D D Q P D */
1906 break;
1907 case ALGORITHM_LEFT_ASYMMETRIC_6:
1908 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1909 if (i > sh->pd_idx)
1910 i--;
1911 break;
1912 case ALGORITHM_LEFT_SYMMETRIC_6:
1913 case ALGORITHM_RIGHT_SYMMETRIC_6:
1914 if (i < sh->pd_idx)
1915 i += data_disks + 1;
1916 i -= (sh->pd_idx + 1);
1917 break;
1918 case ALGORITHM_PARITY_0_6:
1919 i -= 1;
1920 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07001921 default:
NeilBrownd710e132008-10-13 11:55:12 +11001922 printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
NeilBrowne183eae2009-03-31 15:20:22 +11001923 algorithm);
NeilBrown99c0fb52009-03-31 14:39:38 +11001924 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07001925 }
1926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928
1929 chunk_number = stripe * data_disks + i;
1930 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
1931
NeilBrown112bf892009-03-31 14:39:38 +11001932 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11001933 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11001934 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
1935 || sh2.qd_idx != sh->qd_idx) {
NeilBrown14f8d262006-01-06 00:20:14 -08001936 printk(KERN_ERR "compute_blocknr: map not correct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return 0;
1938 }
1939 return r_sector;
1940}
1941
1942
Dan Williams600aa102008-06-28 08:32:05 +10001943static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001944schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10001945 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07001946{
1947 int i, pd_idx = sh->pd_idx, disks = sh->disks;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001948 raid5_conf_t *conf = sh->raid_conf;
1949 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07001950
Dan Williamse33129d2007-01-02 13:52:30 -07001951 if (rcw) {
1952 /* if we are not expanding this is a proper write request, and
1953 * there will be bios with new data to be drained into the
1954 * stripe cache
1955 */
1956 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10001957 sh->reconstruct_state = reconstruct_state_drain_run;
1958 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
1959 } else
1960 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07001961
Dan Williamsac6b53b2009-07-14 13:40:19 -07001962 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001963
1964 for (i = disks; i--; ) {
1965 struct r5dev *dev = &sh->dev[i];
1966
1967 if (dev->towrite) {
1968 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10001969 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001970 if (!expand)
1971 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001972 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07001973 }
1974 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001975 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07001976 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001977 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07001978 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07001979 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07001980 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
1981 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
1982
Dan Williamsd8ee0722008-06-28 08:32:06 +10001983 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10001984 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
1985 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001986 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07001987
1988 for (i = disks; i--; ) {
1989 struct r5dev *dev = &sh->dev[i];
1990 if (i == pd_idx)
1991 continue;
1992
Dan Williamse33129d2007-01-02 13:52:30 -07001993 if (dev->towrite &&
1994 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10001995 test_bit(R5_Wantcompute, &dev->flags))) {
1996 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07001997 set_bit(R5_LOCKED, &dev->flags);
1998 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10001999 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002000 }
2001 }
2002 }
2003
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002004 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002005 * are in flight
2006 */
2007 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2008 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002009 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002010
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002011 if (level == 6) {
2012 int qd_idx = sh->qd_idx;
2013 struct r5dev *dev = &sh->dev[qd_idx];
2014
2015 set_bit(R5_LOCKED, &dev->flags);
2016 clear_bit(R5_UPTODATE, &dev->flags);
2017 s->locked++;
2018 }
2019
Dan Williams600aa102008-06-28 08:32:05 +10002020 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002021 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002022 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002023}
NeilBrown16a53ec2006-06-26 00:27:38 -07002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025/*
2026 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002027 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 * The bi_next chain must be in order.
2029 */
2030static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2031{
2032 struct bio **bip;
2033 raid5_conf_t *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002034 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Dan Williams45b42332007-07-09 11:56:43 -07002036 pr_debug("adding bh b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 (unsigned long long)bi->bi_sector,
2038 (unsigned long long)sh->sector);
2039
2040
2041 spin_lock(&sh->lock);
2042 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07002043 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 bip = &sh->dev[dd_idx].towrite;
NeilBrown72626682005-09-09 16:23:54 -07002045 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2046 firstwrite = 1;
2047 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 bip = &sh->dev[dd_idx].toread;
2049 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2050 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2051 goto overlap;
2052 bip = & (*bip)->bi_next;
2053 }
2054 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2055 goto overlap;
2056
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002057 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (*bip)
2059 bi->bi_next = *bip;
2060 *bip = bi;
Jens Axboe960e7392008-08-15 10:41:18 +02002061 bi->bi_phys_segments++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 spin_unlock_irq(&conf->device_lock);
2063 spin_unlock(&sh->lock);
2064
Dan Williams45b42332007-07-09 11:56:43 -07002065 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 (unsigned long long)bi->bi_sector,
2067 (unsigned long long)sh->sector, dd_idx);
2068
NeilBrown72626682005-09-09 16:23:54 -07002069 if (conf->mddev->bitmap && firstwrite) {
NeilBrown72626682005-09-09 16:23:54 -07002070 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2071 STRIPE_SECTORS, 0);
NeilBrownae3c20c2006-07-10 04:44:17 -07002072 sh->bm_seq = conf->seq_flush+1;
NeilBrown72626682005-09-09 16:23:54 -07002073 set_bit(STRIPE_BIT_DELAY, &sh->state);
2074 }
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (forwrite) {
2077 /* check if page is covered */
2078 sector_t sector = sh->dev[dd_idx].sector;
2079 for (bi=sh->dev[dd_idx].towrite;
2080 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2081 bi && bi->bi_sector <= sector;
2082 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2083 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2084 sector = bi->bi_sector + (bi->bi_size>>9);
2085 }
2086 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2087 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2088 }
2089 return 1;
2090
2091 overlap:
2092 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2093 spin_unlock_irq(&conf->device_lock);
2094 spin_unlock(&sh->lock);
2095 return 0;
2096}
2097
NeilBrown29269552006-03-27 01:18:10 -08002098static void end_reshape(raid5_conf_t *conf);
2099
NeilBrown911d4ee2009-03-31 14:39:38 +11002100static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2101 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002102{
NeilBrown784052e2009-03-31 15:19:07 +11002103 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002104 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002105 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002106 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002107 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002108
NeilBrown112bf892009-03-31 14:39:38 +11002109 raid5_compute_sector(conf,
2110 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002111 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002112 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002113 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002114}
2115
Dan Williamsa4456852007-07-09 11:56:43 -07002116static void
Dan Williams1fe797e2008-06-28 09:16:30 +10002117handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002118 struct stripe_head_state *s, int disks,
2119 struct bio **return_bi)
2120{
2121 int i;
2122 for (i = disks; i--; ) {
2123 struct bio *bi;
2124 int bitmap_end = 0;
2125
2126 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2127 mdk_rdev_t *rdev;
2128 rcu_read_lock();
2129 rdev = rcu_dereference(conf->disks[i].rdev);
2130 if (rdev && test_bit(In_sync, &rdev->flags))
2131 /* multiple read failures in one stripe */
2132 md_error(conf->mddev, rdev);
2133 rcu_read_unlock();
2134 }
2135 spin_lock_irq(&conf->device_lock);
2136 /* fail all writes first */
2137 bi = sh->dev[i].towrite;
2138 sh->dev[i].towrite = NULL;
2139 if (bi) {
2140 s->to_write--;
2141 bitmap_end = 1;
2142 }
2143
2144 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2145 wake_up(&conf->wait_for_overlap);
2146
2147 while (bi && bi->bi_sector <
2148 sh->dev[i].sector + STRIPE_SECTORS) {
2149 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2150 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002151 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002152 md_write_end(conf->mddev);
2153 bi->bi_next = *return_bi;
2154 *return_bi = bi;
2155 }
2156 bi = nextbi;
2157 }
2158 /* and fail all 'written' */
2159 bi = sh->dev[i].written;
2160 sh->dev[i].written = NULL;
2161 if (bi) bitmap_end = 1;
2162 while (bi && bi->bi_sector <
2163 sh->dev[i].sector + STRIPE_SECTORS) {
2164 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2165 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002166 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002167 md_write_end(conf->mddev);
2168 bi->bi_next = *return_bi;
2169 *return_bi = bi;
2170 }
2171 bi = bi2;
2172 }
2173
Dan Williamsb5e98d62007-01-02 13:52:31 -07002174 /* fail any reads if this device is non-operational and
2175 * the data has not reached the cache yet.
2176 */
2177 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2178 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2179 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002180 bi = sh->dev[i].toread;
2181 sh->dev[i].toread = NULL;
2182 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2183 wake_up(&conf->wait_for_overlap);
2184 if (bi) s->to_read--;
2185 while (bi && bi->bi_sector <
2186 sh->dev[i].sector + STRIPE_SECTORS) {
2187 struct bio *nextbi =
2188 r5_next_bio(bi, sh->dev[i].sector);
2189 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Jens Axboe960e7392008-08-15 10:41:18 +02002190 if (!raid5_dec_bi_phys_segments(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002191 bi->bi_next = *return_bi;
2192 *return_bi = bi;
2193 }
2194 bi = nextbi;
2195 }
2196 }
2197 spin_unlock_irq(&conf->device_lock);
2198 if (bitmap_end)
2199 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2200 STRIPE_SECTORS, 0, 0);
2201 }
2202
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002203 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2204 if (atomic_dec_and_test(&conf->pending_full_writes))
2205 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002206}
2207
Dan Williams1fe797e2008-06-28 09:16:30 +10002208/* fetch_block5 - checks the given member device to see if its data needs
2209 * to be read or computed to satisfy a request.
2210 *
2211 * Returns 1 when no more member devices need to be checked, otherwise returns
2212 * 0 to tell the loop in handle_stripe_fill5 to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002213 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002214static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
2215 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002216{
2217 struct r5dev *dev = &sh->dev[disk_idx];
2218 struct r5dev *failed_dev = &sh->dev[s->failed_num];
2219
Dan Williamsf38e1212007-01-02 13:52:30 -07002220 /* is the data in this block needed, and can we get it? */
2221 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002222 !test_bit(R5_UPTODATE, &dev->flags) &&
2223 (dev->toread ||
2224 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2225 s->syncing || s->expanding ||
2226 (s->failed &&
2227 (failed_dev->toread ||
2228 (failed_dev->towrite &&
2229 !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002230 /* We would like to get this block, possibly by computing it,
2231 * otherwise read it if the backing disk is insync
Dan Williamsf38e1212007-01-02 13:52:30 -07002232 */
2233 if ((s->uptodate == disks - 1) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10002234 (s->failed && disk_idx == s->failed_num)) {
Dan Williams976ea8d2008-06-28 08:32:03 +10002235 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2236 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
Dan Williamsf38e1212007-01-02 13:52:30 -07002237 set_bit(R5_Wantcompute, &dev->flags);
2238 sh->ops.target = disk_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002239 sh->ops.target2 = -1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002240 s->req_compute = 1;
Dan Williamsf38e1212007-01-02 13:52:30 -07002241 /* Careful: from this point on 'uptodate' is in the eye
Dan Williamsac6b53b2009-07-14 13:40:19 -07002242 * of raid_run_ops which services 'compute' operations
Dan Williamsf38e1212007-01-02 13:52:30 -07002243 * before writes. R5_Wantcompute flags a block that will
2244 * be R5_UPTODATE by the time it is needed for a
2245 * subsequent operation.
2246 */
2247 s->uptodate++;
Dan Williams1fe797e2008-06-28 09:16:30 +10002248 return 1; /* uptodate + compute == disks */
Dan Williams7a1fc532008-07-10 04:54:57 -07002249 } else if (test_bit(R5_Insync, &dev->flags)) {
Dan Williamsf38e1212007-01-02 13:52:30 -07002250 set_bit(R5_LOCKED, &dev->flags);
2251 set_bit(R5_Wantread, &dev->flags);
Dan Williamsf38e1212007-01-02 13:52:30 -07002252 s->locked++;
2253 pr_debug("Reading block %d (sync=%d)\n", disk_idx,
2254 s->syncing);
2255 }
2256 }
2257
Dan Williams1fe797e2008-06-28 09:16:30 +10002258 return 0;
Dan Williamsf38e1212007-01-02 13:52:30 -07002259}
2260
Dan Williams1fe797e2008-06-28 09:16:30 +10002261/**
2262 * handle_stripe_fill5 - read or compute data to satisfy pending requests.
2263 */
2264static void handle_stripe_fill5(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002265 struct stripe_head_state *s, int disks)
2266{
2267 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07002268
Dan Williamsf38e1212007-01-02 13:52:30 -07002269 /* look for blocks to read/compute, skip this if a compute
2270 * is already in flight, or if the stripe contents are in the
2271 * midst of changing due to a write
2272 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002273 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002274 !sh->reconstruct_state)
Dan Williamsf38e1212007-01-02 13:52:30 -07002275 for (i = disks; i--; )
Dan Williams1fe797e2008-06-28 09:16:30 +10002276 if (fetch_block5(sh, s, i, disks))
Dan Williamsf38e1212007-01-02 13:52:30 -07002277 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002278 set_bit(STRIPE_HANDLE, &sh->state);
2279}
2280
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002281/* fetch_block6 - checks the given member device to see if its data needs
2282 * to be read or computed to satisfy a request.
2283 *
2284 * Returns 1 when no more member devices need to be checked, otherwise returns
2285 * 0 to tell the loop in handle_stripe_fill6 to continue
2286 */
2287static int fetch_block6(struct stripe_head *sh, struct stripe_head_state *s,
2288 struct r6_state *r6s, int disk_idx, int disks)
2289{
2290 struct r5dev *dev = &sh->dev[disk_idx];
2291 struct r5dev *fdev[2] = { &sh->dev[r6s->failed_num[0]],
2292 &sh->dev[r6s->failed_num[1]] };
2293
2294 if (!test_bit(R5_LOCKED, &dev->flags) &&
2295 !test_bit(R5_UPTODATE, &dev->flags) &&
2296 (dev->toread ||
2297 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2298 s->syncing || s->expanding ||
2299 (s->failed >= 1 &&
2300 (fdev[0]->toread || s->to_write)) ||
2301 (s->failed >= 2 &&
2302 (fdev[1]->toread || s->to_write)))) {
2303 /* we would like to get this block, possibly by computing it,
2304 * otherwise read it if the backing disk is insync
2305 */
2306 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2307 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2308 if ((s->uptodate == disks - 1) &&
2309 (s->failed && (disk_idx == r6s->failed_num[0] ||
2310 disk_idx == r6s->failed_num[1]))) {
2311 /* have disk failed, and we're requested to fetch it;
2312 * do compute it
2313 */
2314 pr_debug("Computing stripe %llu block %d\n",
2315 (unsigned long long)sh->sector, disk_idx);
2316 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2317 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2318 set_bit(R5_Wantcompute, &dev->flags);
2319 sh->ops.target = disk_idx;
2320 sh->ops.target2 = -1; /* no 2nd target */
2321 s->req_compute = 1;
2322 s->uptodate++;
2323 return 1;
2324 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2325 /* Computing 2-failure is *very* expensive; only
2326 * do it if failed >= 2
2327 */
2328 int other;
2329 for (other = disks; other--; ) {
2330 if (other == disk_idx)
2331 continue;
2332 if (!test_bit(R5_UPTODATE,
2333 &sh->dev[other].flags))
2334 break;
2335 }
2336 BUG_ON(other < 0);
2337 pr_debug("Computing stripe %llu blocks %d,%d\n",
2338 (unsigned long long)sh->sector,
2339 disk_idx, other);
2340 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2341 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2342 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2343 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2344 sh->ops.target = disk_idx;
2345 sh->ops.target2 = other;
2346 s->uptodate += 2;
2347 s->req_compute = 1;
2348 return 1;
2349 } else if (test_bit(R5_Insync, &dev->flags)) {
2350 set_bit(R5_LOCKED, &dev->flags);
2351 set_bit(R5_Wantread, &dev->flags);
2352 s->locked++;
2353 pr_debug("Reading block %d (sync=%d)\n",
2354 disk_idx, s->syncing);
2355 }
2356 }
2357
2358 return 0;
2359}
2360
2361/**
2362 * handle_stripe_fill6 - read or compute data to satisfy pending requests.
2363 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002364static void handle_stripe_fill6(struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002365 struct stripe_head_state *s, struct r6_state *r6s,
2366 int disks)
2367{
2368 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002369
2370 /* look for blocks to read/compute, skip this if a compute
2371 * is already in flight, or if the stripe contents are in the
2372 * midst of changing due to a write
2373 */
2374 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2375 !sh->reconstruct_state)
2376 for (i = disks; i--; )
2377 if (fetch_block6(sh, s, r6s, i, disks))
2378 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002379 set_bit(STRIPE_HANDLE, &sh->state);
2380}
2381
2382
Dan Williams1fe797e2008-06-28 09:16:30 +10002383/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002384 * any written block on an uptodate or failed drive can be returned.
2385 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2386 * never LOCKED, so we don't need to test 'failed' directly.
2387 */
Dan Williams1fe797e2008-06-28 09:16:30 +10002388static void handle_stripe_clean_event(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002389 struct stripe_head *sh, int disks, struct bio **return_bi)
2390{
2391 int i;
2392 struct r5dev *dev;
2393
2394 for (i = disks; i--; )
2395 if (sh->dev[i].written) {
2396 dev = &sh->dev[i];
2397 if (!test_bit(R5_LOCKED, &dev->flags) &&
2398 test_bit(R5_UPTODATE, &dev->flags)) {
2399 /* We can return any write requests */
2400 struct bio *wbi, *wbi2;
2401 int bitmap_end = 0;
Dan Williams45b42332007-07-09 11:56:43 -07002402 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002403 spin_lock_irq(&conf->device_lock);
2404 wbi = dev->written;
2405 dev->written = NULL;
2406 while (wbi && wbi->bi_sector <
2407 dev->sector + STRIPE_SECTORS) {
2408 wbi2 = r5_next_bio(wbi, dev->sector);
Jens Axboe960e7392008-08-15 10:41:18 +02002409 if (!raid5_dec_bi_phys_segments(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002410 md_write_end(conf->mddev);
2411 wbi->bi_next = *return_bi;
2412 *return_bi = wbi;
2413 }
2414 wbi = wbi2;
2415 }
2416 if (dev->towrite == NULL)
2417 bitmap_end = 1;
2418 spin_unlock_irq(&conf->device_lock);
2419 if (bitmap_end)
2420 bitmap_endwrite(conf->mddev->bitmap,
2421 sh->sector,
2422 STRIPE_SECTORS,
2423 !test_bit(STRIPE_DEGRADED, &sh->state),
2424 0);
2425 }
2426 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002427
2428 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2429 if (atomic_dec_and_test(&conf->pending_full_writes))
2430 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002431}
2432
Dan Williams1fe797e2008-06-28 09:16:30 +10002433static void handle_stripe_dirtying5(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002434 struct stripe_head *sh, struct stripe_head_state *s, int disks)
2435{
2436 int rmw = 0, rcw = 0, i;
2437 for (i = disks; i--; ) {
2438 /* would I have to read this buffer for read_modify_write */
2439 struct r5dev *dev = &sh->dev[i];
2440 if ((dev->towrite || i == sh->pd_idx) &&
2441 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002442 !(test_bit(R5_UPTODATE, &dev->flags) ||
2443 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002444 if (test_bit(R5_Insync, &dev->flags))
2445 rmw++;
2446 else
2447 rmw += 2*disks; /* cannot read it */
2448 }
2449 /* Would I have to read this buffer for reconstruct_write */
2450 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2451 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002452 !(test_bit(R5_UPTODATE, &dev->flags) ||
2453 test_bit(R5_Wantcompute, &dev->flags))) {
2454 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002455 else
2456 rcw += 2*disks;
2457 }
2458 }
Dan Williams45b42332007-07-09 11:56:43 -07002459 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002460 (unsigned long long)sh->sector, rmw, rcw);
2461 set_bit(STRIPE_HANDLE, &sh->state);
2462 if (rmw < rcw && rmw > 0)
2463 /* prefer read-modify-write, but need to get some data */
2464 for (i = disks; i--; ) {
2465 struct r5dev *dev = &sh->dev[i];
2466 if ((dev->towrite || i == sh->pd_idx) &&
2467 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002468 !(test_bit(R5_UPTODATE, &dev->flags) ||
2469 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002470 test_bit(R5_Insync, &dev->flags)) {
2471 if (
2472 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002473 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002474 "%d for r-m-w\n", i);
2475 set_bit(R5_LOCKED, &dev->flags);
2476 set_bit(R5_Wantread, &dev->flags);
2477 s->locked++;
2478 } else {
2479 set_bit(STRIPE_DELAYED, &sh->state);
2480 set_bit(STRIPE_HANDLE, &sh->state);
2481 }
2482 }
2483 }
2484 if (rcw <= rmw && rcw > 0)
2485 /* want reconstruct write, but need to get some data */
2486 for (i = disks; i--; ) {
2487 struct r5dev *dev = &sh->dev[i];
2488 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2489 i != sh->pd_idx &&
2490 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002491 !(test_bit(R5_UPTODATE, &dev->flags) ||
2492 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002493 test_bit(R5_Insync, &dev->flags)) {
2494 if (
2495 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002496 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002497 "%d for Reconstruct\n", i);
2498 set_bit(R5_LOCKED, &dev->flags);
2499 set_bit(R5_Wantread, &dev->flags);
2500 s->locked++;
2501 } else {
2502 set_bit(STRIPE_DELAYED, &sh->state);
2503 set_bit(STRIPE_HANDLE, &sh->state);
2504 }
2505 }
2506 }
2507 /* now if nothing is locked, and if we have enough data,
2508 * we can start a write request
2509 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002510 /* since handle_stripe can be called at any time we need to handle the
2511 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002512 * subsequent call wants to start a write request. raid_run_ops only
2513 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002514 * simultaneously. If this is not the case then new writes need to be
2515 * held off until the compute completes.
2516 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002517 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2518 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2519 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002520 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002521}
2522
Dan Williams1fe797e2008-06-28 09:16:30 +10002523static void handle_stripe_dirtying6(raid5_conf_t *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002524 struct stripe_head *sh, struct stripe_head_state *s,
2525 struct r6_state *r6s, int disks)
2526{
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002527 int rcw = 0, pd_idx = sh->pd_idx, i;
NeilBrown34e04e82009-03-31 15:10:16 +11002528 int qd_idx = sh->qd_idx;
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002529
2530 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002531 for (i = disks; i--; ) {
2532 struct r5dev *dev = &sh->dev[i];
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002533 /* check if we haven't enough data */
2534 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2535 i != pd_idx && i != qd_idx &&
2536 !test_bit(R5_LOCKED, &dev->flags) &&
2537 !(test_bit(R5_UPTODATE, &dev->flags) ||
2538 test_bit(R5_Wantcompute, &dev->flags))) {
2539 rcw++;
2540 if (!test_bit(R5_Insync, &dev->flags))
2541 continue; /* it's a failed drive */
2542
2543 if (
2544 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2545 pr_debug("Read_old stripe %llu "
2546 "block %d for Reconstruct\n",
2547 (unsigned long long)sh->sector, i);
2548 set_bit(R5_LOCKED, &dev->flags);
2549 set_bit(R5_Wantread, &dev->flags);
2550 s->locked++;
2551 } else {
2552 pr_debug("Request delayed stripe %llu "
2553 "block %d for Reconstruct\n",
2554 (unsigned long long)sh->sector, i);
2555 set_bit(STRIPE_DELAYED, &sh->state);
2556 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002557 }
2558 }
2559 }
Dan Williamsa4456852007-07-09 11:56:43 -07002560 /* now if nothing is locked, and if we have enough data, we can start a
2561 * write request
2562 */
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002563 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2564 s->locked == 0 && rcw == 0 &&
Dan Williamsa4456852007-07-09 11:56:43 -07002565 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07002566 schedule_reconstruction(sh, s, 1, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002567 }
2568}
2569
2570static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2571 struct stripe_head_state *s, int disks)
2572{
Dan Williamsecc65c92008-06-28 08:31:57 +10002573 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002574
Dan Williamsbd2ab672008-04-10 21:29:27 -07002575 set_bit(STRIPE_HANDLE, &sh->state);
2576
Dan Williamsecc65c92008-06-28 08:31:57 +10002577 switch (sh->check_state) {
2578 case check_state_idle:
2579 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002580 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002581 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002582 sh->check_state = check_state_run;
2583 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002584 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002585 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002586 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002587 }
Dan Williamsa4456852007-07-09 11:56:43 -07002588 dev = &sh->dev[s->failed_num];
Dan Williamsecc65c92008-06-28 08:31:57 +10002589 /* fall through */
2590 case check_state_compute_result:
2591 sh->check_state = check_state_idle;
2592 if (!dev)
2593 dev = &sh->dev[sh->pd_idx];
2594
2595 /* check that a write has not made the stripe insync */
2596 if (test_bit(STRIPE_INSYNC, &sh->state))
2597 break;
2598
2599 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002600 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2601 BUG_ON(s->uptodate != disks);
2602
2603 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002604 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002605 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002606
Dan Williamsa4456852007-07-09 11:56:43 -07002607 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002608 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002609 break;
2610 case check_state_run:
2611 break; /* we will be called again upon completion */
2612 case check_state_check_result:
2613 sh->check_state = check_state_idle;
2614
2615 /* if a failure occurred during the check operation, leave
2616 * STRIPE_INSYNC not set and let the stripe be handled again
2617 */
2618 if (s->failed)
2619 break;
2620
2621 /* handle a successful check operation, if parity is correct
2622 * we are done. Otherwise update the mismatch count and repair
2623 * parity if !MD_RECOVERY_CHECK
2624 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002625 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002626 /* parity is correct (on disc,
2627 * not in buffer any more)
2628 */
2629 set_bit(STRIPE_INSYNC, &sh->state);
2630 else {
2631 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2632 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2633 /* don't try to repair!! */
2634 set_bit(STRIPE_INSYNC, &sh->state);
2635 else {
2636 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002637 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002638 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2639 set_bit(R5_Wantcompute,
2640 &sh->dev[sh->pd_idx].flags);
2641 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002642 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002643 s->uptodate++;
2644 }
2645 }
2646 break;
2647 case check_state_compute_run:
2648 break;
2649 default:
2650 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2651 __func__, sh->check_state,
2652 (unsigned long long) sh->sector);
2653 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002654 }
2655}
2656
2657
2658static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002659 struct stripe_head_state *s,
2660 struct r6_state *r6s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002661{
Dan Williamsa4456852007-07-09 11:56:43 -07002662 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002663 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002664 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002665
2666 set_bit(STRIPE_HANDLE, &sh->state);
2667
2668 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002669
Dan Williamsa4456852007-07-09 11:56:43 -07002670 /* Want to check and possibly repair P and Q.
2671 * However there could be one 'failed' device, in which
2672 * case we can only check one of them, possibly using the
2673 * other to generate missing data
2674 */
2675
Dan Williamsd82dfee2009-07-14 13:40:57 -07002676 switch (sh->check_state) {
2677 case check_state_idle:
2678 /* start a new check operation if there are < 2 failures */
Dan Williamsa4456852007-07-09 11:56:43 -07002679 if (s->failed == r6s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002680 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002681 * makes sense to check P (If anything else were failed,
2682 * we would have used P to recreate it).
2683 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002684 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002685 }
2686 if (!r6s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002687 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002688 * anything, so it makes sense to check it
2689 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002690 if (sh->check_state == check_state_run)
2691 sh->check_state = check_state_run_pq;
2692 else
2693 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002694 }
Dan Williams36d1c642009-07-14 11:48:22 -07002695
Dan Williamsd82dfee2009-07-14 13:40:57 -07002696 /* discard potentially stale zero_sum_result */
2697 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002698
Dan Williamsd82dfee2009-07-14 13:40:57 -07002699 if (sh->check_state == check_state_run) {
2700 /* async_xor_zero_sum destroys the contents of P */
2701 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2702 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07002703 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002704 if (sh->check_state >= check_state_run &&
2705 sh->check_state <= check_state_run_pq) {
2706 /* async_syndrome_zero_sum preserves P and Q, so
2707 * no need to mark them !uptodate here
2708 */
2709 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2710 break;
2711 }
Dan Williams36d1c642009-07-14 11:48:22 -07002712
Dan Williamsd82dfee2009-07-14 13:40:57 -07002713 /* we have 2-disk failure */
2714 BUG_ON(s->failed != 2);
2715 /* fall through */
2716 case check_state_compute_result:
2717 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07002718
Dan Williamsd82dfee2009-07-14 13:40:57 -07002719 /* check that a write has not made the stripe insync */
2720 if (test_bit(STRIPE_INSYNC, &sh->state))
2721 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002722
2723 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07002724 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07002725 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002726 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07002727 if (s->failed == 2) {
2728 dev = &sh->dev[r6s->failed_num[1]];
2729 s->locked++;
2730 set_bit(R5_LOCKED, &dev->flags);
2731 set_bit(R5_Wantwrite, &dev->flags);
2732 }
2733 if (s->failed >= 1) {
2734 dev = &sh->dev[r6s->failed_num[0]];
2735 s->locked++;
2736 set_bit(R5_LOCKED, &dev->flags);
2737 set_bit(R5_Wantwrite, &dev->flags);
2738 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002739 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002740 dev = &sh->dev[pd_idx];
2741 s->locked++;
2742 set_bit(R5_LOCKED, &dev->flags);
2743 set_bit(R5_Wantwrite, &dev->flags);
2744 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07002745 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07002746 dev = &sh->dev[qd_idx];
2747 s->locked++;
2748 set_bit(R5_LOCKED, &dev->flags);
2749 set_bit(R5_Wantwrite, &dev->flags);
2750 }
2751 clear_bit(STRIPE_DEGRADED, &sh->state);
2752
2753 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002754 break;
2755 case check_state_run:
2756 case check_state_run_q:
2757 case check_state_run_pq:
2758 break; /* we will be called again upon completion */
2759 case check_state_check_result:
2760 sh->check_state = check_state_idle;
2761
2762 /* handle a successful check operation, if parity is correct
2763 * we are done. Otherwise update the mismatch count and repair
2764 * parity if !MD_RECOVERY_CHECK
2765 */
2766 if (sh->ops.zero_sum_result == 0) {
2767 /* both parities are correct */
2768 if (!s->failed)
2769 set_bit(STRIPE_INSYNC, &sh->state);
2770 else {
2771 /* in contrast to the raid5 case we can validate
2772 * parity, but still have a failure to write
2773 * back
2774 */
2775 sh->check_state = check_state_compute_result;
2776 /* Returning at this point means that we may go
2777 * off and bring p and/or q uptodate again so
2778 * we make sure to check zero_sum_result again
2779 * to verify if p or q need writeback
2780 */
2781 }
2782 } else {
2783 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2784 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2785 /* don't try to repair!! */
2786 set_bit(STRIPE_INSYNC, &sh->state);
2787 else {
2788 int *target = &sh->ops.target;
2789
2790 sh->ops.target = -1;
2791 sh->ops.target2 = -1;
2792 sh->check_state = check_state_compute_run;
2793 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2794 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2795 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2796 set_bit(R5_Wantcompute,
2797 &sh->dev[pd_idx].flags);
2798 *target = pd_idx;
2799 target = &sh->ops.target2;
2800 s->uptodate++;
2801 }
2802 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2803 set_bit(R5_Wantcompute,
2804 &sh->dev[qd_idx].flags);
2805 *target = qd_idx;
2806 s->uptodate++;
2807 }
2808 }
2809 }
2810 break;
2811 case check_state_compute_run:
2812 break;
2813 default:
2814 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2815 __func__, sh->check_state,
2816 (unsigned long long) sh->sector);
2817 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002818 }
2819}
2820
2821static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
2822 struct r6_state *r6s)
2823{
2824 int i;
2825
2826 /* We have read all the blocks in this stripe and now we need to
2827 * copy some of them into a target stripe for expand.
2828 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07002829 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002830 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2831 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11002832 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11002833 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07002834 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07002835 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07002836
NeilBrown784052e2009-03-31 15:19:07 +11002837 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11002838 sector_t s = raid5_compute_sector(conf, bn, 0,
2839 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10002840 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07002841 if (sh2 == NULL)
2842 /* so far only the early blocks of this stripe
2843 * have been requested. When later blocks
2844 * get requested, we will try again
2845 */
2846 continue;
2847 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2848 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2849 /* must have already done this block */
2850 release_stripe(sh2);
2851 continue;
2852 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07002853
2854 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07002855 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002856 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07002857 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002858 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002859
Dan Williamsa4456852007-07-09 11:56:43 -07002860 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2861 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2862 for (j = 0; j < conf->raid_disks; j++)
2863 if (j != sh2->pd_idx &&
NeilBrownd0dabf72009-03-31 14:39:38 +11002864 (!r6s || j != sh2->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002865 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2866 break;
2867 if (j == conf->raid_disks) {
2868 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2869 set_bit(STRIPE_HANDLE, &sh2->state);
2870 }
2871 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07002872
Dan Williamsa4456852007-07-09 11:56:43 -07002873 }
NeilBrowna2e08552007-09-11 15:23:36 -07002874 /* done submitting copies, wait for them to complete */
2875 if (tx) {
2876 async_tx_ack(tx);
2877 dma_wait_for_async_tx(tx);
2878 }
Dan Williamsa4456852007-07-09 11:56:43 -07002879}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Dan Williams6bfe0b42008-04-30 00:52:32 -07002881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/*
2883 * handle_stripe - do things to a stripe.
2884 *
2885 * We lock the stripe and then examine the state of various bits
2886 * to see what needs to be done.
2887 * Possible results:
2888 * return some read request which now have data
2889 * return some write requests which are safely on disc
2890 * schedule a read on some buffers
2891 * schedule a write of some buffers
2892 * return confirmation of parity correctness
2893 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 * buffers are taken off read_list or write_list, and bh_cache buffers
2895 * get BH_Lock set before the stripe lock is released.
2896 *
2897 */
Dan Williamsa4456852007-07-09 11:56:43 -07002898
Dan Williamsdf10cfb2008-07-28 23:10:39 -07002899static bool handle_stripe5(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
2901 raid5_conf_t *conf = sh->raid_conf;
Dan Williamsa4456852007-07-09 11:56:43 -07002902 int disks = sh->disks, i;
2903 struct bio *return_bi = NULL;
2904 struct stripe_head_state s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 struct r5dev *dev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002906 mdk_rdev_t *blocked_rdev = NULL;
Dan Williamse0a115e2008-06-05 22:45:52 -07002907 int prexor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Dan Williamsa4456852007-07-09 11:56:43 -07002909 memset(&s, 0, sizeof(s));
Dan Williams600aa102008-06-28 08:32:05 +10002910 pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
2911 "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
2912 atomic_read(&sh->count), sh->pd_idx, sh->check_state,
2913 sh->reconstruct_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 spin_lock(&sh->lock);
2916 clear_bit(STRIPE_HANDLE, &sh->state);
2917 clear_bit(STRIPE_DELAYED, &sh->state);
2918
Dan Williamsa4456852007-07-09 11:56:43 -07002919 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
2920 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2921 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
Dan Williams83de75c2008-06-28 08:31:58 +10002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 /* Now to look around and see what can be done */
NeilBrown9910f162006-01-06 00:20:24 -08002924 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 for (i=disks; i--; ) {
2926 mdk_rdev_t *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002927 struct r5dev *dev = &sh->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 clear_bit(R5_Insync, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Dan Williamsb5e98d62007-01-02 13:52:31 -07002930 pr_debug("check %d: state 0x%lx toread %p read %p write %p "
2931 "written %p\n", i, dev->flags, dev->toread, dev->read,
2932 dev->towrite, dev->written);
2933
2934 /* maybe we can request a biofill operation
2935 *
2936 * new wantfill requests are only permitted while
Dan Williams83de75c2008-06-28 08:31:58 +10002937 * ops_complete_biofill is guaranteed to be inactive
Dan Williamsb5e98d62007-01-02 13:52:31 -07002938 */
2939 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
Dan Williams83de75c2008-06-28 08:31:58 +10002940 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
Dan Williamsb5e98d62007-01-02 13:52:31 -07002941 set_bit(R5_Wantfill, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07002944 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
2945 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williamsf38e1212007-01-02 13:52:30 -07002946 if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Dan Williamsb5e98d62007-01-02 13:52:31 -07002948 if (test_bit(R5_Wantfill, &dev->flags))
2949 s.to_fill++;
2950 else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07002951 s.to_read++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07002953 s.to_write++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07002955 s.non_overwrite++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
Dan Williamsa4456852007-07-09 11:56:43 -07002957 if (dev->written)
2958 s.written++;
NeilBrown9910f162006-01-06 00:20:24 -08002959 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10002960 if (blocked_rdev == NULL &&
2961 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002962 blocked_rdev = rdev;
2963 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002964 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002965 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
NeilBrown14f8d262006-01-06 00:20:14 -08002966 /* The ReadError flag will just be confusing now */
NeilBrown4e5314b2005-11-08 21:39:22 -08002967 clear_bit(R5_ReadError, &dev->flags);
2968 clear_bit(R5_ReWrite, &dev->flags);
2969 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002970 if (!rdev || !test_bit(In_sync, &rdev->flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08002971 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002972 s.failed++;
2973 s.failed_num = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 } else
2975 set_bit(R5_Insync, &dev->flags);
2976 }
NeilBrown9910f162006-01-06 00:20:24 -08002977 rcu_read_unlock();
Dan Williamsb5e98d62007-01-02 13:52:31 -07002978
Dan Williams6bfe0b42008-04-30 00:52:32 -07002979 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10002980 if (s.syncing || s.expanding || s.expanded ||
2981 s.to_write || s.written) {
2982 set_bit(STRIPE_HANDLE, &sh->state);
2983 goto unlock;
2984 }
2985 /* There is nothing for the blocked_rdev to block */
2986 rdev_dec_pending(blocked_rdev, conf->mddev);
2987 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002988 }
2989
Dan Williams83de75c2008-06-28 08:31:58 +10002990 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
2991 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
2992 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
2993 }
Dan Williamsb5e98d62007-01-02 13:52:31 -07002994
Dan Williams45b42332007-07-09 11:56:43 -07002995 pr_debug("locked=%d uptodate=%d to_read=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 " to_write=%d failed=%d failed_num=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002997 s.locked, s.uptodate, s.to_read, s.to_write,
2998 s.failed, s.failed_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 /* check if the array has lost two devices and, if so, some requests might
3000 * need to be failed
3001 */
Dan Williamsa4456852007-07-09 11:56:43 -07003002 if (s.failed > 1 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003003 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003004 if (s.failed > 1 && s.syncing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3006 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003007 s.syncing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
3009
3010 /* might be able to return some write requests if the parity block
3011 * is safe, or on a failed drive
3012 */
3013 dev = &sh->dev[sh->pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003014 if ( s.written &&
3015 ((test_bit(R5_Insync, &dev->flags) &&
3016 !test_bit(R5_LOCKED, &dev->flags) &&
3017 test_bit(R5_UPTODATE, &dev->flags)) ||
3018 (s.failed == 1 && s.failed_num == sh->pd_idx)))
Dan Williams1fe797e2008-06-28 09:16:30 +10003019 handle_stripe_clean_event(conf, sh, disks, &return_bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021 /* Now we might consider reading some blocks, either to check/generate
3022 * parity, or to satisfy requests
3023 * or to load a block that is being partially written.
3024 */
Dan Williamsa4456852007-07-09 11:56:43 -07003025 if (s.to_read || s.non_overwrite ||
Dan Williams976ea8d2008-06-28 08:32:03 +10003026 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003027 handle_stripe_fill5(sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Dan Williamse33129d2007-01-02 13:52:30 -07003029 /* Now we check to see if any write operations have recently
3030 * completed
3031 */
Dan Williamse0a115e2008-06-05 22:45:52 -07003032 prexor = 0;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003033 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
Dan Williamse0a115e2008-06-05 22:45:52 -07003034 prexor = 1;
Dan Williamsd8ee0722008-06-28 08:32:06 +10003035 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3036 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
Dan Williams600aa102008-06-28 08:32:05 +10003037 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamse33129d2007-01-02 13:52:30 -07003038
3039 /* All the 'written' buffers and the parity block are ready to
3040 * be written back to disk
3041 */
3042 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3043 for (i = disks; i--; ) {
3044 dev = &sh->dev[i];
3045 if (test_bit(R5_LOCKED, &dev->flags) &&
3046 (i == sh->pd_idx || dev->written)) {
3047 pr_debug("Writing block %d\n", i);
3048 set_bit(R5_Wantwrite, &dev->flags);
Dan Williamse0a115e2008-06-05 22:45:52 -07003049 if (prexor)
3050 continue;
Dan Williamse33129d2007-01-02 13:52:30 -07003051 if (!test_bit(R5_Insync, &dev->flags) ||
3052 (i == sh->pd_idx && s.failed == 0))
3053 set_bit(STRIPE_INSYNC, &sh->state);
3054 }
3055 }
3056 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3057 atomic_dec(&conf->preread_active_stripes);
3058 if (atomic_read(&conf->preread_active_stripes) <
3059 IO_THRESHOLD)
3060 md_wakeup_thread(conf->mddev->thread);
3061 }
3062 }
3063
3064 /* Now to consider new write requests and what else, if anything
3065 * should be read. We do not handle new writes when:
3066 * 1/ A 'write' operation (copy+xor) is already in flight.
3067 * 2/ A 'check' operation is in flight, as it may clobber the parity
3068 * block.
3069 */
Dan Williams600aa102008-06-28 08:32:05 +10003070 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003071 handle_stripe_dirtying5(conf, sh, &s, disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamse89f8962007-01-02 13:52:31 -07003074 * Any reads will already have been scheduled, so we just see if enough
3075 * data is available. The parity check is held off while parity
3076 * dependent operations are in flight.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 */
Dan Williamsecc65c92008-06-28 08:31:57 +10003078 if (sh->check_state ||
3079 (s.syncing && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003080 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
Dan Williamsecc65c92008-06-28 08:31:57 +10003081 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williamsa4456852007-07-09 11:56:43 -07003082 handle_parity_checks5(conf, sh, &s, disks);
Dan Williamse89f8962007-01-02 13:52:31 -07003083
Dan Williamsa4456852007-07-09 11:56:43 -07003084 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3086 clear_bit(STRIPE_SYNCING, &sh->state);
3087 }
NeilBrown4e5314b2005-11-08 21:39:22 -08003088
3089 /* If the failed drive is just a ReadError, then we might need to progress
3090 * the repair/check process
3091 */
Dan Williamsa4456852007-07-09 11:56:43 -07003092 if (s.failed == 1 && !conf->mddev->ro &&
3093 test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
3094 && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
3095 && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
NeilBrown4e5314b2005-11-08 21:39:22 -08003096 ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003097 dev = &sh->dev[s.failed_num];
NeilBrown4e5314b2005-11-08 21:39:22 -08003098 if (!test_bit(R5_ReWrite, &dev->flags)) {
3099 set_bit(R5_Wantwrite, &dev->flags);
3100 set_bit(R5_ReWrite, &dev->flags);
3101 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003102 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003103 } else {
3104 /* let's read it back */
3105 set_bit(R5_Wantread, &dev->flags);
3106 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003107 s.locked++;
NeilBrown4e5314b2005-11-08 21:39:22 -08003108 }
3109 }
3110
Dan Williams600aa102008-06-28 08:32:05 +10003111 /* Finish reconstruct operations initiated by the expansion process */
3112 if (sh->reconstruct_state == reconstruct_state_result) {
NeilBrownab69ae12009-03-31 15:26:47 +11003113 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003114 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003115 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3116 /* sh cannot be written until sh2 has been read.
3117 * so arrange for sh to be delayed a little
3118 */
3119 set_bit(STRIPE_DELAYED, &sh->state);
3120 set_bit(STRIPE_HANDLE, &sh->state);
3121 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3122 &sh2->state))
3123 atomic_inc(&conf->preread_active_stripes);
3124 release_stripe(sh2);
3125 goto unlock;
3126 }
3127 if (sh2)
3128 release_stripe(sh2);
3129
Dan Williams600aa102008-06-28 08:32:05 +10003130 sh->reconstruct_state = reconstruct_state_idle;
Dan Williamsf0a50d32007-01-02 13:52:31 -07003131 clear_bit(STRIPE_EXPANDING, &sh->state);
Dan Williams23397882008-07-23 20:05:34 -07003132 for (i = conf->raid_disks; i--; ) {
Dan Williamsf0a50d32007-01-02 13:52:31 -07003133 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Dan Williams23397882008-07-23 20:05:34 -07003134 set_bit(R5_LOCKED, &sh->dev[i].flags);
Neil Brownefe31142008-06-28 08:31:14 +10003135 s.locked++;
Dan Williams23397882008-07-23 20:05:34 -07003136 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003137 }
3138
3139 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
Dan Williams600aa102008-06-28 08:32:05 +10003140 !sh->reconstruct_state) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003141 /* Need to write out all blocks after computing parity */
3142 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003143 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003144 schedule_reconstruction(sh, &s, 1, 1);
Dan Williams600aa102008-06-28 08:32:05 +10003145 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003146 clear_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrownf6705572006-03-27 01:18:11 -08003147 atomic_dec(&conf->reshape_stripes);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003148 wake_up(&conf->wait_for_overlap);
3149 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3150 }
3151
Dan Williams0f94e87c2008-01-08 15:32:53 -08003152 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003153 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003154 handle_stripe_expansion(conf, sh, NULL);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003155
Dan Williams6bfe0b42008-04-30 00:52:32 -07003156 unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 spin_unlock(&sh->lock);
3158
Dan Williams6bfe0b42008-04-30 00:52:32 -07003159 /* wait for this device to become unblocked */
3160 if (unlikely(blocked_rdev))
3161 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3162
Dan Williams600aa102008-06-28 08:32:05 +10003163 if (s.ops_request)
Dan Williamsac6b53b2009-07-14 13:40:19 -07003164 raid_run_ops(sh, s.ops_request);
Dan Williamsd84e0f12007-01-02 13:52:30 -07003165
Dan Williamsc4e5ac02008-06-28 08:31:53 +10003166 ops_run_io(sh, &s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Dan Williamsa4456852007-07-09 11:56:43 -07003168 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003169
3170 return blocked_rdev == NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
Dan Williams36d1c642009-07-14 11:48:22 -07003173static bool handle_stripe6(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003174{
NeilBrownbff61972009-03-31 14:33:13 +11003175 raid5_conf_t *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003176 int disks = sh->disks;
Dan Williamsa4456852007-07-09 11:56:43 -07003177 struct bio *return_bi = NULL;
NeilBrown34e04e82009-03-31 15:10:16 +11003178 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
Dan Williamsa4456852007-07-09 11:56:43 -07003179 struct stripe_head_state s;
3180 struct r6_state r6s;
NeilBrown16a53ec2006-06-26 00:27:38 -07003181 struct r5dev *dev, *pdev, *qdev;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003182 mdk_rdev_t *blocked_rdev = NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003183
Dan Williams45b42332007-07-09 11:56:43 -07003184 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003185 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003186 (unsigned long long)sh->sector, sh->state,
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003187 atomic_read(&sh->count), pd_idx, qd_idx,
3188 sh->check_state, sh->reconstruct_state);
Dan Williamsa4456852007-07-09 11:56:43 -07003189 memset(&s, 0, sizeof(s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003190
3191 spin_lock(&sh->lock);
3192 clear_bit(STRIPE_HANDLE, &sh->state);
3193 clear_bit(STRIPE_DELAYED, &sh->state);
3194
Dan Williamsa4456852007-07-09 11:56:43 -07003195 s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
3196 s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3197 s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003198 /* Now to look around and see what can be done */
3199
3200 rcu_read_lock();
3201 for (i=disks; i--; ) {
3202 mdk_rdev_t *rdev;
3203 dev = &sh->dev[i];
3204 clear_bit(R5_Insync, &dev->flags);
3205
Dan Williams45b42332007-07-09 11:56:43 -07003206 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown16a53ec2006-06-26 00:27:38 -07003207 i, dev->flags, dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003208 /* maybe we can reply to a read
3209 *
3210 * new wantfill requests are only permitted while
3211 * ops_complete_biofill is guaranteed to be inactive
3212 */
3213 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3214 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3215 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003216
3217 /* now count some things */
Dan Williamsa4456852007-07-09 11:56:43 -07003218 if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
3219 if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003220 if (test_bit(R5_Wantcompute, &dev->flags)) {
3221 s.compute++;
3222 BUG_ON(s.compute > 2);
3223 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003224
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003225 if (test_bit(R5_Wantfill, &dev->flags)) {
3226 s.to_fill++;
3227 } else if (dev->toread)
Dan Williamsa4456852007-07-09 11:56:43 -07003228 s.to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003229 if (dev->towrite) {
Dan Williamsa4456852007-07-09 11:56:43 -07003230 s.to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003231 if (!test_bit(R5_OVERWRITE, &dev->flags))
Dan Williamsa4456852007-07-09 11:56:43 -07003232 s.non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003233 }
Dan Williamsa4456852007-07-09 11:56:43 -07003234 if (dev->written)
3235 s.written++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003236 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownac4090d2008-08-05 15:54:13 +10003237 if (blocked_rdev == NULL &&
3238 rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07003239 blocked_rdev = rdev;
3240 atomic_inc(&rdev->nr_pending);
Dan Williams6bfe0b42008-04-30 00:52:32 -07003241 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003242 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
3243 /* The ReadError flag will just be confusing now */
3244 clear_bit(R5_ReadError, &dev->flags);
3245 clear_bit(R5_ReWrite, &dev->flags);
3246 }
3247 if (!rdev || !test_bit(In_sync, &rdev->flags)
3248 || test_bit(R5_ReadError, &dev->flags)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003249 if (s.failed < 2)
3250 r6s.failed_num[s.failed] = i;
3251 s.failed++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003252 } else
3253 set_bit(R5_Insync, &dev->flags);
3254 }
3255 rcu_read_unlock();
Dan Williams6bfe0b42008-04-30 00:52:32 -07003256
3257 if (unlikely(blocked_rdev)) {
NeilBrownac4090d2008-08-05 15:54:13 +10003258 if (s.syncing || s.expanding || s.expanded ||
3259 s.to_write || s.written) {
3260 set_bit(STRIPE_HANDLE, &sh->state);
3261 goto unlock;
3262 }
3263 /* There is nothing for the blocked_rdev to block */
3264 rdev_dec_pending(blocked_rdev, conf->mddev);
3265 blocked_rdev = NULL;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003266 }
NeilBrownac4090d2008-08-05 15:54:13 +10003267
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003268 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3269 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3270 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3271 }
3272
Dan Williams45b42332007-07-09 11:56:43 -07003273 pr_debug("locked=%d uptodate=%d to_read=%d"
NeilBrown16a53ec2006-06-26 00:27:38 -07003274 " to_write=%d failed=%d failed_num=%d,%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003275 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3276 r6s.failed_num[0], r6s.failed_num[1]);
3277 /* check if the array has lost >2 devices and, if so, some requests
3278 * might need to be failed
NeilBrown16a53ec2006-06-26 00:27:38 -07003279 */
Dan Williamsa4456852007-07-09 11:56:43 -07003280 if (s.failed > 2 && s.to_read+s.to_write+s.written)
Dan Williams1fe797e2008-06-28 09:16:30 +10003281 handle_failed_stripe(conf, sh, &s, disks, &return_bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003282 if (s.failed > 2 && s.syncing) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003283 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
3284 clear_bit(STRIPE_SYNCING, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003285 s.syncing = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003286 }
3287
3288 /*
3289 * might be able to return some write requests if the parity blocks
3290 * are safe, or on a failed drive
3291 */
3292 pdev = &sh->dev[pd_idx];
Dan Williamsa4456852007-07-09 11:56:43 -07003293 r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
3294 || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
NeilBrown34e04e82009-03-31 15:10:16 +11003295 qdev = &sh->dev[qd_idx];
3296 r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
3297 || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
NeilBrown16a53ec2006-06-26 00:27:38 -07003298
Dan Williamsa4456852007-07-09 11:56:43 -07003299 if ( s.written &&
3300 ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003301 && !test_bit(R5_LOCKED, &pdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003302 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3303 ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
NeilBrown16a53ec2006-06-26 00:27:38 -07003304 && !test_bit(R5_LOCKED, &qdev->flags)
Dan Williamsa4456852007-07-09 11:56:43 -07003305 && test_bit(R5_UPTODATE, &qdev->flags)))))
Dan Williams1fe797e2008-06-28 09:16:30 +10003306 handle_stripe_clean_event(conf, sh, disks, &return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003307
3308 /* Now we might consider reading some blocks, either to check/generate
3309 * parity, or to satisfy requests
3310 * or to load a block that is being partially written.
3311 */
Dan Williamsa4456852007-07-09 11:56:43 -07003312 if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003313 (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
Dan Williams1fe797e2008-06-28 09:16:30 +10003314 handle_stripe_fill6(sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003315
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003316 /* Now we check to see if any write operations have recently
3317 * completed
3318 */
3319 if (sh->reconstruct_state == reconstruct_state_drain_result) {
3320 int qd_idx = sh->qd_idx;
3321
3322 sh->reconstruct_state = reconstruct_state_idle;
3323 /* All the 'written' buffers and the parity blocks are ready to
3324 * be written back to disk
3325 */
3326 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3327 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags));
3328 for (i = disks; i--; ) {
3329 dev = &sh->dev[i];
3330 if (test_bit(R5_LOCKED, &dev->flags) &&
3331 (i == sh->pd_idx || i == qd_idx ||
3332 dev->written)) {
3333 pr_debug("Writing block %d\n", i);
3334 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3335 set_bit(R5_Wantwrite, &dev->flags);
3336 if (!test_bit(R5_Insync, &dev->flags) ||
3337 ((i == sh->pd_idx || i == qd_idx) &&
3338 s.failed == 0))
3339 set_bit(STRIPE_INSYNC, &sh->state);
3340 }
3341 }
3342 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
3343 atomic_dec(&conf->preread_active_stripes);
3344 if (atomic_read(&conf->preread_active_stripes) <
3345 IO_THRESHOLD)
3346 md_wakeup_thread(conf->mddev->thread);
3347 }
3348 }
3349
Yuri Tikhonova9b39a72009-08-29 19:13:12 -07003350 /* Now to consider new write requests and what else, if anything
3351 * should be read. We do not handle new writes when:
3352 * 1/ A 'write' operation (copy+gen_syndrome) is already in flight.
3353 * 2/ A 'check' operation is in flight, as it may clobber the parity
3354 * block.
3355 */
3356 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
Dan Williams1fe797e2008-06-28 09:16:30 +10003357 handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003358
3359 /* maybe we need to check and possibly fix the parity for this stripe
Dan Williamsa4456852007-07-09 11:56:43 -07003360 * Any reads will already have been scheduled, so we just see if enough
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003361 * data is available. The parity check is held off while parity
3362 * dependent operations are in flight.
NeilBrown16a53ec2006-06-26 00:27:38 -07003363 */
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003364 if (sh->check_state ||
3365 (s.syncing && s.locked == 0 &&
3366 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3367 !test_bit(STRIPE_INSYNC, &sh->state)))
Dan Williams36d1c642009-07-14 11:48:22 -07003368 handle_parity_checks6(conf, sh, &s, &r6s, disks);
NeilBrown16a53ec2006-06-26 00:27:38 -07003369
Dan Williamsa4456852007-07-09 11:56:43 -07003370 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003371 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
3372 clear_bit(STRIPE_SYNCING, &sh->state);
3373 }
3374
3375 /* If the failed drives are just a ReadError, then we might need
3376 * to progress the repair/check process
3377 */
Dan Williamsa4456852007-07-09 11:56:43 -07003378 if (s.failed <= 2 && !conf->mddev->ro)
3379 for (i = 0; i < s.failed; i++) {
3380 dev = &sh->dev[r6s.failed_num[i]];
NeilBrown16a53ec2006-06-26 00:27:38 -07003381 if (test_bit(R5_ReadError, &dev->flags)
3382 && !test_bit(R5_LOCKED, &dev->flags)
3383 && test_bit(R5_UPTODATE, &dev->flags)
3384 ) {
3385 if (!test_bit(R5_ReWrite, &dev->flags)) {
3386 set_bit(R5_Wantwrite, &dev->flags);
3387 set_bit(R5_ReWrite, &dev->flags);
3388 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003389 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003390 } else {
3391 /* let's read it back */
3392 set_bit(R5_Wantread, &dev->flags);
3393 set_bit(R5_LOCKED, &dev->flags);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003394 s.locked++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003395 }
3396 }
3397 }
NeilBrownf4168852007-02-28 20:11:53 -08003398
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003399 /* Finish reconstruct operations initiated by the expansion process */
3400 if (sh->reconstruct_state == reconstruct_state_result) {
3401 sh->reconstruct_state = reconstruct_state_idle;
3402 clear_bit(STRIPE_EXPANDING, &sh->state);
3403 for (i = conf->raid_disks; i--; ) {
3404 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3405 set_bit(R5_LOCKED, &sh->dev[i].flags);
3406 s.locked++;
3407 }
3408 }
3409
3410 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3411 !sh->reconstruct_state) {
NeilBrownab69ae12009-03-31 15:26:47 +11003412 struct stripe_head *sh2
NeilBrowna8c906c2009-06-09 14:39:59 +10003413 = get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrownab69ae12009-03-31 15:26:47 +11003414 if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
3415 /* sh cannot be written until sh2 has been read.
3416 * so arrange for sh to be delayed a little
3417 */
3418 set_bit(STRIPE_DELAYED, &sh->state);
3419 set_bit(STRIPE_HANDLE, &sh->state);
3420 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3421 &sh2->state))
3422 atomic_inc(&conf->preread_active_stripes);
3423 release_stripe(sh2);
3424 goto unlock;
3425 }
3426 if (sh2)
3427 release_stripe(sh2);
3428
NeilBrownf4168852007-02-28 20:11:53 -08003429 /* Need to write out all blocks after computing P&Q */
3430 sh->disks = conf->raid_disks;
NeilBrown911d4ee2009-03-31 14:39:38 +11003431 stripe_set_idx(sh->sector, conf, 0, sh);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003432 schedule_reconstruction(sh, &s, 1, 1);
3433 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
NeilBrownf4168852007-02-28 20:11:53 -08003434 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3435 atomic_dec(&conf->reshape_stripes);
3436 wake_up(&conf->wait_for_overlap);
3437 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3438 }
3439
Dan Williams0f94e87c2008-01-08 15:32:53 -08003440 if (s.expanding && s.locked == 0 &&
Dan Williams976ea8d2008-06-28 08:32:03 +10003441 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
Dan Williamsa4456852007-07-09 11:56:43 -07003442 handle_stripe_expansion(conf, sh, &r6s);
NeilBrownf4168852007-02-28 20:11:53 -08003443
Dan Williams6bfe0b42008-04-30 00:52:32 -07003444 unlock:
NeilBrown16a53ec2006-06-26 00:27:38 -07003445 spin_unlock(&sh->lock);
3446
Dan Williams6bfe0b42008-04-30 00:52:32 -07003447 /* wait for this device to become unblocked */
3448 if (unlikely(blocked_rdev))
3449 md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
3450
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003451 if (s.ops_request)
3452 raid_run_ops(sh, s.ops_request);
3453
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003454 ops_run_io(sh, &s);
3455
Dan Williamsa4456852007-07-09 11:56:43 -07003456 return_io(return_bi);
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003457
3458 return blocked_rdev == NULL;
NeilBrown16a53ec2006-06-26 00:27:38 -07003459}
3460
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003461/* returns true if the stripe was handled */
Dan Williams36d1c642009-07-14 11:48:22 -07003462static bool handle_stripe(struct stripe_head *sh)
NeilBrown16a53ec2006-06-26 00:27:38 -07003463{
3464 if (sh->raid_conf->level == 6)
Dan Williams36d1c642009-07-14 11:48:22 -07003465 return handle_stripe6(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003466 else
Dan Williamsdf10cfb2008-07-28 23:10:39 -07003467 return handle_stripe5(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -07003468}
3469
Arjan van de Ven858119e2006-01-14 13:20:43 -08003470static void raid5_activate_delayed(raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471{
3472 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3473 while (!list_empty(&conf->delayed_list)) {
3474 struct list_head *l = conf->delayed_list.next;
3475 struct stripe_head *sh;
3476 sh = list_entry(l, struct stripe_head, lru);
3477 list_del_init(l);
3478 clear_bit(STRIPE_DELAYED, &sh->state);
3479 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3480 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003481 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 }
NeilBrown6ed30032008-02-06 01:40:00 -08003483 } else
3484 blk_plug_device(conf->mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Arjan van de Ven858119e2006-01-14 13:20:43 -08003487static void activate_bit_delay(raid5_conf_t *conf)
NeilBrown72626682005-09-09 16:23:54 -07003488{
3489 /* device_lock is held */
3490 struct list_head head;
3491 list_add(&head, &conf->bitmap_list);
3492 list_del_init(&conf->bitmap_list);
3493 while (!list_empty(&head)) {
3494 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3495 list_del_init(&sh->lru);
3496 atomic_inc(&sh->count);
3497 __release_stripe(conf, sh);
3498 }
3499}
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501static void unplug_slaves(mddev_t *mddev)
3502{
NeilBrown070ec552009-06-16 16:54:21 +10003503 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 int i;
3505
3506 rcu_read_lock();
NeilBrownf001a702009-06-09 14:30:31 +10003507 for (i = 0; i < conf->raid_disks; i++) {
Suzanne Woodd6065f72005-11-08 21:39:27 -08003508 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownb2d444d2005-11-08 21:39:31 -08003509 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
Jens Axboe165125e2007-07-24 09:28:11 +02003510 struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512 atomic_inc(&rdev->nr_pending);
3513 rcu_read_unlock();
3514
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05003515 blk_unplug(r_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 rdev_dec_pending(rdev, mddev);
3518 rcu_read_lock();
3519 }
3520 }
3521 rcu_read_unlock();
3522}
3523
Jens Axboe165125e2007-07-24 09:28:11 +02003524static void raid5_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
3526 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003527 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 unsigned long flags;
3529
3530 spin_lock_irqsave(&conf->device_lock, flags);
3531
NeilBrown72626682005-09-09 16:23:54 -07003532 if (blk_remove_plug(q)) {
3533 conf->seq_flush++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07003535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 md_wakeup_thread(mddev->thread);
3537
3538 spin_unlock_irqrestore(&conf->device_lock, flags);
3539
3540 unplug_slaves(mddev);
3541}
3542
NeilBrownf022b2f2006-10-03 01:15:56 -07003543static int raid5_congested(void *data, int bits)
3544{
3545 mddev_t *mddev = data;
NeilBrown070ec552009-06-16 16:54:21 +10003546 raid5_conf_t *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003547
3548 /* No difference between reads and writes. Just check
3549 * how busy the stripe_cache is
3550 */
3551 if (conf->inactive_blocked)
3552 return 1;
3553 if (conf->quiesce)
3554 return 1;
3555 if (list_empty_careful(&conf->inactive_list))
3556 return 1;
3557
3558 return 0;
3559}
3560
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003561/* We want read requests to align with chunks where possible,
3562 * but write requests don't need to.
3563 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003564static int raid5_mergeable_bvec(struct request_queue *q,
3565 struct bvec_merge_data *bvm,
3566 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003567{
3568 mddev_t *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003569 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003570 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003571 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003572 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003573
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003574 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003575 return biovec->bv_len; /* always allow writes to be mergeable */
3576
Andre Noll664e7c42009-06-18 08:45:27 +10003577 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3578 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003579 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3580 if (max < 0) max = 0;
3581 if (max <= biovec->bv_len && bio_sectors == 0)
3582 return biovec->bv_len;
3583 else
3584 return max;
3585}
3586
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003587
3588static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3589{
3590 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003591 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003592 unsigned int bio_sectors = bio->bi_size >> 9;
3593
Andre Noll664e7c42009-06-18 08:45:27 +10003594 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3595 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003596 return chunk_sectors >=
3597 ((sector & (chunk_sectors - 1)) + bio_sectors);
3598}
3599
3600/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003601 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3602 * later sampled by raid5d.
3603 */
3604static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3605{
3606 unsigned long flags;
3607
3608 spin_lock_irqsave(&conf->device_lock, flags);
3609
3610 bi->bi_next = conf->retry_read_aligned_list;
3611 conf->retry_read_aligned_list = bi;
3612
3613 spin_unlock_irqrestore(&conf->device_lock, flags);
3614 md_wakeup_thread(conf->mddev->thread);
3615}
3616
3617
3618static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3619{
3620 struct bio *bi;
3621
3622 bi = conf->retry_read_aligned;
3623 if (bi) {
3624 conf->retry_read_aligned = NULL;
3625 return bi;
3626 }
3627 bi = conf->retry_read_aligned_list;
3628 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003629 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003630 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003631 /*
3632 * this sets the active strip count to 1 and the processed
3633 * strip count to zero (upper 8 bits)
3634 */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003635 bi->bi_phys_segments = 1; /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003636 }
3637
3638 return bi;
3639}
3640
3641
3642/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003643 * The "raid5_align_endio" should check if the read succeeded and if it
3644 * did, call bio_endio on the original bio (having bio_put the new bio
3645 * first).
3646 * If the read failed..
3647 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003648static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003649{
3650 struct bio* raid_bi = bi->bi_private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003651 mddev_t *mddev;
3652 raid5_conf_t *conf;
3653 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3654 mdk_rdev_t *rdev;
3655
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003656 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003657
3658 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003659 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003660 rdev = (void*)raid_bi->bi_next;
3661 raid_bi->bi_next = NULL;
3662
3663 rdev_dec_pending(rdev, conf->mddev);
3664
3665 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003666 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003667 if (atomic_dec_and_test(&conf->active_aligned_reads))
3668 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003669 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003670 }
3671
3672
Dan Williams45b42332007-07-09 11:56:43 -07003673 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003674
3675 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003676}
3677
Neil Brown387bb172007-02-08 14:20:29 -08003678static int bio_fits_rdev(struct bio *bi)
3679{
Jens Axboe165125e2007-07-24 09:28:11 +02003680 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003681
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003682 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003683 return 0;
3684 blk_recount_segments(q, bi);
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003685 if (bi->bi_phys_segments > queue_max_phys_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003686 return 0;
3687
3688 if (q->merge_bvec_fn)
3689 /* it's too hard to apply the merge_bvec_fn at this stage,
3690 * just just give up
3691 */
3692 return 0;
3693
3694 return 1;
3695}
3696
3697
Jens Axboe165125e2007-07-24 09:28:11 +02003698static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003699{
3700 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003701 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003702 unsigned int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003703 struct bio* align_bi;
3704 mdk_rdev_t *rdev;
3705
3706 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003707 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003708 return 0;
3709 }
3710 /*
NeilBrown99c0fb52009-03-31 14:39:38 +11003711 * use bio_clone to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003712 */
3713 align_bi = bio_clone(raid_bio, GFP_NOIO);
3714 if (!align_bi)
3715 return 0;
3716 /*
3717 * set bi_end_io to a new function, and set bi_private to the
3718 * original bio.
3719 */
3720 align_bi->bi_end_io = raid5_align_endio;
3721 align_bi->bi_private = raid_bio;
3722 /*
3723 * compute position
3724 */
NeilBrown112bf892009-03-31 14:39:38 +11003725 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3726 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003727 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003728
3729 rcu_read_lock();
3730 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3731 if (rdev && test_bit(In_sync, &rdev->flags)) {
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003732 atomic_inc(&rdev->nr_pending);
3733 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003734 raid_bio->bi_next = (void*)rdev;
3735 align_bi->bi_bdev = rdev->bdev;
3736 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3737 align_bi->bi_sector += rdev->data_offset;
3738
Neil Brown387bb172007-02-08 14:20:29 -08003739 if (!bio_fits_rdev(align_bi)) {
3740 /* too big in some way */
3741 bio_put(align_bi);
3742 rdev_dec_pending(rdev, mddev);
3743 return 0;
3744 }
3745
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003746 spin_lock_irq(&conf->device_lock);
3747 wait_event_lock_irq(conf->wait_for_stripe,
3748 conf->quiesce == 0,
3749 conf->device_lock, /* nothing */);
3750 atomic_inc(&conf->active_aligned_reads);
3751 spin_unlock_irq(&conf->device_lock);
3752
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003753 generic_make_request(align_bi);
3754 return 1;
3755 } else {
3756 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003757 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003758 return 0;
3759 }
3760}
3761
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003762/* __get_priority_stripe - get the next stripe to process
3763 *
3764 * Full stripe writes are allowed to pass preread active stripes up until
3765 * the bypass_threshold is exceeded. In general the bypass_count
3766 * increments when the handle_list is handled before the hold_list; however, it
3767 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3768 * stripe with in flight i/o. The bypass_count will be reset when the
3769 * head of the hold_list has changed, i.e. the head was promoted to the
3770 * handle_list.
3771 */
3772static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3773{
3774 struct stripe_head *sh;
3775
3776 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3777 __func__,
3778 list_empty(&conf->handle_list) ? "empty" : "busy",
3779 list_empty(&conf->hold_list) ? "empty" : "busy",
3780 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3781
3782 if (!list_empty(&conf->handle_list)) {
3783 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3784
3785 if (list_empty(&conf->hold_list))
3786 conf->bypass_count = 0;
3787 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3788 if (conf->hold_list.next == conf->last_hold)
3789 conf->bypass_count++;
3790 else {
3791 conf->last_hold = conf->hold_list.next;
3792 conf->bypass_count -= conf->bypass_threshold;
3793 if (conf->bypass_count < 0)
3794 conf->bypass_count = 0;
3795 }
3796 }
3797 } else if (!list_empty(&conf->hold_list) &&
3798 ((conf->bypass_threshold &&
3799 conf->bypass_count > conf->bypass_threshold) ||
3800 atomic_read(&conf->pending_full_writes) == 0)) {
3801 sh = list_entry(conf->hold_list.next,
3802 typeof(*sh), lru);
3803 conf->bypass_count -= conf->bypass_threshold;
3804 if (conf->bypass_count < 0)
3805 conf->bypass_count = 0;
3806 } else
3807 return NULL;
3808
3809 list_del_init(&sh->lru);
3810 atomic_inc(&sh->count);
3811 BUG_ON(atomic_read(&sh->count) != 1);
3812 return sh;
3813}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003814
Jens Axboe165125e2007-07-24 09:28:11 +02003815static int make_request(struct request_queue *q, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
3817 mddev_t *mddev = q->queuedata;
NeilBrown070ec552009-06-16 16:54:21 +10003818 raid5_conf_t *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11003819 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 sector_t new_sector;
3821 sector_t logical_sector, last_sector;
3822 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01003823 const int rw = bio_data_dir(bi);
Tejun Heoc9959052008-08-25 19:47:21 +09003824 int cpu, remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
NeilBrowne5dcdd82005-09-09 16:23:41 -07003826 if (unlikely(bio_barrier(bi))) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003827 bio_endio(bi, -EOPNOTSUPP);
NeilBrowne5dcdd82005-09-09 16:23:41 -07003828 return 0;
3829 }
3830
NeilBrown3d310eb2005-06-21 17:17:26 -07003831 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07003832
Tejun Heo074a7ac2008-08-25 19:56:14 +09003833 cpu = part_stat_lock();
3834 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
3835 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
3836 bio_sectors(bi));
3837 part_stat_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
NeilBrown802ba062006-12-13 00:34:13 -08003839 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003840 mddev->reshape_position == MaxSector &&
3841 chunk_aligned_read(q,bi))
NeilBrown99c0fb52009-03-31 14:39:38 +11003842 return 0;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08003843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3845 last_sector = bi->bi_sector + (bi->bi_size>>9);
3846 bi->bi_next = NULL;
3847 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07003848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3850 DEFINE_WAIT(w);
NeilBrown16a53ec2006-06-26 00:27:38 -07003851 int disks, data_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003852 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08003853
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003854 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11003855 previous = 0;
NeilBrownb0f9ec02009-03-31 15:27:18 +11003856 disks = conf->raid_disks;
NeilBrownb578d552006-03-27 01:18:12 -08003857 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003858 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11003859 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08003860 * 64bit on a 32bit platform, and so it might be
3861 * possible to see a half-updated value
NeilBrownfef9c612009-03-31 15:16:46 +11003862 * Ofcourse reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08003863 * the lock is dropped, so once we get a reference
3864 * to the stripe that we think it is, we will have
3865 * to check again.
3866 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003867 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11003868 if (mddev->delta_disks < 0
3869 ? logical_sector < conf->reshape_progress
3870 : logical_sector >= conf->reshape_progress) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003871 disks = conf->previous_raid_disks;
NeilBrownb5663ba2009-03-31 14:39:38 +11003872 previous = 1;
3873 } else {
NeilBrownfef9c612009-03-31 15:16:46 +11003874 if (mddev->delta_disks < 0
3875 ? logical_sector < conf->reshape_safe
3876 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08003877 spin_unlock_irq(&conf->device_lock);
3878 schedule();
3879 goto retry;
3880 }
3881 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003882 spin_unlock_irq(&conf->device_lock);
3883 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003884 data_disks = disks - conf->max_degraded;
3885
NeilBrown112bf892009-03-31 14:39:38 +11003886 new_sector = raid5_compute_sector(conf, logical_sector,
3887 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003888 &dd_idx, NULL);
Dan Williams45b42332007-07-09 11:56:43 -07003889 pr_debug("raid5: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 (unsigned long long)new_sector,
3891 (unsigned long long)logical_sector);
3892
NeilBrownb5663ba2009-03-31 14:39:38 +11003893 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10003894 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11003896 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003897 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08003898 * stripe, so we must do the range check again.
3899 * Expansion could still move past after this
3900 * test, but as we are holding a reference to
3901 * 'sh', we know that if that happens,
3902 * STRIPE_EXPANDING will get set and the expansion
3903 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003904 */
3905 int must_retry = 0;
3906 spin_lock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11003907 if (mddev->delta_disks < 0
3908 ? logical_sector >= conf->reshape_progress
3909 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003910 /* mismatch, need to try again */
3911 must_retry = 1;
3912 spin_unlock_irq(&conf->device_lock);
3913 if (must_retry) {
3914 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07003915 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003916 goto retry;
3917 }
3918 }
NeilBrowne62e58a2009-07-01 13:15:35 +10003919
NeilBrowna5c308d2009-07-01 13:15:35 +10003920 if (bio_data_dir(bi) == WRITE &&
3921 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08003922 logical_sector < mddev->suspend_hi) {
3923 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10003924 /* As the suspend_* range is controlled by
3925 * userspace, we want an interruptible
3926 * wait.
3927 */
3928 flush_signals(current);
3929 prepare_to_wait(&conf->wait_for_overlap,
3930 &w, TASK_INTERRUPTIBLE);
3931 if (logical_sector >= mddev->suspend_lo &&
3932 logical_sector < mddev->suspend_hi)
3933 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08003934 goto retry;
3935 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08003936
3937 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3938 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
3939 /* Stripe is busy expanding or
3940 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 * and wait a while
3942 */
3943 raid5_unplug_device(mddev->queue);
3944 release_stripe(sh);
3945 schedule();
3946 goto retry;
3947 }
3948 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08003949 set_bit(STRIPE_HANDLE, &sh->state);
3950 clear_bit(STRIPE_DELAYED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 } else {
3953 /* cannot get stripe for read-ahead, just give-up */
3954 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3955 finish_wait(&conf->wait_for_overlap, &w);
3956 break;
3957 }
3958
3959 }
3960 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02003961 remaining = raid5_dec_bi_phys_segments(bi);
NeilBrownf6344752006-03-27 01:18:17 -08003962 spin_unlock_irq(&conf->device_lock);
3963 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964
NeilBrown16a53ec2006-06-26 00:27:38 -07003965 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02003967
Neil Brown0e13fe232008-06-28 08:31:20 +10003968 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 return 0;
3971}
3972
Dan Williamsb522adc2009-03-31 15:00:31 +11003973static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
3974
NeilBrown52c03292006-06-26 00:27:43 -07003975static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
NeilBrown52c03292006-06-26 00:27:43 -07003977 /* reshaping is quite different to recovery/resync so it is
3978 * handled quite separately ... here.
3979 *
3980 * On each call to sync_request, we gather one chunk worth of
3981 * destination stripes and flag them as expanding.
3982 * Then we find all the source stripes and request reads.
3983 * As the reads complete, handle_stripe will copy the data
3984 * into the destination stripe and release that stripe.
3985 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3987 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08003988 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08003989 int raid_disks = conf->previous_raid_disks;
3990 int data_disks = raid_disks - conf->max_degraded;
3991 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07003992 int i;
3993 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11003994 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11003995 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11003996 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11003997 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07003998
NeilBrownfef9c612009-03-31 15:16:46 +11003999 if (sector_nr == 0) {
4000 /* If restarting in the middle, skip the initial sectors */
4001 if (mddev->delta_disks < 0 &&
4002 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4003 sector_nr = raid5_size(mddev, 0, 0)
4004 - conf->reshape_progress;
NeilBrowna6397552009-08-13 10:13:00 +10004005 } else if (mddev->delta_disks >= 0 &&
NeilBrownfef9c612009-03-31 15:16:46 +11004006 conf->reshape_progress > 0)
4007 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004008 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004009 if (sector_nr) {
4010 *skipped = 1;
4011 return sector_nr;
4012 }
NeilBrown52c03292006-06-26 00:27:43 -07004013 }
4014
NeilBrown7a661382009-03-31 15:21:40 +11004015 /* We need to process a full chunk at a time.
4016 * If old and new chunk sizes differ, we need to process the
4017 * largest of these
4018 */
Andre Noll664e7c42009-06-18 08:45:27 +10004019 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4020 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004021 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004022 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004023
NeilBrown52c03292006-06-26 00:27:43 -07004024 /* we update the metadata when there is more than 3Meg
4025 * in the block range (that is rather arbitrary, should
4026 * probably be time based) or when the data about to be
4027 * copied would over-write the source of the data at
4028 * the front of the range.
NeilBrownfef9c612009-03-31 15:16:46 +11004029 * i.e. one new_stripe along from reshape_progress new_maps
4030 * to after where reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004031 */
NeilBrownfef9c612009-03-31 15:16:46 +11004032 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004033 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004034 readpos = conf->reshape_progress;
4035 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004036 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004037 sector_div(safepos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004038 if (mddev->delta_disks < 0) {
NeilBrowned37d832009-05-27 21:39:05 +10004039 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004040 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004041 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004042 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004043 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004044 readpos -= min_t(sector_t, reshape_sectors, readpos);
4045 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004046 }
NeilBrown52c03292006-06-26 00:27:43 -07004047
NeilBrownc8f517c2009-03-31 15:28:40 +11004048 /* 'writepos' is the most advanced device address we might write.
4049 * 'readpos' is the least advanced device address we might read.
4050 * 'safepos' is the least address recorded in the metadata as having
4051 * been reshaped.
4052 * If 'readpos' is behind 'writepos', then there is no way that we can
4053 * ensure safety in the face of a crash - that must be done by userspace
4054 * making a backup of the data. So in that case there is no particular
4055 * rush to update metadata.
4056 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4057 * update the metadata to advance 'safepos' to match 'readpos' so that
4058 * we can be safe in the event of a crash.
4059 * So we insist on updating metadata if safepos is behind writepos and
4060 * readpos is beyond writepos.
4061 * In any case, update the metadata every 10 seconds.
4062 * Maybe that number should be configurable, but I'm not sure it is
4063 * worth it.... maybe it could be a multiple of safemode_delay???
4064 */
NeilBrownfef9c612009-03-31 15:16:46 +11004065 if ((mddev->delta_disks < 0
NeilBrownc8f517c2009-03-31 15:28:40 +11004066 ? (safepos > writepos && readpos < writepos)
4067 : (safepos < writepos && readpos > writepos)) ||
4068 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004069 /* Cannot proceed until we've updated the superblock... */
4070 wait_event(conf->wait_for_overlap,
4071 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004072 mddev->reshape_position = conf->reshape_progress;
NeilBrownacb180b2009-04-14 16:28:34 +10004073 mddev->curr_resync_completed = mddev->curr_resync;
NeilBrownc8f517c2009-03-31 15:28:40 +11004074 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004075 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004076 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004077 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004078 kthread_should_stop());
4079 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004080 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004081 spin_unlock_irq(&conf->device_lock);
4082 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004083 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004084 }
4085
NeilBrownec32a2b2009-03-31 15:17:38 +11004086 if (mddev->delta_disks < 0) {
4087 BUG_ON(conf->reshape_progress == 0);
4088 stripe_addr = writepos;
4089 BUG_ON((mddev->dev_sectors &
NeilBrown7a661382009-03-31 15:21:40 +11004090 ~((sector_t)reshape_sectors - 1))
4091 - reshape_sectors - stripe_addr
NeilBrownec32a2b2009-03-31 15:17:38 +11004092 != sector_nr);
4093 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004094 BUG_ON(writepos != sector_nr + reshape_sectors);
NeilBrownec32a2b2009-03-31 15:17:38 +11004095 stripe_addr = sector_nr;
4096 }
NeilBrownab69ae12009-03-31 15:26:47 +11004097 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004098 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004099 int j;
4100 int skipped = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004101 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004102 set_bit(STRIPE_EXPANDING, &sh->state);
4103 atomic_inc(&conf->reshape_stripes);
4104 /* If any of this stripe is beyond the end of the old
4105 * array, then we need to zero those blocks
4106 */
4107 for (j=sh->disks; j--;) {
4108 sector_t s;
4109 if (j == sh->pd_idx)
4110 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004111 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004112 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004113 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004114 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004115 if (s < raid5_size(mddev, 0, 0)) {
NeilBrown52c03292006-06-26 00:27:43 -07004116 skipped = 1;
4117 continue;
4118 }
4119 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4120 set_bit(R5_Expanded, &sh->dev[j].flags);
4121 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4122 }
4123 if (!skipped) {
4124 set_bit(STRIPE_EXPAND_READY, &sh->state);
4125 set_bit(STRIPE_HANDLE, &sh->state);
4126 }
NeilBrownab69ae12009-03-31 15:26:47 +11004127 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004128 }
4129 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004130 if (mddev->delta_disks < 0)
NeilBrown7a661382009-03-31 15:21:40 +11004131 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004132 else
NeilBrown7a661382009-03-31 15:21:40 +11004133 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004134 spin_unlock_irq(&conf->device_lock);
4135 /* Ok, those stripe are ready. We can start scheduling
4136 * reads on the source stripes.
4137 * The source stripes are determined by mapping the first and last
4138 * block on the destination stripes.
4139 */
NeilBrown52c03292006-06-26 00:27:43 -07004140 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004141 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004142 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004143 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004144 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004145 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004146 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004147 if (last_sector >= mddev->dev_sectors)
4148 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004149 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004150 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004151 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4152 set_bit(STRIPE_HANDLE, &sh->state);
4153 release_stripe(sh);
4154 first_sector += STRIPE_SECTORS;
4155 }
NeilBrownab69ae12009-03-31 15:26:47 +11004156 /* Now that the sources are clearly marked, we can release
4157 * the destination stripes
4158 */
4159 while (!list_empty(&stripes)) {
4160 sh = list_entry(stripes.next, struct stripe_head, lru);
4161 list_del_init(&sh->lru);
4162 release_stripe(sh);
4163 }
NeilBrownc6207272008-02-06 01:39:52 -08004164 /* If this takes us to the resync_max point where we have to pause,
4165 * then we need to write out the superblock.
4166 */
NeilBrown7a661382009-03-31 15:21:40 +11004167 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004168 if ((sector_nr - mddev->curr_resync_completed) * 2
4169 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004170 /* Cannot proceed until we've updated the superblock... */
4171 wait_event(conf->wait_for_overlap,
4172 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004173 mddev->reshape_position = conf->reshape_progress;
NeilBrown48606a92009-06-18 09:14:12 +10004174 mddev->curr_resync_completed = mddev->curr_resync + reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11004175 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004176 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4177 md_wakeup_thread(mddev->thread);
4178 wait_event(mddev->sb_wait,
4179 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4180 || kthread_should_stop());
4181 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004182 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004183 spin_unlock_irq(&conf->device_lock);
4184 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004185 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004186 }
NeilBrown7a661382009-03-31 15:21:40 +11004187 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004188}
4189
4190/* FIXME go_faster isn't used */
4191static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4192{
4193 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
4194 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004195 sector_t max_sector = mddev->dev_sectors;
NeilBrown72626682005-09-09 16:23:54 -07004196 int sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004197 int still_degraded = 0;
4198 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
NeilBrown72626682005-09-09 16:23:54 -07004200 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 /* just being told to finish up .. nothing much to do */
4202 unplug_slaves(mddev);
NeilBrowncea9c222009-03-31 15:15:05 +11004203
NeilBrown29269552006-03-27 01:18:10 -08004204 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4205 end_reshape(conf);
4206 return 0;
4207 }
NeilBrown72626682005-09-09 16:23:54 -07004208
4209 if (mddev->curr_resync < max_sector) /* aborted */
4210 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4211 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004212 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004213 conf->fullsync = 0;
4214 bitmap_close_sync(mddev->bitmap);
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return 0;
4217 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004218
NeilBrown64bd6602009-08-03 10:59:58 +10004219 /* Allow raid5_quiesce to complete */
4220 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4221
NeilBrown52c03292006-06-26 00:27:43 -07004222 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4223 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004224
NeilBrownc6207272008-02-06 01:39:52 -08004225 /* No need to check resync_max as we never do more than one
4226 * stripe, and as resync_max will always be on a chunk boundary,
4227 * if the check in md_do_sync didn't fire, there is no chance
4228 * of overstepping resync_max here
4229 */
4230
NeilBrown16a53ec2006-06-26 00:27:38 -07004231 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 * to resync, then assert that we are finished, because there is
4233 * nothing we can do.
4234 */
NeilBrown3285edf2006-06-26 00:27:55 -07004235 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004236 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004237 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004238 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return rv;
4240 }
NeilBrown72626682005-09-09 16:23:54 -07004241 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004242 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004243 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4244 /* we can skip this block, and probably more */
4245 sync_blocks /= STRIPE_SECTORS;
4246 *skipped = 1;
4247 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
NeilBrownb47490c2008-02-06 01:39:50 -08004250
4251 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4252
NeilBrowna8c906c2009-06-09 14:39:59 +10004253 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004255 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004257 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004259 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004261 /* Need to check if array will still be degraded after recovery/resync
4262 * We don't need to check the 'failed' flag as when that gets set,
4263 * recovery aborts.
4264 */
NeilBrownf001a702009-06-09 14:30:31 +10004265 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004266 if (conf->disks[i].rdev == NULL)
4267 still_degraded = 1;
4268
4269 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4270
4271 spin_lock(&sh->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 set_bit(STRIPE_SYNCING, &sh->state);
4273 clear_bit(STRIPE_INSYNC, &sh->state);
4274 spin_unlock(&sh->lock);
4275
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004276 /* wait for any blocked device to be handled */
Dan Williams36d1c642009-07-14 11:48:22 -07004277 while (unlikely(!handle_stripe(sh)))
Dan Williamsdf10cfb2008-07-28 23:10:39 -07004278 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 release_stripe(sh);
4280
4281 return STRIPE_SECTORS;
4282}
4283
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004284static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4285{
4286 /* We may not be able to submit a whole bio at once as there
4287 * may not be enough stripe_heads available.
4288 * We cannot pre-allocate enough stripe_heads as we may need
4289 * more than exist in the cache (if we allow ever large chunks).
4290 * So we do one stripe head at a time and record in
4291 * ->bi_hw_segments how many have been done.
4292 *
4293 * We *know* that this entire raid_bio is in one chunk, so
4294 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4295 */
4296 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004297 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004298 sector_t sector, logical_sector, last_sector;
4299 int scnt = 0;
4300 int remaining;
4301 int handled = 0;
4302
4303 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004304 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004305 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004306 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4307
4308 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004309 logical_sector += STRIPE_SECTORS,
4310 sector += STRIPE_SECTORS,
4311 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004312
Jens Axboe960e7392008-08-15 10:41:18 +02004313 if (scnt < raid5_bi_hw_segments(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004314 /* already done this stripe */
4315 continue;
4316
NeilBrowna8c906c2009-06-09 14:39:59 +10004317 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004318
4319 if (!sh) {
4320 /* failed to get a stripe - must wait */
Jens Axboe960e7392008-08-15 10:41:18 +02004321 raid5_set_bi_hw_segments(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004322 conf->retry_read_aligned = raid_bio;
4323 return handled;
4324 }
4325
4326 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
Neil Brown387bb172007-02-08 14:20:29 -08004327 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4328 release_stripe(sh);
Jens Axboe960e7392008-08-15 10:41:18 +02004329 raid5_set_bi_hw_segments(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004330 conf->retry_read_aligned = raid_bio;
4331 return handled;
4332 }
4333
Dan Williams36d1c642009-07-14 11:48:22 -07004334 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004335 release_stripe(sh);
4336 handled++;
4337 }
4338 spin_lock_irq(&conf->device_lock);
Jens Axboe960e7392008-08-15 10:41:18 +02004339 remaining = raid5_dec_bi_phys_segments(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004340 spin_unlock_irq(&conf->device_lock);
Neil Brown0e13fe232008-06-28 08:31:20 +10004341 if (remaining == 0)
4342 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004343 if (atomic_dec_and_test(&conf->active_aligned_reads))
4344 wake_up(&conf->wait_for_stripe);
4345 return handled;
4346}
4347
Dan Williams07a3b412009-08-29 19:13:13 -07004348#ifdef CONFIG_MULTICORE_RAID456
4349static void __process_stripe(void *param, async_cookie_t cookie)
4350{
4351 struct stripe_head *sh = param;
4352
4353 handle_stripe(sh);
4354 release_stripe(sh);
4355}
4356
4357static void process_stripe(struct stripe_head *sh, struct list_head *domain)
4358{
4359 async_schedule_domain(__process_stripe, sh, domain);
4360}
4361
4362static void synchronize_stripe_processing(struct list_head *domain)
4363{
4364 async_synchronize_full_domain(domain);
4365}
4366#else
4367static void process_stripe(struct stripe_head *sh, struct list_head *domain)
4368{
4369 handle_stripe(sh);
4370 release_stripe(sh);
4371 cond_resched();
4372}
4373
4374static void synchronize_stripe_processing(struct list_head *domain)
4375{
4376}
4377#endif
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004378
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380/*
4381 * This is our raid5 kernel thread.
4382 *
4383 * We scan the hash table for stripes which can be handled now.
4384 * During the scan, completed stripes are saved for us by the interrupt
4385 * handler, so that they will not have to wait for our next wakeup.
4386 */
NeilBrown6ed30032008-02-06 01:40:00 -08004387static void raid5d(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
4389 struct stripe_head *sh;
NeilBrown070ec552009-06-16 16:54:21 +10004390 raid5_conf_t *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 int handled;
Dan Williams07a3b412009-08-29 19:13:13 -07004392 LIST_HEAD(raid_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Dan Williams45b42332007-07-09 11:56:43 -07004394 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
4398 handled = 0;
4399 spin_lock_irq(&conf->device_lock);
4400 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004401 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
NeilBrownae3c20c2006-07-10 04:44:17 -07004403 if (conf->seq_flush != conf->seq_write) {
NeilBrown72626682005-09-09 16:23:54 -07004404 int seq = conf->seq_flush;
NeilBrown700e4322005-11-28 13:44:10 -08004405 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004406 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004407 spin_lock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004408 conf->seq_write = seq;
4409 activate_bit_delay(conf);
4410 }
4411
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004412 while ((bio = remove_bio_from_retry(conf))) {
4413 int ok;
4414 spin_unlock_irq(&conf->device_lock);
4415 ok = retry_aligned_read(conf, bio);
4416 spin_lock_irq(&conf->device_lock);
4417 if (!ok)
4418 break;
4419 handled++;
4420 }
4421
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004422 sh = __get_priority_stripe(conf);
4423
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004424 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 spin_unlock_irq(&conf->device_lock);
4427
4428 handled++;
Dan Williams07a3b412009-08-29 19:13:13 -07004429 process_stripe(sh, &raid_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 spin_lock_irq(&conf->device_lock);
4432 }
Dan Williams45b42332007-07-09 11:56:43 -07004433 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 spin_unlock_irq(&conf->device_lock);
4436
Dan Williams07a3b412009-08-29 19:13:13 -07004437 synchronize_stripe_processing(&raid_domain);
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004438 async_tx_issue_pending_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 unplug_slaves(mddev);
4440
Dan Williams45b42332007-07-09 11:56:43 -07004441 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
NeilBrown3f294f42005-11-08 21:39:25 -08004444static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004445raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004446{
NeilBrown070ec552009-06-16 16:54:21 +10004447 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004448 if (conf)
4449 return sprintf(page, "%d\n", conf->max_nr_stripes);
4450 else
4451 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004452}
4453
4454static ssize_t
NeilBrown007583c2005-11-08 21:39:30 -08004455raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004456{
NeilBrown070ec552009-06-16 16:54:21 +10004457 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004458 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004459 int err;
4460
NeilBrown3f294f42005-11-08 21:39:25 -08004461 if (len >= PAGE_SIZE)
4462 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004463 if (!conf)
4464 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004465
Dan Williams4ef197d82008-04-28 02:15:54 -07004466 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004467 return -EINVAL;
4468 if (new <= 16 || new > 32768)
4469 return -EINVAL;
4470 while (new < conf->max_nr_stripes) {
4471 if (drop_one_stripe(conf))
4472 conf->max_nr_stripes--;
4473 else
4474 break;
4475 }
Dan Williamsb5470dc2008-06-27 21:44:04 -07004476 err = md_allow_write(mddev);
4477 if (err)
4478 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004479 while (new > conf->max_nr_stripes) {
4480 if (grow_one_stripe(conf))
4481 conf->max_nr_stripes++;
4482 else break;
4483 }
4484 return len;
4485}
NeilBrown007583c2005-11-08 21:39:30 -08004486
NeilBrown96de1e62005-11-08 21:39:39 -08004487static struct md_sysfs_entry
4488raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4489 raid5_show_stripe_cache_size,
4490 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004491
4492static ssize_t
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004493raid5_show_preread_threshold(mddev_t *mddev, char *page)
4494{
NeilBrown070ec552009-06-16 16:54:21 +10004495 raid5_conf_t *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004496 if (conf)
4497 return sprintf(page, "%d\n", conf->bypass_threshold);
4498 else
4499 return 0;
4500}
4501
4502static ssize_t
4503raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4504{
NeilBrown070ec552009-06-16 16:54:21 +10004505 raid5_conf_t *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004506 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004507 if (len >= PAGE_SIZE)
4508 return -EINVAL;
4509 if (!conf)
4510 return -ENODEV;
4511
Dan Williams4ef197d82008-04-28 02:15:54 -07004512 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004513 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004514 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004515 return -EINVAL;
4516 conf->bypass_threshold = new;
4517 return len;
4518}
4519
4520static struct md_sysfs_entry
4521raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4522 S_IRUGO | S_IWUSR,
4523 raid5_show_preread_threshold,
4524 raid5_store_preread_threshold);
4525
4526static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004527stripe_cache_active_show(mddev_t *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004528{
NeilBrown070ec552009-06-16 16:54:21 +10004529 raid5_conf_t *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004530 if (conf)
4531 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4532 else
4533 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004534}
4535
NeilBrown96de1e62005-11-08 21:39:39 -08004536static struct md_sysfs_entry
4537raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004538
NeilBrown007583c2005-11-08 21:39:30 -08004539static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004540 &raid5_stripecache_size.attr,
4541 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004542 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004543 NULL,
4544};
NeilBrown007583c2005-11-08 21:39:30 -08004545static struct attribute_group raid5_attrs_group = {
4546 .name = NULL,
4547 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004548};
4549
Dan Williams80c3a6c2009-03-17 18:10:40 -07004550static sector_t
4551raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4552{
NeilBrown070ec552009-06-16 16:54:21 +10004553 raid5_conf_t *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004554
4555 if (!sectors)
4556 sectors = mddev->dev_sectors;
NeilBrown7ec05472009-03-31 15:10:36 +11004557 if (!raid_disks) {
4558 /* size is defined by the smallest of previous and new size */
4559 if (conf->raid_disks < conf->previous_raid_disks)
4560 raid_disks = conf->raid_disks;
4561 else
4562 raid_disks = conf->previous_raid_disks;
4563 }
Dan Williams80c3a6c2009-03-17 18:10:40 -07004564
Andre Noll9d8f0362009-06-18 08:45:01 +10004565 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004566 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004567 return sectors * (raid_disks - conf->max_degraded);
4568}
4569
Dan Williams36d1c642009-07-14 11:48:22 -07004570static void raid5_free_percpu(raid5_conf_t *conf)
4571{
4572 struct raid5_percpu *percpu;
4573 unsigned long cpu;
4574
4575 if (!conf->percpu)
4576 return;
4577
4578 get_online_cpus();
4579 for_each_possible_cpu(cpu) {
4580 percpu = per_cpu_ptr(conf->percpu, cpu);
4581 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004582 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004583 }
4584#ifdef CONFIG_HOTPLUG_CPU
4585 unregister_cpu_notifier(&conf->cpu_notify);
4586#endif
4587 put_online_cpus();
4588
4589 free_percpu(conf->percpu);
4590}
4591
Dan Williamsa11034b2009-07-14 11:48:16 -07004592static void free_conf(raid5_conf_t *conf)
4593{
4594 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004595 raid5_free_percpu(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07004596 kfree(conf->disks);
4597 kfree(conf->stripe_hashtbl);
4598 kfree(conf);
4599}
4600
Dan Williams36d1c642009-07-14 11:48:22 -07004601#ifdef CONFIG_HOTPLUG_CPU
4602static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4603 void *hcpu)
4604{
4605 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4606 long cpu = (long)hcpu;
4607 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4608
4609 switch (action) {
4610 case CPU_UP_PREPARE:
4611 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004612 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004613 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004614 if (!percpu->scribble)
4615 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4616
4617 if (!percpu->scribble ||
4618 (conf->level == 6 && !percpu->spare_page)) {
4619 safe_put_page(percpu->spare_page);
4620 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004621 pr_err("%s: failed memory allocation for cpu%ld\n",
4622 __func__, cpu);
4623 return NOTIFY_BAD;
4624 }
4625 break;
4626 case CPU_DEAD:
4627 case CPU_DEAD_FROZEN:
4628 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004629 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004630 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004631 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004632 break;
4633 default:
4634 break;
4635 }
4636 return NOTIFY_OK;
4637}
4638#endif
4639
4640static int raid5_alloc_percpu(raid5_conf_t *conf)
4641{
4642 unsigned long cpu;
4643 struct page *spare_page;
4644 struct raid5_percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004645 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004646 int err;
4647
Dan Williams36d1c642009-07-14 11:48:22 -07004648 allcpus = alloc_percpu(struct raid5_percpu);
4649 if (!allcpus)
4650 return -ENOMEM;
4651 conf->percpu = allcpus;
4652
4653 get_online_cpus();
4654 err = 0;
4655 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004656 if (conf->level == 6) {
4657 spare_page = alloc_page(GFP_KERNEL);
4658 if (!spare_page) {
4659 err = -ENOMEM;
4660 break;
4661 }
4662 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4663 }
4664 scribble = kmalloc(scribble_len(conf->raid_disks), GFP_KERNEL);
4665 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004666 err = -ENOMEM;
4667 break;
4668 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004669 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004670 }
4671#ifdef CONFIG_HOTPLUG_CPU
4672 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4673 conf->cpu_notify.priority = 0;
4674 if (err == 0)
4675 err = register_cpu_notifier(&conf->cpu_notify);
4676#endif
4677 put_online_cpus();
4678
4679 return err;
4680}
4681
NeilBrown91adb562009-03-31 14:39:39 +11004682static raid5_conf_t *setup_conf(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
4684 raid5_conf_t *conf;
4685 int raid_disk, memory;
4686 mdk_rdev_t *rdev;
4687 struct disk_info *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
NeilBrown91adb562009-03-31 14:39:39 +11004689 if (mddev->new_level != 5
4690 && mddev->new_level != 4
4691 && mddev->new_level != 6) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004692 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004693 mdname(mddev), mddev->new_level);
4694 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
NeilBrown91adb562009-03-31 14:39:39 +11004696 if ((mddev->new_level == 5
4697 && !algorithm_valid_raid5(mddev->new_layout)) ||
4698 (mddev->new_level == 6
4699 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown99c0fb52009-03-31 14:39:38 +11004700 printk(KERN_ERR "raid5: %s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004701 mdname(mddev), mddev->new_layout);
4702 return ERR_PTR(-EIO);
4703 }
4704 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4705 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
4706 mdname(mddev), mddev->raid_disks);
4707 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Andre Noll664e7c42009-06-18 08:45:27 +10004710 if (!mddev->new_chunk_sectors ||
4711 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4712 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown91adb562009-03-31 14:39:39 +11004713 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
Andre Noll664e7c42009-06-18 08:45:27 +10004714 mddev->new_chunk_sectors << 9, mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004715 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004716 }
4717
NeilBrown91adb562009-03-31 14:39:39 +11004718 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4719 if (conf == NULL)
4720 goto abort;
4721
4722 conf->raid_disks = mddev->raid_disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004723 conf->scribble_len = scribble_len(conf->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004724 if (mddev->reshape_position == MaxSector)
4725 conf->previous_raid_disks = mddev->raid_disks;
4726 else
4727 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4728
4729 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
4730 GFP_KERNEL);
4731 if (!conf->disks)
4732 goto abort;
4733
4734 conf->mddev = mddev;
4735
4736 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4737 goto abort;
4738
Dan Williams36d1c642009-07-14 11:48:22 -07004739 conf->level = mddev->new_level;
4740 if (raid5_alloc_percpu(conf) != 0)
4741 goto abort;
4742
NeilBrown91adb562009-03-31 14:39:39 +11004743 spin_lock_init(&conf->device_lock);
4744 init_waitqueue_head(&conf->wait_for_stripe);
4745 init_waitqueue_head(&conf->wait_for_overlap);
4746 INIT_LIST_HEAD(&conf->handle_list);
4747 INIT_LIST_HEAD(&conf->hold_list);
4748 INIT_LIST_HEAD(&conf->delayed_list);
4749 INIT_LIST_HEAD(&conf->bitmap_list);
4750 INIT_LIST_HEAD(&conf->inactive_list);
4751 atomic_set(&conf->active_stripes, 0);
4752 atomic_set(&conf->preread_active_stripes, 0);
4753 atomic_set(&conf->active_aligned_reads, 0);
4754 conf->bypass_threshold = BYPASS_THRESHOLD;
4755
4756 pr_debug("raid5: run(%s) called.\n", mdname(mddev));
4757
4758 list_for_each_entry(rdev, &mddev->disks, same_set) {
4759 raid_disk = rdev->raid_disk;
4760 if (raid_disk >= conf->raid_disks
4761 || raid_disk < 0)
4762 continue;
4763 disk = conf->disks + raid_disk;
4764
4765 disk->rdev = rdev;
4766
4767 if (test_bit(In_sync, &rdev->flags)) {
4768 char b[BDEVNAME_SIZE];
4769 printk(KERN_INFO "raid5: device %s operational as raid"
4770 " disk %d\n", bdevname(rdev->bdev,b),
4771 raid_disk);
4772 } else
4773 /* Cannot rely on bitmap to complete recovery */
4774 conf->fullsync = 1;
4775 }
4776
Andre Noll09c9e5f2009-06-18 08:45:55 +10004777 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004778 conf->level = mddev->new_level;
4779 if (conf->level == 6)
4780 conf->max_degraded = 2;
4781 else
4782 conf->max_degraded = 1;
4783 conf->algorithm = mddev->new_layout;
4784 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004785 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004786 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004787 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004788 conf->prev_algo = mddev->layout;
4789 }
NeilBrown91adb562009-03-31 14:39:39 +11004790
4791 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4792 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4793 if (grow_stripes(conf, conf->max_nr_stripes)) {
4794 printk(KERN_ERR
4795 "raid5: couldn't allocate %dkB for buffers\n", memory);
4796 goto abort;
4797 } else
4798 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
4799 memory, mdname(mddev));
4800
4801 conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
4802 if (!conf->thread) {
4803 printk(KERN_ERR
4804 "raid5: couldn't allocate thread for %s\n",
4805 mdname(mddev));
4806 goto abort;
4807 }
4808
4809 return conf;
4810
4811 abort:
4812 if (conf) {
Dan Williamsa11034b2009-07-14 11:48:16 -07004813 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11004814 return ERR_PTR(-EIO);
4815 } else
4816 return ERR_PTR(-ENOMEM);
4817}
4818
4819static int run(mddev_t *mddev)
4820{
4821 raid5_conf_t *conf;
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004822 int working_disks = 0, chunk_size;
NeilBrown91adb562009-03-31 14:39:39 +11004823 mdk_rdev_t *rdev;
4824
Andre Noll8c6ac8682009-06-18 08:48:06 +10004825 if (mddev->recovery_cp != MaxSector)
4826 printk(KERN_NOTICE "raid5: %s is not clean"
4827 " -- starting background reconstruction\n",
4828 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08004829 if (mddev->reshape_position != MaxSector) {
4830 /* Check that we can continue the reshape.
4831 * Currently only disks can change, it must
4832 * increase, and we must be past the point where
4833 * a stripe over-writes itself
4834 */
4835 sector_t here_new, here_old;
4836 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11004837 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08004838
NeilBrown88ce4932009-03-31 15:24:23 +11004839 if (mddev->new_level != mddev->level) {
NeilBrownf4168852007-02-28 20:11:53 -08004840 printk(KERN_ERR "raid5: %s: unsupported reshape "
4841 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08004842 mdname(mddev));
4843 return -EINVAL;
4844 }
NeilBrownf6705572006-03-27 01:18:11 -08004845 old_disks = mddev->raid_disks - mddev->delta_disks;
4846 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08004847 * further up in new geometry must map after here in old
4848 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08004849 */
4850 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10004851 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004852 (mddev->raid_disks - max_degraded))) {
4853 printk(KERN_ERR "raid5: reshape_position not "
4854 "on a stripe boundary\n");
NeilBrownf6705572006-03-27 01:18:11 -08004855 return -EINVAL;
4856 }
4857 /* here_new is the stripe we will write to */
4858 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10004859 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08004860 (old_disks-max_degraded));
4861 /* here_old is the first stripe that we might need to read
4862 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10004863 if (mddev->delta_disks == 0) {
4864 /* We cannot be sure it is safe to start an in-place
4865 * reshape. It is only safe if user-space if monitoring
4866 * and taking constant backups.
4867 * mdadm always starts a situation like this in
4868 * readonly mode so it can take control before
4869 * allowing any writes. So just check for that.
4870 */
4871 if ((here_new * mddev->new_chunk_sectors !=
4872 here_old * mddev->chunk_sectors) ||
4873 mddev->ro == 0) {
4874 printk(KERN_ERR "raid5: in-place reshape must be started"
4875 " in read-only mode - aborting\n");
4876 return -EINVAL;
4877 }
4878 } else if (mddev->delta_disks < 0
4879 ? (here_new * mddev->new_chunk_sectors <=
4880 here_old * mddev->chunk_sectors)
4881 : (here_new * mddev->new_chunk_sectors >=
4882 here_old * mddev->chunk_sectors)) {
NeilBrownf6705572006-03-27 01:18:11 -08004883 /* Reading from the same stripe as writing to - bad */
NeilBrownf4168852007-02-28 20:11:53 -08004884 printk(KERN_ERR "raid5: reshape_position too early for "
4885 "auto-recovery - aborting.\n");
NeilBrownf6705572006-03-27 01:18:11 -08004886 return -EINVAL;
4887 }
4888 printk(KERN_INFO "raid5: reshape will continue\n");
4889 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08004890 } else {
NeilBrown91adb562009-03-31 14:39:39 +11004891 BUG_ON(mddev->level != mddev->new_level);
4892 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10004893 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11004894 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08004895 }
4896
NeilBrown245f46c2009-03-31 14:39:39 +11004897 if (mddev->private == NULL)
4898 conf = setup_conf(mddev);
4899 else
4900 conf = mddev->private;
4901
NeilBrown91adb562009-03-31 14:39:39 +11004902 if (IS_ERR(conf))
4903 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08004904
NeilBrown91adb562009-03-31 14:39:39 +11004905 mddev->thread = conf->thread;
4906 conf->thread = NULL;
4907 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07004910 * 0 for a fully functional array, 1 or 2 for a degraded array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 */
NeilBrown91adb562009-03-31 14:39:39 +11004912 list_for_each_entry(rdev, &mddev->disks, same_set)
4913 if (rdev->raid_disk >= 0 &&
4914 test_bit(In_sync, &rdev->flags))
4915 working_disks++;
4916
NeilBrown02c2de82006-10-03 01:15:47 -07004917 mddev->degraded = conf->raid_disks - working_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
NeilBrown16a53ec2006-06-26 00:27:38 -07004919 if (mddev->degraded > conf->max_degraded) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 printk(KERN_ERR "raid5: not enough operational devices for %s"
4921 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07004922 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 goto abort;
4924 }
4925
NeilBrown91adb562009-03-31 14:39:39 +11004926 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004927 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11004928 mddev->resync_max_sectors = mddev->dev_sectors;
4929
NeilBrown16a53ec2006-06-26 00:27:38 -07004930 if (mddev->degraded > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004932 if (mddev->ok_start_degraded)
4933 printk(KERN_WARNING
4934 "raid5: starting dirty degraded array: %s"
4935 "- data corruption possible.\n",
4936 mdname(mddev));
4937 else {
4938 printk(KERN_ERR
4939 "raid5: cannot start dirty degraded array for %s\n",
4940 mdname(mddev));
4941 goto abort;
4942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 }
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (mddev->degraded == 0)
4946 printk("raid5: raid level %d set %s active with %d out of %d"
NeilBrowne183eae2009-03-31 15:20:22 +11004947 " devices, algorithm %d\n", conf->level, mdname(mddev),
4948 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4949 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 else
4951 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
4952 " out of %d devices, algorithm %d\n", conf->level,
4953 mdname(mddev), mddev->raid_disks - mddev->degraded,
NeilBrowne183eae2009-03-31 15:20:22 +11004954 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
4956 print_raid5_conf(conf);
4957
NeilBrownfef9c612009-03-31 15:16:46 +11004958 if (conf->reshape_progress != MaxSector) {
NeilBrownf6705572006-03-27 01:18:11 -08004959 printk("...ok start reshape thread\n");
NeilBrownfef9c612009-03-31 15:16:46 +11004960 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08004961 atomic_set(&conf->reshape_stripes, 0);
4962 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4963 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4964 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4965 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4966 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4967 "%s_reshape");
NeilBrownf6705572006-03-27 01:18:11 -08004968 }
4969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 /* read-ahead size must cover two whole stripes, which is
NeilBrown16a53ec2006-06-26 00:27:38 -07004971 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 */
4973 {
NeilBrown16a53ec2006-06-26 00:27:38 -07004974 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4975 int stripe = data_disks *
Andre Noll9d8f0362009-06-18 08:45:01 +10004976 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4978 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4979 }
4980
4981 /* Ok, everything is just fine now */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004982 if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4983 printk(KERN_WARNING
4984 "raid5: failed to create sysfs attributes for %s\n",
4985 mdname(mddev));
NeilBrown7a5febe2005-05-16 21:53:16 -07004986
NeilBrown91adb562009-03-31 14:39:39 +11004987 mddev->queue->queue_lock = &conf->device_lock;
4988
NeilBrown7a5febe2005-05-16 21:53:16 -07004989 mddev->queue->unplug_fn = raid5_unplug_device;
NeilBrownf022b2f2006-10-03 01:15:56 -07004990 mddev->queue->backing_dev_info.congested_data = mddev;
NeilBrown041ae522007-03-26 21:32:14 -08004991 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrownf022b2f2006-10-03 01:15:56 -07004992
Dan Williams1f403622009-03-31 14:59:03 +11004993 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
NeilBrown7a5febe2005-05-16 21:53:16 -07004994
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004995 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
Martin K. Petersen8f6c2e42009-07-01 11:13:45 +10004996 chunk_size = mddev->chunk_sectors << 9;
4997 blk_queue_io_min(mddev->queue, chunk_size);
4998 blk_queue_io_opt(mddev->queue, chunk_size *
4999 (conf->raid_disks - conf->max_degraded));
5000
5001 list_for_each_entry(rdev, &mddev->disks, same_set)
5002 disk_stack_limits(mddev->gendisk, rdev->bdev,
5003 rdev->data_offset << 9);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08005004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 return 0;
5006abort:
NeilBrowne0cf8f02009-03-31 14:39:39 +11005007 md_unregister_thread(mddev->thread);
NeilBrown91adb562009-03-31 14:39:39 +11005008 mddev->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 if (conf) {
5010 print_raid5_conf(conf);
Dan Williamsa11034b2009-07-14 11:48:16 -07005011 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
5013 mddev->private = NULL;
5014 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
5015 return -EIO;
5016}
5017
5018
5019
NeilBrown3f294f42005-11-08 21:39:25 -08005020static int stop(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
5022 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5023
5024 md_unregister_thread(mddev->thread);
5025 mddev->thread = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08005026 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
NeilBrown007583c2005-11-08 21:39:30 -08005028 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
Dan Williamsa11034b2009-07-14 11:48:16 -07005029 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 mddev->private = NULL;
5031 return 0;
5032}
5033
Dan Williams45b42332007-07-09 11:56:43 -07005034#ifdef DEBUG
NeilBrownd710e132008-10-13 11:55:12 +11005035static void print_sh(struct seq_file *seq, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036{
5037 int i;
5038
NeilBrown16a53ec2006-06-26 00:27:38 -07005039 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
5040 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
5041 seq_printf(seq, "sh %llu, count %d.\n",
5042 (unsigned long long)sh->sector, atomic_read(&sh->count));
5043 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005044 for (i = 0; i < sh->disks; i++) {
NeilBrown16a53ec2006-06-26 00:27:38 -07005045 seq_printf(seq, "(cache%d: %p %ld) ",
5046 i, sh->dev[i].page, sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005048 seq_printf(seq, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
NeilBrownd710e132008-10-13 11:55:12 +11005051static void printall(struct seq_file *seq, raid5_conf_t *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052{
5053 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -08005054 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 int i;
5056
5057 spin_lock_irq(&conf->device_lock);
5058 for (i = 0; i < NR_HASH; i++) {
NeilBrownfccddba2006-01-06 00:20:33 -08005059 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 if (sh->raid_conf != conf)
5061 continue;
NeilBrown16a53ec2006-06-26 00:27:38 -07005062 print_sh(seq, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 }
5064 }
5065 spin_unlock_irq(&conf->device_lock);
5066}
5067#endif
5068
NeilBrownd710e132008-10-13 11:55:12 +11005069static void status(struct seq_file *seq, mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070{
5071 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
5072 int i;
5073
Andre Noll9d8f0362009-06-18 08:45:01 +10005074 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5075 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005076 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 for (i = 0; i < conf->raid_disks; i++)
5078 seq_printf (seq, "%s",
5079 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005080 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 seq_printf (seq, "]");
Dan Williams45b42332007-07-09 11:56:43 -07005082#ifdef DEBUG
NeilBrown16a53ec2006-06-26 00:27:38 -07005083 seq_printf (seq, "\n");
5084 printall(seq, conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085#endif
5086}
5087
5088static void print_raid5_conf (raid5_conf_t *conf)
5089{
5090 int i;
5091 struct disk_info *tmp;
5092
5093 printk("RAID5 conf printout:\n");
5094 if (!conf) {
5095 printk("(conf==NULL)\n");
5096 return;
5097 }
NeilBrown02c2de82006-10-03 01:15:47 -07005098 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
5099 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
5101 for (i = 0; i < conf->raid_disks; i++) {
5102 char b[BDEVNAME_SIZE];
5103 tmp = conf->disks + i;
5104 if (tmp->rdev)
5105 printk(" disk %d, o:%d, dev:%s\n",
NeilBrownb2d444d2005-11-08 21:39:31 -08005106 i, !test_bit(Faulty, &tmp->rdev->flags),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 bdevname(tmp->rdev->bdev,b));
5108 }
5109}
5110
5111static int raid5_spare_active(mddev_t *mddev)
5112{
5113 int i;
5114 raid5_conf_t *conf = mddev->private;
5115 struct disk_info *tmp;
5116
5117 for (i = 0; i < conf->raid_disks; i++) {
5118 tmp = conf->disks + i;
5119 if (tmp->rdev
NeilBrownb2d444d2005-11-08 21:39:31 -08005120 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005121 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5122 unsigned long flags;
5123 spin_lock_irqsave(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 mddev->degraded--;
NeilBrownc04be0a2006-10-03 01:15:53 -07005125 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 }
5127 }
5128 print_raid5_conf(conf);
5129 return 0;
5130}
5131
5132static int raid5_remove_disk(mddev_t *mddev, int number)
5133{
5134 raid5_conf_t *conf = mddev->private;
5135 int err = 0;
5136 mdk_rdev_t *rdev;
5137 struct disk_info *p = conf->disks + number;
5138
5139 print_raid5_conf(conf);
5140 rdev = p->rdev;
5141 if (rdev) {
NeilBrownec32a2b2009-03-31 15:17:38 +11005142 if (number >= conf->raid_disks &&
5143 conf->reshape_progress == MaxSector)
5144 clear_bit(In_sync, &rdev->flags);
5145
NeilBrownb2d444d2005-11-08 21:39:31 -08005146 if (test_bit(In_sync, &rdev->flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 atomic_read(&rdev->nr_pending)) {
5148 err = -EBUSY;
5149 goto abort;
5150 }
NeilBrowndfc70642008-05-23 13:04:39 -07005151 /* Only remove non-faulty devices if recovery
5152 * isn't possible.
5153 */
5154 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrownec32a2b2009-03-31 15:17:38 +11005155 mddev->degraded <= conf->max_degraded &&
5156 number < conf->raid_disks) {
NeilBrowndfc70642008-05-23 13:04:39 -07005157 err = -EBUSY;
5158 goto abort;
5159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 p->rdev = NULL;
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005161 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (atomic_read(&rdev->nr_pending)) {
5163 /* lost the race, try later */
5164 err = -EBUSY;
5165 p->rdev = rdev;
5166 }
5167 }
5168abort:
5169
5170 print_raid5_conf(conf);
5171 return err;
5172}
5173
5174static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5175{
5176 raid5_conf_t *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005177 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 int disk;
5179 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005180 int first = 0;
5181 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182
NeilBrown16a53ec2006-06-26 00:27:38 -07005183 if (mddev->degraded > conf->max_degraded)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005185 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Neil Brown6c2fce22008-06-28 08:31:31 +10005187 if (rdev->raid_disk >= 0)
5188 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
5190 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005191 * find the disk ... but prefer rdev->saved_raid_disk
5192 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005194 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005195 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005196 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5197 disk = rdev->saved_raid_disk;
5198 else
Neil Brown6c2fce22008-06-28 08:31:31 +10005199 disk = first;
5200 for ( ; disk <= last ; disk++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 if ((p=conf->disks + disk)->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005202 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005204 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005205 if (rdev->saved_raid_disk != disk)
5206 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005207 rcu_assign_pointer(p->rdev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 break;
5209 }
5210 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005211 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212}
5213
5214static int raid5_resize(mddev_t *mddev, sector_t sectors)
5215{
5216 /* no resync is happening, and there is enough space
5217 * on all devices, so we can resize.
5218 * We need to make sure resync covers any new space.
5219 * If the array is shrinking we should possibly wait until
5220 * any io in the removed space completes, but it hardly seems
5221 * worth it.
5222 */
Andre Noll9d8f0362009-06-18 08:45:01 +10005223 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Dan Williams1f403622009-03-31 14:59:03 +11005224 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5225 mddev->raid_disks));
Dan Williamsb522adc2009-03-31 15:00:31 +11005226 if (mddev->array_sectors >
5227 raid5_size(mddev, sectors, mddev->raid_disks))
5228 return -EINVAL;
Andre Nollf233ea52008-07-21 17:05:22 +10005229 set_capacity(mddev->gendisk, mddev->array_sectors);
Linus Torvalds44ce62942007-05-09 18:51:36 -07005230 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005231 revalidate_disk(mddev->gendisk);
Andre Noll58c0fed2009-03-31 14:33:13 +11005232 if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
5233 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5235 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005236 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005237 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 return 0;
5239}
5240
NeilBrown01ee22b2009-06-18 08:47:20 +10005241static int check_stripe_cache(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005242{
NeilBrown01ee22b2009-06-18 08:47:20 +10005243 /* Can only proceed if there are plenty of stripe_heads.
5244 * We need a minimum of one full stripe,, and for sensible progress
5245 * it is best to have about 4 times that.
5246 * If we require 4 times, then the default 256 4K stripe_heads will
5247 * allow for chunk sizes up to 256K, which is probably OK.
5248 * If the chunk size is greater, user-space should request more
5249 * stripe_heads first.
5250 */
5251 raid5_conf_t *conf = mddev->private;
5252 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5253 > conf->max_nr_stripes ||
5254 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5255 > conf->max_nr_stripes) {
5256 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
5257 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5258 / STRIPE_SIZE)*4);
5259 return 0;
5260 }
5261 return 1;
5262}
5263
NeilBrown50ac1682009-06-18 08:47:55 +10005264static int check_reshape(mddev_t *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005265{
NeilBrown070ec552009-06-16 16:54:21 +10005266 raid5_conf_t *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005267
NeilBrown88ce4932009-03-31 15:24:23 +11005268 if (mddev->delta_disks == 0 &&
5269 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005270 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005271 return 0; /* nothing to do */
NeilBrowndba034e2008-08-05 15:54:13 +10005272 if (mddev->bitmap)
5273 /* Cannot grow a bitmap yet */
5274 return -EBUSY;
NeilBrownec32a2b2009-03-31 15:17:38 +11005275 if (mddev->degraded > conf->max_degraded)
5276 return -EINVAL;
5277 if (mddev->delta_disks < 0) {
5278 /* We might be able to shrink, but the devices must
5279 * be made bigger first.
5280 * For raid6, 4 is the minimum size.
5281 * Otherwise 2 is the minimum
5282 */
5283 int min = 2;
5284 if (mddev->level == 6)
5285 min = 4;
5286 if (mddev->raid_disks + mddev->delta_disks < min)
5287 return -EINVAL;
5288 }
NeilBrown29269552006-03-27 01:18:10 -08005289
NeilBrown01ee22b2009-06-18 08:47:20 +10005290 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005291 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005292
NeilBrownec32a2b2009-03-31 15:17:38 +11005293 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005294}
5295
5296static int raid5_start_reshape(mddev_t *mddev)
5297{
NeilBrown070ec552009-06-16 16:54:21 +10005298 raid5_conf_t *conf = mddev->private;
NeilBrown63c70c42006-03-27 01:18:13 -08005299 mdk_rdev_t *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005300 int spares = 0;
5301 int added_devices = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005302 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005303
NeilBrownf4168852007-02-28 20:11:53 -08005304 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005305 return -EBUSY;
5306
NeilBrown01ee22b2009-06-18 08:47:20 +10005307 if (!check_stripe_cache(mddev))
5308 return -ENOSPC;
5309
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005310 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005311 if (rdev->raid_disk < 0 &&
5312 !test_bit(Faulty, &rdev->flags))
5313 spares++;
NeilBrown63c70c42006-03-27 01:18:13 -08005314
NeilBrownf4168852007-02-28 20:11:53 -08005315 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005316 /* Not enough devices even to make a degraded array
5317 * of that size
5318 */
5319 return -EINVAL;
5320
NeilBrownec32a2b2009-03-31 15:17:38 +11005321 /* Refuse to reduce size of the array. Any reductions in
5322 * array size must be through explicit setting of array_size
5323 * attribute.
5324 */
5325 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5326 < mddev->array_sectors) {
5327 printk(KERN_ERR "md: %s: array size must be reduced "
5328 "before number of disks\n", mdname(mddev));
5329 return -EINVAL;
5330 }
5331
NeilBrownf6705572006-03-27 01:18:11 -08005332 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005333 spin_lock_irq(&conf->device_lock);
5334 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005335 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005336 conf->prev_chunk_sectors = conf->chunk_sectors;
5337 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005338 conf->prev_algo = conf->algorithm;
5339 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005340 if (mddev->delta_disks < 0)
5341 conf->reshape_progress = raid5_size(mddev, 0, 0);
5342 else
5343 conf->reshape_progress = 0;
5344 conf->reshape_safe = conf->reshape_progress;
NeilBrown86b42c72009-03-31 15:19:03 +11005345 conf->generation++;
NeilBrown29269552006-03-27 01:18:10 -08005346 spin_unlock_irq(&conf->device_lock);
5347
5348 /* Add some new drives, as many as will fit.
5349 * We know there are enough to make the newly sized array work.
5350 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005351 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown29269552006-03-27 01:18:10 -08005352 if (rdev->raid_disk < 0 &&
5353 !test_bit(Faulty, &rdev->flags)) {
Neil Brown199050e2008-06-28 08:31:33 +10005354 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown29269552006-03-27 01:18:10 -08005355 char nm[20];
5356 set_bit(In_sync, &rdev->flags);
NeilBrown29269552006-03-27 01:18:10 -08005357 added_devices++;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005358 rdev->recovery_offset = 0;
NeilBrown29269552006-03-27 01:18:10 -08005359 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005360 if (sysfs_create_link(&mddev->kobj,
5361 &rdev->kobj, nm))
5362 printk(KERN_WARNING
5363 "raid5: failed to create "
5364 " link %s for %s\n",
5365 nm, mdname(mddev));
NeilBrown29269552006-03-27 01:18:10 -08005366 } else
5367 break;
5368 }
5369
NeilBrownec32a2b2009-03-31 15:17:38 +11005370 if (mddev->delta_disks > 0) {
5371 spin_lock_irqsave(&conf->device_lock, flags);
5372 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
5373 - added_devices;
5374 spin_unlock_irqrestore(&conf->device_lock, flags);
5375 }
NeilBrown63c70c42006-03-27 01:18:13 -08005376 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005377 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005378 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005379
NeilBrown29269552006-03-27 01:18:10 -08005380 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5381 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5382 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5383 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5384 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5385 "%s_reshape");
5386 if (!mddev->sync_thread) {
5387 mddev->recovery = 0;
5388 spin_lock_irq(&conf->device_lock);
5389 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005390 conf->reshape_progress = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005391 spin_unlock_irq(&conf->device_lock);
5392 return -EAGAIN;
5393 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005394 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005395 md_wakeup_thread(mddev->sync_thread);
5396 md_new_event(mddev);
5397 return 0;
5398}
NeilBrown29269552006-03-27 01:18:10 -08005399
NeilBrownec32a2b2009-03-31 15:17:38 +11005400/* This is called from the reshape thread and should make any
5401 * changes needed in 'conf'
5402 */
NeilBrown29269552006-03-27 01:18:10 -08005403static void end_reshape(raid5_conf_t *conf)
5404{
NeilBrown29269552006-03-27 01:18:10 -08005405
NeilBrownf6705572006-03-27 01:18:11 -08005406 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07005407
NeilBrownf6705572006-03-27 01:18:11 -08005408 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005409 conf->previous_raid_disks = conf->raid_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005410 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005411 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005412 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005413
5414 /* read-ahead size must cover two whole stripes, which is
5415 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5416 */
5417 {
NeilBrowncea9c222009-03-31 15:15:05 +11005418 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005419 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005420 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005421 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5422 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5423 }
NeilBrown29269552006-03-27 01:18:10 -08005424 }
NeilBrown29269552006-03-27 01:18:10 -08005425}
5426
NeilBrownec32a2b2009-03-31 15:17:38 +11005427/* This is called from the raid5d thread with mddev_lock held.
5428 * It makes config changes to the device.
5429 */
NeilBrowncea9c222009-03-31 15:15:05 +11005430static void raid5_finish_reshape(mddev_t *mddev)
5431{
NeilBrown070ec552009-06-16 16:54:21 +10005432 raid5_conf_t *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005433
5434 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5435
NeilBrownec32a2b2009-03-31 15:17:38 +11005436 if (mddev->delta_disks > 0) {
5437 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5438 set_capacity(mddev->gendisk, mddev->array_sectors);
5439 mddev->changed = 1;
NeilBrown449aad32009-08-03 10:59:58 +10005440 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005441 } else {
5442 int d;
NeilBrownec32a2b2009-03-31 15:17:38 +11005443 mddev->degraded = conf->raid_disks;
5444 for (d = 0; d < conf->raid_disks ; d++)
5445 if (conf->disks[d].rdev &&
5446 test_bit(In_sync,
5447 &conf->disks[d].rdev->flags))
5448 mddev->degraded--;
5449 for (d = conf->raid_disks ;
5450 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005451 d++) {
5452 mdk_rdev_t *rdev = conf->disks[d].rdev;
5453 if (rdev && raid5_remove_disk(mddev, d) == 0) {
5454 char nm[20];
5455 sprintf(nm, "rd%d", rdev->raid_disk);
5456 sysfs_remove_link(&mddev->kobj, nm);
5457 rdev->raid_disk = -1;
5458 }
5459 }
NeilBrowncea9c222009-03-31 15:15:05 +11005460 }
NeilBrown88ce4932009-03-31 15:24:23 +11005461 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005462 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005463 mddev->reshape_position = MaxSector;
5464 mddev->delta_disks = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005465 }
5466}
5467
NeilBrown72626682005-09-09 16:23:54 -07005468static void raid5_quiesce(mddev_t *mddev, int state)
5469{
NeilBrown070ec552009-06-16 16:54:21 +10005470 raid5_conf_t *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005471
5472 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005473 case 2: /* resume for a suspend */
5474 wake_up(&conf->wait_for_overlap);
5475 break;
5476
NeilBrown72626682005-09-09 16:23:54 -07005477 case 1: /* stop all writes */
5478 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005479 /* '2' tells resync/reshape to pause so that all
5480 * active stripes can drain
5481 */
5482 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005483 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005484 atomic_read(&conf->active_stripes) == 0 &&
5485 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005486 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005487 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005488 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005489 /* allow reshape to continue */
5490 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005491 break;
5492
5493 case 0: /* re-enable writes */
5494 spin_lock_irq(&conf->device_lock);
5495 conf->quiesce = 0;
5496 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005497 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005498 spin_unlock_irq(&conf->device_lock);
5499 break;
5500 }
NeilBrown72626682005-09-09 16:23:54 -07005501}
NeilBrownb15c2e52006-01-06 00:20:16 -08005502
NeilBrownd562b0c2009-03-31 14:39:39 +11005503
5504static void *raid5_takeover_raid1(mddev_t *mddev)
5505{
5506 int chunksect;
5507
5508 if (mddev->raid_disks != 2 ||
5509 mddev->degraded > 1)
5510 return ERR_PTR(-EINVAL);
5511
5512 /* Should check if there are write-behind devices? */
5513
5514 chunksect = 64*2; /* 64K by default */
5515
5516 /* The array must be an exact multiple of chunksize */
5517 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5518 chunksect >>= 1;
5519
5520 if ((chunksect<<9) < STRIPE_SIZE)
5521 /* array size does not allow a suitable chunk size */
5522 return ERR_PTR(-EINVAL);
5523
5524 mddev->new_level = 5;
5525 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005526 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005527
5528 return setup_conf(mddev);
5529}
5530
NeilBrownfc9739c2009-03-31 14:57:20 +11005531static void *raid5_takeover_raid6(mddev_t *mddev)
5532{
5533 int new_layout;
5534
5535 switch (mddev->layout) {
5536 case ALGORITHM_LEFT_ASYMMETRIC_6:
5537 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5538 break;
5539 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5540 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5541 break;
5542 case ALGORITHM_LEFT_SYMMETRIC_6:
5543 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5544 break;
5545 case ALGORITHM_RIGHT_SYMMETRIC_6:
5546 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5547 break;
5548 case ALGORITHM_PARITY_0_6:
5549 new_layout = ALGORITHM_PARITY_0;
5550 break;
5551 case ALGORITHM_PARITY_N:
5552 new_layout = ALGORITHM_PARITY_N;
5553 break;
5554 default:
5555 return ERR_PTR(-EINVAL);
5556 }
5557 mddev->new_level = 5;
5558 mddev->new_layout = new_layout;
5559 mddev->delta_disks = -1;
5560 mddev->raid_disks -= 1;
5561 return setup_conf(mddev);
5562}
5563
NeilBrownd562b0c2009-03-31 14:39:39 +11005564
NeilBrown50ac1682009-06-18 08:47:55 +10005565static int raid5_check_reshape(mddev_t *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005566{
NeilBrown88ce4932009-03-31 15:24:23 +11005567 /* For a 2-drive array, the layout and chunk size can be changed
5568 * immediately as not restriping is needed.
5569 * For larger arrays we record the new value - after validation
5570 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005571 */
NeilBrown070ec552009-06-16 16:54:21 +10005572 raid5_conf_t *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005573 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005574
NeilBrown597a7112009-06-18 08:47:42 +10005575 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005576 return -EINVAL;
5577 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005578 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005579 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005580 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005581 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005582 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005583 /* not factor of array size */
5584 return -EINVAL;
5585 }
5586
5587 /* They look valid */
5588
NeilBrown88ce4932009-03-31 15:24:23 +11005589 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005590 /* can make the change immediately */
5591 if (mddev->new_layout >= 0) {
5592 conf->algorithm = mddev->new_layout;
5593 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005594 }
5595 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005596 conf->chunk_sectors = new_chunk ;
5597 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005598 }
5599 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5600 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005601 }
NeilBrown50ac1682009-06-18 08:47:55 +10005602 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005603}
5604
NeilBrown50ac1682009-06-18 08:47:55 +10005605static int raid6_check_reshape(mddev_t *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005606{
NeilBrown597a7112009-06-18 08:47:42 +10005607 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005608
NeilBrown597a7112009-06-18 08:47:42 +10005609 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005610 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005611 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005612 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005613 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005614 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005615 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005616 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005617 /* not factor of array size */
5618 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005619 }
NeilBrown88ce4932009-03-31 15:24:23 +11005620
5621 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10005622 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11005623}
5624
NeilBrownd562b0c2009-03-31 14:39:39 +11005625static void *raid5_takeover(mddev_t *mddev)
5626{
5627 /* raid5 can take over:
5628 * raid0 - if all devices are the same - make it a raid4 layout
5629 * raid1 - if there are two drives. We need to know the chunk size
5630 * raid4 - trivial - just use a raid4 layout.
5631 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11005632 */
5633
5634 if (mddev->level == 1)
5635 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11005636 if (mddev->level == 4) {
5637 mddev->new_layout = ALGORITHM_PARITY_N;
5638 mddev->new_level = 5;
5639 return setup_conf(mddev);
5640 }
NeilBrownfc9739c2009-03-31 14:57:20 +11005641 if (mddev->level == 6)
5642 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11005643
5644 return ERR_PTR(-EINVAL);
5645}
5646
5647
NeilBrown245f46c2009-03-31 14:39:39 +11005648static struct mdk_personality raid5_personality;
5649
5650static void *raid6_takeover(mddev_t *mddev)
5651{
5652 /* Currently can only take over a raid5. We map the
5653 * personality to an equivalent raid6 personality
5654 * with the Q block at the end.
5655 */
5656 int new_layout;
5657
5658 if (mddev->pers != &raid5_personality)
5659 return ERR_PTR(-EINVAL);
5660 if (mddev->degraded > 1)
5661 return ERR_PTR(-EINVAL);
5662 if (mddev->raid_disks > 253)
5663 return ERR_PTR(-EINVAL);
5664 if (mddev->raid_disks < 3)
5665 return ERR_PTR(-EINVAL);
5666
5667 switch (mddev->layout) {
5668 case ALGORITHM_LEFT_ASYMMETRIC:
5669 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5670 break;
5671 case ALGORITHM_RIGHT_ASYMMETRIC:
5672 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5673 break;
5674 case ALGORITHM_LEFT_SYMMETRIC:
5675 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5676 break;
5677 case ALGORITHM_RIGHT_SYMMETRIC:
5678 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5679 break;
5680 case ALGORITHM_PARITY_0:
5681 new_layout = ALGORITHM_PARITY_0_6;
5682 break;
5683 case ALGORITHM_PARITY_N:
5684 new_layout = ALGORITHM_PARITY_N;
5685 break;
5686 default:
5687 return ERR_PTR(-EINVAL);
5688 }
5689 mddev->new_level = 6;
5690 mddev->new_layout = new_layout;
5691 mddev->delta_disks = 1;
5692 mddev->raid_disks += 1;
5693 return setup_conf(mddev);
5694}
5695
5696
NeilBrown16a53ec2006-06-26 00:27:38 -07005697static struct mdk_personality raid6_personality =
5698{
5699 .name = "raid6",
5700 .level = 6,
5701 .owner = THIS_MODULE,
5702 .make_request = make_request,
5703 .run = run,
5704 .stop = stop,
5705 .status = status,
5706 .error_handler = error,
5707 .hot_add_disk = raid5_add_disk,
5708 .hot_remove_disk= raid5_remove_disk,
5709 .spare_active = raid5_spare_active,
5710 .sync_request = sync_request,
5711 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005712 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10005713 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08005714 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005715 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07005716 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11005717 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07005718};
NeilBrown2604b702006-01-06 00:20:36 -08005719static struct mdk_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720{
5721 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08005722 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 .owner = THIS_MODULE,
5724 .make_request = make_request,
5725 .run = run,
5726 .stop = stop,
5727 .status = status,
5728 .error_handler = error,
5729 .hot_add_disk = raid5_add_disk,
5730 .hot_remove_disk= raid5_remove_disk,
5731 .spare_active = raid5_spare_active,
5732 .sync_request = sync_request,
5733 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005734 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08005735 .check_reshape = raid5_check_reshape,
5736 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005737 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07005738 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11005739 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740};
5741
NeilBrown2604b702006-01-06 00:20:36 -08005742static struct mdk_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743{
NeilBrown2604b702006-01-06 00:20:36 -08005744 .name = "raid4",
5745 .level = 4,
5746 .owner = THIS_MODULE,
5747 .make_request = make_request,
5748 .run = run,
5749 .stop = stop,
5750 .status = status,
5751 .error_handler = error,
5752 .hot_add_disk = raid5_add_disk,
5753 .hot_remove_disk= raid5_remove_disk,
5754 .spare_active = raid5_spare_active,
5755 .sync_request = sync_request,
5756 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07005757 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08005758 .check_reshape = raid5_check_reshape,
5759 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11005760 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08005761 .quiesce = raid5_quiesce,
5762};
5763
5764static int __init raid5_init(void)
5765{
NeilBrown16a53ec2006-06-26 00:27:38 -07005766 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005767 register_md_personality(&raid5_personality);
5768 register_md_personality(&raid4_personality);
5769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770}
5771
NeilBrown2604b702006-01-06 00:20:36 -08005772static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773{
NeilBrown16a53ec2006-06-26 00:27:38 -07005774 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08005775 unregister_md_personality(&raid5_personality);
5776 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
5778
5779module_init(raid5_init);
5780module_exit(raid5_exit);
5781MODULE_LICENSE("GPL");
5782MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08005783MODULE_ALIAS("md-raid5");
5784MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08005785MODULE_ALIAS("md-level-5");
5786MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07005787MODULE_ALIAS("md-personality-8"); /* RAID6 */
5788MODULE_ALIAS("md-raid6");
5789MODULE_ALIAS("md-level-6");
5790
5791/* This used to be two separate modules, they were: */
5792MODULE_ALIAS("raid5");
5793MODULE_ALIAS("raid6");