blob: 348a857ab0fff23bba5d5b79611dd190ac031a77 [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)
Shaohua Libfc90cb2013-08-29 15:40:32 +080080#define MAX_STRIPE_BATCH 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
NeilBrownd1688a62011-10-11 16:49:52 +110082static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110083{
84 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
85 return &conf->stripe_hashtbl[hash];
86}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Shaohua Li566c09c2013-11-14 15:16:17 +110088static inline int stripe_hash_locks_hash(sector_t sect)
89{
90 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
91}
92
93static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
94{
95 spin_lock_irq(conf->hash_locks + hash);
96 spin_lock(&conf->device_lock);
97}
98
99static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
100{
101 spin_unlock(&conf->device_lock);
102 spin_unlock_irq(conf->hash_locks + hash);
103}
104
105static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
106{
107 int i;
108 local_irq_disable();
109 spin_lock(conf->hash_locks);
110 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
111 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
112 spin_lock(&conf->device_lock);
113}
114
115static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
116{
117 int i;
118 spin_unlock(&conf->device_lock);
119 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
120 spin_unlock(conf->hash_locks + i - 1);
121 local_irq_enable();
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* bio's attached to a stripe+device for I/O are linked together in bi_sector
125 * order without overlap. There may be several bio's per stripe+device, and
126 * a bio could span several devices.
127 * When walking this list for a particular stripe+device, we must never proceed
128 * beyond a bio that extends past this device, as the next bio might no longer
129 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +1100130 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * of the current stripe+device
132 */
NeilBrowndb298e12011-10-07 14:23:00 +1100133static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
134{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800135 int sectors = bio_sectors(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700136 if (bio->bi_iter.bi_sector + sectors < sector + STRIPE_SECTORS)
NeilBrowndb298e12011-10-07 14:23:00 +1100137 return bio->bi_next;
138 else
139 return NULL;
140}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jens Axboe960e7392008-08-15 10:41:18 +0200142/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200143 * We maintain a biased count of active stripes in the bottom 16 bits of
144 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200145 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000146static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200147{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000148 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
149 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200150}
151
Shaohua Lie7836bd62012-07-19 16:01:31 +1000152static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200153{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000154 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
155 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200156}
157
Shaohua Lie7836bd62012-07-19 16:01:31 +1000158static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200159{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000160 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
161 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200162}
163
Shaohua Lie7836bd62012-07-19 16:01:31 +1000164static inline void raid5_set_bi_processed_stripes(struct bio *bio,
165 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200166{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000167 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
168 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200169
Shaohua Lie7836bd62012-07-19 16:01:31 +1000170 do {
171 old = atomic_read(segments);
172 new = (old & 0xffff) | (cnt << 16);
173 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200174}
175
Shaohua Lie7836bd62012-07-19 16:01:31 +1000176static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200177{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000178 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
179 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200180}
181
NeilBrownd0dabf72009-03-31 14:39:38 +1100182/* Find first data disk in a raid6 stripe */
183static inline int raid6_d0(struct stripe_head *sh)
184{
NeilBrown67cc2b82009-03-31 14:39:38 +1100185 if (sh->ddf_layout)
186 /* ddf always start from first device */
187 return 0;
188 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100189 if (sh->qd_idx == sh->disks - 1)
190 return 0;
191 else
192 return sh->qd_idx + 1;
193}
NeilBrown16a53ec2006-06-26 00:27:38 -0700194static inline int raid6_next_disk(int disk, int raid_disks)
195{
196 disk++;
197 return (disk < raid_disks) ? disk : 0;
198}
Dan Williamsa4456852007-07-09 11:56:43 -0700199
NeilBrownd0dabf72009-03-31 14:39:38 +1100200/* When walking through the disks in a raid5, starting at raid6_d0,
201 * We need to map each disk to a 'slot', where the data disks are slot
202 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
203 * is raid_disks-1. This help does that mapping.
204 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100205static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
206 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100207{
Dan Williams66295422009-10-19 18:09:32 -0700208 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100209
NeilBrowne4424fe2009-10-16 16:27:34 +1100210 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700211 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100212 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100213 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100214 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100215 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100216 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700217 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100218 return slot;
219}
220
Dan Williamsa4456852007-07-09 11:56:43 -0700221static void return_io(struct bio *return_bi)
222{
223 struct bio *bi = return_bi;
224 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700225
226 return_bi = bi->bi_next;
227 bi->bi_next = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700228 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700229 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
230 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000231 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700232 bi = return_bi;
233 }
234}
235
NeilBrownd1688a62011-10-11 16:49:52 +1100236static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Dan Williams600aa102008-06-28 08:32:05 +1000238static int stripe_operations_active(struct stripe_head *sh)
239{
240 return sh->check_state || sh->reconstruct_state ||
241 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
242 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
243}
244
Shaohua Li851c30c2013-08-28 14:30:16 +0800245static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
246{
247 struct r5conf *conf = sh->raid_conf;
248 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800249 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800250 int i, cpu = sh->cpu;
251
252 if (!cpu_online(cpu)) {
253 cpu = cpumask_any(cpu_online_mask);
254 sh->cpu = cpu;
255 }
256
257 if (list_empty(&sh->lru)) {
258 struct r5worker_group *group;
259 group = conf->worker_groups + cpu_to_group(cpu);
260 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800261 group->stripes_cnt++;
262 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800263 }
264
265 if (conf->worker_cnt_per_group == 0) {
266 md_wakeup_thread(conf->mddev->thread);
267 return;
268 }
269
270 group = conf->worker_groups + cpu_to_group(sh->cpu);
271
Shaohua Libfc90cb2013-08-29 15:40:32 +0800272 group->workers[0].working = true;
273 /* at least one worker should run to avoid race */
274 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
275
276 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
277 /* wakeup more workers */
278 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
279 if (group->workers[i].working == false) {
280 group->workers[i].working = true;
281 queue_work_on(sh->cpu, raid5_wq,
282 &group->workers[i].work);
283 thread_cnt--;
284 }
285 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800286}
287
Shaohua Li566c09c2013-11-14 15:16:17 +1100288static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
289 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000291 BUG_ON(!list_empty(&sh->lru));
292 BUG_ON(atomic_read(&conf->active_stripes)==0);
293 if (test_bit(STRIPE_HANDLE, &sh->state)) {
294 if (test_bit(STRIPE_DELAYED, &sh->state) &&
NeilBrown67f45542014-05-28 13:39:22 +1000295 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Shaohua Li4eb788d2012-07-19 16:01:31 +1000296 list_add_tail(&sh->lru, &conf->delayed_list);
NeilBrown67f45542014-05-28 13:39:22 +1000297 if (atomic_read(&conf->preread_active_stripes)
298 < IO_THRESHOLD)
299 md_wakeup_thread(conf->mddev->thread);
300 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000301 sh->bm_seq - conf->seq_write > 0)
302 list_add_tail(&sh->lru, &conf->bitmap_list);
303 else {
304 clear_bit(STRIPE_DELAYED, &sh->state);
305 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800306 if (conf->worker_cnt_per_group == 0) {
307 list_add_tail(&sh->lru, &conf->handle_list);
308 } else {
309 raid5_wakeup_stripe_thread(sh);
310 return;
311 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000312 }
313 md_wakeup_thread(conf->mddev->thread);
314 } else {
315 BUG_ON(stripe_operations_active(sh));
316 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
317 if (atomic_dec_return(&conf->preread_active_stripes)
318 < IO_THRESHOLD)
319 md_wakeup_thread(conf->mddev->thread);
320 atomic_dec(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100321 if (!test_bit(STRIPE_EXPANDING, &sh->state))
322 list_add_tail(&sh->lru, temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324}
NeilBrownd0dabf72009-03-31 14:39:38 +1100325
Shaohua Li566c09c2013-11-14 15:16:17 +1100326static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
327 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000328{
329 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100330 do_release_stripe(conf, sh, temp_inactive_list);
331}
332
333/*
334 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
335 *
336 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
337 * given time. Adding stripes only takes device lock, while deleting stripes
338 * only takes hash lock.
339 */
340static void release_inactive_stripe_list(struct r5conf *conf,
341 struct list_head *temp_inactive_list,
342 int hash)
343{
344 int size;
345 bool do_wakeup = false;
346 unsigned long flags;
347
348 if (hash == NR_STRIPE_HASH_LOCKS) {
349 size = NR_STRIPE_HASH_LOCKS;
350 hash = NR_STRIPE_HASH_LOCKS - 1;
351 } else
352 size = 1;
353 while (size) {
354 struct list_head *list = &temp_inactive_list[size - 1];
355
356 /*
357 * We don't hold any lock here yet, get_active_stripe() might
358 * remove stripes from the list
359 */
360 if (!list_empty_careful(list)) {
361 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100362 if (list_empty(conf->inactive_list + hash) &&
363 !list_empty(list))
364 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100365 list_splice_tail_init(list, conf->inactive_list + hash);
366 do_wakeup = true;
367 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
368 }
369 size--;
370 hash--;
371 }
372
373 if (do_wakeup) {
374 wake_up(&conf->wait_for_stripe);
375 if (conf->retry_read_aligned)
376 md_wakeup_thread(conf->mddev->thread);
377 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000378}
379
Shaohua Li773ca822013-08-27 17:50:39 +0800380/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100381static int release_stripe_list(struct r5conf *conf,
382 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800383{
384 struct stripe_head *sh;
385 int count = 0;
386 struct llist_node *head;
387
388 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800389 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800390 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100391 int hash;
392
Shaohua Li773ca822013-08-27 17:50:39 +0800393 sh = llist_entry(head, struct stripe_head, release_list);
394 head = llist_next(head);
395 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
396 smp_mb();
397 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
398 /*
399 * Don't worry the bit is set here, because if the bit is set
400 * again, the count is always > 1. This is true for
401 * STRIPE_ON_UNPLUG_LIST bit too.
402 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100403 hash = sh->hash_lock_index;
404 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800405 count++;
406 }
407
408 return count;
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static void release_stripe(struct stripe_head *sh)
412{
NeilBrownd1688a62011-10-11 16:49:52 +1100413 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100415 struct list_head list;
416 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800417 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700418
Eivind Sartocf170f32014-05-28 13:39:23 +1000419 /* Avoid release_list until the last reference.
420 */
421 if (atomic_add_unless(&sh->count, -1, 1))
422 return;
423
majianpengad4068d2013-11-14 15:16:15 +1100424 if (unlikely(!conf->mddev->thread) ||
425 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800426 goto slow_path;
427 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
428 if (wakeup)
429 md_wakeup_thread(conf->mddev->thread);
430 return;
431slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000432 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800433 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000434 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100435 INIT_LIST_HEAD(&list);
436 hash = sh->hash_lock_index;
437 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000438 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100439 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000440 }
441 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
NeilBrownfccddba2006-01-06 00:20:33 -0800444static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Dan Williams45b42332007-07-09 11:56:43 -0700446 pr_debug("remove_hash(), stripe %llu\n",
447 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
NeilBrownfccddba2006-01-06 00:20:33 -0800449 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
NeilBrownd1688a62011-10-11 16:49:52 +1100452static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
NeilBrownfccddba2006-01-06 00:20:33 -0800454 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dan Williams45b42332007-07-09 11:56:43 -0700456 pr_debug("insert_hash(), stripe %llu\n",
457 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
NeilBrownfccddba2006-01-06 00:20:33 -0800459 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462
463/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100464static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct stripe_head *sh = NULL;
467 struct list_head *first;
468
Shaohua Li566c09c2013-11-14 15:16:17 +1100469 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100471 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 sh = list_entry(first, struct stripe_head, lru);
473 list_del_init(first);
474 remove_hash(sh);
475 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100476 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100477 if (list_empty(conf->inactive_list + hash))
478 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479out:
480 return sh;
481}
482
NeilBrowne4e11e32010-06-16 16:45:16 +1000483static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct page *p;
486 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000487 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
NeilBrowne4e11e32010-06-16 16:45:16 +1000489 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 p = sh->dev[i].page;
491 if (!p)
492 continue;
493 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800494 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496}
497
NeilBrowne4e11e32010-06-16 16:45:16 +1000498static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000501 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
NeilBrowne4e11e32010-06-16 16:45:16 +1000503 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct page *page;
505
506 if (!(page = alloc_page(GFP_KERNEL))) {
507 return 1;
508 }
509 sh->dev[i].page = page;
510 }
511 return 0;
512}
513
NeilBrown784052e2009-03-31 15:19:07 +1100514static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100515static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100516 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
NeilBrownb5663ba2009-03-31 14:39:38 +1100518static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
NeilBrownd1688a62011-10-11 16:49:52 +1100520 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100521 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200523 BUG_ON(atomic_read(&sh->count) != 0);
524 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000525 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700526
Dan Williams45b42332007-07-09 11:56:43 -0700527 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 (unsigned long long)sh->sector);
529
530 remove_hash(sh);
Shaohua Li566c09c2013-11-14 15:16:17 +1100531retry:
532 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100533 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100534 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100536 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sh->state = 0;
538
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800539
540 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct r5dev *dev = &sh->dev[i];
542
Dan Williamsd84e0f12007-01-02 13:52:30 -0700543 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700545 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700547 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000549 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100552 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100554 if (read_seqcount_retry(&conf->gen_lock, seq))
555 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800557 sh->cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
NeilBrownd1688a62011-10-11 16:49:52 +1100560static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100561 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct stripe_head *sh;
564
Dan Williams45b42332007-07-09 11:56:43 -0700565 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800566 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100567 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700569 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return NULL;
571}
572
NeilBrown674806d2010-06-16 17:17:53 +1000573/*
574 * Need to check if array has failed when deciding whether to:
575 * - start an array
576 * - remove non-faulty devices
577 * - add a spare
578 * - allow a reshape
579 * This determination is simple when no reshape is happening.
580 * However if there is a reshape, we need to carefully check
581 * both the before and after sections.
582 * This is because some failed devices may only affect one
583 * of the two sections, and some non-in_sync devices may
584 * be insync in the section most affected by failed devices.
585 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100586static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000587{
NeilBrown908f4fb2011-12-23 10:17:50 +1100588 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000589 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000590
591 rcu_read_lock();
592 degraded = 0;
593 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100594 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000595 if (rdev && test_bit(Faulty, &rdev->flags))
596 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000597 if (!rdev || test_bit(Faulty, &rdev->flags))
598 degraded++;
599 else if (test_bit(In_sync, &rdev->flags))
600 ;
601 else
602 /* not in-sync or faulty.
603 * If the reshape increases the number of devices,
604 * this is being recovered by the reshape, so
605 * this 'previous' section is not in_sync.
606 * If the number of devices is being reduced however,
607 * the device can only be part of the array if
608 * we are reverting a reshape, so this section will
609 * be in-sync.
610 */
611 if (conf->raid_disks >= conf->previous_raid_disks)
612 degraded++;
613 }
614 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100615 if (conf->raid_disks == conf->previous_raid_disks)
616 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000617 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100618 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000619 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100620 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000621 if (rdev && test_bit(Faulty, &rdev->flags))
622 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000623 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100624 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000625 else if (test_bit(In_sync, &rdev->flags))
626 ;
627 else
628 /* not in-sync or faulty.
629 * If reshape increases the number of devices, this
630 * section has already been recovered, else it
631 * almost certainly hasn't.
632 */
633 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100634 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000635 }
636 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100637 if (degraded2 > degraded)
638 return degraded2;
639 return degraded;
640}
641
642static int has_failed(struct r5conf *conf)
643{
644 int degraded;
645
646 if (conf->mddev->reshape_position == MaxSector)
647 return conf->mddev->degraded > conf->max_degraded;
648
649 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000650 if (degraded > conf->max_degraded)
651 return 1;
652 return 0;
653}
654
NeilBrownb5663ba2009-03-31 14:39:38 +1100655static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100656get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000657 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100660 int hash = stripe_hash_locks_hash(sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Dan Williams45b42332007-07-09 11:56:43 -0700662 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Shaohua Li566c09c2013-11-14 15:16:17 +1100664 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 do {
NeilBrown72626682005-09-09 16:23:54 -0700667 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000668 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100669 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100670 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!sh) {
672 if (!conf->inactive_blocked)
Shaohua Li566c09c2013-11-14 15:16:17 +1100673 sh = get_free_stripe(conf, hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (noblock && sh == NULL)
675 break;
676 if (!sh) {
677 conf->inactive_blocked = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +1100678 wait_event_lock_irq(
679 conf->wait_for_stripe,
680 !list_empty(conf->inactive_list + hash) &&
681 (atomic_read(&conf->active_stripes)
682 < (conf->max_nr_stripes * 3 / 4)
683 || !conf->inactive_blocked),
684 *(conf->hash_locks + hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 conf->inactive_blocked = 0;
NeilBrown7da9d452014-01-22 11:45:03 +1100686 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100687 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100688 atomic_inc(&sh->count);
689 }
Shaohua Lie240c182014-04-09 11:27:42 +0800690 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100691 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800692 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!test_bit(STRIPE_HANDLE, &sh->state))
694 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100695 BUG_ON(list_empty(&sh->lru) &&
696 !test_bit(STRIPE_EXPANDING, &sh->state));
NeilBrown16a53ec2006-06-26 00:27:38 -0700697 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800698 if (sh->group) {
699 sh->group->stripes_cnt--;
700 sh->group = NULL;
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
NeilBrown7da9d452014-01-22 11:45:03 +1100703 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100704 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 } while (sh == NULL);
707
Shaohua Li566c09c2013-11-14 15:16:17 +1100708 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return sh;
710}
711
NeilBrown05616be2012-05-21 09:27:00 +1000712/* Determine if 'data_offset' or 'new_data_offset' should be used
713 * in this stripe_head.
714 */
715static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
716{
717 sector_t progress = conf->reshape_progress;
718 /* Need a memory barrier to make sure we see the value
719 * of conf->generation, or ->data_offset that was set before
720 * reshape_progress was updated.
721 */
722 smp_rmb();
723 if (progress == MaxSector)
724 return 0;
725 if (sh->generation == conf->generation - 1)
726 return 0;
727 /* We are in a reshape, and this is a new-generation stripe,
728 * so use new_data_offset.
729 */
730 return 1;
731}
732
NeilBrown6712ecf2007-09-27 12:47:43 +0200733static void
734raid5_end_read_request(struct bio *bi, int error);
735static void
736raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700737
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000738static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700739{
NeilBrownd1688a62011-10-11 16:49:52 +1100740 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700741 int i, disks = sh->disks;
742
743 might_sleep();
744
745 for (i = disks; i--; ) {
746 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100747 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100748 struct bio *bi, *rbi;
749 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200750 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
751 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
752 rw = WRITE_FUA;
753 else
754 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100755 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100756 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200757 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700758 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100759 else if (test_and_clear_bit(R5_WantReplace,
760 &sh->dev[i].flags)) {
761 rw = WRITE;
762 replace_only = 1;
763 } else
Dan Williams91c00922007-01-02 13:52:30 -0700764 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000765 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
766 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700767
768 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100769 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700770
Dan Williams91c00922007-01-02 13:52:30 -0700771 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100772 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100773 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
774 rdev = rcu_dereference(conf->disks[i].rdev);
775 if (!rdev) {
776 rdev = rrdev;
777 rrdev = NULL;
778 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100779 if (rw & WRITE) {
780 if (replace_only)
781 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100782 if (rdev == rrdev)
783 /* We raced and saw duplicates */
784 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100785 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100786 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100787 rdev = rrdev;
788 rrdev = NULL;
789 }
NeilBrown977df362011-12-23 10:17:53 +1100790
Dan Williams91c00922007-01-02 13:52:30 -0700791 if (rdev && test_bit(Faulty, &rdev->flags))
792 rdev = NULL;
793 if (rdev)
794 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100795 if (rrdev && test_bit(Faulty, &rrdev->flags))
796 rrdev = NULL;
797 if (rrdev)
798 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700799 rcu_read_unlock();
800
NeilBrown73e92e52011-07-28 11:39:22 +1000801 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100802 * need to check for writes. We never accept write errors
803 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000804 */
805 while ((rw & WRITE) && rdev &&
806 test_bit(WriteErrorSeen, &rdev->flags)) {
807 sector_t first_bad;
808 int bad_sectors;
809 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
810 &first_bad, &bad_sectors);
811 if (!bad)
812 break;
813
814 if (bad < 0) {
815 set_bit(BlockedBadBlocks, &rdev->flags);
816 if (!conf->mddev->external &&
817 conf->mddev->flags) {
818 /* It is very unlikely, but we might
819 * still need to write out the
820 * bad block log - better give it
821 * a chance*/
822 md_check_recovery(conf->mddev);
823 }
majianpeng18507532012-07-03 12:11:54 +1000824 /*
825 * Because md_wait_for_blocked_rdev
826 * will dec nr_pending, we must
827 * increment it first.
828 */
829 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000830 md_wait_for_blocked_rdev(rdev, conf->mddev);
831 } else {
832 /* Acknowledged bad block - skip the write */
833 rdev_dec_pending(rdev, conf->mddev);
834 rdev = NULL;
835 }
836 }
837
Dan Williams91c00922007-01-02 13:52:30 -0700838 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100839 if (s->syncing || s->expanding || s->expanded
840 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700841 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
842
Dan Williams2b7497f2008-06-28 08:31:52 +1000843 set_bit(STRIPE_IO_STARTED, &sh->state);
844
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700845 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700846 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700847 bi->bi_rw = rw;
848 bi->bi_end_io = (rw & WRITE)
849 ? raid5_end_write_request
850 : raid5_end_read_request;
851 bi->bi_private = sh;
852
Dan Williams91c00922007-01-02 13:52:30 -0700853 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700854 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700855 bi->bi_rw, i);
856 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000857 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700858 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000859 + rdev->new_data_offset);
860 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700861 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000862 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000863 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
majianpenge59aa232013-11-14 15:16:19 +1100864 bi->bi_rw |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +1000865
Kent Overstreet4997b722013-05-30 08:44:39 +0200866 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700867 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
868 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700869 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800870 /*
871 * If this is discard request, set bi_vcnt 0. We don't
872 * want to confuse SCSI because SCSI will replace payload
873 */
874 if (rw & REQ_DISCARD)
875 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +1100876 if (rrdev)
877 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600878
879 if (conf->mddev->gendisk)
880 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
881 bi, disk_devt(conf->mddev->gendisk),
882 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700883 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100884 }
885 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100886 if (s->syncing || s->expanding || s->expanded
887 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100888 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
889
890 set_bit(STRIPE_IO_STARTED, &sh->state);
891
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700892 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100893 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700894 rbi->bi_rw = rw;
895 BUG_ON(!(rw & WRITE));
896 rbi->bi_end_io = raid5_end_write_request;
897 rbi->bi_private = sh;
898
NeilBrown977df362011-12-23 10:17:53 +1100899 pr_debug("%s: for %llu schedule op %ld on "
900 "replacement disc %d\n",
901 __func__, (unsigned long long)sh->sector,
902 rbi->bi_rw, i);
903 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000904 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700905 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000906 + rrdev->new_data_offset);
907 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700908 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +1000909 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200910 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100911 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
912 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700913 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +0800914 /*
915 * If this is discard request, set bi_vcnt 0. We don't
916 * want to confuse SCSI because SCSI will replace payload
917 */
918 if (rw & REQ_DISCARD)
919 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600920 if (conf->mddev->gendisk)
921 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
922 rbi, disk_devt(conf->mddev->gendisk),
923 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100924 generic_make_request(rbi);
925 }
926 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000927 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700928 set_bit(STRIPE_DEGRADED, &sh->state);
929 pr_debug("skip op %ld on disc %d for sector %llu\n",
930 bi->bi_rw, i, (unsigned long long)sh->sector);
931 clear_bit(R5_LOCKED, &sh->dev[i].flags);
932 set_bit(STRIPE_HANDLE, &sh->state);
933 }
934 }
935}
936
937static struct dma_async_tx_descriptor *
938async_copy_data(int frombio, struct bio *bio, struct page *page,
939 sector_t sector, struct dma_async_tx_descriptor *tx)
940{
Kent Overstreet79886132013-11-23 17:19:00 -0800941 struct bio_vec bvl;
942 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -0700943 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -0700944 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700945 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700946 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700947
Kent Overstreet4f024f32013-10-11 15:44:27 -0700948 if (bio->bi_iter.bi_sector >= sector)
949 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -0700950 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700951 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700952
Dan Williams0403e382009-09-08 17:42:50 -0700953 if (frombio)
954 flags |= ASYNC_TX_FENCE;
955 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
956
Kent Overstreet79886132013-11-23 17:19:00 -0800957 bio_for_each_segment(bvl, bio, iter) {
958 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700959 int clen;
960 int b_offset = 0;
961
962 if (page_offset < 0) {
963 b_offset = -page_offset;
964 page_offset += b_offset;
965 len -= b_offset;
966 }
967
968 if (len > 0 && page_offset + len > STRIPE_SIZE)
969 clen = STRIPE_SIZE - page_offset;
970 else
971 clen = len;
972
973 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -0800974 b_offset += bvl.bv_offset;
975 bio_page = bvl.bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700976 if (frombio)
977 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700978 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700979 else
980 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700981 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700982 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700983 /* chain the operations */
984 submit.depend_tx = tx;
985
Dan Williams91c00922007-01-02 13:52:30 -0700986 if (clen < len) /* hit end of page */
987 break;
988 page_offset += len;
989 }
990
991 return tx;
992}
993
994static void ops_complete_biofill(void *stripe_head_ref)
995{
996 struct stripe_head *sh = stripe_head_ref;
997 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700998 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700999
Harvey Harrisone46b2722008-04-28 02:15:50 -07001000 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001001 (unsigned long long)sh->sector);
1002
1003 /* clear completed biofills */
1004 for (i = sh->disks; i--; ) {
1005 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001006
1007 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001008 /* and check if we need to reply to a read request,
1009 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001010 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001011 */
1012 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001013 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001014
Dan Williams91c00922007-01-02 13:52:30 -07001015 BUG_ON(!dev->read);
1016 rbi = dev->read;
1017 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001018 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001019 dev->sector + STRIPE_SECTORS) {
1020 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10001021 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -07001022 rbi->bi_next = return_bi;
1023 return_bi = rbi;
1024 }
Dan Williams91c00922007-01-02 13:52:30 -07001025 rbi = rbi2;
1026 }
1027 }
1028 }
Dan Williams83de75c2008-06-28 08:31:58 +10001029 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001030
1031 return_io(return_bi);
1032
Dan Williamse4d84902007-09-24 10:06:13 -07001033 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001034 release_stripe(sh);
1035}
1036
1037static void ops_run_biofill(struct stripe_head *sh)
1038{
1039 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001040 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001041 int i;
1042
Harvey Harrisone46b2722008-04-28 02:15:50 -07001043 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001044 (unsigned long long)sh->sector);
1045
1046 for (i = sh->disks; i--; ) {
1047 struct r5dev *dev = &sh->dev[i];
1048 if (test_bit(R5_Wantfill, &dev->flags)) {
1049 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001050 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001051 dev->read = rbi = dev->toread;
1052 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001053 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001054 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001055 dev->sector + STRIPE_SECTORS) {
1056 tx = async_copy_data(0, rbi, dev->page,
1057 dev->sector, tx);
1058 rbi = r5_next_bio(rbi, dev->sector);
1059 }
1060 }
1061 }
1062
1063 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001064 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1065 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001066}
1067
Dan Williams4e7d2c02009-08-29 19:13:11 -07001068static void mark_target_uptodate(struct stripe_head *sh, int target)
1069{
1070 struct r5dev *tgt;
1071
1072 if (target < 0)
1073 return;
1074
1075 tgt = &sh->dev[target];
1076 set_bit(R5_UPTODATE, &tgt->flags);
1077 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1078 clear_bit(R5_Wantcompute, &tgt->flags);
1079}
1080
Dan Williamsac6b53b2009-07-14 13:40:19 -07001081static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001082{
1083 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001084
Harvey Harrisone46b2722008-04-28 02:15:50 -07001085 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001086 (unsigned long long)sh->sector);
1087
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001089 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001091
Dan Williamsecc65c92008-06-28 08:31:57 +10001092 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1093 if (sh->check_state == check_state_compute_run)
1094 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001095 set_bit(STRIPE_HANDLE, &sh->state);
1096 release_stripe(sh);
1097}
1098
Dan Williamsd6f38f32009-07-14 11:50:52 -07001099/* return a pointer to the address conversion region of the scribble buffer */
1100static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1101 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001102{
Dan Williamsd6f38f32009-07-14 11:50:52 -07001103 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
1104}
1105
1106static struct dma_async_tx_descriptor *
1107ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1108{
Dan Williams91c00922007-01-02 13:52:30 -07001109 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001110 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001111 int target = sh->ops.target;
1112 struct r5dev *tgt = &sh->dev[target];
1113 struct page *xor_dest = tgt->page;
1114 int count = 0;
1115 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001116 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001117 int i;
1118
1119 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001120 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001121 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1122
1123 for (i = disks; i--; )
1124 if (i != target)
1125 xor_srcs[count++] = sh->dev[i].page;
1126
1127 atomic_inc(&sh->count);
1128
Dan Williams0403e382009-09-08 17:42:50 -07001129 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001130 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -07001131 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001132 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001133 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001134 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001135
Dan Williams91c00922007-01-02 13:52:30 -07001136 return tx;
1137}
1138
Dan Williamsac6b53b2009-07-14 13:40:19 -07001139/* set_syndrome_sources - populate source buffers for gen_syndrome
1140 * @srcs - (struct page *) array of size sh->disks
1141 * @sh - stripe_head to parse
1142 *
1143 * Populates srcs in proper layout order for the stripe and returns the
1144 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1145 * destination buffer is recorded in srcs[count] and the Q destination
1146 * is recorded in srcs[count+1]].
1147 */
1148static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
1149{
1150 int disks = sh->disks;
1151 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1152 int d0_idx = raid6_d0(sh);
1153 int count;
1154 int i;
1155
1156 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001157 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001158
1159 count = 0;
1160 i = d0_idx;
1161 do {
1162 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1163
1164 srcs[slot] = sh->dev[i].page;
1165 i = raid6_next_disk(i, disks);
1166 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001167
NeilBrowne4424fe2009-10-16 16:27:34 +11001168 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001169}
1170
1171static struct dma_async_tx_descriptor *
1172ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1173{
1174 int disks = sh->disks;
1175 struct page **blocks = percpu->scribble;
1176 int target;
1177 int qd_idx = sh->qd_idx;
1178 struct dma_async_tx_descriptor *tx;
1179 struct async_submit_ctl submit;
1180 struct r5dev *tgt;
1181 struct page *dest;
1182 int i;
1183 int count;
1184
1185 if (sh->ops.target < 0)
1186 target = sh->ops.target2;
1187 else if (sh->ops.target2 < 0)
1188 target = sh->ops.target;
1189 else
1190 /* we should only have one valid target */
1191 BUG();
1192 BUG_ON(target < 0);
1193 pr_debug("%s: stripe %llu block: %d\n",
1194 __func__, (unsigned long long)sh->sector, target);
1195
1196 tgt = &sh->dev[target];
1197 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1198 dest = tgt->page;
1199
1200 atomic_inc(&sh->count);
1201
1202 if (target == qd_idx) {
1203 count = set_syndrome_sources(blocks, sh);
1204 blocks[count] = NULL; /* regenerating p is not necessary */
1205 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001206 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1207 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001208 to_addr_conv(sh, percpu));
1209 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1210 } else {
1211 /* Compute any data- or p-drive using XOR */
1212 count = 0;
1213 for (i = disks; i-- ; ) {
1214 if (i == target || i == qd_idx)
1215 continue;
1216 blocks[count++] = sh->dev[i].page;
1217 }
1218
Dan Williams0403e382009-09-08 17:42:50 -07001219 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1220 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001221 to_addr_conv(sh, percpu));
1222 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1223 }
1224
1225 return tx;
1226}
1227
1228static struct dma_async_tx_descriptor *
1229ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1230{
1231 int i, count, disks = sh->disks;
1232 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1233 int d0_idx = raid6_d0(sh);
1234 int faila = -1, failb = -1;
1235 int target = sh->ops.target;
1236 int target2 = sh->ops.target2;
1237 struct r5dev *tgt = &sh->dev[target];
1238 struct r5dev *tgt2 = &sh->dev[target2];
1239 struct dma_async_tx_descriptor *tx;
1240 struct page **blocks = percpu->scribble;
1241 struct async_submit_ctl submit;
1242
1243 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1244 __func__, (unsigned long long)sh->sector, target, target2);
1245 BUG_ON(target < 0 || target2 < 0);
1246 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1247 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1248
Dan Williams6c910a72009-09-16 12:24:54 -07001249 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001250 * slot number conversion for 'faila' and 'failb'
1251 */
1252 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001253 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001254 count = 0;
1255 i = d0_idx;
1256 do {
1257 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1258
1259 blocks[slot] = sh->dev[i].page;
1260
1261 if (i == target)
1262 faila = slot;
1263 if (i == target2)
1264 failb = slot;
1265 i = raid6_next_disk(i, disks);
1266 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001267
1268 BUG_ON(faila == failb);
1269 if (failb < faila)
1270 swap(faila, failb);
1271 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1272 __func__, (unsigned long long)sh->sector, faila, failb);
1273
1274 atomic_inc(&sh->count);
1275
1276 if (failb == syndrome_disks+1) {
1277 /* Q disk is one of the missing disks */
1278 if (faila == syndrome_disks) {
1279 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001280 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1281 ops_complete_compute, sh,
1282 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001283 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001284 STRIPE_SIZE, &submit);
1285 } else {
1286 struct page *dest;
1287 int data_target;
1288 int qd_idx = sh->qd_idx;
1289
1290 /* Missing D+Q: recompute D from P, then recompute Q */
1291 if (target == qd_idx)
1292 data_target = target2;
1293 else
1294 data_target = target;
1295
1296 count = 0;
1297 for (i = disks; i-- ; ) {
1298 if (i == data_target || i == qd_idx)
1299 continue;
1300 blocks[count++] = sh->dev[i].page;
1301 }
1302 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001303 init_async_submit(&submit,
1304 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1305 NULL, NULL, NULL,
1306 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001307 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1308 &submit);
1309
1310 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001311 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1312 ops_complete_compute, sh,
1313 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001314 return async_gen_syndrome(blocks, 0, count+2,
1315 STRIPE_SIZE, &submit);
1316 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001317 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001318 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1319 ops_complete_compute, sh,
1320 to_addr_conv(sh, percpu));
1321 if (failb == syndrome_disks) {
1322 /* We're missing D+P. */
1323 return async_raid6_datap_recov(syndrome_disks+2,
1324 STRIPE_SIZE, faila,
1325 blocks, &submit);
1326 } else {
1327 /* We're missing D+D. */
1328 return async_raid6_2data_recov(syndrome_disks+2,
1329 STRIPE_SIZE, faila, failb,
1330 blocks, &submit);
1331 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001332 }
1333}
1334
1335
Dan Williams91c00922007-01-02 13:52:30 -07001336static void ops_complete_prexor(void *stripe_head_ref)
1337{
1338 struct stripe_head *sh = stripe_head_ref;
1339
Harvey Harrisone46b2722008-04-28 02:15:50 -07001340 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001341 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001342}
1343
1344static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001345ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1346 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001347{
Dan Williams91c00922007-01-02 13:52:30 -07001348 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001349 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001350 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001351 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001352
1353 /* existing parity data subtracted */
1354 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1355
Harvey Harrisone46b2722008-04-28 02:15:50 -07001356 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001357 (unsigned long long)sh->sector);
1358
1359 for (i = disks; i--; ) {
1360 struct r5dev *dev = &sh->dev[i];
1361 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001362 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001363 xor_srcs[count++] = dev->page;
1364 }
1365
Dan Williams0403e382009-09-08 17:42:50 -07001366 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001367 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001368 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001369
1370 return tx;
1371}
1372
1373static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001374ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001375{
1376 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001377 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001378
Harvey Harrisone46b2722008-04-28 02:15:50 -07001379 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001380 (unsigned long long)sh->sector);
1381
1382 for (i = disks; i--; ) {
1383 struct r5dev *dev = &sh->dev[i];
1384 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001385
Dan Williamsd8ee0722008-06-28 08:32:06 +10001386 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001387 struct bio *wbi;
1388
Shaohua Lib17459c2012-07-19 16:01:31 +10001389 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001390 chosen = dev->towrite;
1391 dev->towrite = NULL;
1392 BUG_ON(dev->written);
1393 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001394 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001395
Kent Overstreet4f024f32013-10-11 15:44:27 -07001396 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001397 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001398 if (wbi->bi_rw & REQ_FUA)
1399 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001400 if (wbi->bi_rw & REQ_SYNC)
1401 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001402 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001403 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001404 else
Shaohua Li620125f2012-10-11 13:49:05 +11001405 tx = async_copy_data(1, wbi, dev->page,
1406 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001407 wbi = r5_next_bio(wbi, dev->sector);
1408 }
1409 }
1410 }
1411
1412 return tx;
1413}
1414
Dan Williamsac6b53b2009-07-14 13:40:19 -07001415static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001416{
1417 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001418 int disks = sh->disks;
1419 int pd_idx = sh->pd_idx;
1420 int qd_idx = sh->qd_idx;
1421 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001422 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001423
Harvey Harrisone46b2722008-04-28 02:15:50 -07001424 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001425 (unsigned long long)sh->sector);
1426
Shaohua Libc0934f2012-05-22 13:55:05 +10001427 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001428 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001429 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001430 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001431 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001432
Dan Williams91c00922007-01-02 13:52:30 -07001433 for (i = disks; i--; ) {
1434 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001435
Tejun Heoe9c74692010-09-03 11:56:18 +02001436 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001437 if (!discard)
1438 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001439 if (fua)
1440 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001441 if (sync)
1442 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001443 }
Dan Williams91c00922007-01-02 13:52:30 -07001444 }
1445
Dan Williamsd8ee0722008-06-28 08:32:06 +10001446 if (sh->reconstruct_state == reconstruct_state_drain_run)
1447 sh->reconstruct_state = reconstruct_state_drain_result;
1448 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1449 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1450 else {
1451 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1452 sh->reconstruct_state = reconstruct_state_result;
1453 }
Dan Williams91c00922007-01-02 13:52:30 -07001454
1455 set_bit(STRIPE_HANDLE, &sh->state);
1456 release_stripe(sh);
1457}
1458
1459static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001460ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1461 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001462{
Dan Williams91c00922007-01-02 13:52:30 -07001463 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001464 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001465 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001466 int count = 0, pd_idx = sh->pd_idx, i;
1467 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001468 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001469 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001470
Harvey Harrisone46b2722008-04-28 02:15:50 -07001471 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001472 (unsigned long long)sh->sector);
1473
Shaohua Li620125f2012-10-11 13:49:05 +11001474 for (i = 0; i < sh->disks; i++) {
1475 if (pd_idx == i)
1476 continue;
1477 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1478 break;
1479 }
1480 if (i >= sh->disks) {
1481 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001482 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1483 ops_complete_reconstruct(sh);
1484 return;
1485 }
Dan Williams91c00922007-01-02 13:52:30 -07001486 /* check if prexor is active which means only process blocks
1487 * that are part of a read-modify-write (written)
1488 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001489 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1490 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001491 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1492 for (i = disks; i--; ) {
1493 struct r5dev *dev = &sh->dev[i];
1494 if (dev->written)
1495 xor_srcs[count++] = dev->page;
1496 }
1497 } else {
1498 xor_dest = sh->dev[pd_idx].page;
1499 for (i = disks; i--; ) {
1500 struct r5dev *dev = &sh->dev[i];
1501 if (i != pd_idx)
1502 xor_srcs[count++] = dev->page;
1503 }
1504 }
1505
Dan Williams91c00922007-01-02 13:52:30 -07001506 /* 1/ if we prexor'd then the dest is reused as a source
1507 * 2/ if we did not prexor then we are redoing the parity
1508 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1509 * for the synchronous xor case
1510 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001511 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001512 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1513
1514 atomic_inc(&sh->count);
1515
Dan Williamsac6b53b2009-07-14 13:40:19 -07001516 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001517 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001518 if (unlikely(count == 1))
1519 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1520 else
1521 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001522}
1523
Dan Williamsac6b53b2009-07-14 13:40:19 -07001524static void
1525ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1526 struct dma_async_tx_descriptor *tx)
1527{
1528 struct async_submit_ctl submit;
1529 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001530 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001531
1532 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1533
Shaohua Li620125f2012-10-11 13:49:05 +11001534 for (i = 0; i < sh->disks; i++) {
1535 if (sh->pd_idx == i || sh->qd_idx == i)
1536 continue;
1537 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1538 break;
1539 }
1540 if (i >= sh->disks) {
1541 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001542 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1543 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1544 ops_complete_reconstruct(sh);
1545 return;
1546 }
1547
Dan Williamsac6b53b2009-07-14 13:40:19 -07001548 count = set_syndrome_sources(blocks, sh);
1549
1550 atomic_inc(&sh->count);
1551
1552 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1553 sh, to_addr_conv(sh, percpu));
1554 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001555}
1556
1557static void ops_complete_check(void *stripe_head_ref)
1558{
1559 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001560
Harvey Harrisone46b2722008-04-28 02:15:50 -07001561 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001562 (unsigned long long)sh->sector);
1563
Dan Williamsecc65c92008-06-28 08:31:57 +10001564 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001565 set_bit(STRIPE_HANDLE, &sh->state);
1566 release_stripe(sh);
1567}
1568
Dan Williamsac6b53b2009-07-14 13:40:19 -07001569static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001570{
Dan Williams91c00922007-01-02 13:52:30 -07001571 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001572 int pd_idx = sh->pd_idx;
1573 int qd_idx = sh->qd_idx;
1574 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001575 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001576 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001577 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001578 int count;
1579 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001580
Harvey Harrisone46b2722008-04-28 02:15:50 -07001581 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001582 (unsigned long long)sh->sector);
1583
Dan Williamsac6b53b2009-07-14 13:40:19 -07001584 count = 0;
1585 xor_dest = sh->dev[pd_idx].page;
1586 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001587 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001588 if (i == pd_idx || i == qd_idx)
1589 continue;
1590 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001591 }
1592
Dan Williamsd6f38f32009-07-14 11:50:52 -07001593 init_async_submit(&submit, 0, NULL, NULL, NULL,
1594 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001595 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001596 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001597
Dan Williams91c00922007-01-02 13:52:30 -07001598 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001599 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1600 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001601}
1602
Dan Williamsac6b53b2009-07-14 13:40:19 -07001603static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1604{
1605 struct page **srcs = percpu->scribble;
1606 struct async_submit_ctl submit;
1607 int count;
1608
1609 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1610 (unsigned long long)sh->sector, checkp);
1611
1612 count = set_syndrome_sources(srcs, sh);
1613 if (!checkp)
1614 srcs[count] = NULL;
1615
1616 atomic_inc(&sh->count);
1617 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1618 sh, to_addr_conv(sh, percpu));
1619 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1620 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1621}
1622
NeilBrown51acbce2013-02-28 09:08:34 +11001623static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001624{
1625 int overlap_clear = 0, i, disks = sh->disks;
1626 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001627 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001628 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001629 struct raid5_percpu *percpu;
1630 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001631
Dan Williamsd6f38f32009-07-14 11:50:52 -07001632 cpu = get_cpu();
1633 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001634 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001635 ops_run_biofill(sh);
1636 overlap_clear++;
1637 }
1638
Dan Williams7b3a8712008-06-28 08:32:09 +10001639 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001640 if (level < 6)
1641 tx = ops_run_compute5(sh, percpu);
1642 else {
1643 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1644 tx = ops_run_compute6_1(sh, percpu);
1645 else
1646 tx = ops_run_compute6_2(sh, percpu);
1647 }
1648 /* terminate the chain if reconstruct is not set to be run */
1649 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001650 async_tx_ack(tx);
1651 }
Dan Williams91c00922007-01-02 13:52:30 -07001652
Dan Williams600aa102008-06-28 08:32:05 +10001653 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001654 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001655
Dan Williams600aa102008-06-28 08:32:05 +10001656 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001657 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001658 overlap_clear++;
1659 }
1660
Dan Williamsac6b53b2009-07-14 13:40:19 -07001661 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1662 if (level < 6)
1663 ops_run_reconstruct5(sh, percpu, tx);
1664 else
1665 ops_run_reconstruct6(sh, percpu, tx);
1666 }
Dan Williams91c00922007-01-02 13:52:30 -07001667
Dan Williamsac6b53b2009-07-14 13:40:19 -07001668 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1669 if (sh->check_state == check_state_run)
1670 ops_run_check_p(sh, percpu);
1671 else if (sh->check_state == check_state_run_q)
1672 ops_run_check_pq(sh, percpu, 0);
1673 else if (sh->check_state == check_state_run_pq)
1674 ops_run_check_pq(sh, percpu, 1);
1675 else
1676 BUG();
1677 }
Dan Williams91c00922007-01-02 13:52:30 -07001678
Dan Williams91c00922007-01-02 13:52:30 -07001679 if (overlap_clear)
1680 for (i = disks; i--; ) {
1681 struct r5dev *dev = &sh->dev[i];
1682 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1683 wake_up(&sh->raid_conf->wait_for_overlap);
1684 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001685 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001686}
1687
Shaohua Li566c09c2013-11-14 15:16:17 +11001688static int grow_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001691 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001692 if (!sh)
1693 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001694
NeilBrown3f294f42005-11-08 21:39:25 -08001695 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001696
Shaohua Lib17459c2012-07-19 16:01:31 +10001697 spin_lock_init(&sh->stripe_lock);
1698
NeilBrowne4e11e32010-06-16 16:45:16 +10001699 if (grow_buffers(sh)) {
1700 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001701 kmem_cache_free(conf->slab_cache, sh);
1702 return 0;
1703 }
Shaohua Li566c09c2013-11-14 15:16:17 +11001704 sh->hash_lock_index = hash;
NeilBrown3f294f42005-11-08 21:39:25 -08001705 /* we just created an active stripe so... */
1706 atomic_set(&sh->count, 1);
1707 atomic_inc(&conf->active_stripes);
1708 INIT_LIST_HEAD(&sh->lru);
1709 release_stripe(sh);
1710 return 1;
1711}
1712
NeilBrownd1688a62011-10-11 16:49:52 +11001713static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001714{
Christoph Lametere18b8902006-12-06 20:33:20 -08001715 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001716 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Shaohua Li566c09c2013-11-14 15:16:17 +11001717 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
NeilBrownf4be6b42010-06-01 19:37:25 +10001719 if (conf->mddev->gendisk)
1720 sprintf(conf->cache_name[0],
1721 "raid%d-%s", conf->level, mdname(conf->mddev));
1722 else
1723 sprintf(conf->cache_name[0],
1724 "raid%d-%p", conf->level, conf->mddev);
1725 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1726
NeilBrownad01c9e2006-03-27 01:18:07 -08001727 conf->active_name = 0;
1728 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001730 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (!sc)
1732 return 1;
1733 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001734 conf->pool_size = devs;
Shaohua Li566c09c2013-11-14 15:16:17 +11001735 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
1736 while (num--) {
1737 if (!grow_one_stripe(conf, hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11001739 conf->max_nr_stripes++;
1740 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
1741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return 0;
1743}
NeilBrown29269552006-03-27 01:18:10 -08001744
Dan Williamsd6f38f32009-07-14 11:50:52 -07001745/**
1746 * scribble_len - return the required size of the scribble region
1747 * @num - total number of disks in the array
1748 *
1749 * The size must be enough to contain:
1750 * 1/ a struct page pointer for each device in the array +2
1751 * 2/ room to convert each entry in (1) to its corresponding dma
1752 * (dma_map_page()) or page (page_address()) address.
1753 *
1754 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1755 * calculate over all devices (not just the data blocks), using zeros in place
1756 * of the P and Q blocks.
1757 */
1758static size_t scribble_len(int num)
1759{
1760 size_t len;
1761
1762 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1763
1764 return len;
1765}
1766
NeilBrownd1688a62011-10-11 16:49:52 +11001767static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001768{
1769 /* Make all the stripes able to hold 'newsize' devices.
1770 * New slots in each stripe get 'page' set to a new page.
1771 *
1772 * This happens in stages:
1773 * 1/ create a new kmem_cache and allocate the required number of
1774 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001775 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001776 * to the new stripe_heads. This will have the side effect of
1777 * freezing the array as once all stripe_heads have been collected,
1778 * no IO will be possible. Old stripe heads are freed once their
1779 * pages have been transferred over, and the old kmem_cache is
1780 * freed when all stripes are done.
1781 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1782 * we simple return a failre status - no need to clean anything up.
1783 * 4/ allocate new pages for the new slots in the new stripe_heads.
1784 * If this fails, we don't bother trying the shrink the
1785 * stripe_heads down again, we just leave them as they are.
1786 * As each stripe_head is processed the new one is released into
1787 * active service.
1788 *
1789 * Once step2 is started, we cannot afford to wait for a write,
1790 * so we use GFP_NOIO allocations.
1791 */
1792 struct stripe_head *osh, *nsh;
1793 LIST_HEAD(newstripes);
1794 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001795 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001796 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001797 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001798 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11001799 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08001800
1801 if (newsize <= conf->pool_size)
1802 return 0; /* never bother to shrink */
1803
Dan Williamsb5470dc2008-06-27 21:44:04 -07001804 err = md_allow_write(conf->mddev);
1805 if (err)
1806 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001807
NeilBrownad01c9e2006-03-27 01:18:07 -08001808 /* Step 1 */
1809 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1810 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001811 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001812 if (!sc)
1813 return -ENOMEM;
1814
1815 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001816 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001817 if (!nsh)
1818 break;
1819
NeilBrownad01c9e2006-03-27 01:18:07 -08001820 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001821 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001822
1823 list_add(&nsh->lru, &newstripes);
1824 }
1825 if (i) {
1826 /* didn't get enough, give up */
1827 while (!list_empty(&newstripes)) {
1828 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1829 list_del(&nsh->lru);
1830 kmem_cache_free(sc, nsh);
1831 }
1832 kmem_cache_destroy(sc);
1833 return -ENOMEM;
1834 }
1835 /* Step 2 - Must use GFP_NOIO now.
1836 * OK, we have enough stripes, start collecting inactive
1837 * stripes and copying them over
1838 */
Shaohua Li566c09c2013-11-14 15:16:17 +11001839 hash = 0;
1840 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08001841 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11001842 lock_device_hash_lock(conf, hash);
1843 wait_event_cmd(conf->wait_for_stripe,
1844 !list_empty(conf->inactive_list + hash),
1845 unlock_device_hash_lock(conf, hash),
1846 lock_device_hash_lock(conf, hash));
1847 osh = get_free_stripe(conf, hash);
1848 unlock_device_hash_lock(conf, hash);
NeilBrownad01c9e2006-03-27 01:18:07 -08001849 atomic_set(&nsh->count, 1);
1850 for(i=0; i<conf->pool_size; i++)
1851 nsh->dev[i].page = osh->dev[i].page;
1852 for( ; i<newsize; i++)
1853 nsh->dev[i].page = NULL;
Shaohua Li566c09c2013-11-14 15:16:17 +11001854 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08001855 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11001856 cnt++;
1857 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
1858 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
1859 hash++;
1860 cnt = 0;
1861 }
NeilBrownad01c9e2006-03-27 01:18:07 -08001862 }
1863 kmem_cache_destroy(conf->slab_cache);
1864
1865 /* Step 3.
1866 * At this point, we are holding all the stripes so the array
1867 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001868 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001869 */
1870 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1871 if (ndisks) {
1872 for (i=0; i<conf->raid_disks; i++)
1873 ndisks[i] = conf->disks[i];
1874 kfree(conf->disks);
1875 conf->disks = ndisks;
1876 } else
1877 err = -ENOMEM;
1878
Dan Williamsd6f38f32009-07-14 11:50:52 -07001879 get_online_cpus();
1880 conf->scribble_len = scribble_len(newsize);
1881 for_each_present_cpu(cpu) {
1882 struct raid5_percpu *percpu;
1883 void *scribble;
1884
1885 percpu = per_cpu_ptr(conf->percpu, cpu);
1886 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1887
1888 if (scribble) {
1889 kfree(percpu->scribble);
1890 percpu->scribble = scribble;
1891 } else {
1892 err = -ENOMEM;
1893 break;
1894 }
1895 }
1896 put_online_cpus();
1897
NeilBrownad01c9e2006-03-27 01:18:07 -08001898 /* Step 4, return new stripes to service */
1899 while(!list_empty(&newstripes)) {
1900 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1901 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001902
NeilBrownad01c9e2006-03-27 01:18:07 -08001903 for (i=conf->raid_disks; i < newsize; i++)
1904 if (nsh->dev[i].page == NULL) {
1905 struct page *p = alloc_page(GFP_NOIO);
1906 nsh->dev[i].page = p;
1907 if (!p)
1908 err = -ENOMEM;
1909 }
1910 release_stripe(nsh);
1911 }
1912 /* critical section pass, GFP_NOIO no longer needed */
1913
1914 conf->slab_cache = sc;
1915 conf->active_name = 1-conf->active_name;
1916 conf->pool_size = newsize;
1917 return err;
1918}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Shaohua Li566c09c2013-11-14 15:16:17 +11001920static int drop_one_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 struct stripe_head *sh;
1923
Shaohua Li566c09c2013-11-14 15:16:17 +11001924 spin_lock_irq(conf->hash_locks + hash);
1925 sh = get_free_stripe(conf, hash);
1926 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08001927 if (!sh)
1928 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001929 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001930 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001931 kmem_cache_free(conf->slab_cache, sh);
1932 atomic_dec(&conf->active_stripes);
1933 return 1;
1934}
1935
NeilBrownd1688a62011-10-11 16:49:52 +11001936static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001937{
Shaohua Li566c09c2013-11-14 15:16:17 +11001938 int hash;
1939 for (hash = 0; hash < NR_STRIPE_HASH_LOCKS; hash++)
1940 while (drop_one_stripe(conf, hash))
1941 ;
NeilBrown3f294f42005-11-08 21:39:25 -08001942
NeilBrown29fc7e32006-02-03 03:03:41 -08001943 if (conf->slab_cache)
1944 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 conf->slab_cache = NULL;
1946}
1947
NeilBrown6712ecf2007-09-27 12:47:43 +02001948static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
NeilBrown99c0fb52009-03-31 14:39:38 +11001950 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001951 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001952 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001954 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001955 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001956 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 for (i=0 ; i<disks; i++)
1959 if (bi == &sh->dev[i].req)
1960 break;
1961
Dan Williams45b42332007-07-09 11:56:43 -07001962 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1963 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 uptodate);
1965 if (i == disks) {
1966 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
NeilBrown14a75d32011-12-23 10:17:52 +11001969 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001970 /* If replacement finished while this request was outstanding,
1971 * 'replacement' might be NULL already.
1972 * In that case it moved down to 'rdev'.
1973 * rdev is not removed until all requests are finished.
1974 */
NeilBrown14a75d32011-12-23 10:17:52 +11001975 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001976 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001977 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
NeilBrown05616be2012-05-21 09:27:00 +10001979 if (use_new_offset(conf, sh))
1980 s = sh->sector + rdev->new_data_offset;
1981 else
1982 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001985 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001986 /* Note that this cannot happen on a
1987 * replacement device. We just fail those on
1988 * any error
1989 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001990 printk_ratelimited(
1991 KERN_INFO
1992 "md/raid:%s: read error corrected"
1993 " (%lu sectors at %llu on %s)\n",
1994 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001995 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001996 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001997 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001998 clear_bit(R5_ReadError, &sh->dev[i].flags);
1999 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002000 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2001 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2002
NeilBrown14a75d32011-12-23 10:17:52 +11002003 if (atomic_read(&rdev->read_errors))
2004 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002006 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002007 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002008 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002011 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002012 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2013 printk_ratelimited(
2014 KERN_WARNING
2015 "md/raid:%s: read error on replacement device "
2016 "(sector %llu on %s).\n",
2017 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002018 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002019 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002020 else if (conf->mddev->degraded >= conf->max_degraded) {
2021 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002022 printk_ratelimited(
2023 KERN_WARNING
2024 "md/raid:%s: read error not correctable "
2025 "(sector %llu on %s).\n",
2026 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002027 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002028 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002029 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002030 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002031 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002032 printk_ratelimited(
2033 KERN_WARNING
2034 "md/raid:%s: read error NOT corrected!! "
2035 "(sector %llu on %s).\n",
2036 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002037 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002038 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002039 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002040 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002041 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002042 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002043 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002044 else
2045 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002046 if (set_bad && test_bit(In_sync, &rdev->flags)
2047 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2048 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002049 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002050 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2051 set_bit(R5_ReadError, &sh->dev[i].flags);
2052 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2053 } else
2054 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002055 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002056 clear_bit(R5_ReadError, &sh->dev[i].flags);
2057 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002058 if (!(set_bad
2059 && test_bit(In_sync, &rdev->flags)
2060 && rdev_set_badblocks(
2061 rdev, sh->sector, STRIPE_SECTORS, 0)))
2062 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
NeilBrown14a75d32011-12-23 10:17:52 +11002065 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2067 set_bit(STRIPE_HANDLE, &sh->state);
2068 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
NeilBrownd710e132008-10-13 11:55:12 +11002071static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
NeilBrown99c0fb52009-03-31 14:39:38 +11002073 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002074 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002075 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002076 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10002078 sector_t first_bad;
2079 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002080 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
NeilBrown977df362011-12-23 10:17:53 +11002082 for (i = 0 ; i < disks; i++) {
2083 if (bi == &sh->dev[i].req) {
2084 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 break;
NeilBrown977df362011-12-23 10:17:53 +11002086 }
2087 if (bi == &sh->dev[i].rreq) {
2088 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002089 if (rdev)
2090 replacement = 1;
2091 else
2092 /* rdev was removed and 'replacement'
2093 * replaced it. rdev is not removed
2094 * until all requests are finished.
2095 */
2096 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002097 break;
2098 }
2099 }
Dan Williams45b42332007-07-09 11:56:43 -07002100 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
2102 uptodate);
2103 if (i == disks) {
2104 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002105 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107
NeilBrown977df362011-12-23 10:17:53 +11002108 if (replacement) {
2109 if (!uptodate)
2110 md_error(conf->mddev, rdev);
2111 else if (is_badblock(rdev, sh->sector,
2112 STRIPE_SECTORS,
2113 &first_bad, &bad_sectors))
2114 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2115 } else {
2116 if (!uptodate) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002117 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002118 set_bit(WriteErrorSeen, &rdev->flags);
2119 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002120 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2121 set_bit(MD_RECOVERY_NEEDED,
2122 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002123 } else if (is_badblock(rdev, sh->sector,
2124 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002125 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002126 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002127 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2128 /* That was a successful write so make
2129 * sure it looks like we already did
2130 * a re-write.
2131 */
2132 set_bit(R5_ReWrite, &sh->dev[i].flags);
2133 }
NeilBrown977df362011-12-23 10:17:53 +11002134 }
2135 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
NeilBrown977df362011-12-23 10:17:53 +11002137 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2138 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07002140 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
NeilBrown784052e2009-03-31 15:19:07 +11002143static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
NeilBrown784052e2009-03-31 15:19:07 +11002145static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 struct r5dev *dev = &sh->dev[i];
2148
2149 bio_init(&dev->req);
2150 dev->req.bi_io_vec = &dev->vec;
2151 dev->req.bi_vcnt++;
2152 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11002154 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
NeilBrown977df362011-12-23 10:17:53 +11002156 bio_init(&dev->rreq);
2157 dev->rreq.bi_io_vec = &dev->rvec;
2158 dev->rreq.bi_vcnt++;
2159 dev->rreq.bi_max_vecs++;
2160 dev->rreq.bi_private = sh;
2161 dev->rvec.bv_page = dev->page;
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11002164 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
NeilBrownfd01b882011-10-11 16:47:53 +11002167static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002170 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002171 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002172 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
NeilBrown908f4fb2011-12-23 10:17:50 +11002174 spin_lock_irqsave(&conf->device_lock, flags);
2175 clear_bit(In_sync, &rdev->flags);
2176 mddev->degraded = calc_degraded(conf);
2177 spin_unlock_irqrestore(&conf->device_lock, flags);
2178 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2179
NeilBrownde393cd2011-07-28 11:31:48 +10002180 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002181 set_bit(Faulty, &rdev->flags);
2182 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2183 printk(KERN_ALERT
2184 "md/raid:%s: Disk failure on %s, disabling device.\n"
2185 "md/raid:%s: Operation continuing on %d devices.\n",
2186 mdname(mddev),
2187 bdevname(rdev->bdev, b),
2188 mdname(mddev),
2189 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192/*
2193 * Input: a 'big' sector number,
2194 * Output: index of the data and parity disk, and the sector # in them.
2195 */
NeilBrownd1688a62011-10-11 16:49:52 +11002196static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11002197 int previous, int *dd_idx,
2198 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002200 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002201 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002203 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002204 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002206 int algorithm = previous ? conf->prev_algo
2207 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002208 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2209 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002210 int raid_disks = previous ? conf->previous_raid_disks
2211 : conf->raid_disks;
2212 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* First compute the information on this sector */
2215
2216 /*
2217 * Compute the chunk number and the sector offset inside the chunk
2218 */
2219 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2220 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 /*
2223 * Compute the stripe number
2224 */
NeilBrown35f2a592010-04-20 14:13:34 +10002225 stripe = chunk_number;
2226 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002227 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /*
2229 * Select the parity disk based on the user selected algorithm.
2230 */
NeilBrown84789552011-07-27 11:00:36 +10002231 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002232 switch(conf->level) {
2233 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002234 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002235 break;
2236 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002237 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002239 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002240 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 (*dd_idx)++;
2242 break;
2243 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002244 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002245 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 (*dd_idx)++;
2247 break;
2248 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002249 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002250 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 break;
2252 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002253 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002254 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002256 case ALGORITHM_PARITY_0:
2257 pd_idx = 0;
2258 (*dd_idx)++;
2259 break;
2260 case ALGORITHM_PARITY_N:
2261 pd_idx = data_disks;
2262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002264 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002265 }
2266 break;
2267 case 6:
2268
NeilBrowne183eae2009-03-31 15:20:22 +11002269 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002270 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002271 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002272 qd_idx = pd_idx + 1;
2273 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002274 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002275 qd_idx = 0;
2276 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002277 (*dd_idx) += 2; /* D D P Q D */
2278 break;
2279 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002280 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002281 qd_idx = pd_idx + 1;
2282 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002283 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002284 qd_idx = 0;
2285 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002286 (*dd_idx) += 2; /* D D P Q D */
2287 break;
2288 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002289 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002290 qd_idx = (pd_idx + 1) % raid_disks;
2291 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002292 break;
2293 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002294 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002295 qd_idx = (pd_idx + 1) % raid_disks;
2296 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002297 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002298
2299 case ALGORITHM_PARITY_0:
2300 pd_idx = 0;
2301 qd_idx = 1;
2302 (*dd_idx) += 2;
2303 break;
2304 case ALGORITHM_PARITY_N:
2305 pd_idx = data_disks;
2306 qd_idx = data_disks + 1;
2307 break;
2308
2309 case ALGORITHM_ROTATING_ZERO_RESTART:
2310 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2311 * of blocks for computing Q is different.
2312 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002313 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002314 qd_idx = pd_idx + 1;
2315 if (pd_idx == raid_disks-1) {
2316 (*dd_idx)++; /* Q D D D P */
2317 qd_idx = 0;
2318 } else if (*dd_idx >= pd_idx)
2319 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002320 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002321 break;
2322
2323 case ALGORITHM_ROTATING_N_RESTART:
2324 /* Same a left_asymmetric, by first stripe is
2325 * D D D P Q rather than
2326 * Q D D D P
2327 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002328 stripe2 += 1;
2329 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002330 qd_idx = pd_idx + 1;
2331 if (pd_idx == raid_disks-1) {
2332 (*dd_idx)++; /* Q D D D P */
2333 qd_idx = 0;
2334 } else if (*dd_idx >= pd_idx)
2335 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002336 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002337 break;
2338
2339 case ALGORITHM_ROTATING_N_CONTINUE:
2340 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002341 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002342 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2343 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002344 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002345 break;
2346
2347 case ALGORITHM_LEFT_ASYMMETRIC_6:
2348 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002349 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002350 if (*dd_idx >= pd_idx)
2351 (*dd_idx)++;
2352 qd_idx = raid_disks - 1;
2353 break;
2354
2355 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002356 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002357 if (*dd_idx >= pd_idx)
2358 (*dd_idx)++;
2359 qd_idx = raid_disks - 1;
2360 break;
2361
2362 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002363 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002364 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2365 qd_idx = raid_disks - 1;
2366 break;
2367
2368 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002369 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002370 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2371 qd_idx = raid_disks - 1;
2372 break;
2373
2374 case ALGORITHM_PARITY_0_6:
2375 pd_idx = 0;
2376 (*dd_idx)++;
2377 qd_idx = raid_disks - 1;
2378 break;
2379
NeilBrown16a53ec2006-06-26 00:27:38 -07002380 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002381 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002382 }
2383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385
NeilBrown911d4ee2009-03-31 14:39:38 +11002386 if (sh) {
2387 sh->pd_idx = pd_idx;
2388 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002389 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 /*
2392 * Finally, compute the new sector number
2393 */
2394 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2395 return new_sector;
2396}
2397
2398
NeilBrown784052e2009-03-31 15:19:07 +11002399static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
NeilBrownd1688a62011-10-11 16:49:52 +11002401 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002402 int raid_disks = sh->disks;
2403 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002405 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2406 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002407 int algorithm = previous ? conf->prev_algo
2408 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 sector_t stripe;
2410 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002411 sector_t chunk_number;
2412 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002414 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
NeilBrown16a53ec2006-06-26 00:27:38 -07002416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2418 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
NeilBrown16a53ec2006-06-26 00:27:38 -07002420 if (i == sh->pd_idx)
2421 return 0;
2422 switch(conf->level) {
2423 case 4: break;
2424 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002425 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 case ALGORITHM_LEFT_ASYMMETRIC:
2427 case ALGORITHM_RIGHT_ASYMMETRIC:
2428 if (i > sh->pd_idx)
2429 i--;
2430 break;
2431 case ALGORITHM_LEFT_SYMMETRIC:
2432 case ALGORITHM_RIGHT_SYMMETRIC:
2433 if (i < sh->pd_idx)
2434 i += raid_disks;
2435 i -= (sh->pd_idx + 1);
2436 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002437 case ALGORITHM_PARITY_0:
2438 i -= 1;
2439 break;
2440 case ALGORITHM_PARITY_N:
2441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002443 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002444 }
2445 break;
2446 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002447 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002448 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002449 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002450 case ALGORITHM_LEFT_ASYMMETRIC:
2451 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002452 case ALGORITHM_ROTATING_ZERO_RESTART:
2453 case ALGORITHM_ROTATING_N_RESTART:
2454 if (sh->pd_idx == raid_disks-1)
2455 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002456 else if (i > sh->pd_idx)
2457 i -= 2; /* D D P Q D */
2458 break;
2459 case ALGORITHM_LEFT_SYMMETRIC:
2460 case ALGORITHM_RIGHT_SYMMETRIC:
2461 if (sh->pd_idx == raid_disks-1)
2462 i--; /* Q D D D P */
2463 else {
2464 /* D D P Q D */
2465 if (i < sh->pd_idx)
2466 i += raid_disks;
2467 i -= (sh->pd_idx + 2);
2468 }
2469 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002470 case ALGORITHM_PARITY_0:
2471 i -= 2;
2472 break;
2473 case ALGORITHM_PARITY_N:
2474 break;
2475 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002476 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002477 if (sh->pd_idx == 0)
2478 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002479 else {
2480 /* D D Q P D */
2481 if (i < sh->pd_idx)
2482 i += raid_disks;
2483 i -= (sh->pd_idx + 1);
2484 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002485 break;
2486 case ALGORITHM_LEFT_ASYMMETRIC_6:
2487 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2488 if (i > sh->pd_idx)
2489 i--;
2490 break;
2491 case ALGORITHM_LEFT_SYMMETRIC_6:
2492 case ALGORITHM_RIGHT_SYMMETRIC_6:
2493 if (i < sh->pd_idx)
2494 i += data_disks + 1;
2495 i -= (sh->pd_idx + 1);
2496 break;
2497 case ALGORITHM_PARITY_0_6:
2498 i -= 1;
2499 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002500 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002501 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002502 }
2503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
2506 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002507 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
NeilBrown112bf892009-03-31 14:39:38 +11002509 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002510 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002511 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2512 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002513 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2514 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return 0;
2516 }
2517 return r_sector;
2518}
2519
2520
Dan Williams600aa102008-06-28 08:32:05 +10002521static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002522schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002523 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002524{
2525 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002526 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002527 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002528
Dan Williamse33129d2007-01-02 13:52:30 -07002529 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002530
2531 for (i = disks; i--; ) {
2532 struct r5dev *dev = &sh->dev[i];
2533
2534 if (dev->towrite) {
2535 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002536 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002537 if (!expand)
2538 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002539 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002540 }
2541 }
NeilBrownce7d3632013-03-04 12:37:14 +11002542 /* if we are not expanding this is a proper write request, and
2543 * there will be bios with new data to be drained into the
2544 * stripe cache
2545 */
2546 if (!expand) {
2547 if (!s->locked)
2548 /* False alarm, nothing to do */
2549 return;
2550 sh->reconstruct_state = reconstruct_state_drain_run;
2551 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2552 } else
2553 sh->reconstruct_state = reconstruct_state_run;
2554
2555 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2556
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002557 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002558 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002559 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002560 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002561 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002562 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2563 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2564
Dan Williamse33129d2007-01-02 13:52:30 -07002565 for (i = disks; i--; ) {
2566 struct r5dev *dev = &sh->dev[i];
2567 if (i == pd_idx)
2568 continue;
2569
Dan Williamse33129d2007-01-02 13:52:30 -07002570 if (dev->towrite &&
2571 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002572 test_bit(R5_Wantcompute, &dev->flags))) {
2573 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002574 set_bit(R5_LOCKED, &dev->flags);
2575 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002576 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002577 }
2578 }
NeilBrownce7d3632013-03-04 12:37:14 +11002579 if (!s->locked)
2580 /* False alarm - nothing to do */
2581 return;
2582 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2583 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2584 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2585 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002586 }
2587
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002588 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002589 * are in flight
2590 */
2591 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2592 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002593 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002594
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002595 if (level == 6) {
2596 int qd_idx = sh->qd_idx;
2597 struct r5dev *dev = &sh->dev[qd_idx];
2598
2599 set_bit(R5_LOCKED, &dev->flags);
2600 clear_bit(R5_UPTODATE, &dev->flags);
2601 s->locked++;
2602 }
2603
Dan Williams600aa102008-06-28 08:32:05 +10002604 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002605 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002606 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002607}
NeilBrown16a53ec2006-06-26 00:27:38 -07002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609/*
2610 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002611 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 * The bi_next chain must be in order.
2613 */
2614static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2615{
2616 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002617 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002618 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
NeilBrowncbe47ec2011-07-26 11:20:35 +10002620 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002621 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 (unsigned long long)sh->sector);
2623
Shaohua Lib17459c2012-07-19 16:01:31 +10002624 /*
2625 * If several bio share a stripe. The bio bi_phys_segments acts as a
2626 * reference count to avoid race. The reference count should already be
2627 * increased before this function is called (for example, in
2628 * make_request()), so other bio sharing this stripe will not free the
2629 * stripe. If a stripe is owned by one stripe, the stripe lock will
2630 * protect it.
2631 */
2632 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002633 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002635 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002636 firstwrite = 1;
2637 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002639 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
2640 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 goto overlap;
2642 bip = & (*bip)->bi_next;
2643 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07002644 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 goto overlap;
2646
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002647 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (*bip)
2649 bi->bi_next = *bip;
2650 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002651 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (forwrite) {
2654 /* check if page is covered */
2655 sector_t sector = sh->dev[dd_idx].sector;
2656 for (bi=sh->dev[dd_idx].towrite;
2657 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07002658 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002660 if (bio_end_sector(bi) >= sector)
2661 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2664 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2665 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002666
2667 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002668 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10002669 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002670 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002671
2672 if (conf->mddev->bitmap && firstwrite) {
2673 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2674 STRIPE_SECTORS, 0);
2675 sh->bm_seq = conf->seq_flush+1;
2676 set_bit(STRIPE_BIT_DELAY, &sh->state);
2677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return 1;
2679
2680 overlap:
2681 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002682 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
2684}
2685
NeilBrownd1688a62011-10-11 16:49:52 +11002686static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002687
NeilBrownd1688a62011-10-11 16:49:52 +11002688static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002689 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002690{
NeilBrown784052e2009-03-31 15:19:07 +11002691 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002692 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002693 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002694 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002695 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002696
NeilBrown112bf892009-03-31 14:39:38 +11002697 raid5_compute_sector(conf,
2698 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002699 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002700 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002701 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002702}
2703
Dan Williamsa4456852007-07-09 11:56:43 -07002704static void
NeilBrownd1688a62011-10-11 16:49:52 +11002705handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002706 struct stripe_head_state *s, int disks,
2707 struct bio **return_bi)
2708{
2709 int i;
2710 for (i = disks; i--; ) {
2711 struct bio *bi;
2712 int bitmap_end = 0;
2713
2714 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002715 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002716 rcu_read_lock();
2717 rdev = rcu_dereference(conf->disks[i].rdev);
2718 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002719 atomic_inc(&rdev->nr_pending);
2720 else
2721 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002722 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002723 if (rdev) {
2724 if (!rdev_set_badblocks(
2725 rdev,
2726 sh->sector,
2727 STRIPE_SECTORS, 0))
2728 md_error(conf->mddev, rdev);
2729 rdev_dec_pending(rdev, conf->mddev);
2730 }
Dan Williamsa4456852007-07-09 11:56:43 -07002731 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002732 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002733 /* fail all writes first */
2734 bi = sh->dev[i].towrite;
2735 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002736 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002737 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002738 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002739
2740 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2741 wake_up(&conf->wait_for_overlap);
2742
Kent Overstreet4f024f32013-10-11 15:44:27 -07002743 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002744 sh->dev[i].sector + STRIPE_SECTORS) {
2745 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2746 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002747 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002748 md_write_end(conf->mddev);
2749 bi->bi_next = *return_bi;
2750 *return_bi = bi;
2751 }
2752 bi = nextbi;
2753 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002754 if (bitmap_end)
2755 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2756 STRIPE_SECTORS, 0, 0);
2757 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002758 /* and fail all 'written' */
2759 bi = sh->dev[i].written;
2760 sh->dev[i].written = NULL;
2761 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002762 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002763 sh->dev[i].sector + STRIPE_SECTORS) {
2764 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2765 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002766 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002767 md_write_end(conf->mddev);
2768 bi->bi_next = *return_bi;
2769 *return_bi = bi;
2770 }
2771 bi = bi2;
2772 }
2773
Dan Williamsb5e98d62007-01-02 13:52:31 -07002774 /* fail any reads if this device is non-operational and
2775 * the data has not reached the cache yet.
2776 */
2777 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2778 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2779 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002780 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002781 bi = sh->dev[i].toread;
2782 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002783 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002784 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2785 wake_up(&conf->wait_for_overlap);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002786 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07002787 sh->dev[i].sector + STRIPE_SECTORS) {
2788 struct bio *nextbi =
2789 r5_next_bio(bi, sh->dev[i].sector);
2790 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002791 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002792 bi->bi_next = *return_bi;
2793 *return_bi = bi;
2794 }
2795 bi = nextbi;
2796 }
2797 }
Dan Williamsa4456852007-07-09 11:56:43 -07002798 if (bitmap_end)
2799 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2800 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002801 /* If we were in the middle of a write the parity block might
2802 * still be locked - so just clear all R5_LOCKED flags
2803 */
2804 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002805 }
2806
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002807 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2808 if (atomic_dec_and_test(&conf->pending_full_writes))
2809 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002810}
2811
NeilBrown7f0da592011-07-28 11:39:22 +10002812static void
NeilBrownd1688a62011-10-11 16:49:52 +11002813handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002814 struct stripe_head_state *s)
2815{
2816 int abort = 0;
2817 int i;
2818
NeilBrown7f0da592011-07-28 11:39:22 +10002819 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002820 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2821 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002822 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002823 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002824 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002825 * Don't even need to abort as that is handled elsewhere
2826 * if needed, and not always wanted e.g. if there is a known
2827 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002828 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002829 * non-sync devices, or abort the recovery
2830 */
NeilBrown18b98372012-04-01 23:48:38 +10002831 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2832 /* During recovery devices cannot be removed, so
2833 * locking and refcounting of rdevs is not needed
2834 */
2835 for (i = 0; i < conf->raid_disks; i++) {
2836 struct md_rdev *rdev = conf->disks[i].rdev;
2837 if (rdev
2838 && !test_bit(Faulty, &rdev->flags)
2839 && !test_bit(In_sync, &rdev->flags)
2840 && !rdev_set_badblocks(rdev, sh->sector,
2841 STRIPE_SECTORS, 0))
2842 abort = 1;
2843 rdev = conf->disks[i].replacement;
2844 if (rdev
2845 && !test_bit(Faulty, &rdev->flags)
2846 && !test_bit(In_sync, &rdev->flags)
2847 && !rdev_set_badblocks(rdev, sh->sector,
2848 STRIPE_SECTORS, 0))
2849 abort = 1;
2850 }
2851 if (abort)
2852 conf->recovery_disabled =
2853 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002854 }
NeilBrown18b98372012-04-01 23:48:38 +10002855 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002856}
2857
NeilBrown9a3e1102011-12-23 10:17:53 +11002858static int want_replace(struct stripe_head *sh, int disk_idx)
2859{
2860 struct md_rdev *rdev;
2861 int rv = 0;
2862 /* Doing recovery so rcu locking not required */
2863 rdev = sh->raid_conf->disks[disk_idx].replacement;
2864 if (rdev
2865 && !test_bit(Faulty, &rdev->flags)
2866 && !test_bit(In_sync, &rdev->flags)
2867 && (rdev->recovery_offset <= sh->sector
2868 || rdev->mddev->recovery_cp <= sh->sector))
2869 rv = 1;
2870
2871 return rv;
2872}
2873
NeilBrown93b3dbc2011-07-27 11:00:36 +10002874/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002875 * to be read or computed to satisfy a request.
2876 *
2877 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002878 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002879 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002880static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2881 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002882{
2883 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002884 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2885 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002886
Dan Williamsf38e1212007-01-02 13:52:30 -07002887 /* is the data in this block needed, and can we get it? */
2888 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002889 !test_bit(R5_UPTODATE, &dev->flags) &&
2890 (dev->toread ||
2891 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2892 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002893 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002894 (s->failed >= 1 && fdev[0]->toread) ||
2895 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002896 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
NeilBrown67f45542014-05-28 13:39:22 +10002897 (!test_bit(R5_Insync, &dev->flags) || test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) &&
NeilBrown93b3dbc2011-07-27 11:00:36 +10002898 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
NeilBrown67f45542014-05-28 13:39:22 +10002899 (sh->raid_conf->level == 6 && s->failed && s->to_write &&
2900 s->to_write < sh->raid_conf->raid_disks - 2 &&
2901 (!test_bit(R5_Insync, &dev->flags) || test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002902 /* we would like to get this block, possibly by computing it,
2903 * otherwise read it if the backing disk is insync
2904 */
2905 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2906 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2907 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002908 (s->failed && (disk_idx == s->failed_num[0] ||
2909 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002910 /* have disk failed, and we're requested to fetch it;
2911 * do compute it
2912 */
2913 pr_debug("Computing stripe %llu block %d\n",
2914 (unsigned long long)sh->sector, disk_idx);
2915 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2916 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2917 set_bit(R5_Wantcompute, &dev->flags);
2918 sh->ops.target = disk_idx;
2919 sh->ops.target2 = -1; /* no 2nd target */
2920 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002921 /* Careful: from this point on 'uptodate' is in the eye
2922 * of raid_run_ops which services 'compute' operations
2923 * before writes. R5_Wantcompute flags a block that will
2924 * be R5_UPTODATE by the time it is needed for a
2925 * subsequent operation.
2926 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002927 s->uptodate++;
2928 return 1;
2929 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2930 /* Computing 2-failure is *very* expensive; only
2931 * do it if failed >= 2
2932 */
2933 int other;
2934 for (other = disks; other--; ) {
2935 if (other == disk_idx)
2936 continue;
2937 if (!test_bit(R5_UPTODATE,
2938 &sh->dev[other].flags))
2939 break;
2940 }
2941 BUG_ON(other < 0);
2942 pr_debug("Computing stripe %llu blocks %d,%d\n",
2943 (unsigned long long)sh->sector,
2944 disk_idx, other);
2945 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2946 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2947 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2948 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2949 sh->ops.target = disk_idx;
2950 sh->ops.target2 = other;
2951 s->uptodate += 2;
2952 s->req_compute = 1;
2953 return 1;
2954 } else if (test_bit(R5_Insync, &dev->flags)) {
2955 set_bit(R5_LOCKED, &dev->flags);
2956 set_bit(R5_Wantread, &dev->flags);
2957 s->locked++;
2958 pr_debug("Reading block %d (sync=%d)\n",
2959 disk_idx, s->syncing);
2960 }
2961 }
2962
2963 return 0;
2964}
2965
2966/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002967 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002968 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002969static void handle_stripe_fill(struct stripe_head *sh,
2970 struct stripe_head_state *s,
2971 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002972{
2973 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002974
2975 /* look for blocks to read/compute, skip this if a compute
2976 * is already in flight, or if the stripe contents are in the
2977 * midst of changing due to a write
2978 */
2979 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2980 !sh->reconstruct_state)
2981 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002982 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002983 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002984 set_bit(STRIPE_HANDLE, &sh->state);
2985}
2986
2987
Dan Williams1fe797e2008-06-28 09:16:30 +10002988/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002989 * any written block on an uptodate or failed drive can be returned.
2990 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2991 * never LOCKED, so we don't need to test 'failed' directly.
2992 */
NeilBrownd1688a62011-10-11 16:49:52 +11002993static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002994 struct stripe_head *sh, int disks, struct bio **return_bi)
2995{
2996 int i;
2997 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002998 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002999
3000 for (i = disks; i--; )
3001 if (sh->dev[i].written) {
3002 dev = &sh->dev[i];
3003 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003004 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11003005 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003006 /* We can return any write requests */
3007 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003008 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003009 if (test_and_clear_bit(R5_Discard, &dev->flags))
3010 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003011 wbi = dev->written;
3012 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003013 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003014 dev->sector + STRIPE_SECTORS) {
3015 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003016 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003017 md_write_end(conf->mddev);
3018 wbi->bi_next = *return_bi;
3019 *return_bi = wbi;
3020 }
3021 wbi = wbi2;
3022 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003023 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3024 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003025 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003026 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003027 } else if (test_bit(R5_Discard, &dev->flags))
3028 discard_pending = 1;
3029 }
3030 if (!discard_pending &&
3031 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3032 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3033 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3034 if (sh->qd_idx >= 0) {
3035 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3036 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3037 }
3038 /* now that discard is done we can proceed with any sync */
3039 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003040 /*
3041 * SCSI discard will change some bio fields and the stripe has
3042 * no updated data, so remove it from hash list and the stripe
3043 * will be reinitialized
3044 */
3045 spin_lock_irq(&conf->device_lock);
3046 remove_hash(sh);
3047 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003048 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3049 set_bit(STRIPE_HANDLE, &sh->state);
3050
3051 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003052
3053 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3054 if (atomic_dec_and_test(&conf->pending_full_writes))
3055 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003056}
3057
NeilBrownd1688a62011-10-11 16:49:52 +11003058static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003059 struct stripe_head *sh,
3060 struct stripe_head_state *s,
3061 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003062{
3063 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003064 sector_t recovery_cp = conf->mddev->recovery_cp;
3065
3066 /* RAID6 requires 'rcw' in current implementation.
3067 * Otherwise, check whether resync is now happening or should start.
3068 * If yes, then the array is dirty (after unclean shutdown or
3069 * initial creation), so parity in some stripes might be inconsistent.
3070 * In this case, we need to always do reconstruct-write, to ensure
3071 * that in case of drive failure or read-error correction, we
3072 * generate correct data from the parity.
3073 */
3074 if (conf->max_degraded == 2 ||
3075 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
3076 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003077 * look like rcw is cheaper
3078 */
3079 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003080 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
3081 conf->max_degraded, (unsigned long long)recovery_cp,
3082 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003083 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003084 /* would I have to read this buffer for read_modify_write */
3085 struct r5dev *dev = &sh->dev[i];
3086 if ((dev->towrite || i == sh->pd_idx) &&
3087 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003088 !(test_bit(R5_UPTODATE, &dev->flags) ||
3089 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003090 if (test_bit(R5_Insync, &dev->flags))
3091 rmw++;
3092 else
3093 rmw += 2*disks; /* cannot read it */
3094 }
3095 /* Would I have to read this buffer for reconstruct_write */
3096 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
3097 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003098 !(test_bit(R5_UPTODATE, &dev->flags) ||
3099 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003100 if (test_bit(R5_Insync, &dev->flags))
3101 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003102 else
3103 rcw += 2*disks;
3104 }
3105 }
Dan Williams45b42332007-07-09 11:56:43 -07003106 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003107 (unsigned long long)sh->sector, rmw, rcw);
3108 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11003109 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003110 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003111 if (conf->mddev->queue)
3112 blk_add_trace_msg(conf->mddev->queue,
3113 "raid5 rmw %llu %d",
3114 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003115 for (i = disks; i--; ) {
3116 struct r5dev *dev = &sh->dev[i];
3117 if ((dev->towrite || i == sh->pd_idx) &&
3118 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003119 !(test_bit(R5_UPTODATE, &dev->flags) ||
3120 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003121 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003122 if (test_bit(STRIPE_PREREAD_ACTIVE,
3123 &sh->state)) {
3124 pr_debug("Read_old block %d for r-m-w\n",
3125 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003126 set_bit(R5_LOCKED, &dev->flags);
3127 set_bit(R5_Wantread, &dev->flags);
3128 s->locked++;
3129 } else {
3130 set_bit(STRIPE_DELAYED, &sh->state);
3131 set_bit(STRIPE_HANDLE, &sh->state);
3132 }
3133 }
3134 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003135 }
NeilBrownc8ac1802011-07-27 11:00:36 +10003136 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003137 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003138 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003139 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003140 for (i = disks; i--; ) {
3141 struct r5dev *dev = &sh->dev[i];
3142 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003143 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003144 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003145 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003146 test_bit(R5_Wantcompute, &dev->flags))) {
3147 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003148 if (test_bit(R5_Insync, &dev->flags) &&
3149 test_bit(STRIPE_PREREAD_ACTIVE,
3150 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003151 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003152 "%d for Reconstruct\n", i);
3153 set_bit(R5_LOCKED, &dev->flags);
3154 set_bit(R5_Wantread, &dev->flags);
3155 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003156 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003157 } else {
3158 set_bit(STRIPE_DELAYED, &sh->state);
3159 set_bit(STRIPE_HANDLE, &sh->state);
3160 }
3161 }
3162 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003163 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003164 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3165 (unsigned long long)sh->sector,
3166 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003167 }
Dan Williamsa4456852007-07-09 11:56:43 -07003168 /* now if nothing is locked, and if we have enough data,
3169 * we can start a write request
3170 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003171 /* since handle_stripe can be called at any time we need to handle the
3172 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003173 * subsequent call wants to start a write request. raid_run_ops only
3174 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003175 * simultaneously. If this is not the case then new writes need to be
3176 * held off until the compute completes.
3177 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003178 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3179 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3180 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003181 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003182}
3183
NeilBrownd1688a62011-10-11 16:49:52 +11003184static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003185 struct stripe_head_state *s, int disks)
3186{
Dan Williamsecc65c92008-06-28 08:31:57 +10003187 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003188
Dan Williamsbd2ab672008-04-10 21:29:27 -07003189 set_bit(STRIPE_HANDLE, &sh->state);
3190
Dan Williamsecc65c92008-06-28 08:31:57 +10003191 switch (sh->check_state) {
3192 case check_state_idle:
3193 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003194 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003195 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003196 sh->check_state = check_state_run;
3197 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003198 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003199 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003200 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003201 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003202 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003203 /* fall through */
3204 case check_state_compute_result:
3205 sh->check_state = check_state_idle;
3206 if (!dev)
3207 dev = &sh->dev[sh->pd_idx];
3208
3209 /* check that a write has not made the stripe insync */
3210 if (test_bit(STRIPE_INSYNC, &sh->state))
3211 break;
3212
3213 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003214 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3215 BUG_ON(s->uptodate != disks);
3216
3217 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003218 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003219 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003220
Dan Williamsa4456852007-07-09 11:56:43 -07003221 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003222 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003223 break;
3224 case check_state_run:
3225 break; /* we will be called again upon completion */
3226 case check_state_check_result:
3227 sh->check_state = check_state_idle;
3228
3229 /* if a failure occurred during the check operation, leave
3230 * STRIPE_INSYNC not set and let the stripe be handled again
3231 */
3232 if (s->failed)
3233 break;
3234
3235 /* handle a successful check operation, if parity is correct
3236 * we are done. Otherwise update the mismatch count and repair
3237 * parity if !MD_RECOVERY_CHECK
3238 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003239 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003240 /* parity is correct (on disc,
3241 * not in buffer any more)
3242 */
3243 set_bit(STRIPE_INSYNC, &sh->state);
3244 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003245 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003246 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3247 /* don't try to repair!! */
3248 set_bit(STRIPE_INSYNC, &sh->state);
3249 else {
3250 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003251 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003252 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3253 set_bit(R5_Wantcompute,
3254 &sh->dev[sh->pd_idx].flags);
3255 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003256 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003257 s->uptodate++;
3258 }
3259 }
3260 break;
3261 case check_state_compute_run:
3262 break;
3263 default:
3264 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3265 __func__, sh->check_state,
3266 (unsigned long long) sh->sector);
3267 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003268 }
3269}
3270
3271
NeilBrownd1688a62011-10-11 16:49:52 +11003272static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003273 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003274 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003275{
Dan Williamsa4456852007-07-09 11:56:43 -07003276 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003277 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003278 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003279
3280 set_bit(STRIPE_HANDLE, &sh->state);
3281
3282 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003283
Dan Williamsa4456852007-07-09 11:56:43 -07003284 /* Want to check and possibly repair P and Q.
3285 * However there could be one 'failed' device, in which
3286 * case we can only check one of them, possibly using the
3287 * other to generate missing data
3288 */
3289
Dan Williamsd82dfee2009-07-14 13:40:57 -07003290 switch (sh->check_state) {
3291 case check_state_idle:
3292 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003293 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003294 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003295 * makes sense to check P (If anything else were failed,
3296 * we would have used P to recreate it).
3297 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003298 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003299 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003300 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003301 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003302 * anything, so it makes sense to check it
3303 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003304 if (sh->check_state == check_state_run)
3305 sh->check_state = check_state_run_pq;
3306 else
3307 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003308 }
Dan Williams36d1c642009-07-14 11:48:22 -07003309
Dan Williamsd82dfee2009-07-14 13:40:57 -07003310 /* discard potentially stale zero_sum_result */
3311 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003312
Dan Williamsd82dfee2009-07-14 13:40:57 -07003313 if (sh->check_state == check_state_run) {
3314 /* async_xor_zero_sum destroys the contents of P */
3315 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3316 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003317 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003318 if (sh->check_state >= check_state_run &&
3319 sh->check_state <= check_state_run_pq) {
3320 /* async_syndrome_zero_sum preserves P and Q, so
3321 * no need to mark them !uptodate here
3322 */
3323 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3324 break;
3325 }
Dan Williams36d1c642009-07-14 11:48:22 -07003326
Dan Williamsd82dfee2009-07-14 13:40:57 -07003327 /* we have 2-disk failure */
3328 BUG_ON(s->failed != 2);
3329 /* fall through */
3330 case check_state_compute_result:
3331 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003332
Dan Williamsd82dfee2009-07-14 13:40:57 -07003333 /* check that a write has not made the stripe insync */
3334 if (test_bit(STRIPE_INSYNC, &sh->state))
3335 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003336
3337 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003338 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003339 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003340 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003341 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003342 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003343 s->locked++;
3344 set_bit(R5_LOCKED, &dev->flags);
3345 set_bit(R5_Wantwrite, &dev->flags);
3346 }
3347 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003348 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003349 s->locked++;
3350 set_bit(R5_LOCKED, &dev->flags);
3351 set_bit(R5_Wantwrite, &dev->flags);
3352 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003353 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003354 dev = &sh->dev[pd_idx];
3355 s->locked++;
3356 set_bit(R5_LOCKED, &dev->flags);
3357 set_bit(R5_Wantwrite, &dev->flags);
3358 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003359 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003360 dev = &sh->dev[qd_idx];
3361 s->locked++;
3362 set_bit(R5_LOCKED, &dev->flags);
3363 set_bit(R5_Wantwrite, &dev->flags);
3364 }
3365 clear_bit(STRIPE_DEGRADED, &sh->state);
3366
3367 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003368 break;
3369 case check_state_run:
3370 case check_state_run_q:
3371 case check_state_run_pq:
3372 break; /* we will be called again upon completion */
3373 case check_state_check_result:
3374 sh->check_state = check_state_idle;
3375
3376 /* handle a successful check operation, if parity is correct
3377 * we are done. Otherwise update the mismatch count and repair
3378 * parity if !MD_RECOVERY_CHECK
3379 */
3380 if (sh->ops.zero_sum_result == 0) {
3381 /* both parities are correct */
3382 if (!s->failed)
3383 set_bit(STRIPE_INSYNC, &sh->state);
3384 else {
3385 /* in contrast to the raid5 case we can validate
3386 * parity, but still have a failure to write
3387 * back
3388 */
3389 sh->check_state = check_state_compute_result;
3390 /* Returning at this point means that we may go
3391 * off and bring p and/or q uptodate again so
3392 * we make sure to check zero_sum_result again
3393 * to verify if p or q need writeback
3394 */
3395 }
3396 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003397 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003398 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3399 /* don't try to repair!! */
3400 set_bit(STRIPE_INSYNC, &sh->state);
3401 else {
3402 int *target = &sh->ops.target;
3403
3404 sh->ops.target = -1;
3405 sh->ops.target2 = -1;
3406 sh->check_state = check_state_compute_run;
3407 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3408 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3409 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3410 set_bit(R5_Wantcompute,
3411 &sh->dev[pd_idx].flags);
3412 *target = pd_idx;
3413 target = &sh->ops.target2;
3414 s->uptodate++;
3415 }
3416 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3417 set_bit(R5_Wantcompute,
3418 &sh->dev[qd_idx].flags);
3419 *target = qd_idx;
3420 s->uptodate++;
3421 }
3422 }
3423 }
3424 break;
3425 case check_state_compute_run:
3426 break;
3427 default:
3428 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3429 __func__, sh->check_state,
3430 (unsigned long long) sh->sector);
3431 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003432 }
3433}
3434
NeilBrownd1688a62011-10-11 16:49:52 +11003435static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003436{
3437 int i;
3438
3439 /* We have read all the blocks in this stripe and now we need to
3440 * copy some of them into a target stripe for expand.
3441 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003442 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003443 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3444 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003445 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003446 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003447 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003448 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003449
NeilBrown784052e2009-03-31 15:19:07 +11003450 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003451 sector_t s = raid5_compute_sector(conf, bn, 0,
3452 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003453 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003454 if (sh2 == NULL)
3455 /* so far only the early blocks of this stripe
3456 * have been requested. When later blocks
3457 * get requested, we will try again
3458 */
3459 continue;
3460 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3461 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3462 /* must have already done this block */
3463 release_stripe(sh2);
3464 continue;
3465 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003466
3467 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003468 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003469 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003470 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003471 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003472
Dan Williamsa4456852007-07-09 11:56:43 -07003473 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3474 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3475 for (j = 0; j < conf->raid_disks; j++)
3476 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003477 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003478 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3479 break;
3480 if (j == conf->raid_disks) {
3481 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3482 set_bit(STRIPE_HANDLE, &sh2->state);
3483 }
3484 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003485
Dan Williamsa4456852007-07-09 11:56:43 -07003486 }
NeilBrowna2e08552007-09-11 15:23:36 -07003487 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003488 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491/*
3492 * handle_stripe - do things to a stripe.
3493 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003494 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3495 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003497 * return some read requests which now have data
3498 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 * schedule a read on some buffers
3500 * schedule a write of some buffers
3501 * return confirmation of parity correctness
3502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 */
Dan Williamsa4456852007-07-09 11:56:43 -07003504
NeilBrownacfe7262011-07-27 11:00:36 +10003505static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003506{
NeilBrownd1688a62011-10-11 16:49:52 +11003507 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003508 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003509 struct r5dev *dev;
3510 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003511 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003512
NeilBrownacfe7262011-07-27 11:00:36 +10003513 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003514
NeilBrownacfe7262011-07-27 11:00:36 +10003515 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3516 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3517 s->failed_num[0] = -1;
3518 s->failed_num[1] = -1;
3519
3520 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003521 rcu_read_lock();
3522 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003523 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003524 sector_t first_bad;
3525 int bad_sectors;
3526 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003527
NeilBrown16a53ec2006-06-26 00:27:38 -07003528 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003529
Dan Williams45b42332007-07-09 11:56:43 -07003530 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003531 i, dev->flags,
3532 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003533 /* maybe we can reply to a read
3534 *
3535 * new wantfill requests are only permitted while
3536 * ops_complete_biofill is guaranteed to be inactive
3537 */
3538 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3539 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3540 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003541
3542 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003543 if (test_bit(R5_LOCKED, &dev->flags))
3544 s->locked++;
3545 if (test_bit(R5_UPTODATE, &dev->flags))
3546 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003547 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003548 s->compute++;
3549 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003550 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003551
NeilBrownacfe7262011-07-27 11:00:36 +10003552 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003553 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003554 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003555 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003556 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003557 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003558 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003559 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003560 }
Dan Williamsa4456852007-07-09 11:56:43 -07003561 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003562 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003563 /* Prefer to use the replacement for reads, but only
3564 * if it is recovered enough and has no bad blocks.
3565 */
3566 rdev = rcu_dereference(conf->disks[i].replacement);
3567 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3568 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3569 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3570 &first_bad, &bad_sectors))
3571 set_bit(R5_ReadRepl, &dev->flags);
3572 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003573 if (rdev)
3574 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003575 rdev = rcu_dereference(conf->disks[i].rdev);
3576 clear_bit(R5_ReadRepl, &dev->flags);
3577 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003578 if (rdev && test_bit(Faulty, &rdev->flags))
3579 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003580 if (rdev) {
3581 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3582 &first_bad, &bad_sectors);
3583 if (s->blocked_rdev == NULL
3584 && (test_bit(Blocked, &rdev->flags)
3585 || is_bad < 0)) {
3586 if (is_bad < 0)
3587 set_bit(BlockedBadBlocks,
3588 &rdev->flags);
3589 s->blocked_rdev = rdev;
3590 atomic_inc(&rdev->nr_pending);
3591 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003592 }
NeilBrown415e72d2010-06-17 17:25:21 +10003593 clear_bit(R5_Insync, &dev->flags);
3594 if (!rdev)
3595 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003596 else if (is_bad) {
3597 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003598 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3599 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003600 /* treat as in-sync, but with a read error
3601 * which we can now try to correct
3602 */
3603 set_bit(R5_Insync, &dev->flags);
3604 set_bit(R5_ReadError, &dev->flags);
3605 }
3606 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003607 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003608 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003609 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003610 set_bit(R5_Insync, &dev->flags);
3611 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3612 test_bit(R5_Expanded, &dev->flags))
3613 /* If we've reshaped into here, we assume it is Insync.
3614 * We will shortly update recovery_offset to make
3615 * it official.
3616 */
3617 set_bit(R5_Insync, &dev->flags);
3618
NeilBrown1cc03eb2014-01-06 13:19:42 +11003619 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003620 /* This flag does not apply to '.replacement'
3621 * only to .rdev, so make sure to check that*/
3622 struct md_rdev *rdev2 = rcu_dereference(
3623 conf->disks[i].rdev);
3624 if (rdev2 == rdev)
3625 clear_bit(R5_Insync, &dev->flags);
3626 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003627 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003628 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003629 } else
3630 clear_bit(R5_WriteError, &dev->flags);
3631 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11003632 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003633 /* This flag does not apply to '.replacement'
3634 * only to .rdev, so make sure to check that*/
3635 struct md_rdev *rdev2 = rcu_dereference(
3636 conf->disks[i].rdev);
3637 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003638 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003639 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003640 } else
3641 clear_bit(R5_MadeGood, &dev->flags);
3642 }
NeilBrown977df362011-12-23 10:17:53 +11003643 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3644 struct md_rdev *rdev2 = rcu_dereference(
3645 conf->disks[i].replacement);
3646 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3647 s->handle_bad_blocks = 1;
3648 atomic_inc(&rdev2->nr_pending);
3649 } else
3650 clear_bit(R5_MadeGoodRepl, &dev->flags);
3651 }
NeilBrown415e72d2010-06-17 17:25:21 +10003652 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003653 /* The ReadError flag will just be confusing now */
3654 clear_bit(R5_ReadError, &dev->flags);
3655 clear_bit(R5_ReWrite, &dev->flags);
3656 }
NeilBrown415e72d2010-06-17 17:25:21 +10003657 if (test_bit(R5_ReadError, &dev->flags))
3658 clear_bit(R5_Insync, &dev->flags);
3659 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003660 if (s->failed < 2)
3661 s->failed_num[s->failed] = i;
3662 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003663 if (rdev && !test_bit(Faulty, &rdev->flags))
3664 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003665 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003666 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003667 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3668 /* If there is a failed device being replaced,
3669 * we must be recovering.
3670 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003671 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003672 * else we can only be replacing
3673 * sync and recovery both need to read all devices, and so
3674 * use the same flag.
3675 */
3676 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003677 sh->sector >= conf->mddev->recovery_cp ||
3678 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003679 s->syncing = 1;
3680 else
3681 s->replacing = 1;
3682 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003683 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003684}
NeilBrownf4168852007-02-28 20:11:53 -08003685
NeilBrowncc940152011-07-26 11:35:35 +10003686static void handle_stripe(struct stripe_head *sh)
3687{
3688 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003689 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003690 int i;
NeilBrown84789552011-07-27 11:00:36 +10003691 int prexor;
3692 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003693 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003694
3695 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003696 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003697 /* already being handled, ensure it gets handled
3698 * again when current action finishes */
3699 set_bit(STRIPE_HANDLE, &sh->state);
3700 return;
3701 }
3702
NeilBrownf8dfcff2013-03-12 12:18:06 +11003703 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3704 spin_lock(&sh->stripe_lock);
3705 /* Cannot process 'sync' concurrently with 'discard' */
3706 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3707 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3708 set_bit(STRIPE_SYNCING, &sh->state);
3709 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10003710 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003711 }
3712 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003713 }
3714 clear_bit(STRIPE_DELAYED, &sh->state);
3715
3716 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3717 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3718 (unsigned long long)sh->sector, sh->state,
3719 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3720 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003721
NeilBrownacfe7262011-07-27 11:00:36 +10003722 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003723
NeilBrownbc2607f2011-07-28 11:39:22 +10003724 if (s.handle_bad_blocks) {
3725 set_bit(STRIPE_HANDLE, &sh->state);
3726 goto finish;
3727 }
3728
NeilBrown474af965fe2011-07-27 11:00:36 +10003729 if (unlikely(s.blocked_rdev)) {
3730 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003731 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003732 set_bit(STRIPE_HANDLE, &sh->state);
3733 goto finish;
3734 }
3735 /* There is nothing for the blocked_rdev to block */
3736 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3737 s.blocked_rdev = NULL;
3738 }
3739
3740 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3741 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3742 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3743 }
3744
3745 pr_debug("locked=%d uptodate=%d to_read=%d"
3746 " to_write=%d failed=%d failed_num=%d,%d\n",
3747 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3748 s.failed_num[0], s.failed_num[1]);
3749 /* check if the array has lost more than max_degraded devices and,
3750 * if so, some requests might need to be failed.
3751 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003752 if (s.failed > conf->max_degraded) {
3753 sh->check_state = 0;
3754 sh->reconstruct_state = 0;
3755 if (s.to_read+s.to_write+s.written)
3756 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003757 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003758 handle_failed_sync(conf, sh, &s);
3759 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003760
NeilBrown84789552011-07-27 11:00:36 +10003761 /* Now we check to see if any write operations have recently
3762 * completed
3763 */
3764 prexor = 0;
3765 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3766 prexor = 1;
3767 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3768 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3769 sh->reconstruct_state = reconstruct_state_idle;
3770
3771 /* All the 'written' buffers and the parity block are ready to
3772 * be written back to disk
3773 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003774 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3775 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003776 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003777 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3778 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003779 for (i = disks; i--; ) {
3780 struct r5dev *dev = &sh->dev[i];
3781 if (test_bit(R5_LOCKED, &dev->flags) &&
3782 (i == sh->pd_idx || i == sh->qd_idx ||
3783 dev->written)) {
3784 pr_debug("Writing block %d\n", i);
3785 set_bit(R5_Wantwrite, &dev->flags);
3786 if (prexor)
3787 continue;
3788 if (!test_bit(R5_Insync, &dev->flags) ||
3789 ((i == sh->pd_idx || i == sh->qd_idx) &&
3790 s.failed == 0))
3791 set_bit(STRIPE_INSYNC, &sh->state);
3792 }
3793 }
3794 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3795 s.dec_preread_active = 1;
3796 }
3797
NeilBrownef5b7c62012-11-22 09:13:36 +11003798 /*
3799 * might be able to return some write requests if the parity blocks
3800 * are safe, or on a failed drive
3801 */
3802 pdev = &sh->dev[sh->pd_idx];
3803 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3804 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3805 qdev = &sh->dev[sh->qd_idx];
3806 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3807 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3808 || conf->level < 6;
3809
3810 if (s.written &&
3811 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3812 && !test_bit(R5_LOCKED, &pdev->flags)
3813 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3814 test_bit(R5_Discard, &pdev->flags))))) &&
3815 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3816 && !test_bit(R5_LOCKED, &qdev->flags)
3817 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3818 test_bit(R5_Discard, &qdev->flags))))))
3819 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3820
3821 /* Now we might consider reading some blocks, either to check/generate
3822 * parity, or to satisfy requests
3823 * or to load a block that is being partially written.
3824 */
3825 if (s.to_read || s.non_overwrite
3826 || (conf->level == 6 && s.to_write && s.failed)
3827 || (s.syncing && (s.uptodate + s.compute < disks))
3828 || s.replacing
3829 || s.expanding)
3830 handle_stripe_fill(sh, &s, disks);
3831
NeilBrown84789552011-07-27 11:00:36 +10003832 /* Now to consider new write requests and what else, if anything
3833 * should be read. We do not handle new writes when:
3834 * 1/ A 'write' operation (copy+xor) is already in flight.
3835 * 2/ A 'check' operation is in flight, as it may clobber the parity
3836 * block.
3837 */
3838 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3839 handle_stripe_dirtying(conf, sh, &s, disks);
3840
3841 /* maybe we need to check and possibly fix the parity for this stripe
3842 * Any reads will already have been scheduled, so we just see if enough
3843 * data is available. The parity check is held off while parity
3844 * dependent operations are in flight.
3845 */
3846 if (sh->check_state ||
3847 (s.syncing && s.locked == 0 &&
3848 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3849 !test_bit(STRIPE_INSYNC, &sh->state))) {
3850 if (conf->level == 6)
3851 handle_parity_checks6(conf, sh, &s, disks);
3852 else
3853 handle_parity_checks5(conf, sh, &s, disks);
3854 }
NeilBrownc5a31002011-07-27 11:00:36 +10003855
NeilBrownf94c0b62013-07-22 12:57:21 +10003856 if ((s.replacing || s.syncing) && s.locked == 0
3857 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3858 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003859 /* Write out to replacement devices where possible */
3860 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10003861 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3862 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003863 set_bit(R5_WantReplace, &sh->dev[i].flags);
3864 set_bit(R5_LOCKED, &sh->dev[i].flags);
3865 s.locked++;
3866 }
NeilBrownf94c0b62013-07-22 12:57:21 +10003867 if (s.replacing)
3868 set_bit(STRIPE_INSYNC, &sh->state);
3869 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003870 }
3871 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10003872 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003873 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003874 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3875 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003876 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3877 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003878 }
3879
3880 /* If the failed drives are just a ReadError, then we might need
3881 * to progress the repair/check process
3882 */
3883 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3884 for (i = 0; i < s.failed; i++) {
3885 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3886 if (test_bit(R5_ReadError, &dev->flags)
3887 && !test_bit(R5_LOCKED, &dev->flags)
3888 && test_bit(R5_UPTODATE, &dev->flags)
3889 ) {
3890 if (!test_bit(R5_ReWrite, &dev->flags)) {
3891 set_bit(R5_Wantwrite, &dev->flags);
3892 set_bit(R5_ReWrite, &dev->flags);
3893 set_bit(R5_LOCKED, &dev->flags);
3894 s.locked++;
3895 } else {
3896 /* let's read it back */
3897 set_bit(R5_Wantread, &dev->flags);
3898 set_bit(R5_LOCKED, &dev->flags);
3899 s.locked++;
3900 }
3901 }
3902 }
3903
3904
NeilBrown3687c062011-07-27 11:00:36 +10003905 /* Finish reconstruct operations initiated by the expansion process */
3906 if (sh->reconstruct_state == reconstruct_state_result) {
3907 struct stripe_head *sh_src
3908 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3909 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3910 /* sh cannot be written until sh_src has been read.
3911 * so arrange for sh to be delayed a little
3912 */
3913 set_bit(STRIPE_DELAYED, &sh->state);
3914 set_bit(STRIPE_HANDLE, &sh->state);
3915 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3916 &sh_src->state))
3917 atomic_inc(&conf->preread_active_stripes);
3918 release_stripe(sh_src);
3919 goto finish;
3920 }
3921 if (sh_src)
3922 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003923
NeilBrown3687c062011-07-27 11:00:36 +10003924 sh->reconstruct_state = reconstruct_state_idle;
3925 clear_bit(STRIPE_EXPANDING, &sh->state);
3926 for (i = conf->raid_disks; i--; ) {
3927 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3928 set_bit(R5_LOCKED, &sh->dev[i].flags);
3929 s.locked++;
3930 }
3931 }
3932
3933 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3934 !sh->reconstruct_state) {
3935 /* Need to write out all blocks after computing parity */
3936 sh->disks = conf->raid_disks;
3937 stripe_set_idx(sh->sector, conf, 0, sh);
3938 schedule_reconstruction(sh, &s, 1, 1);
3939 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3940 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3941 atomic_dec(&conf->reshape_stripes);
3942 wake_up(&conf->wait_for_overlap);
3943 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3944 }
3945
3946 if (s.expanding && s.locked == 0 &&
3947 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3948 handle_stripe_expansion(conf, sh);
3949
3950finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003951 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003952 if (unlikely(s.blocked_rdev)) {
3953 if (conf->mddev->external)
3954 md_wait_for_blocked_rdev(s.blocked_rdev,
3955 conf->mddev);
3956 else
3957 /* Internal metadata will immediately
3958 * be written by raid5d, so we don't
3959 * need to wait here.
3960 */
3961 rdev_dec_pending(s.blocked_rdev,
3962 conf->mddev);
3963 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003964
NeilBrownbc2607f2011-07-28 11:39:22 +10003965 if (s.handle_bad_blocks)
3966 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003967 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003968 struct r5dev *dev = &sh->dev[i];
3969 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3970 /* We own a safe reference to the rdev */
3971 rdev = conf->disks[i].rdev;
3972 if (!rdev_set_badblocks(rdev, sh->sector,
3973 STRIPE_SECTORS, 0))
3974 md_error(conf->mddev, rdev);
3975 rdev_dec_pending(rdev, conf->mddev);
3976 }
NeilBrownb84db562011-07-28 11:39:23 +10003977 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3978 rdev = conf->disks[i].rdev;
3979 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003980 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003981 rdev_dec_pending(rdev, conf->mddev);
3982 }
NeilBrown977df362011-12-23 10:17:53 +11003983 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3984 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003985 if (!rdev)
3986 /* rdev have been moved down */
3987 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003988 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003989 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003990 rdev_dec_pending(rdev, conf->mddev);
3991 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003992 }
3993
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003994 if (s.ops_request)
3995 raid_run_ops(sh, s.ops_request);
3996
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003997 ops_run_io(sh, &s);
3998
NeilBrownc5709ef2011-07-26 11:35:20 +10003999 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004000 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004001 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004002 * have actually been submitted.
4003 */
4004 atomic_dec(&conf->preread_active_stripes);
4005 if (atomic_read(&conf->preread_active_stripes) <
4006 IO_THRESHOLD)
4007 md_wakeup_thread(conf->mddev->thread);
4008 }
4009
NeilBrownc5709ef2011-07-26 11:35:20 +10004010 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07004011
Dan Williams257a4b42011-11-08 16:22:06 +11004012 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004013}
4014
NeilBrownd1688a62011-10-11 16:49:52 +11004015static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
4017 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4018 while (!list_empty(&conf->delayed_list)) {
4019 struct list_head *l = conf->delayed_list.next;
4020 struct stripe_head *sh;
4021 sh = list_entry(l, struct stripe_head, lru);
4022 list_del_init(l);
4023 clear_bit(STRIPE_DELAYED, &sh->state);
4024 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4025 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004026 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004027 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 }
NeilBrown482c0832011-04-18 18:25:42 +10004029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
Shaohua Li566c09c2013-11-14 15:16:17 +11004032static void activate_bit_delay(struct r5conf *conf,
4033 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004034{
4035 /* device_lock is held */
4036 struct list_head head;
4037 list_add(&head, &conf->bitmap_list);
4038 list_del_init(&conf->bitmap_list);
4039 while (!list_empty(&head)) {
4040 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004041 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004042 list_del_init(&sh->lru);
4043 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004044 hash = sh->hash_lock_index;
4045 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004046 }
4047}
4048
NeilBrownfd01b882011-10-11 16:47:53 +11004049int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004050{
NeilBrownd1688a62011-10-11 16:49:52 +11004051 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004052
4053 /* No difference between reads and writes. Just check
4054 * how busy the stripe_cache is
4055 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004056
NeilBrownf022b2f2006-10-03 01:15:56 -07004057 if (conf->inactive_blocked)
4058 return 1;
4059 if (conf->quiesce)
4060 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004061 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004062 return 1;
4063
4064 return 0;
4065}
NeilBrown11d8a6e2010-07-26 11:57:07 +10004066EXPORT_SYMBOL_GPL(md_raid5_congested);
4067
4068static int raid5_congested(void *data, int bits)
4069{
NeilBrownfd01b882011-10-11 16:47:53 +11004070 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10004071
4072 return mddev_congested(mddev, bits) ||
4073 md_raid5_congested(mddev, bits);
4074}
NeilBrownf022b2f2006-10-03 01:15:56 -07004075
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004076/* We want read requests to align with chunks where possible,
4077 * but write requests don't need to.
4078 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004079static int raid5_mergeable_bvec(struct request_queue *q,
4080 struct bvec_merge_data *bvm,
4081 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004082{
NeilBrownfd01b882011-10-11 16:47:53 +11004083 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004084 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004085 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004086 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004087 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004088
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02004089 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004090 return biovec->bv_len; /* always allow writes to be mergeable */
4091
Andre Noll664e7c42009-06-18 08:45:27 +10004092 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4093 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08004094 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
4095 if (max < 0) max = 0;
4096 if (max <= biovec->bv_len && bio_sectors == 0)
4097 return biovec->bv_len;
4098 else
4099 return max;
4100}
4101
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004102
NeilBrownfd01b882011-10-11 16:47:53 +11004103static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004104{
Kent Overstreet4f024f32013-10-11 15:44:27 -07004105 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10004106 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004107 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004108
Andre Noll664e7c42009-06-18 08:45:27 +10004109 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
4110 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004111 return chunk_sectors >=
4112 ((sector & (chunk_sectors - 1)) + bio_sectors);
4113}
4114
4115/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004116 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4117 * later sampled by raid5d.
4118 */
NeilBrownd1688a62011-10-11 16:49:52 +11004119static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004120{
4121 unsigned long flags;
4122
4123 spin_lock_irqsave(&conf->device_lock, flags);
4124
4125 bi->bi_next = conf->retry_read_aligned_list;
4126 conf->retry_read_aligned_list = bi;
4127
4128 spin_unlock_irqrestore(&conf->device_lock, flags);
4129 md_wakeup_thread(conf->mddev->thread);
4130}
4131
4132
NeilBrownd1688a62011-10-11 16:49:52 +11004133static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004134{
4135 struct bio *bi;
4136
4137 bi = conf->retry_read_aligned;
4138 if (bi) {
4139 conf->retry_read_aligned = NULL;
4140 return bi;
4141 }
4142 bi = conf->retry_read_aligned_list;
4143 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004144 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004145 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004146 /*
4147 * this sets the active strip count to 1 and the processed
4148 * strip count to zero (upper 8 bits)
4149 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004150 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004151 }
4152
4153 return bi;
4154}
4155
4156
4157/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004158 * The "raid5_align_endio" should check if the read succeeded and if it
4159 * did, call bio_endio on the original bio (having bio_put the new bio
4160 * first).
4161 * If the read failed..
4162 */
NeilBrown6712ecf2007-09-27 12:47:43 +02004163static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004164{
4165 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004166 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004167 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004168 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11004169 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004170
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004171 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004172
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004173 rdev = (void*)raid_bi->bi_next;
4174 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004175 mddev = rdev->mddev;
4176 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004177
4178 rdev_dec_pending(rdev, conf->mddev);
4179
4180 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004181 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4182 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02004183 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004184 if (atomic_dec_and_test(&conf->active_aligned_reads))
4185 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02004186 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004187 }
4188
4189
Dan Williams45b42332007-07-09 11:56:43 -07004190 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004191
4192 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004193}
4194
Neil Brown387bb172007-02-08 14:20:29 -08004195static int bio_fits_rdev(struct bio *bi)
4196{
Jens Axboe165125e2007-07-24 09:28:11 +02004197 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08004198
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004199 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08004200 return 0;
4201 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05004202 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08004203 return 0;
4204
4205 if (q->merge_bvec_fn)
4206 /* it's too hard to apply the merge_bvec_fn at this stage,
4207 * just just give up
4208 */
4209 return 0;
4210
4211 return 1;
4212}
4213
4214
NeilBrownfd01b882011-10-11 16:47:53 +11004215static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004216{
NeilBrownd1688a62011-10-11 16:49:52 +11004217 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004218 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004219 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004220 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004221 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004222
4223 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004224 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004225 return 0;
4226 }
4227 /*
NeilBrowna167f662010-10-26 18:31:13 +11004228 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004229 */
NeilBrowna167f662010-10-26 18:31:13 +11004230 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004231 if (!align_bi)
4232 return 0;
4233 /*
4234 * set bi_end_io to a new function, and set bi_private to the
4235 * original bio.
4236 */
4237 align_bi->bi_end_io = raid5_align_endio;
4238 align_bi->bi_private = raid_bio;
4239 /*
4240 * compute position
4241 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004242 align_bi->bi_iter.bi_sector =
4243 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
4244 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004245
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004246 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004247 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004248 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4249 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4250 rdev->recovery_offset < end_sector) {
4251 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4252 if (rdev &&
4253 (test_bit(Faulty, &rdev->flags) ||
4254 !(test_bit(In_sync, &rdev->flags) ||
4255 rdev->recovery_offset >= end_sector)))
4256 rdev = NULL;
4257 }
4258 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004259 sector_t first_bad;
4260 int bad_sectors;
4261
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004262 atomic_inc(&rdev->nr_pending);
4263 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004264 raid_bio->bi_next = (void*)rdev;
4265 align_bi->bi_bdev = rdev->bdev;
4266 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004267
NeilBrown31c176e2011-07-28 11:39:22 +10004268 if (!bio_fits_rdev(align_bi) ||
Kent Overstreet4f024f32013-10-11 15:44:27 -07004269 is_badblock(rdev, align_bi->bi_iter.bi_sector,
4270 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004271 &first_bad, &bad_sectors)) {
4272 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004273 bio_put(align_bi);
4274 rdev_dec_pending(rdev, mddev);
4275 return 0;
4276 }
4277
majianpeng6c0544e2012-06-12 08:31:10 +08004278 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004279 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08004280
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004281 spin_lock_irq(&conf->device_lock);
4282 wait_event_lock_irq(conf->wait_for_stripe,
4283 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004284 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004285 atomic_inc(&conf->active_aligned_reads);
4286 spin_unlock_irq(&conf->device_lock);
4287
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004288 if (mddev->gendisk)
4289 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4290 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07004291 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004292 generic_make_request(align_bi);
4293 return 1;
4294 } else {
4295 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004296 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004297 return 0;
4298 }
4299}
4300
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004301/* __get_priority_stripe - get the next stripe to process
4302 *
4303 * Full stripe writes are allowed to pass preread active stripes up until
4304 * the bypass_threshold is exceeded. In general the bypass_count
4305 * increments when the handle_list is handled before the hold_list; however, it
4306 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4307 * stripe with in flight i/o. The bypass_count will be reset when the
4308 * head of the hold_list has changed, i.e. the head was promoted to the
4309 * handle_list.
4310 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004311static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004312{
Shaohua Li851c30c2013-08-28 14:30:16 +08004313 struct stripe_head *sh = NULL, *tmp;
4314 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004315 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004316
4317 if (conf->worker_cnt_per_group == 0) {
4318 handle_list = &conf->handle_list;
4319 } else if (group != ANY_GROUP) {
4320 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004321 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004322 } else {
4323 int i;
4324 for (i = 0; i < conf->group_cnt; i++) {
4325 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004326 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004327 if (!list_empty(handle_list))
4328 break;
4329 }
4330 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004331
4332 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4333 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004334 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004335 list_empty(&conf->hold_list) ? "empty" : "busy",
4336 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4337
Shaohua Li851c30c2013-08-28 14:30:16 +08004338 if (!list_empty(handle_list)) {
4339 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004340
4341 if (list_empty(&conf->hold_list))
4342 conf->bypass_count = 0;
4343 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4344 if (conf->hold_list.next == conf->last_hold)
4345 conf->bypass_count++;
4346 else {
4347 conf->last_hold = conf->hold_list.next;
4348 conf->bypass_count -= conf->bypass_threshold;
4349 if (conf->bypass_count < 0)
4350 conf->bypass_count = 0;
4351 }
4352 }
4353 } else if (!list_empty(&conf->hold_list) &&
4354 ((conf->bypass_threshold &&
4355 conf->bypass_count > conf->bypass_threshold) ||
4356 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004357
4358 list_for_each_entry(tmp, &conf->hold_list, lru) {
4359 if (conf->worker_cnt_per_group == 0 ||
4360 group == ANY_GROUP ||
4361 !cpu_online(tmp->cpu) ||
4362 cpu_to_group(tmp->cpu) == group) {
4363 sh = tmp;
4364 break;
4365 }
4366 }
4367
4368 if (sh) {
4369 conf->bypass_count -= conf->bypass_threshold;
4370 if (conf->bypass_count < 0)
4371 conf->bypass_count = 0;
4372 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004373 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004374 }
4375
4376 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004377 return NULL;
4378
Shaohua Libfc90cb2013-08-29 15:40:32 +08004379 if (wg) {
4380 wg->stripes_cnt--;
4381 sh->group = NULL;
4382 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004383 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08004384 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004385 return sh;
4386}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004387
Shaohua Li8811b592012-08-02 08:33:00 +10004388struct raid5_plug_cb {
4389 struct blk_plug_cb cb;
4390 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11004391 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10004392};
4393
4394static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4395{
4396 struct raid5_plug_cb *cb = container_of(
4397 blk_cb, struct raid5_plug_cb, cb);
4398 struct stripe_head *sh;
4399 struct mddev *mddev = cb->cb.data;
4400 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004401 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11004402 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10004403
4404 if (cb->list.next && !list_empty(&cb->list)) {
4405 spin_lock_irq(&conf->device_lock);
4406 while (!list_empty(&cb->list)) {
4407 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4408 list_del_init(&sh->lru);
4409 /*
4410 * avoid race release_stripe_plug() sees
4411 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4412 * is still in our list
4413 */
4414 smp_mb__before_clear_bit();
4415 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004416 /*
4417 * STRIPE_ON_RELEASE_LIST could be set here. In that
4418 * case, the count is always > 1 here
4419 */
Shaohua Li566c09c2013-11-14 15:16:17 +11004420 hash = sh->hash_lock_index;
4421 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11004422 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004423 }
4424 spin_unlock_irq(&conf->device_lock);
4425 }
Shaohua Li566c09c2013-11-14 15:16:17 +11004426 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4427 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004428 if (mddev->queue)
4429 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004430 kfree(cb);
4431}
4432
4433static void release_stripe_plug(struct mddev *mddev,
4434 struct stripe_head *sh)
4435{
4436 struct blk_plug_cb *blk_cb = blk_check_plugged(
4437 raid5_unplug, mddev,
4438 sizeof(struct raid5_plug_cb));
4439 struct raid5_plug_cb *cb;
4440
4441 if (!blk_cb) {
4442 release_stripe(sh);
4443 return;
4444 }
4445
4446 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4447
Shaohua Li566c09c2013-11-14 15:16:17 +11004448 if (cb->list.next == NULL) {
4449 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10004450 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11004451 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
4452 INIT_LIST_HEAD(cb->temp_inactive_list + i);
4453 }
Shaohua Li8811b592012-08-02 08:33:00 +10004454
4455 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4456 list_add_tail(&sh->lru, &cb->list);
4457 else
4458 release_stripe(sh);
4459}
4460
Shaohua Li620125f2012-10-11 13:49:05 +11004461static void make_discard_request(struct mddev *mddev, struct bio *bi)
4462{
4463 struct r5conf *conf = mddev->private;
4464 sector_t logical_sector, last_sector;
4465 struct stripe_head *sh;
4466 int remaining;
4467 int stripe_sectors;
4468
4469 if (mddev->reshape_position != MaxSector)
4470 /* Skip discard while reshape is happening */
4471 return;
4472
Kent Overstreet4f024f32013-10-11 15:44:27 -07004473 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4474 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11004475
4476 bi->bi_next = NULL;
4477 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4478
4479 stripe_sectors = conf->chunk_sectors *
4480 (conf->raid_disks - conf->max_degraded);
4481 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4482 stripe_sectors);
4483 sector_div(last_sector, stripe_sectors);
4484
4485 logical_sector *= conf->chunk_sectors;
4486 last_sector *= conf->chunk_sectors;
4487
4488 for (; logical_sector < last_sector;
4489 logical_sector += STRIPE_SECTORS) {
4490 DEFINE_WAIT(w);
4491 int d;
4492 again:
4493 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4494 prepare_to_wait(&conf->wait_for_overlap, &w,
4495 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004496 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4497 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4498 release_stripe(sh);
4499 schedule();
4500 goto again;
4501 }
4502 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004503 spin_lock_irq(&sh->stripe_lock);
4504 for (d = 0; d < conf->raid_disks; d++) {
4505 if (d == sh->pd_idx || d == sh->qd_idx)
4506 continue;
4507 if (sh->dev[d].towrite || sh->dev[d].toread) {
4508 set_bit(R5_Overlap, &sh->dev[d].flags);
4509 spin_unlock_irq(&sh->stripe_lock);
4510 release_stripe(sh);
4511 schedule();
4512 goto again;
4513 }
4514 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004515 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004516 finish_wait(&conf->wait_for_overlap, &w);
4517 for (d = 0; d < conf->raid_disks; d++) {
4518 if (d == sh->pd_idx || d == sh->qd_idx)
4519 continue;
4520 sh->dev[d].towrite = bi;
4521 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4522 raid5_inc_bi_active_stripes(bi);
4523 }
4524 spin_unlock_irq(&sh->stripe_lock);
4525 if (conf->mddev->bitmap) {
4526 for (d = 0;
4527 d < conf->raid_disks - conf->max_degraded;
4528 d++)
4529 bitmap_startwrite(mddev->bitmap,
4530 sh->sector,
4531 STRIPE_SECTORS,
4532 0);
4533 sh->bm_seq = conf->seq_flush + 1;
4534 set_bit(STRIPE_BIT_DELAY, &sh->state);
4535 }
4536
4537 set_bit(STRIPE_HANDLE, &sh->state);
4538 clear_bit(STRIPE_DELAYED, &sh->state);
4539 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4540 atomic_inc(&conf->preread_active_stripes);
4541 release_stripe_plug(mddev, sh);
4542 }
4543
4544 remaining = raid5_dec_bi_active_stripes(bi);
4545 if (remaining == 0) {
4546 md_write_end(mddev);
4547 bio_endio(bi, 0);
4548 }
4549}
4550
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004551static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
NeilBrownd1688a62011-10-11 16:49:52 +11004553 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004554 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 sector_t new_sector;
4556 sector_t logical_sector, last_sector;
4557 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004558 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004559 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08004560 DEFINE_WAIT(w);
4561 bool do_prepare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Tejun Heoe9c74692010-09-03 11:56:18 +02004563 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4564 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004565 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004566 }
4567
NeilBrown3d310eb2005-06-21 17:17:26 -07004568 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004569
NeilBrown802ba062006-12-13 00:34:13 -08004570 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004571 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004572 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004573 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004574
Shaohua Li620125f2012-10-11 13:49:05 +11004575 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4576 make_discard_request(mddev, bi);
4577 return;
4578 }
4579
Kent Overstreet4f024f32013-10-11 15:44:27 -07004580 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004581 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 bi->bi_next = NULL;
4583 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004584
Shaohua Li27c0f682014-04-09 11:25:47 +08004585 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004587 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10004588 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08004589
Shaohua Li27c0f682014-04-09 11:25:47 +08004590 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004591 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10004592 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11004593 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08004594 if (do_prepare)
4595 prepare_to_wait(&conf->wait_for_overlap, &w,
4596 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004597 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004598 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004599 * 64bit on a 32bit platform, and so it might be
4600 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004601 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004602 * the lock is dropped, so once we get a reference
4603 * to the stripe that we think it is, we will have
4604 * to check again.
4605 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004606 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004607 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004608 ? logical_sector < conf->reshape_progress
4609 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004610 previous = 1;
4611 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004612 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004613 ? logical_sector < conf->reshape_safe
4614 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004615 spin_unlock_irq(&conf->device_lock);
4616 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004617 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08004618 goto retry;
4619 }
4620 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004621 spin_unlock_irq(&conf->device_lock);
4622 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004623
NeilBrown112bf892009-03-31 14:39:38 +11004624 new_sector = raid5_compute_sector(conf, logical_sector,
4625 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004626 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004627 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10004628 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 (unsigned long long)logical_sector);
4630
NeilBrownb5663ba2009-03-31 14:39:38 +11004631 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004632 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004634 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004635 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004636 * stripe, so we must do the range check again.
4637 * Expansion could still move past after this
4638 * test, but as we are holding a reference to
4639 * 'sh', we know that if that happens,
4640 * STRIPE_EXPANDING will get set and the expansion
4641 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004642 */
4643 int must_retry = 0;
4644 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004645 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004646 ? logical_sector >= conf->reshape_progress
4647 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004648 /* mismatch, need to try again */
4649 must_retry = 1;
4650 spin_unlock_irq(&conf->device_lock);
4651 if (must_retry) {
4652 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004653 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004654 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004655 goto retry;
4656 }
4657 }
NeilBrownc46501b2013-08-27 15:52:13 +10004658 if (read_seqcount_retry(&conf->gen_lock, seq)) {
4659 /* Might have got the wrong stripe_head
4660 * by accident
4661 */
4662 release_stripe(sh);
4663 goto retry;
4664 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004665
Namhyung Kimffd96e32011-07-18 17:38:51 +10004666 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004667 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004668 logical_sector < mddev->suspend_hi) {
4669 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004670 /* As the suspend_* range is controlled by
4671 * userspace, we want an interruptible
4672 * wait.
4673 */
4674 flush_signals(current);
4675 prepare_to_wait(&conf->wait_for_overlap,
4676 &w, TASK_INTERRUPTIBLE);
4677 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08004678 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10004679 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004680 do_prepare = true;
4681 }
NeilBrowne464eaf2006-03-27 01:18:14 -08004682 goto retry;
4683 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004684
4685 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004686 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004687 /* Stripe is busy expanding or
4688 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 * and wait a while
4690 */
NeilBrown482c0832011-04-18 18:25:42 +10004691 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 release_stripe(sh);
4693 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08004694 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 goto retry;
4696 }
NeilBrown6ed30032008-02-06 01:40:00 -08004697 set_bit(STRIPE_HANDLE, &sh->state);
4698 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004699 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004700 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4701 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004702 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 } else {
4704 /* cannot get stripe for read-ahead, just give-up */
4705 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 break;
4707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 }
Shaohua Li27c0f682014-04-09 11:25:47 +08004709 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10004710
Shaohua Lie7836bd62012-07-19 16:01:31 +10004711 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004712 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
NeilBrown16a53ec2006-06-26 00:27:38 -07004714 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004716
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004717 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4718 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004719 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
NeilBrownfd01b882011-10-11 16:47:53 +11004723static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004724
NeilBrownfd01b882011-10-11 16:47:53 +11004725static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
NeilBrown52c03292006-06-26 00:27:43 -07004727 /* reshaping is quite different to recovery/resync so it is
4728 * handled quite separately ... here.
4729 *
4730 * On each call to sync_request, we gather one chunk worth of
4731 * destination stripes and flag them as expanding.
4732 * Then we find all the source stripes and request reads.
4733 * As the reads complete, handle_stripe will copy the data
4734 * into the destination stripe and release that stripe.
4735 */
NeilBrownd1688a62011-10-11 16:49:52 +11004736 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004738 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004739 int raid_disks = conf->previous_raid_disks;
4740 int data_disks = raid_disks - conf->max_degraded;
4741 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004742 int i;
4743 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004744 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004745 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004746 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004747 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004748
NeilBrownfef9c612009-03-31 15:16:46 +11004749 if (sector_nr == 0) {
4750 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004751 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004752 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4753 sector_nr = raid5_size(mddev, 0, 0)
4754 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004755 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004756 conf->reshape_progress > 0)
4757 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004758 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004759 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004760 mddev->curr_resync_completed = sector_nr;
4761 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004762 *skipped = 1;
4763 return sector_nr;
4764 }
NeilBrown52c03292006-06-26 00:27:43 -07004765 }
4766
NeilBrown7a661382009-03-31 15:21:40 +11004767 /* We need to process a full chunk at a time.
4768 * If old and new chunk sizes differ, we need to process the
4769 * largest of these
4770 */
Andre Noll664e7c42009-06-18 08:45:27 +10004771 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4772 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004773 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004774 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004775
NeilBrownb5254dd2012-05-21 09:27:01 +10004776 /* We update the metadata at least every 10 seconds, or when
4777 * the data about to be copied would over-write the source of
4778 * the data at the front of the range. i.e. one new_stripe
4779 * along from reshape_progress new_maps to after where
4780 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004781 */
NeilBrownfef9c612009-03-31 15:16:46 +11004782 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004783 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004784 readpos = conf->reshape_progress;
4785 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004786 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004787 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004788 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004789 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004790 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004791 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004792 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004793 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004794 readpos -= min_t(sector_t, reshape_sectors, readpos);
4795 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004796 }
NeilBrown52c03292006-06-26 00:27:43 -07004797
NeilBrownb5254dd2012-05-21 09:27:01 +10004798 /* Having calculated the 'writepos' possibly use it
4799 * to set 'stripe_addr' which is where we will write to.
4800 */
4801 if (mddev->reshape_backwards) {
4802 BUG_ON(conf->reshape_progress == 0);
4803 stripe_addr = writepos;
4804 BUG_ON((mddev->dev_sectors &
4805 ~((sector_t)reshape_sectors - 1))
4806 - reshape_sectors - stripe_addr
4807 != sector_nr);
4808 } else {
4809 BUG_ON(writepos != sector_nr + reshape_sectors);
4810 stripe_addr = sector_nr;
4811 }
4812
NeilBrownc8f517c2009-03-31 15:28:40 +11004813 /* 'writepos' is the most advanced device address we might write.
4814 * 'readpos' is the least advanced device address we might read.
4815 * 'safepos' is the least address recorded in the metadata as having
4816 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004817 * If there is a min_offset_diff, these are adjusted either by
4818 * increasing the safepos/readpos if diff is negative, or
4819 * increasing writepos if diff is positive.
4820 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004821 * ensure safety in the face of a crash - that must be done by userspace
4822 * making a backup of the data. So in that case there is no particular
4823 * rush to update metadata.
4824 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4825 * update the metadata to advance 'safepos' to match 'readpos' so that
4826 * we can be safe in the event of a crash.
4827 * So we insist on updating metadata if safepos is behind writepos and
4828 * readpos is beyond writepos.
4829 * In any case, update the metadata every 10 seconds.
4830 * Maybe that number should be configurable, but I'm not sure it is
4831 * worth it.... maybe it could be a multiple of safemode_delay???
4832 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004833 if (conf->min_offset_diff < 0) {
4834 safepos += -conf->min_offset_diff;
4835 readpos += -conf->min_offset_diff;
4836 } else
4837 writepos += conf->min_offset_diff;
4838
NeilBrown2c810cd2012-05-21 09:27:00 +10004839 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004840 ? (safepos > writepos && readpos < writepos)
4841 : (safepos < writepos && readpos > writepos)) ||
4842 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004843 /* Cannot proceed until we've updated the superblock... */
4844 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11004845 atomic_read(&conf->reshape_stripes)==0
4846 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4847 if (atomic_read(&conf->reshape_stripes) != 0)
4848 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11004849 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004850 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004851 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004852 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004853 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004854 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11004855 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4856 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4857 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07004858 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004859 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004860 spin_unlock_irq(&conf->device_lock);
4861 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004862 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004863 }
4864
NeilBrownab69ae12009-03-31 15:26:47 +11004865 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004866 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004867 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004868 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004869 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004870 set_bit(STRIPE_EXPANDING, &sh->state);
4871 atomic_inc(&conf->reshape_stripes);
4872 /* If any of this stripe is beyond the end of the old
4873 * array, then we need to zero those blocks
4874 */
4875 for (j=sh->disks; j--;) {
4876 sector_t s;
4877 if (j == sh->pd_idx)
4878 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004879 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004880 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004881 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004882 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004883 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004884 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004885 continue;
4886 }
4887 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4888 set_bit(R5_Expanded, &sh->dev[j].flags);
4889 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4890 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004891 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004892 set_bit(STRIPE_EXPAND_READY, &sh->state);
4893 set_bit(STRIPE_HANDLE, &sh->state);
4894 }
NeilBrownab69ae12009-03-31 15:26:47 +11004895 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004896 }
4897 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004898 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004899 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004900 else
NeilBrown7a661382009-03-31 15:21:40 +11004901 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004902 spin_unlock_irq(&conf->device_lock);
4903 /* Ok, those stripe are ready. We can start scheduling
4904 * reads on the source stripes.
4905 * The source stripes are determined by mapping the first and last
4906 * block on the destination stripes.
4907 */
NeilBrown52c03292006-06-26 00:27:43 -07004908 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004909 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004910 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004911 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004912 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004913 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004914 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004915 if (last_sector >= mddev->dev_sectors)
4916 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004917 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004918 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004919 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4920 set_bit(STRIPE_HANDLE, &sh->state);
4921 release_stripe(sh);
4922 first_sector += STRIPE_SECTORS;
4923 }
NeilBrownab69ae12009-03-31 15:26:47 +11004924 /* Now that the sources are clearly marked, we can release
4925 * the destination stripes
4926 */
4927 while (!list_empty(&stripes)) {
4928 sh = list_entry(stripes.next, struct stripe_head, lru);
4929 list_del_init(&sh->lru);
4930 release_stripe(sh);
4931 }
NeilBrownc6207272008-02-06 01:39:52 -08004932 /* If this takes us to the resync_max point where we have to pause,
4933 * then we need to write out the superblock.
4934 */
NeilBrown7a661382009-03-31 15:21:40 +11004935 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004936 if ((sector_nr - mddev->curr_resync_completed) * 2
4937 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004938 /* Cannot proceed until we've updated the superblock... */
4939 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11004940 atomic_read(&conf->reshape_stripes) == 0
4941 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4942 if (atomic_read(&conf->reshape_stripes) != 0)
4943 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11004944 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004945 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004946 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004947 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4948 md_wakeup_thread(mddev->thread);
4949 wait_event(mddev->sb_wait,
4950 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
NeilBrownc91abf52013-11-19 12:02:01 +11004951 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4952 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4953 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08004954 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004955 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004956 spin_unlock_irq(&conf->device_lock);
4957 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004958 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004959 }
NeilBrownc91abf52013-11-19 12:02:01 +11004960ret:
NeilBrown7a661382009-03-31 15:21:40 +11004961 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004962}
4963
4964/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004965static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004966{
NeilBrownd1688a62011-10-11 16:49:52 +11004967 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004968 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004969 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004970 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004971 int still_degraded = 0;
4972 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
NeilBrown72626682005-09-09 16:23:54 -07004974 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004976
NeilBrown29269552006-03-27 01:18:10 -08004977 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4978 end_reshape(conf);
4979 return 0;
4980 }
NeilBrown72626682005-09-09 16:23:54 -07004981
4982 if (mddev->curr_resync < max_sector) /* aborted */
4983 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4984 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004985 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004986 conf->fullsync = 0;
4987 bitmap_close_sync(mddev->bitmap);
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 return 0;
4990 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004991
NeilBrown64bd6602009-08-03 10:59:58 +10004992 /* Allow raid5_quiesce to complete */
4993 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4994
NeilBrown52c03292006-06-26 00:27:43 -07004995 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4996 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004997
NeilBrownc6207272008-02-06 01:39:52 -08004998 /* No need to check resync_max as we never do more than one
4999 * stripe, and as resync_max will always be on a chunk boundary,
5000 * if the check in md_do_sync didn't fire, there is no chance
5001 * of overstepping resync_max here
5002 */
5003
NeilBrown16a53ec2006-06-26 00:27:38 -07005004 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 * to resync, then assert that we are finished, because there is
5006 * nothing we can do.
5007 */
NeilBrown3285edf2006-06-26 00:27:55 -07005008 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005009 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005010 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005011 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 return rv;
5013 }
majianpeng6f608042013-04-24 11:42:41 +10005014 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5015 !conf->fullsync &&
5016 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5017 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005018 /* we can skip this block, and probably more */
5019 sync_blocks /= STRIPE_SECTORS;
5020 *skipped = 1;
5021 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
NeilBrownb47490c2008-02-06 01:39:50 -08005024 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
5025
NeilBrowna8c906c2009-06-09 14:39:59 +10005026 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10005028 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005030 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005032 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005034 /* Need to check if array will still be degraded after recovery/resync
5035 * We don't need to check the 'failed' flag as when that gets set,
5036 * recovery aborts.
5037 */
NeilBrownf001a702009-06-09 14:30:31 +10005038 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07005039 if (conf->disks[i].rdev == NULL)
5040 still_degraded = 1;
5041
5042 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5043
NeilBrown83206d62011-07-26 11:19:49 +10005044 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
NeilBrown14425772009-10-16 15:55:25 +11005046 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 release_stripe(sh);
5048
5049 return STRIPE_SECTORS;
5050}
5051
NeilBrownd1688a62011-10-11 16:49:52 +11005052static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005053{
5054 /* We may not be able to submit a whole bio at once as there
5055 * may not be enough stripe_heads available.
5056 * We cannot pre-allocate enough stripe_heads as we may need
5057 * more than exist in the cache (if we allow ever large chunks).
5058 * So we do one stripe head at a time and record in
5059 * ->bi_hw_segments how many have been done.
5060 *
5061 * We *know* that this entire raid_bio is in one chunk, so
5062 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5063 */
5064 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005065 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005066 sector_t sector, logical_sector, last_sector;
5067 int scnt = 0;
5068 int remaining;
5069 int handled = 0;
5070
Kent Overstreet4f024f32013-10-11 15:44:27 -07005071 logical_sector = raid_bio->bi_iter.bi_sector &
5072 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005073 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005074 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005075 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005076
5077 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005078 logical_sector += STRIPE_SECTORS,
5079 sector += STRIPE_SECTORS,
5080 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005081
Shaohua Lie7836bd62012-07-19 16:01:31 +10005082 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005083 /* already done this stripe */
5084 continue;
5085
NeilBrowna8c906c2009-06-09 14:39:59 +10005086 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005087
5088 if (!sh) {
5089 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005090 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005091 conf->retry_read_aligned = raid_bio;
5092 return handled;
5093 }
5094
Neil Brown387bb172007-02-08 14:20:29 -08005095 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
5096 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005097 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005098 conf->retry_read_aligned = raid_bio;
5099 return handled;
5100 }
5101
majianpeng3f9e7c12012-07-31 10:04:21 +10005102 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005103 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005104 release_stripe(sh);
5105 handled++;
5106 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005107 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005108 if (remaining == 0) {
5109 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5110 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10005111 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005112 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005113 if (atomic_dec_and_test(&conf->active_aligned_reads))
5114 wake_up(&conf->wait_for_stripe);
5115 return handled;
5116}
5117
Shaohua Libfc90cb2013-08-29 15:40:32 +08005118static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005119 struct r5worker *worker,
5120 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005121{
5122 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005123 int i, batch_size = 0, hash;
5124 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005125
5126 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005127 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005128 batch[batch_size++] = sh;
5129
Shaohua Li566c09c2013-11-14 15:16:17 +11005130 if (batch_size == 0) {
5131 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5132 if (!list_empty(temp_inactive_list + i))
5133 break;
5134 if (i == NR_STRIPE_HASH_LOCKS)
5135 return batch_size;
5136 release_inactive = true;
5137 }
Shaohua Li46a06402012-08-02 08:33:15 +10005138 spin_unlock_irq(&conf->device_lock);
5139
Shaohua Li566c09c2013-11-14 15:16:17 +11005140 release_inactive_stripe_list(conf, temp_inactive_list,
5141 NR_STRIPE_HASH_LOCKS);
5142
5143 if (release_inactive) {
5144 spin_lock_irq(&conf->device_lock);
5145 return 0;
5146 }
5147
Shaohua Li46a06402012-08-02 08:33:15 +10005148 for (i = 0; i < batch_size; i++)
5149 handle_stripe(batch[i]);
5150
5151 cond_resched();
5152
5153 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005154 for (i = 0; i < batch_size; i++) {
5155 hash = batch[i]->hash_lock_index;
5156 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5157 }
Shaohua Li46a06402012-08-02 08:33:15 +10005158 return batch_size;
5159}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005160
Shaohua Li851c30c2013-08-28 14:30:16 +08005161static void raid5_do_work(struct work_struct *work)
5162{
5163 struct r5worker *worker = container_of(work, struct r5worker, work);
5164 struct r5worker_group *group = worker->group;
5165 struct r5conf *conf = group->conf;
5166 int group_id = group - conf->worker_groups;
5167 int handled;
5168 struct blk_plug plug;
5169
5170 pr_debug("+++ raid5worker active\n");
5171
5172 blk_start_plug(&plug);
5173 handled = 0;
5174 spin_lock_irq(&conf->device_lock);
5175 while (1) {
5176 int batch_size, released;
5177
Shaohua Li566c09c2013-11-14 15:16:17 +11005178 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005179
Shaohua Li566c09c2013-11-14 15:16:17 +11005180 batch_size = handle_active_stripes(conf, group_id, worker,
5181 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005182 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005183 if (!batch_size && !released)
5184 break;
5185 handled += batch_size;
5186 }
5187 pr_debug("%d stripes handled\n", handled);
5188
5189 spin_unlock_irq(&conf->device_lock);
5190 blk_finish_plug(&plug);
5191
5192 pr_debug("--- raid5worker inactive\n");
5193}
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195/*
5196 * This is our raid5 kernel thread.
5197 *
5198 * We scan the hash table for stripes which can be handled now.
5199 * During the scan, completed stripes are saved for us by the interrupt
5200 * handler, so that they will not have to wait for our next wakeup.
5201 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005202static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203{
Shaohua Li4ed87312012-10-11 13:34:00 +11005204 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005205 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005207 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
Dan Williams45b42332007-07-09 11:56:43 -07005209 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
5211 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005213 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 handled = 0;
5215 spin_lock_irq(&conf->device_lock);
5216 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005217 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005218 int batch_size, released;
5219
Shaohua Li566c09c2013-11-14 15:16:17 +11005220 released = release_stripe_list(conf, conf->temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
NeilBrown0021b7b2012-07-31 09:08:14 +02005222 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005223 !list_empty(&conf->bitmap_list)) {
5224 /* Now is a good time to flush some bitmap updates */
5225 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005226 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005227 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005228 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005229 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005230 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005231 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005232 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005233
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005234 while ((bio = remove_bio_from_retry(conf))) {
5235 int ok;
5236 spin_unlock_irq(&conf->device_lock);
5237 ok = retry_aligned_read(conf, bio);
5238 spin_lock_irq(&conf->device_lock);
5239 if (!ok)
5240 break;
5241 handled++;
5242 }
5243
Shaohua Li566c09c2013-11-14 15:16:17 +11005244 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5245 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005246 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005248 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Shaohua Li46a06402012-08-02 08:33:15 +10005250 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5251 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005252 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005253 spin_lock_irq(&conf->device_lock);
5254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 }
Dan Williams45b42332007-07-09 11:56:43 -07005256 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
5258 spin_unlock_irq(&conf->device_lock);
5259
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005260 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005261 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
Dan Williams45b42332007-07-09 11:56:43 -07005263 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264}
5265
NeilBrown3f294f42005-11-08 21:39:25 -08005266static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005267raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005268{
NeilBrownd1688a62011-10-11 16:49:52 +11005269 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005270 if (conf)
5271 return sprintf(page, "%d\n", conf->max_nr_stripes);
5272 else
5273 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005274}
5275
NeilBrownc41d4ac2010-06-01 19:37:24 +10005276int
NeilBrownfd01b882011-10-11 16:47:53 +11005277raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005278{
NeilBrownd1688a62011-10-11 16:49:52 +11005279 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005280 int err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005281 int hash;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005282
5283 if (size <= 16 || size > 32768)
5284 return -EINVAL;
Shaohua Li566c09c2013-11-14 15:16:17 +11005285 hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005286 while (size < conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005287 if (drop_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005288 conf->max_nr_stripes--;
5289 else
5290 break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005291 hash--;
5292 if (hash < 0)
5293 hash = NR_STRIPE_HASH_LOCKS - 1;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005294 }
5295 err = md_allow_write(mddev);
5296 if (err)
5297 return err;
Shaohua Li566c09c2013-11-14 15:16:17 +11005298 hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005299 while (size > conf->max_nr_stripes) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005300 if (grow_one_stripe(conf, hash))
NeilBrownc41d4ac2010-06-01 19:37:24 +10005301 conf->max_nr_stripes++;
5302 else break;
Shaohua Li566c09c2013-11-14 15:16:17 +11005303 hash = (hash + 1) % NR_STRIPE_HASH_LOCKS;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005304 }
5305 return 0;
5306}
5307EXPORT_SYMBOL(raid5_set_cache_size);
5308
NeilBrown3f294f42005-11-08 21:39:25 -08005309static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005310raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005311{
NeilBrownd1688a62011-10-11 16:49:52 +11005312 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005313 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005314 int err;
5315
NeilBrown3f294f42005-11-08 21:39:25 -08005316 if (len >= PAGE_SIZE)
5317 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08005318 if (!conf)
5319 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08005320
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005321 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005322 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005323 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005324 if (err)
5325 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08005326 return len;
5327}
NeilBrown007583c2005-11-08 21:39:30 -08005328
NeilBrown96de1e62005-11-08 21:39:39 -08005329static struct md_sysfs_entry
5330raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5331 raid5_show_stripe_cache_size,
5332 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005333
5334static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005335raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005336{
NeilBrownd1688a62011-10-11 16:49:52 +11005337 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005338 if (conf)
5339 return sprintf(page, "%d\n", conf->bypass_threshold);
5340 else
5341 return 0;
5342}
5343
5344static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005345raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005346{
NeilBrownd1688a62011-10-11 16:49:52 +11005347 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07005348 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005349 if (len >= PAGE_SIZE)
5350 return -EINVAL;
5351 if (!conf)
5352 return -ENODEV;
5353
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005354 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005355 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07005356 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005357 return -EINVAL;
5358 conf->bypass_threshold = new;
5359 return len;
5360}
5361
5362static struct md_sysfs_entry
5363raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5364 S_IRUGO | S_IWUSR,
5365 raid5_show_preread_threshold,
5366 raid5_store_preread_threshold);
5367
5368static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005369stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005370{
NeilBrownd1688a62011-10-11 16:49:52 +11005371 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005372 if (conf)
5373 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5374 else
5375 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005376}
5377
NeilBrown96de1e62005-11-08 21:39:39 -08005378static struct md_sysfs_entry
5379raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005380
Shaohua Lib7214202013-08-27 17:50:42 +08005381static ssize_t
5382raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
5383{
5384 struct r5conf *conf = mddev->private;
5385 if (conf)
5386 return sprintf(page, "%d\n", conf->worker_cnt_per_group);
5387 else
5388 return 0;
5389}
5390
majianpeng60aaf932013-11-14 15:16:20 +11005391static int alloc_thread_groups(struct r5conf *conf, int cnt,
5392 int *group_cnt,
5393 int *worker_cnt_per_group,
5394 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08005395static ssize_t
5396raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
5397{
5398 struct r5conf *conf = mddev->private;
5399 unsigned long new;
5400 int err;
majianpeng60aaf932013-11-14 15:16:20 +11005401 struct r5worker_group *new_groups, *old_groups;
5402 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08005403
5404 if (len >= PAGE_SIZE)
5405 return -EINVAL;
5406 if (!conf)
5407 return -ENODEV;
5408
5409 if (kstrtoul(page, 10, &new))
5410 return -EINVAL;
5411
5412 if (new == conf->worker_cnt_per_group)
5413 return len;
5414
5415 mddev_suspend(mddev);
5416
5417 old_groups = conf->worker_groups;
majianpengd206dcf2013-11-14 15:16:19 +11005418 if (old_groups)
5419 flush_workqueue(raid5_wq);
Shaohua Lib7214202013-08-27 17:50:42 +08005420
majianpeng60aaf932013-11-14 15:16:20 +11005421 err = alloc_thread_groups(conf, new,
5422 &group_cnt, &worker_cnt_per_group,
5423 &new_groups);
5424 if (!err) {
5425 spin_lock_irq(&conf->device_lock);
5426 conf->group_cnt = group_cnt;
5427 conf->worker_cnt_per_group = worker_cnt_per_group;
5428 conf->worker_groups = new_groups;
5429 spin_unlock_irq(&conf->device_lock);
5430
Shaohua Lib7214202013-08-27 17:50:42 +08005431 if (old_groups)
5432 kfree(old_groups[0].workers);
5433 kfree(old_groups);
5434 }
5435
5436 mddev_resume(mddev);
5437
5438 if (err)
5439 return err;
5440 return len;
5441}
5442
5443static struct md_sysfs_entry
5444raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
5445 raid5_show_group_thread_cnt,
5446 raid5_store_group_thread_cnt);
5447
NeilBrown007583c2005-11-08 21:39:30 -08005448static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005449 &raid5_stripecache_size.attr,
5450 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005451 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08005452 &raid5_group_thread_cnt.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005453 NULL,
5454};
NeilBrown007583c2005-11-08 21:39:30 -08005455static struct attribute_group raid5_attrs_group = {
5456 .name = NULL,
5457 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005458};
5459
majianpeng60aaf932013-11-14 15:16:20 +11005460static int alloc_thread_groups(struct r5conf *conf, int cnt,
5461 int *group_cnt,
5462 int *worker_cnt_per_group,
5463 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08005464{
Shaohua Li566c09c2013-11-14 15:16:17 +11005465 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08005466 ssize_t size;
5467 struct r5worker *workers;
5468
majianpeng60aaf932013-11-14 15:16:20 +11005469 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08005470 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11005471 *group_cnt = 0;
5472 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005473 return 0;
5474 }
majianpeng60aaf932013-11-14 15:16:20 +11005475 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08005476 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11005477 workers = kzalloc(size * *group_cnt, GFP_NOIO);
5478 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
5479 *group_cnt, GFP_NOIO);
5480 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005481 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11005482 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08005483 return -ENOMEM;
5484 }
5485
majianpeng60aaf932013-11-14 15:16:20 +11005486 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005487 struct r5worker_group *group;
5488
NeilBrown0c775d52013-11-25 11:12:43 +11005489 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005490 INIT_LIST_HEAD(&group->handle_list);
5491 group->conf = conf;
5492 group->workers = workers + i * cnt;
5493
5494 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11005495 struct r5worker *worker = group->workers + j;
5496 worker->group = group;
5497 INIT_WORK(&worker->work, raid5_do_work);
5498
5499 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
5500 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08005501 }
5502 }
5503
5504 return 0;
5505}
5506
5507static void free_thread_groups(struct r5conf *conf)
5508{
5509 if (conf->worker_groups)
5510 kfree(conf->worker_groups[0].workers);
5511 kfree(conf->worker_groups);
5512 conf->worker_groups = NULL;
5513}
5514
Dan Williams80c3a6c2009-03-17 18:10:40 -07005515static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005516raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005517{
NeilBrownd1688a62011-10-11 16:49:52 +11005518 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005519
5520 if (!sectors)
5521 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005522 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005523 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005524 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005525
Andre Noll9d8f0362009-06-18 08:45:01 +10005526 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005527 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005528 return sectors * (raid_disks - conf->max_degraded);
5529}
5530
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305531static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5532{
5533 safe_put_page(percpu->spare_page);
5534 kfree(percpu->scribble);
5535 percpu->spare_page = NULL;
5536 percpu->scribble = NULL;
5537}
5538
5539static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5540{
5541 if (conf->level == 6 && !percpu->spare_page)
5542 percpu->spare_page = alloc_page(GFP_KERNEL);
5543 if (!percpu->scribble)
5544 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5545
5546 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
5547 free_scratch_buffer(conf, percpu);
5548 return -ENOMEM;
5549 }
5550
5551 return 0;
5552}
5553
NeilBrownd1688a62011-10-11 16:49:52 +11005554static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005555{
Dan Williams36d1c642009-07-14 11:48:22 -07005556 unsigned long cpu;
5557
5558 if (!conf->percpu)
5559 return;
5560
Dan Williams36d1c642009-07-14 11:48:22 -07005561#ifdef CONFIG_HOTPLUG_CPU
5562 unregister_cpu_notifier(&conf->cpu_notify);
5563#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305564
5565 get_online_cpus();
5566 for_each_possible_cpu(cpu)
5567 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005568 put_online_cpus();
5569
5570 free_percpu(conf->percpu);
5571}
5572
NeilBrownd1688a62011-10-11 16:49:52 +11005573static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005574{
Shaohua Li851c30c2013-08-28 14:30:16 +08005575 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005576 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005577 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005578 kfree(conf->disks);
5579 kfree(conf->stripe_hashtbl);
5580 kfree(conf);
5581}
5582
Dan Williams36d1c642009-07-14 11:48:22 -07005583#ifdef CONFIG_HOTPLUG_CPU
5584static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5585 void *hcpu)
5586{
NeilBrownd1688a62011-10-11 16:49:52 +11005587 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005588 long cpu = (long)hcpu;
5589 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5590
5591 switch (action) {
5592 case CPU_UP_PREPARE:
5593 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305594 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07005595 pr_err("%s: failed memory allocation for cpu%ld\n",
5596 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005597 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005598 }
5599 break;
5600 case CPU_DEAD:
5601 case CPU_DEAD_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305602 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005603 break;
5604 default:
5605 break;
5606 }
5607 return NOTIFY_OK;
5608}
5609#endif
5610
NeilBrownd1688a62011-10-11 16:49:52 +11005611static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005612{
5613 unsigned long cpu;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305614 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07005615
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305616 conf->percpu = alloc_percpu(struct raid5_percpu);
5617 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07005618 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07005619
Dan Williams36d1c642009-07-14 11:48:22 -07005620#ifdef CONFIG_HOTPLUG_CPU
5621 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5622 conf->cpu_notify.priority = 0;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305623 err = register_cpu_notifier(&conf->cpu_notify);
5624 if (err)
5625 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07005626#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05305627
5628 get_online_cpus();
5629 for_each_present_cpu(cpu) {
5630 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5631 if (err) {
5632 pr_err("%s: failed memory allocation for cpu%ld\n",
5633 __func__, cpu);
5634 break;
5635 }
5636 }
Dan Williams36d1c642009-07-14 11:48:22 -07005637 put_online_cpus();
5638
5639 return err;
5640}
5641
NeilBrownd1688a62011-10-11 16:49:52 +11005642static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
NeilBrownd1688a62011-10-11 16:49:52 +11005644 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005645 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005646 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005648 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11005649 int i;
majianpeng60aaf932013-11-14 15:16:20 +11005650 int group_cnt, worker_cnt_per_group;
5651 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
NeilBrown91adb562009-03-31 14:39:39 +11005653 if (mddev->new_level != 5
5654 && mddev->new_level != 4
5655 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005656 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005657 mdname(mddev), mddev->new_level);
5658 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 }
NeilBrown91adb562009-03-31 14:39:39 +11005660 if ((mddev->new_level == 5
5661 && !algorithm_valid_raid5(mddev->new_layout)) ||
5662 (mddev->new_level == 6
5663 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005664 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005665 mdname(mddev), mddev->new_layout);
5666 return ERR_PTR(-EIO);
5667 }
5668 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005669 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005670 mdname(mddev), mddev->raid_disks);
5671 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Andre Noll664e7c42009-06-18 08:45:27 +10005674 if (!mddev->new_chunk_sectors ||
5675 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5676 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005677 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5678 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005679 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005680 }
5681
NeilBrownd1688a62011-10-11 16:49:52 +11005682 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005683 if (conf == NULL)
5684 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08005685 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11005686 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
5687 &new_group)) {
5688 conf->group_cnt = group_cnt;
5689 conf->worker_cnt_per_group = worker_cnt_per_group;
5690 conf->worker_groups = new_group;
5691 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08005692 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005693 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10005694 seqcount_init(&conf->gen_lock);
Dan Williamsf5efd452009-10-16 15:55:38 +11005695 init_waitqueue_head(&conf->wait_for_stripe);
5696 init_waitqueue_head(&conf->wait_for_overlap);
5697 INIT_LIST_HEAD(&conf->handle_list);
5698 INIT_LIST_HEAD(&conf->hold_list);
5699 INIT_LIST_HEAD(&conf->delayed_list);
5700 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005701 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11005702 atomic_set(&conf->active_stripes, 0);
5703 atomic_set(&conf->preread_active_stripes, 0);
5704 atomic_set(&conf->active_aligned_reads, 0);
5705 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005706 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005707
5708 conf->raid_disks = mddev->raid_disks;
5709 if (mddev->reshape_position == MaxSector)
5710 conf->previous_raid_disks = mddev->raid_disks;
5711 else
5712 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005713 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5714 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005715
NeilBrown5e5e3e72009-10-16 16:35:30 +11005716 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005717 GFP_KERNEL);
5718 if (!conf->disks)
5719 goto abort;
5720
5721 conf->mddev = mddev;
5722
5723 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5724 goto abort;
5725
Shaohua Li566c09c2013-11-14 15:16:17 +11005726 /* We init hash_locks[0] separately to that it can be used
5727 * as the reference lock in the spin_lock_nest_lock() call
5728 * in lock_all_device_hash_locks_irq in order to convince
5729 * lockdep that we know what we are doing.
5730 */
5731 spin_lock_init(conf->hash_locks);
5732 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
5733 spin_lock_init(conf->hash_locks + i);
5734
5735 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5736 INIT_LIST_HEAD(conf->inactive_list + i);
5737
5738 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5739 INIT_LIST_HEAD(conf->temp_inactive_list + i);
5740
Dan Williams36d1c642009-07-14 11:48:22 -07005741 conf->level = mddev->new_level;
5742 if (raid5_alloc_percpu(conf) != 0)
5743 goto abort;
5744
NeilBrown0c55e022010-05-03 14:09:02 +10005745 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005746
NeilBrowndafb20f2012-03-19 12:46:39 +11005747 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005748 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005749 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005750 || raid_disk < 0)
5751 continue;
5752 disk = conf->disks + raid_disk;
5753
NeilBrown17045f52011-12-23 10:17:53 +11005754 if (test_bit(Replacement, &rdev->flags)) {
5755 if (disk->replacement)
5756 goto abort;
5757 disk->replacement = rdev;
5758 } else {
5759 if (disk->rdev)
5760 goto abort;
5761 disk->rdev = rdev;
5762 }
NeilBrown91adb562009-03-31 14:39:39 +11005763
5764 if (test_bit(In_sync, &rdev->flags)) {
5765 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005766 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5767 " disk %d\n",
5768 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005769 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005770 /* Cannot rely on bitmap to complete recovery */
5771 conf->fullsync = 1;
5772 }
5773
Andre Noll09c9e5f2009-06-18 08:45:55 +10005774 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005775 conf->level = mddev->new_level;
5776 if (conf->level == 6)
5777 conf->max_degraded = 2;
5778 else
5779 conf->max_degraded = 1;
5780 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11005781 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005782 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005783 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005784 conf->prev_algo = mddev->layout;
5785 }
NeilBrown91adb562009-03-31 14:39:39 +11005786
5787 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005788 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11005789 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
Shaohua Li566c09c2013-11-14 15:16:17 +11005790 if (grow_stripes(conf, NR_STRIPES)) {
NeilBrown91adb562009-03-31 14:39:39 +11005791 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005792 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5793 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005794 goto abort;
5795 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005796 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5797 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005798
NeilBrown02326052012-07-03 15:56:52 +10005799 sprintf(pers_name, "raid%d", mddev->new_level);
5800 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005801 if (!conf->thread) {
5802 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005803 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005804 mdname(mddev));
5805 goto abort;
5806 }
5807
5808 return conf;
5809
5810 abort:
5811 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005812 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005813 return ERR_PTR(-EIO);
5814 } else
5815 return ERR_PTR(-ENOMEM);
5816}
5817
NeilBrownc148ffd2009-11-13 17:47:00 +11005818
5819static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5820{
5821 switch (algo) {
5822 case ALGORITHM_PARITY_0:
5823 if (raid_disk < max_degraded)
5824 return 1;
5825 break;
5826 case ALGORITHM_PARITY_N:
5827 if (raid_disk >= raid_disks - max_degraded)
5828 return 1;
5829 break;
5830 case ALGORITHM_PARITY_0_6:
5831 if (raid_disk == 0 ||
5832 raid_disk == raid_disks - 1)
5833 return 1;
5834 break;
5835 case ALGORITHM_LEFT_ASYMMETRIC_6:
5836 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5837 case ALGORITHM_LEFT_SYMMETRIC_6:
5838 case ALGORITHM_RIGHT_SYMMETRIC_6:
5839 if (raid_disk == raid_disks - 1)
5840 return 1;
5841 }
5842 return 0;
5843}
5844
NeilBrownfd01b882011-10-11 16:47:53 +11005845static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005846{
NeilBrownd1688a62011-10-11 16:49:52 +11005847 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005848 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005849 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005850 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005851 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005852 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005853 long long min_offset_diff = 0;
5854 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005855
Andre Noll8c6ac862009-06-18 08:48:06 +10005856 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005857 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005858 " -- starting background reconstruction\n",
5859 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005860
5861 rdev_for_each(rdev, mddev) {
5862 long long diff;
5863 if (rdev->raid_disk < 0)
5864 continue;
5865 diff = (rdev->new_data_offset - rdev->data_offset);
5866 if (first) {
5867 min_offset_diff = diff;
5868 first = 0;
5869 } else if (mddev->reshape_backwards &&
5870 diff < min_offset_diff)
5871 min_offset_diff = diff;
5872 else if (!mddev->reshape_backwards &&
5873 diff > min_offset_diff)
5874 min_offset_diff = diff;
5875 }
5876
NeilBrownf6705572006-03-27 01:18:11 -08005877 if (mddev->reshape_position != MaxSector) {
5878 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005879 * Difficulties arise if the stripe we would write to
5880 * next is at or after the stripe we would read from next.
5881 * For a reshape that changes the number of devices, this
5882 * is only possible for a very short time, and mdadm makes
5883 * sure that time appears to have past before assembling
5884 * the array. So we fail if that time hasn't passed.
5885 * For a reshape that keeps the number of devices the same
5886 * mdadm must be monitoring the reshape can keeping the
5887 * critical areas read-only and backed up. It will start
5888 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005889 */
5890 sector_t here_new, here_old;
5891 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005892 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005893
NeilBrown88ce4932009-03-31 15:24:23 +11005894 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005895 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005896 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005897 mdname(mddev));
5898 return -EINVAL;
5899 }
NeilBrownf6705572006-03-27 01:18:11 -08005900 old_disks = mddev->raid_disks - mddev->delta_disks;
5901 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005902 * further up in new geometry must map after here in old
5903 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005904 */
5905 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005906 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005907 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005908 printk(KERN_ERR "md/raid:%s: reshape_position not "
5909 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005910 return -EINVAL;
5911 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005912 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005913 /* here_new is the stripe we will write to */
5914 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005915 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005916 (old_disks-max_degraded));
5917 /* here_old is the first stripe that we might need to read
5918 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005919 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005920 if ((here_new * mddev->new_chunk_sectors !=
5921 here_old * mddev->chunk_sectors)) {
5922 printk(KERN_ERR "md/raid:%s: reshape position is"
5923 " confused - aborting\n", mdname(mddev));
5924 return -EINVAL;
5925 }
NeilBrown67ac6012009-08-13 10:06:24 +10005926 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005927 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005928 * and taking constant backups.
5929 * mdadm always starts a situation like this in
5930 * readonly mode so it can take control before
5931 * allowing any writes. So just check for that.
5932 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005933 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5934 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5935 /* not really in-place - so OK */;
5936 else if (mddev->ro == 0) {
5937 printk(KERN_ERR "md/raid:%s: in-place reshape "
5938 "must be started in read-only mode "
5939 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005940 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005941 return -EINVAL;
5942 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005943 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005944 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005945 here_old * mddev->chunk_sectors)
5946 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005947 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005948 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005949 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5950 "auto-recovery - aborting.\n",
5951 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005952 return -EINVAL;
5953 }
NeilBrown0c55e022010-05-03 14:09:02 +10005954 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5955 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005956 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005957 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005958 BUG_ON(mddev->level != mddev->new_level);
5959 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005960 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005961 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005962 }
5963
NeilBrown245f46c2009-03-31 14:39:39 +11005964 if (mddev->private == NULL)
5965 conf = setup_conf(mddev);
5966 else
5967 conf = mddev->private;
5968
NeilBrown91adb562009-03-31 14:39:39 +11005969 if (IS_ERR(conf))
5970 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005971
NeilBrownb5254dd2012-05-21 09:27:01 +10005972 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005973 mddev->thread = conf->thread;
5974 conf->thread = NULL;
5975 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976
NeilBrown17045f52011-12-23 10:17:53 +11005977 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5978 i++) {
5979 rdev = conf->disks[i].rdev;
5980 if (!rdev && conf->disks[i].replacement) {
5981 /* The replacement is all we have yet */
5982 rdev = conf->disks[i].replacement;
5983 conf->disks[i].replacement = NULL;
5984 clear_bit(Replacement, &rdev->flags);
5985 conf->disks[i].rdev = rdev;
5986 }
5987 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005988 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005989 if (conf->disks[i].replacement &&
5990 conf->reshape_progress != MaxSector) {
5991 /* replacements and reshape simply do not mix. */
5992 printk(KERN_ERR "md: cannot handle concurrent "
5993 "replacement and reshape.\n");
5994 goto abort;
5995 }
NeilBrown2f115882010-06-17 17:41:03 +10005996 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005997 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005998 continue;
5999 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006000 /* This disc is not fully in-sync. However if it
6001 * just stored parity (beyond the recovery_offset),
6002 * when we don't need to be concerned about the
6003 * array being dirty.
6004 * When reshape goes 'backwards', we never have
6005 * partially completed devices, so we only need
6006 * to worry about reshape going forwards.
6007 */
6008 /* Hack because v0.91 doesn't store recovery_offset properly. */
6009 if (mddev->major_version == 0 &&
6010 mddev->minor_version > 90)
6011 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006012
NeilBrownc148ffd2009-11-13 17:47:00 +11006013 if (rdev->recovery_offset < reshape_offset) {
6014 /* We need to check old and new layout */
6015 if (!only_parity(rdev->raid_disk,
6016 conf->algorithm,
6017 conf->raid_disks,
6018 conf->max_degraded))
6019 continue;
6020 }
6021 if (!only_parity(rdev->raid_disk,
6022 conf->prev_algo,
6023 conf->previous_raid_disks,
6024 conf->max_degraded))
6025 continue;
6026 dirty_parity_disks++;
6027 }
NeilBrown91adb562009-03-31 14:39:39 +11006028
NeilBrown17045f52011-12-23 10:17:53 +11006029 /*
6030 * 0 for a fully functional array, 1 or 2 for a degraded array.
6031 */
NeilBrown908f4fb2011-12-23 10:17:50 +11006032 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
NeilBrown674806d2010-06-16 17:17:53 +10006034 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006035 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006037 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 goto abort;
6039 }
6040
NeilBrown91adb562009-03-31 14:39:39 +11006041 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006042 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006043 mddev->resync_max_sectors = mddev->dev_sectors;
6044
NeilBrownc148ffd2009-11-13 17:47:00 +11006045 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006047 if (mddev->ok_start_degraded)
6048 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006049 "md/raid:%s: starting dirty degraded array"
6050 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006051 mdname(mddev));
6052 else {
6053 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006054 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006055 mdname(mddev));
6056 goto abort;
6057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 }
6059
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006061 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6062 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006063 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6064 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 else
NeilBrown0c55e022010-05-03 14:09:02 +10006066 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6067 " out of %d devices, algorithm %d\n",
6068 mdname(mddev), conf->level,
6069 mddev->raid_disks - mddev->degraded,
6070 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072 print_raid5_conf(conf);
6073
NeilBrownfef9c612009-03-31 15:16:46 +11006074 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006075 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006076 atomic_set(&conf->reshape_stripes, 0);
6077 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6078 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6079 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6080 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6081 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006082 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006083 }
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
6086 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006087 if (mddev->to_remove == &raid5_attrs_group)
6088 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006089 else if (mddev->kobj.sd &&
6090 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006091 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006092 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006093 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006094 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6095
6096 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006097 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006098 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006099 /* read-ahead size must cover two whole stripes, which
6100 * is 2 * (datadisks) * chunksize where 'n' is the
6101 * number of raid devices
6102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6104 int stripe = data_disks *
6105 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6106 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6107 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006108
6109 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006110
6111 mddev->queue->backing_dev_info.congested_data = mddev;
6112 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10006113
6114 chunk_size = mddev->chunk_sectors << 9;
6115 blk_queue_io_min(mddev->queue, chunk_size);
6116 blk_queue_io_opt(mddev->queue, chunk_size *
6117 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07006118 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006119 /*
6120 * We can only discard a whole stripe. It doesn't make sense to
6121 * discard data disk but write parity disk
6122 */
6123 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11006124 /* Round up to power of 2, as discard handling
6125 * currently assumes that */
6126 while ((stripe-1) & stripe)
6127 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006128 mddev->queue->limits.discard_alignment = stripe;
6129 mddev->queue->limits.discard_granularity = stripe;
6130 /*
6131 * unaligned part of discard request will be ignored, so can't
6132 * guarantee discard_zerors_data
6133 */
6134 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10006135
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006136 blk_queue_max_write_same_sectors(mddev->queue, 0);
6137
NeilBrown05616be2012-05-21 09:27:00 +10006138 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006139 disk_stack_limits(mddev->gendisk, rdev->bdev,
6140 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10006141 disk_stack_limits(mddev->gendisk, rdev->bdev,
6142 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11006143 /*
6144 * discard_zeroes_data is required, otherwise data
6145 * could be lost. Consider a scenario: discard a stripe
6146 * (the stripe could be inconsistent if
6147 * discard_zeroes_data is 0); write one disk of the
6148 * stripe (the stripe could be inconsistent again
6149 * depending on which disks are used to calculate
6150 * parity); the disk is broken; The stripe data of this
6151 * disk is lost.
6152 */
6153 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6154 !bdev_get_queue(rdev->bdev)->
6155 limits.discard_zeroes_data)
6156 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10006157 }
Shaohua Li620125f2012-10-11 13:49:05 +11006158
6159 if (discard_supported &&
6160 mddev->queue->limits.max_discard_sectors >= stripe &&
6161 mddev->queue->limits.discard_granularity >= stripe)
6162 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6163 mddev->queue);
6164 else
6165 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6166 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 }
6168
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 return 0;
6170abort:
NeilBrown01f96c02011-09-21 15:30:20 +10006171 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11006172 print_raid5_conf(conf);
6173 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10006175 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 return -EIO;
6177}
6178
NeilBrownfd01b882011-10-11 16:47:53 +11006179static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180{
NeilBrownd1688a62011-10-11 16:49:52 +11006181 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
NeilBrown01f96c02011-09-21 15:30:20 +10006183 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10006184 if (mddev->queue)
6185 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10006186 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10006187 mddev->private = NULL;
6188 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 return 0;
6190}
6191
NeilBrownfd01b882011-10-11 16:47:53 +11006192static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193{
NeilBrownd1688a62011-10-11 16:49:52 +11006194 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 int i;
6196
Andre Noll9d8f0362009-06-18 08:45:01 +10006197 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
6198 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07006199 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 for (i = 0; i < conf->raid_disks; i++)
6201 seq_printf (seq, "%s",
6202 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08006203 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
NeilBrownd1688a62011-10-11 16:49:52 +11006207static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
6209 int i;
6210 struct disk_info *tmp;
6211
NeilBrown0c55e022010-05-03 14:09:02 +10006212 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 if (!conf) {
6214 printk("(conf==NULL)\n");
6215 return;
6216 }
NeilBrown0c55e022010-05-03 14:09:02 +10006217 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
6218 conf->raid_disks,
6219 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
6221 for (i = 0; i < conf->raid_disks; i++) {
6222 char b[BDEVNAME_SIZE];
6223 tmp = conf->disks + i;
6224 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10006225 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
6226 i, !test_bit(Faulty, &tmp->rdev->flags),
6227 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 }
6229}
6230
NeilBrownfd01b882011-10-11 16:47:53 +11006231static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232{
6233 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11006234 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10006236 int count = 0;
6237 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
6239 for (i = 0; i < conf->raid_disks; i++) {
6240 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11006241 if (tmp->replacement
6242 && tmp->replacement->recovery_offset == MaxSector
6243 && !test_bit(Faulty, &tmp->replacement->flags)
6244 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
6245 /* Replacement has just become active. */
6246 if (!tmp->rdev
6247 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
6248 count++;
6249 if (tmp->rdev) {
6250 /* Replaced device not technically faulty,
6251 * but we need to be sure it gets removed
6252 * and never re-added.
6253 */
6254 set_bit(Faulty, &tmp->rdev->flags);
6255 sysfs_notify_dirent_safe(
6256 tmp->rdev->sysfs_state);
6257 }
6258 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
6259 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10006260 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08006261 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07006262 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10006263 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11006264 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 }
6266 }
NeilBrown6b965622010-08-18 11:56:59 +10006267 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006268 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006269 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10006271 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272}
6273
NeilBrownb8321b62011-12-23 10:17:51 +11006274static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275{
NeilBrownd1688a62011-10-11 16:49:52 +11006276 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11006278 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11006279 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 struct disk_info *p = conf->disks + number;
6281
6282 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11006283 if (rdev == p->rdev)
6284 rdevp = &p->rdev;
6285 else if (rdev == p->replacement)
6286 rdevp = &p->replacement;
6287 else
6288 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11006289
NeilBrown657e3e42011-12-23 10:17:52 +11006290 if (number >= conf->raid_disks &&
6291 conf->reshape_progress == MaxSector)
6292 clear_bit(In_sync, &rdev->flags);
6293
6294 if (test_bit(In_sync, &rdev->flags) ||
6295 atomic_read(&rdev->nr_pending)) {
6296 err = -EBUSY;
6297 goto abort;
6298 }
6299 /* Only remove non-faulty devices if recovery
6300 * isn't possible.
6301 */
6302 if (!test_bit(Faulty, &rdev->flags) &&
6303 mddev->recovery_disabled != conf->recovery_disabled &&
6304 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11006305 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11006306 number < conf->raid_disks) {
6307 err = -EBUSY;
6308 goto abort;
6309 }
6310 *rdevp = NULL;
6311 synchronize_rcu();
6312 if (atomic_read(&rdev->nr_pending)) {
6313 /* lost the race, try later */
6314 err = -EBUSY;
6315 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11006316 } else if (p->replacement) {
6317 /* We must have just cleared 'rdev' */
6318 p->rdev = p->replacement;
6319 clear_bit(Replacement, &p->replacement->flags);
6320 smp_mb(); /* Make sure other CPUs may see both as identical
6321 * but will never see neither - if they are careful
6322 */
6323 p->replacement = NULL;
6324 clear_bit(WantReplacement, &rdev->flags);
6325 } else
6326 /* We might have just removed the Replacement as faulty-
6327 * clear the bit just in case
6328 */
6329 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330abort:
6331
6332 print_raid5_conf(conf);
6333 return err;
6334}
6335
NeilBrownfd01b882011-10-11 16:47:53 +11006336static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337{
NeilBrownd1688a62011-10-11 16:49:52 +11006338 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10006339 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 int disk;
6341 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10006342 int first = 0;
6343 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
NeilBrown7f0da592011-07-28 11:39:22 +10006345 if (mddev->recovery_disabled == conf->recovery_disabled)
6346 return -EBUSY;
6347
NeilBrowndc10c642012-03-19 12:46:37 +11006348 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10006350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
Neil Brown6c2fce22008-06-28 08:31:31 +10006352 if (rdev->raid_disk >= 0)
6353 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
6355 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07006356 * find the disk ... but prefer rdev->saved_raid_disk
6357 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 */
NeilBrown16a53ec2006-06-26 00:27:38 -07006359 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10006360 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006361 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10006362 first = rdev->saved_raid_disk;
6363
6364 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11006365 p = conf->disks + disk;
6366 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08006367 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10006369 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07006370 if (rdev->saved_raid_disk != disk)
6371 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08006372 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10006373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006375 }
6376 for (disk = first; disk <= last; disk++) {
6377 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11006378 if (test_bit(WantReplacement, &p->rdev->flags) &&
6379 p->replacement == NULL) {
6380 clear_bit(In_sync, &rdev->flags);
6381 set_bit(Replacement, &rdev->flags);
6382 rdev->raid_disk = disk;
6383 err = 0;
6384 conf->fullsync = 1;
6385 rcu_assign_pointer(p->replacement, rdev);
6386 break;
6387 }
6388 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10006389out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10006391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392}
6393
NeilBrownfd01b882011-10-11 16:47:53 +11006394static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395{
6396 /* no resync is happening, and there is enough space
6397 * on all devices, so we can resize.
6398 * We need to make sure resync covers any new space.
6399 * If the array is shrinking we should possibly wait until
6400 * any io in the removed space completes, but it hardly seems
6401 * worth it.
6402 */
NeilBrowna4a61252012-05-22 13:55:27 +10006403 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10006404 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10006405 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
6406 if (mddev->external_size &&
6407 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11006408 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10006409 if (mddev->bitmap) {
6410 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
6411 if (ret)
6412 return ret;
6413 }
6414 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10006415 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006416 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10006417 if (sectors > mddev->dev_sectors &&
6418 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006419 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6421 }
Andre Noll58c0fed2009-03-31 14:33:13 +11006422 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07006423 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 return 0;
6425}
6426
NeilBrownfd01b882011-10-11 16:47:53 +11006427static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10006428{
6429 /* Can only proceed if there are plenty of stripe_heads.
6430 * We need a minimum of one full stripe,, and for sensible progress
6431 * it is best to have about 4 times that.
6432 * If we require 4 times, then the default 256 4K stripe_heads will
6433 * allow for chunk sizes up to 256K, which is probably OK.
6434 * If the chunk size is greater, user-space should request more
6435 * stripe_heads first.
6436 */
NeilBrownd1688a62011-10-11 16:49:52 +11006437 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10006438 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
6439 > conf->max_nr_stripes ||
6440 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
6441 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10006442 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
6443 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10006444 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
6445 / STRIPE_SIZE)*4);
6446 return 0;
6447 }
6448 return 1;
6449}
6450
NeilBrownfd01b882011-10-11 16:47:53 +11006451static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08006452{
NeilBrownd1688a62011-10-11 16:49:52 +11006453 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08006454
NeilBrown88ce4932009-03-31 15:24:23 +11006455 if (mddev->delta_disks == 0 &&
6456 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10006457 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10006458 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10006459 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11006460 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10006461 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11006462 /* We might be able to shrink, but the devices must
6463 * be made bigger first.
6464 * For raid6, 4 is the minimum size.
6465 * Otherwise 2 is the minimum
6466 */
6467 int min = 2;
6468 if (mddev->level == 6)
6469 min = 4;
6470 if (mddev->raid_disks + mddev->delta_disks < min)
6471 return -EINVAL;
6472 }
NeilBrown29269552006-03-27 01:18:10 -08006473
NeilBrown01ee22b2009-06-18 08:47:20 +10006474 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08006475 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08006476
NeilBrowne56108d62012-10-11 14:24:13 +11006477 return resize_stripes(conf, (conf->previous_raid_disks
6478 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08006479}
6480
NeilBrownfd01b882011-10-11 16:47:53 +11006481static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08006482{
NeilBrownd1688a62011-10-11 16:49:52 +11006483 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11006484 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08006485 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07006486 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08006487
NeilBrownf4168852007-02-28 20:11:53 -08006488 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08006489 return -EBUSY;
6490
NeilBrown01ee22b2009-06-18 08:47:20 +10006491 if (!check_stripe_cache(mddev))
6492 return -ENOSPC;
6493
NeilBrown30b67642012-05-22 13:55:28 +10006494 if (has_failed(conf))
6495 return -EINVAL;
6496
NeilBrownc6563a82012-05-21 09:27:00 +10006497 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11006498 if (!test_bit(In_sync, &rdev->flags)
6499 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006500 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006501 }
NeilBrown63c70c42006-03-27 01:18:13 -08006502
NeilBrownf4168852007-02-28 20:11:53 -08006503 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006504 /* Not enough devices even to make a degraded array
6505 * of that size
6506 */
6507 return -EINVAL;
6508
NeilBrownec32a2b2009-03-31 15:17:38 +11006509 /* Refuse to reduce size of the array. Any reductions in
6510 * array size must be through explicit setting of array_size
6511 * attribute.
6512 */
6513 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6514 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006515 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006516 "before number of disks\n", mdname(mddev));
6517 return -EINVAL;
6518 }
6519
NeilBrownf6705572006-03-27 01:18:11 -08006520 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006521 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006522 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006523 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006524 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006525 conf->prev_chunk_sectors = conf->chunk_sectors;
6526 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006527 conf->prev_algo = conf->algorithm;
6528 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006529 conf->generation++;
6530 /* Code that selects data_offset needs to see the generation update
6531 * if reshape_progress has been set - so a memory barrier needed.
6532 */
6533 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006534 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006535 conf->reshape_progress = raid5_size(mddev, 0, 0);
6536 else
6537 conf->reshape_progress = 0;
6538 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10006539 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006540 spin_unlock_irq(&conf->device_lock);
6541
NeilBrown4d77e3b2013-08-27 15:57:47 +10006542 /* Now make sure any requests that proceeded on the assumption
6543 * the reshape wasn't running - like Discard or Read - have
6544 * completed.
6545 */
6546 mddev_suspend(mddev);
6547 mddev_resume(mddev);
6548
NeilBrown29269552006-03-27 01:18:10 -08006549 /* Add some new drives, as many as will fit.
6550 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006551 * Don't add devices if we are reducing the number of
6552 * devices in the array. This is because it is not possible
6553 * to correctly record the "partially reconstructed" state of
6554 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006555 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006556 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006557 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006558 if (rdev->raid_disk < 0 &&
6559 !test_bit(Faulty, &rdev->flags)) {
6560 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006561 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006562 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006563 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006564 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006565 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006566
6567 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006568 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006569 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006570 } else if (rdev->raid_disk >= conf->previous_raid_disks
6571 && !test_bit(Faulty, &rdev->flags)) {
6572 /* This is a spare that was manually added */
6573 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006574 }
NeilBrown29269552006-03-27 01:18:10 -08006575
NeilBrown87a8dec2011-01-31 11:57:43 +11006576 /* When a reshape changes the number of devices,
6577 * ->degraded is measured against the larger of the
6578 * pre and post number of devices.
6579 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006580 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006581 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006582 spin_unlock_irqrestore(&conf->device_lock, flags);
6583 }
NeilBrown63c70c42006-03-27 01:18:13 -08006584 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006585 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006586 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006587
NeilBrown29269552006-03-27 01:18:10 -08006588 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6589 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6590 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6591 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6592 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006593 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006594 if (!mddev->sync_thread) {
6595 mddev->recovery = 0;
6596 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11006597 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006598 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11006599 mddev->new_chunk_sectors =
6600 conf->chunk_sectors = conf->prev_chunk_sectors;
6601 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10006602 rdev_for_each(rdev, mddev)
6603 rdev->new_data_offset = rdev->data_offset;
6604 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11006605 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11006606 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006607 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11006608 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08006609 spin_unlock_irq(&conf->device_lock);
6610 return -EAGAIN;
6611 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006612 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006613 md_wakeup_thread(mddev->sync_thread);
6614 md_new_event(mddev);
6615 return 0;
6616}
NeilBrown29269552006-03-27 01:18:10 -08006617
NeilBrownec32a2b2009-03-31 15:17:38 +11006618/* This is called from the reshape thread and should make any
6619 * changes needed in 'conf'
6620 */
NeilBrownd1688a62011-10-11 16:49:52 +11006621static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006622{
NeilBrown29269552006-03-27 01:18:10 -08006623
NeilBrownf6705572006-03-27 01:18:11 -08006624 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006625 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006626
NeilBrownf6705572006-03-27 01:18:11 -08006627 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006628 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006629 rdev_for_each(rdev, conf->mddev)
6630 rdev->data_offset = rdev->new_data_offset;
6631 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006632 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006633 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006634 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006635
6636 /* read-ahead size must cover two whole stripes, which is
6637 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6638 */
NeilBrown4a5add42010-06-01 19:37:28 +10006639 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006640 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006641 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006642 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006643 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6644 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6645 }
NeilBrown29269552006-03-27 01:18:10 -08006646 }
NeilBrown29269552006-03-27 01:18:10 -08006647}
6648
NeilBrownec32a2b2009-03-31 15:17:38 +11006649/* This is called from the raid5d thread with mddev_lock held.
6650 * It makes config changes to the device.
6651 */
NeilBrownfd01b882011-10-11 16:47:53 +11006652static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006653{
NeilBrownd1688a62011-10-11 16:49:52 +11006654 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006655
6656 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6657
NeilBrownec32a2b2009-03-31 15:17:38 +11006658 if (mddev->delta_disks > 0) {
6659 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6660 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006661 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006662 } else {
6663 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006664 spin_lock_irq(&conf->device_lock);
6665 mddev->degraded = calc_degraded(conf);
6666 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006667 for (d = conf->raid_disks ;
6668 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006669 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006670 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006671 if (rdev)
6672 clear_bit(In_sync, &rdev->flags);
6673 rdev = conf->disks[d].replacement;
6674 if (rdev)
6675 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006676 }
NeilBrowncea9c222009-03-31 15:15:05 +11006677 }
NeilBrown88ce4932009-03-31 15:24:23 +11006678 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006679 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006680 mddev->reshape_position = MaxSector;
6681 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006682 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006683 }
6684}
6685
NeilBrownfd01b882011-10-11 16:47:53 +11006686static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006687{
NeilBrownd1688a62011-10-11 16:49:52 +11006688 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006689
6690 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006691 case 2: /* resume for a suspend */
6692 wake_up(&conf->wait_for_overlap);
6693 break;
6694
NeilBrown72626682005-09-09 16:23:54 -07006695 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006696 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006697 /* '2' tells resync/reshape to pause so that all
6698 * active stripes can drain
6699 */
6700 conf->quiesce = 2;
Shaohua Li566c09c2013-11-14 15:16:17 +11006701 wait_event_cmd(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006702 atomic_read(&conf->active_stripes) == 0 &&
6703 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11006704 unlock_all_device_hash_locks_irq(conf),
6705 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10006706 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11006707 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10006708 /* allow reshape to continue */
6709 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006710 break;
6711
6712 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11006713 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006714 conf->quiesce = 0;
6715 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006716 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11006717 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07006718 break;
6719 }
NeilBrown72626682005-09-09 16:23:54 -07006720}
NeilBrownb15c2e52006-01-06 00:20:16 -08006721
NeilBrownd562b0c2009-03-31 14:39:39 +11006722
NeilBrownfd01b882011-10-11 16:47:53 +11006723static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006724{
NeilBrowne373ab12011-10-11 16:48:59 +11006725 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006726 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006727
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006728 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006729 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006730 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6731 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006732 return ERR_PTR(-EINVAL);
6733 }
6734
NeilBrowne373ab12011-10-11 16:48:59 +11006735 sectors = raid0_conf->strip_zone[0].zone_end;
6736 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006737 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006738 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006739 mddev->new_layout = ALGORITHM_PARITY_N;
6740 mddev->new_chunk_sectors = mddev->chunk_sectors;
6741 mddev->raid_disks += 1;
6742 mddev->delta_disks = 1;
6743 /* make sure it will be not marked as dirty */
6744 mddev->recovery_cp = MaxSector;
6745
6746 return setup_conf(mddev);
6747}
6748
6749
NeilBrownfd01b882011-10-11 16:47:53 +11006750static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006751{
6752 int chunksect;
6753
6754 if (mddev->raid_disks != 2 ||
6755 mddev->degraded > 1)
6756 return ERR_PTR(-EINVAL);
6757
6758 /* Should check if there are write-behind devices? */
6759
6760 chunksect = 64*2; /* 64K by default */
6761
6762 /* The array must be an exact multiple of chunksize */
6763 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6764 chunksect >>= 1;
6765
6766 if ((chunksect<<9) < STRIPE_SIZE)
6767 /* array size does not allow a suitable chunk size */
6768 return ERR_PTR(-EINVAL);
6769
6770 mddev->new_level = 5;
6771 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006772 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006773
6774 return setup_conf(mddev);
6775}
6776
NeilBrownfd01b882011-10-11 16:47:53 +11006777static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006778{
6779 int new_layout;
6780
6781 switch (mddev->layout) {
6782 case ALGORITHM_LEFT_ASYMMETRIC_6:
6783 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6784 break;
6785 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6786 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6787 break;
6788 case ALGORITHM_LEFT_SYMMETRIC_6:
6789 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6790 break;
6791 case ALGORITHM_RIGHT_SYMMETRIC_6:
6792 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6793 break;
6794 case ALGORITHM_PARITY_0_6:
6795 new_layout = ALGORITHM_PARITY_0;
6796 break;
6797 case ALGORITHM_PARITY_N:
6798 new_layout = ALGORITHM_PARITY_N;
6799 break;
6800 default:
6801 return ERR_PTR(-EINVAL);
6802 }
6803 mddev->new_level = 5;
6804 mddev->new_layout = new_layout;
6805 mddev->delta_disks = -1;
6806 mddev->raid_disks -= 1;
6807 return setup_conf(mddev);
6808}
6809
NeilBrownd562b0c2009-03-31 14:39:39 +11006810
NeilBrownfd01b882011-10-11 16:47:53 +11006811static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006812{
NeilBrown88ce4932009-03-31 15:24:23 +11006813 /* For a 2-drive array, the layout and chunk size can be changed
6814 * immediately as not restriping is needed.
6815 * For larger arrays we record the new value - after validation
6816 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006817 */
NeilBrownd1688a62011-10-11 16:49:52 +11006818 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006819 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006820
NeilBrown597a7112009-06-18 08:47:42 +10006821 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006822 return -EINVAL;
6823 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006824 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006825 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006826 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006827 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006828 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006829 /* not factor of array size */
6830 return -EINVAL;
6831 }
6832
6833 /* They look valid */
6834
NeilBrown88ce4932009-03-31 15:24:23 +11006835 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006836 /* can make the change immediately */
6837 if (mddev->new_layout >= 0) {
6838 conf->algorithm = mddev->new_layout;
6839 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006840 }
6841 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006842 conf->chunk_sectors = new_chunk ;
6843 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006844 }
6845 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6846 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006847 }
NeilBrown50ac1682009-06-18 08:47:55 +10006848 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006849}
6850
NeilBrownfd01b882011-10-11 16:47:53 +11006851static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006852{
NeilBrown597a7112009-06-18 08:47:42 +10006853 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006854
NeilBrown597a7112009-06-18 08:47:42 +10006855 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006856 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006857 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006858 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006859 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006860 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006861 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006862 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006863 /* not factor of array size */
6864 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006865 }
NeilBrown88ce4932009-03-31 15:24:23 +11006866
6867 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006868 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006869}
6870
NeilBrownfd01b882011-10-11 16:47:53 +11006871static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006872{
6873 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006874 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006875 * raid1 - if there are two drives. We need to know the chunk size
6876 * raid4 - trivial - just use a raid4 layout.
6877 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006878 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006879 if (mddev->level == 0)
6880 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006881 if (mddev->level == 1)
6882 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006883 if (mddev->level == 4) {
6884 mddev->new_layout = ALGORITHM_PARITY_N;
6885 mddev->new_level = 5;
6886 return setup_conf(mddev);
6887 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006888 if (mddev->level == 6)
6889 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006890
6891 return ERR_PTR(-EINVAL);
6892}
6893
NeilBrownfd01b882011-10-11 16:47:53 +11006894static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006895{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006896 /* raid4 can take over:
6897 * raid0 - if there is only one strip zone
6898 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006899 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006900 if (mddev->level == 0)
6901 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006902 if (mddev->level == 5 &&
6903 mddev->layout == ALGORITHM_PARITY_N) {
6904 mddev->new_layout = 0;
6905 mddev->new_level = 4;
6906 return setup_conf(mddev);
6907 }
6908 return ERR_PTR(-EINVAL);
6909}
NeilBrownd562b0c2009-03-31 14:39:39 +11006910
NeilBrown84fc4b52011-10-11 16:49:58 +11006911static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006912
NeilBrownfd01b882011-10-11 16:47:53 +11006913static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006914{
6915 /* Currently can only take over a raid5. We map the
6916 * personality to an equivalent raid6 personality
6917 * with the Q block at the end.
6918 */
6919 int new_layout;
6920
6921 if (mddev->pers != &raid5_personality)
6922 return ERR_PTR(-EINVAL);
6923 if (mddev->degraded > 1)
6924 return ERR_PTR(-EINVAL);
6925 if (mddev->raid_disks > 253)
6926 return ERR_PTR(-EINVAL);
6927 if (mddev->raid_disks < 3)
6928 return ERR_PTR(-EINVAL);
6929
6930 switch (mddev->layout) {
6931 case ALGORITHM_LEFT_ASYMMETRIC:
6932 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6933 break;
6934 case ALGORITHM_RIGHT_ASYMMETRIC:
6935 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6936 break;
6937 case ALGORITHM_LEFT_SYMMETRIC:
6938 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6939 break;
6940 case ALGORITHM_RIGHT_SYMMETRIC:
6941 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6942 break;
6943 case ALGORITHM_PARITY_0:
6944 new_layout = ALGORITHM_PARITY_0_6;
6945 break;
6946 case ALGORITHM_PARITY_N:
6947 new_layout = ALGORITHM_PARITY_N;
6948 break;
6949 default:
6950 return ERR_PTR(-EINVAL);
6951 }
6952 mddev->new_level = 6;
6953 mddev->new_layout = new_layout;
6954 mddev->delta_disks = 1;
6955 mddev->raid_disks += 1;
6956 return setup_conf(mddev);
6957}
6958
6959
NeilBrown84fc4b52011-10-11 16:49:58 +11006960static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006961{
6962 .name = "raid6",
6963 .level = 6,
6964 .owner = THIS_MODULE,
6965 .make_request = make_request,
6966 .run = run,
6967 .stop = stop,
6968 .status = status,
6969 .error_handler = error,
6970 .hot_add_disk = raid5_add_disk,
6971 .hot_remove_disk= raid5_remove_disk,
6972 .spare_active = raid5_spare_active,
6973 .sync_request = sync_request,
6974 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006975 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006976 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006977 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006978 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006979 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006980 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006981};
NeilBrown84fc4b52011-10-11 16:49:58 +11006982static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983{
6984 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006985 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 .owner = THIS_MODULE,
6987 .make_request = make_request,
6988 .run = run,
6989 .stop = stop,
6990 .status = status,
6991 .error_handler = error,
6992 .hot_add_disk = raid5_add_disk,
6993 .hot_remove_disk= raid5_remove_disk,
6994 .spare_active = raid5_spare_active,
6995 .sync_request = sync_request,
6996 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006997 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006998 .check_reshape = raid5_check_reshape,
6999 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007000 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07007001 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11007002 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003};
7004
NeilBrown84fc4b52011-10-11 16:49:58 +11007005static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006{
NeilBrown2604b702006-01-06 00:20:36 -08007007 .name = "raid4",
7008 .level = 4,
7009 .owner = THIS_MODULE,
7010 .make_request = make_request,
7011 .run = run,
7012 .stop = stop,
7013 .status = status,
7014 .error_handler = error,
7015 .hot_add_disk = raid5_add_disk,
7016 .hot_remove_disk= raid5_remove_disk,
7017 .spare_active = raid5_spare_active,
7018 .sync_request = sync_request,
7019 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007020 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08007021 .check_reshape = raid5_check_reshape,
7022 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007023 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08007024 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11007025 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08007026};
7027
7028static int __init raid5_init(void)
7029{
Shaohua Li851c30c2013-08-28 14:30:16 +08007030 raid5_wq = alloc_workqueue("raid5wq",
7031 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7032 if (!raid5_wq)
7033 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07007034 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007035 register_md_personality(&raid5_personality);
7036 register_md_personality(&raid4_personality);
7037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038}
7039
NeilBrown2604b702006-01-06 00:20:36 -08007040static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041{
NeilBrown16a53ec2006-06-26 00:27:38 -07007042 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007043 unregister_md_personality(&raid5_personality);
7044 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08007045 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046}
7047
7048module_init(raid5_init);
7049module_exit(raid5_exit);
7050MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007051MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08007053MODULE_ALIAS("md-raid5");
7054MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08007055MODULE_ALIAS("md-level-5");
7056MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07007057MODULE_ALIAS("md-personality-8"); /* RAID6 */
7058MODULE_ALIAS("md-raid6");
7059MODULE_ALIAS("md-level-6");
7060
7061/* This used to be two separate modules, they were: */
7062MODULE_ALIAS("raid5");
7063MODULE_ALIAS("raid6");