blob: d79ecd9032694a85bebcfe7e354c1b1bff146614 [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.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * 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
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * 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>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080056#include <linux/nodemask.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110057#include <trace/events/block.h>
58
NeilBrown43b2e5d2009-03-31 14:33:13 +110059#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110060#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110061#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110062#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070063
Shaohua Li851c30c2013-08-28 14:30:16 +080064#define cpu_to_group(cpu) cpu_to_node(cpu)
65#define ANY_GROUP NUMA_NO_NODE
66
67static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * Stripe cache
70 */
71
72#define NR_STRIPES 256
73#define STRIPE_SIZE PAGE_SIZE
74#define STRIPE_SHIFT (PAGE_SHIFT - 9)
75#define STRIPE_SECTORS (STRIPE_SIZE>>9)
76#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070077#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080078#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define HASH_MASK (NR_HASH - 1)
80
NeilBrownd1688a62011-10-11 16:49:52 +110081static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110082{
83 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
84 return &conf->stripe_hashtbl[hash];
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* bio's attached to a stripe+device for I/O are linked together in bi_sector
88 * order without overlap. There may be several bio's per stripe+device, and
89 * a bio could span several devices.
90 * When walking this list for a particular stripe+device, we must never proceed
91 * beyond a bio that extends past this device, as the next bio might no longer
92 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110093 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * of the current stripe+device
95 */
NeilBrowndb298e12011-10-07 14:23:00 +110096static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
97{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -080098 int sectors = bio_sectors(bio);
NeilBrowndb298e12011-10-07 14:23:00 +110099 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
100 return bio->bi_next;
101 else
102 return NULL;
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Jens Axboe960e7392008-08-15 10:41:18 +0200105/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200106 * We maintain a biased count of active stripes in the bottom 16 bits of
107 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200108 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000109static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200110{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000111 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
112 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200113}
114
Shaohua Lie7836bd62012-07-19 16:01:31 +1000115static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200116{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000117 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
118 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200119}
120
Shaohua Lie7836bd62012-07-19 16:01:31 +1000121static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200122{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200125}
126
Shaohua Lie7836bd62012-07-19 16:01:31 +1000127static inline void raid5_set_bi_processed_stripes(struct bio *bio,
128 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200129{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000130 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
131 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200132
Shaohua Lie7836bd62012-07-19 16:01:31 +1000133 do {
134 old = atomic_read(segments);
135 new = (old & 0xffff) | (cnt << 16);
136 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200137}
138
Shaohua Lie7836bd62012-07-19 16:01:31 +1000139static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200140{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000141 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
142 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200143}
144
NeilBrownd0dabf72009-03-31 14:39:38 +1100145/* Find first data disk in a raid6 stripe */
146static inline int raid6_d0(struct stripe_head *sh)
147{
NeilBrown67cc2b82009-03-31 14:39:38 +1100148 if (sh->ddf_layout)
149 /* ddf always start from first device */
150 return 0;
151 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100152 if (sh->qd_idx == sh->disks - 1)
153 return 0;
154 else
155 return sh->qd_idx + 1;
156}
NeilBrown16a53ec2006-06-26 00:27:38 -0700157static inline int raid6_next_disk(int disk, int raid_disks)
158{
159 disk++;
160 return (disk < raid_disks) ? disk : 0;
161}
Dan Williamsa4456852007-07-09 11:56:43 -0700162
NeilBrownd0dabf72009-03-31 14:39:38 +1100163/* When walking through the disks in a raid5, starting at raid6_d0,
164 * We need to map each disk to a 'slot', where the data disks are slot
165 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
166 * is raid_disks-1. This help does that mapping.
167 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100168static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
169 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100170{
Dan Williams66295422009-10-19 18:09:32 -0700171 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100172
NeilBrowne4424fe2009-10-16 16:27:34 +1100173 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700174 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100175 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100176 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100177 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100178 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100179 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700180 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100181 return slot;
182}
183
Dan Williamsa4456852007-07-09 11:56:43 -0700184static void return_io(struct bio *return_bi)
185{
186 struct bio *bi = return_bi;
187 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700188
189 return_bi = bi->bi_next;
190 bi->bi_next = NULL;
191 bi->bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700192 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
193 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000194 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700195 bi = return_bi;
196 }
197}
198
NeilBrownd1688a62011-10-11 16:49:52 +1100199static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Dan Williams600aa102008-06-28 08:32:05 +1000201static int stripe_operations_active(struct stripe_head *sh)
202{
203 return sh->check_state || sh->reconstruct_state ||
204 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
205 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
206}
207
Shaohua Li851c30c2013-08-28 14:30:16 +0800208static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
209{
210 struct r5conf *conf = sh->raid_conf;
211 struct r5worker_group *group;
212 int i, cpu = sh->cpu;
213
214 if (!cpu_online(cpu)) {
215 cpu = cpumask_any(cpu_online_mask);
216 sh->cpu = cpu;
217 }
218
219 if (list_empty(&sh->lru)) {
220 struct r5worker_group *group;
221 group = conf->worker_groups + cpu_to_group(cpu);
222 list_add_tail(&sh->lru, &group->handle_list);
223 }
224
225 if (conf->worker_cnt_per_group == 0) {
226 md_wakeup_thread(conf->mddev->thread);
227 return;
228 }
229
230 group = conf->worker_groups + cpu_to_group(sh->cpu);
231
232 for (i = 0; i < conf->worker_cnt_per_group; i++)
233 queue_work_on(sh->cpu, raid5_wq, &group->workers[i].work);
234}
235
Shaohua Li4eb788d2012-07-19 16:01:31 +1000236static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000238 BUG_ON(!list_empty(&sh->lru));
239 BUG_ON(atomic_read(&conf->active_stripes)==0);
240 if (test_bit(STRIPE_HANDLE, &sh->state)) {
241 if (test_bit(STRIPE_DELAYED, &sh->state) &&
242 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
243 list_add_tail(&sh->lru, &conf->delayed_list);
244 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
245 sh->bm_seq - conf->seq_write > 0)
246 list_add_tail(&sh->lru, &conf->bitmap_list);
247 else {
248 clear_bit(STRIPE_DELAYED, &sh->state);
249 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800250 if (conf->worker_cnt_per_group == 0) {
251 list_add_tail(&sh->lru, &conf->handle_list);
252 } else {
253 raid5_wakeup_stripe_thread(sh);
254 return;
255 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000256 }
257 md_wakeup_thread(conf->mddev->thread);
258 } else {
259 BUG_ON(stripe_operations_active(sh));
260 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
261 if (atomic_dec_return(&conf->preread_active_stripes)
262 < IO_THRESHOLD)
263 md_wakeup_thread(conf->mddev->thread);
264 atomic_dec(&conf->active_stripes);
265 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
266 list_add_tail(&sh->lru, &conf->inactive_list);
267 wake_up(&conf->wait_for_stripe);
268 if (conf->retry_read_aligned)
269 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 }
272}
NeilBrownd0dabf72009-03-31 14:39:38 +1100273
Shaohua Li4eb788d2012-07-19 16:01:31 +1000274static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
275{
276 if (atomic_dec_and_test(&sh->count))
277 do_release_stripe(conf, sh);
278}
279
Shaohua Lid265d9d2013-08-28 14:29:05 +0800280static struct llist_node *llist_reverse_order(struct llist_node *head)
281{
282 struct llist_node *new_head = NULL;
283
284 while (head) {
285 struct llist_node *tmp = head;
286 head = head->next;
287 tmp->next = new_head;
288 new_head = tmp;
289 }
290
291 return new_head;
292}
293
Shaohua Li773ca822013-08-27 17:50:39 +0800294/* should hold conf->device_lock already */
295static int release_stripe_list(struct r5conf *conf)
296{
297 struct stripe_head *sh;
298 int count = 0;
299 struct llist_node *head;
300
301 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800302 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800303 while (head) {
304 sh = llist_entry(head, struct stripe_head, release_list);
305 head = llist_next(head);
306 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
307 smp_mb();
308 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
309 /*
310 * Don't worry the bit is set here, because if the bit is set
311 * again, the count is always > 1. This is true for
312 * STRIPE_ON_UNPLUG_LIST bit too.
313 */
314 __release_stripe(conf, sh);
315 count++;
316 }
317
318 return count;
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static void release_stripe(struct stripe_head *sh)
322{
NeilBrownd1688a62011-10-11 16:49:52 +1100323 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned long flags;
Shaohua Li773ca822013-08-27 17:50:39 +0800325 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700326
Shaohua Li773ca822013-08-27 17:50:39 +0800327 if (test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
328 goto slow_path;
329 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
330 if (wakeup)
331 md_wakeup_thread(conf->mddev->thread);
332 return;
333slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000334 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800335 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000336 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
337 do_release_stripe(conf, sh);
338 spin_unlock(&conf->device_lock);
339 }
340 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
NeilBrownfccddba2006-01-06 00:20:33 -0800343static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Dan Williams45b42332007-07-09 11:56:43 -0700345 pr_debug("remove_hash(), stripe %llu\n",
346 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
NeilBrownfccddba2006-01-06 00:20:33 -0800348 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
NeilBrownd1688a62011-10-11 16:49:52 +1100351static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
NeilBrownfccddba2006-01-06 00:20:33 -0800353 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Dan Williams45b42332007-07-09 11:56:43 -0700355 pr_debug("insert_hash(), stripe %llu\n",
356 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
NeilBrownfccddba2006-01-06 00:20:33 -0800358 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361
362/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100363static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct stripe_head *sh = NULL;
366 struct list_head *first;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (list_empty(&conf->inactive_list))
369 goto out;
370 first = conf->inactive_list.next;
371 sh = list_entry(first, struct stripe_head, lru);
372 list_del_init(first);
373 remove_hash(sh);
374 atomic_inc(&conf->active_stripes);
375out:
376 return sh;
377}
378
NeilBrowne4e11e32010-06-16 16:45:16 +1000379static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct page *p;
382 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000383 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
NeilBrowne4e11e32010-06-16 16:45:16 +1000385 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 p = sh->dev[i].page;
387 if (!p)
388 continue;
389 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800390 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392}
393
NeilBrowne4e11e32010-06-16 16:45:16 +1000394static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000397 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
NeilBrowne4e11e32010-06-16 16:45:16 +1000399 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct page *page;
401
402 if (!(page = alloc_page(GFP_KERNEL))) {
403 return 1;
404 }
405 sh->dev[i].page = page;
406 }
407 return 0;
408}
409
NeilBrown784052e2009-03-31 15:19:07 +1100410static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100411static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100412 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
NeilBrownb5663ba2009-03-31 14:39:38 +1100414static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
NeilBrownd1688a62011-10-11 16:49:52 +1100416 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800417 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200419 BUG_ON(atomic_read(&sh->count) != 0);
420 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000421 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700422
Dan Williams45b42332007-07-09 11:56:43 -0700423 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 (unsigned long long)sh->sector);
425
426 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700427
NeilBrown86b42c72009-03-31 15:19:03 +1100428 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100429 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100431 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sh->state = 0;
433
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800434
435 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct r5dev *dev = &sh->dev[i];
437
Dan Williamsd84e0f12007-01-02 13:52:30 -0700438 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700440 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700442 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000444 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100447 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800450 sh->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
NeilBrownd1688a62011-10-11 16:49:52 +1100453static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100454 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct stripe_head *sh;
457
Dan Williams45b42332007-07-09 11:56:43 -0700458 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800459 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100460 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700462 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return NULL;
464}
465
NeilBrown674806d2010-06-16 17:17:53 +1000466/*
467 * Need to check if array has failed when deciding whether to:
468 * - start an array
469 * - remove non-faulty devices
470 * - add a spare
471 * - allow a reshape
472 * This determination is simple when no reshape is happening.
473 * However if there is a reshape, we need to carefully check
474 * both the before and after sections.
475 * This is because some failed devices may only affect one
476 * of the two sections, and some non-in_sync devices may
477 * be insync in the section most affected by failed devices.
478 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100479static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000480{
NeilBrown908f4fb2011-12-23 10:17:50 +1100481 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000482 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000483
484 rcu_read_lock();
485 degraded = 0;
486 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100487 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000488 if (rdev && test_bit(Faulty, &rdev->flags))
489 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000490 if (!rdev || test_bit(Faulty, &rdev->flags))
491 degraded++;
492 else if (test_bit(In_sync, &rdev->flags))
493 ;
494 else
495 /* not in-sync or faulty.
496 * If the reshape increases the number of devices,
497 * this is being recovered by the reshape, so
498 * this 'previous' section is not in_sync.
499 * If the number of devices is being reduced however,
500 * the device can only be part of the array if
501 * we are reverting a reshape, so this section will
502 * be in-sync.
503 */
504 if (conf->raid_disks >= conf->previous_raid_disks)
505 degraded++;
506 }
507 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100508 if (conf->raid_disks == conf->previous_raid_disks)
509 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000510 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100511 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000512 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100513 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000514 if (rdev && test_bit(Faulty, &rdev->flags))
515 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000516 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100517 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000518 else if (test_bit(In_sync, &rdev->flags))
519 ;
520 else
521 /* not in-sync or faulty.
522 * If reshape increases the number of devices, this
523 * section has already been recovered, else it
524 * almost certainly hasn't.
525 */
526 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100527 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000528 }
529 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100530 if (degraded2 > degraded)
531 return degraded2;
532 return degraded;
533}
534
535static int has_failed(struct r5conf *conf)
536{
537 int degraded;
538
539 if (conf->mddev->reshape_position == MaxSector)
540 return conf->mddev->degraded > conf->max_degraded;
541
542 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000543 if (degraded > conf->max_degraded)
544 return 1;
545 return 0;
546}
547
NeilBrownb5663ba2009-03-31 14:39:38 +1100548static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100549get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000550 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct stripe_head *sh;
553
Dan Williams45b42332007-07-09 11:56:43 -0700554 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 spin_lock_irq(&conf->device_lock);
557
558 do {
NeilBrown72626682005-09-09 16:23:54 -0700559 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000560 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100561 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100562 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (!sh) {
564 if (!conf->inactive_blocked)
565 sh = get_free_stripe(conf);
566 if (noblock && sh == NULL)
567 break;
568 if (!sh) {
569 conf->inactive_blocked = 1;
570 wait_event_lock_irq(conf->wait_for_stripe,
571 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800572 (atomic_read(&conf->active_stripes)
573 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100575 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 conf->inactive_blocked = 0;
577 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100578 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else {
580 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100581 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000582 && !test_bit(STRIPE_EXPANDING, &sh->state)
Shaohua Li773ca822013-08-27 17:50:39 +0800583 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)
584 && !test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 } else {
586 if (!test_bit(STRIPE_HANDLE, &sh->state))
587 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700588 if (list_empty(&sh->lru) &&
589 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700590 BUG();
591 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 }
594 } while (sh == NULL);
595
596 if (sh)
597 atomic_inc(&sh->count);
598
599 spin_unlock_irq(&conf->device_lock);
600 return sh;
601}
602
NeilBrown05616be2012-05-21 09:27:00 +1000603/* Determine if 'data_offset' or 'new_data_offset' should be used
604 * in this stripe_head.
605 */
606static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
607{
608 sector_t progress = conf->reshape_progress;
609 /* Need a memory barrier to make sure we see the value
610 * of conf->generation, or ->data_offset that was set before
611 * reshape_progress was updated.
612 */
613 smp_rmb();
614 if (progress == MaxSector)
615 return 0;
616 if (sh->generation == conf->generation - 1)
617 return 0;
618 /* We are in a reshape, and this is a new-generation stripe,
619 * so use new_data_offset.
620 */
621 return 1;
622}
623
NeilBrown6712ecf2007-09-27 12:47:43 +0200624static void
625raid5_end_read_request(struct bio *bi, int error);
626static void
627raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700628
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000629static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700630{
NeilBrownd1688a62011-10-11 16:49:52 +1100631 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700632 int i, disks = sh->disks;
633
634 might_sleep();
635
636 for (i = disks; i--; ) {
637 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100638 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100639 struct bio *bi, *rbi;
640 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200641 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
642 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
643 rw = WRITE_FUA;
644 else
645 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100646 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100647 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200648 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700649 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100650 else if (test_and_clear_bit(R5_WantReplace,
651 &sh->dev[i].flags)) {
652 rw = WRITE;
653 replace_only = 1;
654 } else
Dan Williams91c00922007-01-02 13:52:30 -0700655 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000656 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
657 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700658
659 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100660 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700661
Dan Williams91c00922007-01-02 13:52:30 -0700662 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100663 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100664 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
665 rdev = rcu_dereference(conf->disks[i].rdev);
666 if (!rdev) {
667 rdev = rrdev;
668 rrdev = NULL;
669 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100670 if (rw & WRITE) {
671 if (replace_only)
672 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100673 if (rdev == rrdev)
674 /* We raced and saw duplicates */
675 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100676 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100677 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100678 rdev = rrdev;
679 rrdev = NULL;
680 }
NeilBrown977df362011-12-23 10:17:53 +1100681
Dan Williams91c00922007-01-02 13:52:30 -0700682 if (rdev && test_bit(Faulty, &rdev->flags))
683 rdev = NULL;
684 if (rdev)
685 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100686 if (rrdev && test_bit(Faulty, &rrdev->flags))
687 rrdev = NULL;
688 if (rrdev)
689 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700690 rcu_read_unlock();
691
NeilBrown73e92e52011-07-28 11:39:22 +1000692 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100693 * need to check for writes. We never accept write errors
694 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000695 */
696 while ((rw & WRITE) && rdev &&
697 test_bit(WriteErrorSeen, &rdev->flags)) {
698 sector_t first_bad;
699 int bad_sectors;
700 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
701 &first_bad, &bad_sectors);
702 if (!bad)
703 break;
704
705 if (bad < 0) {
706 set_bit(BlockedBadBlocks, &rdev->flags);
707 if (!conf->mddev->external &&
708 conf->mddev->flags) {
709 /* It is very unlikely, but we might
710 * still need to write out the
711 * bad block log - better give it
712 * a chance*/
713 md_check_recovery(conf->mddev);
714 }
majianpeng18507532012-07-03 12:11:54 +1000715 /*
716 * Because md_wait_for_blocked_rdev
717 * will dec nr_pending, we must
718 * increment it first.
719 */
720 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000721 md_wait_for_blocked_rdev(rdev, conf->mddev);
722 } else {
723 /* Acknowledged bad block - skip the write */
724 rdev_dec_pending(rdev, conf->mddev);
725 rdev = NULL;
726 }
727 }
728
Dan Williams91c00922007-01-02 13:52:30 -0700729 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100730 if (s->syncing || s->expanding || s->expanded
731 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700732 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
733
Dan Williams2b7497f2008-06-28 08:31:52 +1000734 set_bit(STRIPE_IO_STARTED, &sh->state);
735
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700736 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700737 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700738 bi->bi_rw = rw;
739 bi->bi_end_io = (rw & WRITE)
740 ? raid5_end_write_request
741 : raid5_end_read_request;
742 bi->bi_private = sh;
743
Dan Williams91c00922007-01-02 13:52:30 -0700744 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700745 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700746 bi->bi_rw, i);
747 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000748 if (use_new_offset(conf, sh))
749 bi->bi_sector = (sh->sector
750 + rdev->new_data_offset);
751 else
752 bi->bi_sector = (sh->sector
753 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000754 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
755 bi->bi_rw |= REQ_FLUSH;
756
Kent Overstreet4997b722013-05-30 08:44:39 +0200757 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700758 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
759 bi->bi_io_vec[0].bv_offset = 0;
760 bi->bi_size = STRIPE_SIZE;
NeilBrown977df362011-12-23 10:17:53 +1100761 if (rrdev)
762 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600763
764 if (conf->mddev->gendisk)
765 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
766 bi, disk_devt(conf->mddev->gendisk),
767 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700768 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100769 }
770 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100771 if (s->syncing || s->expanding || s->expanded
772 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100773 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
774
775 set_bit(STRIPE_IO_STARTED, &sh->state);
776
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700777 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100778 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700779 rbi->bi_rw = rw;
780 BUG_ON(!(rw & WRITE));
781 rbi->bi_end_io = raid5_end_write_request;
782 rbi->bi_private = sh;
783
NeilBrown977df362011-12-23 10:17:53 +1100784 pr_debug("%s: for %llu schedule op %ld on "
785 "replacement disc %d\n",
786 __func__, (unsigned long long)sh->sector,
787 rbi->bi_rw, i);
788 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000789 if (use_new_offset(conf, sh))
790 rbi->bi_sector = (sh->sector
791 + rrdev->new_data_offset);
792 else
793 rbi->bi_sector = (sh->sector
794 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200795 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100796 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
797 rbi->bi_io_vec[0].bv_offset = 0;
798 rbi->bi_size = STRIPE_SIZE;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600799 if (conf->mddev->gendisk)
800 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
801 rbi, disk_devt(conf->mddev->gendisk),
802 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100803 generic_make_request(rbi);
804 }
805 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000806 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700807 set_bit(STRIPE_DEGRADED, &sh->state);
808 pr_debug("skip op %ld on disc %d for sector %llu\n",
809 bi->bi_rw, i, (unsigned long long)sh->sector);
810 clear_bit(R5_LOCKED, &sh->dev[i].flags);
811 set_bit(STRIPE_HANDLE, &sh->state);
812 }
813 }
814}
815
816static struct dma_async_tx_descriptor *
817async_copy_data(int frombio, struct bio *bio, struct page *page,
818 sector_t sector, struct dma_async_tx_descriptor *tx)
819{
820 struct bio_vec *bvl;
821 struct page *bio_page;
822 int i;
823 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700824 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700825 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700826
827 if (bio->bi_sector >= sector)
828 page_offset = (signed)(bio->bi_sector - sector) * 512;
829 else
830 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700831
Dan Williams0403e382009-09-08 17:42:50 -0700832 if (frombio)
833 flags |= ASYNC_TX_FENCE;
834 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
835
Dan Williams91c00922007-01-02 13:52:30 -0700836 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000837 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700838 int clen;
839 int b_offset = 0;
840
841 if (page_offset < 0) {
842 b_offset = -page_offset;
843 page_offset += b_offset;
844 len -= b_offset;
845 }
846
847 if (len > 0 && page_offset + len > STRIPE_SIZE)
848 clen = STRIPE_SIZE - page_offset;
849 else
850 clen = len;
851
852 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000853 b_offset += bvl->bv_offset;
854 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700855 if (frombio)
856 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700857 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700858 else
859 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700860 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700861 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700862 /* chain the operations */
863 submit.depend_tx = tx;
864
Dan Williams91c00922007-01-02 13:52:30 -0700865 if (clen < len) /* hit end of page */
866 break;
867 page_offset += len;
868 }
869
870 return tx;
871}
872
873static void ops_complete_biofill(void *stripe_head_ref)
874{
875 struct stripe_head *sh = stripe_head_ref;
876 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700877 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700878
Harvey Harrisone46b2722008-04-28 02:15:50 -0700879 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700880 (unsigned long long)sh->sector);
881
882 /* clear completed biofills */
883 for (i = sh->disks; i--; ) {
884 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700885
886 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700887 /* and check if we need to reply to a read request,
888 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000889 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700890 */
891 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700892 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700893
Dan Williams91c00922007-01-02 13:52:30 -0700894 BUG_ON(!dev->read);
895 rbi = dev->read;
896 dev->read = NULL;
897 while (rbi && rbi->bi_sector <
898 dev->sector + STRIPE_SECTORS) {
899 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000900 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700901 rbi->bi_next = return_bi;
902 return_bi = rbi;
903 }
Dan Williams91c00922007-01-02 13:52:30 -0700904 rbi = rbi2;
905 }
906 }
907 }
Dan Williams83de75c2008-06-28 08:31:58 +1000908 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700909
910 return_io(return_bi);
911
Dan Williamse4d84902007-09-24 10:06:13 -0700912 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700913 release_stripe(sh);
914}
915
916static void ops_run_biofill(struct stripe_head *sh)
917{
918 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700919 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700920 int i;
921
Harvey Harrisone46b2722008-04-28 02:15:50 -0700922 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700923 (unsigned long long)sh->sector);
924
925 for (i = sh->disks; i--; ) {
926 struct r5dev *dev = &sh->dev[i];
927 if (test_bit(R5_Wantfill, &dev->flags)) {
928 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000929 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700930 dev->read = rbi = dev->toread;
931 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000932 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700933 while (rbi && rbi->bi_sector <
934 dev->sector + STRIPE_SECTORS) {
935 tx = async_copy_data(0, rbi, dev->page,
936 dev->sector, tx);
937 rbi = r5_next_bio(rbi, dev->sector);
938 }
939 }
940 }
941
942 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700943 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
944 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700945}
946
Dan Williams4e7d2c02009-08-29 19:13:11 -0700947static void mark_target_uptodate(struct stripe_head *sh, int target)
948{
949 struct r5dev *tgt;
950
951 if (target < 0)
952 return;
953
954 tgt = &sh->dev[target];
955 set_bit(R5_UPTODATE, &tgt->flags);
956 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
957 clear_bit(R5_Wantcompute, &tgt->flags);
958}
959
Dan Williamsac6b53b2009-07-14 13:40:19 -0700960static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700961{
962 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700963
Harvey Harrisone46b2722008-04-28 02:15:50 -0700964 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700965 (unsigned long long)sh->sector);
966
Dan Williamsac6b53b2009-07-14 13:40:19 -0700967 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700968 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700969 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700970
Dan Williamsecc65c92008-06-28 08:31:57 +1000971 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
972 if (sh->check_state == check_state_compute_run)
973 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700974 set_bit(STRIPE_HANDLE, &sh->state);
975 release_stripe(sh);
976}
977
Dan Williamsd6f38f32009-07-14 11:50:52 -0700978/* return a pointer to the address conversion region of the scribble buffer */
979static addr_conv_t *to_addr_conv(struct stripe_head *sh,
980 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700981{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700982 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
983}
984
985static struct dma_async_tx_descriptor *
986ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
987{
Dan Williams91c00922007-01-02 13:52:30 -0700988 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700989 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700990 int target = sh->ops.target;
991 struct r5dev *tgt = &sh->dev[target];
992 struct page *xor_dest = tgt->page;
993 int count = 0;
994 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700995 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700996 int i;
997
998 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700999 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001000 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1001
1002 for (i = disks; i--; )
1003 if (i != target)
1004 xor_srcs[count++] = sh->dev[i].page;
1005
1006 atomic_inc(&sh->count);
1007
Dan Williams0403e382009-09-08 17:42:50 -07001008 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001009 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -07001010 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001011 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001012 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001013 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001014
Dan Williams91c00922007-01-02 13:52:30 -07001015 return tx;
1016}
1017
Dan Williamsac6b53b2009-07-14 13:40:19 -07001018/* set_syndrome_sources - populate source buffers for gen_syndrome
1019 * @srcs - (struct page *) array of size sh->disks
1020 * @sh - stripe_head to parse
1021 *
1022 * Populates srcs in proper layout order for the stripe and returns the
1023 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1024 * destination buffer is recorded in srcs[count] and the Q destination
1025 * is recorded in srcs[count+1]].
1026 */
1027static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1028{
1029 int disks = sh->disks;
1030 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1031 int d0_idx = raid6_d0(sh);
1032 int count;
1033 int i;
1034
1035 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001036 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001037
1038 count = 0;
1039 i = d0_idx;
1040 do {
1041 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1042
1043 srcs[slot] = sh->dev[i].page;
1044 i = raid6_next_disk(i, disks);
1045 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001046
NeilBrowne4424fe2009-10-16 16:27:34 +11001047 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001048}
1049
1050static struct dma_async_tx_descriptor *
1051ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1052{
1053 int disks = sh->disks;
1054 struct page **blocks = percpu->scribble;
1055 int target;
1056 int qd_idx = sh->qd_idx;
1057 struct dma_async_tx_descriptor *tx;
1058 struct async_submit_ctl submit;
1059 struct r5dev *tgt;
1060 struct page *dest;
1061 int i;
1062 int count;
1063
1064 if (sh->ops.target < 0)
1065 target = sh->ops.target2;
1066 else if (sh->ops.target2 < 0)
1067 target = sh->ops.target;
1068 else
1069 /* we should only have one valid target */
1070 BUG();
1071 BUG_ON(target < 0);
1072 pr_debug("%s: stripe %llu block: %d\n",
1073 __func__, (unsigned long long)sh->sector, target);
1074
1075 tgt = &sh->dev[target];
1076 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1077 dest = tgt->page;
1078
1079 atomic_inc(&sh->count);
1080
1081 if (target == qd_idx) {
1082 count = set_syndrome_sources(blocks, sh);
1083 blocks[count] = NULL; /* regenerating p is not necessary */
1084 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001085 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1086 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001087 to_addr_conv(sh, percpu));
1088 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1089 } else {
1090 /* Compute any data- or p-drive using XOR */
1091 count = 0;
1092 for (i = disks; i-- ; ) {
1093 if (i == target || i == qd_idx)
1094 continue;
1095 blocks[count++] = sh->dev[i].page;
1096 }
1097
Dan Williams0403e382009-09-08 17:42:50 -07001098 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1099 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001100 to_addr_conv(sh, percpu));
1101 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1102 }
1103
1104 return tx;
1105}
1106
1107static struct dma_async_tx_descriptor *
1108ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1109{
1110 int i, count, disks = sh->disks;
1111 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1112 int d0_idx = raid6_d0(sh);
1113 int faila = -1, failb = -1;
1114 int target = sh->ops.target;
1115 int target2 = sh->ops.target2;
1116 struct r5dev *tgt = &sh->dev[target];
1117 struct r5dev *tgt2 = &sh->dev[target2];
1118 struct dma_async_tx_descriptor *tx;
1119 struct page **blocks = percpu->scribble;
1120 struct async_submit_ctl submit;
1121
1122 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1123 __func__, (unsigned long long)sh->sector, target, target2);
1124 BUG_ON(target < 0 || target2 < 0);
1125 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1126 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1127
Dan Williams6c910a72009-09-16 12:24:54 -07001128 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001129 * slot number conversion for 'faila' and 'failb'
1130 */
1131 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001132 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001133 count = 0;
1134 i = d0_idx;
1135 do {
1136 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1137
1138 blocks[slot] = sh->dev[i].page;
1139
1140 if (i == target)
1141 faila = slot;
1142 if (i == target2)
1143 failb = slot;
1144 i = raid6_next_disk(i, disks);
1145 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001146
1147 BUG_ON(faila == failb);
1148 if (failb < faila)
1149 swap(faila, failb);
1150 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1151 __func__, (unsigned long long)sh->sector, faila, failb);
1152
1153 atomic_inc(&sh->count);
1154
1155 if (failb == syndrome_disks+1) {
1156 /* Q disk is one of the missing disks */
1157 if (faila == syndrome_disks) {
1158 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001159 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1160 ops_complete_compute, sh,
1161 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001162 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001163 STRIPE_SIZE, &submit);
1164 } else {
1165 struct page *dest;
1166 int data_target;
1167 int qd_idx = sh->qd_idx;
1168
1169 /* Missing D+Q: recompute D from P, then recompute Q */
1170 if (target == qd_idx)
1171 data_target = target2;
1172 else
1173 data_target = target;
1174
1175 count = 0;
1176 for (i = disks; i-- ; ) {
1177 if (i == data_target || i == qd_idx)
1178 continue;
1179 blocks[count++] = sh->dev[i].page;
1180 }
1181 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001182 init_async_submit(&submit,
1183 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1184 NULL, NULL, NULL,
1185 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001186 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1187 &submit);
1188
1189 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001190 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1191 ops_complete_compute, sh,
1192 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001193 return async_gen_syndrome(blocks, 0, count+2,
1194 STRIPE_SIZE, &submit);
1195 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001196 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001197 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1198 ops_complete_compute, sh,
1199 to_addr_conv(sh, percpu));
1200 if (failb == syndrome_disks) {
1201 /* We're missing D+P. */
1202 return async_raid6_datap_recov(syndrome_disks+2,
1203 STRIPE_SIZE, faila,
1204 blocks, &submit);
1205 } else {
1206 /* We're missing D+D. */
1207 return async_raid6_2data_recov(syndrome_disks+2,
1208 STRIPE_SIZE, faila, failb,
1209 blocks, &submit);
1210 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001211 }
1212}
1213
1214
Dan Williams91c00922007-01-02 13:52:30 -07001215static void ops_complete_prexor(void *stripe_head_ref)
1216{
1217 struct stripe_head *sh = stripe_head_ref;
1218
Harvey Harrisone46b2722008-04-28 02:15:50 -07001219 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001220 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001221}
1222
1223static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001224ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1225 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001226{
Dan Williams91c00922007-01-02 13:52:30 -07001227 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001228 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001229 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001230 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001231
1232 /* existing parity data subtracted */
1233 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1234
Harvey Harrisone46b2722008-04-28 02:15:50 -07001235 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001236 (unsigned long long)sh->sector);
1237
1238 for (i = disks; i--; ) {
1239 struct r5dev *dev = &sh->dev[i];
1240 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001241 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001242 xor_srcs[count++] = dev->page;
1243 }
1244
Dan Williams0403e382009-09-08 17:42:50 -07001245 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001246 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001247 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001248
1249 return tx;
1250}
1251
1252static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001253ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001254{
1255 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001256 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001257
Harvey Harrisone46b2722008-04-28 02:15:50 -07001258 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001259 (unsigned long long)sh->sector);
1260
1261 for (i = disks; i--; ) {
1262 struct r5dev *dev = &sh->dev[i];
1263 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001264
Dan Williamsd8ee0722008-06-28 08:32:06 +10001265 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001266 struct bio *wbi;
1267
Shaohua Lib17459c2012-07-19 16:01:31 +10001268 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001269 chosen = dev->towrite;
1270 dev->towrite = NULL;
1271 BUG_ON(dev->written);
1272 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001273 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001274
1275 while (wbi && wbi->bi_sector <
1276 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001277 if (wbi->bi_rw & REQ_FUA)
1278 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001279 if (wbi->bi_rw & REQ_SYNC)
1280 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001281 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001282 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001283 else
Shaohua Li620125f2012-10-11 13:49:05 +11001284 tx = async_copy_data(1, wbi, dev->page,
1285 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001286 wbi = r5_next_bio(wbi, dev->sector);
1287 }
1288 }
1289 }
1290
1291 return tx;
1292}
1293
Dan Williamsac6b53b2009-07-14 13:40:19 -07001294static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001295{
1296 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001297 int disks = sh->disks;
1298 int pd_idx = sh->pd_idx;
1299 int qd_idx = sh->qd_idx;
1300 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001301 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001302
Harvey Harrisone46b2722008-04-28 02:15:50 -07001303 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001304 (unsigned long long)sh->sector);
1305
Shaohua Libc0934f2012-05-22 13:55:05 +10001306 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001307 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001308 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001309 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001310 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001311
Dan Williams91c00922007-01-02 13:52:30 -07001312 for (i = disks; i--; ) {
1313 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001314
Tejun Heoe9c74692010-09-03 11:56:18 +02001315 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001316 if (!discard)
1317 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001318 if (fua)
1319 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001320 if (sync)
1321 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001322 }
Dan Williams91c00922007-01-02 13:52:30 -07001323 }
1324
Dan Williamsd8ee0722008-06-28 08:32:06 +10001325 if (sh->reconstruct_state == reconstruct_state_drain_run)
1326 sh->reconstruct_state = reconstruct_state_drain_result;
1327 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1328 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1329 else {
1330 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1331 sh->reconstruct_state = reconstruct_state_result;
1332 }
Dan Williams91c00922007-01-02 13:52:30 -07001333
1334 set_bit(STRIPE_HANDLE, &sh->state);
1335 release_stripe(sh);
1336}
1337
1338static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001339ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1340 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001341{
Dan Williams91c00922007-01-02 13:52:30 -07001342 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001343 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001344 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001345 int count = 0, pd_idx = sh->pd_idx, i;
1346 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001347 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001348 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001349
Harvey Harrisone46b2722008-04-28 02:15:50 -07001350 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001351 (unsigned long long)sh->sector);
1352
Shaohua Li620125f2012-10-11 13:49:05 +11001353 for (i = 0; i < sh->disks; i++) {
1354 if (pd_idx == i)
1355 continue;
1356 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1357 break;
1358 }
1359 if (i >= sh->disks) {
1360 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001361 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1362 ops_complete_reconstruct(sh);
1363 return;
1364 }
Dan Williams91c00922007-01-02 13:52:30 -07001365 /* check if prexor is active which means only process blocks
1366 * that are part of a read-modify-write (written)
1367 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001368 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1369 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001370 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1371 for (i = disks; i--; ) {
1372 struct r5dev *dev = &sh->dev[i];
1373 if (dev->written)
1374 xor_srcs[count++] = dev->page;
1375 }
1376 } else {
1377 xor_dest = sh->dev[pd_idx].page;
1378 for (i = disks; i--; ) {
1379 struct r5dev *dev = &sh->dev[i];
1380 if (i != pd_idx)
1381 xor_srcs[count++] = dev->page;
1382 }
1383 }
1384
Dan Williams91c00922007-01-02 13:52:30 -07001385 /* 1/ if we prexor'd then the dest is reused as a source
1386 * 2/ if we did not prexor then we are redoing the parity
1387 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1388 * for the synchronous xor case
1389 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001390 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001391 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1392
1393 atomic_inc(&sh->count);
1394
Dan Williamsac6b53b2009-07-14 13:40:19 -07001395 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001396 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001397 if (unlikely(count == 1))
1398 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1399 else
1400 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001401}
1402
Dan Williamsac6b53b2009-07-14 13:40:19 -07001403static void
1404ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1405 struct dma_async_tx_descriptor *tx)
1406{
1407 struct async_submit_ctl submit;
1408 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001409 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001410
1411 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1412
Shaohua Li620125f2012-10-11 13:49:05 +11001413 for (i = 0; i < sh->disks; i++) {
1414 if (sh->pd_idx == i || sh->qd_idx == i)
1415 continue;
1416 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1417 break;
1418 }
1419 if (i >= sh->disks) {
1420 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001421 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1422 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1423 ops_complete_reconstruct(sh);
1424 return;
1425 }
1426
Dan Williamsac6b53b2009-07-14 13:40:19 -07001427 count = set_syndrome_sources(blocks, sh);
1428
1429 atomic_inc(&sh->count);
1430
1431 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1432 sh, to_addr_conv(sh, percpu));
1433 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001434}
1435
1436static void ops_complete_check(void *stripe_head_ref)
1437{
1438 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001439
Harvey Harrisone46b2722008-04-28 02:15:50 -07001440 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001441 (unsigned long long)sh->sector);
1442
Dan Williamsecc65c92008-06-28 08:31:57 +10001443 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001444 set_bit(STRIPE_HANDLE, &sh->state);
1445 release_stripe(sh);
1446}
1447
Dan Williamsac6b53b2009-07-14 13:40:19 -07001448static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001449{
Dan Williams91c00922007-01-02 13:52:30 -07001450 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001451 int pd_idx = sh->pd_idx;
1452 int qd_idx = sh->qd_idx;
1453 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001454 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001455 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001456 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001457 int count;
1458 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001459
Harvey Harrisone46b2722008-04-28 02:15:50 -07001460 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001461 (unsigned long long)sh->sector);
1462
Dan Williamsac6b53b2009-07-14 13:40:19 -07001463 count = 0;
1464 xor_dest = sh->dev[pd_idx].page;
1465 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001466 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001467 if (i == pd_idx || i == qd_idx)
1468 continue;
1469 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001470 }
1471
Dan Williamsd6f38f32009-07-14 11:50:52 -07001472 init_async_submit(&submit, 0, NULL, NULL, NULL,
1473 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001474 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001475 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001476
Dan Williams91c00922007-01-02 13:52:30 -07001477 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001478 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1479 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001480}
1481
Dan Williamsac6b53b2009-07-14 13:40:19 -07001482static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1483{
1484 struct page **srcs = percpu->scribble;
1485 struct async_submit_ctl submit;
1486 int count;
1487
1488 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1489 (unsigned long long)sh->sector, checkp);
1490
1491 count = set_syndrome_sources(srcs, sh);
1492 if (!checkp)
1493 srcs[count] = NULL;
1494
1495 atomic_inc(&sh->count);
1496 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1497 sh, to_addr_conv(sh, percpu));
1498 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1499 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1500}
1501
NeilBrown51acbce2013-02-28 09:08:34 +11001502static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001503{
1504 int overlap_clear = 0, i, disks = sh->disks;
1505 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001506 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001507 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001508 struct raid5_percpu *percpu;
1509 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001510
Dan Williamsd6f38f32009-07-14 11:50:52 -07001511 cpu = get_cpu();
1512 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001513 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001514 ops_run_biofill(sh);
1515 overlap_clear++;
1516 }
1517
Dan Williams7b3a8712008-06-28 08:32:09 +10001518 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001519 if (level < 6)
1520 tx = ops_run_compute5(sh, percpu);
1521 else {
1522 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1523 tx = ops_run_compute6_1(sh, percpu);
1524 else
1525 tx = ops_run_compute6_2(sh, percpu);
1526 }
1527 /* terminate the chain if reconstruct is not set to be run */
1528 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001529 async_tx_ack(tx);
1530 }
Dan Williams91c00922007-01-02 13:52:30 -07001531
Dan Williams600aa102008-06-28 08:32:05 +10001532 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001533 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001534
Dan Williams600aa102008-06-28 08:32:05 +10001535 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001536 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001537 overlap_clear++;
1538 }
1539
Dan Williamsac6b53b2009-07-14 13:40:19 -07001540 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1541 if (level < 6)
1542 ops_run_reconstruct5(sh, percpu, tx);
1543 else
1544 ops_run_reconstruct6(sh, percpu, tx);
1545 }
Dan Williams91c00922007-01-02 13:52:30 -07001546
Dan Williamsac6b53b2009-07-14 13:40:19 -07001547 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1548 if (sh->check_state == check_state_run)
1549 ops_run_check_p(sh, percpu);
1550 else if (sh->check_state == check_state_run_q)
1551 ops_run_check_pq(sh, percpu, 0);
1552 else if (sh->check_state == check_state_run_pq)
1553 ops_run_check_pq(sh, percpu, 1);
1554 else
1555 BUG();
1556 }
Dan Williams91c00922007-01-02 13:52:30 -07001557
Dan Williams91c00922007-01-02 13:52:30 -07001558 if (overlap_clear)
1559 for (i = disks; i--; ) {
1560 struct r5dev *dev = &sh->dev[i];
1561 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1562 wake_up(&sh->raid_conf->wait_for_overlap);
1563 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001564 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001565}
1566
NeilBrownd1688a62011-10-11 16:49:52 +11001567static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001570 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001571 if (!sh)
1572 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001573
NeilBrown3f294f42005-11-08 21:39:25 -08001574 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001575
Shaohua Lib17459c2012-07-19 16:01:31 +10001576 spin_lock_init(&sh->stripe_lock);
1577
NeilBrowne4e11e32010-06-16 16:45:16 +10001578 if (grow_buffers(sh)) {
1579 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001580 kmem_cache_free(conf->slab_cache, sh);
1581 return 0;
1582 }
1583 /* we just created an active stripe so... */
1584 atomic_set(&sh->count, 1);
1585 atomic_inc(&conf->active_stripes);
1586 INIT_LIST_HEAD(&sh->lru);
1587 release_stripe(sh);
1588 return 1;
1589}
1590
NeilBrownd1688a62011-10-11 16:49:52 +11001591static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001592{
Christoph Lametere18b8902006-12-06 20:33:20 -08001593 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001594 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
NeilBrownf4be6b42010-06-01 19:37:25 +10001596 if (conf->mddev->gendisk)
1597 sprintf(conf->cache_name[0],
1598 "raid%d-%s", conf->level, mdname(conf->mddev));
1599 else
1600 sprintf(conf->cache_name[0],
1601 "raid%d-%p", conf->level, conf->mddev);
1602 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1603
NeilBrownad01c9e2006-03-27 01:18:07 -08001604 conf->active_name = 0;
1605 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001607 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (!sc)
1609 return 1;
1610 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001611 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001612 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001613 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return 0;
1616}
NeilBrown29269552006-03-27 01:18:10 -08001617
Dan Williamsd6f38f32009-07-14 11:50:52 -07001618/**
1619 * scribble_len - return the required size of the scribble region
1620 * @num - total number of disks in the array
1621 *
1622 * The size must be enough to contain:
1623 * 1/ a struct page pointer for each device in the array +2
1624 * 2/ room to convert each entry in (1) to its corresponding dma
1625 * (dma_map_page()) or page (page_address()) address.
1626 *
1627 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1628 * calculate over all devices (not just the data blocks), using zeros in place
1629 * of the P and Q blocks.
1630 */
1631static size_t scribble_len(int num)
1632{
1633 size_t len;
1634
1635 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1636
1637 return len;
1638}
1639
NeilBrownd1688a62011-10-11 16:49:52 +11001640static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001641{
1642 /* Make all the stripes able to hold 'newsize' devices.
1643 * New slots in each stripe get 'page' set to a new page.
1644 *
1645 * This happens in stages:
1646 * 1/ create a new kmem_cache and allocate the required number of
1647 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001648 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001649 * to the new stripe_heads. This will have the side effect of
1650 * freezing the array as once all stripe_heads have been collected,
1651 * no IO will be possible. Old stripe heads are freed once their
1652 * pages have been transferred over, and the old kmem_cache is
1653 * freed when all stripes are done.
1654 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1655 * we simple return a failre status - no need to clean anything up.
1656 * 4/ allocate new pages for the new slots in the new stripe_heads.
1657 * If this fails, we don't bother trying the shrink the
1658 * stripe_heads down again, we just leave them as they are.
1659 * As each stripe_head is processed the new one is released into
1660 * active service.
1661 *
1662 * Once step2 is started, we cannot afford to wait for a write,
1663 * so we use GFP_NOIO allocations.
1664 */
1665 struct stripe_head *osh, *nsh;
1666 LIST_HEAD(newstripes);
1667 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001668 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001669 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001670 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001671 int i;
1672
1673 if (newsize <= conf->pool_size)
1674 return 0; /* never bother to shrink */
1675
Dan Williamsb5470dc2008-06-27 21:44:04 -07001676 err = md_allow_write(conf->mddev);
1677 if (err)
1678 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001679
NeilBrownad01c9e2006-03-27 01:18:07 -08001680 /* Step 1 */
1681 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1682 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001683 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001684 if (!sc)
1685 return -ENOMEM;
1686
1687 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001688 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001689 if (!nsh)
1690 break;
1691
NeilBrownad01c9e2006-03-27 01:18:07 -08001692 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001693 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001694
1695 list_add(&nsh->lru, &newstripes);
1696 }
1697 if (i) {
1698 /* didn't get enough, give up */
1699 while (!list_empty(&newstripes)) {
1700 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1701 list_del(&nsh->lru);
1702 kmem_cache_free(sc, nsh);
1703 }
1704 kmem_cache_destroy(sc);
1705 return -ENOMEM;
1706 }
1707 /* Step 2 - Must use GFP_NOIO now.
1708 * OK, we have enough stripes, start collecting inactive
1709 * stripes and copying them over
1710 */
1711 list_for_each_entry(nsh, &newstripes, lru) {
1712 spin_lock_irq(&conf->device_lock);
1713 wait_event_lock_irq(conf->wait_for_stripe,
1714 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001715 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001716 osh = get_free_stripe(conf);
1717 spin_unlock_irq(&conf->device_lock);
1718 atomic_set(&nsh->count, 1);
1719 for(i=0; i<conf->pool_size; i++)
1720 nsh->dev[i].page = osh->dev[i].page;
1721 for( ; i<newsize; i++)
1722 nsh->dev[i].page = NULL;
1723 kmem_cache_free(conf->slab_cache, osh);
1724 }
1725 kmem_cache_destroy(conf->slab_cache);
1726
1727 /* Step 3.
1728 * At this point, we are holding all the stripes so the array
1729 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001730 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001731 */
1732 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1733 if (ndisks) {
1734 for (i=0; i<conf->raid_disks; i++)
1735 ndisks[i] = conf->disks[i];
1736 kfree(conf->disks);
1737 conf->disks = ndisks;
1738 } else
1739 err = -ENOMEM;
1740
Dan Williamsd6f38f32009-07-14 11:50:52 -07001741 get_online_cpus();
1742 conf->scribble_len = scribble_len(newsize);
1743 for_each_present_cpu(cpu) {
1744 struct raid5_percpu *percpu;
1745 void *scribble;
1746
1747 percpu = per_cpu_ptr(conf->percpu, cpu);
1748 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1749
1750 if (scribble) {
1751 kfree(percpu->scribble);
1752 percpu->scribble = scribble;
1753 } else {
1754 err = -ENOMEM;
1755 break;
1756 }
1757 }
1758 put_online_cpus();
1759
NeilBrownad01c9e2006-03-27 01:18:07 -08001760 /* Step 4, return new stripes to service */
1761 while(!list_empty(&newstripes)) {
1762 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1763 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001764
NeilBrownad01c9e2006-03-27 01:18:07 -08001765 for (i=conf->raid_disks; i < newsize; i++)
1766 if (nsh->dev[i].page == NULL) {
1767 struct page *p = alloc_page(GFP_NOIO);
1768 nsh->dev[i].page = p;
1769 if (!p)
1770 err = -ENOMEM;
1771 }
1772 release_stripe(nsh);
1773 }
1774 /* critical section pass, GFP_NOIO no longer needed */
1775
1776 conf->slab_cache = sc;
1777 conf->active_name = 1-conf->active_name;
1778 conf->pool_size = newsize;
1779 return err;
1780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
NeilBrownd1688a62011-10-11 16:49:52 +11001782static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct stripe_head *sh;
1785
NeilBrown3f294f42005-11-08 21:39:25 -08001786 spin_lock_irq(&conf->device_lock);
1787 sh = get_free_stripe(conf);
1788 spin_unlock_irq(&conf->device_lock);
1789 if (!sh)
1790 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001791 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001792 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001793 kmem_cache_free(conf->slab_cache, sh);
1794 atomic_dec(&conf->active_stripes);
1795 return 1;
1796}
1797
NeilBrownd1688a62011-10-11 16:49:52 +11001798static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001799{
1800 while (drop_one_stripe(conf))
1801 ;
1802
NeilBrown29fc7e32006-02-03 03:03:41 -08001803 if (conf->slab_cache)
1804 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 conf->slab_cache = NULL;
1806}
1807
NeilBrown6712ecf2007-09-27 12:47:43 +02001808static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
NeilBrown99c0fb52009-03-31 14:39:38 +11001810 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001811 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001812 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001814 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001815 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001816 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 for (i=0 ; i<disks; i++)
1819 if (bi == &sh->dev[i].req)
1820 break;
1821
Dan Williams45b42332007-07-09 11:56:43 -07001822 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1823 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 uptodate);
1825 if (i == disks) {
1826 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001827 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
NeilBrown14a75d32011-12-23 10:17:52 +11001829 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001830 /* If replacement finished while this request was outstanding,
1831 * 'replacement' might be NULL already.
1832 * In that case it moved down to 'rdev'.
1833 * rdev is not removed until all requests are finished.
1834 */
NeilBrown14a75d32011-12-23 10:17:52 +11001835 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001836 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001837 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
NeilBrown05616be2012-05-21 09:27:00 +10001839 if (use_new_offset(conf, sh))
1840 s = sh->sector + rdev->new_data_offset;
1841 else
1842 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001845 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001846 /* Note that this cannot happen on a
1847 * replacement device. We just fail those on
1848 * any error
1849 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001850 printk_ratelimited(
1851 KERN_INFO
1852 "md/raid:%s: read error corrected"
1853 " (%lu sectors at %llu on %s)\n",
1854 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001855 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001856 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001857 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001858 clear_bit(R5_ReadError, &sh->dev[i].flags);
1859 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001860 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1861 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1862
NeilBrown14a75d32011-12-23 10:17:52 +11001863 if (atomic_read(&rdev->read_errors))
1864 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001866 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001867 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001868 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001871 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001872 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1873 printk_ratelimited(
1874 KERN_WARNING
1875 "md/raid:%s: read error on replacement device "
1876 "(sector %llu on %s).\n",
1877 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001878 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001879 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001880 else if (conf->mddev->degraded >= conf->max_degraded) {
1881 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001882 printk_ratelimited(
1883 KERN_WARNING
1884 "md/raid:%s: read error not correctable "
1885 "(sector %llu on %s).\n",
1886 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001887 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001888 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001889 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001890 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001891 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001892 printk_ratelimited(
1893 KERN_WARNING
1894 "md/raid:%s: read error NOT corrected!! "
1895 "(sector %llu on %s).\n",
1896 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001897 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001898 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001899 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001900 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001901 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001902 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001903 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001904 else
1905 retry = 1;
1906 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001907 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1908 set_bit(R5_ReadError, &sh->dev[i].flags);
1909 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1910 } else
1911 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001912 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001913 clear_bit(R5_ReadError, &sh->dev[i].flags);
1914 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001915 if (!(set_bad
1916 && test_bit(In_sync, &rdev->flags)
1917 && rdev_set_badblocks(
1918 rdev, sh->sector, STRIPE_SECTORS, 0)))
1919 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
NeilBrown14a75d32011-12-23 10:17:52 +11001922 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1924 set_bit(STRIPE_HANDLE, &sh->state);
1925 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
NeilBrownd710e132008-10-13 11:55:12 +11001928static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
NeilBrown99c0fb52009-03-31 14:39:38 +11001930 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001931 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001932 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001933 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001935 sector_t first_bad;
1936 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001937 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
NeilBrown977df362011-12-23 10:17:53 +11001939 for (i = 0 ; i < disks; i++) {
1940 if (bi == &sh->dev[i].req) {
1941 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
NeilBrown977df362011-12-23 10:17:53 +11001943 }
1944 if (bi == &sh->dev[i].rreq) {
1945 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001946 if (rdev)
1947 replacement = 1;
1948 else
1949 /* rdev was removed and 'replacement'
1950 * replaced it. rdev is not removed
1951 * until all requests are finished.
1952 */
1953 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001954 break;
1955 }
1956 }
Dan Williams45b42332007-07-09 11:56:43 -07001957 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1959 uptodate);
1960 if (i == disks) {
1961 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001962 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
NeilBrown977df362011-12-23 10:17:53 +11001965 if (replacement) {
1966 if (!uptodate)
1967 md_error(conf->mddev, rdev);
1968 else if (is_badblock(rdev, sh->sector,
1969 STRIPE_SECTORS,
1970 &first_bad, &bad_sectors))
1971 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1972 } else {
1973 if (!uptodate) {
1974 set_bit(WriteErrorSeen, &rdev->flags);
1975 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001976 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1977 set_bit(MD_RECOVERY_NEEDED,
1978 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001979 } else if (is_badblock(rdev, sh->sector,
1980 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10001981 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11001982 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10001983 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1984 /* That was a successful write so make
1985 * sure it looks like we already did
1986 * a re-write.
1987 */
1988 set_bit(R5_ReWrite, &sh->dev[i].flags);
1989 }
NeilBrown977df362011-12-23 10:17:53 +11001990 }
1991 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
NeilBrown977df362011-12-23 10:17:53 +11001993 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1994 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001996 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
NeilBrown784052e2009-03-31 15:19:07 +11001999static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
NeilBrown784052e2009-03-31 15:19:07 +11002001static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct r5dev *dev = &sh->dev[i];
2004
2005 bio_init(&dev->req);
2006 dev->req.bi_io_vec = &dev->vec;
2007 dev->req.bi_vcnt++;
2008 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11002010 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
NeilBrown977df362011-12-23 10:17:53 +11002012 bio_init(&dev->rreq);
2013 dev->rreq.bi_io_vec = &dev->rvec;
2014 dev->rreq.bi_vcnt++;
2015 dev->rreq.bi_max_vecs++;
2016 dev->rreq.bi_private = sh;
2017 dev->rvec.bv_page = dev->page;
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002020 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
NeilBrownfd01b882011-10-11 16:47:53 +11002023static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002026 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002027 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002028 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
NeilBrown908f4fb2011-12-23 10:17:50 +11002030 spin_lock_irqsave(&conf->device_lock, flags);
2031 clear_bit(In_sync, &rdev->flags);
2032 mddev->degraded = calc_degraded(conf);
2033 spin_unlock_irqrestore(&conf->device_lock, flags);
2034 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2035
NeilBrownde393cd2011-07-28 11:31:48 +10002036 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002037 set_bit(Faulty, &rdev->flags);
2038 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2039 printk(KERN_ALERT
2040 "md/raid:%s: Disk failure on %s, disabling device.\n"
2041 "md/raid:%s: Operation continuing on %d devices.\n",
2042 mdname(mddev),
2043 bdevname(rdev->bdev, b),
2044 mdname(mddev),
2045 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002046}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048/*
2049 * Input: a 'big' sector number,
2050 * Output: index of the data and parity disk, and the sector # in them.
2051 */
NeilBrownd1688a62011-10-11 16:49:52 +11002052static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002053 int previous, int *dd_idx,
2054 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002056 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002057 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002059 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002060 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002062 int algorithm = previous ? conf->prev_algo
2063 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002064 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2065 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002066 int raid_disks = previous ? conf->previous_raid_disks
2067 : conf->raid_disks;
2068 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* First compute the information on this sector */
2071
2072 /*
2073 * Compute the chunk number and the sector offset inside the chunk
2074 */
2075 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2076 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 /*
2079 * Compute the stripe number
2080 */
NeilBrown35f2a592010-04-20 14:13:34 +10002081 stripe = chunk_number;
2082 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002083 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Select the parity disk based on the user selected algorithm.
2086 */
NeilBrown84789552011-07-27 11:00:36 +10002087 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002088 switch(conf->level) {
2089 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002090 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002091 break;
2092 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002093 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002095 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002096 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 (*dd_idx)++;
2098 break;
2099 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002100 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002101 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 (*dd_idx)++;
2103 break;
2104 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002105 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002106 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 break;
2108 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002109 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002110 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002112 case ALGORITHM_PARITY_0:
2113 pd_idx = 0;
2114 (*dd_idx)++;
2115 break;
2116 case ALGORITHM_PARITY_N:
2117 pd_idx = data_disks;
2118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002121 }
2122 break;
2123 case 6:
2124
NeilBrowne183eae2009-03-31 15:20:22 +11002125 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002126 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002127 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002128 qd_idx = pd_idx + 1;
2129 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002130 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002131 qd_idx = 0;
2132 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002133 (*dd_idx) += 2; /* D D P Q D */
2134 break;
2135 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002136 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002137 qd_idx = pd_idx + 1;
2138 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002139 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002140 qd_idx = 0;
2141 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002142 (*dd_idx) += 2; /* D D P Q D */
2143 break;
2144 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002145 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002146 qd_idx = (pd_idx + 1) % raid_disks;
2147 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002148 break;
2149 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002150 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002151 qd_idx = (pd_idx + 1) % raid_disks;
2152 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002153 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002154
2155 case ALGORITHM_PARITY_0:
2156 pd_idx = 0;
2157 qd_idx = 1;
2158 (*dd_idx) += 2;
2159 break;
2160 case ALGORITHM_PARITY_N:
2161 pd_idx = data_disks;
2162 qd_idx = data_disks + 1;
2163 break;
2164
2165 case ALGORITHM_ROTATING_ZERO_RESTART:
2166 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2167 * of blocks for computing Q is different.
2168 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002169 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002170 qd_idx = pd_idx + 1;
2171 if (pd_idx == raid_disks-1) {
2172 (*dd_idx)++; /* Q D D D P */
2173 qd_idx = 0;
2174 } else if (*dd_idx >= pd_idx)
2175 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002176 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002177 break;
2178
2179 case ALGORITHM_ROTATING_N_RESTART:
2180 /* Same a left_asymmetric, by first stripe is
2181 * D D D P Q rather than
2182 * Q D D D P
2183 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002184 stripe2 += 1;
2185 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002186 qd_idx = pd_idx + 1;
2187 if (pd_idx == raid_disks-1) {
2188 (*dd_idx)++; /* Q D D D P */
2189 qd_idx = 0;
2190 } else if (*dd_idx >= pd_idx)
2191 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002192 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002193 break;
2194
2195 case ALGORITHM_ROTATING_N_CONTINUE:
2196 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002197 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002198 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2199 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002200 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002201 break;
2202
2203 case ALGORITHM_LEFT_ASYMMETRIC_6:
2204 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002205 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002206 if (*dd_idx >= pd_idx)
2207 (*dd_idx)++;
2208 qd_idx = raid_disks - 1;
2209 break;
2210
2211 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002212 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002213 if (*dd_idx >= pd_idx)
2214 (*dd_idx)++;
2215 qd_idx = raid_disks - 1;
2216 break;
2217
2218 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002219 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002220 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2221 qd_idx = raid_disks - 1;
2222 break;
2223
2224 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002225 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002226 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2227 qd_idx = raid_disks - 1;
2228 break;
2229
2230 case ALGORITHM_PARITY_0_6:
2231 pd_idx = 0;
2232 (*dd_idx)++;
2233 qd_idx = raid_disks - 1;
2234 break;
2235
NeilBrown16a53ec2006-06-26 00:27:38 -07002236 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002237 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002238 }
2239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241
NeilBrown911d4ee2009-03-31 14:39:38 +11002242 if (sh) {
2243 sh->pd_idx = pd_idx;
2244 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002245 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /*
2248 * Finally, compute the new sector number
2249 */
2250 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2251 return new_sector;
2252}
2253
2254
NeilBrown784052e2009-03-31 15:19:07 +11002255static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
NeilBrownd1688a62011-10-11 16:49:52 +11002257 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002258 int raid_disks = sh->disks;
2259 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002261 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2262 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002263 int algorithm = previous ? conf->prev_algo
2264 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 sector_t stripe;
2266 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002267 sector_t chunk_number;
2268 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002270 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
NeilBrown16a53ec2006-06-26 00:27:38 -07002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2274 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
NeilBrown16a53ec2006-06-26 00:27:38 -07002276 if (i == sh->pd_idx)
2277 return 0;
2278 switch(conf->level) {
2279 case 4: break;
2280 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002281 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 case ALGORITHM_LEFT_ASYMMETRIC:
2283 case ALGORITHM_RIGHT_ASYMMETRIC:
2284 if (i > sh->pd_idx)
2285 i--;
2286 break;
2287 case ALGORITHM_LEFT_SYMMETRIC:
2288 case ALGORITHM_RIGHT_SYMMETRIC:
2289 if (i < sh->pd_idx)
2290 i += raid_disks;
2291 i -= (sh->pd_idx + 1);
2292 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002293 case ALGORITHM_PARITY_0:
2294 i -= 1;
2295 break;
2296 case ALGORITHM_PARITY_N:
2297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002299 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002300 }
2301 break;
2302 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002303 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002304 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002305 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002306 case ALGORITHM_LEFT_ASYMMETRIC:
2307 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002308 case ALGORITHM_ROTATING_ZERO_RESTART:
2309 case ALGORITHM_ROTATING_N_RESTART:
2310 if (sh->pd_idx == raid_disks-1)
2311 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002312 else if (i > sh->pd_idx)
2313 i -= 2; /* D D P Q D */
2314 break;
2315 case ALGORITHM_LEFT_SYMMETRIC:
2316 case ALGORITHM_RIGHT_SYMMETRIC:
2317 if (sh->pd_idx == raid_disks-1)
2318 i--; /* Q D D D P */
2319 else {
2320 /* D D P Q D */
2321 if (i < sh->pd_idx)
2322 i += raid_disks;
2323 i -= (sh->pd_idx + 2);
2324 }
2325 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002326 case ALGORITHM_PARITY_0:
2327 i -= 2;
2328 break;
2329 case ALGORITHM_PARITY_N:
2330 break;
2331 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002332 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002333 if (sh->pd_idx == 0)
2334 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002335 else {
2336 /* D D Q P D */
2337 if (i < sh->pd_idx)
2338 i += raid_disks;
2339 i -= (sh->pd_idx + 1);
2340 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002341 break;
2342 case ALGORITHM_LEFT_ASYMMETRIC_6:
2343 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2344 if (i > sh->pd_idx)
2345 i--;
2346 break;
2347 case ALGORITHM_LEFT_SYMMETRIC_6:
2348 case ALGORITHM_RIGHT_SYMMETRIC_6:
2349 if (i < sh->pd_idx)
2350 i += data_disks + 1;
2351 i -= (sh->pd_idx + 1);
2352 break;
2353 case ALGORITHM_PARITY_0_6:
2354 i -= 1;
2355 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002356 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002357 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002358 }
2359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361
2362 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002363 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
NeilBrown112bf892009-03-31 14:39:38 +11002365 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002366 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002367 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2368 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002369 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2370 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return 0;
2372 }
2373 return r_sector;
2374}
2375
2376
Dan Williams600aa102008-06-28 08:32:05 +10002377static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002378schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002379 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002380{
2381 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002382 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002383 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002384
Dan Williamse33129d2007-01-02 13:52:30 -07002385 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002386
2387 for (i = disks; i--; ) {
2388 struct r5dev *dev = &sh->dev[i];
2389
2390 if (dev->towrite) {
2391 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002392 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002393 if (!expand)
2394 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002395 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002396 }
2397 }
NeilBrownce7d3632013-03-04 12:37:14 +11002398 /* if we are not expanding this is a proper write request, and
2399 * there will be bios with new data to be drained into the
2400 * stripe cache
2401 */
2402 if (!expand) {
2403 if (!s->locked)
2404 /* False alarm, nothing to do */
2405 return;
2406 sh->reconstruct_state = reconstruct_state_drain_run;
2407 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2408 } else
2409 sh->reconstruct_state = reconstruct_state_run;
2410
2411 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2412
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002413 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002414 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002415 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002416 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002417 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002418 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2419 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2420
Dan Williamse33129d2007-01-02 13:52:30 -07002421 for (i = disks; i--; ) {
2422 struct r5dev *dev = &sh->dev[i];
2423 if (i == pd_idx)
2424 continue;
2425
Dan Williamse33129d2007-01-02 13:52:30 -07002426 if (dev->towrite &&
2427 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002428 test_bit(R5_Wantcompute, &dev->flags))) {
2429 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002430 set_bit(R5_LOCKED, &dev->flags);
2431 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002432 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002433 }
2434 }
NeilBrownce7d3632013-03-04 12:37:14 +11002435 if (!s->locked)
2436 /* False alarm - nothing to do */
2437 return;
2438 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2439 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2440 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2441 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002442 }
2443
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002444 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002445 * are in flight
2446 */
2447 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2448 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002449 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002450
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002451 if (level == 6) {
2452 int qd_idx = sh->qd_idx;
2453 struct r5dev *dev = &sh->dev[qd_idx];
2454
2455 set_bit(R5_LOCKED, &dev->flags);
2456 clear_bit(R5_UPTODATE, &dev->flags);
2457 s->locked++;
2458 }
2459
Dan Williams600aa102008-06-28 08:32:05 +10002460 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002461 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002462 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002463}
NeilBrown16a53ec2006-06-26 00:27:38 -07002464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465/*
2466 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002467 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 * The bi_next chain must be in order.
2469 */
2470static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2471{
2472 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002473 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002474 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
NeilBrowncbe47ec2011-07-26 11:20:35 +10002476 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 (unsigned long long)bi->bi_sector,
2478 (unsigned long long)sh->sector);
2479
Shaohua Lib17459c2012-07-19 16:01:31 +10002480 /*
2481 * If several bio share a stripe. The bio bi_phys_segments acts as a
2482 * reference count to avoid race. The reference count should already be
2483 * increased before this function is called (for example, in
2484 * make_request()), so other bio sharing this stripe will not free the
2485 * stripe. If a stripe is owned by one stripe, the stripe lock will
2486 * protect it.
2487 */
2488 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002489 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002491 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002492 firstwrite = 1;
2493 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 bip = &sh->dev[dd_idx].toread;
2495 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002496 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 goto overlap;
2498 bip = & (*bip)->bi_next;
2499 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002500 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 goto overlap;
2502
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002503 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (*bip)
2505 bi->bi_next = *bip;
2506 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002507 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (forwrite) {
2510 /* check if page is covered */
2511 sector_t sector = sh->dev[dd_idx].sector;
2512 for (bi=sh->dev[dd_idx].towrite;
2513 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2514 bi && bi->bi_sector <= sector;
2515 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002516 if (bio_end_sector(bi) >= sector)
2517 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
2519 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2520 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2521 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002522
2523 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2524 (unsigned long long)(*bip)->bi_sector,
2525 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002526 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002527
2528 if (conf->mddev->bitmap && firstwrite) {
2529 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2530 STRIPE_SECTORS, 0);
2531 sh->bm_seq = conf->seq_flush+1;
2532 set_bit(STRIPE_BIT_DELAY, &sh->state);
2533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return 1;
2535
2536 overlap:
2537 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002538 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return 0;
2540}
2541
NeilBrownd1688a62011-10-11 16:49:52 +11002542static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002543
NeilBrownd1688a62011-10-11 16:49:52 +11002544static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002545 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002546{
NeilBrown784052e2009-03-31 15:19:07 +11002547 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002548 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002549 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002550 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002551 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002552
NeilBrown112bf892009-03-31 14:39:38 +11002553 raid5_compute_sector(conf,
2554 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002555 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002556 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002557 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002558}
2559
Dan Williamsa4456852007-07-09 11:56:43 -07002560static void
NeilBrownd1688a62011-10-11 16:49:52 +11002561handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002562 struct stripe_head_state *s, int disks,
2563 struct bio **return_bi)
2564{
2565 int i;
2566 for (i = disks; i--; ) {
2567 struct bio *bi;
2568 int bitmap_end = 0;
2569
2570 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002571 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002572 rcu_read_lock();
2573 rdev = rcu_dereference(conf->disks[i].rdev);
2574 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002575 atomic_inc(&rdev->nr_pending);
2576 else
2577 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002578 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002579 if (rdev) {
2580 if (!rdev_set_badblocks(
2581 rdev,
2582 sh->sector,
2583 STRIPE_SECTORS, 0))
2584 md_error(conf->mddev, rdev);
2585 rdev_dec_pending(rdev, conf->mddev);
2586 }
Dan Williamsa4456852007-07-09 11:56:43 -07002587 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002588 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002589 /* fail all writes first */
2590 bi = sh->dev[i].towrite;
2591 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002592 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002593 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002594 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002595
2596 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2597 wake_up(&conf->wait_for_overlap);
2598
2599 while (bi && bi->bi_sector <
2600 sh->dev[i].sector + STRIPE_SECTORS) {
2601 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2602 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002603 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002604 md_write_end(conf->mddev);
2605 bi->bi_next = *return_bi;
2606 *return_bi = bi;
2607 }
2608 bi = nextbi;
2609 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002610 if (bitmap_end)
2611 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2612 STRIPE_SECTORS, 0, 0);
2613 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002614 /* and fail all 'written' */
2615 bi = sh->dev[i].written;
2616 sh->dev[i].written = NULL;
2617 if (bi) bitmap_end = 1;
2618 while (bi && bi->bi_sector <
2619 sh->dev[i].sector + STRIPE_SECTORS) {
2620 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2621 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002622 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002623 md_write_end(conf->mddev);
2624 bi->bi_next = *return_bi;
2625 *return_bi = bi;
2626 }
2627 bi = bi2;
2628 }
2629
Dan Williamsb5e98d62007-01-02 13:52:31 -07002630 /* fail any reads if this device is non-operational and
2631 * the data has not reached the cache yet.
2632 */
2633 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2634 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2635 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002636 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002637 bi = sh->dev[i].toread;
2638 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002639 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002640 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2641 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002642 while (bi && bi->bi_sector <
2643 sh->dev[i].sector + STRIPE_SECTORS) {
2644 struct bio *nextbi =
2645 r5_next_bio(bi, sh->dev[i].sector);
2646 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002647 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002648 bi->bi_next = *return_bi;
2649 *return_bi = bi;
2650 }
2651 bi = nextbi;
2652 }
2653 }
Dan Williamsa4456852007-07-09 11:56:43 -07002654 if (bitmap_end)
2655 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2656 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002657 /* If we were in the middle of a write the parity block might
2658 * still be locked - so just clear all R5_LOCKED flags
2659 */
2660 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002661 }
2662
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002663 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2664 if (atomic_dec_and_test(&conf->pending_full_writes))
2665 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002666}
2667
NeilBrown7f0da592011-07-28 11:39:22 +10002668static void
NeilBrownd1688a62011-10-11 16:49:52 +11002669handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002670 struct stripe_head_state *s)
2671{
2672 int abort = 0;
2673 int i;
2674
NeilBrown7f0da592011-07-28 11:39:22 +10002675 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002676 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2677 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002678 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002679 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002680 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002681 * Don't even need to abort as that is handled elsewhere
2682 * if needed, and not always wanted e.g. if there is a known
2683 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002684 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002685 * non-sync devices, or abort the recovery
2686 */
NeilBrown18b98372012-04-01 23:48:38 +10002687 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2688 /* During recovery devices cannot be removed, so
2689 * locking and refcounting of rdevs is not needed
2690 */
2691 for (i = 0; i < conf->raid_disks; i++) {
2692 struct md_rdev *rdev = conf->disks[i].rdev;
2693 if (rdev
2694 && !test_bit(Faulty, &rdev->flags)
2695 && !test_bit(In_sync, &rdev->flags)
2696 && !rdev_set_badblocks(rdev, sh->sector,
2697 STRIPE_SECTORS, 0))
2698 abort = 1;
2699 rdev = conf->disks[i].replacement;
2700 if (rdev
2701 && !test_bit(Faulty, &rdev->flags)
2702 && !test_bit(In_sync, &rdev->flags)
2703 && !rdev_set_badblocks(rdev, sh->sector,
2704 STRIPE_SECTORS, 0))
2705 abort = 1;
2706 }
2707 if (abort)
2708 conf->recovery_disabled =
2709 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002710 }
NeilBrown18b98372012-04-01 23:48:38 +10002711 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002712}
2713
NeilBrown9a3e1102011-12-23 10:17:53 +11002714static int want_replace(struct stripe_head *sh, int disk_idx)
2715{
2716 struct md_rdev *rdev;
2717 int rv = 0;
2718 /* Doing recovery so rcu locking not required */
2719 rdev = sh->raid_conf->disks[disk_idx].replacement;
2720 if (rdev
2721 && !test_bit(Faulty, &rdev->flags)
2722 && !test_bit(In_sync, &rdev->flags)
2723 && (rdev->recovery_offset <= sh->sector
2724 || rdev->mddev->recovery_cp <= sh->sector))
2725 rv = 1;
2726
2727 return rv;
2728}
2729
NeilBrown93b3dbc2011-07-27 11:00:36 +10002730/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002731 * to be read or computed to satisfy a request.
2732 *
2733 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002734 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002735 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002736static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2737 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002738{
2739 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002740 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2741 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002742
Dan Williamsf38e1212007-01-02 13:52:30 -07002743 /* is the data in this block needed, and can we get it? */
2744 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002745 !test_bit(R5_UPTODATE, &dev->flags) &&
2746 (dev->toread ||
2747 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2748 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002749 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002750 (s->failed >= 1 && fdev[0]->toread) ||
2751 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002752 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2753 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2754 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002755 /* we would like to get this block, possibly by computing it,
2756 * otherwise read it if the backing disk is insync
2757 */
2758 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2759 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2760 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002761 (s->failed && (disk_idx == s->failed_num[0] ||
2762 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002763 /* have disk failed, and we're requested to fetch it;
2764 * do compute it
2765 */
2766 pr_debug("Computing stripe %llu block %d\n",
2767 (unsigned long long)sh->sector, disk_idx);
2768 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2769 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2770 set_bit(R5_Wantcompute, &dev->flags);
2771 sh->ops.target = disk_idx;
2772 sh->ops.target2 = -1; /* no 2nd target */
2773 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002774 /* Careful: from this point on 'uptodate' is in the eye
2775 * of raid_run_ops which services 'compute' operations
2776 * before writes. R5_Wantcompute flags a block that will
2777 * be R5_UPTODATE by the time it is needed for a
2778 * subsequent operation.
2779 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002780 s->uptodate++;
2781 return 1;
2782 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2783 /* Computing 2-failure is *very* expensive; only
2784 * do it if failed >= 2
2785 */
2786 int other;
2787 for (other = disks; other--; ) {
2788 if (other == disk_idx)
2789 continue;
2790 if (!test_bit(R5_UPTODATE,
2791 &sh->dev[other].flags))
2792 break;
2793 }
2794 BUG_ON(other < 0);
2795 pr_debug("Computing stripe %llu blocks %d,%d\n",
2796 (unsigned long long)sh->sector,
2797 disk_idx, other);
2798 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2799 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2800 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2801 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2802 sh->ops.target = disk_idx;
2803 sh->ops.target2 = other;
2804 s->uptodate += 2;
2805 s->req_compute = 1;
2806 return 1;
2807 } else if (test_bit(R5_Insync, &dev->flags)) {
2808 set_bit(R5_LOCKED, &dev->flags);
2809 set_bit(R5_Wantread, &dev->flags);
2810 s->locked++;
2811 pr_debug("Reading block %d (sync=%d)\n",
2812 disk_idx, s->syncing);
2813 }
2814 }
2815
2816 return 0;
2817}
2818
2819/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002820 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002821 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002822static void handle_stripe_fill(struct stripe_head *sh,
2823 struct stripe_head_state *s,
2824 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002825{
2826 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002827
2828 /* look for blocks to read/compute, skip this if a compute
2829 * is already in flight, or if the stripe contents are in the
2830 * midst of changing due to a write
2831 */
2832 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2833 !sh->reconstruct_state)
2834 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002835 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002836 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002837 set_bit(STRIPE_HANDLE, &sh->state);
2838}
2839
2840
Dan Williams1fe797e2008-06-28 09:16:30 +10002841/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002842 * any written block on an uptodate or failed drive can be returned.
2843 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2844 * never LOCKED, so we don't need to test 'failed' directly.
2845 */
NeilBrownd1688a62011-10-11 16:49:52 +11002846static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002847 struct stripe_head *sh, int disks, struct bio **return_bi)
2848{
2849 int i;
2850 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002851 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002852
2853 for (i = disks; i--; )
2854 if (sh->dev[i].written) {
2855 dev = &sh->dev[i];
2856 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002857 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002858 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002859 /* We can return any write requests */
2860 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002861 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002862 if (test_and_clear_bit(R5_Discard, &dev->flags))
2863 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002864 wbi = dev->written;
2865 dev->written = NULL;
2866 while (wbi && wbi->bi_sector <
2867 dev->sector + STRIPE_SECTORS) {
2868 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002869 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002870 md_write_end(conf->mddev);
2871 wbi->bi_next = *return_bi;
2872 *return_bi = wbi;
2873 }
2874 wbi = wbi2;
2875 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002876 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2877 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002878 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002879 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002880 } else if (test_bit(R5_Discard, &dev->flags))
2881 discard_pending = 1;
2882 }
2883 if (!discard_pending &&
2884 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2885 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2886 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2887 if (sh->qd_idx >= 0) {
2888 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2889 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2890 }
2891 /* now that discard is done we can proceed with any sync */
2892 clear_bit(STRIPE_DISCARD, &sh->state);
2893 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2894 set_bit(STRIPE_HANDLE, &sh->state);
2895
2896 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002897
2898 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2899 if (atomic_dec_and_test(&conf->pending_full_writes))
2900 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002901}
2902
NeilBrownd1688a62011-10-11 16:49:52 +11002903static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002904 struct stripe_head *sh,
2905 struct stripe_head_state *s,
2906 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002907{
2908 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002909 sector_t recovery_cp = conf->mddev->recovery_cp;
2910
2911 /* RAID6 requires 'rcw' in current implementation.
2912 * Otherwise, check whether resync is now happening or should start.
2913 * If yes, then the array is dirty (after unclean shutdown or
2914 * initial creation), so parity in some stripes might be inconsistent.
2915 * In this case, we need to always do reconstruct-write, to ensure
2916 * that in case of drive failure or read-error correction, we
2917 * generate correct data from the parity.
2918 */
2919 if (conf->max_degraded == 2 ||
2920 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2921 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002922 * look like rcw is cheaper
2923 */
2924 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002925 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2926 conf->max_degraded, (unsigned long long)recovery_cp,
2927 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002928 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002929 /* would I have to read this buffer for read_modify_write */
2930 struct r5dev *dev = &sh->dev[i];
2931 if ((dev->towrite || i == sh->pd_idx) &&
2932 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002933 !(test_bit(R5_UPTODATE, &dev->flags) ||
2934 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002935 if (test_bit(R5_Insync, &dev->flags))
2936 rmw++;
2937 else
2938 rmw += 2*disks; /* cannot read it */
2939 }
2940 /* Would I have to read this buffer for reconstruct_write */
2941 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2942 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002943 !(test_bit(R5_UPTODATE, &dev->flags) ||
2944 test_bit(R5_Wantcompute, &dev->flags))) {
2945 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002946 else
2947 rcw += 2*disks;
2948 }
2949 }
Dan Williams45b42332007-07-09 11:56:43 -07002950 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002951 (unsigned long long)sh->sector, rmw, rcw);
2952 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002953 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002954 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002955 if (conf->mddev->queue)
2956 blk_add_trace_msg(conf->mddev->queue,
2957 "raid5 rmw %llu %d",
2958 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002959 for (i = disks; i--; ) {
2960 struct r5dev *dev = &sh->dev[i];
2961 if ((dev->towrite || i == sh->pd_idx) &&
2962 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002963 !(test_bit(R5_UPTODATE, &dev->flags) ||
2964 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002965 test_bit(R5_Insync, &dev->flags)) {
2966 if (
2967 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002968 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002969 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002970 set_bit(R5_LOCKED, &dev->flags);
2971 set_bit(R5_Wantread, &dev->flags);
2972 s->locked++;
2973 } else {
2974 set_bit(STRIPE_DELAYED, &sh->state);
2975 set_bit(STRIPE_HANDLE, &sh->state);
2976 }
2977 }
2978 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002979 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002980 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002981 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002982 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002983 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002984 for (i = disks; i--; ) {
2985 struct r5dev *dev = &sh->dev[i];
2986 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002987 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002988 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002989 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002990 test_bit(R5_Wantcompute, &dev->flags))) {
2991 rcw++;
2992 if (!test_bit(R5_Insync, &dev->flags))
2993 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002994 if (
2995 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002996 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002997 "%d for Reconstruct\n", i);
2998 set_bit(R5_LOCKED, &dev->flags);
2999 set_bit(R5_Wantread, &dev->flags);
3000 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003001 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003002 } else {
3003 set_bit(STRIPE_DELAYED, &sh->state);
3004 set_bit(STRIPE_HANDLE, &sh->state);
3005 }
3006 }
3007 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003008 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003009 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3010 (unsigned long long)sh->sector,
3011 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003012 }
Dan Williamsa4456852007-07-09 11:56:43 -07003013 /* now if nothing is locked, and if we have enough data,
3014 * we can start a write request
3015 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003016 /* since handle_stripe can be called at any time we need to handle the
3017 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003018 * subsequent call wants to start a write request. raid_run_ops only
3019 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003020 * simultaneously. If this is not the case then new writes need to be
3021 * held off until the compute completes.
3022 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003023 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3024 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3025 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003026 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003027}
3028
NeilBrownd1688a62011-10-11 16:49:52 +11003029static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003030 struct stripe_head_state *s, int disks)
3031{
Dan Williamsecc65c92008-06-28 08:31:57 +10003032 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003033
Dan Williamsbd2ab672008-04-10 21:29:27 -07003034 set_bit(STRIPE_HANDLE, &sh->state);
3035
Dan Williamsecc65c92008-06-28 08:31:57 +10003036 switch (sh->check_state) {
3037 case check_state_idle:
3038 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003039 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003040 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003041 sh->check_state = check_state_run;
3042 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003043 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003044 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003045 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003046 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003047 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003048 /* fall through */
3049 case check_state_compute_result:
3050 sh->check_state = check_state_idle;
3051 if (!dev)
3052 dev = &sh->dev[sh->pd_idx];
3053
3054 /* check that a write has not made the stripe insync */
3055 if (test_bit(STRIPE_INSYNC, &sh->state))
3056 break;
3057
3058 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003059 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3060 BUG_ON(s->uptodate != disks);
3061
3062 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003063 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003064 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003065
Dan Williamsa4456852007-07-09 11:56:43 -07003066 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003067 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003068 break;
3069 case check_state_run:
3070 break; /* we will be called again upon completion */
3071 case check_state_check_result:
3072 sh->check_state = check_state_idle;
3073
3074 /* if a failure occurred during the check operation, leave
3075 * STRIPE_INSYNC not set and let the stripe be handled again
3076 */
3077 if (s->failed)
3078 break;
3079
3080 /* handle a successful check operation, if parity is correct
3081 * we are done. Otherwise update the mismatch count and repair
3082 * parity if !MD_RECOVERY_CHECK
3083 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003084 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003085 /* parity is correct (on disc,
3086 * not in buffer any more)
3087 */
3088 set_bit(STRIPE_INSYNC, &sh->state);
3089 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003090 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003091 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3092 /* don't try to repair!! */
3093 set_bit(STRIPE_INSYNC, &sh->state);
3094 else {
3095 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003096 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003097 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3098 set_bit(R5_Wantcompute,
3099 &sh->dev[sh->pd_idx].flags);
3100 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003101 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003102 s->uptodate++;
3103 }
3104 }
3105 break;
3106 case check_state_compute_run:
3107 break;
3108 default:
3109 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3110 __func__, sh->check_state,
3111 (unsigned long long) sh->sector);
3112 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003113 }
3114}
3115
3116
NeilBrownd1688a62011-10-11 16:49:52 +11003117static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003118 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003119 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003120{
Dan Williamsa4456852007-07-09 11:56:43 -07003121 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003122 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003123 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003124
3125 set_bit(STRIPE_HANDLE, &sh->state);
3126
3127 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003128
Dan Williamsa4456852007-07-09 11:56:43 -07003129 /* Want to check and possibly repair P and Q.
3130 * However there could be one 'failed' device, in which
3131 * case we can only check one of them, possibly using the
3132 * other to generate missing data
3133 */
3134
Dan Williamsd82dfee2009-07-14 13:40:57 -07003135 switch (sh->check_state) {
3136 case check_state_idle:
3137 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003138 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003139 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003140 * makes sense to check P (If anything else were failed,
3141 * we would have used P to recreate it).
3142 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003143 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003144 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003145 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003146 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003147 * anything, so it makes sense to check it
3148 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003149 if (sh->check_state == check_state_run)
3150 sh->check_state = check_state_run_pq;
3151 else
3152 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003153 }
Dan Williams36d1c642009-07-14 11:48:22 -07003154
Dan Williamsd82dfee2009-07-14 13:40:57 -07003155 /* discard potentially stale zero_sum_result */
3156 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003157
Dan Williamsd82dfee2009-07-14 13:40:57 -07003158 if (sh->check_state == check_state_run) {
3159 /* async_xor_zero_sum destroys the contents of P */
3160 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3161 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003162 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003163 if (sh->check_state >= check_state_run &&
3164 sh->check_state <= check_state_run_pq) {
3165 /* async_syndrome_zero_sum preserves P and Q, so
3166 * no need to mark them !uptodate here
3167 */
3168 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3169 break;
3170 }
Dan Williams36d1c642009-07-14 11:48:22 -07003171
Dan Williamsd82dfee2009-07-14 13:40:57 -07003172 /* we have 2-disk failure */
3173 BUG_ON(s->failed != 2);
3174 /* fall through */
3175 case check_state_compute_result:
3176 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003177
Dan Williamsd82dfee2009-07-14 13:40:57 -07003178 /* check that a write has not made the stripe insync */
3179 if (test_bit(STRIPE_INSYNC, &sh->state))
3180 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003181
3182 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003183 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003184 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003185 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003186 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003187 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003188 s->locked++;
3189 set_bit(R5_LOCKED, &dev->flags);
3190 set_bit(R5_Wantwrite, &dev->flags);
3191 }
3192 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003193 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003194 s->locked++;
3195 set_bit(R5_LOCKED, &dev->flags);
3196 set_bit(R5_Wantwrite, &dev->flags);
3197 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003198 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003199 dev = &sh->dev[pd_idx];
3200 s->locked++;
3201 set_bit(R5_LOCKED, &dev->flags);
3202 set_bit(R5_Wantwrite, &dev->flags);
3203 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003204 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003205 dev = &sh->dev[qd_idx];
3206 s->locked++;
3207 set_bit(R5_LOCKED, &dev->flags);
3208 set_bit(R5_Wantwrite, &dev->flags);
3209 }
3210 clear_bit(STRIPE_DEGRADED, &sh->state);
3211
3212 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003213 break;
3214 case check_state_run:
3215 case check_state_run_q:
3216 case check_state_run_pq:
3217 break; /* we will be called again upon completion */
3218 case check_state_check_result:
3219 sh->check_state = check_state_idle;
3220
3221 /* handle a successful check operation, if parity is correct
3222 * we are done. Otherwise update the mismatch count and repair
3223 * parity if !MD_RECOVERY_CHECK
3224 */
3225 if (sh->ops.zero_sum_result == 0) {
3226 /* both parities are correct */
3227 if (!s->failed)
3228 set_bit(STRIPE_INSYNC, &sh->state);
3229 else {
3230 /* in contrast to the raid5 case we can validate
3231 * parity, but still have a failure to write
3232 * back
3233 */
3234 sh->check_state = check_state_compute_result;
3235 /* Returning at this point means that we may go
3236 * off and bring p and/or q uptodate again so
3237 * we make sure to check zero_sum_result again
3238 * to verify if p or q need writeback
3239 */
3240 }
3241 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003242 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003243 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3244 /* don't try to repair!! */
3245 set_bit(STRIPE_INSYNC, &sh->state);
3246 else {
3247 int *target = &sh->ops.target;
3248
3249 sh->ops.target = -1;
3250 sh->ops.target2 = -1;
3251 sh->check_state = check_state_compute_run;
3252 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3253 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3254 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3255 set_bit(R5_Wantcompute,
3256 &sh->dev[pd_idx].flags);
3257 *target = pd_idx;
3258 target = &sh->ops.target2;
3259 s->uptodate++;
3260 }
3261 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3262 set_bit(R5_Wantcompute,
3263 &sh->dev[qd_idx].flags);
3264 *target = qd_idx;
3265 s->uptodate++;
3266 }
3267 }
3268 }
3269 break;
3270 case check_state_compute_run:
3271 break;
3272 default:
3273 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3274 __func__, sh->check_state,
3275 (unsigned long long) sh->sector);
3276 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003277 }
3278}
3279
NeilBrownd1688a62011-10-11 16:49:52 +11003280static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003281{
3282 int i;
3283
3284 /* We have read all the blocks in this stripe and now we need to
3285 * copy some of them into a target stripe for expand.
3286 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003287 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003288 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3289 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003290 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003291 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003292 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003293 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003294
NeilBrown784052e2009-03-31 15:19:07 +11003295 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003296 sector_t s = raid5_compute_sector(conf, bn, 0,
3297 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003298 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003299 if (sh2 == NULL)
3300 /* so far only the early blocks of this stripe
3301 * have been requested. When later blocks
3302 * get requested, we will try again
3303 */
3304 continue;
3305 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3306 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3307 /* must have already done this block */
3308 release_stripe(sh2);
3309 continue;
3310 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003311
3312 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003313 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003314 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003315 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003316 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003317
Dan Williamsa4456852007-07-09 11:56:43 -07003318 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3319 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3320 for (j = 0; j < conf->raid_disks; j++)
3321 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003322 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003323 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3324 break;
3325 if (j == conf->raid_disks) {
3326 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3327 set_bit(STRIPE_HANDLE, &sh2->state);
3328 }
3329 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003330
Dan Williamsa4456852007-07-09 11:56:43 -07003331 }
NeilBrowna2e08552007-09-11 15:23:36 -07003332 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003333 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003334}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336/*
3337 * handle_stripe - do things to a stripe.
3338 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003339 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3340 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003342 * return some read requests which now have data
3343 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 * schedule a read on some buffers
3345 * schedule a write of some buffers
3346 * return confirmation of parity correctness
3347 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 */
Dan Williamsa4456852007-07-09 11:56:43 -07003349
NeilBrownacfe7262011-07-27 11:00:36 +10003350static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003351{
NeilBrownd1688a62011-10-11 16:49:52 +11003352 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003353 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003354 struct r5dev *dev;
3355 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003356 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003357
NeilBrownacfe7262011-07-27 11:00:36 +10003358 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003359
NeilBrownacfe7262011-07-27 11:00:36 +10003360 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3361 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3362 s->failed_num[0] = -1;
3363 s->failed_num[1] = -1;
3364
3365 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003366 rcu_read_lock();
3367 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003368 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003369 sector_t first_bad;
3370 int bad_sectors;
3371 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003372
NeilBrown16a53ec2006-06-26 00:27:38 -07003373 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003374
Dan Williams45b42332007-07-09 11:56:43 -07003375 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003376 i, dev->flags,
3377 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003378 /* maybe we can reply to a read
3379 *
3380 * new wantfill requests are only permitted while
3381 * ops_complete_biofill is guaranteed to be inactive
3382 */
3383 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3384 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3385 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003386
3387 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003388 if (test_bit(R5_LOCKED, &dev->flags))
3389 s->locked++;
3390 if (test_bit(R5_UPTODATE, &dev->flags))
3391 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003392 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003393 s->compute++;
3394 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003395 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003396
NeilBrownacfe7262011-07-27 11:00:36 +10003397 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003398 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003399 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003400 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003401 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003402 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003403 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003404 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003405 }
Dan Williamsa4456852007-07-09 11:56:43 -07003406 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003407 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003408 /* Prefer to use the replacement for reads, but only
3409 * if it is recovered enough and has no bad blocks.
3410 */
3411 rdev = rcu_dereference(conf->disks[i].replacement);
3412 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3413 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3414 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3415 &first_bad, &bad_sectors))
3416 set_bit(R5_ReadRepl, &dev->flags);
3417 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003418 if (rdev)
3419 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003420 rdev = rcu_dereference(conf->disks[i].rdev);
3421 clear_bit(R5_ReadRepl, &dev->flags);
3422 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003423 if (rdev && test_bit(Faulty, &rdev->flags))
3424 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003425 if (rdev) {
3426 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3427 &first_bad, &bad_sectors);
3428 if (s->blocked_rdev == NULL
3429 && (test_bit(Blocked, &rdev->flags)
3430 || is_bad < 0)) {
3431 if (is_bad < 0)
3432 set_bit(BlockedBadBlocks,
3433 &rdev->flags);
3434 s->blocked_rdev = rdev;
3435 atomic_inc(&rdev->nr_pending);
3436 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003437 }
NeilBrown415e72d2010-06-17 17:25:21 +10003438 clear_bit(R5_Insync, &dev->flags);
3439 if (!rdev)
3440 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003441 else if (is_bad) {
3442 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003443 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3444 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003445 /* treat as in-sync, but with a read error
3446 * which we can now try to correct
3447 */
3448 set_bit(R5_Insync, &dev->flags);
3449 set_bit(R5_ReadError, &dev->flags);
3450 }
3451 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003452 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003453 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003454 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003455 set_bit(R5_Insync, &dev->flags);
3456 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3457 test_bit(R5_Expanded, &dev->flags))
3458 /* If we've reshaped into here, we assume it is Insync.
3459 * We will shortly update recovery_offset to make
3460 * it official.
3461 */
3462 set_bit(R5_Insync, &dev->flags);
3463
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003464 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003465 /* This flag does not apply to '.replacement'
3466 * only to .rdev, so make sure to check that*/
3467 struct md_rdev *rdev2 = rcu_dereference(
3468 conf->disks[i].rdev);
3469 if (rdev2 == rdev)
3470 clear_bit(R5_Insync, &dev->flags);
3471 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003472 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003473 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003474 } else
3475 clear_bit(R5_WriteError, &dev->flags);
3476 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003477 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003478 /* This flag does not apply to '.replacement'
3479 * only to .rdev, so make sure to check that*/
3480 struct md_rdev *rdev2 = rcu_dereference(
3481 conf->disks[i].rdev);
3482 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003483 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003484 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003485 } else
3486 clear_bit(R5_MadeGood, &dev->flags);
3487 }
NeilBrown977df362011-12-23 10:17:53 +11003488 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3489 struct md_rdev *rdev2 = rcu_dereference(
3490 conf->disks[i].replacement);
3491 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3492 s->handle_bad_blocks = 1;
3493 atomic_inc(&rdev2->nr_pending);
3494 } else
3495 clear_bit(R5_MadeGoodRepl, &dev->flags);
3496 }
NeilBrown415e72d2010-06-17 17:25:21 +10003497 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003498 /* The ReadError flag will just be confusing now */
3499 clear_bit(R5_ReadError, &dev->flags);
3500 clear_bit(R5_ReWrite, &dev->flags);
3501 }
NeilBrown415e72d2010-06-17 17:25:21 +10003502 if (test_bit(R5_ReadError, &dev->flags))
3503 clear_bit(R5_Insync, &dev->flags);
3504 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003505 if (s->failed < 2)
3506 s->failed_num[s->failed] = i;
3507 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003508 if (rdev && !test_bit(Faulty, &rdev->flags))
3509 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003510 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003511 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003512 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3513 /* If there is a failed device being replaced,
3514 * we must be recovering.
3515 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003516 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003517 * else we can only be replacing
3518 * sync and recovery both need to read all devices, and so
3519 * use the same flag.
3520 */
3521 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003522 sh->sector >= conf->mddev->recovery_cp ||
3523 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003524 s->syncing = 1;
3525 else
3526 s->replacing = 1;
3527 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003528 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003529}
NeilBrownf4168852007-02-28 20:11:53 -08003530
NeilBrowncc940152011-07-26 11:35:35 +10003531static void handle_stripe(struct stripe_head *sh)
3532{
3533 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003534 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003535 int i;
NeilBrown84789552011-07-27 11:00:36 +10003536 int prexor;
3537 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003538 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003539
3540 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003541 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003542 /* already being handled, ensure it gets handled
3543 * again when current action finishes */
3544 set_bit(STRIPE_HANDLE, &sh->state);
3545 return;
3546 }
3547
NeilBrownf8dfcff2013-03-12 12:18:06 +11003548 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3549 spin_lock(&sh->stripe_lock);
3550 /* Cannot process 'sync' concurrently with 'discard' */
3551 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3552 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3553 set_bit(STRIPE_SYNCING, &sh->state);
3554 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003555 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003556 }
3557 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003558 }
3559 clear_bit(STRIPE_DELAYED, &sh->state);
3560
3561 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3562 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3563 (unsigned long long)sh->sector, sh->state,
3564 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3565 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003566
NeilBrownacfe7262011-07-27 11:00:36 +10003567 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003568
NeilBrownbc2607f2011-07-28 11:39:22 +10003569 if (s.handle_bad_blocks) {
3570 set_bit(STRIPE_HANDLE, &sh->state);
3571 goto finish;
3572 }
3573
NeilBrown474af965fe2011-07-27 11:00:36 +10003574 if (unlikely(s.blocked_rdev)) {
3575 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003576 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003577 set_bit(STRIPE_HANDLE, &sh->state);
3578 goto finish;
3579 }
3580 /* There is nothing for the blocked_rdev to block */
3581 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3582 s.blocked_rdev = NULL;
3583 }
3584
3585 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3586 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3587 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3588 }
3589
3590 pr_debug("locked=%d uptodate=%d to_read=%d"
3591 " to_write=%d failed=%d failed_num=%d,%d\n",
3592 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3593 s.failed_num[0], s.failed_num[1]);
3594 /* check if the array has lost more than max_degraded devices and,
3595 * if so, some requests might need to be failed.
3596 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003597 if (s.failed > conf->max_degraded) {
3598 sh->check_state = 0;
3599 sh->reconstruct_state = 0;
3600 if (s.to_read+s.to_write+s.written)
3601 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003602 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003603 handle_failed_sync(conf, sh, &s);
3604 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003605
NeilBrown84789552011-07-27 11:00:36 +10003606 /* Now we check to see if any write operations have recently
3607 * completed
3608 */
3609 prexor = 0;
3610 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3611 prexor = 1;
3612 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3613 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3614 sh->reconstruct_state = reconstruct_state_idle;
3615
3616 /* All the 'written' buffers and the parity block are ready to
3617 * be written back to disk
3618 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003619 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3620 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003621 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003622 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3623 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003624 for (i = disks; i--; ) {
3625 struct r5dev *dev = &sh->dev[i];
3626 if (test_bit(R5_LOCKED, &dev->flags) &&
3627 (i == sh->pd_idx || i == sh->qd_idx ||
3628 dev->written)) {
3629 pr_debug("Writing block %d\n", i);
3630 set_bit(R5_Wantwrite, &dev->flags);
3631 if (prexor)
3632 continue;
3633 if (!test_bit(R5_Insync, &dev->flags) ||
3634 ((i == sh->pd_idx || i == sh->qd_idx) &&
3635 s.failed == 0))
3636 set_bit(STRIPE_INSYNC, &sh->state);
3637 }
3638 }
3639 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3640 s.dec_preread_active = 1;
3641 }
3642
NeilBrownef5b7c62012-11-22 09:13:36 +11003643 /*
3644 * might be able to return some write requests if the parity blocks
3645 * are safe, or on a failed drive
3646 */
3647 pdev = &sh->dev[sh->pd_idx];
3648 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3649 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3650 qdev = &sh->dev[sh->qd_idx];
3651 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3652 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3653 || conf->level < 6;
3654
3655 if (s.written &&
3656 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3657 && !test_bit(R5_LOCKED, &pdev->flags)
3658 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3659 test_bit(R5_Discard, &pdev->flags))))) &&
3660 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3661 && !test_bit(R5_LOCKED, &qdev->flags)
3662 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3663 test_bit(R5_Discard, &qdev->flags))))))
3664 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3665
3666 /* Now we might consider reading some blocks, either to check/generate
3667 * parity, or to satisfy requests
3668 * or to load a block that is being partially written.
3669 */
3670 if (s.to_read || s.non_overwrite
3671 || (conf->level == 6 && s.to_write && s.failed)
3672 || (s.syncing && (s.uptodate + s.compute < disks))
3673 || s.replacing
3674 || s.expanding)
3675 handle_stripe_fill(sh, &s, disks);
3676
NeilBrown84789552011-07-27 11:00:36 +10003677 /* Now to consider new write requests and what else, if anything
3678 * should be read. We do not handle new writes when:
3679 * 1/ A 'write' operation (copy+xor) is already in flight.
3680 * 2/ A 'check' operation is in flight, as it may clobber the parity
3681 * block.
3682 */
3683 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3684 handle_stripe_dirtying(conf, sh, &s, disks);
3685
3686 /* maybe we need to check and possibly fix the parity for this stripe
3687 * Any reads will already have been scheduled, so we just see if enough
3688 * data is available. The parity check is held off while parity
3689 * dependent operations are in flight.
3690 */
3691 if (sh->check_state ||
3692 (s.syncing && s.locked == 0 &&
3693 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3694 !test_bit(STRIPE_INSYNC, &sh->state))) {
3695 if (conf->level == 6)
3696 handle_parity_checks6(conf, sh, &s, disks);
3697 else
3698 handle_parity_checks5(conf, sh, &s, disks);
3699 }
NeilBrownc5a31002011-07-27 11:00:36 +10003700
NeilBrownf94c0b62013-07-22 12:57:21 +10003701 if ((s.replacing || s.syncing) && s.locked == 0
3702 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3703 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003704 /* Write out to replacement devices where possible */
3705 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003706 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3707 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003708 set_bit(R5_WantReplace, &sh->dev[i].flags);
3709 set_bit(R5_LOCKED, &sh->dev[i].flags);
3710 s.locked++;
3711 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003712 if (s.replacing)
3713 set_bit(STRIPE_INSYNC, &sh->state);
3714 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003715 }
3716 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003717 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003718 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003719 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3720 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003721 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3722 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003723 }
3724
3725 /* If the failed drives are just a ReadError, then we might need
3726 * to progress the repair/check process
3727 */
3728 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3729 for (i = 0; i < s.failed; i++) {
3730 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3731 if (test_bit(R5_ReadError, &dev->flags)
3732 && !test_bit(R5_LOCKED, &dev->flags)
3733 && test_bit(R5_UPTODATE, &dev->flags)
3734 ) {
3735 if (!test_bit(R5_ReWrite, &dev->flags)) {
3736 set_bit(R5_Wantwrite, &dev->flags);
3737 set_bit(R5_ReWrite, &dev->flags);
3738 set_bit(R5_LOCKED, &dev->flags);
3739 s.locked++;
3740 } else {
3741 /* let's read it back */
3742 set_bit(R5_Wantread, &dev->flags);
3743 set_bit(R5_LOCKED, &dev->flags);
3744 s.locked++;
3745 }
3746 }
3747 }
3748
3749
NeilBrown3687c062011-07-27 11:00:36 +10003750 /* Finish reconstruct operations initiated by the expansion process */
3751 if (sh->reconstruct_state == reconstruct_state_result) {
3752 struct stripe_head *sh_src
3753 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3754 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3755 /* sh cannot be written until sh_src has been read.
3756 * so arrange for sh to be delayed a little
3757 */
3758 set_bit(STRIPE_DELAYED, &sh->state);
3759 set_bit(STRIPE_HANDLE, &sh->state);
3760 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3761 &sh_src->state))
3762 atomic_inc(&conf->preread_active_stripes);
3763 release_stripe(sh_src);
3764 goto finish;
3765 }
3766 if (sh_src)
3767 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003768
NeilBrown3687c062011-07-27 11:00:36 +10003769 sh->reconstruct_state = reconstruct_state_idle;
3770 clear_bit(STRIPE_EXPANDING, &sh->state);
3771 for (i = conf->raid_disks; i--; ) {
3772 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3773 set_bit(R5_LOCKED, &sh->dev[i].flags);
3774 s.locked++;
3775 }
3776 }
3777
3778 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3779 !sh->reconstruct_state) {
3780 /* Need to write out all blocks after computing parity */
3781 sh->disks = conf->raid_disks;
3782 stripe_set_idx(sh->sector, conf, 0, sh);
3783 schedule_reconstruction(sh, &s, 1, 1);
3784 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3785 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3786 atomic_dec(&conf->reshape_stripes);
3787 wake_up(&conf->wait_for_overlap);
3788 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3789 }
3790
3791 if (s.expanding && s.locked == 0 &&
3792 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3793 handle_stripe_expansion(conf, sh);
3794
3795finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003796 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003797 if (unlikely(s.blocked_rdev)) {
3798 if (conf->mddev->external)
3799 md_wait_for_blocked_rdev(s.blocked_rdev,
3800 conf->mddev);
3801 else
3802 /* Internal metadata will immediately
3803 * be written by raid5d, so we don't
3804 * need to wait here.
3805 */
3806 rdev_dec_pending(s.blocked_rdev,
3807 conf->mddev);
3808 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003809
NeilBrownbc2607f2011-07-28 11:39:22 +10003810 if (s.handle_bad_blocks)
3811 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003812 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003813 struct r5dev *dev = &sh->dev[i];
3814 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3815 /* We own a safe reference to the rdev */
3816 rdev = conf->disks[i].rdev;
3817 if (!rdev_set_badblocks(rdev, sh->sector,
3818 STRIPE_SECTORS, 0))
3819 md_error(conf->mddev, rdev);
3820 rdev_dec_pending(rdev, conf->mddev);
3821 }
NeilBrownb84db562011-07-28 11:39:23 +10003822 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3823 rdev = conf->disks[i].rdev;
3824 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003825 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003826 rdev_dec_pending(rdev, conf->mddev);
3827 }
NeilBrown977df362011-12-23 10:17:53 +11003828 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3829 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003830 if (!rdev)
3831 /* rdev have been moved down */
3832 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003833 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003834 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003835 rdev_dec_pending(rdev, conf->mddev);
3836 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003837 }
3838
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003839 if (s.ops_request)
3840 raid_run_ops(sh, s.ops_request);
3841
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003842 ops_run_io(sh, &s);
3843
NeilBrownc5709ef2011-07-26 11:35:20 +10003844 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003845 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003846 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003847 * have actually been submitted.
3848 */
3849 atomic_dec(&conf->preread_active_stripes);
3850 if (atomic_read(&conf->preread_active_stripes) <
3851 IO_THRESHOLD)
3852 md_wakeup_thread(conf->mddev->thread);
3853 }
3854
NeilBrownc5709ef2011-07-26 11:35:20 +10003855 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003856
Dan Williams257a4b42011-11-08 16:22:06 +11003857 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003858}
3859
NeilBrownd1688a62011-10-11 16:49:52 +11003860static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
3862 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3863 while (!list_empty(&conf->delayed_list)) {
3864 struct list_head *l = conf->delayed_list.next;
3865 struct stripe_head *sh;
3866 sh = list_entry(l, struct stripe_head, lru);
3867 list_del_init(l);
3868 clear_bit(STRIPE_DELAYED, &sh->state);
3869 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3870 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003871 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08003872 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 }
NeilBrown482c0832011-04-18 18:25:42 +10003874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875}
3876
NeilBrownd1688a62011-10-11 16:49:52 +11003877static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003878{
3879 /* device_lock is held */
3880 struct list_head head;
3881 list_add(&head, &conf->bitmap_list);
3882 list_del_init(&conf->bitmap_list);
3883 while (!list_empty(&head)) {
3884 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3885 list_del_init(&sh->lru);
3886 atomic_inc(&sh->count);
3887 __release_stripe(conf, sh);
3888 }
3889}
3890
NeilBrownfd01b882011-10-11 16:47:53 +11003891int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003892{
NeilBrownd1688a62011-10-11 16:49:52 +11003893 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003894
3895 /* No difference between reads and writes. Just check
3896 * how busy the stripe_cache is
3897 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003898
NeilBrownf022b2f2006-10-03 01:15:56 -07003899 if (conf->inactive_blocked)
3900 return 1;
3901 if (conf->quiesce)
3902 return 1;
3903 if (list_empty_careful(&conf->inactive_list))
3904 return 1;
3905
3906 return 0;
3907}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003908EXPORT_SYMBOL_GPL(md_raid5_congested);
3909
3910static int raid5_congested(void *data, int bits)
3911{
NeilBrownfd01b882011-10-11 16:47:53 +11003912 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003913
3914 return mddev_congested(mddev, bits) ||
3915 md_raid5_congested(mddev, bits);
3916}
NeilBrownf022b2f2006-10-03 01:15:56 -07003917
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003918/* We want read requests to align with chunks where possible,
3919 * but write requests don't need to.
3920 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003921static int raid5_mergeable_bvec(struct request_queue *q,
3922 struct bvec_merge_data *bvm,
3923 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003924{
NeilBrownfd01b882011-10-11 16:47:53 +11003925 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003926 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003927 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003928 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003929 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003930
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003931 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003932 return biovec->bv_len; /* always allow writes to be mergeable */
3933
Andre Noll664e7c42009-06-18 08:45:27 +10003934 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3935 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003936 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3937 if (max < 0) max = 0;
3938 if (max <= biovec->bv_len && bio_sectors == 0)
3939 return biovec->bv_len;
3940 else
3941 return max;
3942}
3943
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003944
NeilBrownfd01b882011-10-11 16:47:53 +11003945static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003946{
3947 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003948 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003949 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003950
Andre Noll664e7c42009-06-18 08:45:27 +10003951 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3952 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003953 return chunk_sectors >=
3954 ((sector & (chunk_sectors - 1)) + bio_sectors);
3955}
3956
3957/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003958 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3959 * later sampled by raid5d.
3960 */
NeilBrownd1688a62011-10-11 16:49:52 +11003961static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003962{
3963 unsigned long flags;
3964
3965 spin_lock_irqsave(&conf->device_lock, flags);
3966
3967 bi->bi_next = conf->retry_read_aligned_list;
3968 conf->retry_read_aligned_list = bi;
3969
3970 spin_unlock_irqrestore(&conf->device_lock, flags);
3971 md_wakeup_thread(conf->mddev->thread);
3972}
3973
3974
NeilBrownd1688a62011-10-11 16:49:52 +11003975static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003976{
3977 struct bio *bi;
3978
3979 bi = conf->retry_read_aligned;
3980 if (bi) {
3981 conf->retry_read_aligned = NULL;
3982 return bi;
3983 }
3984 bi = conf->retry_read_aligned_list;
3985 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003986 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003987 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003988 /*
3989 * this sets the active strip count to 1 and the processed
3990 * strip count to zero (upper 8 bits)
3991 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003992 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003993 }
3994
3995 return bi;
3996}
3997
3998
3999/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004000 * The "raid5_align_endio" should check if the read succeeded and if it
4001 * did, call bio_endio on the original bio (having bio_put the new bio
4002 * first).
4003 * If the read failed..
4004 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004005static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004006{
4007 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004008 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004009 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004010 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004011 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004012
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004013 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004014
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004015 rdev = (void*)raid_bi->bi_next;
4016 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004017 mddev = rdev->mddev;
4018 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004019
4020 rdev_dec_pending(rdev, conf->mddev);
4021
4022 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004023 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4024 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004025 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004026 if (atomic_dec_and_test(&conf->active_aligned_reads))
4027 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004028 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004029 }
4030
4031
Dan Williams45b42332007-07-09 11:56:43 -07004032 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004033
4034 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004035}
4036
Neil Brown387bb172007-02-08 14:20:29 -08004037static int bio_fits_rdev(struct bio *bi)
4038{
Jens Axboe165125e2007-07-24 09:28:11 +02004039 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004040
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004041 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004042 return 0;
4043 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004044 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004045 return 0;
4046
4047 if (q->merge_bvec_fn)
4048 /* it's too hard to apply the merge_bvec_fn at this stage,
4049 * just just give up
4050 */
4051 return 0;
4052
4053 return 1;
4054}
4055
4056
NeilBrownfd01b882011-10-11 16:47:53 +11004057static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004058{
NeilBrownd1688a62011-10-11 16:49:52 +11004059 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11004060 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004061 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004062 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004063 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004064
4065 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004066 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004067 return 0;
4068 }
4069 /*
NeilBrowna167f662010-10-26 18:31:13 +11004070 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004071 */
NeilBrowna167f662010-10-26 18:31:13 +11004072 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004073 if (!align_bi)
4074 return 0;
4075 /*
4076 * set bi_end_io to a new function, and set bi_private to the
4077 * original bio.
4078 */
4079 align_bi->bi_end_io = raid5_align_endio;
4080 align_bi->bi_private = raid_bio;
4081 /*
4082 * compute position
4083 */
NeilBrown112bf892009-03-31 14:39:38 +11004084 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4085 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004086 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004087
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004088 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004089 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004090 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4091 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4092 rdev->recovery_offset < end_sector) {
4093 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4094 if (rdev &&
4095 (test_bit(Faulty, &rdev->flags) ||
4096 !(test_bit(In_sync, &rdev->flags) ||
4097 rdev->recovery_offset >= end_sector)))
4098 rdev = NULL;
4099 }
4100 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004101 sector_t first_bad;
4102 int bad_sectors;
4103
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004104 atomic_inc(&rdev->nr_pending);
4105 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004106 raid_bio->bi_next = (void*)rdev;
4107 align_bi->bi_bdev = rdev->bdev;
4108 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004109
NeilBrown31c176e2011-07-28 11:39:22 +10004110 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004111 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004112 &first_bad, &bad_sectors)) {
4113 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004114 bio_put(align_bi);
4115 rdev_dec_pending(rdev, mddev);
4116 return 0;
4117 }
4118
majianpeng6c0544e2012-06-12 08:31:10 +08004119 /* No reshape active, so we can trust rdev->data_offset */
4120 align_bi->bi_sector += rdev->data_offset;
4121
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004122 spin_lock_irq(&conf->device_lock);
4123 wait_event_lock_irq(conf->wait_for_stripe,
4124 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004125 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004126 atomic_inc(&conf->active_aligned_reads);
4127 spin_unlock_irq(&conf->device_lock);
4128
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004129 if (mddev->gendisk)
4130 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4131 align_bi, disk_devt(mddev->gendisk),
4132 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004133 generic_make_request(align_bi);
4134 return 1;
4135 } else {
4136 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004137 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004138 return 0;
4139 }
4140}
4141
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004142/* __get_priority_stripe - get the next stripe to process
4143 *
4144 * Full stripe writes are allowed to pass preread active stripes up until
4145 * the bypass_threshold is exceeded. In general the bypass_count
4146 * increments when the handle_list is handled before the hold_list; however, it
4147 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4148 * stripe with in flight i/o. The bypass_count will be reset when the
4149 * head of the hold_list has changed, i.e. the head was promoted to the
4150 * handle_list.
4151 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004152static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004153{
Shaohua Li851c30c2013-08-28 14:30:16 +08004154 struct stripe_head *sh = NULL, *tmp;
4155 struct list_head *handle_list = NULL;
4156
4157 if (conf->worker_cnt_per_group == 0) {
4158 handle_list = &conf->handle_list;
4159 } else if (group != ANY_GROUP) {
4160 handle_list = &conf->worker_groups[group].handle_list;
4161 } else {
4162 int i;
4163 for (i = 0; i < conf->group_cnt; i++) {
4164 handle_list = &conf->worker_groups[i].handle_list;
4165 if (!list_empty(handle_list))
4166 break;
4167 }
4168 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004169
4170 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4171 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004172 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004173 list_empty(&conf->hold_list) ? "empty" : "busy",
4174 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4175
Shaohua Li851c30c2013-08-28 14:30:16 +08004176 if (!list_empty(handle_list)) {
4177 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004178
4179 if (list_empty(&conf->hold_list))
4180 conf->bypass_count = 0;
4181 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4182 if (conf->hold_list.next == conf->last_hold)
4183 conf->bypass_count++;
4184 else {
4185 conf->last_hold = conf->hold_list.next;
4186 conf->bypass_count -= conf->bypass_threshold;
4187 if (conf->bypass_count < 0)
4188 conf->bypass_count = 0;
4189 }
4190 }
4191 } else if (!list_empty(&conf->hold_list) &&
4192 ((conf->bypass_threshold &&
4193 conf->bypass_count > conf->bypass_threshold) ||
4194 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004195
4196 list_for_each_entry(tmp, &conf->hold_list, lru) {
4197 if (conf->worker_cnt_per_group == 0 ||
4198 group == ANY_GROUP ||
4199 !cpu_online(tmp->cpu) ||
4200 cpu_to_group(tmp->cpu) == group) {
4201 sh = tmp;
4202 break;
4203 }
4204 }
4205
4206 if (sh) {
4207 conf->bypass_count -= conf->bypass_threshold;
4208 if (conf->bypass_count < 0)
4209 conf->bypass_count = 0;
4210 }
4211 }
4212
4213 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004214 return NULL;
4215
4216 list_del_init(&sh->lru);
4217 atomic_inc(&sh->count);
4218 BUG_ON(atomic_read(&sh->count) != 1);
4219 return sh;
4220}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004221
Shaohua Li8811b592012-08-02 08:33:00 +10004222struct raid5_plug_cb {
4223 struct blk_plug_cb cb;
4224 struct list_head list;
4225};
4226
4227static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4228{
4229 struct raid5_plug_cb *cb = container_of(
4230 blk_cb, struct raid5_plug_cb, cb);
4231 struct stripe_head *sh;
4232 struct mddev *mddev = cb->cb.data;
4233 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004234 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004235
4236 if (cb->list.next && !list_empty(&cb->list)) {
4237 spin_lock_irq(&conf->device_lock);
4238 while (!list_empty(&cb->list)) {
4239 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4240 list_del_init(&sh->lru);
4241 /*
4242 * avoid race release_stripe_plug() sees
4243 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4244 * is still in our list
4245 */
4246 smp_mb__before_clear_bit();
4247 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004248 /*
4249 * STRIPE_ON_RELEASE_LIST could be set here. In that
4250 * case, the count is always > 1 here
4251 */
Shaohua Li8811b592012-08-02 08:33:00 +10004252 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004253 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004254 }
4255 spin_unlock_irq(&conf->device_lock);
4256 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004257 if (mddev->queue)
4258 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004259 kfree(cb);
4260}
4261
4262static void release_stripe_plug(struct mddev *mddev,
4263 struct stripe_head *sh)
4264{
4265 struct blk_plug_cb *blk_cb = blk_check_plugged(
4266 raid5_unplug, mddev,
4267 sizeof(struct raid5_plug_cb));
4268 struct raid5_plug_cb *cb;
4269
4270 if (!blk_cb) {
4271 release_stripe(sh);
4272 return;
4273 }
4274
4275 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4276
4277 if (cb->list.next == NULL)
4278 INIT_LIST_HEAD(&cb->list);
4279
4280 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4281 list_add_tail(&sh->lru, &cb->list);
4282 else
4283 release_stripe(sh);
4284}
4285
Shaohua Li620125f2012-10-11 13:49:05 +11004286static void make_discard_request(struct mddev *mddev, struct bio *bi)
4287{
4288 struct r5conf *conf = mddev->private;
4289 sector_t logical_sector, last_sector;
4290 struct stripe_head *sh;
4291 int remaining;
4292 int stripe_sectors;
4293
4294 if (mddev->reshape_position != MaxSector)
4295 /* Skip discard while reshape is happening */
4296 return;
4297
4298 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4299 last_sector = bi->bi_sector + (bi->bi_size>>9);
4300
4301 bi->bi_next = NULL;
4302 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4303
4304 stripe_sectors = conf->chunk_sectors *
4305 (conf->raid_disks - conf->max_degraded);
4306 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4307 stripe_sectors);
4308 sector_div(last_sector, stripe_sectors);
4309
4310 logical_sector *= conf->chunk_sectors;
4311 last_sector *= conf->chunk_sectors;
4312
4313 for (; logical_sector < last_sector;
4314 logical_sector += STRIPE_SECTORS) {
4315 DEFINE_WAIT(w);
4316 int d;
4317 again:
4318 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4319 prepare_to_wait(&conf->wait_for_overlap, &w,
4320 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004321 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4322 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4323 release_stripe(sh);
4324 schedule();
4325 goto again;
4326 }
4327 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004328 spin_lock_irq(&sh->stripe_lock);
4329 for (d = 0; d < conf->raid_disks; d++) {
4330 if (d == sh->pd_idx || d == sh->qd_idx)
4331 continue;
4332 if (sh->dev[d].towrite || sh->dev[d].toread) {
4333 set_bit(R5_Overlap, &sh->dev[d].flags);
4334 spin_unlock_irq(&sh->stripe_lock);
4335 release_stripe(sh);
4336 schedule();
4337 goto again;
4338 }
4339 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004340 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004341 finish_wait(&conf->wait_for_overlap, &w);
4342 for (d = 0; d < conf->raid_disks; d++) {
4343 if (d == sh->pd_idx || d == sh->qd_idx)
4344 continue;
4345 sh->dev[d].towrite = bi;
4346 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4347 raid5_inc_bi_active_stripes(bi);
4348 }
4349 spin_unlock_irq(&sh->stripe_lock);
4350 if (conf->mddev->bitmap) {
4351 for (d = 0;
4352 d < conf->raid_disks - conf->max_degraded;
4353 d++)
4354 bitmap_startwrite(mddev->bitmap,
4355 sh->sector,
4356 STRIPE_SECTORS,
4357 0);
4358 sh->bm_seq = conf->seq_flush + 1;
4359 set_bit(STRIPE_BIT_DELAY, &sh->state);
4360 }
4361
4362 set_bit(STRIPE_HANDLE, &sh->state);
4363 clear_bit(STRIPE_DELAYED, &sh->state);
4364 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4365 atomic_inc(&conf->preread_active_stripes);
4366 release_stripe_plug(mddev, sh);
4367 }
4368
4369 remaining = raid5_dec_bi_active_stripes(bi);
4370 if (remaining == 0) {
4371 md_write_end(mddev);
4372 bio_endio(bi, 0);
4373 }
4374}
4375
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004376static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
NeilBrownd1688a62011-10-11 16:49:52 +11004378 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004379 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 sector_t new_sector;
4381 sector_t logical_sector, last_sector;
4382 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004383 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004384 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Tejun Heoe9c74692010-09-03 11:56:18 +02004386 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4387 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004388 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004389 }
4390
NeilBrown3d310eb2005-06-21 17:17:26 -07004391 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004392
NeilBrown802ba062006-12-13 00:34:13 -08004393 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004394 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004395 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004396 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004397
Shaohua Li620125f2012-10-11 13:49:05 +11004398 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4399 make_discard_request(mddev, bi);
4400 return;
4401 }
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004404 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 bi->bi_next = NULL;
4406 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4409 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004410 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004411
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004412 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004413 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004414 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004415 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004416 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004417 * 64bit on a 32bit platform, and so it might be
4418 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004419 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004420 * the lock is dropped, so once we get a reference
4421 * to the stripe that we think it is, we will have
4422 * to check again.
4423 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004424 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004425 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004426 ? logical_sector < conf->reshape_progress
4427 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004428 previous = 1;
4429 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004430 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004431 ? logical_sector < conf->reshape_safe
4432 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004433 spin_unlock_irq(&conf->device_lock);
4434 schedule();
4435 goto retry;
4436 }
4437 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004438 spin_unlock_irq(&conf->device_lock);
4439 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004440
NeilBrown112bf892009-03-31 14:39:38 +11004441 new_sector = raid5_compute_sector(conf, logical_sector,
4442 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004443 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004444 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 (unsigned long long)new_sector,
4446 (unsigned long long)logical_sector);
4447
NeilBrownb5663ba2009-03-31 14:39:38 +11004448 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004449 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004451 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004452 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004453 * stripe, so we must do the range check again.
4454 * Expansion could still move past after this
4455 * test, but as we are holding a reference to
4456 * 'sh', we know that if that happens,
4457 * STRIPE_EXPANDING will get set and the expansion
4458 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004459 */
4460 int must_retry = 0;
4461 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004462 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004463 ? logical_sector >= conf->reshape_progress
4464 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004465 /* mismatch, need to try again */
4466 must_retry = 1;
4467 spin_unlock_irq(&conf->device_lock);
4468 if (must_retry) {
4469 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004470 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004471 goto retry;
4472 }
4473 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004474
Namhyung Kimffd96e32011-07-18 17:38:51 +10004475 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004476 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004477 logical_sector < mddev->suspend_hi) {
4478 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004479 /* As the suspend_* range is controlled by
4480 * userspace, we want an interruptible
4481 * wait.
4482 */
4483 flush_signals(current);
4484 prepare_to_wait(&conf->wait_for_overlap,
4485 &w, TASK_INTERRUPTIBLE);
4486 if (logical_sector >= mddev->suspend_lo &&
4487 logical_sector < mddev->suspend_hi)
4488 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004489 goto retry;
4490 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004491
4492 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004493 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004494 /* Stripe is busy expanding or
4495 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 * and wait a while
4497 */
NeilBrown482c0832011-04-18 18:25:42 +10004498 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 release_stripe(sh);
4500 schedule();
4501 goto retry;
4502 }
4503 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004504 set_bit(STRIPE_HANDLE, &sh->state);
4505 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004506 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004507 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4508 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004509 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 } else {
4511 /* cannot get stripe for read-ahead, just give-up */
4512 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4513 finish_wait(&conf->wait_for_overlap, &w);
4514 break;
4515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004517
Shaohua Lie7836bd62012-07-19 16:01:31 +10004518 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004519 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
NeilBrown16a53ec2006-06-26 00:27:38 -07004521 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004523
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004524 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4525 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004526 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
NeilBrownfd01b882011-10-11 16:47:53 +11004530static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004531
NeilBrownfd01b882011-10-11 16:47:53 +11004532static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
NeilBrown52c03292006-06-26 00:27:43 -07004534 /* reshaping is quite different to recovery/resync so it is
4535 * handled quite separately ... here.
4536 *
4537 * On each call to sync_request, we gather one chunk worth of
4538 * destination stripes and flag them as expanding.
4539 * Then we find all the source stripes and request reads.
4540 * As the reads complete, handle_stripe will copy the data
4541 * into the destination stripe and release that stripe.
4542 */
NeilBrownd1688a62011-10-11 16:49:52 +11004543 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004545 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004546 int raid_disks = conf->previous_raid_disks;
4547 int data_disks = raid_disks - conf->max_degraded;
4548 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004549 int i;
4550 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004551 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004552 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004553 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004554 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004555
NeilBrownfef9c612009-03-31 15:16:46 +11004556 if (sector_nr == 0) {
4557 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004558 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004559 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4560 sector_nr = raid5_size(mddev, 0, 0)
4561 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004562 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004563 conf->reshape_progress > 0)
4564 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004565 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004566 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004567 mddev->curr_resync_completed = sector_nr;
4568 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004569 *skipped = 1;
4570 return sector_nr;
4571 }
NeilBrown52c03292006-06-26 00:27:43 -07004572 }
4573
NeilBrown7a661382009-03-31 15:21:40 +11004574 /* We need to process a full chunk at a time.
4575 * If old and new chunk sizes differ, we need to process the
4576 * largest of these
4577 */
Andre Noll664e7c42009-06-18 08:45:27 +10004578 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4579 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004580 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004581 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004582
NeilBrownb5254dd2012-05-21 09:27:01 +10004583 /* We update the metadata at least every 10 seconds, or when
4584 * the data about to be copied would over-write the source of
4585 * the data at the front of the range. i.e. one new_stripe
4586 * along from reshape_progress new_maps to after where
4587 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004588 */
NeilBrownfef9c612009-03-31 15:16:46 +11004589 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004590 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004591 readpos = conf->reshape_progress;
4592 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004593 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004594 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004595 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004596 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004597 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004598 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004599 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004600 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004601 readpos -= min_t(sector_t, reshape_sectors, readpos);
4602 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004603 }
NeilBrown52c03292006-06-26 00:27:43 -07004604
NeilBrownb5254dd2012-05-21 09:27:01 +10004605 /* Having calculated the 'writepos' possibly use it
4606 * to set 'stripe_addr' which is where we will write to.
4607 */
4608 if (mddev->reshape_backwards) {
4609 BUG_ON(conf->reshape_progress == 0);
4610 stripe_addr = writepos;
4611 BUG_ON((mddev->dev_sectors &
4612 ~((sector_t)reshape_sectors - 1))
4613 - reshape_sectors - stripe_addr
4614 != sector_nr);
4615 } else {
4616 BUG_ON(writepos != sector_nr + reshape_sectors);
4617 stripe_addr = sector_nr;
4618 }
4619
NeilBrownc8f517c2009-03-31 15:28:40 +11004620 /* 'writepos' is the most advanced device address we might write.
4621 * 'readpos' is the least advanced device address we might read.
4622 * 'safepos' is the least address recorded in the metadata as having
4623 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004624 * If there is a min_offset_diff, these are adjusted either by
4625 * increasing the safepos/readpos if diff is negative, or
4626 * increasing writepos if diff is positive.
4627 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004628 * ensure safety in the face of a crash - that must be done by userspace
4629 * making a backup of the data. So in that case there is no particular
4630 * rush to update metadata.
4631 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4632 * update the metadata to advance 'safepos' to match 'readpos' so that
4633 * we can be safe in the event of a crash.
4634 * So we insist on updating metadata if safepos is behind writepos and
4635 * readpos is beyond writepos.
4636 * In any case, update the metadata every 10 seconds.
4637 * Maybe that number should be configurable, but I'm not sure it is
4638 * worth it.... maybe it could be a multiple of safemode_delay???
4639 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004640 if (conf->min_offset_diff < 0) {
4641 safepos += -conf->min_offset_diff;
4642 readpos += -conf->min_offset_diff;
4643 } else
4644 writepos += conf->min_offset_diff;
4645
NeilBrown2c810cd2012-05-21 09:27:00 +10004646 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004647 ? (safepos > writepos && readpos < writepos)
4648 : (safepos < writepos && readpos > writepos)) ||
4649 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004650 /* Cannot proceed until we've updated the superblock... */
4651 wait_event(conf->wait_for_overlap,
4652 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004653 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004654 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004655 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004656 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004657 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004658 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004659 kthread_should_stop());
4660 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004661 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004662 spin_unlock_irq(&conf->device_lock);
4663 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004664 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004665 }
4666
NeilBrownab69ae12009-03-31 15:26:47 +11004667 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004668 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004669 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004670 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004671 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004672 set_bit(STRIPE_EXPANDING, &sh->state);
4673 atomic_inc(&conf->reshape_stripes);
4674 /* If any of this stripe is beyond the end of the old
4675 * array, then we need to zero those blocks
4676 */
4677 for (j=sh->disks; j--;) {
4678 sector_t s;
4679 if (j == sh->pd_idx)
4680 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004681 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004682 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004683 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004684 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004685 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004686 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004687 continue;
4688 }
4689 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4690 set_bit(R5_Expanded, &sh->dev[j].flags);
4691 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4692 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004693 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004694 set_bit(STRIPE_EXPAND_READY, &sh->state);
4695 set_bit(STRIPE_HANDLE, &sh->state);
4696 }
NeilBrownab69ae12009-03-31 15:26:47 +11004697 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004698 }
4699 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004700 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004701 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004702 else
NeilBrown7a661382009-03-31 15:21:40 +11004703 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004704 spin_unlock_irq(&conf->device_lock);
4705 /* Ok, those stripe are ready. We can start scheduling
4706 * reads on the source stripes.
4707 * The source stripes are determined by mapping the first and last
4708 * block on the destination stripes.
4709 */
NeilBrown52c03292006-06-26 00:27:43 -07004710 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004711 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004712 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004713 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004714 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004715 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004716 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004717 if (last_sector >= mddev->dev_sectors)
4718 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004719 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004720 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004721 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4722 set_bit(STRIPE_HANDLE, &sh->state);
4723 release_stripe(sh);
4724 first_sector += STRIPE_SECTORS;
4725 }
NeilBrownab69ae12009-03-31 15:26:47 +11004726 /* Now that the sources are clearly marked, we can release
4727 * the destination stripes
4728 */
4729 while (!list_empty(&stripes)) {
4730 sh = list_entry(stripes.next, struct stripe_head, lru);
4731 list_del_init(&sh->lru);
4732 release_stripe(sh);
4733 }
NeilBrownc6207272008-02-06 01:39:52 -08004734 /* If this takes us to the resync_max point where we have to pause,
4735 * then we need to write out the superblock.
4736 */
NeilBrown7a661382009-03-31 15:21:40 +11004737 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004738 if ((sector_nr - mddev->curr_resync_completed) * 2
4739 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004740 /* Cannot proceed until we've updated the superblock... */
4741 wait_event(conf->wait_for_overlap,
4742 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004743 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004744 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004745 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004746 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4747 md_wakeup_thread(mddev->thread);
4748 wait_event(mddev->sb_wait,
4749 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4750 || kthread_should_stop());
4751 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004752 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004753 spin_unlock_irq(&conf->device_lock);
4754 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004755 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004756 }
NeilBrown7a661382009-03-31 15:21:40 +11004757 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004758}
4759
4760/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004761static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004762{
NeilBrownd1688a62011-10-11 16:49:52 +11004763 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004764 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004765 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004766 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004767 int still_degraded = 0;
4768 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
NeilBrown72626682005-09-09 16:23:54 -07004770 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004772
NeilBrown29269552006-03-27 01:18:10 -08004773 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4774 end_reshape(conf);
4775 return 0;
4776 }
NeilBrown72626682005-09-09 16:23:54 -07004777
4778 if (mddev->curr_resync < max_sector) /* aborted */
4779 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4780 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004781 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004782 conf->fullsync = 0;
4783 bitmap_close_sync(mddev->bitmap);
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return 0;
4786 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004787
NeilBrown64bd6602009-08-03 10:59:58 +10004788 /* Allow raid5_quiesce to complete */
4789 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4790
NeilBrown52c03292006-06-26 00:27:43 -07004791 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4792 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004793
NeilBrownc6207272008-02-06 01:39:52 -08004794 /* No need to check resync_max as we never do more than one
4795 * stripe, and as resync_max will always be on a chunk boundary,
4796 * if the check in md_do_sync didn't fire, there is no chance
4797 * of overstepping resync_max here
4798 */
4799
NeilBrown16a53ec2006-06-26 00:27:38 -07004800 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 * to resync, then assert that we are finished, because there is
4802 * nothing we can do.
4803 */
NeilBrown3285edf2006-06-26 00:27:55 -07004804 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004805 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004806 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004807 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 return rv;
4809 }
majianpeng6f608042013-04-24 11:42:41 +10004810 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4811 !conf->fullsync &&
4812 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4813 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004814 /* we can skip this block, and probably more */
4815 sync_blocks /= STRIPE_SECTORS;
4816 *skipped = 1;
4817 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
NeilBrownb47490c2008-02-06 01:39:50 -08004820 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4821
NeilBrowna8c906c2009-06-09 14:39:59 +10004822 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004824 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004826 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004828 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004830 /* Need to check if array will still be degraded after recovery/resync
4831 * We don't need to check the 'failed' flag as when that gets set,
4832 * recovery aborts.
4833 */
NeilBrownf001a702009-06-09 14:30:31 +10004834 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004835 if (conf->disks[i].rdev == NULL)
4836 still_degraded = 1;
4837
4838 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4839
NeilBrown83206d62011-07-26 11:19:49 +10004840 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
NeilBrown14425772009-10-16 15:55:25 +11004842 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 release_stripe(sh);
4844
4845 return STRIPE_SECTORS;
4846}
4847
NeilBrownd1688a62011-10-11 16:49:52 +11004848static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004849{
4850 /* We may not be able to submit a whole bio at once as there
4851 * may not be enough stripe_heads available.
4852 * We cannot pre-allocate enough stripe_heads as we may need
4853 * more than exist in the cache (if we allow ever large chunks).
4854 * So we do one stripe head at a time and record in
4855 * ->bi_hw_segments how many have been done.
4856 *
4857 * We *know* that this entire raid_bio is in one chunk, so
4858 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4859 */
4860 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004861 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004862 sector_t sector, logical_sector, last_sector;
4863 int scnt = 0;
4864 int remaining;
4865 int handled = 0;
4866
4867 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004868 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004869 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004870 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004871
4872 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004873 logical_sector += STRIPE_SECTORS,
4874 sector += STRIPE_SECTORS,
4875 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004876
Shaohua Lie7836bd62012-07-19 16:01:31 +10004877 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004878 /* already done this stripe */
4879 continue;
4880
NeilBrowna8c906c2009-06-09 14:39:59 +10004881 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004882
4883 if (!sh) {
4884 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004885 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004886 conf->retry_read_aligned = raid_bio;
4887 return handled;
4888 }
4889
Neil Brown387bb172007-02-08 14:20:29 -08004890 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4891 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004892 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004893 conf->retry_read_aligned = raid_bio;
4894 return handled;
4895 }
4896
majianpeng3f9e7c12012-07-31 10:04:21 +10004897 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004898 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004899 release_stripe(sh);
4900 handled++;
4901 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004902 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004903 if (remaining == 0) {
4904 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4905 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004906 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004907 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004908 if (atomic_dec_and_test(&conf->active_aligned_reads))
4909 wake_up(&conf->wait_for_stripe);
4910 return handled;
4911}
4912
Shaohua Li46a06402012-08-02 08:33:15 +10004913#define MAX_STRIPE_BATCH 8
Shaohua Li851c30c2013-08-28 14:30:16 +08004914static int handle_active_stripes(struct r5conf *conf, int group)
Shaohua Li46a06402012-08-02 08:33:15 +10004915{
4916 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4917 int i, batch_size = 0;
4918
4919 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08004920 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10004921 batch[batch_size++] = sh;
4922
4923 if (batch_size == 0)
4924 return batch_size;
4925 spin_unlock_irq(&conf->device_lock);
4926
4927 for (i = 0; i < batch_size; i++)
4928 handle_stripe(batch[i]);
4929
4930 cond_resched();
4931
4932 spin_lock_irq(&conf->device_lock);
4933 for (i = 0; i < batch_size; i++)
4934 __release_stripe(conf, batch[i]);
4935 return batch_size;
4936}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004937
Shaohua Li851c30c2013-08-28 14:30:16 +08004938static void raid5_do_work(struct work_struct *work)
4939{
4940 struct r5worker *worker = container_of(work, struct r5worker, work);
4941 struct r5worker_group *group = worker->group;
4942 struct r5conf *conf = group->conf;
4943 int group_id = group - conf->worker_groups;
4944 int handled;
4945 struct blk_plug plug;
4946
4947 pr_debug("+++ raid5worker active\n");
4948
4949 blk_start_plug(&plug);
4950 handled = 0;
4951 spin_lock_irq(&conf->device_lock);
4952 while (1) {
4953 int batch_size, released;
4954
4955 released = release_stripe_list(conf);
4956
4957 batch_size = handle_active_stripes(conf, group_id);
4958 if (!batch_size && !released)
4959 break;
4960 handled += batch_size;
4961 }
4962 pr_debug("%d stripes handled\n", handled);
4963
4964 spin_unlock_irq(&conf->device_lock);
4965 blk_finish_plug(&plug);
4966
4967 pr_debug("--- raid5worker inactive\n");
4968}
4969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970/*
4971 * This is our raid5 kernel thread.
4972 *
4973 * We scan the hash table for stripes which can be handled now.
4974 * During the scan, completed stripes are saved for us by the interrupt
4975 * handler, so that they will not have to wait for our next wakeup.
4976 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004977static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
Shaohua Li4ed87312012-10-11 13:34:00 +11004979 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004980 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004982 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
Dan Williams45b42332007-07-09 11:56:43 -07004984 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
4986 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004988 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 handled = 0;
4990 spin_lock_irq(&conf->device_lock);
4991 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004992 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08004993 int batch_size, released;
4994
4995 released = release_stripe_list(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
NeilBrown0021b7b2012-07-31 09:08:14 +02004997 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004998 !list_empty(&conf->bitmap_list)) {
4999 /* Now is a good time to flush some bitmap updates */
5000 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005001 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005002 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005003 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005004 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07005005 activate_bit_delay(conf);
5006 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005007 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005008
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005009 while ((bio = remove_bio_from_retry(conf))) {
5010 int ok;
5011 spin_unlock_irq(&conf->device_lock);
5012 ok = retry_aligned_read(conf, bio);
5013 spin_lock_irq(&conf->device_lock);
5014 if (!ok)
5015 break;
5016 handled++;
5017 }
5018
Shaohua Li851c30c2013-08-28 14:30:16 +08005019 batch_size = handle_active_stripes(conf, ANY_GROUP);
Shaohua Li773ca822013-08-27 17:50:39 +08005020 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005022 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Shaohua Li46a06402012-08-02 08:33:15 +10005024 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5025 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005026 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005027 spin_lock_irq(&conf->device_lock);
5028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 }
Dan Williams45b42332007-07-09 11:56:43 -07005030 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
5032 spin_unlock_irq(&conf->device_lock);
5033
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005034 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005035 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
Dan Williams45b42332007-07-09 11:56:43 -07005037 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
NeilBrown3f294f42005-11-08 21:39:25 -08005040static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005041raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005042{
NeilBrownd1688a62011-10-11 16:49:52 +11005043 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005044 if (conf)
5045 return sprintf(page, "%d\n", conf->max_nr_stripes);
5046 else
5047 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005048}
5049
NeilBrownc41d4ac2010-06-01 19:37:24 +10005050int
NeilBrownfd01b882011-10-11 16:47:53 +11005051raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005052{
NeilBrownd1688a62011-10-11 16:49:52 +11005053 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005054 int err;
5055
5056 if (size <= 16 || size > 32768)
5057 return -EINVAL;
5058 while (size < conf->max_nr_stripes) {
5059 if (drop_one_stripe(conf))
5060 conf->max_nr_stripes--;
5061 else
5062 break;
5063 }
5064 err = md_allow_write(mddev);
5065 if (err)
5066 return err;
5067 while (size > conf->max_nr_stripes) {
5068 if (grow_one_stripe(conf))
5069 conf->max_nr_stripes++;
5070 else break;
5071 }
5072 return 0;
5073}
5074EXPORT_SYMBOL(raid5_set_cache_size);
5075
NeilBrown3f294f42005-11-08 21:39:25 -08005076static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005077raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005078{
NeilBrownd1688a62011-10-11 16:49:52 +11005079 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005080 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005081 int err;
5082
NeilBrown3f294f42005-11-08 21:39:25 -08005083 if (len >= PAGE_SIZE)
5084 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08005085 if (!conf)
5086 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08005087
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005088 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005089 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005090 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005091 if (err)
5092 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08005093 return len;
5094}
NeilBrown007583c2005-11-08 21:39:30 -08005095
NeilBrown96de1e62005-11-08 21:39:39 -08005096static struct md_sysfs_entry
5097raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5098 raid5_show_stripe_cache_size,
5099 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005100
5101static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005102raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005103{
NeilBrownd1688a62011-10-11 16:49:52 +11005104 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005105 if (conf)
5106 return sprintf(page, "%d\n", conf->bypass_threshold);
5107 else
5108 return 0;
5109}
5110
5111static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005112raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005113{
NeilBrownd1688a62011-10-11 16:49:52 +11005114 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005115 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005116 if (len >= PAGE_SIZE)
5117 return -EINVAL;
5118 if (!conf)
5119 return -ENODEV;
5120
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005121 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005122 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005123 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005124 return -EINVAL;
5125 conf->bypass_threshold = new;
5126 return len;
5127}
5128
5129static struct md_sysfs_entry
5130raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5131 S_IRUGO | S_IWUSR,
5132 raid5_show_preread_threshold,
5133 raid5_store_preread_threshold);
5134
5135static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005136stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005137{
NeilBrownd1688a62011-10-11 16:49:52 +11005138 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005139 if (conf)
5140 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5141 else
5142 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005143}
5144
NeilBrown96de1e62005-11-08 21:39:39 -08005145static struct md_sysfs_entry
5146raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005147
Shaohua Lib7214202013-08-27 17:50:42 +08005148static ssize_t
5149raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5150{
5151 struct r5conf *conf = mddev->private;
5152 if (conf)
5153 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5154 else
5155 return 0;
5156}
5157
5158static int alloc_thread_groups(struct r5conf *conf, int cnt);
5159static ssize_t
5160raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5161{
5162 struct r5conf *conf = mddev->private;
5163 unsigned long new;
5164 int err;
5165 struct r5worker_group *old_groups;
5166 int old_group_cnt;
5167
5168 if (len >= PAGE_SIZE)
5169 return -EINVAL;
5170 if (!conf)
5171 return -ENODEV;
5172
5173 if (kstrtoul(page, 10, &new))
5174 return -EINVAL;
5175
5176 if (new == conf->worker_cnt_per_group)
5177 return len;
5178
5179 mddev_suspend(mddev);
5180
5181 old_groups = conf->worker_groups;
5182 old_group_cnt = conf->worker_cnt_per_group;
5183
5184 conf->worker_groups = NULL;
5185 err = alloc_thread_groups(conf, new);
5186 if (err) {
5187 conf->worker_groups = old_groups;
5188 conf->worker_cnt_per_group = old_group_cnt;
5189 } else {
5190 if (old_groups)
5191 kfree(old_groups[0].workers);
5192 kfree(old_groups);
5193 }
5194
5195 mddev_resume(mddev);
5196
5197 if (err)
5198 return err;
5199 return len;
5200}
5201
5202static struct md_sysfs_entry
5203raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5204 raid5_show_group_thread_cnt,
5205 raid5_store_group_thread_cnt);
5206
NeilBrown007583c2005-11-08 21:39:30 -08005207static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005208 &raid5_stripecache_size.attr,
5209 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005210 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08005211 &raid5_group_thread_cnt.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005212 NULL,
5213};
NeilBrown007583c2005-11-08 21:39:30 -08005214static struct attribute_group raid5_attrs_group = {
5215 .name = NULL,
5216 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005217};
5218
Shaohua Li851c30c2013-08-28 14:30:16 +08005219static int alloc_thread_groups(struct r5conf *conf, int cnt)
5220{
5221 int i, j;
5222 ssize_t size;
5223 struct r5worker *workers;
5224
5225 conf->worker_cnt_per_group = cnt;
5226 if (cnt == 0) {
5227 conf->worker_groups = NULL;
5228 return 0;
5229 }
5230 conf->group_cnt = num_possible_nodes();
5231 size = sizeof(struct r5worker) * cnt;
5232 workers = kzalloc(size * conf->group_cnt, GFP_NOIO);
5233 conf->worker_groups = kzalloc(sizeof(struct r5worker_group) *
5234 conf->group_cnt, GFP_NOIO);
5235 if (!conf->worker_groups || !workers) {
5236 kfree(workers);
5237 kfree(conf->worker_groups);
5238 conf->worker_groups = NULL;
5239 return -ENOMEM;
5240 }
5241
5242 for (i = 0; i < conf->group_cnt; i++) {
5243 struct r5worker_group *group;
5244
5245 group = &conf->worker_groups[i];
5246 INIT_LIST_HEAD(&group->handle_list);
5247 group->conf = conf;
5248 group->workers = workers + i * cnt;
5249
5250 for (j = 0; j < cnt; j++) {
5251 group->workers[j].group = group;
5252 INIT_WORK(&group->workers[j].work, raid5_do_work);
5253 }
5254 }
5255
5256 return 0;
5257}
5258
5259static void free_thread_groups(struct r5conf *conf)
5260{
5261 if (conf->worker_groups)
5262 kfree(conf->worker_groups[0].workers);
5263 kfree(conf->worker_groups);
5264 conf->worker_groups = NULL;
5265}
5266
Dan Williams80c3a6c2009-03-17 18:10:40 -07005267static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005268raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005269{
NeilBrownd1688a62011-10-11 16:49:52 +11005270 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005271
5272 if (!sectors)
5273 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005274 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005275 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005276 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005277
Andre Noll9d8f0362009-06-18 08:45:01 +10005278 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005279 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005280 return sectors * (raid_disks - conf->max_degraded);
5281}
5282
NeilBrownd1688a62011-10-11 16:49:52 +11005283static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005284{
5285 struct raid5_percpu *percpu;
5286 unsigned long cpu;
5287
5288 if (!conf->percpu)
5289 return;
5290
5291 get_online_cpus();
5292 for_each_possible_cpu(cpu) {
5293 percpu = per_cpu_ptr(conf->percpu, cpu);
5294 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005295 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005296 }
5297#ifdef CONFIG_HOTPLUG_CPU
5298 unregister_cpu_notifier(&conf->cpu_notify);
5299#endif
5300 put_online_cpus();
5301
5302 free_percpu(conf->percpu);
5303}
5304
NeilBrownd1688a62011-10-11 16:49:52 +11005305static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005306{
Shaohua Li851c30c2013-08-28 14:30:16 +08005307 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005308 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005309 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005310 kfree(conf->disks);
5311 kfree(conf->stripe_hashtbl);
5312 kfree(conf);
5313}
5314
Dan Williams36d1c642009-07-14 11:48:22 -07005315#ifdef CONFIG_HOTPLUG_CPU
5316static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5317 void *hcpu)
5318{
NeilBrownd1688a62011-10-11 16:49:52 +11005319 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005320 long cpu = (long)hcpu;
5321 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5322
5323 switch (action) {
5324 case CPU_UP_PREPARE:
5325 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005326 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005327 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005328 if (!percpu->scribble)
5329 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5330
5331 if (!percpu->scribble ||
5332 (conf->level == 6 && !percpu->spare_page)) {
5333 safe_put_page(percpu->spare_page);
5334 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005335 pr_err("%s: failed memory allocation for cpu%ld\n",
5336 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005337 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005338 }
5339 break;
5340 case CPU_DEAD:
5341 case CPU_DEAD_FROZEN:
5342 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005343 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005344 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005345 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005346 break;
5347 default:
5348 break;
5349 }
5350 return NOTIFY_OK;
5351}
5352#endif
5353
NeilBrownd1688a62011-10-11 16:49:52 +11005354static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005355{
5356 unsigned long cpu;
5357 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005358 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005359 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005360 int err;
5361
Dan Williams36d1c642009-07-14 11:48:22 -07005362 allcpus = alloc_percpu(struct raid5_percpu);
5363 if (!allcpus)
5364 return -ENOMEM;
5365 conf->percpu = allcpus;
5366
5367 get_online_cpus();
5368 err = 0;
5369 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005370 if (conf->level == 6) {
5371 spare_page = alloc_page(GFP_KERNEL);
5372 if (!spare_page) {
5373 err = -ENOMEM;
5374 break;
5375 }
5376 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5377 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005378 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005379 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005380 err = -ENOMEM;
5381 break;
5382 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005383 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005384 }
5385#ifdef CONFIG_HOTPLUG_CPU
5386 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5387 conf->cpu_notify.priority = 0;
5388 if (err == 0)
5389 err = register_cpu_notifier(&conf->cpu_notify);
5390#endif
5391 put_online_cpus();
5392
5393 return err;
5394}
5395
NeilBrownd1688a62011-10-11 16:49:52 +11005396static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397{
NeilBrownd1688a62011-10-11 16:49:52 +11005398 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005399 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005400 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005402 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
NeilBrown91adb562009-03-31 14:39:39 +11005404 if (mddev->new_level != 5
5405 && mddev->new_level != 4
5406 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005407 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005408 mdname(mddev), mddev->new_level);
5409 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 }
NeilBrown91adb562009-03-31 14:39:39 +11005411 if ((mddev->new_level == 5
5412 && !algorithm_valid_raid5(mddev->new_layout)) ||
5413 (mddev->new_level == 6
5414 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005415 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005416 mdname(mddev), mddev->new_layout);
5417 return ERR_PTR(-EIO);
5418 }
5419 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005420 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005421 mdname(mddev), mddev->raid_disks);
5422 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
Andre Noll664e7c42009-06-18 08:45:27 +10005425 if (!mddev->new_chunk_sectors ||
5426 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5427 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005428 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5429 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005430 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005431 }
5432
NeilBrownd1688a62011-10-11 16:49:52 +11005433 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005434 if (conf == NULL)
5435 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08005436 /* Don't enable multi-threading by default*/
5437 if (alloc_thread_groups(conf, 0))
5438 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005439 spin_lock_init(&conf->device_lock);
5440 init_waitqueue_head(&conf->wait_for_stripe);
5441 init_waitqueue_head(&conf->wait_for_overlap);
5442 INIT_LIST_HEAD(&conf->handle_list);
5443 INIT_LIST_HEAD(&conf->hold_list);
5444 INIT_LIST_HEAD(&conf->delayed_list);
5445 INIT_LIST_HEAD(&conf->bitmap_list);
5446 INIT_LIST_HEAD(&conf->inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005447 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005448 atomic_set(&conf->active_stripes, 0);
5449 atomic_set(&conf->preread_active_stripes, 0);
5450 atomic_set(&conf->active_aligned_reads, 0);
5451 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005452 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005453
5454 conf->raid_disks = mddev->raid_disks;
5455 if (mddev->reshape_position == MaxSector)
5456 conf->previous_raid_disks = mddev->raid_disks;
5457 else
5458 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005459 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5460 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005461
NeilBrown5e5e3e72009-10-16 16:35:30 +11005462 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005463 GFP_KERNEL);
5464 if (!conf->disks)
5465 goto abort;
5466
5467 conf->mddev = mddev;
5468
5469 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5470 goto abort;
5471
Dan Williams36d1c642009-07-14 11:48:22 -07005472 conf->level = mddev->new_level;
5473 if (raid5_alloc_percpu(conf) != 0)
5474 goto abort;
5475
NeilBrown0c55e022010-05-03 14:09:02 +10005476 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005477
NeilBrowndafb20f2012-03-19 12:46:39 +11005478 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005479 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005480 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005481 || raid_disk < 0)
5482 continue;
5483 disk = conf->disks + raid_disk;
5484
NeilBrown17045f52011-12-23 10:17:53 +11005485 if (test_bit(Replacement, &rdev->flags)) {
5486 if (disk->replacement)
5487 goto abort;
5488 disk->replacement = rdev;
5489 } else {
5490 if (disk->rdev)
5491 goto abort;
5492 disk->rdev = rdev;
5493 }
NeilBrown91adb562009-03-31 14:39:39 +11005494
5495 if (test_bit(In_sync, &rdev->flags)) {
5496 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005497 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5498 " disk %d\n",
5499 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005500 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005501 /* Cannot rely on bitmap to complete recovery */
5502 conf->fullsync = 1;
5503 }
5504
Andre Noll09c9e5f2009-06-18 08:45:55 +10005505 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005506 conf->level = mddev->new_level;
5507 if (conf->level == 6)
5508 conf->max_degraded = 2;
5509 else
5510 conf->max_degraded = 1;
5511 conf->algorithm = mddev->new_layout;
5512 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005513 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005514 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005515 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005516 conf->prev_algo = mddev->layout;
5517 }
NeilBrown91adb562009-03-31 14:39:39 +11005518
5519 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005520 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005521 if (grow_stripes(conf, conf->max_nr_stripes)) {
5522 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005523 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5524 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005525 goto abort;
5526 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005527 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5528 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005529
NeilBrown02326052012-07-03 15:56:52 +10005530 sprintf(pers_name, "raid%d", mddev->new_level);
5531 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005532 if (!conf->thread) {
5533 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005534 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005535 mdname(mddev));
5536 goto abort;
5537 }
5538
5539 return conf;
5540
5541 abort:
5542 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005543 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005544 return ERR_PTR(-EIO);
5545 } else
5546 return ERR_PTR(-ENOMEM);
5547}
5548
NeilBrownc148ffd2009-11-13 17:47:00 +11005549
5550static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5551{
5552 switch (algo) {
5553 case ALGORITHM_PARITY_0:
5554 if (raid_disk < max_degraded)
5555 return 1;
5556 break;
5557 case ALGORITHM_PARITY_N:
5558 if (raid_disk >= raid_disks - max_degraded)
5559 return 1;
5560 break;
5561 case ALGORITHM_PARITY_0_6:
5562 if (raid_disk == 0 ||
5563 raid_disk == raid_disks - 1)
5564 return 1;
5565 break;
5566 case ALGORITHM_LEFT_ASYMMETRIC_6:
5567 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5568 case ALGORITHM_LEFT_SYMMETRIC_6:
5569 case ALGORITHM_RIGHT_SYMMETRIC_6:
5570 if (raid_disk == raid_disks - 1)
5571 return 1;
5572 }
5573 return 0;
5574}
5575
NeilBrownfd01b882011-10-11 16:47:53 +11005576static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005577{
NeilBrownd1688a62011-10-11 16:49:52 +11005578 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005579 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005580 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005581 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005582 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005583 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005584 long long min_offset_diff = 0;
5585 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005586
Andre Noll8c6ac862009-06-18 08:48:06 +10005587 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005588 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005589 " -- starting background reconstruction\n",
5590 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005591
5592 rdev_for_each(rdev, mddev) {
5593 long long diff;
5594 if (rdev->raid_disk < 0)
5595 continue;
5596 diff = (rdev->new_data_offset - rdev->data_offset);
5597 if (first) {
5598 min_offset_diff = diff;
5599 first = 0;
5600 } else if (mddev->reshape_backwards &&
5601 diff < min_offset_diff)
5602 min_offset_diff = diff;
5603 else if (!mddev->reshape_backwards &&
5604 diff > min_offset_diff)
5605 min_offset_diff = diff;
5606 }
5607
NeilBrownf6705572006-03-27 01:18:11 -08005608 if (mddev->reshape_position != MaxSector) {
5609 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005610 * Difficulties arise if the stripe we would write to
5611 * next is at or after the stripe we would read from next.
5612 * For a reshape that changes the number of devices, this
5613 * is only possible for a very short time, and mdadm makes
5614 * sure that time appears to have past before assembling
5615 * the array. So we fail if that time hasn't passed.
5616 * For a reshape that keeps the number of devices the same
5617 * mdadm must be monitoring the reshape can keeping the
5618 * critical areas read-only and backed up. It will start
5619 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005620 */
5621 sector_t here_new, here_old;
5622 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005623 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005624
NeilBrown88ce4932009-03-31 15:24:23 +11005625 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005626 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005627 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005628 mdname(mddev));
5629 return -EINVAL;
5630 }
NeilBrownf6705572006-03-27 01:18:11 -08005631 old_disks = mddev->raid_disks - mddev->delta_disks;
5632 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005633 * further up in new geometry must map after here in old
5634 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005635 */
5636 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005637 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005638 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005639 printk(KERN_ERR "md/raid:%s: reshape_position not "
5640 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005641 return -EINVAL;
5642 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005643 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005644 /* here_new is the stripe we will write to */
5645 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005646 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005647 (old_disks-max_degraded));
5648 /* here_old is the first stripe that we might need to read
5649 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005650 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005651 if ((here_new * mddev->new_chunk_sectors !=
5652 here_old * mddev->chunk_sectors)) {
5653 printk(KERN_ERR "md/raid:%s: reshape position is"
5654 " confused - aborting\n", mdname(mddev));
5655 return -EINVAL;
5656 }
NeilBrown67ac6012009-08-13 10:06:24 +10005657 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005658 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005659 * and taking constant backups.
5660 * mdadm always starts a situation like this in
5661 * readonly mode so it can take control before
5662 * allowing any writes. So just check for that.
5663 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005664 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5665 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5666 /* not really in-place - so OK */;
5667 else if (mddev->ro == 0) {
5668 printk(KERN_ERR "md/raid:%s: in-place reshape "
5669 "must be started in read-only mode "
5670 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005671 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005672 return -EINVAL;
5673 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005674 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005675 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005676 here_old * mddev->chunk_sectors)
5677 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005678 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005679 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005680 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5681 "auto-recovery - aborting.\n",
5682 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005683 return -EINVAL;
5684 }
NeilBrown0c55e022010-05-03 14:09:02 +10005685 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5686 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005687 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005688 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005689 BUG_ON(mddev->level != mddev->new_level);
5690 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005691 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005692 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005693 }
5694
NeilBrown245f46c2009-03-31 14:39:39 +11005695 if (mddev->private == NULL)
5696 conf = setup_conf(mddev);
5697 else
5698 conf = mddev->private;
5699
NeilBrown91adb562009-03-31 14:39:39 +11005700 if (IS_ERR(conf))
5701 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005702
NeilBrownb5254dd2012-05-21 09:27:01 +10005703 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005704 mddev->thread = conf->thread;
5705 conf->thread = NULL;
5706 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
NeilBrown17045f52011-12-23 10:17:53 +11005708 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5709 i++) {
5710 rdev = conf->disks[i].rdev;
5711 if (!rdev && conf->disks[i].replacement) {
5712 /* The replacement is all we have yet */
5713 rdev = conf->disks[i].replacement;
5714 conf->disks[i].replacement = NULL;
5715 clear_bit(Replacement, &rdev->flags);
5716 conf->disks[i].rdev = rdev;
5717 }
5718 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005719 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005720 if (conf->disks[i].replacement &&
5721 conf->reshape_progress != MaxSector) {
5722 /* replacements and reshape simply do not mix. */
5723 printk(KERN_ERR "md: cannot handle concurrent "
5724 "replacement and reshape.\n");
5725 goto abort;
5726 }
NeilBrown2f115882010-06-17 17:41:03 +10005727 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005728 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005729 continue;
5730 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005731 /* This disc is not fully in-sync. However if it
5732 * just stored parity (beyond the recovery_offset),
5733 * when we don't need to be concerned about the
5734 * array being dirty.
5735 * When reshape goes 'backwards', we never have
5736 * partially completed devices, so we only need
5737 * to worry about reshape going forwards.
5738 */
5739 /* Hack because v0.91 doesn't store recovery_offset properly. */
5740 if (mddev->major_version == 0 &&
5741 mddev->minor_version > 90)
5742 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005743
NeilBrownc148ffd2009-11-13 17:47:00 +11005744 if (rdev->recovery_offset < reshape_offset) {
5745 /* We need to check old and new layout */
5746 if (!only_parity(rdev->raid_disk,
5747 conf->algorithm,
5748 conf->raid_disks,
5749 conf->max_degraded))
5750 continue;
5751 }
5752 if (!only_parity(rdev->raid_disk,
5753 conf->prev_algo,
5754 conf->previous_raid_disks,
5755 conf->max_degraded))
5756 continue;
5757 dirty_parity_disks++;
5758 }
NeilBrown91adb562009-03-31 14:39:39 +11005759
NeilBrown17045f52011-12-23 10:17:53 +11005760 /*
5761 * 0 for a fully functional array, 1 or 2 for a degraded array.
5762 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005763 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
NeilBrown674806d2010-06-16 17:17:53 +10005765 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005766 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005768 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 goto abort;
5770 }
5771
NeilBrown91adb562009-03-31 14:39:39 +11005772 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005773 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005774 mddev->resync_max_sectors = mddev->dev_sectors;
5775
NeilBrownc148ffd2009-11-13 17:47:00 +11005776 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005778 if (mddev->ok_start_degraded)
5779 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005780 "md/raid:%s: starting dirty degraded array"
5781 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005782 mdname(mddev));
5783 else {
5784 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005785 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005786 mdname(mddev));
5787 goto abort;
5788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 }
5790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005792 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5793 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005794 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5795 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 else
NeilBrown0c55e022010-05-03 14:09:02 +10005797 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5798 " out of %d devices, algorithm %d\n",
5799 mdname(mddev), conf->level,
5800 mddev->raid_disks - mddev->degraded,
5801 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
5803 print_raid5_conf(conf);
5804
NeilBrownfef9c612009-03-31 15:16:46 +11005805 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005806 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005807 atomic_set(&conf->reshape_stripes, 0);
5808 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5809 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5810 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5811 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5812 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005813 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005814 }
5815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
5817 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005818 if (mddev->to_remove == &raid5_attrs_group)
5819 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005820 else if (mddev->kobj.sd &&
5821 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005822 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005823 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005824 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005825 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5826
5827 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005828 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005829 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005830 /* read-ahead size must cover two whole stripes, which
5831 * is 2 * (datadisks) * chunksize where 'n' is the
5832 * number of raid devices
5833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5835 int stripe = data_disks *
5836 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5837 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5838 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005839
5840 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005841
5842 mddev->queue->backing_dev_info.congested_data = mddev;
5843 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005844
5845 chunk_size = mddev->chunk_sectors << 9;
5846 blk_queue_io_min(mddev->queue, chunk_size);
5847 blk_queue_io_opt(mddev->queue, chunk_size *
5848 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005849 /*
5850 * We can only discard a whole stripe. It doesn't make sense to
5851 * discard data disk but write parity disk
5852 */
5853 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005854 /* Round up to power of 2, as discard handling
5855 * currently assumes that */
5856 while ((stripe-1) & stripe)
5857 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005858 mddev->queue->limits.discard_alignment = stripe;
5859 mddev->queue->limits.discard_granularity = stripe;
5860 /*
5861 * unaligned part of discard request will be ignored, so can't
5862 * guarantee discard_zerors_data
5863 */
5864 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005865
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005866 blk_queue_max_write_same_sectors(mddev->queue, 0);
5867
NeilBrown05616be2012-05-21 09:27:00 +10005868 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005869 disk_stack_limits(mddev->gendisk, rdev->bdev,
5870 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005871 disk_stack_limits(mddev->gendisk, rdev->bdev,
5872 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005873 /*
5874 * discard_zeroes_data is required, otherwise data
5875 * could be lost. Consider a scenario: discard a stripe
5876 * (the stripe could be inconsistent if
5877 * discard_zeroes_data is 0); write one disk of the
5878 * stripe (the stripe could be inconsistent again
5879 * depending on which disks are used to calculate
5880 * parity); the disk is broken; The stripe data of this
5881 * disk is lost.
5882 */
5883 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5884 !bdev_get_queue(rdev->bdev)->
5885 limits.discard_zeroes_data)
5886 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005887 }
Shaohua Li620125f2012-10-11 13:49:05 +11005888
5889 if (discard_supported &&
5890 mddev->queue->limits.max_discard_sectors >= stripe &&
5891 mddev->queue->limits.discard_granularity >= stripe)
5892 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5893 mddev->queue);
5894 else
5895 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5896 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 }
5898
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 return 0;
5900abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005901 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005902 print_raid5_conf(conf);
5903 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005905 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 return -EIO;
5907}
5908
NeilBrownfd01b882011-10-11 16:47:53 +11005909static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910{
NeilBrownd1688a62011-10-11 16:49:52 +11005911 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
NeilBrown01f96c02011-09-21 15:30:20 +10005913 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005914 if (mddev->queue)
5915 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005916 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005917 mddev->private = NULL;
5918 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 return 0;
5920}
5921
NeilBrownfd01b882011-10-11 16:47:53 +11005922static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923{
NeilBrownd1688a62011-10-11 16:49:52 +11005924 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 int i;
5926
Andre Noll9d8f0362009-06-18 08:45:01 +10005927 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5928 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005929 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 for (i = 0; i < conf->raid_disks; i++)
5931 seq_printf (seq, "%s",
5932 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005933 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935}
5936
NeilBrownd1688a62011-10-11 16:49:52 +11005937static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938{
5939 int i;
5940 struct disk_info *tmp;
5941
NeilBrown0c55e022010-05-03 14:09:02 +10005942 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 if (!conf) {
5944 printk("(conf==NULL)\n");
5945 return;
5946 }
NeilBrown0c55e022010-05-03 14:09:02 +10005947 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5948 conf->raid_disks,
5949 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
5951 for (i = 0; i < conf->raid_disks; i++) {
5952 char b[BDEVNAME_SIZE];
5953 tmp = conf->disks + i;
5954 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005955 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5956 i, !test_bit(Faulty, &tmp->rdev->flags),
5957 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 }
5959}
5960
NeilBrownfd01b882011-10-11 16:47:53 +11005961static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962{
5963 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005964 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005966 int count = 0;
5967 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
5969 for (i = 0; i < conf->raid_disks; i++) {
5970 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005971 if (tmp->replacement
5972 && tmp->replacement->recovery_offset == MaxSector
5973 && !test_bit(Faulty, &tmp->replacement->flags)
5974 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5975 /* Replacement has just become active. */
5976 if (!tmp->rdev
5977 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5978 count++;
5979 if (tmp->rdev) {
5980 /* Replaced device not technically faulty,
5981 * but we need to be sure it gets removed
5982 * and never re-added.
5983 */
5984 set_bit(Faulty, &tmp->rdev->flags);
5985 sysfs_notify_dirent_safe(
5986 tmp->rdev->sysfs_state);
5987 }
5988 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5989 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005990 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005991 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005992 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005993 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005994 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 }
5996 }
NeilBrown6b965622010-08-18 11:56:59 +10005997 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005998 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005999 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006001 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002}
6003
NeilBrownb8321b62011-12-23 10:17:51 +11006004static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005{
NeilBrownd1688a62011-10-11 16:49:52 +11006006 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006008 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006009 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 struct disk_info *p = conf->disks + number;
6011
6012 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006013 if (rdev == p->rdev)
6014 rdevp = &p->rdev;
6015 else if (rdev == p->replacement)
6016 rdevp = &p->replacement;
6017 else
6018 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006019
NeilBrown657e3e42011-12-23 10:17:52 +11006020 if (number >= conf->raid_disks &&
6021 conf->reshape_progress == MaxSector)
6022 clear_bit(In_sync, &rdev->flags);
6023
6024 if (test_bit(In_sync, &rdev->flags) ||
6025 atomic_read(&rdev->nr_pending)) {
6026 err = -EBUSY;
6027 goto abort;
6028 }
6029 /* Only remove non-faulty devices if recovery
6030 * isn't possible.
6031 */
6032 if (!test_bit(Faulty, &rdev->flags) &&
6033 mddev->recovery_disabled != conf->recovery_disabled &&
6034 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006035 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006036 number < conf->raid_disks) {
6037 err = -EBUSY;
6038 goto abort;
6039 }
6040 *rdevp = NULL;
6041 synchronize_rcu();
6042 if (atomic_read(&rdev->nr_pending)) {
6043 /* lost the race, try later */
6044 err = -EBUSY;
6045 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006046 } else if (p->replacement) {
6047 /* We must have just cleared 'rdev' */
6048 p->rdev = p->replacement;
6049 clear_bit(Replacement, &p->replacement->flags);
6050 smp_mb(); /* Make sure other CPUs may see both as identical
6051 * but will never see neither - if they are careful
6052 */
6053 p->replacement = NULL;
6054 clear_bit(WantReplacement, &rdev->flags);
6055 } else
6056 /* We might have just removed the Replacement as faulty-
6057 * clear the bit just in case
6058 */
6059 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060abort:
6061
6062 print_raid5_conf(conf);
6063 return err;
6064}
6065
NeilBrownfd01b882011-10-11 16:47:53 +11006066static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067{
NeilBrownd1688a62011-10-11 16:49:52 +11006068 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10006069 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 int disk;
6071 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10006072 int first = 0;
6073 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
NeilBrown7f0da592011-07-28 11:39:22 +10006075 if (mddev->recovery_disabled == conf->recovery_disabled)
6076 return -EBUSY;
6077
NeilBrowndc10c642012-03-19 12:46:37 +11006078 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10006080 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Neil Brown6c2fce22008-06-28 08:31:31 +10006082 if (rdev->raid_disk >= 0)
6083 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
6085 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07006086 * find the disk ... but prefer rdev->saved_raid_disk
6087 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 */
NeilBrown16a53ec2006-06-26 00:27:38 -07006089 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10006090 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006091 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10006092 first = rdev->saved_raid_disk;
6093
6094 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11006095 p = conf->disks + disk;
6096 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08006097 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10006099 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07006100 if (rdev->saved_raid_disk != disk)
6101 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08006102 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10006103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006105 }
6106 for (disk = first; disk <= last; disk++) {
6107 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11006108 if (test_bit(WantReplacement, &p->rdev->flags) &&
6109 p->replacement == NULL) {
6110 clear_bit(In_sync, &rdev->flags);
6111 set_bit(Replacement, &rdev->flags);
6112 rdev->raid_disk = disk;
6113 err = 0;
6114 conf->fullsync = 1;
6115 rcu_assign_pointer(p->replacement, rdev);
6116 break;
6117 }
6118 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006119out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10006121 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122}
6123
NeilBrownfd01b882011-10-11 16:47:53 +11006124static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125{
6126 /* no resync is happening, and there is enough space
6127 * on all devices, so we can resize.
6128 * We need to make sure resync covers any new space.
6129 * If the array is shrinking we should possibly wait until
6130 * any io in the removed space completes, but it hardly seems
6131 * worth it.
6132 */
NeilBrowna4a61252012-05-22 13:55:27 +10006133 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10006134 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10006135 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6136 if (mddev->external_size &&
6137 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11006138 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10006139 if (mddev->bitmap) {
6140 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6141 if (ret)
6142 return ret;
6143 }
6144 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10006145 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006146 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10006147 if (sectors > mddev->dev_sectors &&
6148 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006149 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6151 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006152 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07006153 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 return 0;
6155}
6156
NeilBrownfd01b882011-10-11 16:47:53 +11006157static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10006158{
6159 /* Can only proceed if there are plenty of stripe_heads.
6160 * We need a minimum of one full stripe,, and for sensible progress
6161 * it is best to have about 4 times that.
6162 * If we require 4 times, then the default 256 4K stripe_heads will
6163 * allow for chunk sizes up to 256K, which is probably OK.
6164 * If the chunk size is greater, user-space should request more
6165 * stripe_heads first.
6166 */
NeilBrownd1688a62011-10-11 16:49:52 +11006167 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10006168 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6169 > conf->max_nr_stripes ||
6170 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6171 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10006172 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6173 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10006174 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6175 / STRIPE_SIZE)*4);
6176 return 0;
6177 }
6178 return 1;
6179}
6180
NeilBrownfd01b882011-10-11 16:47:53 +11006181static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08006182{
NeilBrownd1688a62011-10-11 16:49:52 +11006183 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08006184
NeilBrown88ce4932009-03-31 15:24:23 +11006185 if (mddev->delta_disks == 0 &&
6186 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10006187 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10006188 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10006189 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11006190 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10006191 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11006192 /* We might be able to shrink, but the devices must
6193 * be made bigger first.
6194 * For raid6, 4 is the minimum size.
6195 * Otherwise 2 is the minimum
6196 */
6197 int min = 2;
6198 if (mddev->level == 6)
6199 min = 4;
6200 if (mddev->raid_disks + mddev->delta_disks < min)
6201 return -EINVAL;
6202 }
NeilBrown29269552006-03-27 01:18:10 -08006203
NeilBrown01ee22b2009-06-18 08:47:20 +10006204 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08006205 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08006206
NeilBrowne56108d62012-10-11 14:24:13 +11006207 return resize_stripes(conf, (conf->previous_raid_disks
6208 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08006209}
6210
NeilBrownfd01b882011-10-11 16:47:53 +11006211static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006212{
NeilBrownd1688a62011-10-11 16:49:52 +11006213 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006214 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006215 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006216 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006217
NeilBrownf4168852007-02-28 20:11:53 -08006218 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006219 return -EBUSY;
6220
NeilBrown01ee22b2009-06-18 08:47:20 +10006221 if (!check_stripe_cache(mddev))
6222 return -ENOSPC;
6223
NeilBrown30b67642012-05-22 13:55:28 +10006224 if (has_failed(conf))
6225 return -EINVAL;
6226
NeilBrownc6563a82012-05-21 09:27:00 +10006227 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006228 if (!test_bit(In_sync, &rdev->flags)
6229 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006230 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006231 }
NeilBrown63c70c42006-03-27 01:18:13 -08006232
NeilBrownf4168852007-02-28 20:11:53 -08006233 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006234 /* Not enough devices even to make a degraded array
6235 * of that size
6236 */
6237 return -EINVAL;
6238
NeilBrownec32a2b2009-03-31 15:17:38 +11006239 /* Refuse to reduce size of the array. Any reductions in
6240 * array size must be through explicit setting of array_size
6241 * attribute.
6242 */
6243 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6244 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006245 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006246 "before number of disks\n", mdname(mddev));
6247 return -EINVAL;
6248 }
6249
NeilBrownf6705572006-03-27 01:18:11 -08006250 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006251 spin_lock_irq(&conf->device_lock);
6252 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006253 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006254 conf->prev_chunk_sectors = conf->chunk_sectors;
6255 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006256 conf->prev_algo = conf->algorithm;
6257 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006258 conf->generation++;
6259 /* Code that selects data_offset needs to see the generation update
6260 * if reshape_progress has been set - so a memory barrier needed.
6261 */
6262 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006263 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006264 conf->reshape_progress = raid5_size(mddev, 0, 0);
6265 else
6266 conf->reshape_progress = 0;
6267 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08006268 spin_unlock_irq(&conf->device_lock);
6269
6270 /* Add some new drives, as many as will fit.
6271 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006272 * Don't add devices if we are reducing the number of
6273 * devices in the array. This is because it is not possible
6274 * to correctly record the "partially reconstructed" state of
6275 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006276 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006277 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006278 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006279 if (rdev->raid_disk < 0 &&
6280 !test_bit(Faulty, &rdev->flags)) {
6281 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006282 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006283 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006284 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006285 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006286 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006287
6288 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006289 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006290 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006291 } else if (rdev->raid_disk >= conf->previous_raid_disks
6292 && !test_bit(Faulty, &rdev->flags)) {
6293 /* This is a spare that was manually added */
6294 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006295 }
NeilBrown29269552006-03-27 01:18:10 -08006296
NeilBrown87a8dec2011-01-31 11:57:43 +11006297 /* When a reshape changes the number of devices,
6298 * ->degraded is measured against the larger of the
6299 * pre and post number of devices.
6300 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006301 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006302 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006303 spin_unlock_irqrestore(&conf->device_lock, flags);
6304 }
NeilBrown63c70c42006-03-27 01:18:13 -08006305 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006306 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006307 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006308
NeilBrown29269552006-03-27 01:18:10 -08006309 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6310 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6311 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6312 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6313 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006314 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006315 if (!mddev->sync_thread) {
6316 mddev->recovery = 0;
6317 spin_lock_irq(&conf->device_lock);
6318 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006319 rdev_for_each(rdev, mddev)
6320 rdev->new_data_offset = rdev->data_offset;
6321 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006322 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006323 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006324 spin_unlock_irq(&conf->device_lock);
6325 return -EAGAIN;
6326 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006327 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006328 md_wakeup_thread(mddev->sync_thread);
6329 md_new_event(mddev);
6330 return 0;
6331}
NeilBrown29269552006-03-27 01:18:10 -08006332
NeilBrownec32a2b2009-03-31 15:17:38 +11006333/* This is called from the reshape thread and should make any
6334 * changes needed in 'conf'
6335 */
NeilBrownd1688a62011-10-11 16:49:52 +11006336static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006337{
NeilBrown29269552006-03-27 01:18:10 -08006338
NeilBrownf6705572006-03-27 01:18:11 -08006339 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006340 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006341
NeilBrownf6705572006-03-27 01:18:11 -08006342 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006343 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006344 rdev_for_each(rdev, conf->mddev)
6345 rdev->data_offset = rdev->new_data_offset;
6346 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006347 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006348 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006349 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006350
6351 /* read-ahead size must cover two whole stripes, which is
6352 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6353 */
NeilBrown4a5add42010-06-01 19:37:28 +10006354 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006355 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006356 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006357 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006358 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6359 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6360 }
NeilBrown29269552006-03-27 01:18:10 -08006361 }
NeilBrown29269552006-03-27 01:18:10 -08006362}
6363
NeilBrownec32a2b2009-03-31 15:17:38 +11006364/* This is called from the raid5d thread with mddev_lock held.
6365 * It makes config changes to the device.
6366 */
NeilBrownfd01b882011-10-11 16:47:53 +11006367static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006368{
NeilBrownd1688a62011-10-11 16:49:52 +11006369 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006370
6371 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6372
NeilBrownec32a2b2009-03-31 15:17:38 +11006373 if (mddev->delta_disks > 0) {
6374 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6375 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006376 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006377 } else {
6378 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006379 spin_lock_irq(&conf->device_lock);
6380 mddev->degraded = calc_degraded(conf);
6381 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006382 for (d = conf->raid_disks ;
6383 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006384 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006385 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006386 if (rdev)
6387 clear_bit(In_sync, &rdev->flags);
6388 rdev = conf->disks[d].replacement;
6389 if (rdev)
6390 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006391 }
NeilBrowncea9c222009-03-31 15:15:05 +11006392 }
NeilBrown88ce4932009-03-31 15:24:23 +11006393 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006394 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006395 mddev->reshape_position = MaxSector;
6396 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006397 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006398 }
6399}
6400
NeilBrownfd01b882011-10-11 16:47:53 +11006401static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006402{
NeilBrownd1688a62011-10-11 16:49:52 +11006403 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006404
6405 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006406 case 2: /* resume for a suspend */
6407 wake_up(&conf->wait_for_overlap);
6408 break;
6409
NeilBrown72626682005-09-09 16:23:54 -07006410 case 1: /* stop all writes */
6411 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006412 /* '2' tells resync/reshape to pause so that all
6413 * active stripes can drain
6414 */
6415 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006416 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006417 atomic_read(&conf->active_stripes) == 0 &&
6418 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006419 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006420 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006421 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006422 /* allow reshape to continue */
6423 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006424 break;
6425
6426 case 0: /* re-enable writes */
6427 spin_lock_irq(&conf->device_lock);
6428 conf->quiesce = 0;
6429 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006430 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006431 spin_unlock_irq(&conf->device_lock);
6432 break;
6433 }
NeilBrown72626682005-09-09 16:23:54 -07006434}
NeilBrownb15c2e52006-01-06 00:20:16 -08006435
NeilBrownd562b0c2009-03-31 14:39:39 +11006436
NeilBrownfd01b882011-10-11 16:47:53 +11006437static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006438{
NeilBrowne373ab12011-10-11 16:48:59 +11006439 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006440 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006441
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006442 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006443 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006444 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6445 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006446 return ERR_PTR(-EINVAL);
6447 }
6448
NeilBrowne373ab12011-10-11 16:48:59 +11006449 sectors = raid0_conf->strip_zone[0].zone_end;
6450 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006451 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006452 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006453 mddev->new_layout = ALGORITHM_PARITY_N;
6454 mddev->new_chunk_sectors = mddev->chunk_sectors;
6455 mddev->raid_disks += 1;
6456 mddev->delta_disks = 1;
6457 /* make sure it will be not marked as dirty */
6458 mddev->recovery_cp = MaxSector;
6459
6460 return setup_conf(mddev);
6461}
6462
6463
NeilBrownfd01b882011-10-11 16:47:53 +11006464static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006465{
6466 int chunksect;
6467
6468 if (mddev->raid_disks != 2 ||
6469 mddev->degraded > 1)
6470 return ERR_PTR(-EINVAL);
6471
6472 /* Should check if there are write-behind devices? */
6473
6474 chunksect = 64*2; /* 64K by default */
6475
6476 /* The array must be an exact multiple of chunksize */
6477 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6478 chunksect >>= 1;
6479
6480 if ((chunksect<<9) < STRIPE_SIZE)
6481 /* array size does not allow a suitable chunk size */
6482 return ERR_PTR(-EINVAL);
6483
6484 mddev->new_level = 5;
6485 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006486 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006487
6488 return setup_conf(mddev);
6489}
6490
NeilBrownfd01b882011-10-11 16:47:53 +11006491static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006492{
6493 int new_layout;
6494
6495 switch (mddev->layout) {
6496 case ALGORITHM_LEFT_ASYMMETRIC_6:
6497 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6498 break;
6499 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6500 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6501 break;
6502 case ALGORITHM_LEFT_SYMMETRIC_6:
6503 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6504 break;
6505 case ALGORITHM_RIGHT_SYMMETRIC_6:
6506 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6507 break;
6508 case ALGORITHM_PARITY_0_6:
6509 new_layout = ALGORITHM_PARITY_0;
6510 break;
6511 case ALGORITHM_PARITY_N:
6512 new_layout = ALGORITHM_PARITY_N;
6513 break;
6514 default:
6515 return ERR_PTR(-EINVAL);
6516 }
6517 mddev->new_level = 5;
6518 mddev->new_layout = new_layout;
6519 mddev->delta_disks = -1;
6520 mddev->raid_disks -= 1;
6521 return setup_conf(mddev);
6522}
6523
NeilBrownd562b0c2009-03-31 14:39:39 +11006524
NeilBrownfd01b882011-10-11 16:47:53 +11006525static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006526{
NeilBrown88ce4932009-03-31 15:24:23 +11006527 /* For a 2-drive array, the layout and chunk size can be changed
6528 * immediately as not restriping is needed.
6529 * For larger arrays we record the new value - after validation
6530 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006531 */
NeilBrownd1688a62011-10-11 16:49:52 +11006532 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006533 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006534
NeilBrown597a7112009-06-18 08:47:42 +10006535 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006536 return -EINVAL;
6537 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006538 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006539 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006540 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006541 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006542 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006543 /* not factor of array size */
6544 return -EINVAL;
6545 }
6546
6547 /* They look valid */
6548
NeilBrown88ce4932009-03-31 15:24:23 +11006549 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006550 /* can make the change immediately */
6551 if (mddev->new_layout >= 0) {
6552 conf->algorithm = mddev->new_layout;
6553 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006554 }
6555 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006556 conf->chunk_sectors = new_chunk ;
6557 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006558 }
6559 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6560 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006561 }
NeilBrown50ac1682009-06-18 08:47:55 +10006562 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006563}
6564
NeilBrownfd01b882011-10-11 16:47:53 +11006565static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006566{
NeilBrown597a7112009-06-18 08:47:42 +10006567 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006568
NeilBrown597a7112009-06-18 08:47:42 +10006569 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006570 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006571 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006572 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006573 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006574 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006575 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006576 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006577 /* not factor of array size */
6578 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006579 }
NeilBrown88ce4932009-03-31 15:24:23 +11006580
6581 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006582 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006583}
6584
NeilBrownfd01b882011-10-11 16:47:53 +11006585static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006586{
6587 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006588 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006589 * raid1 - if there are two drives. We need to know the chunk size
6590 * raid4 - trivial - just use a raid4 layout.
6591 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006592 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006593 if (mddev->level == 0)
6594 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006595 if (mddev->level == 1)
6596 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006597 if (mddev->level == 4) {
6598 mddev->new_layout = ALGORITHM_PARITY_N;
6599 mddev->new_level = 5;
6600 return setup_conf(mddev);
6601 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006602 if (mddev->level == 6)
6603 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006604
6605 return ERR_PTR(-EINVAL);
6606}
6607
NeilBrownfd01b882011-10-11 16:47:53 +11006608static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006609{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006610 /* raid4 can take over:
6611 * raid0 - if there is only one strip zone
6612 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006613 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006614 if (mddev->level == 0)
6615 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006616 if (mddev->level == 5 &&
6617 mddev->layout == ALGORITHM_PARITY_N) {
6618 mddev->new_layout = 0;
6619 mddev->new_level = 4;
6620 return setup_conf(mddev);
6621 }
6622 return ERR_PTR(-EINVAL);
6623}
NeilBrownd562b0c2009-03-31 14:39:39 +11006624
NeilBrown84fc4b52011-10-11 16:49:58 +11006625static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006626
NeilBrownfd01b882011-10-11 16:47:53 +11006627static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006628{
6629 /* Currently can only take over a raid5. We map the
6630 * personality to an equivalent raid6 personality
6631 * with the Q block at the end.
6632 */
6633 int new_layout;
6634
6635 if (mddev->pers != &raid5_personality)
6636 return ERR_PTR(-EINVAL);
6637 if (mddev->degraded > 1)
6638 return ERR_PTR(-EINVAL);
6639 if (mddev->raid_disks > 253)
6640 return ERR_PTR(-EINVAL);
6641 if (mddev->raid_disks < 3)
6642 return ERR_PTR(-EINVAL);
6643
6644 switch (mddev->layout) {
6645 case ALGORITHM_LEFT_ASYMMETRIC:
6646 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6647 break;
6648 case ALGORITHM_RIGHT_ASYMMETRIC:
6649 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6650 break;
6651 case ALGORITHM_LEFT_SYMMETRIC:
6652 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6653 break;
6654 case ALGORITHM_RIGHT_SYMMETRIC:
6655 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6656 break;
6657 case ALGORITHM_PARITY_0:
6658 new_layout = ALGORITHM_PARITY_0_6;
6659 break;
6660 case ALGORITHM_PARITY_N:
6661 new_layout = ALGORITHM_PARITY_N;
6662 break;
6663 default:
6664 return ERR_PTR(-EINVAL);
6665 }
6666 mddev->new_level = 6;
6667 mddev->new_layout = new_layout;
6668 mddev->delta_disks = 1;
6669 mddev->raid_disks += 1;
6670 return setup_conf(mddev);
6671}
6672
6673
NeilBrown84fc4b52011-10-11 16:49:58 +11006674static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006675{
6676 .name = "raid6",
6677 .level = 6,
6678 .owner = THIS_MODULE,
6679 .make_request = make_request,
6680 .run = run,
6681 .stop = stop,
6682 .status = status,
6683 .error_handler = error,
6684 .hot_add_disk = raid5_add_disk,
6685 .hot_remove_disk= raid5_remove_disk,
6686 .spare_active = raid5_spare_active,
6687 .sync_request = sync_request,
6688 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006689 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006690 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006691 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006692 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006693 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006694 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006695};
NeilBrown84fc4b52011-10-11 16:49:58 +11006696static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697{
6698 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006699 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 .owner = THIS_MODULE,
6701 .make_request = make_request,
6702 .run = run,
6703 .stop = stop,
6704 .status = status,
6705 .error_handler = error,
6706 .hot_add_disk = raid5_add_disk,
6707 .hot_remove_disk= raid5_remove_disk,
6708 .spare_active = raid5_spare_active,
6709 .sync_request = sync_request,
6710 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006711 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006712 .check_reshape = raid5_check_reshape,
6713 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006714 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006715 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006716 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717};
6718
NeilBrown84fc4b52011-10-11 16:49:58 +11006719static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720{
NeilBrown2604b702006-01-06 00:20:36 -08006721 .name = "raid4",
6722 .level = 4,
6723 .owner = THIS_MODULE,
6724 .make_request = make_request,
6725 .run = run,
6726 .stop = stop,
6727 .status = status,
6728 .error_handler = error,
6729 .hot_add_disk = raid5_add_disk,
6730 .hot_remove_disk= raid5_remove_disk,
6731 .spare_active = raid5_spare_active,
6732 .sync_request = sync_request,
6733 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006734 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006735 .check_reshape = raid5_check_reshape,
6736 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006737 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006738 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006739 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006740};
6741
6742static int __init raid5_init(void)
6743{
Shaohua Li851c30c2013-08-28 14:30:16 +08006744 raid5_wq = alloc_workqueue("raid5wq",
6745 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
6746 if (!raid5_wq)
6747 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07006748 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006749 register_md_personality(&raid5_personality);
6750 register_md_personality(&raid4_personality);
6751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752}
6753
NeilBrown2604b702006-01-06 00:20:36 -08006754static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755{
NeilBrown16a53ec2006-06-26 00:27:38 -07006756 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006757 unregister_md_personality(&raid5_personality);
6758 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08006759 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760}
6761
6762module_init(raid5_init);
6763module_exit(raid5_exit);
6764MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006765MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006767MODULE_ALIAS("md-raid5");
6768MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006769MODULE_ALIAS("md-level-5");
6770MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006771MODULE_ALIAS("md-personality-8"); /* RAID6 */
6772MODULE_ALIAS("md-raid6");
6773MODULE_ALIAS("md-level-6");
6774
6775/* This used to be two separate modules, they were: */
6776MODULE_ALIAS("raid5");
6777MODULE_ALIAS("raid6");